anti-aliased text with wx.GraphicsContext?

Hello. Does anyone know if it is possible to get anti-aliased
drawn-on text using wx,GraphicsContext? I'm trying it, and I'm
getting text, but it is ignoring the dc's color and it is not
anti-aliasing the text.

I'm doing this within a OnPaint handler:

dc = wx.BufferedPaintDC(self.panel1)
dc.SetTextForeground(wx.Colour(23,44,244))
gc = wx.GraphicsContext.Create(dc)
font = wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.BOLD)
gc.SetFont(font)
gc.DrawText('testing anti-aliasing', 10,6)

I'm on wx 2.8.10.1, Python 2.5, WinXP.

Any help is appreciated.

Che

Try,

gc.SetFont(font, wx.BLUE)

You can use wx.BLUE/wx.BLACK/... and other colors.

You can also use:
gc.SetFont(font, wx.Colour(125,125,125,125))

Syntax : wx.Colour(r,g,b,alpha)

Go to you "Appearance/Effects" settings dialog on win xp . Right click
on your desktop and click "Properties".

You have one option there saying "Use the following method to smooth
edges of screen fonts".

Select either "Standard" or "Clear Type"

Prashant

···

On Aug 29, 8:57 am, C M <cmpyt...@gmail.com> wrote:

Hello. Does anyone know if it is possible to get anti-aliased
drawn-on text using wx,GraphicsContext? I'm trying it, and I'm
getting text, but it is ignoring the dc's color and it is not
anti-aliasing the text.

I'm doing this within a OnPaint handler:

dc = wx.BufferedPaintDC(self.panel1)
dc.SetTextForeground(wx.Colour(23,44,244))
gc = wx.GraphicsContext.Create(dc)
font = wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.BOLD)
gc.SetFont(font)
gc.DrawText('testing anti-aliasing', 10,6)

I'm on wx 2.8.10.1, Python 2.5, WinXP.

Any help is appreciated.

Che

Try,

gc.SetFont(font, wx.BLUE)

You can use wx.BLUE/wx.BLACK/... and other colors.
You can also use:
gc.SetFont(font, wx.Colour(125,125,125,125))

Thank you. I didn't realize you could do that since for a regular dc
there is a SetForegroundColour() option. Should've studied the API
better.

Go to you "Appearance/Effects" settings dialog on win xp . Right click
on your desktop and click "Properties".

You have one option there saying "Use the following method to smooth
edges of screen fonts".

Select either "Standard" or "Clear Type"

OK, but what I want is that the text looks anti-aliased regardless of
what the user has their system set to.

I thought the point of wx.GraphicsContext was that it could do this,
whereas the regular text relies on the native widget which may or may
not anti-alias depending on the OS. (Gotten from here:
http://osdir.com/ml/wxpython-users/2010-02/msg00450.html)

So I'm still looking for an answer on that one...

Thanks,
Che

···

Prashant

On Aug 29, 8:57 am, C M <cmpyt...@gmail.com> wrote:

Hello. Does anyone know if it is possible to get anti-aliased
drawn-on text using wx,GraphicsContext? I'm trying it, and I'm
getting text, but it is ignoring the dc's color and it is not
anti-aliasing the text.

I'm doing this within a OnPaint handler:

dc = wx.BufferedPaintDC(self.panel1)
dc.SetTextForeground(wx.Colour(23,44,244))
gc = wx.GraphicsContext.Create(dc)
font = wx.Font(24, wx.DEFAULT, wx.NORMAL, wx.BOLD)
gc.SetFont(font)
gc.DrawText('testing anti-aliasing', 10,6)

I'm on wx 2.8.10.1, Python 2.5, WinXP.

Any help is appreciated.

Che

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

wx.GraphicsContext wraps GDI+, which is a system API, so it is still dependent on the system settings for AA. You could try using wx.lib.graphics.GraphicsContext instead, it uses Cairo on all the platforms instead of the native APIs.

···

On 8/29/10 7:42 AM, C M wrote:

Go to you "Appearance/Effects" settings dialog on win xp . Right click
on your desktop and click "Properties".

You have one option there saying "Use the following method to smooth
edges of screen fonts".

Select either "Standard" or "Clear Type"

OK, but what I want is that the text looks anti-aliased regardless of
what the user has their system set to.

I thought the point of wx.GraphicsContext was that it could do this,
whereas the regular text relies on the native widget which may or may
not anti-alias depending on the OS.

--
Robin Dunn
Software Craftsman

I thought the point of wx.GraphicsContext was that it could do this,
whereas the regular text relies on the native widget which may or may
not anti-alias depending on the OS.

wx.GraphicsContext wraps GDI+, which is a system API, so it is still
dependent on the system settings for AA. You could try using
wx.lib.graphics.GraphicsContext instead, it uses Cairo on all the platforms
instead of the native APIs.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Thanks, Robin. OK, I've now downloaded Cario and am trying to do
this, but I'm stuck at trying to get Cairo (and therefore
wx.lib.graphics.GraphicsContext) to work. When I try it, the window
becomes unresponsive and I'm getting the following error:

