[cairo] transforming text

Matt Bartolome mattxbart at gmail.com
Wed Aug 5 14:02:30 PDT 2009


On Wed, Aug 5, 2009 at 1:42 PM, Behdad Esfahbod<behdad at behdad.org> wrote:
> On 08/05/2009 04:31 PM, Matt Bartolome wrote:
>>
>> On Wed, Aug 5, 2009 at 11:08 AM, Behdad Esfahbod<behdad at behdad.org>
>>  wrote:
>>>
>>> On 08/05/2009 01:47 PM, Matt Bartolome wrote:
>>>>
>>>> Hi,
>>>> I'm drawing in geographic units so I applied a transform and translate
>>>> to draw correctly in pixel space. This works fine. When I draw the
>>>> text using the same transformation it appears vertically reflected
>>>> (this is what I would expect it to do). How would you go about drawing
>>>> the text so it doesn't appear flipped, but still in the correct x,y
>>>> position and rotation? Attached is a clip of the image I'm getting,
>>>> obviously the text is wrong and is what I'm trying to correct.
>>>
>>> Hi Matt,
>>>
>>> Use a font matrix that reflects it back to normal.
>>>  cairo_set_font_matrix()
>>> for example.  It normally is just a scale the size of the font size.
>>
>> Thanks for your help behdad,
>>
>> Hmm, I'm still lost with how to use set_font_matrix. When I set it (I
>> must be setting it wrong) the output image isn't changing.
>
> cairo_set_font_size() overrides the font matrix.  What you can do is
> get_font_matrix() *after* calling set_font_size(), do a
> cairo_matrix_scale(1,-1) on it, and set it back.  Or instead of calling
> cairo_set_font_size, do a cairo_matrix_scale(fontsize,-fontsize) manually.
>

Great! It works...everything is reflected at the proper coordinates.

Thanks,
Matt

> behdad
>
>
>> # scale
>> ctx.scale(scale,scale)
>>
>> # flip y
>> matrix = cairo.Matrix(1, 0, 0, -1, 0, 0)
>> ctx.transform(matrix)
>>
>> # output from 0,0
>> ctx.translate(xmin, -ymax)
>>
>> # draw 2d profile line components
>> ctx = horizontal_center(ctx, sticks)
>> ctx = stick_head(ctx, sticks)
>> ctx = surface_elevations(ctx, sticks)
>>
>> # now draw text
>> # I would think this would be where I would flip the text somehow in
>> the current matrix so it draws in the same place but is reflected to
>> the normal position, but not sure how to do it.
>> ctx.set_font_matrix(cairo.Matrix(1, 0, 0, -1, 0, 0))
>>
>> ctx.new_path()
>> ctx.select_font_face('DejaVu Sans ExtraLight')
>> ctx.set_font_size(2)
>> ctx.set_source_rgb(0, 0, 0)
>> ctx.move_to(x, y)
>> ctx.rotate(math.atan(slope))
>> ctx.show_text("test")
>> ctx.restore()
>>
>> Thanks for your help!
>>
>>> Cheers,
>>> behdad
>>>
>>>
>>>> Hints, pointers appreciated!
>>>> Thanks,
>>>> Matt
>>>>
>>>> #bounds in geographic units
>>>> xmin, ymin, xmax, ymax = max_bounds(sticks)
>>>>
>>>> #scale factor
>>>> scale_x = width / (xmax-xmin)
>>>> scale_y = height / (ymax-ymin)
>>>> scale = min([scale_x, scale_y])
>>>>
>>>> ctx.save()
>>>> ctx.scale(scale,scale)
>>>> #flip y
>>>> matrix = cairo.Matrix(1, 0, 0, -1, 0, 0)
>>>> ctx.transform(matrix)
>>>> #output from 0,0
>>>> ctx.translate(xmin, -ymax)
>>>>
>>>> #draw 2d profile components
>>>> ctx = horizontal_center(ctx, sticks)
>>>> ctx = stick_head(ctx, sticks)
>>>> ctx = surface_elevations(ctx, sticks)
>>>> ctx.restore()
>>>>
>>>> #at this point I would like to draw the text somehow with the proper
>>>> transform, rotation etc...
>>>> ctx = draw_text(ctx,sticks)
>>>>
>>>> surface.finish()
>>>> _______________________________________________
>>>> cairo mailing list
>>>> cairo at cairographics.org
>>>> http://lists.cairographics.org/mailman/listinfo/cairo
>>>>
>>
>


More information about the cairo mailing list