The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Text.vala in libbirdfont/Renderer

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/Renderer/Text.vala.
Fix memory leak
1 /* 2 Copyright (C) 2014 2015 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 /** Test implementation of a birdfont rendering engine. */ 20 public class Text : Widget { 21 FontCache font_cache; 22 public FontCache.CachedFont cached_font; 23 24 public string text; 25 26 GlyphSequence glyph_sequence { 27 get { 28 if (gs == null) { 29 gs = generate_glyphs (); 30 } 31 32 return (!) gs; 33 } 34 } 35 36 Gee.ArrayList<string> glyph_names; 37 GlyphSequence? gs = null; 38 39 public delegate void Iterator (Glyph glyph, double kerning, bool last); 40 public double font_size; 41 public double sidebearing_extent = 0; 42 43 double r = 0; 44 double g = 0; 45 double b = 0; 46 double a = 1; 47 48 bool use_cached_glyphs = true; 49 double truncated_width = -1; 50 51 static int ntext = 0; 52 53 public Text (string text = "", double size = 17, double margin_bottom = 0) { 54 this.margin_bottom = margin_bottom; 55 font_cache = FontCache.get_default_cache (); 56 cached_font = font_cache.get_fallback (); 57 58 set_font_size (size); 59 set_text (text); 60 61 ntext++; 62 } 63 64 ~Text () { 65 ntext--; 66 } 67 68 public void use_cache (bool cache) { 69 use_cached_glyphs = cache; 70 } 71 72 /** Set font for this text area. 73 * @param font_absolute path to the font file or a file name for one of the font files in search paths. 74 * @return true if the font was found 75 */ 76 public bool load_font (string font_file) { 77 File path; 78 File f; 79 80 f = File.new_for_path (font_file); 81 path = (f.query_exists ()) ? f : SearchPaths.find_file (null, font_file); 82 83 cached_font = FontCache.get_default_cache ().get_font ((!) path.get_path ()); 84 85 return cached_font.font != null; 86 } 87 88 public void set_font_size (double height_in_pixels) { 89 font_size = height_in_pixels; 90 sidebearing_extent = 0; 91 } 92 93 public void set_font_cache (FontCache font_cache) { 94 this.font_cache = font_cache; 95 } 96 97 public void set_text (string text) { 98 this.text = text; 99 gs = null; 100 sidebearing_extent = 0; 101 } 102 103 private GlyphSequence generate_glyphs () { 104 int index; 105 unichar c; 106 string name; 107 Glyph? g; 108 GlyphSequence gs; 109 110 gs = new GlyphSequence (); 111 112 glyph_names = new Gee.ArrayList<string> (); 113 index = 0; 114 while (text.get_next_char (ref index, out c)) { 115 name = (!) c.to_string (); 116 g = cached_font.get_glyph_by_name (name); 117 118 gs.glyph.add (g); 119 glyph_names.add (name); 120 } 121 122 return gs; 123 } 124 125 public void iterate (Iterator iter) { 126 Glyph glyph; 127 double w, kern; 128 int wi; 129 Glyph? prev; 130 Glyph? g; 131 GlyphSequence word_with_ligatures; 132 GlyphRange? gr_left, gr_right; 133 GlyphSequence word; 134 KerningClasses kc; 135 Font empty = new Font (); 136 137 glyph = new Glyph.no_lines ("", '\0'); 138 139 w = 0; 140 prev = null; 141 kern = 0; 142 143 word = glyph_sequence; 144 wi = 0; 145 146 if (cached_font.font != null) { 147 word_with_ligatures = word.process_ligatures ((!) cached_font.font); 148 } else { 149 word_with_ligatures = word.process_ligatures (new Font ()); 150 } 151 152 gr_left = null; 153 gr_right = null; 154 155 if (cached_font.font != null) { 156 kc = ((!) cached_font.font).get_kerning_classes (); 157 } else { 158 kc = new KerningClasses (empty); 159 } 160 161 for (int i = 0; i < word_with_ligatures.glyph.size; i++) { 162 g = word_with_ligatures.glyph.get (i); 163 164 if (g == null || prev == null || wi == 0) { 165 kern = 0; 166 } else { 167 return_if_fail (wi < word_with_ligatures.ranges.size); 168 return_if_fail (wi - 1 >= 0); 169 170 gr_left = word_with_ligatures.ranges.get (wi - 1); 171 gr_right = word_with_ligatures.ranges.get (wi); 172 173 kern = kc.get_kerning_for_pair (((!) prev).get_name (), ((!) g).get_name (), gr_left, gr_right); 174 } 175 176 // process glyph 177 if (g == null && (0 <= i < glyph_names.size)) { 178 g = cached_font.get_glyph_by_name (glyph_names.get (i)); 179 } 180 181 glyph = (g == null) ? cached_font.get_not_def_character ().get_current () : (!) g; 182 iter (glyph, kern, i + 1 == word_with_ligatures.glyph.size); 183 prev = g; 184 wi++; 185 186 } 187 } 188 189 // FIXME: some fonts doesn't have on curve extrema 190 public double get_extent () { 191 double x = 0; 192 double ratio = get_scale (); 193 194 iterate ((glyph, kerning, last) => { 195 double x1, y1, x2, y2; 196 double lsb; 197 198 lsb = glyph.left_limit; 199 200 if (!last) { 201 x += (glyph.get_width () + kerning) * ratio; 202 } else { 203 glyph.boundaries (out x1, out y1, out x2, out y2); 204 x += (x2 - lsb) * ratio; 205 } 206 }); 207 208 return x; 209 } 210 211 public double get_sidebearing_extent () { 212 double x ; 213 double ratio; 214 215 if (likely (sidebearing_extent > 0)) { 216 return sidebearing_extent; 217 } 218 219 x = 0; 220 ratio = get_scale (); 221 222 if (unlikely (ratio == 0)) { 223 warning ("No scale."); 224 } 225 226 iterate ((glyph, kerning, last) => { 227 double lsb; 228 lsb = glyph.left_limit; 229 x += (glyph.get_width () + kerning) * ratio; 230 }); 231 232 sidebearing_extent = x; 233 return x; 234 } 235 236 public override double get_height () { 237 return font_size; 238 } 239 240 public double get_acender () { 241 double ratio = get_scale (); 242 double max_height = 0; 243 244 iterate ((glyph, kerning, last) => { 245 double x1, y1, x2, y2; 246 double h; 247 glyph.boundaries (out x1, out y1, out x2, out y2); 248 h = Math.fmax (y1, y2) - Math.fmin (y1, y2) ; 249 if (h > max_height) { 250 max_height = h; 251 } 252 }); 253 254 return max_height * ratio - cached_font.base_line * ratio; 255 } 256 257 public override double get_width () { 258 double x = 0; 259 double ratio = get_scale (); 260 bool first = true; 261 262 iterate ((glyph, kerning, last) => { 263 double x1, y1, x2, y2; 264 double lsb; 265 266 lsb = glyph.left_limit; 267 268 if (first) { 269 glyph.boundaries (out x1, out y1, out x2, out y2); 270 x += (glyph.get_width () + kerning - Math.fmin (x1, x2)) * ratio; 271 first = false; 272 } else if (!last) { 273 x += (glyph.get_width () + kerning) * ratio; 274 } else { 275 glyph.boundaries (out x1, out y1, out x2, out y2); 276 x += (x2 - lsb) * ratio; 277 } 278 }); 279 280 return x; 281 } 282 283 public double get_decender () { 284 double ratio = get_scale (); 285 double min_y = 0; 286 double decender; 287 288 iterate ((glyph, kerning, last) => { 289 double x1, y1, x2, y2; 290 double y; 291 glyph.boundaries (out x1, out y1, out x2, out y2); 292 y = Math.fmin (y1, y2); 293 if (y < min_y) { 294 min_y = y; 295 } 296 }); 297 298 decender = cached_font.base_line * ratio - min_y * ratio; 299 return decender > 0 ? decender : 0; 300 } 301 302 public override void draw (Context cr) { 303 double y = widget_y + get_height () + get_scale () * (cached_font.bottom_limit + cached_font.base_line); 304 draw_at_baseline (cr, widget_x, y); 305 } 306 307 public void draw_at_top (Context cr, double px, double py, string cacheid = "") { 308 double s = get_scale (); 309 double y = py + s * (cached_font.top_limit - cached_font.base_line); 310 draw_at_baseline (cr, px, y, cacheid); 311 } 312 313 public void set_source_rgba (double r, double g, double b, double a) { 314 this.r = r; 315 this.g = g; 316 this.b = b; 317 this.a = a; 318 } 319 320 public string get_cache_id (int offset_x, int offset_y) { 321 string key; 322 int64 c; 323 324 c = (((int64) (r * 255)) << 24) 325 | (((int64) (g * 255)) << 16) 326 | (((int64) (b * 255)) << 8) 327 | (((int64) (a * 255)) << 0); 328 329 // FIXME: use binary key 330 key = @"$font_size $c $offset_x $offset_y"; 331 332 return key; 333 } 334 335 public void draw_at_baseline (Context cr, double px, double py, string cacheid = "") { 336 double x, y; 337 double ratio; 338 double cc_y; 339 340 ratio = get_scale (); 341 cc_y = (cached_font.top_limit - cached_font.base_line) * ratio; 342 343 y = py; 344 x = px; 345 346 if (use_cached_glyphs) { 347 iterate ((glyph, kerning, last) => { 348 double end; 349 350 x += kerning * ratio; 351 end = x + glyph.get_width () * ratio; 352 353 // truncation 354 if (truncated_width > 0 && end - px > truncated_width) { 355 return; 356 } 357 358 draw_chached (cr, glyph, kerning, last, x, y, cc_y, 359 ratio, cacheid); 360 361 x = end; 362 }); 363 } else { 364 iterate ((glyph, kerning, last) => { 365 double end; 366 367 x += kerning * ratio; 368 end = x + glyph.get_width () * ratio; 369 370 // truncation 371 if (truncated_width > 0 && end - px > truncated_width) { 372 return; 373 } 374 375 draw_without_cache (cr, glyph, kerning, last, x, y, cc_y, ratio); 376 x = end; 377 }); 378 } 379 } 380 381 void draw_without_cache (Context cr, Glyph glyph, double kerning, bool last, 382 double x, double y, double cc_y, double ratio) { 383 384 double lsb; 385 386 cr.save (); 387 cr.set_source_rgba (r, g, b, a); 388 cr.new_path (); 389 390 lsb = glyph.left_limit; 391 392 foreach (Path path in glyph.path_list) { 393 draw_path (cr, path, lsb, x, y, ratio); 394 } 395 396 cr.fill (); 397 cr.restore (); 398 399 } 400 401 void draw_chached (Context cr, Glyph glyph, double kerning, bool last, 402 double x, double y, double cc_y, double ratio, 403 string cacheid = "") { 404 405 double lsb; 406 Surface cache; 407 Context cc; 408 string cache_id; 409 double xp = x; 410 double yp = y - cc_y; 411 int offset_x, offset_y; 412 413 offset_x = (int) (10 * (xp - (int) xp)); 414 offset_y = (int) (10 * (yp - (int) yp)); 415 416 cache_id = (cacheid == "") ? get_cache_id (offset_x, offset_y) : cacheid; 417 418 if (unlikely (!glyph.has_cache (cache_id))) { 419 cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, (int) (glyph.get_width () * ratio) + 2, (int) font_size + 2); 420 cc = new Context (cache); 421 422 lsb = glyph.left_limit; 423 424 cc.save (); 425 cc.set_source_rgba (r, g, b, a); 426 cc.new_path (); 427 428 foreach (Path path in glyph.path_list) { 429 draw_path (cc, path, lsb, offset_x / 10.0, cc_y + offset_y / 10.0, ratio); 430 } 431 432 cc.fill (); 433 cc.restore (); 434 435 glyph.set_cache (cache_id, cache); 436 } 437 438 cr.save (); 439 cr.set_antialias (Cairo.Antialias.NONE); 440 cr.set_source_surface (glyph.get_cache (cache_id), (int) xp, (int) yp); 441 cr.paint (); 442 cr.restore (); 443 } 444 445 void draw_path (Context cr, Path path, double lsb, double x, double y, double scale) { 446 EditPoint e, prev; 447 double xa, ya, xb, yb, xc, yc, xd, yd; 448 double by; 449 450 if (path.points.size > 0) { 451 path.add_hidden_double_points (); 452 453 prev = path.points.get (path.points.size - 1); 454 xa = (prev.x - lsb) * scale + x; 455 ya = y - prev.y * scale; 456 cr.move_to (xa, ya); 457 458 by = (y - cached_font.base_line * scale); 459 for (int i = 0; i < path.points.size; i++) { 460 e = path.points.get (i).copy (); 461 PenTool.convert_point_segment_type (prev, e, PointType.CUBIC); 462 463 xb = (prev.get_right_handle ().x - lsb) * scale + x; 464 yb = by - prev.get_right_handle ().y * scale; 465 466 xc = (e.get_left_handle ().x - lsb) * scale + x; 467 yc = by - e.get_left_handle ().y * scale; 468 469 xd = (e.x - lsb) * scale + x; 470 yd = by - e.y * scale; 471 472 cr.curve_to (xb, yb, xc, yc, xd, yd); 473 cr.line_to (xd, yd); 474 475 prev = e; 476 } 477 } 478 } 479 480 public double get_baseline_to_bottom () { 481 return get_scale () * (-cached_font.base_line - cached_font.bottom_limit); 482 } 483 484 public double get_scale () { 485 return font_size / (cached_font.top_limit - cached_font.bottom_limit); 486 } 487 488 public void truncate (double max_width) { 489 truncated_width = max_width; 490 } 491 } 492 493 } 494