Traceback (most recent call last):
  File "testingCairo.py", line 34, in OnPanel1Paint
    self.Render(dc)
  File "testingCairo.py", line 39, in Render
    wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))
  File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\wxcairo.py",
line 148, in FontFaceFromFont
    fontfaceptr = voidp( cairoLib.cairo_win32_font_face_create_for_hfont(
  File "C:\Python25\lib\ctypes\__init__.py", line 325, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python25\lib\ctypes\__init__.py", line 330, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'cairo_win32_font_face_create_for_hfont' not found

I installed Cairo 1.8.4 and put the dlls for libcairo-2.dll,
libpng14-14.dll and zlib1.dll in the site-packages/cairo directory,
and installed pyCairo 1.8.4 for Python 2.5.

There is an attached small runnable sample mostly taken from the wxPython demo.

Any idea what I'm doing wrong?

Thank you,
Che

testingCairo.py (1.47 KB)

At least part of the problem is that you are getting the paint event for the panel but creating the paint DC for the frame. That will cause endless paint events to be sent on Windows because it assumes that the event has not been handled yet, and that will make the window appear to be non-responsive.

I'm not able to duplicate the function not found error, but my guess is that a different version of the cairo DLL is being found than what you are expecting.

···

On 8/29/10 10:42 PM, C M wrote:

I thought the point of wx.GraphicsContext was that it could do this,
whereas the regular text relies on the native widget which may or may
not anti-alias depending on the OS.

wx.GraphicsContext wraps GDI+, which is a system API, so it is still
dependent on the system settings for AA. You could try using
wx.lib.graphics.GraphicsContext instead, it uses Cairo on all the platforms
instead of the native APIs.

Thanks, Robin. OK, I've now downloaded Cario and am trying to do
this, but I'm stuck at trying to get Cairo (and therefore
wx.lib.graphics.GraphicsContext) to work. When I try it, the window
becomes unresponsive and I'm getting the following error:

Traceback (most recent call last):
   File "testingCairo.py", line 34, in OnPanel1Paint
     self.Render(dc)
   File "testingCairo.py", line 39, in Render
     wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))
   File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\wxcairo.py",
line 148, in FontFaceFromFont
     fontfaceptr = voidp( cairoLib.cairo_win32_font_face_create_for_hfont(
   File "C:\Python25\lib\ctypes\__init__.py", line 325, in __getattr__
     func = self.__getitem__(name)
   File "C:\Python25\lib\ctypes\__init__.py", line 330, in __getitem__
     func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'cairo_win32_font_face_create_for_hfont' not found

I installed Cairo 1.8.4 and put the dlls for libcairo-2.dll,
libpng14-14.dll and zlib1.dll in the site-packages/cairo directory,
and installed pyCairo 1.8.4 for Python 2.5.

There is an attached small runnable sample mostly taken from the wxPython demo.

Any idea what I'm doing wrong?

--
Robin Dunn
Software Craftsman

I thought the point of wx.GraphicsContext was that it could do this,
whereas the regular text relies on the native widget which may or may
not anti-alias depending on the OS.

wx.GraphicsContext wraps GDI+, which is a system API, so it is still
dependent on the system settings for AA. You could try using
wx.lib.graphics.GraphicsContext instead, it uses Cairo on all the
platforms
instead of the native APIs.

Thanks, Robin. OK, I've now downloaded Cario and am trying to do
this, but I'm stuck at trying to get Cairo (and therefore
wx.lib.graphics.GraphicsContext) to work. When I try it, the window
becomes unresponsive and I'm getting the following error:

Traceback (most recent call last):
File "testingCairo.py", line 34, in OnPanel1Paint
self.Render(dc)
File "testingCairo.py", line 39, in Render
wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))
File
"C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\wxcairo.py",
line 148, in FontFaceFromFont
fontfaceptr = voidp( cairoLib.cairo_win32_font_face_create_for_hfont(
File "C:\Python25\lib\ctypes\__init__.py", line 325, in __getattr__
func = self.__getitem__(name)
File "C:\Python25\lib\ctypes\__init__.py", line 330, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'cairo_win32_font_face_create_for_hfont' not
found

I installed Cairo 1.8.4 and put the dlls for libcairo-2.dll,
libpng14-14.dll and zlib1.dll in the site-packages/cairo directory,
and installed pyCairo 1.8.4 for Python 2.5.

There is an attached small runnable sample mostly taken from the wxPython
demo.

Any idea what I'm doing wrong?

At least part of the problem is that you are getting the paint event for the
panel but creating the paint DC for the frame. That will cause endless
paint events to be sent on Windows because it assumes that the event has not
been handled yet, and that will make the window appear to be non-responsive.

Whoops, didn't catch that. Thanks.

I'm not able to duplicate the function not found error, but my guess is that
a different version of the cairo DLL is being found than what you are
expecting.

I tried installing cairo as per the instructions on the demo. As that
isn't working, I then tried based on these instructions (though using
Python 2.5):

http://alex.matan.ca/install-cairo-wxpyton-pycairo-python-windows

But I still have the error.

Is there a definitive list of which versions of, and where should be
installed, cairo, pyCairo, zlib and libpng that I should be running
with Python 2.5/wxPython 2.8.10.1 on WinXP?

Che

···

On Mon, Aug 30, 2010 at 1:06 PM, Robin Dunn <robin@alldunn.com> wrote:

On 8/29/10 10:42 PM, C M wrote:

I'm not able to duplicate the function not found error, but my guess is that
a different version of the cairo DLL is being found than what you are
expecting.

I tried installing cairo as per the instructions on the demo. As that
isn't working,

You can use DependencyWalker to see which DLLs are being loaded at runtime.

I then tried based on these instructions (though using
Python 2.5):

http://alex.matan.ca/install-cairo-wxpyton-pycairo-python-windows

But I still have the error.

Is there a definitive list of which versions of, and where should be
installed, cairo, pyCairo, zlib and libpng that I should be running
with Python 2.5/wxPython 2.8.10.1 on WinXP?

All I can say for sure is that the files here: Index of /cairo worked for me, and that DLLs are searched for by name on the PATH, so if other DLLs are found first because of the PATH then that could be part of your problem.

···

On 8/30/10 11:03 AM, C M wrote:

--
Robin Dunn
Software Craftsman

I'm not able to duplicate the function not found error, but my guess is
that
a different version of the cairo DLL is being found than what you are
expecting.

I tried installing cairo as per the instructions on the demo. As that
isn't working,

You can use DependencyWalker to see which DLLs are being loaded at runtime.

I then tried based on these instructions (though using
Python 2.5):

http://alex.matan.ca/install-cairo-wxpyton-pycairo-python-windows

But I still have the error.

Is there a definitive list of which versions of, and where should be
installed, cairo, pyCairo, zlib and libpng that I should be running
with Python 2.5/wxPython 2.8.10.1 on WinXP?

All I can say for sure is that the files here: Index of /cairo
worked for me,

OK, but can I ask *which* of those? There are choices for

cairo-1.6.4-2.zip
cairo_1.8.6-1_win32.zip

libpng-1.2.29.zip
libpng_1.2.34-1_win32.zip

pycairo-1.6.4.win32-py2.5.exe
pycairo-1.6.4.win32-py2.6.exe
pycairo-1.8.4.win32-py2.5.exe
pycairo-1.8.4.win32-py2.6.exe

(I'll want to use for py2.5...hopefully 2.5 vs. 2.6 isn't the issue)

I'll reinstall fresh with only the latest of all these and try again,
but if there is an easy way for you to check which are installed it
would be good to know.

and that DLLs are searched for by name on the PATH, so if
other DLLs are found first because of the PATH then that could be part of
your problem.

I haven't specified a PATH but if I look at sys.path, I have:

import sys
print sys.path

['C:\\Python25\\lib\\site-packages\\wx-2.8-msw-unicode',
'C:\\Python25\\lib\\site-packages\\wx-2.8-msw-unicode',
'C:\\Python25\\Lib\\site-packages\\boa-constructor',
'C:\\WINDOWS\\system32\\python25.zip',
'C:\\Python25\\DLLs',
'C:\\Python25\\lib',
'C:\\Python25\\lib\\plat-win',
'C:\\Python25\\lib\\lib-tk',
'C:\\Python25',
'C:\\Python25\\lib\\site-packages',
'C:\\Python25\\lib\\site-packages\\win32',
'C:\\Python25\\lib\\site-packages\\win32\\lib',
'C:\\Python25\\lib\\site-packages\\Pythonwin',
'C:\\Python25\\lib\\site-packages\\wx-2.8-msw-unicode']

I put the three DLLs for Cairo under the cairo subfolder under
site-packages, as per the instructions on the 2nd link I mentioned.

Thanks,
Che

···

On Mon, Aug 30, 2010 at 2:30 PM, Robin Dunn <robin@alldunn.com> wrote:

On 8/30/10 11:03 AM, C M wrote:

I'll reinstall fresh with only the latest of all these

Yes, if you are using 2.8.10.1 or later then you'll want the latest.

and try again,
but if there is an easy way for you to check which are installed it
would be good to know.

  >>> import cairo
  >>> cairo.version
  '1.8.4'
  >>> cairo.cairo_version_string
  <built-in function cairo_version_string>
  >>> cairo.cairo_version_string()
  '1.8.6'
  >>>

and that DLLs are searched for by name on the PATH, so if
other DLLs are found first because of the PATH then that could be part of
your problem.

I haven't specified a PATH

All windows systems should have a PATH setting in the environment. Look at os.environ['PATH']. This is where executables and DLLs are searched for when the loader has just the base name of the file, and not the full path name.

but if I look at sys.path, I have:

Only Python searches the sys.path when looking for modules or packages to import. DLLs loaded by EXEs or other DLLs will use the PATH in the environment, and typically will also lok in the same location as the binary doing the loading.

Another thought just occurred to me. You may want to do some debugging of the wx.lib.wxcairo._findCairoLib function as it uses ctypes to try to locate the DLL. If it can't find it using just the basename of the DLL then it uses ctypes.util.find_library so it may be finding and old version and not what you are not expecting it to find... However IIRC the first search using ctypes.CDLL(name) should be using the PATH and that would be the best way for you to solve this. (Either add the files to a folder already in the PATH, or add the folder you placed the DLLs in to the PATH/)

···

On 8/30/10 11:47 AM, C M wrote:

--
Robin Dunn
Software Craftsman

but if there is an easy way for you to check which are installed it
would be good to know.

>>> import cairo
>>> cairo.version
'1.8.4'
>>> cairo.cairo_version_string
<built-in function cairo_version_string>
>>> cairo.cairo_version_string()
'1.8.6'

Same as mine.

Another thought just occurred to me. You may want to do some debugging of
the wx.lib.wxcairo._findCairoLib function as it uses ctypes to try to locate
the DLL.

Haven't tried that yet.

If it can't find it using just the basename of the DLL then it
uses ctypes.util.find_library so it may be finding and old version and not
what you are not expecting it to find... However IIRC the first search
using ctypes.CDLL(name) should be using the PATH and that would be the best
way for you to solve this. (Either add the files to a folder already in the
PATH, or add the folder you placed the DLLs in to the PATH/)

I added the DLLs to the python25 folder, which is specified in the
PATH. I still have the problem.

Oddly, when I run the wxPython demo, Cairo works fine. How can this be?

If it can't find it using just the basename of the DLL then it
uses ctypes.util.find_library so it may be finding and old version and not
what you are not expecting it to find... However IIRC the first search
using ctypes.CDLL(name) should be using the PATH and that would be the best
way for you to solve this. (Either add the files to a folder already in the
PATH, or add the folder you placed the DLLs in to the PATH/)

I added the DLLs to the python25 folder, which is specified in the
PATH. I still have the problem.

Yes, but is it the first dir in the PATH that has copies of those DLLs?

Oddly, when I run the wxPython demo, Cairo works fine. How can this be?

Does it use the same function in wx.lib.wxcairo where your problem is manifesting?

···

On 8/30/10 12:44 PM, C M wrote:

--
Robin Dunn
Software Craftsman

I added the DLLs to the python25 folder, which is specified in the
PATH. I still have the problem.

Yes, but is it the first dir in the PATH that has copies of those DLLs?

No. That first dir was C:/WINDOWS/System32. I tried that and it
created a crash, so I cut the DLLs from it. Ultimately, I'll want the
Cairo-based anti-aliasing to work on apps that I deliver to others, so
I would rather not have to have users put (or the app put) DLLs in
that directory nor have to worry about their PATH settings or muck
with anything on their computers; I want it antialiasing to just work.

Oddly, when I run the wxPython demo, Cairo works fine. How can this be?

Does it use the same function in wx.lib.wxcairo where your problem is
manifesting?

Yes, *but*... I had taken the code to show some text from line 70 of
the Cairo demo in the wxPython demo. Which is confusing, since when I
run that demo, there IS no text displayed, only three geometric
shapes. The demo's log window does actually mention an error, though
one I don't understand:

    Debug: ..\..\src\msw\dc.cpp(2338): 'BitBlt' failed with error
0x00000057 (the parameter
    is incorrect.).

However, I have a part-way solution now. I've now tried taking code
to show text from the Cairo_Snippets code in the demo. *That* is
mostly working, and uses a different function. This is what I have
now for the Render(self,dc) function from the previous sample:

def Render(self, dc):
        cr = wx.lib.wxcairo.ContextFromDC(dc)
        cr.select_font_face ("Sans", cairo.FONT_SLANT_NORMAL,
                     cairo.FONT_WEIGHT_NORMAL)
        cairo.ANTIALIAS_SUBPIXEL
        cr.set_antialias(True)
        cr.set_font_size (65)

        cr.move_to (100, 100)
        cr.show_text ("Hello")

The difference in the two pieces of code is this:

NOT WORKING FOR ME: wx.lib.wxcairo.FontFaceFromFont(
            wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))

