| 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 "CDRParser.h" |
| 11 | |
| 12 | #include <locale.h> |
| 13 | #include <math.h> |
| 14 | #include <string.h> |
| 15 | #include <sstream> |
| 16 | #include <set> |
| 17 | #ifndef BOOST_ALL_NO_LIB |
| 18 | #define BOOST_ALL_NO_LIB 1 |
| 19 | #endif |
| 20 | #ifndef BOOST_BIND_GLOBAL_PLACEHOLDERS |
| 21 | #define BOOST_BIND_GLOBAL_PLACEHOLDERS 1 |
| 22 | #endif |
| 23 | #include <boost/algorithm/string.hpp> |
| 24 | #include <boost/optional.hpp> |
| 25 | #include <boost/property_tree/json_parser.hpp> |
| 26 | #include <boost/property_tree/ptree.hpp> |
| 27 | #include <boost/spirit/include/qi.hpp> |
| 28 | #include "libcdr_utils.h" |
| 29 | #include "CDRDocumentStructure.h" |
| 30 | #include "CDRInternalStream.h" |
| 31 | #include "CDRCollector.h" |
| 32 | #include "CDRColorPalettes.h" |
| 33 | |
| 34 | #ifndef DUMP_PREVIEW_IMAGE |
| 35 | #define DUMP_PREVIEW_IMAGE 0 |
| 36 | #endif |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | |
| 41 | unsigned getCDRVersion(char c) |
| 42 | { |
| 43 | if (c == 0x20) |
| 44 | return 300; |
| 45 | else if (c < 0x31) |
| 46 | return 0; |
| 47 | else if (c < 0x3a) |
| 48 | return 100 * ((unsigned char)c - 0x30); |
| 49 | else if (c < 0x41) |
| 50 | return 0; |
| 51 | return 100 * ((unsigned char)c - 0x37); |
| 52 | } |
| 53 | |
| 54 | struct CDRStltRecord |
| 55 | { |
| 56 | CDRStltRecord() |
| 57 | : parentId(0), fillId(0), outlId(0), fontRecId(0), alignId(0), |
| 58 | intervalId(0), set5Id(0), set11Id(0), tabId(0), |
| 59 | bulletId(0), indentId(0), hyphenId(0), dropCapId(0) {} |
| 60 | unsigned parentId; |
| 61 | unsigned fillId; |
| 62 | unsigned outlId; |
| 63 | unsigned fontRecId; |
| 64 | unsigned alignId; |
| 65 | unsigned intervalId; |
| 66 | unsigned set5Id; |
| 67 | unsigned set11Id; |
| 68 | unsigned tabId; |
| 69 | unsigned bulletId; |
| 70 | unsigned indentId; |
| 71 | unsigned hyphenId; |
| 72 | unsigned dropCapId; |
| 73 | }; |
| 74 | |
| 75 | static void processNameForEncoding(librevenge::RVNGString &name, unsigned short &encoding) |
| 76 | { |
| 77 | namespace qi = boost::spirit::qi; |
| 78 | |
| 79 | qi::symbols<char, unsigned short> codepage; |
| 80 | codepage.add |
| 81 | ("EC ", 0xee) |
| 82 | ("cilliryC ", 0xcc) |
| 83 | ("ryC ", 0xcc) |
| 84 | ("RYC ", 0xcc) |
| 85 | ("citlaB ", 0xba) |
| 86 | ("keerG ", 0xa1) |
| 87 | ("ruT ", 0xa2) |
| 88 | ("RUT ", 0xa2) |
| 89 | ("werbeH ", 0xb1) |
| 90 | ("cibarA ", 0xb2) |
| 91 | ("iahT ", 0xde) |
| 92 | ; |
| 93 | |
| 94 | std::string fontName(name.cstr()); |
| 95 | unsigned short enc = encoding; |
| 96 | std::string revName; |
| 97 | if (qi::parse(fontName.rbegin(), fontName.rend(), codepage >> +qi::char_, enc, revName)) |
| 98 | { |
| 99 | encoding = enc; |
| 100 | name = std::string(revName.rbegin(), revName.rend()).c_str(); |
| 101 | } |
| 102 | else if (boost::starts_with(fontName, "GOST")) |
| 103 | { |
| 104 | encoding = 0xcc; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static int parseColourString(const char *colourString, libcdr::CDRColor &colour, double &opacity) |
| 109 | { |
| 110 | using namespace boost::spirit::qi; |
| 111 | |
| 112 | bool bRes = false; |
| 113 | |
| 114 | boost::optional<unsigned> colourModel, colourPalette; |
| 115 | std::vector<std::string> fallbackColours; |
| 116 | std::vector<unsigned> val; |
| 117 | |
| 118 | if (colourString) |
| 119 | { |
| 120 | symbols<char, unsigned> cmodel; |
| 121 | cmodel.add |
| 122 | ("CMYK", 2) |
| 123 | ("CMYK255", 3) |
| 124 | ("RGB255", 5) |
| 125 | ("HSB", 6) |
| 126 | ("HLS", 7) |
| 127 | ("GRAY255", 9) |
| 128 | ("YIQ255", 11) |
| 129 | ("LAB", 12) |
| 130 | ("PANTONEHX", 14) |
| 131 | ("LAB255", 18) |
| 132 | ("REGCOLOR", 20) |
| 133 | ("SPOT", 25) |
| 134 | ; |
| 135 | symbols<char, unsigned> cpalette; |
| 136 | cpalette.add |
| 137 | ("USER", 5) |
| 138 | ("FOCTONE", 8) |
| 139 | ("DUPONT", 9) |
| 140 | ("TOYO", 10) |
| 141 | ("DIC", 11) |
| 142 | ("PAN_HEX", 12) |
| 143 | ("HKS", 0x17) |
| 144 | ("HKS_K", 0x1a) |
| 145 | ("HKS_N", 0x1b) |
| 146 | ("HKS_Z", 0x1c) |
| 147 | ("HKS_E", 0x1d) |
| 148 | ; |
| 149 | auto it = colourString; |
| 150 | const auto end = it + std::strlen(it); |
| 151 | bRes = phrase_parse(it, end, |
| 152 | // Begin grammar |
| 153 | ( |
| 154 | (cmodel | omit[+iso8859_1::alnum]) >> lit(',') |
| 155 | >> (cpalette | omit[+iso8859_1::alnum]) >> lit(',') |
| 156 | >> *(uint_ >> lit(',')) |
| 157 | >> omit[(repeat(8)[iso8859_1::xdigit] >> '-' >> repeat(3)[repeat(4)[iso8859_1::xdigit] >> '-'] >> repeat(12)[iso8859_1::xdigit])] |
| 158 | >> -(lit(',') |
| 159 | >> -(lit("~,") >> omit[+(iso8859_1::char_ - lit(','))] >> lit(',') >> omit[uint_] >> lit(',') |
| 160 | >> repeat[+(iso8859_1::char_ - lit(",~,")) >> lit(",~,")]) |
| 161 | >> omit[*iso8859_1::char_]) |
| 162 | ), |
| 163 | // End grammar |
| 164 | iso8859_1::space, |
| 165 | colourModel, colourPalette, val, fallbackColours) |
| 166 | && it == end; |
| 167 | } |
| 168 | |
| 169 | if (!bRes) |
| 170 | { |
| 171 | CDR_DEBUG_MSG(("parseColourString --> spirit grammar failed with string: %s\n", colourString)); |
| 172 | return -1; |
| 173 | } |
| 174 | CDR_DEBUG_MSG(("parseColourString --> colourModel %u, colourPalette %u, val size %lu, num fallback %lu\n", |
| 175 | colourModel ? colourModel.get() : 0, colourPalette ? colourPalette.get() : 0, val.size(), fallbackColours.size())); |
| 176 | |
| 177 | // If fallback colours exist, use the first of them, since we are more likely |
| 178 | // to get them right then the paletted spot colours |
| 179 | if (!fallbackColours.empty()) |
| 180 | return parseColourString(fallbackColours.begin()->c_str(), colour, opacity); |
| 181 | |
| 182 | if (colourModel) |
| 183 | colour.m_colorModel = (unsigned short)(get(colourModel)); |
| 184 | |
| 185 | if (colourPalette) |
| 186 | colour.m_colorPalette = (unsigned short)(get(colourPalette)); |
| 187 | |
| 188 | if (val.size() >= 5) |
| 189 | { |
| 190 | colour.m_colorValue = val[0] | (val[1] << 8) | (val[2] << 16) | (val[3] << 24); |
| 191 | opacity = (double)val[4] / 100.0; |
| 192 | } |
| 193 | else if (val.size() >= 4) |
| 194 | { |
| 195 | if (colour.m_colorModel == 5) |
| 196 | colour.m_colorValue = val[2] | (val[1] << 8) | (val[0] << 16); |
| 197 | else if (colour.m_colorModel == 6 || colour.m_colorModel == 7) |
| 198 | colour.m_colorValue = val[0] | (val[1] << 16) | (val[2] << 24); |
| 199 | else |
| 200 | colour.m_colorValue = val[0] | (val[1] << 8) | (val[2] << 16); |
| 201 | opacity = (double)val[3] / 100.0; |
| 202 | } |
| 203 | else if (val.size() >= 2) |
| 204 | { |
| 205 | if (colour.m_colorModel == 25 || colour.m_colorModel == 14) |
| 206 | colour.m_colorValue = (val[1] << 16) | val[0]; |
| 207 | else |
| 208 | { |
| 209 | colour.m_colorValue = val[0]; |
| 210 | opacity = (double)val[1] / 100.0; |
| 211 | } |
| 212 | } |
| 213 | else if (val.size() >= 1) |
| 214 | { |
| 215 | colour.m_colorValue = val[0]; |
| 216 | opacity = 1.0; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | CDR_DEBUG_MSG(("parseColourString --> colourModel %u, colorValue 0x%.8x, return 0\n", colour.m_colorModel, colour.m_colorValue)); |
| 221 | return 0; |
| 222 | } |
| 223 | CDR_DEBUG_MSG(("parseColourString --> colourModel %u, colorValue 0x%.8x, return 1\n", colour.m_colorModel, colour.m_colorValue)); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | void normalizeAngle(double &angle) |
| 228 | { |
| 229 | angle = std::fmod(angle, 2*M_PI); |
| 230 | if (angle < 0) |
| 231 | angle += 2*M_PI; |
| 232 | } |
| 233 | |
| 234 | } // anonymous namespace |
| 235 | |
| 236 | libcdr::CDRParser::CDRParser(const std::vector<std::unique_ptr<librevenge::RVNGInputStream>> &externalStreams, libcdr::CDRCollector *collector) |
| 237 | : CommonParser(collector), m_externalStreams(externalStreams), |
| 238 | m_fonts(), m_fillStyles(), m_lineStyles(), m_arrows(), m_version(0), m_waldoOutlId(0), m_waldoFillId(0) {} |
| 239 | |
| 240 | libcdr::CDRParser::~CDRParser() |
| 241 | { |
| 242 | m_collector->collectLevel(0); |
| 243 | } |
| 244 | |
| 245 | bool libcdr::CDRParser::parseWaldo(librevenge::RVNGInputStream *input) |
| 246 | { |
| 247 | try |
| 248 | { |
| 249 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 250 | unsigned short magic = readU16(input); |
| 251 | if (magic != 0x4c57) |
| 252 | return false; |
| 253 | m_version = 200; |
| 254 | m_precision = libcdr::PRECISION_16BIT; |
| 255 | if ('e' >= readU8(input)) |
| 256 | m_version = 100; |
| 257 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 258 | std::vector<unsigned> offsets; |
| 259 | unsigned i = 0; |
| 260 | for (i = 0; i < 8; ++i) |
| 261 | offsets.push_back(readU32(input)); |
| 262 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 263 | for (i = 0; i < 10; i++) |
| 264 | offsets.push_back(readU32(input)); |
| 265 | input->seek(offsets[0], librevenge::RVNG_SEEK_SET); |
| 266 | CDR_DEBUG_MSG(("CDRParser::parseWaldo, Mcfg offset 0x%x\n", (unsigned)input->tell())); |
| 267 | readMcfg(input, 275); |
| 268 | std::vector<WaldoRecordInfo> records; |
| 269 | std::map<unsigned, WaldoRecordInfo> records2; |
| 270 | std::map<unsigned, WaldoRecordInfo> records3; |
| 271 | std::map<unsigned, WaldoRecordInfo> records4; |
| 272 | std::map<unsigned, WaldoRecordInfo> records6; |
| 273 | std::map<unsigned, WaldoRecordInfo> records8; |
| 274 | std::map<unsigned, WaldoRecordInfo> records7; |
| 275 | std::map<unsigned, WaldoRecordInfo> recordsOther; |
| 276 | if (offsets[3]) |
| 277 | { |
| 278 | input->seek(offsets[3], librevenge::RVNG_SEEK_SET); |
| 279 | if (!gatherWaldoInformation(input, records, records2, records3, records4, records6, records7, records8, recordsOther)) |
| 280 | return false; |
| 281 | } |
| 282 | if (offsets[5]) |
| 283 | { |
| 284 | input->seek(offsets[5], librevenge::RVNG_SEEK_SET); |
| 285 | gatherWaldoInformation(input, records, records2, records3, records4, records6, records7, records8, recordsOther); |
| 286 | } |
| 287 | if (offsets[11]) |
| 288 | { |
| 289 | input->seek(offsets[11], librevenge::RVNG_SEEK_SET); |
| 290 | gatherWaldoInformation(input, records, records2, records3, records4, records6, records7, records8, recordsOther); |
| 291 | } |
| 292 | std::map<unsigned, WaldoRecordType1> records1; |
| 293 | for (auto &record : records) |
| 294 | { |
| 295 | input->seek(record.offset, librevenge::RVNG_SEEK_SET); |
| 296 | unsigned length = readU32(input); |
| 297 | if (length != 0x18) |
| 298 | { |
| 299 | CDR_DEBUG_MSG(("Throwing GenericException\n")); |
| 300 | throw GenericException(); |
| 301 | } |
| 302 | unsigned short next = readU16(input); |
| 303 | unsigned short previous = readU16(input); |
| 304 | unsigned short child = readU16(input); |
| 305 | unsigned short parent = readU16(input); |
| 306 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 307 | unsigned short moreDataID = readU16(input); |
| 308 | double x0 = readCoordinate(input); |
| 309 | double y0 = readCoordinate(input); |
| 310 | double x1 = readCoordinate(input); |
| 311 | double y1 = readCoordinate(input); |
| 312 | unsigned short flags = readU16(input); |
| 313 | CDRTransform trafo; |
| 314 | if (moreDataID) |
| 315 | { |
| 316 | std::map<unsigned, WaldoRecordInfo>::const_iterator iter7 = records7.find(moreDataID); |
| 317 | if (iter7 != records7.end()) |
| 318 | input->seek(iter7->second.offset, librevenge::RVNG_SEEK_SET); |
| 319 | input->seek(0x26, librevenge::RVNG_SEEK_CUR); |
| 320 | double v0 = readFixedPoint(input); |
| 321 | double v1 = readFixedPoint(input); |
| 322 | double v2 = readFixedPoint(input) / 1000.0; |
| 323 | double v3 = readFixedPoint(input); |
| 324 | double v4 = readFixedPoint(input); |
| 325 | double v5 = readFixedPoint(input) / 1000.0; |
| 326 | trafo = CDRTransform(v0, v1, v2, v3, v4, v5); |
| 327 | } |
| 328 | records1[record.id] = WaldoRecordType1(record.id, next, previous, child, parent, flags, x0, y0, x1, y1, trafo); |
| 329 | } |
| 330 | std::map<unsigned, WaldoRecordInfo>::const_iterator iter; |
| 331 | for (iter = records3.begin(); iter != records3.end(); ++iter) |
| 332 | readWaldoRecord(input, iter->second); |
| 333 | for (iter = records6.begin(); iter != records6.end(); ++iter) |
| 334 | readWaldoRecord(input, iter->second); |
| 335 | for (iter = records8.begin(); iter != records8.end(); ++iter) |
| 336 | readWaldoRecord(input, iter->second); |
| 337 | for (iter = recordsOther.begin(); iter != recordsOther.end(); ++iter) |
| 338 | readWaldoRecord(input, iter->second); |
| 339 | if (!records1.empty() && !records2.empty()) |
| 340 | { |
| 341 | |
| 342 | auto iter1 = records1.find(1); |
| 343 | std::stack<WaldoRecordType1> waldoStack; |
| 344 | if (iter1 != records1.end()) |
| 345 | { |
| 346 | waldoStack.push(iter1->second); |
| 347 | m_collector->collectVect((unsigned)(waldoStack.size())); |
| 348 | parseWaldoStructure(input, waldoStack, records1, records2); |
| 349 | } |
| 350 | iter1 = records1.find(0); |
| 351 | if (iter1 == records1.end()) |
| 352 | return false; |
| 353 | waldoStack = std::stack<WaldoRecordType1>(); |
| 354 | waldoStack.push(iter1->second); |
| 355 | m_collector->collectPage((unsigned)(waldoStack.size())); |
| 356 | if (!parseWaldoStructure(input, waldoStack, records1, records2)) |
| 357 | return false; |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | catch (...) |
| 362 | { |
| 363 | return false; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | bool libcdr::CDRParser::gatherWaldoInformation(librevenge::RVNGInputStream *input, std::vector<WaldoRecordInfo> &records, std::map<unsigned, WaldoRecordInfo> &records2, |
| 368 | std::map<unsigned, WaldoRecordInfo> &records3, std::map<unsigned, WaldoRecordInfo> &records4, |
| 369 | std::map<unsigned, WaldoRecordInfo> &records6, std::map<unsigned, WaldoRecordInfo> &records7, |
| 370 | std::map<unsigned, WaldoRecordInfo> &records8, std::map<unsigned, WaldoRecordInfo> recordsOther) |
| 371 | { |
| 372 | try |
| 373 | { |
| 374 | unsigned short numRecords = readU16(input); |
| 375 | for (; numRecords > 0 && !input->isEnd(); --numRecords) |
| 376 | { |
| 377 | unsigned char recordType = readU8(input); |
| 378 | unsigned recordId = readU32(input); |
| 379 | unsigned recordOffset = readU32(input); |
| 380 | switch (recordType) |
| 381 | { |
| 382 | case 1: |
| 383 | records.push_back(WaldoRecordInfo(recordType, recordId, recordOffset)); |
| 384 | break; |
| 385 | case 2: |
| 386 | records2[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 387 | break; |
| 388 | case 3: |
| 389 | records3[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 390 | break; |
| 391 | case 4: |
| 392 | records4[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 393 | break; |
| 394 | case 6: |
| 395 | records6[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 396 | break; |
| 397 | case 7: |
| 398 | records7[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 399 | break; |
| 400 | case 8: |
| 401 | records8[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 402 | break; |
| 403 | default: |
| 404 | recordsOther[recordId] = WaldoRecordInfo(recordType, recordId, recordOffset); |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | return true; |
| 409 | } |
| 410 | catch (...) |
| 411 | { |
| 412 | CDR_DEBUG_MSG(("CDRParser::gatherWaldoInformation: something went wrong during information gathering\n")); |
| 413 | return false; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | |
| 418 | bool libcdr::CDRParser::parseWaldoStructure(librevenge::RVNGInputStream *input, std::stack<WaldoRecordType1> &waldoStack, |
| 419 | const std::map<unsigned, WaldoRecordType1> &records1, std::map<unsigned, WaldoRecordInfo> &records2) |
| 420 | { |
| 421 | std::set<unsigned> visited; |
| 422 | while (!waldoStack.empty() && visited.insert(waldoStack.top().m_id).second) |
| 423 | { |
| 424 | m_collector->collectBBox(waldoStack.top().m_x0, waldoStack.top().m_y0, waldoStack.top().m_x1, waldoStack.top().m_y1); |
| 425 | std::map<unsigned, WaldoRecordType1>::const_iterator iter1; |
| 426 | if (waldoStack.top().m_flags & 0x01) |
| 427 | { |
| 428 | if (waldoStack.size() > 1) |
| 429 | { |
| 430 | m_collector->collectGroup((unsigned)(waldoStack.size())); |
| 431 | m_collector->collectSpnd(waldoStack.top().m_id); |
| 432 | CDRTransforms trafos; |
| 433 | trafos.append(waldoStack.top().m_trafo); |
| 434 | m_collector->collectTransform(trafos, true); |
| 435 | } |
| 436 | iter1 = records1.find(waldoStack.top().m_child); |
| 437 | if (iter1 == records1.end()) |
| 438 | return false; |
| 439 | waldoStack.push(iter1->second); |
| 440 | m_collector->collectLevel((unsigned)(waldoStack.size())); |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | if (waldoStack.size() > 1) |
| 445 | m_collector->collectObject((unsigned)(waldoStack.size())); |
| 446 | std::map<unsigned, WaldoRecordInfo>::const_iterator iter2 = records2.find(waldoStack.top().m_child); |
| 447 | if (iter2 == records2.end()) |
| 448 | return false; |
| 449 | readWaldoRecord(input, iter2->second); |
| 450 | while (!waldoStack.empty() && !waldoStack.top().m_next) |
| 451 | waldoStack.pop(); |
| 452 | m_collector->collectLevel((unsigned)(waldoStack.size())); |
| 453 | if (waldoStack.empty()) |
| 454 | return true; |
| 455 | iter1 = records1.find(waldoStack.top().m_next); |
| 456 | if (iter1 == records1.end()) |
| 457 | return false; |
| 458 | waldoStack.top() = iter1->second; |
| 459 | } |
| 460 | } |
| 461 | return waldoStack.empty(); |
| 462 | } |
| 463 | |
| 464 | void libcdr::CDRParser::readWaldoRecord(librevenge::RVNGInputStream *input, const WaldoRecordInfo &info) |
| 465 | { |
| 466 | CDR_DEBUG_MSG(("CDRParser::readWaldoRecord, type %i, id %x, offset %x\n", info.type, info.id, info.offset)); |
| 467 | input->seek(info.offset, librevenge::RVNG_SEEK_SET); |
| 468 | switch (info.type) |
| 469 | { |
| 470 | case 2: |
| 471 | { |
| 472 | unsigned length = readU32(input); |
| 473 | readWaldoLoda(input, length); |
| 474 | } |
| 475 | break; |
| 476 | case 3: |
| 477 | { |
| 478 | unsigned length = readU32(input); |
| 479 | readWaldoBmp(input, length, info.id); |
| 480 | } |
| 481 | break; |
| 482 | case 6: |
| 483 | readWaldoBmpf(input, info.id); |
| 484 | break; |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void libcdr::CDRParser::readWaldoTrfd(librevenge::RVNGInputStream *input) |
| 491 | { |
| 492 | if (m_version >= 400) |
| 493 | return; |
| 494 | double v0 = 0.0; |
| 495 | double v1 = 0.0; |
| 496 | double x0 = 0.0; |
| 497 | double v3 = 0.0; |
| 498 | double v4 = 0.0; |
| 499 | double y0 = 0.0; |
| 500 | if (m_version >= 300) |
| 501 | { |
| 502 | long startPosition = input->tell(); |
| 503 | input->seek(0x0a, librevenge::RVNG_SEEK_CUR); |
| 504 | unsigned offset = readUnsigned(input); |
| 505 | input->seek(startPosition+offset, librevenge::RVNG_SEEK_SET); |
| 506 | v0 = readFixedPoint(input); |
| 507 | v1 = readFixedPoint(input); |
| 508 | x0 = (double)readS32(input) / 1000.0; |
| 509 | v3 = readFixedPoint(input); |
| 510 | v4 = readFixedPoint(input); |
| 511 | y0 = (double)readS32(input) / 1000.0; |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | x0 = readCoordinate(input); |
| 516 | y0 = readCoordinate(input); |
| 517 | v0 = readFixedPoint(input); |
| 518 | v1 = readFixedPoint(input); |
| 519 | x0 += readFixedPoint(input) / 1000.0; |
| 520 | v3 = readFixedPoint(input); |
| 521 | v4 = readFixedPoint(input); |
| 522 | y0 += readFixedPoint(input) / 1000.0; |
| 523 | } |
| 524 | CDR_DEBUG_MSG(("CDRParser::readWaldoTrfd %f %f %f %f %f %f %u\n", v0, v1, x0, v3, v4, y0, m_version)); |
| 525 | CDRTransforms trafos; |
| 526 | trafos.append(v0, v1, x0, v3, v4, y0); |
| 527 | m_collector->collectTransform(trafos, m_version < 400); |
| 528 | } |
| 529 | |
| 530 | void libcdr::CDRParser::readWaldoLoda(librevenge::RVNGInputStream *input, unsigned length) |
| 531 | { |
| 532 | if (m_version >= 300) |
| 533 | return; |
| 534 | long startPosition = input->tell(); |
| 535 | readWaldoTrfd(input); |
| 536 | unsigned chunkType = readU8(input); |
| 537 | unsigned shapeOffset = readU16(input); |
| 538 | unsigned outlOffset = readU16(input); |
| 539 | unsigned fillOffset = readU16(input); |
| 540 | if (outlOffset) |
| 541 | { |
| 542 | input->seek(startPosition + outlOffset, librevenge::RVNG_SEEK_SET); |
| 543 | readWaldoOutl(input); |
| 544 | } |
| 545 | if (fillOffset) |
| 546 | { |
| 547 | input->seek(startPosition + fillOffset, librevenge::RVNG_SEEK_SET); |
| 548 | readWaldoFill(input); |
| 549 | } |
| 550 | if (shapeOffset) |
| 551 | { |
| 552 | input->seek(startPosition + shapeOffset, librevenge::RVNG_SEEK_SET); |
| 553 | if (chunkType == 0x00) // Rectangle |
| 554 | readRectangle(input); |
| 555 | else if (chunkType == 0x01) // Ellipse |
| 556 | readEllipse(input); |
| 557 | else if (chunkType == 0x02) // Line and curve |
| 558 | readLineAndCurve(input); |
| 559 | /* else if (chunkType == 0x03) // Text |
| 560 | readText(input); */ |
| 561 | else if (chunkType == 0x04) // Bitmap |
| 562 | readBitmap(input); |
| 563 | } |
| 564 | input->seek(startPosition + length, librevenge::RVNG_SEEK_SET); |
| 565 | } |
| 566 | |
| 567 | // Matches the sibling CMXParser::MAX_RECORD_DEPTH for the same kind of |
| 568 | // RIFF-style chunk nesting in the CMX parser. |
| 569 | constexpr unsigned MAX_RECORD_NESTING = 1 << 10; |
| 570 | |
| 571 | bool libcdr::CDRParser::parseRecords(librevenge::RVNGInputStream *input, const std::vector<unsigned> &blockLengths, unsigned level) |
| 572 | { |
| 573 | if (!input) |
| 574 | { |
| 575 | return false; |
| 576 | } |
| 577 | if (level > MAX_RECORD_NESTING) |
| 578 | return false; |
| 579 | m_collector->collectLevel(level); |
| 580 | while (!input->isEnd()) |
| 581 | { |
| 582 | if (!parseRecord(input, blockLengths, level)) |
| 583 | return false; |
| 584 | } |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | bool libcdr::CDRParser::parseRecord(librevenge::RVNGInputStream *input, const std::vector<unsigned> &blockLengths, unsigned level) |
| 589 | { |
| 590 | if (!input) |
| 591 | { |
| 592 | return false; |
| 593 | } |
| 594 | try |
| 595 | { |
| 596 | m_collector->collectLevel(level); |
| 597 | while (!input->isEnd() && readU8(input) == 0) |
| 598 | { |
| 599 | } |
| 600 | if (!input->isEnd()) |
| 601 | input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 602 | else |
| 603 | return true; |
| 604 | unsigned fourCC = readU32(input); |
| 605 | unsigned length = readU32(input); |
| 606 | if (blockLengths.size() > length) |
| 607 | length=blockLengths[length]; |
| 608 | unsigned long position = input->tell(); |
| 609 | unsigned listType(0); |
| 610 | if (fourCC == CDR_FOURCC_RIFF || fourCC == CDR_FOURCC_LIST) |
| 611 | { |
| 612 | listType = readU32(input); |
| 613 | if (listType == CDR_FOURCC_stlt && m_version >= 700) |
| 614 | fourCC = listType; |
| 615 | else |
| 616 | m_collector->collectOtherList(); |
| 617 | } |
| 618 | CDR_DEBUG_MSG(("Record: level %u %s, length: 0x%.8x (%u)\n", level, toFourCC(fourCC), length, length)); |
| 619 | |
| 620 | if (fourCC == CDR_FOURCC_RIFF || fourCC == CDR_FOURCC_LIST) |
| 621 | { |
| 622 | CDR_DEBUG_MSG(("CDR listType: %s\n", toFourCC(listType))); |
| 623 | unsigned cmprsize = length-4; |
| 624 | if (listType == CDR_FOURCC_cmpr) |
| 625 | { |
| 626 | cmprsize = readU32(input); |
| 627 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 628 | if (readU32(input) != CDR_FOURCC_CPng) |
| 629 | return false; |
| 630 | if (readU16(input) != 1) |
| 631 | return false; |
| 632 | if (readU16(input) != 4) |
| 633 | return false; |
| 634 | } |
| 635 | else if (listType == CDR_FOURCC_page) |
| 636 | m_collector->collectPage(level); |
| 637 | else if (listType == CDR_FOURCC_obj) |
| 638 | m_collector->collectObject(level); |
| 639 | else if (listType == CDR_FOURCC_grp || listType == CDR_FOURCC_lnkg) |
| 640 | m_collector->collectGroup(level); |
| 641 | else if ((listType & 0xffffff) == CDR_FOURCC_CDR || (listType & 0xffffff) == CDR_FOURCC_cdr) |
| 642 | { |
| 643 | m_version = getCDRVersion((unsigned char)((listType & 0xff000000) >> 24)); |
| 644 | if (m_version < 600) |
| 645 | m_precision = libcdr::PRECISION_16BIT; |
| 646 | else |
| 647 | m_precision = libcdr::PRECISION_32BIT; |
| 648 | } |
| 649 | else if (listType == CDR_FOURCC_vect || listType == CDR_FOURCC_clpt) |
| 650 | m_collector->collectVect(level); |
| 651 | |
| 652 | bool compressed = (listType == CDR_FOURCC_cmpr ? true : false); |
| 653 | CDRInternalStream tmpStream(input, cmprsize, compressed); |
| 654 | if (!compressed) |
| 655 | { |
| 656 | if (!parseRecords(&tmpStream, blockLengths, level+1)) |
| 657 | return false; |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | const long here = input->tell(); |
| 662 | if (here < 0 || static_cast<unsigned long>(here) > length + position) |
| 663 | return false; |
| 664 | std::vector<unsigned> tmpBlockLengths; |
| 665 | unsigned long blocksLength = length + position - here; |
| 666 | CDRInternalStream tmpBlocksStream(input, blocksLength, compressed); |
| 667 | while (!tmpBlocksStream.isEnd()) |
| 668 | tmpBlockLengths.push_back(readU32(&tmpBlocksStream)); |
| 669 | if (!parseRecords(&tmpStream, tmpBlockLengths, level+1)) |
| 670 | return false; |
| 671 | } |
| 672 | } |
| 673 | else |
| 674 | readRecord(fourCC, length, input); |
| 675 | |
| 676 | input->seek(position + length, librevenge::RVNG_SEEK_SET); |
| 677 | return true; |
| 678 | } |
| 679 | catch (...) |
| 680 | { |
| 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void libcdr::CDRParser::readRecord(unsigned fourCC, unsigned length, librevenge::RVNGInputStream *input) |
| 686 | { |
| 687 | long recordStart = input->tell(); |
| 688 | switch (fourCC) |
| 689 | { |
| 690 | case CDR_FOURCC_DISP: |
| 691 | readDisp(input, length); |
| 692 | break; |
| 693 | case CDR_FOURCC_loda: |
| 694 | case CDR_FOURCC_lobj: |
| 695 | readLoda(input, length); |
| 696 | break; |
| 697 | case CDR_FOURCC_vrsn: |
| 698 | readVersion(input, length); |
| 699 | break; |
| 700 | case CDR_FOURCC_trfd: |
| 701 | readTrfd(input, length); |
| 702 | break; |
| 703 | case CDR_FOURCC_outl: |
| 704 | readOutl(input, length); |
| 705 | break; |
| 706 | case CDR_FOURCC_fild: |
| 707 | case CDR_FOURCC_fill: |
| 708 | readFild(input, length); |
| 709 | break; |
| 710 | case CDR_FOURCC_arrw: |
| 711 | readArrw(input, length); |
| 712 | break; |
| 713 | case CDR_FOURCC_flgs: |
| 714 | readFlags(input, length); |
| 715 | break; |
| 716 | case CDR_FOURCC_mcfg: |
| 717 | readMcfg(input, length); |
| 718 | break; |
| 719 | case CDR_FOURCC_bmp: |
| 720 | readBmp(input, length); |
| 721 | break; |
| 722 | case CDR_FOURCC_bmpf: |
| 723 | readBmpf(input, length); |
| 724 | break; |
| 725 | case CDR_FOURCC_ppdt: |
| 726 | readPpdt(input, length); |
| 727 | break; |
| 728 | case CDR_FOURCC_ftil: |
| 729 | readFtil(input, length); |
| 730 | break; |
| 731 | case CDR_FOURCC_iccd: |
| 732 | readIccd(input, length); |
| 733 | break; |
| 734 | case CDR_FOURCC_bbox: |
| 735 | readBBox(input, length); |
| 736 | break; |
| 737 | case CDR_FOURCC_spnd: |
| 738 | readSpnd(input, length); |
| 739 | break; |
| 740 | case CDR_FOURCC_uidr: |
| 741 | readUidr(input, length); |
| 742 | break; |
| 743 | case CDR_FOURCC_vpat: |
| 744 | readVpat(input, length); |
| 745 | break; |
| 746 | case CDR_FOURCC_font: |
| 747 | readFont(input, length); |
| 748 | break; |
| 749 | case CDR_FOURCC_stlt: |
| 750 | readStlt(input, length); |
| 751 | break; |
| 752 | case CDR_FOURCC_txsm: |
| 753 | readTxsm(input, length); |
| 754 | break; |
| 755 | case CDR_FOURCC_udta: |
| 756 | readUdta(input); |
| 757 | break; |
| 758 | case CDR_FOURCC_styd: |
| 759 | readStyd(input); |
| 760 | break; |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | input->seek(recordStart + length, librevenge::RVNG_SEEK_CUR); |
| 765 | } |
| 766 | |
| 767 | double libcdr::CDRParser::readRectCoord(librevenge::RVNGInputStream *input) |
| 768 | { |
| 769 | if (m_version < 1500) |
| 770 | return readCoordinate(input); |
| 771 | return readDouble(input) / 254000.0; |
| 772 | } |
| 773 | |
| 774 | libcdr::CDRColor libcdr::CDRParser::readColor(librevenge::RVNGInputStream *input) |
| 775 | { |
| 776 | libcdr::CDRColor tmpColor; |
| 777 | |
| 778 | if (m_version >= 500) |
| 779 | { |
| 780 | tmpColor.m_colorModel = readU16(input); |
| 781 | if (tmpColor.m_colorModel == 0x01 && m_version >= 1300) |
| 782 | tmpColor.m_colorModel = 0x19; |
| 783 | /* A bug in CorelDraw x7 creates a corrupted file. If we meet colorModel of 0x1e, |
| 784 | * it is actually colorModel 0x19 and paletteId 0x1e */ |
| 785 | if (tmpColor.m_colorModel == 0x1e) |
| 786 | { |
| 787 | tmpColor.m_colorModel = 0x19; |
| 788 | tmpColor.m_colorPalette = 0x1e; |
| 789 | } |
| 790 | else |
| 791 | { |
| 792 | tmpColor.m_colorPalette = readU16(input); |
| 793 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 794 | } |
| 795 | tmpColor.m_colorValue = readU32(input); |
| 796 | |
| 797 | } |
| 798 | else if (m_version >= 400) |
| 799 | { |
| 800 | tmpColor.m_colorModel = readU16(input); |
| 801 | unsigned short c = readU16(input); |
| 802 | unsigned short m = readU16(input); |
| 803 | unsigned short y = readU16(input); |
| 804 | unsigned short k = readU16(input); |
| 805 | tmpColor.m_colorValue = (k & 0xff); |
| 806 | tmpColor.m_colorValue <<= 8; |
| 807 | tmpColor.m_colorValue |= (y & 0xff); |
| 808 | tmpColor.m_colorValue <<= 8; |
| 809 | tmpColor.m_colorValue |= (m & 0xff); |
| 810 | tmpColor.m_colorValue <<= 8; |
| 811 | tmpColor.m_colorValue |= (c & 0xff); |
| 812 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | tmpColor.m_colorModel = readU8(input); |
| 817 | tmpColor.m_colorValue = readU32(input); |
| 818 | } |
| 819 | |
| 820 | _resolveColorPalette(tmpColor); |
| 821 | |
| 822 | return tmpColor; |
| 823 | } |
| 824 | |
| 825 | void libcdr::CDRParser::readRectangle(librevenge::RVNGInputStream *input) |
| 826 | { |
| 827 | double x0 = readRectCoord(input); |
| 828 | double y0 = readRectCoord(input); |
| 829 | double r3 = 0.0; |
| 830 | double r2 = 0.0; |
| 831 | double r1 = 0.0; |
| 832 | double r0 = 0.0; |
| 833 | unsigned int corner_type = 0; |
| 834 | double scaleX = 1.0; |
| 835 | double scaleY = 1.0; |
| 836 | |
| 837 | if (m_version < 1500) |
| 838 | { |
| 839 | r3 = readRectCoord(input); |
| 840 | r2 = m_version < 900 ? r3 : readRectCoord(input); |
| 841 | r1 = m_version < 900 ? r3 : readRectCoord(input); |
| 842 | r0 = m_version < 900 ? r3 : readRectCoord(input); |
| 843 | } |
| 844 | else |
| 845 | { |
| 846 | double scale = readDouble(input); |
| 847 | if (!CDR_ALMOST_ZERO(scale)) |
| 848 | scaleX = scale; |
| 849 | scale = readDouble(input); |
| 850 | if (!CDR_ALMOST_ZERO(scale)) |
| 851 | scaleY = scale; |
| 852 | unsigned int scale_with = readU8(input); |
| 853 | input->seek(7, librevenge::RVNG_SEEK_CUR); |
| 854 | if (scale_with == 0) |
| 855 | { |
| 856 | r3 = readDouble(input); |
| 857 | corner_type = readU8(input); |
| 858 | input->seek(15, librevenge::RVNG_SEEK_CUR); |
| 859 | r2 = readDouble(input); |
| 860 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 861 | r1 = readDouble(input); |
| 862 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 863 | r0 = readDouble(input); |
| 864 | |
| 865 | double width = fabs(x0*scaleX / 2.0); |
| 866 | double height = fabs(y0*scaleY / 2.0); |
| 867 | r3 *= width < height ? width : height; |
| 868 | r2 *= width < height ? width : height; |
| 869 | r1 *= width < height ? width : height; |
| 870 | r0 *= width < height ? width : height; |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | r3 = readRectCoord(input); |
| 875 | corner_type = readU8(input); |
| 876 | input->seek(15, librevenge::RVNG_SEEK_CUR); |
| 877 | r2 = readRectCoord(input); |
| 878 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 879 | r1 = readRectCoord(input); |
| 880 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 881 | r0 = readRectCoord(input); |
| 882 | } |
| 883 | } |
| 884 | CDRPath path; |
| 885 | if (r0 > 0.0) |
| 886 | path.appendMoveTo(0.0, -r0/scaleY); |
| 887 | else |
| 888 | path.appendMoveTo(0.0, 0.0); |
| 889 | if (r1 > 0.0) |
| 890 | { |
| 891 | path.appendLineTo(0.0, y0+r1/scaleY); |
| 892 | if (corner_type == 0) |
| 893 | path.appendQuadraticBezierTo(0.0, y0, r1/scaleX, y0); |
| 894 | else if (corner_type == 1) |
| 895 | path.appendQuadraticBezierTo(r1/scaleX, y0+r1/scaleY, r1/scaleX, y0); |
| 896 | else if (corner_type == 2) |
| 897 | path.appendLineTo(r1/scaleX, y0); |
| 898 | } |
| 899 | else |
| 900 | path.appendLineTo(0.0, y0); |
| 901 | if (r2 > 0.0) |
| 902 | { |
| 903 | path.appendLineTo(x0-r2/scaleX, y0); |
| 904 | if (corner_type == 0) |
| 905 | path.appendQuadraticBezierTo(x0, y0, x0, y0+r2/scaleY); |
| 906 | else if (corner_type == 1) |
| 907 | path.appendQuadraticBezierTo(x0-r2/scaleX, y0+r2/scaleY, x0, y0+r2/scaleY); |
| 908 | else if (corner_type == 2) |
| 909 | path.appendLineTo(x0, y0+r2/scaleY); |
| 910 | } |
| 911 | else |
| 912 | path.appendLineTo(x0, y0); |
| 913 | if (r3 > 0.0) |
| 914 | { |
| 915 | path.appendLineTo(x0, -r3/scaleY); |
| 916 | if (corner_type == 0) |
| 917 | path.appendQuadraticBezierTo(x0, 0.0, x0-r3/scaleX, 0.0); |
| 918 | else if (corner_type == 1) |
| 919 | path.appendQuadraticBezierTo(x0-r3/scaleX, -r3/scaleY, x0-r3/scaleX, 0.0); |
| 920 | else if (corner_type == 2) |
| 921 | path.appendLineTo(x0-r3/scaleX, 0.0); |
| 922 | } |
| 923 | else |
| 924 | path.appendLineTo(x0, 0.0); |
| 925 | if (r0 > 0.0) |
| 926 | { |
| 927 | path.appendLineTo(r0/scaleX, 0.0); |
| 928 | if (corner_type == 0) |
| 929 | path.appendQuadraticBezierTo(0.0, 0.0, 0.0, -r0/scaleY); |
| 930 | else if (corner_type == 1) |
| 931 | path.appendQuadraticBezierTo(r0/scaleX, -r0/scaleY, 0.0, -r0/scaleY); |
| 932 | else if (corner_type == 2) |
| 933 | path.appendLineTo(0.0, -r0/scaleY); |
| 934 | } |
| 935 | else |
| 936 | path.appendLineTo(0.0, 0.0); |
| 937 | path.appendClosePath(); |
| 938 | m_collector->collectPath(path); |
| 939 | } |
| 940 | |
| 941 | void libcdr::CDRParser::readEllipse(librevenge::RVNGInputStream *input) |
| 942 | { |
| 943 | CDR_DEBUG_MSG(("CDRParser::readEllipse\n")); |
| 944 | |
| 945 | double x = readCoordinate(input); |
| 946 | double y = readCoordinate(input); |
| 947 | double angle1 = readAngle(input); |
| 948 | double angle2 = readAngle(input); |
| 949 | bool pie(0 != readUnsigned(input)); |
| 950 | |
| 951 | double cx = x/2.0; |
| 952 | double cy = y/2.0; |
| 953 | double rx = fabs(cx); |
| 954 | double ry = fabs(cy); |
| 955 | |
| 956 | normalizeAngle(angle1); |
| 957 | normalizeAngle(angle2); |
| 958 | |
| 959 | CDRPath path; |
| 960 | if (!CDR_ALMOST_EQUAL(angle1, angle2)) |
| 961 | { |
| 962 | if (angle2 < angle1) |
| 963 | angle2 += 2*M_PI; |
| 964 | double x0 = cx + rx*cos(angle1); |
| 965 | double y0 = cy - ry*sin(angle1); |
| 966 | |
| 967 | double x1 = cx + rx*cos(angle2); |
| 968 | double y1 = cy - ry*sin(angle2); |
| 969 | |
| 970 | bool largeArc = (angle2 - angle1 > M_PI); |
| 971 | |
| 972 | path.appendMoveTo(x0, y0); |
| 973 | path.appendArcTo(rx, ry, 0.0, largeArc, false, x1, y1); |
| 974 | if (pie) |
| 975 | { |
| 976 | path.appendLineTo(cx, cy); |
| 977 | path.appendLineTo(x0, y0); |
| 978 | path.appendClosePath(); |
| 979 | } |
| 980 | } |
| 981 | else |
| 982 | { |
| 983 | angle2 += M_PI/2.0; |
| 984 | double x0 = cx + rx*cos(angle1); |
| 985 | double y0 = cy - ry*sin(angle1); |
| 986 | |
| 987 | double x1 = cx + rx*cos(angle2); |
| 988 | double y1 = cy - ry*sin(angle2); |
| 989 | |
| 990 | path.appendMoveTo(x0, y0); |
| 991 | path.appendArcTo(rx, ry, 0.0, false, false, x1, y1); |
| 992 | path.appendArcTo(rx, ry, 0.0, true, false, x0, y0); |
| 993 | path.appendClosePath(); |
| 994 | } |
| 995 | m_collector->collectPath(path); |
| 996 | } |
| 997 | |
| 998 | void libcdr::CDRParser::readDisp(librevenge::RVNGInputStream *input, unsigned length) |
| 999 | { |
| 1000 | if (!_redirectX6Chunk(&input, length)) |
| 1001 | throw GenericException(); |
| 1002 | #if DUMP_PREVIEW_IMAGE |
| 1003 | librevenge::RVNGBinaryData previewImage; |
| 1004 | previewImage.append(0x42); |
| 1005 | previewImage.append(0x4d); |
| 1006 | |
| 1007 | previewImage.append((unsigned char)((length+8) & 0x000000ff)); |
| 1008 | previewImage.append((unsigned char)(((length+8) & 0x0000ff00) >> 8)); |
| 1009 | previewImage.append((unsigned char)(((length+8) & 0x00ff0000) >> 16)); |
| 1010 | previewImage.append((unsigned char)(((length+8) & 0xff000000) >> 24)); |
| 1011 | |
| 1012 | previewImage.append(0x00); |
| 1013 | previewImage.append(0x00); |
| 1014 | previewImage.append(0x00); |
| 1015 | previewImage.append(0x00); |
| 1016 | |
| 1017 | long startPosition = input->tell(); |
| 1018 | input->seek(0x18, librevenge::RVNG_SEEK_CUR); |
| 1019 | int lengthX = length + 10 - readU32(input); |
| 1020 | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
| 1021 | |
| 1022 | previewImage.append((unsigned char)((lengthX) & 0x000000ff)); |
| 1023 | previewImage.append((unsigned char)(((lengthX) & 0x0000ff00) >> 8)); |
| 1024 | previewImage.append((unsigned char)(((lengthX) & 0x00ff0000) >> 16)); |
| 1025 | previewImage.append((unsigned char)(((lengthX) & 0xff000000) >> 24)); |
| 1026 | |
| 1027 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1028 | for (unsigned i = 4; i<length; i++) |
| 1029 | previewImage.append(readU8(input)); |
| 1030 | FILE *f = fopen("previewImage.bmp", "wb"); |
| 1031 | if (f) |
| 1032 | { |
| 1033 | const unsigned char *tmpBuffer = previewImage.getDataBuffer(); |
| 1034 | for (unsigned long k = 0; k < previewImage.size(); k++) |
| 1035 | fprintf(f, "%c",tmpBuffer[k]); |
| 1036 | fclose(f); |
| 1037 | } |
| 1038 | #endif |
| 1039 | } |
| 1040 | |
| 1041 | void libcdr::CDRParser::readLineAndCurve(librevenge::RVNGInputStream *input) |
| 1042 | { |
| 1043 | CDR_DEBUG_MSG(("CDRParser::readLineAndCurve\n")); |
| 1044 | |
| 1045 | unsigned long pointNum = readU16(input); |
| 1046 | const unsigned short pointSize = 2 * (m_precision == PRECISION_16BIT ? 2 : 4) + 1; |
| 1047 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1048 | if (pointNum > getRemainingLength(input) / pointSize) |
| 1049 | pointNum = getRemainingLength(input) / pointSize; |
| 1050 | std::vector<std::pair<double, double> > points; |
| 1051 | std::vector<unsigned char> pointTypes; |
| 1052 | points.reserve(pointNum); |
| 1053 | pointTypes.reserve(pointNum); |
| 1054 | for (unsigned long j=0; j<pointNum; j++) |
| 1055 | { |
| 1056 | std::pair<double, double> point; |
| 1057 | point.first = (double)readCoordinate(input); |
| 1058 | point.second = (double)readCoordinate(input); |
| 1059 | points.push_back(point); |
| 1060 | } |
| 1061 | for (unsigned long k=0; k<pointNum; k++) |
| 1062 | pointTypes.push_back(readU8(input)); |
| 1063 | outputPath(points, pointTypes); |
| 1064 | } |
| 1065 | |
| 1066 | void libcdr::CDRParser::readPath(librevenge::RVNGInputStream *input) |
| 1067 | { |
| 1068 | CDR_DEBUG_MSG(("CDRParser::readPath\n")); |
| 1069 | |
| 1070 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1071 | unsigned long pointNum = readU16(input)+readU16(input); |
| 1072 | const unsigned short pointSize = 2 * (m_precision == PRECISION_16BIT ? 2 : 4) + 1; |
| 1073 | const unsigned long maxLength = getRemainingLength(input); |
| 1074 | if (maxLength < 16) |
| 1075 | pointNum = 0; |
| 1076 | else if (pointNum > (maxLength - 16) / pointSize) |
| 1077 | pointNum = (maxLength - 16) / pointSize; |
| 1078 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 1079 | std::vector<std::pair<double, double> > points; |
| 1080 | std::vector<unsigned char> pointTypes; |
| 1081 | points.reserve(pointNum); |
| 1082 | pointTypes.reserve(pointNum); |
| 1083 | for (unsigned long j=0; j<pointNum; j++) |
| 1084 | { |
| 1085 | std::pair<double, double> point; |
| 1086 | point.first = (double)readCoordinate(input); |
| 1087 | point.second = (double)readCoordinate(input); |
| 1088 | points.push_back(point); |
| 1089 | } |
| 1090 | for (unsigned long k=0; k<pointNum; k++) |
| 1091 | pointTypes.push_back(readU8(input)); |
| 1092 | outputPath(points, pointTypes); |
| 1093 | } |
| 1094 | |
| 1095 | void libcdr::CDRParser::readArrw(librevenge::RVNGInputStream *input, unsigned length) |
| 1096 | { |
| 1097 | CDR_DEBUG_MSG(("CDRParser::readArrw\n")); |
| 1098 | if (m_version < 600) |
| 1099 | return; |
| 1100 | if (!_redirectX6Chunk(&input, length)) |
| 1101 | throw GenericException(); |
| 1102 | unsigned arrowId = readU32(input); |
| 1103 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1104 | unsigned long pointNum = readU16(input); |
| 1105 | const unsigned short pointSize = 2 * (m_precision == PRECISION_16BIT ? 2 : 4) + 1; |
| 1106 | const unsigned long maxLength = getRemainingLength(input); |
| 1107 | if (maxLength < 5) |
| 1108 | pointNum = 0; |
| 1109 | else if (pointNum > (maxLength - 5) / pointSize) |
| 1110 | pointNum = (maxLength - 5) / pointSize; |
| 1111 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1112 | std::vector<unsigned char> pointTypes; |
| 1113 | pointTypes.reserve(pointSize); |
| 1114 | for (unsigned long k=0; k<pointNum; k++) |
| 1115 | pointTypes.push_back(readU8(input)); |
| 1116 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 1117 | std::vector<std::pair<double, double> > points; |
| 1118 | points.reserve(pointSize); |
| 1119 | for (unsigned long j=0; j<pointNum; j++) |
| 1120 | { |
| 1121 | std::pair<double, double> point; |
| 1122 | point.second = (double)readCoordinate(input); |
| 1123 | point.first = (double)readCoordinate(input); |
| 1124 | points.push_back(point); |
| 1125 | } |
| 1126 | CDRPath path; |
| 1127 | processPath(points, pointTypes, path); |
| 1128 | m_arrows[arrowId] = path; |
| 1129 | } |
| 1130 | |
| 1131 | void libcdr::CDRParser::readBitmap(librevenge::RVNGInputStream *input) |
| 1132 | { |
| 1133 | CDR_DEBUG_MSG(("CDRParser::readBitmap\n")); |
| 1134 | |
| 1135 | double x1 = 0.0; |
| 1136 | double y1 = 0.0; |
| 1137 | double x2 = 0.0; |
| 1138 | double y2 = 0.0; |
| 1139 | unsigned imageId = 0; |
| 1140 | if (m_version < 600) |
| 1141 | { |
| 1142 | x1 = readCoordinate(input); |
| 1143 | y1 = readCoordinate(input); |
| 1144 | x2 = 0.0; |
| 1145 | y2 = 0.0; |
| 1146 | if (m_version < 400) |
| 1147 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1148 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1149 | imageId = readUnsigned(input); |
| 1150 | input->seek(20, librevenge::RVNG_SEEK_CUR); |
| 1151 | CDRPath path; |
| 1152 | path.appendMoveTo(x1, y1); |
| 1153 | path.appendLineTo(x1, y2); |
| 1154 | path.appendLineTo(x2, y2); |
| 1155 | path.appendLineTo(x2, y1); |
| 1156 | path.appendLineTo(x1, y1); |
| 1157 | m_collector->collectPath(path); |
| 1158 | } |
| 1159 | else |
| 1160 | { |
| 1161 | x1 = (double)readCoordinate(input); |
| 1162 | y1 = (double)readCoordinate(input); |
| 1163 | x2 = (double)readCoordinate(input); |
| 1164 | y2 = (double)readCoordinate(input); |
| 1165 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 1166 | |
| 1167 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 1168 | imageId = readUnsigned(input); |
| 1169 | if (m_version < 800) |
| 1170 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1171 | else if (m_version >= 800 && m_version < 900) |
| 1172 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 1173 | else |
| 1174 | input->seek(20, librevenge::RVNG_SEEK_CUR); |
| 1175 | |
| 1176 | unsigned long pointNum = readU16(input); |
| 1177 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1178 | const unsigned short pointSize = 2 * (m_precision == PRECISION_16BIT ? 2 : 4) + 1; |
| 1179 | if (pointNum > getRemainingLength(input) / pointSize) |
| 1180 | pointNum = getRemainingLength(input) / pointSize; |
| 1181 | std::vector<std::pair<double, double> > points; |
| 1182 | std::vector<unsigned char> pointTypes; |
| 1183 | points.reserve(pointNum); |
| 1184 | pointTypes.reserve(pointNum); |
| 1185 | for (unsigned long j=0; j<pointNum; j++) |
| 1186 | { |
| 1187 | std::pair<double, double> point; |
| 1188 | point.first = (double)readCoordinate(input); |
| 1189 | point.second = (double)readCoordinate(input); |
| 1190 | points.push_back(point); |
| 1191 | } |
| 1192 | for (unsigned long k=0; k<pointNum; k++) |
| 1193 | pointTypes.push_back(readU8(input)); |
| 1194 | outputPath(points, pointTypes); |
| 1195 | } |
| 1196 | m_collector->collectBitmap(imageId, x1, x2, y1, y2); |
| 1197 | } |
| 1198 | |
| 1199 | void libcdr::CDRParser::readWaldoOutl(librevenge::RVNGInputStream *input) |
| 1200 | { |
| 1201 | if (m_version >= 400) |
| 1202 | return; |
| 1203 | unsigned short lineType = readU8(input); |
| 1204 | lineType <<= 1; |
| 1205 | auto lineWidth = (double)readCoordinate(input); |
| 1206 | double stretch = (double)readU16(input) / 100.0; |
| 1207 | double angle = readAngle(input); |
| 1208 | libcdr::CDRColor color = readColor(input); |
| 1209 | input->seek(7, librevenge::RVNG_SEEK_CUR); |
| 1210 | unsigned short numDash = readU8(input); |
| 1211 | long fixPosition = input->tell(); |
| 1212 | std::vector<unsigned> dashArray; |
| 1213 | for (unsigned short i = 0; i < numDash; ++i) |
| 1214 | dashArray.push_back(readU8(input)); |
| 1215 | input->seek(fixPosition + 10, librevenge::RVNG_SEEK_SET); |
| 1216 | unsigned short joinType = readU16(input); |
| 1217 | unsigned short capsType = readU16(input); |
| 1218 | unsigned startMarkerId = readU32(input); |
| 1219 | std::map<unsigned, CDRPath>::const_iterator iter = m_arrows.find(startMarkerId); |
| 1220 | CDRPath startMarker; |
| 1221 | if (iter != m_arrows.end()) |
| 1222 | startMarker = iter->second; |
| 1223 | unsigned endMarkerId = readU32(input); |
| 1224 | iter = m_arrows.find(endMarkerId); |
| 1225 | CDRPath endMarker; |
| 1226 | if (iter != m_arrows.end()) |
| 1227 | endMarker = iter->second; |
| 1228 | m_collector->collectLineStyle(++m_waldoOutlId, CDRLineStyle(lineType, capsType, joinType, lineWidth, stretch, angle, color, dashArray, startMarker, endMarker)); |
| 1229 | m_collector->collectLineStyleId(m_waldoOutlId); |
| 1230 | } |
| 1231 | |
| 1232 | void libcdr::CDRParser::readWaldoFill(librevenge::RVNGInputStream *input) |
| 1233 | { |
| 1234 | if (m_version >= 400) |
| 1235 | return; |
| 1236 | unsigned short fillType = readU8(input); |
| 1237 | libcdr::CDRColor color1; |
| 1238 | libcdr::CDRColor color2; |
| 1239 | libcdr::CDRImageFill imageFill; |
| 1240 | libcdr::CDRGradient gradient; |
| 1241 | switch (fillType) |
| 1242 | { |
| 1243 | case 1: // Solid |
| 1244 | { |
| 1245 | color1 = readColor(input); |
| 1246 | } |
| 1247 | break; |
| 1248 | case 2: // Linear Gradient |
| 1249 | { |
| 1250 | gradient.m_type = 1; |
| 1251 | gradient.m_angle = readAngle(input); |
| 1252 | color1 = readColor(input); |
| 1253 | color2 = readColor(input); |
| 1254 | if (m_version >= 200) |
| 1255 | { |
| 1256 | input->seek(7, librevenge::RVNG_SEEK_CUR); |
| 1257 | gradient.m_edgeOffset = readS16(input); |
| 1258 | gradient.m_centerXOffset = readInteger(input); |
| 1259 | gradient.m_centerYOffset = readInteger(input); |
| 1260 | } |
| 1261 | libcdr::CDRGradientStop stop; |
| 1262 | stop.m_color = color1; |
| 1263 | stop.m_offset = 0.0; |
| 1264 | gradient.m_stops.push_back(stop); |
| 1265 | stop.m_color = color2; |
| 1266 | stop.m_offset = 1.0; |
| 1267 | gradient.m_stops.push_back(stop); |
| 1268 | } |
| 1269 | break; |
| 1270 | case 4: // Radial Gradient |
| 1271 | { |
| 1272 | gradient.m_type = 2; |
| 1273 | fillType = 2; |
| 1274 | gradient.m_angle = readAngle(input); |
| 1275 | color1 = readColor(input); |
| 1276 | color2 = readColor(input); |
| 1277 | if (m_version >= 200) |
| 1278 | { |
| 1279 | input->seek(7, librevenge::RVNG_SEEK_CUR); |
| 1280 | gradient.m_edgeOffset = readS16(input); |
| 1281 | gradient.m_centerXOffset = readInteger(input); |
| 1282 | gradient.m_centerYOffset = readInteger(input); |
| 1283 | } |
| 1284 | libcdr::CDRGradientStop stop; |
| 1285 | stop.m_color = color1; |
| 1286 | stop.m_offset = 0.0; |
| 1287 | gradient.m_stops.push_back(stop); |
| 1288 | stop.m_color = color2; |
| 1289 | stop.m_offset = 1.0; |
| 1290 | gradient.m_stops.push_back(stop); |
| 1291 | } |
| 1292 | break; |
| 1293 | case 7: // Pattern |
| 1294 | { |
| 1295 | unsigned patternId = m_version < 300 ? readU16(input) : readU32(input); |
| 1296 | double patternWidth = readCoordinate(input); |
| 1297 | double patternHeight = readCoordinate(input); |
| 1298 | double tileOffsetX = (double)readU16(input) / 100.0; |
| 1299 | double tileOffsetY = (double)readU16(input) / 100.0; |
| 1300 | double rcpOffset = (double)readU16(input) / 100.0; |
| 1301 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 1302 | color1 = readColor(input); |
| 1303 | color2 = readColor(input); |
| 1304 | imageFill = libcdr::CDRImageFill(patternId, patternWidth, patternHeight, false, tileOffsetX, tileOffsetY, rcpOffset, 0); |
| 1305 | } |
| 1306 | break; |
| 1307 | case 10: // Full color |
| 1308 | { |
| 1309 | unsigned patternId = readU16(input); |
| 1310 | double patternWidth = readCoordinate(input); |
| 1311 | double patternHeight = readCoordinate(input); |
| 1312 | double tileOffsetX = (double)readU16(input) / 100.0; |
| 1313 | double tileOffsetY = (double)readU16(input) / 100.0; |
| 1314 | double rcpOffset = (double)readU16(input) / 100.0; |
| 1315 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 1316 | imageFill = libcdr::CDRImageFill(patternId, patternWidth, patternHeight, false, tileOffsetX, tileOffsetY, rcpOffset, 0); |
| 1317 | } |
| 1318 | break; |
| 1319 | default: |
| 1320 | break; |
| 1321 | } |
| 1322 | m_collector->collectFillStyle(++m_waldoFillId, CDRFillStyle(fillType, color1, color2, gradient, imageFill)); |
| 1323 | m_collector->collectFillStyleId(m_waldoFillId); |
| 1324 | } |
| 1325 | |
| 1326 | void libcdr::CDRParser::readTrfd(librevenge::RVNGInputStream *input, unsigned length) |
| 1327 | { |
| 1328 | if (!_redirectX6Chunk(&input, length)) |
| 1329 | throw GenericException(); |
| 1330 | long startPosition = input->tell(); |
| 1331 | const unsigned long maxLength = getLength(input); |
| 1332 | if (startPosition >= long(maxLength)) |
| 1333 | return; |
| 1334 | if ((length > maxLength) || (long(maxLength - length) < startPosition)) |
| 1335 | length = unsigned(maxLength - static_cast<unsigned long>(startPosition)); // sanitize length |
| 1336 | unsigned chunkLength = readUnsigned(input); |
| 1337 | unsigned numOfArgs = readUnsigned(input); |
| 1338 | unsigned startOfArgs = readUnsigned(input); |
| 1339 | if (startOfArgs >= length) |
| 1340 | return; |
| 1341 | if (numOfArgs > (length - startOfArgs) / 4) // avoid extra big allocation in case of a broken file |
| 1342 | numOfArgs = (length - startOfArgs) / 4; |
| 1343 | std::vector<unsigned> argOffsets(numOfArgs, 0); |
| 1344 | size_t i = 0; |
| 1345 | input->seek(startPosition+startOfArgs, librevenge::RVNG_SEEK_SET); |
| 1346 | while (i<numOfArgs) |
| 1347 | argOffsets[i++] = readUnsigned(input); |
| 1348 | |
| 1349 | CDRTransforms trafos; |
| 1350 | for (i=0; i < argOffsets.size(); i++) |
| 1351 | { |
| 1352 | input->seek(startPosition+argOffsets[i], librevenge::RVNG_SEEK_SET); |
| 1353 | if (m_version >= 1300) |
| 1354 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1355 | unsigned short tmpType = readU16(input); |
| 1356 | if (tmpType == 0x08) // trafo |
| 1357 | { |
| 1358 | double v0 = 0.0; |
| 1359 | double v1 = 0.0; |
| 1360 | double x0 = 0.0; |
| 1361 | double v3 = 0.0; |
| 1362 | double v4 = 0.0; |
| 1363 | double y0 = 0.0; |
| 1364 | if (m_version >= 600) |
| 1365 | input->seek(6, librevenge::RVNG_SEEK_CUR); |
| 1366 | if (m_version >= 500) |
| 1367 | { |
| 1368 | v0 = readDouble(input); |
| 1369 | v1 = readDouble(input); |
| 1370 | x0 = readDouble(input) / (m_version < 600 ? 1000.0 : 254000.0); |
| 1371 | v3 = readDouble(input); |
| 1372 | v4 = readDouble(input); |
| 1373 | y0 = readDouble(input) / (m_version < 600 ? 1000.0 : 254000.0); |
| 1374 | } |
| 1375 | else |
| 1376 | { |
| 1377 | v0 = readFixedPoint(input); |
| 1378 | v1 = readFixedPoint(input); |
| 1379 | x0 = (double)readS32(input) / 1000.0; |
| 1380 | v3 = readFixedPoint(input); |
| 1381 | v4 = readFixedPoint(input); |
| 1382 | y0 = (double)readS32(input) / 1000.0; |
| 1383 | } |
| 1384 | trafos.append(v0, v1, x0, v3, v4, y0); |
| 1385 | } |
| 1386 | else if (tmpType == 0x10) |
| 1387 | { |
| 1388 | #if 0 |
| 1389 | input->seek(6, librevenge::RVNG_SEEK_CUR); |
| 1390 | unsigned short type = readU16(input); |
| 1391 | double x = readCoordinate(input); |
| 1392 | double y = readCoordinate(input); |
| 1393 | unsigned subType = readU32(input); |
| 1394 | if (!subType) |
| 1395 | { |
| 1396 | } |
| 1397 | else |
| 1398 | { |
| 1399 | if (subType&1) // Smooth |
| 1400 | { |
| 1401 | } |
| 1402 | if (subType&2) // Random |
| 1403 | { |
| 1404 | } |
| 1405 | if (subType&4) // Local |
| 1406 | { |
| 1407 | } |
| 1408 | } |
| 1409 | unsigned opt1 = readU32(input); |
| 1410 | unsigned opt2 = readU32(input); |
| 1411 | #endif |
| 1412 | } |
| 1413 | } |
| 1414 | if (!trafos.empty()) |
| 1415 | m_collector->collectTransform(trafos,m_version < 400); |
| 1416 | input->seek(startPosition+chunkLength, librevenge::RVNG_SEEK_SET); |
| 1417 | } |
| 1418 | |
| 1419 | void libcdr::CDRParser::readFild(librevenge::RVNGInputStream *input, unsigned length) |
| 1420 | { |
| 1421 | if (!_redirectX6Chunk(&input, length)) |
| 1422 | throw GenericException(); |
| 1423 | unsigned fillId = readU32(input); |
| 1424 | if (m_version >= 1300) |
| 1425 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1426 | unsigned short fillType = readU16(input); |
| 1427 | libcdr::CDRColor color1; |
| 1428 | libcdr::CDRColor color2; |
| 1429 | libcdr::CDRImageFill imageFill; |
| 1430 | libcdr::CDRGradient gradient; |
| 1431 | switch (fillType) |
| 1432 | { |
| 1433 | case 1: // Solid |
| 1434 | { |
| 1435 | if (m_version >= 1300) |
| 1436 | input->seek(13, librevenge::RVNG_SEEK_CUR); |
| 1437 | else |
| 1438 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1439 | color1 = readColor(input); |
| 1440 | } |
| 1441 | break; |
| 1442 | case 2: // Gradient |
| 1443 | { |
| 1444 | if (m_version >= 1300) |
| 1445 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1446 | else |
| 1447 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1448 | gradient.m_type = readU8(input); |
| 1449 | if (m_version >= 1300) |
| 1450 | { |
| 1451 | input->seek(17, librevenge::RVNG_SEEK_CUR); |
| 1452 | gradient.m_edgeOffset = readS16(input); |
| 1453 | } |
| 1454 | else if (m_version >= 600) |
| 1455 | { |
| 1456 | input->seek(19, librevenge::RVNG_SEEK_CUR); |
| 1457 | gradient.m_edgeOffset = readS32(input); |
| 1458 | } |
| 1459 | else |
| 1460 | { |
| 1461 | input->seek(11, librevenge::RVNG_SEEK_CUR); |
| 1462 | gradient.m_edgeOffset = readS16(input); |
| 1463 | } |
| 1464 | gradient.m_angle = readAngle(input); |
| 1465 | gradient.m_centerXOffset = readInteger(input); |
| 1466 | gradient.m_centerYOffset = readInteger(input); |
| 1467 | if (m_version >= 600) |
| 1468 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1469 | gradient.m_mode = (unsigned char)(readUnsigned(input) & 0xff); |
| 1470 | gradient.m_midPoint = (double)readU8(input) / 100.0; |
| 1471 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 1472 | auto numStops = (unsigned short)(readUnsigned(input) & 0xffff); |
| 1473 | if (m_version >= 1300) |
| 1474 | input->seek(3, librevenge::RVNG_SEEK_CUR); |
| 1475 | for (unsigned short i = 0; i < numStops; ++i) |
| 1476 | { |
| 1477 | libcdr::CDRGradientStop stop; |
| 1478 | stop.m_color = readColor(input); |
| 1479 | // tdf130914 tdf158268 Version 1400 is using 5 bytes seek for Gradients |
| 1480 | if (m_version >= 1500) |
| 1481 | input->seek(26, librevenge::RVNG_SEEK_CUR); |
| 1482 | else if (m_version >= 1300) |
| 1483 | input->seek(5, librevenge::RVNG_SEEK_CUR); |
| 1484 | stop.m_offset = (double)(readUnsigned(input) & 0xffff) / 100.0; |
| 1485 | if (m_version >= 1300) |
| 1486 | input->seek(3, librevenge::RVNG_SEEK_CUR); |
| 1487 | gradient.m_stops.push_back(stop); |
| 1488 | } |
| 1489 | } |
| 1490 | break; |
| 1491 | case 7: // Pattern |
| 1492 | { |
| 1493 | if (m_version >= 1300) |
| 1494 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1495 | else |
| 1496 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1497 | unsigned patternId = readU32(input); |
| 1498 | int tmpWidth = readInteger(input); |
| 1499 | int tmpHeight = readInteger(input); |
| 1500 | double tileOffsetX = 0.0; |
| 1501 | double tileOffsetY = 0.0; |
| 1502 | if (m_version < 900) |
| 1503 | { |
| 1504 | tileOffsetX = (double)readU16(input) / 100.0; |
| 1505 | tileOffsetY = (double)readU16(input) / 100.0; |
| 1506 | } |
| 1507 | else |
| 1508 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1509 | double rcpOffset = (double)readU16(input) / 100.0; |
| 1510 | unsigned char flags = readU8(input); |
| 1511 | double patternWidth = (double)tmpWidth / (m_version < 600 ? 1000.0 : 254000.0); |
| 1512 | double patternHeight = (double)tmpHeight / (m_version < 600 ? 1000.0 : 254000.0); |
| 1513 | bool isRelative = false; |
| 1514 | if ((flags & 0x04) && (m_version < 900)) |
| 1515 | { |
| 1516 | isRelative = true; |
| 1517 | patternWidth = (double)tmpWidth / 100.0; |
| 1518 | patternHeight = (double)tmpHeight / 100.0; |
| 1519 | } |
| 1520 | if (m_version >= 1300) |
| 1521 | input->seek(6, librevenge::RVNG_SEEK_CUR); |
| 1522 | else |
| 1523 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 1524 | color1 = readColor(input); |
| 1525 | if (m_version >= 1600) |
| 1526 | input->seek(31, librevenge::RVNG_SEEK_CUR); |
| 1527 | else if (m_version >= 1300) |
| 1528 | input->seek(10, librevenge::RVNG_SEEK_CUR); |
| 1529 | color2 = readColor(input); |
| 1530 | imageFill = libcdr::CDRImageFill(patternId, patternWidth, patternHeight, isRelative, tileOffsetX, tileOffsetY, rcpOffset, flags); |
| 1531 | } |
| 1532 | break; |
| 1533 | case 9: // bitmap |
| 1534 | { |
| 1535 | if (m_version < 600) |
| 1536 | fillType = 10; |
| 1537 | if (m_version >= 1300) |
| 1538 | { |
| 1539 | _skipX3Optional(input); |
| 1540 | input->seek(-4, librevenge::RVNG_SEEK_CUR); |
| 1541 | } |
| 1542 | else |
| 1543 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1544 | unsigned patternId = readUnsigned(input); |
| 1545 | int tmpWidth = readUnsigned(input); |
| 1546 | int tmpHeight = readUnsigned(input); |
| 1547 | double tileOffsetX = 0.0; |
| 1548 | double tileOffsetY = 0.0; |
| 1549 | if (m_version < 900) |
| 1550 | { |
| 1551 | tileOffsetX = (double)readU16(input) / 100.0; |
| 1552 | tileOffsetY = (double)readU16(input) / 100.0; |
| 1553 | } |
| 1554 | else |
| 1555 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1556 | double rcpOffset = (double)readU16(input) / 100.0; |
| 1557 | unsigned char flags = readU8(input); |
| 1558 | double patternWidth = (double)tmpWidth / (m_version < 600 ? 1000.0 :254000.0); |
| 1559 | double patternHeight = (double)tmpHeight / (m_version < 600 ? 1000.0 :254000.0); |
| 1560 | bool isRelative = false; |
| 1561 | if ((flags & 0x04) && (m_version < 900)) |
| 1562 | { |
| 1563 | isRelative = true; |
| 1564 | patternWidth = (double)tmpWidth / 100.0; |
| 1565 | patternHeight = (double)tmpHeight / 100.0; |
| 1566 | } |
| 1567 | if (m_version >= 1300) |
| 1568 | input->seek(17, librevenge::RVNG_SEEK_CUR); |
| 1569 | else |
| 1570 | input->seek(21, librevenge::RVNG_SEEK_CUR); |
| 1571 | if (m_version >= 600) |
| 1572 | patternId = readUnsigned(input); |
| 1573 | imageFill = libcdr::CDRImageFill(patternId, patternWidth, patternHeight, isRelative, tileOffsetX, tileOffsetY, rcpOffset, flags); |
| 1574 | } |
| 1575 | break; |
| 1576 | case 10: // Full color |
| 1577 | { |
| 1578 | if (m_version >= 1300) |
| 1579 | { |
| 1580 | _skipX3Optional(input); |
| 1581 | input->seek(-4, librevenge::RVNG_SEEK_CUR); |
| 1582 | } |
| 1583 | else |
| 1584 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1585 | unsigned patternId = readUnsigned(input); |
| 1586 | int tmpWidth = readUnsigned(input); |
| 1587 | int tmpHeight = readUnsigned(input); |
| 1588 | double tileOffsetX = 0.0; |
| 1589 | double tileOffsetY = 0.0; |
| 1590 | if (m_version < 900) |
| 1591 | { |
| 1592 | tileOffsetX = (double)readU16(input) / 100.0; |
| 1593 | tileOffsetY = (double)readU16(input) / 100.0; |
| 1594 | } |
| 1595 | else |
| 1596 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1597 | double rcpOffset = (double)readU16(input) / 100.0; |
| 1598 | unsigned char flags = readU8(input); |
| 1599 | double patternWidth = (double)tmpWidth / (m_version < 600 ? 1000.0 :254000.0); |
| 1600 | double patternHeight = (double)tmpHeight / (m_version < 600 ? 1000.0 :254000.0); |
| 1601 | bool isRelative = false; |
| 1602 | if ((flags & 0x04) && (m_version < 900)) |
| 1603 | { |
| 1604 | isRelative = true; |
| 1605 | patternWidth = (double)tmpWidth / 100.0; |
| 1606 | patternHeight = (double)tmpHeight / 100.0; |
| 1607 | } |
| 1608 | if (m_version >= 1300) |
| 1609 | input->seek(17, librevenge::RVNG_SEEK_CUR); |
| 1610 | else |
| 1611 | input->seek(21, librevenge::RVNG_SEEK_CUR); |
| 1612 | if (m_version >= 600) |
| 1613 | patternId = readUnsigned(input); |
| 1614 | imageFill = libcdr::CDRImageFill(patternId, patternWidth, patternHeight, isRelative, tileOffsetX, tileOffsetY, rcpOffset, flags); |
| 1615 | } |
| 1616 | break; |
| 1617 | case 11: // Texture |
| 1618 | { |
| 1619 | if (m_version < 600) |
| 1620 | fillType = 10; |
| 1621 | if (m_version >= 1300) |
| 1622 | { |
| 1623 | _skipX3Optional(input); |
| 1624 | input->seek(-4, librevenge::RVNG_SEEK_CUR); |
| 1625 | } |
| 1626 | else |
| 1627 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1628 | unsigned patternId = readU32(input); |
| 1629 | double patternWidth = 1.0; |
| 1630 | double patternHeight = 1.0; |
| 1631 | bool isRelative = true; |
| 1632 | double tileOffsetX = 0.0; |
| 1633 | double tileOffsetY = 0.0; |
| 1634 | double rcpOffset = 0.0; |
| 1635 | unsigned char flags = 0; |
| 1636 | if (m_version >= 600) |
| 1637 | { |
| 1638 | int tmpWidth = readUnsigned(input); |
| 1639 | int tmpHeight = readUnsigned(input); |
| 1640 | if (m_version < 900) |
| 1641 | { |
| 1642 | tileOffsetX = (double)readU16(input) / 100.0; |
| 1643 | tileOffsetY = (double)readU16(input) / 100.0; |
| 1644 | } |
| 1645 | else |
| 1646 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1647 | rcpOffset = (double)readU16(input) / 100.0; |
| 1648 | flags = readU8(input); |
| 1649 | patternWidth = (double)tmpWidth / 254000.0; |
| 1650 | patternHeight = (double)tmpHeight / 254000.0; |
| 1651 | isRelative = false; |
| 1652 | if ((flags & 0x04) && (m_version < 900)) |
| 1653 | { |
| 1654 | isRelative = true; |
| 1655 | patternWidth = (double)tmpWidth / 100.0; |
| 1656 | patternHeight = (double)tmpHeight / 100.0; |
| 1657 | } |
| 1658 | if (m_version >= 1300) |
| 1659 | input->seek(17, librevenge::RVNG_SEEK_CUR); |
| 1660 | else |
| 1661 | input->seek(21, librevenge::RVNG_SEEK_CUR); |
| 1662 | patternId = readUnsigned(input); |
| 1663 | } |
| 1664 | imageFill = libcdr::CDRImageFill(patternId, patternWidth, patternHeight, isRelative, tileOffsetX, tileOffsetY, rcpOffset, flags); |
| 1665 | } |
| 1666 | break; |
| 1667 | default: |
| 1668 | break; |
| 1669 | } |
| 1670 | m_fillStyles[fillId] = CDRFillStyle(fillType, color1, color2, gradient, imageFill); |
| 1671 | m_collector->collectFillStyle(fillId, CDRFillStyle(fillType, color1, color2, gradient, imageFill)); |
| 1672 | } |
| 1673 | |
| 1674 | void libcdr::CDRParser::readOutl(librevenge::RVNGInputStream *input, unsigned length) |
| 1675 | { |
| 1676 | if (!_redirectX6Chunk(&input, length)) |
| 1677 | throw GenericException(); |
| 1678 | unsigned lineId = readU32(input); |
| 1679 | if (m_version >= 1300) |
| 1680 | { |
| 1681 | unsigned id = 0; |
| 1682 | unsigned lngth = 0; |
| 1683 | while (id != 1) |
| 1684 | { |
| 1685 | input->seek(lngth, librevenge::RVNG_SEEK_CUR); |
| 1686 | id = readU32(input); |
| 1687 | lngth = readU32(input); |
| 1688 | } |
| 1689 | } |
| 1690 | unsigned short lineType = readU16(input); |
| 1691 | unsigned short capsType = readU16(input); |
| 1692 | unsigned short joinType = readU16(input); |
| 1693 | if (m_version < 1300 && m_version >= 600) |
| 1694 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1695 | auto lineWidth = (double)readCoordinate(input); |
| 1696 | double stretch = (double)readU16(input) / 100.0; |
| 1697 | if (m_version >= 600) |
| 1698 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1699 | double angle = readAngle(input); |
| 1700 | if (m_version >= 1300) |
| 1701 | input->seek(46, librevenge::RVNG_SEEK_CUR); |
| 1702 | else if (m_version >= 600) |
| 1703 | input->seek(52, librevenge::RVNG_SEEK_CUR); |
| 1704 | libcdr::CDRColor color = readColor(input); |
| 1705 | if (m_version < 600) |
| 1706 | input->seek(10, librevenge::RVNG_SEEK_CUR); |
| 1707 | else |
| 1708 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 1709 | unsigned long numDash = readU16(input); |
| 1710 | if (numDash > getRemainingLength(input) / 2) |
| 1711 | numDash = getRemainingLength(input) / 2; |
| 1712 | long fixPosition = input->tell(); |
| 1713 | std::vector<unsigned> dashArray; |
| 1714 | dashArray.reserve(numDash); |
| 1715 | for (unsigned long i = 0; i < numDash; ++i) |
| 1716 | dashArray.push_back(readU16(input)); |
| 1717 | if (m_version < 600) |
| 1718 | input->seek(fixPosition + 20, librevenge::RVNG_SEEK_SET); |
| 1719 | else |
| 1720 | input->seek(fixPosition + 22, librevenge::RVNG_SEEK_SET); |
| 1721 | unsigned startMarkerId = readU32(input); |
| 1722 | std::map<unsigned, CDRPath>::const_iterator iter = m_arrows.find(startMarkerId); |
| 1723 | CDRPath startMarker; |
| 1724 | if (iter != m_arrows.end()) |
| 1725 | startMarker = iter->second; |
| 1726 | unsigned endMarkerId = readU32(input); |
| 1727 | iter = m_arrows.find(endMarkerId); |
| 1728 | CDRPath endMarker; |
| 1729 | if (iter != m_arrows.end()) |
| 1730 | endMarker = iter->second; |
| 1731 | m_lineStyles[lineId] = CDRLineStyle(lineType, capsType, joinType, lineWidth, stretch, angle, color, dashArray, startMarker, endMarker); |
| 1732 | m_collector->collectLineStyle(lineId, CDRLineStyle(lineType, capsType, joinType, lineWidth, stretch, angle, color, dashArray, startMarker, endMarker)); |
| 1733 | } |
| 1734 | |
| 1735 | void libcdr::CDRParser::readLoda(librevenge::RVNGInputStream *input, unsigned length) |
| 1736 | { |
| 1737 | if (!_redirectX6Chunk(&input, length)) |
| 1738 | throw GenericException(); |
| 1739 | long startPosition = input->tell(); |
| 1740 | const unsigned long maxLength = getLength(input); |
| 1741 | if (startPosition >= long(maxLength)) |
| 1742 | return; |
| 1743 | if ((length > maxLength) || (long(maxLength - length) < startPosition)) |
| 1744 | length = unsigned(maxLength - static_cast<unsigned long>(startPosition)); // sanitize length |
| 1745 | unsigned chunkLength = readUnsigned(input); |
| 1746 | unsigned numOfArgs = readUnsigned(input); |
| 1747 | unsigned startOfArgs = readUnsigned(input); |
| 1748 | if (startOfArgs >= length) |
| 1749 | return; |
| 1750 | unsigned startOfArgTypes = readUnsigned(input); |
| 1751 | if (startOfArgTypes >= length) |
| 1752 | return; |
| 1753 | if (numOfArgs > (length - startOfArgs) / 4) // avoid extra big allocation in case of a broken file |
| 1754 | numOfArgs = (length - startOfArgs) / 4; |
| 1755 | unsigned chunkType = readUnsigned(input); |
| 1756 | if (chunkType == 0x26) |
| 1757 | m_collector->collectSpline(); |
| 1758 | std::vector<unsigned> argOffsets(numOfArgs, 0); |
| 1759 | std::vector<unsigned> argTypes(numOfArgs, 0); |
| 1760 | size_t i = 0; |
| 1761 | input->seek(startPosition+startOfArgs, librevenge::RVNG_SEEK_SET); |
| 1762 | while (i<numOfArgs) |
| 1763 | argOffsets[i++] = readUnsigned(input); |
| 1764 | input->seek(startPosition+startOfArgTypes, librevenge::RVNG_SEEK_SET); |
| 1765 | while (i>0) |
| 1766 | argTypes[--i] = readUnsigned(input); |
| 1767 | |
| 1768 | for (i=0; i < argTypes.size(); i++) |
| 1769 | { |
| 1770 | input->seek(startPosition+argOffsets[i], librevenge::RVNG_SEEK_SET); |
| 1771 | if (argTypes[i] == 0x1e) // loda coords |
| 1772 | { |
| 1773 | if ((m_version >= 400 && chunkType == 0x01) || (m_version < 400 && chunkType == 0x00)) // Rectangle |
| 1774 | readRectangle(input); |
| 1775 | else if ((m_version >= 400 && chunkType == 0x02) || (m_version < 400 && chunkType == 0x01)) // Ellipse |
| 1776 | readEllipse(input); |
| 1777 | else if ((m_version >= 400 && chunkType == 0x03) || (m_version < 400 && chunkType == 0x02)) // Line and curve |
| 1778 | readLineAndCurve(input); |
| 1779 | else if (chunkType == 0x25) // Path |
| 1780 | readPath(input); |
| 1781 | else if ((m_version >= 400 && chunkType == 0x04) || (m_version < 400 && chunkType == 0x03)) // Artistic text |
| 1782 | readArtisticText(input); |
| 1783 | else if ((m_version >= 400 && chunkType == 0x05) || (m_version < 400 && chunkType == 0x04)) // Bitmap |
| 1784 | readBitmap(input); |
| 1785 | else if ((m_version >= 400 && chunkType == 0x06) || (m_version < 400 && chunkType == 0x05)) // Paragraph text |
| 1786 | readParagraphText(input); |
| 1787 | else if (chunkType == 0x14) // Polygon |
| 1788 | readPolygonCoords(input); |
| 1789 | } |
| 1790 | else if (argTypes[i] == 0x14) |
| 1791 | { |
| 1792 | if (m_version < 400) |
| 1793 | readWaldoFill(input); |
| 1794 | else |
| 1795 | { |
| 1796 | unsigned fillId = readU32(input); |
| 1797 | if (fillId) |
| 1798 | m_collector->collectFillStyleId(fillId); |
| 1799 | } |
| 1800 | } |
| 1801 | else if (argTypes[i] == 0x0a) |
| 1802 | { |
| 1803 | if (m_version < 400) |
| 1804 | readWaldoOutl(input); |
| 1805 | else |
| 1806 | { |
| 1807 | unsigned outlId = readU32(input); |
| 1808 | if (outlId) |
| 1809 | m_collector->collectLineStyleId(outlId); |
| 1810 | } |
| 1811 | } |
| 1812 | else if (argTypes[i] == 0xc8) |
| 1813 | { |
| 1814 | unsigned styleId = readUnsigned(input); |
| 1815 | if (styleId) |
| 1816 | m_collector->collectStyleId(styleId); |
| 1817 | } |
| 1818 | else if (argTypes[i] == 0x2af8) |
| 1819 | readPolygonTransform(input); |
| 1820 | else if (argTypes[i] == 0x1f40) |
| 1821 | readOpacity(input, length); |
| 1822 | else if (argTypes[i] == 0x64) |
| 1823 | { |
| 1824 | if (m_version < 400) |
| 1825 | readWaldoTrfd(input); |
| 1826 | } |
| 1827 | else if (argTypes[i] == 0x4aba) |
| 1828 | readPageSize(input); |
| 1829 | } |
| 1830 | input->seek(startPosition+chunkLength, librevenge::RVNG_SEEK_SET); |
| 1831 | } |
| 1832 | |
| 1833 | void libcdr::CDRParser::readFlags(librevenge::RVNGInputStream *input, unsigned length) |
| 1834 | { |
| 1835 | if (!_redirectX6Chunk(&input, length)) |
| 1836 | throw GenericException(); |
| 1837 | unsigned flags = readU32(input); |
| 1838 | m_collector->collectFlags(flags, m_version >= 400); |
| 1839 | } |
| 1840 | |
| 1841 | void libcdr::CDRParser::readMcfg(librevenge::RVNGInputStream *input, unsigned length) |
| 1842 | { |
| 1843 | if (!_redirectX6Chunk(&input, length)) |
| 1844 | throw GenericException(); |
| 1845 | if (m_version >= 1300) |
| 1846 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 1847 | else if (m_version >= 900) |
| 1848 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1849 | else if (m_version < 700 && m_version >= 600) |
| 1850 | input->seek(0x1c, librevenge::RVNG_SEEK_CUR); |
| 1851 | double width = 0.0; |
| 1852 | double height = 0.0; |
| 1853 | if (m_version < 400) |
| 1854 | { |
| 1855 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1856 | double x0 = readCoordinate(input); |
| 1857 | double y0 = readCoordinate(input); |
| 1858 | double x1 = readCoordinate(input); |
| 1859 | double y1 = readCoordinate(input); |
| 1860 | width = fabs(x1-x0); |
| 1861 | height = fabs(y1-y0); |
| 1862 | } |
| 1863 | else |
| 1864 | { |
| 1865 | width = readCoordinate(input); |
| 1866 | height = readCoordinate(input); |
| 1867 | } |
| 1868 | m_collector->collectPageSize(width, height, -width/2.0, -height/2.0); |
| 1869 | } |
| 1870 | |
| 1871 | void libcdr::CDRParser::readPolygonCoords(librevenge::RVNGInputStream *input) |
| 1872 | { |
| 1873 | CDR_DEBUG_MSG(("CDRParser::readPolygonCoords\n")); |
| 1874 | |
| 1875 | unsigned long pointNum = readU16(input); |
| 1876 | const unsigned short pointSize = 2 * (m_precision == PRECISION_16BIT ? 2 : 4) + 1; |
| 1877 | if (pointNum > getRemainingLength(input) / pointSize) |
| 1878 | pointNum = getRemainingLength(input) / pointSize; |
| 1879 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1880 | std::vector<std::pair<double, double> > points; |
| 1881 | std::vector<unsigned char> pointTypes; |
| 1882 | points.reserve(pointNum); |
| 1883 | pointTypes.reserve(pointNum); |
| 1884 | for (unsigned long j=0; j<pointNum; j++) |
| 1885 | { |
| 1886 | std::pair<double, double> point; |
| 1887 | point.first = (double)readCoordinate(input); |
| 1888 | point.second = (double)readCoordinate(input); |
| 1889 | points.push_back(point); |
| 1890 | } |
| 1891 | for (unsigned long k=0; k<pointNum; k++) |
| 1892 | pointTypes.push_back(readU8(input)); |
| 1893 | outputPath(points, pointTypes); |
| 1894 | m_collector->collectPolygon(); |
| 1895 | } |
| 1896 | |
| 1897 | void libcdr::CDRParser::readPolygonTransform(librevenge::RVNGInputStream *input) |
| 1898 | { |
| 1899 | if (m_version < 1300) |
| 1900 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1901 | unsigned numAngles = readU32(input); |
| 1902 | unsigned nextPoint = readU32(input); |
| 1903 | if (nextPoint <= 1) |
| 1904 | nextPoint = readU32(input); |
| 1905 | else |
| 1906 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1907 | if (m_version >= 1300) |
| 1908 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1909 | double rx = readDouble(input); |
| 1910 | double ry = readDouble(input); |
| 1911 | double cx = readCoordinate(input); |
| 1912 | double cy = readCoordinate(input); |
| 1913 | m_collector->collectPolygonTransform(numAngles, nextPoint, rx, ry, cx, cy); |
| 1914 | } |
| 1915 | |
| 1916 | void libcdr::CDRParser::readPageSize(librevenge::RVNGInputStream *input) |
| 1917 | { |
| 1918 | double width = readCoordinate(input); |
| 1919 | double height = readCoordinate(input); |
| 1920 | m_collector->collectPageSize(width, height, -width/2.0, -height/2.0); |
| 1921 | } |
| 1922 | |
| 1923 | void libcdr::CDRParser::readWaldoBmp(librevenge::RVNGInputStream *input, unsigned length, unsigned id) |
| 1924 | { |
| 1925 | if (m_version >= 400) |
| 1926 | return; |
| 1927 | if (readU8(input) != 0x42) |
| 1928 | return; |
| 1929 | if (readU8(input) != 0x4d) |
| 1930 | return; |
| 1931 | input->seek(-2, librevenge::RVNG_SEEK_CUR); |
| 1932 | unsigned long tmpNumBytesRead = 0; |
| 1933 | const unsigned char *tmpBuffer = input->read(length, tmpNumBytesRead); |
| 1934 | if (!tmpNumBytesRead || length != tmpNumBytesRead) |
| 1935 | return; |
| 1936 | std::vector<unsigned char> bitmap(tmpNumBytesRead); |
| 1937 | memcpy(&bitmap[0], tmpBuffer, tmpNumBytesRead); |
| 1938 | m_collector->collectBmp(id, bitmap); |
| 1939 | return; |
| 1940 | } |
| 1941 | |
| 1942 | void libcdr::CDRParser::readBmp(librevenge::RVNGInputStream *input, unsigned length) |
| 1943 | { |
| 1944 | if (!_redirectX6Chunk(&input, length)) |
| 1945 | throw GenericException(); |
| 1946 | unsigned imageId = readUnsigned(input); |
| 1947 | std::vector<unsigned char> bitmap; |
| 1948 | if (m_version < 500) |
| 1949 | { |
| 1950 | if (readU8(input) != 0x42) |
| 1951 | return; |
| 1952 | if (readU8(input) != 0x4d) |
| 1953 | return; |
| 1954 | unsigned lngth = readU32(input); |
| 1955 | input->seek(-6, librevenge::RVNG_SEEK_CUR); |
| 1956 | unsigned long tmpNumBytesRead = 0; |
| 1957 | const unsigned char *tmpBuffer = input->read(lngth, tmpNumBytesRead); |
| 1958 | if (!tmpNumBytesRead || lngth != tmpNumBytesRead) |
| 1959 | return; |
| 1960 | bitmap.resize(tmpNumBytesRead); |
| 1961 | memcpy(&bitmap[0], tmpBuffer, tmpNumBytesRead); |
| 1962 | m_collector->collectBmp(imageId, bitmap); |
| 1963 | return; |
| 1964 | } |
| 1965 | if (m_version < 600) |
| 1966 | input->seek(14, librevenge::RVNG_SEEK_CUR); |
| 1967 | else if (m_version < 700) |
| 1968 | input->seek(46, librevenge::RVNG_SEEK_CUR); |
| 1969 | else |
| 1970 | input->seek(50, librevenge::RVNG_SEEK_CUR); |
| 1971 | unsigned colorModel, width, height, bpp; |
| 1972 | std::vector<unsigned> palette; |
| 1973 | readRImage(colorModel, width, height, bpp, palette, bitmap, input); |
| 1974 | m_collector->collectBmp(imageId, colorModel, width, height, bpp, palette, bitmap); |
| 1975 | } |
| 1976 | |
| 1977 | void libcdr::CDRParser::readOpacity(librevenge::RVNGInputStream *input, unsigned /* length */) |
| 1978 | { |
| 1979 | if (m_version < 1300) |
| 1980 | input->seek(10, librevenge::RVNG_SEEK_CUR); |
| 1981 | else |
| 1982 | input->seek(14, librevenge::RVNG_SEEK_CUR); |
| 1983 | double opacity = (double)readU16(input) / 1000.0; |
| 1984 | m_collector->collectFillOpacity(opacity); |
| 1985 | } |
| 1986 | |
| 1987 | void libcdr::CDRParser::readBmpf(librevenge::RVNGInputStream *input, unsigned length) |
| 1988 | { |
| 1989 | if (!_redirectX6Chunk(&input, length)) |
| 1990 | throw GenericException(); |
| 1991 | unsigned patternId = readU32(input); |
| 1992 | unsigned width{}, height{}; |
| 1993 | std::vector<unsigned char> pattern; |
| 1994 | readBmpPattern(width, height, pattern, length-4, input); |
| 1995 | m_collector->collectBmpf(patternId, width, height, pattern); |
| 1996 | } |
| 1997 | |
| 1998 | void libcdr::CDRParser::readWaldoBmpf(librevenge::RVNGInputStream *input, unsigned id) |
| 1999 | { |
| 2000 | unsigned headerLength = readU32(input); |
| 2001 | if (headerLength != 40) |
| 2002 | return; |
| 2003 | unsigned width = readU32(input); |
| 2004 | unsigned height = readU32(input); |
| 2005 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2006 | unsigned bpp = readU16(input); |
| 2007 | if (bpp != 1) |
| 2008 | return; |
| 2009 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2010 | unsigned dataSize = readU32(input); |
| 2011 | unsigned long tmpNumBytesRead = 0; |
| 2012 | input->seek(24, librevenge::RVNG_SEEK_CUR); // TODO: is this empirical experience universal??? |
| 2013 | const unsigned char *tmpBuffer = input->read(dataSize, tmpNumBytesRead); |
| 2014 | if (dataSize != tmpNumBytesRead) |
| 2015 | return; |
| 2016 | std::vector<unsigned char> pattern(dataSize); |
| 2017 | memcpy(&pattern[0], tmpBuffer, dataSize); |
| 2018 | m_collector->collectBmpf(id, width, height, pattern); |
| 2019 | } |
| 2020 | |
| 2021 | void libcdr::CDRParser::readPpdt(librevenge::RVNGInputStream *input, unsigned length) |
| 2022 | { |
| 2023 | if (!_redirectX6Chunk(&input, length)) |
| 2024 | throw GenericException(); |
| 2025 | unsigned long pointNum = readU16(input); |
| 2026 | const unsigned short pointSize = 2 * (m_precision == PRECISION_16BIT ? 2 : 4) + 4; |
| 2027 | if (pointNum > getRemainingLength(input) / pointSize) |
| 2028 | pointNum = getRemainingLength(input) / pointSize; |
| 2029 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2030 | std::vector<std::pair<double, double> > points; |
| 2031 | std::vector<unsigned> knotVector; |
| 2032 | points.reserve(pointNum); |
| 2033 | knotVector.reserve(pointNum); |
| 2034 | for (unsigned long j=0; j<pointNum; j++) |
| 2035 | { |
| 2036 | std::pair<double, double> point; |
| 2037 | point.first = (double)readCoordinate(input); |
| 2038 | point.second = (double)readCoordinate(input); |
| 2039 | points.push_back(point); |
| 2040 | } |
| 2041 | for (unsigned long k=0; k<pointNum; k++) |
| 2042 | knotVector.push_back(readU32(input)); |
| 2043 | m_collector->collectPpdt(points, knotVector); |
| 2044 | } |
| 2045 | |
| 2046 | void libcdr::CDRParser::readFtil(librevenge::RVNGInputStream *input, unsigned length) |
| 2047 | { |
| 2048 | if (!_redirectX6Chunk(&input, length)) |
| 2049 | throw GenericException(); |
| 2050 | double v0 = readDouble(input); |
| 2051 | double v1 = readDouble(input); |
| 2052 | double x0 = readDouble(input) / 254000.0; |
| 2053 | double v3 = readDouble(input); |
| 2054 | double v4 = readDouble(input); |
| 2055 | double y0 = readDouble(input) / 254000.0; |
| 2056 | CDRTransforms fillTrafos; |
| 2057 | fillTrafos.append(v0, v1, x0, v3, v4, y0); |
| 2058 | m_collector->collectFillTransform(fillTrafos); |
| 2059 | } |
| 2060 | |
| 2061 | void libcdr::CDRParser::readVersion(librevenge::RVNGInputStream *input, unsigned length) |
| 2062 | { |
| 2063 | if (!_redirectX6Chunk(&input, length)) |
| 2064 | throw GenericException(); |
| 2065 | m_version = readU16(input); |
| 2066 | if (m_version < 600) |
| 2067 | m_precision = libcdr::PRECISION_16BIT; |
| 2068 | else |
| 2069 | m_precision = libcdr::PRECISION_32BIT; |
| 2070 | } |
| 2071 | |
| 2072 | bool libcdr::CDRParser::_redirectX6Chunk(librevenge::RVNGInputStream **input, unsigned &length) |
| 2073 | { |
| 2074 | if (m_version >= 1600 && length == 0x10) |
| 2075 | { |
| 2076 | unsigned streamNumber = readU32(*input); |
| 2077 | length = readU32(*input); |
| 2078 | if (streamNumber < m_externalStreams.size()) |
| 2079 | { |
| 2080 | unsigned streamOffset = readU32(*input); |
| 2081 | *input = m_externalStreams[streamNumber].get(); |
| 2082 | if (*input) |
| 2083 | { |
| 2084 | (*input)->seek(streamOffset, librevenge::RVNG_SEEK_SET); |
| 2085 | return !(*input)->isEnd(); |
| 2086 | } |
| 2087 | else |
| 2088 | return false; |
| 2089 | } |
| 2090 | else if (streamNumber == 0xffffffff) |
| 2091 | return true; |
| 2092 | return false; |
| 2093 | } |
| 2094 | return true; |
| 2095 | } |
| 2096 | |
| 2097 | void libcdr::CDRParser::readIccd(librevenge::RVNGInputStream *input, unsigned length) |
| 2098 | { |
| 2099 | if (!_redirectX6Chunk(&input, length)) |
| 2100 | throw GenericException(); |
| 2101 | unsigned long numBytesRead = 0; |
| 2102 | const unsigned char *tmpProfile = input->read(length, numBytesRead); |
| 2103 | if (length != numBytesRead) |
| 2104 | throw EndOfStreamException(); |
| 2105 | if (!numBytesRead) |
| 2106 | return; |
| 2107 | std::vector<unsigned char> profile(numBytesRead); |
| 2108 | memcpy(&profile[0], tmpProfile, numBytesRead); |
| 2109 | m_collector->collectColorProfile(profile); |
| 2110 | } |
| 2111 | |
| 2112 | void libcdr::CDRParser::readBBox(librevenge::RVNGInputStream *input, unsigned length) |
| 2113 | { |
| 2114 | if (!_redirectX6Chunk(&input, length)) |
| 2115 | throw GenericException(); |
| 2116 | double x0 = readCoordinate(input); |
| 2117 | double y0 = readCoordinate(input); |
| 2118 | double x1 = readCoordinate(input); |
| 2119 | double y1 = readCoordinate(input); |
| 2120 | m_collector->collectBBox(x0, y0, x1, y1); |
| 2121 | } |
| 2122 | |
| 2123 | void libcdr::CDRParser::readSpnd(librevenge::RVNGInputStream *input, unsigned length) |
| 2124 | { |
| 2125 | if (!_redirectX6Chunk(&input, length)) |
| 2126 | throw GenericException(); |
| 2127 | unsigned spnd = readUnsigned(input); |
| 2128 | m_collector->collectSpnd(spnd); |
| 2129 | } |
| 2130 | |
| 2131 | void libcdr::CDRParser::readVpat(librevenge::RVNGInputStream *input, unsigned length) |
| 2132 | { |
| 2133 | if (!_redirectX6Chunk(&input, length)) |
| 2134 | throw GenericException(); |
| 2135 | unsigned fillId = readUnsigned(input); |
| 2136 | unsigned long numBytesRead = 0; |
| 2137 | const unsigned char *buffer = input->read(length-4, numBytesRead); |
| 2138 | if (numBytesRead) |
| 2139 | { |
| 2140 | librevenge::RVNGBinaryData data(buffer, numBytesRead); |
| 2141 | m_collector->collectVectorPattern(fillId, data); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | void libcdr::CDRParser::readUidr(librevenge::RVNGInputStream *input, unsigned length) |
| 2146 | { |
| 2147 | if (!_redirectX6Chunk(&input, length)) |
| 2148 | throw GenericException(); |
| 2149 | unsigned colorId = readU32(input); |
| 2150 | unsigned userId = readU32(input); |
| 2151 | input->seek(36, librevenge::RVNG_SEEK_CUR); |
| 2152 | CDRColor color = readColor(input); |
| 2153 | m_collector->collectPaletteEntry(colorId, userId, color); |
| 2154 | } |
| 2155 | |
| 2156 | void libcdr::CDRParser::readFont(librevenge::RVNGInputStream *input, unsigned length) |
| 2157 | { |
| 2158 | if (!_redirectX6Chunk(&input, length)) |
| 2159 | throw GenericException(); |
| 2160 | unsigned short fontId = readU16(input); |
| 2161 | unsigned short fontEncoding = readU16(input); |
| 2162 | input->seek(14, librevenge::RVNG_SEEK_CUR); |
| 2163 | std::vector<unsigned char> name; |
| 2164 | librevenge::RVNGString fontName; |
| 2165 | if (m_version >= 1200) |
| 2166 | { |
| 2167 | unsigned short character = 0; |
| 2168 | while (true) |
| 2169 | { |
| 2170 | character = readU16(input); |
| 2171 | if (character) |
| 2172 | { |
| 2173 | name.push_back((unsigned char)(character & 0xff)); |
| 2174 | name.push_back((unsigned char)(character >> 8)); |
| 2175 | } |
| 2176 | else |
| 2177 | break; |
| 2178 | } |
| 2179 | appendCharacters(fontName, name); |
| 2180 | } |
| 2181 | else |
| 2182 | { |
| 2183 | unsigned char character = 0; |
| 2184 | while (true) |
| 2185 | { |
| 2186 | character = readU8(input); |
| 2187 | if (character) |
| 2188 | name.push_back(character); |
| 2189 | else |
| 2190 | break; |
| 2191 | } |
| 2192 | appendCharacters(fontName, name, fontEncoding); |
| 2193 | } |
| 2194 | if (!fontEncoding) |
| 2195 | processNameForEncoding(fontName, fontEncoding); |
| 2196 | std::map<unsigned, CDRFont>::const_iterator iter = m_fonts.find(fontId); |
| 2197 | // Asume that the first font with the given ID is a font |
| 2198 | // that we want, the others are substitution fonts. We might |
| 2199 | // be utterly wrong in this one |
| 2200 | if (iter == m_fonts.end()) |
| 2201 | m_fonts[fontId] = CDRFont(fontName, fontEncoding); |
| 2202 | } |
| 2203 | |
| 2204 | void libcdr::CDRParser::readStlt(librevenge::RVNGInputStream *input, unsigned length) |
| 2205 | { |
| 2206 | #ifndef DEBUG |
| 2207 | unsigned version = m_version; |
| 2208 | try |
| 2209 | { |
| 2210 | #endif |
| 2211 | if (m_version < 700) |
| 2212 | return; |
| 2213 | if (!_redirectX6Chunk(&input, length)) |
| 2214 | throw GenericException(); |
| 2215 | long startPosition = input->tell(); |
| 2216 | unsigned numRecords = readU32(input); |
| 2217 | CDR_DEBUG_MSG(("CDRParser::readStlt numRecords 0x%x\n", numRecords)); |
| 2218 | if (!numRecords) |
| 2219 | return; |
| 2220 | unsigned long numFills = readU32(input); |
| 2221 | const unsigned fillSize = 3 * 4 + (m_version >= 1300 ? 48 : 0); |
| 2222 | if (numFills > getRemainingLength(input) / fillSize) |
| 2223 | numFills = getRemainingLength(input) / fillSize; |
| 2224 | CDR_DEBUG_MSG(("CDRParser::readStlt numFills 0x%lx\n", numFills)); |
| 2225 | std::map<unsigned, unsigned> fillIds; |
| 2226 | for (unsigned long i=0; i<numFills; ++i) |
| 2227 | { |
| 2228 | unsigned fillId = readU32(input); |
| 2229 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2230 | fillIds[fillId] = readU32(input); |
| 2231 | if (m_version >= 1300) |
| 2232 | input->seek(48, librevenge::RVNG_SEEK_CUR); |
| 2233 | } |
| 2234 | unsigned long numOutls = readU32(input); |
| 2235 | if (numOutls > getRemainingLength(input) / 12) |
| 2236 | numOutls = getRemainingLength(input) / 12; |
| 2237 | CDR_DEBUG_MSG(("CDRParser::readStlt numOutls 0x%lx\n", numOutls)); |
| 2238 | std::map<unsigned, unsigned> outlIds; |
| 2239 | for (unsigned long i=0; i<numOutls; ++i) |
| 2240 | { |
| 2241 | unsigned outlId = readU32(input); |
| 2242 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2243 | outlIds[outlId] = readU32(input); |
| 2244 | } |
| 2245 | unsigned long numFonts = readU32(input); |
| 2246 | const unsigned fontsSize = 4 + 2 * 2 + 8 + (m_precision == PRECISION_16BIT ? 2 : 4) + 2 * (m_version < 1000 ? 12 : 20); |
| 2247 | if (numFonts > getRemainingLength(input) / fontsSize) |
| 2248 | numFonts = getRemainingLength(input) / fontsSize; |
| 2249 | CDR_DEBUG_MSG(("CDRParser::readStlt numFonts 0x%lx\n", numFonts)); |
| 2250 | std::map<unsigned,unsigned short> fontIds, fontEncodings; |
| 2251 | std::map<unsigned,double> fontSizes; |
| 2252 | for (unsigned long i=0; i<numFonts; ++i) |
| 2253 | { |
| 2254 | unsigned fontStyleId = readU32(input); |
| 2255 | if (m_version < 1000) |
| 2256 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 2257 | else |
| 2258 | input->seek(20, librevenge::RVNG_SEEK_CUR); |
| 2259 | fontIds[fontStyleId] = readU16(input); |
| 2260 | fontEncodings[fontStyleId] = readU16(input); |
| 2261 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2262 | fontSizes[fontStyleId] = readCoordinate(input); |
| 2263 | if (m_version < 1000) |
| 2264 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 2265 | else |
| 2266 | input->seek(20, librevenge::RVNG_SEEK_CUR); |
| 2267 | } |
| 2268 | unsigned long numAligns = readU32(input); |
| 2269 | if (numAligns > getRemainingLength(input) / 12) |
| 2270 | numAligns = getRemainingLength(input) / 12; |
| 2271 | std::map<unsigned,unsigned> aligns; |
| 2272 | CDR_DEBUG_MSG(("CDRParser::readStlt numAligns 0x%lx\n", numAligns)); |
| 2273 | for (unsigned long i=0; i<numAligns; ++i) |
| 2274 | { |
| 2275 | unsigned alignId = readU32(input); |
| 2276 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2277 | aligns[alignId] = readU32(input); |
| 2278 | } |
| 2279 | unsigned numIntervals = readU32(input); |
| 2280 | CDR_DEBUG_MSG(("CDRParser::readStlt numIntervals 0x%x\n", numIntervals)); |
| 2281 | input->seek(52 * static_cast<long>(numIntervals), librevenge::RVNG_SEEK_CUR); |
| 2282 | unsigned numSet5s = readU32(input); |
| 2283 | CDR_DEBUG_MSG(("CDRParser::readStlt numSet5s 0x%x\n", numSet5s)); |
| 2284 | input->seek(152 * static_cast<long>(numSet5s), librevenge::RVNG_SEEK_CUR); |
| 2285 | unsigned numTabs = readU32(input); |
| 2286 | CDR_DEBUG_MSG(("CDRParser::readStlt numTabs 0x%x\n", numTabs)); |
| 2287 | input->seek(784 * static_cast<long>(numTabs), librevenge::RVNG_SEEK_CUR); |
| 2288 | unsigned numBullets = readU32(input); |
| 2289 | CDR_DEBUG_MSG(("CDRParser::readStlt numBullets 0x%x\n", numBullets)); |
| 2290 | for (unsigned i=0; i<numBullets && getRemainingLength(input) >= 16; ++i) |
| 2291 | { |
| 2292 | input->seek(40, librevenge::RVNG_SEEK_CUR); |
| 2293 | if (m_version > 1300) |
| 2294 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2295 | if (m_version >= 1300) |
| 2296 | { |
| 2297 | if (readU32(input)) |
| 2298 | input->seek(68, librevenge::RVNG_SEEK_CUR); |
| 2299 | else |
| 2300 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 2301 | } |
| 2302 | else |
| 2303 | { |
| 2304 | input->seek(20, librevenge::RVNG_SEEK_CUR); |
| 2305 | if (m_version >= 1000) |
| 2306 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2307 | if (readU32(input)) |
| 2308 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2309 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2310 | } |
| 2311 | } |
| 2312 | unsigned long numIndents = readU32(input); |
| 2313 | const unsigned indentSize = 4 + 3 * (m_precision == PRECISION_16BIT ? 2 : 4); |
| 2314 | if (numIndents > getRemainingLength(input) / indentSize) |
| 2315 | numIndents = getRemainingLength(input) / indentSize; |
| 2316 | std::map<unsigned, double> rightIndents, firstIndents, leftIndents; |
| 2317 | CDR_DEBUG_MSG(("CDRParser::readStlt numIndents 0x%lx\n", numIndents)); |
| 2318 | for (unsigned long i=0; i<numIndents; ++i) |
| 2319 | { |
| 2320 | unsigned indentId = readU32(input); |
| 2321 | input->seek(12, librevenge::RVNG_SEEK_CUR); |
| 2322 | rightIndents[indentId] = readCoordinate(input); |
| 2323 | firstIndents[indentId] = readCoordinate(input); |
| 2324 | leftIndents[indentId] = readCoordinate(input); |
| 2325 | } |
| 2326 | unsigned numHypens = readU32(input); |
| 2327 | CDR_DEBUG_MSG(("CDRParser::readStlt numHypens 0x%x\n", numHypens)); |
| 2328 | long hypensLen = 32; |
| 2329 | if (m_version >= 1300) |
| 2330 | hypensLen += 4; |
| 2331 | input->seek(hypensLen * static_cast<long>(numHypens), librevenge::RVNG_SEEK_CUR); |
| 2332 | unsigned numDropcaps = readU32(input); |
| 2333 | CDR_DEBUG_MSG(("CDRParser::readStlt numDropcaps 0x%x\n", numDropcaps)); |
| 2334 | input->seek(28 * static_cast<long>(numDropcaps), librevenge::RVNG_SEEK_CUR); |
| 2335 | try |
| 2336 | { |
| 2337 | bool set11Flag(false); |
| 2338 | if (m_version > 800) |
| 2339 | { |
| 2340 | set11Flag = true; |
| 2341 | unsigned numSet11s = readU32(input); |
| 2342 | CDR_DEBUG_MSG(("CDRParser::readStlt numSet11s 0x%x\n", numSet11s)); |
| 2343 | input->seek(12 * static_cast<long>(numSet11s), librevenge::RVNG_SEEK_CUR); |
| 2344 | } |
| 2345 | std::map<unsigned, CDRStltRecord> styles; |
| 2346 | for (unsigned i=0; i<numRecords && getRemainingLength(input) >= 32; ++i) |
| 2347 | { |
| 2348 | CDR_DEBUG_MSG(("CDRParser::readStlt parsing styles\n")); |
| 2349 | unsigned num = readU32(input); |
| 2350 | CDR_DEBUG_MSG(("CDRParser::readStlt parsing styles num 0x%x\n", num)); |
| 2351 | unsigned styleId = readU32(input); |
| 2352 | CDRStltRecord style; |
| 2353 | style.parentId = readU32(input); |
| 2354 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2355 | unsigned namelen = readU32(input); |
| 2356 | CDR_DEBUG_MSG(("CDRParser::readStlt parsing styles namelen 0x%x\n", namelen)); |
| 2357 | if (m_version >= 1200) |
| 2358 | namelen *= 2; |
| 2359 | input->seek(namelen, librevenge::RVNG_SEEK_CUR); |
| 2360 | style.fillId = readU32(input); |
| 2361 | style.outlId = readU32(input); |
| 2362 | if (num > 1) |
| 2363 | { |
| 2364 | style.fontRecId = readU32(input); |
| 2365 | style.alignId = readU32(input); |
| 2366 | style.intervalId = readU32(input); |
| 2367 | style.set5Id = readU32(input); |
| 2368 | if (set11Flag) |
| 2369 | style.set11Id = readU32(input); |
| 2370 | } |
| 2371 | if (num > 2) |
| 2372 | { |
| 2373 | style.tabId = readU32(input); |
| 2374 | style.bulletId = readU32(input); |
| 2375 | style.indentId = readU32(input); |
| 2376 | style.hyphenId = readU32(input); |
| 2377 | style.dropCapId = readU32(input); |
| 2378 | } |
| 2379 | styles[styleId] = style; |
| 2380 | } |
| 2381 | for (std::map<unsigned, CDRStltRecord>::const_iterator iter = styles.begin(); |
| 2382 | iter != styles.end(); ++iter) |
| 2383 | { |
| 2384 | CDRStyle tmpCharStyle; |
| 2385 | unsigned fontRecId = iter->second.fontRecId; |
| 2386 | if (fontRecId) |
| 2387 | { |
| 2388 | std::map<unsigned, unsigned short>::const_iterator iterFontId = fontIds.find(fontRecId); |
| 2389 | if (iterFontId != fontIds.end()) |
| 2390 | { |
| 2391 | std::map<unsigned, CDRFont>::const_iterator iterFonts = m_fonts.find(iterFontId->second); |
| 2392 | if (iterFonts != m_fonts.end()) |
| 2393 | { |
| 2394 | tmpCharStyle.m_fontName = iterFonts->second.m_name; |
| 2395 | tmpCharStyle.m_charSet = iterFonts->second.m_encoding; |
| 2396 | } |
| 2397 | } |
| 2398 | std::map<unsigned, unsigned short>::const_iterator iterCharSet = fontEncodings.find(fontRecId); |
| 2399 | if (iterCharSet != fontEncodings.end()) |
| 2400 | if (iterCharSet->second) |
| 2401 | tmpCharStyle.m_charSet = iterCharSet->second; |
| 2402 | std::map<unsigned, double>::const_iterator iterFontSize = fontSizes.find(fontRecId); |
| 2403 | if (iterFontSize != fontSizes.end()) |
| 2404 | tmpCharStyle.m_fontSize = iterFontSize->second; |
| 2405 | } |
| 2406 | unsigned alignId = iter->second.alignId; |
| 2407 | if (alignId) |
| 2408 | { |
| 2409 | std::map<unsigned, unsigned>::const_iterator iterAlign = aligns.find(alignId); |
| 2410 | if (iterAlign != aligns.end()) |
| 2411 | tmpCharStyle.m_align = iterAlign->second; |
| 2412 | } |
| 2413 | unsigned indentId = iter->second.indentId; |
| 2414 | if (indentId) |
| 2415 | { |
| 2416 | std::map<unsigned, double>::const_iterator iterRight = rightIndents.find(indentId); |
| 2417 | if (iterRight != rightIndents.end()) |
| 2418 | tmpCharStyle.m_rightIndent = iterRight->second; |
| 2419 | std::map<unsigned, double>::const_iterator iterFirst = firstIndents.find(indentId); |
| 2420 | if (iterFirst != firstIndents.end()) |
| 2421 | tmpCharStyle.m_firstIndent = iterFirst->second; |
| 2422 | std::map<unsigned, double>::const_iterator iterLeft = leftIndents.find(indentId); |
| 2423 | if (iterLeft != leftIndents.end()) |
| 2424 | tmpCharStyle.m_leftIndent = iterLeft->second; |
| 2425 | } |
| 2426 | unsigned fillId = iter->second.fillId; |
| 2427 | if (fillId) |
| 2428 | { |
| 2429 | std::map<unsigned, unsigned>::const_iterator iterFillId = fillIds.find(fillId); |
| 2430 | if (iterFillId != fillIds.end()) |
| 2431 | { |
| 2432 | std::map<unsigned, CDRFillStyle>::const_iterator iterFillStyle = m_fillStyles.find(iterFillId->second); |
| 2433 | if (iterFillStyle != m_fillStyles.end()) |
| 2434 | tmpCharStyle.m_fillStyle = iterFillStyle->second; |
| 2435 | } |
| 2436 | } |
| 2437 | unsigned outlId = iter->second.outlId; |
| 2438 | if (outlId) |
| 2439 | { |
| 2440 | std::map<unsigned, unsigned>::const_iterator iterOutlId = outlIds.find(outlId); |
| 2441 | if (iterOutlId != outlIds.end()) |
| 2442 | { |
| 2443 | std::map<unsigned, CDRLineStyle>::const_iterator iterLineStyle = m_lineStyles.find(iterOutlId->second); |
| 2444 | if (iterLineStyle != m_lineStyles.end()) |
| 2445 | tmpCharStyle.m_lineStyle = iterLineStyle->second; |
| 2446 | } |
| 2447 | } |
| 2448 | unsigned parentId = iter->second.parentId; |
| 2449 | if (parentId) |
| 2450 | tmpCharStyle.m_parentId = parentId; |
| 2451 | m_collector->collectStld(iter->first, tmpCharStyle); |
| 2452 | } |
| 2453 | } |
| 2454 | catch (libcdr::EndOfStreamException &) |
| 2455 | { |
| 2456 | if (m_version == 800) |
| 2457 | { |
| 2458 | CDR_DEBUG_MSG(("Catching EndOfStreamException and trying to parse as version 801\n")); |
| 2459 | m_version = 801; |
| 2460 | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
| 2461 | readStlt(input, length); |
| 2462 | return; |
| 2463 | } |
| 2464 | else |
| 2465 | { |
| 2466 | CDR_DEBUG_MSG(("Rethrowing EndOfStreamException\n")); |
| 2467 | throw libcdr::EndOfStreamException(); |
| 2468 | } |
| 2469 | } |
| 2470 | #ifndef DEBUG |
| 2471 | } |
| 2472 | catch (...) |
| 2473 | { |
| 2474 | m_version = version; |
| 2475 | } |
| 2476 | #endif |
| 2477 | } |
| 2478 | |
| 2479 | void libcdr::CDRParser::readTxsm(librevenge::RVNGInputStream *input, unsigned length) |
| 2480 | { |
| 2481 | #ifndef DEBUG |
| 2482 | try |
| 2483 | { |
| 2484 | #endif |
| 2485 | if (m_version < 500) |
| 2486 | return; |
| 2487 | if (!_redirectX6Chunk(&input, length)) |
| 2488 | throw GenericException(); |
| 2489 | if (m_version < 600) |
| 2490 | return readTxsm5(input); |
| 2491 | if (m_version < 700) |
| 2492 | return readTxsm6(input); |
| 2493 | if (m_version >= 1600) |
| 2494 | return readTxsm16(input); |
| 2495 | |
| 2496 | unsigned frameFlag = readU32(input); |
| 2497 | input->seek(0x20, librevenge::RVNG_SEEK_CUR); |
| 2498 | if (m_version >= 1500) |
| 2499 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2500 | |
| 2501 | if (m_version <= 700) |
| 2502 | { |
| 2503 | unsigned textOnPath = readU32(input); |
| 2504 | |
| 2505 | if (textOnPath == 1) |
| 2506 | { |
| 2507 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var1 |
| 2508 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var3 |
| 2509 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Offset |
| 2510 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var4 |
| 2511 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Distance |
| 2512 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var5 |
| 2513 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var6 |
| 2514 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var7 |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | unsigned numFrames = readU32(input); |
| 2519 | unsigned textId = 0; |
| 2520 | for (unsigned i=0; i < numFrames; ++i) |
| 2521 | { |
| 2522 | unsigned frameId = readU32(input); |
| 2523 | textId = frameId; |
| 2524 | input->seek(48, librevenge::RVNG_SEEK_CUR); |
| 2525 | if (m_version > 700) |
| 2526 | { |
| 2527 | unsigned textOnPath = readU32(input); |
| 2528 | if (textOnPath == 1) |
| 2529 | { |
| 2530 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var1 |
| 2531 | if (m_version > 1200) |
| 2532 | { |
| 2533 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Orientation |
| 2534 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var2 |
| 2535 | } |
| 2536 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var3 |
| 2537 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Offset |
| 2538 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var4 |
| 2539 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Distance |
| 2540 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var5 |
| 2541 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Mirror Vert |
| 2542 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Mirror Hor |
| 2543 | if (m_version >= 1500) |
| 2544 | { |
| 2545 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var6 |
| 2546 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var7 |
| 2547 | } |
| 2548 | } |
| 2549 | else if (m_version >= 1500) |
| 2550 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2551 | } |
| 2552 | |
| 2553 | if (!frameFlag) |
| 2554 | { |
| 2555 | if (m_version >= 1500) |
| 2556 | input->seek(40, librevenge::RVNG_SEEK_CUR); |
| 2557 | else if (m_version >= 1400) |
| 2558 | input->seek(36, librevenge::RVNG_SEEK_CUR); |
| 2559 | else if (m_version > 800) |
| 2560 | input->seek(34, librevenge::RVNG_SEEK_CUR); |
| 2561 | else if (m_version >= 800) |
| 2562 | input->seek(32, librevenge::RVNG_SEEK_CUR); |
| 2563 | else if (m_version >= 700) |
| 2564 | input->seek(36, librevenge::RVNG_SEEK_CUR); // !!! txt-on-path is before frame, hence things are rearranged |
| 2565 | } |
| 2566 | else if (m_version >= 1500) |
| 2567 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2568 | } |
| 2569 | |
| 2570 | unsigned numPara = readU32(input); |
| 2571 | |
| 2572 | for (unsigned j = 0; j < numPara; ++j) |
| 2573 | { |
| 2574 | unsigned stlId = readU32(input); |
| 2575 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2576 | if (m_version > 1200 && frameFlag) |
| 2577 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2578 | |
| 2579 | unsigned numStyles = readU32(input); |
| 2580 | std::map<unsigned, CDRStyle> styles; |
| 2581 | for (unsigned i = 0; i < numStyles; ++i) |
| 2582 | { |
| 2583 | readU16(input); // num chars using this style |
| 2584 | unsigned char fl2 = readU8(input); |
| 2585 | unsigned char fl3 = 0; |
| 2586 | if (m_version >= 800) |
| 2587 | fl3 = readU8(input); |
| 2588 | |
| 2589 | CDRStyle style; |
| 2590 | // Read more information depending on the flags |
| 2591 | if (fl2&1) // Font |
| 2592 | { |
| 2593 | unsigned short fontId = readU16(input); |
| 2594 | std::map<unsigned, CDRFont>::const_iterator iterFont = m_fonts.find(fontId); |
| 2595 | if (iterFont != m_fonts.end()) |
| 2596 | { |
| 2597 | style.m_fontName = iterFont->second.m_name; |
| 2598 | style.m_charSet = iterFont->second.m_encoding; |
| 2599 | } |
| 2600 | unsigned short charSet = readU16(input); |
| 2601 | if (charSet) |
| 2602 | style.m_charSet = charSet; |
| 2603 | } |
| 2604 | if (fl2&2) // Bold/Italic, etc. |
| 2605 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2606 | if (fl2&4) // Font Size |
| 2607 | style.m_fontSize = readCoordinate(input); |
| 2608 | if (fl2&8) // assumption |
| 2609 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2610 | if (fl2&0x10) // Offset X |
| 2611 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2612 | if (fl2&0x20) // Offset Y |
| 2613 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2614 | if (fl2&0x40) // Font Colour |
| 2615 | { |
| 2616 | unsigned fillId = readU32(input); |
| 2617 | std::map<unsigned, CDRFillStyle>::const_iterator iter = m_fillStyles.find(fillId); |
| 2618 | if (iter != m_fillStyles.end()) |
| 2619 | style.m_fillStyle = iter->second; |
| 2620 | if (m_version >= 1300) |
| 2621 | input->seek(48, librevenge::RVNG_SEEK_CUR); |
| 2622 | } |
| 2623 | if (fl2&0x80) // Font Outl ColourStld |
| 2624 | { |
| 2625 | unsigned outlId = readU32(input); |
| 2626 | std::map<unsigned, CDRLineStyle>::const_iterator iter = m_lineStyles.find(outlId); |
| 2627 | if (iter != m_lineStyles.end()) |
| 2628 | style.m_lineStyle = iter->second; |
| 2629 | } |
| 2630 | if (fl3&8) // Encoding |
| 2631 | { |
| 2632 | if (m_version >= 1300) |
| 2633 | { |
| 2634 | unsigned tlen = readU32(input); |
| 2635 | input->seek(tlen*2, librevenge::RVNG_SEEK_CUR); |
| 2636 | } |
| 2637 | else |
| 2638 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2639 | } |
| 2640 | if (fl3&0x20) // Something |
| 2641 | { |
| 2642 | unsigned flag = readU8(input); |
| 2643 | input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 2644 | if (flag) |
| 2645 | { |
| 2646 | input->seek(4, librevenge::RVNG_SEEK_CUR); // ftil fild Id |
| 2647 | if (m_version >= 1500) |
| 2648 | input->seek(48, librevenge::RVNG_SEEK_CUR); |
| 2649 | } |
| 2650 | } |
| 2651 | styles[2*i] = style; |
| 2652 | } |
| 2653 | unsigned long numChars = readU32(input); |
| 2654 | const unsigned charSize = m_version >= 1200 ? 8 : 4; |
| 2655 | if (numChars > getRemainingLength(input) / charSize) |
| 2656 | numChars = getRemainingLength(input) / charSize; |
| 2657 | std::vector<unsigned char> charDescriptions(numChars); |
| 2658 | for (unsigned long i = 0; i < numChars; ++i) |
| 2659 | { |
| 2660 | unsigned tmpCharDescription = 0; |
| 2661 | if (m_version >= 1200) |
| 2662 | tmpCharDescription = readU64(input) & 0xffffffff; |
| 2663 | else |
| 2664 | tmpCharDescription = readU32(input); |
| 2665 | charDescriptions[i] = (unsigned char)((tmpCharDescription >> 16) | (tmpCharDescription & 0x01)); |
| 2666 | } |
| 2667 | unsigned long numBytes = numChars; |
| 2668 | if (m_version >= 1200) |
| 2669 | numBytes = readU32(input); |
| 2670 | unsigned long numBytesRead = 0; |
| 2671 | const unsigned char *buffer = input->read(numBytes, numBytesRead); |
| 2672 | if (numBytesRead != numBytes) |
| 2673 | throw GenericException(); |
| 2674 | std::vector<unsigned char> textData(numBytesRead); |
| 2675 | if (numBytesRead) |
| 2676 | memcpy(&textData[0], buffer, numBytesRead); |
| 2677 | input->seek(1, librevenge::RVNG_SEEK_CUR); //skip the 0 ending character |
| 2678 | |
| 2679 | if (!textData.empty() || !styles.empty()) |
| 2680 | { |
| 2681 | m_collector->collectText(textId, stlId, textData, charDescriptions, styles); |
| 2682 | } |
| 2683 | } |
| 2684 | #ifndef DEBUG |
| 2685 | } |
| 2686 | catch (...) |
| 2687 | { |
| 2688 | } |
| 2689 | #endif |
| 2690 | } |
| 2691 | |
| 2692 | void libcdr::CDRParser::readTxsm16(librevenge::RVNGInputStream *input) |
| 2693 | { |
| 2694 | #ifndef DEBUG |
| 2695 | try |
| 2696 | { |
| 2697 | #endif |
| 2698 | unsigned frameFlag = readU32(input); |
| 2699 | input->seek(37, librevenge::RVNG_SEEK_CUR); |
| 2700 | |
| 2701 | unsigned numFrames = readU32(input); |
| 2702 | |
| 2703 | unsigned textId = 0; |
| 2704 | for (unsigned i = 0; i < numFrames; ++i) |
| 2705 | { |
| 2706 | unsigned frameId = readU32(input); |
| 2707 | textId = frameId; |
| 2708 | input->seek(48, librevenge::RVNG_SEEK_CUR); |
| 2709 | unsigned textOnPath = readU32(input); |
| 2710 | if (textOnPath == 1) |
| 2711 | { |
| 2712 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var1 |
| 2713 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Orientation |
| 2714 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var2 |
| 2715 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var3 |
| 2716 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Offset |
| 2717 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var4 |
| 2718 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Distance |
| 2719 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var5 |
| 2720 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Mirror Vert |
| 2721 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Mirror Hor |
| 2722 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var6 |
| 2723 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var7 |
| 2724 | } |
| 2725 | else |
| 2726 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 2727 | |
| 2728 | if (!frameFlag) |
| 2729 | { |
| 2730 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 2731 | unsigned tlen = readU32(input); |
| 2732 | if (m_version > 1600) |
| 2733 | input->seek(tlen, librevenge::RVNG_SEEK_CUR); |
| 2734 | else |
| 2735 | input->seek(tlen*2, librevenge::RVNG_SEEK_CUR); |
| 2736 | |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | unsigned numPara = readU32(input); |
| 2741 | |
| 2742 | for (unsigned j = 0; j < numPara; ++j) |
| 2743 | { |
| 2744 | |
| 2745 | unsigned stlId = readU32(input); |
| 2746 | |
| 2747 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2748 | if (frameFlag) |
| 2749 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2750 | |
| 2751 | unsigned styleLen = readU32(input); |
| 2752 | if (m_version < 1700) |
| 2753 | styleLen *= 2; |
| 2754 | CDRStyle defaultStyle; |
| 2755 | _readX6StyleString(input, styleLen, defaultStyle); |
| 2756 | |
| 2757 | unsigned numRecords = readU32(input); |
| 2758 | |
| 2759 | unsigned i = 0; |
| 2760 | std::map<unsigned, CDRStyle> styles; |
| 2761 | for (i=0; i<numRecords && getRemainingLength(input) >= 17; ++i) |
| 2762 | { |
| 2763 | styles[i*2] = defaultStyle; |
| 2764 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2765 | unsigned short stFlag1 = readU16(input); |
| 2766 | unsigned short stFlag2 = readU16(input); |
| 2767 | if (stFlag2 & 0x04) |
| 2768 | { |
| 2769 | // encoding |
| 2770 | unsigned encLen = readU32(input); |
| 2771 | encLen *= 2; |
| 2772 | input->seek(encLen, librevenge::RVNG_SEEK_CUR); |
| 2773 | } |
| 2774 | if (stFlag1 || stFlag2 & 0x4) |
| 2775 | { |
| 2776 | unsigned jsonLen = readU32(input); |
| 2777 | if (m_version < 1700) |
| 2778 | jsonLen *= 2; |
| 2779 | _readX6StyleString(input, jsonLen, styles[i*2]); |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | unsigned numChars = readU32(input); |
| 2784 | if (numChars > getRemainingLength(input) / 8) |
| 2785 | numChars = getRemainingLength(input) / 8; |
| 2786 | std::vector<unsigned char> charDescriptions(numChars); |
| 2787 | for (i=0; i<numChars; ++i) |
| 2788 | { |
| 2789 | unsigned tmpCharDescription = 0; |
| 2790 | tmpCharDescription = readU64(input) & 0xffffffff; |
| 2791 | charDescriptions[i] = (unsigned char)((tmpCharDescription >> 16) | (tmpCharDescription & 0x01)); |
| 2792 | } |
| 2793 | unsigned numBytes = readU32(input); |
| 2794 | unsigned long numBytesRead = 0; |
| 2795 | const unsigned char *buffer = input->read(numBytes, numBytesRead); |
| 2796 | if (numBytesRead != numBytes) |
| 2797 | throw GenericException(); |
| 2798 | std::vector<unsigned char> textData(numBytesRead); |
| 2799 | if (numBytesRead) |
| 2800 | memcpy(&textData[0], buffer, numBytesRead); |
| 2801 | input->seek(1, librevenge::RVNG_SEEK_CUR); //skip the 0 ending character |
| 2802 | |
| 2803 | |
| 2804 | if (!textData.empty()) |
| 2805 | m_collector->collectText(textId, stlId, textData, charDescriptions, styles); |
| 2806 | } |
| 2807 | #ifndef DEBUG |
| 2808 | } |
| 2809 | catch (...) |
| 2810 | { |
| 2811 | } |
| 2812 | #endif |
| 2813 | } |
| 2814 | |
| 2815 | void libcdr::CDRParser::readTxsm6(librevenge::RVNGInputStream *input) |
| 2816 | { |
| 2817 | unsigned frameFlag = readU32(input); |
| 2818 | input->seek(0x18, librevenge::RVNG_SEEK_CUR); |
| 2819 | unsigned textOnPath = readU32(input); |
| 2820 | |
| 2821 | if (textOnPath == 1) |
| 2822 | { |
| 2823 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var1 |
| 2824 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var3 |
| 2825 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Offset |
| 2826 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var4 |
| 2827 | input->seek(4, librevenge::RVNG_SEEK_CUR); // Distance |
| 2828 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var5 |
| 2829 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var6 |
| 2830 | input->seek(4, librevenge::RVNG_SEEK_CUR); // var7 |
| 2831 | } |
| 2832 | |
| 2833 | unsigned numFrames = readU32(input); |
| 2834 | unsigned textId = 0; |
| 2835 | for (unsigned j=0; j<numFrames; ++j) |
| 2836 | { |
| 2837 | textId = readU32(input); // Frame Id |
| 2838 | input->seek(48, librevenge::RVNG_SEEK_CUR); // Trafo 6*8 bytes |
| 2839 | if (!frameFlag) |
| 2840 | input->seek(8, librevenge::RVNG_SEEK_CUR); // Maybe flags |
| 2841 | } |
| 2842 | unsigned numPara = readU32(input); |
| 2843 | for (unsigned j=0; j<numPara; ++j) |
| 2844 | { |
| 2845 | unsigned stlId = readU32(input); |
| 2846 | unsigned numSt = readU32(input); |
| 2847 | std::map<unsigned, CDRStyle> styles; |
| 2848 | for (unsigned i=0; i<numSt; ++i) |
| 2849 | { |
| 2850 | CDRStyle style; |
| 2851 | |
| 2852 | unsigned char flag = readU8(input); |
| 2853 | input->seek(3, librevenge::RVNG_SEEK_CUR); |
| 2854 | if (flag&0x01) |
| 2855 | { |
| 2856 | unsigned short fontId = readU16(input); |
| 2857 | std::map<unsigned, CDRFont>::const_iterator iterFont = m_fonts.find(fontId); |
| 2858 | if (iterFont != m_fonts.end()) |
| 2859 | { |
| 2860 | style.m_fontName = iterFont->second.m_name; |
| 2861 | style.m_charSet = iterFont->second.m_encoding; |
| 2862 | } |
| 2863 | unsigned short charSet = readU16(input); |
| 2864 | if (charSet) |
| 2865 | style.m_charSet = charSet; |
| 2866 | } |
| 2867 | else |
| 2868 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2869 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2870 | if (flag&0x04) |
| 2871 | style.m_fontSize = readCoordinate(input); |
| 2872 | else |
| 2873 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2874 | input->seek(44, librevenge::RVNG_SEEK_CUR); |
| 2875 | if (flag&0x10) |
| 2876 | { |
| 2877 | unsigned fillId = readU32(input); |
| 2878 | std::map<unsigned, CDRFillStyle>::const_iterator iter = m_fillStyles.find(fillId); |
| 2879 | if (iter != m_fillStyles.end()) |
| 2880 | style.m_fillStyle = iter->second; |
| 2881 | } |
| 2882 | if (flag&0x20) |
| 2883 | { |
| 2884 | unsigned outlId = readU32(input); |
| 2885 | std::map<unsigned, CDRLineStyle>::const_iterator iter = m_lineStyles.find(outlId); |
| 2886 | if (iter != m_lineStyles.end()) |
| 2887 | style.m_lineStyle = iter->second; |
| 2888 | } |
| 2889 | styles[2*i] = style; |
| 2890 | } |
| 2891 | unsigned numChars = readU32(input); |
| 2892 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2893 | if (numChars > getRemainingLength(input) / 12) |
| 2894 | numChars = getRemainingLength(input) / 12; |
| 2895 | std::vector<unsigned char> textData; |
| 2896 | std::vector<unsigned char> charDescriptions; |
| 2897 | textData.reserve(numChars); |
| 2898 | charDescriptions.reserve(numChars); |
| 2899 | for (unsigned i=0; i<numChars; ++i) |
| 2900 | { |
| 2901 | textData.push_back(readU8(input)); |
| 2902 | input->seek(5, librevenge::RVNG_SEEK_CUR); |
| 2903 | charDescriptions.push_back(readU8(input) << 1); |
| 2904 | input->seek(5, librevenge::RVNG_SEEK_CUR); |
| 2905 | } |
| 2906 | if (!textData.empty()) |
| 2907 | m_collector->collectText(textId, stlId, textData, charDescriptions, styles); |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | void libcdr::CDRParser::readTxsm5(librevenge::RVNGInputStream *input) |
| 2912 | { |
| 2913 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2914 | unsigned numFrames = readU16(input); |
| 2915 | unsigned textId = 0; |
| 2916 | for (unsigned j=0; j<numFrames; ++j) |
| 2917 | { |
| 2918 | textId = readU16(input); // Bogus, not really needed and it is frame Id anyway |
| 2919 | /* Possibly flags that might indicate frame styles, and the structure might |
| 2920 | be of variable length, but for the while we have seen only zeros in those two |
| 2921 | bytes in the documents we have. */ |
| 2922 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2923 | } |
| 2924 | unsigned numPara = readU16(input); |
| 2925 | for (unsigned j=0; j<numPara; ++j) |
| 2926 | { |
| 2927 | unsigned stlId = readU16(input); |
| 2928 | unsigned numSt = readU16(input); |
| 2929 | std::map<unsigned, CDRStyle> styles; |
| 2930 | for (unsigned i= 0; i<numSt /* && getRemainingLength(input) >= 34 */; ++i) |
| 2931 | { |
| 2932 | CDRStyle style; |
| 2933 | unsigned char flag = readU8(input); |
| 2934 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2935 | if (flag&0x01) |
| 2936 | { |
| 2937 | unsigned short fontId = readU8(input); |
| 2938 | std::map<unsigned, CDRFont>::const_iterator iterFont = m_fonts.find(fontId); |
| 2939 | if (iterFont != m_fonts.end()) |
| 2940 | { |
| 2941 | style.m_fontName = iterFont->second.m_name; |
| 2942 | style.m_charSet = iterFont->second.m_encoding; |
| 2943 | } |
| 2944 | unsigned short charSet = readU8(input); |
| 2945 | if (charSet) |
| 2946 | style.m_charSet = charSet; |
| 2947 | } |
| 2948 | else |
| 2949 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2950 | input->seek(6, librevenge::RVNG_SEEK_CUR); |
| 2951 | if (flag&0x04) |
| 2952 | style.m_fontSize = readCoordinate(input); |
| 2953 | else |
| 2954 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2955 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 2956 | if (flag&0x10) |
| 2957 | { |
| 2958 | unsigned fillId = readU32(input); |
| 2959 | std::map<unsigned, CDRFillStyle>::const_iterator iter = m_fillStyles.find(fillId); |
| 2960 | if (iter != m_fillStyles.end()) |
| 2961 | style.m_fillStyle = iter->second; |
| 2962 | } |
| 2963 | else |
| 2964 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2965 | if (flag&0x20) |
| 2966 | { |
| 2967 | unsigned outlId = readU32(input); |
| 2968 | std::map<unsigned, CDRLineStyle>::const_iterator iter = m_lineStyles.find(outlId); |
| 2969 | if (iter != m_lineStyles.end()) |
| 2970 | style.m_lineStyle = iter->second; |
| 2971 | } |
| 2972 | else |
| 2973 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2974 | input->seek(14, librevenge::RVNG_SEEK_CUR); |
| 2975 | styles[2*i] = style; |
| 2976 | } |
| 2977 | unsigned long numChars = readU16(input); |
| 2978 | if (numChars > getRemainingLength(input) / 8) |
| 2979 | numChars = getRemainingLength(input) / 8; |
| 2980 | std::vector<unsigned char> textData; |
| 2981 | std::vector<unsigned char> charDescriptions; |
| 2982 | textData.reserve(numChars); |
| 2983 | charDescriptions.reserve(numChars); |
| 2984 | for (unsigned long i=0; i<numChars; ++i) |
| 2985 | { |
| 2986 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 2987 | textData.push_back(readU8(input)); |
| 2988 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 2989 | charDescriptions.push_back((unsigned char)((readU16(input) >> 3) & 0xff)); |
| 2990 | } |
| 2991 | if (!textData.empty()) |
| 2992 | m_collector->collectText(textId, stlId, textData, charDescriptions, styles); |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | void libcdr::CDRParser::readUdta(librevenge::RVNGInputStream *input) |
| 2997 | { |
| 2998 | CDR_DEBUG_MSG(("libcdr::CDRParser::readUdta\n")); |
| 2999 | if (m_version >= 1300 && m_version < 1600) |
| 3000 | { |
| 3001 | input->seek(6, librevenge::RVNG_SEEK_CUR); // Not sure what these bytes are for. Field id? |
| 3002 | std::vector<unsigned char> name; |
| 3003 | unsigned short c; |
| 3004 | for (;;) |
| 3005 | { |
| 3006 | if ((c = readU16(input)) == 0) break; |
| 3007 | name.push_back((unsigned char)(c & 0xff)); |
| 3008 | name.push_back((unsigned char)(c >> 8)); |
| 3009 | } |
| 3010 | librevenge::RVNGString fieldName; |
| 3011 | appendCharacters(fieldName, name); |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | void libcdr::CDRParser::readStyd(librevenge::RVNGInputStream *input) |
| 3016 | { |
| 3017 | CDR_DEBUG_MSG(("libcdr::CDRParser::readStyd\n")); |
| 3018 | if (m_version >= 700) |
| 3019 | { |
| 3020 | CDR_DEBUG_MSG(("Styd should not be present in this file version\n")); |
| 3021 | return; |
| 3022 | } |
| 3023 | unsigned styleId = readU16(input); |
| 3024 | long startPosition = input->tell(); |
| 3025 | const unsigned long maxLength = getLength(input); |
| 3026 | if (startPosition >= long(maxLength)) |
| 3027 | return; |
| 3028 | unsigned chunkLength = readUnsigned(input); |
| 3029 | if ((chunkLength > maxLength) || (long(maxLength - chunkLength) < startPosition)) |
| 3030 | chunkLength = unsigned(maxLength - static_cast<unsigned long>(startPosition)); // sanitize length |
| 3031 | unsigned numOfArgs = readUnsigned(input); |
| 3032 | unsigned startOfArgs = readUnsigned(input); |
| 3033 | if (startOfArgs >= chunkLength) |
| 3034 | return; |
| 3035 | unsigned startOfArgTypes = readUnsigned(input); |
| 3036 | if (startOfArgTypes >= chunkLength) |
| 3037 | return; |
| 3038 | if (numOfArgs > (chunkLength - startOfArgs) / 4) // avoid extra big allocation in case of a broken file |
| 3039 | numOfArgs = (chunkLength - startOfArgs) / 4; |
| 3040 | CDRStyle style; |
| 3041 | style.m_parentId = readUnsigned(input); |
| 3042 | std::vector<unsigned> argOffsets(numOfArgs, 0); |
| 3043 | std::vector<unsigned> argTypes(numOfArgs, 0); |
| 3044 | size_t i = 0; |
| 3045 | input->seek(startPosition+startOfArgs, librevenge::RVNG_SEEK_SET); |
| 3046 | while (i<numOfArgs) |
| 3047 | argOffsets[i++] = readUnsigned(input); |
| 3048 | input->seek(startPosition+startOfArgTypes, librevenge::RVNG_SEEK_SET); |
| 3049 | while (i>0) |
| 3050 | argTypes[--i] = readUnsigned(input); |
| 3051 | |
| 3052 | for (i=0; i < argTypes.size(); i++) |
| 3053 | { |
| 3054 | input->seek(startPosition+argOffsets[i], librevenge::RVNG_SEEK_SET); |
| 3055 | CDR_DEBUG_MSG(("Styd: argument type: 0x%x\n", argTypes[i])); |
| 3056 | switch (argTypes[i]) |
| 3057 | { |
| 3058 | case STYD_NAME: |
| 3059 | break; |
| 3060 | case STYD_FILL_ID: |
| 3061 | { |
| 3062 | unsigned fillId = readU32(input); |
| 3063 | std::map<unsigned, CDRFillStyle>::const_iterator iter = m_fillStyles.find(fillId); |
| 3064 | if (iter != m_fillStyles.end()) |
| 3065 | style.m_fillStyle = iter->second; |
| 3066 | break; |
| 3067 | } |
| 3068 | case STYD_OUTL_ID: |
| 3069 | { |
| 3070 | unsigned outlId = readU32(input); |
| 3071 | std::map<unsigned, CDRLineStyle>::const_iterator iter = m_lineStyles.find(outlId); |
| 3072 | if (iter != m_lineStyles.end()) |
| 3073 | style.m_lineStyle = iter->second; |
| 3074 | break; |
| 3075 | } |
| 3076 | case STYD_FONTS: |
| 3077 | { |
| 3078 | if (m_version >= 600) |
| 3079 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 3080 | unsigned short fontId = readUnsignedShort(input); |
| 3081 | std::map<unsigned, CDRFont>::const_iterator iterFont = m_fonts.find(fontId); |
| 3082 | if (iterFont != m_fonts.end()) |
| 3083 | { |
| 3084 | style.m_fontName = iterFont->second.m_name; |
| 3085 | style.m_charSet = iterFont->second.m_encoding; |
| 3086 | } |
| 3087 | unsigned short charSet = readUnsignedShort(input); |
| 3088 | if (charSet) |
| 3089 | style.m_charSet = charSet; |
| 3090 | if (m_version >= 600) |
| 3091 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 3092 | style.m_fontSize = readCoordinate(input); |
| 3093 | break; |
| 3094 | } |
| 3095 | case STYD_ALIGN: |
| 3096 | style.m_align = readUnsigned(input); |
| 3097 | break; |
| 3098 | case STYD_BULLETS: |
| 3099 | break; |
| 3100 | case STYD_INTERVALS: |
| 3101 | break; |
| 3102 | case STYD_TABS: |
| 3103 | break; |
| 3104 | case STYD_IDENTS: |
| 3105 | break; |
| 3106 | case STYD_HYPHENS: |
| 3107 | break; |
| 3108 | case STYD_SET5S: |
| 3109 | break; |
| 3110 | case STYD_DROPCAPS: |
| 3111 | break; |
| 3112 | default: |
| 3113 | break; |
| 3114 | } |
| 3115 | } |
| 3116 | input->seek(startPosition+chunkLength, librevenge::RVNG_SEEK_SET); |
| 3117 | m_collector->collectStld(styleId, style); |
| 3118 | } |
| 3119 | |
| 3120 | void libcdr::CDRParser::readArtisticText(librevenge::RVNGInputStream *input) |
| 3121 | { |
| 3122 | double x = readCoordinate(input); |
| 3123 | double y = readCoordinate(input); |
| 3124 | m_collector->collectArtisticText(x, y); |
| 3125 | } |
| 3126 | |
| 3127 | void libcdr::CDRParser::readParagraphText(librevenge::RVNGInputStream *input) |
| 3128 | { |
| 3129 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 3130 | double width = readCoordinate(input); |
| 3131 | double height = readCoordinate(input); |
| 3132 | m_collector->collectParagraphText(0.0, 0.0, width, height); |
| 3133 | } |
| 3134 | |
| 3135 | void libcdr::CDRParser::_readX6StyleString(librevenge::RVNGInputStream *input, unsigned long length, libcdr::CDRStyle &style) |
| 3136 | { |
| 3137 | if (length > getRemainingLength(input)) |
| 3138 | { |
| 3139 | length = getRemainingLength(input); |
| 3140 | if ((m_version < 1700) && (length & 1)) |
| 3141 | --length; // the length must be even |
| 3142 | } |
| 3143 | |
| 3144 | std::vector<unsigned char> styleBuffer(length); |
| 3145 | unsigned long numBytesRead = 0; |
| 3146 | const unsigned char *tmpBuffer = input->read(length, numBytesRead); |
| 3147 | if (numBytesRead) |
| 3148 | memcpy(&styleBuffer[0], tmpBuffer, numBytesRead); |
| 3149 | librevenge::RVNGString styleString; |
| 3150 | if (m_version >= 1700) |
| 3151 | libcdr::appendUTF8Characters(styleString, styleBuffer); |
| 3152 | else |
| 3153 | libcdr::appendCharacters(styleString, styleBuffer); |
| 3154 | CDR_DEBUG_MSG(("CDRParser::_readX6StyleString - styleString = \"%s\"\n", styleString.cstr())); |
| 3155 | |
| 3156 | boost::property_tree::ptree pt; |
| 3157 | #ifndef DEBUG |
| 3158 | try |
| 3159 | #endif |
| 3160 | { |
| 3161 | std::stringstream ss; |
| 3162 | ss << styleString.cstr(); |
| 3163 | boost::property_tree::read_json(ss, pt); |
| 3164 | } |
| 3165 | #ifndef DEBUG |
| 3166 | catch (...) |
| 3167 | { |
| 3168 | return; |
| 3169 | } |
| 3170 | #endif |
| 3171 | |
| 3172 | if (pt.count("character")) |
| 3173 | { |
| 3174 | boost::optional<std::string> fontName = pt.get_optional<std::string>("character.latin.font"); |
| 3175 | if (!!fontName) |
| 3176 | style.m_fontName = fontName.get().c_str(); |
| 3177 | unsigned short encoding = (unsigned short)(pt.get("character.latin.charset", 0)); |
| 3178 | if (encoding || style.m_charSet == (unsigned short)-1) |
| 3179 | style.m_charSet = encoding; |
| 3180 | processNameForEncoding(style.m_fontName, style.m_charSet); |
| 3181 | boost::optional<unsigned> fontSize = pt.get_optional<unsigned>("character.latin.size"); |
| 3182 | if (!!fontSize) |
| 3183 | style.m_fontSize = (double)fontSize.get() / 254000.0; |
| 3184 | |
| 3185 | if (!!pt.get_child_optional("character.outline")) |
| 3186 | { |
| 3187 | style.m_lineStyle.lineType = 0; |
| 3188 | boost::optional<unsigned> lineWidth = pt.get_optional<unsigned>("character.outline.width"); |
| 3189 | if (!!lineWidth) |
| 3190 | style.m_lineStyle.lineWidth = (double)lineWidth.get() / 254000.0; |
| 3191 | boost::optional<std::string> color = pt.get_optional<std::string>("character.outline.color"); |
| 3192 | if (!!color) |
| 3193 | { |
| 3194 | double opacity = 1.0; |
| 3195 | parseColourString(color.get().c_str(), style.m_lineStyle.color, opacity); |
| 3196 | _resolveColorPalette(style.m_lineStyle.color); |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | if (!!pt.get_child_optional("character.fill")) |
| 3201 | { |
| 3202 | boost::optional<unsigned short> type = pt.get_optional<unsigned short>("character.fill.type"); |
| 3203 | if (!!type) |
| 3204 | style.m_fillStyle.fillType = type.get(); |
| 3205 | boost::optional<std::string> color1 = pt.get_optional<std::string>("character.fill.primaryColor"); |
| 3206 | if (!!color1) |
| 3207 | { |
| 3208 | double opacity = 1.0; |
| 3209 | parseColourString(color1.get().c_str(), style.m_fillStyle.color1, opacity); |
| 3210 | _resolveColorPalette(style.m_fillStyle.color1); |
| 3211 | } |
| 3212 | boost::optional<std::string> color2 = pt.get_optional<std::string>("character.fill.primaryColor"); |
| 3213 | if (!!color2) |
| 3214 | { |
| 3215 | double opacity = 1.0; |
| 3216 | parseColourString(color2.get().c_str(), style.m_fillStyle.color2, opacity); |
| 3217 | _resolveColorPalette(style.m_fillStyle.color2); |
| 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | if (pt.count("paragraph")) |
| 3223 | { |
| 3224 | boost::optional<unsigned> align = pt.get_optional<unsigned>("paragraph.justify"); |
| 3225 | if (!!align) |
| 3226 | style.m_align = align.get(); |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | void libcdr::CDRParser::_skipX3Optional(librevenge::RVNGInputStream *input) |
| 3231 | { |
| 3232 | if (m_version < 1300) |
| 3233 | return; |
| 3234 | |
| 3235 | bool goOut = false; |
| 3236 | |
| 3237 | while (!goOut) |
| 3238 | { |
| 3239 | switch (readU32(input)) |
| 3240 | { |
| 3241 | case 0x640: |
| 3242 | { |
| 3243 | unsigned length = readU32(input); |
| 3244 | input->seek(length, librevenge::RVNG_SEEK_CUR); |
| 3245 | break; |
| 3246 | } |
| 3247 | case 0x514: |
| 3248 | { |
| 3249 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 3250 | break; |
| 3251 | } |
| 3252 | default: |
| 3253 | { |
| 3254 | input->seek(-4, librevenge::RVNG_SEEK_CUR); |
| 3255 | goOut = true; |
| 3256 | break; |
| 3257 | } |
| 3258 | } |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | void libcdr::CDRParser::_resolveColorPalette(libcdr::CDRColor &color) |
| 3263 | { |
| 3264 | CDR_DEBUG_MSG(("CDRParser::_resolveColorPalette --> model 0x%x -- palette 0x%x -- value 0x%x\n", color.m_colorModel, color.m_colorPalette, color.m_colorValue)); |
| 3265 | |
| 3266 | unsigned char r = 0; |
| 3267 | unsigned char g = 0; |
| 3268 | unsigned char b = 0; |
| 3269 | unsigned char c = 0; |
| 3270 | unsigned char m = 0; |
| 3271 | unsigned char y = 0; |
| 3272 | unsigned char k = 100; |
| 3273 | |
| 3274 | unsigned short ix = color.m_colorValue & 0xffff; |
| 3275 | unsigned short tint = (unsigned short)((color.m_colorValue >> 16) & 0xffff); |
| 3276 | |
| 3277 | if (color.m_colorModel == 0x19) |
| 3278 | { |
| 3279 | switch (color.m_colorPalette) |
| 3280 | { |
| 3281 | case 0x03: |
| 3282 | if (ix > 0 |
| 3283 | && ix <= sizeof(palette_19_03_C)/sizeof(palette_19_03_C[0]) |
| 3284 | && ix <= sizeof(palette_19_03_M)/sizeof(palette_19_03_M[0]) |
| 3285 | && ix <= sizeof(palette_19_03_Y)/sizeof(palette_19_03_Y[0]) |
| 3286 | && ix <= sizeof(palette_19_03_K)/sizeof(palette_19_03_K[0])) |
| 3287 | { |
| 3288 | c = palette_19_03_C[ix-1]; |
| 3289 | m = palette_19_03_M[ix-1]; |
| 3290 | y = palette_19_03_Y[ix-1]; |
| 3291 | k = palette_19_03_K[ix-1]; |
| 3292 | } |
| 3293 | break; |
| 3294 | case 0x08: |
| 3295 | if (ix > 0 |
| 3296 | && ix <= sizeof(palette_19_08_C)/sizeof(palette_19_08_C[0]) |
| 3297 | && ix <= sizeof(palette_19_08_M)/sizeof(palette_19_08_M[0]) |
| 3298 | && ix <= sizeof(palette_19_08_Y)/sizeof(palette_19_08_Y[0]) |
| 3299 | && ix <= sizeof(palette_19_08_K)/sizeof(palette_19_08_K[0])) |
| 3300 | { |
| 3301 | c = palette_19_08_C[ix-1]; |
| 3302 | m = palette_19_08_M[ix-1]; |
| 3303 | y = palette_19_08_Y[ix-1]; |
| 3304 | k = palette_19_08_K[ix-1]; |
| 3305 | } |
| 3306 | break; |
| 3307 | case 0x09: |
| 3308 | if (ix > 0 |
| 3309 | && ix <= sizeof(palette_19_09_L)/sizeof(palette_19_09_L[0]) |
| 3310 | && ix <= sizeof(palette_19_09_A)/sizeof(palette_19_09_A[0]) |
| 3311 | && ix <= sizeof(palette_19_09_B)/sizeof(palette_19_09_B[0])) |
| 3312 | { |
| 3313 | color.m_colorValue = palette_19_09_B[ix-1]; |
| 3314 | color.m_colorValue <<= 8; |
| 3315 | color.m_colorValue |= palette_19_09_A[ix-1]; |
| 3316 | color.m_colorValue <<= 8; |
| 3317 | color.m_colorValue |= palette_19_09_L[ix-1]; |
| 3318 | } |
| 3319 | break; |
| 3320 | case 0x0a: |
| 3321 | if (ix > 0 |
| 3322 | && ix <= sizeof(palette_19_0A_C)/sizeof(palette_19_0A_C[0]) |
| 3323 | && ix <= sizeof(palette_19_0A_M)/sizeof(palette_19_0A_M[0]) |
| 3324 | && ix <= sizeof(palette_19_0A_Y)/sizeof(palette_19_0A_Y[0]) |
| 3325 | && ix <= sizeof(palette_19_0A_K)/sizeof(palette_19_0A_K[0])) |
| 3326 | { |
| 3327 | c = palette_19_0A_C[ix-1]; |
| 3328 | m = palette_19_0A_M[ix-1]; |
| 3329 | y = palette_19_0A_Y[ix-1]; |
| 3330 | k = palette_19_0A_K[ix-1]; |
| 3331 | } |
| 3332 | break; |
| 3333 | case 0x0b: |
| 3334 | if (ix > 0 |
| 3335 | && ix <= sizeof(palette_19_0B_C)/sizeof(palette_19_0B_C[0]) |
| 3336 | && ix <= sizeof(palette_19_0B_M)/sizeof(palette_19_0B_M[0]) |
| 3337 | && ix <= sizeof(palette_19_0B_Y)/sizeof(palette_19_0B_Y[0]) |
| 3338 | && ix <= sizeof(palette_19_0B_K)/sizeof(palette_19_0B_K[0])) |
| 3339 | { |
| 3340 | c = palette_19_0B_C[ix-1]; |
| 3341 | m = palette_19_0B_M[ix-1]; |
| 3342 | y = palette_19_0B_Y[ix-1]; |
| 3343 | k = palette_19_0B_K[ix-1]; |
| 3344 | } |
| 3345 | break; |
| 3346 | case 0x11: |
| 3347 | if (ix > 0 |
| 3348 | && ix <= sizeof(palette_19_11_C)/sizeof(palette_19_11_C[0]) |
| 3349 | && ix <= sizeof(palette_19_11_M)/sizeof(palette_19_11_M[0]) |
| 3350 | && ix <= sizeof(palette_19_11_Y)/sizeof(palette_19_11_Y[0]) |
| 3351 | && ix <= sizeof(palette_19_11_K)/sizeof(palette_19_11_K[0])) |
| 3352 | { |
| 3353 | c = palette_19_11_C[ix-1]; |
| 3354 | m = palette_19_11_M[ix-1]; |
| 3355 | y = palette_19_11_Y[ix-1]; |
| 3356 | k = palette_19_11_K[ix-1]; |
| 3357 | } |
| 3358 | break; |
| 3359 | case 0x12: |
| 3360 | if (ix > 0 |
| 3361 | && ix <= sizeof(palette_19_12_C)/sizeof(palette_19_12_C[0]) |
| 3362 | && ix <= sizeof(palette_19_12_M)/sizeof(palette_19_12_M[0]) |
| 3363 | && ix <= sizeof(palette_19_12_Y)/sizeof(palette_19_12_Y[0]) |
| 3364 | && ix <= sizeof(palette_19_12_K)/sizeof(palette_19_12_K[0])) |
| 3365 | { |
| 3366 | c = palette_19_12_C[ix-1]; |
| 3367 | m = palette_19_12_M[ix-1]; |
| 3368 | y = palette_19_12_Y[ix-1]; |
| 3369 | k = palette_19_12_K[ix-1]; |
| 3370 | } |
| 3371 | break; |
| 3372 | case 0x14: |
| 3373 | if (ix > 0 |
| 3374 | && ix <= sizeof(palette_19_14_C)/sizeof(palette_19_14_C[0]) |
| 3375 | && ix <= sizeof(palette_19_14_M)/sizeof(palette_19_14_M[0]) |
| 3376 | && ix <= sizeof(palette_19_14_Y)/sizeof(palette_19_14_Y[0]) |
| 3377 | && ix <= sizeof(palette_19_14_K)/sizeof(palette_19_14_K[0])) |
| 3378 | { |
| 3379 | c = palette_19_14_C[ix-1]; |
| 3380 | m = palette_19_14_M[ix-1]; |
| 3381 | y = palette_19_14_Y[ix-1]; |
| 3382 | k = palette_19_14_K[ix-1]; |
| 3383 | } |
| 3384 | break; |
| 3385 | case 0x15: |
| 3386 | if (ix > 0 |
| 3387 | && ix <= sizeof(palette_19_15_C)/sizeof(palette_19_15_C[0]) |
| 3388 | && ix <= sizeof(palette_19_15_M)/sizeof(palette_19_15_M[0]) |
| 3389 | && ix <= sizeof(palette_19_15_Y)/sizeof(palette_19_15_Y[0]) |
| 3390 | && ix <= sizeof(palette_19_15_K)/sizeof(palette_19_15_K[0])) |
| 3391 | { |
| 3392 | c = palette_19_15_C[ix-1]; |
| 3393 | m = palette_19_15_M[ix-1]; |
| 3394 | y = palette_19_15_Y[ix-1]; |
| 3395 | k = palette_19_15_K[ix-1]; |
| 3396 | } |
| 3397 | break; |
| 3398 | case 0x16: |
| 3399 | if (ix > 0 |
| 3400 | && ix <= sizeof(palette_19_16_C)/sizeof(palette_19_16_C[0]) |
| 3401 | && ix <= sizeof(palette_19_16_M)/sizeof(palette_19_16_M[0]) |
| 3402 | && ix <= sizeof(palette_19_16_Y)/sizeof(palette_19_16_Y[0]) |
| 3403 | && ix <= sizeof(palette_19_16_K)/sizeof(palette_19_16_K[0])) |
| 3404 | { |
| 3405 | c = palette_19_16_C[ix-1]; |
| 3406 | m = palette_19_16_M[ix-1]; |
| 3407 | y = palette_19_16_Y[ix-1]; |
| 3408 | k = palette_19_16_K[ix-1]; |
| 3409 | } |
| 3410 | break; |
| 3411 | case 0x17: |
| 3412 | if (ix > 0 |
| 3413 | && ix <= sizeof(palette_19_17_C)/sizeof(palette_19_17_C[0]) |
| 3414 | && ix <= sizeof(palette_19_17_M)/sizeof(palette_19_17_M[0]) |
| 3415 | && ix <= sizeof(palette_19_17_Y)/sizeof(palette_19_17_Y[0]) |
| 3416 | && ix <= sizeof(palette_19_17_K)/sizeof(palette_19_17_K[0])) |
| 3417 | { |
| 3418 | c = palette_19_17_C[ix-1]; |
| 3419 | m = palette_19_17_M[ix-1]; |
| 3420 | y = palette_19_17_Y[ix-1]; |
| 3421 | k = palette_19_17_K[ix-1]; |
| 3422 | } |
| 3423 | break; |
| 3424 | case 0x1a: |
| 3425 | if (ix < sizeof(palette_19_1A_C)/sizeof(palette_19_1A_C[0]) |
| 3426 | && ix < sizeof(palette_19_1A_M)/sizeof(palette_19_1A_M[0]) |
| 3427 | && ix < sizeof(palette_19_1A_Y)/sizeof(palette_19_1A_Y[0]) |
| 3428 | && ix < sizeof(palette_19_1A_K)/sizeof(palette_19_1A_K[0])) |
| 3429 | { |
| 3430 | c = palette_19_1A_C[ix]; |
| 3431 | m = palette_19_1A_M[ix]; |
| 3432 | y = palette_19_1A_Y[ix]; |
| 3433 | k = palette_19_1A_K[ix]; |
| 3434 | } |
| 3435 | break; |
| 3436 | case 0x1b: |
| 3437 | if (ix < sizeof(palette_19_1B_C)/sizeof(palette_19_1B_C[0]) |
| 3438 | && ix < sizeof(palette_19_1B_M)/sizeof(palette_19_1B_M[0]) |
| 3439 | && ix < sizeof(palette_19_1B_Y)/sizeof(palette_19_1B_Y[0]) |
| 3440 | && ix < sizeof(palette_19_1B_K)/sizeof(palette_19_1B_K[0])) |
| 3441 | { |
| 3442 | c = palette_19_1B_C[ix]; |
| 3443 | m = palette_19_1B_M[ix]; |
| 3444 | y = palette_19_1B_Y[ix]; |
| 3445 | k = palette_19_1B_K[ix]; |
| 3446 | } |
| 3447 | break; |
| 3448 | case 0x1c: |
| 3449 | if (ix < sizeof(palette_19_1C_C)/sizeof(palette_19_1C_C[0]) |
| 3450 | && ix < sizeof(palette_19_1C_M)/sizeof(palette_19_1C_M[0]) |
| 3451 | && ix < sizeof(palette_19_1C_Y)/sizeof(palette_19_1C_Y[0]) |
| 3452 | && ix < sizeof(palette_19_1C_K)/sizeof(palette_19_1C_K[0])) |
| 3453 | { |
| 3454 | c = palette_19_1C_C[ix]; |
| 3455 | m = palette_19_1C_M[ix]; |
| 3456 | y = palette_19_1C_Y[ix]; |
| 3457 | k = palette_19_1C_K[ix]; |
| 3458 | } |
| 3459 | break; |
| 3460 | case 0x1d: |
| 3461 | if (ix < sizeof(palette_19_1D_C)/sizeof(palette_19_1D_C[0]) |
| 3462 | && ix < sizeof(palette_19_1D_M)/sizeof(palette_19_1D_M[0]) |
| 3463 | && ix < sizeof(palette_19_1D_Y)/sizeof(palette_19_1D_Y[0]) |
| 3464 | && ix < sizeof(palette_19_1D_K)/sizeof(palette_19_1D_K[0])) |
| 3465 | { |
| 3466 | c = palette_19_1D_C[ix]; |
| 3467 | m = palette_19_1D_M[ix]; |
| 3468 | y = palette_19_1D_Y[ix]; |
| 3469 | k = palette_19_1D_K[ix]; |
| 3470 | } |
| 3471 | break; |
| 3472 | case 0x1e: |
| 3473 | if (ix > 0 |
| 3474 | && ix <= sizeof(palette_19_1E_R)/sizeof(palette_19_1E_R[0]) |
| 3475 | && ix <= sizeof(palette_19_1E_G)/sizeof(palette_19_1E_G[0]) |
| 3476 | && ix <= sizeof(palette_19_1E_B)/sizeof(palette_19_1E_B[0])) |
| 3477 | { |
| 3478 | r = palette_19_1E_R[ix-1]; |
| 3479 | g = palette_19_1E_G[ix-1]; |
| 3480 | b = palette_19_1E_B[ix-1]; |
| 3481 | } |
| 3482 | break; |
| 3483 | case 0x1f: |
| 3484 | if (ix > 0 |
| 3485 | && ix <= sizeof(palette_19_1F_R)/sizeof(palette_19_1F_R[0]) |
| 3486 | && ix <= sizeof(palette_19_1F_G)/sizeof(palette_19_1F_G[0]) |
| 3487 | && ix <= sizeof(palette_19_1F_B)/sizeof(palette_19_1F_B[0])) |
| 3488 | { |
| 3489 | r = palette_19_1F_R[ix-1]; |
| 3490 | g = palette_19_1F_G[ix-1]; |
| 3491 | b = palette_19_1F_B[ix-1]; |
| 3492 | } |
| 3493 | break; |
| 3494 | case 0x20: |
| 3495 | if (ix > 0 |
| 3496 | && ix <= sizeof(palette_19_20_R)/sizeof(palette_19_20_R[0]) |
| 3497 | && ix <= sizeof(palette_19_20_G)/sizeof(palette_19_20_G[0]) |
| 3498 | && ix <= sizeof(palette_19_20_B)/sizeof(palette_19_20_B[0])) |
| 3499 | { |
| 3500 | r = palette_19_20_R[ix-1]; |
| 3501 | g = palette_19_20_G[ix-1]; |
| 3502 | b = palette_19_20_B[ix-1]; |
| 3503 | } |
| 3504 | break; |
| 3505 | case 0x23: |
| 3506 | if (ix > 0 |
| 3507 | && ix <= sizeof(palette_19_23_C)/sizeof(palette_19_23_C[0]) |
| 3508 | && ix <= sizeof(palette_19_23_M)/sizeof(palette_19_23_M[0]) |
| 3509 | && ix <= sizeof(palette_19_23_Y)/sizeof(palette_19_23_Y[0]) |
| 3510 | && ix <= sizeof(palette_19_23_K)/sizeof(palette_19_23_K[0])) |
| 3511 | { |
| 3512 | c = palette_19_23_C[ix-1]; |
| 3513 | m = palette_19_23_M[ix-1]; |
| 3514 | y = palette_19_23_Y[ix-1]; |
| 3515 | k = palette_19_23_K[ix-1]; |
| 3516 | } |
| 3517 | break; |
| 3518 | case 0x24: |
| 3519 | if (ix > 0 |
| 3520 | && ix <= sizeof(palette_19_24_C)/sizeof(palette_19_24_C[0]) |
| 3521 | && ix <= sizeof(palette_19_24_M)/sizeof(palette_19_24_M[0]) |
| 3522 | && ix <= sizeof(palette_19_24_Y)/sizeof(palette_19_24_Y[0]) |
| 3523 | && ix <= sizeof(palette_19_24_K)/sizeof(palette_19_24_K[0])) |
| 3524 | { |
| 3525 | c = palette_19_24_C[ix-1]; |
| 3526 | m = palette_19_24_M[ix-1]; |
| 3527 | y = palette_19_24_Y[ix-1]; |
| 3528 | k = palette_19_24_K[ix-1]; |
| 3529 | } |
| 3530 | break; |
| 3531 | case 0x25: |
| 3532 | if (ix > 0 |
| 3533 | && ix <= sizeof(palette_19_25_C)/sizeof(palette_19_25_C[0]) |
| 3534 | && ix <= sizeof(palette_19_25_M)/sizeof(palette_19_25_M[0]) |
| 3535 | && ix <= sizeof(palette_19_25_Y)/sizeof(palette_19_25_Y[0]) |
| 3536 | && ix <= sizeof(palette_19_25_K)/sizeof(palette_19_25_K[0])) |
| 3537 | { |
| 3538 | c = palette_19_25_C[ix-1]; |
| 3539 | m = palette_19_25_M[ix-1]; |
| 3540 | y = palette_19_25_Y[ix-1]; |
| 3541 | k = palette_19_25_K[ix-1]; |
| 3542 | } |
| 3543 | break; |
| 3544 | default: |
| 3545 | break; |
| 3546 | } |
| 3547 | |
| 3548 | switch (color.m_colorPalette) |
| 3549 | { |
| 3550 | case 0x03: |
| 3551 | case 0x08: |
| 3552 | case 0x0a: |
| 3553 | case 0x0b: |
| 3554 | case 0x11: |
| 3555 | case 0x12: |
| 3556 | case 0x14: |
| 3557 | case 0x15: |
| 3558 | case 0x16: |
| 3559 | case 0x17: |
| 3560 | case 0x1a: |
| 3561 | case 0x1b: |
| 3562 | case 0x1c: |
| 3563 | case 0x1d: |
| 3564 | case 0x23: |
| 3565 | case 0x24: |
| 3566 | case 0x25: |
| 3567 | { |
| 3568 | color.m_colorModel = 0x02; // CMYK100 |
| 3569 | unsigned cyan = (unsigned)tint * (unsigned)c / 100; |
| 3570 | unsigned magenta = (unsigned)tint * (unsigned)m / 100; |
| 3571 | unsigned yellow = (unsigned)tint * (unsigned)y / 100; |
| 3572 | unsigned black = (unsigned)tint * (unsigned)k / 100; |
| 3573 | color.m_colorValue = (black & 0xff); |
| 3574 | color.m_colorValue <<= 8; |
| 3575 | color.m_colorValue |= (yellow & 0xff); |
| 3576 | color.m_colorValue <<= 8; |
| 3577 | color.m_colorValue |= (magenta & 0xff); |
| 3578 | color.m_colorValue <<= 8; |
| 3579 | color.m_colorValue |= (cyan & 0xff); |
| 3580 | color.m_colorPalette = 0; |
| 3581 | break; |
| 3582 | } |
| 3583 | case 0x1e: |
| 3584 | case 0x1f: |
| 3585 | case 0x20: |
| 3586 | { |
| 3587 | color.m_colorModel = 0x05; // RGB |
| 3588 | unsigned red = (unsigned)tint * (unsigned)r + 255 * (100 - tint); |
| 3589 | unsigned green = (unsigned)tint * (unsigned)g + 255 * (100 - tint); |
| 3590 | unsigned blue = (unsigned)tint * (unsigned)b + 255 * (100 - tint); |
| 3591 | red /= 100; |
| 3592 | green /= 100; |
| 3593 | blue /= 100; |
| 3594 | color.m_colorValue = (red & 0xff); |
| 3595 | color.m_colorValue <<= 8; |
| 3596 | color.m_colorValue |= (green & 0xff); |
| 3597 | color.m_colorValue <<= 8; |
| 3598 | color.m_colorValue |= (blue & 0xff); |
| 3599 | color.m_colorPalette = 0; |
| 3600 | break; |
| 3601 | } |
| 3602 | case 0x09: |
| 3603 | color.m_colorModel = 0x12; // L*a*b |
| 3604 | color.m_colorPalette = 0; |
| 3605 | break; |
| 3606 | default: |
| 3607 | break; |
| 3608 | } |
| 3609 | } |
| 3610 | else if (color.m_colorModel == 0x0e) |
| 3611 | { |
| 3612 | switch (color.m_colorPalette) |
| 3613 | { |
| 3614 | case 0x0c: |
| 3615 | if (ix > 0 |
| 3616 | && ix <= sizeof(palette_0E_0C_L)/sizeof(palette_0E_0C_L[0]) |
| 3617 | && ix <= sizeof(palette_0E_0C_A)/sizeof(palette_0E_0C_A[0]) |
| 3618 | && ix <= sizeof(palette_0E_0C_B)/sizeof(palette_0E_0C_B[0])) |
| 3619 | { |
| 3620 | color.m_colorValue = palette_0E_0C_B[ix-1]; |
| 3621 | color.m_colorValue <<= 8; |
| 3622 | color.m_colorValue |= palette_0E_0C_A[ix-1]; |
| 3623 | color.m_colorValue <<= 8; |
| 3624 | color.m_colorValue |= palette_0E_0C_L[ix-1]; |
| 3625 | } |
| 3626 | break; |
| 3627 | case 0x18: |
| 3628 | if (ix > 0 |
| 3629 | && ix <= sizeof(palette_0E_18_L)/sizeof(palette_0E_18_L[0]) |
| 3630 | && ix <= sizeof(palette_0E_18_A)/sizeof(palette_0E_18_A[0]) |
| 3631 | && ix <= sizeof(palette_0E_18_B)/sizeof(palette_0E_18_B[0])) |
| 3632 | { |
| 3633 | color.m_colorValue = palette_0E_18_B[ix-1]; |
| 3634 | color.m_colorValue <<= 8; |
| 3635 | color.m_colorValue |= palette_0E_18_A[ix-1]; |
| 3636 | color.m_colorValue <<= 8; |
| 3637 | color.m_colorValue |= palette_0E_18_L[ix-1]; |
| 3638 | } |
| 3639 | break; |
| 3640 | case 0x21: |
| 3641 | if (ix > 0 |
| 3642 | && ix <= sizeof(palette_0E_21_L)/sizeof(palette_0E_21_L[0]) |
| 3643 | && ix <= sizeof(palette_0E_21_A)/sizeof(palette_0E_21_A[0]) |
| 3644 | && ix <= sizeof(palette_0E_21_B)/sizeof(palette_0E_21_B[0])) |
| 3645 | { |
| 3646 | color.m_colorValue = palette_0E_21_B[ix-1]; |
| 3647 | color.m_colorValue <<= 8; |
| 3648 | color.m_colorValue |= palette_0E_21_A[ix-1]; |
| 3649 | color.m_colorValue <<= 8; |
| 3650 | color.m_colorValue |= palette_0E_21_L[ix-1]; |
| 3651 | } |
| 3652 | break; |
| 3653 | case 0x22: |
| 3654 | if (ix > 0 |
| 3655 | && ix <= sizeof(palette_0E_22_L)/sizeof(palette_0E_22_L[0]) |
| 3656 | && ix <= sizeof(palette_0E_22_A)/sizeof(palette_0E_22_A[0]) |
| 3657 | && ix <= sizeof(palette_0E_22_B)/sizeof(palette_0E_22_B[0])) |
| 3658 | { |
| 3659 | color.m_colorValue = palette_0E_22_B[ix-1]; |
| 3660 | color.m_colorValue <<= 8; |
| 3661 | color.m_colorValue |= palette_0E_22_A[ix-1]; |
| 3662 | color.m_colorValue <<= 8; |
| 3663 | color.m_colorValue |= palette_0E_22_L[ix-1]; |
| 3664 | } |
| 3665 | break; |
| 3666 | default: |
| 3667 | break; |
| 3668 | } |
| 3669 | |
| 3670 | switch (color.m_colorPalette) |
| 3671 | { |
| 3672 | case 0x0c: |
| 3673 | case 0x18: |
| 3674 | case 0x21: |
| 3675 | case 0x22: |
| 3676 | color.m_colorModel = 0x12; // L*a*b |
| 3677 | color.m_colorPalette = 0; |
| 3678 | break; |
| 3679 | default: |
| 3680 | break; |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | CDR_DEBUG_MSG(("CDRParser::_resolveColorPalette resolved --> model 0x%x -- palette 0x%x -- value 0x%x\n", color.m_colorModel, color.m_colorPalette, color.m_colorValue)); |
| 3685 | |
| 3686 | } |
| 3687 | |
| 3688 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |