| 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 "ZMF4Parser.h" |
| 11 | #include <algorithm> |
| 12 | #include <numeric> |
| 13 | #include <string> |
| 14 | #include "BMIParser.h" |
| 15 | |
| 16 | #define NO_ID 0xffffffff |
| 17 | |
| 18 | namespace libzmf |
| 19 | { |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | template<typename T> |
| 25 | boost::optional<T> getByRefId(uint32_t id, const std::map<uint32_t, T> &idObjMap) |
| 26 | { |
| 27 | if (id == NO_ID) |
| 28 | { |
| 29 | return boost::optional<T>(); |
| 30 | } |
| 31 | if (idObjMap.find(id) != idObjMap.end()) |
| 32 | { |
| 33 | return idObjMap.at(id); |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | ZMF_DEBUG_MSG(("%s with ID 0x%x not found\n", prettyTypeName<T>().c_str(), id)); |
| 38 | return boost::optional<T>(); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | } |
| 43 | |
| 44 | ZMF4Parser::ZMF4Parser(const RVNGInputStreamPtr &input, librevenge::RVNGDrawingInterface *painter) |
| 45 | : m_input(input) |
| 46 | , m_inputLength(0) |
| 47 | , m_collector(painter) |
| 48 | , m_header() |
| 49 | , m_pageSettings() |
| 50 | , m_pageNumber(0) |
| 51 | , m_currentObjectHeader() |
| 52 | , m_pens() |
| 53 | , m_fills() |
| 54 | , m_transparencies() |
| 55 | , m_shadows() |
| 56 | , m_arrows() |
| 57 | , m_images() |
| 58 | , m_fonts() |
| 59 | , m_paragraphStyles() |
| 60 | , m_texts() |
| 61 | { |
| 62 | // fill with ID 0x3 used by default for text (black) |
| 63 | m_fills[0x3] = Color(0, 0, 0); |
| 64 | |
| 65 | // pen with ID 0x1 is used for borders in table cells, rows and columns when they have no border |
| 66 | // (0xffffffff aka None probably not used because it would not override column/row pen) |
| 67 | Pen pen(Color(255, 255, 255)); |
| 68 | pen.isInvisible = true; |
| 69 | m_pens[0x1] = pen; |
| 70 | } |
| 71 | |
| 72 | bool ZMF4Parser::parse() |
| 73 | { |
| 74 | m_inputLength = getLength(m_input); |
| 75 | |
| 76 | if (!m_header.load(m_input) || !m_header.isSupported()) |
| 77 | return false; |
| 78 | |
| 79 | m_collector.startDocument(); |
| 80 | |
| 81 | if (m_header.startBitmapOffset() > 0) |
| 82 | { |
| 83 | seek(m_input, m_header.startBitmapOffset()); |
| 84 | |
| 85 | readPreviewBitmap(); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | seek(m_input, m_header.startContentOffset()); |
| 90 | } |
| 91 | |
| 92 | readDocumentSettings(); |
| 93 | |
| 94 | while (!m_input->isEnd()) |
| 95 | { |
| 96 | readPage(); |
| 97 | } |
| 98 | |
| 99 | m_collector.endDocument(); |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | ZMF4Parser::ObjectType ZMF4Parser::parseObjectType(uint8_t type) |
| 105 | { |
| 106 | switch (type) |
| 107 | { |
| 108 | case 0xa: |
| 109 | return ObjectType::FILL; |
| 110 | case 0xb: |
| 111 | return ObjectType::TRANSPARENCY; |
| 112 | case 0xc: |
| 113 | return ObjectType::PEN; |
| 114 | case 0xd: |
| 115 | return ObjectType::SHADOW; |
| 116 | case 0xf: |
| 117 | return ObjectType::ARROW; |
| 118 | case 0x10: |
| 119 | return ObjectType::FONT; |
| 120 | case 0x11: |
| 121 | return ObjectType::PARAGRAPH; |
| 122 | case 0x12: |
| 123 | return ObjectType::TEXT; |
| 124 | case 0xe: |
| 125 | return ObjectType::BITMAP; |
| 126 | case 0x21: |
| 127 | return ObjectType::PAGE_START; |
| 128 | case 0x22: |
| 129 | return ObjectType::GUIDELINES; |
| 130 | case 0x23: |
| 131 | return ObjectType::PAGE_END; |
| 132 | case 0x24: |
| 133 | return ObjectType::LAYER_START; |
| 134 | case 0x25: |
| 135 | return ObjectType::LAYER_END; |
| 136 | case 0x27: |
| 137 | return ObjectType::DOCUMENT_SETTINGS; |
| 138 | case 0x28: |
| 139 | return ObjectType::COLOR_PALETTE; |
| 140 | case 0x32: |
| 141 | return ObjectType::RECTANGLE; |
| 142 | case 0x33: |
| 143 | return ObjectType::ELLIPSE; |
| 144 | case 0x34: |
| 145 | return ObjectType::POLYGON; |
| 146 | case 0x36: |
| 147 | return ObjectType::CURVE; |
| 148 | case 0x37: |
| 149 | return ObjectType::IMAGE; |
| 150 | case 0x3a: |
| 151 | return ObjectType::TEXT_FRAME; |
| 152 | case 0x3b: |
| 153 | return ObjectType::TABLE; |
| 154 | case 0x41: |
| 155 | return ObjectType::GROUP_START; |
| 156 | case 0x42: |
| 157 | return ObjectType::GROUP_END; |
| 158 | default: |
| 159 | ZMF_DEBUG_MSG(("Unknown object type 0x%x\n", type)); |
| 160 | return ObjectType::UNKNOWN; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | ZMF4Parser::ObjectHeader ZMF4Parser::readObjectHeader() |
| 165 | { |
| 166 | uint32_t startOffset = m_input->tell(); |
| 167 | |
| 168 | ObjectHeader header; |
| 169 | |
| 170 | header.size = readU32(m_input); |
| 171 | header.type = parseObjectType(readU8(m_input)); |
| 172 | |
| 173 | skip(m_input, 7); |
| 174 | |
| 175 | header.refObjCount = readU32(m_input); |
| 176 | header.refListStartOffset = readU32(m_input); |
| 177 | |
| 178 | if (header.size == 0 || header.size > m_inputLength - startOffset || |
| 179 | header.refListStartOffset >= header.size || |
| 180 | header.refObjCount > (header.size - header.refListStartOffset) / 8) |
| 181 | { |
| 182 | ZMF_DEBUG_MSG(("Incorrect object header, offset %u\n", startOffset)); |
| 183 | throw GenericException(); |
| 184 | } |
| 185 | |
| 186 | skip(m_input, 4); |
| 187 | |
| 188 | uint32_t id = readU32(m_input); |
| 189 | if (id != NO_ID) |
| 190 | { |
| 191 | header.id = id; |
| 192 | } |
| 193 | |
| 194 | header.nextObjectOffset = startOffset + header.size; |
| 195 | if (header.refListStartOffset > 0) |
| 196 | header.refListStartOffset += startOffset; |
| 197 | |
| 198 | return header; |
| 199 | } |
| 200 | |
| 201 | std::vector<ZMF4Parser::ObjectRef> ZMF4Parser::readObjectRefs() |
| 202 | { |
| 203 | uint32_t refObjCount = m_currentObjectHeader.refObjCount; |
| 204 | long maxRefObjCount = long(static_cast<unsigned long>(m_currentObjectHeader.nextObjectOffset)) - m_input->tell() / 8; |
| 205 | if (maxRefObjCount < 0) |
| 206 | maxRefObjCount = 0; |
| 207 | if (refObjCount > maxRefObjCount) |
| 208 | refObjCount = uint32_t(static_cast<unsigned long>(maxRefObjCount)); |
| 209 | |
| 210 | std::vector<ZMF4Parser::ObjectRef> refs; |
| 211 | |
| 212 | if (refObjCount > 0) |
| 213 | { |
| 214 | refs.resize(refObjCount); |
| 215 | seek(m_input, m_currentObjectHeader.nextObjectOffset - 8 * refObjCount); |
| 216 | |
| 217 | for (uint32_t i = 0; i < refObjCount; i++) |
| 218 | { |
| 219 | refs[i].id = readU32(m_input); |
| 220 | } |
| 221 | |
| 222 | for (uint32_t i = 0; i < refObjCount; i++) |
| 223 | { |
| 224 | refs[i].tag = readU32(m_input); |
| 225 | } |
| 226 | |
| 227 | refs.erase(std::remove_if(refs.begin(), refs.end(), |
| 228 | [](const ObjectRef &ref) |
| 229 | { |
| 230 | return ref.id == NO_ID; |
| 231 | }), refs.end()); |
| 232 | } |
| 233 | |
| 234 | return refs; |
| 235 | } |
| 236 | |
| 237 | boost::optional<Fill> ZMF4Parser::getFillByRefId(uint32_t id) |
| 238 | { |
| 239 | return getByRefId<Fill>(id, m_fills); |
| 240 | } |
| 241 | |
| 242 | boost::optional<Pen> ZMF4Parser::getPenByRefId(uint32_t id) |
| 243 | { |
| 244 | return getByRefId<Pen>(id, m_pens); |
| 245 | } |
| 246 | |
| 247 | boost::optional<Shadow> ZMF4Parser::getShadowByRefId(uint32_t id) |
| 248 | { |
| 249 | return getByRefId<Shadow>(id, m_shadows); |
| 250 | } |
| 251 | |
| 252 | boost::optional<Transparency> ZMF4Parser::getTransparencyByRefId(uint32_t id) |
| 253 | { |
| 254 | return getByRefId<Transparency>(id, m_transparencies); |
| 255 | } |
| 256 | |
| 257 | boost::optional<Font> ZMF4Parser::getFontByRefId(uint32_t id) |
| 258 | { |
| 259 | return getByRefId<Font>(id, m_fonts); |
| 260 | } |
| 261 | |
| 262 | boost::optional<ParagraphStyle> ZMF4Parser::getParagraphStyleByRefId(uint32_t id) |
| 263 | { |
| 264 | return getByRefId<ParagraphStyle>(id, m_paragraphStyles); |
| 265 | } |
| 266 | |
| 267 | boost::optional<Text> ZMF4Parser::getTextByRefId(uint32_t id) |
| 268 | { |
| 269 | return getByRefId<Text>(id, m_texts); |
| 270 | } |
| 271 | |
| 272 | boost::optional<Image> ZMF4Parser::getImageByRefId(uint32_t id) |
| 273 | { |
| 274 | return getByRefId<Image>(id, m_images); |
| 275 | } |
| 276 | |
| 277 | ArrowPtr ZMF4Parser::getArrowByRefId(uint32_t id) |
| 278 | { |
| 279 | auto arrow = getByRefId<ArrowPtr>(id, m_arrows); |
| 280 | return get_optional_value_or(arrow, ArrowPtr()); |
| 281 | } |
| 282 | |
| 283 | Style ZMF4Parser::readStyle() |
| 284 | { |
| 285 | auto refs = readObjectRefs(); |
| 286 | |
| 287 | Style style; |
| 288 | |
| 289 | for (const auto &ref : refs) |
| 290 | { |
| 291 | switch (ref.tag) |
| 292 | { |
| 293 | case 1: |
| 294 | style.fill = getFillByRefId(ref.id); |
| 295 | break; |
| 296 | case 2: |
| 297 | style.pen = getPenByRefId(ref.id); |
| 298 | break; |
| 299 | case 3: |
| 300 | style.shadow = getShadowByRefId(ref.id); |
| 301 | break; |
| 302 | case 4: |
| 303 | style.transparency = getTransparencyByRefId(ref.id); |
| 304 | break; |
| 305 | default: |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | return style; |
| 311 | } |
| 312 | |
| 313 | Point ZMF4Parser::readPoint() |
| 314 | { |
| 315 | double x = um2in(readS32(m_input)); |
| 316 | double y = um2in(readS32(m_input)); |
| 317 | return Point(x, y); |
| 318 | } |
| 319 | |
| 320 | Point ZMF4Parser::readUnscaledPoint() |
| 321 | { |
| 322 | double x = readFloat(m_input); |
| 323 | double y = readFloat(m_input); |
| 324 | return Point(x, y); |
| 325 | } |
| 326 | |
| 327 | BoundingBox ZMF4Parser::readBoundingBox() |
| 328 | { |
| 329 | // width and height may not be correct in some cases |
| 330 | // for example looks like it's not updated when resizing objects |
| 331 | skip(m_input, 8); |
| 332 | |
| 333 | std::vector<Point> points; |
| 334 | |
| 335 | for (uint32_t i = 0; i < 4; i++) |
| 336 | { |
| 337 | points.push_back(readPoint()); |
| 338 | } |
| 339 | |
| 340 | #ifdef DEBUG_DRAW_BBOX |
| 341 | Curve curve; |
| 342 | curve.points = points; |
| 343 | curve.sectionTypes = std::vector<CurveType>(curve.points.size() - 1, CurveType::LINE); |
| 344 | curve.closed = true; |
| 345 | |
| 346 | Style style; |
| 347 | style.pen = Pen(Color(0, 0, 255)); |
| 348 | m_collector.setStyle(style); |
| 349 | |
| 350 | m_collector.collectPath(curve); |
| 351 | #endif |
| 352 | |
| 353 | return BoundingBox(points); |
| 354 | } |
| 355 | |
| 356 | void ZMF4Parser::readCurveSectionTypes(std::vector<CurveType> §ionTypes) |
| 357 | { |
| 358 | while (true) |
| 359 | { |
| 360 | uint32_t type = readU32(m_input); |
| 361 | switch (type) |
| 362 | { |
| 363 | default: |
| 364 | ZMF_DEBUG_MSG(("Unknown point type %u\n", type)); |
| 365 | // fall-through intended: pick a default |
| 366 | case 1: |
| 367 | sectionTypes.push_back(CurveType::LINE); |
| 368 | break; |
| 369 | case 2: |
| 370 | sectionTypes.push_back(CurveType::BEZIER_CURVE); |
| 371 | skip(m_input, 8); |
| 372 | break; |
| 373 | case 0x64: |
| 374 | return; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | std::vector<Curve> ZMF4Parser::readCurveComponents(std::function<Point()> readPointFunc) |
| 380 | { |
| 381 | uint32_t componentsCount = readU32(m_input); |
| 382 | if (componentsCount == 0 || componentsCount > 10000) // just a random big number |
| 383 | { |
| 384 | ZMF_DEBUG_MSG(("Incorrect curve component count, offset %ld\n", m_input->tell())); |
| 385 | return std::vector<Curve>(); |
| 386 | } |
| 387 | |
| 388 | std::vector<Curve> curves(componentsCount); |
| 389 | |
| 390 | for (uint32_t i = 0; i < componentsCount; i++) |
| 391 | { |
| 392 | skip(m_input, 8); |
| 393 | |
| 394 | uint32_t componentPointsCount = readU32(m_input); |
| 395 | if (componentPointsCount == 0 || componentPointsCount > 10000) // just a random big number |
| 396 | { |
| 397 | ZMF_DEBUG_MSG(("Incorrect curve point count, offset %ld\n", m_input->tell())); |
| 398 | return std::vector<Curve>(); |
| 399 | } |
| 400 | |
| 401 | curves[i].points.resize(componentPointsCount); |
| 402 | curves[i].sectionTypes.reserve(componentPointsCount - 1); |
| 403 | |
| 404 | curves[i].closed = static_cast<bool>(readU32(m_input)); |
| 405 | } |
| 406 | |
| 407 | for (auto &curve : curves) |
| 408 | { |
| 409 | for (size_t i = 0; i < curve.points.size(); i++) |
| 410 | { |
| 411 | curve.points[i] = readPointFunc(); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | for (auto &curve : curves) |
| 416 | { |
| 417 | readCurveSectionTypes(curve.sectionTypes); |
| 418 | } |
| 419 | |
| 420 | return curves; |
| 421 | } |
| 422 | |
| 423 | Color ZMF4Parser::readColor() |
| 424 | { |
| 425 | Color color; |
| 426 | color.red = readU8(m_input); |
| 427 | color.green = readU8(m_input); |
| 428 | color.blue = readU8(m_input); |
| 429 | return color; |
| 430 | } |
| 431 | |
| 432 | Gradient ZMF4Parser::readGradient(uint32_t type) |
| 433 | { |
| 434 | Gradient gradient; |
| 435 | |
| 436 | switch (type) |
| 437 | { |
| 438 | case 2: |
| 439 | gradient.type = GradientType::LINEAR; |
| 440 | break; |
| 441 | case 3: |
| 442 | gradient.type = GradientType::RADIAL; |
| 443 | break; |
| 444 | case 4: |
| 445 | gradient.type = GradientType::CONICAL; |
| 446 | break; |
| 447 | case 5: |
| 448 | gradient.type = GradientType::CROSS; |
| 449 | break; |
| 450 | case 6: |
| 451 | gradient.type = GradientType::RECTANGULAR; |
| 452 | break; |
| 453 | case 7: |
| 454 | gradient.type = GradientType::FLEXIBLE; |
| 455 | break; |
| 456 | default: |
| 457 | ZMF_DEBUG_MSG(("unknown gradient type %u\n", type)); |
| 458 | gradient.type = GradientType::LINEAR; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | skip(m_input, 4); |
| 463 | |
| 464 | uint32_t stopCount = readU32(m_input); |
| 465 | if (m_input->tell() + 20 + stopCount * 16 > m_currentObjectHeader.nextObjectOffset || |
| 466 | m_input->tell() + 20 + stopCount * 16 < stopCount) // overflow |
| 467 | { |
| 468 | ZMF_DEBUG_MSG(("Incorrect gradient stop count, offset %ld\n", m_input->tell())); |
| 469 | return gradient; |
| 470 | } |
| 471 | |
| 472 | skip(m_input, 4); |
| 473 | |
| 474 | gradient.center.x = readFloat(m_input); |
| 475 | gradient.center.y = readFloat(m_input); |
| 476 | |
| 477 | gradient.angle = readFloat(m_input); |
| 478 | |
| 479 | skip(m_input, 4); |
| 480 | |
| 481 | gradient.stops.reserve(stopCount); |
| 482 | for (uint32_t i = 0; i < stopCount; i++) |
| 483 | { |
| 484 | GradientStop stop; |
| 485 | |
| 486 | skip(m_input, 4); |
| 487 | stop.color = readColor(); |
| 488 | |
| 489 | skip(m_input, 5); |
| 490 | stop.offset = readFloat(m_input); |
| 491 | |
| 492 | gradient.stops.push_back(stop); |
| 493 | } |
| 494 | |
| 495 | return gradient; |
| 496 | } |
| 497 | |
| 498 | void ZMF4Parser::readPreviewBitmap() |
| 499 | { |
| 500 | skip(m_input, 2); |
| 501 | |
| 502 | uint32_t size = readU32(m_input); |
| 503 | |
| 504 | skip(m_input, size - 2 - 4); |
| 505 | } |
| 506 | |
| 507 | void ZMF4Parser::readDocumentSettings() |
| 508 | { |
| 509 | ObjectHeader header = readObjectHeader(); |
| 510 | |
| 511 | if (header.type != ObjectType::DOCUMENT_SETTINGS) |
| 512 | throw GenericException(); |
| 513 | |
| 514 | skip(m_input, 32); |
| 515 | |
| 516 | Color color = readColor(); |
| 517 | |
| 518 | skip(m_input, 5); |
| 519 | |
| 520 | double pageWidth = um2in(readU32(m_input)); |
| 521 | double pageHeight = um2in(readU32(m_input)); |
| 522 | |
| 523 | skip(m_input, 68); |
| 524 | |
| 525 | double leftOffset = um2in(readU32(m_input)); |
| 526 | double topOffset = um2in(readU32(m_input)); |
| 527 | |
| 528 | m_pageSettings = ZMFPageSettings(pageWidth, pageHeight, leftOffset, topOffset, color); |
| 529 | |
| 530 | seek(m_input, header.nextObjectOffset); |
| 531 | } |
| 532 | |
| 533 | void ZMF4Parser::readPage() |
| 534 | { |
| 535 | ObjectHeader startPageHeader; |
| 536 | |
| 537 | // skip color palettes, all used colors are included in Fill/Pen/... |
| 538 | while (true) |
| 539 | { |
| 540 | startPageHeader = readObjectHeader(); |
| 541 | if (startPageHeader.type == ObjectType::COLOR_PALETTE) |
| 542 | { |
| 543 | seek(m_input, startPageHeader.nextObjectOffset); |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (startPageHeader.type != ObjectType::PAGE_START) |
| 552 | throw GenericException(); |
| 553 | |
| 554 | m_pageNumber++; |
| 555 | |
| 556 | // skip first empty/master page |
| 557 | if (m_pageNumber == 1) |
| 558 | { |
| 559 | do |
| 560 | { |
| 561 | seek(m_input, startPageHeader.nextObjectOffset); |
| 562 | startPageHeader = readObjectHeader(); |
| 563 | } |
| 564 | while (startPageHeader.type != ObjectType::PAGE_START); |
| 565 | } |
| 566 | |
| 567 | m_collector.startPage(m_pageSettings); |
| 568 | |
| 569 | seek(m_input, startPageHeader.nextObjectOffset); |
| 570 | |
| 571 | while (true) |
| 572 | { |
| 573 | ObjectHeader header = readObjectHeader(); |
| 574 | |
| 575 | switch (header.type) |
| 576 | { |
| 577 | case ObjectType::GUIDELINES: |
| 578 | seek(m_input, header.nextObjectOffset); |
| 579 | break; |
| 580 | case ObjectType::PAGE_END: |
| 581 | m_collector.endPage(); |
| 582 | if (!m_input->isEnd()) |
| 583 | seek(m_input, header.nextObjectOffset); |
| 584 | return; |
| 585 | case ObjectType::LAYER_START: |
| 586 | readLayer(header); |
| 587 | break; |
| 588 | default: |
| 589 | throw GenericException(); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void ZMF4Parser::readLayer(const ZMF4Parser::ObjectHeader &layerStartObjHeader) |
| 595 | { |
| 596 | if (layerStartObjHeader.type != ObjectType::LAYER_START) |
| 597 | throw GenericException(); |
| 598 | |
| 599 | m_collector.startLayer(); |
| 600 | |
| 601 | seek(m_input, layerStartObjHeader.nextObjectOffset); |
| 602 | |
| 603 | while (true) |
| 604 | { |
| 605 | m_currentObjectHeader = readObjectHeader(); |
| 606 | |
| 607 | switch (m_currentObjectHeader.type) |
| 608 | { |
| 609 | case ObjectType::LAYER_END: |
| 610 | seek(m_input, m_currentObjectHeader.nextObjectOffset); |
| 611 | m_collector.endLayer(); |
| 612 | return; |
| 613 | case ObjectType::FILL: |
| 614 | readFill(); |
| 615 | break; |
| 616 | case ObjectType::TRANSPARENCY: |
| 617 | readTransparency(); |
| 618 | break; |
| 619 | case ObjectType::PEN: |
| 620 | readPen(); |
| 621 | break; |
| 622 | case ObjectType::SHADOW: |
| 623 | readShadow(); |
| 624 | break; |
| 625 | case ObjectType::ARROW: |
| 626 | readArrow(); |
| 627 | break; |
| 628 | case ObjectType::FONT: |
| 629 | readFont(); |
| 630 | break; |
| 631 | case ObjectType::PARAGRAPH: |
| 632 | readParagraphStyle(); |
| 633 | break; |
| 634 | case ObjectType::TEXT: |
| 635 | readText(); |
| 636 | break; |
| 637 | case ObjectType::BITMAP: |
| 638 | readBitmap(); |
| 639 | break; |
| 640 | case ObjectType::RECTANGLE: |
| 641 | readRectangle(); |
| 642 | break; |
| 643 | case ObjectType::ELLIPSE: |
| 644 | readEllipse(); |
| 645 | break; |
| 646 | case ObjectType::POLYGON: |
| 647 | readPolygon(); |
| 648 | break; |
| 649 | case ObjectType::CURVE: |
| 650 | readCurve(); |
| 651 | break; |
| 652 | case ObjectType::IMAGE: |
| 653 | readImage(); |
| 654 | break; |
| 655 | case ObjectType::TEXT_FRAME: |
| 656 | readTextFrame(); |
| 657 | break; |
| 658 | case ObjectType::TABLE: |
| 659 | readTable(); |
| 660 | break; |
| 661 | case ObjectType::GROUP_START: |
| 662 | m_collector.startGroup(); |
| 663 | break; |
| 664 | case ObjectType::GROUP_END: |
| 665 | m_collector.endGroup(); |
| 666 | break; |
| 667 | default: |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | if (m_currentObjectHeader.type != ObjectType::BITMAP) |
| 672 | { |
| 673 | seek(m_input, m_currentObjectHeader.nextObjectOffset); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | void ZMF4Parser::readFill() |
| 679 | { |
| 680 | if (!m_currentObjectHeader.id) |
| 681 | { |
| 682 | ZMF_DEBUG_MSG(("Fill without ID, offset %ld\n", m_input->tell())); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | skip(m_input, 8); |
| 687 | |
| 688 | uint32_t type = readU32(m_input); |
| 689 | |
| 690 | if (type == 1) |
| 691 | { |
| 692 | skip(m_input, 8); |
| 693 | |
| 694 | Color color = readColor(); |
| 695 | |
| 696 | m_fills[get(m_currentObjectHeader.id)] = color; |
| 697 | } |
| 698 | else if (type >= 2 && type <= 7) |
| 699 | { |
| 700 | Gradient gradient = readGradient(type); |
| 701 | |
| 702 | m_fills[get(m_currentObjectHeader.id)] = gradient; |
| 703 | } |
| 704 | else if (type == 8) |
| 705 | { |
| 706 | ImageFill imageFill; |
| 707 | |
| 708 | skip(m_input, 4); |
| 709 | |
| 710 | imageFill.tile = static_cast<bool>(readU32(m_input)); |
| 711 | imageFill.tileWidth = um2in(readU32(m_input)); |
| 712 | imageFill.tileHeight = um2in(readU32(m_input)); |
| 713 | |
| 714 | boost::optional<Image> image; |
| 715 | |
| 716 | auto refs = readObjectRefs(); |
| 717 | |
| 718 | for (const auto &ref : refs) |
| 719 | { |
| 720 | switch (ref.tag) |
| 721 | { |
| 722 | case 0: |
| 723 | { |
| 724 | image = getImageByRefId(ref.id); |
| 725 | } |
| 726 | break; |
| 727 | default: |
| 728 | ZMF_DEBUG_MSG(("unknown bitmap fill ref tag %u\n", ref.tag)); |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if (!image) |
| 734 | { |
| 735 | ZMF_DEBUG_MSG(("image not found for bitmap fill ID0x%x\n", get(m_currentObjectHeader.id))); |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | imageFill.image = get(image); |
| 740 | |
| 741 | m_fills[get(m_currentObjectHeader.id)] = imageFill; |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | ZMF_DEBUG_MSG(("unknown fill type %u\n", type)); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void ZMF4Parser::readTransparency() |
| 750 | { |
| 751 | if (!m_currentObjectHeader.id) |
| 752 | { |
| 753 | ZMF_DEBUG_MSG(("Transparency without ID, offset %ld\n", m_input->tell())); |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | skip(m_input, 8); |
| 758 | |
| 759 | uint32_t type = readU32(m_input); |
| 760 | |
| 761 | if (type == 1) |
| 762 | { |
| 763 | skip(m_input, 8); |
| 764 | |
| 765 | Transparency transparency; |
| 766 | transparency.color = readColor(); |
| 767 | |
| 768 | m_transparencies[get(m_currentObjectHeader.id)] = transparency; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | void ZMF4Parser::readPen() |
| 773 | { |
| 774 | if (!m_currentObjectHeader.id) |
| 775 | { |
| 776 | ZMF_DEBUG_MSG(("Pen without ID, offset %ld\n", m_input->tell())); |
| 777 | return; |
| 778 | } |
| 779 | |
| 780 | Pen pen; |
| 781 | |
| 782 | skip(m_input, 12); |
| 783 | |
| 784 | uint32_t lineJoin = readU32(m_input); |
| 785 | |
| 786 | switch (lineJoin) |
| 787 | { |
| 788 | default: |
| 789 | ZMF_DEBUG_MSG(("Unknown line join type %u\n", lineJoin)); |
| 790 | // fall-through intended: pick a default |
| 791 | case 0: |
| 792 | pen.lineJoinType = LineJoinType::MITER; |
| 793 | break; |
| 794 | case 1: |
| 795 | pen.lineJoinType = LineJoinType::ROUND; |
| 796 | break; |
| 797 | case 2: |
| 798 | pen.lineJoinType = LineJoinType::BEVEL; |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | uint32_t lineCap = readU32(m_input); |
| 803 | |
| 804 | switch (lineCap) |
| 805 | { |
| 806 | default: |
| 807 | ZMF_DEBUG_MSG(("Unknown line cap type %u\n", lineCap)); |
| 808 | // fall-through intended: pick a default |
| 809 | case 0: |
| 810 | pen.lineCapType = LineCapType::BUTT; |
| 811 | break; |
| 812 | case 1: |
| 813 | pen.lineCapType = LineCapType::FLAT; |
| 814 | break; |
| 815 | case 2: |
| 816 | pen.lineCapType = LineCapType::ROUND; |
| 817 | break; |
| 818 | case 3: |
| 819 | pen.lineCapType = LineCapType::POINTED; |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | skip(m_input, 4); |
| 824 | |
| 825 | pen.width = um2in(readU32(m_input)); |
| 826 | |
| 827 | skip(m_input, 4); |
| 828 | |
| 829 | pen.color = readColor(); |
| 830 | |
| 831 | skip(m_input, 17); |
| 832 | |
| 833 | auto dashBits = bytesToBitset<6>(readNBytes(m_input, 6)); |
| 834 | uint16_t dashLength = readU16(m_input); |
| 835 | |
| 836 | if (!dashBits.all()) |
| 837 | { |
| 838 | int curLength = 1; |
| 839 | bool prevVal = true; |
| 840 | // sometimes the first bit of the 4th byte is set |
| 841 | // not sure what it means but librevenge/ODF don't support complex patterns anyway, |
| 842 | // so 3 bytes should be enough |
| 843 | for (std::size_t i = 1; i < 24; i++) |
| 844 | { |
| 845 | if (dashBits[i] != prevVal) |
| 846 | { |
| 847 | pen.dashPattern.push_back(curLength); |
| 848 | curLength = 0; |
| 849 | } |
| 850 | |
| 851 | curLength++; |
| 852 | prevVal = dashBits[i]; |
| 853 | } |
| 854 | |
| 855 | int sum = static_cast<int>(std::accumulate(pen.dashPattern.begin(), pen.dashPattern.end(), 0)); |
| 856 | // not sure how to properly use length |
| 857 | // looks like it is length of all used "dots" (bits), empty or not, |
| 858 | // and each dot usually is 1024 of whatever units it is |
| 859 | // so we can easily get number of empty dots at the end |
| 860 | // (sometimes it is a bit bigger or smaller than 1024 though) |
| 861 | pen.dashDistance = (dashLength / 1024) - sum; |
| 862 | if (pen.dashDistance < 1) |
| 863 | { |
| 864 | pen.dashDistance = 1; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | auto refs = readObjectRefs(); |
| 869 | |
| 870 | for (const auto &ref : refs) |
| 871 | { |
| 872 | switch (ref.tag) |
| 873 | { |
| 874 | case 0: |
| 875 | pen.startArrow = getArrowByRefId(ref.id); |
| 876 | break; |
| 877 | case 1: |
| 878 | pen.endArrow = getArrowByRefId(ref.id); |
| 879 | break; |
| 880 | default: |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | m_pens[get(m_currentObjectHeader.id)] = pen; |
| 886 | } |
| 887 | |
| 888 | void ZMF4Parser::readShadow() |
| 889 | { |
| 890 | if (!m_currentObjectHeader.id) |
| 891 | { |
| 892 | ZMF_DEBUG_MSG(("Shadow without ID, offset %ld\n", m_input->tell())); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | Shadow shadow; |
| 897 | |
| 898 | skip(m_input, 8); |
| 899 | |
| 900 | uint32_t type = readU32(m_input); |
| 901 | |
| 902 | shadow.offset = readPoint(); |
| 903 | shadow.angle = readFloat(m_input); |
| 904 | |
| 905 | switch (type) |
| 906 | { |
| 907 | case 1: // Color |
| 908 | case 3: // Soft |
| 909 | { |
| 910 | skip(m_input, 4); |
| 911 | |
| 912 | shadow.color = readColor(); |
| 913 | |
| 914 | if (type == 1) |
| 915 | break; |
| 916 | |
| 917 | skip(m_input, 5); |
| 918 | |
| 919 | shadow.opacity = 1.0 - readFloat(m_input); |
| 920 | |
| 921 | // todo: blur |
| 922 | } |
| 923 | break; |
| 924 | case 2: // Brightness |
| 925 | case 4: // Transparent |
| 926 | { |
| 927 | shadow.opacity = 1.0 - readFloat(m_input); |
| 928 | |
| 929 | // todo: ... |
| 930 | } |
| 931 | break; |
| 932 | default: |
| 933 | ZMF_DEBUG_MSG(("Unknown shadow type %u\n", type)); |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | m_shadows[get(m_currentObjectHeader.id)] = shadow; |
| 938 | } |
| 939 | |
| 940 | void ZMF4Parser::readArrow() |
| 941 | { |
| 942 | if (!m_currentObjectHeader.id) |
| 943 | { |
| 944 | ZMF_DEBUG_MSG(("Arrow without ID, offset %ld\n", m_input->tell())); |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | ArrowPtr arrow(new Arrow); |
| 949 | |
| 950 | skip(m_input, 4); |
| 951 | |
| 952 | arrow->lineEndX = readFloat(m_input); |
| 953 | |
| 954 | skip(m_input, 12); |
| 955 | |
| 956 | arrow->curves = readCurveComponents(std::bind(&ZMF4Parser::readUnscaledPoint, this)); |
| 957 | |
| 958 | m_arrows[get(m_currentObjectHeader.id)] = arrow; |
| 959 | } |
| 960 | |
| 961 | void ZMF4Parser::readBitmap() |
| 962 | { |
| 963 | if (!m_currentObjectHeader.id) |
| 964 | { |
| 965 | ZMF_DEBUG_MSG(("Bitmap without ID, offset %ld\n", m_input->tell())); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | skip(m_input, 4); |
| 970 | |
| 971 | uint32_t something = readU32(m_input); |
| 972 | bool hasData = static_cast<bool>(something); |
| 973 | |
| 974 | seek(m_input, m_currentObjectHeader.nextObjectOffset); |
| 975 | |
| 976 | if (hasData) |
| 977 | { |
| 978 | BMIParser parser(m_input); |
| 979 | |
| 980 | auto image = parser.readImage(); |
| 981 | |
| 982 | const auto &header = parser.header(); |
| 983 | |
| 984 | if (image.data.empty()) |
| 985 | { |
| 986 | ZMF_DEBUG_MSG(("Failed to parse bitmap, ID 0x%x\n", get(m_currentObjectHeader.id))); |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | m_images[get(m_currentObjectHeader.id)] = image; |
| 991 | } |
| 992 | |
| 993 | seek(m_input, m_currentObjectHeader.nextObjectOffset + header.size()); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | void ZMF4Parser::readImage() |
| 998 | { |
| 999 | auto bbox = readBoundingBox(); |
| 1000 | |
| 1001 | boost::optional<Image> image; |
| 1002 | |
| 1003 | auto refs = readObjectRefs(); |
| 1004 | |
| 1005 | for (const auto &ref : refs) |
| 1006 | { |
| 1007 | switch (ref.tag) |
| 1008 | { |
| 1009 | case 5: |
| 1010 | { |
| 1011 | image = getImageByRefId(ref.id); |
| 1012 | } |
| 1013 | break; |
| 1014 | default: |
| 1015 | break; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if (image) |
| 1020 | { |
| 1021 | m_collector.setStyle(readStyle()); |
| 1022 | |
| 1023 | m_collector.collectImage(get(image).data, bbox.topLeft(), bbox.width(), bbox.height(), |
| 1024 | bbox.rotation(), bbox.mirrorHorizontal(), bbox.mirrorVertical()); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | void ZMF4Parser::readFont() |
| 1029 | { |
| 1030 | if (!m_currentObjectHeader.id) |
| 1031 | { |
| 1032 | ZMF_DEBUG_MSG(("Font without ID, offset %ld\n", m_input->tell())); |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | skip(m_input, 4); |
| 1037 | |
| 1038 | Font font; |
| 1039 | |
| 1040 | auto formatFlags = readU8(m_input); |
| 1041 | |
| 1042 | font.isBold = formatFlags & 0x1; |
| 1043 | font.isItalic = formatFlags & 0x2; |
| 1044 | |
| 1045 | skip(m_input, 3); |
| 1046 | |
| 1047 | font.size = readFloat(m_input); |
| 1048 | |
| 1049 | skip(m_input, 4); |
| 1050 | |
| 1051 | const std::string name(reinterpret_cast<const char *>(readNBytes(m_input, 32)), 31); |
| 1052 | font.name = librevenge::RVNGString(name.c_str()); |
| 1053 | |
| 1054 | auto style = readStyle(); |
| 1055 | font.fill = style.fill; |
| 1056 | font.outline = style.pen; |
| 1057 | |
| 1058 | m_fonts[get(m_currentObjectHeader.id)] = font; |
| 1059 | } |
| 1060 | |
| 1061 | void ZMF4Parser::readParagraphStyle() |
| 1062 | { |
| 1063 | if (!m_currentObjectHeader.id) |
| 1064 | { |
| 1065 | ZMF_DEBUG_MSG(("Paragraph without ID, offset %ld\n", m_input->tell())); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | skip(m_input, 4); |
| 1070 | |
| 1071 | ParagraphStyle parStyle; |
| 1072 | |
| 1073 | auto align = readU8(m_input); |
| 1074 | |
| 1075 | switch (align) |
| 1076 | { |
| 1077 | default: |
| 1078 | ZMF_DEBUG_MSG(("Unknown paragraph alignment %u\n", align)); |
| 1079 | // fall-through intended: pick a default |
| 1080 | case 0: |
| 1081 | parStyle.alignment = HorizontalAlignment::LEFT; |
| 1082 | break; |
| 1083 | case 1: |
| 1084 | parStyle.alignment = HorizontalAlignment::RIGHT; |
| 1085 | break; |
| 1086 | case 2: |
| 1087 | parStyle.alignment = HorizontalAlignment::BLOCK; |
| 1088 | break; |
| 1089 | case 3: |
| 1090 | parStyle.alignment = HorizontalAlignment::CENTER; |
| 1091 | break; |
| 1092 | case 4: |
| 1093 | parStyle.alignment = HorizontalAlignment::FULL; |
| 1094 | break; |
| 1095 | } |
| 1096 | |
| 1097 | skip(m_input, 3); |
| 1098 | |
| 1099 | parStyle.lineSpacing = readFloat(m_input); |
| 1100 | |
| 1101 | auto refs = readObjectRefs(); |
| 1102 | |
| 1103 | for (const auto &ref : refs) |
| 1104 | { |
| 1105 | switch (ref.tag) |
| 1106 | { |
| 1107 | case 1: |
| 1108 | { |
| 1109 | auto font = getFontByRefId(ref.id); |
| 1110 | if (font) |
| 1111 | parStyle.font = get(font); |
| 1112 | } |
| 1113 | break; |
| 1114 | default: |
| 1115 | ZMF_DEBUG_MSG(("unknown paragraph ref tag %u\n", ref.tag)); |
| 1116 | break; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | m_paragraphStyles[get(m_currentObjectHeader.id)] = parStyle; |
| 1121 | } |
| 1122 | |
| 1123 | void ZMF4Parser::readText() |
| 1124 | { |
| 1125 | skip(m_input, 12); |
| 1126 | |
| 1127 | uint32_t paragraphCount = readU32(m_input); |
| 1128 | if (paragraphCount == 0 || paragraphCount > 1000) // just a random big number |
| 1129 | { |
| 1130 | ZMF_DEBUG_MSG(("Incorrect paragraph count, offset %ld\n", m_input->tell())); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | Text text; |
| 1135 | |
| 1136 | text.paragraphs.resize(paragraphCount); |
| 1137 | |
| 1138 | skip(m_input, 4); |
| 1139 | |
| 1140 | for (auto ¶graph : text.paragraphs) |
| 1141 | { |
| 1142 | uint32_t spanCount = readU32(m_input); |
| 1143 | if (spanCount > 1000) // just a random big number |
| 1144 | { |
| 1145 | ZMF_DEBUG_MSG(("Incorrect span count, offset %ld\n", m_input->tell())); |
| 1146 | return; |
| 1147 | } |
| 1148 | paragraph.spans.resize(spanCount); |
| 1149 | |
| 1150 | uint32_t styleId = readU32(m_input); |
| 1151 | auto style = getParagraphStyleByRefId(styleId); |
| 1152 | if (style) |
| 1153 | paragraph.style = get(style); |
| 1154 | |
| 1155 | skip(m_input, 4); |
| 1156 | } |
| 1157 | |
| 1158 | for (auto ¶graph : text.paragraphs) |
| 1159 | { |
| 1160 | for (auto &span : paragraph.spans) |
| 1161 | { |
| 1162 | span.length = readU32(m_input); |
| 1163 | if (span.length > m_currentObjectHeader.size) |
| 1164 | { |
| 1165 | ZMF_DEBUG_MSG(("Incorrect span length, offset %ld\n", m_input->tell())); |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | skip(m_input, 4); |
| 1170 | |
| 1171 | uint32_t fontId = readU32(m_input); |
| 1172 | auto font = getFontByRefId(fontId); |
| 1173 | if (font) |
| 1174 | span.font = get(font); |
| 1175 | else |
| 1176 | { |
| 1177 | span.font = paragraph.style.font; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | for (auto ¶graph : text.paragraphs) |
| 1183 | { |
| 1184 | for (auto &span : paragraph.spans) |
| 1185 | { |
| 1186 | auto size = span.length * 2; |
| 1187 | auto chars = readNBytes(m_input, size); |
| 1188 | |
| 1189 | appendCharacters(span.text, chars, size, "UTF-16LE"); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | m_texts[get(m_currentObjectHeader.id)] = text; |
| 1194 | } |
| 1195 | |
| 1196 | void ZMF4Parser::readTextFrame() |
| 1197 | { |
| 1198 | auto bbox = readBoundingBox(); |
| 1199 | |
| 1200 | auto flags = readU8(m_input); |
| 1201 | |
| 1202 | auto alignment = VerticalAlignment::TOP; |
| 1203 | if (flags & 0x10) |
| 1204 | alignment = VerticalAlignment::MIDDLE; |
| 1205 | else if (flags & 0x20) |
| 1206 | alignment = VerticalAlignment::BOTTOM; |
| 1207 | |
| 1208 | boost::optional<Text> text; |
| 1209 | |
| 1210 | auto refs = readObjectRefs(); |
| 1211 | |
| 1212 | for (const auto &ref : refs) |
| 1213 | { |
| 1214 | switch (ref.tag) |
| 1215 | { |
| 1216 | case 6: |
| 1217 | { |
| 1218 | text = getTextByRefId(ref.id); |
| 1219 | } |
| 1220 | break; |
| 1221 | default: |
| 1222 | ZMF_DEBUG_MSG(("unknown text frame ref tag %u\n", ref.tag)); |
| 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | if (text) |
| 1228 | { |
| 1229 | m_collector.collectTextObject(get(text), bbox.topLeft(), bbox.width(), bbox.height(), alignment, bbox.rotation()); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | void ZMF4Parser::readCurve() |
| 1234 | { |
| 1235 | skip(m_input, 52); |
| 1236 | |
| 1237 | auto curves = readCurveComponents(std::bind(&ZMF4Parser::readPoint, this)); |
| 1238 | |
| 1239 | m_collector.setStyle(readStyle()); |
| 1240 | |
| 1241 | m_collector.collectPath(curves); |
| 1242 | } |
| 1243 | |
| 1244 | void ZMF4Parser::readRectangle() |
| 1245 | { |
| 1246 | Curve curve; |
| 1247 | curve.points = readBoundingBox().points(); |
| 1248 | curve.sectionTypes = std::vector<CurveType>(curve.points.size() - 1, CurveType::LINE); |
| 1249 | curve.closed = true; |
| 1250 | |
| 1251 | m_collector.setStyle(readStyle()); |
| 1252 | |
| 1253 | m_collector.collectPath(curve); |
| 1254 | } |
| 1255 | |
| 1256 | void ZMF4Parser::readEllipse() |
| 1257 | { |
| 1258 | auto bbox = readBoundingBox(); |
| 1259 | |
| 1260 | float beginAngle = readFloat(m_input); |
| 1261 | float endAngle = readFloat(m_input); |
| 1262 | |
| 1263 | bool closed = !readU8(m_input); |
| 1264 | |
| 1265 | double rx = bbox.width() / 2; |
| 1266 | double ry = bbox.height() / 2; |
| 1267 | |
| 1268 | m_collector.setStyle(readStyle()); |
| 1269 | |
| 1270 | if (ZMF_ALMOST_ZERO(beginAngle) && ZMF_ALMOST_ZERO(endAngle)) |
| 1271 | { |
| 1272 | m_collector.collectEllipse(bbox.center(), rx, ry, bbox.rotation()); |
| 1273 | } |
| 1274 | else |
| 1275 | { |
| 1276 | m_collector.collectArc(bbox.center(), rx, ry, beginAngle, endAngle, closed, bbox.rotation()); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | void ZMF4Parser::readPolygon() |
| 1281 | { |
| 1282 | auto bbox = readBoundingBox(); |
| 1283 | |
| 1284 | double rx = bbox.width() / 2; |
| 1285 | double ry = bbox.height() / 2; |
| 1286 | |
| 1287 | uint32_t peaksCount = readU32(m_input); |
| 1288 | if (peaksCount == 0 || peaksCount > 99) |
| 1289 | { |
| 1290 | ZMF_DEBUG_MSG(("Incorrect peak count, offset %ld\n", m_input->tell())); |
| 1291 | return; |
| 1292 | } |
| 1293 | |
| 1294 | uint32_t pointsCount = readU32(m_input); |
| 1295 | const uint32_t endOffset = m_currentObjectHeader.refListStartOffset == 0 |
| 1296 | ? m_currentObjectHeader.nextObjectOffset |
| 1297 | : m_currentObjectHeader.refListStartOffset |
| 1298 | ; |
| 1299 | if (pointsCount == 0 || m_input->tell() + 8 >= endOffset || pointsCount > (endOffset - m_input->tell() - 8) / 12) |
| 1300 | { |
| 1301 | ZMF_DEBUG_MSG(("Incorrect peak point count, offset %ld\n", m_input->tell())); |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | skip(m_input, 8); |
| 1306 | |
| 1307 | Curve peakCurve; |
| 1308 | peakCurve.points.reserve(pointsCount); |
| 1309 | |
| 1310 | for (uint32_t i = 0; i < pointsCount; i++) |
| 1311 | { |
| 1312 | peakCurve.points.push_back(readUnscaledPoint()); |
| 1313 | } |
| 1314 | |
| 1315 | readCurveSectionTypes(peakCurve.sectionTypes); |
| 1316 | |
| 1317 | m_collector.setStyle(readStyle()); |
| 1318 | |
| 1319 | m_collector.collectPolygon(bbox.center(), rx, ry, peaksCount, peakCurve, |
| 1320 | bbox.rotation(), bbox.mirrorHorizontal(), bbox.mirrorVertical()); |
| 1321 | } |
| 1322 | |
| 1323 | void ZMF4Parser::readTable() |
| 1324 | { |
| 1325 | Table table; |
| 1326 | |
| 1327 | auto bbox = readBoundingBox(); |
| 1328 | |
| 1329 | table.width = bbox.width(); |
| 1330 | table.height = bbox.height(); |
| 1331 | |
| 1332 | table.topLeftPoint = bbox.topLeft(); |
| 1333 | |
| 1334 | skip(m_input, 8); |
| 1335 | |
| 1336 | uint32_t rowCount = readU32(m_input); |
| 1337 | uint32_t colCount = readU32(m_input); |
| 1338 | |
| 1339 | if (rowCount < 1 || rowCount > 100 || colCount < 1 || colCount > 100) |
| 1340 | { |
| 1341 | ZMF_DEBUG_MSG(("Incorrect table size, offset %ld\n", m_input->tell())); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | skip(m_input, 8); |
| 1346 | |
| 1347 | table.rows.resize(rowCount); |
| 1348 | table.columns.resize(colCount); |
| 1349 | |
| 1350 | for (auto &row : table.rows) |
| 1351 | { |
| 1352 | row.cells.resize(colCount); |
| 1353 | |
| 1354 | for (auto &cell : row.cells) |
| 1355 | { |
| 1356 | skip(m_input, 4); |
| 1357 | |
| 1358 | uint32_t fillRefId = readU32(m_input); |
| 1359 | cell.fill = getFillByRefId(fillRefId); |
| 1360 | |
| 1361 | uint32_t textId = readU32(m_input); |
| 1362 | auto text = getTextByRefId(textId); |
| 1363 | if (text) |
| 1364 | { |
| 1365 | cell.text = get(text); |
| 1366 | } |
| 1367 | |
| 1368 | uint32_t rightPenRefId = readU32(m_input); |
| 1369 | cell.rightBorder = getPenByRefId(rightPenRefId); |
| 1370 | |
| 1371 | uint32_t bottomPenRefId = readU32(m_input); |
| 1372 | cell.bottomBorder = getPenByRefId(bottomPenRefId); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | for (auto &row : table.rows) |
| 1377 | { |
| 1378 | skip(m_input, 4); |
| 1379 | |
| 1380 | uint32_t leftPenRefId = readU32(m_input); |
| 1381 | auto leftBorderPen = getPenByRefId(leftPenRefId); |
| 1382 | |
| 1383 | if (leftBorderPen) |
| 1384 | { |
| 1385 | for (auto &cell : row.cells) |
| 1386 | { |
| 1387 | if (!cell.leftBorder) |
| 1388 | { |
| 1389 | cell.leftBorder = leftBorderPen; |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | double relHeight = readFloat(m_input) / rowCount; |
| 1395 | row.height = table.height * relHeight; |
| 1396 | } |
| 1397 | |
| 1398 | for (size_t i = 0; i < table.columns.size(); i++) |
| 1399 | { |
| 1400 | auto &col = table.columns[i]; |
| 1401 | skip(m_input, 4); |
| 1402 | |
| 1403 | uint32_t topPenRefId = readU32(m_input); |
| 1404 | auto topBorderPen = getPenByRefId(topPenRefId); |
| 1405 | |
| 1406 | if (topBorderPen) |
| 1407 | { |
| 1408 | for (auto &row : table.rows) |
| 1409 | { |
| 1410 | auto &cell = row.cells[i]; |
| 1411 | |
| 1412 | if (!cell.topBorder) |
| 1413 | { |
| 1414 | cell.topBorder = topBorderPen; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | double relWidth = readFloat(m_input) / colCount; |
| 1420 | col.width = table.width * relWidth; |
| 1421 | } |
| 1422 | |
| 1423 | auto tableStyle = readStyle(); |
| 1424 | |
| 1425 | for (auto &row : table.rows) |
| 1426 | { |
| 1427 | for (auto &cell : row.cells) |
| 1428 | { |
| 1429 | if (tableStyle.fill && !cell.fill) |
| 1430 | { |
| 1431 | cell.fill = tableStyle.fill; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | if (tableStyle.pen) |
| 1437 | { |
| 1438 | for (auto &cell : table.rows.front().cells) |
| 1439 | { |
| 1440 | cell.topBorder = tableStyle.pen; |
| 1441 | } |
| 1442 | for (auto &cell : table.rows.back().cells) |
| 1443 | { |
| 1444 | cell.bottomBorder = tableStyle.pen; |
| 1445 | } |
| 1446 | for (auto &row : table.rows) |
| 1447 | { |
| 1448 | row.cells.front().leftBorder = tableStyle.pen; |
| 1449 | row.cells.back().rightBorder = tableStyle.pen; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | m_collector.collectTable(table); |
| 1454 | } |
| 1455 | |
| 1456 | } |
| 1457 | |
| 1458 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |