"Fossies" - the Fresh Open Source Software Archive 
Member "ansifilter-2.18-x64/src/svggenerator.cpp" (30 Jan 2021, 9215 Bytes) of package /windows/misc/ansifilter-2.18-x64.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /***************************************************************************
2 svggenerator.cpp - description
3 -------------------
4 begin : Mo 01.04.2019
5 copyright : (C) 2019 by Andre Simon
6 email : a.simon@mailbox.org
7 ***************************************************************************/
8
9
10 /*
11 This file is part of Highlight.
12
13 Highlight is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 Highlight is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with Highlight. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27
28 #include <fstream>
29 #include <string>
30 #include <sstream>
31 #include <algorithm>
32
33 #include "version.h"
34 #include "svggenerator.h"
35
36 namespace ansifilter
37 {
38
39 SVGGenerator::SVGGenerator()
40 : CodeGenerator ( SVG ),
41 fileSuffix(".svg")
42 {
43 spacer = " ";
44 newLineTag = "\n";
45 styleCommentOpen="/*";
46 styleCommentClose="*/";
47 }
48
49 SVGGenerator::~SVGGenerator() {}
50
51
52 string SVGGenerator::getOpenTag()
53 {
54 ostringstream fmtStream;
55 string attrName("style");
56
57 if (applyDynStyles){
58 attrName = "class";
59
60 ostringstream fgColStream;
61 if (elementStyle.isFgColorSet()) {
62 fgColStream << elementStyle.getFgColour().getRed(HTML)
63 << elementStyle.getFgColour().getGreen(HTML)
64 << elementStyle.getFgColour().getBlue(HTML);
65 }
66
67 ostringstream bgColStream;
68 if (elementStyle.isBgColorSet()) {
69
70 bgColStream << elementStyle.getBgColour().getRed(HTML)
71 << elementStyle.getBgColour().getGreen(HTML)
72 << elementStyle.getBgColour().getBlue(HTML);
73 }
74
75 StyleInfo sInfo( fgColStream.str(), bgColStream.str(),
76 elementStyle.isBold(), elementStyle.isItalic(), elementStyle.isConceal(),
77 elementStyle.isBlink(), elementStyle.isUnderline() );
78
79 std::vector<StyleInfo>::iterator fit = std::find(documentStyles.begin(), documentStyles.end(), sInfo );
80 if (fit == documentStyles.end()){
81 documentStyles.push_back(sInfo);
82 fmtStream << "af_"<< documentStyles.size();
83 } else {
84 fmtStream << "af_"<< 1+(int)std::distance(documentStyles.begin(), fit);
85 }
86
87 } else {
88
89 if (elementStyle.isBold()) {
90 fmtStream<< "font-weight:bold;";
91 }
92 if (elementStyle.isItalic()) {
93 fmtStream<< "font-style:italic;";
94 }
95 if (elementStyle.isBlink()) {
96 fmtStream<< "text-decoration:blink;";
97 }
98 if (elementStyle.isUnderline()) {
99 fmtStream<< "text-decoration:underline;";
100 }
101 if (elementStyle.isConceal()) {
102 fmtStream<< "display:none;";
103 }
104
105 if (elementStyle.isFgColorSet()) {
106 fmtStream << "fill:#"
107 << elementStyle.getFgColour().getRed(HTML)
108 << elementStyle.getFgColour().getGreen(HTML)
109 << elementStyle.getFgColour().getBlue(HTML)
110 << ";";
111 }
112
113 /*if (elementStyle.isBgColorSet()) {
114 fmtStream <<"background-color:#"
115 << elementStyle.getBgColour().getRed(HTML)
116 << elementStyle.getBgColour().getGreen(HTML)
117 << elementStyle.getBgColour().getBlue(HTML)
118 <<";";
119 }*/
120 }
121
122
123 string fmt = fmtStream.str();
124 tagIsOpen = fmt.size()>0;
125 if (tagIsOpen) {
126 ostringstream spanTag;
127 spanTag<< "<tspan "<<attrName<<"=\""<<fmt<<"\">";
128 return spanTag.str();
129 }
130 return "";
131 }
132
133 string SVGGenerator::getCloseTag()
134 {
135 string retVal = tagIsOpen ? "</tspan>" : "";
136 tagIsOpen = false;
137 return retVal;
138 }
139
140 string SVGGenerator::getGeneratorComment()
141 {
142 ostringstream s;
143 s << "<!--SVG generated by ansifilter "
144 << ANSIFILTER_VERSION << ", " << ANSIFILTER_URL <<"-->\n";
145
146 return s.str();
147 }
148
149 string SVGGenerator::getHeader()
150 {
151 ostringstream header;
152 header << "<?xml version=\"1.0\"";
153 if ( encodingDefined() ) {
154 header << " encoding=\"" << encoding << "\"";
155 }
156 header << "?>\n";
157 if (!styleSheetPath.empty() ) {
158 header << "<?xml-stylesheet type=\"text/css\" href=\""
159 << styleSheetPath
160 << "\"?>\n";
161 }
162 header << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.2//EN\" "
163 << "\"http://www.w3.org/Graphics/SVG/1.2/DTD/svg12.dtd\">\n";
164 header << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.2\" "
165 << "baseProfile=\"full\" xml:space=\"preserve\"";
166 if ( width.size() ) header << " width=\""<<width<<"\"";
167 if ( height.size() ) header << " height=\""<<height<<"\"";
168
169 header << ">\n<desc>" << docTitle << "</desc>\n";
170
171 if ( styleSheetPath.empty() ) {
172 header << "<defs><style type=\"text/css\">\n"
173 << "<![CDATA[\n";
174
175 header << "rect { fill:#ffffff; } \n";
176 header << "g { font-size: " << fontSize;
177 header << "; font-family: " << font << "; white-space: pre; }\n";
178
179 header << "]]>\n"
180 << "</style></defs>";
181 }
182
183 return header.str();
184 }
185
186 void SVGGenerator::printBody()
187 {
188 *out << "<g>\n<rect x=\"0\" y=\"0\" width=\"100%\" height=\"100%\"/>"; // rect: background color
189 int fontSizeSVG=10;
190 StringTools::str2num<int>(fontSizeSVG, fontSize, std::dec);
191
192 *out << "\n<text x=\"10\" y=\""<<fontSizeSVG*2<<"\">";
193 processInput();
194 *out << "</text>\n</g>\n";
195 }
196
197
198 string SVGGenerator::getFooter()
199 {
200 ostringstream os;
201 os <<"</svg>\n";
202
203 if (true) {
204 os << "<!-- SVG generated by ansifilter "
205 << ANSIFILTER_VERSION
206 << ", "
207 << ANSIFILTER_URL
208 <<" -->\n";
209 }
210 return os.str();
211 }
212
213 string SVGGenerator::maskCharacter ( unsigned char c )
214 {
215 switch ( c ) {
216 case ' ' :
217 return spacer;
218 break;
219 case '<' :
220 return "<";
221 break;
222 case '>' :
223 return ">";
224 break;
225 case '&' :
226 return "&";
227 break;
228 case '\"' :
229 return """;
230 break;
231 default:
232 return string ( 1, c );
233 }
234 }
235
236 void SVGGenerator::insertLineNumber()
237 {
238
239 int fontSizeSVG=10;
240 StringTools::str2num<int>(fontSizeSVG, fontSize, std::dec);
241
242 if ( showLineNumbers ) {
243 ostringstream lnum;
244 lnum << setw ( 5 ) << right;
245
246 if ( numberCurrentLine ) {
247 lnum << lineNumber;
248 *out<< "</text>\n<text x=\"10\" y=\""<< ( lineNumber*fontSizeSVG*2 ) <<"\">";
249 *out << lnum.str() ;
250 } else {
251 *out << lnum.str(); //for indentation
252 }
253 *out << " ";
254 } else {
255 *out<< "</text>\n<text x=\"10\" y=\""<< ( lineNumber*fontSizeSVG*2 ) <<"\">";
256
257 }
258 }
259
260
261 bool SVGGenerator::printDynamicStyleFile ( const std::string &outPath ) {
262
263 //do not overwrite
264 std::ifstream infile(outPath.c_str());
265 if (infile.good()) return true;
266
267 ofstream indexfile ( outPath.c_str() );
268
269 if ( !indexfile.fail() ) {
270 indexfile << "/* CSS generated by ansifilter - styles derived from document formatting\n Ansifilter will not overwrite this file\n*/\n";
271
272
273 indexfile << "rect { fill:#ffffff; }\n";
274 indexfile << "g { font-size: " << fontSize;
275 indexfile << "; font-family: " << font << "; white-space: pre; }\n";
276
277 for (unsigned int i=0; i<documentStyles.size();i++){
278 StyleInfo sInfo = documentStyles[i];
279 indexfile << "tspan.af_" << (i+1) <<" {";
280
281 if (sInfo.isBold) {
282 indexfile<< "font-weight:bold;";
283 }
284 if (sInfo.isItalic) {
285 indexfile<< "font-style:italic;";
286 }
287 if (sInfo.isBlink) {
288 indexfile<< "text-decoration:blink;";
289 }
290 if (sInfo.isUnderLine) {
291 indexfile<< "text-decoration:underline;";
292 }
293 if (sInfo.isConcealed) {
294 indexfile<< "display:none;";
295 }
296
297 if (sInfo.fgColor!="") {
298 indexfile << "fill:#"
299 << sInfo.fgColor
300 << ";";
301 }
302
303 /* if (sInfo.bgColor!="") {
304 indexfile << "background-color:#"
305 << sInfo.bgColor
306 << ";";
307 }*/
308
309 indexfile << "}\n";
310 }
311
312 } else {
313 return false;
314 }
315 return true;
316 }
317
318 }