The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Doubles.vala in /libsvgbird

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
Circle boundaries heads/master
1 /* 2 Copyright (C) 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 SvgBird { 18 19 public class Doubles : GLib.Object { 20 public PointValue* data; 21 public int size = 0; 22 int capacity = 10; 23 24 public Doubles () { 25 data = new PointValue[capacity]; 26 } 27 28 public Doubles.for_capacity (int capacity) { 29 data = new PointValue[capacity]; 30 this.capacity = capacity; 31 } 32 33 ~Doubles () { 34 delete data; 35 data = null; 36 } 37 38 public void clear () { 39 size = 0; 40 } 41 42 public void set_double (int index, double p) { 43 if (unlikely (index < 0)) { 44 warning ("index < 0"); 45 return; 46 } 47 48 if (unlikely (index >= size)) { 49 warning ("index >= size"); 50 return; 51 } 52 53 data[index].value = p; 54 } 55 56 public void set_type (int index, uint32 t) { 57 if (unlikely (index < 0)) { 58 warning ("index < 0"); 59 return; 60 } 61 62 if (unlikely (index >= size)) { 63 warning ("index >= size"); 64 return; 65 } 66 67 data[index].type = t; 68 } 69 70 public void insert (int index, double p) { 71 insert_element (index); 72 data[index].value = p; 73 } 74 75 public void insert_type (int index, uint32 p) { 76 insert_element (index); 77 data[index].type = p; 78 } 79 80 public void insert_element (int index) { 81 if (capacity < size + 1) { 82 increase_capacity (); 83 } 84 85 if (unlikely (index < 0 || index > size)) { 86 warning (@"Bad index $index."); 87 return; 88 } 89 90 PointValue* point_data = new PointValue[capacity]; 91 92 if (index > 0) { 93 Posix.memcpy (point_data, data, sizeof (PointValue) * index); 94 } 95 96 if (index < size) { 97 int dest_position = index + 1; 98 Posix.memcpy (point_data + dest_position, data + index, sizeof (PointValue) * (size - index)); 99 } 100 101 size += 1; 102 delete data; 103 data = point_data; 104 } 105 106 public void remove_first (int n) { 107 if (size < n) { 108 return; 109 } 110 111 size -= n; 112 113 for (int i = 0; i < size; i++) { 114 data[i] = data[i + n]; 115 } 116 } 117 118 public void remove (int offset, int length) { 119 if (unlikely (offset < 0 || offset + length > size)) { 120 warning (@"Invalid offset: $offset, length: $length, size: $size"); 121 return; 122 } 123 124 for (int i = offset; i < size; i++) { 125 data[i] = data[i + length]; 126 } 127 128 size -= length; 129 } 130 131 void increase_capacity () { 132 int new_capacity = 2 * capacity; 133 PointValue* new_data = new PointValue[new_capacity]; 134 Posix.memcpy (new_data, data, sizeof (PointValue) * size); 135 delete data; 136 data = new_data; 137 capacity = new_capacity; 138 } 139 140 public void add_type (uint32 type) { 141 if (size >= capacity) { 142 increase_capacity (); 143 } 144 145 data[size].type = type; 146 size++; 147 } 148 149 public void add (double d) { 150 if (size >= capacity) { 151 increase_capacity (); 152 } 153 154 data[size].value = d; 155 size++; 156 } 157 158 public Doubles copy () { 159 Doubles d = new Doubles (); 160 delete d.data; 161 d.data = new PointValue[capacity]; 162 d.capacity = capacity; 163 d.size = size; 164 Posix.memcpy (d.data, data, sizeof (PointValue) * size); 165 return d; 166 } 167 168 public double get_double (int index) { 169 if (unlikely (index < 0)) { 170 warning ("index < 0"); 171 return 0; 172 } 173 174 if (unlikely (index >= size)) { 175 warning ("index >= size"); 176 return 0; 177 } 178 179 return data[index].value; 180 } 181 182 public uint32 get_point_type (int index) { 183 if (unlikely (index < 0)) { 184 warning ("index < 0"); 185 return 0; 186 } 187 188 if (unlikely (index >= size)) { 189 warning ("index >= size"); 190 return 0; 191 } 192 193 return data[index].type; 194 } 195 196 public void set_point_type (int index, uint32 type) { 197 if (unlikely (index < 0)) { 198 warning ("index < 0"); 199 return; 200 } 201 202 if (unlikely (index >= size)) { 203 warning ("index >= size"); 204 return; 205 } 206 207 data[index].type = type; 208 } 209 210 public string get_string (int i) { 211 return round (get_double (i)); 212 } 213 214 public static string round (double p) { 215 string v = p.to_string (); 216 char[] c = new char [501]; 217 218 v = p.format (c, "%3.5f"); 219 220 if (v.index_of ("e") != -1) { 221 return "0.0"; 222 } 223 224 return v; 225 } 226 } 227 228 } 229 230