How to compute font size ?

Hi,

In my app, I have a window(a panel) where I write some text as an overlay :

 def \_set\_overlay\(self, win, text, colour=\(192, 192, 192\)\):
     dc = wx\.ClientDC\(win\)
     odc = wx\.DCOverlay\(self\.overlay, dc\)
     odc\.Clear\(\)
     ctx = wx\.GraphicsContext\.Create\(dc\)
     col = wx\.Colour\(\*colour, 128\)
     brush = ctx\.CreateBrush\(wx\.Brush\(wx\.Colour\(\(0,0,0,128\)\)\)\)
     font = ctx\.CreateFont\(10, "ARIAL", flags=wx\.FONTFLAG\_DEFAULT, col=col\)
     ctx\.SetFont\(font\)
     ctx\.DrawText\(text, 10, 10, brush\)
     del odc

For now, the font size is fixed. I'd like to compute the font size to make the text half the width of the window.
What is the best way to compute the font size ?

Regards,
Nicolas

https://wxpython.org/Phoenix/docs/html/wx.GraphicsContext.html#wx.GraphicsContext.GetFullTextExtent

-CHB

···

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

GetFullTextExtent() is usefull but not enought.
I tried several methods. I ended with the following code :
So : Create a font of size 10, determine the text width with this
font size, scale the font, and draw with the scaled font.
Nicolas

···

Le 01/01/2019 à 18:03, ‘Chris Barker’
via wxPython-dev a écrit :

https://wxpython.org/Phoenix/docs/html/wx.GraphicsContext.html#wx.GraphicsContext.GetFullTextExtent

-CHB

` def _set_overlay(self, win, text,
colour=(192, 192, 192)):``

  ``        dc = wx.ClientDC(win)``

  ``        odc = wx.DCOverlay(self.overlay, dc)``

  ``        odc.Clear()``

  ``        ctx = wx.GraphicsContext.Create(dc)``

  ``                ctx_size =

ctx.GetSize() # Get window size``

  ``                col = wx.Colour(*colour,
  1.                   # Add alpha to colour``
    

    brush = ctx.CreateBrush(wx.Brush(wx.Colour((0,0,0,128)))) # Create background brush with alpha

    fnt = wx.Font(wx.FontInfo(10).FaceName("Arial")) # Create font with size 10

    font = ctx.CreateFont(fnt, col=col) # Create graphics font

    ctx.SetFont(font) # Use graphics font

    txt_size = ctx.GetFullTextExtent(text) # Get text size

    scale = (ctx_size[0] / 3) / txt_size[0] # Compute scale (text is third the width of the window)

    fnt.Scale(scale) # Scale font

    font = ctx.CreateFont(fnt, col=col) # Create new graphics font

    ctx.SetFont(font) # Use new graphicsfont

    txt_size = ctx.GetFullTextExtent(text) # Get new text size

    ctx.DrawText(text, (ctx_size[0]-txt_size[0])/2, (ctx_size[1]-txt_size[1])/2, brush) # Draw text (centered)

    del odc

    `

      On Fri, Dec 28, 2018 at 2:40 PM Nicolas Pinault

<314@drpi.fr >
wrote:

Hi,

      In my app, I have a window(a panel) where I write some text as

an overlay :

           def _set_overlay(self, win, text, colour=(192, 192,

192)):

               dc = wx.ClientDC(win)

               odc = wx.DCOverlay(self.overlay, dc)

               odc.Clear()

               ctx = wx.GraphicsContext.Create(dc)

               col = wx.Colour(*colour, 128)

               brush =

ctx.CreateBrush(wx.Brush(wx.Colour((0,0,0,128))))

               font = ctx.CreateFont(10, "ARIAL",

flags=wx.FONTFLAG_DEFAULT,

      col=col)

               ctx.SetFont(font)

               ctx.DrawText(text, 10, 10, brush)

               del odc



      For now, the font size is fixed. I'd like to compute the font

