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

Revisions

View the latest version of libsvgbird/Doubles.vala.
SVG M instruction
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 set_double (int index, double p) { 39 if (unlikely (index < 0)) { 40 warning ("index < 0"); 41 return; 42 } 43 44 if (unlikely (index >= size)) { 45 warning ("index >= size"); 46 return; 47 } 48 49 data[index].value = p; 50 } 51 52 public void insert (int position, double p) { 53 if (capacity < size + 1) { 54 increase_capacity (); 55 } 56 57 if (unlikely (position < 0 || position > size)) { 58 warning (@"Bad poisition $position."); 59 return; 60 } 61 62 PointValue* point_data = new PointValue[capacity]; 63 64 if (position > 0) { 65 Posix.memcpy (point_data, data, sizeof (PointValue) * position); 66 } 67 68 point_data[position].value = p; 69 70 if (position < size) { 71 int dest_position = position + 1; 72 Posix.memcpy (point_data + dest_position, data + position, sizeof (PointValue) * (size - position)); 73 } 74 75 size += 1; 76 delete data; 77 data = point_data; 78 } 79 80 public void remove_first (int n) { 81 if (size < n) { 82 return; 83 } 84 85 size -= n; 86 87 for (int i = 0; i < size; i++) { 88 data[i] = data[i + n]; 89 } 90 } 91 92 93 void increase_capacity () { 94 int new_capacity = 2 * capacity; 95 PointValue* new_data = new PointValue[new_capacity]; 96 Posix.memcpy (new_data, data, sizeof (PointValue) * size); 97 delete data; 98 data = new_data; 99 capacity = new_capacity; 100 } 101 102 public void add_type (uchar type) { 103 if (size >= capacity) { 104 increase_capacity (); 105 } 106 107 data[size].type = type; 108 size++; 109 } 110 111 public void add (double d) { 112 if (size >= capacity) { 113 increase_capacity (); 114 } 115 116 data[size].value = d; 117 size++; 118 } 119 120 public Doubles copy () { 121 Doubles d = new Doubles (); 122 delete d.data; 123 d.data = new PointValue[capacity]; 124 d.capacity = capacity; 125 d.size = size; 126 Posix.memcpy (d.data, data, sizeof (PointValue) * size); 127 return d; 128 } 129 130 public double get_double (int index) { 131 if (unlikely (index < 0)) { 132 warning ("index < 0"); 133 return 0; 134 } 135 136 if (unlikely (index >= size)) { 137 warning ("index >= size"); 138 return 0; 139 } 140 141 return data[index].value; 142 } 143 144 public uchar get_point_type (int index) { 145 if (unlikely (index < 0)) { 146 warning ("index < 0"); 147 return 0; 148 } 149 150 if (unlikely (index >= size)) { 151 warning ("index >= size"); 152 return 0; 153 } 154 155 return data[index].type; 156 } 157 158 public string get_string (int i) { 159 return round (get_double (i)); 160 } 161 162 public static string round (double p) { 163 string v = p.to_string (); 164 char[] c = new char [501]; 165 166 v = p.format (c, "%3.5f"); 167 168 if (v.index_of ("e") != -1) { 169 return "0.0"; 170 } 171 172 return v; 173 } 174 } 175 176 } 177 178