1

Tested on Windows 10 only, may not be the same on other systems.

I want users to be able to download fonts and use them in my wxpython app, as such I have a sub-dir called "fonts" from which my app loads any ttf fonts at startup. I have found that when drawing text via wxpython GraphicsContext it seems that, if Bold or Italic is not set, it defaults to the first Font of a Family that was loaded, even if that is Bold or Italic, rather than the "Regular" version of the Font.

Here is some example code, firstly with the expected results:

import wx

app = wx.App()

# Add fonts for wx to use
wx.Font.AddPrivateFont("fonts/OpenSans-Regular.ttf")
wx.Font.AddPrivateFont("fonts/OpenSans-Bold.ttf")

frame = wx.Frame(None, title='Simple application')
frame.Show()

dc = wx.ClientDC(frame)
gc = wx.GraphicsContext.Create(dc)
gc.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, "Open Sans"), wx.BLACK)
gc.DrawText("Opens Sans", 10, 10)
gc.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, "Open Sans"), wx.BLACK)
gc.DrawText("Opens Sans Bold", 10, 30)

app.MainLoop()

exit()

Correctly shows Regular text followed by Bold text:

correct results

Now with unexpected results:

import wx

app = wx.App()

# Add fonts for wx to use
wx.Font.AddPrivateFont("fonts/OpenSans-Bold.ttf")
wx.Font.AddPrivateFont("fonts/OpenSans-Regular.ttf")

frame = wx.Frame(None, title='Simple application')
frame.Show()

dc = wx.ClientDC(frame)
gc = wx.GraphicsContext.Create(dc)
gc.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False, "Open Sans"), wx.BLACK)
gc.DrawText("Opens Sans", 10, 10)
gc.SetFont(wx.Font(12, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, "Open Sans"), wx.BLACK)
gc.DrawText("Opens Sans Bold", 10, 30)

app.MainLoop()

exit()

Now both pieces of text are Bold:

Unexpected results

The only difference being that I have switched round the load order of the font files.

I have worked round this in my app by parsing the file list from the fonts dir and ensuring that Bold and Italic fonts are loaded last, but I am sure I should not have to do this, and am not sure it will be 100% reliable (it relies on the words "Bold" or "Italic" being in the filename).

Am I doing something wrong here or is this a genuine bug?

Setting fonts on normal controls works fine, it just seems to be GraphicsContext that gets it wrong.

Giles
  • 1,597
  • 11
  • 15

0 Answers0