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 text rendering
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 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 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 = new Font (); 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 double ratio = get_scale (); 191 192 iterate ((glyph, kerning, last) => { 193 double x1, y1, x2, y2; 194 double lsb; 195 196 lsb = glyph.left_limit; 197 198 if (!last) { 199 x += (glyph.get_width () + kerning) * ratio; 200 } else { 201 glyph.boundaries (out x1, out y1, out x2, out y2); 202 x += (x2 - lsb) * ratio; 203 } 204 }); 205 206 return x; 207 } 208 209 public double get_sidebearing_extent () { 210 double x ; 211 double ratio; 212 213 if (likely (sidebearing_extent > 0)) { 214 return sidebearing_extent; 215 } 216 217 x = 0; 218 ratio = get_scale (); 219 220 if (unlikely (ratio == 0)) { 221 warning ("No scale."); 222 } 223 224 iterate ((glyph, kerning, last) => { 225 double lsb; 226 lsb = glyph.left_limit; 227 x += (glyph.get_width () + kerning) * ratio; 228 }); 229 230 sidebearing_extent = x; 231 return x; 232 } 233 234 public override double get_height () { 235 return font_size; 236 } 237 238 public double get_acender () { 239 double ratio = get_scale (); 240 double max_height = 0; 241 242 iterate ((glyph, kerning, last) => { 243 double x1, y1, x2, y2; 244 double h; 245 glyph.boundaries (out x1, out y1, out x2, out y2); 246 h = Math.fmax (y1, y2) - Math.fmin (y1, y2) ; 247 if (h > max_height) { 248 max_height = h; 249 } 250 }); 251 252 return max_height * ratio - cached_font.base_line * ratio; 253 } 254 255 public override double get_width () { 256 double x = 0; 257 double ratio = get_scale (); 258 bool first = true; 259 260 iterate ((glyph, kerning, last) => { 261 double x1, y1, x2, y2; 262 double lsb; 263 264 lsb = glyph.left_limit; 265 266 if (first) { 267 glyph.boundaries (out x1, out y1, out x2, out y2); 268 x += (glyph.get_width () + kerning - Math.fmin (x1, x2)) * ratio; 269 first = false; 270 } else if (!last) { 271 x += (glyph.get_width () + kerning) * ratio; 272 } else { 273 glyph.boundaries (out x1, out y1, out x2, out y2); 274 x += (x2 - lsb) * ratio; 275 } 276 }); 277 278 return x; 279 } 280 281 public double get_decender () { 282 double ratio = get_scale (); 283 double min_y = 0; 284 double decender; 285 286 iterate ((glyph, kerning, last) => { 287 double x1, y1, x2, y2; 288 double y; 289 glyph.boundaries (out x1, out y1, out x2, out y2); 290 y = Math.fmin (y1, y2); 291 if (y < min_y) { 292 min_y = y; 293 } 294 }); 295 296 decender = cached_font.base_line * ratio - min_y * ratio; 297 return decender > 0 ? decender : 0; 298 } 299 300 public override void draw (Context cr) { 301 double descender = cached_font.bottom_limit + cached_font.base_line; 302 double y = widget_y + get_height () + get_scale () * descender; 303 draw_at_baseline (cr, widget_x, y); 304 } 305 306 public void draw_at_top (Context cr, double px, double py, string cacheid = "") { 307 double s = get_scale (); 308 double y = py + s * (cached_font.top_limit - cached_font.base_line); 309 draw_at_baseline (cr, px, y, cacheid); 310 } 311 312 public void set_source_rgba (double r, double g, double b, double a) { 313 this.r = r; 314 this.g = g; 315 this.b = b; 316 this.a = a; 317 } 318 319 public string get_cache_id (int offset_x, int offset_y) { 320 string key; 321 int64 c; 322 323 c = (((int64) (r * 255)) << 24) 324 | (((int64) (g * 255)) << 16) 325 | (((int64) (b * 255)) << 8) 326 | (((int64) (a * 255)) << 0); 327 328 // FIXME: use binary key 329 key = @"$font_size $c $offset_x $offset_y"; 330 331 return key; 332 } 333 334 public void draw_at_baseline (Context cr, double px, double py, string cacheid = "") { 335 double x, y; 336 double ratio; 337 double cc_y; 338 339 ratio = get_scale (); 340 cc_y = (cached_font.top_limit - cached_font.base_line) * ratio; 341 342 y = py; 343 x = px; 344 345 if (unlikely (cached_font.base_line != 0)) { 346 warning ("Base line not zero."); 347 } 348 349 if (use_cached_glyphs) { 350 iterate ((glyph, kerning, last) => { 351 double end; 352 353 x += kerning * ratio; 354 end = x + glyph.get_width () * ratio; 355 356 // truncation 357 if (truncated_width > 0 && end - px > truncated_width) { 358 return; 359 } 360 361 draw_chached (cr, glyph, kerning, last, x, y, cc_y, 362 ratio, cacheid); 363 364 x = end; 365 }); 366 } else { 367 iterate ((glyph, kerning, last) => { 368 double end; 369 370 x += kerning * ratio; 371 end = x + glyph.get_width () * ratio; 372 373 // truncation 374 if (truncated_width > 0 && end - px > truncated_width) { 375 return; 376 } 377 378 draw_without_cache (cr, glyph, kerning, last, x, y, cc_y, ratio); 379 x = end; 380 }); 381 } 382 } 383 384 void draw_without_cache (Context cr, Glyph glyph, double kerning, bool last, 385 double x, double y, double cc_y, double ratio) { 386 387 double lsb; 388 389 cr.save (); 390 cr.set_source_rgba (r, g, b, a); 391 cr.new_path (); 392 393 lsb = glyph.left_limit; 394 395 foreach (Path path in glyph.path_list) { 396 draw_path (cr, glyph, path, lsb, x, y, ratio); 397 } 398 399 cr.fill (); 400 cr.restore (); 401 402 } 403 404 void draw_chached (Context cr, Glyph glyph, double kerning, bool last, 405 double x, double y, double cc_y, double ratio, 406 string cacheid = "") { 407 408 double lsb; 409 Surface cache; 410 Context cc; 411 string cache_id; 412 double xp = x; 413 double yp = y - cc_y; 414 int offset_x, offset_y; 415 416 offset_x = (int) (10 * (xp - (int) xp)); 417 offset_y = (int) (10 * (yp - (int) yp)); 418 419 cache_id = (cacheid == "") ? get_cache_id (offset_x, offset_y) : cacheid; 420 421 if (unlikely (!glyph.has_cache (cache_id))) { 422 cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, (int) (glyph.get_width () * ratio) + 2, (int) font_size + 2); 423 cc = new Context (cache); 424 425 lsb = glyph.left_limit; 426 427 cc.save (); 428 cc.set_source_rgba (r, g, b, a); 429 cc.new_path (); 430 431 foreach (Path path in glyph.path_list) { 432 draw_path (cc, glyph, path, lsb, offset_x / 10.0, cc_y + offset_y / 10.0, ratio); 433 } 434 435 cc.fill (); 436 cc.restore (); 437 438 glyph.set_cache (cache_id, cache); 439 } 440 441 cr.save (); 442 cr.set_antialias (Cairo.Antialias.NONE); 443 cr.set_source_surface (glyph.get_cache (cache_id), (int) xp, (int) yp); 444 cr.paint (); 445 cr.restore (); 446 } 447 448 void draw_path (Context cr, Glyph glyph, Path path, 449 double lsb, double x, double y, double scale) { 450 451 EditPoint e, prev; 452 double xa, ya, xb, yb, xc, yc, xd, yd; 453 double by; 454 455 if (path.points.size > 0) { 456 if (unlikely (path.is_open ())) { 457 warning (@"Path is open in $(glyph.get_name ())."); 458 } 459 460 path.add_hidden_double_points (); 461 462 prev = path.points.get (path.points.size - 1); 463 xa = (prev.x - lsb) * scale + x; 464 ya = y - prev.y * scale; 465 cr.move_to (xa, ya); 466 467 by = (y - cached_font.base_line * scale); 468 for (int i = 0; i < path.points.size; i++) { 469 e = path.points.get (i).copy (); 470 PenTool.convert_point_segment_type (prev, e, PointType.CUBIC); 471 472 xb = (prev.get_right_handle ().x - lsb) * scale + x; 473 yb = by - prev.get_right_handle ().y * scale; 474 475 xc = (e.get_left_handle ().x - lsb) * scale + x; 476 yc = by - e.get_left_handle ().y * scale; 477 478 xd = (e.x - lsb) * scale + x; 479 yd = by - e.y * scale; 480 481 cr.curve_to (xb, yb, xc, yc, xd, yd); 482 cr.line_to (xd, yd); 483 484 prev = e; 485 } 486 } 487 } 488 489 public double get_baseline_to_bottom () { 490 return get_scale () * (-cached_font.base_line - cached_font.bottom_limit); 491 } 492 493 public double get_scale () { 494 return font_size / (cached_font.top_limit - cached_font.bottom_limit); 495 } 496 497 public void truncate (double max_width) { 498 truncated_width = max_width; 499 } 500 } 501 502 } 503