Debian Codegraph @

src/lib/CDRContentCollector.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libcdr project.
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
10 #include "CDRContentCollector.h"
11
12 #include <math.h>
13 #include <string.h>
14 #include <librevenge/librevenge.h>
15 #include <libcdr/libcdr.h>
16 #include "CDROutputElementList.h"
17 #include "libcdr_utils.h"
18
19 #ifndef DUMP_PATTERN
20 #define DUMP_PATTERN 0
21 #endif
22
23 #ifndef DUMP_VECT
24 #define DUMP_VECT 0
25 #endif
26
27 namespace libcdr
28 {
29 namespace
30 {
31
32 /// Move the number into [0;1] range.
33 void normalize(double &d)
34 {
35 if (d < 0.0)
36 {
37 const double n = d + static_cast<unsigned long>(-d) + 1.0;
38 if ((n < 0.0) || (n > 1.0))
39 d = 0.0; // The number was too big, thus rounded. Just pick a value.
40 else
41 d = n;
42 }
43 if (d > 1.0)
44 {
45 const double n = d - static_cast<unsigned long>(d);
46 if ((n < 0.0) || (n > 1.0))
47 d = 0.0; // The number was too big, thus rounded. Just pick a value.
48 else
49 d = n;
50 }
51 }
52
53 void normalizeAngle(double &angle)
54 {
55 angle = fmod(angle, 360);
56 if (angle < 0)
57 angle += 360;
58 }
59
60 }
61 }
62
63 libcdr::CDRContentCollector::CDRContentCollector(libcdr::CDRParserState &ps, librevenge::RVNGDrawingInterface *painter,
64 bool reverseOrder)
65 : m_painter(painter), m_isDocumentStarted(false), m_isPageProperties(false), m_isPageStarted(false),
66 m_ignorePage(false), m_page(ps.m_pages[0]), m_pageIndex(0), m_currentFillStyle(), m_currentLineStyle(),
67 m_spnd(0), m_currentObjectLevel(0), m_currentGroupLevel(0), m_currentVectLevel(0), m_currentPageLevel(0),
68 m_currentStyleId(0), m_currentImage(), m_currentText(nullptr), m_currentBBox(), m_currentTextBox(), m_currentPath(),
69 m_currentTransforms(), m_fillTransforms(), m_polygon(), m_isInPolygon(false), m_isInSpline(false),
70 m_outputElementsStack(nullptr), m_contentOutputElementsStack(), m_fillOutputElementsStack(),
71 m_outputElementsQueue(nullptr), m_contentOutputElementsQueue(), m_fillOutputElementsQueue(),
72 m_groupLevels(), m_groupTransforms(), m_splineData(), m_fillOpacity(1.0), m_reverseOrder(reverseOrder),
73 m_ps(ps)
74 {
75 m_outputElementsStack = &m_contentOutputElementsStack;
76 m_outputElementsQueue = &m_contentOutputElementsQueue;
77 }
78
79 libcdr::CDRContentCollector::~CDRContentCollector()
80 {
81 if (m_isPageStarted)
82 _endPage();
83 if (m_isDocumentStarted)
84 _endDocument();
85 }
86
87 void libcdr::CDRContentCollector::_startDocument()
88 {
89 if (m_isDocumentStarted)
90 return;
91 librevenge::RVNGPropertyList propList;
92 if (m_painter)
93 m_painter->startDocument(propList);
94 m_isDocumentStarted = true;
95 }
96
97 void libcdr::CDRContentCollector::_endDocument()
98 {
99 if (!m_isDocumentStarted)
100 return;
101 if (m_isPageStarted)
102 _endPage();
103 if (m_painter)
104 m_painter->endDocument();
105 m_isDocumentStarted = false;
106 }
107
108 void libcdr::CDRContentCollector::_startPage(double width, double height)
109 {
110 if (m_ignorePage)
111 return;
112 if (!m_isDocumentStarted)
113 _startDocument();
114 librevenge::RVNGPropertyList propList;
115 propList.insert("svg:width", width);
116 propList.insert("svg:height", height);
117 if (m_painter)
118 m_painter->startPage(propList);
119 m_isPageStarted = true;
120 }
121
122 void libcdr::CDRContentCollector::_endPage()
123 {
124 if (!m_isPageStarted)
125 return;
126 while (!m_contentOutputElementsStack.empty())
127 {
128 m_contentOutputElementsStack.top().draw(m_painter);
129 m_contentOutputElementsStack.pop();
130 }
131 while (!m_contentOutputElementsQueue.empty())
132 {
133 m_contentOutputElementsQueue.front().draw(m_painter);
134 m_contentOutputElementsQueue.pop();
135 }
136 if (m_painter)
137 m_painter->endPage();
138 m_isPageStarted = false;
139 }
140
141 void libcdr::CDRContentCollector::collectPage(unsigned level)
142 {
143 m_isPageProperties = true;
144 m_ignorePage = false;
145 m_currentPageLevel = level;
146 m_page = m_ps.m_pages[m_pageIndex++];
147
148 }
149
150 void libcdr::CDRContentCollector::collectObject(unsigned level)
151 {
152 if (!m_isPageStarted && !m_currentVectLevel && !m_ignorePage)
153 _startPage(m_page.width, m_page.height);
154 m_currentObjectLevel = level;
155 m_currentFillStyle = CDRFillStyle();
156 m_currentLineStyle = CDRLineStyle();
157 m_currentStyleId = 0;
158 m_currentBBox = CDRBox();
159 }
160
161 void libcdr::CDRContentCollector::collectGroup(unsigned level)
162 {
163 if (!m_isPageStarted && !m_currentVectLevel && !m_ignorePage)
164 _startPage(m_page.width, m_page.height);
165 CDROutputElementList outputElement;
166 if (m_reverseOrder)
167 {
168 // Since the CDR objects are drawn in reverse order, reverse the logic of groups too
169 outputElement.addEndGroup();
170 m_outputElementsStack->push(outputElement);
171 }
172 else
173 {
174 librevenge::RVNGPropertyList propList;
175 outputElement.addStartGroup(propList);
176 m_outputElementsQueue->push(outputElement);
177 }
178 m_groupLevels.push(level);
179 m_groupTransforms.push(CDRTransforms());
180 }
181
182 void libcdr::CDRContentCollector::collectVect(unsigned level)
183 {
184 m_currentVectLevel = level;
185 m_outputElementsStack = &m_fillOutputElementsStack;
186 m_outputElementsQueue = &m_fillOutputElementsQueue;
187 m_page.width = 0.0;
188 m_page.height = 0.0;
189 m_page.offsetX = 0.0;
190 m_page.offsetY = 0.0;
191 }
192
193 void libcdr::CDRContentCollector::collectFlags(unsigned flags, bool considerFlags)
194 {
195 if (m_isPageProperties && !(flags & 0x00ff0000))
196 {
197 if (!m_isPageStarted)
198 _startPage(m_page.width, m_page.height);
199 }
200 else if (m_isPageProperties && considerFlags)
201 m_ignorePage = true;
202 m_isPageProperties = false;
203 }
204
205 void libcdr::CDRContentCollector::collectOtherList()
206 {
207 // m_isPageProperties = false;
208 }
209
210 void libcdr::CDRContentCollector::collectPath(const CDRPath &path)
211 {
212 CDR_DEBUG_MSG(("CDRContentCollector::collectPath\n"));
213 m_currentPath.appendPath(path);
214 }
215
216 void libcdr::CDRContentCollector::_flushCurrentPath()
217 {
218 CDR_DEBUG_MSG(("CDRContentCollector::_flushCurrentPath\n"));
219 CDROutputElementList outputElement;
220 if (!m_currentPath.empty() || (!m_splineData.empty() && m_isInSpline))
221 {
222 if (m_polygon && m_isInPolygon)
223 m_polygon->create(m_currentPath);
224 m_polygon.reset();
225 m_isInPolygon = false;
226 if (!m_splineData.empty() && m_isInSpline)
227 m_splineData.create(m_currentPath);
228 m_splineData.clear();
229 m_isInSpline = false;
230 bool firstPoint = true;
231 bool wasMove = false;
232 double initialX = 0.0;
233 double initialY = 0.0;
234 double previousX = 0.0;
235 double previousY = 0.0;
236 double x = 0.0;
237 double y = 0.0;
238 librevenge::RVNGPropertyList style;
239 _fillProperties(style);
240 _lineProperties(style);
241 outputElement.addStyle(style);
242 m_currentPath.transform(m_currentTransforms);
243 if (!m_groupTransforms.empty())
244 m_currentPath.transform(m_groupTransforms.top());
245 CDRTransform tmpTrafo(1.0, 0.0, -m_page.offsetX, 0.0, 1.0, -m_page.offsetY);
246 m_currentPath.transform(tmpTrafo);
247 tmpTrafo = CDRTransform(1.0, 0.0, 0.0, 0.0, -1.0, m_page.height);
248 m_currentPath.transform(tmpTrafo);
249
250 std::vector<librevenge::RVNGPropertyList> tmpPath;
251
252 librevenge::RVNGPropertyListVector path;
253 m_currentPath.writeOut(path);
254
255 bool isPathClosed = m_currentPath.isClosed();
256
257 librevenge::RVNGPropertyListVector::Iter i(path);
258 for (i.rewind(); i.next();)
259 {
260 if (!i()["librevenge:path-action"])
261 continue;
262 if (i()["svg:x"] && i()["svg:y"])
263 {
264 bool ignoreM = false;
265 x = i()["svg:x"]->getDouble();
266 y = i()["svg:y"]->getDouble();
267 if (firstPoint)
268 {
269 initialX = x;
270 initialY = y;
271 firstPoint = false;
272 wasMove = true;
273 }
274 else if (i()["librevenge:path-action"]->getStr() == "M")
275 {
276 // This is needed for a good generation of path from polygon
277 if (CDR_ALMOST_ZERO(previousX - x) && CDR_ALMOST_ZERO(previousY - y))
278 ignoreM = true;
279 else
280 {
281 if (!tmpPath.empty())
282 {
283 if (!wasMove)
284 {
285 if ((CDR_ALMOST_ZERO(initialX - previousX) && CDR_ALMOST_ZERO(initialY - previousY)) || isPathClosed)
286 {
287 librevenge::RVNGPropertyList node;
288 node.insert("librevenge:path-action", "Z");
289 tmpPath.push_back(node);
290 }
291 }
292 else
293 tmpPath.pop_back();
294 }
295 }
296
297 if (!ignoreM)
298 {
299 initialX = x;
300 initialY = y;
301 wasMove = true;
302 }
303
304 }
305 else
306 wasMove = false;
307
308 if (!ignoreM)
309 {
310 tmpPath.push_back(i());
311 previousX = x;
312 previousY = y;
313 }
314
315 }
316 else if (i()["librevenge:path-action"]->getStr() == "Z")
317 {
318 if (!tmpPath.empty() && tmpPath.back()["librevenge:path-action"] && tmpPath.back()["librevenge:path-action"]->getStr() != "Z")
319 tmpPath.push_back(i());
320 }
321 }
322 if (!tmpPath.empty())
323 {
324 if (!wasMove)
325 {
326 if ((CDR_ALMOST_ZERO(initialX - previousX) && CDR_ALMOST_ZERO(initialY - previousY)) || isPathClosed)
327 {
328 if (tmpPath.back()["librevenge:path-action"] && tmpPath.back()["librevenge:path-action"]->getStr() != "Z")
329 {
330 librevenge::RVNGPropertyList closedPath;
331 closedPath.insert("librevenge:path-action", "Z");
332 tmpPath.push_back(closedPath);
333 }
334 }
335 }
336 else
337 tmpPath.pop_back();
338 }
339 if (!tmpPath.empty())
340 {
341 librevenge::RVNGPropertyListVector outputPath;
342 for (std::vector<librevenge::RVNGPropertyList>::const_iterator iter = tmpPath.begin(); iter != tmpPath.end(); ++iter)
343 outputPath.append(*iter);
344 librevenge::RVNGPropertyList propList;
345 propList.insert("svg:d", outputPath);
346 outputElement.addPath(propList);
347
348 }
349 m_currentPath.clear();
350 }
351
352 if (m_currentImage.getImage().size())
353 {
354 double cx = m_currentImage.getMiddleX();
355 double cy = m_currentImage.getMiddleY();
356 double corner1x = m_currentImage.m_x1;
357 double corner1y = m_currentImage.m_y1;
358 double corner2x = m_currentImage.m_x1;
359 double corner2y = m_currentImage.m_y2;
360 double corner3x = m_currentImage.m_x2;
361 double corner3y = m_currentImage.m_y2;
362 m_currentTransforms.applyToPoint(cx, cy);
363 m_currentTransforms.applyToPoint(corner1x, corner1y);
364 m_currentTransforms.applyToPoint(corner2x, corner2y);
365 m_currentTransforms.applyToPoint(corner3x, corner3y);
366 if (!m_groupTransforms.empty())
367 {
368 m_groupTransforms.top().applyToPoint(cx, cy);
369 m_groupTransforms.top().applyToPoint(corner1x, corner1y);
370 m_groupTransforms.top().applyToPoint(corner2x, corner2y);
371 m_groupTransforms.top().applyToPoint(corner3x, corner3y);
372 }
373 CDRTransform tmpTrafo(1.0, 0.0, -m_page.offsetX, 0.0, 1.0, -m_page.offsetY);
374 tmpTrafo.applyToPoint(cx, cy);
375 tmpTrafo.applyToPoint(corner1x, corner1y);
376 tmpTrafo.applyToPoint(corner2x, corner2y);
377 tmpTrafo.applyToPoint(corner3x, corner3y);
378 tmpTrafo = CDRTransform(1.0, 0.0, 0.0, 0.0, -1.0, m_page.height);
379 tmpTrafo.applyToPoint(cx, cy);
380 tmpTrafo.applyToPoint(corner1x, corner1y);
381 tmpTrafo.applyToPoint(corner2x, corner2y);
382 tmpTrafo.applyToPoint(corner3x, corner3y);
383 bool flipX(m_currentTransforms.getFlipX());
384 bool flipY(m_currentTransforms.getFlipY());
385 double width = sqrt((corner2x - corner3x)*(corner2x - corner3x) + (corner2y - corner3y)*(corner2y - corner3y));
386 double height = sqrt((corner2x - corner1x)*(corner2x - corner1x) + (corner2y - corner1y)*(corner2y - corner1y));
387 double rotate = atan2(corner3y-corner2y, corner3x-corner2x);
388
389 librevenge::RVNGPropertyList propList;
390
391 propList.insert("svg:x", cx - width / 2.0);
392 propList.insert("svg:width", width);
393 propList.insert("svg:y", cy - height / 2.0);
394 propList.insert("svg:height", height);
395
396 if (flipX)
397 {
398 propList.insert("draw:mirror-horizontal", true);
399 rotate = M_PI - rotate;
400 }
401 if (flipY)
402 {
403 propList.insert("draw:mirror-vertical", true);
404 rotate *= -1.0;
405 }
406
407 while (rotate < 0.0)
408 rotate += 2.0*M_PI;
409 while (rotate > 2.0*M_PI)
410 rotate -= 2.0*M_PI;
411
412 if (!CDR_ALMOST_ZERO(rotate))
413 propList.insert("librevenge:rotate", rotate * 180 / M_PI, librevenge::RVNG_GENERIC);
414
415 propList.insert("librevenge:mime-type", "image/bmp");
416 propList.insert("office:binary-data", m_currentImage.getImage());
417 outputElement.addGraphicObject(propList);
418 }
419 if (m_currentText && !m_currentText->empty())
420 {
421 double x1 = m_currentTextBox.m_x;
422 double y1 = m_currentTextBox.m_y;
423 double x2 = m_currentTextBox.m_x + m_currentTextBox.m_w;
424 double y2 = m_currentTextBox.m_y - m_currentTextBox.m_h;
425 if (!CDR_ALMOST_ZERO(m_currentTextBox.m_h) && !CDR_ALMOST_ZERO(m_currentTextBox.m_w))
426 {
427 m_currentTransforms.applyToPoint(x1, y1);
428 m_currentTransforms.applyToPoint(x2, y2);
429 if (!m_groupTransforms.empty())
430 {
431 m_groupTransforms.top().applyToPoint(x1, y1);
432 m_groupTransforms.top().applyToPoint(x2, y2);
433 }
434 }
435 else if (!CDR_ALMOST_ZERO(m_currentBBox.getWidth()) && !CDR_ALMOST_ZERO(m_currentBBox.getHeight()))
436 {
437 y1 = m_currentBBox.getMinY();
438 y2 = m_currentBBox.getMinY() + m_currentBBox.getHeight();
439 if ((*m_currentText)[0].m_line[0].m_style.m_align == 2) // Center
440 {
441 x1 = m_currentBBox.getMinX() - m_currentBBox.getWidth() / 4.0;
442 x2 = m_currentBBox.getMinX() + (3.0 * m_currentBBox.getWidth() / 4.0);
443 }
444 else if ((*m_currentText)[0].m_line[0].m_style.m_align == 3) // Right
445 {
446 x1 = m_currentBBox.getMinX() - m_currentBBox.getWidth() / 2.0;
447 x2 = m_currentBBox.getMinX() + m_currentBBox.getWidth() / 2.0;
448 }
449 else
450 {
451 x1 = m_currentBBox.getMinX();
452 x2 = m_currentBBox.getMinX() + m_currentBBox.getWidth();
453 }
454 }
455 else if (!m_currentTransforms.empty())
456 {
457 x1 = m_currentTransforms.getTranslateX();
458 y1 = m_currentTransforms.getTranslateY();
459 x2 = x1;
460 y2 = y1;
461 }
462
463 CDRTransform tmpTrafo(1.0, 0.0, -m_page.offsetX, 0.0, 1.0, -m_page.offsetY);
464 tmpTrafo.applyToPoint(x1, y1);
465 tmpTrafo.applyToPoint(x2, y2);
466 tmpTrafo = CDRTransform(1.0, 0.0, 0.0, 0.0, -1.0, m_page.height);
467 tmpTrafo.applyToPoint(x1, y1);
468 tmpTrafo.applyToPoint(x2, y2);
469 if (x1 > x2)
470 std::swap(x1, x2);
471 if (y1 > y2)
472 std::swap(y1, y2);
473
474 librevenge::RVNGPropertyList textFrameProps;
475 textFrameProps.insert("svg:width", fabs(x2-x1));
476 textFrameProps.insert("svg:height", fabs(y2-y1));
477 textFrameProps.insert("svg:x", x1);
478 textFrameProps.insert("svg:y", y1);
479 textFrameProps.insert("fo:padding-top", 0.0);
480 textFrameProps.insert("fo:padding-bottom", 0.0);
481 textFrameProps.insert("fo:padding-left", 0.0);
482 textFrameProps.insert("fo:padding-right", 0.0);
483 outputElement.addStartTextObject(textFrameProps);
484 for (const auto &i : *m_currentText)
485 {
486 const std::vector<CDRText> &currentLine = i.m_line;
487 if (currentLine.empty())
488 continue;
489 librevenge::RVNGPropertyList paraProps;
490 bool rtl = false;
491 switch (currentLine[0].m_style.m_align)
492 {
493 case 1: // Left
494 if (!rtl)
495 paraProps.insert("fo:text-align", "left");
496 else
497 paraProps.insert("fo:text-align", "end");
498 break;
499 case 2: // Center
500 paraProps.insert("fo:text-align", "center");
501 break;
502 case 3: // Right
503 if (!rtl)
504 paraProps.insert("fo:text-align", "end");
505 else
506 paraProps.insert("fo:text-align", "left");
507 break;
508 case 4: // Full justify
509 paraProps.insert("fo:text-align", "justify");
510 break;
511 case 5: // Force justify
512 paraProps.insert("fo:text-align", "full");
513 break;
514 case 0: // None
515 default:
516 break;
517 }
518 outputElement.addOpenParagraph(paraProps);
519 for (const auto &j : currentLine)
520 {
521 if (!j.m_text.empty())
522 {
523 librevenge::RVNGPropertyList spanProps;
524 double fontSize = (double)cdr_round(144.0*j.m_style.m_fontSize) / 2.0;
525 spanProps.insert("fo:font-size", fontSize, librevenge::RVNG_POINT);
526 if (j.m_style.m_fontName.len())
527 spanProps.insert("style:font-name", j.m_style.m_fontName);
528 if (j.m_style.m_fillStyle.fillType != (unsigned short)-1)
529 spanProps.insert("fo:color", m_ps.getRGBColorString(j.m_style.m_fillStyle.color1));
530 outputElement.addOpenSpan(spanProps);
531 outputElement.addInsertText(j.m_text);
532 outputElement.addCloseSpan();
533 }
534 }
535 outputElement.addCloseParagraph();
536 }
537 outputElement.addEndTextObject();
538 }
539 m_currentImage = libcdr::CDRImage();
540 if (!outputElement.empty())
541 {
542 if (m_reverseOrder)
543 m_outputElementsStack->push(outputElement);
544 else
545 m_outputElementsQueue->push(outputElement);
546 }
547 m_currentTransforms.clear();
548 m_fillTransforms = libcdr::CDRTransforms();
549 m_fillOpacity = 1.0;
550 m_currentText = nullptr;
551 }
552
553 void libcdr::CDRContentCollector::collectTransform(const CDRTransforms &transforms, bool considerGroupTransform)
554 {
555 if (m_currentObjectLevel)
556 m_currentTransforms = transforms;
557 else if (!m_groupLevels.empty() && considerGroupTransform)
558 m_groupTransforms.top() = transforms;
559 }
560
561 void libcdr::CDRContentCollector::collectFillTransform(const CDRTransforms &fillTrafos)
562 {
563 m_fillTransforms = fillTrafos;
564 }
565
566 void libcdr::CDRContentCollector::collectLevel(unsigned level)
567 {
568 if (level <= m_currentObjectLevel)
569 {
570 _flushCurrentPath();
571 m_currentObjectLevel = 0;
572 }
573 while (!m_groupLevels.empty() && level <= m_groupLevels.top())
574 {
575 CDROutputElementList outputElement;
576 // since the CDR objects are drawn in reverse order, reverse group marks too
577 if (m_reverseOrder)
578 {
579 librevenge::RVNGPropertyList propList;
580 outputElement.addStartGroup(propList);
581 m_outputElementsStack->push(outputElement);
582 }
583 else
584 {
585 outputElement.addEndGroup();
586 m_outputElementsQueue->push(outputElement);
587 }
588 m_groupLevels.pop();
589 m_groupTransforms.pop();
590 }
591 if (m_currentVectLevel && m_spnd && m_groupLevels.empty() && (!m_fillOutputElementsStack.empty() || !m_fillOutputElementsQueue.empty()))
592 {
593 librevenge::RVNGStringVector svgOutput;
594 librevenge::RVNGSVGDrawingGenerator generator(svgOutput, "");
595 librevenge::RVNGPropertyList propList;
596 propList.insert("svg:width", m_page.width);
597 propList.insert("svg:height", m_page.height);
598 generator.startPage(propList);
599 while (!m_fillOutputElementsStack.empty())
600 {
601 m_fillOutputElementsStack.top().draw(&generator);
602 m_fillOutputElementsStack.pop();
603 }
604 while (!m_fillOutputElementsQueue.empty())
605 {
606 m_fillOutputElementsQueue.front().draw(&generator);
607 m_fillOutputElementsQueue.pop();
608 }
609
610 generator.endPage();
611 if (!svgOutput.empty())
612 {
613 const char *header =
614 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
615 librevenge::RVNGBinaryData output((const unsigned char *)header, strlen(header));
616 output.append((const unsigned char *)svgOutput[0].cstr(), strlen(svgOutput[0].cstr()));
617 m_ps.m_vects[m_spnd] = output;
618 }
619 #if DUMP_VECT
620 librevenge::RVNGString filename;
621 filename.sprintf("vect%.8x.svg", m_spnd);
622 FILE *f = fopen(filename.cstr(), "wb");
623 if (f)
624 {
625 const unsigned char *tmpBuffer = m_ps.m_vects[m_spnd].getDataBuffer();
626 for (unsigned long k = 0; k < m_ps.m_vects[m_spnd].size(); k++)
627 fprintf(f, "%c",tmpBuffer[k]);
628 fclose(f);
629 }
630 #endif
631 m_spnd = 0;
632 m_page.width = 0.0;
633 m_page.height = 0.0;
634 m_page.offsetX = 0.0;
635 m_page.offsetY = 0.0;
636 }
637 if (level <= m_currentVectLevel)
638 {
639 m_currentVectLevel = 0;
640 m_outputElementsStack = &m_contentOutputElementsStack;
641 m_outputElementsQueue = &m_contentOutputElementsQueue;
642 m_page = m_ps.m_pages[m_pageIndex ? m_pageIndex-1 : 0];
643 }
644 if (level <= m_currentPageLevel)
645 {
646 _endPage();
647 m_currentPageLevel = 0;
648 }
649 }
650
651 void libcdr::CDRContentCollector::collectFillStyleId(unsigned id)
652 {
653 std::map<unsigned, CDRFillStyle>::const_iterator iter = m_ps.m_fillStyles.find(id);
654 if (iter != m_ps.m_fillStyles.end())
655 m_currentFillStyle = iter->second;
656 }
657
658 void libcdr::CDRContentCollector::collectLineStyleId(unsigned id)
659 {
660 std::map<unsigned, CDRLineStyle>::const_iterator iter = m_ps.m_lineStyles.find(id);
661 if (iter != m_ps.m_lineStyles.end())
662 m_currentLineStyle = iter->second;
663 }
664
665 void libcdr::CDRContentCollector::collectRotate(double angle, double cx, double cy)
666 {
667 CDRTransform trafo1(1.0, 0.0, -cx, 0.0, 1.0, -cy);
668 m_currentPath.transform(trafo1);
669 CDRTransform trafo2(cos(angle), -sin(angle), 0, sin(angle), cos(angle), 0);
670 m_currentPath.transform(trafo2);
671 CDRTransform trafo3(1.0, 0.0, cx, 0.0, 1.0, cy);
672 m_currentPath.transform(trafo3);
673 }
674
675 void libcdr::CDRContentCollector::collectPolygon()
676 {
677 m_isInPolygon = true;
678 }
679
680 void libcdr::CDRContentCollector::collectSpline()
681 {
682 m_isInSpline = true;
683 }
684
685 void libcdr::CDRContentCollector::collectPolygonTransform(unsigned numAngles, unsigned nextPoint, double rx, double ry, double cx, double cy)
686 {
687 m_polygon.reset(new CDRPolygon(numAngles, nextPoint, rx, ry, cx, cy));
688 }
689
690 void libcdr::CDRContentCollector::_fillProperties(librevenge::RVNGPropertyList &propList)
691 {
692 if (m_currentFillStyle.fillType == (unsigned short)-1 && m_currentStyleId)
693 {
694 CDRStyle tmpStyle;
695 m_ps.getRecursedStyle(tmpStyle, m_currentStyleId);
696 m_currentFillStyle = tmpStyle.m_fillStyle;
697 }
698
699 if (m_fillOpacity < 1.0)
700 propList.insert("draw:opacity", m_fillOpacity, librevenge::RVNG_PERCENT);
701 if (m_currentFillStyle.fillType == 0)
702 propList.insert("draw:fill", "none");
703 else
704 {
705 if (m_currentFillStyle.fillType == (unsigned short)-1)
706 propList.insert("draw:fill", "none");
707 else
708 {
709 switch (m_currentFillStyle.fillType)
710 {
711 case 1: // Solid
712 propList.insert("draw:fill", "solid");
713 propList.insert("draw:fill-color", m_ps.getRGBColorString(m_currentFillStyle.color1));
714 propList.insert("svg:fill-rule", "evenodd");
715 break;
716 case 2: // Gradient
717 if (m_currentFillStyle.gradient.m_stops.empty())
718 propList.insert("draw:fill", "none");
719 else if (m_currentFillStyle.gradient.m_stops.size() == 1)
720 {
721 propList.insert("draw:fill", "solid");
722 propList.insert("draw:fill-color", m_ps.getRGBColorString(m_currentFillStyle.gradient.m_stops[0].m_color));
723 propList.insert("svg:fill-rule", "evenodd");
724 }
725 else if (m_currentFillStyle.gradient.m_stops.size() == 2)
726 {
727 double angle = m_currentFillStyle.gradient.m_angle * 180 / M_PI;
728 normalizeAngle(angle);
729 propList.insert("draw:fill", "gradient");
730 propList.insert("draw:start-color", m_ps.getRGBColorString(m_currentFillStyle.gradient.m_stops[0].m_color));
731 propList.insert("draw:end-color", m_ps.getRGBColorString(m_currentFillStyle.gradient.m_stops[1].m_color));
732 propList.insert("draw:angle", (int)angle);
733 switch (m_currentFillStyle.gradient.m_type)
734 {
735 case 1: // linear
736 case 3: // conical
737 propList.insert("draw:style", "linear");
738 angle += 90.0;
739 normalizeAngle(angle);
740 propList.insert("draw:angle", (int)angle);
741 propList.insert("draw:border", (double)(m_currentFillStyle.gradient.m_edgeOffset)/100.0, librevenge::RVNG_PERCENT);
742 break;
743 case 2: // radial
744 propList.insert("draw:border", (2.0 * (double)(m_currentFillStyle.gradient.m_edgeOffset)/100.0), librevenge::RVNG_PERCENT);
745 propList.insert("draw:style", "radial");
746 propList.insert("svg:cx", (double)(0.5 + m_currentFillStyle.gradient.m_centerXOffset/200.0), librevenge::RVNG_PERCENT);
747 propList.insert("svg:cy", (double)(0.5 + m_currentFillStyle.gradient.m_centerXOffset/200.0), librevenge::RVNG_PERCENT);
748 break;
749 case 4: // square
750 propList.insert("draw:border", (2.0 * (double)(m_currentFillStyle.gradient.m_edgeOffset)/100.0), librevenge::RVNG_PERCENT);
751 propList.insert("draw:style", "square");
752 propList.insert("svg:cx", (double)(0.5 + m_currentFillStyle.gradient.m_centerXOffset/200.0), librevenge::RVNG_PERCENT);
753 propList.insert("svg:cy", (double)(0.5 + m_currentFillStyle.gradient.m_centerXOffset/200.0), librevenge::RVNG_PERCENT);
754 break;
755 default:
756 propList.insert("draw:style", "linear");
757 angle += 90.0;
758 normalizeAngle(angle);
759 propList.insert("draw:angle", (int)angle);
760 librevenge::RVNGPropertyListVector vec;
761 for (auto &gradStop : m_currentFillStyle.gradient.m_stops)
762 {
763 librevenge::RVNGPropertyList stopElement;
764 stopElement.insert("svg:offset", gradStop.m_offset, librevenge::RVNG_PERCENT);
765 stopElement.insert("svg:stop-color", m_ps.getRGBColorString(gradStop.m_color));
766 stopElement.insert("svg:stop-opacity", m_fillOpacity, librevenge::RVNG_PERCENT);
767 vec.append(stopElement);
768 }
769 propList.insert("svg:linearGradient", vec);
770 break;
771 }
772 }
773 else // output svg gradient as a hail mary pass towards ODG that does not really support it
774 {
775 propList.insert("draw:fill", "gradient");
776 propList.insert("draw:style", "linear");
777 double angle = m_currentFillStyle.gradient.m_angle * 180 / M_PI;
778 angle += 90.0;
779 normalizeAngle(angle);
780 propList.insert("draw:angle", (int)angle);
781 librevenge::RVNGPropertyListVector vec;
782 for (auto &gradStop : m_currentFillStyle.gradient.m_stops)
783 {
784 librevenge::RVNGPropertyList stopElement;
785 stopElement.insert("svg:offset", gradStop.m_offset, librevenge::RVNG_PERCENT);
786 stopElement.insert("svg:stop-color", m_ps.getRGBColorString(gradStop.m_color));
787 stopElement.insert("svg:stop-opacity", m_fillOpacity, librevenge::RVNG_PERCENT);
788 vec.append(stopElement);
789 }
790 propList.insert("svg:linearGradient", vec);
791 }
792 break;
793 case 7: // Pattern
794 case 8: // Pattern
795 {
796 auto iterPattern = m_ps.m_patterns.find(m_currentFillStyle.imageFill.id);
797 if (iterPattern != m_ps.m_patterns.end())
798 {
799 propList.insert("draw:fill", "bitmap");
800 librevenge::RVNGBinaryData image;
801 _generateBitmapFromPattern(image, iterPattern->second, m_currentFillStyle.color1, m_currentFillStyle.color2);
802 #if DUMP_PATTERN
803 librevenge::RVNGString filename;
804 filename.sprintf("pattern%.8x.bmp", m_currentFillStyle.imageFill.id);
805 FILE *f = fopen(filename.cstr(), "wb");
806 if (f)
807 {
808 const unsigned char *tmpBuffer = image.getDataBuffer();
809 for (unsigned long k = 0; k < image.size(); k++)
810 fprintf(f, "%c",tmpBuffer[k]);
811 fclose(f);
812 }
813 #endif
814 propList.insert("draw:fill-image", image);
815 propList.insert("librevenge:mime-type", "image/bmp");
816 propList.insert("style:repeat", "repeat");
817 if (m_currentFillStyle.imageFill.isRelative)
818 {
819 propList.insert("svg:width", m_currentFillStyle.imageFill.width, librevenge::RVNG_PERCENT);
820 propList.insert("svg:height", m_currentFillStyle.imageFill.height, librevenge::RVNG_PERCENT);
821 }
822 else
823 {
824 double scaleX = 1.0;
825 double scaleY = 1.0;
826 if (m_currentFillStyle.imageFill.flags & 0x04) // scale fill with image
827 {
828 scaleX = m_currentTransforms.getScaleX();
829 scaleY = m_currentTransforms.getScaleY();
830 }
831 propList.insert("svg:width", m_currentFillStyle.imageFill.width * scaleX);
832 propList.insert("svg:height", m_currentFillStyle.imageFill.height * scaleY);
833 }
834 propList.insert("draw:fill-image-ref-point", "bottom-left");
835 if (m_currentFillStyle.imageFill.isRelative)
836 {
837 if (!CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.xOffset) && !CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.xOffset))
838 propList.insert("draw:fill-image-ref-point-x", m_currentFillStyle.imageFill.xOffset, librevenge::RVNG_PERCENT);
839 if (!CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.yOffset) && !CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.yOffset))
840 propList.insert("draw:fill-image-ref-point-y", m_currentFillStyle.imageFill.yOffset, librevenge::RVNG_PERCENT);
841 }
842 else
843 {
844 if (!CDR_ALMOST_ZERO(m_fillTransforms.getTranslateX()))
845 {
846 double xOffset = m_fillTransforms.getTranslateX() / m_currentFillStyle.imageFill.width;
847 normalize(xOffset);
848 propList.insert("draw:fill-image-ref-point-x", xOffset, librevenge::RVNG_PERCENT);
849 }
850 if (!CDR_ALMOST_ZERO(m_fillTransforms.getTranslateY()))
851 {
852 double yOffset = m_fillTransforms.getTranslateY() / m_currentFillStyle.imageFill.width;
853 normalize(yOffset);
854 propList.insert("draw:fill-image-ref-point-y", 1.0 - yOffset, librevenge::RVNG_PERCENT);
855 }
856 }
857 }
858 else
859 {
860 // We did not find the pattern, so fill solid with the background colour
861 propList.insert("draw:fill", "solid");
862 propList.insert("draw:fill-color", m_ps.getRGBColorString(m_currentFillStyle.color2));
863 propList.insert("svg:fill-rule", "evenodd");
864 }
865 }
866 break;
867 case 9: // Bitmap
868 case 11: // Texture
869 {
870 auto iterBmp = m_ps.m_bmps.find(m_currentFillStyle.imageFill.id);
871 if (iterBmp != m_ps.m_bmps.end())
872 {
873 propList.insert("librevenge:mime-type", "image/bmp");
874 propList.insert("draw:fill", "bitmap");
875 propList.insert("draw:fill-image", iterBmp->second);
876 propList.insert("style:repeat", "repeat");
877 if (m_currentFillStyle.imageFill.isRelative)
878 {
879 propList.insert("svg:width", m_currentFillStyle.imageFill.width, librevenge::RVNG_PERCENT);
880 propList.insert("svg:height", m_currentFillStyle.imageFill.height, librevenge::RVNG_PERCENT);
881 }
882 else
883 {
884 double scaleX = 1.0;
885 double scaleY = 1.0;
886 if (m_currentFillStyle.imageFill.flags & 0x04) // scale fill with image
887 {
888 scaleX = m_currentTransforms.getScaleX();
889 scaleY = m_currentTransforms.getScaleY();
890 }
891 propList.insert("svg:width", m_currentFillStyle.imageFill.width * scaleX);
892 propList.insert("svg:height", m_currentFillStyle.imageFill.height * scaleY);
893 }
894 propList.insert("draw:fill-image-ref-point", "bottom-left");
895 if (m_currentFillStyle.imageFill.isRelative)
896 {
897 if (!CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.xOffset) && !CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.xOffset))
898 propList.insert("draw:fill-image-ref-point-x", m_currentFillStyle.imageFill.xOffset, librevenge::RVNG_PERCENT);
899 if (!CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.yOffset) && !CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.yOffset))
900 propList.insert("draw:fill-image-ref-point-y", m_currentFillStyle.imageFill.yOffset, librevenge::RVNG_PERCENT);
901 }
902 else
903 {
904 if (!CDR_ALMOST_ZERO(m_fillTransforms.getTranslateX()))
905 {
906 double xOffset = m_fillTransforms.getTranslateX() / m_currentFillStyle.imageFill.width;
907 while (xOffset < 0.0)
908 xOffset += 1.0;
909 while (xOffset > 1.0)
910 xOffset -= 1.0;
911 propList.insert("draw:fill-image-ref-point-x", xOffset, librevenge::RVNG_PERCENT);
912 }
913 if (!CDR_ALMOST_ZERO(m_fillTransforms.getTranslateY()))
914 {
915 double yOffset = m_fillTransforms.getTranslateY() / m_currentFillStyle.imageFill.width;
916 while (yOffset < 0.0)
917 yOffset += 1.0;
918 while (yOffset > 1.0)
919 yOffset -= 1.0;
920 propList.insert("draw:fill-image-ref-point-y", 1.0 - yOffset, librevenge::RVNG_PERCENT);
921 }
922 }
923 }
924 else
925 propList.insert("draw:fill", "none");
926 }
927 break;
928 case 10: // Full color
929 {
930 auto iterVect = m_ps.m_vects.find(m_currentFillStyle.imageFill.id);
931 if (iterVect != m_ps.m_vects.end())
932 {
933 propList.insert("draw:fill", "bitmap");
934 propList.insert("librevenge:mime-type", "image/svg+xml");
935 propList.insert("draw:fill-image", iterVect->second);
936 propList.insert("style:repeat", "repeat");
937 if (m_currentFillStyle.imageFill.isRelative)
938 {
939 propList.insert("svg:width", m_currentFillStyle.imageFill.width, librevenge::RVNG_PERCENT);
940 propList.insert("svg:height", m_currentFillStyle.imageFill.height, librevenge::RVNG_PERCENT);
941 }
942 else
943 {
944 double scaleX = 1.0;
945 double scaleY = 1.0;
946 if (m_currentFillStyle.imageFill.flags & 0x04) // scale fill with image
947 {
948 scaleX = m_currentTransforms.getScaleX();
949 scaleY = m_currentTransforms.getScaleY();
950 }
951 propList.insert("svg:width", m_currentFillStyle.imageFill.width * scaleX);
952 propList.insert("svg:height", m_currentFillStyle.imageFill.height * scaleY);
953 }
954 propList.insert("draw:fill-image-ref-point", "bottom-left");
955 if (m_currentFillStyle.imageFill.isRelative)
956 {
957 if (!CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.xOffset) && !CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.xOffset))
958 propList.insert("draw:fill-image-ref-point-x", m_currentFillStyle.imageFill.xOffset, librevenge::RVNG_PERCENT);
959 if (!CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.yOffset) && !CDR_ALMOST_ZERO(m_currentFillStyle.imageFill.yOffset))
960 propList.insert("draw:fill-image-ref-point-y", m_currentFillStyle.imageFill.yOffset, librevenge::RVNG_PERCENT);
961 }
962 else
963 {
964 if (!CDR_ALMOST_ZERO(m_fillTransforms.getTranslateX()))
965 {
966 double xOffset = m_fillTransforms.getTranslateX() / m_currentFillStyle.imageFill.width;
967 while (xOffset < 0.0)
968 xOffset += 1.0;
969 while (xOffset > 1.0)
970 xOffset -= 1.0;
971 propList.insert("draw:fill-image-ref-point-x", xOffset, librevenge::RVNG_PERCENT);
972 }
973 if (!CDR_ALMOST_ZERO(m_fillTransforms.getTranslateY()))
974 {
975 double yOffset = m_fillTransforms.getTranslateY() / m_currentFillStyle.imageFill.width;
976 while (yOffset < 0.0)
977 yOffset += 1.0;
978 while (yOffset > 1.0)
979 yOffset -= 1.0;
980 propList.insert("draw:fill-image-ref-point-y", 1.0 - yOffset, librevenge::RVNG_PERCENT);
981 }
982 }
983 }
984 else
985 propList.insert("draw:fill", "none");
986 }
987 break;
988 default:
989 propList.insert("draw:fill", "none");
990 break;
991 }
992 }
993 }
994 }
995
996 void libcdr::CDRContentCollector::_lineProperties(librevenge::RVNGPropertyList &propList)
997 {
998 if (m_currentLineStyle.lineType == (unsigned short)-1 && m_currentStyleId)
999 {
1000 CDRStyle tmpStyle;
1001 m_ps.getRecursedStyle(tmpStyle, m_currentStyleId);
1002 m_currentLineStyle = tmpStyle.m_lineStyle;
1003 }
1004
1005 if (m_currentLineStyle.lineType == (unsigned short)-1)
1006 /* No line style specified and also no line style from the style id,
1007 the shape has no outline then. */
1008 propList.insert("draw:stroke", "none");
1009 else
1010 {
1011 if (m_currentLineStyle.lineType & 0x1)
1012 propList.insert("draw:stroke", "none");
1013 else if (m_currentLineStyle.lineType & 0x6)
1014 {
1015 if (m_currentLineStyle.dashArray.size() && (m_currentLineStyle.lineType & 0x4))
1016 propList.insert("draw:stroke", "dash");
1017 else
1018 propList.insert("draw:stroke", "solid");
1019 double scale = 1.0;
1020 if (m_currentLineStyle.lineType & 0x20) // scale line with image
1021 {
1022 scale = m_currentTransforms.getScaleX();
1023 double scaleY = m_currentTransforms.getScaleY();
1024 if (scaleY > scale)
1025 scale = scaleY;
1026 }
1027 scale *= m_currentLineStyle.stretch;
1028 propList.insert("svg:stroke-width", m_currentLineStyle.lineWidth * scale);
1029 propList.insert("svg:stroke-color", m_ps.getRGBColorString(m_currentLineStyle.color));
1030
1031 switch (m_currentLineStyle.capsType)
1032 {
1033 case 1:
1034 propList.insert("svg:stroke-linecap", "round");
1035 break;
1036 case 2:
1037 propList.insert("svg:stroke-linecap", "square");
1038 break;
1039 default:
1040 propList.insert("svg:stroke-linecap", "butt");
1041 }
1042
1043 switch (m_currentLineStyle.joinType)
1044 {
1045 case 1:
1046 propList.insert("svg:stroke-linejoin", "round");
1047 break;
1048 case 2:
1049 propList.insert("svg:stroke-linejoin", "bevel");
1050 break;
1051 default:
1052 propList.insert("svg:stroke-linejoin", "miter");
1053 }
1054
1055 if (m_currentLineStyle.dashArray.size())
1056 {
1057 int dots1 = 0;
1058 int dots2 = 0;
1059 unsigned dots1len = 0;
1060 unsigned dots2len = 0;
1061 unsigned gap = 0;
1062
1063 if (m_currentLineStyle.dashArray.size() >= 2)
1064 {
1065 dots1len = m_currentLineStyle.dashArray[0];
1066 gap = m_currentLineStyle.dashArray[1];
1067 }
1068
1069 unsigned long count = m_currentLineStyle.dashArray.size() / 2;
1070 unsigned i = 0;
1071 for (; i < count;)
1072 {
1073 if (dots1len == m_currentLineStyle.dashArray[2*i])
1074 dots1++;
1075 else
1076 break;
1077 gap = gap < m_currentLineStyle.dashArray[2*i+1] ? m_currentLineStyle.dashArray[2*i+1] : gap;
1078 i++;
1079 }
1080 if (i < count)
1081 {
1082 dots2len = m_currentLineStyle.dashArray[2*i];
1083 gap = gap < m_currentLineStyle.dashArray[2*i+1] ? m_currentLineStyle.dashArray[2*i+1] : gap;
1084 }
1085 for (; i < count;)
1086 {
1087 if (dots2len == m_currentLineStyle.dashArray[2*i])
1088 dots2++;
1089 else
1090 break;
1091 gap = gap < m_currentLineStyle.dashArray[2*i+1] ? m_currentLineStyle.dashArray[2*i+1] : gap;
1092 i++;
1093 }
1094 if (!dots2)
1095 {
1096 dots2 = dots1;
1097 dots2len = dots1len;
1098 }
1099 propList.insert("draw:dots1", dots1);
1100 propList.insert("draw:dots1-length", 72.0*(m_currentLineStyle.lineWidth * scale)*dots1len, librevenge::RVNG_POINT);
1101 propList.insert("draw:dots2", dots2);
1102 propList.insert("draw:dots2-length", 72.0*(m_currentLineStyle.lineWidth * scale)*dots2len, librevenge::RVNG_POINT);
1103 propList.insert("draw:distance", 72.0*(m_currentLineStyle.lineWidth * scale)*gap, librevenge::RVNG_POINT);
1104 }
1105 }
1106 else
1107 {
1108 propList.insert("draw:stroke", "solid");
1109 propList.insert("svg:stroke-width", 0.0);
1110 propList.insert("svg:stroke-color", "#000000");
1111 }
1112 }
1113
1114 // Deal with line markers (arrows, etc.)
1115 if (!m_currentLineStyle.startMarker.empty())
1116 {
1117 CDRPath startMarker(m_currentLineStyle.startMarker);
1118 startMarker.transform(m_currentTransforms);
1119 if (!m_groupTransforms.empty())
1120 startMarker.transform(m_groupTransforms.top());
1121 CDRTransform tmpTrafo = CDRTransform(1.0, 0.0, 0.0, 0.0, -1.0, 0);
1122 startMarker.transform(tmpTrafo);
1123 librevenge::RVNGString path, viewBox;
1124 double width;
1125 startMarker.writeOut(path, viewBox, width);
1126 propList.insert("draw:marker-start-viewbox", viewBox);
1127 propList.insert("draw:marker-start-path", path);
1128 // propList.insert("draw:marker-start-width", width);
1129 }
1130 if (!m_currentLineStyle.endMarker.empty())
1131 {
1132 CDRPath endMarker(m_currentLineStyle.endMarker);
1133 endMarker.transform(m_currentTransforms);
1134 if (!m_groupTransforms.empty())
1135 endMarker.transform(m_groupTransforms.top());
1136 CDRTransform tmpTrafo = CDRTransform(-1.0, 0.0, 0.0, 0.0, -1.0, 0);
1137 endMarker.transform(tmpTrafo);
1138 librevenge::RVNGString path, viewBox;
1139 double width;
1140 endMarker.writeOut(path, viewBox, width);
1141 propList.insert("draw:marker-end-viewbox", viewBox);
1142 propList.insert("draw:marker-end-path", path);
1143 // propList.insert("draw:marker-end-width", width);
1144 }
1145
1146
1147
1148 }
1149
1150 void libcdr::CDRContentCollector::_generateBitmapFromPattern(librevenge::RVNGBinaryData &bitmap, const CDRPattern &pattern, const CDRColor &fgColor, const CDRColor &bgColor)
1151 {
1152 unsigned height = pattern.height;
1153 unsigned width = pattern.width;
1154 auto tmpPixelSize = (unsigned)(height * width);
1155 if (tmpPixelSize < (unsigned)height) // overflow
1156 return;
1157
1158 unsigned tmpDIBImageSize = tmpPixelSize * 4;
1159 if (tmpPixelSize > tmpDIBImageSize) // overflow !!!
1160 return;
1161
1162 unsigned tmpDIBOffsetBits = 14 + 40;
1163 unsigned tmpDIBFileSize = tmpDIBOffsetBits + tmpDIBImageSize;
1164 if (tmpDIBImageSize > tmpDIBFileSize) // overflow !!!
1165 return;
1166
1167 // Create DIB file header
1168 writeU16(bitmap, 0x4D42); // Type
1169 writeU32(bitmap, (int)tmpDIBFileSize); // Size
1170 writeU16(bitmap, 0); // Reserved1
1171 writeU16(bitmap, 0); // Reserved2
1172 writeU32(bitmap, (int)tmpDIBOffsetBits); // OffsetBits
1173
1174 // Create DIB Info header
1175 writeU32(bitmap, 40); // Size
1176
1177 writeU32(bitmap, (int)width); // Width
1178 writeU32(bitmap, (int)height); // Height
1179
1180 writeU16(bitmap, 1); // Planes
1181 writeU16(bitmap, 32); // BitCount
1182 writeU32(bitmap, 0); // Compression
1183 writeU32(bitmap, (int)tmpDIBImageSize); // SizeImage
1184 writeU32(bitmap, 0); // XPelsPerMeter
1185 writeU32(bitmap, 0); // YPelsPerMeter
1186 writeU32(bitmap, 0); // ColorsUsed
1187 writeU32(bitmap, 0); // ColorsImportant
1188
1189 // The Bitmaps in CDR are padded to 32bit border
1190 unsigned lineWidth = (width + 7) / 8;
1191
1192 unsigned foreground = m_ps._getRGBColor(fgColor);
1193 unsigned background = m_ps._getRGBColor(bgColor);
1194
1195 for (unsigned j = height; j > 0; --j)
1196 {
1197 unsigned i = 0;
1198 unsigned k = 0;
1199 while (i <lineWidth && k < width)
1200 {
1201 unsigned l = 0;
1202 unsigned char c = 0;
1203 const unsigned index = (j-1)*lineWidth+i;
1204 if (index < pattern.pattern.size())
1205 c = pattern.pattern[index];
1206 i++;
1207 while (k < width && l < 8)
1208 {
1209 if (c & 0x80)
1210 writeU32(bitmap, (int)background);
1211 else
1212 writeU32(bitmap, (int)foreground);
1213 c <<= 1;
1214 l++;
1215 k++;
1216 }
1217 }
1218 }
1219 }
1220
1221 void libcdr::CDRContentCollector::collectBitmap(unsigned imageId, double x1, double x2, double y1, double y2)
1222 {
1223 auto iter = m_ps.m_bmps.find(imageId);
1224 if (iter != m_ps.m_bmps.end())
1225 m_currentImage = CDRImage(iter->second, x1, x2, y1, y2);
1226 }
1227
1228 void libcdr::CDRContentCollector::collectPpdt(const std::vector<std::pair<double, double> > &points, const std::vector<unsigned> &knotVector)
1229 {
1230 m_splineData = CDRSplineData(points, knotVector);
1231 }
1232
1233 void libcdr::CDRContentCollector::collectFillOpacity(double opacity)
1234 {
1235 m_fillOpacity = opacity;
1236 }
1237
1238 void libcdr::CDRContentCollector::collectBBox(double x0, double y0, double x1, double y1)
1239 {
1240 CDRBox bBox(x0, y0, x1, y1);
1241 if (m_currentVectLevel && CDR_ALMOST_ZERO(m_page.width) && CDR_ALMOST_ZERO(m_page.height))
1242 {
1243 m_page.width = bBox.getWidth();
1244 m_page.height = bBox.getHeight();
1245 m_page.offsetX = bBox.getMinX();
1246 m_page.offsetY = bBox.getMinY();
1247 }
1248 m_currentBBox = bBox;
1249 }
1250
1251 void libcdr::CDRContentCollector::collectSpnd(unsigned spnd)
1252 {
1253 if (m_currentVectLevel && !m_spnd)
1254 m_spnd = spnd;
1255 else if (!m_currentVectLevel)
1256 m_spnd = spnd;
1257 }
1258
1259 void libcdr::CDRContentCollector::collectVectorPattern(unsigned id, const librevenge::RVNGBinaryData &data)
1260 {
1261 librevenge::RVNGInputStream *input = data.getDataStream();
1262 if (!input)
1263 return;
1264
1265 input->seek(0, librevenge::RVNG_SEEK_SET);
1266 if (!libcdr::CMXDocument::isSupported(input))
1267 return;
1268 input->seek(0, librevenge::RVNG_SEEK_SET);
1269 librevenge::RVNGStringVector svgOutput;
1270 librevenge::RVNGSVGDrawingGenerator generator(svgOutput, "");
1271 if (!libcdr::CMXDocument::parse(input, &generator))
1272 return;
1273 if (!svgOutput.empty())
1274 {
1275 const char *header =
1276 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
1277 librevenge::RVNGBinaryData output((const unsigned char *)header, strlen(header));
1278 output.append((const unsigned char *)svgOutput[0].cstr(), strlen(svgOutput[0].cstr()));
1279 m_ps.m_vects[id] = output;
1280 }
1281 #if DUMP_VECT
1282 librevenge::RVNGString filename;
1283 filename.sprintf("vect%.8x.cmx", id);
1284 FILE *f = fopen(filename.cstr(), "wb");
1285 if (f)
1286 {
1287 const unsigned char *tmpBuffer = data.getDataBuffer();
1288 for (unsigned long k = 0; k < data.size(); k++)
1289 fprintf(f, "%c",tmpBuffer[k]);
1290 fclose(f);
1291 }
1292 filename.sprintf("vect%.8x.svg", id);
1293 f = fopen(filename.cstr(), "wb");
1294 if (f)
1295 {
1296 const unsigned char *tmpBuffer = m_ps.m_vects[id].getDataBuffer();
1297 for (unsigned long k = 0; k < m_ps.m_vects[id].size(); k++)
1298 fprintf(f, "%c",tmpBuffer[k]);
1299 fclose(f);
1300 }
1301 #endif
1302 }
1303
1304 void libcdr::CDRContentCollector::collectArtisticText(double x, double y)
1305 {
1306 m_currentTextBox = CDRBox(x, y, x, y);
1307 m_currentBBox.m_w *= 2.0;
1308 std::map<unsigned, std::vector<CDRTextLine> >::const_iterator iter = m_ps.m_texts.find(m_spnd);
1309 if (iter != m_ps.m_texts.end())
1310 m_currentText = &(iter->second);
1311 }
1312
1313 void libcdr::CDRContentCollector::collectParagraphText(double x, double y, double width, double height)
1314 {
1315 m_currentTextBox.m_x = x;
1316 m_currentTextBox.m_y = y;
1317 m_currentTextBox.m_w = width;
1318 m_currentTextBox.m_h = height;
1319 std::map<unsigned, std::vector<CDRTextLine> >::const_iterator iter = m_ps.m_texts.find(m_spnd);
1320 if (iter != m_ps.m_texts.end())
1321 m_currentText = &(iter->second);
1322 }
1323
1324 void libcdr::CDRContentCollector::collectStyleId(unsigned styleId)
1325 {
1326 m_currentStyleId = styleId;
1327 }
1328
1329 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */