The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ViewBox.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/ViewBox.vala.
Update boundaries after undo
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 16 using B; 17 using Cairo; 18 using Math; 19 20 namespace SvgBird { 21 22 public class ViewBox : GLib.Object { 23 24 public const uint NONE = 1; 25 public const uint XMIN = 1 << 1; 26 public const uint XMID = 1 << 2; 27 public const uint XMAX = 1 << 3; 28 public const uint YMIN = 1 << 4; 29 public const uint YMID = 1 << 5; 30 public const uint YMAX = 1 << 6; 31 32 public const uint XMIN_YMIN = XMIN | YMIN; 33 public const uint XMID_YMIN = XMID | YMIN; 34 public const uint XMAX_YMIN = XMAX | YMIN; 35 public const uint XMIN_YMID = XMIN | YMID; 36 public const uint XMID_YMID = XMID | YMID; 37 public const uint XMAX_YMID = XMAX | YMID; 38 public const uint XMIN_YMAX = XMIN | YMAX; 39 public const uint XMID_YMAX = XMID | YMAX; 40 public const uint XMAX_YMAX = XMAX | YMAX; 41 42 public double minx = 0; 43 public double miny = 0; 44 public double width = 0; 45 public double height = 0; 46 47 public uint alignment; 48 public bool slice; 49 50 public bool preserve_aspect_ratio; 51 52 public ViewBox.empty () { 53 } 54 55 public ViewBox (double minx, double miny, double width, double height, 56 uint alignment, bool slice, bool preserve_aspect_ratio) { 57 58 this.minx = minx; 59 this.miny = miny; 60 this.width = width; 61 this.height = height; 62 63 this.alignment = alignment; 64 this.slice = slice; 65 this.preserve_aspect_ratio = preserve_aspect_ratio; 66 } 67 68 public ViewBox copy () { 69 ViewBox box = new ViewBox.empty (); 70 71 box.minx = minx; 72 box.miny = miny; 73 box.width = width; 74 box.height = height; 75 76 box.alignment = alignment; 77 box.slice = slice; 78 box.preserve_aspect_ratio = preserve_aspect_ratio; 79 80 return box; 81 } 82 83 public Matrix get_matrix (double original_width, double original_height) { 84 double scale_x = 1; 85 double scale_y = 1; 86 double scale = 1; 87 88 Matrix matrix = Matrix.identity (); 89 90 if (original_width == 0 || original_height == 0 || width == 0 || height == 0) { 91 return matrix; 92 } 93 94 matrix.translate (minx, miny); 95 scale_x = original_width / width; 96 scale_y = original_height / height; 97 98 bool scale_width = scale_x * original_height > scale_y * original_width; 99 100 if (scale_width) { 101 scale = scale_y; 102 } else { 103 scale = scale_x; 104 } 105 106 if (preserve_aspect_ratio) { 107 if ((alignment & ViewBox.XMID) > 0) { 108 matrix.translate ((original_width - width * scale) / 2, 0); 109 } else if ((alignment & ViewBox.XMAX) > 0) { 110 matrix.translate ((original_width - width * scale), 0); 111 } 112 113 if ((alignment & ViewBox.YMID) > 0) { 114 matrix.translate ((original_height - height * scale) / 2, 0); 115 } else if ((alignment & ViewBox.YMAX) > 0) { 116 matrix.translate ((original_height - height * scale), 0); 117 } 118 } 119 120 if (!preserve_aspect_ratio) { 121 matrix.scale (scale_x, scale_y); 122 } else if (scale_width) { 123 scale = scale_y; 124 matrix.scale (scale, scale); 125 } else { 126 scale = scale_x; 127 matrix.scale (scale, scale); 128 } 129 130 return matrix; 131 } 132 } 133 134 } 135