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.
Resize paths
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 x = 0; 25 public double y = 0; 26 public double rotation = 0; 27 public double scale_x = 1; 28 public double scale_y = 1; 29 30 public SvgTransforms () { 31 transforms = new Gee.ArrayList<SvgTransform> (); 32 rotation_matrix = Matrix.identity (); 33 size_matrix = Matrix.identity (); 34 } 35 36 public void collapse_transforms () { 37 SvgTransform rotation_transform = new SvgTransform.for_matrix (rotation_matrix); 38 add (rotation_transform); 39 40 rotation_matrix = Matrix.identity (); 41 rotation = 0; 42 43 SvgTransform size_transform = new SvgTransform.for_matrix (size_matrix); 44 add (size_transform); 45 46 size_matrix = Matrix.identity (); 47 scale_x = 1; 48 scale_y = 1; 49 50 Matrix collapsed = get_matrix (); 51 SvgTransform collapsed_transform = new SvgTransform.for_matrix (collapsed); 52 clear (); 53 add (collapsed_transform); 54 } 55 56 public void clear () { 57 transforms.clear (); 58 59 rotation_matrix = Matrix.identity (); 60 rotation = 0; 61 62 size_matrix = Matrix.identity (); 63 scale_x = 1; 64 scale_y = 1; 65 } 66 67 public void rotate (double theta, double x, double y) { 68 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 rotation_matrix = Matrix.identity (); 79 rotation_matrix.translate (x, y); 80 rotation_matrix.rotate (rotation); 81 rotation_matrix.translate (-x, -y); 82 } 83 84 public void resize (double ratio_x, double ratio_y, double x, double y) { 85 scale_x *= ratio_x; 86 scale_y *= ratio_y; 87 88 if (scale_x <= 0 || scale_y <= 0) { 89 return; 90 } 91 92 size_matrix = Matrix.identity (); 93 size_matrix.translate (x, y); 94 size_matrix.scale (scale_x, scale_y); 95 size_matrix.translate (-x, -y); 96 } 97 98 public SvgTransforms copy () { 99 SvgTransforms copy_transforms = new SvgTransforms (); 100 101 foreach (SvgTransform t in transforms) { 102 copy_transforms.add (t.copy ()); 103 } 104 105 return copy_transforms; 106 } 107 108 public void add (SvgTransform transform) { 109 transforms.add (transform); 110 } 111 112 public Matrix get_matrix () { 113 Matrix transformation_matrix = Matrix.identity (); 114 115 for (int i = 0; i < transforms.size; i++) { 116 Matrix part = transforms.get (i).get_matrix (); 117 transformation_matrix.multiply (transformation_matrix, part); 118 } 119 120 transformation_matrix.multiply (transformation_matrix, rotation_matrix); 121 transformation_matrix.multiply (transformation_matrix, size_matrix); 122 123 return transformation_matrix; 124 } 125 126 public string to_string () { 127 StringBuilder sb = new StringBuilder (); 128 129 foreach (SvgTransform t in transforms) { 130 sb.append (t.to_string ()); 131 sb.append (" "); 132 } 133 134 return sb.str; 135 } 136 137 public string get_xml () { 138 StringBuilder svg = new StringBuilder (); 139 140 foreach (SvgTransform transform in transforms) { 141 svg.append (transform.get_xml ()); 142 svg.append (";"); 143 } 144 145 return svg.str; 146 } 147 } 148 149 } 150