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