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