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 trusty in travis
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 string path = (!) font_file; 181 182 if (!path.has_suffix("LastResort.ttf")) { 183 draw_overview_glyph (c, path, width, height, character); 184 } 185 } 186 } 187 188 c.restore (); 189 190 cache = s; 191 GlyphCanvas.redraw (); 192 } 193 } 194 195 public static void reset_label () { 196 label_background = null; 197 selected_label_background = null; 198 } 199 200 void truncate_label () { 201 double w = has_icons () ? width - 43 : width; 202 label.truncate (w); 203 } 204 205 public string get_name () { 206 StringBuilder s; 207 208 if (glyphs != null) { 209 return ((!) glyphs).get_name (); 210 } 211 212 s = new StringBuilder (); 213 s.append_unichar (character); 214 215 return s.str; 216 } 217 218 public void set_selected (bool s) { 219 selected = s; 220 } 221 222 public static double full_width () { 223 return width + margin; 224 } 225 226 public static double full_height () { 227 return height + margin; 228 } 229 230 public bool click (uint button, double px, double py) { 231 bool a; 232 GlyphCollection g; 233 bool s = (x <= px <= x + width) && (y <= py <= y + height); 234 235 if (has_icons () && glyphs != null) { 236 g = (!) glyphs; 237 version_menu.set_position (x + width - 21, y + height - 18); 238 a = version_menu.menu_item_action (px, py); // select one item on the menu 239 240 if (a) { 241 return s; 242 } 243 244 version_menu.menu_icon_action (px, py); // click in the open menu 245 } 246 247 info.set_position (x + width - 17, y + height - 22.5); 248 if (has_icons () && info.is_over_icon (px, py)) { 249 MainWindow.get_overview ().set_character_info (info); 250 } 251 252 return s; 253 } 254 255 public bool double_click (uint button, double px, double py) { 256 selected = (x <= px <= x + width) && (y <= py <= y + height); 257 return selected; 258 } 259 260 public bool is_on_screen (WidgetAllocation allocation) { 261 return y + height > 0 && y < allocation.height; 262 } 263 264 public void draw (WidgetAllocation allocation, Context cr) { 265 if (!is_on_screen (allocation)) { 266 return; 267 } 268 269 cr.save (); 270 Theme.color (cr, "Background 1"); 271 cr.rectangle (x, y, width, height); 272 cr.fill (); 273 cr.restore (); 274 275 cr.save (); 276 Theme.color (cr, "Overview Item Border"); 277 cr.rectangle (x, y, width, height); 278 cr.set_line_width (1); 279 cr.stroke (); 280 cr.restore (); 281 282 draw_thumbnail (cr, x, y + height); 283 284 draw_caption (cr); 285 draw_menu (cr); 286 } 287 288 public void adjust_scale () { 289 double x1, x2, y1, y2, glyph_width, glyph_height, scale, gx; 290 Glyph g; 291 Font font; 292 293 if (glyphs != null) { 294 font = BirdFont.get_current_font (); 295 g = ((!) glyphs).get_current (); 296 g.boundaries (out x1, out y1, out x2, out y2); 297 298 glyph_width = x2 - x1; 299 glyph_height = y2 - y1; 300 301 if (glyph_scale == 1) { 302 // caption height is 20 303 glyph_scale = (height - 20) / (font.top_limit - font.bottom_limit); 304 } 305 306 scale = glyph_scale; 307 gx = ((width / scale) - glyph_width) / 2; 308 309 if (gx < 0) { 310 glyph_scale = 1 + 2 * gx / width; 311 } 312 } 313 } 314 315 private void draw_thumbnail (Context cr, double x, double y) { 316 if (cache != null) { 317 cr.save (); 318 cr.set_antialias (Cairo.Antialias.NONE); 319 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 320 cr.set_source_surface ((!) cache, (int) (x * Screen.get_scale ()), (int) ((y - height)) * Screen.get_scale ()); 321 cr.paint (); 322 cr.restore (); 323 } 324 } 325 326 public bool has_icons () { 327 return width > 50; 328 } 329 330 public void draw_caption (Context cr) { 331 draw_label_background (cr); 332 333 cr.save (); 334 335 if (glyphs != null) { 336 if (selected) { 337 Theme.text_color (label, "Overview Selected Foreground"); 338 } else { 339 Theme.text_color (label, "Overview Foreground"); 340 } 341 342 label.draw_at_baseline (cr, x + 0.08 * width, y + height - 6); 343 } 344 345 cr.restore (); 346 } 347 348 public void create_label_background_cache (Context cr) { 349 Context cc; 350 Cairo.Pattern p; 351 Surface cache; 352 353 // unselected item 354 cache = Screen.create_background_surface ((int) width + 1, 20); 355 cc = new Context (cache); 356 cc.scale(Screen.get_scale(), Screen.get_scale()); 357 358 cc.rectangle (0, 0, width, 20 - 1); 359 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 360 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 361 cc.set_source (p); 362 363 cc.fill (); 364 365 if (has_icons ()) { 366 draw_menu_icon (cc, false); 367 draw_character_info_icon (cc); 368 } 369 370 label_background = (!) cache; 371 372 // selected item 373 cache = Screen.create_background_surface ((int) width + 1, 20); 374 cc = new Context (cache); 375 cc.scale(Screen.get_scale(), Screen.get_scale()); 376 377 cc.rectangle (0, 0, width, 20 - 1); 378 379 Theme.color (cc, "Selected Overview Item"); 380 381 cc.fill (); 382 383 if (has_icons ()) { 384 draw_menu_icon (cc, true); 385 draw_character_info_icon (cc); 386 } 387 388 selected_label_background = (!) cache; 389 390 // deselected item without menu icon 391 cache = Screen.create_background_surface ((int) width, 20); 392 cc = new Context (cache); 393 cc.scale(Screen.get_scale(), Screen.get_scale()); 394 cc.rectangle (0, 0, width - 1, 20 - 1); 395 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 396 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 397 cc.set_source (p); 398 cc.fill (); 399 400 if (has_icons ()) { 401 draw_character_info_icon (cc); 402 } 403 404 label_background_no_menu = (!) cache; 405 406 // selected item 407 cache = Screen.create_background_surface ((int) width + 1, 20); 408 cc = new Context (cache); 409 cc.scale(Screen.get_scale(), Screen.get_scale()); 410 cc.rectangle (0, 0, width, 20 - 1); 411 Theme.color (cc, "Selected Overview Item"); 412 cc.fill (); 413 414 if (has_icons ()) { 415 draw_character_info_icon (cc); 416 } 417 418 selected_label_background_no_menu = (!) cache; 419 } 420 421 bool has_menu () { 422 return glyphs != null; 423 } 424 425 public void draw_label_background (Context cr) { 426 Surface cache; 427 bool icon; 428 429 if (unlikely (label_background == null)) { 430 create_label_background_cache (cr); 431 } 432 433 if (label_background != null 434 && selected_label_background != null 435 && label_background_no_menu != null 436 && selected_label_background_no_menu != null) { 437 438 icon = has_menu (); 439 if (selected && icon) { 440 cache = (!) selected_label_background; 441 } else if (!selected && icon) { 442 cache = (!) label_background; 443 } else if (selected && !icon) { 444 cache = (!) selected_label_background_no_menu; 445 } else { 446 cache = (!) label_background_no_menu; 447 } 448 449 Screen.paint_background_surface (cr, cache, (int) x, (int) (y + height - 19)); 450 } 451 } 452 453 private void draw_character_info_icon (Context cr) { 454 info.draw_icon (cr, selected, width - 17, -1.5); 455 } 456 457 public void hide_menu () { 458 if (!is_null (version_menu)) { 459 version_menu.menu_visible = false; 460 } 461 } 462 463 private void draw_menu_icon (Context cc, bool selected) { 464 Text icon; 465 466 icon = new Text ("dropdown_menu", 17); 467 icon.load_font ("icons.bf"); 468 469 if (selected) { 470 Theme.text_color (icon, "Overview Selected Foreground"); 471 } else { 472 Theme.text_color (icon, "Overview Foreground"); 473 } 474 475 icon.draw_at_top (cc, width - 32, 0); 476 } 477 478 private void draw_menu (Context cr) { 479 if (likely (glyphs == null || !version_menu.menu_visible)) { 480 return; 481 } 482 483 version_menu.draw_menu (cr); 484 } 485 } 486 487 } 488