Get text size in pixels.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Get text size in pixels.

ioprst0904
I need to get the text size in pixels. How to do it?
Wrote function:

def CalcMarkSize(self, *args):
""" Calculate the size of each element in the arguments in pixels and return a string of the form 'w h w h ... w h' """
# 1px = 0.747213pt
pt = 0.747213
font_size_px = 16
font_size_pt = font_size_px * pt
dc = wx.ClientDC(self)
dc.SetFont(wx.Font(pointSize=font_size_pt, family=wx.FONTFAMILY_DEFAULT, style=wx.FONTSTYLE_NORMAL,
                   weight=wx.FONTWEIGHT_NORMAL, underline=False, faceName='Consolas'))
w_list = [dc.GetTextExtent(label).GetWidth() for label in args]
h_list = [dc.GetTextExtent(label).GetHeight() for label in args]
wh_list = [item for sublist in zip(w_list, h_list) for item in sublist]
return ' '.join(map(str, wh_list))

For the line "Text" I get "32 ​​8". But this is not the right size.
I insert two elements "text" in the svg document with "Text" values. The second value is shifted by 32 pixels. Font: "Consolas", size: 16 pixels. Texts are superimposed on each other. Or am I doing something wrong? Thank.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Get text size in pixels.

ioprst0904
It seems to work better.

def CalcMarkSize(self, *args):
""" Calculate the size of each element in the arguments in pixels and return a string of the form 'w h w h ... w h' """
# 1px = 0.747213pt
pt = 0.747213
font_size_px = 16
font_size_pt = font_size_px * pt
dc = wx.ClientDC(self)
# dc.SetFont(wx.Font(pointSize=font_size_pt, family=wx.FONTFAMILY_DEFAULT, style=wx.FONTSTYLE_NORMAL,
#                    weight=wx.FONTWEIGHT_NORMAL, underline=False, faceName='Consolas'))
# w_list = [dc.GetTextExtent(label).GetWidth() for label in args]
# h_list = [dc.GetTextExtent(label).GetHeight() for label in args]
dc.SetFont(wx.Font(pointSize=font_size_px, family=wx.FONTFAMILY_DEFAULT, style=wx.FONTSTYLE_NORMAL,
   weight=wx.FONTWEIGHT_NORMAL, underline=False, faceName='Consolas'))
w_list = [dc.GetTextExtent(label).GetWidth()*pt for label in args]
h_list = [dc.GetTextExtent(label).GetHeight()*pt for label in args]
wh_list = [item for sublist in zip(w_list, h_list) for item in sublist]
return ' '.join(map(str, wh_list))

Result: 35.866224 17.933112.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink" baseProfile="full" height="100%" style="background:#FFFFFF" version="1.1" viewBox="0 0 576 576" width="100%">
  <g id="root" transform="translate(32,32)">
    <text x="0" y="0" font-family="Consolas">Text</text>
    <text x="35.866224" y="0" font-family="Consolas" font-size="16.0">Text</text>
    <text x="35.866224" y="17.933112" font-family="Consolas" font-size="16.0">Text</text>
  </g>
</svg>


<a imageanchor="1" href="about:invalid#zClosurez" style="margin-left: 1em; margin-right: 1em;">1.PNG

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.