| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9001323 commit c624aec
13 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2059,8 +2059,16 @@ def get_width_height(self, *, physical=False): | |||
| 2059 | 2059 | width, height : int | |
| 2060 | 2060 | The size of the figure, in points or pixels, depending on the | |
| 2061 | 2061 | backend. | |
| 2062 | + | ||
| 2063 | + Notes | ||
| 2064 | + ----- | ||
| 2065 | + This method normally truncates the height/width to remove any fractional pixel. | ||
| 2066 | + However, if the height/width is extremely close to the integer pixel (within | ||
| 2067 | + 1e-8 pixel), the height/width is instead rounded up to account for | ||
| 2068 | + floating-point precision effects. | ||
| 2062 | 2069 | """ | |
| 2063 | - return tuple(int(size / (1 if physical else self.device_pixel_ratio)) | ||
| 2070 | + # The tolerance of 1e-8 covers a floating-point tick for even 100,000 pixels | ||
| 2071 | + return tuple(int(size / (1 if physical else self.device_pixel_ratio) + 1e-8) | ||
| 2064 | 2072 | for size in self.figure.bbox.max) | |
| 2065 | 2073 | ||
| 2066 | 2074 | @classmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -275,7 +275,7 @@ def set_message(self, s): | |||
| 275 | 275 | self.message.set_markup(f'<small>{escaped}</small>') | |
| 276 | 276 | ||
| 277 | 277 | def draw_rubberband(self, event, x0, y0, x1, y1): | |
| 278 | - height = self.canvas.figure.bbox.height | ||
| 278 | + height = self.canvas.get_width_height(physical=True)[1] | ||
| 279 | 279 | y1 = height - y1 | |
| 280 | 280 | y0 = height - y0 | |
| 281 | 281 | rect = [int(val) for val in (x0, y0, x1 - x0, y1 - y0)] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -327,9 +327,10 @@ def get_tk_widget(self): | |||
| 327 | 327 | def _event_mpl_coords(self, event): | |
| 328 | 328 | # calling canvasx/canvasy allows taking scrollbars into account (i.e. | |
| 329 | 329 | # the top of the widget may have been scrolled out of view). | |
| 330 | + height = self.get_width_height(physical=True)[1] | ||
| 330 | 331 | return (self._tkcanvas.canvasx(event.x), | |
| 331 | 332 | # flipy so y=0 is bottom of canvas | |
| 332 | - self.figure.bbox.height - self._tkcanvas.canvasy(event.y)) | ||
| 333 | + height - self._tkcanvas.canvasy(event.y)) | ||
| 333 | 334 | ||
| 334 | 335 | def motion_notify_event(self, event): | |
| 335 | 336 | MouseEvent("motion_notify_event", self, | |
@@ -389,7 +390,7 @@ def scroll_event_windows(self, event): | |||
| 389 | 390 | if w != self._tkcanvas: | |
| 390 | 391 | return | |
| 391 | 392 | x = self._tkcanvas.canvasx(event.x_root - w.winfo_rootx()) | |
| 392 | - y = (self.figure.bbox.height | ||
| 393 | + y = (self.get_width_height(physical=True)[1] | ||
| 393 | 394 | - self._tkcanvas.canvasy(event.y_root - w.winfo_rooty())) | |
| 394 | 395 | step = event.delta / 120 | |
| 395 | 396 | MouseEvent("scroll_event", self, | |
@@ -676,7 +677,7 @@ def __init__(self, canvas, window=None, *, pack_toolbar=True): | |||
| 676 | 677 | if window is None: | |
| 677 | 678 | window = canvas.get_tk_widget().master | |
| 678 | 679 | tk.Frame.__init__(self, master=window, borderwidth=2, | |
| 679 | - width=int(canvas.figure.bbox.width), height=50) | ||
| 680 | + width=canvas.get_width_height()[0], height=50) | ||
| 680 | 681 | # Avoid message_label expanding the toolbar size, and in turn expanding the | |
| 681 | 682 | # canvas size. | |
| 682 | 683 | # Without pack_propagate(False), when the user defines a small figure size | |
@@ -774,7 +775,7 @@ def draw_rubberband(self, event, x0, y0, x1, y1): | |||
| 774 | 775 | self.canvas._tkcanvas.delete(self.canvas._rubberband_rect_white) | |
| 775 | 776 | if self.canvas._rubberband_rect_black: | |
| 776 | 777 | self.canvas._tkcanvas.delete(self.canvas._rubberband_rect_black) | |
| 777 | - height = self.canvas.figure.bbox.height | ||
| 778 | + height = self.canvas.get_width_height(physical=True)[1] | ||
| 778 | 779 | y0 = height - y0 | |
| 779 | 780 | y1 = height - y1 | |
| 780 | 781 | self.canvas._rubberband_rect_black = ( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -441,7 +441,7 @@ def draw(self): | |||
| 441 | 441 | super().draw() | |
| 442 | 442 | ||
| 443 | 443 | def get_renderer(self): | |
| 444 | - w, h = self.figure.bbox.size | ||
| 444 | + w, h = self.get_width_height(physical=True) | ||
| 445 | 445 | key = w, h, self.figure.dpi | |
| 446 | 446 | reuse_renderer = (self._lastKey == key) | |
| 447 | 447 | if not reuse_renderer: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,7 +119,7 @@ def _mpl_coords(self, event=None): | |||
| 119 | 119 | x, y = event.x, event.y | |
| 120 | 120 | x = x * self.device_pixel_ratio | |
| 121 | 121 | # flip y so y=0 is bottom of canvas | |
| 122 | - y = self.figure.bbox.height - y * self.device_pixel_ratio | ||
| 122 | + y = self.get_width_height(physical=True)[1] - y * self.device_pixel_ratio | ||
| 123 | 123 | return x, y | |
| 124 | 124 | ||
| 125 | 125 | def scroll_event(self, widget, event): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,7 +117,7 @@ def _mpl_coords(self, xy=None): | |||
| 117 | 117 | x, y = xy | |
| 118 | 118 | x = x * self.device_pixel_ratio | |
| 119 | 119 | # flip y so y=0 is bottom of canvas | |
| 120 | - y = self.figure.bbox.height - y * self.device_pixel_ratio | ||
| 120 | + y = self.get_width_height(physical=True)[1] - y * self.device_pixel_ratio | ||
| 121 | 121 | return x, y | |
| 122 | 122 | ||
| 123 | 123 | def scroll_event(self, controller, dx, dy): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -302,7 +302,7 @@ def mouseEventCoords(self, pos=None): | |||
| 302 | 302 | # (otherwise, it's already a QPoint) | |
| 303 | 303 | x = pos.x() | |
| 304 | 304 | # flip y so y=0 is bottom of canvas | |
| 305 | - y = self.figure.bbox.height / self.device_pixel_ratio - pos.y() | ||
| 305 | + y = self.get_width_height()[1] - pos.y() | ||
| 306 | 306 | return x * self.device_pixel_ratio, y * self.device_pixel_ratio | |
| 307 | 307 | ||
| 308 | 308 | def enterEvent(self, event): | |
@@ -915,7 +915,7 @@ def set_message(self, s): | |||
| 915 | 915 | self.locLabel.setText(s) | |
| 916 | 916 | ||
| 917 | 917 | def draw_rubberband(self, event, x0, y0, x1, y1): | |
| 918 | - height = self.canvas.figure.bbox.height | ||
| 918 | + height = self.canvas.get_width_height(physical=True)[1] | ||
| 919 | 919 | y1 = height - y1 | |
| 920 | 920 | y0 = height - y0 | |
| 921 | 921 | rect = [int(val) for val in (x0, y0, x1 - x0, y1 - y0)] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,8 +9,7 @@ | |||
| 9 | 9 | ||
| 10 | 10 | class FigureCanvasTkCairo(FigureCanvasCairo, FigureCanvasTk): | |
| 11 | 11 | def draw(self): | |
| 12 | - width = int(self.figure.bbox.width) | ||
| 13 | - height = int(self.figure.bbox.height) | ||
| 12 | + width, height = self.get_width_height(physical=True) | ||
| 14 | 13 | surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) | |
| 15 | 14 | self._renderer.set_context(cairo.Context(surface)) | |
| 16 | 15 | self._renderer.dpi = self.figure.dpi | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -492,7 +492,7 @@ def add_web_socket(self, web_socket): | |||
| 492 | 492 | assert hasattr(web_socket, 'send_binary') | |
| 493 | 493 | assert hasattr(web_socket, 'send_json') | |
| 494 | 494 | self.web_sockets.add(web_socket) | |
| 495 | - self.resize(*self.canvas.figure.bbox.size) | ||
| 495 | + self.resize(*self.canvas.get_width_height(physical=True)) | ||
| 496 | 496 | self._send_event('refresh') | |
| 497 | 497 | ||
| 498 | 498 | def remove_web_socket(self, web_socket): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -746,9 +746,9 @@ def _mpl_coords(self, pos=None): | |||
| 746 | 746 | # flip y so y=0 is bottom of canvas | |
| 747 | 747 | if not wx.Platform == '__WXMSW__': | |
| 748 | 748 | scale = self.GetDPIScaleFactor() | |
| 749 | - return x*scale, self.figure.bbox.height - y*scale | ||
| 749 | + return x*scale, self.get_width_height(physical=True)[1] - y*scale | ||
| 750 | 750 | else: | |
| 751 | - return x, self.figure.bbox.height - y | ||
| 751 | + return x, self.get_width_height(physical=True)[1] - y | ||
| 752 | 752 | ||
| 753 | 753 | def _on_key_down(self, event): | |
| 754 | 754 | """Capture key press.""" | |
@@ -1169,7 +1169,7 @@ def save_figure(self, *args): | |||
| 1169 | 1169 | dialog.Destroy() | |
| 1170 | 1170 | ||
| 1171 | 1171 | def draw_rubberband(self, event, x0, y0, x1, y1): | |
| 1172 | - height = self.canvas.figure.bbox.height | ||
| 1172 | + height = self.canvas.get_width_height(physical=True)[1] | ||
| 1173 | 1173 | sf = 1 if wx.Platform == '__WXMSW__' else self.canvas.GetDPIScaleFactor() | |
| 1174 | 1174 | self.canvas._rubberband_rect = (x0/sf, (height - y0)/sf, | |
| 1175 | 1175 | x1/sf, (height - y1)/sf) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments