cairo+MemoryDC+bitmap saving

23 views
Skip to first unread message

King

unread,
Dec 14, 2009, 9:19:36 AM12/14/09
to wxPython-users
Hi,
I am getting my hands on cairo for the first time. Rendering quality
is good but api is pretty ugly.
Any way, Here I am trying to render a text and saving as bitmap. The
text is not getting rendered on the bitmap.

mdc = wx.MemoryDC()
ctx = wx.lib.wxcairo.ContextFromDC(mdc)

face = wx.lib.wxcairo.FontFaceFromFont(\
wx.FFont(10, wx.SWISS, wx.FONTFLAG_BOLD))
ctx.set_font_face(face)
ctx.set_font_size(20.0)
x_bearing, y_bearing, self.width, self.height,\
x_advance, y_advance = ctx.text_extents(text)
self.size = self.width, self.height
self.colorBitmap = wx.EmptyBitmapRGBA(self.size[0], self.size
[1],\
0, 0, 0, 0)

mdc.SelectObject(self.colorBitmap)
mdc.Clear()
#ctx.move_to(0, 0)
ctx.text_path(text)
ctx.set_source_rgb(1.0, 0.0, 0.0)
ctx.fill_preserve()

self.colorBitmap is coming out as a white colored rectangle of text
size but not text is getting rendered.

Prashant
Python 2.6.2
wxPython 2.8.10.1
win XP 32

Robin Dunn

unread,
Dec 15, 2009, 1:25:18 PM12/15/09
to wxpytho...@googlegroups.com
You may need to delay creating the context (or recreate it) until after
the bitmap has been selected into the DC. The DC won't be fully valid
until it has a bitmap selected into it, so ContextFromDC may be failing.
Also, you may want to try using show_text instead of text_path.

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

Robin Dunn

unread,
Dec 15, 2009, 1:31:17 PM12/15/09
to wxpytho...@googlegroups.com
On 12/14/09 6:19 AM, King wrote:
> Hi,
> I am getting my hands on cairo for the first time. Rendering quality
> is good but api is pretty ugly.

BTW, you may want to look at wx.lib.graphics. It is a
wx.GraphcsContext-like (IOW, mostly compatible) implantation done in
Python code that uses Cairo on all the platforms. The main thing that
it does for you is allowing you to avoid the ugly cairo API, but still
be able to use the same backend on all platforms. You can still use
Cairo APIs if needed.

In some simple benchmarks I did a while back it's lots faster than
wx.GraphicsContext on Windows since it is not using GDI+. On GTK and
Mac it's a little slower than wx.GraphicsContext, but not by very much.

King

unread,
Dec 15, 2009, 1:50:13 PM12/15/09
to wxPython-users

> BTW, you may want to look at wx.lib.graphics.
Are there any examples?

>
> In some simple benchmarks I did a while back it's lots faster than
> wx.GraphicsContext on Windows since it is not using GDI+.  On GTK and
> Mac it's a little slower than wx.GraphicsContext, but not by very much.

The application I am writing is heavily used wxGraphicContext. The
simple reason
is rendering quality offered by it compare to other DCs. Although
cairo rendering
is much better but I am a bit unaware how perfectly cairo+wxPython are
glued on Linux and Mac.

Robin Dunn

unread,
Dec 15, 2009, 2:08:25 PM12/15/09
to wxpytho...@googlegroups.com
On 12/15/09 10:50 AM, King wrote:
>
>> BTW, you may want to look at wx.lib.graphics.
> Are there any examples?

There are some simple tests in the sandbox folder in the source
repository. See the test_cairoContext*.py files in
http://trac.wxwidgets.org/browser/wxPython/branches/WX_2_8_BRANCH/sandbox.
There is also a test_cairo.py script there that tests using the basic
Cairo APIs from wxPython.

King

unread,
Dec 16, 2009, 11:53:50 AM12/16/09
to wxPython-users

Thanks for providing a nicer interface for cairo graphics. It is glued
without into my application without
any problems.

Just one question.
To draw antialised text in cario graphics, you can do:

ctx.text_path("World")
ctx.set_source_rgb(0.39, 0.07, 0.78)
ctx.fill_preserve()

How do you do this using wx.lib.graphics? or may be a new function
DrawAAText(text, x, y, backgroundbrush).

I tried using this:
def DrawAAText(self, text, x, y):
self.PushState()
self.Translate(x, y)
self._context.text_path(text)
self.SetBrush(self._brush)
self._context.fill_preserve()
self.PopState()

Although it's rendering the text but not at correct position and does
not render
as per the font that has been assigned earlier using SetFont method.

In simple words draw exactly like DrawText but aniti-alised using
path.

Prashant
Python 2.6.

Robin Dunn

unread,
Dec 16, 2009, 3:50:46 PM12/16/09
to wxpytho...@googlegroups.com
On 12/16/09 8:53 AM, King wrote:
>
> Thanks for providing a nicer interface for cairo graphics. It is glued
> without into my application without
> any problems.
>
> Just one question.
> To draw antialised text in cario graphics, you can do:
>
> ctx.text_path("World")
> ctx.set_source_rgb(0.39, 0.07, 0.78)
> ctx.fill_preserve()
>
> How do you do this using wx.lib.graphics?

DrawText should be drawing the text in anti-alias mode, or at least with
the same anti-alias setting that it is using for everything else. See
the attached screenshot of part of one of the samples I pointed you to
yesterday, along with a zoom in on the text. If you're not getting the
same results then something is wrong.

> Although it's rendering the text but not at correct position and does
> not render
> as per the font that has been assigned earlier using SetFont method.

See the implementation of DrawText for what has to be done to apply the
font and to offset the text origin correctly. You have the source code,
use it.
Snap0001.png
Reply all
Reply to author
Forward
0 new messages