Debian Codegraph @

src/lib/RVNGSVGDrawingGenerator.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* librevenge
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2006 Ariya Hidayat ([email protected])
11 * Copyright (C) 2005 Fridrich Strba ([email protected])
12 *
13 * For minor contributions see the git repository.
14 *
15 * Alternatively, the contents of this file may be used under the terms
16 * of the GNU Lesser General Public License Version 2.1 or later
17 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
18 * applicable instead of those above.
19 */
20
21 #include <librevenge-generators/librevenge-generators.h>
22
23 #include <stdio.h>
24
25 #include <iostream>
26 #include <map>
27 #include <memory>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31
32 #include "librevenge_internal.h"
33
34 namespace librevenge
35 {
36
37 namespace DrawingSVG
38 {
39
40 static double getInchValue(librevenge::RVNGProperty const &prop)
41 {
42 double value=prop.getDouble();
43 switch (prop.getUnit())
44 {
45 case librevenge::RVNG_GENERIC: // assume inch
46 case librevenge::RVNG_INCH:
47 return value;
48 case librevenge::RVNG_POINT:
49 value /= 72.;
50 return value;
51 case librevenge::RVNG_TWIP:
52 value /= 1440.;
53 return value;
54 case librevenge::RVNG_PERCENT:
55 case librevenge::RVNG_UNIT_ERROR:
56 default:
57 {
58 static bool first=true;
59 if (first)
60 {
61 RVNG_DEBUG_MSG(("librevenge::getInchValue: call with no double value\n"));
62 first=false;
63 }
64 break;
65 }
66 }
67 return value;
68 }
69
70 static std::string doubleToString(const double value)
71 {
72 std::unique_ptr<RVNGProperty> prop{RVNGPropertyFactory::newDoubleProp(value)};
73 std::string retVal = prop->getStr().cstr();
74 return retVal;
75 }
76
77 static unsigned stringToColour(const RVNGString &s)
78 {
79 std::string str(s.cstr());
80 if (str[0] == '#')
81 {
82 if (str.length() != 7)
83 return 0;
84 else
85 str.erase(str.begin());
86 }
87 else
88 return 0;
89
90 std::istringstream istr(str);
91 unsigned val = 0;
92 istr >> std::hex >> val;
93 return val;
94 }
95
96 //! basic class used to store table information
97 struct Table
98 {
99 //! constructor
100 Table(const RVNGPropertyList &propList) : m_column(0), m_row(0), m_x(0), m_y(0), m_columnsDistanceFromOrigin(), m_rowsDistanceFromOrigin()
101 {
102 if (propList["svg:x"])
103 m_x=getInchValue(*propList["svg:x"]);
104 if (propList["svg:y"])
105 m_y=getInchValue(*propList["svg:y"]);
106 // we do not actually use height/width, so...
107
108 m_columnsDistanceFromOrigin.push_back(0);
109 m_rowsDistanceFromOrigin.push_back(0);
110
111 const librevenge::RVNGPropertyListVector *columns = propList.child("librevenge:table-columns");
112 if (columns)
113 {
114 double actDist=0;
115 for (unsigned long i=0; i<columns->count(); ++i)
116 {
117 if ((*columns)[i]["style:column-width"])
118 actDist+=getInchValue(*(*columns)[i]["style:column-width"]);
119 m_columnsDistanceFromOrigin.push_back(actDist);
120 }
121 }
122 else
123 {
124 RVNG_DEBUG_MSG(("librevenge::DrawingSVG::Table::Table: can not find any columns\n"));
125 }
126 }
127 //! calls to open a row
128 void openRow(const RVNGPropertyList &propList)
129 {
130 double height=0;
131 if (propList["style:row-height"])
132 height=getInchValue(*propList["style:row-height"]);
133 // changeme
134 else if (propList["style:min-row-height"])
135 height=getInchValue(*propList["style:min-row-height"]);
136 else
137 {
138 RVNG_DEBUG_MSG(("librevenge::DrawingSVG::Table::openRow: can not determine row height\n"));
139 }
140 m_rowsDistanceFromOrigin.push_back(m_rowsDistanceFromOrigin.back()+height);
141 }
142 //! call to close a row
143 void closeRow()
144 {
145 ++m_row;
146 }
147 //! returns the position of a cellule
148 bool getPosition(int column, int row, double &x, double &y) const
149 {
150 bool ok=true;
151 if (column>=0 && column <int(m_columnsDistanceFromOrigin.size()))
152 x=m_x+m_columnsDistanceFromOrigin[size_t(column)];
153 else
154 {
155 ok=false;
156 RVNG_DEBUG_MSG(("librevenge::DrawingSVG::Table::getPosition: the column %d seems bad\n", column));
157 x=(column<0 || m_columnsDistanceFromOrigin.empty()) ? m_x : m_x + m_columnsDistanceFromOrigin.back();
158 }
159 if (row>=0 && row <int(m_rowsDistanceFromOrigin.size()))
160 y=m_y+m_rowsDistanceFromOrigin[size_t(row)];
161 else
162 {
163 ok=false;
164 RVNG_DEBUG_MSG(("librevenge::DrawingSVG::Table::getPosition: the row %d seems bad\n", row));
165 y=(row<0 || m_rowsDistanceFromOrigin.empty()) ? m_y : m_y + m_rowsDistanceFromOrigin.back();
166 }
167 return ok;
168 }
169 //! the actual column
170 int m_column;
171 //! the actual row
172 int m_row;
173 //! the origin table position in inches
174 double m_x, m_y;
175 //! the distance of each begin column in inches from origin
176 std::vector<double> m_columnsDistanceFromOrigin;
177 //! the distance of each begin row in inches from origin
178 std::vector<double> m_rowsDistanceFromOrigin;
179 };
180
181 } // DrawingSVG namespace
182
183 using namespace DrawingSVG;
184
185 struct RVNGSVGDrawingGeneratorPrivate
186 {
187 RVNGSVGDrawingGeneratorPrivate(RVNGStringVector &vec, const RVNGString &nmSpace);
188
189 void setStyle(const RVNGPropertyList &propList);
190 void writeStyle(bool isClosed=true);
191 void drawPolySomething(const RVNGPropertyListVector &vertices, bool isClosed);
192
193 //! return the namespace and the delimiter
194 std::string const &getNamespaceAndDelim() const
195 {
196 return m_nmSpaceAndDelim;
197 }
198
199 std::map<int, RVNGPropertyList> m_idSpanMap;
200
201 RVNGPropertyListVector m_gradient;
202 RVNGPropertyList m_style;
203 int m_gradientIndex, m_shadowIndex;
204 //! index uses when fill=bitmap
205 int m_patternIndex;
206 int m_arrowStartIndex /** start arrow index*/, m_arrowEndIndex /** end arrow index */;
207 //! groupId used if svg:id is not defined when calling openGroup
208 int m_groupId;
209 //! layerId used if svg:id is not defined when calling startLayer
210 int m_layerId;
211 //! a prefix used to define the svg namespace
212 std::string m_nmSpace;
213 //! a prefix used to define the svg namespace with delimiter
214 std::string m_nmSpaceAndDelim;
215 std::ostringstream m_outputSink;
216 RVNGStringVector &m_vec;
217 //! the actual master name
218 RVNGString m_masterName;
219 //! a map master name to master content
220 std::map<RVNGString, std::string> m_masterNameToContentMap;
221 //! the actual opened table
222 std::shared_ptr<Table> m_table;
223 };
224
225 RVNGSVGDrawingGeneratorPrivate::RVNGSVGDrawingGeneratorPrivate(RVNGStringVector &vec, const RVNGString &nmSpace) :
226 m_idSpanMap(),
227 m_gradient(),
228 m_style(),
229 m_gradientIndex(1),
230 m_shadowIndex(1),
231 m_patternIndex(1),
232 m_arrowStartIndex(1),
233 m_arrowEndIndex(1),
234 m_groupId(1000), m_layerId(1000),
235 m_nmSpace(nmSpace.cstr()),
236 m_nmSpaceAndDelim(""),
237 m_outputSink(),
238 m_vec(vec),
239 m_masterName(), m_masterNameToContentMap(),
240 m_table()
241 {
242 if (!m_nmSpace.empty())
243 m_nmSpaceAndDelim = m_nmSpace+":";
244 }
245
246 void RVNGSVGDrawingGeneratorPrivate::drawPolySomething(const RVNGPropertyListVector &vertices, bool isClosed)
247 {
248 if (vertices.count() < 2)
249 return;
250
251 if (vertices.count() == 2)
252 {
253 if (!vertices[0]["svg:x"]||!vertices[0]["svg:y"]||!vertices[1]["svg:x"]||!vertices[1]["svg:y"])
254 return;
255 m_outputSink << "<" << getNamespaceAndDelim() << "line ";
256 m_outputSink << "x1=\"" << doubleToString(72*getInchValue(*vertices[0]["svg:x"])) << "\" y1=\"" << doubleToString(72*getInchValue(*vertices[0]["svg:y"])) << "\" ";
257 m_outputSink << "x2=\"" << doubleToString(72*getInchValue(*vertices[1]["svg:x"])) << "\" y2=\"" << doubleToString(72*getInchValue(*vertices[1]["svg:y"])) << "\"\n";
258 writeStyle();
259 m_outputSink << "/>\n";
260 }
261 else
262 {
263 if (isClosed)
264 m_outputSink << "<" << getNamespaceAndDelim() << "polygon ";
265 else
266 m_outputSink << "<" << getNamespaceAndDelim() << "polyline ";
267
268 m_outputSink << "points=\"";
269 for (unsigned long i = 0; i < vertices.count(); i++)
270 {
271 if (!vertices[i]["svg:x"]||!vertices[i]["svg:y"])
272 continue;
273 m_outputSink << doubleToString(72*getInchValue(*vertices[i]["svg:x"])) << " " << doubleToString(72*getInchValue(*vertices[i]["svg:y"]));
274 if (i < vertices.count()-1)
275 m_outputSink << ", ";
276 }
277 m_outputSink << "\"\n";
278 writeStyle(isClosed);
279 m_outputSink << "/>\n";
280 }
281 }
282
283 void RVNGSVGDrawingGeneratorPrivate::setStyle(const RVNGPropertyList &propList)
284 {
285 m_style.clear();
286 m_style = propList;
287
288 const librevenge::RVNGPropertyListVector *gradient = propList.child("svg:linearGradient");
289 if (!gradient)
290 gradient = propList.child("svg:radialGradient");
291 m_gradient = gradient ? *gradient : librevenge::RVNGPropertyListVector();
292 if (m_style["draw:shadow"] && m_style["draw:shadow"]->getStr() == "visible" && m_style["draw:shadow-opacity"])
293 {
294 double shadowRed = 0.0;
295 double shadowGreen = 0.0;
296 double shadowBlue = 0.0;
297 if (m_style["draw:shadow-color"])
298 {
299 unsigned shadowColour = stringToColour(m_style["draw:shadow-color"]->getStr());
300 shadowRed = (double)((shadowColour & 0x00ff0000) >> 16)/255.0;
301 shadowGreen = (double)((shadowColour & 0x0000ff00) >> 8)/255.0;
302 shadowBlue = (double)(shadowColour & 0x000000ff)/255.0;
303 }
304 m_outputSink << "<" << getNamespaceAndDelim() << "defs>\n";
305 m_outputSink << "<" << getNamespaceAndDelim() << "filter filterUnits=\"userSpaceOnUse\" id=\"shadow" << m_shadowIndex++ << "\">";
306 m_outputSink << "<" << getNamespaceAndDelim() << "feOffset in=\"SourceGraphic\" result=\"offset\" ";
307 if (m_style["draw:shadow-offset-x"])
308 m_outputSink << "dx=\"" << doubleToString(72*getInchValue(*m_style["draw:shadow-offset-x"])) << "\" ";
309 if (m_style["draw:shadow-offset-y"])
310 m_outputSink << "dy=\"" << doubleToString(72*getInchValue(*m_style["draw:shadow-offset-y"])) << "\" ";
311 m_outputSink << "/>";
312 m_outputSink << "<" << getNamespaceAndDelim() << "feColorMatrix in=\"offset\" result=\"offset-color\" type=\"matrix\" values=\"";
313 m_outputSink << "0 0 0 0 " << doubleToString(shadowRed) ;
314 m_outputSink << " 0 0 0 0 " << doubleToString(shadowGreen);
315 m_outputSink << " 0 0 0 0 " << doubleToString(shadowBlue);
316 if (m_style["draw:opacity"] && m_style["draw:opacity"]->getDouble() < 1)
317 m_outputSink << " 0 0 0 " << doubleToString(m_style["draw:shadow-opacity"]->getDouble()/m_style["draw:opacity"]->getDouble()) << " 0\"/>";
318 else
319 m_outputSink << " 0 0 0 " << doubleToString(m_style["draw:shadow-opacity"]->getDouble()) << " 0\"/>";
320
321 m_outputSink << "<" << getNamespaceAndDelim() << "feMerge>";
322 m_outputSink << "<" << getNamespaceAndDelim() << "feMergeNode in=\"offset-color\" />";
323 m_outputSink << "<" << getNamespaceAndDelim() << "feMergeNode in=\"SourceGraphic\" />";
324 m_outputSink << "</" << getNamespaceAndDelim() << "feMerge>";
325 m_outputSink << "</" << getNamespaceAndDelim() << "filter>";
326 m_outputSink << "</" << getNamespaceAndDelim() << "defs>";
327 }
328
329 if (m_style["draw:fill"] && m_style["draw:fill"]->getStr() == "gradient")
330 {
331 double angle = (m_style["draw:angle"] ? m_style["draw:angle"]->getDouble() : 0.0);
332 angle *= -1.0;
333 while (angle < 0)
334 angle += 360;
335 while (angle > 360)
336 angle -= 360;
337
338 if (m_style["draw:style"] && (m_style["draw:style"]->getStr() == "radial" || m_style["draw:style"]->getStr() == "rectangular" ||
339 m_style["draw:style"]->getStr() == "square" || m_style["draw:style"]->getStr() == "ellipsoid"))
340 {
341 m_outputSink << "<" << getNamespaceAndDelim() << "defs>\n";
342 m_outputSink << " <" << getNamespaceAndDelim() << "radialGradient id=\"grad" << m_gradientIndex++ << "\"";
343
344 if (m_style["svg:cx"])
345 m_outputSink << " cx=\"" << m_style["svg:cx"]->getStr().cstr() << "\"";
346 else if (m_style["draw:cx"])
347 m_outputSink << " cx=\"" << m_style["draw:cx"]->getStr().cstr() << "\"";
348
349 if (m_style["svg:cy"])
350 m_outputSink << " cy=\"" << m_style["svg:cy"]->getStr().cstr() << "\"";
351 else if (m_style["draw:cy"])
352 m_outputSink << " cy=\"" << m_style["draw:cy"]->getStr().cstr() << "\"";
353 if (m_style["svg:r"])
354 m_outputSink << " r=\"" << m_style["svg:r"]->getStr().cstr() << "\"";
355 else if (m_style["draw:border"])
356 m_outputSink << " r=\"" << doubleToString((1 - m_style["draw:border"]->getDouble())*100.0) << "%\"";
357 else
358 m_outputSink << " r=\"100%\"";
359 m_outputSink << " >\n";
360 if (m_gradient.count())
361 {
362 for (unsigned long c = 0; c < m_gradient.count(); c++)
363 {
364 RVNGPropertyList const &grad=m_gradient[c];
365 m_outputSink << " <" << getNamespaceAndDelim() << "stop";
366 if (grad["svg:offset"])
367 m_outputSink << " offset=\"" << grad["svg:offset"]->getStr().cstr() << "\"";
368 if (grad["svg:stop-color"])
369 m_outputSink << " stop-color=\"" << grad["svg:stop-color"]->getStr().cstr() << "\"";
370 if (grad["svg:stop-opacity"])
371 m_outputSink << " stop-opacity=\"" << doubleToString(grad["svg:stop-opacity"]->getDouble()) << "\"";
372 m_outputSink << "/>" << std::endl;
373 }
374 }
375 else if (m_style["draw:start-color"] && m_style["draw:end-color"])
376 {
377 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"0%\"";
378 m_outputSink << " stop-color=\"" << m_style["draw:end-color"]->getStr().cstr() << "\"";
379 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:end-opacity"] ? m_style["librevenge:end-opacity"]->getDouble() : 1) << "\" />" << std::endl;
380
381 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"100%\"";
382 m_outputSink << " stop-color=\"" << m_style["draw:start-color"]->getStr().cstr() << "\"";
383 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:start-opacity"] ? m_style["librevenge:start-opacity"]->getDouble() : 1) << "\" />" << std::endl;
384 }
385 m_outputSink << " </" << getNamespaceAndDelim() << "radialGradient>\n";
386 m_outputSink << "</" << getNamespaceAndDelim() << "defs>\n";
387 }
388 else if (!m_style["draw:style"] || m_style["draw:style"]->getStr() == "linear" || m_style["draw:style"]->getStr() == "axial")
389 {
390 m_outputSink << "<" << getNamespaceAndDelim() << "defs>\n";
391 m_outputSink << " <" << getNamespaceAndDelim() << "linearGradient id=\"grad" << m_gradientIndex++ << "\" >\n";
392
393 if (m_gradient.count())
394 {
395 bool canBuildAxial = false;
396 if (m_style["draw:style"] && m_style["draw:style"]->getStr() == "axial")
397 {
398 // check if we can reconstruct the linear offset, ie. if each offset is a valid percent%
399 canBuildAxial = true;
400 for (unsigned long c = 0; c < m_gradient.count(); ++c)
401 {
402 RVNGPropertyList const &grad=m_gradient[c];
403 if (!grad["svg:offset"] || grad["svg:offset"]->getDouble()<0 || grad["svg:offset"]->getDouble() > 1)
404 {
405 canBuildAxial=false;
406 break;
407 }
408 RVNGString str=grad["svg:offset"]->getStr();
409 int len=str.len();
410 if (len<1 || str.cstr()[len-1]!='%')
411 {
412 canBuildAxial=false;
413 break;
414 }
415 }
416 }
417 if (canBuildAxial)
418 {
419 for (unsigned long c = m_gradient.count(); c>0 ;)
420 {
421 RVNGPropertyList const &grad=m_gradient[--c];
422 m_outputSink << " <" << getNamespaceAndDelim() << "stop ";
423 if (grad["svg:offset"])
424 m_outputSink << "offset=\"" << doubleToString(50.-50.*grad["svg:offset"]->getDouble()) << "%\"";
425 if (grad["svg:stop-color"])
426 m_outputSink << " stop-color=\"" << grad["svg:stop-color"]->getStr().cstr() << "\"";
427 if (grad["svg:stop-opacity"])
428 m_outputSink << " stop-opacity=\"" << doubleToString(grad["svg:stop-opacity"]->getDouble()) << "\"";
429 m_outputSink << "/>" << std::endl;
430 }
431 for (unsigned long c = 0; c < m_gradient.count(); ++c)
432 {
433 RVNGPropertyList const &grad=m_gradient[c];
434 if (c==0 && grad["svg:offset"] && grad["svg:offset"]->getDouble() <= 0)
435 continue;
436 m_outputSink << " <" << getNamespaceAndDelim() << "stop ";
437 if (grad["svg:offset"])
438 m_outputSink << "offset=\"" << doubleToString(50.+50.*grad["svg:offset"]->getDouble()) << "%\"";
439 if (grad["svg:stop-color"])
440 m_outputSink << " stop-color=\"" << grad["svg:stop-color"]->getStr().cstr() << "\"";
441 if (grad["svg:stop-opacity"])
442 m_outputSink << " stop-opacity=\"" << doubleToString(grad["svg:stop-opacity"]->getDouble()) << "\"";
443 m_outputSink << "/>" << std::endl;
444 }
445 }
446 else
447 {
448 for (unsigned long c = 0; c < m_gradient.count(); c++)
449 {
450 RVNGPropertyList const &grad=m_gradient[c];
451 m_outputSink << " <" << getNamespaceAndDelim() << "stop";
452 if (grad["svg:offset"])
453 m_outputSink << " offset=\"" << grad["svg:offset"]->getStr().cstr() << "\"";
454 if (grad["svg:stop-color"])
455 m_outputSink << " stop-color=\"" << grad["svg:stop-color"]->getStr().cstr() << "\"";
456 if (grad["svg:stop-opacity"])
457 m_outputSink << " stop-opacity=\"" << doubleToString(grad["svg:stop-opacity"]->getDouble()) << "\"";
458 m_outputSink << "/>" << std::endl;
459 }
460 }
461 }
462 else if (m_style["draw:start-color"] && m_style["draw:end-color"])
463 {
464 if (!m_style["draw:style"] || m_style["draw:style"]->getStr() == "linear")
465 {
466 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"0%\"";
467 m_outputSink << " stop-color=\"" << m_style["draw:start-color"]->getStr().cstr() << "\"";
468 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:start-opacity"] ? m_style["librevenge:start-opacity"]->getDouble() : 1) << "\" />" << std::endl;
469
470 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"100%\"";
471 m_outputSink << " stop-color=\"" << m_style["draw:end-color"]->getStr().cstr() << "\"";
472 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:end-opacity"] ? m_style["librevenge:end-opacity"]->getDouble() : 1) << "\" />" << std::endl;
473 }
474 else
475 {
476 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"0%\"";
477 m_outputSink << " stop-color=\"" << m_style["draw:end-color"]->getStr().cstr() << "\"";
478 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:end-opacity"] ? m_style["librevenge:end-opacity"]->getDouble() : 1) << "\" />" << std::endl;
479
480 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"50%\"";
481 m_outputSink << " stop-color=\"" << m_style["draw:start-color"]->getStr().cstr() << "\"";
482 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:start-opacity"] ? m_style["librevenge:start-opacity"]->getDouble() : 1) << "\" />" << std::endl;
483
484 m_outputSink << " <" << getNamespaceAndDelim() << "stop offset=\"100%\"";
485 m_outputSink << " stop-color=\"" << m_style["draw:end-color"]->getStr().cstr() << "\"";
486 m_outputSink << " stop-opacity=\"" << doubleToString(m_style["librevenge:end-opacity"] ? m_style["librevenge:end-opacity"]->getDouble() : 1) << "\" />" << std::endl;
487 }
488 }
489 m_outputSink << " </" << getNamespaceAndDelim() << "linearGradient>\n";
490
491 // not a simple horizontal gradient
492 if (angle<270 || angle>270)
493 {
494 m_outputSink << " <" << getNamespaceAndDelim() << "linearGradient xlink:href=\"#grad" << m_gradientIndex-1 << "\"";
495 m_outputSink << " id=\"grad" << m_gradientIndex++ << "\" ";
496 m_outputSink << "x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\" ";
497 m_outputSink << "gradientTransform=\"rotate(" << angle << " .5 .5)\" ";
498 m_outputSink << "gradientUnits=\"objectBoundingBox\" >\n";
499 m_outputSink << " </" << getNamespaceAndDelim() << "linearGradient>\n";
500 }
501
502 m_outputSink << "</" << getNamespaceAndDelim() << "defs>\n";
503 }
504 }
505 else if (m_style["draw:fill"] && m_style["draw:fill"]->getStr() == "bitmap" && m_style["draw:fill-image"] && m_style["librevenge:mime-type"])
506 {
507 m_outputSink << "<" << getNamespaceAndDelim() << "defs>\n";
508 m_outputSink << " <" << getNamespaceAndDelim() << "pattern id=\"img" << m_patternIndex++ << "\" patternUnits=\"userSpaceOnUse\" ";
509 if (m_style["svg:width"])
510 m_outputSink << "width=\"" << doubleToString(72*getInchValue(*m_style["svg:width"])) << "\" ";
511 else
512 m_outputSink << "width=\"100\" ";
513
514 if (m_style["svg:height"])
515 m_outputSink << "height=\"" << doubleToString(72*getInchValue(*m_style["svg:height"])) << "\">" << std::endl;
516 else
517 m_outputSink << "height=\"100\">" << std::endl;
518 m_outputSink << "<" << getNamespaceAndDelim() << "image ";
519
520 if (m_style["svg:x"])
521 m_outputSink << "x=\"" << doubleToString(72*getInchValue(*m_style["svg:x"])) << "\" ";
522 else
523 m_outputSink << "x=\"0\" ";
524
525 if (m_style["svg:y"])
526 m_outputSink << "y=\"" << doubleToString(72*getInchValue(*m_style["svg:y"])) << "\" ";
527 else
528 m_outputSink << "y=\"0\" ";
529
530 if (m_style["svg:width"])
531 m_outputSink << "width=\"" << doubleToString(72*getInchValue(*m_style["svg:width"])) << "\" ";
532 else
533 m_outputSink << "width=\"100\" ";
534
535 if (m_style["svg:height"])
536 m_outputSink << "height=\"" << doubleToString(72*getInchValue(*m_style["svg:height"])) << "\" ";
537 else
538 m_outputSink << "height=\"100\" ";
539
540 m_outputSink << "xlink:href=\"data:" << m_style["librevenge:mime-type"]->getStr().cstr() << ";base64,";
541 m_outputSink << m_style["draw:fill-image"]->getStr().cstr();
542 m_outputSink << "\" />\n";
543 m_outputSink << " </" << getNamespaceAndDelim() << "pattern>\n";
544 m_outputSink << "</" << getNamespaceAndDelim() << "defs>\n";
545 }
546
547 // check for arrow and if find some, define a basic arrow
548 if (m_style["draw:marker-start-path"])
549 {
550 m_outputSink << "<" << getNamespaceAndDelim() << "defs>\n";
551 m_outputSink << "<" << getNamespaceAndDelim() << "marker id=\"startMarker" << m_arrowStartIndex++ << "\" ";
552 m_outputSink << " markerUnits=\"strokeWidth\" orient=\"auto\" markerWidth=\"8\" markerHeight=\"6\"\n";
553 m_outputSink << " viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\">\n";
554 m_outputSink << "<" << getNamespaceAndDelim() << "polyline points=\"10,0 0,5 10,10 9,5\" fill=\"solid\" />\n";
555 m_outputSink << "</" << getNamespaceAndDelim() << "marker>\n";
556 m_outputSink << "</" << getNamespaceAndDelim() << "defs>\n";
557 }
558 if (m_style["draw:marker-end-path"])
559 {
560 m_outputSink << "<" << getNamespaceAndDelim() << "defs>\n";
561 m_outputSink << "<" << getNamespaceAndDelim() << "marker id=\"endMarker" << m_arrowEndIndex++ << "\" ";
562 m_outputSink << " markerUnits=\"strokeWidth\" orient=\"auto\" markerWidth=\"8\" markerHeight=\"6\"\n";
563 m_outputSink << " viewBox=\"0 0 10 10\" refX=\"1\" refY=\"5\">\n";
564 m_outputSink << "<" << getNamespaceAndDelim() << "polyline points=\"0,0 10,5 0,10 1,5\" fill=\"solid\" />\n";
565 m_outputSink << "</" << getNamespaceAndDelim() << "marker>\n";
566 m_outputSink << "</" << getNamespaceAndDelim() << "defs>\n";
567 }
568 }
569
570 // create "style" attribute based on current pen and brush
571 void RVNGSVGDrawingGeneratorPrivate::writeStyle(bool /* isClosed */)
572 {
573 m_outputSink << "style=\"";
574
575 double width = 1.0 / 72.0;
576 if (m_style["svg:stroke-width"])
577 {
578 width = getInchValue(*m_style["svg:stroke-width"]);
579 #if 0
580 // add me in libmspub and libcdr
581 if (width <= 0.0 && m_style["draw:stroke"] && m_style["draw:stroke"]->getStr() != "none")
582 width = 0.2 / 72.0; // reasonable hairline
583 #endif
584 m_outputSink << "stroke-width: " << doubleToString(72*width) << "; ";
585 }
586
587 if (m_style["draw:stroke"] && m_style["draw:stroke"]->getStr() != "none")
588 {
589 if (m_style["svg:stroke-color"])
590 m_outputSink << "stroke: " << m_style["svg:stroke-color"]->getStr().cstr() << "; ";
591 if (m_style["svg:stroke-opacity"] && m_style["svg:stroke-opacity"]->getInt()!= 1)
592 m_outputSink << "stroke-opacity: " << doubleToString(m_style["svg:stroke-opacity"]->getDouble()) << "; ";
593 }
594
595 if (m_style["draw:stroke"] && m_style["draw:stroke"]->getStr() == "solid")
596 m_outputSink << "stroke-dasharray: none; ";
597 else if (m_style["draw:stroke"] && m_style["draw:stroke"]->getStr() == "dash")
598 {
599 int dots1 = m_style["draw:dots1"] ? m_style["draw:dots1"]->getInt() : 0;
600 int dots2 = m_style["draw:dots2"] ? m_style["draw:dots2"]->getInt() : 0;
601 double dots1len = 72.*width, dots2len = 72.*width, gap = 72.*width;
602 if (m_style["draw:dots1-length"])
603 {
604 if (m_style["draw:dots1-length"]->getUnit()==librevenge::RVNG_PERCENT)
605 dots1len=72*m_style["draw:dots1-length"]->getDouble()*width;
606 else
607 dots1len=72*getInchValue(*m_style["draw:dots1-length"]);
608 }
609 if (m_style["draw:dots2-length"])
610 {
611 if (m_style["draw:dots2-length"]->getUnit()==librevenge::RVNG_PERCENT)
612 dots2len=72*m_style["draw:dots2-length"]->getDouble()*width;
613 else
614 dots2len=72*getInchValue(*m_style["draw:dots2-length"]);
615 }
616 if (m_style["draw:distance"])
617 {
618 if (m_style["draw:distance"]->getUnit()==librevenge::RVNG_PERCENT)
619 gap=72*m_style["draw:distance"]->getDouble()*width;
620 else
621 gap=72*getInchValue(*m_style["draw:distance"]);
622 }
623 m_outputSink << "stroke-dasharray: ";
624 for (int i = 0; i < dots1; i++)
625 {
626 if (i)
627 m_outputSink << ", ";
628 m_outputSink << doubleToString(dots1len);
629 m_outputSink << ", ";
630 m_outputSink << doubleToString(gap);
631 }
632 for (int j = 0; j < dots2; j++)
633 {
634 m_outputSink << ", ";
635 m_outputSink << doubleToString(dots2len);
636 m_outputSink << ", ";
637 m_outputSink << doubleToString(gap);
638 }
639 m_outputSink << "; ";
640 }
641
642 if (m_style["svg:stroke-linecap"])
643 m_outputSink << "stroke-linecap: " << m_style["svg:stroke-linecap"]->getStr().cstr() << "; ";
644
645 if (m_style["svg:stroke-linejoin"])
646 m_outputSink << "stroke-linejoin: " << m_style["svg:stroke-linejoin"]->getStr().cstr() << "; ";
647
648 if (m_style["draw:fill"] && m_style["draw:fill"]->getStr() == "none")
649 m_outputSink << "fill: none; ";
650 else if (m_style["svg:fill-rule"])
651 m_outputSink << "fill-rule: " << m_style["svg:fill-rule"]->getStr().cstr() << "; ";
652
653 if (m_style["draw:fill"] && m_style["draw:fill"]->getStr() == "gradient")
654 m_outputSink << "fill: url(#grad" << m_gradientIndex-1 << "); ";
655 else if (m_style["draw:fill"] && m_style["draw:fill"]->getStr() == "bitmap")
656 m_outputSink << "fill: url(#img" << m_patternIndex-1 << "); ";
657
658 if (m_style["draw:shadow"] && m_style["draw:shadow"]->getStr() == "visible")
659 m_outputSink << "filter:url(#shadow" << m_shadowIndex-1 << "); ";
660
661 if (m_style["draw:fill"] && m_style["draw:fill"]->getStr() == "solid")
662 if (m_style["draw:fill-color"])
663 m_outputSink << "fill: " << m_style["draw:fill-color"]->getStr().cstr() << "; ";
664 if (m_style["draw:opacity"] && m_style["draw:opacity"]->getDouble() < 1)
665 m_outputSink << "fill-opacity: " << doubleToString(m_style["draw:opacity"]->getDouble()) << "; ";
666
667 if (m_style["draw:marker-start-path"])
668 m_outputSink << "marker-start: url(#startMarker" << m_arrowStartIndex-1 << "); ";
669 if (m_style["draw:marker-end-path"])
670 m_outputSink << "marker-end: url(#endMarker" << m_arrowEndIndex-1 << "); ";
671
672 m_outputSink << "\""; // style
673 }
674
675
676 RVNGSVGDrawingGenerator::RVNGSVGDrawingGenerator(RVNGStringVector &vec, const RVNGString &nmSpace) :
677 m_pImpl(new RVNGSVGDrawingGeneratorPrivate(vec, nmSpace))
678 {
679 }
680
681 RVNGSVGDrawingGenerator::~RVNGSVGDrawingGenerator()
682 {
683 delete m_pImpl;
684 }
685
686 void RVNGSVGDrawingGenerator::startDocument(const RVNGPropertyList & /*propList*/) {}
687 void RVNGSVGDrawingGenerator::endDocument() {}
688 void RVNGSVGDrawingGenerator::setDocumentMetaData(const RVNGPropertyList & /*propList*/) {}
689 void RVNGSVGDrawingGenerator::defineEmbeddedFont(const RVNGPropertyList & /*propList*/) {}
690
691 void RVNGSVGDrawingGenerator::startPage(const RVNGPropertyList &propList)
692 {
693 if (propList["librevenge:master-page-name"])
694 {
695 if (m_pImpl->m_masterNameToContentMap.find(propList["librevenge:master-page-name"]->getStr())!=
696 m_pImpl->m_masterNameToContentMap.end())
697 {
698 m_pImpl->m_outputSink << m_pImpl->m_masterNameToContentMap.find(propList["librevenge:master-page-name"]->getStr())->second;
699 return;
700 }
701 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::startPage: can not find page with given master name\n"));
702 }
703
704 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "svg version=\"1.1\" xmlns";
705 m_pImpl->m_outputSink << (m_pImpl->m_nmSpace.empty() ? "" : ":") << m_pImpl->m_nmSpace << "=\"http://www.w3.org/2000/svg\" ";
706 m_pImpl->m_outputSink << "xmlns:xlink=\"http://www.w3.org/1999/xlink\" ";
707 if (propList["svg:width"])
708 m_pImpl->m_outputSink << "width=\"" << doubleToString(getInchValue(*propList["svg:width"])) << "in\" ";
709 if (propList["svg:height"])
710 m_pImpl->m_outputSink << "height=\"" << doubleToString(getInchValue(*propList["svg:height"])) << "in\" ";
711 if (propList["svg:width"] && propList["svg:height"])
712 {
713 m_pImpl->m_outputSink << "viewBox=\"0 0 " << doubleToString(72*getInchValue(*propList["svg:width"]));
714 m_pImpl->m_outputSink << " " << doubleToString(72*getInchValue(*propList["svg:height"])) << "\"";
715 }
716 m_pImpl->m_outputSink << " >\n";
717 }
718
719 void RVNGSVGDrawingGenerator::endPage()
720 {
721 m_pImpl->m_outputSink << "</" << m_pImpl->getNamespaceAndDelim() << "svg>\n";
722 m_pImpl->m_vec.append(m_pImpl->m_outputSink.str().c_str());
723 m_pImpl->m_outputSink.str("");
724 }
725
726 void RVNGSVGDrawingGenerator::startMasterPage(const RVNGPropertyList &propList)
727 {
728 if (!m_pImpl->m_masterName.empty())
729 {
730 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::startMasterPage: a master page is already opened\n"));
731 }
732 else if (propList["librevenge:master-page-name"])
733 {
734 m_pImpl->m_masterName=propList["librevenge:master-page-name"]->getStr();
735 RVNGPropertyList pList(propList);
736 pList.remove("librevenge:master-page-name");
737 startPage(pList);
738 }
739 else
740 {
741 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::startMasterPage: can not find the master name\n"));
742 }
743 }
744
745 void RVNGSVGDrawingGenerator::endMasterPage()
746 {
747 if (m_pImpl->m_masterName.empty())
748 {
749 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::endMasterPage: no master pages are opened\n"));
750 }
751 else
752 {
753 if (m_pImpl->m_masterNameToContentMap.find(m_pImpl->m_masterName)!=m_pImpl->m_masterNameToContentMap.end())
754 {
755 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::endMasterPage: a master page with name %s already exists\n",
756 m_pImpl->m_masterName.cstr()));
757 }
758 // no need to close the page, this will be done when the master page will be used
759 m_pImpl->m_masterNameToContentMap[m_pImpl->m_masterName]=m_pImpl->m_outputSink.str();
760 m_pImpl->m_masterName.clear();
761 }
762 // reset the content
763 m_pImpl->m_outputSink.str("");
764 }
765
766 void RVNGSVGDrawingGenerator::startLayer(const RVNGPropertyList &propList)
767 {
768 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "g";
769 librevenge::RVNGString layer("Layer");
770 if (propList["draw:layer"])
771 layer.append(propList["draw:layer"]->getStr());
772 else if (propList["svg:id"])
773 layer.append(propList["svg:id"]->getStr());
774 else
775 layer.sprintf("Layer%d", m_pImpl->m_layerId++);
776 librevenge::RVNGString finalName("");
777 finalName.appendEscapedXML(layer);
778 m_pImpl->m_outputSink << " id=\"" << finalName.cstr() << "\"";
779 if (propList["svg:fill-rule"])
780 m_pImpl->m_outputSink << " fill-rule=\"" << propList["svg:fill-rule"]->getStr().cstr() << "\"";
781 m_pImpl->m_outputSink << " >\n";
782 }
783
784 void RVNGSVGDrawingGenerator::endLayer()
785 {
786 m_pImpl->m_outputSink << "</" << m_pImpl->getNamespaceAndDelim() << "g>\n";
787 }
788
789 void RVNGSVGDrawingGenerator::startEmbeddedGraphics(const RVNGPropertyList & /*propList*/) {}
790 void RVNGSVGDrawingGenerator::endEmbeddedGraphics() {}
791
792 void RVNGSVGDrawingGenerator::openGroup(const RVNGPropertyList & /*propList*/)
793 {
794 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "g";
795 librevenge::RVNGString group;
796 group.sprintf("Group%d", m_pImpl->m_groupId++);
797 m_pImpl->m_outputSink << " id=\"" << group.cstr() << "\"";
798 m_pImpl->m_outputSink << " >\n";
799 }
800
801 void RVNGSVGDrawingGenerator::closeGroup()
802 {
803 m_pImpl->m_outputSink << "</" << m_pImpl->getNamespaceAndDelim() << "g>\n";
804 }
805
806 void RVNGSVGDrawingGenerator::setStyle(const RVNGPropertyList &propList)
807 {
808 m_pImpl->setStyle(propList);
809 }
810
811 void RVNGSVGDrawingGenerator::drawRectangle(const RVNGPropertyList &propList)
812 {
813 if (!propList["svg:x"] || !propList["svg:y"] || !propList["svg:width"] || !propList["svg:height"])
814 return;
815 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "rect ";
816 m_pImpl->m_outputSink << "x=\"" << doubleToString(72*getInchValue(*propList["svg:x"])) << "\" y=\"" << doubleToString(72*getInchValue(*propList["svg:y"])) << "\" ";
817 m_pImpl->m_outputSink << "width=\"" << doubleToString(72*getInchValue(*propList["svg:width"])) << "\" height=\"" << doubleToString(72*getInchValue(*propList["svg:height"])) << "\" ";
818 if (propList["svg:rx"] && propList["svg:rx"]->getDouble() > 0 && propList["svg:ry"] && propList["svg:ry"]->getDouble()>0)
819 m_pImpl->m_outputSink << "rx=\"" << doubleToString(72*getInchValue(*propList["svg:rx"])) << "\" ry=\"" << doubleToString(72*getInchValue(*propList["svg:ry"])) << "\" ";
820 m_pImpl->writeStyle();
821 m_pImpl->m_outputSink << "/>\n";
822 }
823
824 void RVNGSVGDrawingGenerator::drawEllipse(const RVNGPropertyList &propList)
825 {
826 if (!propList["svg:cx"] || !propList["svg:cy"] || !propList["svg:rx"] || !propList["svg:ry"])
827 return;
828 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "ellipse ";
829 m_pImpl->m_outputSink << "cx=\"" << doubleToString(72*getInchValue(*propList["svg:cx"])) << "\" cy=\"" << doubleToString(72*getInchValue(*propList["svg:cy"])) << "\" ";
830 m_pImpl->m_outputSink << "rx=\"" << doubleToString(72*getInchValue(*propList["svg:rx"])) << "\" ry=\"" << doubleToString(72*getInchValue(*propList["svg:ry"])) << "\" ";
831 m_pImpl->writeStyle();
832 if (propList["librevenge:rotate"] && (propList["librevenge:rotate"]->getDouble()<0||propList["librevenge:rotate"]->getDouble()>0))
833 m_pImpl->m_outputSink << " transform=\" rotate(" << doubleToString(-propList["librevenge:rotate"]->getDouble())
834 << ", " << doubleToString(72*getInchValue(*propList["svg:cx"]))
835 << ", " << doubleToString(72*getInchValue(*propList["svg:cy"]))
836 << ")\" ";
837 m_pImpl->m_outputSink << "/>\n";
838 }
839
840 void RVNGSVGDrawingGenerator::drawPolyline(const RVNGPropertyList &propList)
841 {
842 const RVNGPropertyListVector *vertices = propList.child("svg:points");
843 if (vertices && vertices->count())
844 m_pImpl->drawPolySomething(*vertices, false);
845 }
846
847 void RVNGSVGDrawingGenerator::drawPolygon(const RVNGPropertyList &propList)
848 {
849 const RVNGPropertyListVector *vertices = propList.child("svg:points");
850 if (vertices && vertices->count())
851 m_pImpl->drawPolySomething(*vertices, true);
852 }
853
854 void RVNGSVGDrawingGenerator::drawPath(const RVNGPropertyList &propList)
855 {
856 const RVNGPropertyListVector *path = propList.child("svg:d");
857 if (!path)
858 return;
859 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "path d=\" ";
860 bool isClosed = false;
861 unsigned long i=0;
862 for (i=0; i < path->count(); i++)
863 {
864 RVNGPropertyList pList((*path)[i]);
865 if (!pList["librevenge:path-action"]) continue;
866 std::string action=pList["librevenge:path-action"]->getStr().cstr();
867 if (action.length()!=1) continue;
868 bool coordOk=pList["svg:x"]&&pList["svg:y"];
869 bool coord1Ok=coordOk && pList["svg:x1"]&&pList["svg:y1"];
870 bool coord2Ok=coord1Ok && pList["svg:x2"]&&pList["svg:y2"];
871 if (pList["svg:x"] && action[0] == 'H')
872 m_pImpl->m_outputSink << "\nH" << doubleToString(72*getInchValue(*pList["svg:x"]));
873 else if (pList["svg:y"] && action[0] == 'V')
874 m_pImpl->m_outputSink << "\nV" << doubleToString(72*getInchValue(*pList["svg:y"]));
875 else if (coordOk && (action[0] == 'M' || action[0] == 'L' || action[0] == 'T'))
876 {
877 m_pImpl->m_outputSink << "\n" << action;
878 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x"])) << "," << doubleToString(72*getInchValue(*pList["svg:y"]));
879 }
880 else if (coord1Ok && (action[0] == 'Q' || action[0] == 'S'))
881 {
882 m_pImpl->m_outputSink << "\n" << action;
883 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x1"])) << "," << doubleToString(72*getInchValue(*pList["svg:y1"])) << " ";
884 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x"])) << "," << doubleToString(72*getInchValue(*pList["svg:y"]));
885 }
886 else if (coord2Ok && action[0] == 'C')
887 {
888 m_pImpl->m_outputSink << "\nC";
889 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x1"])) << "," << doubleToString(72*getInchValue(*pList["svg:y1"])) << " ";
890 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x2"])) << "," << doubleToString(72*getInchValue(*pList["svg:y2"])) << " ";
891 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x"])) << "," << doubleToString(72*getInchValue(*pList["svg:y"]));
892 }
893 else if (coordOk && pList["svg:rx"] && pList["svg:ry"] && action[0] == 'A')
894 {
895 m_pImpl->m_outputSink << "\nA";
896 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:rx"])) << "," << doubleToString(72*getInchValue(*pList["svg:ry"])) << " ";
897 m_pImpl->m_outputSink << doubleToString(pList["librevenge:rotate"] ? pList["librevenge:rotate"]->getDouble() : 0) << " ";
898 m_pImpl->m_outputSink << (pList["librevenge:large-arc"] ? pList["librevenge:large-arc"]->getInt() : 1) << ",";
899 m_pImpl->m_outputSink << (pList["librevenge:sweep"] ? pList["librevenge:sweep"]->getInt() : 1) << " ";
900 m_pImpl->m_outputSink << doubleToString(72*getInchValue(*pList["svg:x"])) << "," << doubleToString(72*getInchValue(*pList["svg:y"]));
901 }
902 else if (action[0] == 'Z')
903 {
904 isClosed = true;
905 m_pImpl->m_outputSink << "\nZ";
906 }
907 }
908
909 m_pImpl->m_outputSink << "\" \n";
910 m_pImpl->writeStyle(isClosed);
911 m_pImpl->m_outputSink << "/>\n";
912 }
913
914 void RVNGSVGDrawingGenerator::drawGraphicObject(const RVNGPropertyList &propList)
915 {
916 if (!propList["librevenge:mime-type"] || propList["librevenge:mime-type"]->getStr().len() <= 0)
917 return;
918 if (!propList["office:binary-data"])
919 return;
920 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "image ";
921 if (propList["svg:x"] && propList["svg:y"] && propList["svg:width"] && propList["svg:height"])
922 {
923 double x=getInchValue(*propList["svg:x"]);
924 double y=getInchValue(*propList["svg:y"]);
925 double width=getInchValue(*propList["svg:width"]);
926 double height=getInchValue(*propList["svg:height"]);
927 bool flipX(propList["draw:mirror-horizontal"] && propList["draw:mirror-horizontal"]->getInt());
928 bool flipY(propList["draw:mirror-vertical"] && propList["draw:mirror-vertical"]->getInt());
929
930 m_pImpl->m_outputSink << "x=\"" << doubleToString(72*x) << "\" y=\"" << doubleToString(72*y) << "\" ";
931 m_pImpl->m_outputSink << "width=\"" << doubleToString(72*width) << "\" height=\"" << doubleToString(72*height) << "\" ";
932 if (flipX || flipY || propList["librevenge:rotate"])
933 {
934 double xmiddle = x + width / 2.0;
935 double ymiddle = y + height / 2.0;
936 m_pImpl->m_outputSink << "transform=\"";
937 m_pImpl->m_outputSink << " translate(" << doubleToString(72*xmiddle) << ", " << doubleToString(72*ymiddle) << ") ";
938 m_pImpl->m_outputSink << " scale(" << (flipX ? "-1" : "1") << ", " << (flipY ? "-1" : "1") << ") ";
939 // rotation is around the center of the object's bounding box
940 if (propList["librevenge:rotate"])
941 {
942 double angle(propList["librevenge:rotate"]->getDouble());
943 while (angle > 180.0)
944 angle -= 360.0;
945 while (angle < -180.0)
946 angle += 360.0;
947 m_pImpl->m_outputSink << " rotate(" << doubleToString(angle) << ") ";
948 }
949 m_pImpl->m_outputSink << " translate(" << doubleToString(-72*xmiddle) << ", " << doubleToString(-72*ymiddle) << ") ";
950 m_pImpl->m_outputSink << "\" ";
951 }
952 }
953 m_pImpl->m_outputSink << "xlink:href=\"data:" << propList["librevenge:mime-type"]->getStr().cstr() << ";base64,";
954 m_pImpl->m_outputSink << propList["office:binary-data"]->getStr().cstr();
955 m_pImpl->m_outputSink << "\" />\n";
956 }
957
958 void RVNGSVGDrawingGenerator::drawConnector(const RVNGPropertyList &/*propList*/)
959 {
960 // TODO: implement me
961 }
962
963 void RVNGSVGDrawingGenerator::startTextObject(const RVNGPropertyList &propList)
964 {
965 double x = 0.0;
966 double y = 0.0;
967 double height = 0.0;
968 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "text ";
969 if (propList["svg:x"] && propList["svg:y"])
970 {
971 x = getInchValue(*propList["svg:x"]);
972 y = getInchValue(*propList["svg:y"]);
973 }
974
975 double xmiddle = x;
976 double ymiddle = y;
977
978 if (propList["svg:width"])
979 {
980 double width = getInchValue(*propList["svg:width"]);
981 xmiddle += width / 2.0;
982 }
983
984 if (propList["svg:height"])
985 {
986 height = getInchValue(*propList["svg:height"]);
987 ymiddle += height / 2.0;
988 }
989
990 if (propList["draw:textarea-vertical-align"])
991 {
992 if (propList["draw:textarea-vertical-align"]->getStr() == "middle")
993 y = ymiddle;
994 if (propList["draw:textarea-vertical-align"]->getStr() == "bottom")
995 {
996 y += height;
997 if (propList["fo:padding-bottom"])
998 y -= propList["fo:padding-bottom"]->getDouble();
999 }
1000 }
1001 else
1002 y += height;
1003
1004 if (propList["fo:padding-left"])
1005 x += propList["fo:padding-left"]->getDouble();
1006
1007 m_pImpl->m_outputSink << "x=\"" << doubleToString(72*x) << "\" y=\"" << doubleToString(72*y) << "\"";
1008
1009 // rotation is around the center of the object's bounding box
1010 if (propList["librevenge:rotate"] && (propList["librevenge:rotate"]->getDouble()<0||propList["librevenge:rotate"]->getDouble()>0))
1011 {
1012 double angle(propList["librevenge:rotate"]->getDouble());
1013 while (angle > 180.0)
1014 angle -= 360.0;
1015 while (angle < -180.0)
1016 angle += 360.0;
1017 m_pImpl->m_outputSink << " transform=\"rotate(" << doubleToString(angle) << ", " << doubleToString(72*xmiddle) << ", " << doubleToString(72*ymiddle) << ")\" ";
1018 }
1019 m_pImpl->m_outputSink << ">\n";
1020
1021 }
1022
1023 void RVNGSVGDrawingGenerator::endTextObject()
1024 {
1025 m_pImpl->m_outputSink << "</" << m_pImpl->getNamespaceAndDelim() << "text>\n";
1026 }
1027
1028 void RVNGSVGDrawingGenerator::openOrderedListLevel(const RVNGPropertyList & /*propList*/) {}
1029 void RVNGSVGDrawingGenerator::closeOrderedListLevel() {}
1030
1031 void RVNGSVGDrawingGenerator::openUnorderedListLevel(const RVNGPropertyList & /*propList*/) {}
1032 void RVNGSVGDrawingGenerator::closeUnorderedListLevel() {}
1033
1034 void RVNGSVGDrawingGenerator::openListElement(const RVNGPropertyList & /*propList*/) {}
1035 void RVNGSVGDrawingGenerator::closeListElement() {}
1036
1037 void RVNGSVGDrawingGenerator::defineParagraphStyle(const RVNGPropertyList & /*propList*/) {}
1038 void RVNGSVGDrawingGenerator::openParagraph(const RVNGPropertyList & /*propList*/) {}
1039 void RVNGSVGDrawingGenerator::closeParagraph() {}
1040
1041 void RVNGSVGDrawingGenerator::defineCharacterStyle(const RVNGPropertyList &propList)
1042 {
1043 if (!propList["librevenge:span-id"])
1044 {
1045 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::defineCharacterStyle: can not find the span-id\n"));
1046 return;
1047 }
1048 m_pImpl->m_idSpanMap[propList["librevenge:span-id"]->getInt()]=propList;
1049 }
1050
1051 void RVNGSVGDrawingGenerator::openSpan(const RVNGPropertyList &propList)
1052 {
1053 RVNGPropertyList pList(propList);
1054 if (propList["librevenge:span-id"] &&
1055 m_pImpl->m_idSpanMap.find(propList["librevenge:span-id"]->getInt())!=m_pImpl->m_idSpanMap.end())
1056 pList=m_pImpl->m_idSpanMap.find(propList["librevenge:span-id"]->getInt())->second;
1057
1058 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "tspan ";
1059 if (pList["style:font-name"])
1060 m_pImpl->m_outputSink << "font-family=\"" << pList["style:font-name"]->getStr().cstr() << "\" ";
1061 if (pList["fo:font-style"])
1062 m_pImpl->m_outputSink << "font-style=\"" << pList["fo:font-style"]->getStr().cstr() << "\" ";
1063 if (pList["fo:font-weight"])
1064 m_pImpl->m_outputSink << "font-weight=\"" << pList["fo:font-weight"]->getStr().cstr() << "\" ";
1065 if (pList["fo:font-variant"])
1066 m_pImpl->m_outputSink << "font-variant=\"" << pList["fo:font-variant"]->getStr().cstr() << "\" ";
1067 if (pList["fo:font-size"])
1068 m_pImpl->m_outputSink << "font-size=\"" << doubleToString(pList["fo:font-size"]->getDouble()) << "\" ";
1069 if (pList["fo:color"])
1070 m_pImpl->m_outputSink << "fill=\"" << pList["fo:color"]->getStr().cstr() << "\" ";
1071 if (pList["fo:text-transform"])
1072 m_pImpl->m_outputSink << "text-transform=\"" << pList["fo:text-transform"]->getStr().cstr() << "\" ";
1073 if (pList["svg:fill-opacity"])
1074 m_pImpl->m_outputSink << "fill-opacity=\"" << doubleToString(pList["svg:fill-opacity"]->getDouble()) << "\" ";
1075 if (pList["svg:stroke-opacity"])
1076 m_pImpl->m_outputSink << "stroke-opacity=\"" << doubleToString(pList["svg:stroke-opacity"]->getDouble()) << "\" ";
1077 m_pImpl->m_outputSink << ">\n";
1078 }
1079
1080 void RVNGSVGDrawingGenerator::closeSpan()
1081 {
1082 m_pImpl->m_outputSink << "</" << m_pImpl->getNamespaceAndDelim() << "tspan>\n";
1083 }
1084
1085 void RVNGSVGDrawingGenerator::openLink(const RVNGPropertyList & /*propList*/) {}
1086 void RVNGSVGDrawingGenerator::closeLink() {}
1087
1088 void RVNGSVGDrawingGenerator::insertText(const RVNGString &str)
1089 {
1090 m_pImpl->m_outputSink << RVNGString::escapeXML(str).cstr();
1091 }
1092
1093 void RVNGSVGDrawingGenerator::insertTab()
1094 {
1095 m_pImpl->m_outputSink << "\t";
1096 }
1097
1098 void RVNGSVGDrawingGenerator::insertSpace()
1099 {
1100 m_pImpl->m_outputSink << " ";
1101 }
1102
1103 void RVNGSVGDrawingGenerator::insertLineBreak()
1104 {
1105 m_pImpl->m_outputSink << "\n";
1106 }
1107
1108 void RVNGSVGDrawingGenerator::insertField(const RVNGPropertyList & /*propList*/) {}
1109
1110 void RVNGSVGDrawingGenerator::startTableObject(const RVNGPropertyList &propList)
1111 {
1112 if (m_pImpl->m_table)
1113 {
1114 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::startTableObject: a table is already opened\n"));
1115 return;
1116 }
1117 m_pImpl->m_table.reset(new Table(propList));
1118 }
1119
1120 void RVNGSVGDrawingGenerator::openTableRow(const RVNGPropertyList &propList)
1121 {
1122 if (!m_pImpl->m_table) return;
1123 m_pImpl->m_table->openRow(propList);
1124 }
1125
1126 void RVNGSVGDrawingGenerator::closeTableRow()
1127 {
1128 if (!m_pImpl->m_table) return;
1129 m_pImpl->m_table->closeRow();
1130 }
1131
1132 void RVNGSVGDrawingGenerator::openTableCell(const RVNGPropertyList &propList)
1133 {
1134 if (!m_pImpl->m_table) return;
1135
1136 if (propList["librevenge:column"])
1137 m_pImpl->m_table->m_column=propList["librevenge:column"]->getInt();
1138 if (propList["librevenge:row"])
1139 m_pImpl->m_table->m_row=propList["librevenge:row"]->getInt();
1140
1141 double x = 0, y=0;
1142 m_pImpl->m_table->getPosition(m_pImpl->m_table->m_column, m_pImpl->m_table->m_row, x, y);
1143 m_pImpl->m_outputSink << "<" << m_pImpl->getNamespaceAndDelim() << "text ";
1144 m_pImpl->m_outputSink << "x=\"" << doubleToString(72*x) << "\" y=\"" << doubleToString(72*y) << "\"";
1145 m_pImpl->m_outputSink << ">\n";
1146
1147 // time to update the next cell's column
1148 if (propList["table:number-columns-spanned"])
1149 m_pImpl->m_table->m_column += propList["librevenge:column"]->getInt();
1150 else
1151 ++m_pImpl->m_table->m_column;
1152 }
1153
1154 void RVNGSVGDrawingGenerator::closeTableCell()
1155 {
1156 if (!m_pImpl->m_table) return;
1157 m_pImpl->m_outputSink << "</" << m_pImpl->getNamespaceAndDelim() << "text>\n";
1158 }
1159
1160 void RVNGSVGDrawingGenerator::insertCoveredTableCell(const RVNGPropertyList &/*propList*/)
1161 {
1162 if (!m_pImpl->m_table) return;
1163 // TODO: implement me
1164 }
1165
1166 void RVNGSVGDrawingGenerator::endTableObject()
1167 {
1168 if (!m_pImpl->m_table)
1169 {
1170 RVNG_DEBUG_MSG(("RVNGSVGDrawingGenerator::endTableObject: no table is already opened\n"));
1171 return;
1172 }
1173 m_pImpl->m_table.reset();
1174 }
1175
1176 }
1177
1178 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */