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 remove_first (int n) { 39 if (size < n) { 40 return; 41 } 42 43 size -= n; 44 45 for (int i = 0; i < size; i++) { 46 data[i] = data[i + n]; 47 } 48 } 49 50 void increase_capacity () { 51 int new_capacity = 2 * capacity; 52 PointValue* new_data = new PointValue[new_capacity]; 53 Posix.memcpy (new_data, data, sizeof (PointValue) * size); 54 delete data; 55 data = new_data; 56 capacity = new_capacity; 57 } 58 59 public void add_type (uchar type) { 60 if (size >= capacity) { 61 increase_capacity (); 62 } 63 64 data[size].type = type; 65 size++; 66 } 67 68 public void add (double d) { 69 if (size >= capacity) { 70 increase_capacity (); 71 } 72 73 data[size].value = d; 74 size++; 75 } 76 77 public Doubles copy () { 78 Doubles d = new Doubles (); 79 delete d.data; 80 d.data = new PointValue[capacity]; 81 d.capacity = capacity; 82 d.size = size; 83 Posix.memcpy (d.data, data, sizeof (PointValue) * size); 84 return d; 85 } 86 87 public double get_double (int index) { 88 if (unlikely (index < 0)) { 89 warning ("index < 0"); 90 return 0; 91 } 92 93 if (unlikely (index >= size)) { 94 warning ("index >= size"); 95 return 0; 96 } 97 98 return data[index].value; 99 } 100 101 public uchar get_point_type (int index) { 102 if (unlikely (index < 0)) { 103 warning ("index < 0"); 104 return 0; 105 } 106 107 if (unlikely (index >= size)) { 108 warning ("index >= size"); 109 return 0; 110 } 111 112 return data[index].type; 113 } 114 115 public string get_string (int i) { 116 return round (get_double (i)); 117 } 118 119 public static string round (double p) { 120 string v = p.to_string (); 121 char[] c = new char [501]; 122 123 v = p.format (c, "%3.5f"); 124 125 if (v.index_of ("e") != -1) { 126 return "0.0"; 127 } 128 129 return v; 130 } 131 } 132 133 } 134 135