From 90a7223f2ac1e6bccc7a41cad051ac1af5703d5d Mon Sep 17 00:00:00 2001 From: Thiago Franco de Moraes Date: Wed, 3 Aug 2016 15:10:27 -0300 Subject: [PATCH] Added a method to calc text size rendered with a given font --- invesalius/data/viewer_slice.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+), 0 deletions(-) diff --git a/invesalius/data/viewer_slice.py b/invesalius/data/viewer_slice.py index d3fd971..23d1bed 100755 --- a/invesalius/data/viewer_slice.py +++ b/invesalius/data/viewer_slice.py @@ -258,6 +258,30 @@ class CanvasRendererCTX: self._cv_image.Modified() self.modified = False + def calc_text_size(self, text, font=None): + """ + Given an unicode text and a font returns the width and height of the + rendered text in pixels. + + Params: + text: An unicode text. + font: An wxFont. + + Returns: + A tuple with width and height values in pixels + """ + if self.gc is None: + return None + gc = self.gc + + if font is None: + font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) + + _font = gc.CreateFont(font) + gc.SetFont(_font) + w, h = gc.GetTextExtent(text) + return w, h + def draw_line(self, pos0, pos1, arrow_start=False, arrow_end=False, colour=(255, 0, 0, 128), width=2, style=wx.SOLID): """ Draw a line from pos0 to pos1 -- libgit2 0.21.2