| 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) 2006, 2007 Andrew Ziem |
| 11 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * |
| 14 | * For minor contributions see the git repository. |
| 15 | * |
| 16 | * Alternatively, the contents of this file may be used under the terms |
| 17 | * of the GNU Lesser General Public License Version 2.1 or later |
| 18 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 19 | * applicable instead of those above. |
| 20 | */ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include <map> |
| 26 | |
| 27 | #include "libwps_internal.h" |
| 28 | #include "libwps_tools_win.h" |
| 29 | |
| 30 | #include "WPSEntry.h" |
| 31 | #include "WPSContentListener.h" |
| 32 | #include "WPSFont.h" |
| 33 | #include "WPSList.h" |
| 34 | #include "WPSParagraph.h" |
| 35 | |
| 36 | #include "WPS8.h" |
| 37 | #include "WPS8Struct.h" |
| 38 | #include "WPS8Text.h" |
| 39 | |
| 40 | #include "WPS8TextStyle.h" |
| 41 | |
| 42 | namespace WPS8TextStyleInternal |
| 43 | { |
| 44 | /** Internal: class to store font properties */ |
| 45 | struct Font final : public WPSFont |
| 46 | { |
| 47 | //! constructor |
| 48 | Font() |
| 49 | : WPSFont() |
| 50 | , m_special() { } |
| 51 | Font(Font const &)=default; |
| 52 | //! destructor |
| 53 | ~Font() final; |
| 54 | static Font def() |
| 55 | { |
| 56 | Font res; |
| 57 | res.m_name = "Times New Roman"; // checkme |
| 58 | res.m_size = 10; |
| 59 | return res; |
| 60 | } |
| 61 | |
| 62 | //! operator<< |
| 63 | friend std::ostream &operator<<(std::ostream &o, Font const &ft); |
| 64 | |
| 65 | //! return the auxilliary data |
| 66 | WPS8TextStyle::FontData &special() |
| 67 | { |
| 68 | return m_special; |
| 69 | } |
| 70 | //! return the auxilliary data |
| 71 | WPS8TextStyle::FontData const &special() const |
| 72 | { |
| 73 | return m_special; |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | /** the auxialliary data */ |
| 78 | WPS8TextStyle::FontData m_special; |
| 79 | }; |
| 80 | |
| 81 | Font::~Font() |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | //! operator<< for a font |
| 86 | std::ostream &operator<<(std::ostream &o, Font const &ft) |
| 87 | { |
| 88 | o << reinterpret_cast<WPSFont const &>(ft); |
| 89 | o << ft.m_special; |
| 90 | return o; |
| 91 | } |
| 92 | |
| 93 | /** Internal: the state of a WPS4Text */ |
| 94 | struct State |
| 95 | { |
| 96 | //! constructor |
| 97 | State() |
| 98 | : m_fontNames() |
| 99 | , m_defaultFont(Font::def()) |
| 100 | , m_fontList() |
| 101 | , m_defaultParagraph() |
| 102 | , m_paragraphList() |
| 103 | , m_fontTypes() |
| 104 | , m_paragraphTypes() |
| 105 | { |
| 106 | initTypeMaps(); |
| 107 | } |
| 108 | |
| 109 | //! initializes the type map |
| 110 | void initTypeMaps(); |
| 111 | |
| 112 | //! the font names |
| 113 | std::vector<librevenge::RVNGString> m_fontNames; |
| 114 | |
| 115 | //! the default font |
| 116 | Font m_defaultFont; |
| 117 | //! a list of all font properties |
| 118 | std::vector<Font> m_fontList; |
| 119 | //! the default paragraph |
| 120 | WPSParagraph m_defaultParagraph; |
| 121 | //! a list of all paragraph properties |
| 122 | std::vector<WPSParagraph> m_paragraphList; |
| 123 | |
| 124 | //! the character type |
| 125 | std::map<int,int> m_fontTypes; |
| 126 | //! the paragraph type |
| 127 | std::map<int,int> m_paragraphTypes; |
| 128 | }; |
| 129 | |
| 130 | void State::initTypeMaps() |
| 131 | { |
| 132 | static int const fontTypes[] = |
| 133 | { |
| 134 | 0, 0x12, 2, 0x2, 3, 0x2, 4, 0x2, 5, 0x2, |
| 135 | 0xc, 0x22, 0xf, 0x12, |
| 136 | 0x10, 0x2, 0x12, 0x22, 0x13, 0x2, 0x14, 0x2, 0x15, 0x2, 0x16, 0x2, 0x17, 0x2, |
| 137 | 0x18, 0x22, 0x1a, 0x22, 0x1b, 0x22, 0x1e, 0x12, |
| 138 | 0x22, 0x22, 0x23, 0x22, 0x24, 0x8A, |
| 139 | 0x2d, 0x2, 0x2e, 0x22, |
| 140 | }; |
| 141 | for (int i = 0; i+1 < int(WPS_N_ELEMENTS(fontTypes)); i+=2) |
| 142 | m_fontTypes[fontTypes[i]] = fontTypes[i+1]; |
| 143 | static int const paragraphTypes[] = |
| 144 | { |
| 145 | 2, 0x22, 3, 0x1A, 4, 0x12, 6, 0x22, |
| 146 | 0xc, 0x22, 0xd, 0x22, 0xe, 0x22, |
| 147 | 0x12, 0x22, 0x13, 0x22, 0x14, 0x22, 0x15, 0x22, 0x17, 0x2, |
| 148 | 0x18, 0x2, 0x19, 0x1A, 0x1b, 0x2, 0x1c, 0x2, 0x1d, 0x2, 0x1e, 0x12, 0x1f, 0x22, |
| 149 | 0x20, 0x12, 0x21, 0x22, 0x22, 0x22, 0x23, 0x22, 0x24, 0x22, 0x25, 0x12, |
| 150 | 0x2a, 0x12, |
| 151 | 0x31, 0x12, 0x32, 0x82, 0x33, 0x12, 0x34, 0x22 |
| 152 | }; |
| 153 | for (int i = 0; i+1 < int(WPS_N_ELEMENTS(paragraphTypes)); i+=2) |
| 154 | m_paragraphTypes[paragraphTypes[i]] = paragraphTypes[i+1]; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | //////////////////////////////////////////////////////////// |
| 159 | // constructor destructor |
| 160 | //////////////////////////////////////////////////////////// |
| 161 | WPS8TextStyle::WPS8TextStyle(WPS8Text &parser) |
| 162 | : m_mainParser(parser) |
| 163 | , m_input(parser.getInput()) |
| 164 | , m_listener() |
| 165 | , m_state() |
| 166 | , m_asciiFile(parser.ascii()) |
| 167 | { |
| 168 | m_state.reset(new WPS8TextStyleInternal::State); |
| 169 | } |
| 170 | |
| 171 | WPS8TextStyle::~WPS8TextStyle() |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | //////////////////////////////////////////////////////////// |
| 176 | // called to create the data: |
| 177 | //////////////////////////////////////////////////////////// |
| 178 | bool WPS8TextStyle::readStructures() |
| 179 | { |
| 180 | auto const &nameTable = m_mainParser.getNameEntryMap(); |
| 181 | |
| 182 | /* read fonts table */ |
| 183 | auto pos = nameTable.find("FONT"); |
| 184 | if (nameTable.end() == pos) |
| 185 | { |
| 186 | WPS_DEBUG_MSG(("WPS8TextStyle::parse: error: no FONT in header index table\n")); |
| 187 | return false; |
| 188 | } |
| 189 | readFontNames(pos->second); |
| 190 | |
| 191 | // find the FDDP and FDPC positions |
| 192 | for (int st = 0; st < 2; st++) |
| 193 | { |
| 194 | std::vector<WPSEntry> zones; |
| 195 | if (!findFDPStructures(st, zones)) |
| 196 | findFDPStructuresByHand(st, zones); |
| 197 | |
| 198 | std::vector<WPS8Text::DataFOD> fdps; |
| 199 | WPS8Text::FDPParser parser = st==0 ? WPS8Text::FDPParser(&WPS8Text::readParagraph) |
| 200 | : WPS8Text::FDPParser(&WPS8Text::readFont); |
| 201 | for (auto const &z : zones) |
| 202 | m_mainParser.readFDP(z, fdps, parser); |
| 203 | m_mainParser.m_FODList = m_mainParser.mergeSortedFODLists(m_mainParser.m_FODList, fdps); |
| 204 | } |
| 205 | // read SGP zone |
| 206 | pos = nameTable.lower_bound("SGP "); |
| 207 | while (nameTable.end() != pos) |
| 208 | { |
| 209 | WPSEntry const &entry = pos++->second; |
| 210 | if (!entry.hasName("SGP ")) break; |
| 211 | if (!entry.hasType("SGP ")) continue; |
| 212 | |
| 213 | readSGP(entry); |
| 214 | } |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | //////////////////////////////////////////////////////////// |
| 220 | // the fontname: |
| 221 | //////////////////////////////////////////////////////////// |
| 222 | bool WPS8TextStyle::readFontNames(WPSEntry const &entry) |
| 223 | { |
| 224 | if (!entry.hasType(entry.name())) |
| 225 | { |
| 226 | WPS_DEBUG_MSG(("WPS8TextStyle::readFontNames: name=%s, type=%s\n", |
| 227 | entry.name().c_str(), entry.type().c_str())); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | if (entry.length() < 20) |
| 232 | { |
| 233 | WPS_DEBUG_MSG(("WPS8TextStyle::readFontNames: length=0x%ld\n", entry.length())); |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | long debPos = entry.begin(); |
| 238 | m_input->seek(debPos, librevenge::RVNG_SEEK_SET); |
| 239 | |
| 240 | auto len = long(libwps::readU32(m_input)); // len + 0x14 = size |
| 241 | auto n_fonts = size_t(libwps::readU32(m_input)); |
| 242 | |
| 243 | if (long(4*n_fonts) > len) |
| 244 | { |
| 245 | WPS_DEBUG_MSG(("WPS8TextStyle::readFontNames: number=%d\n", int(n_fonts))); |
| 246 | return false; |
| 247 | } |
| 248 | if (len > entry.length() - 20) |
| 249 | { |
| 250 | WPS_DEBUG_MSG(("WPS8TextStyle::readFontNames: invalid lenth=%ld\n", len)); |
| 251 | return false; |
| 252 | } |
| 253 | libwps::DebugStream f; |
| 254 | |
| 255 | entry.setParsed(); |
| 256 | f << "N=" << n_fonts; |
| 257 | if (len+20 != entry.length()) f << ", ###L=" << std::hex << len+0x14; |
| 258 | |
| 259 | f << ", unkn=(" << std::hex; |
| 260 | for (int i = 0; i < 3; i++) f << libwps::readU32(m_input) << ", "; |
| 261 | f << "), dec=["; |
| 262 | for (int i = 0; i < int(n_fonts); i++) f << ", " << libwps::read32(m_input); |
| 263 | f << "]" << std::dec; |
| 264 | |
| 265 | ascii().addPos(debPos); |
| 266 | ascii().addNote(f.str().c_str()); |
| 267 | |
| 268 | long pageEnd = entry.end(); |
| 269 | |
| 270 | /* read each font in the table */ |
| 271 | while (m_input->tell() > 0 && m_state->m_fontNames.size() < n_fonts) |
| 272 | { |
| 273 | debPos = m_input->tell(); |
| 274 | if (debPos+6 > long(pageEnd)) break; |
| 275 | |
| 276 | auto string_size = int(libwps::readU16(m_input)); |
| 277 | if (debPos+2*string_size+6 > long(pageEnd)) break; |
| 278 | |
| 279 | librevenge::RVNGString s; |
| 280 | for (; string_size>0; string_size--) // checkme |
| 281 | s.append(char(libwps::readU16(m_input))); |
| 282 | |
| 283 | f.str(""); |
| 284 | f << "FONT("<<m_state->m_fontNames.size()<<"): " << s.cstr(); |
| 285 | f << ", unkn=("; |
| 286 | for (int i = 0; i < 4; i++) f << int(libwps::read8(m_input)) << ", "; |
| 287 | f << ")"; |
| 288 | ascii().addPos(debPos); |
| 289 | ascii().addNote(f.str().c_str()); |
| 290 | |
| 291 | m_state->m_fontNames.push_back(s); |
| 292 | } |
| 293 | |
| 294 | if (m_state->m_fontNames.size() != n_fonts) |
| 295 | { |
| 296 | WPS_DEBUG_MSG(("WPS8TextStyle::readFontNames: expected %i fonts but only found %i\n", |
| 297 | int(n_fonts), int(m_state->m_fontNames.size()))); |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | //////////////////////////////////////////////////////////// |
| 303 | // style general property |
| 304 | //////////////////////////////////////////////////////////// |
| 305 | bool WPS8TextStyle::readSGP(WPSEntry const &entry) |
| 306 | { |
| 307 | if (!entry.hasType(entry.name())) |
| 308 | { |
| 309 | WPS_DEBUG_MSG(("WPS8TextStyle::readSGP: warning: SGP name=%s, type=%s\n", |
| 310 | entry.name().c_str(), entry.type().c_str())); |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | long page_offset = entry.begin(); |
| 315 | long length = entry.length(); |
| 316 | long endPage = entry.end(); |
| 317 | |
| 318 | if (length < 2) |
| 319 | { |
| 320 | WPS_DEBUG_MSG(("WPS8TextStyle::readSGP: warning: SGP length=0x%lx\n", static_cast<unsigned long>(length))); |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | entry.setParsed(); |
| 325 | m_input->seek(page_offset, librevenge::RVNG_SEEK_SET); |
| 326 | |
| 327 | libwps::DebugStream f; |
| 328 | if (libwps::read16(m_input) != length) |
| 329 | { |
| 330 | WPS_DEBUG_MSG(("WPS8TextStyle::readSGP: invalid length=%ld\n", length)); |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | WPS8Struct::FileData mainData; |
| 335 | std::string error; |
| 336 | bool readOk=readBlockData(m_input, endPage,mainData, error); |
| 337 | for (auto const &dt : mainData.m_recursData) |
| 338 | { |
| 339 | if (dt.isBad()) continue; |
| 340 | if (dt.id() == 0) |
| 341 | f << "tabSep[default]=" << float(dt.m_value)/914400.f << "(inches),"; |
| 342 | else |
| 343 | f << "###" << dt << ","; |
| 344 | } |
| 345 | |
| 346 | if (!readOk) f << "###or [" << mainData << "]"; |
| 347 | |
| 348 | ascii().addPos(page_offset); |
| 349 | ascii().addNote(f.str().c_str()); |
| 350 | |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | //////////////////////////////////////////////////////////// |
| 355 | // font |
| 356 | //////////////////////////////////////////////////////////// |
| 357 | bool WPS8TextStyle::readFont(long endPos, int &id, std::string &mess) |
| 358 | { |
| 359 | auto font=WPS8TextStyleInternal::Font::def(); |
| 360 | |
| 361 | long actPos = m_input->tell(); |
| 362 | long size = endPos - actPos; |
| 363 | |
| 364 | /* other than blank, the shortest should be 2 bytes */ |
| 365 | if (size && size < 2) |
| 366 | { |
| 367 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: error: charProperty size < 2\n")); |
| 368 | return false; |
| 369 | } |
| 370 | if (size && (size%2) == 1) |
| 371 | { |
| 372 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: error: charProperty size is odd\n")); |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | WPS8Struct::FileData mainData; |
| 377 | std::string error; |
| 378 | |
| 379 | bool readOk= size ? readBlockData(m_input, endPos, mainData, error) : true; |
| 380 | |
| 381 | libwps::DebugStream f; |
| 382 | if (mainData.m_value) f << "unk=" << mainData.m_value << ","; |
| 383 | |
| 384 | uint32_t textAttributeBits = 0; |
| 385 | for (auto const &data : mainData.m_recursData) |
| 386 | { |
| 387 | if (data.isBad()) continue; |
| 388 | if (m_state->m_fontTypes.find(data.id())==m_state->m_fontTypes.end()) |
| 389 | { |
| 390 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: unexpected id %d\n", data.id())); |
| 391 | f << "##" << data << ","; |
| 392 | continue; |
| 393 | } |
| 394 | if (m_state->m_fontTypes.find(data.id())->second != data.type()) |
| 395 | { |
| 396 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: unexpected type for %d\n", data.id())); |
| 397 | f << "###" << data << ","; |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | switch (data.id()) |
| 402 | { |
| 403 | case 0x0: |
| 404 | font.special().m_type = int(data.m_value); |
| 405 | break; |
| 406 | case 0x02: |
| 407 | if (data.isTrue()) |
| 408 | textAttributeBits |= WPS_BOLD_BIT; |
| 409 | else |
| 410 | f << "#bold=false,"; |
| 411 | break; |
| 412 | case 0x03: |
| 413 | if (data.isTrue()) |
| 414 | textAttributeBits |= WPS_ITALICS_BIT; |
| 415 | else |
| 416 | f << "#it=false,"; |
| 417 | break; |
| 418 | case 0x04: |
| 419 | if (data.isTrue()) |
| 420 | textAttributeBits |= WPS_OUTLINE_BIT; |
| 421 | else |
| 422 | f << "#outline=false,"; |
| 423 | break; |
| 424 | case 0x05: |
| 425 | if (data.isTrue()) |
| 426 | textAttributeBits |= WPS_SHADOW_BIT; |
| 427 | else |
| 428 | f << "#shadow=false,"; |
| 429 | break; |
| 430 | case 0x0c: |
| 431 | font.m_size = int(data.m_value/12700); |
| 432 | break; |
| 433 | case 0x0f: |
| 434 | if (data.m_value == 1) textAttributeBits |= WPS_SUPERSCRIPT_BIT; |
| 435 | else if (data.m_value == 2) textAttributeBits |= WPS_SUBSCRIPT_BIT; |
| 436 | else f << "###sub/supScript=" << data.m_value << ","; |
| 437 | break; |
| 438 | case 0x10: |
| 439 | if (data.isTrue()) |
| 440 | textAttributeBits |= WPS_STRIKEOUT_BIT; |
| 441 | else |
| 442 | f << "#strikeout=false,"; |
| 443 | break; |
| 444 | case 0x12: |
| 445 | font.m_languageId = int(data.m_value); |
| 446 | break; |
| 447 | case 0x13: |
| 448 | if (data.isTrue()) |
| 449 | textAttributeBits |= WPS_SMALL_CAPS_BIT; |
| 450 | else |
| 451 | f << "#smallbit=false,"; |
| 452 | break; |
| 453 | case 0x14: |
| 454 | if (data.isTrue()) |
| 455 | textAttributeBits |= WPS_ALL_CAPS_BIT; |
| 456 | else |
| 457 | f << "#allcaps=false,"; |
| 458 | break; |
| 459 | case 0x16: |
| 460 | if (data.isTrue()) |
| 461 | textAttributeBits |= WPS_EMBOSS_BIT; |
| 462 | else |
| 463 | f << "#emboss=false,"; |
| 464 | break; |
| 465 | case 0x17: |
| 466 | if (data.isTrue()) |
| 467 | textAttributeBits |= WPS_ENGRAVE_BIT; |
| 468 | else |
| 469 | f << "#engrave=false,"; |
| 470 | break; |
| 471 | case 0x18: // 0 or 0.25 |
| 472 | f << "##f24(inches)=" << double(data.m_value)/914400 << ","; |
| 473 | break; |
| 474 | case 0x1b: // -3175 |
| 475 | if (data.m_value == -3175) f << "##f27,"; |
| 476 | else f << "##f27=" << data.m_value << ","; |
| 477 | break; |
| 478 | case 0x1e: |
| 479 | { |
| 480 | bool single = true; |
| 481 | switch (data.m_value) |
| 482 | { |
| 483 | case 1: |
| 484 | break; // normal |
| 485 | case 2: |
| 486 | f << "underl[word],"; |
| 487 | break; |
| 488 | case 3: |
| 489 | single = false; |
| 490 | break; // double |
| 491 | case 4: |
| 492 | f << "underl[dot],"; |
| 493 | break; |
| 494 | case 6: |
| 495 | f << "underl[w=2],"; |
| 496 | break; |
| 497 | case 7: |
| 498 | f << "underl[dashed],"; |
| 499 | break; |
| 500 | case 9: |
| 501 | f << "underl[style=.-],"; |
| 502 | break; |
| 503 | case 10: |
| 504 | f << "underl[style=..-],"; |
| 505 | break; |
| 506 | case 11: |
| 507 | f << "underl[curve],"; |
| 508 | break; |
| 509 | case 16: |
| 510 | f << "underl[curve2],"; |
| 511 | break; |
| 512 | case 17: |
| 513 | f << "underl[dot,w=2],"; |
| 514 | break; |
| 515 | case 18: |
| 516 | f << "underl[dashed,w=2],"; |
| 517 | break; |
| 518 | case 19: |
| 519 | f << "underl[style=.-,w=2],"; |
| 520 | break; |
| 521 | case 20: |
| 522 | f << "underl[style=..-,w=2],"; |
| 523 | break; |
| 524 | case 21: |
| 525 | f << "underl[dashed,l*=2],"; |
| 526 | break; // length*=2 |
| 527 | case 22: |
| 528 | f << "underl[dashed,w=2,l*=2],"; |
| 529 | break; |
| 530 | case 23: |
| 531 | f << "underl[curve]"; |
| 532 | single = false; |
| 533 | break; // and double |
| 534 | default: |
| 535 | f << "###underlFlag=" << data.m_value << ","; |
| 536 | } |
| 537 | if (single) |
| 538 | textAttributeBits |= WPS_UNDERLINE_BIT; |
| 539 | else |
| 540 | textAttributeBits |= WPS_DOUBLE_UNDERLINE_BIT; |
| 541 | break; |
| 542 | } |
| 543 | case 0x22: |
| 544 | font.special().m_fieldType = int(data.m_value); |
| 545 | break; |
| 546 | case 0x23: |
| 547 | font.special().m_fieldFormat = int(data.m_value); |
| 548 | break; |
| 549 | case 0x24: |
| 550 | { |
| 551 | if ((!data.isRead() && !data.readArrayBlock() && data.m_recursData.size() == 0) || |
| 552 | !data.isArray()) |
| 553 | { |
| 554 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: can not read font array\n")); |
| 555 | f << "###fontPb"; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | if (data.m_recursData.empty() || data.m_recursData[0].isBad() || data.m_recursData[0].type() != 0x18) |
| 560 | { |
| 561 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: can not read font id\n")); |
| 562 | f << "###f24=[" << data << "]"; |
| 563 | break; |
| 564 | } |
| 565 | auto fontId = uint8_t(data.m_recursData[0].m_value); |
| 566 | if (fontId < m_state->m_fontNames.size()) |
| 567 | |
| 568 | font.m_name = m_state->m_fontNames[fontId]; |
| 569 | else |
| 570 | { |
| 571 | WPS_DEBUG_MSG(("WPS8TextStyle::readFont: can not read find font %d\n", int(fontId))); |
| 572 | } |
| 573 | std::vector<int> formats; |
| 574 | for (auto const &subD : data.m_recursData) |
| 575 | { |
| 576 | if (subD.isBad()) continue; |
| 577 | int formId = subD.id() >> 3; |
| 578 | int sId = subD.id() & 0x7; |
| 579 | if (sId == 0) |
| 580 | { |
| 581 | formats.resize(size_t(formId)+1,-1); |
| 582 | formats[size_t(formId)] = int(subD.m_value); |
| 583 | } |
| 584 | else |
| 585 | f << "###formats"<<formId<<"." << sId << "=" << subD << ","; |
| 586 | } |
| 587 | // CHECKME: list of font ids .... |
| 588 | f << "formats=[" << std::hex; |
| 589 | for (size_t i = 0; i < formats.size(); i++) |
| 590 | { |
| 591 | if (formats[i] != -1) |
| 592 | f << "f" << i << "=" << formats[i] << ","; |
| 593 | } |
| 594 | f << "],"; |
| 595 | break; |
| 596 | } |
| 597 | case 0x2e: |
| 598 | font.m_color=data.getRGBColor(); |
| 599 | break; |
| 600 | default: |
| 601 | f << "##" << data << ","; |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | font.m_attributes = textAttributeBits; |
| 607 | if (!readOk) f << ", ###or mainData=[" << mainData << "]"; |
| 608 | font.m_extra = f.str(); |
| 609 | font.m_extra += error; |
| 610 | |
| 611 | id = int(m_state->m_fontList.size()); |
| 612 | m_state->m_fontList.push_back(font); |
| 613 | f.str(""); |
| 614 | f << font; |
| 615 | mess = f.str(); |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | void WPS8TextStyle::sendFont(int fId, WPS8TextStyle::FontData &data) |
| 620 | { |
| 621 | if (fId >= int(m_state->m_fontList.size())) |
| 622 | { |
| 623 | data = WPS8TextStyle::FontData(); |
| 624 | WPS_DEBUG_MSG(("WPS8TextStyle::sendFont: can not find font id %d\n", fId)); |
| 625 | return; |
| 626 | } |
| 627 | auto const &font = fId < 0 ? m_state->m_defaultFont : m_state->m_fontList[size_t(fId)]; |
| 628 | data = font.special(); |
| 629 | if (m_listener) |
| 630 | m_listener->setFont(font); |
| 631 | } |
| 632 | |
| 633 | //////////////////////////////////////////////////////////// |
| 634 | // font |
| 635 | //////////////////////////////////////////////////////////// |
| 636 | bool WPS8TextStyle::readParagraph(long endPos, int &id, std::string &mess) |
| 637 | { |
| 638 | long actPos = m_input->tell(); |
| 639 | long size = endPos - actPos; |
| 640 | |
| 641 | /* other than blank, the shortest should be 2 bytes */ |
| 642 | if (size && (size%2) == 1) |
| 643 | { |
| 644 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph: paraProperty size is odd\n")); |
| 645 | return false; |
| 646 | } |
| 647 | |
| 648 | libwps::DebugStream f; |
| 649 | WPS8Struct::FileData mainData; |
| 650 | std::string error; |
| 651 | |
| 652 | bool readOk= size ? readBlockData(m_input, endPos, mainData, error) : true; |
| 653 | if (mainData.m_value) f << "unk=" << mainData.m_value << ","; |
| 654 | |
| 655 | WPSParagraph para; |
| 656 | WPSColor paraColor[] = { WPSColor::black(), WPSColor::white() }; |
| 657 | for (auto const &data : mainData.m_recursData) |
| 658 | { |
| 659 | if (data.isBad()) continue; |
| 660 | if (m_state->m_paragraphTypes.find(data.id())==m_state->m_paragraphTypes.end()) |
| 661 | { |
| 662 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph: unexpected id %d\n", data.id())); |
| 663 | f << "###" << data << ","; |
| 664 | continue; |
| 665 | } |
| 666 | if (m_state->m_paragraphTypes.find(data.id())->second != data.type()) |
| 667 | { |
| 668 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph: unexpected type for %d\n", data.id())); |
| 669 | f << "###" << data << ","; |
| 670 | continue; |
| 671 | } |
| 672 | bool ok = true; |
| 673 | switch (data.id()) |
| 674 | { |
| 675 | /* case 0x2: what?=data.m_value/914400.; */ |
| 676 | case 0x3: |
| 677 | switch (data.m_value) |
| 678 | { |
| 679 | case 2: // not frequent: only found one time |
| 680 | f << "#type[bullet]=2,"; |
| 681 | WPS_FALLTHROUGH; |
| 682 | case 1: // the normal case |
| 683 | para.m_listLevelIndex = 1; |
| 684 | para.m_listLevel.m_type = libwps::BULLET; |
| 685 | para.m_listLevel.m_bullet.clear(); |
| 686 | libwps::appendUnicode(0x2022, para.m_listLevel.m_bullet); |
| 687 | break; |
| 688 | default: |
| 689 | f << "###bullet=" << data.m_value << ","; |
| 690 | break; |
| 691 | } |
| 692 | break; |
| 693 | case 0x4: |
| 694 | switch (data.m_value) |
| 695 | { |
| 696 | case 0: |
| 697 | para.m_justify = libwps::JustificationLeft; |
| 698 | break; |
| 699 | case 1: |
| 700 | para.m_justify = libwps::JustificationRight; |
| 701 | break; |
| 702 | case 2: |
| 703 | para.m_justify = libwps::JustificationCenter; |
| 704 | break; |
| 705 | case 3: |
| 706 | para.m_justify = libwps::JustificationFull; |
| 707 | break; |
| 708 | default: |
| 709 | f << "#just=" << std::hex << data.m_value << std::dec << ","; |
| 710 | para.m_justify = libwps::JustificationLeft; |
| 711 | } |
| 712 | break; |
| 713 | case 0x6: |
| 714 | para.m_listLevel.m_labelIndent = double(data.m_value)/914400; |
| 715 | break; |
| 716 | case 0xc: // first line indentation (6*152400 unit = 1 inches) |
| 717 | para.m_margins[0] = double(data.m_value)/914400; |
| 718 | break; |
| 719 | case 0xd: // left indentation |
| 720 | para.m_margins[1] = double(data.m_value)/914400; |
| 721 | break; |
| 722 | case 0xe: // right indentation |
| 723 | para.m_margins[2] = double(data.m_value)/914400; |
| 724 | break; |
| 725 | |
| 726 | case 0x12: // before line spacing 152400 -> 1 line |
| 727 | para.m_spacings[1] = double(data.m_value)/152400; |
| 728 | break; |
| 729 | case 0x13: // after line spacing 152400 -> 1 line |
| 730 | para.m_spacings[2] = double(data.m_value)/152400; |
| 731 | break; |
| 732 | // case 0x15(type22): one time with value 0x29 |
| 733 | case 0x14: |
| 734 | { |
| 735 | // link to bullet or numbering |
| 736 | |
| 737 | // first check if this can be a numbering level |
| 738 | auto suffixId = int(data.m_value >> 16); // 0 -> . 2->) 3-> ?? |
| 739 | int type = data.m_value & 0xFFFF; |
| 740 | |
| 741 | if (data.m_value && suffixId >= 0 && suffixId < 5 && type >= 0 && type <= 6) |
| 742 | { |
| 743 | para.m_listLevelIndex = 1; |
| 744 | /* this seems to implies that we restart a list */ |
| 745 | if (para.m_listLevel.m_type == libwps::BULLET) |
| 746 | para.m_listLevel.m_startValue=1; |
| 747 | |
| 748 | switch (type) |
| 749 | { |
| 750 | case 0: |
| 751 | para.m_listLevel.m_type = libwps::NONE; |
| 752 | break; |
| 753 | case 3: |
| 754 | para.m_listLevel.m_type = libwps::LOWERCASE; |
| 755 | break; |
| 756 | case 4: |
| 757 | para.m_listLevel.m_type = libwps::UPPERCASE; |
| 758 | break; |
| 759 | case 5: |
| 760 | para.m_listLevel.m_type = libwps::LOWERCASE_ROMAN; |
| 761 | break; |
| 762 | case 6: |
| 763 | para.m_listLevel.m_type = libwps::UPPERCASE_ROMAN; |
| 764 | break; |
| 765 | default: |
| 766 | f << "#bullet/type=" << type << ","; |
| 767 | WPS_FALLTHROUGH; |
| 768 | case 2: |
| 769 | para.m_listLevel.m_type = libwps::ARABIC; |
| 770 | break; |
| 771 | } |
| 772 | switch (suffixId) |
| 773 | { |
| 774 | case 0: |
| 775 | para.m_listLevel.m_suffix = ")"; |
| 776 | break; |
| 777 | case 2: |
| 778 | para.m_listLevel.m_suffix = "."; |
| 779 | break; |
| 780 | case 3: |
| 781 | para.m_listLevel.m_suffix = "-"; |
| 782 | break; // checkme |
| 783 | default: |
| 784 | f << "#bullet/suffix=" << suffixId << ","; |
| 785 | } |
| 786 | } |
| 787 | /** Note: I also find a val 0x2D which seems to have some sense |
| 788 | in correspondance with field 15 (val 0x29) and field 1c */ |
| 789 | else if (para.m_listLevel.m_type == libwps::BULLET) |
| 790 | { |
| 791 | para.m_listLevel.m_bullet.clear(); |
| 792 | uint32_t code = 0; |
| 793 | if (data.m_value <= 255) |
| 794 | code = uint32_t(libwps_tools_win::Font::unicode(static_cast<unsigned char>(data.m_value), libwps_tools_win::Font::WIN3_WINGDINGS)); |
| 795 | if (!code) |
| 796 | { |
| 797 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph: can not find bullet code\n")); |
| 798 | f << "#bullet[code]=" << std::hex << data.m_value << "," << std::dec; |
| 799 | code = 0x2022; |
| 800 | } |
| 801 | else if (code >= 0x10000) |
| 802 | code = 0x2022; // this unicode char may not print ok |
| 803 | libwps::appendUnicode(code, para.m_listLevel.m_bullet); |
| 804 | } |
| 805 | else |
| 806 | f << "##bullet/numb=" << std::hex << data.m_value << "," << std::dec; |
| 807 | break; |
| 808 | } |
| 809 | case 0x15: |
| 810 | if (para.m_listLevel.isNumeric() && data.m_value >= 0) |
| 811 | para.m_listLevel.m_startValue = int(data.m_value); |
| 812 | else |
| 813 | // can also be present in the lines preceding/following a line's list : ok |
| 814 | // do we need to use this value as to compute the next starting value ? |
| 815 | f << "#bullet/startValue?=" << std::hex << data.m_value << std::dec << ","; |
| 816 | break; |
| 817 | case 0x17: |
| 818 | f << "modBord,"; |
| 819 | break; |
| 820 | case 0x18: |
| 821 | f << "modTabs,"; |
| 822 | break; |
| 823 | // case 0x19(type1a): number between 1 and 6 : stylesheet index ? |
| 824 | case 0x1b: |
| 825 | if (data.m_value == 1) f << "bColType=rgb?,"; |
| 826 | else f << "#bColType=" << std::hex << data.m_value << std::dec << ","; |
| 827 | break; |
| 828 | case 0x1d: |
| 829 | f << "##f29Set,"; |
| 830 | break; // present if f42(2a) exist ? |
| 831 | case 0x1e: // not filled by word 2000 ? |
| 832 | // 1 -> Left, 2 -> right, 4-> top, 8->bottom |
| 833 | if (data.m_value & 1) para.m_border |= WPSBorder::TopBit; |
| 834 | if (data.m_value & 2) para.m_border |= WPSBorder::BottomBit; |
| 835 | if (data.m_value & 4) para.m_border |= WPSBorder::LeftBit; |
| 836 | if (data.m_value & 8) para.m_border |= WPSBorder::RightBit; |
| 837 | if (data.m_value & 0xF0) |
| 838 | f << "#border=" << std::hex << (data.m_value & 0xF0) << std::dec << ","; |
| 839 | break; |
| 840 | case 0x1f: |
| 841 | para.m_borderStyle.m_color=data.getRGBColor(); |
| 842 | break; |
| 843 | case 0x20: |
| 844 | { |
| 845 | std::string styleMessage(""); |
| 846 | data.getBorderStyles(para.m_borderStyle.m_style, para.m_borderStyle.m_type, styleMessage); |
| 847 | f << styleMessage; |
| 848 | break; |
| 849 | } |
| 850 | case 0x21: |
| 851 | para.m_borderStyle.m_width = int(data.m_value)/12700; |
| 852 | break; |
| 853 | case 0x22: |
| 854 | f << "#bordSzY=" << float(data.m_value)/12700.f << ","; |
| 855 | break; |
| 856 | |
| 857 | case 0x23: // color used to define background: col1*pat+col2*(1-pat) |
| 858 | case 0x24: |
| 859 | // color1/2 : default color1=black and color2=white |
| 860 | paraColor[data.id()-0x23]=data.getRGBColor(); |
| 861 | break; |
| 862 | case 0x25: |
| 863 | { |
| 864 | float percent=0.5; |
| 865 | if (data.m_value == 0) // checkme: no motif |
| 866 | break; |
| 867 | |
| 868 | if (data.m_value >= 3 && data.m_value <= 9) |
| 869 | percent = float(data.m_value)*0.1f; // gray motif |
| 870 | else |
| 871 | f << "backMotif=" << data.m_value << ","; |
| 872 | para.m_backgroundColor=WPSColor::barycenter(percent,paraColor[0],1.f-percent,paraColor[1]); |
| 873 | break; |
| 874 | } |
| 875 | case 0x2a: // exists with f29(1d) in style sheet, find 0|1|3 |
| 876 | f << "##f42=" << data.m_value << ","; |
| 877 | break; |
| 878 | // case 0x31(typ12) : always 1 ? |
| 879 | // case 0x33(typ12) : always 2 ? |
| 880 | case 0x32: |
| 881 | { |
| 882 | if (!data.isRead() && !data.readArrayBlock() && data.m_recursData.size() == 0) |
| 883 | { |
| 884 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph can not find tabs array\n")); |
| 885 | ok = false; |
| 886 | break; |
| 887 | } |
| 888 | size_t nChild = data.m_recursData.size(); |
| 889 | if (nChild < 1 || |
| 890 | data.m_recursData[0].isBad() || data.m_recursData[0].id() != 0x27) |
| 891 | { |
| 892 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph can not find first child\n")); |
| 893 | ok = false; |
| 894 | break; |
| 895 | } |
| 896 | if (nChild == 1) break; |
| 897 | |
| 898 | auto numTabs = int(data.m_recursData[0].m_value); |
| 899 | if (numTabs == 0 || data.m_recursData[1].isBad() || data.m_recursData[1].id() != 0x28) |
| 900 | { |
| 901 | WPS_DEBUG_MSG(("WPS8TextStyle::readParagraph can not find second child\n")); |
| 902 | ok = false; |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | WPS8Struct::FileData const &mData = data.m_recursData[1]; |
| 907 | size_t lastParsed = 0; |
| 908 | if (mData.id() == 0x28 && mData.isArray() && |
| 909 | (mData.isRead() || mData.readArrayBlock() || mData.m_recursData.size() != 0)) |
| 910 | { |
| 911 | lastParsed = 1; |
| 912 | int actTab = 0; |
| 913 | para.m_tabs.resize(size_t(numTabs)); |
| 914 | |
| 915 | for (auto const &mDataC : mData.m_recursData) |
| 916 | { |
| 917 | if (mDataC.isBad()) continue; |
| 918 | int value = mDataC.id(); |
| 919 | int wTab = value/8; |
| 920 | int what = value%8; |
| 921 | |
| 922 | // the first tab can be skipped |
| 923 | // so this may happens only one time |
| 924 | if (wTab > actTab && actTab < numTabs) |
| 925 | { |
| 926 | para.m_tabs[size_t(actTab)].m_alignment = WPSTabStop::LEFT; |
| 927 | para.m_tabs[size_t(actTab)].m_position = 0.; |
| 928 | |
| 929 | actTab++; |
| 930 | } |
| 931 | |
| 932 | if (mDataC.isNumber() && wTab==actTab && what == 0 |
| 933 | && actTab < numTabs) |
| 934 | { |
| 935 | para.m_tabs[size_t(actTab)].m_alignment = WPSTabStop::LEFT; |
| 936 | para.m_tabs[size_t(actTab)].m_position = double(mDataC.m_value)/914400; |
| 937 | |
| 938 | actTab++; |
| 939 | continue; |
| 940 | } |
| 941 | if (mDataC.isNumber() && wTab == actTab-1 && what == 1) |
| 942 | { |
| 943 | auto actVal = int(mDataC.m_value); |
| 944 | switch ((actVal & 0x3)) |
| 945 | { |
| 946 | case 0: |
| 947 | para.m_tabs[size_t(actTab-1)].m_alignment = WPSTabStop::LEFT; |
| 948 | break; |
| 949 | case 1: |
| 950 | para.m_tabs[size_t(actTab-1)].m_alignment = WPSTabStop::RIGHT; |
| 951 | break; |
| 952 | case 2: |
| 953 | para.m_tabs[size_t(actTab-1)].m_alignment = WPSTabStop::CENTER; |
| 954 | break; |
| 955 | case 3: |
| 956 | para.m_tabs[size_t(actTab-1)].m_alignment = WPSTabStop::DECIMAL; |
| 957 | break; |
| 958 | default: |
| 959 | break; |
| 960 | } |
| 961 | if (actVal&0xC) |
| 962 | f << "###tabFl" << actTab<<":low=" << (actVal&0xC) << ","; |
| 963 | actVal = (actVal>>8); |
| 964 | /* not frequent: |
| 965 | but fl1:high=db[C], fl2:high=b7[R] appear relatively often*/ |
| 966 | if (actVal) |
| 967 | { |
| 968 | f << ", fl" << actTab<<":high=" << std::hex |
| 969 | << actVal << std::dec; |
| 970 | switch (para.m_tabs[size_t(actTab-1)].m_alignment) |
| 971 | { |
| 972 | case WPSTabStop::LEFT: |
| 973 | break; |
| 974 | case WPSTabStop::RIGHT: |
| 975 | f << "[R]"; |
| 976 | break; |
| 977 | case WPSTabStop::CENTER: |
| 978 | f << "[C]"; |
| 979 | break; |
| 980 | case WPSTabStop::DECIMAL: |
| 981 | f << "[D]"; |
| 982 | break; |
| 983 | case WPSTabStop::BAR: |
| 984 | default: |
| 985 | f << "[?]"; |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | continue; |
| 990 | } |
| 991 | if (mDataC.isNumber() && wTab == actTab-1 && what == 2) |
| 992 | { |
| 993 | para.m_tabs[size_t(actTab-1)].m_leaderCharacter = uint16_t(mDataC.m_value); |
| 994 | continue; |
| 995 | } |
| 996 | f << "###tabData:fl" << actTab << "=" << mDataC << ","; |
| 997 | } |
| 998 | if (actTab != numTabs) |
| 999 | { |
| 1000 | f << "NTabs[###founds]="<<actTab << ","; |
| 1001 | para.m_tabs.resize(size_t(actTab)); |
| 1002 | } |
| 1003 | } |
| 1004 | for (size_t ch =lastParsed+1; ch < nChild; ch++) |
| 1005 | { |
| 1006 | if (data.m_recursData[ch].isBad()) continue; |
| 1007 | f << "extra[tabs]=[" << data.m_recursData[ch] << "],"; |
| 1008 | } |
| 1009 | } |
| 1010 | WPS_FALLTHROUGH; // checkme |
| 1011 | |
| 1012 | case 0x34: // interline line spacing 8*152400 -> normal, sinon *2 |
| 1013 | { |
| 1014 | float lines = float(data.m_value)/1219200.f; |
| 1015 | if (lines < 0.5f) |
| 1016 | { |
| 1017 | // find in one file some bogus line spacing between 0.2 and 0.3 |
| 1018 | f << "###lineSpacing = " << lines << ","; |
| 1019 | lines = 1; |
| 1020 | } |
| 1021 | para.setInterline(double(lines), librevenge::RVNG_PERCENT); |
| 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | default: |
| 1026 | ok = false; |
| 1027 | } |
| 1028 | |
| 1029 | if (ok) continue; |
| 1030 | f << "###" << data << ","; |
| 1031 | } |
| 1032 | |
| 1033 | if (!readOk) |
| 1034 | f << "###or [" << mainData << "]"; |
| 1035 | para.m_extra = f.str(); |
| 1036 | para.m_extra += error; |
| 1037 | if (para.m_listLevelIndex) |
| 1038 | { |
| 1039 | para.m_margins[0]+=para.m_margins[1]; |
| 1040 | para.m_listLevel.m_labelIndent = para.m_margins[0]; |
| 1041 | } |
| 1042 | id = int(m_state->m_paragraphList.size()); |
| 1043 | m_state->m_paragraphList.push_back(para); |
| 1044 | |
| 1045 | f.str(""); |
| 1046 | f << para; |
| 1047 | mess = f.str(); |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | void WPS8TextStyle::sendParagraph(int pId) |
| 1053 | { |
| 1054 | if (pId >= int(m_state->m_paragraphList.size())) |
| 1055 | { |
| 1056 | WPS_DEBUG_MSG(("WPS8TextStyle::sendParagraph: can not find paragraph id %d\n", pId)); |
| 1057 | return; |
| 1058 | } |
| 1059 | if (!m_listener) return; |
| 1060 | auto const ¶=pId < 0 ? m_state->m_defaultParagraph : m_state->m_paragraphList[size_t(pId)]; |
| 1061 | m_listener->setParagraph(para); |
| 1062 | } |
| 1063 | |
| 1064 | //////////////////////////////////////////////////////////// |
| 1065 | // StyleSheet: STSH Zone (Checkme) |
| 1066 | //////////////////////////////////////////////////////////// |
| 1067 | bool WPS8TextStyle::readSTSH(WPSEntry const &entry) |
| 1068 | { |
| 1069 | if (!entry.hasType(entry.name())) |
| 1070 | { |
| 1071 | WPS_DEBUG_MSG(("WPS8TextStyle::readSTSH: warning: STSH name=%s, type=%s\n", |
| 1072 | entry.name().c_str(), entry.type().c_str())); |
| 1073 | return false; |
| 1074 | } |
| 1075 | long page_offset = entry.begin(); |
| 1076 | long length = entry.length(); |
| 1077 | long endPage = entry.end(); |
| 1078 | |
| 1079 | if (length < 20) |
| 1080 | { |
| 1081 | WPS_DEBUG_MSG(("WPS8TextStyle::readSTSH: warning: STSH length=0x%lx\n", static_cast<unsigned long>(length))); |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | entry.setParsed(); |
| 1086 | m_input->seek(page_offset, librevenge::RVNG_SEEK_SET); |
| 1087 | |
| 1088 | libwps::DebugStream f; |
| 1089 | |
| 1090 | if (libwps::read32(m_input) != length-20) return false; |
| 1091 | int N = libwps::read32(m_input); |
| 1092 | |
| 1093 | if (N < 0) return false; |
| 1094 | f << "N=" << N; // 1 or 2 |
| 1095 | |
| 1096 | f << std::hex << ", unk1=" << libwps::read32(m_input) << std::dec; |
| 1097 | int type = libwps::read32(m_input); |
| 1098 | f << ", type=" << type; // 4 : string ? 1 : unknown |
| 1099 | f << std::hex << ", unk2=" << libwps::read32(m_input); // "HST" |
| 1100 | f << ", pos=("; |
| 1101 | |
| 1102 | long debZone = m_input->tell(); |
| 1103 | std::vector<long> ptrs; |
| 1104 | if (debZone + 4*N > endPage) return false; |
| 1105 | |
| 1106 | bool ok = true; |
| 1107 | for (int i = 0; i < N; i++) |
| 1108 | { |
| 1109 | long val = libwps::read32(m_input) + debZone; |
| 1110 | if (val >= endPage) |
| 1111 | { |
| 1112 | ok = false; |
| 1113 | break; |
| 1114 | } |
| 1115 | f << val << ","; |
| 1116 | ptrs.push_back(val); |
| 1117 | } |
| 1118 | f << ")"; |
| 1119 | |
| 1120 | ascii().addPos(page_offset); |
| 1121 | ascii().addNote(f.str().c_str()); |
| 1122 | |
| 1123 | if (!ok) return false; |
| 1124 | |
| 1125 | for (size_t i = 0; i < size_t(N); i++) |
| 1126 | { |
| 1127 | long pos = ptrs[i]; |
| 1128 | long endZPos = (i+1 == size_t(N)) ? endPage : ptrs[i+1]; |
| 1129 | length = endZPos-pos; |
| 1130 | if (length < 2) |
| 1131 | { |
| 1132 | ok = false; |
| 1133 | continue; |
| 1134 | } |
| 1135 | |
| 1136 | f.str(""); |
| 1137 | f << std::dec << "STSH("; |
| 1138 | if (type == 4) f << i; |
| 1139 | else |
| 1140 | { |
| 1141 | if (i%2) f << "P" << i/2; |
| 1142 | else f << "C" << i/2; |
| 1143 | } |
| 1144 | f << "):"; |
| 1145 | |
| 1146 | m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 1147 | auto size = int(libwps::readU16(m_input)); |
| 1148 | bool correct = true; |
| 1149 | if (2*size + 2 + type != length) correct = false; |
| 1150 | else |
| 1151 | { |
| 1152 | switch (type) |
| 1153 | { |
| 1154 | case 4: |
| 1155 | { |
| 1156 | librevenge::RVNGString str; |
| 1157 | if (size) WPS8Text::readString(m_input, 2*size, str); |
| 1158 | f << "'" << str.cstr() << "'"; |
| 1159 | m_input->seek(pos+2+2*size, librevenge::RVNG_SEEK_SET); |
| 1160 | f << ", unkn=" << libwps::read32(m_input); |
| 1161 | break; |
| 1162 | } |
| 1163 | case 0: |
| 1164 | { |
| 1165 | auto dataSz = int(libwps::readU16(m_input)); |
| 1166 | if (dataSz+2 != 2*size) |
| 1167 | { |
| 1168 | correct = false; |
| 1169 | break; |
| 1170 | } |
| 1171 | |
| 1172 | int id; |
| 1173 | std::string mess; |
| 1174 | if (i%2 == 0) |
| 1175 | { |
| 1176 | readFont(pos+2*size, id, mess); |
| 1177 | f << "Font" << id << "=" << mess; |
| 1178 | } |
| 1179 | else |
| 1180 | { |
| 1181 | readParagraph(pos+2*size, id, mess); |
| 1182 | f << "Paragraph" << id << "=" << mess; |
| 1183 | } |
| 1184 | break; |
| 1185 | } |
| 1186 | default: |
| 1187 | correct = false; |
| 1188 | } |
| 1189 | } |
| 1190 | if (!correct) |
| 1191 | { |
| 1192 | f << "###"; |
| 1193 | ok = false; |
| 1194 | } |
| 1195 | |
| 1196 | ascii().addPos(pos); |
| 1197 | ascii().addNote(f.str().c_str()); |
| 1198 | |
| 1199 | } |
| 1200 | |
| 1201 | return ok; |
| 1202 | } |
| 1203 | |
| 1204 | //////////////////////////////////////////////////////////// |
| 1205 | // code to find the fdpc and fdpp entries; normal then by hand |
| 1206 | //////////////////////////////////////////////////////////// |
| 1207 | bool WPS8TextStyle::findFDPStructures(int which, std::vector<WPSEntry> &zones) |
| 1208 | { |
| 1209 | zones.resize(0); |
| 1210 | |
| 1211 | char const *indexName = which ? "BTEC" : "BTEP"; |
| 1212 | char const *sIndexName = which ? "FDPC" : "FDPP"; |
| 1213 | |
| 1214 | auto pos = m_mainParser.getNameEntryMap().lower_bound(indexName); |
| 1215 | |
| 1216 | std::vector<WPSEntry const *> listIndexed; |
| 1217 | while (pos != m_mainParser.getNameEntryMap().end()) |
| 1218 | { |
| 1219 | WPSEntry const &entry = pos++->second; |
| 1220 | if (!entry.hasName(indexName)) break; |
| 1221 | if (!entry.hasType("PLC ")) continue; |
| 1222 | listIndexed.push_back(&entry); |
| 1223 | } |
| 1224 | |
| 1225 | size_t nFind = listIndexed.size(); |
| 1226 | if (nFind==0) return false; |
| 1227 | |
| 1228 | // can nFind be > 1 ? |
| 1229 | for (size_t i = 0; i+1 < nFind; i++) |
| 1230 | { |
| 1231 | bool ok = true; |
| 1232 | for (size_t j = 0; j+i+1 < nFind; j++) |
| 1233 | { |
| 1234 | if (listIndexed[j]->id() <= listIndexed[j+1]->id()) |
| 1235 | continue; |
| 1236 | WPSEntry const *tmp = listIndexed[j]; |
| 1237 | listIndexed[j] = listIndexed[j+1]; |
| 1238 | listIndexed[j+1] = tmp; |
| 1239 | ok = false; |
| 1240 | } |
| 1241 | if (ok) break; |
| 1242 | } |
| 1243 | |
| 1244 | for (size_t i = 0; i+1 < nFind; i++) |
| 1245 | if (listIndexed[i]->id() == listIndexed[i+1]->id()) return false; |
| 1246 | |
| 1247 | // create a map offset -> entry |
| 1248 | std::map<long, WPSEntry const *> offsetMap; |
| 1249 | std::map<long, WPSEntry const *>::iterator offsIt; |
| 1250 | pos = m_mainParser.getNameEntryMap().lower_bound(sIndexName); |
| 1251 | while (pos != m_mainParser.getNameEntryMap().end()) |
| 1252 | { |
| 1253 | WPSEntry const &entry = pos++->second; |
| 1254 | if (!entry.hasName(sIndexName)) break; |
| 1255 | offsetMap.insert(std::map<long, WPSEntry const *>::value_type |
| 1256 | (entry.begin(), &entry)); |
| 1257 | } |
| 1258 | |
| 1259 | for (size_t i = 0; i < nFind; i++) |
| 1260 | { |
| 1261 | WPSEntry const &entry = *(listIndexed[i]); |
| 1262 | std::vector<long> textPtrs; |
| 1263 | std::vector<long> listValues; |
| 1264 | |
| 1265 | if (!m_mainParser.readPLC(entry, textPtrs, listValues)) return false; |
| 1266 | |
| 1267 | if (textPtrs.size() != listValues.size()+1) return false; |
| 1268 | |
| 1269 | for (auto position : listValues) |
| 1270 | { |
| 1271 | if (position <= 0) return false; |
| 1272 | |
| 1273 | offsIt = offsetMap.find(position); |
| 1274 | if (offsIt == offsetMap.end() || !offsIt->second->hasName(sIndexName)) |
| 1275 | return false; |
| 1276 | |
| 1277 | zones.push_back(*offsIt->second); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | return true; |
| 1282 | } |
| 1283 | |
| 1284 | bool WPS8TextStyle::findFDPStructuresByHand(int which, std::vector<WPSEntry> &zones) |
| 1285 | { |
| 1286 | char const *indexName = which ? "FDPC" : "FDPP"; |
| 1287 | WPS_DEBUG_MSG(("WPS8TextStyle::findFDPStructuresByHand: error: need to create %s list by hand \n", indexName)); |
| 1288 | zones.resize(0); |
| 1289 | |
| 1290 | auto pos = m_mainParser.getNameEntryMap().lower_bound(indexName); |
| 1291 | while (pos != m_mainParser.getNameEntryMap().end()) |
| 1292 | { |
| 1293 | WPSEntry const &entry = pos++->second; |
| 1294 | if (!entry.hasName(indexName)) break; |
| 1295 | if (!entry.hasType(indexName)) continue; |
| 1296 | |
| 1297 | zones.push_back(entry); |
| 1298 | } |
| 1299 | return zones.size() != 0; |
| 1300 | } |
| 1301 | |
| 1302 | //////////////////////////////////////////////////////////// |
| 1303 | // FontData functions |
| 1304 | //////////////////////////////////////////////////////////// |
| 1305 | std::ostream &operator<<(std::ostream &o, WPS8TextStyle::FontData const &fData) |
| 1306 | { |
| 1307 | switch (fData.m_type) |
| 1308 | { |
| 1309 | case WPS8TextStyle::FontData::T_None: |
| 1310 | break; |
| 1311 | case WPS8TextStyle::FontData::T_Object: |
| 1312 | o << "object,"; |
| 1313 | break; |
| 1314 | case WPS8TextStyle::FontData::T_Footnote: |
| 1315 | o << "footnote,"; |
| 1316 | break; |
| 1317 | case WPS8TextStyle::FontData::T_Endnote: |
| 1318 | o << "endnote,"; |
| 1319 | break; |
| 1320 | case WPS8TextStyle::FontData::T_Field: |
| 1321 | o << "field,"; |
| 1322 | break; |
| 1323 | case WPS8TextStyle::FontData::T_Comment: // or maybe link |
| 1324 | o << "comment,"; |
| 1325 | break; |
| 1326 | default: |
| 1327 | o << "#type=" << fData.m_type << ","; |
| 1328 | break; |
| 1329 | } |
| 1330 | switch (fData.m_fieldType) |
| 1331 | { |
| 1332 | case WPS8TextStyle::FontData::F_None: |
| 1333 | break; |
| 1334 | case WPS8TextStyle::FontData::F_PageNumber: |
| 1335 | o << "pNumber,"; |
| 1336 | break; |
| 1337 | case WPS8TextStyle::FontData::F_Date: |
| 1338 | o << "date,"; |
| 1339 | break; |
| 1340 | case WPS8TextStyle::FontData::F_Time: |
| 1341 | o << "time,"; |
| 1342 | break; |
| 1343 | default: |
| 1344 | o << "#fType=" << fData.m_fieldType << ","; |
| 1345 | break; |
| 1346 | } |
| 1347 | if (fData.m_fieldFormat) o << "fFormat=" << std::hex << fData.m_fieldFormat << std::dec << ","; |
| 1348 | return o; |
| 1349 | } |
| 1350 | |
| 1351 | std::string WPS8TextStyle::FontData::format() const |
| 1352 | { |
| 1353 | switch (m_fieldFormat) |
| 1354 | { |
| 1355 | case 0x75: // 13/8/12 |
| 1356 | return "%m/%d/%y"; |
| 1357 | case 0x77: // 12 aout 12 |
| 1358 | return "%d %B %y"; |
| 1359 | case 0x78: // 2 aout 2012 |
| 1360 | case 0x79: // 02 aout 2012 |
| 1361 | return "%d %B %Y"; |
| 1362 | case 0x7b: // aout 12 |
| 1363 | return "%B %y"; |
| 1364 | case 0x7c: // aout 2012, |
| 1365 | return "%B %Y"; |
| 1366 | case 0x84: // lundi 13 aout 2012) |
| 1367 | return "%A %d %B %Y"; |
| 1368 | case 0x87: // 09:06 p.m |
| 1369 | return "%I:%M %p"; |
| 1370 | case 0x89: // 21:06 |
| 1371 | return "%H:%M"; |
| 1372 | case 0x8e: // 13/08/2012 |
| 1373 | case 0x8f: // 13/8/2012 |
| 1374 | return "%m/%d/%Y"; |
| 1375 | case 0x90: // 13/8 |
| 1376 | return "%d/%m"; |
| 1377 | case 0x91: // 8/12 |
| 1378 | return "%m/%y"; |
| 1379 | case 0x102: // aout 2012 |
| 1380 | return "%B %Y"; |
| 1381 | case 0x125: //2012/8/13 |
| 1382 | return "%Y/%m/%d"; |
| 1383 | default: |
| 1384 | break; |
| 1385 | } |
| 1386 | if (m_fieldType == F_Date) return "%m/%d/%y"; |
| 1387 | if (m_fieldType == F_Time) return "%I:%M:%S %p"; |
| 1388 | WPS_DEBUG_MSG(("WPS8TextStyle::FontData::format: called with no date/time field\n")); |
| 1389 | return ""; |
| 1390 | } |
| 1391 | |
| 1392 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |
| 1393 |