cairo+MemoryDC+bitmap saving

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

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.

···

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

--
Robin Dunn
Software Craftsman

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.

···

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.

--
Robin Dunn
Software Craftsman

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.

Prashant
Python 2.6.2
wxPython 2.8.10.1

There are some simple tests in the sandbox folder in the source repository. See the test_cairoContext*.py files in wxTrac has been migrated to GitHub Issues - wxWidgets. There is also a test_cairo.py script there that tests using the basic Cairo APIs from wxPython.

···

On 12/15/09 10:50 AM, King wrote:

BTW, you may want to look at wx.lib.graphics.

Are there any examples?

--
Robin Dunn
Software Craftsman

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.

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.

···

On 12/16/09 8:53 AM, King wrote:

--
Robin Dunn
Software Craftsman