The Birdfont Source Code
Fix alignment in view box
These changes was commited to the Birdfont repository Sun, 03 Jul 2016 10:51:59 +0000.
Contributing
Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Fix alignment in view box
--- a/libsvgbird/SvgDrawing.vala
+++ b/libsvgbird/SvgDrawing.vala
@@ -64,49 +64,48 @@
}
void apply_view_box (Context cr) {
+ double scale_x = 1;
+ double scale_y = 1;
+ double scale = 1;
+
if (view_box != null) {
ViewBox box = (!) view_box;
- double scale_x = 1;
- double scale_y = 1;
- double scale = 1;
cr.translate (box.minx, box.miny);
scale_x = width / box.width;
scale_y = height / box.height;
- bool scale_width = height * box.width > width * box.height;
+ bool scale_width = scale_x > scale_y;
- if (box.alignment == ViewBox.NONE) {
- cr.scale (scale_x, scale_y);
- } else if (scale_width && box.slice) {
- scale = scale_x;
- cr.scale (scale, scale);
- } else {
+ if (scale_width) {
scale = scale_y;
- cr.scale (scale, scale);
+ } else {
+ scale = scale_x;
}
- if (!box.slice) {
+ if (box.preserve_aspect_ratio) {
if ((box.alignment & ViewBox.XMID) > 0) {
- cr.translate (((box.width - width) * scale) / 2, 0);
+ cr.translate ((width - box.width * scale) / 2, 0);
} else if ((box.alignment & ViewBox.XMAX) > 0) {
- cr.translate ((box.width - width) * scale, 0);
+ cr.translate ((width - box.width * scale), 0);
}
if ((box.alignment & ViewBox.YMID) > 0) {
- cr.translate (0, ((box.height - height) * scale) / 2);
+ cr.translate ((height - box.height * scale) / 2, 0);
} else if ((box.alignment & ViewBox.YMAX) > 0) {
- cr.translate (0, (box.height - height) * scale);
+ cr.translate ((height - box.height * scale), 0);
}
- } else {
- Layer layer = new Layer ();
- Rectangle rectangle = new Rectangle ();
- rectangle.width = box.width;
- rectangle.height = box.height;
- layer.add_object (rectangle);
- ClipPath clip = new ClipPath (layer);
- clip_path = clip;
}
+
+ if (!box.preserve_aspect_ratio) {
+ cr.scale (scale_x, scale_y);
+ } else if (scale_width) {
+ scale = scale_y;
+ cr.scale (scale, scale);
+ } else {
+ scale = scale_x;
+ cr.scale (scale, scale);
+ }
}
}
--- a/libsvgbird/SvgFile.vala
+++ b/libsvgbird/SvgFile.vala
@@ -81,7 +81,8 @@
ViewBox? parse_view_box (string parameters, XmlElement tag) {
string arguments = parameters.replace (",", " ");
string aspect_ratio = "";
- bool slice = false;
+ bool slice = true;
+ bool preserve_aspect_ratio = true;
foreach (Attribute attribute in tag.get_attributes ()) {
if (attribute.get_name () == "preserveAspectRatio") {
@@ -96,6 +97,8 @@
if (preserveSettings.length >= 2) {
slice = preserveSettings[1] == "slice";
}
+
+ preserve_aspect_ratio = false;
}
}
@@ -120,27 +123,27 @@
if (aspect_ratio == "NONE") {
alignment = ViewBox.NONE;
- } else if (aspect_ratio == "XMIN_YMIN") {
+ } else if (aspect_ratio == "XMINYMIN") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMID_YMIN") {
+ } else if (aspect_ratio == "XMINYMIN") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMAX_YMIN") {
+ } else if (aspect_ratio == "XMAXYMIN") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMIN_YMID") {
+ } else if (aspect_ratio == "XMINYMID") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMID_YMID") {
+ } else if (aspect_ratio == "XMIDYMID") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMAX_YMID") {
+ } else if (aspect_ratio == "XMAXYMID") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMIN_YMAX") {
+ } else if (aspect_ratio == "XMINYMAX") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMID_YMAX") {
+ } else if (aspect_ratio == "XMIDYMAX") {
alignment = ViewBox.XMIN_YMIN;
- } else if (aspect_ratio == "XMAX_YMAX") {
+ } else if (aspect_ratio == "XMAXYMAX") {
alignment = ViewBox.XMIN_YMIN;
}
- return new ViewBox (minx, miny, width, height, alignment, slice);
+ return new ViewBox (minx, miny, width, height, alignment, slice, preserve_aspect_ratio);
}
private void parse_layer (Layer layer, SvgStyle parent_style, XmlElement tag) {
--- a/libsvgbird/ViewBox.vala
+++ b/libsvgbird/ViewBox.vala
@@ -46,9 +46,11 @@
public uint alignment;
public bool slice;
+
+ public bool preserve_aspect_ratio;
public ViewBox (double minx, double miny, double width, double height,
- uint alignment, bool slice) {
+ uint alignment, bool slice, bool preserve_aspect_ratio) {
this.minx = minx;
this.miny = miny;
@@ -57,9 +59,10 @@
this.alignment = alignment;
this.slice = slice;
+ this.preserve_aspect_ratio = preserve_aspect_ratio;
}
}
}