The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Svg.vala in libbirdfont/Svg

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/Svg/Svg.vala.
Move the SVG parser and the SVG rendering code to libsvgbird
1 /* 2 Copyright (C) 2012 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 using Cairo; 16 17 namespace BirdFont { 18 19 // FIXME: substrings 20 21 public class Svg { 22 23 /** Export to svg glyph data. */ 24 public static string to_svg_glyph (Glyph g) { 25 StringBuilder svg = new StringBuilder (); 26 PathList stroke_list; 27 28 foreach (Path p in g.get_visible_paths ()) { 29 if (p.stroke == 0) { 30 write_path_as_glyph (p, svg, g); 31 } else { 32 stroke_list = p.get_completed_stroke (); 33 write_paths_as_glyph (stroke_list, svg, g); 34 } 35 } 36 37 return svg.str; 38 } 39 40 /** Export to svg-font data. */ 41 public static string to_svg_path (Path pl, Glyph g) { 42 StringBuilder svg = new StringBuilder (); 43 pl.create_list (); 44 write_path (pl, svg, g, false); 45 return svg.str; 46 } 47 48 private static void write_paths_as_glyph (PathList pl, StringBuilder svg, Glyph g) { 49 foreach (Path p in pl.paths) { 50 write_path_as_glyph (p, svg, g); 51 } 52 } 53 54 private static void write_path_as_glyph (Path pl, StringBuilder svg, Glyph g) { 55 write_path (pl, svg, g, true); 56 } 57 58 private static void write_path (Path p, StringBuilder svg, Glyph g, bool do_glyph) { 59 int i = 0; 60 EditPoint? n = null; 61 EditPoint m; 62 63 if (p.points.size < 2) { 64 return; 65 } 66 67 p.create_list (); 68 69 foreach (var e in p.points) { 70 if (i == 0) { 71 add_abs_start (e, svg, g, do_glyph); 72 i++; 73 n = e; 74 continue; 75 } 76 77 m = (!) n; 78 79 add_abs_next (m, e, svg, g, do_glyph); 80 81 n = e; 82 i++; 83 } 84 85 if (!p.is_open ()) { 86 m = p.points.get (0); 87 add_abs_next ((!) n, m, svg, g, do_glyph); 88 close_path (svg); 89 } 90 } 91 92 private static void add_abs_next (EditPoint start, EditPoint end, StringBuilder svg, Glyph g, bool do_glyph) { 93 if (start.right_handle.type == PointType.LINE_QUADRATIC) { 94 add_abs_line_to (start, end, svg, g, do_glyph); 95 } else if (start.right_handle.type == PointType.LINE_CUBIC && end.left_handle.type == PointType.LINE_CUBIC) { 96 add_abs_line_to (start, end, svg, g, do_glyph); 97 } else if (end.left_handle.type == PointType.QUADRATIC || start.right_handle.type == PointType.QUADRATIC) { 98 add_quadratic_abs_path (start, end, svg, g, do_glyph); 99 } else if (end.left_handle.type == PointType.DOUBLE_CURVE || start.right_handle.type == PointType.DOUBLE_CURVE) { 100 add_double_quadratic_abs_path (start, end, svg, g, do_glyph); 101 } else { 102 add_cubic_abs_path (start, end, svg, g, do_glyph); 103 } 104 } 105 106 private static void add_abs_start (EditPoint ep, StringBuilder svg, Glyph g, bool to_glyph) { 107 double left = g.left_limit; 108 double baseline = -BirdFont.get_current_font ().base_line; 109 double height = g.get_height (); 110 111 svg.append_printf ("M"); 112 113 if (!to_glyph) { 114 svg.append_printf ("%s ", round (ep.x - left)); 115 svg.append_printf ("%s ", round (-ep.y + height / 2)); 116 } else { 117 svg.append_printf ("%s ", round (ep.x - left)); 118 svg.append_printf ("%s ", round (ep.y + baseline)); 119 } 120 } 121 122 private static void close_path (StringBuilder svg) { 123 svg.append ("z"); 124 } 125 126 private static void add_abs_line_to (EditPoint start, EditPoint stop, StringBuilder svg, Glyph g, bool to_glyph) { 127 double baseline = -BirdFont.get_current_font ().base_line; 128 double left = g.left_limit; 129 double height = g.get_height (); 130 131 double xa, ya, xb, yb; 132 133 Path.get_line_points (start, stop, out xa, out ya, out xb, out yb); 134 135 double center_x = Glyph.xc (); 136 double center_y = Glyph.yc (); 137 138 svg.append ("L"); 139 140 if (!to_glyph) { 141 svg.append_printf ("%s ", round (xb - center_x - left)); 142 svg.append_printf ("%s ", round (yb - center_y + height / 2)); 143 } else { 144 svg.append_printf ("%s ", round (xb - center_x - left)); 145 svg.append_printf ("%s ", round (-yb + center_y + baseline)); 146 } 147 } 148 149 private static void add_double_quadratic_abs_path (EditPoint start, EditPoint end, StringBuilder svg, Glyph g, bool to_glyph) { 150 EditPoint middle; 151 double x, y; 152 153 x = start.get_right_handle ().x + (end.get_left_handle ().x - start.get_right_handle ().x) / 2; 154 y = start.get_right_handle ().y + (end.get_left_handle ().y - start.get_right_handle ().y) / 2; 155 156 middle = new EditPoint (x, y, PointType.QUADRATIC); 157 middle.right_handle = end.get_left_handle ().copy (); 158 159 add_quadratic_abs_path (start, middle, svg, g, to_glyph); 160 add_quadratic_abs_path (middle, end, svg, g, to_glyph); 161 } 162 163 private static void add_quadratic_abs_path (EditPoint start, EditPoint end, StringBuilder svg, Glyph g, bool to_glyph) { 164 double left = g.left_limit; 165 double baseline = -BirdFont.get_current_font ().base_line; 166 double height = g.get_height (); 167 168 double xa, ya, xb, yb, xc, yc, xd, yd; 169 170 Path.get_bezier_points (start, end, out xa, out ya, out xb, out yb, out xc, out yc, out xd, out yd); 171 172 double center_x = Glyph.xc (); 173 double center_y = Glyph.yc (); 174 175 // cubic path 176 if (!to_glyph) { 177 svg.append_printf ("Q"); 178 179 svg.append_printf ("%s ", round (xb - center_x - left)); 180 svg.append_printf ("%s ", round (yb - center_y + height / 2)); 181 182 svg.append_printf ("%s ", round (xd - center_x - left)); 183 svg.append_printf ("%s ", round (yd - center_y + height / 2)); 184 185 } else { 186 svg.append_printf ("Q"); 187 188 svg.append_printf ("%s ", round (xb - center_x - left)); 189 svg.append_printf ("%s ", round (-yb + center_y + baseline)); 190 191 svg.append_printf ("%s ", round (xd - center_x - left)); 192 svg.append_printf ("%s ", round (-yd + center_y + baseline)); 193 } 194 } 195 196 private static void add_cubic_abs_path (EditPoint start, EditPoint end, StringBuilder svg, Glyph g, bool to_glyph) { 197 double left = g.left_limit; 198 double baseline = -BirdFont.get_current_font ().base_line; 199 double height = g.get_height (); 200 201 double xa, ya, xb, yb, xc, yc, xd, yd; 202 203 Path.get_bezier_points (start, end, out xa, out ya, out xb, out yb, out xc, out yc, out xd, out yd); 204 205 double center_x = Glyph.xc (); 206 double center_y = Glyph.yc (); 207 208 // cubic path 209 if (!to_glyph) { 210 svg.append_printf ("C"); 211 212 svg.append_printf ("%s ", round (xb - center_x - left)); 213 svg.append_printf ("%s ", round (yb - center_y + height / 2)); 214 215 svg.append_printf ("%s ", round (xc - center_x - left)); 216 svg.append_printf ("%s ", round (yc - center_y + height / 2)); 217 218 svg.append_printf ("%s ", round (xd - center_x - left)); 219 svg.append_printf ("%s ", round (yd - center_y + height / 2)); 220 221 } else { 222 svg.append_printf ("C"); 223 224 svg.append_printf ("%s ", round (xb - center_x - left)); 225 svg.append_printf ("%s ", round (-yb + center_y + baseline)); 226 227 svg.append_printf ("%s ", round (xc - center_x - left)); 228 svg.append_printf ("%s ", round (-yc + center_y + baseline)); 229 230 svg.append_printf ("%s ", round (xd - center_x - left)); 231 svg.append_printf ("%s ", round (-yd + center_y + baseline)); 232 } 233 } 234 235 /** Draw path from svg font data. */ 236 public static void draw_svg_path (Context cr, string svg, double x, double y) { 237 double x1, x2, x3; 238 double y1, y2, y3; 239 double px, py; 240 string[] d = svg.split (" "); 241 242 if (d.length == 0) { 243 return; 244 } 245 246 px = 0; 247 py = 0; 248 249 cr.save (); 250 251 cr.set_line_width (0); 252 253 if (svg == "") { 254 return; 255 } 256 257 for (int i = 0; i < d.length; i++) { 258 259 // trim off leading white space 260 while (d[i].index_of (" ") == 0) { 261 d[i] = d[i].substring (1); // FIXME: maybe no ascii 262 } 263 264 if (d[i].index_of ("L") == 0) { 265 x1 = double.parse (d[i].substring (1)) + x; 266 y1 = -double.parse (d[i+1]) + y; 267 cr.line_to (x1, y1); 268 269 px = x1; 270 py = y1; 271 continue; 272 } 273 274 if (d[i].index_of ("Q") == 0) { 275 x1 = double.parse (d[i].substring (1)) + x; 276 y1 = -double.parse (d[i+1]) + y; 277 278 x2 = double.parse (d[i+2]) + x; 279 y2 = -double.parse (d[i+3]) + y; 280 281 cr.curve_to ((px + 2 * x1) / 3, (py + 2 * y1) / 3, (x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3, x2, y2); 282 283 px = x2; 284 py = y2; 285 continue; 286 } 287 288 if (d[i].index_of ("C") == 0) { 289 x1 = double.parse (d[i].substring (1)) + x; 290 y1 = -double.parse (d[i+1]) + y; 291 292 x2 = double.parse (d[i+2]) + x; 293 y2 = -double.parse (d[i+3]) + y; 294 295 x3 = double.parse (d[i+4]) + x; 296 y3 = -double.parse (d[i+5]) + y; 297 298 cr.curve_to (x1, y1, x2, y2, x3, y3); 299 300 px = x3; 301 py = y3; 302 continue; 303 } 304 305 if (d[i].index_of ("M") == 0) { 306 x1 = double.parse (d[i].substring (1)) + x; 307 y1 = -double.parse (d[i+1]) + y; 308 309 cr.move_to (x1, y1); 310 311 px = x1; 312 py = y1; 313 continue; 314 } 315 316 if (d[i].index_of ("zM") == 0) { 317 cr.close_path (); 318 319 x1 = double.parse (d[i].substring (2)) + x; 320 y1 = -double.parse (d[i+1]) + y; 321 322 cr.move_to (x1, y1); 323 324 px = x1; 325 py = y1; 326 continue; 327 } 328 329 if (d[i].index_of ("z") == 0) { 330 cr.close_path (); 331 continue; 332 } 333 334 } 335 336 cr.fill (); 337 cr.restore (); 338 } 339 340 } 341 342 internal static string round (double p) { 343 string v = p.to_string (); 344 char[] c = new char [501]; 345 346 v = p.format (c, "%3.15f"); 347 348 if (v.index_of ("e") != -1) { 349 return "0.0"; 350 } 351 352 return v; 353 } 354 355 } 356