The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Doubles.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/Doubles.vala.
Fix resize tool
1 /* 2 Copyright (C) 2015 2019 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 Doubles : GLib.Object { 20 public double* data; 21 public int size = 0; 22 int capacity = 10; 23 24 public Doubles () { 25 data = new double[capacity]; 26 } 27 28 public Doubles.for_capacity (int capacity) { 29 data = new double[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] = p; 54 } 55 56 public void insert (int index, double p) { 57 insert_element (index); 58 data[index] = p; 59 } 60 61 public void insert_element (int index) { 62 if (capacity < size + 1) { 63 increase_capacity (); 64 } 65 66 if (unlikely (index < 0 || index > size)) { 67 warning (@"Bad index $index."); 68 return; 69 } 70 71 double* point_data = new double[capacity]; 72 73 if (index > 0) { 74 Posix.memcpy (point_data, data, sizeof (double) * index); 75 } 76 77 if (index < size) { 78 int dest_position = index + 1; 79 Posix.memcpy (point_data + dest_position, data + index, sizeof (double) * (size - index)); 80 } 81 82 size += 1; 83 delete data; 84 data = point_data; 85 } 86 87 public void remove_first (int n) { 88 if (size < n) { 89 return; 90 } 91 92 size -= n; 93 94 for (int i = 0; i < size; i++) { 95 data[i] = data[i + n]; 96 } 97 } 98 99 public void remove (int offset, int length) { 100 if (unlikely (offset < 0 || offset + length > size)) { 101 warning (@"Invalid offset: $offset, length: $length, size: $size"); 102 return; 103 } 104 105 for (int i = offset; i < size; i++) { 106 data[i] = data[i + length]; 107 } 108 109 size -= length; 110 } 111 112 void increase_capacity () { 113 int new_capacity = 2 * capacity; 114 double* new_data = new double[new_capacity]; 115 Posix.memcpy (new_data, data, sizeof (double) * size); 116 delete data; 117 data = new_data; 118 capacity = new_capacity; 119 } 120 121 public void add (double d) { 122 if (size >= capacity) { 123 increase_capacity (); 124 } 125 126 data[size] = d; 127 size++; 128 } 129 130 public Doubles copy () { 131 Doubles d = new Doubles (); 132 delete d.data; 133 d.data = new double[capacity]; 134 d.capacity = capacity; 135 d.size = size; 136 Posix.memcpy (d.data, data, sizeof (double) * size); 137 return d; 138 } 139 140 public double get_double (int index) { 141 if (unlikely (index < 0)) { 142 warning ("index < 0"); 143 return 0; 144 } 145 146 if (unlikely (index >= size)) { 147 warning ("index >= size"); 148 return 0; 149 } 150 151 return data[index]; 152 } 153 154 155 public string get_string (int i) { 156 return round (get_double (i)); 157 } 158 159 public static string round (double p) { 160 string v = p.to_string (); 161 char[] c = new char [501]; 162 163 v = p.format (c, "%3.5f"); 164 165 if (v.index_of ("e") != -1) { 166 return "0.0"; 167 } 168 169 return v; 170 } 171 } 172 173 } 174 175