Clone in a terminal:
Commit f633dc5
Johan Mattsson - 2013-02-10T11:49:28+00:00
f633dc53a3bc47137cdffb5867298a83d6c94a91johan.mattsson.m@gmail.com

c code to load fonts with freetype2

Changed files (5)

Diff

diff --git a/bavala.py b/bavala.pyindex 598b5be..751caa2 100644--- a/bavala.py+++ b/bavala.py@@ -34,8 +34,14 @@ class Vala(object):         self.vala_deps = vala_deps or []          self.vala = list(glob.glob(src + '/*.vala'))-        self.cc = [join(build, f.replace('.vala', '.c')) for f in self.vala]-        self.obj = [join(build, f.replace('.vala', '.o')) for f in self.vala]+        self.c = list(glob.glob(src + '/*.c')) # copy regular c sources+        self.cc = [join(build, f) for f in self.c]+        self.cc += [join(build, f.replace('.vala', '.c')) for f in self.vala]+        self.obj = [f.replace('.c', '.o') for f in self.cc]+        self.obj += [join(build, f.replace('.vala', '.o')) for f in self.vala]+        +        print (self.cc)+                 if library:             self.header = join(build, library) + '.h'             self.vapi = join(build, library) + '.vapi'@@ -61,7 +67,14 @@ class Vala(object):         targets = self.cc[:]         if self.library:             targets += [self.header, self.vapi]-        return {+        +        for f in self.c:+            yield {+                'name': 'copy_c',+                'actions': [ 'cp ' + f + ' ' + self.build + '/' + self.src ],+                }+                                    +        yield {             'name': 'compile_c',             'actions': [ action ],             'file_dep': self.vala + dep_vapi,diff --git a/libbirdfont/Font.vala b/libbirdfont/Font.valaindex 255b664..c89e902 100644--- a/libbirdfont/Font.vala+++ b/libbirdfont/Font.vala@@ -18,6 +18,11 @@ using Cairo; using Xml; +enum FontFormat {+	FFI,+	SVG+}+ namespace Supplement {  public class Font : GLib.Object {@@ -68,6 +73,9 @@ public class Font : GLib.Object { 	 	public List<string> grid_width = new List<string> (); 	+	/** File format. */+	public FontFormat format = FontFormat.FFI;+	 	public Font () { 		// positions in pixels at first zoom level 		// default x-height should be 60 in 1:1diff --git a/libbirdfont/OpenFontFormatReader.vala b/libbirdfont/OpenFontFormatReader.valaindex 51de12e..1c21de9 100644--- a/libbirdfont/OpenFontFormatReader.vala+++ b/libbirdfont/OpenFontFormatReader.vala@@ -15,6 +15,9 @@     along with this program.  If not, see <http://www.gnu.org/licenses/>. */ +[CCode (cname = "load_svg_font")]+public extern static string? load_svg_font (string file, out bool error);+ namespace Supplement {  [SimpleType]diff --git a/libbirdfont/TestCases.vala b/libbirdfont/TestCases.valaindex 35b0e8b..65bd7db 100644--- a/libbirdfont/TestCases.vala+++ b/libbirdfont/TestCases.vala@@ -46,11 +46,35 @@ class TestCases { 		add (test_inkscape_import, "Inkscape import"); 		add (test_illustrator_import, "Illustrator import"); 		add (test_parse_quadratic_paths, "Quadratic paths");+		add (test_freetype, "Freetype");+	}++	public static void test_freetype () {+		string svg;+		string? data;+		bool error;+		+		data = load_svg_font ("", out error);+		+		if (error) {+			warning ("Failed to load font.");+			return;+		}+		+		if (data == null) {+			warning ("No svg data.");+			return;+		}+		+		svg = (!) data;+		+		print (@"svg: $svg\n"); 	}  	public static void test_parse_quadratic_paths () { 		Glyph g; 		Tool.test_open_next_glyph ();+		 		g = MainWindow.get_current_glyph (); 		ImportSvg.parse_svg_data ("M20,300 Q400,50 600,300 T1000,300Q1200 50 1400 300Q1600 50 1800 600 L 1800 700 L 200 700 z", g); 		Toolbox.select_tool_by_name ("full_glyph");diff --git a/libbirdfont/load_svg_font.c b/libbirdfont/load_svg_font.cnew file mode 100644index 0000000..151d9e4--- /dev/null+++ b/libbirdfont/load_svg_font.c@@ -0,0 +1,92 @@+#include <assert.h>+#include <stdio.h>+#include <string.h>+#include <stdlib.h>++#include <ft2build.h>+#include FT_FREETYPE_H+#include FT_GLYPH_H+#include FT_OPENTYPE_VALIDATE_H+#include FT_TRUETYPE_TABLES_H++#define OK 0++char* get_svg_font (FT_Face face, int* err) {+	char* svg = (char*) malloc (2 * sizeof (char));+	*err = 	OK;+	+	// TODO: parse the file.+	+	return svg;+}++int validate_font (FT_Face face) {+	// these tables can be validated+	const FT_Byte *BASE_table = NULL; +	const FT_Byte *GDEF_table = NULL;+	const FT_Byte *GPOS_table = NULL;+	const FT_Byte *GSUB_table = NULL;+	const FT_Byte *JSTF_table = NULL;+	int error = OK;++	error = FT_OpenType_Validate (face, FT_VALIDATE_BASE | FT_VALIDATE_GDEF | FT_VALIDATE_GPOS | FT_VALIDATE_GSUB | FT_VALIDATE_JSTF, &BASE_table, &GDEF_table, &GPOS_table, &GSUB_table, &JSTF_table);++	if (error) {+		printf ("Freetype validation error %d\n", error);+		return error;+	} +	+	FT_OpenType_Free (face, BASE_table);+	FT_OpenType_Free (face, GDEF_table);+	FT_OpenType_Free (face, GPOS_table);+	FT_OpenType_Free (face, GSUB_table);+	FT_OpenType_Free (face, JSTF_table);+	+	return error;+}++/** Load typeface and return the result as a SVG font. + *  Parameter err will be set to non zero vaule if an error occurs.+ */+char* load_svg_font (char* file, int* err) {+	char* svg = NULL;++	FT_Library library;+	FT_Face face;+	FT_Error error;+	FT_Glyph glyph;+	FT_UInt glyph_index;++	error = FT_Init_FreeType (&library);+	if (error != OK) {+		printf ("Freetype init error %d\n", error);+		*err = error;+		return svg;+	}++	error = FT_New_Face (library, "FONT FILE", 0, &face);+	if (error) {+		printf ("Freetype font face error %d\n", error);+		*err = error;+		return svg;+	}++	error = validate_font (face);+	if (error) {+		*err = error;+		return svg;		+	}++	svg = get_svg_font (face, error);+	if (error != OK) {+		printf ("Failed to parse font.\n");+		*err = error;+		return svg;	+	}++	FT_Done_Face ( face );+	FT_Done_FreeType( library );+	+	*err = OK;+	return svg;+}

Contact

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