WORKING FOR ME: cr.select_font_face ("Sans", cairo.FONT_SLANT_NORMAL,
                     cairo.FONT_WEIGHT_NORMAL)

My remaining problem, though, is I don't think it is anti-aliasing the
text. The word "Hello" does not look smoothed to me. And it looks
the same whether I include those antialias lines or not.

Am I applying the antialiasing correctly? I'm finding the API hard to
figure out, probably because I haven't seen an example anywhere that
applies antialiasing to text (or maybe I have and don't realize it).

Thanks,
Che

I added the DLLs to the python25 folder, which is specified in the
PATH. I still have the problem.

Yes, but is it the first dir in the PATH that has copies of those DLLs?

No. That first dir was C:/WINDOWS/System32. I tried that and it
created a crash, so I cut the DLLs from it. Ultimately, I'll want the
Cairo-based anti-aliasing to work on apps that I deliver to others, so
I would rather not have to have users put (or the app put) DLLs in
that directory nor have to worry about their PATH settings or muck
with anything on their computers;

Experiment with wx.lib.wxcairo._findCairoLib. Like I implied before I'm not 100% clear on the semantics of where ctypes.CDLL and ctypes.util.find_library look for the DLLs they load. If it can be improved in any way I'll take a patch.

I want it antialiasing to just work.

Oddly, when I run the wxPython demo, Cairo works fine. How can this be?

Does it use the same function in wx.lib.wxcairo where your problem is
manifesting?

Yes, *but*... I had taken the code to show some text from line 70 of
the Cairo demo in the wxPython demo. Which is confusing, since when I
run that demo, there IS no text displayed, only three geometric
shapes.

Uh, you don't see any text? It should look like the attachment. If that's the case then I think that there is probably some deeper problem here, but I've no clue what it might be.

The demo's log window does actually mention an error, though
one I don't understand:

     Debug: ..\..\src\msw\dc.cpp(2338): 'BitBlt' failed with error
0x00000057 (the parameter
     is incorrect.).

That would probably be something to do with the bitmap, not necessarily just Cairo. Can you determine where that error is coming from by commenting out portions of the sample until the error goes away?

However, I have a part-way solution now. I've now tried taking code
to show text from the Cairo_Snippets code in the demo. *That* is
mostly working, and uses a different function. This is what I have
now for the Render(self,dc) function from the previous sample:

def Render(self, dc):
         cr = wx.lib.wxcairo.ContextFromDC(dc)
         cr.select_font_face ("Sans", cairo.FONT_SLANT_NORMAL,
                      cairo.FONT_WEIGHT_NORMAL)
         cairo.ANTIALIAS_SUBPIXEL
         cr.set_antialias(True)
         cr.set_font_size (65)

         cr.move_to (100, 100)
         cr.show_text ("Hello")

The difference in the two pieces of code is this:

NOT WORKING FOR ME: wx.lib.wxcairo.FontFaceFromFont(
             wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))

