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.
Apply css styles
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 ~Doubles () { 29 delete data; 30 data = null; 31 } 32 33 public Doubles.for_capacity (int capacity) { 34 data = new PointValue[capacity]; 35 this.capacity = capacity; 36 } 37 38 void increase_capacity () { 39 int new_capacity = 2 * capacity; 40 PointValue* new_data = new PointValue[new_capacity]; 41 Posix.memcpy (new_data, data, sizeof (PointValue) * size); 42 delete data; 43 data = new_data; 44 capacity = new_capacity; 45 } 46 47 public void add_type (uchar type) { 48 if (size >= capacity) { 49 increase_capacity (); 50 } 51 52 data[size].type = type; 53 size++; 54 } 55 56 public void add (double d) { 57 if (size >= capacity) { 58 increase_capacity (); 59 } 60 61 data[size].value = d; 62 size++; 63 } 64 65 public Doubles copy () { 66 Doubles d = new Doubles (); 67 delete d.data; 68 d.data = new PointValue[capacity]; 69 d.capacity = capacity; 70 d.size = size; 71 Posix.memcpy (d.data, data, sizeof (PointValue) * size); 72 return d; 73 } 74 75 public double get_double (int index) { 76 if (unlikely (index < 0)) { 77 warning ("index < 0"); 78 return 0; 79 } 80 81 if (unlikely (index >= size)) { 82 warning ("index >= size"); 83 return 0; 84 } 85 86 return data[index].value; 87 } 88 89 public string get_string (int i) { 90 return round (get_double (i)); 91 } 92 93 public static string round (double p) { 94 string v = p.to_string (); 95 char[] c = new char [501]; 96 97 v = p.format (c, "%3.5f"); 98 99 if (v.index_of ("e") != -1) { 100 return "0.0"; 101 } 102 103 return v; 104 } 105 } 106 107 } 108 109