The Birdfont Source Code


All Repositories / birdfont.git / commitdiff – RSS feed

Load fallback fonts with fontconfig

These changes was commited to the Birdfont repository Sat, 23 May 2015 19:45:54 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
[Sat, 23 May 2015 19:45:54 +0000]

Updated Files

configure
dodo.py
fontconfig.vapi
libbirdfont/OverViewItem.vala
libbirdfont/Renderer/FallbackFont.vala
libbirdfont/Renderer/fontconfig.c
--- a/configure +++ b/configure @@ -79,7 +79,8 @@ 'gdk-pixbuf-2.0', 'gio-2.0', 'glib-2.0', - 'sqlite3' + 'sqlite3', + 'fontconfig' ] for lib in libs:
diff --git a/dodo.py b/dodo.py
--- a/dodo.py +++ b/dodo.py @@ -48,6 +48,7 @@ 'sqlite3', 'glib-2.0', 'gio-2.0', + 'fontconfig', 'cairo', 'gdk-pixbuf-2.0', 'webkitgtk-3.0',
diff --git fontconfig.vapi(new)
--- /dev/null +++ b/fontconfig.vapi
--- a/libbirdfont/OverViewItem.vala +++ b/libbirdfont/OverViewItem.vala @@ -174,7 +174,6 @@ private void draw_thumbnail (Context cr, GlyphCollection? gl, double x, double y) { Glyph g; - Glyph? glyph; Font font; double gx, gy; double x1, x2, y1, y2; @@ -183,7 +182,6 @@ double glyph_width, glyph_height; Surface s; Context c; - OverView o; Color color = Color.black (); w = width; @@ -298,8 +296,6 @@ } private void draw_menu (Context cr) { - GlyphCollection g; - if (glyphs == null) { return; }
--- a/libbirdfont/Renderer/FallbackFont.vala +++ b/libbirdfont/Renderer/FallbackFont.vala @@ -15,6 +15,17 @@ using Gee; using Sqlite; using Bird; + + [SimpleType] + [CCode (has_type_id = false)] + extern struct FcConfig { + } + + [CCode (cname = "FcInitLoadConfigAndFonts")] + extern FcConfig* FcInitLoadConfigAndFonts (); + + [CCode (cname = "find_font")] + extern string? find_font (FcConfig* font_config, string characters); namespace BirdFont { @@ -25,12 +36,16 @@ Gee.ArrayList<File> fallback_fonts; Gee.ArrayList<File> font_directories; + + FcConfig* font_config; public FallbackFont () { string home = Environment.get_home_dir (); fallback_fonts = new Gee.ArrayList<File> (); font_directories = new Gee.ArrayList<File> (); + + font_config = FcInitLoadConfigAndFonts (); open_database (); @@ -115,6 +130,40 @@ } public Font get_single_glyph_font (unichar c) { + string? font_file; + string file; + BirdFontFile bf_parser; + Font bf_font; + StringBuilder? glyph_data; + FontFace* font; + + font_file = find_font (font_config, (!) c.to_string ()); + + if (font_file == null) { + warning ("No font returned from fontconfig."); + return get_single_glyph_font_without_font_config (c); + } + + file = (!) font_file; + print (@"font_file: $(file)\n"); + + print (@"Load from TTF $((!) c.to_string ())\n"); + + font = open_font (file); + glyph_data = get_glyph_in_font (font, c); + close_font (font); + + bf_font = new Font (); + if (glyph_data != null) { + bf_parser = new BirdFontFile (bf_font); + bf_parser.load_data (((!) glyph_data).str); + } + + return bf_font; + } + + // FIXME: remove after fixing the windows version + public Font get_single_glyph_font_without_font_config (unichar c) { BirdFontFile bf_parser; Font bf_font; StringBuilder? glyph_data; @@ -143,7 +192,6 @@ public StringBuilder? load_glyph_from_ttf (unichar c) { StringBuilder? glyph_data; - string data; glyph_data = load_glyph_data_from_ttf (c); @@ -179,8 +227,7 @@ public StringBuilder? get_glyph_in_font (FontFace font, unichar c) { StringBuilder? glyph_data = null; GlyphCollection gc; - Font bf_font = new Font (); - + gc = new GlyphCollection (c, (!)c.to_string ()); glyph_data = load_glyph (font, (uint) c);
--- /dev/null +++ b/libbirdfont/Renderer/fontconfig.c @@ -1,1 +1,71 @@ + /* + Copyright (C) 2015 Johan Mattsson + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + */ + + #include <stdio.h> + #include <glib.h> + #include <fontconfig/fontconfig.h> + + /** Find a fallback font for a set of characters. + * @return A path to the font file. + */ + gchar* find_font (FcConfig* fontconfig, const gchar* characters) { + FcPattern* pattern; + FcCharSet* character_set; + FcObjectSet* font_properties; + FcFontSet* fonts; + FcPattern* font; + FcChar8* path; + gchar* result; + gchar* remaining_characters; + gunichar character; + + result = NULL; + pattern = FcPatternCreate (); + + character_set = FcCharSetCreate (); + + remaining_characters = characters; + while (TRUE) { + character = g_utf8_get_char (remaining_characters); + + if (character == '\0') { + break; + } + + FcCharSetAddChar(character_set, character); + + remaining_characters = g_utf8_next_char (remaining_characters); + } + + FcPatternAddCharSet (pattern, FC_CHARSET, character_set); + FcCharSetDestroy (character_set); + + FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); + font_properties = FcObjectSetBuild (FC_FILE, NULL); + fonts = FcFontList (fontconfig, pattern, font_properties); + + if (fonts->nfont > 0) { + font = fonts->fonts[0]; + if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch) { + result = g_strdup ((gchar*) path); + } + } + + if (fonts) { + FcFontSetDestroy(fonts); + } + + return result; + }