The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ClickMap.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/ClickMap.vala.
Merge stroke parts
1 /* 2 Copyright (C) 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 17 namespace BirdFont { 18 19 public class ClickMap : GLib.Object { 20 public ImageSurface map; 21 public int width; 22 23 public double xmax; 24 public double ymax; 25 public double xmin; 26 public double ymin; 27 28 public ClickMap (int width) { 29 this.width = width; 30 map = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, width); 31 xmax = 0; 32 ymax = 0; 33 xmin = 0; 34 ymin = 0; 35 } 36 37 public bool get_value (int x, int y) { 38 unowned uchar[] d = map.get_data (); 39 bool transparent; 40 41 Context c; 42 43 c = new Context (map); 44 c.set_fill_rule (Cairo.FillRule.WINDING); 45 46 if (unlikely (!(0 <= x < width && 0 <= y < width))) { 47 warning (@"Array index out of bounds. x: $x y: $y size: $width"); 48 return true; 49 } 50 51 transparent = d[y * map.get_stride () + 4 * x + 3] == 0; 52 53 return !transparent; 54 } 55 56 bool add_point (Context c, double cx, double cy) { 57 int px = (int) (width * ((cx - xmin) / (xmax - xmin))); 58 int py = (int) (width * ((cy - ymin) / (ymax - ymin))); 59 c.line_to (px, py); 60 return true; 61 } 62 63 public void create_click_map (Path path, Path? counter = null) { // FIXME: clean up 64 Path p; 65 Context c; 66 67 c = new Context (map); 68 69 if (counter == null) { 70 xmax = path.xmax; 71 ymax = path.ymax; 72 xmin = path.xmin; 73 ymin = path.ymin; 74 } else { 75 p = (!) counter; 76 77 xmax = fmax (p.xmax, path.xmax); 78 ymax = fmax (p.ymax, path.ymax); 79 xmin = fmin (p.xmin, path.xmin); 80 ymin = fmin (p.ymin, path.ymin); 81 } 82 83 c.save (); 84 c.set_fill_rule (FillRule.EVEN_ODD); 85 c.set_source_rgba (0, 0, 0, 1); 86 c.new_path (); 87 path.all_of_path ((cx, cy) => { 88 add_point (c, cx, cy); 89 return true; 90 }, 2 * width); 91 c.close_path (); 92 93 if (counter != null) { 94 c.new_path (); 95 96 p = (!) counter; 97 p.all_of_path ((cx, cy) => { 98 add_point (c, cx, cy); 99 return true; 100 }, 2 * width); 101 102 c.close_path (); 103 } 104 105 c.fill (); 106 c.restore (); 107 } 108 } 109 110 static double fmin (double a, double b) { 111 return a < b ? a : b; 112 } 113 114 static double fmax (double a, double b) { 115 return a > b ? a : b; 116 } 117 118 } 119