WORKING FOR ME: cr.select_font_face ("Sans", cairo.FONT_SLANT_NORMAL,
                      cairo.FONT_WEIGHT_NORMAL)

The advantage of FontFaceFromFont is that it will use the same native font handle as the wx.Font does to make the Cairo font object, so you can be sure that it is the same as what would have been used if you draw with a wx.DC.

My remaining problem, though, is I don't think it is anti-aliasing the
text. The word "Hello" does not look smoothed to me. And it looks
the same whether I include those antialias lines or not.

Am I applying the antialiasing correctly?

As far as I know, yes. But my Cairo-fu is pretty weak, I just learned enough of it to write the glue layer in wx.lib.wxcairo and to implement a GrpahicsContext-like API using it. At this point I'm leaning to this being some deeper Cairo issue, so researching in cairo maillists or forums might turn up something. Or perhaps some issue on your PC and/or your installed fonts. (For example, perhaps the font being chosen for "Sans" is not a TrueType font?)

If you send another sample app of what you've got so far I'll run it and send a screenshot so you can compare.

···

On 8/31/10 8:13 AM, C M wrote:

--
Robin Dunn
Software Craftsman

Experiment with wx.lib.wxcairo._findCairoLib. Like I implied before I'm not
100% clear on the semantics of where ctypes.CDLL and
ctypes.util.find_library look for the DLLs they load. If it can be improved
in any way I'll take a patch.

