The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Svg.vala in libbirdfont

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