.
1 /*
2 Copyright (C) 2012, 2014 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 namespace BirdFont {
16
17 class SvgFontFormatWriter : Object {
18
19 DataOutputStream os;
20
21 public SvgFontFormatWriter () {
22 }
23
24 public void open (File file) throws Error {
25 if (file.query_exists ()) {
26 throw new FileError.EXIST ("SvgFontFormatWriter: file exists.");
27 }
28
29 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION));
30 }
31
32 public void close () throws Error {
33 os.close ();
34 }
35
36 public void write_font_file (Font font) throws Error {
37 string font_name = font.get_full_name ();
38
39 int units_per_em = 100;
40
41 int ascent = 80;
42 int descent = -20;
43
44 StringBuilder b;
45
46 Glyph? g;
47 Glyph glyph;
48 unichar index = 0;
49
50 string uni;
51
52 KerningClasses classes;
53
54 put ("""<?xml version="1.0" standalone="no"?>""");
55 put ("""<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >""");
56 put ("""<svg>""");
57
58 // (metadata goes here)
59
60 put ("<defs>");
61
62 put (@"<font id=\"$font_name\" horiz-adv-x=\"250\" >");
63 put (@"<font-face units-per-em=\"$(to_float (units_per_em))\" ascent=\"$(to_float (ascent))\" descent=\"$(to_float (descent))\" />");
64
65 // (missing-glyph goes here)
66
67 // regular glyphs
68 while (true) {
69 g = font.get_glyph_index (index++);
70
71 if (g == null) {
72 break;
73 }
74
75 glyph = (!) g;
76
77 b = new StringBuilder ();
78 b.append_unichar (glyph.get_unichar ());
79
80 if (glyph.get_unichar () >= ' ' && b.str.validate ()) {
81 if (b.str == "\"" || b.str == "&" || b.str == "<" || b.str == ">") {
82 uni = Font.to_hex_code (glyph.get_unichar ());
83 put (@"<glyph unicode=\"&#x$(uni);\" horiz-adv-x=\"$(to_float (glyph.get_width ()))\" d=\"$(glyph.get_svg_data ())\" />");
84 } else {
85 put (@"<glyph unicode=\"$(b.str)\" horiz-adv-x=\"$(to_float (glyph.get_width ()))\" d=\"$(glyph.get_svg_data ())\" />");
86 }
87 }
88 }
89
90 // FIXME: ligatures
91 classes = BirdFont.get_current_font ().get_kerning_classes ();
92 classes.all_pairs ((kerning) => {
93 string l, r;
94
95 foreach (Kerning k in kerning.kerning) {
96 try {
97 if (k.glyph != null) {
98 l = Font.to_hex_code (kerning.character.unichar_code);
99 r = Font.to_hex_code (((!)k.glyph).unichar_code);
100 os.put_string (@"<hkern u1=\"&#x$l;\" u2=\"&#x$r;\" k=\"$(to_float (-k.val))\"/>\n");
101 } else {
102 warning ("No glyph.");
103 }
104 } catch (GLib.Error e) {
105 warning (e.message);
106 }
107 }
108 });
109
110 put ("</font>");
111 put ("</defs>");
112 put ("</svg>");
113 }
114
115 string to_float (double d) {
116 string s = @"$d";
117 if (s.index_of ("e") != -1) {
118 return "0".dup ();
119 }
120 return s.replace (",", ".");
121 }
122
123 /** Write a new line */
124 private void put (string line) throws Error {
125 os.put_string (line);
126 os.put_string ("\n");
127 }
128
129 }
130
131
132 }
133