Commit f633dc5
c code to load fonts with freetype2
Changed files (5)
modifiedbavala.pymodifiedlibbirdfont/Font.valamodifiedlibbirdfont/OpenFontFormatReader.valamodifiedlibbirdfont/TestCases.valaaddedlibbirdfont/load_svg_font.c
Diff
@@ -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,@@ -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:1@@ -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]@@ -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");new file mode 100644@@ -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;+}