Fixing messed-up fonts in gschem
Fixing messed-up fonts in gschem
2011
I was very happy after managing to successfully compile all the gEDA tools (PCB, gschem, gerbv, ...) on my MacBook Pro running Mac OS X 10.7.1, but then a little bit disappointed to see that gschem used a large, fixed font size for all text objects instead of applying the zoom factor. Others have discovered this issue before but I couldn’t find any work-around, other than installing MacPorts which I didn’t want to do. So I decided to spend some time to figure out what was going on.
First, I tried with geda-gaf-1.7.1 instead of 1.6.2, but this didn’t help. Then I tried other versions of Pango and Cairo and other configuration flags, but that didn’t help either. I had no other idea and thus decided to have a look at the source code. Some experimenting showed that the function setup_pango_return_metrics in o_text.c has an effect on the font size:
pango_cairo_context_set_resolution (context, 1000. * scale_factor);
font_size_pt = o_text_get_font_size_in_points (w_current->toplevel,
o_current);
desc = pango_font_description_from_string (FONT_NAME);
pango_font_description_set_size (desc, (double)PANGO_SCALE * font_size_pt);
If I passed different, smaller font sizes to pango_font_description_set_size, the text on the screen was smaller. Also, I could include the scale_factor in this call and get a somewhat working hotfix for the issue. Yet, I wasn’t happy because on other platforms, the code works as-is, so the actual “but” had to be waiting somewhere else.
It took a while to get an overview of the interesting CoreText-related classes in Pango, and somehow I was stuck and had a confusion about all the classes in Pango, like the PangoLayout, PangoContext, PangoFontSet, PangoFontMap, PangoCoreTextFontMap, PangoCairoCoreTextFontMap, PangoFont, PangoCairoCoreTextFont, PangoFontDescriptor and so on. I collected some relations between these in a class diagram and experimented a bit with the code, but still no progress.
So, I decided to set up a virtual machine with Linux and build the same packages again. This took another couple of hours, but worked fine. Text was correctly rendered in gschem, as expected. With some printf in the code and side-by-side comparison of the output, I could slowly get used to the code.
What I noticed was that the Linux version instantiated more fonts, while the Mac version re-used cached fonts. I figured out that the Linux version of Pango (i.e. the FontConfig implementation) is a bit more complex than the Mac version (i.e. the CoreText implementation). PangoFcFontMapPrivate caches fontsets, fonts, patterns and more while PangoCoreTextFontMap only caches fonts and font families.
Comparing the caching of the two implementation, I realized that the FontConfig implementation includes some FcPattern data in the hash key for the cache which contains information about the font size; the CoreText implementation only included the PostScript name of the font, but no size and resolution information.
To check if I was on the right track, I disabled the font caching in pangocoretext-fontmap.c by replacing...
best_font = pango_core_text_font_map_lookup (ctfontmap,
context,
best_description,
best_face);
if (best_font)
g_object_ref (best_font);
else
... with ...
best_font = NULL;
if (best_font)
g_object_ref (best_font);
else
This did the job! I spent many more hours to analyze the relationship between the resolution in PangoCairoContextInfo and PangoCairoFcFontmap and compare the various setter and getter functions with the CoreText counterpart and to implement the same in the CoreText code, but finally decided to try to simply add the font size and resolution to the struct _FontHashKey. At the right side, you can find the patch file. If you want to try, copy the patch file into the pango-1.29.3 directory and cd there, then run the following command:
patch -p1 -i ../pango-1.29.3-coretext.patch
And that’s how it looks now, ready for using :-)
So, high time to go to bed...
Hopefully, I won’t dream of stack traces :-P
CoreText vs. FontConfig
06.10.2011
I spent the last two week’s evenings trying to build the gEDA tools on my MacBook Pro with Mac OS X 10.7 Lion. The first big resistance came from GNU Guile, the second one from gschem, or more precisely, Pango.