I'll try, but I know nothing about it yet.

Does it use the same function in wx.lib.wxcairo where your problem is
manifesting?

Yes. See below...

Uh, you don't see any text? It should look like the attachment. If that's
the case then I think that there is probably some deeper problem here, but
I've no clue what it might be.

The problem was that same function, wx.lib.wxcairo.FontFaceFromFont.
The code was hitting an error there and not progressing to render the
toucan image, etc. Once I modified the demo code to change that
function to the cr.select_font_face one, everything displayed fine.
Up until then, I didn't even realize I hadn't been seeing the whole
Cairo demo display.

Debug: \.\.\\\.\.\\src\\msw\\dc\.cpp\(2338\): &#39;BitBlt&#39; failed with error

0x00000057 (the parameter
is incorrect.).

That would probably be something to do with the bitmap, not necessarily just
Cairo. Can you determine where that error is coming from by commenting
out portions of the sample until the error goes away?

Removing that function, wx.lib.wxcairo.FontFaceFromFont, removes the error.

The advantage of FontFaceFromFont is that it will use the same native font
handle as the wx.Font does to make the Cairo font object, so you can be sure
that it is the same as what would have been used if you draw with a wx.DC.

Good to know. It would be nice to root out why this isn't working for me.

Am I applying the antialiasing correctly?

