The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgTransform.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
Circle boundaries heads/master
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 B; 16 using Cairo; 17 18 namespace SvgBird { 19 20 public enum TransformType { 21 NONE, 22 TRANSLATE, 23 MATRIX, 24 SCALE, 25 ROTATE 26 } 27 28 public class SvgTransform : GLib.Object { 29 public TransformType type = TransformType.NONE; 30 public Doubles arguments = new Doubles.for_capacity (10); 31 32 public SvgTransform () { 33 } 34 35 public SvgTransform.for_matrix (Matrix matrix) { 36 type = TransformType.MATRIX; 37 38 arguments.add (matrix.xx); 39 arguments.add (matrix.yx); 40 arguments.add (matrix.xy); 41 arguments.add (matrix.yy); 42 arguments.add (matrix.x0); 43 arguments.add (matrix.y0); 44 } 45 46 public SvgTransform copy () { 47 SvgTransform transform = new SvgTransform (); 48 transform.type = type; 49 transform.arguments = arguments.copy (); 50 return transform; 51 } 52 53 public string to_string () { 54 StringBuilder sb = new StringBuilder (); 55 56 sb.append (@"$type"); 57 sb.append (" "); 58 59 for (int i = 0; i < arguments.size; i++) { 60 sb.append (@"$(arguments.get_double (i)) "); 61 } 62 63 return sb.str; 64 } 65 66 public Matrix get_matrix () { 67 Matrix matrix; 68 69 matrix = Matrix.identity (); 70 71 if (type == TransformType.ROTATE) { 72 if (arguments.size == 1) { 73 double r = arguments.get_double (0); 74 r *= Math.PI / 180; 75 matrix.rotate (r); 76 } 77 } else if (type == TransformType.SCALE) { 78 if (arguments.size == 1) { 79 double s = arguments.get_double (0); 80 matrix.scale (s, s); 81 } else if (arguments.size == 2) { 82 double s0 = arguments.get_double (0); 83 double s1 = arguments.get_double (1); 84 matrix.scale (s0, s1); 85 } 86 } else if (type == TransformType.TRANSLATE) { 87 if (arguments.size == 1) { 88 double s = arguments.get_double (0); 89 matrix.translate (s, 0); 90 } else if (arguments.size == 2) { 91 double s0 = arguments.get_double (0); 92 double s1 = arguments.get_double (1); 93 matrix.translate (s0, s1); 94 } 95 } else if (type == TransformType.MATRIX) { 96 if (arguments.size == 6) { 97 double s0 = arguments.get_double (0); 98 double s1 = arguments.get_double (1); 99 double s2 = arguments.get_double (2); 100 double s3 = arguments.get_double (3); 101 double s4 = arguments.get_double (4); 102 double s5 = arguments.get_double (5); 103 104 matrix = Matrix (s0, s1, s2, s3, s4, s5); 105 } 106 } 107 108 return matrix; 109 } 110 111 public string get_xml () { 112 if (type == TransformType.SCALE) { 113 if (arguments.size == 1) { 114 string s = arguments.get_string (0); 115 return @"scale ($s)"; 116 } else if (arguments.size == 2) { 117 string s0 = arguments.get_string (0); 118 string s1 = arguments.get_string (1); 119 return @"scale ($s0, $s1)"; 120 } 121 } else if (type == TransformType.TRANSLATE) { 122 if (arguments.size == 1) { 123 string s = arguments.get_string (0); 124 return @"translate ($s)"; 125 } else if (arguments.size == 2) { 126 string s0 = arguments.get_string (0); 127 string s1 = arguments.get_string (1); 128 return @"translate ($s0, $s1)"; 129 } 130 } else if (type == TransformType.MATRIX) { 131 if (arguments.size == 6) { 132 string s0 = arguments.get_string (0); 133 string s1 = arguments.get_string (1); 134 string s2 = arguments.get_string (2); 135 string s3 = arguments.get_string (3); 136 string s4 = arguments.get_string (4); 137 string s5 = arguments.get_string (5); 138 return @"matrix ($s0, $s1, $s2, $s3, $s4, $s5)"; 139 } 140 } 141 142 warning ("Invalid transform."); 143 144 return ""; 145 } 146 } 147 148 } 149