The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgTransforms.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/SvgTransforms.vala.
Apply css styles
1 /* 2 Copyright (C) 2016 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 SvgTransforms : GLib.Object { 20 public Matrix rotation_matrix; 21 public Matrix size_matrix; 22 public Gee.ArrayList<SvgTransform> transforms; 23 24 public double rotation = 0; 25 public double total_rotation = 0; 26 public double scale_x = 1; 27 public double scale_y = 1; 28 29 public SvgTransforms () { 30 transforms = new Gee.ArrayList<SvgTransform> (); 31 rotation_matrix = Matrix.identity (); 32 size_matrix = Matrix.identity (); 33 } 34 35 public void collapse_transforms () { 36 SvgTransform rotation_transform = new SvgTransform.for_matrix (rotation_matrix); 37 add (rotation_transform); 38 39 rotation_matrix = Matrix.identity (); 40 rotation = 0; 41 42 SvgTransform size_transform = new SvgTransform.for_matrix (size_matrix); 43 add (size_transform); 44 45 size_matrix = Matrix.identity (); 46 scale_x = 1; 47 scale_y = 1; 48 49 Matrix collapsed = get_matrix (); 50 SvgTransform collapsed_transform = new SvgTransform.for_matrix (collapsed); 51 clear (); 52 add (collapsed_transform); 53 } 54 55 public void clear () { 56 transforms.clear (); 57 58 rotation_matrix = Matrix.identity (); 59 rotation = 0; 60 61 size_matrix = Matrix.identity (); 62 scale_x = 1; 63 scale_y = 1; 64 } 65 66 public void rotate (double theta, double x, double y) { 67 rotation += theta; 68 total_rotation += theta; 69 70 while (rotation > 2 * Math.PI) { 71 rotation -= 2 * Math.PI; 72 } 73 74 while (rotation < -2 * Math.PI) { 75 rotation += 2 * Math.PI; 76 } 77 78 while (total_rotation > 2 * Math.PI) { 79 total_rotation -= 2 * Math.PI; 80 } 81 82 while (total_rotation < -2 * Math.PI) { 83 total_rotation += 2 * Math.PI; 84 } 85 86 rotation_matrix = Matrix.identity (); 87 rotation_matrix.translate (x, y); 88 rotation_matrix.rotate (rotation); 89 rotation_matrix.translate (-x, -y); 90 } 91 92 public void resize (double ratio_x, double ratio_y, double x, double y) { 93 scale_x *= ratio_x; 94 scale_y *= ratio_y; 95 96 if (scale_x <= 0 || scale_y <= 0) { 97 return; 98 } 99 100 size_matrix = Matrix.identity (); 101 size_matrix.translate (x, y); 102 size_matrix.scale (scale_x, scale_y); 103 size_matrix.translate (-x, -y); 104 } 105 106 public SvgTransforms copy () { 107 SvgTransforms copy_transforms = new SvgTransforms (); 108 109 foreach (SvgTransform t in transforms) { 110 copy_transforms.add (t.copy ()); 111 } 112 113 return copy_transforms; 114 } 115 116 public void add (SvgTransform transform) { 117 transforms.add (transform); 118 } 119 120 public Matrix get_matrix () { 121 Matrix transformation_matrix = Matrix.identity (); 122 123 for (int i = 0; i < transforms.size; i++) { 124 Matrix part = transforms.get (i).get_matrix (); 125 transformation_matrix.multiply (transformation_matrix, part); 126 } 127 128 transformation_matrix.multiply (transformation_matrix, size_matrix); 129 transformation_matrix.multiply (transformation_matrix, rotation_matrix); 130 131 return transformation_matrix; 132 } 133 134 public string to_string () { 135 StringBuilder sb = new StringBuilder (); 136 137 foreach (SvgTransform t in transforms) { 138 sb.append (t.to_string ()); 139 sb.append (" "); 140 } 141 142 return sb.str; 143 } 144 145 public string get_xml () { 146 StringBuilder svg = new StringBuilder (); 147 bool first = true; 148 149 foreach (SvgTransform transform in transforms) { 150 if (!first) { 151 svg.append (" "); 152 } 153 154 svg.append (transform.get_xml ()); 155 first = false; 156 } 157 158 return svg.str; 159 } 160 } 161 162 } 163