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