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