size to

      make the text half the width of the window.

      What is the best way to compute the font size ?



      Regards,

      Nicolas



      --

      You received this message because you are subscribed to the

Google Groups “wxPython-dev” group.

      To unsubscribe from this group and stop receiving emails from

it, send an email to wxPython-dev+unsubscribe@googlegroups.com.

      For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
  --


    Christopher Barker, Ph.D.

    Oceanographer



    Emergency Response Division

    NOAA/NOS/OR&R            (206) 526-6959   voice

    7600 Sand Point Way NE   (206) 526-6329   fax

    Seattle, WA  98115       (206) 526-6317   main reception



    Chris.Barker@noaa.gov

  You received this message because you are subscribed to the Google

Groups “wxPython-dev” group.

  To unsubscribe from this group and stop receiving emails from it,

send an email to wxPython-dev+unsubscribe@googlegroups.com.

  For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
1 Like

GetFullTextExtent() is usefull but not enought.

indeed – there are complications that make it hard to do properly:

Fonts do not, necessarily, scale directly with font size. So text in a 20 pt font is not exactly twice as big as text in a 10 pt font. But it’s pretty close.

I’m not sure I quite understand your use case, but I’d be tempted to get the font size close by assuming “typical” text – and then use the actual text extent to center it.

A little less precise, but easier.

-CHB

···

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

how can I HELP?

···

On Friday, December 28, 2018 at 5:40:27 PM UTC-5, Nicolas Pinault wrote:

Hi,

In my app, I have a window(a panel) where I write some text as an overlay :

 def _set_overlay(self, win, text, colour=(192, 192, 192)):

     dc = wx.ClientDC(win)

     odc = wx.DCOverlay(self.overlay, dc)

     odc.Clear()

     ctx = wx.GraphicsContext.Create(dc)

     col = wx.Colour(*colour, 128)

     brush = ctx.CreateBrush(wx.Brush(wx.Colour((0,0,0,128))))

     font = ctx.CreateFont(10, "ARIAL", flags=wx.FONTFLAG_DEFAULT,

col=col)

     ctx.SetFont(font)

     ctx.DrawText(text, 10, 10, brush)

     del odc

For now, the font size is fixed. I’d like to compute the font size to
make the text half the width of the window.

What is the best way to compute the font size ?

Regards,

Nicolas

In my function, a text (parameter of the function) is displayed
centered in a window. The text width is one third of the window
width. The problem is to compute the font size to make the text the
correct width.
I tried to compute the font size without success. Using a font of size 10 then scaling it works. Nicolas

···

Hi Chris,

  Le 03/01/2019 à 06:43, 'Chris Barker' via wxPython-dev a écrit :
      On Tue, Jan 1, 2019 at 9:53 AM Nicolas Pinault

<314@drpi.fr >
wrote:

          GetFullTextExtent() is usefull but

not enought.

        indeed -- there are complications that make it hard to do

properly:

        Fonts do not, necessarily, scale directly with font size.

So text in a 20 pt font is not exactly twice as big as text
in a 10 pt font. But it’s pretty close.

        I'm not sure I quite understand your use case, but I'd be

tempted to get the font size close by assuming “typical”
text – and then use the actual text extent to center it.

A little less precise, but easier.

-CHB

          I tried several methods. I ended with

