The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

OverViewItem.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/OverViewItem.vala.
Fix a bug in fallback font rendering with cairo
1 /* 2 Copyright (C) 2012 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 using Math; 17 18 [CCode (cname = "draw_overview_glyph")] 19 public extern bool draw_overview_glyph (Context context, string font_file, double width, double height, unichar character); 20 21 namespace BirdFont { 22 23 public class OverViewItem : GLib.Object { 24 public unichar character = '\0'; 25 public GlyphCollection? glyphs; 26 public double x; 27 public double y; 28 public bool selected = false; 29 public CharacterInfo info; 30 31 public static double DEFAULT_WIDTH = 100; 32 public static double DEFAULT_HEIGHT = 130; 33 public static double DEFAULT_MARGIN = 20; 34 35 public static double width = 100; 36 public static double height = 130; 37 public static double margin = 20; 38 39 public static double glyph_scale = 1.0; 40 41 public VersionList version_menu; 42 Text label; 43 44 private Surface? cache = null; 45 46 public static Surface? label_background = null; 47 public static Surface? selected_label_background = null; 48 public static Surface? label_background_no_menu = null; 49 public static Surface? selected_label_background_no_menu = null; 50 51 public OverViewItem () { 52 } 53 54 public void set_character (unichar character) { 55 this.character = character; 56 } 57 58 public void set_glyphs (GlyphCollection? gc) { 59 glyphs = gc; 60 61 if (glyphs != null) { 62 version_menu = new VersionList ((!) glyphs); 63 version_menu.add_glyph_item.connect ((glyph) => { 64 ((!) glyphs).insert_glyph (glyph, true); 65 }); 66 67 version_menu.signal_delete_item.connect ((glyph_index) => { 68 OverView v = MainWindow.get_overview (); 69 version_menu = new VersionList ((!) glyphs); 70 v.update_item_list (); 71 GlyphCanvas.redraw (); 72 }); 73 } 74 75 info = new CharacterInfo (character, glyphs); 76 77 if (glyphs == null) { 78 label = new Text (); 79 } else { 80 label = new Text ((!) character.to_string (), 17); 81 truncate_label (); 82 } 83 84 draw_background (); 85 } 86 87 public void draw_glyph_from_font () { 88 if (glyphs == null) { 89 return; 90 } 91 92 Glyph g; 93 Font font; 94 double gx, gy; 95 double x1, x2, y1, y2; 96 double scale_box; 97 double w, h; 98 double glyph_width, glyph_height; 99 Surface s; 100 Context c; 101 Color color = Color.black (); 102 103 g = ((!) glyphs).get_current (); 104 105 if (g.overview_thumbnail != null) { 106 cache = g.overview_thumbnail; 107 return; 108 } 109 110 w = width; 111 h = height; 112 113 scale_box = width / DEFAULT_WIDTH; 114 115 s = Screen.create_background_surface ((int) width, (int) height - 20); 116 c = new Context (s); 117 118 c.save (); 119 g.boundaries (out x1, out y1, out x2, out y2); 120 121 glyph_width = x2 - x1; 122 glyph_height = y2 - y1; 123 124 c.save (); 125 c.scale (glyph_scale * Screen.get_scale (), glyph_scale * Screen.get_scale ()); 126 127 g.add_help_lines (); 128 129 gx = ((w / glyph_scale) - glyph_width) / 2 - g.get_left_side_bearing (); 130 gy = (h / glyph_scale) - 25 / glyph_scale; 131 132 c.translate (gx - Glyph.xc () - g.get_lsb (), g.get_baseline () + gy - Glyph.yc ()); 133 134 g.draw_paths (c, color); 135 c.restore (); 136 137 cache = s; 138 GlyphCanvas.redraw (); 139 } 140 141 public void draw_background () { 142 Glyph g; 143 Font font; 144 double gx, gy; 145 double x1, x2, y1, y2; 146 double scale_box; 147 double w, h; 148 double glyph_width, glyph_height; 149 Surface s; 150 Context c; 151 Color color = Color.black (); 152 153 w = width; 154 h = height; 155 156 scale_box = width / DEFAULT_WIDTH; 157 158 s = Screen.create_background_surface ((int) width, (int) height - 20); 159 c = new Context (s); 160 161 if (glyphs != null) { // FIXME: lock 162 draw_glyph_from_font (); 163 } else { 164 c.scale (Screen.get_scale (), Screen.get_scale ()); 165 166 c.save (); 167 168 bool glyph_found; 169 string? font_file; 170 171 Theme.color (c, "Overview Glyph"); 172 173 font_file = FontCache.fallback_font.get_default_font_file (); 174 glyph_found = draw_overview_glyph (c, (!) font_file, width, height, character); 175 176 if (!glyph_found) { 177 font_file = find_font (FallbackFont.font_config, (!) character.to_string ()); 178 179 if (font_file != null) { 180 draw_overview_glyph (c, (!) font_file, width, height, character); 181 } 182 } 183 184 c.restore (); 185 186 cache = s; 187 GlyphCanvas.redraw (); 188 } 189 } 190 191 public static void reset_label () { 192 label_background = null; 193 selected_label_background = null; 194 } 195 196 void truncate_label () { 197 double w = has_icons () ? width - 43 : width; 198 label.truncate (w); 199 } 200 201 public string get_name () { 202 StringBuilder s; 203 204 if (glyphs != null) { 205 return ((!) glyphs).get_name (); 206 } 207 208 s = new StringBuilder (); 209 s.append_unichar (character); 210 211 return s.str; 212 } 213 214 public void set_selected (bool s) { 215 selected = s; 216 } 217 218 public static double full_width () { 219 return width + margin; 220 } 221 222 public static double full_height () { 223 return height + margin; 224 } 225 226 public bool click (uint button, double px, double py) { 227 bool a; 228 GlyphCollection g; 229 bool s = (x <= px <= x + width) && (y <= py <= y + height); 230 231 if (has_icons () && glyphs != null) { 232 g = (!) glyphs; 233 version_menu.set_position (x + width - 21, y + height - 18); 234 a = version_menu.menu_item_action (px, py); // select one item on the menu 235 236 if (a) { 237 return s; 238 } 239 240 version_menu.menu_icon_action (px, py); // click in the open menu 241 } 242 243 info.set_position (x + width - 17, y + height - 22.5); 244 if (has_icons () && info.is_over_icon (px, py)) { 245 MainWindow.get_overview ().set_character_info (info); 246 } 247 248 return s; 249 } 250 251 public bool double_click (uint button, double px, double py) { 252 selected = (x <= px <= x + width) && (y <= py <= y + height); 253 return selected; 254 } 255 256 public bool is_on_screen (WidgetAllocation allocation) { 257 return y + height > 0 && y < allocation.height; 258 } 259 260 public void draw (WidgetAllocation allocation, Context cr) { 261 if (!is_on_screen (allocation)) { 262 return; 263 } 264 265 cr.save (); 266 Theme.color (cr, "Background 1"); 267 cr.rectangle (x, y, width, height); 268 cr.fill (); 269 cr.restore (); 270 271 cr.save (); 272 Theme.color (cr, "Overview Item Border"); 273 cr.rectangle (x, y, width, height); 274 cr.set_line_width (1); 275 cr.stroke (); 276 cr.restore (); 277 278 draw_caption (cr); 279 draw_menu (cr); 280 281 draw_thumbnail (cr, x, y + height); 282 } 283 284 public void adjust_scale () { 285 double x1, x2, y1, y2, glyph_width, glyph_height, scale, gx; 286 Glyph g; 287 Font font; 288 289 if (glyphs != null) { 290 font = BirdFont.get_current_font (); 291 g = ((!) glyphs).get_current (); 292 g.boundaries (out x1, out y1, out x2, out y2); 293 294 glyph_width = x2 - x1; 295 glyph_height = y2 - y1; 296 297 if (glyph_scale == 1) { 298 // caption height is 20 299 glyph_scale = (height - 20) / (font.top_limit - font.bottom_limit); 300 } 301 302 scale = glyph_scale; 303 gx = ((width / scale) - glyph_width) / 2; 304 305 if (gx < 0) { 306 glyph_scale = 1 + 2 * gx / width; 307 } 308 } 309 } 310 311 private void draw_thumbnail (Context cr, double x, double y) { 312 if (cache != null) { 313 cr.save (); 314 cr.set_antialias (Cairo.Antialias.NONE); 315 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 316 cr.set_source_surface ((!) cache, (int) (x * Screen.get_scale ()), (int) ((y - height)) * Screen.get_scale ()); 317 cr.paint (); 318 cr.restore (); 319 } 320 } 321 322 public bool has_icons () { 323 return width > 50; 324 } 325 326 public void draw_caption (Context cr) { 327 draw_label_background (cr); 328 329 cr.save (); 330 331 if (glyphs != null) { 332 if (selected) { 333 Theme.text_color (label, "Overview Selected Foreground"); 334 } else { 335 Theme.text_color (label, "Overview Foreground"); 336 } 337 338 label.draw_at_baseline (cr, x + 0.08 * width, y + height - 6); 339 } 340 341 cr.restore (); 342 } 343 344 public void create_label_background_cache (Context cr) { 345 Context cc; 346 Cairo.Pattern p; 347 Surface cache; 348 349 // unselected item 350 cache = Screen.create_background_surface ((int) width + 1, 20); 351 cc = new Context (cache); 352 cc.scale(Screen.get_scale(), Screen.get_scale()); 353 354 cc.rectangle (0, 0, width, 20 - 1); 355 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 356 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 357 cc.set_source (p); 358 359 cc.fill (); 360 361 if (has_icons ()) { 362 draw_menu_icon (cc, false); 363 draw_character_info_icon (cc); 364 } 365 366 label_background = (!) cache; 367 368 // selected item 369 cache = Screen.create_background_surface ((int) width + 1, 20); 370 cc = new Context (cache); 371 cc.scale(Screen.get_scale(), Screen.get_scale()); 372 373 cc.rectangle (0, 0, width, 20 - 1); 374 375 Theme.color (cc, "Selected Overview Item"); 376 377 cc.fill (); 378 379 if (has_icons ()) { 380 draw_menu_icon (cc, true); 381 draw_character_info_icon (cc); 382 } 383 384 selected_label_background = (!) cache; 385 386 // deselected item without menu icon 387 cache = Screen.create_background_surface ((int) width, 20); 388 cc = new Context (cache); 389 cc.scale(Screen.get_scale(), Screen.get_scale()); 390 cc.rectangle (0, 0, width - 1, 20 - 1); 391 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 392 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 393 cc.set_source (p); 394 cc.fill (); 395 396 if (has_icons ()) { 397 draw_character_info_icon (cc); 398 } 399 400 label_background_no_menu = (!) cache; 401 402 // selected item 403 cache = Screen.create_background_surface ((int) width + 1, 20); 404 cc = new Context (cache); 405 cc.scale(Screen.get_scale(), Screen.get_scale()); 406 cc.rectangle (0, 0, width, 20 - 1); 407 Theme.color (cc, "Selected Overview Item"); 408 cc.fill (); 409 410 if (has_icons ()) { 411 draw_character_info_icon (cc); 412 } 413 414 selected_label_background_no_menu = (!) cache; 415 } 416 417 bool has_menu () { 418 return glyphs != null; 419 } 420 421 public void draw_label_background (Context cr) { 422 Surface cache; 423 bool icon; 424 425 if (unlikely (label_background == null)) { 426 create_label_background_cache (cr); 427 } 428 429 if (label_background != null 430 && selected_label_background != null 431 && label_background_no_menu != null 432 && selected_label_background_no_menu != null) { 433 434 icon = has_menu (); 435 if (selected && icon) { 436 cache = (!) selected_label_background; 437 } else if (!selected && icon) { 438 cache = (!) label_background; 439 } else if (selected && !icon) { 440 cache = (!) selected_label_background_no_menu; 441 } else { 442 cache = (!) label_background_no_menu; 443 } 444 445 Screen.paint_background_surface (cr, cache, (int) x, (int) (y + height - 19)); 446 } 447 } 448 449 private void draw_character_info_icon (Context cr) { 450 info.draw_icon (cr, selected, width - 17, -1.5); 451 } 452 453 public void hide_menu () { 454 version_menu.menu_visible = false; 455 } 456 457 private void draw_menu_icon (Context cc, bool selected) { 458 Text icon; 459 460 icon = new Text ("dropdown_menu", 17); 461 icon.load_font ("icons.bf"); 462 463 if (selected) { 464 Theme.text_color (icon, "Overview Selected Foreground"); 465 } else { 466 Theme.text_color (icon, "Overview Foreground"); 467 } 468 469 icon.draw_at_top (cc, width - 32, 0); 470 } 471 472 private void draw_menu (Context cr) { 473 if (likely (glyphs == null || !version_menu.menu_visible)) { 474 return; 475 } 476 477 version_menu.draw_menu (cr); 478 } 479 } 480 481 } 482