printing accent with the PrintFramework

Hi,

I'm trying to print unicode text.

If I make a preview of my page to print, all is OK.

But when I try to print it, there is a problem, accents are not well
printed.

Here is my simple code:

# -*- coding: utf-8 -*-
import wx

def DoDrawing(dc, numImpress,mmToPixel,pixelToMm):

    def MmToPixel(a):
        return int(a*mmToPixel)

    def PixelToMm(a):
        return int(a*pixelToMm)
    dc.StartDoc("impression")
    dc.StartPage()

    dc.SetFont(wx.Font(MmToPixel(8), wx.DEFAULT, wx.NORMAL,
wx.NORMAL,wx.FONTENCODING_UTF8 ))

    dc.DrawText(u"adhésion", MmToPixel(50), MmToPixel(50))

    dc.EndPage()
    dc.EndDoc()

class MyPrintout(wx.Printout):
    def __init__(self, numImpress):
        wx.Printout.__init__(self)

        self.numImpress = numImpress

    def OnPreparePrinting(self):
        #méthode appelée avant que wx.Printout ne fasse quoique ce
soit
        #C'est un bon endroit pour mettre des calculs à faire avant
l'impression
        #comme les calculs d'échelle par exemple
        self.base_OnPreparePrinting() # <===Cette ligne est
utile si on supprime tout ce qui précède dans la méthode
OnPreparePrinting()

    def OnBeginPrinting(self):
        #Cette méthode ne fait rien
        #Elle n'est appelée qu'une fois au début de l'impression
        self.base_OnBeginPrinting()

    def OnBeginDocument(self, start, end):
        #Chaque copie du document à imprimer déclenche cette méthode
        #start et end sont des entiers qui indiquent quelles pages
imprimer, ces arguments sont inclusifs
        #retourner False depuis cette méthode annule l'impression
        return self.base_OnBeginDocument(start, end)#méthode de base
obligatoire car elle fat des choses importantes, comme appeler
wx.DC.StartDoc()

    def OnPrintPage(self, page):
        #C'est ici qu'on place les dessins à effectuer
        #page est le numéro de la page à imprimer

        #calcul de l'échelle
        dc = self.GetDC()
        dw, dh = dc.GetSize()
        ppiPrinterX, ppiPrinterY = self.GetPPIPrinter()
        mmToPixel=float(ppiPrinterX)/25.4
        pixelToMm=25.4/float(ppiPrinterX)
        DoDrawing(dc, self.numImpress,mmToPixel, pixelToMm)
        return True

    def OnEndDocument(self):
        #Méthode appelée à la fin de chaque copie du document
        self.base_OnEndDocument()#méthode de base obligatoire car elle
fait des choses importantes comme wx.DC.EndDoc()

    def OnEndPrinting(self):
        #Cette méthode cloture l'impression
        self.base_OnEndPrinting()

    def HasPage(self, page):
        #page est un entier représentant le numéro de page
        #Cette méthode doit retourner True si la page est dans le
document et False sinon
        #Par défaut, elle retourne True uniquement si page==1
(impression d'une seule page)
        if page <= 1:
            return True
        else:
            return False

    def GetPageInfo(self):
        #retourne les valeurs minimales et maximales des numéros de
pages que l'utilisateur peut sélectionner
        # et l'intervalle des pages à imprimer
        return (1, 1, 1, 1)#(minPage, maxPage, pageFrom, pageTo)

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):

        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
wx.DefaultSize)

        self.b=wx.Button(self, -1, "imprimer", (20,20),
wx.DefaultSize)

        self.printData = wx.PrintData()
        self.printData.SetPaperId(wx.PAPER_A4)
        self.printData.SetOrientation(wx.PORTRAIT)
        self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)

        self.Bind(wx.EVT_BUTTON, self.OnClickBouton, self.b)

    def OnClickBouton(self, event):

        pdd = wx.PrintDialogData(self.printData)
        pdd.SetToPage(1)
        printer = wx.Printer(pdd)
        printout = MyPrintout(1)
        printer.Print(self, printout,False)
        printout.Destroy()

if __name__ == "__main__":

    app = wx.App(False)
    frame = MyFrame(None, -1, "Test impression")
    frame.Show()
    app.MainLoop()

Can anyone help me please ?

Please make a sample app. http://wiki.wxpython.org/MakingSampleApps

and if possible, replace the French comments with English to make it easier to understand for some people.

···


Hi, I will kill all ads in google gmail!
They will all be dead and gone for all my emails to you. HA HA bye bye ads I just massacred you!!!

???

Your code seems to work for me. The printout consists of the large underlined word “adhesion” with the letter “E” with an aigu ( a forward tick) accent over it. The aigu is definitely “well formed”. Is this what you want ?

Perhaps you are not using the latest versions of Python and wxPython.

Perhaps you are using a bad font.

I’m using:

Windows 6.1.7600
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]
Wx 2.8.11.0

I have ni idea what default font is being used.

See the attached app.

Ray

PRINTING_UNICODE.PY (5.12 KB)

···

2010/11/22 oli5465 o.martin396@gmail.com

Hi,

I’m trying to print unicode text.

If I make a preview of my page to print, all is OK.

But when I try to print it, there is a problem, accents are not well
printed.

You need to use the Unicode version of wxPython 2.8.11.

Thanks for your answer.

I work under Linux Ubuntu with Python2.6 and wxPython 2.8.11.
Is there a unicode of wxPython for Linux too ?

My code doesn't work on my computer, instead of a "é", it print "~A©"

I think it may be a driver problem or a unicode one.

I'll try to compile the last version of wxPython to see the result.

Olivier

···

On 24 nov, 19:33, Boštjan Mejak <bostjan.me...@gmail.com> wrote:

You need to use the Unicode version of wxPython 2.8.11.

Thanks for your answer.

I work under Linux Ubuntu with Python2.6 and wxPython 2.8.11.
Is there a unicode of wxPython for Linux too ?

Yes, the default packages for ubuntu are a Unicode build.

My code doesn't work on my computer, instead of a "é", it print "~A©"

I think it may be a driver problem or a unicode one.

Are the strings you are printing Unicode or str objects? If str then what is the encoding used? What is your locale's default encoding?

···

On 11/25/10 12:02 PM, oli5465 wrote:

--
Robin Dunn
Software Craftsman