The Birdfont Source Code


All Repositories / birdfont.git / commitdiff – RSS feed

Use Roboto as the default font for the application

These changes was commited to the Birdfont repository Sun, 24 May 2015 08:14:44 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
[Sun, 24 May 2015 08:14:44 +0000]

Updated Files

README.md
libbirdfont/Expander.vala
libbirdfont/OverView.vala
libbirdfont/Renderer/FallbackFont.vala
libbirdfont/Renderer/fontconfig.c
--- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ packages with a -dev or -devel affix: valac + font-roboto libgee-dev libglib2.0-dev libgtk-3-dev
--- a/libbirdfont/Expander.vala +++ b/libbirdfont/Expander.vala @@ -53,9 +53,6 @@ if (headline != null) { title.set_text ((!) headline); - title.use_cache (true); - Theme.text_color (title, "Text Tool Box"); - title.set_font_size (17 * Toolbox.get_scale ()); } tool = new Gee.ArrayList<Tool> (); @@ -139,6 +136,10 @@ } if (headline != null && tool.size > 0) { + Theme.text_color (title, "Text Tool Box"); + title.use_cache (true); + title.set_font_size (17 * Toolbox.get_scale ()); + yt += 17 * scale + HEADLINE_MARGIN; content_height += 17 * scale + HEADLINE_MARGIN; }
--- a/libbirdfont/OverView.vala +++ b/libbirdfont/OverView.vala @@ -46,7 +46,18 @@ Gee.ArrayList<OverViewUndoItem> redo_items = new Gee.ArrayList<OverViewUndoItem> (); /** Show all characters that has been drawn. */ - public bool all_available = true; + public bool all_available { + set { + _all_available = value; + update_item_list (); + } + + get { + return _all_available; + } + } + + bool _all_available = false; /** Show unicode database info. */ CharacterInfo? character_info = null;
--- a/libbirdfont/Renderer/FallbackFont.vala +++ b/libbirdfont/Renderer/FallbackFont.vala @@ -13,8 +13,6 @@ */ using Gee; - using Sqlite; - using Bird; [SimpleType] [CCode (has_type_id = false)] @@ -26,38 +24,120 @@ [CCode (cname = "find_font")] extern string? find_font (FcConfig* font_config, string characters); + + [CCode (cname = "find_font_file")] + extern string? find_font_file (FcConfig* font_config, string font_name); namespace BirdFont { // TODO: use font config public class FallbackFont : GLib.Object { + Gee.ArrayList<File> font_directories; + FcConfig* font_config; - public FallbackFont () { + public FallbackFont () { + string home = Environment.get_home_dir (); + + font_directories = new Gee.ArrayList<File> (); font_config = FcInitLoadConfigAndFonts (); + + add_font_folder ("/usr/share/fonts/"); + add_font_folder ("/usr/local/share/fonts/"); + add_font_folder (home + "/.local/share/fonts"); + add_font_folder (home + "/.fonts"); + add_font_folder ("C:\\Windows\\Fonts"); + //FIXME: MAC } + void add_font_folder (string f) { + File folder = File.new_for_path (f); + FileInfo? file_info; + string fn; + string file_attributes; + try { + if (folder.query_exists ()) { + font_directories.add (folder); + + file_attributes = FileAttribute.STANDARD_NAME; + file_attributes += ","; + file_attributes += FileAttribute.STANDARD_TYPE; + var enumerator = folder.enumerate_children (file_attributes, 0); + + while ((file_info = enumerator.next_file ()) != null) { + fn = ((!) file_info).get_name (); + + if (((!)file_info).get_file_type () == FileType.DIRECTORY) { + add_font_folder ((!) get_child (folder, fn).get_path ()); + } + } + } + } catch (GLib.Error e) { + warning (e.message); + } + } + + File search_font_file (string font_file) { + File d, f; + + for (int i = font_directories.size - 1; i >= 0; i--) { + d = font_directories.get (i); + f = get_child (d, font_file); + + if (f.query_exists ()) { + return f; + } + } + + warning (@"The font $font_file not found"); + return File.new_for_path (font_file); + } + public Font get_single_glyph_font (unichar c) { string? font_file; - string file; BirdFontFile bf_parser; Font bf_font; StringBuilder? glyph_data; FontFace* font; + File roboto; - font_file = find_font (font_config, (!) c.to_string ()); - + bf_font = new Font (); + font_file = null; + glyph_data = null; + + // check if glyph is available in roboto if (font_file == null) { - font_file = (!) SearchPaths.find_file (null, "Roboto-Regular.ttf").get_path (); + roboto = SearchPaths.search_file (null, "Roboto-Regular.ttf"); + + if (roboto.query_exists ()) { + font_file = (!) roboto.get_path (); + } else { + roboto = search_font_file ("Roboto-Regular.ttf"); + + if (roboto.query_exists ()) { + font_file = (!) roboto.get_path (); + } else { + font_file = find_font_file (font_config, "Roboto"); + } + } } - file = (!) font_file; - - font = open_font (file); - glyph_data = get_glyph_in_font (font, c); - close_font (font); + if (font_file != null) { + font = open_font ((!) font_file); + glyph_data = get_glyph_in_font (font, c); + close_font (font); + } + + // use fontconfig to find a fallback font + if (glyph_data == null) { + font_file = find_font (font_config, (!) c.to_string ()); + if (font_file != null) { + font = open_font ((!) 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);
--- a/libbirdfont/Renderer/fontconfig.c +++ b/libbirdfont/Renderer/fontconfig.c @@ -35,7 +35,7 @@ character_set = FcCharSetCreate (); - remaining_characters = characters; + remaining_characters = (gchar*) characters; while (TRUE) { character = g_utf8_get_char (remaining_characters); @@ -50,12 +50,13 @@ FcPatternAddCharSet (pattern, FC_CHARSET, character_set); FcCharSetDestroy (character_set); + FcPatternAddInteger (pattern, FC_SLANT, FC_SLANT_ROMAN); FcPatternAddBool(pattern, FC_SCALABLE, FcTrue); font_properties = FcObjectSetBuild (FC_FILE, NULL); fonts = FcFontList (fontconfig, pattern, font_properties); - if (fonts->nfont > 0) { + if (fonts && fonts->nfont > 0) { font = fonts->fonts[0]; if (FcPatternGetString(font, FC_FILE, 0, &path) == FcResultMatch) { result = g_strdup ((gchar*) path); @@ -64,8 +65,56 @@ if (fonts) { FcFontSetDestroy(fonts); + } + + if (pattern) { + FcPatternDestroy(pattern); } return result; + } + + /** Find a font file from its family name. + * @param font_config fontconfig instance + * @param font_name name of the font + * @return full path to the font file + */ + gchar* find_font_file (FcConfig* font_config, const gchar* font_name) { + const FcChar8* name; + FcPattern* search_pattern; + FcPattern* font; + FcChar8* file; + gchar* path; + FcObjectSet* font_properties; + FcFontSet* fonts; + int i; + + path = NULL; + name = font_name; + + search_pattern = FcPatternCreate (); + FcPatternAddString (search_pattern, FC_FAMILY, name); + FcPatternAddBool (search_pattern, FC_SCALABLE, FcTrue); + FcPatternAddInteger (search_pattern, FC_WEIGHT, FC_WEIGHT_MEDIUM); + FcPatternAddInteger (search_pattern, FC_SLANT, FC_SLANT_ROMAN); + + font_properties = FcObjectSetBuild (FC_FILE, NULL); + fonts = FcFontList (font_config, search_pattern, font_properties); + + if (fonts->nfont > 0) { + for (i = 0; i < fonts->nfont; i++) { + font = fonts->fonts[i]; + + if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) { + path = g_strdup ((gchar*) file); + break; + } + } + FcPatternDestroy (font); + } + + FcPatternDestroy (search_pattern); + + return path; }