As far as I know, yes. But my Cairo-fu is pretty weak, I just learned
enough of it to write the glue layer in wx.lib.wxcairo and to implement a
GrpahicsContext-like API using it. At this point I'm leaning to this being
some deeper Cairo issue, so researching in cairo maillists or forums might
turn up something. Or perhaps some issue on your PC and/or your installed
fonts. (For example, perhaps the font being chosen for "Sans" is not a
TrueType font?)

I will try it on another computer and also maybe hit up the Cairo list.

If you send another sample app of what you've got so far I'll run it and
send a screenshot so you can compare.

OK, it's attached. I used words that have some curves in them to show
the small but noticeable jaggies.

Thanks,
Che

testingCairo.py (1.49 KB)

It's not quite as smooth as the Mac version (it looks like different AA algorithms are being used, perhaps due to the different native font APIs) but if you zoom in on it you can see that it is doing AA. How does yours compare?

···

On 8/31/10 11:49 AM, C M wrote:

If you send another sample app of what you've got so far I'll run it and
send a screenshot so you can compare.

OK, it's attached. I used words that have some curves in them to show
the small but noticeable jaggies.

--
Robin Dunn
Software Craftsman

It's not quite as smooth as the Mac version (it looks like different AA
algorithms are being used, perhaps due to the different native font APIs)
but if you zoom in on it you can see that it is doing AA. How does yours
compare?

My MSW version is the same as yours, I think. I'm not sure what to
conclude. For example, I saw no difference with Cairo's text whether
I set cr.set_antialias(True) or cr.set_antialias(False), so I wasn't
sure it was even doing anything.

To consider it further, I am attaching a screenshot that I've then
added to with The GIMP to show the Cairo text, the regular DC
(PaintDC) text, and then anti-aliased text from The GIMP. They are
all slightly different fonts but the point is that they are all
similarly smooth (though not as smooth as I expected). I think GIMP
wins overall but DC and Cairo do better with the "W" in "Words".

If this is the case, maybe there's no advantage to using Cairo (over a
regular DC) to get super-smooth text, since it apparently doesn't do
any better. But maybe there is more to it than that?

Is there any other processing that I could apply through Cairo to the
text to produce a more printed text-like effect?

Che

···

--
Robin Dunn
Software Craftsman
http://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

It's not quite as smooth as the Mac version (it looks like different AA
algorithms are being used, perhaps due to the different native font APIs)
but if you zoom in on it you can see that it is doing AA. How does yours
compare?

My MSW version is the same as yours, I think. I'm not sure what to
conclude. For example, I saw no difference with Cairo's text whether
I set cr.set_antialias(True) or cr.set_antialias(False), so I wasn't
sure it was even doing anything.

To consider it further, I am attaching a screenshot that I've then
added to with The GIMP to show the Cairo text, the regular DC
(PaintDC) text, and then anti-aliased text from The GIMP. They are
all slightly different fonts but the point is that they are all
similarly smooth (though not as smooth as I expected). I think GIMP
wins overall but DC and Cairo do better with the "W" in "Words".

If you zoom in on the image you can see that in all cases the letters are not rendered with just fully black or white pixels, but a mixture of shades. That tells you that AA is in fact being done. Since the Cairo and the DC images are essentially the same that tells me that Cairo is also using win32 APIs to draw the text. My guess is that GIMP is using Pango and/or FreeType.

If this is the case, maybe there's no advantage to using Cairo (over a
regular DC) to get super-smooth text, since it apparently doesn't do
any better. But maybe there is more to it than that?

Nope, see above. Although you get lots of other benefits if you're drawing anything besides text.

Is there any other processing that I could apply through Cairo to the
text to produce a more printed text-like effect?

