[cairo] Using Pango and TrueType Fonts

Behdad Esfahbod behdad at behdad.org
Thu Feb 26 23:29:58 PST 2015


Ok so you are saying that some symbol fonts, like Wingdings, don't work.
That's rather expected.  Pango uses Unicode encoding only, so you would need
to encode symbol characters using their Unicode numbers instead of, eg, "Some
text".

On 15-02-26 10:42 AM, Michael Gerbracht wrote:
> In article <54EE62FA.8020602 at behdad.org>, Behdad Esfahbod <behdad at behdad.org>
> wrote:
>>> I would like to use Pango with Cairo to display data in graphs.[...] I am
>>> using a pangocairo library for lua running on windows to generate the
>>> graphics [...] First I noiced that I can display OpenType fonts but
>>> TrueType fonts are not displayed (just a crossed out rectangle is shown
>>> instead). I am using the pango_font_description_from_string method to set
>>> the font. Do I have to convert TrueType fonts first?
> 
>> What do you mean by TrueType fonts/  Give us an example.
> 
> local outfile = "C:\\font_test.svg"
> local surface = cairo.SvgSurface.create(outfile,744,526)
> local cr = cairo.Context.create(surface)
> cr:scale(1,1)
> cr:set_source_rgb(1, 1, 1)
> cr:paint()
> 
> local desc = pango.FontDescription.from_string("Wingdings regular 14")
> local text = pangocairo.create_layout(cr)
> text:set_text("Some Text", -1)
> text:set_font_description(desc)
> width, height = text:get_size()
> cr:move_to((20480-height/4)/512,526-(129536-width/4)/512)
> cr:rotate(-alpha * math.pi / 180)
> pangocairo.update_layout(cr, text)
> pangocairo.show_layout(cr, text)
> cr:rotate(alpha * math.pi / 180)
> 
> cr:destroy()
> collectgarbage()
> surface:destroy()
> 
> My problem is, that the resulting svg does not use the Wingdings font to
> display the text. So I would expect the output to show some symbols from the
> Wingdings font but instead it uses an standard font. I played around a little
> bit and found out, that all OpenType fonts are shown correctly in the output
> - also symbolic fonts. But all TrueType fonts are not shown - a default font
> is used instead.
> 
> I guess I have to use pango_win32_font. But there is no
> pango_win32_font_FontDescription_from_string methode so I expect that I need
> to access the font in several steps but unfortunately I do not know how I can
> do that. An simple example that shows the use of pango_win32_font would help.
> I know the manual page but I don't understand how to combine the commands to
> get what I want:
> https://developer.gnome.org/pango/unstable/pango-Win32-Fonts-and-Rendering.html
> 
>>> Is there a better way to get the dimension of a single character?
>> An example would help.  Try get_extents(), which returns two different
>> boxes, try both, see which one suits your need better.
> 
> Ok, thanks I will try whether this gives better results.
> 
> 
>>> Finally I would like to be able to rotate the symbol. I can do this by
>>> rotating the surface, drawing the character, and rotate back. But is this
>>> the best way to do it or would you prefer another solution?
> 
>> cairo_rotate before calling into Pango should work.
> 
> Ok, so the above code is ok regarding rotation?
> 
> thank you very much!
> Michael
> 

-- 
behdad
http://behdad.org/


More information about the cairo mailing list