Clone in a terminal:
Commit 2a37fee
Johan Mattsson - 2015-05-15T16:55:07+00:00
2a37feef8ea0b7607c8efa3822c288e12305384djohan.mattsson.m@gmail.com

Create a database for fallback fonts

Changed files (3)

Diff

diff --git a/libbirdfont/BirdFontFile.vala b/libbirdfont/BirdFontFile.valaindex bfe5260..87f1bc0 100644--- a/libbirdfont/BirdFontFile.vala+++ b/libbirdfont/BirdFontFile.vala@@ -377,12 +377,12 @@ class BirdFontFile : GLib.Object { 		write_glyph_collection_start (gc, os); 		write_selected (gc, os); 		foreach (Glyph g in gc.get_version_list ().glyphs) {-			write_glyph (g, gc, os);+			write_glyph (g, os); 		} 		write_glyph_collection_end (os); 	}  -	public void write_glyph (Glyph g, GlyphCollection gc, DataOutputStream os) throws GLib.Error {+	public void write_glyph (Glyph g, DataOutputStream os) throws GLib.Error { 		string data; 		 		os.put_string (@"\t<glyph id=\"$(g.version_id)\" left=\"$(double_to_string (g.left_limit))\" right=\"$(double_to_string (g.right_limit))\">\n");@@ -390,7 +390,10 @@ class BirdFontFile : GLib.Object { 			data = get_point_data (p); 			if (data != "") { 				os.put_string (@"\t\t<path ");-				os.put_string (@"stroke=\"$(double_to_string (p.stroke))\" ");+				+				if (p.stroke != 0) {+					os.put_string (@"stroke=\"$(double_to_string (p.stroke))\" ");+				} 				 				if (p.line_cap != LineCap.BUTT) { 					if (p.line_cap == LineCap.ROUND) {diff --git a/libbirdfont/BirdFontPart.vala b/libbirdfont/BirdFontPart.valaindex 61b0e2d..9a94419 100644--- a/libbirdfont/BirdFontPart.vala+++ b/libbirdfont/BirdFontPart.vala@@ -265,7 +265,7 @@ public class BirdFontPart : GLib.Object{ 		os = create_file (@"$(file_name).bfp", "glyphs", dir_name); 		bf.write_root_tag (os); 		bf.write_glyph_collection_start (gc, os);-		bf.write_glyph (g, gc, os);+		bf.write_glyph (g, os); 		bf.write_glyph_collection_end (os); 		bf.write_closing_root_tag (os); 		os.close ();diff --git a/libbirdfont/FallbackFont.vala b/libbirdfont/FallbackFont.valanew file mode 100644index 0000000..bd4d62f--- /dev/null+++ b/libbirdfont/FallbackFont.vala@@ -0,0 +1,122 @@+/*+    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.+*/++using Gee;+using Sqlite;++namespace BirdFont {++public class FallbackFont : GLib.Object {+	public static unowned Database db;+	public static Database? database = null;++	public FallbackFont () {	+	}++	public File get_database_file () {+		return SearchPaths.find_file (null, "fallback-font.sqlite");+	}+	+	public File get_new_database_file () {+		string? fn = BirdFont.get_argument ("--fallback-font");+		+		if (fn != null && ((!) fn) != "") {+			return File.new_for_path ((!) fn);+		}+		+		return File.new_for_path ("fallback-font.sqlite");+	}+		+	public void generate_fallback_font () {+		File f = get_new_database_file ();+		string? fonts = BirdFont.get_argument ("--fonts");+		string fallback;+		+		if (fonts == null) {+			stderr.printf ("Add a list of fonts to use as fallback in the --fonts parameter.\n");+			stderr.printf ("Separate each path with \":\"\n");+			return;+		}+		+		fallback = (!) fonts;+		+		stdout.printf ("Generating fallback font: %s\n", (!) f.get_path ());+		+		try {+			if (f.query_exists ()) {+				f.delete ();+			}+			+			open_database (f);+			create_tables ();+			+			foreach (string font in fallback.split (":")) {+				add_font (font);+			}		+		} catch (GLib.Error e) {+			warning (e.message);+		}+		+	}++	public void add_font (string font_file) {+		Font font;+		Glyph g;+		BirdFontFile bf;+		DataOutputStream os;+		File single_glyph_font;+		+		font = new Font ();+		single_glyph_font = File.new_for_path ("/tmp/fallback_glyph.bf");+		+		font.set_file (font_file);+		if (!font.load ()) {+			stderr.printf ("Failed to load font: " + font_file);+			return;+		}+		+		for (int i = 0; i < font.length (); i++) {+			g = (!) font.get_glyph_indice (i);+			bf = new BirdFontFile (font);+		}+	}++	public void open_database (File db_file) {+		int rc = Database.open ((!) db_file.get_path (), out database);++		db = (!) database;++		if (rc != Sqlite.OK) {+			stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());+		}+	}++	public void create_tables () {+		int ec;+		string? errmsg;+		string create_font_table = """+			CREATE TABLE FallbackFont (+				unicode        INTEGER     PRIMARY KEY    NOT NULL,+				font_data      TEXT                       NOT NULL+			);+		""";++		ec = db.exec (create_font_table, null, out errmsg);+		if (ec != Sqlite.OK) {+			warning ("Error: %s\n", (!) errmsg);+		}+	}+}++}

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112