Not that I know of but you can assume that the rendering being done with the win32 APIs (wx.DC or wx.GraphicsContext and apparently Cairo) has been optimized for use on the screen with a balance between rendering speed and AA quality, and also that it can possibly take into account the characteristics of the display such as the ordering of the RGB sub-elements of each pixel on an LCD. Obviously there are different AA algorithms that could be applied, (as shown by the GIMP text in your screenshot) but they will likely have different strengths and weaknesses, and trying to do it yourself from Python would certainly not be worth the effort.

···

On 8/31/10 4:27 PM, C M wrote:

--
Robin Dunn
Software Craftsman

Robin Dunn wrote:

Is there any other processing that I could apply through Cairo to the
text to produce a more printed text-like effect?

Not that I know of but you can assume that the rendering being done with the win32 APIs (wx.DC or wx.GraphicsContext and apparently Cairo) has been optimized for use on the screen with a balance between rendering speed and AA quality, and also that it can possibly take into account the characteristics of the display such as the ordering of the RGB sub-elements of each pixel on an LCD.

All true, however, there is much debate about whether MS has done a good job with all of that or not. In general comparing, for instance, MS to Apple, folks seem to say that Apple's approach is "prettier", and MS's perhaps easier to read on the screen.

See this discussion:

http://www.antigrain.com/research/font_rasterization/index.html

trying to do it yourself from Python would certainly not be worth the effort.

True, though if there are any functional freetype bindings that might be an option.

Easier, however, would be to see if PIL does what you want -- there is even an (optional) AGG renderer for PIL. It's pretty easy to move PIL images to wx for display -- see the wxPython Wiki for examples.

-Chris

···

On 8/31/10 4:27 PM, C M wrote:

--
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

Robin, thank you very much for all the protracted help on this topic.
I think at this point it seems that further work on this issue for me
would, as you say, not be worth the effort. If there were something
already well set up that would allow the Mac or GIMP quality AA, I'd
use it, but if not, I can live with a little less than wonderful AA in
my app or maybe think of a different approach to the GUI that obviates
the need for smooth text at that part. It really doesn't look that
bad--I was just "polishing" some of the visual aspects of what I might
do.

Che

···

On Tue, Aug 31, 2010 at 11:10 PM, Robin Dunn <robin@alldunn.com> wrote:

On 8/31/10 4:27 PM, C M wrote:

It's not quite as smooth as the Mac version (it looks like different AA
algorithms are being used, perhaps due to the different native font APIs)
but if you zoom in on it you can see that it is doing AA. How does yours
compare?

My MSW version is the same as yours, I think. I'm not sure what to
conclude. For example, I saw no difference with Cairo's text whether
I set cr.set_antialias(True) or cr.set_antialias(False), so I wasn't
sure it was even doing anything.

To consider it further, I am attaching a screenshot that I've then
added to with The GIMP to show the Cairo text, the regular DC
(PaintDC) text, and then anti-aliased text from The GIMP. They are
all slightly different fonts but the point is that they are all
similarly smooth (though not as smooth as I expected). I think GIMP
wins overall but DC and Cairo do better with the "W" in "Words".

If you zoom in on the image you can see that in all cases the letters are
not rendered with just fully black or white pixels, but a mixture of shades.
That tells you that AA is in fact being done. Since the Cairo and the DC
images are essentially the same that tells me that Cairo is also using win32
APIs to draw the text. My guess is that GIMP is using Pango and/or
FreeType.

If this is the case, maybe there's no advantage to using Cairo (over a
regular DC) to get super-smooth text, since it apparently doesn't do
any better. But maybe there is more to it than that?

Nope, see above. Although you get lots of other benefits if you're drawing
anything besides text.

Is there any other processing that I could apply through Cairo to the
text to produce a more printed text-like effect?

Not that I know of but you can assume that the rendering being done with the
win32 APIs (wx.DC or wx.GraphicsContext and apparently Cairo) has been
optimized for use on the screen with a balance between rendering speed and
AA quality, and also that it can possibly take into account the
characteristics of the display such as the ordering of the RGB sub-elements
of each pixel on an LCD. Obviously there are different AA algorithms that
could be applied, (as shown by the GIMP text in your screenshot) but they
will likely have different strengths and weaknesses, and trying to do it
yourself from Python would certainly not be worth the effort.

--
Robin Dunn
Software Craftsman
http://wxPython.org