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