| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* libwps |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2015 Sean Young <[email protected]> |
| 11 | * |
| 12 | * For minor contributions see the git repository. |
| 13 | * |
| 14 | * Alternatively, the contents of this file may be used under the terms |
| 15 | * of the GNU Lesser General Public License Version 2.1 or later |
| 16 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 17 | * applicable instead of those above. |
| 18 | * |
| 19 | * For further information visit http://libwps.sourceforge.net |
| 20 | */ |
| 21 | |
| 22 | #include <librevenge-stream/librevenge-stream.h> |
| 23 | #include "libwps_internal.h" |
| 24 | |
| 25 | #include "WPSContentListener.h" |
| 26 | #include "WPSEntry.h" |
| 27 | #include "WPSPageSpan.h" |
| 28 | #include "WPSParagraph.h" |
| 29 | #include "WPSPosition.h" |
| 30 | #include "WPSFont.h" |
| 31 | #include "WPSTextSubDocument.h" |
| 32 | |
| 33 | #include "MSWrite.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <cstring> |
| 37 | |
| 38 | namespace MSWriteParserInternal |
| 39 | { |
| 40 | Font::~Font() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | Paragraph::~Paragraph() |
| 45 | { |
| 46 | } |
| 47 | //! Internal: the subdocument of a MSWriteParser |
| 48 | class SubDocument final : public WPSTextSubDocument |
| 49 | { |
| 50 | public: |
| 51 | //! constructor for a text entry |
| 52 | SubDocument(RVNGInputStreamPtr const &input, MSWriteParser &pars, WPSEntry const &entry, Paragraph::Location location) |
| 53 | : WPSTextSubDocument(input, &pars) |
| 54 | , m_entry(entry) |
| 55 | , m_Location(location) {} |
| 56 | //! destructor |
| 57 | ~SubDocument() final {} |
| 58 | |
| 59 | //! operator== |
| 60 | bool operator==(std::shared_ptr<WPSSubDocument> const &doc) const final |
| 61 | { |
| 62 | if (!doc || !WPSTextSubDocument::operator==(doc)) |
| 63 | return false; |
| 64 | auto const *sDoc = dynamic_cast<SubDocument const *>(doc.get()); |
| 65 | if (!sDoc) return false; |
| 66 | return m_entry == sDoc->m_entry; |
| 67 | } |
| 68 | |
| 69 | //! the parser function |
| 70 | void parse(std::shared_ptr<WPSContentListener> &listener, libwps::SubDocumentType subDocumentType) final; |
| 71 | //! the entry |
| 72 | WPSEntry m_entry; |
| 73 | Paragraph::Location m_Location; |
| 74 | }; |
| 75 | |
| 76 | void SubDocument::parse(std::shared_ptr<WPSContentListener> &listener, libwps::SubDocumentType subDocumentType) |
| 77 | { |
| 78 | if (!listener.get()) |
| 79 | { |
| 80 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: no listener\n")); |
| 81 | return; |
| 82 | } |
| 83 | if (!dynamic_cast<WPSContentListener *>(listener.get())) |
| 84 | { |
| 85 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: bad listener\n")); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | auto &listen = reinterpret_cast<WPSContentListenerPtr &>(listener); |
| 90 | if (!m_parser) |
| 91 | { |
| 92 | listen->insertCharacter(' '); |
| 93 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: bad parser\n")); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (m_entry.isParsed() && subDocumentType != libwps::DOC_HEADER_FOOTER) |
| 98 | { |
| 99 | listen->insertCharacter(' '); |
| 100 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: this zone is already parsed\n")); |
| 101 | return; |
| 102 | } |
| 103 | m_entry.setParsed(true); |
| 104 | if (m_entry.type() != "TEXT") |
| 105 | { |
| 106 | listen->insertCharacter(' '); |
| 107 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: send not Text entry is not implemented\n")); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (!m_entry.valid() || !m_input) |
| 112 | { |
| 113 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: empty document found...\n")); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | auto *mnParser = dynamic_cast<MSWriteParser *>(m_parser); |
| 118 | if (!mnParser) |
| 119 | { |
| 120 | WPS_DEBUG_MSG(("MSWriteParserInternal::SubDocument::parse: bad parser...\n")); |
| 121 | listen->insertCharacter(' '); |
| 122 | return; |
| 123 | } |
| 124 | long actPos=m_input->tell(); |
| 125 | mnParser->readText(m_entry, m_Location); |
| 126 | m_input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 127 | } |
| 128 | |
| 129 | struct PAP |
| 130 | { |
| 131 | //! constructor |
| 132 | PAP() |
| 133 | : m_reserved1(0) |
| 134 | , m_justification(0) |
| 135 | , m_dxaRight(0) |
| 136 | , m_dxaLeft(0) |
| 137 | , m_dxaLeft1(0) |
| 138 | , m_dyaLine(0) |
| 139 | , m_dyaBefore(0) |
| 140 | , m_dyaAfter(0) |
| 141 | , m_rhcPage(0) |
| 142 | { |
| 143 | m_reserved2[0]=m_reserved2[1]=0; |
| 144 | for (unsigned char &i : m_reserved3) i=0; |
| 145 | } |
| 146 | uint8_t m_reserved1; |
| 147 | uint8_t m_justification; |
| 148 | uint8_t m_reserved2[2]; |
| 149 | uint16_t m_dxaRight, m_dxaLeft, m_dxaLeft1, m_dyaLine; |
| 150 | uint16_t m_dyaBefore, m_dyaAfter; |
| 151 | uint8_t m_rhcPage; |
| 152 | uint8_t m_reserved3[5]; |
| 153 | struct TBD |
| 154 | { |
| 155 | TBD() |
| 156 | : m_dxa(0) |
| 157 | , m_jcTab(0) |
| 158 | , m_chAlign(0) |
| 159 | { |
| 160 | } |
| 161 | uint16_t m_dxa; |
| 162 | uint8_t m_jcTab; |
| 163 | uint8_t m_chAlign; |
| 164 | } m_TBD[14]; |
| 165 | }; |
| 166 | |
| 167 | struct CHP |
| 168 | { |
| 169 | CHP() |
| 170 | : m_reserved1(0) |
| 171 | , m_fBold(0) |
| 172 | , m_hps(0) |
| 173 | , m_fUline(0) |
| 174 | , m_ftcXtra(0) |
| 175 | , m_hpsPos(0) |
| 176 | { |
| 177 | } |
| 178 | uint8_t m_reserved1; |
| 179 | uint8_t m_fBold; |
| 180 | uint8_t m_hps; |
| 181 | uint8_t m_fUline; |
| 182 | uint8_t m_ftcXtra; |
| 183 | uint8_t m_hpsPos; |
| 184 | }; |
| 185 | |
| 186 | // the file header offsets |
| 187 | enum HeaderOffset |
| 188 | { |
| 189 | HEADER_W_WIDENT = 0, |
| 190 | HEADER_W_DTY = 2, |
| 191 | HEADER_W_WTOOL = 4, |
| 192 | HEADER_D_FCMAC = 14, |
| 193 | HEADER_W_PNPARA = 18, |
| 194 | HEADER_W_PNFNTB = 20, |
| 195 | HEADER_W_PNSEP = 22, |
| 196 | HEADER_W_PNSETB = 24, |
| 197 | HEADER_W_PNBFTB = 26, |
| 198 | HEADER_W_PNFFNTB = 28, |
| 199 | HEADER_W_PNMAC = 96 |
| 200 | }; |
| 201 | |
| 202 | // pictures in Write 3.0, either DDB of WMF |
| 203 | enum PicOffset |
| 204 | { |
| 205 | PIC_W_MM = 0, |
| 206 | PIC_W_XEXT = 2, |
| 207 | PIC_W_YEXT = 4, |
| 208 | PIC_W_DXAOFFSET = 8, |
| 209 | PIC_W_DXASIZE = 10, |
| 210 | PIC_W_DYASIZE = 12, |
| 211 | PIC_W_BMWIDTH = 18, |
| 212 | PIC_W_BMHEIGHT = 20, |
| 213 | PIC_W_BMWIDTHBYTES = 22, |
| 214 | PIC_B_BMPLANES = 24, |
| 215 | PIC_B_BMBITSPIXEL = 25, |
| 216 | PIC_D_CBSIZE = 32, |
| 217 | PIC_W_MX = 36, |
| 218 | PIC_W_MY = 38 |
| 219 | }; |
| 220 | |
| 221 | // OLE objects in Write 3.1 |
| 222 | enum OleOffset |
| 223 | { |
| 224 | OLE_W_MM = 0, |
| 225 | OLE_W_OBJECTTYPE = 6, |
| 226 | OLE_W_DXAOFFSET = 8, |
| 227 | OLE_W_DXASIZE = 10, |
| 228 | OLE_W_DYASIZE = 12, |
| 229 | OLE_D_DWDATASIZE = 16, |
| 230 | OLE_W_MX = 36, |
| 231 | OLE_W_MY = 38 |
| 232 | }; |
| 233 | |
| 234 | enum BitmapInfoHeaderV3Offset |
| 235 | { |
| 236 | BM_INFO_V3_SIZE = 0, |
| 237 | BM_INFO_V3_WIDTH = 4, |
| 238 | BM_INFO_V3_HEIGHT = 8, |
| 239 | BM_INFO_V3_PLANES = 12, |
| 240 | BM_INFO_V3_BITS_PIXEL = 14, |
| 241 | BM_INFO_V3_COMPRESSION = 16, |
| 242 | BM_INFO_V3_BITMAP_SIZE = 20, |
| 243 | BM_INFO_V3_HORZ_RES = 24, |
| 244 | BM_INFO_V3_VERT_RES = 28, |
| 245 | BM_INFO_V3_COLORS_USED = 32, |
| 246 | BM_INFO_V3_COLORS_IMPORTANT = 36, |
| 247 | BM_INFO_V3_STRUCT_SIZE = 40 |
| 248 | }; |
| 249 | |
| 250 | enum BitmapFileHeaderOffset |
| 251 | { |
| 252 | BM_FILE_MAGIC = 0, |
| 253 | BM_FILE_SIZE = 2, |
| 254 | BM_FILE_RESERVED = 6, |
| 255 | BM_FILE_OFFSET = 10, |
| 256 | BM_FILE_STRUCT_SIZE = 14 |
| 257 | }; |
| 258 | |
| 259 | enum BitmapInfoHeaderV2Offset |
| 260 | { |
| 261 | BM_INFO_V2_SIZE = 0, |
| 262 | BM_INFO_V2_WIDTH = 4, |
| 263 | BM_INFO_V2_HEIGHT = 6, |
| 264 | BM_INFO_V2_PLANES = 8, |
| 265 | BM_INFO_V2_BITS_PIXEL = 10, |
| 266 | BM_INFO_V2_STRUCT_SIZE = 12 |
| 267 | }; |
| 268 | |
| 269 | struct BitmapPalette |
| 270 | { |
| 271 | uint8_t m_r, m_g, m_b; |
| 272 | }; |
| 273 | |
| 274 | static void appendU16(librevenge::RVNGBinaryData &b, uint16_t val) |
| 275 | { |
| 276 | b.append(static_cast<unsigned char>(val)); |
| 277 | b.append(static_cast<unsigned char>(val >> 8)); |
| 278 | } |
| 279 | |
| 280 | static void appendU32(librevenge::RVNGBinaryData &b, uint32_t val) |
| 281 | { |
| 282 | b.append(static_cast<unsigned char>(val)); |
| 283 | b.append(static_cast<unsigned char>(val >> 8)); |
| 284 | b.append(static_cast<unsigned char>(val >> 16)); |
| 285 | b.append(static_cast<unsigned char>(val >> 24)); |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | |
| 290 | MSWriteParser::MSWriteParser(RVNGInputStreamPtr &input, WPSHeaderPtr &header, |
| 291 | libwps_tools_win::Font::Type encoding) |
| 292 | : WPSParser(input, header) |
| 293 | , m_fileLength(0) |
| 294 | , m_fcMac(0) |
| 295 | , m_paragraphList(0) |
| 296 | , m_fontList(0) |
| 297 | , m_footnotes(0) |
| 298 | , m_sections(0) |
| 299 | , m_fonts(0) |
| 300 | , m_fontType(encoding) |
| 301 | , m_listener() |
| 302 | , m_metaData() |
| 303 | { |
| 304 | if (!input) |
| 305 | { |
| 306 | WPS_DEBUG_MSG(("MSWriteParser::MSWriteParser: called without input\n")); |
| 307 | throw libwps::ParseException(); |
| 308 | } |
| 309 | |
| 310 | input->seek(0, librevenge::RVNG_SEEK_END); |
| 311 | m_fileLength=uint32_t(input->tell()); |
| 312 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 313 | m_fontType = getFileEncoding(encoding); |
| 314 | } |
| 315 | |
| 316 | MSWriteParser::~MSWriteParser() |
| 317 | { |
| 318 | } |
| 319 | |
| 320 | libwps_tools_win::Font::Type MSWriteParser::getFileEncoding(libwps_tools_win::Font::Type encoding) |
| 321 | { |
| 322 | if (encoding == libwps_tools_win::Font::UNKNOWN) |
| 323 | encoding = libwps_tools_win::Font::WIN3_WEUROPE; |
| 324 | |
| 325 | return encoding; |
| 326 | } |
| 327 | |
| 328 | void MSWriteParser::readFIB() |
| 329 | { |
| 330 | RVNGInputStreamPtr input = getInput(); |
| 331 | |
| 332 | if (!checkFilePosition(0x100)) |
| 333 | { |
| 334 | WPS_DEBUG_MSG(("MSWriteParser::readFIB File is too short to be a MSWrite file\n")); |
| 335 | throw (libwps::ParseException()); |
| 336 | } |
| 337 | |
| 338 | input->seek(MSWriteParserInternal::HEADER_D_FCMAC, librevenge::RVNG_SEEK_SET); |
| 339 | m_fcMac = libwps::readU32(input); |
| 340 | |
| 341 | if (m_fcMac <= 0x80) |
| 342 | { |
| 343 | WPS_DEBUG_MSG(("MSWriteParser::readFIB fcMac %u is impossible\n", m_fcMac)); |
| 344 | throw (libwps::ParseException()); |
| 345 | } |
| 346 | |
| 347 | if (!checkFilePosition(m_fcMac)) |
| 348 | { |
| 349 | WPS_DEBUG_MSG(("MSWriteParser::readFIB File is corrupt, position %u beyond end of file\n", m_fcMac)); |
| 350 | throw (libwps::ParseException()); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void MSWriteParser::readFFNTB() |
| 355 | { |
| 356 | unsigned font_count = 0, pnFfntb, pnMac; |
| 357 | RVNGInputStreamPtr input = getInput(); |
| 358 | |
| 359 | input->seek(MSWriteParserInternal::HEADER_W_PNFFNTB, librevenge::RVNG_SEEK_SET); |
| 360 | pnFfntb = libwps::readU16(input); |
| 361 | |
| 362 | input->seek(MSWriteParserInternal::HEADER_W_PNMAC, librevenge::RVNG_SEEK_SET); |
| 363 | pnMac = libwps::readU16(input); |
| 364 | |
| 365 | if (pnFfntb != 0 && pnFfntb != pnMac) |
| 366 | { |
| 367 | if (!checkFilePosition(pnFfntb * 0x80 + 2)) |
| 368 | { |
| 369 | WPS_DEBUG_MSG(("MSWriteParser::readFFNTB Font table is missing\n")); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | input->seek(long(pnFfntb) * 0x80, librevenge::RVNG_SEEK_SET); |
| 374 | font_count = libwps::readU16(input); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (font_count) |
| 379 | { |
| 380 | // Ignore font_count, some write files have more |
| 381 | unsigned offset = 0; |
| 382 | |
| 383 | for (;;) |
| 384 | { |
| 385 | offset += 2; |
| 386 | if (!checkFilePosition(pnFfntb * 0x80 + offset)) |
| 387 | { |
| 388 | WPS_DEBUG_MSG(("MSWriteParser::readFFNTB Font table is truncated\n")); |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | unsigned int cbFfn = libwps::readU16(input); |
| 393 | if (cbFfn == 0) |
| 394 | break; |
| 395 | |
| 396 | if (cbFfn == 0xffff) |
| 397 | { |
| 398 | pnFfntb++; |
| 399 | |
| 400 | if (!checkFilePosition(pnFfntb * 0x80 + 2)) |
| 401 | { |
| 402 | WPS_DEBUG_MSG(("MSWriteParser::readFFNTB Font table is truncated\n")); |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | offset = 0; |
| 407 | input->seek(long(pnFfntb) * 0x80, librevenge::RVNG_SEEK_SET); |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | offset += cbFfn; |
| 412 | if (offset > 0x80) |
| 413 | { |
| 414 | WPS_DEBUG_MSG(("MSWriteParser::readFFNTB Font straddles page, file corrupt\n")); |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | if (!checkFilePosition(pnFfntb * 0x80 + offset)) |
| 419 | { |
| 420 | WPS_DEBUG_MSG(("MSWriteParser::readFFNTB Font table is truncated\n")); |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | // We're not interested in the font family |
| 425 | input->seek(1, librevenge::RVNG_SEEK_CUR); |
| 426 | |
| 427 | // Read font name |
| 428 | unsigned fnlen = cbFfn - 1; |
| 429 | unsigned long read_bytes; |
| 430 | const unsigned char *fn = input->read(fnlen, read_bytes); |
| 431 | if (read_bytes != fnlen) |
| 432 | { |
| 433 | WPS_DEBUG_MSG(("MSWriteParser::readFFNTB failed to read font name\n")); |
| 434 | throw (libwps::ParseException()); |
| 435 | } |
| 436 | |
| 437 | librevenge::RVNGString fontname; |
| 438 | |
| 439 | // Remove trailing 0s |
| 440 | while (fnlen > 0 && !fn[fnlen - 1]) |
| 441 | fnlen--; |
| 442 | |
| 443 | fontname = libwps_tools_win::Font::unicodeString(fn, fnlen, m_fontType); |
| 444 | |
| 445 | m_fonts.push_back(fontname); |
| 446 | } |
| 447 | } |
| 448 | if (m_fonts.empty()) |
| 449 | m_fonts.push_back("Arial"); |
| 450 | } |
| 451 | |
| 452 | void MSWriteParser::readSED() |
| 453 | { |
| 454 | unsigned pnSetb, pnBftb; |
| 455 | RVNGInputStreamPtr input = getInput(); |
| 456 | |
| 457 | input->seek(MSWriteParserInternal::HEADER_W_PNSETB, librevenge::RVNG_SEEK_SET); |
| 458 | pnSetb = libwps::readU16(input); |
| 459 | |
| 460 | input->seek(MSWriteParserInternal::HEADER_W_PNBFTB, librevenge::RVNG_SEEK_SET); |
| 461 | pnBftb = libwps::readU16(input); |
| 462 | |
| 463 | MSWriteParserInternal::Section sep; |
| 464 | |
| 465 | if (pnSetb && pnSetb != pnBftb) |
| 466 | { |
| 467 | if (!checkFilePosition(pnSetb * 0x80 + 14)) |
| 468 | { |
| 469 | WPS_DEBUG_MSG(("Section is truncated\n")); |
| 470 | throw (libwps::ParseException()); |
| 471 | } |
| 472 | |
| 473 | input->seek(long(pnSetb) * 0x80, librevenge::RVNG_SEEK_SET); |
| 474 | uint16_t cset = libwps::readU16(input); |
| 475 | |
| 476 | // ignore csetMax, cp, fn |
| 477 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 478 | |
| 479 | uint32_t fcSep = libwps::readU32(input); |
| 480 | if (cset > 1 && checkFilePosition(fcSep + 22)) |
| 481 | { |
| 482 | input->seek(long(fcSep), librevenge::RVNG_SEEK_SET); |
| 483 | uint8_t headerSize = libwps::readU8(input); |
| 484 | if (headerSize<22 || !checkFilePosition(fcSep+2+headerSize)) |
| 485 | { |
| 486 | WPS_DEBUG_MSG(("MSWriteParser::readSECT: can not read the structure, using default\n")); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | input->seek(2, librevenge::RVNG_SEEK_CUR); // skip reserved 1 |
| 491 | // read section |
| 492 | sep.m_yaMac=double(libwps::readU16(input))/1440.; |
| 493 | sep.m_xaMac=double(libwps::readU16(input))/1440.; |
| 494 | sep.m_startPageNumber=libwps::readU16(input); |
| 495 | sep.m_yaTop=double(libwps::readU16(input))/1440.; |
| 496 | sep.m_dyaText=double(libwps::readU16(input))/1440.; |
| 497 | sep.m_xaLeft=double(libwps::readU16(input))/1440.; |
| 498 | sep.m_dxaText=double(libwps::readU16(input))/1440.; |
| 499 | input->seek(2, librevenge::RVNG_SEEK_CUR); // skip reserved 2 |
| 500 | sep.m_yaHeader=double(libwps::readU16(input))/1440.; |
| 501 | sep.m_yaFooter=double(libwps::readU16(input))/1440.; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | sep.m_fcLim = m_fcMac; |
| 507 | m_sections.push_back(sep); |
| 508 | } |
| 509 | |
| 510 | void MSWriteParser::getPageStyle(MSWriteParserInternal::Section &sep, WPSPageSpan &pageSpan) |
| 511 | { |
| 512 | pageSpan.setFormLength(sep.m_yaMac); |
| 513 | pageSpan.setFormWidth(sep.m_xaMac); |
| 514 | |
| 515 | if (sep.m_yaTop < sep.m_yaMac && sep.m_yaMac - sep.m_yaTop - sep.m_dyaText >= 0 && |
| 516 | sep.m_yaMac - sep.m_dyaText < sep.m_yaMac) |
| 517 | { |
| 518 | pageSpan.setMarginTop(sep.m_yaTop); |
| 519 | pageSpan.setMarginBottom(sep.m_yaMac - sep.m_yaTop - sep.m_dyaText); |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | WPS_DEBUG_MSG(("MSWriteParser::readSECT: the top bottom margin seems bad\n")); |
| 524 | } |
| 525 | |
| 526 | if (sep.m_columns > 1 && sep.m_xaLeft < sep.m_xaMac) |
| 527 | { |
| 528 | // m_dxaText is width of column |
| 529 | double dxaText = sep.m_dxaText * sep.m_columns + sep.m_dxaColumns * (sep.m_columns - 1); |
| 530 | |
| 531 | if (sep.m_xaMac - dxaText >= sep.m_xaLeft) |
| 532 | { |
| 533 | pageSpan.setMarginLeft(sep.m_xaLeft); |
| 534 | pageSpan.setMarginRight(sep.m_xaMac - sep.m_xaLeft - dxaText); |
| 535 | } |
| 536 | } |
| 537 | else if (sep.m_xaLeft < sep.m_xaMac && sep.m_xaMac - sep.m_xaLeft - sep.m_dxaText >= 0 && |
| 538 | sep.m_xaMac - sep.m_dxaText < sep.m_xaMac) |
| 539 | { |
| 540 | pageSpan.setMarginLeft(sep.m_xaLeft); |
| 541 | pageSpan.setMarginRight(sep.m_xaMac - sep.m_xaLeft - sep.m_dxaText); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | WPS_DEBUG_MSG(("MSWriteParser::readSECT: the left right margin seems bad\n")); |
| 546 | } |
| 547 | |
| 548 | if (sep.m_startPageNumber != 0xffff) |
| 549 | pageSpan.setPageNumber(sep.m_startPageNumber); |
| 550 | |
| 551 | // FIXME: |
| 552 | // - page numbers |
| 553 | // - line numbers |
| 554 | // - page break style |
| 555 | } |
| 556 | |
| 557 | void MSWriteParser::readFOD(unsigned page, void (MSWriteParser::*parseFOD)(uint32_t fcFirst, uint32_t fcLim, unsigned size)) |
| 558 | { |
| 559 | RVNGInputStreamPtr input = getInput(); |
| 560 | unsigned fcLim, fc = 0x80; |
| 561 | |
| 562 | for (;;) |
| 563 | { |
| 564 | auto const pageBegin=long(page * 0x80); |
| 565 | |
| 566 | if (!checkFilePosition(page * 0x80 + 0x7f)) |
| 567 | { |
| 568 | WPS_DEBUG_MSG(("MSWriteParser::readFOD: FOD list is truncated\n")); |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | input->seek(pageBegin + 0x7f, librevenge::RVNG_SEEK_SET); |
| 573 | uint8_t cfod = libwps::readU8(input); |
| 574 | |
| 575 | if (cfod > 20) |
| 576 | { |
| 577 | WPS_DEBUG_MSG(("MSWriteParser::readFOD: Too many FODs %d on one page\n", cfod)); |
| 578 | cfod = 20; |
| 579 | } |
| 580 | |
| 581 | for (unsigned fod = 0; fod < cfod; ++fod) |
| 582 | { |
| 583 | input->seek(pageBegin + int(fod) * 6 + 4, librevenge::RVNG_SEEK_SET); |
| 584 | fcLim = libwps::readU32(input); |
| 585 | uint16_t bfProp = libwps::readU16(input); |
| 586 | unsigned cch = 0; |
| 587 | |
| 588 | if (bfProp < 0x7f - 4) |
| 589 | { |
| 590 | input->seek(pageBegin + long(bfProp) + 4, librevenge::RVNG_SEEK_SET); |
| 591 | cch = libwps::readU8(input); |
| 592 | |
| 593 | // Does it fit on the page |
| 594 | if ((bfProp + cch + 4) >= 0x80) |
| 595 | { |
| 596 | cch = 0; |
| 597 | WPS_DEBUG_MSG(("MSWriteParser::readFOD: entry %d on page %d invalid\n", fod, page)); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | (this->*parseFOD)(fc, fcLim, cch); |
| 602 | |
| 603 | if (fcLim >= m_fcMac) |
| 604 | return; |
| 605 | |
| 606 | fc = fcLim; |
| 607 | } |
| 608 | page++; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | void MSWriteParser::readPAP(uint32_t fcFirst, uint32_t fcLim, unsigned cch) |
| 613 | { |
| 614 | RVNGInputStreamPtr input = getInput(); |
| 615 | |
| 616 | struct MSWriteParserInternal::PAP pap; |
| 617 | |
| 618 | WPS_LE_PUT_GUINT16(&pap.m_dyaLine, 240); |
| 619 | |
| 620 | if (cch) |
| 621 | { |
| 622 | if (cch > sizeof(pap)) |
| 623 | cch = sizeof(pap); |
| 624 | |
| 625 | unsigned long read_bytes; |
| 626 | const unsigned char *p = input->read(cch, read_bytes); |
| 627 | if (read_bytes != cch) |
| 628 | { |
| 629 | WPS_DEBUG_MSG(("MSWriteParser::readPAP failed to read PAP\n")); |
| 630 | throw (libwps::ParseException()); |
| 631 | } |
| 632 | |
| 633 | memcpy(&pap, p, cch); |
| 634 | } |
| 635 | |
| 636 | auto dxaLeft = int16_t(WPS_LE_GET_GUINT16(&pap.m_dxaLeft)); |
| 637 | auto dxaLeft1 = int16_t(WPS_LE_GET_GUINT16(&pap.m_dxaLeft1)); |
| 638 | auto dxaRight = int16_t(WPS_LE_GET_GUINT16(&pap.m_dxaRight)); |
| 639 | |
| 640 | MSWriteParserInternal::Paragraph para; |
| 641 | int i; |
| 642 | |
| 643 | for (i=0; i<14; i++) |
| 644 | { |
| 645 | auto pos = WPS_LE_GET_GUINT16(&pap.m_TBD[i].m_dxa); |
| 646 | |
| 647 | if (!pos) |
| 648 | break; |
| 649 | |
| 650 | WPSTabStop tab(pos / 1440., (pap.m_TBD[i].m_jcTab & 3) == 3 ? |
| 651 | WPSTabStop::DECIMAL : WPSTabStop::LEFT); |
| 652 | |
| 653 | para.m_tabs.push_back(tab); |
| 654 | |
| 655 | if (dxaLeft + dxaLeft1 == pos) |
| 656 | para.m_skiptab = true; |
| 657 | } |
| 658 | |
| 659 | switch (pap.m_justification & 3) |
| 660 | { |
| 661 | default: |
| 662 | case 0: |
| 663 | para.m_justify = libwps::JustificationLeft; |
| 664 | break; |
| 665 | case 1: |
| 666 | para.m_justify = libwps::JustificationCenter; |
| 667 | break; |
| 668 | case 2: |
| 669 | para.m_justify = libwps::JustificationRight; |
| 670 | break; |
| 671 | case 3: |
| 672 | para.m_justify = libwps::JustificationFull; |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | para.m_margins[0] = dxaLeft1 / 1440.0; |
| 677 | para.m_margins[1] = dxaLeft / 1440.0; |
| 678 | para.m_margins[2] = dxaRight / 1440.0; |
| 679 | |
| 680 | auto dyaLine = WPS_LE_GET_GUINT16(&pap.m_dyaLine); |
| 681 | para.m_interLine = dyaLine / 240.0; |
| 682 | |
| 683 | para.m_fcFirst = fcFirst; |
| 684 | para.m_fcLim = fcLim; |
| 685 | if (pap.m_rhcPage & 0x10) |
| 686 | { |
| 687 | para.m_graphics = true; |
| 688 | /* MS Write doesn't take first indent into |
| 689 | account for images, but odt does */ |
| 690 | para.m_margins[0] = 0.0; |
| 691 | } |
| 692 | else |
| 693 | { |
| 694 | if (pap.m_rhcPage & 6) |
| 695 | { |
| 696 | if (pap.m_rhcPage & 1) |
| 697 | { |
| 698 | para.m_Location = MSWriteParserInternal::Paragraph::FOOTER; |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | para.m_Location = MSWriteParserInternal::Paragraph::HEADER; |
| 703 | } |
| 704 | para.m_firstpage = (pap.m_rhcPage & 0x08) != 0; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | m_paragraphList.push_back(para); |
| 709 | } |
| 710 | |
| 711 | void MSWriteParser::readCHP(uint32_t fcFirst, uint32_t fcLim, unsigned cch) |
| 712 | { |
| 713 | RVNGInputStreamPtr input = getInput(); |
| 714 | |
| 715 | struct MSWriteParserInternal::CHP chp; |
| 716 | |
| 717 | chp.m_hps = 24; |
| 718 | |
| 719 | if (cch) |
| 720 | { |
| 721 | if (cch > sizeof(chp)) |
| 722 | cch = sizeof(chp); |
| 723 | |
| 724 | unsigned long read_bytes; |
| 725 | const unsigned char *p = input->read(cch, read_bytes); |
| 726 | if (read_bytes != cch) |
| 727 | { |
| 728 | WPS_DEBUG_MSG(("MSWriteParser::readCHP failed to read CHP entry\n")); |
| 729 | throw (libwps::ParseException()); |
| 730 | } |
| 731 | |
| 732 | memcpy(&chp, p, cch); |
| 733 | } |
| 734 | |
| 735 | MSWriteParserInternal::Font font; |
| 736 | |
| 737 | unsigned ftc = (chp.m_fBold / 4) | ((chp.m_ftcXtra & 7) * 64); |
| 738 | if (ftc >= m_fonts.size()) ftc = 0; |
| 739 | |
| 740 | font.m_name = m_fonts[ftc]; |
| 741 | font.m_size = chp.m_hps / 2.0; |
| 742 | if (chp.m_fBold & 1) |
| 743 | font.m_attributes |= WPS_BOLD_BIT; |
| 744 | if (chp.m_fBold & 2) |
| 745 | font.m_attributes |= WPS_ITALICS_BIT; |
| 746 | if (chp.m_fUline & 1) |
| 747 | font.m_attributes |= WPS_UNDERLINE_BIT; |
| 748 | if (chp.m_fUline & 0x40) |
| 749 | font.m_special = true; |
| 750 | if (chp.m_hpsPos) |
| 751 | { |
| 752 | if (chp.m_hpsPos & 0x80) |
| 753 | font.m_attributes |= WPS_SUBSCRIPT_BIT; |
| 754 | else |
| 755 | font.m_attributes |= WPS_SUPERSCRIPT_BIT; |
| 756 | } |
| 757 | |
| 758 | font.m_fcFirst = fcFirst; |
| 759 | font.m_fcLim = fcLim; |
| 760 | font.m_encoding = libwps_tools_win::Font::getFontType(font.m_name); |
| 761 | if (font.m_encoding == libwps_tools_win::Font::UNKNOWN) |
| 762 | font.m_encoding = m_fontType; |
| 763 | |
| 764 | m_fontList.push_back(font); |
| 765 | } |
| 766 | |
| 767 | void MSWriteParser::getHeaderFooters(uint32_t first, MSWriteParserInternal::Section §ion, WPSPageSpan &ps) |
| 768 | { |
| 769 | bool headerPage1 = true, footerPage1 = true; |
| 770 | bool reachedMain = false; |
| 771 | WPSEntry empty, footer, header; |
| 772 | WPSPageSpan::HeaderFooterOccurrence headerOccurrence = WPSPageSpan::ALL; |
| 773 | WPSPageSpan::HeaderFooterOccurrence footerOccurrence = WPSPageSpan::ALL; |
| 774 | |
| 775 | MSWriteParserInternal::Paragraph::Location location = MSWriteParserInternal::Paragraph::MAIN; |
| 776 | RVNGInputStreamPtr input = getInput(); |
| 777 | int numPage = 0; // each section ends with a page break (0x0c) |
| 778 | |
| 779 | size_t firstP = 0; |
| 780 | size_t i; |
| 781 | |
| 782 | for (i = 0; i < m_paragraphList.size(); i++) |
| 783 | { |
| 784 | MSWriteParserInternal::Paragraph &p = m_paragraphList[i]; |
| 785 | |
| 786 | if (p.m_fcLim <= first) |
| 787 | { |
| 788 | firstP = i + 1; |
| 789 | continue; |
| 790 | } |
| 791 | |
| 792 | if (p.m_fcFirst >= section.m_fcLim) |
| 793 | break; |
| 794 | |
| 795 | if (!p.m_graphics) |
| 796 | { |
| 797 | uint32_t fc = p.m_fcFirst; |
| 798 | |
| 799 | input->seek(long(fc), librevenge::RVNG_SEEK_SET); |
| 800 | |
| 801 | while (fc < p.m_fcLim && fc < m_fcMac) |
| 802 | { |
| 803 | if (libwps::readU8(input) == 0x0C) numPage++; |
| 804 | fc++; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if (!p.m_headerUseMargin && (p.m_Location == MSWriteParserInternal::Paragraph::HEADER || |
| 809 | p.m_Location == MSWriteParserInternal::Paragraph::FOOTER)) |
| 810 | { |
| 811 | // Indents in header/footer are off paper, not margins |
| 812 | p.m_margins[1] -= ps.getMarginLeft(); |
| 813 | p.m_margins[2] -= ps.getMarginRight(); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | if (p.m_Location != location && !reachedMain) |
| 818 | { |
| 819 | if (location == MSWriteParserInternal::Paragraph::HEADER) |
| 820 | { |
| 821 | headerPage1 = m_paragraphList[firstP].m_firstpage; |
| 822 | headerOccurrence = m_paragraphList[firstP].m_HeaderFooterOccurrence; |
| 823 | header.setBegin(long(m_paragraphList[firstP].m_fcFirst)); |
| 824 | header.setEnd(long(m_paragraphList[i - 1].m_fcLim)); |
| 825 | header.setType("TEXT"); |
| 826 | } |
| 827 | else if (location == MSWriteParserInternal::Paragraph::FOOTER) |
| 828 | { |
| 829 | footerPage1 = m_paragraphList[firstP].m_firstpage; |
| 830 | footerOccurrence = m_paragraphList[firstP].m_HeaderFooterOccurrence; |
| 831 | footer.setBegin(long(m_paragraphList[firstP].m_fcFirst)); |
| 832 | footer.setEnd(long(m_paragraphList[i - 1].m_fcLim)); |
| 833 | footer.setType("TEXT"); |
| 834 | } |
| 835 | |
| 836 | location = p.m_Location; |
| 837 | firstP = i; |
| 838 | } |
| 839 | |
| 840 | if (p.m_Location == MSWriteParserInternal::Paragraph::FOOTNOTE) |
| 841 | break; |
| 842 | |
| 843 | if (p.m_Location == MSWriteParserInternal::Paragraph::MAIN) |
| 844 | reachedMain = true; |
| 845 | } |
| 846 | |
| 847 | if (i <= firstP) |
| 848 | { |
| 849 | WPS_DEBUG_MSG(("MSWriteParser::getHeaderFooters: missing body text\n")); |
| 850 | throw (libwps::ParseException()); |
| 851 | } |
| 852 | |
| 853 | section.m_Main.setBegin(long(m_paragraphList[firstP].m_fcFirst)); |
| 854 | section.m_Main.setEnd(long(std::min(m_paragraphList[i - 1].m_fcLim, m_fcMac))); |
| 855 | section.m_Main.setType("TEXT"); |
| 856 | |
| 857 | empty.setType("TEXT"); |
| 858 | |
| 859 | WPSSubDocumentPtr subemptydoc(new MSWriteParserInternal::SubDocument |
| 860 | (getInput(), *this, empty, MSWriteParserInternal::Paragraph::MAIN)); |
| 861 | |
| 862 | if (header.valid()) |
| 863 | { |
| 864 | WPSSubDocumentPtr subdoc(new MSWriteParserInternal::SubDocument |
| 865 | (getInput(), *this, header, MSWriteParserInternal::Paragraph::HEADER)); |
| 866 | |
| 867 | if (headerOccurrence == WPSPageSpan::NEVER) |
| 868 | { |
| 869 | if (headerPage1) |
| 870 | ps.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::FIRST, subdoc); |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | ps.setHeaderFooter(WPSPageSpan::HEADER, headerOccurrence, subdoc); |
| 875 | |
| 876 | if (!headerPage1) |
| 877 | ps.setHeaderFooter(WPSPageSpan::HEADER, WPSPageSpan::FIRST, subemptydoc); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if (footer.valid()) |
| 882 | { |
| 883 | WPSSubDocumentPtr subdoc(new MSWriteParserInternal::SubDocument |
| 884 | (getInput(), *this, footer, MSWriteParserInternal::Paragraph::FOOTER)); |
| 885 | |
| 886 | if (footerOccurrence == WPSPageSpan::NEVER) |
| 887 | { |
| 888 | if (footerPage1) |
| 889 | ps.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::FIRST, subdoc); |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | ps.setHeaderFooter(WPSPageSpan::FOOTER, footerOccurrence, subdoc); |
| 894 | |
| 895 | if (!footerPage1) |
| 896 | ps.setHeaderFooter(WPSPageSpan::FOOTER, WPSPageSpan::FIRST, subemptydoc); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | if (section.m_bkc == 0) |
| 901 | numPage--; |
| 902 | |
| 903 | ps.setPageSpan(numPage); |
| 904 | } |
| 905 | |
| 906 | std::shared_ptr<WPSContentListener> MSWriteParser::createListener(librevenge::RVNGTextInterface *interface) |
| 907 | { |
| 908 | std::vector<WPSPageSpan> pageList; |
| 909 | uint32_t fc = 0x80; |
| 910 | std::vector<MSWriteParserInternal::Section>::iterator sections; |
| 911 | |
| 912 | for (sections = m_sections.begin(); sections != m_sections.end();) |
| 913 | { |
| 914 | WPSPageSpan ps; |
| 915 | // Get margins etc from SEP |
| 916 | getPageStyle(*sections, ps); |
| 917 | // Get headers, footers and page count from actual text |
| 918 | getHeaderFooters(fc, *sections, ps); |
| 919 | fc = sections->m_fcLim; |
| 920 | // Add extra page for last section |
| 921 | if (++sections == m_sections.end()) |
| 922 | ps.setPageSpan(ps.getPageSpan()+1); |
| 923 | |
| 924 | pageList.push_back(ps); |
| 925 | } |
| 926 | |
| 927 | return std::shared_ptr<WPSContentListener> |
| 928 | (new WPSContentListener(pageList, interface)); |
| 929 | } |
| 930 | |
| 931 | void MSWriteParser::readText(WPSEntry const &e, MSWriteParserInternal::Paragraph::Location location) |
| 932 | { |
| 933 | auto fc = uint32_t(e.begin()); |
| 934 | std::vector<MSWriteParserInternal::Paragraph>::iterator paps; |
| 935 | std::vector<MSWriteParserInternal::Font>::iterator chps; |
| 936 | paps = m_paragraphList.begin(); |
| 937 | chps = m_fontList.begin(); |
| 938 | RVNGInputStreamPtr input = getInput(); |
| 939 | float lastObjectHeight = 0.0; |
| 940 | |
| 941 | while (fc < uint32_t(e.end())) |
| 942 | { |
| 943 | bool skiptab = false; |
| 944 | |
| 945 | while (fc >= paps->m_fcLim) |
| 946 | { |
| 947 | if (++paps == m_paragraphList.end()) |
| 948 | { |
| 949 | WPS_DEBUG_MSG(("MSWriteParser::readText PAP not found for offset %u\n", fc)); |
| 950 | throw (libwps::ParseException()); |
| 951 | } |
| 952 | skiptab = paps->m_skiptab; |
| 953 | } |
| 954 | |
| 955 | if (paps->m_Location != location) |
| 956 | { |
| 957 | // ignore e.g. headers in main body of text |
| 958 | fc = paps->m_fcLim; |
| 959 | continue; |
| 960 | } |
| 961 | |
| 962 | if (paps->m_graphics) |
| 963 | { |
| 964 | m_listener->setParagraph(*paps); |
| 965 | |
| 966 | // The last pap can have m_fcLim of greater than m_fcMac |
| 967 | unsigned fcLim = std::min(paps->m_fcLim, m_fcMac); |
| 968 | |
| 969 | if (fcLim - fc < 40) |
| 970 | { |
| 971 | WPS_DEBUG_MSG(("MSWriteParser::readText object too short\n")); |
| 972 | fc = paps->m_fcLim; |
| 973 | continue; |
| 974 | } |
| 975 | |
| 976 | WPSPosition pos; |
| 977 | WPSPosition::XPos align; |
| 978 | |
| 979 | switch (paps->m_justify) |
| 980 | { |
| 981 | case libwps::JustificationFull: |
| 982 | case libwps::JustificationFullAllLines: |
| 983 | case libwps::JustificationLeft: |
| 984 | default: |
| 985 | align = WPSPosition::XLeft; |
| 986 | break; |
| 987 | case libwps::JustificationRight: |
| 988 | align = WPSPosition::XRight; |
| 989 | break; |
| 990 | case libwps::JustificationCenter: |
| 991 | align = WPSPosition::XCenter; |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | pos.setRelativePosition(WPSPosition::ParagraphContent, align); |
| 996 | |
| 997 | input->seek(long(fc) + 8, librevenge::RVNG_SEEK_SET); |
| 998 | uint16_t dxaOffset = libwps::readU16(input); |
| 999 | uint16_t dxaSize = libwps::readU16(input); |
| 1000 | uint16_t dyaSize = libwps::readU16(input); |
| 1001 | |
| 1002 | input->seek(22, librevenge::RVNG_SEEK_CUR); |
| 1003 | uint16_t mx = libwps::readU16(input); |
| 1004 | uint16_t my = libwps::readU16(input); |
| 1005 | |
| 1006 | WPS_DEBUG_MSG(("MSWriteParser::readText object found %utwx%utw, offset %utw, mx=%u my=%u\n", dxaSize, dyaSize, dxaOffset, mx, my)); |
| 1007 | |
| 1008 | Vec2f size(Vec2f(dxaSize/1440.0f, dyaSize/1440.0f)); |
| 1009 | if ((mx > 10 && my > 10) && !(mx == 1000 && my == 1000)) |
| 1010 | { |
| 1011 | size[0] *= mx / 1000.0f; |
| 1012 | size[1] *= my / 1000.0f; |
| 1013 | } |
| 1014 | |
| 1015 | pos.setUnit(librevenge::RVNG_INCH); |
| 1016 | pos.setSize(size); |
| 1017 | |
| 1018 | Vec2f origin; |
| 1019 | if (dxaOffset && align == WPSPosition::XLeft) |
| 1020 | origin[0] = dxaOffset/1440.0f; |
| 1021 | origin[1] = lastObjectHeight; |
| 1022 | pos.setOrigin(origin); |
| 1023 | input->seek(long(fc), librevenge::RVNG_SEEK_SET); |
| 1024 | |
| 1025 | processObject(pos, fcLim); |
| 1026 | lastObjectHeight += pos.size()[1]; |
| 1027 | fc = paps->m_fcLim; |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | lastObjectHeight = 0.0f; |
| 1032 | |
| 1033 | while (fc >= chps->m_fcLim) |
| 1034 | { |
| 1035 | if (++chps == m_fontList.end()) |
| 1036 | { |
| 1037 | WPS_DEBUG_MSG(("MSWriteParser::readText CHP not found for offset %u\n", fc)); |
| 1038 | throw (libwps::ParseException()); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | MSWriteParserInternal::Paragraph para = *paps; |
| 1043 | if (paps->m_interLine>0) |
| 1044 | para.setInterline((paps->m_interLine * chps->m_size)/72., librevenge::RVNG_INCH, WPSParagraph::AtLeast); |
| 1045 | |
| 1046 | m_listener->setParagraph(para); |
| 1047 | m_listener->setFont(*chps); |
| 1048 | |
| 1049 | uint32_t lim = std::min(chps->m_fcLim, paps->m_fcLim); |
| 1050 | lim = std::min(lim, m_fcMac); |
| 1051 | |
| 1052 | while (fc < lim) |
| 1053 | { |
| 1054 | unsigned size = lim - fc; |
| 1055 | unsigned long read_bytes; |
| 1056 | |
| 1057 | input->seek(long(fc), librevenge::RVNG_SEEK_SET); |
| 1058 | const unsigned char *p = input->read(size, read_bytes); |
| 1059 | if (!p || read_bytes != size) |
| 1060 | { |
| 1061 | WPS_DEBUG_MSG(("MSWriteParser::readText failed to read\n")); |
| 1062 | throw (libwps::ParseException()); |
| 1063 | } |
| 1064 | |
| 1065 | if (chps->m_special) |
| 1066 | { |
| 1067 | insertSpecial(p[0], fc, paps->m_Location); |
| 1068 | size = 1; |
| 1069 | } |
| 1070 | else if (chps->m_footnote || chps->m_annotation) |
| 1071 | { |
| 1072 | if (paps->m_Location == MSWriteParserInternal::Paragraph::MAIN) |
| 1073 | { |
| 1074 | auto label = libwps_tools_win::Font::unicodeString(p, size, chps->m_encoding); |
| 1075 | insertNote(chps->m_annotation, fc, label); |
| 1076 | } |
| 1077 | } |
| 1078 | else |
| 1079 | { |
| 1080 | if (p[0] == 9 && skiptab) |
| 1081 | { |
| 1082 | size--; |
| 1083 | p++; |
| 1084 | fc++; |
| 1085 | } |
| 1086 | if (size && *p < ' ') |
| 1087 | { |
| 1088 | insertControl(*p, fc); |
| 1089 | size = 1; |
| 1090 | } |
| 1091 | else if (size) |
| 1092 | size = insertString(p, size, chps->m_encoding); |
| 1093 | } |
| 1094 | fc += size; |
| 1095 | |
| 1096 | skiptab = false; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | void MSWriteParser::insertSpecial(uint8_t val, uint32_t /*fc*/, MSWriteParserInternal::Paragraph::Location /*location*/) |
| 1102 | { |
| 1103 | if (val == 1) |
| 1104 | m_listener->insertField(WPSField(WPSField::PageNumber)); |
| 1105 | } |
| 1106 | |
| 1107 | void MSWriteParser::insertControl(uint8_t val, uint32_t /*fc*/) |
| 1108 | { |
| 1109 | switch (val) |
| 1110 | { |
| 1111 | case 9: |
| 1112 | m_listener->insertTab(); |
| 1113 | break; |
| 1114 | case 10: |
| 1115 | case 11: |
| 1116 | m_listener->insertEOL(); |
| 1117 | break; |
| 1118 | case 12: |
| 1119 | m_listener->insertBreak(WPS_PAGE_BREAK); |
| 1120 | break; |
| 1121 | case 30: |
| 1122 | m_listener->insertUnicode(0x20); |
| 1123 | break; |
| 1124 | case 31: // soft hyphen (ignored by Write) |
| 1125 | case 13: // carriage return |
| 1126 | break; |
| 1127 | default: |
| 1128 | // MS Write displays these as boxes |
| 1129 | m_listener->insertUnicode(0x25af); |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | unsigned MSWriteParser::insertString(const unsigned char *str, unsigned size, libwps_tools_win::Font::Type type) |
| 1135 | { |
| 1136 | unsigned len = 0; |
| 1137 | |
| 1138 | while (len < size && str[len] >= ' ') |
| 1139 | len++; |
| 1140 | m_listener->insertUnicodeString(libwps_tools_win::Font::unicodeString(str, len, type)); |
| 1141 | |
| 1142 | return len; |
| 1143 | } |
| 1144 | |
| 1145 | bool MSWriteParser::readString(std::string &res, unsigned long lastPos) |
| 1146 | { |
| 1147 | RVNGInputStreamPtr input = getInput(); |
| 1148 | unsigned size = libwps::readU32(input); |
| 1149 | if (static_cast<unsigned long>(input->tell()) + size > lastPos || !checkFilePosition(uint32_t(lastPos))) |
| 1150 | { |
| 1151 | WPS_DEBUG_MSG(("MSWriteParser::readString too short\n")); |
| 1152 | return false; |
| 1153 | } |
| 1154 | |
| 1155 | if (size == 0) |
| 1156 | { |
| 1157 | res = std::string(); |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | unsigned long read_bytes; |
| 1162 | const unsigned char *data = input->read(size, read_bytes); |
| 1163 | if (read_bytes != size) |
| 1164 | { |
| 1165 | WPS_DEBUG_MSG(("MSWriteParser::readString failed to read\n")); |
| 1166 | throw (libwps::ParseException()); |
| 1167 | } |
| 1168 | |
| 1169 | // Must be ascii |
| 1170 | for (unsigned i=0; i < size - 1; i++) |
| 1171 | { |
| 1172 | if (data[i] < ' ' || data[i] >= 0x7f) |
| 1173 | { |
| 1174 | WPS_DEBUG_MSG(("MSWriteParser::readString non-ascii found\n")); |
| 1175 | return false; |
| 1176 | } |
| 1177 | } |
| 1178 | if (data[size - 1]) |
| 1179 | return false; |
| 1180 | |
| 1181 | res = std::string(reinterpret_cast<const char *>(data), size - 1u); |
| 1182 | |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
| 1186 | void MSWriteParser::processObject(WPSPosition &pos, unsigned long lastPos) |
| 1187 | { |
| 1188 | if (!checkFilePosition(uint32_t(lastPos))) |
| 1189 | { |
| 1190 | WPS_DEBUG_MSG(("MSWriteParser::processObject last position is bad\n")); |
| 1191 | return; |
| 1192 | } |
| 1193 | RVNGInputStreamPtr input = getInput(); |
| 1194 | unsigned mm = libwps::readU16(input); |
| 1195 | |
| 1196 | if (mm == 0x88) // Windows metafile (.wmf) |
| 1197 | { |
| 1198 | // Step over unused fields |
| 1199 | input->seek(30, librevenge::RVNG_SEEK_CUR); |
| 1200 | |
| 1201 | unsigned cbSize = libwps::readU32(input); |
| 1202 | |
| 1203 | // Step over unused fields |
| 1204 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1205 | |
| 1206 | if (static_cast<unsigned long>(input->tell()) + cbSize > lastPos) |
| 1207 | { |
| 1208 | WPS_DEBUG_MSG(("MSWriteParser::processObject bad size for wmf\n")); |
| 1209 | return; |
| 1210 | } |
| 1211 | WPS_DEBUG_MSG(("MSWriteParser::processObject 3.0 WMF object\n")); |
| 1212 | |
| 1213 | librevenge::RVNGBinaryData wmfdata; |
| 1214 | if (processWMF(wmfdata, cbSize)) |
| 1215 | { |
| 1216 | m_listener->insertPicture(pos, wmfdata, "application/x-wmf"); |
| 1217 | } |
| 1218 | } |
| 1219 | else if (mm == 0xe3) // this is a picture |
| 1220 | { |
| 1221 | // Step over unused fields |
| 1222 | input->seek(16, librevenge::RVNG_SEEK_CUR); |
| 1223 | |
| 1224 | // read BITMAP structure |
| 1225 | unsigned width = libwps::readU16(input); |
| 1226 | unsigned height = libwps::readU16(input); |
| 1227 | unsigned byte_width = libwps::readU16(input); |
| 1228 | unsigned planes = libwps::readU8(input); |
| 1229 | unsigned bits_pixel = libwps::readU8(input); |
| 1230 | |
| 1231 | // Step over unused fields |
| 1232 | input->seek(6, librevenge::RVNG_SEEK_CUR); |
| 1233 | |
| 1234 | unsigned cbSize = libwps::readU32(input); |
| 1235 | |
| 1236 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 1237 | |
| 1238 | if (static_cast<unsigned long>(input->tell()) + cbSize > lastPos) |
| 1239 | { |
| 1240 | WPS_DEBUG_MSG(("MSWriteParser::processObject bad size for DDB\n")); |
| 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | WPS_DEBUG_MSG(("MSWriteParser::processObject 3.0 DDB object %ux%u\n", width, height)); |
| 1245 | |
| 1246 | librevenge::RVNGBinaryData bmpdata; |
| 1247 | if (processDDB(bmpdata, pos, width, height, byte_width, planes, bits_pixel, cbSize)) |
| 1248 | { |
| 1249 | m_listener->insertPicture(pos, bmpdata, "image/bmp"); |
| 1250 | } |
| 1251 | } |
| 1252 | else if (mm == 0xe4) // OLE object |
| 1253 | { |
| 1254 | // Step over unused fields |
| 1255 | input->seek(38, librevenge::RVNG_SEEK_CUR); |
| 1256 | |
| 1257 | unsigned ole_id = libwps::readU32(input); |
| 1258 | unsigned type = libwps::readU32(input); |
| 1259 | |
| 1260 | if (ole_id != 0x501) |
| 1261 | { |
| 1262 | WPS_DEBUG_MSG(("MSWriteParser::processObject Unknown ole identifier %x\n", ole_id)); |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | switch (type) |
| 1267 | { |
| 1268 | case 3: // static OLE |
| 1269 | { |
| 1270 | librevenge::RVNGBinaryData staticOle; |
| 1271 | std::string mimetype; |
| 1272 | if (processStaticOLE(staticOle, mimetype, pos, lastPos)) |
| 1273 | { |
| 1274 | m_listener->insertPicture(pos, staticOle, mimetype); |
| 1275 | } |
| 1276 | return; |
| 1277 | } |
| 1278 | case 2: // Embedded OLE |
| 1279 | processEmbeddedOLE(pos, lastPos); |
| 1280 | return; |
| 1281 | case 1: |
| 1282 | WPS_DEBUG_MSG(("MSWriteParser::processObject:OLE link\n")); |
| 1283 | return; |
| 1284 | default: |
| 1285 | WPS_DEBUG_MSG(("MSWriteParser::processObject:unknown ole type %d\n", type)); |
| 1286 | return; |
| 1287 | } |
| 1288 | } |
| 1289 | else |
| 1290 | { |
| 1291 | WPS_DEBUG_MSG(("MSWriteParser::processObject:Unknown object encountered (mm=%02x)\n", mm)); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | bool MSWriteParser::processDDB(librevenge::RVNGBinaryData &bmpdata, WPSPosition &pos, unsigned width, unsigned height, unsigned byte_width, unsigned planes, unsigned bits_pixel, unsigned size) |
| 1296 | { |
| 1297 | /* A ddb image has no color palette; it is BMP Version 1, but this format |
| 1298 | is not widely supported. Create BMP version 2 file with a color palette. |
| 1299 | */ |
| 1300 | static const MSWriteParserInternal::BitmapPalette pal1[2] = |
| 1301 | { |
| 1302 | { 0,0,0 }, { 255,255,255 } |
| 1303 | }; |
| 1304 | static const MSWriteParserInternal::BitmapPalette pal4[16] = |
| 1305 | { |
| 1306 | { 0,0,0 }, // black |
| 1307 | { 0,0,255 }, // Blue |
| 1308 | { 0,128,0 }, // Cyan |
| 1309 | { 0,255,0 }, // Green |
| 1310 | { 192,0,192 }, // Magenta |
| 1311 | { 255,0,0 }, // Red |
| 1312 | { 255,255,0 }, // Yellow |
| 1313 | { 255,255,255 }, // White |
| 1314 | |
| 1315 | { 0,0,128 }, // Dark blue |
| 1316 | { 0,128,128 }, // Dark cyan |
| 1317 | { 0,128,0 }, // Dark green |
| 1318 | { 128,0,128 }, // Dark magenta |
| 1319 | { 128,0,0 }, // Dark red |
| 1320 | { 192,192,0 }, // Dark yellow |
| 1321 | { 192,192,192 }, // Light gray |
| 1322 | { 128,128,128 }, // Dark gray |
| 1323 | }; |
| 1324 | static const MSWriteParserInternal::BitmapPalette pal8[256] = |
| 1325 | { |
| 1326 | { 0,0,0 }, { 128,0,0 }, { 0,128,0 }, { 128,128,0 }, |
| 1327 | { 0,0,128 }, { 128,0,128 }, { 0,128,128 }, { 192,192,192 }, |
| 1328 | { 192,220,192 }, { 166,202,240 }, { 42,63,170 }, { 42,63,255 }, |
| 1329 | { 42,95,0 }, { 42,95,85 }, { 42,95,170 }, { 42,95,255 }, |
| 1330 | { 42,127,0 }, { 42,127,85 }, { 42,127,170 }, { 42,127,255 }, |
| 1331 | { 42,159,0 }, { 42,159,85 }, { 42,159,170 }, { 42,159,255 }, |
| 1332 | { 42,191,0 }, { 42,191,85 }, { 42,191,170 }, { 42,191,255 }, |
| 1333 | { 42,223,0 }, { 42,223,85 }, { 42,223,170 }, { 42,223,255 }, |
| 1334 | { 42,255,0 }, { 42,255,85 }, { 42,255,170 }, { 42,255,255 }, |
| 1335 | { 85,0,0 }, { 85,0,85 }, { 85,0,170 }, { 85,0,255 }, |
| 1336 | { 85,31,0 }, { 85,31,85 }, { 85,31,170 }, { 85,31,255 }, |
| 1337 | { 85,63,0 }, { 85,63,85 }, { 85,63,170 }, { 85,63,255 }, |
| 1338 | { 85,95,0 }, { 85,95,85 }, { 85,95,170 }, { 85,95,255 }, |
| 1339 | { 85,127,0 }, { 85,127,85 }, { 85,127,170 }, { 85,127,255 }, |
| 1340 | { 85,159,0 }, { 85,159,85 }, { 85,159,170 }, { 85,159,255 }, |
| 1341 | { 85,191,0 }, { 85,191,85 }, { 85,191,170 }, { 85,191,255 }, |
| 1342 | { 85,223,0 }, { 85,223,85 }, { 85,223,170 }, { 85,223,255 }, |
| 1343 | { 85,255,0 }, { 85,255,85 }, { 85,255,170 }, { 85,255,255 }, |
| 1344 | { 127,0,0 }, { 127,0,85 }, { 127,0,170 }, { 127,0,255 }, |
| 1345 | { 127,31,0 }, { 127,31,85 }, { 127,31,170 }, { 127,31,255 }, |
| 1346 | { 127,63,0 }, { 127,63,85 }, { 127,63,170 }, { 127,63,255 }, |
| 1347 | { 127,95,0 }, { 127,95,85 }, { 127,95,170 }, { 127,95,255 }, |
| 1348 | { 127,127,0 }, { 127,127,85 }, { 127,127,170 }, { 127,127,255 }, |
| 1349 | { 127,159,0 }, { 127,159,85 }, { 127,159,170 }, { 127,159,255 }, |
| 1350 | { 127,191,0 }, { 127,191,85 }, { 127,191,170 }, { 127,191,255 }, |
| 1351 | { 127,223,0 }, { 127,223,85 }, { 127,223,170 }, { 127,223,255 }, |
| 1352 | { 127,255,0 }, { 127,255,85 }, { 127,255,170 }, { 127,255,255 }, |
| 1353 | { 170,0,0 }, { 170,0,85 }, { 170,0,170 }, { 170,0,255 }, |
| 1354 | { 170,31,0 }, { 170,31,85 }, { 170,31,170 }, { 170,31,255 }, |
| 1355 | { 170,63,0 }, { 170,63,85 }, { 170,63,170 }, { 170,63,255 }, |
| 1356 | { 170,95,0 }, { 170,95,85 }, { 170,95,170 }, { 170,95,255 }, |
| 1357 | { 170,127,0 }, { 170,127,85 }, { 170,127,170 }, { 170,127,255 }, |
| 1358 | { 170,159,0 }, { 170,159,85 }, { 170,159,170 }, { 170,159,255 }, |
| 1359 | { 170,191,0 }, { 170,191,85 }, { 170,191,170 }, { 170,191,255 }, |
| 1360 | { 170,223,0 }, { 170,223,85 }, { 170,223,170 }, { 170,223,255 }, |
| 1361 | { 170,255,0 }, { 170,255,85 }, { 170,255,170 }, { 170,255,255 }, |
| 1362 | { 212,0,0 }, { 212,0,85 }, { 212,0,170 }, { 212,0,255 }, |
| 1363 | { 212,31,0 }, { 212,31,85 }, { 212,31,170 }, { 212,31,255 }, |
| 1364 | { 212,63,0 }, { 212,63,85 }, { 212,63,170 }, { 212,63,255 }, |
| 1365 | { 212,95,0 }, { 212,95,85 }, { 212,95,170 }, { 212,95,255 }, |
| 1366 | { 212,127,0 }, { 212,127,85 }, { 212,127,170 }, { 212,127,255 }, |
| 1367 | { 212,159,0 }, { 212,159,85 }, { 212,159,170 }, { 212,159,255 }, |
| 1368 | { 212,191,0 }, { 212,191,85 }, { 212,191,170 }, { 212,191,255 }, |
| 1369 | { 212,223,0 }, { 212,223,85 }, { 212,223,170 }, { 212,223,255 }, |
| 1370 | { 212,255,0 }, { 212,255,85 }, { 212,255,170 }, { 212,255,255 }, |
| 1371 | { 255,0,85 }, { 255,0,170 }, { 255,31,0 }, { 255,31,85 }, |
| 1372 | { 255,31,170 }, { 255,31,255 }, { 255,63,0 }, { 255,63,85 }, |
| 1373 | { 255,63,170 }, { 255,63,255 }, { 255,95,0 }, { 255,95,85 }, |
| 1374 | { 255,95,170 }, { 255,95,255 }, { 255,127,0 }, { 255,127,85 }, |
| 1375 | { 255,127,170 }, { 255,127,255 }, { 255,159,0 }, { 255,159,85 }, |
| 1376 | { 255,159,170 }, { 255,159,255 }, { 255,191,0 }, { 255,191,85 }, |
| 1377 | { 255,191,170 }, { 255,191,255 }, { 255,223,0 }, { 255,223,85 }, |
| 1378 | { 255,223,170 }, { 255,223,255 }, { 255,255,85 }, { 255,255,170 }, |
| 1379 | { 204,204,255 }, { 255,204,255 }, { 51,255,255 }, { 102,255,255 }, |
| 1380 | { 153,255,255 }, { 204,255,255 }, { 0,127,0 }, { 0,127,85 }, |
| 1381 | { 0,127,170 }, { 0,127,255 }, { 0,159,0 }, { 0,159,85 }, |
| 1382 | { 0,159,170 }, { 0,159,255 }, { 0,191,0 }, { 0,191,85 }, |
| 1383 | { 0,191,170 }, { 0,191,255 }, { 0,223,0 }, { 0,223,85 }, |
| 1384 | { 0,223,170 }, { 0,223,255 }, { 0,255,85 }, { 0,255,170 }, |
| 1385 | { 42,0,0 }, { 42,0,85 }, { 42,0,170 }, { 42,0,255 }, |
| 1386 | { 42,31,0 }, { 42,31,85 }, { 42,31,170 }, { 42,31,255 }, |
| 1387 | { 42,63,0 }, { 42,63,85 }, { 255,251,240 }, { 160,160,164 }, |
| 1388 | { 128,128,128 }, { 255,0,0 }, { 0,255,0 }, { 255,255,0 }, |
| 1389 | { 0,0,255 }, { 255,0,255 }, { 0,255,255 }, { 255,255,255 } |
| 1390 | }; |
| 1391 | |
| 1392 | if (size < byte_width * height) |
| 1393 | { |
| 1394 | WPS_DEBUG_MSG(("MSWriteParser::processDDB DDB image data missing, skipping\n")); |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | if (planes != 1) |
| 1399 | { |
| 1400 | WPS_DEBUG_MSG(("MSWriteParser::processDDB DDB image has planes %d, must be 1\n", planes)); |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
| 1404 | WPS_DEBUG_MSG(("MSWriteParser::processDDB DDB size %ux%ux%u, byte width %u\n", width, height, |
| 1405 | bits_pixel, byte_width)); |
| 1406 | |
| 1407 | unsigned colors; |
| 1408 | switch (bits_pixel) |
| 1409 | { |
| 1410 | case 1: |
| 1411 | colors = 2; |
| 1412 | break; |
| 1413 | case 4: |
| 1414 | colors = 16; |
| 1415 | break; |
| 1416 | case 8: |
| 1417 | colors = 256; |
| 1418 | break; |
| 1419 | default: // no palette |
| 1420 | colors = 0; |
| 1421 | break; |
| 1422 | } |
| 1423 | |
| 1424 | unsigned offset = MSWriteParserInternal::BM_FILE_STRUCT_SIZE + MSWriteParserInternal::BM_INFO_V2_STRUCT_SIZE + colors * unsigned(sizeof(MSWriteParserInternal::BitmapPalette)); |
| 1425 | |
| 1426 | // File header |
| 1427 | bmpdata.append('B'); |
| 1428 | bmpdata.append('M'); |
| 1429 | MSWriteParserInternal::appendU32(bmpdata, offset + height * byte_width); |
| 1430 | MSWriteParserInternal::appendU32(bmpdata, 0); |
| 1431 | MSWriteParserInternal::appendU32(bmpdata, offset); |
| 1432 | |
| 1433 | // Info v2 header |
| 1434 | MSWriteParserInternal::appendU32(bmpdata, 12); |
| 1435 | MSWriteParserInternal::appendU16(bmpdata, uint16_t(width)); |
| 1436 | MSWriteParserInternal::appendU16(bmpdata, uint16_t(height)); |
| 1437 | MSWriteParserInternal::appendU16(bmpdata, uint16_t(planes)); |
| 1438 | MSWriteParserInternal::appendU16(bmpdata, uint16_t(bits_pixel)); |
| 1439 | |
| 1440 | switch (bits_pixel) |
| 1441 | { |
| 1442 | case 1: |
| 1443 | bmpdata.append(reinterpret_cast<const unsigned char *>(&pal1), sizeof(pal1)); |
| 1444 | break; |
| 1445 | case 4: |
| 1446 | bmpdata.append(reinterpret_cast<const unsigned char *>(&pal4), sizeof(pal4)); |
| 1447 | break; |
| 1448 | case 8: |
| 1449 | bmpdata.append(reinterpret_cast<const unsigned char *>(&pal8), sizeof(pal8)); |
| 1450 | break; |
| 1451 | default: |
| 1452 | break; |
| 1453 | } |
| 1454 | |
| 1455 | RVNGInputStreamPtr input = getInput(); |
| 1456 | unsigned long read_bytes; |
| 1457 | const unsigned char *data = input->read(size, read_bytes); |
| 1458 | if (read_bytes != size) |
| 1459 | { |
| 1460 | WPS_DEBUG_MSG(("MSWriteParser::processDDB failed to read object\n")); |
| 1461 | throw (libwps::ParseException()); |
| 1462 | } |
| 1463 | |
| 1464 | // Scanlines must be on 4-byte boundary, but some DDB images have |
| 1465 | // byte_width which is not on a 4-byte boundary. Also flip the image |
| 1466 | // vertically, DDB are stored upside down |
| 1467 | for (unsigned i=0; i<height; i++) |
| 1468 | { |
| 1469 | bmpdata.append(data + byte_width * (height - i - 1), byte_width); |
| 1470 | if (byte_width % 4) |
| 1471 | bmpdata.append(data, 4 - byte_width % 4); |
| 1472 | } |
| 1473 | |
| 1474 | // default is 96 dpi |
| 1475 | pos.setSize(Vec2f(float(width) / 96.0f, float(height) / 96.0f)); |
| 1476 | |
| 1477 | return true; |
| 1478 | } |
| 1479 | |
| 1480 | bool MSWriteParser::processDIB(librevenge::RVNGBinaryData &bmpdata, unsigned size) |
| 1481 | { |
| 1482 | if (size < MSWriteParserInternal::BM_INFO_V3_STRUCT_SIZE) |
| 1483 | { |
| 1484 | WPS_DEBUG_MSG(("MSWriteParser::processDIB DIB image missing data, skipping\n")); |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
| 1488 | RVNGInputStreamPtr input = getInput(); |
| 1489 | unsigned long read_bytes; |
| 1490 | const unsigned char *data = input->read(size, read_bytes); |
| 1491 | if (read_bytes != size) |
| 1492 | { |
| 1493 | WPS_DEBUG_MSG(("MSWriteParser::processDIB failed to read object\n")); |
| 1494 | throw (libwps::ParseException()); |
| 1495 | } |
| 1496 | |
| 1497 | if (WPS_LE_GET_GUINT32(data + MSWriteParserInternal::BM_INFO_V3_SIZE) != MSWriteParserInternal::BM_INFO_V3_STRUCT_SIZE) |
| 1498 | { |
| 1499 | WPS_DEBUG_MSG(("MSWriteParser::processDIB DIB header incorrect size, skipping\n")); |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
| 1503 | unsigned bits_pixel = WPS_LE_GET_GUINT16(data + MSWriteParserInternal::BM_INFO_V3_BITS_PIXEL); |
| 1504 | unsigned colors_used = WPS_LE_GET_GUINT16(data + MSWriteParserInternal::BM_INFO_V3_COLORS_USED); |
| 1505 | |
| 1506 | unsigned colors = 0; |
| 1507 | if (bits_pixel && bits_pixel <= 8) |
| 1508 | { |
| 1509 | colors = colors_used ? colors_used : 1 << bits_pixel; |
| 1510 | } |
| 1511 | |
| 1512 | bmpdata.append('B'); |
| 1513 | bmpdata.append('M'); |
| 1514 | MSWriteParserInternal::appendU32(bmpdata, size + MSWriteParserInternal::BM_FILE_STRUCT_SIZE); |
| 1515 | MSWriteParserInternal::appendU32(bmpdata, 0); |
| 1516 | MSWriteParserInternal::appendU32(bmpdata, MSWriteParserInternal::BM_FILE_STRUCT_SIZE + MSWriteParserInternal::BM_INFO_V3_STRUCT_SIZE + 4 * colors); |
| 1517 | |
| 1518 | bmpdata.append(data, size); |
| 1519 | |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
| 1523 | bool MSWriteParser::processStaticOLE(librevenge::RVNGBinaryData &b, std::string &mimetype, WPSPosition &pos, unsigned long lastPos) |
| 1524 | { |
| 1525 | RVNGInputStreamPtr input = getInput(); |
| 1526 | std::string objtype; |
| 1527 | |
| 1528 | if (!readString(objtype, lastPos)) |
| 1529 | { |
| 1530 | return false; |
| 1531 | } |
| 1532 | WPS_DEBUG_MSG(("MSWriteParser::processStaticOLE object %s\n", objtype.c_str())); |
| 1533 | |
| 1534 | // Step over unused fields |
| 1535 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1536 | |
| 1537 | unsigned cbSize = libwps::readU32(input); |
| 1538 | |
| 1539 | if (static_cast<unsigned long>(input->tell()) + cbSize > lastPos) |
| 1540 | { |
| 1541 | WPS_DEBUG_MSG(("MSWriteParser::processStaticOLE bad size\n")); |
| 1542 | return false; |
| 1543 | } |
| 1544 | |
| 1545 | if (objtype == "BITMAP") |
| 1546 | { |
| 1547 | if (cbSize < 10) |
| 1548 | { |
| 1549 | WPS_DEBUG_MSG(("MSWriteParser::processStaticOLE bad size for DDB\n")); |
| 1550 | return false; |
| 1551 | } |
| 1552 | input->seek(2, librevenge::RVNG_SEEK_CUR); |
| 1553 | unsigned width = libwps::readU16(input); |
| 1554 | unsigned height = libwps::readU16(input); |
| 1555 | unsigned byte_width = libwps::readU16(input); |
| 1556 | unsigned planes = libwps::readU8(input); |
| 1557 | unsigned bits_pixel = libwps::readU8(input); |
| 1558 | |
| 1559 | mimetype = "image/bmp"; |
| 1560 | |
| 1561 | return processDDB(b, pos, width, height, byte_width, planes, bits_pixel, cbSize - 10); |
| 1562 | } |
| 1563 | else if (objtype == "DIB") |
| 1564 | { |
| 1565 | mimetype = "image/bmp"; |
| 1566 | |
| 1567 | return processDIB(b, cbSize); |
| 1568 | } |
| 1569 | else if (objtype == "METAFILEPICT" && cbSize>8) |
| 1570 | { |
| 1571 | // Step over unused fields |
| 1572 | input->seek(8, librevenge::RVNG_SEEK_CUR); |
| 1573 | mimetype = "application/x-wmf"; |
| 1574 | return processWMF(b, cbSize - 8); |
| 1575 | } |
| 1576 | |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
| 1580 | void MSWriteParser::processEmbeddedOLE(WPSPosition &pos, unsigned long lastPos) |
| 1581 | { |
| 1582 | if (!checkFilePosition(uint32_t(lastPos))) |
| 1583 | { |
| 1584 | WPS_DEBUG_MSG(("MSWriteParser::processEmbeddedOLE last position is bad\n")); |
| 1585 | return; |
| 1586 | } |
| 1587 | RVNGInputStreamPtr input = getInput(); |
| 1588 | |
| 1589 | // First read entire Object including ole_id and type |
| 1590 | long startPos = input->tell(); |
| 1591 | input->seek(-8, librevenge::RVNG_SEEK_CUR); |
| 1592 | |
| 1593 | unsigned long read_bytes; |
| 1594 | unsigned long embeddedSize = lastPos - static_cast<unsigned long>(input->tell()); |
| 1595 | const unsigned char *data = input->read(embeddedSize, read_bytes); |
| 1596 | if (read_bytes != embeddedSize) |
| 1597 | { |
| 1598 | WPS_DEBUG_MSG(("MSWriteParser::processEmbeddedOLE: failed to read object\n")); |
| 1599 | throw (libwps::ParseException()); |
| 1600 | } |
| 1601 | |
| 1602 | librevenge::RVNGBinaryData embeddedOle; |
| 1603 | embeddedOle.append(data, embeddedSize); |
| 1604 | |
| 1605 | // Seek back to beginning; parse in search of replacement object |
| 1606 | input->seek(startPos, librevenge::RVNG_SEEK_SET); |
| 1607 | |
| 1608 | std::string strings[3]; |
| 1609 | unsigned i; |
| 1610 | |
| 1611 | for (i=0; i<3; i++) |
| 1612 | { |
| 1613 | if (!readString(strings[i], lastPos)) |
| 1614 | { |
| 1615 | return; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | WPS_DEBUG_MSG(("MSWriteParser::processEmbeddedOLE Embedded OLE object %s,filename=%s,parameter=%s\n", |
| 1620 | strings[0].c_str(), strings[1].c_str(), strings[2].c_str())); |
| 1621 | unsigned size = libwps::readU32(input); |
| 1622 | |
| 1623 | if (static_cast<unsigned long>(input->tell()) + size > lastPos) |
| 1624 | { |
| 1625 | WPS_DEBUG_MSG(("MSWriteParser::processEmbeddedOLE: bad size for object\n")); |
| 1626 | return; |
| 1627 | } |
| 1628 | |
| 1629 | if (strings[0] == "PBrush" || strings[0] == "Paint.Picture") |
| 1630 | { |
| 1631 | // fish out the bmp file |
| 1632 | data = input->read(size, read_bytes); |
| 1633 | if (read_bytes != size) |
| 1634 | { |
| 1635 | WPS_DEBUG_MSG(("MSWriteParser::processEmbeddedOLE: failed to read %s object\n", strings[0].c_str())); |
| 1636 | throw (libwps::ParseException()); |
| 1637 | } |
| 1638 | |
| 1639 | librevenge::RVNGBinaryData bmpdata(data, size); |
| 1640 | m_listener->insertPicture(pos, bmpdata, "image/bmp"); |
| 1641 | return; |
| 1642 | } |
| 1643 | |
| 1644 | input->seek(long(size), librevenge::RVNG_SEEK_CUR); |
| 1645 | |
| 1646 | WPSEmbeddedObject obj(embeddedOle, "object/ole"); |
| 1647 | unsigned ole_id = libwps::readU32(input); |
| 1648 | unsigned type = libwps::readU32(input); |
| 1649 | librevenge::RVNGBinaryData replacement; |
| 1650 | std::string mimetype; |
| 1651 | |
| 1652 | if (ole_id == 0x501 && type == 5 && processStaticOLE(replacement, mimetype, pos, lastPos)) |
| 1653 | { |
| 1654 | WPS_DEBUG_MSG(("MSWriteParser::processEmbeddedOLE found replacement object of mime %s\n", mimetype.c_str())); |
| 1655 | obj.add(replacement, mimetype); |
| 1656 | } |
| 1657 | m_listener->insertObject(pos, obj); |
| 1658 | } |
| 1659 | |
| 1660 | bool MSWriteParser::processWMF(librevenge::RVNGBinaryData &wmfdata, unsigned size) |
| 1661 | { |
| 1662 | WPS_DEBUG_MSG(("MSWriteParser::processWMF Windows metafile (wmf)\n")); |
| 1663 | |
| 1664 | |
| 1665 | RVNGInputStreamPtr input = getInput(); |
| 1666 | unsigned long read_bytes; |
| 1667 | const unsigned char *data = input->read(size, read_bytes); |
| 1668 | if (read_bytes != size) |
| 1669 | { |
| 1670 | WPS_DEBUG_MSG(("MSWriteParser::processWMF failed to read object\n")); |
| 1671 | throw (libwps::ParseException()); |
| 1672 | } |
| 1673 | |
| 1674 | wmfdata.append(data, size); |
| 1675 | |
| 1676 | return true; |
| 1677 | } |
| 1678 | |
| 1679 | void MSWriteParser::readSUMD() |
| 1680 | { |
| 1681 | // no metadata stored in MS Write |
| 1682 | } |
| 1683 | |
| 1684 | void MSWriteParser::readFNTB() |
| 1685 | { |
| 1686 | // no footnotes supported in MS Write |
| 1687 | } |
| 1688 | |
| 1689 | void MSWriteParser::readStructures() |
| 1690 | { |
| 1691 | RVNGInputStreamPtr input = getInput(); |
| 1692 | |
| 1693 | readFIB(); |
| 1694 | readFFNTB(); |
| 1695 | readSED(); |
| 1696 | readSUMD(); |
| 1697 | readFNTB(); |
| 1698 | |
| 1699 | input->seek(MSWriteParserInternal::HEADER_W_PNPARA, librevenge::RVNG_SEEK_SET); |
| 1700 | unsigned pnPara = libwps::readU16(input); |
| 1701 | readFOD(pnPara, &MSWriteParser::readPAP); |
| 1702 | |
| 1703 | if (m_paragraphList.empty()) |
| 1704 | { |
| 1705 | WPS_DEBUG_MSG(("MSWriteParser::parse: failed to read any PAP entries\n")); |
| 1706 | throw (libwps::ParseException()); |
| 1707 | } |
| 1708 | |
| 1709 | readFOD((m_fcMac + 127) / 128, &MSWriteParser::readCHP); |
| 1710 | if (m_fontList.empty()) |
| 1711 | { |
| 1712 | WPS_DEBUG_MSG(("MSWriteParser::parse: failed to read any CHP entries\n")); |
| 1713 | throw (libwps::ParseException()); |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | void MSWriteParser::insertNote(bool annotation, uint32_t fcPos, librevenge::RVNGString const &label) |
| 1718 | { |
| 1719 | std::vector<MSWriteParserInternal::Footnote>::iterator iter; |
| 1720 | |
| 1721 | for (iter = m_footnotes.begin(); iter != m_footnotes.end(); ++iter) |
| 1722 | { |
| 1723 | if (fcPos == iter->m_fcRef) |
| 1724 | { |
| 1725 | WPSEntry e; |
| 1726 | e.setBegin(long(iter->m_fcFtn)); |
| 1727 | if (++iter == m_footnotes.end()) |
| 1728 | { |
| 1729 | WPS_DEBUG_MSG(("MSWriteParser::insertNote missing sentinel footnote\n")); |
| 1730 | return; |
| 1731 | } |
| 1732 | e.setEnd(long(iter->m_fcFtn)); |
| 1733 | e.setType("TEXT"); |
| 1734 | |
| 1735 | if (e.valid() && e.end() <= long(m_fcMac)) |
| 1736 | { |
| 1737 | WPSSubDocumentPtr subdoc(new MSWriteParserInternal::SubDocument |
| 1738 | (getInput(), *this, e, MSWriteParserInternal::Paragraph::FOOTNOTE)); |
| 1739 | if (annotation) |
| 1740 | m_listener->insertComment(subdoc); |
| 1741 | else if (label.size()) |
| 1742 | m_listener->insertLabelNote(WPSContentListener::FOOTNOTE, label, subdoc); |
| 1743 | else |
| 1744 | m_listener->insertNote(WPSContentListener::FOOTNOTE, subdoc); |
| 1745 | } |
| 1746 | |
| 1747 | return; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | WPS_DEBUG_MSG(("MSWriteParser::insertNote note not found!\n")); |
| 1752 | } |
| 1753 | |
| 1754 | void MSWriteParser::startSection(MSWriteParserInternal::Section const §ion) |
| 1755 | { |
| 1756 | std::vector<int> widths; |
| 1757 | |
| 1758 | if (section.m_columns > 1) |
| 1759 | widths.resize(section.m_columns, int(section.m_dxaText * 1440.0)); |
| 1760 | |
| 1761 | m_listener->openSection(widths, librevenge::RVNG_TWIP); |
| 1762 | } |
| 1763 | |
| 1764 | void MSWriteParser::parse(librevenge::RVNGTextInterface *document) |
| 1765 | { |
| 1766 | readStructures(); |
| 1767 | |
| 1768 | m_listener = createListener(document); |
| 1769 | if (!m_listener) |
| 1770 | { |
| 1771 | WPS_DEBUG_MSG(("MSWriteParser::parse: can not create the listener\n")); |
| 1772 | |
| 1773 | throw (libwps::ParseException()); |
| 1774 | } |
| 1775 | |
| 1776 | m_listener->setMetaData(m_metaData); |
| 1777 | m_listener->startDocument(); |
| 1778 | |
| 1779 | for (auto § : m_sections) |
| 1780 | { |
| 1781 | startSection(sect); |
| 1782 | |
| 1783 | readText(sect.m_Main, MSWriteParserInternal::Paragraph::MAIN); |
| 1784 | m_listener->closeSection(); |
| 1785 | } |
| 1786 | |
| 1787 | m_listener->endDocument(); |
| 1788 | m_listener.reset(); |
| 1789 | } |
| 1790 | |
| 1791 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |