Commit 05ab74c
Load fallback fonts with fontconfig
Changed files (6)
modifiedconfiguremodifieddodo.pyaddedfontconfig.vapimodifiedlibbirdfont/OverViewItem.valamodifiedlibbirdfont/Renderer/FallbackFont.valaaddedlibbirdfont/Renderer/fontconfig.c
Diff
@@ -79,7 +79,8 @@ def configure (gtk, libbgee, posixvala): 'gdk-pixbuf-2.0', 'gio-2.0', 'glib-2.0', - 'sqlite3'+ 'sqlite3',+ 'fontconfig' ] for lib in libs:@@ -48,6 +48,7 @@ LIBS = [ 'sqlite3', 'glib-2.0', 'gio-2.0',+ 'fontconfig', 'cairo', 'gdk-pixbuf-2.0', 'webkitgtk-3.0',new file mode 100644@@ -174,7 +174,6 @@ public class OverViewItem : GLib.Object { 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 @@ public class OverViewItem : GLib.Object { double glyph_width, glyph_height; Surface s; Context c;- OverView o; Color color = Color.black (); w = width;@@ -298,8 +296,6 @@ public class OverViewItem : GLib.Object { } private void draw_menu (Context cr) {- GlyphCollection g;- if (glyphs == null) { return; }@@ -16,6 +16,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 { // TODO: use font config@@ -26,12 +37,16 @@ public class FallbackFont : GLib.Object { 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 (); add_font_folder ("/usr/share/fonts/");@@ -115,6 +130,40 @@ public class FallbackFont : GLib.Object { } 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 class FallbackFont : GLib.Object { 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 class FallbackFont : GLib.Object { 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); new file mode 100644@@ -0,0 +1,70 @@+/*+ 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;+}