| 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) 2002, 2005 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2002, 2004 Marc Maurer ([email protected]) |
| 12 | * |
| 13 | * For minor contributions see the git repository. |
| 14 | * |
| 15 | * Alternatively, the contents of this file may be used under the terms |
| 16 | * of the GNU Lesser General Public License Version 2.1 or later |
| 17 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 18 | * applicable instead of those above. |
| 19 | * |
| 20 | * For further information visit http://libwps.sourceforge.net |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <time.h> |
| 25 | |
| 26 | #include <cmath> |
| 27 | #include <cstdarg> |
| 28 | #include <cstdio> |
| 29 | #include <iomanip> |
| 30 | #include <limits> |
| 31 | #include <sstream> |
| 32 | #include <string> |
| 33 | |
| 34 | #include <librevenge/librevenge.h> |
| 35 | |
| 36 | #include "libwps_internal.h" |
| 37 | |
| 38 | namespace libwps |
| 39 | { |
| 40 | uint8_t readU8(librevenge::RVNGInputStream *input) |
| 41 | { |
| 42 | unsigned long numBytesRead; |
| 43 | unsigned char const *p = input->read(sizeof(uint8_t), numBytesRead); |
| 44 | |
| 45 | if (!p || numBytesRead != sizeof(uint8_t)) |
| 46 | { |
| 47 | static bool first = true; |
| 48 | if (first) |
| 49 | { |
| 50 | first = false; |
| 51 | WPS_DEBUG_MSG(("libwps::readU8: can not read data\n")); |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | return *reinterpret_cast<uint8_t const *>(p); |
| 57 | } |
| 58 | |
| 59 | int8_t read8(librevenge::RVNGInputStream *input) |
| 60 | { |
| 61 | return int8_t(readU8(input)); |
| 62 | } |
| 63 | |
| 64 | uint16_t readU16(librevenge::RVNGInputStream *input) |
| 65 | { |
| 66 | uint8_t p0 = readU8(input); |
| 67 | uint8_t p1 = readU8(input); |
| 68 | return uint16_t(p0|(p1<<8)); |
| 69 | } |
| 70 | |
| 71 | int16_t read16(librevenge::RVNGInputStream *input) |
| 72 | { |
| 73 | return int16_t(readU16(input)); |
| 74 | } |
| 75 | |
| 76 | uint32_t readU32(librevenge::RVNGInputStream *input) |
| 77 | { |
| 78 | uint8_t p0 = readU8(input); |
| 79 | uint8_t p1 = readU8(input); |
| 80 | uint8_t p2 = readU8(input); |
| 81 | uint8_t p3 = readU8(input); |
| 82 | return uint32_t((p0<<0)|(p1<<8)|(p2<<16)|(p3<<24)); |
| 83 | } |
| 84 | |
| 85 | int32_t read32(librevenge::RVNGInputStream *input) |
| 86 | { |
| 87 | return int32_t(readU32(input)); |
| 88 | } |
| 89 | |
| 90 | bool readDouble4(RVNGInputStreamPtr &input, double &res, bool &isNaN) |
| 91 | { |
| 92 | isNaN=false; |
| 93 | res = 0; |
| 94 | long pos = input->tell(); |
| 95 | if (input->seek(4, librevenge::RVNG_SEEK_CUR) || input->tell()!=pos+4) |
| 96 | { |
| 97 | WPS_DEBUG_MSG(("libwps::readDouble4: the zone seems too short\n")); |
| 98 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 99 | return false; |
| 100 | } |
| 101 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 102 | |
| 103 | double mantisse = 0; |
| 104 | /** (first&3)==1: is used to decide if we store 100*N or N., |
| 105 | (first&3)==2: indicates a basic int number (appears mainly when converting a dos file in a windows file) |
| 106 | (first&3)==3: Can this exist ? What does this mean: 100*a basic int ? |
| 107 | The other bytes seem to have classic meaning... |
| 108 | */ |
| 109 | auto first = int(readU8(input)); |
| 110 | if ((first&3)==2) |
| 111 | { |
| 112 | // so read it as a normal number |
| 113 | input->seek(-1, librevenge::RVNG_SEEK_CUR); |
| 114 | auto val=long(readU16(input)>>2); |
| 115 | val+=long(readU16(input))<<14; |
| 116 | if (val&0x20000000) |
| 117 | res = double(val-0x40000000); |
| 118 | else |
| 119 | res = double(val); |
| 120 | return true; |
| 121 | } |
| 122 | mantisse = double(first & 0xFC)/256 + double(readU8(input)); |
| 123 | auto mantExp = int(readU8(input)); |
| 124 | mantisse = (mantisse/256 + double(0x10+(mantExp & 0x0F)))/16; |
| 125 | int exp = ((mantExp&0xF0)>>4)+int(readU8(input)<<4); |
| 126 | int sign = 1; |
| 127 | if (exp & 0x800) |
| 128 | { |
| 129 | exp &= 0x7ff; |
| 130 | sign = -1; |
| 131 | } |
| 132 | |
| 133 | if (exp == 0) |
| 134 | { |
| 135 | if (double(mantisse) > 1.-1e-4) return true; // ok zero |
| 136 | // fixme find Nan representation |
| 137 | return false; |
| 138 | } |
| 139 | if (exp == 0x7FF) |
| 140 | { |
| 141 | if (double(mantisse) > 1.-1e-4) |
| 142 | { |
| 143 | res=std::numeric_limits<double>::quiet_NaN(); |
| 144 | isNaN=true; |
| 145 | /* 0x7FFFF.. are nan(infinite, ...):ok |
| 146 | |
| 147 | 0xFFFFF.. are nan(in the sense, not a number but |
| 148 | text...). In this case wps2csv and wps2text will |
| 149 | display a nan. Not good, but difficult to retrieve the |
| 150 | cell's content without excuting the formula associated |
| 151 | to this cell :-~ |
| 152 | */ |
| 153 | return true; |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | exp -= 0x3ff; |
| 159 | res = std::ldexp(mantisse, exp); |
| 160 | if (sign == -1) |
| 161 | { |
| 162 | res *= -1.; |
| 163 | } |
| 164 | if (first & 1) res/=100; |
| 165 | if (first & 2) |
| 166 | { |
| 167 | // CHECKME... |
| 168 | WPS_DEBUG_MSG(("libwps::readDouble4: ARRGGGGGGGGGG find a float with first & 3 ARRGGGGGGGGGG in pos%lx,\n some float can be broken\n", static_cast<unsigned long>(pos))); |
| 169 | } |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | bool readDouble8(RVNGInputStreamPtr &input, double &res, bool &isNaN) |
| 174 | { |
| 175 | isNaN=false; |
| 176 | res = 0; |
| 177 | long pos = input->tell(); |
| 178 | if (input->seek(8, librevenge::RVNG_SEEK_CUR) || input->tell()!=pos+8) |
| 179 | { |
| 180 | WPS_DEBUG_MSG(("libwps::readDouble8: the zone seems too short\n")); |
| 181 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 182 | return false; |
| 183 | } |
| 184 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 185 | double mantisse = 0; |
| 186 | for (int i = 0; i < 6; i++) |
| 187 | mantisse = mantisse/256 + double(readU8(input)); |
| 188 | auto mantExp = int(readU8(input)); |
| 189 | mantisse = (mantisse/256 + double(0x10+(mantExp & 0x0F)))/16; |
| 190 | int exp = ((mantExp&0xF0)>>4)+int(readU8(input)<<4); |
| 191 | int sign = 1; |
| 192 | if (exp & 0x800) |
| 193 | { |
| 194 | exp &= 0x7ff; |
| 195 | sign = -1; |
| 196 | } |
| 197 | |
| 198 | double const epsilon=1.e-5; |
| 199 | if (exp == 0) |
| 200 | { |
| 201 | if (mantisse > 1-epsilon && mantisse < 1+epsilon) return true; // ok zero |
| 202 | // fixme find Nan representation |
| 203 | return false; |
| 204 | } |
| 205 | if (exp == 0x7FF) |
| 206 | { |
| 207 | if (mantisse >= 1-epsilon) |
| 208 | { |
| 209 | res=std::numeric_limits<double>::quiet_NaN(); |
| 210 | return true; // ok 0x7FF and 0xFFF are nan |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | exp -= 0x3ff; |
| 216 | res = std::ldexp(mantisse, exp); |
| 217 | if (sign == -1) |
| 218 | { |
| 219 | res *= -1.; |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool readDouble10(RVNGInputStreamPtr &input, double &res, bool &isNaN) |
| 225 | { |
| 226 | isNaN=false; |
| 227 | res = 0; |
| 228 | long pos = input->tell(); |
| 229 | if (input->seek(10, librevenge::RVNG_SEEK_CUR) || input->tell()!=pos+10) |
| 230 | { |
| 231 | WPS_DEBUG_MSG(("libwps::readDouble8: the zone seems too short\n")); |
| 232 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 233 | return false; |
| 234 | } |
| 235 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 236 | double mantisse = 0; |
| 237 | for (int i = 0; i < 8; i++) |
| 238 | mantisse = mantisse/256 + double(readU8(input))/128; |
| 239 | auto exp = int(readU16(input)); |
| 240 | int sign = 1; |
| 241 | if (exp & 0x8000) |
| 242 | { |
| 243 | exp &= 0x7fff; |
| 244 | sign = -1; |
| 245 | } |
| 246 | |
| 247 | double const epsilon=1.e-5; |
| 248 | if (exp == 0) |
| 249 | { |
| 250 | if (mantisse < epsilon) return true; // checkme is this zero or a nan |
| 251 | // fixme find Nan representation |
| 252 | return false; |
| 253 | } |
| 254 | if (exp == 0x7FFf) |
| 255 | { |
| 256 | if (mantisse >= 1-epsilon) |
| 257 | { |
| 258 | res=std::numeric_limits<double>::quiet_NaN(); |
| 259 | return true; // ok 0x7FF and 0xFFF are nan |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | exp -= 0x3fff; |
| 265 | res = std::ldexp(mantisse, exp); |
| 266 | if (sign == -1) |
| 267 | { |
| 268 | res *= -1.; |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool readDouble2Inv(RVNGInputStreamPtr &input, double &res, bool &isNaN) |
| 274 | { |
| 275 | isNaN=false; |
| 276 | res = 0; |
| 277 | long pos = input->tell(); |
| 278 | if (input->seek(2, librevenge::RVNG_SEEK_CUR) || input->tell()!=pos+2) |
| 279 | { |
| 280 | WPS_DEBUG_MSG(("libwps::readDouble2Inv: the zone seems too short\n")); |
| 281 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 282 | return false; |
| 283 | } |
| 284 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 285 | auto val=int(libwps::readU16(input)); |
| 286 | int exp=val&0xf; |
| 287 | if ((exp&1)==1) |
| 288 | { |
| 289 | int mantisse=(val>>4); |
| 290 | if ((mantisse&0x800)) |
| 291 | mantisse -= 0x1000; |
| 292 | exp/=2; |
| 293 | const double factors[8]= { 5000, 500, 0.05, 0.005, 0.0005, 0.00005, 1/16., 1/64. }; |
| 294 | res=double(mantisse)*factors[exp]; |
| 295 | return true; |
| 296 | } |
| 297 | if ((val&0x8000)) |
| 298 | val-=0x10000; |
| 299 | res=double(val>>1); |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | bool readDouble4Inv(RVNGInputStreamPtr &input, double &res, bool &isNaN) |
| 304 | { |
| 305 | isNaN=false; |
| 306 | res = 0; |
| 307 | long pos = input->tell(); |
| 308 | if (input->seek(4, librevenge::RVNG_SEEK_CUR) || input->tell()!=pos+4) |
| 309 | { |
| 310 | WPS_DEBUG_MSG(("libwps::readDouble4Inv: the zone seems too short\n")); |
| 311 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 312 | return false; |
| 313 | } |
| 314 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 315 | auto val=long(libwps::readU32(input)); |
| 316 | auto exp=int(val&0xf); |
| 317 | auto mantisse=int(val>>6); |
| 318 | if (val&0x20) |
| 319 | mantisse *= -1; |
| 320 | if (exp) |
| 321 | { |
| 322 | if (val&0x10) |
| 323 | res=mantisse/std::pow(10., exp); |
| 324 | else |
| 325 | res=mantisse*std::pow(10., exp); |
| 326 | return true; |
| 327 | } |
| 328 | res=double(mantisse); |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | bool readData(RVNGInputStreamPtr &input, unsigned long size, librevenge::RVNGBinaryData &data) |
| 333 | { |
| 334 | data.clear(); |
| 335 | if (size == 0) return true; |
| 336 | |
| 337 | const unsigned char *readData; |
| 338 | unsigned long sizeRead; |
| 339 | if ((readData=input->read(size, sizeRead)) == nullptr || sizeRead!=size) |
| 340 | return false; |
| 341 | data.append(readData, sizeRead); |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | bool readDataToEnd(RVNGInputStreamPtr &input, librevenge::RVNGBinaryData &data) |
| 347 | { |
| 348 | data.clear(); |
| 349 | long pos=input->tell(); |
| 350 | input->seek(0,librevenge::RVNG_SEEK_END); |
| 351 | long sz=input->tell()-pos; |
| 352 | if (sz < 0) return false; |
| 353 | input->seek(pos,librevenge::RVNG_SEEK_SET); |
| 354 | return readData(input, static_cast<unsigned long>(sz), data) && input->isEnd(); |
| 355 | } |
| 356 | |
| 357 | std::string numberingTypeToString(NumberingType type) |
| 358 | { |
| 359 | switch (type) |
| 360 | { |
| 361 | case ARABIC: |
| 362 | return "1"; |
| 363 | case LOWERCASE: |
| 364 | return "a"; |
| 365 | case UPPERCASE: |
| 366 | return "A"; |
| 367 | case LOWERCASE_ROMAN: |
| 368 | return "i"; |
| 369 | case UPPERCASE_ROMAN: |
| 370 | return "I"; |
| 371 | case NONE: |
| 372 | case BULLET: |
| 373 | default: |
| 374 | break; |
| 375 | } |
| 376 | WPS_DEBUG_MSG(("libwps::numberingTypeToString: must not be called with type %d\n", int(type))); |
| 377 | return "1"; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | //////////////////////////////////////////////////////////// |
| 382 | // color |
| 383 | //////////////////////////////////////////////////////////// |
| 384 | |
| 385 | // color function |
| 386 | WPSColor WPSColor::barycenter(float alpha, WPSColor const &colA, |
| 387 | float beta, WPSColor const &colB) |
| 388 | { |
| 389 | uint32_t res = 0; |
| 390 | for (int i=0, depl=0; i<4; i++, depl+=8) |
| 391 | { |
| 392 | float val=alpha*float((colA.m_value>>depl)&0xFF)+beta*float((colB.m_value>>depl)&0xFF); |
| 393 | if (val < 0) val=0; |
| 394 | if (val > 256) val=256; |
| 395 | auto comp= static_cast<unsigned char>(val); |
| 396 | res+=uint32_t(comp<<depl); |
| 397 | } |
| 398 | return WPSColor(res); |
| 399 | } |
| 400 | |
| 401 | std::ostream &operator<< (std::ostream &o, WPSColor const &c) |
| 402 | { |
| 403 | auto const width = o.width(); |
| 404 | auto const fill = o.fill(); |
| 405 | o << "#" << std::hex << std::setfill('0') << std::setw(6) |
| 406 | << (c.m_value&0xFFFFFF) |
| 407 | // std::ios::width() takes/returns std::streamsize (long), but |
| 408 | // std::setw() takes int. Go figure... |
| 409 | << std::dec << std::setfill(fill) << std::setw(static_cast<int>(width)); |
| 410 | return o; |
| 411 | } |
| 412 | |
| 413 | std::string WPSColor::str() const |
| 414 | { |
| 415 | std::stringstream stream; |
| 416 | stream << *this; |
| 417 | return stream.str(); |
| 418 | } |
| 419 | |
| 420 | //////////////////////////////////////////////////////////// |
| 421 | // field |
| 422 | //////////////////////////////////////////////////////////// |
| 423 | // format function |
| 424 | namespace libwps |
| 425 | { |
| 426 | static bool convertDTFormat(std::string const &dtFormat, librevenge::RVNGPropertyListVector &propVect) |
| 427 | { |
| 428 | propVect.clear(); |
| 429 | std::string text(""); |
| 430 | librevenge::RVNGPropertyList list; |
| 431 | size_t len=dtFormat.size(); |
| 432 | for (size_t c=0; c < len; ++c) |
| 433 | { |
| 434 | if (dtFormat[c]!='%' || c+1==len) |
| 435 | { |
| 436 | text+=dtFormat[c]; |
| 437 | continue; |
| 438 | } |
| 439 | char ch=dtFormat[++c]; |
| 440 | if (ch=='%') |
| 441 | { |
| 442 | text += '%'; |
| 443 | continue; |
| 444 | } |
| 445 | if (!text.empty()) |
| 446 | { |
| 447 | list.clear(); |
| 448 | list.insert("librevenge:value-type", "text"); |
| 449 | list.insert("librevenge:text", text.c_str()); |
| 450 | propVect.append(list); |
| 451 | text.clear(); |
| 452 | } |
| 453 | list.clear(); |
| 454 | switch (ch) |
| 455 | { |
| 456 | case 'Y': |
| 457 | list.insert("number:style", "long"); |
| 458 | WPS_FALLTHROUGH; |
| 459 | case 'y': |
| 460 | list.insert("librevenge:value-type", "year"); |
| 461 | propVect.append(list); |
| 462 | break; |
| 463 | case 'B': |
| 464 | list.insert("number:style", "long"); |
| 465 | WPS_FALLTHROUGH; |
| 466 | case 'b': |
| 467 | case 'h': |
| 468 | list.insert("librevenge:value-type", "month"); |
| 469 | list.insert("number:textual", true); |
| 470 | propVect.append(list); |
| 471 | break; |
| 472 | case 'm': |
| 473 | list.insert("librevenge:value-type", "month"); |
| 474 | propVect.append(list); |
| 475 | break; |
| 476 | case 'e': |
| 477 | list.insert("number:style", "long"); |
| 478 | WPS_FALLTHROUGH; |
| 479 | case 'd': |
| 480 | list.insert("librevenge:value-type", "day"); |
| 481 | propVect.append(list); |
| 482 | break; |
| 483 | case 'A': |
| 484 | list.insert("number:style", "long"); |
| 485 | WPS_FALLTHROUGH; |
| 486 | case 'a': |
| 487 | list.insert("librevenge:value-type", "day-of-week"); |
| 488 | propVect.append(list); |
| 489 | break; |
| 490 | |
| 491 | case 'H': |
| 492 | list.insert("number:style", "long"); |
| 493 | WPS_FALLTHROUGH; |
| 494 | case 'I': |
| 495 | list.insert("librevenge:value-type", "hours"); |
| 496 | propVect.append(list); |
| 497 | break; |
| 498 | case 'M': |
| 499 | list.insert("librevenge:value-type", "minutes"); |
| 500 | list.insert("number:style", "long"); |
| 501 | propVect.append(list); |
| 502 | break; |
| 503 | case 'S': |
| 504 | list.insert("librevenge:value-type", "seconds"); |
| 505 | list.insert("number:style", "long"); |
| 506 | propVect.append(list); |
| 507 | break; |
| 508 | case 'p': |
| 509 | list.clear(); |
| 510 | list.insert("librevenge:value-type", "am-pm"); |
| 511 | propVect.append(list); |
| 512 | break; |
| 513 | #if !defined(__clang__) |
| 514 | default: |
| 515 | WPS_DEBUG_MSG(("convertDTFormat: find unimplement command %c(ignored)\n", ch)); |
| 516 | #endif |
| 517 | } |
| 518 | } |
| 519 | if (!text.empty()) |
| 520 | { |
| 521 | list.clear(); |
| 522 | list.insert("librevenge:value-type", "text"); |
| 523 | list.insert("librevenge:text", text.c_str()); |
| 524 | propVect.append(list); |
| 525 | } |
| 526 | return propVect.count()!=0; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | bool WPSField::addTo(librevenge::RVNGPropertyList &propList) const |
| 531 | { |
| 532 | switch (m_type) |
| 533 | { |
| 534 | case Date: |
| 535 | { |
| 536 | propList.insert("librevenge:field-type", "text:date"); |
| 537 | |
| 538 | librevenge::RVNGPropertyListVector pVect; |
| 539 | if (!libwps::convertDTFormat(m_DTFormat.empty() ? std::string("%m/%d/%y") : m_DTFormat, pVect)) |
| 540 | break; |
| 541 | |
| 542 | propList.insert("librevenge:value-type", "date"); |
| 543 | propList.insert("number:automatic-order", "true"); |
| 544 | propList.insert("librevenge:format", pVect); |
| 545 | break; |
| 546 | } |
| 547 | case PageCount: |
| 548 | propList.insert("librevenge:field-type", "text:page-count"); |
| 549 | propList.insert("style:num-format", numberingTypeToString(m_numberingType).c_str()); |
| 550 | break; |
| 551 | case PageNumber: |
| 552 | case PageNumberNext: |
| 553 | propList.insert("librevenge:field-type", "text:page-number"); |
| 554 | propList.insert("style:num-format", numberingTypeToString(m_numberingType).c_str()); |
| 555 | if (m_type==PageNumberNext) |
| 556 | propList.insert("text:select-page", "next"); |
| 557 | break; |
| 558 | case Title: |
| 559 | propList.insert("librevenge:field-type", "text:title"); |
| 560 | break; |
| 561 | case Time: |
| 562 | { |
| 563 | propList.insert("librevenge:field-type", "text:time"); |
| 564 | |
| 565 | librevenge::RVNGPropertyListVector pVect; |
| 566 | if (!libwps::convertDTFormat(m_DTFormat.empty() ? std::string("%I:%M:%S %p") : m_DTFormat, pVect)) |
| 567 | break; |
| 568 | |
| 569 | propList.insert("librevenge:value-type", "time"); |
| 570 | propList.insert("number:automatic-order", "true"); |
| 571 | propList.insert("librevenge:format", pVect); |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | case Database: |
| 576 | case Link: |
| 577 | case None: |
| 578 | default: |
| 579 | return false; |
| 580 | } |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | librevenge::RVNGString WPSField::getString() const |
| 585 | { |
| 586 | librevenge::RVNGString res; |
| 587 | switch (m_type) |
| 588 | { |
| 589 | case Database: |
| 590 | if (m_data.length()) |
| 591 | res=librevenge::RVNGString(m_data.c_str()); |
| 592 | else |
| 593 | res=librevenge::RVNGString("#DATAFIELD#"); |
| 594 | break; |
| 595 | case Link: |
| 596 | if (m_data.length()) |
| 597 | res=librevenge::RVNGString(m_data.c_str()); |
| 598 | else |
| 599 | res=librevenge::RVNGString("#LINK#"); |
| 600 | break; |
| 601 | case Title: |
| 602 | if (m_data.length()) |
| 603 | res=librevenge::RVNGString(m_data.c_str()); |
| 604 | else |
| 605 | res=librevenge::RVNGString("#TITLE#"); |
| 606 | break; |
| 607 | case Date: |
| 608 | case PageCount: |
| 609 | case PageNumber: |
| 610 | case PageNumberNext: |
| 611 | case Time: |
| 612 | case None: |
| 613 | default: |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | return res; |
| 618 | } |
| 619 | //////////////////////////////////////////////////////////// |
| 620 | // border |
| 621 | //////////////////////////////////////////////////////////// |
| 622 | |
| 623 | int WPSBorder::compare(WPSBorder const &orig) const |
| 624 | { |
| 625 | int diff = int(m_style)-int(orig.m_style); |
| 626 | if (diff) return diff; |
| 627 | diff = m_width-orig.m_width; |
| 628 | if (diff) return diff; |
| 629 | if (m_color < orig.m_color) return -1; |
| 630 | if (m_color > orig.m_color) return 1; |
| 631 | if (m_widthsList.size() != orig.m_widthsList.size()) |
| 632 | return m_widthsList.size() < orig.m_widthsList.size() ? -1 : 1; |
| 633 | for (size_t i=0; i<m_widthsList.size(); ++i) |
| 634 | { |
| 635 | if (m_widthsList[i]<orig.m_widthsList[i]) return -1; |
| 636 | if (m_widthsList[i]>orig.m_widthsList[i]) return 1; |
| 637 | } |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | bool WPSBorder::addTo(librevenge::RVNGPropertyList &propList, std::string const which) const |
| 642 | { |
| 643 | std::stringstream stream, field; |
| 644 | stream << m_width << "pt "; |
| 645 | if (m_type==WPSBorder::Double || m_type==WPSBorder::Triple) |
| 646 | { |
| 647 | static bool first = true; |
| 648 | if (first && m_style!=Simple) |
| 649 | { |
| 650 | WPS_DEBUG_MSG(("WPSBorder::addTo: find double or tripe border with complex style\n")); |
| 651 | first = false; |
| 652 | } |
| 653 | stream << "double"; |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | switch (m_style) |
| 658 | { |
| 659 | case Dot: |
| 660 | case LargeDot: |
| 661 | stream << "dotted"; |
| 662 | break; |
| 663 | case Dash: |
| 664 | stream << "dashed"; |
| 665 | break; |
| 666 | case Simple: |
| 667 | stream << "solid"; |
| 668 | break; |
| 669 | case None: |
| 670 | default: |
| 671 | stream << "none"; |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | stream << " " << m_color.str(); |
| 676 | field << "fo:border"; |
| 677 | if (which.length()) |
| 678 | field << "-" << which; |
| 679 | propList.insert(field.str().c_str(), stream.str().c_str()); |
| 680 | size_t numRelWidth=m_widthsList.size(); |
| 681 | if (!numRelWidth) |
| 682 | return true; |
| 683 | if (m_type!=WPSBorder::Double || numRelWidth!=3) |
| 684 | { |
| 685 | static bool first = true; |
| 686 | if (first) |
| 687 | { |
| 688 | WPS_DEBUG_MSG(("WPSBorder::addTo: relative width is only implemented with double style\n")); |
| 689 | first = false; |
| 690 | } |
| 691 | return true; |
| 692 | } |
| 693 | double totalWidth=0; |
| 694 | for (auto const &w : m_widthsList) |
| 695 | totalWidth+=w; |
| 696 | if (totalWidth <= 0) |
| 697 | { |
| 698 | WPS_DEBUG_MSG(("WPSBorder::addTo: can not compute total width\n")); |
| 699 | return true; |
| 700 | } |
| 701 | double factor=m_width/totalWidth; |
| 702 | stream.str(""); |
| 703 | for (size_t w=0; w < numRelWidth; w++) |
| 704 | { |
| 705 | stream << factor *m_widthsList[w]<< "pt"; |
| 706 | if (w+1!=numRelWidth) |
| 707 | stream << " "; |
| 708 | } |
| 709 | field.str(""); |
| 710 | field << "style:border-line-width"; |
| 711 | if (!which.empty()) |
| 712 | field << "-" << which; |
| 713 | propList.insert(field.str().c_str(), stream.str().c_str()); |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | std::ostream &operator<< (std::ostream &o, WPSBorder::Style const &style) |
| 718 | { |
| 719 | switch (style) |
| 720 | { |
| 721 | case WPSBorder::None: |
| 722 | o << "none"; |
| 723 | break; |
| 724 | case WPSBorder::Simple: |
| 725 | break; |
| 726 | case WPSBorder::Dot: |
| 727 | o << "dot"; |
| 728 | break; |
| 729 | case WPSBorder::LargeDot: |
| 730 | o << "large dot"; |
| 731 | break; |
| 732 | case WPSBorder::Dash: |
| 733 | o << "dash"; |
| 734 | break; |
| 735 | default: |
| 736 | WPS_DEBUG_MSG(("WPSBorder::operator<<: find unknown style\n")); |
| 737 | o << "#style=" << int(style); |
| 738 | break; |
| 739 | } |
| 740 | return o; |
| 741 | } |
| 742 | |
| 743 | std::ostream &operator<< (std::ostream &o, WPSBorder const &border) |
| 744 | { |
| 745 | o << border.m_style << ":"; |
| 746 | switch (border.m_type) |
| 747 | { |
| 748 | case WPSBorder::Single: |
| 749 | break; |
| 750 | case WPSBorder::Double: |
| 751 | o << "double:"; |
| 752 | break; |
| 753 | case WPSBorder::Triple: |
| 754 | o << "triple:"; |
| 755 | break; |
| 756 | default: |
| 757 | WPS_DEBUG_MSG(("WPSBorder::operator<<: find unknown type\n")); |
| 758 | o << "#type=" << int(border.m_type) << ":"; |
| 759 | break; |
| 760 | } |
| 761 | if (border.m_width > 1 || border.m_width < 1) o << "w=" << border.m_width << ":"; |
| 762 | if (!border.m_color.isBlack()) |
| 763 | o << "col=" << std::hex << border.m_color << std::dec << ":"; |
| 764 | o << ","; |
| 765 | size_t numRelWidth=border.m_widthsList.size(); |
| 766 | if (numRelWidth) |
| 767 | { |
| 768 | o << "bordW[rel]=["; |
| 769 | for (auto const &w : border.m_widthsList) |
| 770 | o << w << ","; |
| 771 | o << "]:"; |
| 772 | } |
| 773 | o << border.m_extra; |
| 774 | return o; |
| 775 | } |
| 776 | |
| 777 | //////////////////////////////////////////////////////////// |
| 778 | // object |
| 779 | //////////////////////////////////////////////////////////// |
| 780 | WPSEmbeddedObject::~WPSEmbeddedObject() |
| 781 | { |
| 782 | } |
| 783 | |
| 784 | bool WPSEmbeddedObject::addTo(librevenge::RVNGPropertyList &propList) const |
| 785 | { |
| 786 | bool firstSet=false; |
| 787 | librevenge::RVNGPropertyListVector auxiliarVector; |
| 788 | for (size_t i=0; i<m_dataList.size(); ++i) |
| 789 | { |
| 790 | if (m_dataList[i].empty()) continue; |
| 791 | std::string type=m_typeList.size() ? m_typeList[i] : "image/pict"; |
| 792 | if (!firstSet) |
| 793 | { |
| 794 | propList.insert("librevenge:mime-type", type.c_str()); |
| 795 | propList.insert("office:binary-data", m_dataList[i]); |
| 796 | firstSet=true; |
| 797 | continue; |
| 798 | } |
| 799 | librevenge::RVNGPropertyList auxiList; |
| 800 | auxiList.insert("librevenge:mime-type", type.c_str()); |
| 801 | auxiList.insert("office:binary-data", m_dataList[i]); |
| 802 | auxiliarVector.append(auxiList); |
| 803 | } |
| 804 | if (!auxiliarVector.empty()) |
| 805 | propList.insert("librevenge:replacement-objects", auxiliarVector); |
| 806 | if (!firstSet) |
| 807 | { |
| 808 | WPS_DEBUG_MSG(("WPSEmbeddedObject::addTo: called without picture\n")); |
| 809 | return false; |
| 810 | } |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | std::ostream &operator<<(std::ostream &o, WPSEmbeddedObject const &pict) |
| 815 | { |
| 816 | if (pict.isEmpty()) return o; |
| 817 | if (pict.m_size!=Vec2f()) |
| 818 | o << "size=" << pict.m_size << ","; |
| 819 | o << "["; |
| 820 | for (auto const &type : pict.m_typeList) |
| 821 | { |
| 822 | if (type.empty()) |
| 823 | o << "_,"; |
| 824 | else |
| 825 | o << type << ","; |
| 826 | } |
| 827 | o << "],"; |
| 828 | return o; |
| 829 | } |
| 830 | |
| 831 | //////////////////////////////////////////////////////////// |
| 832 | // unicode |
| 833 | //////////////////////////////////////////////////////////// |
| 834 | namespace libwps |
| 835 | { |
| 836 | void appendUnicode(uint32_t val, librevenge::RVNGString &buffer) |
| 837 | { |
| 838 | if (val < 0x20) |
| 839 | { |
| 840 | WPS_DEBUG_MSG(("libwps::appendUnicode: find an old char %x, skip it\n", val)); |
| 841 | return; |
| 842 | } |
| 843 | uint8_t first; |
| 844 | int len; |
| 845 | if (val < 0x80) |
| 846 | { |
| 847 | first = 0; |
| 848 | len = 1; |
| 849 | } |
| 850 | else if (val < 0x800) |
| 851 | { |
| 852 | first = 0xc0; |
| 853 | len = 2; |
| 854 | } |
| 855 | else if (val < 0x10000) |
| 856 | { |
| 857 | first = 0xe0; |
| 858 | len = 3; |
| 859 | } |
| 860 | else if (val < 0x200000) |
| 861 | { |
| 862 | first = 0xf0; |
| 863 | len = 4; |
| 864 | } |
| 865 | else if (val < 0x4000000) |
| 866 | { |
| 867 | first = 0xf8; |
| 868 | len = 5; |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | first = 0xfc; |
| 873 | len = 6; |
| 874 | } |
| 875 | |
| 876 | char outbuf[7]; |
| 877 | int i; |
| 878 | for (i = len - 1; i > 0; --i) |
| 879 | { |
| 880 | outbuf[i] = char((val & 0x3f) | 0x80); |
| 881 | val >>= 6; |
| 882 | } |
| 883 | outbuf[0] = char(val | first); |
| 884 | outbuf[len] = 0; |
| 885 | buffer.append(outbuf); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // a little geometry |
| 890 | WPSTransformation WPSTransformation::rotation(float angle, Vec2f const ¢er) |
| 891 | { |
| 892 | auto angl=float(double(angle)*M_PI/180); |
| 893 | auto cosA=float(std::cos(angl)); |
| 894 | auto sinA=float(std::sin(angl)); |
| 895 | return WPSTransformation(WPSVec3f(cosA, -sinA, center[0]-cosA*center[0]+sinA*center[1]), |
| 896 | WPSVec3f(sinA, cosA, center[1]-sinA*center[0]-cosA*center[1])); |
| 897 | } |
| 898 | |
| 899 | bool WPSTransformation::decompose(float &rot, Vec2f &shearing, WPSTransformation &transform, Vec2f const &origCenter) const |
| 900 | { |
| 901 | if (m_isIdentity) return false; |
| 902 | WPSVec3f const &xRow=(*this)[0]; |
| 903 | WPSVec3f const &yRow=(*this)[1]; |
| 904 | Vec2f const ¢er=*this * origCenter; |
| 905 | // first check shearing |
| 906 | float shearY=0; |
| 907 | float val1=xRow[0]*xRow[1]; |
| 908 | float val2=yRow[0]*yRow[1]; |
| 909 | float diff=val2-val1; |
| 910 | if (diff<-0.01f || diff>0.01f) |
| 911 | { |
| 912 | float const &A=val1; |
| 913 | float const B=xRow[1]*yRow[0]+xRow[0]*yRow[1]; |
| 914 | float const &C=diff; |
| 915 | if (A>=0 && A<=0) |
| 916 | { |
| 917 | if (B>=0 && A<=0) |
| 918 | { |
| 919 | WPS_DEBUG_MSG(("WPSTransformation::decompose: can not determine the shearing\n")); |
| 920 | return false; |
| 921 | } |
| 922 | shearY=C/B; |
| 923 | } |
| 924 | else |
| 925 | { |
| 926 | float const &delta=B*B-4*A*C; |
| 927 | if (delta<0) |
| 928 | { |
| 929 | WPS_DEBUG_MSG(("WPSTransformation::decompose: can not determine the shearing\n")); |
| 930 | return false; |
| 931 | } |
| 932 | shearY=(B-float(std::sqrt(delta)))/2.f/A; |
| 933 | } |
| 934 | transform=WPSTransformation::shear(Vec2f(0,-shearY), center) **this; |
| 935 | } |
| 936 | else |
| 937 | transform=*this; |
| 938 | shearing=Vec2f(0,shearY); |
| 939 | // fixme: we must first check for symetry here... |
| 940 | // now the rotation |
| 941 | rot=-std::atan2(-transform[1][0],transform[1][1]); |
| 942 | rot *= float(180/M_PI); |
| 943 | transform=WPSTransformation::rotation(-rot, center) * transform; |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | //////////////////////////////////////////////////////////// |
| 948 | // utility |
| 949 | //////////////////////////////////////////////////////////// |
| 950 | |
| 951 | namespace libwps |
| 952 | { |
| 953 | std::string getCellName(Vec2i const &cellPos, Vec2b const &relative) |
| 954 | { |
| 955 | if (cellPos[0]<0 || cellPos[1]<0) |
| 956 | { |
| 957 | WPS_DEBUG_MSG(("libwps::getCellName: invalid cell position\n")); |
| 958 | return ""; |
| 959 | } |
| 960 | std::stringstream o; |
| 961 | if (!relative[0]) o << "$"; |
| 962 | int c=cellPos[0]; |
| 963 | std::string colString(1, char(c%26+'A')); |
| 964 | c /= 26; |
| 965 | while (c>0) |
| 966 | { |
| 967 | --c; |
| 968 | colString.insert(0, std::string(1,char(c%26+'A'))); |
| 969 | c /= 26; |
| 970 | } |
| 971 | o << colString; |
| 972 | if (!relative[1]) o << "$"; |
| 973 | o << cellPos[1]+1; |
| 974 | return o.str(); |
| 975 | } |
| 976 | |
| 977 | bool encodeLotusPassword(char const *password, uint16_t &key, std::vector<uint8_t> &keys, uint8_t const(&defValues)[16]) |
| 978 | { |
| 979 | if (!password) |
| 980 | { |
| 981 | WPS_DEBUG_MSG(("libwps::encodeLotusPassword: called without password\n")); |
| 982 | return false; |
| 983 | } |
| 984 | size_t const len=16; |
| 985 | key=0xFFFF; |
| 986 | uint16_t val=0; |
| 987 | for (size_t i=0; i<len; ++i) |
| 988 | { |
| 989 | if (password[i]==0) |
| 990 | break; |
| 991 | auto c=uint8_t(password[i]); |
| 992 | key=uint16_t(key^c); |
| 993 | val=uint16_t((val&0xFF)|(key<<8)); |
| 994 | val=uint16_t(((val<<4)&0xFFF0)|(val>>12)); |
| 995 | key^=val; |
| 996 | val=uint16_t((val<<8)|(val>>8)); |
| 997 | val=uint16_t((val<<1)|(val>>15)); |
| 998 | val=uint16_t((val<<8)|(val>>8)); |
| 999 | key=uint16_t((key<<8)|(key>>8)); |
| 1000 | key^=val; |
| 1001 | |
| 1002 | val=uint16_t((((val>>4)&0xfff)|(val<<12))&0xe0ff); |
| 1003 | key^=val; |
| 1004 | val=uint16_t((val>>1)|(val<<15)); |
| 1005 | key=uint16_t(key^(val>>8)); |
| 1006 | } |
| 1007 | |
| 1008 | size_t cPos; |
| 1009 | keys.resize(len); |
| 1010 | // copy password in keys and fill the remaining space with |
| 1011 | // defValues |
| 1012 | for (cPos=0 ; cPos<len; ++cPos) |
| 1013 | { |
| 1014 | if (password[cPos]==0) |
| 1015 | break; |
| 1016 | keys[cPos]=uint8_t(password[cPos]); |
| 1017 | } |
| 1018 | uint8_t const *defPtr=defValues; |
| 1019 | for (; cPos<len; ++cPos) |
| 1020 | keys[cPos] = *(defPtr++); |
| 1021 | // now do an xor to code the result |
| 1022 | for (size_t i=0; i<len; ++i) |
| 1023 | keys[i]=uint8_t(keys[i]^(key>>((i%2)==0 ? 8 : 0))); |
| 1024 | return true; |
| 1025 | } |
| 1026 | } |
| 1027 | //////////////////////////////////////////////////////////// |
| 1028 | // debug |
| 1029 | //////////////////////////////////////////////////////////// |
| 1030 | |
| 1031 | namespace libwps |
| 1032 | { |
| 1033 | #ifdef DEBUG |
| 1034 | void printDebugMsg(const char *format, ...) |
| 1035 | { |
| 1036 | va_list args; |
| 1037 | va_start(args, format); |
| 1038 | std::vfprintf(stderr, format, args); |
| 1039 | va_end(args); |
| 1040 | } |
| 1041 | #endif |
| 1042 | } |
| 1043 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |