[cairo] Problem with cairo_win32_font_face_create_for_hfont()

Elmar Braun elmar.braun at sh-p.de
Mon Jul 30 14:12:03 PDT 2007


Hi,

I have a problem with font creation in Cairo (1.4.10) on Windows.

My code works fine if I create fonts like this:

LOGFONTW lf = { /* ... */ };
cairo_win32_font_face_create_for_logfontw(&lf);

But it does not work if I create fonts like this:

LOGFONTW lf = { /* same as above */ };
HFONT hf = CreateFontIndirectW(&lf);
cairo_win32_font_face_create_for_hfont(hf);

I'd expect both versions to create identical results. But in my tests 
the logfont version does what it's supposed to do, while the hfont 
version always results in garbage. Depending on the surface (I tried 
Win32, PDF, PostScript and SVG) the result is either a broken file, or 
at least unreadable text.

Results also depend somewhat on whether I run the program from within my 
debugger (Visual Studio 2005). I get these error messages, but only when 
debugging:

_cairo_win32_scaled_font_init_glyph_metrics:GetGlyphOutlineW: Cannot 
complete this function.
_cairo_win32_scaled_font_glyph_path: Cannot complete this function.

Debugging reveals that _cairo_win32_scaled_font_init_glyph_metrics() 
calls _cairo_win32_scaled_font_select_unscaled_font(), which calls 
_win32_scaled_font_get_unscaled_hfont(). That one invokes 
CreateFontIndirectW() using the uninitialized LOGFONTW structure 
scaled_font->logfont (filled with 0xbaadf00d, which is how the debugger 
marks malloc'd but not-yet-written-to memory).

This call to CreateFontIndirectW() does not fail, despite being passed 
garbage, but returns a seemingly valid HFONT. Cairo then uses this 
garbage HFONT, which later results in the failed GetGlyphOutlineW() call 
in _cairo_win32_scaled_font_init_glyph_metrics(). Behavior outside of 
the debugger is somewhat different because scaled_font->logfont contains 
random garbage rather than 0xbaadf00d, resulting in a different (but 
still useless) HFONT.

I have attached a small program that demonstrates the problem.

Best regards,
Elmar
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: CairoFontBug.cpp
Url: http://lists.cairographics.org/archives/cairo/attachments/20070730/6272a192/attachment.ksh 


More information about the cairo mailing list