1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2010-2011, 2014 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "SmartHandle.h"
25 , viewportHeight(1000)
33 bool SVG::Save( const CString
& path
)
36 CAutoFile hFile
= CreateFile(path
, GENERIC_WRITE
, FILE_SHARE_DELETE
, nullptr, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, nullptr);
41 header
.Format("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\">\r\n", viewportWidth
, viewportHeight
, viewportWidth
, viewportHeight
);
42 CStringA footer
= "\r\n</svg>";
44 if (!WriteFile(hFile
, header
, (DWORD
)header
.GetLength(), &dwWritten
, nullptr))
47 for (const auto& object
: objects
)
49 if (!WriteFile(hFile
, object
, (DWORD
)object
.GetLength(), &dwWritten
, nullptr))
51 if (!WriteFile(hFile
, "\r\n", (DWORD
)2, &dwWritten
, nullptr))
54 if (!WriteFile(hFile
, footer
, (DWORD
)footer
.GetLength(), &dwWritten
, nullptr))
61 void SVG::RoundedRectangle( int x
, int y
, int width
, int height
, Gdiplus::Color stroke
, int penWidth
, Gdiplus::Color fill
, int radius
/*= 0*/, int mode
)
66 sObj
.Format("<rect x=\"%d\" y=\"%d\" height=\"%d\" width=\"%d\" rx=\"%d\" ry=\"%d\" style=\"stroke:#%06lx; stroke-width:%d; fill: #%06lx\"/>",
67 x
, y
, height
, width
, radius
, radius
, GetColor(stroke
), penWidth
, GetColor(fill
));
73 tmp
.Format("M %d %d a %d %d 0 0 1 %d %d ", x
, y
+radius
, radius
, radius
, radius
, -radius
);
75 tmp
.Format("h %d ", width
- 2*radius
);
77 tmp
.Format("a %d %d 0 0 1 %d %d ", radius
, radius
, radius
, radius
);
81 tmp
.Format("M %d %d h %d ",x
, y
, width
);
87 tmp
.Format("V %d a %d %d 0 0 1 %d %d ", y
+height
-radius
, radius
,radius
, -radius
, radius
);
89 tmp
.Format("h %d a %d %d 0 0 1 %d %d z ", - width
+ 2* radius
, radius
,radius
, -radius
, -radius
);
93 tmp
.Format("V %d h %d z ", y
+height
, -width
);
97 tmp
.Format("style=\"stroke:#%06lx; stroke-width:%d; fill: #%06lx\"/>", GetColor(stroke
), penWidth
, GetColor(fill
));
100 objects
.push_back(sObj
);
103 void SVG::Polygon( const Gdiplus::PointF
* points
, int numPoints
, Gdiplus::Color stroke
, int penWidth
, Gdiplus::Color fill
)
105 CStringA pointstring
;
107 for (int i
= 0; i
< numPoints
; ++i
)
109 sTemp
.Format("%d,%d ", (int)points
[i
].X
, (int)points
[i
].Y
);
110 pointstring
+= sTemp
;
112 pointstring
.TrimRight();
115 sObj
.Format("<polygon points=\"%s\" style=\"stroke:#%06lx; stroke-width:%d; fill:#%06lx;\"/>",
116 (LPCSTR
)pointstring
, GetColor(stroke
), penWidth
, GetColor(fill
));
118 objects
.push_back(sObj
);
121 void SVG::DrawPath( const Gdiplus::PointF
* points
, int numPoints
, Gdiplus::Color stroke
, int penWidth
, Gdiplus::Color fill
)
123 CStringA pointstring
;
125 for (int i
= 0; i
< numPoints
; ++i
)
127 sTemp
.Format("%c %d %d ", i
==0? 'M':'L', (int)points
[i
].X
, (int)points
[i
].Y
);
128 pointstring
+= sTemp
;
130 pointstring
.TrimRight();
133 sObj
.Format("<path d=\"%s\" style=\"stroke:#%06lx; stroke-width:%d; fill:#%06lx;\"/>",
134 (LPCSTR
)pointstring
, GetColor(stroke
), penWidth
, GetColor(fill
));
136 objects
.push_back(sObj
);
139 void SVG::Polyline( const Gdiplus::PointF
* points
, int numPoints
, Gdiplus::Color stroke
, int penWidth
)
141 CStringA pointstring
;
143 for (int i
= 0; i
< numPoints
; ++i
)
145 sTemp
.Format("%d,%d ", (int)points
[i
].X
, (int)points
[i
].Y
);
146 pointstring
+= sTemp
;
148 pointstring
.TrimRight();
151 sObj
.Format("<polyline points=\"%s\" style=\"stroke:#%06lx; stroke-width:%d; fill:none;\"/>",
152 (LPCSTR
)pointstring
, GetColor(stroke
), penWidth
);
154 objects
.push_back(sObj
);
156 void SVG::GradientRectangle( int x
, int y
, int width
, int height
, Gdiplus::Color topColor
, Gdiplus::Color bottomColor
, Gdiplus::Color stroke
)
162 <linearGradient id=\"linearGradient%d\" \
163 x1=\"0%%\" y1=\"0%%\" \
164 x2=\"0%%\" y2=\"100%%\" \
165 spreadMethod=\"pad\">\
166 <stop offset=\"0%%\" stop-color=\"#%06lx\" stop-opacity=\"1\"/>\
167 <stop offset=\"100%%\" stop-color=\"#%06lx\" stop-opacity=\"1\"/>\
170 <rect x=\"%d\" y=\"%d\" height=\"%d\" width=\"%d\" style=\"stroke:#%06lx; fill::url(#linearGradient%d)\"/>\
172 (int)objects
.size(), GetColor(topColor
), GetColor(bottomColor
), x
, y
, height
, width
, GetColor(stroke
), (int)objects
.size());
174 objects
.push_back(sObj
);
177 void SVG::PolyBezier( const POINT
* points
, int numPoints
, Gdiplus::Color stroke
)
182 CStringA pointstring
;
184 sTemp
.Format("M%d,%d ", points
[0].x
, points
[0].y
);
185 pointstring
+= sTemp
;
187 for (int i
= 1; i
< numPoints
; i
+= 3)
189 sTemp
.Format("C%d,%d %d,%d %d,%d", points
[i
].x
, points
[i
].y
, points
[i
+1].x
, points
[i
+1].y
, points
[i
+2].x
, points
[i
+2].y
);
190 pointstring
+= sTemp
;
192 pointstring
.TrimRight();
195 sObj
.Format("<path d=\"%s\" style=\"stroke:#%06lx; fill:none;\"/>",
196 (LPCSTR
)pointstring
, GetColor(stroke
));
198 objects
.push_back(sObj
);
201 void SVG::Ellipse( int x
, int y
, int width
, int height
, Gdiplus::Color stroke
, int penWidth
, Gdiplus::Color fill
)
203 int cx
= x
+ width
/2;
204 int cy
= y
+ height
/2;
208 sObj
.Format("<ellipse cx=\"%d\" cy=\"%d\" rx=\"%d\" ry=\"%d\" style=\"stroke:#%06lx; stroke-width:%d; fill: #%06lx\"/>",
209 cx
, cy
, rx
, ry
, GetColor(stroke
), penWidth
, GetColor(fill
));
211 objects
.push_back(sObj
);
214 void SVG::Text( int x
, int y
, LPCSTR font
, int fontsize
, bool italic
, bool bold
, Gdiplus::Color color
, LPCSTR text
, int al
)
217 sObj
.Format("<text x=\"%d\" y=\"%d\" \
218 style=\"font-family:%s;\
224 fill:#%06lx;\">%s</text>",
225 x
, y
, font
, fontsize
, italic
? "italic" : "none", bold
? "bold" : "none",
226 al
==SVG::left
? "left": al
==SVG::middle
? "middle": "right",
227 GetColor(color
),text
);
229 objects
.push_back(sObj
);
232 DWORD
SVG::GetColor( Gdiplus::Color c
) const
234 return ((DWORD
)c
.GetRed() << 16) | ((DWORD
)c
.GetGreen() << 8) | ((DWORD
)c
.GetBlue());