Brad_2 0 Newbie Poster

My platform is Ubuntu 12.10 with wx.Python 2.8.12.1 (gtk2-unicode).

My application uses wxPython to print text to a printer. I want to use Ubuntu Condensed font face for some of the text. The problem is that when the text is printed the text that should be in Ubuntu Condensed face prints in the same font as all the other text. I am quite certain I have it coded properly since the correct face is used in the print preview.

I am quite certain the Ubuntu Condensed face is installed correctly since it shows up correctly in a wxPython app I found tyhat enumerates all installed font families and font faces.

The code follows. The SSLabel::OnPrintPage method is where the fonts are switched and 3 lines of test text is drawn to the device context. The first line should be in the default font face; the last two lines should be in the Ubuntu Condensed face and they are in Ubuntu Condensed in print preview but not on the printout.

Run the code. you'll get a frame with 2 buttons: one will produce a print preview of the 3 aforementioned test text lines, the other will send the same text to the printer.

import wx

class SSLabel(wx.Printout):

    def __init__(self, s1):
        wx.Printout.__init__(self)
        self.s1 = s1

    def OnPrintPage(self, page):
        dc = self.GetDC()
        font = wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        dc.SetFont(font)
        dc.DrawText('Test text: ABCDEF abcdef', 50, 50)
        font = wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL,
                       False, 'Ubuntu Condensed')
        dc.SetFont(font)
        dc.DrawText('Test text: ABCDEF abcdef', 50, 100)
        font = wx.Font(11, wx.SWISS, wx.NORMAL, wx.NORMAL, 
                       False, 'Ubuntu Condensed')
        dc.SetFont(font)
        dc.DrawText('Test text: ABCDEF abcdef', 50, 150)

class PrinterFontDemo(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600, 400))

        wx.StaticText(self, -1, 'your wxPy version: ' + wx.version(), (20, 20))
        self.PreviewBtn = wx.Button(self, -1, 'Print Preview', (20, 60), (120, 30))
        self.PrintBtn = wx.Button(self, -1, 'Print', (20, 100), (120, 30))
        self.Bind(wx.EVT_BUTTON, self.OnPrint, self.PrintBtn)
        self.Bind(wx.EVT_BUTTON, self.OnPrintPreview, self.PreviewBtn)

        # initialize the print data and set some default values
        self.pdata = wx.PrintData()
        self.pdata.SetPaperId(wx.PAPER_LETTER)
        self.pdata.SetOrientation(wx.PORTRAIT)
        self.margins = (wx.Point(15,15), wx.Point(15,15))


    def OnPrintPreview(self, evt):
        data = wx.PrintDialogData(self.pdata)
        printout1 = SSLabel(self)
        printout2 = None #LabelPrintout(text, "title", self.margins)
        preview = wx.PrintPreview(printout1, printout2, data)
        if not preview.Ok():
            wx.MessageBox("Unable to create PrintPreview!", "Error")
        else:
            # create the preview frame such that it overlays the app frame
            frame = wx.PreviewFrame(preview, self, "Print Preview",
                                    pos=self.GetPosition(),
                                    size=self.GetSize())
            frame.Initialize()
            frame.Show()


    def OnPrint(self, evt):
        data = wx.PrintDialogData(self.pdata)
        printer = wx.Printer(data)
        printout = SSLabel(self)
        useSetupDialog = True
        if not printer.Print(self, printout, useSetupDialog) \
           and printer.GetLastError() == wx.PRINTER_ERROR:
            wx.MessageBox(
                "There was a problem printing.\n"
                "Perhaps your current printer is not set correctly?",
                "Printing Error", wx.OK)
        else:
            data = printer.GetPrintDialogData()
            self.pdata = wx.PrintData(data.GetPrintData()) # force a copy
        printout.Destroy()


class MyApp(wx.App):
    def OnInit(self):
         frame = PrinterFontDemo(None, -1, 'Printer Font Demo')
         frame.Show(True)
         frame.Centre()
         return True

app = MyApp(0)
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.