The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

SvgDrawing.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/SvgDrawing.vala.
Scale view box
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 SvgDrawing : Object { 23 public Layer root_layer = new Layer (); 24 public Defs defs = new Defs (); 25 public ViewBox? view_box = null; 26 27 public double width { 28 get { 29 return svg_width; 30 } 31 32 set { 33 svg_width = value; 34 } 35 } 36 37 public double height { 38 get { 39 return svg_height; 40 } 41 42 set { 43 svg_height = value; 44 } 45 } 46 47 public double svg_width = 0; 48 public double svg_height = 0; 49 50 public override bool update_boundaries (Context cr) { 51 apply_transform (cr); 52 root_layer.update_boundaries (cr); 53 54 left = root_layer.left; 55 right = root_layer.right; 56 top = root_layer.top; 57 bottom = root_layer.bottom; 58 59 return true; 60 } 61 62 public override bool is_over (double x, double y) { 63 return false; 64 } 65 66 void apply_view_box (Context cr) { 67 if (view_box != null) { 68 ViewBox box = (!) view_box; 69 double scale_x = 1; 70 double scale_y = 1; 71 double scale = 1; 72 73 cr.translate (box.minx, box.miny); 74 scale_x = width / box.width; 75 scale_y = height / box.height; 76 77 bool scale_width = height * box.width > width * box.height; 78 79 if (box.alignment == ViewBox.NONE) { 80 cr.scale (scale_x, scale_y); 81 } else if (scale_width && box.slice) { 82 scale = scale_x; 83 cr.scale (scale, scale); 84 } else { 85 scale = scale_y; 86 cr.scale (scale, scale); 87 } 88 89 if (!box.slice) { 90 if ((box.alignment & ViewBox.XMID) > 0) { 91 cr.translate ((box.width - width) / 2, 0); 92 } else if ((box.alignment & ViewBox.XMAX) > 0) { 93 cr.translate (box.width - width, 0); 94 } 95 96 if ((box.alignment & ViewBox.YMID) > 0) { 97 cr.translate (0, (box.height - height) / 2); 98 } else if ((box.alignment & ViewBox.YMAX) > 0) { 99 cr.translate (0, box.height - height); 100 } 101 } else { 102 Layer layer = new Layer (); 103 Rectangle rectangle = new Rectangle (); 104 rectangle.width = box.width; 105 rectangle.height = box.height; 106 layer.add_object (rectangle); 107 ClipPath clip = new ClipPath (layer); 108 clip_path = clip; 109 } 110 } 111 } 112 113 public override void apply_transform (Context cr) { 114 apply_view_box (cr); 115 base.apply_transform (cr); 116 } 117 118 public void draw (Context cr) { 119 cr.save (); 120 apply_transform (cr); 121 root_layer.draw (cr); 122 cr.restore (); 123 } 124 125 public override void draw_outline (Context cr) { 126 root_layer.draw_outline (cr); 127 } 128 129 public override Object copy () { 130 SvgDrawing drawing = new SvgDrawing (); 131 SvgBird.Object.copy_attributes (this, drawing); 132 drawing.root_layer = (Layer) root_layer.copy (); 133 drawing.defs = defs.copy (); 134 drawing.width = width; 135 drawing.height = height; 136 return drawing; 137 } 138 139 public override void move (double dx, double dy) { 140 } 141 142 public override bool is_empty () { 143 return false; 144 } 145 146 public override string to_string () { 147 return @"SvgDrawing width: $width, height: $height"; 148 } 149 } 150 151 } 152