the following code :

          `                      def _set_overlay(self, win, text,

colour=(192, 192, 192)):``

            ``        dc = wx.ClientDC(win)``

            ``        odc = wx.DCOverlay(self.overlay, dc)``

            ``        odc.Clear()``

            ``        ctx = wx.GraphicsContext.Create(dc)``

            ``                          ctx_size =

ctx.GetSize() # Get window
size``

            ``                          col = wx.Colour(*colour,
  1.                   # Add alpha to colour``
    
           ``                          brush =
    

ctx.CreateBrush(wx.Brush(wx.Colour((0,0,0,128)))) #
Create background brush with alpha``

            ``                          fnt =

wx.Font(wx.FontInfo(10).FaceName(“Arial”)) # Create
font with size 10``

            ``                          font = ctx.CreateFont(fnt,

col=col) # Create graphics font``

            ``                         

ctx.SetFont(font) #
Use graphics font``

            ``                          txt_size =

ctx.GetFullTextExtent(text) # Get text
size``

            ``                          scale = (ctx_size[0] / 3) /

txt_size[0] # Compute scale (text is third
the width of the window)``

            ``                         

fnt.Scale(scale) #
Scale font ``

            ``                          font = ctx.CreateFont(fnt,

col=col) # Create new graphics font``

            ``                         

ctx.SetFont(font) #
Use new graphicsfont``

            ``                          txt_size =

ctx.GetFullTextExtent(text) # Get new
text size``

            ``                          ctx.DrawText(text,

(ctx_size[0]-txt_size[0])/2,
(ctx_size[1]-txt_size[1])/2, brush) # Draw text
(centered)``

            ``        del odc``

            `

          So : Create a font of size 10, determine the text width

with this font size, scale the font, and draw with the
scaled font.

          Nicolas
                On Fri, Dec 28, 2018 at 2:40 PM Nicolas

Pinault <314@drpi.fr >
wrote:

Hi,

                In my app, I have a window(a panel) where I write

some text as an overlay :

                     def _set_overlay(self, win, text, colour=(192,

192, 192)):

                         dc = wx.ClientDC(win)

                         odc = wx.DCOverlay(self.overlay, dc)

                         odc.Clear()

                         ctx = wx.GraphicsContext.Create(dc)

                         col = wx.Colour(*colour, 128)

                         brush =

ctx.CreateBrush(wx.Brush(wx.Colour((0,0,0,128))))

                         font = ctx.CreateFont(10, "ARIAL",

flags=wx.FONTFLAG_DEFAULT,

                col=col)

                         ctx.SetFont(font)

                         ctx.DrawText(text, 10, 10, brush)

                         del odc



                For now, the font size is fixed. I'd like to compute

the font size to

                make the text half the width of the window.

                What is the best way to compute the font size ?



                Regards,

                Nicolas



                --

                You received this message because you are subscribed

to the Google Groups “wxPython-dev” group.

                To unsubscribe from this group and stop receiving

emails from it, send an email to wxPython-dev+unsubscribe@googlegroups.com.

                For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
            --


              Christopher Barker, Ph.D.

              Oceanographer



              Emergency Response Division

              NOAA/NOS/OR&R            (206) 526-6959   voice

              7600 Sand Point Way NE   (206) 526-6329   fax

              Seattle, WA  98115       (206) 526-6317   main

reception

              Chris.Barker@noaa.gov

            You received this message because you are subscribed to

the Google Groups “wxPython-dev” group.

            To unsubscribe from this group and stop receiving emails

from it, send an email to wxPython-dev+unsubscribe@googlegroups.com.

            For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

        You received this message because you are subscribed to the

Google Groups “wxPython-dev” group.

        To unsubscribe from this group and stop receiving emails

from it, send an email to wxPython-dev+unsubscribe@googlegroups.com.

        For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
    --


      Christopher Barker, Ph.D.

      Oceanographer



      Emergency Response Division

      NOAA/NOS/OR&R            (206) 526-6959   voice

      7600 Sand Point Way NE   (206) 526-6329   fax

      Seattle, WA  98115       (206) 526-6317   main reception



      Chris.Barker@noaa.gov

  You received this message because you are subscribed to the Google

Groups “wxPython-dev” group.

  To unsubscribe from this group and stop receiving emails from it,

send an email to wxPython-dev+unsubscribe@googlegroups.com.

  For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

In my function, a text (parameter of the function) is displayed
centered in a window. The text width is one third of the window
width. The problem is to compute the font size to make the text the
correct width.

I tried to compute the font size without success.

Using a font of size 10  then scaling it works.

Your solution is about as good as you can do. If it needs to be drawn a lot with the same text, then you may want to cache the font size and coordinates, but it it’s at enough as is, then you’re done :slight_smile:

-CHB

···

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov