| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is a part of the libzmf 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 "ZMFCollector.h" |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <iterator> |
| 14 | #include <string> |
| 15 | #include <boost/math/constants/constants.hpp> |
| 16 | #include <boost/variant.hpp> |
| 17 | |
| 18 | namespace libzmf |
| 19 | { |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | Point calculateEllipsePoint(const Point &c, double rx, double ry, double angle) |
| 25 | { |
| 26 | return Point(c.x + rx * std::cos(angle), c.y + ry * std::sin(angle)); |
| 27 | } |
| 28 | |
| 29 | void writeBorder(librevenge::RVNGPropertyList &propList, const char *const name, const Pen &pen) |
| 30 | { |
| 31 | if (pen.isInvisible) |
| 32 | return; |
| 33 | |
| 34 | librevenge::RVNGString border; |
| 35 | |
| 36 | border.sprintf("%fin", pen.width); |
| 37 | |
| 38 | border.append(" solid"); |
| 39 | |
| 40 | border.append(" "); |
| 41 | border.append(pen.color.toString()); |
| 42 | |
| 43 | propList.insert(name, border); |
| 44 | } |
| 45 | |
| 46 | librevenge::RVNGPropertyListVector createPath(const std::vector<Curve> &curves, |
| 47 | double leftOffset = 0.0, double topOffset = 0.0) |
| 48 | { |
| 49 | librevenge::RVNGPropertyListVector path; |
| 50 | |
| 51 | for (const auto &curve : curves) |
| 52 | { |
| 53 | if (curve.points.size() < 2) |
| 54 | continue; |
| 55 | |
| 56 | { |
| 57 | librevenge::RVNGPropertyList pathPart; |
| 58 | pathPart.insert("librevenge:path-action", "M"); |
| 59 | pathPart.insert("svg:x", curve.points[0].x - leftOffset); |
| 60 | pathPart.insert("svg:y", curve.points[0].y - topOffset); |
| 61 | |
| 62 | path.append(pathPart); |
| 63 | } |
| 64 | |
| 65 | size_t i = 1; |
| 66 | |
| 67 | for (const auto §ionType : curve.sectionTypes) |
| 68 | { |
| 69 | librevenge::RVNGPropertyList pathPart; |
| 70 | |
| 71 | switch (sectionType) |
| 72 | { |
| 73 | default: |
| 74 | case CurveType::LINE: |
| 75 | if (i >= curve.points.size()) |
| 76 | { |
| 77 | ZMF_DEBUG_MSG(("Unexpected end of curve points\n")); |
| 78 | break; |
| 79 | } |
| 80 | pathPart.insert("librevenge:path-action", "L"); |
| 81 | pathPart.insert("svg:x", curve.points[i].x - leftOffset); |
| 82 | pathPart.insert("svg:y", curve.points[i].y - topOffset); |
| 83 | i++; |
| 84 | break; |
| 85 | case CurveType::BEZIER_CURVE: |
| 86 | if (i + 2 >= curve.points.size()) |
| 87 | { |
| 88 | ZMF_DEBUG_MSG(("Unexpected end of curve points\n")); |
| 89 | break; |
| 90 | } |
| 91 | pathPart.insert("librevenge:path-action", "C"); |
| 92 | pathPart.insert("svg:x1", curve.points[i].x - leftOffset); |
| 93 | pathPart.insert("svg:y1", curve.points[i].y - topOffset); |
| 94 | pathPart.insert("svg:x2", curve.points[i + 1].x - leftOffset); |
| 95 | pathPart.insert("svg:y2", curve.points[i + 1].y - topOffset); |
| 96 | pathPart.insert("svg:x", curve.points[i + 2].x - leftOffset); |
| 97 | pathPart.insert("svg:y", curve.points[i + 2].y - topOffset); |
| 98 | i += 3; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | path.append(pathPart); |
| 103 | } |
| 104 | |
| 105 | if (curve.closed) |
| 106 | { |
| 107 | librevenge::RVNGPropertyList pathPart; |
| 108 | pathPart.insert("librevenge:path-action", "Z"); |
| 109 | |
| 110 | path.append(pathPart); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return path; |
| 115 | } |
| 116 | |
| 117 | librevenge::RVNGString getPathStr(const librevenge::RVNGPropertyListVector &path) |
| 118 | { |
| 119 | librevenge::RVNGString s(""); |
| 120 | |
| 121 | for (unsigned i = 0; i < path.count(); ++i) |
| 122 | { |
| 123 | if (!path[i]["librevenge:path-action"]) |
| 124 | continue; |
| 125 | |
| 126 | std::string action = path[i]["librevenge:path-action"]->getStr().cstr(); |
| 127 | bool coordOk = path[i]["svg:x"] && path[i]["svg:y"]; |
| 128 | bool coord1Ok = coordOk && path[i]["svg:x1"] && path[i]["svg:y1"]; |
| 129 | bool coord2Ok = coord1Ok && path[i]["svg:x2"] && path[i]["svg:y2"]; |
| 130 | |
| 131 | librevenge::RVNGString sElement; |
| 132 | |
| 133 | switch (action[0]) |
| 134 | { |
| 135 | case 'M': |
| 136 | case 'L': |
| 137 | if (!coordOk) |
| 138 | { |
| 139 | ZMF_DEBUG_MSG(("Incorrect path coordinates\n")); |
| 140 | break; |
| 141 | } |
| 142 | sElement.sprintf("%c%lf %lf ", action[0], path[i]["svg:x"]->getDouble(), path[i]["svg:y"]->getDouble()); |
| 143 | s.append(sElement); |
| 144 | break; |
| 145 | case 'C': |
| 146 | if (!coord2Ok) |
| 147 | { |
| 148 | ZMF_DEBUG_MSG(("Incorrect path coordinates\n")); |
| 149 | break; |
| 150 | } |
| 151 | sElement.sprintf("C%lf %lf %lf %lf %lf %lf ", |
| 152 | path[i]["svg:x1"]->getDouble(), path[i]["svg:y1"]->getDouble(), |
| 153 | path[i]["svg:x2"]->getDouble(), path[i]["svg:y2"]->getDouble(), |
| 154 | path[i]["svg:x"]->getDouble(), path[i]["svg:y"]->getDouble()); |
| 155 | s.append(sElement); |
| 156 | break; |
| 157 | case 'Z': |
| 158 | s.append("Z "); |
| 159 | break; |
| 160 | default: |
| 161 | ZMF_DEBUG_MSG(("Unknown path-action %s\n", action.c_str())); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return s; |
| 166 | } |
| 167 | |
| 168 | void writeArrow(librevenge::RVNGPropertyList &propList, const char *const name, Arrow arrow, double penWidth) |
| 169 | { |
| 170 | using namespace boost::math::double_constants; |
| 171 | |
| 172 | Point lineEnd(arrow.lineEndX, 0); |
| 173 | |
| 174 | lineEnd = lineEnd.rotate(half_pi, Point(0, 0)); |
| 175 | |
| 176 | double dist = 1; |
| 177 | |
| 178 | for (auto &curve : arrow.curves) |
| 179 | { |
| 180 | auto minmaxY = std::minmax_element(curve.points.begin(), curve.points.end(), [](const Point &p1, const Point &p2) |
| 181 | { |
| 182 | return p1.y < p2.y; |
| 183 | }); |
| 184 | |
| 185 | dist = std::max(dist, std::abs(minmaxY.first->y - minmaxY.second->y)); |
| 186 | |
| 187 | std::transform(curve.points.begin(), curve.points.end(), curve.points.begin(), [&lineEnd](const Point &p) |
| 188 | { |
| 189 | return p.rotate(half_pi, Point(0, 0)).move(0, -lineEnd.y); |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | librevenge::RVNGString propName; |
| 194 | |
| 195 | propName.sprintf("draw:marker-%s-viewbox", name); |
| 196 | propList.insert(propName.cstr(), "-10 -10 10 10"); |
| 197 | propName.sprintf("draw:marker-%s-path", name); |
| 198 | propList.insert(propName.cstr(), getPathStr(createPath(arrow.curves))); |
| 199 | propName.sprintf("draw:marker-%s-width", name); |
| 200 | propList.insert(propName.cstr(), penWidth * dist); |
| 201 | propName.sprintf("draw:marker-%s-center", name); |
| 202 | propList.insert(propName.cstr(), true); |
| 203 | } |
| 204 | |
| 205 | class FillWriter : public boost::static_visitor<void> |
| 206 | { |
| 207 | public: |
| 208 | explicit FillWriter(librevenge::RVNGPropertyList &propList, |
| 209 | const boost::optional<Transparency> &transparency) |
| 210 | : m_propList(propList) |
| 211 | , m_transparency(transparency) |
| 212 | { } |
| 213 | |
| 214 | void operator()(const Color &color) |
| 215 | { |
| 216 | m_propList.insert("draw:fill", "solid"); |
| 217 | m_propList.insert("draw:fill-color", color.toString()); |
| 218 | |
| 219 | if (m_transparency) |
| 220 | { |
| 221 | m_propList.insert("draw:opacity", get(m_transparency).opacity(), librevenge::RVNG_PERCENT); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void operator()(const Gradient &gradient) |
| 226 | { |
| 227 | using namespace boost::math::double_constants; |
| 228 | |
| 229 | if (gradient.stops.size() < 2) |
| 230 | return; |
| 231 | |
| 232 | m_propList.insert("draw:fill", "gradient"); |
| 233 | |
| 234 | auto stops = gradient.stops; |
| 235 | std::sort(stops.begin(), stops.end(), |
| 236 | [&gradient](const GradientStop &s1, const GradientStop &s2) |
| 237 | { |
| 238 | return gradient.type == GradientType::LINEAR ? s1.offset < s2.offset : s1.offset > s2.offset; |
| 239 | }); |
| 240 | if (gradient.type != GradientType::LINEAR) |
| 241 | { |
| 242 | for (auto &stop : stops) |
| 243 | { |
| 244 | stop.offset = 1.0 - stop.offset; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | librevenge::RVNGPropertyListVector gradientVector; |
| 249 | for (const auto &stop : stops) |
| 250 | { |
| 251 | librevenge::RVNGPropertyList grad; |
| 252 | grad.insert("svg:offset", stop.offset, librevenge::RVNG_PERCENT); |
| 253 | grad.insert("svg:stop-color", stop.color.toString()); |
| 254 | grad.insert("svg:stop-opacity", m_transparency ? get(m_transparency).opacity() : 1.0, librevenge::RVNG_PERCENT); |
| 255 | gradientVector.append(grad); |
| 256 | } |
| 257 | |
| 258 | switch (gradient.type) |
| 259 | { |
| 260 | default: |
| 261 | case GradientType::LINEAR: |
| 262 | m_propList.insert("draw:style", "linear"); |
| 263 | m_propList.insert("draw:angle", rad2deg(gradient.angle + half_pi)); |
| 264 | m_propList.insert("svg:linearGradient", gradientVector); |
| 265 | break; |
| 266 | case GradientType::RADIAL: |
| 267 | m_propList.insert("draw:style", "radial"); |
| 268 | m_propList.insert("draw:cx", gradient.center.x, librevenge::RVNG_PERCENT); |
| 269 | m_propList.insert("draw:cy", gradient.center.y, librevenge::RVNG_PERCENT); |
| 270 | m_propList.insert("draw:border", 0.25 - gradient.center.distance(Point(0.5, 0.5)), librevenge::RVNG_PERCENT); |
| 271 | m_propList.insert("svg:radialGradient", gradientVector); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void operator()(const ImageFill &imageFill) |
| 277 | { |
| 278 | m_propList.insert("draw:fill", "bitmap"); |
| 279 | |
| 280 | m_propList.insert("draw:fill-image", imageFill.image.data); |
| 281 | m_propList.insert("librevenge:mime-type", "image/png"); |
| 282 | |
| 283 | if (imageFill.tile) |
| 284 | { |
| 285 | m_propList.insert("style:repeat", "repeat"); |
| 286 | m_propList.insert("draw:fill-image-width", imageFill.tileWidth, librevenge::RVNG_INCH); |
| 287 | m_propList.insert("draw:fill-image-height", imageFill.tileHeight, librevenge::RVNG_INCH); |
| 288 | m_propList.insert("draw:fill-image-ref-point", "top-left"); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | m_propList.insert("style:repeat", "stretch"); |
| 293 | } |
| 294 | |
| 295 | if (m_transparency) |
| 296 | { |
| 297 | m_propList.insert("draw:opacity", get(m_transparency).opacity(), librevenge::RVNG_PERCENT); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | private: |
| 302 | librevenge::RVNGPropertyList &m_propList; |
| 303 | |
| 304 | const boost::optional<Transparency> &m_transparency; |
| 305 | }; |
| 306 | |
| 307 | } |
| 308 | |
| 309 | ZMFCollector::ZMFCollector(librevenge::RVNGDrawingInterface *painter) |
| 310 | : m_painter(painter) |
| 311 | , m_pageSettings() |
| 312 | , m_isDocumentStarted(false) |
| 313 | , m_isPageStarted(false) |
| 314 | , m_isLayerStarted(false) |
| 315 | , m_style() |
| 316 | { |
| 317 | } |
| 318 | |
| 319 | ZMFCollector::~ZMFCollector() |
| 320 | { |
| 321 | if (m_isDocumentStarted) |
| 322 | { |
| 323 | endDocument(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void ZMFCollector::startDocument() |
| 328 | { |
| 329 | if (m_isDocumentStarted) |
| 330 | return; |
| 331 | |
| 332 | librevenge::RVNGPropertyList propList; |
| 333 | |
| 334 | m_painter->startDocument(propList); |
| 335 | |
| 336 | m_isDocumentStarted = true; |
| 337 | } |
| 338 | |
| 339 | void ZMFCollector::endDocument() |
| 340 | { |
| 341 | if (!m_isDocumentStarted) |
| 342 | return; |
| 343 | |
| 344 | if (m_isPageStarted) |
| 345 | endPage(); |
| 346 | |
| 347 | m_painter->endDocument(); |
| 348 | |
| 349 | m_isDocumentStarted = false; |
| 350 | } |
| 351 | |
| 352 | void ZMFCollector::startPage(const ZMFPageSettings &pageSettings) |
| 353 | { |
| 354 | if (m_isPageStarted) |
| 355 | return; |
| 356 | |
| 357 | if (m_isLayerStarted) |
| 358 | endLayer(); |
| 359 | |
| 360 | librevenge::RVNGPropertyList propList; |
| 361 | |
| 362 | propList.insert("svg:width", pageSettings.width); |
| 363 | propList.insert("svg:height", pageSettings.height); |
| 364 | |
| 365 | propList.insert("draw:fill", "solid"); |
| 366 | propList.insert("draw:fill-color", pageSettings.color.toString()); |
| 367 | |
| 368 | m_painter->startPage(propList); |
| 369 | |
| 370 | m_pageSettings = pageSettings; |
| 371 | |
| 372 | m_isPageStarted = true; |
| 373 | } |
| 374 | |
| 375 | void ZMFCollector::endPage() |
| 376 | { |
| 377 | if (!m_isPageStarted) |
| 378 | return; |
| 379 | |
| 380 | m_painter->endPage(); |
| 381 | |
| 382 | m_isPageStarted = false; |
| 383 | } |
| 384 | |
| 385 | void ZMFCollector::startLayer() |
| 386 | { |
| 387 | if (m_isLayerStarted) |
| 388 | return; |
| 389 | |
| 390 | librevenge::RVNGPropertyList propList; |
| 391 | |
| 392 | m_painter->startLayer(propList); |
| 393 | |
| 394 | m_isLayerStarted = true; |
| 395 | } |
| 396 | |
| 397 | void ZMFCollector::endLayer() |
| 398 | { |
| 399 | if (!m_isLayerStarted) |
| 400 | return; |
| 401 | |
| 402 | m_painter->endLayer(); |
| 403 | |
| 404 | m_isLayerStarted = false; |
| 405 | } |
| 406 | |
| 407 | void ZMFCollector::startGroup() |
| 408 | { |
| 409 | librevenge::RVNGPropertyList propList; |
| 410 | |
| 411 | m_painter->openGroup(propList); |
| 412 | } |
| 413 | |
| 414 | void ZMFCollector::endGroup() |
| 415 | { |
| 416 | m_painter->closeGroup(); |
| 417 | } |
| 418 | |
| 419 | void ZMFCollector::setStyle(const Style &style) |
| 420 | { |
| 421 | m_style = style; |
| 422 | } |
| 423 | |
| 424 | void ZMFCollector::collectPath(const std::vector<Curve> &curves) |
| 425 | { |
| 426 | librevenge::RVNGPropertyList pathPropList; |
| 427 | |
| 428 | writeStyle(pathPropList, m_style, |
| 429 | !std::any_of(curves.begin(), curves.end(), [](const Curve &c) |
| 430 | { |
| 431 | return c.closed; |
| 432 | })); |
| 433 | m_painter->setStyle(pathPropList); |
| 434 | pathPropList.clear(); |
| 435 | |
| 436 | auto path = createPath(curves, m_pageSettings.leftOffset, m_pageSettings.topOffset); |
| 437 | |
| 438 | pathPropList.insert("svg:d", path); |
| 439 | |
| 440 | m_painter->drawPath(pathPropList); |
| 441 | } |
| 442 | |
| 443 | void ZMFCollector::collectPath(const Curve &curve) |
| 444 | { |
| 445 | collectPath(std::vector<Curve> { curve }); |
| 446 | } |
| 447 | |
| 448 | void ZMFCollector::collectEllipse(const Point &c, double rx, double ry, double rotation) |
| 449 | { |
| 450 | librevenge::RVNGPropertyList ellipse; |
| 451 | |
| 452 | writeStyle(ellipse, m_style); |
| 453 | m_painter->setStyle(ellipse); |
| 454 | ellipse.clear(); |
| 455 | |
| 456 | ellipse.insert("svg:cx", pageX(c.x)); |
| 457 | ellipse.insert("svg:cy", pageY(c.y)); |
| 458 | ellipse.insert("svg:rx", rx); |
| 459 | ellipse.insert("svg:ry", ry); |
| 460 | if (!ZMF_ALMOST_ZERO(rotation)) |
| 461 | { |
| 462 | ellipse.insert("librevenge:rotate", -rad2deg(rotation)); |
| 463 | } |
| 464 | |
| 465 | m_painter->drawEllipse(ellipse); |
| 466 | } |
| 467 | |
| 468 | void ZMFCollector::collectArc(const Point &c, double rx, double ry, double beginAngle, double endAngle, bool closed, double rotation) |
| 469 | { |
| 470 | using namespace boost::math::double_constants; |
| 471 | |
| 472 | librevenge::RVNGPropertyList pathPropList; |
| 473 | |
| 474 | writeStyle(pathPropList, m_style, !closed); |
| 475 | m_painter->setStyle(pathPropList); |
| 476 | pathPropList.clear(); |
| 477 | |
| 478 | auto beginPoint = calculateEllipsePoint(c, rx, ry, beginAngle); |
| 479 | auto endPoint = calculateEllipsePoint(c, rx, ry, endAngle); |
| 480 | |
| 481 | if (!ZMF_ALMOST_ZERO(rotation)) |
| 482 | { |
| 483 | beginPoint = beginPoint.rotate(rotation, c); |
| 484 | endPoint = endPoint.rotate(rotation, c); |
| 485 | } |
| 486 | |
| 487 | double angleDiff = std::fabs(endAngle - beginAngle); |
| 488 | bool largeArc = (beginAngle < endAngle && angleDiff > pi) || |
| 489 | (beginAngle > endAngle && angleDiff < pi); |
| 490 | |
| 491 | librevenge::RVNGPropertyListVector path; |
| 492 | |
| 493 | { |
| 494 | librevenge::RVNGPropertyList pathStart; |
| 495 | pathStart.insert("librevenge:path-action", "M"); |
| 496 | pathStart.insert("svg:x", pageX(beginPoint.x)); |
| 497 | pathStart.insert("svg:y", pageY(beginPoint.y)); |
| 498 | |
| 499 | path.append(pathStart); |
| 500 | } |
| 501 | |
| 502 | { |
| 503 | librevenge::RVNGPropertyList arc; |
| 504 | arc.insert("librevenge:path-action", "A"); |
| 505 | arc.insert("svg:rx", rx); |
| 506 | arc.insert("svg:ry", ry); |
| 507 | arc.insert("librevenge:large-arc", largeArc ? 1 : 0); |
| 508 | arc.insert("librevenge:sweep", 1); |
| 509 | arc.insert("svg:x", pageX(endPoint.x)); |
| 510 | arc.insert("svg:y", pageY(endPoint.y)); |
| 511 | |
| 512 | path.append(arc); |
| 513 | } |
| 514 | |
| 515 | if (closed) |
| 516 | { |
| 517 | librevenge::RVNGPropertyList pathEndLine; |
| 518 | pathEndLine.insert("librevenge:path-action", "L"); |
| 519 | pathEndLine.insert("svg:x", pageX(c.x)); |
| 520 | pathEndLine.insert("svg:y", pageY(c.y)); |
| 521 | |
| 522 | path.append(pathEndLine); |
| 523 | |
| 524 | librevenge::RVNGPropertyList pathEnd; |
| 525 | pathEnd.insert("librevenge:path-action", "Z"); |
| 526 | |
| 527 | path.append(pathEnd); |
| 528 | } |
| 529 | |
| 530 | pathPropList.insert("svg:d", path); |
| 531 | |
| 532 | m_painter->drawPath(pathPropList); |
| 533 | } |
| 534 | |
| 535 | void ZMFCollector::collectPolygon(const Point &c, double rx, double ry, uint32_t peaksCount, const Curve &peak, |
| 536 | double rotation, bool mirrorHorizontal, bool mirrorVertical) |
| 537 | { |
| 538 | using namespace boost::math::double_constants; |
| 539 | |
| 540 | if (peak.points.size() < 2) |
| 541 | { |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | const double peakAngle = two_pi / peaksCount; |
| 546 | |
| 547 | // create a single side of the polygon in an unit square; the |
| 548 | // center of the (future) polygon is (0, 0) |
| 549 | std::vector<Point> side; |
| 550 | side.reserve(peak.points.size()); |
| 551 | std::transform(peak.points.begin(), peak.points.end(), std::back_inserter(side), [peakAngle](const Point &p) |
| 552 | { |
| 553 | return calculateEllipsePoint(Point(0, 0), p.y, p.y, p.x * peakAngle); |
| 554 | }); |
| 555 | |
| 556 | Curve polygonCurve; |
| 557 | polygonCurve.points.reserve(peak.points.size() * peaksCount); |
| 558 | polygonCurve.sectionTypes.reserve(peak.sectionTypes.size() * peaksCount); |
| 559 | |
| 560 | // generate complete polygon |
| 561 | for (uint32_t i = 0; i < peaksCount; ++i) |
| 562 | { |
| 563 | std::transform(side.begin() + (i == 0 ? 0 : 1), side.end(), std::back_inserter(polygonCurve.points), [i, peakAngle](Point p) |
| 564 | { |
| 565 | p = p.rotate(i * peakAngle, Point(0, 0)); |
| 566 | return p; |
| 567 | }); |
| 568 | std::copy(peak.sectionTypes.begin(), peak.sectionTypes.end(), std::back_inserter(polygonCurve.sectionTypes)); |
| 569 | } |
| 570 | |
| 571 | // fit the polygon into the bbox and mirror |
| 572 | for (auto &p: polygonCurve.points) |
| 573 | { |
| 574 | p.x = p.x * rx; |
| 575 | p.y = p.y * ry; |
| 576 | p = p.move(c.x, c.y); |
| 577 | |
| 578 | p.y = -p.y; |
| 579 | p = p.move(0, 2 * c.y); |
| 580 | |
| 581 | if (mirrorHorizontal) |
| 582 | { |
| 583 | p.x = -p.x; |
| 584 | p = p.move(2 * c.x, 0); |
| 585 | } |
| 586 | if (mirrorVertical) |
| 587 | { |
| 588 | p.y = -p.y; |
| 589 | p = p.move(0, 2 * c.y); |
| 590 | } |
| 591 | |
| 592 | p = p.rotate(rotation, c); |
| 593 | } |
| 594 | |
| 595 | polygonCurve.closed = true; |
| 596 | collectPath(polygonCurve); |
| 597 | } |
| 598 | |
| 599 | void ZMFCollector::collectTextObject(const Text &text, const Point &topLeft, double width, double height, |
| 600 | VerticalAlignment align, double rotation) |
| 601 | { |
| 602 | librevenge::RVNGPropertyList textObjPropList; |
| 603 | |
| 604 | textObjPropList.insert("svg:x", pageX(topLeft.x)); |
| 605 | textObjPropList.insert("svg:y", pageY(topLeft.y)); |
| 606 | textObjPropList.insert("svg:width", width); |
| 607 | textObjPropList.insert("svg:height", height); |
| 608 | |
| 609 | switch (align) |
| 610 | { |
| 611 | case VerticalAlignment::TOP: |
| 612 | textObjPropList.insert("draw:textarea-vertical-align", "top"); |
| 613 | break; |
| 614 | case VerticalAlignment::MIDDLE: |
| 615 | textObjPropList.insert("draw:textarea-vertical-align", "middle"); |
| 616 | break; |
| 617 | case VerticalAlignment::BOTTOM: |
| 618 | textObjPropList.insert("draw:textarea-vertical-align", "bottom"); |
| 619 | break; |
| 620 | } |
| 621 | if (!ZMF_ALMOST_ZERO(rotation)) |
| 622 | { |
| 623 | textObjPropList.insert("librevenge:rotate", rad2deg(rotation)); |
| 624 | } |
| 625 | |
| 626 | m_painter->startTextObject(textObjPropList); |
| 627 | |
| 628 | collectText(text); |
| 629 | |
| 630 | m_painter->endTextObject(); |
| 631 | } |
| 632 | |
| 633 | void ZMFCollector::collectText(const Text &text) |
| 634 | { |
| 635 | for (auto ¶graph : text.paragraphs) |
| 636 | { |
| 637 | librevenge::RVNGPropertyList paragraphPropList; |
| 638 | |
| 639 | paragraphPropList.insert("fo:line-height", paragraph.style.lineSpacing, librevenge::RVNG_PERCENT); |
| 640 | |
| 641 | switch (paragraph.style.alignment) |
| 642 | { |
| 643 | case HorizontalAlignment::LEFT: |
| 644 | paragraphPropList.insert("fo:text-align", "left"); |
| 645 | break; |
| 646 | case HorizontalAlignment::RIGHT: |
| 647 | paragraphPropList.insert("fo:text-align", "end"); |
| 648 | break; |
| 649 | case HorizontalAlignment::CENTER: |
| 650 | paragraphPropList.insert("fo:text-align", "center"); |
| 651 | break; |
| 652 | case HorizontalAlignment::BLOCK: |
| 653 | case HorizontalAlignment::FULL: |
| 654 | paragraphPropList.insert("fo:text-align", "justify"); |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | m_painter->openParagraph(paragraphPropList); |
| 659 | |
| 660 | for (auto &span : paragraph.spans) |
| 661 | { |
| 662 | librevenge::RVNGPropertyList spanPropList; |
| 663 | |
| 664 | spanPropList.insert("style:font-name", span.font.name); |
| 665 | spanPropList.insert("fo:font-size", span.font.size, librevenge::RVNG_POINT); |
| 666 | spanPropList.insert("fo:font-weight", span.font.isBold ? "bold" : "normal"); |
| 667 | spanPropList.insert("fo:font-style", span.font.isItalic ? "italic" : "normal"); |
| 668 | spanPropList.insert("style:text-outline", static_cast<bool>(span.font.outline)); |
| 669 | |
| 670 | if (span.font.fill && get(span.font.fill).type() == typeid(Color)) |
| 671 | { |
| 672 | auto color = boost::get<Color>(get(span.font.fill)); |
| 673 | spanPropList.insert("fo:color", color.toString()); |
| 674 | } |
| 675 | |
| 676 | m_painter->openSpan(spanPropList); |
| 677 | |
| 678 | bool wasSpace = false; |
| 679 | std::string curText; |
| 680 | |
| 681 | librevenge::RVNGString::Iter iter(span.text); |
| 682 | iter.rewind(); |
| 683 | while (iter.next()) |
| 684 | { |
| 685 | const char *const utf8Char = iter(); |
| 686 | switch (utf8Char[0]) |
| 687 | { |
| 688 | // looks like Zoner Draw doesn't allow tabs, |
| 689 | // and \r (without \n) can be only at the end of paragraph |
| 690 | case '\r': |
| 691 | case '\n': |
| 692 | break; |
| 693 | case ' ': |
| 694 | if (wasSpace) |
| 695 | { |
| 696 | flushText(curText); |
| 697 | m_painter->insertSpace(); |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | wasSpace = true; |
| 702 | curText.push_back(' '); |
| 703 | } |
| 704 | break; |
| 705 | default: |
| 706 | wasSpace = false; |
| 707 | curText.append(utf8Char); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | flushText(curText); |
| 713 | |
| 714 | m_painter->closeSpan(); |
| 715 | } |
| 716 | |
| 717 | m_painter->closeParagraph(); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | void ZMFCollector::flushText(std::string &text) |
| 722 | { |
| 723 | if (!text.empty()) |
| 724 | { |
| 725 | m_painter->insertText(librevenge::RVNGString(text.c_str())); |
| 726 | text.clear(); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | void ZMFCollector::collectTable(const Table &table) |
| 731 | { |
| 732 | librevenge::RVNGPropertyList tablePropList; |
| 733 | |
| 734 | tablePropList.insert("svg:x", pageX(table.topLeftPoint.x)); |
| 735 | tablePropList.insert("svg:y", pageY(table.topLeftPoint.y)); |
| 736 | tablePropList.insert("svg:width", table.width); |
| 737 | tablePropList.insert("svg:height", table.height); |
| 738 | |
| 739 | librevenge::RVNGPropertyListVector columnSizes; |
| 740 | |
| 741 | for (const auto &col : table.columns) |
| 742 | { |
| 743 | librevenge::RVNGPropertyList columnPropList; |
| 744 | columnPropList.insert("style:column-width", col.width); |
| 745 | columnSizes.append(columnPropList); |
| 746 | } |
| 747 | |
| 748 | tablePropList.insert("librevenge:table-columns", columnSizes); |
| 749 | |
| 750 | m_painter->startTableObject(tablePropList); |
| 751 | |
| 752 | for (const auto &row : table.rows) |
| 753 | { |
| 754 | librevenge::RVNGPropertyList rowPropList; |
| 755 | rowPropList.insert("style:row-height", row.height); |
| 756 | m_painter->openTableRow(rowPropList); |
| 757 | |
| 758 | for (const Cell &cell : row.cells) |
| 759 | { |
| 760 | librevenge::RVNGPropertyList cellPropList; |
| 761 | |
| 762 | if (cell.fill && get(cell.fill).type() == typeid(Color)) |
| 763 | { |
| 764 | auto backgroundColor = boost::get<Color>(get(cell.fill)); |
| 765 | cellPropList.insert("fo:background-color", backgroundColor.toString()); |
| 766 | } |
| 767 | |
| 768 | cellPropList.insert("draw:textarea-vertical-align", "middle"); |
| 769 | |
| 770 | if (cell.leftBorder) |
| 771 | { |
| 772 | writeBorder(cellPropList, "fo:border-left", get(cell.leftBorder)); |
| 773 | } |
| 774 | if (cell.rightBorder) |
| 775 | { |
| 776 | writeBorder(cellPropList, "fo:border-right", get(cell.rightBorder)); |
| 777 | } |
| 778 | if (cell.topBorder) |
| 779 | { |
| 780 | writeBorder(cellPropList, "fo:border-top", get(cell.topBorder)); |
| 781 | } |
| 782 | if (cell.bottomBorder) |
| 783 | { |
| 784 | writeBorder(cellPropList, "fo:border-bottom", get(cell.bottomBorder)); |
| 785 | } |
| 786 | |
| 787 | m_painter->openTableCell(cellPropList); |
| 788 | |
| 789 | collectText(cell.text); |
| 790 | |
| 791 | m_painter->closeTableCell(); |
| 792 | } |
| 793 | |
| 794 | m_painter->closeTableRow(); |
| 795 | } |
| 796 | |
| 797 | m_painter->endTableObject(); |
| 798 | } |
| 799 | |
| 800 | void ZMFCollector::collectImage(const librevenge::RVNGBinaryData &image, const Point &topLeft, double width, double height, |
| 801 | double rotation, bool mirrorHorizontal, bool mirrorVertical) |
| 802 | { |
| 803 | librevenge::RVNGPropertyList propList; |
| 804 | |
| 805 | writeStyle(propList, m_style); |
| 806 | |
| 807 | if (m_style.transparency) |
| 808 | { |
| 809 | propList.insert("draw:opacity", get(m_style.transparency).opacity(), librevenge::RVNG_PERCENT); |
| 810 | } |
| 811 | |
| 812 | m_painter->setStyle(propList); |
| 813 | propList.clear(); |
| 814 | |
| 815 | propList.insert("svg:x", pageX(topLeft.x)); |
| 816 | propList.insert("svg:y", pageY(topLeft.y)); |
| 817 | propList.insert("svg:width", width); |
| 818 | propList.insert("svg:height", height); |
| 819 | if (!ZMF_ALMOST_ZERO(rotation)) |
| 820 | { |
| 821 | propList.insert("librevenge:rotate", rad2deg(rotation)); |
| 822 | } |
| 823 | propList.insert("draw:mirror-vertical", mirrorVertical); |
| 824 | propList.insert("draw:mirror-horizontal", mirrorHorizontal); |
| 825 | propList.insert("librevenge:mime-type", "image/png"); |
| 826 | propList.insert("office:binary-data", image); |
| 827 | |
| 828 | m_painter->drawGraphicObject(propList); |
| 829 | } |
| 830 | |
| 831 | double ZMFCollector::pageX(double canvasX) |
| 832 | { |
| 833 | return canvasX - m_pageSettings.leftOffset; |
| 834 | } |
| 835 | |
| 836 | double ZMFCollector::pageY(double canvasY) |
| 837 | { |
| 838 | return canvasY - m_pageSettings.topOffset; |
| 839 | } |
| 840 | |
| 841 | // noFill is used to ignore Fill object even if it exists |
| 842 | // some implementations (such as svg in web browsers) try to fill not closed paths if fill is specified |
| 843 | void ZMFCollector::writeStyle(librevenge::RVNGPropertyList &propList, const Style &style, bool noFill) |
| 844 | { |
| 845 | propList.insert("draw:stroke", "none"); |
| 846 | propList.insert("draw:fill", "none"); |
| 847 | |
| 848 | if (style.pen) |
| 849 | { |
| 850 | writePen(propList, get(style.pen)); |
| 851 | } |
| 852 | |
| 853 | if (style.fill && !noFill) |
| 854 | { |
| 855 | writeFill(propList, get(style.fill)); |
| 856 | } |
| 857 | |
| 858 | if (style.shadow) |
| 859 | { |
| 860 | writeShadow(propList, get(style.shadow)); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | void ZMFCollector::writePen(librevenge::RVNGPropertyList &propList, const Pen &pen) |
| 865 | { |
| 866 | propList.insert("svg:stroke-color", pen.color.toString()); |
| 867 | if (!ZMF_ALMOST_ZERO(pen.width)) |
| 868 | propList.insert("svg:stroke-width", pen.width); |
| 869 | |
| 870 | if (pen.dashPattern.size() > 0) |
| 871 | { |
| 872 | double dots1 = pen.dashPattern[0]; |
| 873 | double dots2 = pen.dashPattern[0]; |
| 874 | double dist = pen.dashDistance; |
| 875 | if (pen.dashPattern.size() >= 3) |
| 876 | { |
| 877 | dist = pen.dashPattern[1]; |
| 878 | dots2 = pen.dashPattern[2]; |
| 879 | } |
| 880 | |
| 881 | propList.insert("draw:stroke", "dash"); |
| 882 | propList.insert("draw:dots1", 1); |
| 883 | propList.insert("draw:dots1-length", dots1, librevenge::RVNG_PERCENT); |
| 884 | propList.insert("draw:dots2", 1); |
| 885 | propList.insert("draw:dots2-length", dots2, librevenge::RVNG_PERCENT); |
| 886 | propList.insert("draw:distance", dist, librevenge::RVNG_PERCENT); |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | propList.insert("draw:stroke", "solid"); |
| 891 | } |
| 892 | |
| 893 | switch (pen.lineCapType) |
| 894 | { |
| 895 | default: |
| 896 | case LineCapType::BUTT: |
| 897 | propList.insert("svg:stroke-linecap", "butt"); |
| 898 | break; |
| 899 | case LineCapType::ROUND: |
| 900 | propList.insert("svg:stroke-linecap", "round"); |
| 901 | break; |
| 902 | case LineCapType::FLAT: |
| 903 | propList.insert("svg:stroke-linecap", "square"); |
| 904 | break; |
| 905 | } |
| 906 | |
| 907 | switch (pen.lineJoinType) |
| 908 | { |
| 909 | default: |
| 910 | case LineJoinType::BEVEL: |
| 911 | propList.insert("svg:stroke-linejoin", "bevel"); |
| 912 | break; |
| 913 | case LineJoinType::MITER: |
| 914 | propList.insert("svg:stroke-linejoin", "miter"); |
| 915 | break; |
| 916 | case LineJoinType::ROUND: |
| 917 | propList.insert("svg:stroke-linejoin", "round"); |
| 918 | break; |
| 919 | } |
| 920 | |
| 921 | if (m_style.transparency) |
| 922 | { |
| 923 | propList.insert("svg:stroke-opacity", get(m_style.transparency).opacity(), librevenge::RVNG_PERCENT); |
| 924 | } |
| 925 | |
| 926 | if (pen.startArrow) |
| 927 | { |
| 928 | writeArrow(propList, "start", *pen.startArrow, pen.width); |
| 929 | } |
| 930 | if (pen.endArrow) |
| 931 | { |
| 932 | writeArrow(propList, "end", *pen.endArrow, pen.width); |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | void ZMFCollector::writeFill(librevenge::RVNGPropertyList &propList, const Fill &fill) |
| 937 | { |
| 938 | FillWriter fillWriter(propList, m_style.transparency); |
| 939 | boost::apply_visitor(fillWriter, fill); |
| 940 | |
| 941 | propList.insert("svg:fill-rule", "evenodd"); |
| 942 | } |
| 943 | |
| 944 | void ZMFCollector::writeShadow(librevenge::RVNGPropertyList &propList, const Shadow &shadow) |
| 945 | { |
| 946 | propList.insert("draw:shadow", "visible"); |
| 947 | propList.insert("draw:shadow-color", shadow.color.toString()); |
| 948 | propList.insert("draw:shadow-opacity", shadow.opacity, librevenge::RVNG_PERCENT); |
| 949 | propList.insert("draw:shadow-offset-x", shadow.offset.x); |
| 950 | propList.insert("draw:shadow-offset-y", shadow.offset.y); |
| 951 | } |
| 952 | |
| 953 | } |
| 954 | |
| 955 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |