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 branch 'master' of https://github.com/johanmattssonm/birdfont into HEAD
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 ImageSurface map; 21 int width; 22 23 public ClickMap (int width) { 24 this.width = width; 25 map = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, width); 26 } 27 28 public bool get_value (int x, int y) { 29 unowned uchar[] d = map.get_data (); 30 bool transparent; 31 32 if (unlikely (!(0 <= x < width && 0 <= y < width))) { 33 warning ("Array index out of bounds."); 34 return true; 35 } 36 37 transparent = d[y * map.get_stride () + 4 * x + 3] == 0; 38 39 return !transparent; 40 } 41 42 public void create_click_map (Path path) { 43 Context c; 44 45 c = new Context (map); 46 47 c.save (); 48 49 c.set_source_rgba (0, 0, 0, 1); 50 c.new_path (); 51 52 path.all_of_path ((cx, cy, ct) => { 53 int px = (int) (width * ((cx - path.xmin) / (path.xmax - path.xmin))); 54 int py = (int) (width * ((cy - path.ymin) / (path.ymax - path.ymin))); 55 56 c.line_to (px, py); 57 58 return true; 59 }, 2 * width); 60 61 c.close_path (); 62 c.fill (); 63 64 c.restore (); 65 } 66 } 67 68 } 69