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