1 //////////////////////////////////////////////////////////////////////////////
2 // Name:        SVGFitToViewBox.cpp
3 // Purpose:
4 // Author:      Alex Thuering
5 // Created:     2006/01/07
6 // RCS-ID:      $Id: SVGFitToViewBox.cpp,v 1.2 2014/07/06 15:20:38 ntalex Exp $
7 // Copyright:   (c) 2006 Alex Thuering
8 // Licence:     wxWindows licence
9 //////////////////////////////////////////////////////////////////////////////
10 
11 #include "SVGFitToViewBox.h"
12 
UpdateMatrix(wxSVGMatrix & matrix,const wxSVGLength & width,const wxSVGLength & height)13 void wxSVGFitToViewBox::UpdateMatrix(wxSVGMatrix& matrix, const wxSVGLength& width, const wxSVGLength& height) {
14 	// view box
15 	wxSVGRect viewbox = GetViewBox().GetAnimVal();
16 	if (viewbox.GetWidth() <= 0 || viewbox.GetHeight() <= 0)
17 		return;
18 
19 	wxSVG_PRESERVEASPECTRATIO align = GetPreserveAspectRatio().GetAnimVal().GetAlign();
20 	if (align == wxSVG_PRESERVEASPECTRATIO_UNKNOWN) {
21 		align = wxSVG_PRESERVEASPECTRATIO_XMIDYMID;
22 	}
23 
24 	if (align == wxSVG_PRESERVEASPECTRATIO_NONE) {
25 		matrix = matrix.ScaleNonUniform(width / viewbox.GetWidth(), height / viewbox.GetHeight());
26 	} else {
27 		double scale = 1;
28 		scale = width / viewbox.GetWidth();
29 		if (scale > height / viewbox.GetHeight())
30 			scale = height / viewbox.GetHeight();
31 
32 		double x = 0;
33 		if (abs(width - viewbox.GetWidth() * scale) > width / 1000000) {
34 			if (align == wxSVG_PRESERVEASPECTRATIO_XMIDYMIN
35 					|| align == wxSVG_PRESERVEASPECTRATIO_XMIDYMID
36 					|| align == wxSVG_PRESERVEASPECTRATIO_XMIDYMAX)
37 				x = (width.GetValue() - viewbox.GetWidth() * scale) / 2;
38 			else if (align == wxSVG_PRESERVEASPECTRATIO_XMAXYMIN
39 					|| align == wxSVG_PRESERVEASPECTRATIO_XMAXYMID
40 					|| align == wxSVG_PRESERVEASPECTRATIO_XMAXYMAX)
41 				x = width.GetValue() - viewbox.GetWidth() * scale;
42 		}
43 
44 		double y = 0;
45 		if (height.GetValue() - viewbox.GetHeight() * scale > height / 1000000) {
46 			if (align == wxSVG_PRESERVEASPECTRATIO_XMINYMID
47 					|| align == wxSVG_PRESERVEASPECTRATIO_XMIDYMID
48 					|| align == wxSVG_PRESERVEASPECTRATIO_XMAXYMID)
49 				y = (height.GetValue() - viewbox.GetHeight() * scale) / 2;
50 			else if (align == wxSVG_PRESERVEASPECTRATIO_XMINYMAX
51 					|| align == wxSVG_PRESERVEASPECTRATIO_XMIDYMAX
52 					|| align == wxSVG_PRESERVEASPECTRATIO_XMAXYMAX)
53 				y = height.GetValue() - viewbox.GetHeight() * scale;
54 		}
55 
56 		if (x != 0 || y != 0)
57 			matrix = matrix.Translate(x, y);
58 
59 		matrix = matrix.Scale(scale);
60 	}
61 	if (viewbox.GetX() != 0 || viewbox.GetY() != 0)
62 		matrix = matrix.Translate(-viewbox.GetX(), -viewbox.GetY());
63 }
64 
65