Commit 90a7223f2ac1e6bccc7a41cad051ac1af5703d5d
1 parent
fa77b6dc
Exists in
canvas_reeng
Added a method to calc text size rendered with a given font
Showing
1 changed file
with
24 additions
and
0 deletions
Show diff stats
invesalius/data/viewer_slice.py
... | ... | @@ -258,6 +258,30 @@ class CanvasRendererCTX: |
258 | 258 | self._cv_image.Modified() |
259 | 259 | self.modified = False |
260 | 260 | |
261 | + def calc_text_size(self, text, font=None): | |
262 | + """ | |
263 | + Given an unicode text and a font returns the width and height of the | |
264 | + rendered text in pixels. | |
265 | + | |
266 | + Params: | |
267 | + text: An unicode text. | |
268 | + font: An wxFont. | |
269 | + | |
270 | + Returns: | |
271 | + A tuple with width and height values in pixels | |
272 | + """ | |
273 | + if self.gc is None: | |
274 | + return None | |
275 | + gc = self.gc | |
276 | + | |
277 | + if font is None: | |
278 | + font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) | |
279 | + | |
280 | + _font = gc.CreateFont(font) | |
281 | + gc.SetFont(_font) | |
282 | + w, h = gc.GetTextExtent(text) | |
283 | + return w, h | |
284 | + | |
261 | 285 | def draw_line(self, pos0, pos1, arrow_start=False, arrow_end=False, colour=(255, 0, 0, 128), width=2, style=wx.SOLID): |
262 | 286 | """ |
263 | 287 | Draw a line from pos0 to pos1 | ... | ... |