| 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 <cmath> |
| 26 | #include <sstream> |
| 27 | #include <limits> |
| 28 | #include <map> |
| 29 | #include <stack> |
| 30 | |
| 31 | #include <librevenge-stream/librevenge-stream.h> |
| 32 | |
| 33 | #include "libwps_internal.h" |
| 34 | #include "libwps_tools_win.h" |
| 35 | |
| 36 | #include "WKSChart.h" |
| 37 | #include "WKSContentListener.h" |
| 38 | #include "WPSEntry.h" |
| 39 | #include "WPSFont.h" |
| 40 | #include "WPSPosition.h" |
| 41 | |
| 42 | #include "QuattroDos.h" |
| 43 | |
| 44 | #include "QuattroDosChart.h" |
| 45 | |
| 46 | namespace QuattroDosChartInternal |
| 47 | { |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////// |
| 50 | //! the chart of a Quattro Pro Dos |
| 51 | |
| 52 | class Chart final : public WKSChart |
| 53 | { |
| 54 | public: |
| 55 | //! constructor |
| 56 | explicit Chart(QuattroDosChart &parser, RVNGInputStreamPtr const &input) |
| 57 | : WKSChart() |
| 58 | , m_spreadsheetId(-1) |
| 59 | , m_parser(parser) |
| 60 | , m_input(input) |
| 61 | { |
| 62 | } |
| 63 | //! send the zone content (called when the zone is of text type) |
| 64 | void sendContent(TextZone const &zone, WPSListenerPtr &listener) const final; |
| 65 | |
| 66 | //! the chart range |
| 67 | Position m_ranges[2]; |
| 68 | //! the sheet id |
| 69 | int m_spreadsheetId; |
| 70 | protected: |
| 71 | //! the parser |
| 72 | QuattroDosChart &m_parser; |
| 73 | //! the input |
| 74 | RVNGInputStreamPtr m_input; |
| 75 | }; |
| 76 | |
| 77 | void Chart::sendContent(TextZone const &zone, WPSListenerPtr &listener) const |
| 78 | { |
| 79 | if (!listener.get()) |
| 80 | { |
| 81 | WPS_DEBUG_MSG(("QuattroDosChartInternal::Chart::sendContent: no listener\n")); |
| 82 | return; |
| 83 | } |
| 84 | long pos = m_input->tell(); |
| 85 | listener->setFont(zone.m_font); |
| 86 | bool sendText=false; |
| 87 | for (auto const &e : zone.m_textEntryList) |
| 88 | { |
| 89 | if (!e.valid()) continue; |
| 90 | if (sendText) listener->insertEOL(true); |
| 91 | m_parser.sendText(e); |
| 92 | sendText=true; |
| 93 | } |
| 94 | m_input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 95 | } |
| 96 | |
| 97 | //! the state of QuattroDosChart |
| 98 | struct State |
| 99 | { |
| 100 | //! constructor |
| 101 | State() |
| 102 | : m_eof(-1) |
| 103 | , m_version(-1) |
| 104 | , m_chartType(-1) |
| 105 | , m_idToChartMap() |
| 106 | { |
| 107 | } |
| 108 | //! returns the pattern corresponding to a pattern id between 0 and 15 |
| 109 | static bool getPattern(int id, WPSGraphicStyle::Pattern &pattern); |
| 110 | |
| 111 | //! the last file position |
| 112 | long m_eof; |
| 113 | //! the file version |
| 114 | int m_version; |
| 115 | //! the chart type |
| 116 | int m_chartType; |
| 117 | //! map sheet id to chart |
| 118 | std::multimap<int,std::shared_ptr<Chart> > m_idToChartMap; |
| 119 | |
| 120 | }; |
| 121 | |
| 122 | bool State::getPattern(int id, WPSGraphicStyle::Pattern &pat) |
| 123 | { |
| 124 | if (id<0 || id>15) |
| 125 | { |
| 126 | WPS_DEBUG_MSG(("QuattroDosChartInternal::State::getPattern(): unknown pattern id: %d\n",id)); |
| 127 | return false; |
| 128 | } |
| 129 | static const uint16_t patterns[]= |
| 130 | { |
| 131 | 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x00ff, 0xff00, 0x00ff, 0xff00, 0xeedd, 0xbb77, 0xeedd, 0xbb77, // 0-3 |
| 132 | 0x3366, 0xcc99, 0x3366, 0xcc99, 0x8844, 0x2211, 0x8844, 0x2211, 0x99cc, 0x6633, 0x99cc, 0x6633, 0xff10, 0x1010, 0xff10, 0xff10, // 4-7 |
| 133 | 0xbf7f, 0xfefc, 0x7bb7, 0xcfdf, 0xaa55, 0xaa55, 0xaa55, 0xaa55, 0x7fff, 0xffff, 0xf7ff, 0xffff, 0x77ff, 0xddff, 0x77ff, 0xddff, // 8-9 |
| 134 | 0x990f, 0x050f, 0x99f0, 0x50f0, 0x0101, 0x01ff, 0x1010, 0x10ff, 0xbf7f, 0xfefc, 0x7bb7, 0xcfdf, 0xf77f, 0xbfdf, 0xeffe, 0xfdfb // 12-15 |
| 135 | }; |
| 136 | pat.m_dim=Vec2i(8,8); |
| 137 | uint16_t const *ptr=&patterns[4*id]; |
| 138 | pat.m_data.resize(8); |
| 139 | for (size_t i=0; i < 8; i+=2) |
| 140 | { |
| 141 | uint16_t val=*(ptr++); |
| 142 | pat.m_data[i]=static_cast<unsigned char>((val>>8) & 0xFF); |
| 143 | pat.m_data[i+1]=static_cast<unsigned char>(val & 0xFF); |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // constructor, destructor |
| 150 | QuattroDosChart::QuattroDosChart(QuattroDosParser &parser) |
| 151 | : m_input(parser.getInput()) |
| 152 | , m_listener() |
| 153 | , m_mainParser(parser) |
| 154 | , m_state(new QuattroDosChartInternal::State) |
| 155 | , m_asciiFile(parser.ascii()) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | QuattroDosChart::~QuattroDosChart() |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | int QuattroDosChart::version() const |
| 164 | { |
| 165 | if (m_state->m_version<0) |
| 166 | m_state->m_version=m_mainParser.version(); |
| 167 | return m_state->m_version; |
| 168 | } |
| 169 | |
| 170 | bool QuattroDosChart::checkFilePosition(long pos) |
| 171 | { |
| 172 | if (m_state->m_eof < 0) |
| 173 | { |
| 174 | long actPos = m_input->tell(); |
| 175 | m_input->seek(0, librevenge::RVNG_SEEK_END); |
| 176 | m_state->m_eof=m_input->tell(); |
| 177 | m_input->seek(actPos, librevenge::RVNG_SEEK_SET); |
| 178 | } |
| 179 | return pos <= m_state->m_eof; |
| 180 | } |
| 181 | |
| 182 | int QuattroDosChart::getNumSpreadsheets() const |
| 183 | { |
| 184 | auto it = m_state->m_idToChartMap.end(); |
| 185 | if (it==m_state->m_idToChartMap.begin()) |
| 186 | return 0; |
| 187 | --it; |
| 188 | if (it->first>255) |
| 189 | { |
| 190 | WPS_DEBUG_MSG(("QuattroDosChart::getNumSpreadsheets: the number of spreadsheets seems to big: %d\n", it->first)); |
| 191 | return 256; |
| 192 | } |
| 193 | return it->first+1; |
| 194 | } |
| 195 | |
| 196 | void QuattroDosChart::getChartPositionMap(int sheetId, std::map<Vec2i,Vec2i> &cellList) const |
| 197 | { |
| 198 | cellList.clear(); |
| 199 | auto it = m_state->m_idToChartMap.lower_bound(sheetId); |
| 200 | while (it!=m_state->m_idToChartMap.end() && it->first==sheetId) |
| 201 | { |
| 202 | if (it->second) |
| 203 | cellList[it->second->m_ranges[0].m_pos]=it->second->m_ranges[1].m_pos; |
| 204 | ++it; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | bool QuattroDosChart::updateSpreadsheetNames(QuattroDosChartInternal::Chart &chart) |
| 209 | { |
| 210 | for (auto &range : chart.m_ranges) |
| 211 | { |
| 212 | if (range.m_sheetId>=0) |
| 213 | range.m_sheetName = m_mainParser.getSheetName(range.m_sheetId); |
| 214 | } |
| 215 | for (auto &it : chart.getIdSerieMap()) |
| 216 | { |
| 217 | auto &serie=const_cast<WKSChart::Serie &>(it.second); |
| 218 | for (auto &range : serie.m_ranges) |
| 219 | { |
| 220 | if (range.m_sheetId>=0) |
| 221 | range.m_sheetName = m_mainParser.getSheetName(range.m_sheetId); |
| 222 | } |
| 223 | for (auto &range : serie.m_labelRanges) |
| 224 | { |
| 225 | if (range.m_sheetId>=0) |
| 226 | range.m_sheetName = m_mainParser.getSheetName(range.m_sheetId); |
| 227 | } |
| 228 | } |
| 229 | for (auto &range : chart.getAxis(0).m_labelRanges) |
| 230 | { |
| 231 | if (range.m_sheetId>=0) |
| 232 | range.m_sheetName = m_mainParser.getSheetName(range.m_sheetId); |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool QuattroDosChart::sendChart(int sheetId, Vec2i const &cell, Vec2f const &chartSize) |
| 238 | { |
| 239 | if (m_listener.get() == nullptr) |
| 240 | { |
| 241 | WPS_DEBUG_MSG(("QuattroDosChart::sendChart: I can not find the listener\n")); |
| 242 | return false; |
| 243 | } |
| 244 | auto it = m_state->m_idToChartMap.lower_bound(sheetId); |
| 245 | while (it!=m_state->m_idToChartMap.end() && it->first==sheetId) |
| 246 | { |
| 247 | if (it->second && it->second->m_ranges[0].m_pos==cell) |
| 248 | { |
| 249 | updateSpreadsheetNames(*it->second); |
| 250 | Vec2f size(chartSize[0]>0 ? chartSize[0] : 100, chartSize[1]>0 ? chartSize[1] : 100); |
| 251 | WPSPosition pos(Vec2f(0,0), size, librevenge::RVNG_POINT); |
| 252 | pos.m_anchorTo = WPSPosition::Cell; |
| 253 | auto endRange=it->second->m_ranges[1]; |
| 254 | endRange.m_pos += Vec2i(1,1); |
| 255 | pos.m_anchorCellName = endRange.getCellName(); |
| 256 | // the chart is in fact a 1024*7?? windows scaled back on the chosen cells |
| 257 | it->second->m_dimension=Vec2f(1024,700); |
| 258 | m_listener->insertChart(pos, *it->second); |
| 259 | return true; |
| 260 | } |
| 261 | ++it; |
| 262 | } |
| 263 | WPS_DEBUG_MSG(("QuattroDosChart::sendChart: can not find chart %dx%d[%d]\n", cell[0], cell[1], sheetId)); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | bool QuattroDosChart::sendText(WPSEntry const &entry) |
| 268 | { |
| 269 | if (m_listener.get() == nullptr) |
| 270 | { |
| 271 | WPS_DEBUG_MSG(("QuattroDosChart::sendText: I can not find the listener\n")); |
| 272 | return false; |
| 273 | } |
| 274 | if (!entry.valid()) |
| 275 | return true; |
| 276 | m_input->seek(entry.begin(), librevenge::RVNG_SEEK_SET); |
| 277 | std::string text; |
| 278 | for (long i=0; i<entry.length(); ++i) |
| 279 | { |
| 280 | auto c = char(libwps::readU8(m_input)); |
| 281 | if (c == '\0') continue; |
| 282 | text.push_back(c); |
| 283 | } |
| 284 | if (!text.empty()) |
| 285 | m_listener->insertUnicodeString(libwps_tools_win::Font::unicodeString(text, m_mainParser.getDefaultFontType())); |
| 286 | return true; |
| 287 | } |
| 288 | //////////////////////////////////////////////////////////// |
| 289 | // low level |
| 290 | |
| 291 | //////////////////////////////////////////////////////////// |
| 292 | // general |
| 293 | //////////////////////////////////////////////////////////// |
| 294 | bool QuattroDosChart::readChartSetType() |
| 295 | { |
| 296 | |
| 297 | long pos = m_input->tell(); |
| 298 | auto type = long(libwps::readU16(m_input)); |
| 299 | if (type != 0xb8 && type != 0xca) |
| 300 | { |
| 301 | WPS_DEBUG_MSG(("QuattroDosChart::readChartSetType: not a chart definition\n")); |
| 302 | return false; |
| 303 | } |
| 304 | libwps::DebugStream f; |
| 305 | f << "Entries(" << (type==0xb8 ? "Chart3d" : "ChartBubble") << "):"; |
| 306 | auto sz = long(libwps::readU16(m_input)); |
| 307 | if (sz!=1) |
| 308 | { |
| 309 | WPS_DEBUG_MSG(("QuattroDosChart::readChartSetType: find unexpected size\n")); |
| 310 | f << "###"; |
| 311 | ascii().addPos(pos); |
| 312 | ascii().addNote(f.str().c_str()); |
| 313 | m_input->seek(sz, librevenge::RVNG_SEEK_CUR); |
| 314 | return true; |
| 315 | } |
| 316 | m_state->m_chartType=int(libwps::readU8(m_input)); |
| 317 | f << "type=" << m_state->m_chartType << ","; |
| 318 | ascii().addPos(pos); |
| 319 | ascii().addNote(f.str().c_str()); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | bool QuattroDosChart::readChartName() |
| 324 | { |
| 325 | |
| 326 | long pos = m_input->tell(); |
| 327 | auto type = long(libwps::readU16(m_input)); |
| 328 | if (type != 0xb9) |
| 329 | { |
| 330 | WPS_DEBUG_MSG(("QuattroDosChart::readChartName: not a chart definition\n")); |
| 331 | return false; |
| 332 | } |
| 333 | libwps::DebugStream f; |
| 334 | f << "Entries(ChartName):"; |
| 335 | auto sz = long(libwps::readU16(m_input)); |
| 336 | if (sz!=0x10) |
| 337 | { |
| 338 | WPS_DEBUG_MSG(("QuattroDosChart::readChartName: find unexpected size\n")); |
| 339 | f << "###"; |
| 340 | ascii().addPos(pos); |
| 341 | ascii().addNote(f.str().c_str()); |
| 342 | m_input->seek(sz, librevenge::RVNG_SEEK_CUR); |
| 343 | return true; |
| 344 | } |
| 345 | librevenge::RVNGString name(""); |
| 346 | if (!m_mainParser.readPString(name,15)) |
| 347 | f << "##sSz,"; |
| 348 | else if (!name.empty()) |
| 349 | f << name.cstr() << ','; |
| 350 | ascii().addPos(pos); |
| 351 | ascii().addNote(f.str().c_str()); |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool QuattroDosChart::readChart() |
| 356 | { |
| 357 | int vers=version(); |
| 358 | libwps::DebugStream f; |
| 359 | |
| 360 | long pos = m_input->tell(); |
| 361 | auto type = long(libwps::readU16(m_input)); |
| 362 | if (type != 0x2D && type != 0x2e) |
| 363 | { |
| 364 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: not a chart definition\n")); |
| 365 | return false; |
| 366 | } |
| 367 | auto sz = long(libwps::readU16(m_input)); |
| 368 | int normalSz = 0x2ca; |
| 369 | if (type == 0x2e) normalSz += 0x10; |
| 370 | if (vers>=2) normalSz+=2*26+4; |
| 371 | if (sz < normalSz) |
| 372 | { |
| 373 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: chart definition too short\n")); |
| 374 | ascii().addPos(pos); |
| 375 | ascii().addNote("Entries(ChartDef):###"); |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | f << "Entries(ChartDef):"; |
| 380 | std::shared_ptr<QuattroDosChartInternal::Chart> chart(new QuattroDosChartInternal::Chart(*this, m_input)); |
| 381 | if (type == 0x2e) |
| 382 | { |
| 383 | long actPos = m_input->tell(); |
| 384 | librevenge::RVNGString name(""); |
| 385 | if (!m_mainParser.readPString(name,15)) |
| 386 | f << "##sSz,"; |
| 387 | else if (!name.empty()) |
| 388 | { |
| 389 | chart->m_name=name; |
| 390 | f << "title=" << name.cstr() << ','; |
| 391 | } |
| 392 | m_input->seek(actPos+16, librevenge::RVNG_SEEK_SET); |
| 393 | } |
| 394 | bool hasLegend=false; |
| 395 | for (int i = 0; i < 13; i++) // 0: X-serie, 1-6: data[series%d], 7-12:label serie%d |
| 396 | { |
| 397 | WKSChart::Position ranges[2]; |
| 398 | for (auto &range : ranges) |
| 399 | { |
| 400 | int dim[3]; |
| 401 | for (int d=0; d<(vers>=2 ? 3 : 2); ++d) dim[d] = int(libwps::read16(m_input)); |
| 402 | if (dim[0]==-1) |
| 403 | continue; |
| 404 | range = WKSChart::Position(Vec2i(dim[0],dim[1])); |
| 405 | range.m_sheetId=vers<2 ? 0 : dim[2]; |
| 406 | } |
| 407 | if (ranges[0].valid(ranges[1])) |
| 408 | { |
| 409 | f << "z" << i << "=" << ranges[0] << ":" << ranges[1] << ","; |
| 410 | if (i==0) |
| 411 | { |
| 412 | auto &axis=chart->getAxis(0); |
| 413 | axis.m_labelRanges[0]=ranges[0]; |
| 414 | axis.m_labelRanges[1]=ranges[1]; |
| 415 | } |
| 416 | else if (i<=6) |
| 417 | { |
| 418 | auto *serie=chart->getSerie(i-1, true); |
| 419 | serie->m_ranges[0]=ranges[0]; |
| 420 | serie->m_ranges[1]=ranges[1]; |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | auto *serie=chart->getSerie(i-7, false); |
| 425 | if (serie) |
| 426 | { |
| 427 | serie->m_labelRanges[0]=ranges[0]; |
| 428 | serie->m_labelRanges[1]=ranges[1]; |
| 429 | } |
| 430 | if (ranges[0]!=ranges[1]) |
| 431 | { |
| 432 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: unexpected label ranges for %d\n", i)); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | ascii().addPos(pos); |
| 438 | ascii().addNote(f.str().c_str()); |
| 439 | char const *axisNames[]= {"X", "Y", "Y2"}; |
| 440 | pos=m_input->tell(); |
| 441 | f.str(""); |
| 442 | f << "ChartDef-A:"; // GraphDef0 |
| 443 | auto chartType=int(libwps::readU8(m_input)); |
| 444 | if (m_state->m_chartType>=0) |
| 445 | { |
| 446 | chartType=m_state->m_chartType; |
| 447 | m_state->m_chartType=-1; |
| 448 | } |
| 449 | WKSChart::Serie::Type serieType=WKSChart::Serie::S_Bar; |
| 450 | switch (chartType) |
| 451 | { |
| 452 | case 0: // XY |
| 453 | serieType=WKSChart::Serie::S_Scatter; |
| 454 | break; |
| 455 | case 1: // basic column |
| 456 | break; |
| 457 | case 2: // pie |
| 458 | case 6: // column (like circle) |
| 459 | serieType=WKSChart::Serie::S_Circle; |
| 460 | break; |
| 461 | case 3: // area stacked |
| 462 | serieType=WKSChart::Serie::S_Area; |
| 463 | chart->m_dataStacked=true; |
| 464 | break; |
| 465 | case 4: // line |
| 466 | serieType=WKSChart::Serie::S_Line; |
| 467 | break; |
| 468 | case 5: // basic column |
| 469 | chart->m_dataStacked=true; |
| 470 | break; |
| 471 | case 7: // min-max |
| 472 | serieType=WKSChart::Serie::S_Stock; |
| 473 | break; |
| 474 | case 8: // basic bar |
| 475 | chart->m_dataVertical=true; |
| 476 | break; |
| 477 | case 9: // bubble |
| 478 | serieType=WKSChart::Serie::S_Bubble; |
| 479 | break; |
| 480 | case 10: // 3D-bar |
| 481 | case 12: // 3D-bar tie together |
| 482 | chart->m_is3D=true; |
| 483 | break; |
| 484 | case 11: // 3D-ribbon |
| 485 | chart->m_is3D=true; |
| 486 | serieType=WKSChart::Serie::S_Line; |
| 487 | break; |
| 488 | case 13: |
| 489 | chart->m_is3D=true; |
| 490 | serieType=WKSChart::Serie::S_Area; |
| 491 | chart->m_dataStacked=true; |
| 492 | break; |
| 493 | default: |
| 494 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: unknown chart type\n")); |
| 495 | f << "###"; |
| 496 | } |
| 497 | if (chartType) f << "type=" << chartType << ","; |
| 498 | |
| 499 | chart->m_type=serieType; |
| 500 | if (chartType==3 || chartType==5) chart->m_dataStacked=true; |
| 501 | if (chartType==8) chart->m_dataVertical=true; |
| 502 | auto pointType = serieType==WKSChart::Serie::S_Scatter ? WKSChart::Serie::P_Automatic : WKSChart::Serie::P_None; |
| 503 | for (int i=0; i<6; ++i) |
| 504 | { |
| 505 | auto *serie=chart->getSerie(i, false); |
| 506 | if (!serie) continue; |
| 507 | serie->m_type=serieType; |
| 508 | serie->m_pointType=pointType; |
| 509 | serie->m_style.m_lineWidth=1; |
| 510 | } |
| 511 | auto val=int(libwps::readU8(m_input)); |
| 512 | f << "grid="; |
| 513 | if (val&1) |
| 514 | f << "X"; |
| 515 | else |
| 516 | chart->getAxis(0).m_showGrid=false; |
| 517 | if (val&2) |
| 518 | f << "Y"; |
| 519 | else |
| 520 | chart->getAxis(1).m_showGrid=false; |
| 521 | if (val&0xFC) |
| 522 | f << "[##" << std::hex << (val&0xFC) << std::dec; |
| 523 | f << ","; |
| 524 | val=int(libwps::readU8(m_input)); |
| 525 | // bool useColor=val!=0; USEME |
| 526 | if (val==0) |
| 527 | f << "use[color]=no,"; |
| 528 | else if (val!=0xFF) |
| 529 | f << "use[color]=###"<< val << ","; |
| 530 | f << "unkn=["; |
| 531 | for (int i=0; i<6; ++i) |
| 532 | { |
| 533 | val=int(libwps::readU8(m_input)); |
| 534 | if (val) |
| 535 | f << val << ","; |
| 536 | else |
| 537 | f << "_,"; |
| 538 | } |
| 539 | f << "],"; |
| 540 | f << "align[serie]=["; |
| 541 | for (int i=0; i<6; ++i) // serie, label position, safe to ignore ? |
| 542 | { |
| 543 | val=int(libwps::readU8(m_input)); |
| 544 | if (val<6) |
| 545 | { |
| 546 | char const *wh[]= {"center", "left", "above", "right", "below", "none"}; |
| 547 | f << wh[val] << ","; |
| 548 | } |
| 549 | else |
| 550 | f << "##" << val << ","; |
| 551 | } |
| 552 | f << "],"; |
| 553 | for (int i=0; i<2; ++i) |
| 554 | { |
| 555 | auto &axis=chart->getAxis(i); |
| 556 | f << "axis" << axisNames[i] << "=["; |
| 557 | val=int(libwps::readU8(m_input)); |
| 558 | if (val==1) |
| 559 | { |
| 560 | f << "scale[manual],"; |
| 561 | axis.m_automaticScaling=false; |
| 562 | } |
| 563 | else if (val) |
| 564 | f << "scale=##" << val << ","; |
| 565 | for (int j=0; j<2; ++j) |
| 566 | { |
| 567 | long actPos=m_input->tell(); |
| 568 | double value; |
| 569 | bool isNaN; |
| 570 | if (!libwps::readDouble8(m_input, value, isNaN)) |
| 571 | { |
| 572 | m_input->seek(actPos+8, librevenge::RVNG_SEEK_SET); |
| 573 | f << "##value,"; |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | if (value<0 || value>0) |
| 578 | f << (j==0 ? "low" : "high") << "=" << value << ","; |
| 579 | axis.m_scaling[j]=float(value); |
| 580 | } |
| 581 | } |
| 582 | f << "],"; |
| 583 | } |
| 584 | m_input->seek(pos+49, librevenge::RVNG_SEEK_SET); |
| 585 | ascii().addPos(pos); |
| 586 | ascii().addNote(f.str().c_str()); |
| 587 | pos=m_input->tell(); |
| 588 | f.str(""); |
| 589 | f << "ChartDef-names:"; |
| 590 | for (int i = 0; i < 10; i++) |
| 591 | { |
| 592 | // ok to find string before i < 6 |
| 593 | // checkme after |
| 594 | long actPos = m_input->tell(); |
| 595 | int dataSz = i < 4 ? 40 : 20; |
| 596 | librevenge::RVNGString name(""); |
| 597 | if (!m_mainParser.readPString(name,dataSz-1)) |
| 598 | f << "##sSz,"; |
| 599 | if (name.empty()) |
| 600 | { |
| 601 | m_input->seek(actPos+dataSz, librevenge::RVNG_SEEK_SET); |
| 602 | continue; |
| 603 | } |
| 604 | if (i<2) |
| 605 | { |
| 606 | WPSEntry entry; |
| 607 | entry.setBegin(actPos+1); |
| 608 | entry.setEnd(m_input->tell()); |
| 609 | auto *textZone=chart->getTextZone(i==0 ? WKSChart::TextZone::T_Title : WKSChart::TextZone::T_SubTitle, true); |
| 610 | textZone->m_contentType=WKSChart::TextZone::C_Text; |
| 611 | textZone->m_textEntryList.push_back(entry); |
| 612 | f << (i==0 ? "title" : "subTitle") << "=" << name.cstr() << ","; |
| 613 | } |
| 614 | else if (i<4) |
| 615 | { |
| 616 | chart->getAxis(i-2).m_title=name; |
| 617 | f << (i==2 ? "x" : "y") << "Title=" << name.cstr() << ","; |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | auto *serie=chart->getSerie(i-3, false); |
| 622 | if (serie) |
| 623 | { |
| 624 | serie->m_legendText=name; |
| 625 | hasLegend=true; |
| 626 | } |
| 627 | f << "serie" << i-3 << "=" << name.cstr() << ","; |
| 628 | } |
| 629 | m_input->seek(actPos+dataSz, librevenge::RVNG_SEEK_SET); |
| 630 | } |
| 631 | ascii().addPos(pos); |
| 632 | ascii().addNote(f.str().c_str()); |
| 633 | |
| 634 | pos=m_input->tell(); |
| 635 | f.str(""); |
| 636 | f << "ChartDef-B:"; |
| 637 | for (int i=0; i<2; ++i) // axisX, axisY: format: USEME |
| 638 | { |
| 639 | val=int(libwps::readU8(m_input)); |
| 640 | if (val) f << "fmt" << axisNames[i] << "=" << val << ","; |
| 641 | } |
| 642 | for (int i=0; i<2; ++i) // axisX, axisY, number of tick: USEME |
| 643 | { |
| 644 | val=int(libwps::readU8(m_input)); |
| 645 | if (val) f << "num[tick" << axisNames[i] << "]=" << val << ","; |
| 646 | } |
| 647 | for (int i=0; i<2; ++i) |
| 648 | { |
| 649 | val=int(libwps::readU8(m_input)); |
| 650 | if (val==1 || val==255) |
| 651 | f << "display[scale" << axisNames[i] << "]=no,"; |
| 652 | else if (val) |
| 653 | f << "display[scale" << axisNames[i] << "]=##" << val << ","; |
| 654 | } |
| 655 | f << "pattern[series]=["; |
| 656 | int patternSeriesId[6]; |
| 657 | for (auto &pId : patternSeriesId) |
| 658 | { |
| 659 | pId=int(libwps::readU16(m_input)); |
| 660 | if (pId==0) |
| 661 | f << "_,"; |
| 662 | else |
| 663 | f << pId << ","; |
| 664 | } |
| 665 | f << "],"; |
| 666 | f << "colors2=["; |
| 667 | for (int i=0; i<3; ++i) |
| 668 | f << int(libwps::readU16(m_input)) << ","; |
| 669 | f << "],"; |
| 670 | f << "color[series]=["; |
| 671 | WPSColor colorSeriesId[6]; |
| 672 | for (auto &color : colorSeriesId) |
| 673 | { |
| 674 | auto cId=int(libwps::readU16(m_input)); |
| 675 | if (m_mainParser.getColor(cId, color)) |
| 676 | f << color << ","; |
| 677 | else |
| 678 | { |
| 679 | color=WPSColor(128,128,128); |
| 680 | f << "###" << cId << ","; |
| 681 | } |
| 682 | } |
| 683 | f << "],"; |
| 684 | f << "unkn=["; |
| 685 | for (int i=0; i<4; ++i) // 3 colors + big number |
| 686 | f << int(libwps::readU16(m_input)) << ","; |
| 687 | f << "],"; |
| 688 | auto cId=int(libwps::readU8(m_input)); |
| 689 | WPSColor backColor(WPSColor::white()); |
| 690 | if (m_mainParser.getColor(cId, backColor)) |
| 691 | { |
| 692 | chart->m_style.setSurfaceColor(backColor); |
| 693 | f << "col[background]=" << backColor << ","; |
| 694 | } |
| 695 | else |
| 696 | f << "col[background]=###" << val << ","; |
| 697 | val=int(libwps::readU8(m_input)); |
| 698 | if (val) f << "f0=" << val << ","; |
| 699 | m_input->seek(pos+46, librevenge::RVNG_SEEK_SET); |
| 700 | ascii().addPos(pos); |
| 701 | ascii().addNote(f.str().c_str()); |
| 702 | |
| 703 | pos=m_input->tell(); |
| 704 | f.str(""); |
| 705 | f << "ChartDef-fonts:"; |
| 706 | for (int i = 0; i < 6; ++i) // the series font? |
| 707 | { |
| 708 | WPSFont font; |
| 709 | libwps_tools_win::Font::Type fFontType; |
| 710 | if (!m_mainParser.readFont(font, fFontType)) |
| 711 | { |
| 712 | f << "###"; |
| 713 | m_input->seek(pos+(i+1)*8, librevenge::RVNG_SEEK_SET); |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | auto *serie=chart->getSerie(i, false); |
| 718 | if (serie) |
| 719 | serie->m_font=font; |
| 720 | } |
| 721 | f << "[" << font << "],"; |
| 722 | } |
| 723 | ascii().addPos(pos); |
| 724 | ascii().addNote(f.str().c_str()); |
| 725 | |
| 726 | pos=m_input->tell(); |
| 727 | f.str(""); |
| 728 | f << "ChartDef-C:"; |
| 729 | val=int(libwps::readU16(m_input)); |
| 730 | if (val) f << "f0=" << std::hex << val << std::dec << ","; |
| 731 | for (int i=0; i<6; ++i) |
| 732 | { |
| 733 | f << "serie" << i << "=["; |
| 734 | auto *serie = chart->getSerie(i, false); |
| 735 | val=int(libwps::readU8(m_input)); |
| 736 | if (val==1 || val==255) |
| 737 | { |
| 738 | if (serie) serie->m_useSecondaryY=true; |
| 739 | f << "secondary,"; |
| 740 | } |
| 741 | else if (val) |
| 742 | f << "coordinate=###" << val << ","; |
| 743 | val=int(libwps::readU8(m_input)); |
| 744 | if (val==1) |
| 745 | { |
| 746 | if (serie && serie->m_type==WKSChart::Serie::S_Line) |
| 747 | serie->m_type=WKSChart::Serie::S_Bar; |
| 748 | f << "bar,"; |
| 749 | } |
| 750 | else if (val==2) |
| 751 | { |
| 752 | if (serie && serie->m_type==WKSChart::Serie::S_Bar) |
| 753 | serie->m_type=WKSChart::Serie::S_Line; |
| 754 | f << "line,"; |
| 755 | } |
| 756 | else if (val) |
| 757 | f << "type=##" << val << ","; |
| 758 | f << "],"; |
| 759 | } |
| 760 | val=int(libwps::readU8(m_input)); |
| 761 | auto &axisYSecond=chart->getAxis(2); |
| 762 | if (val==1) |
| 763 | { |
| 764 | f << "scaleY2[manual],"; |
| 765 | axisYSecond.m_automaticScaling=false; |
| 766 | } |
| 767 | else if (val) |
| 768 | f << "scaleY2=##" << val << ","; |
| 769 | for (int i=0; i<2; ++i) |
| 770 | { |
| 771 | long actPos=m_input->tell(); |
| 772 | double value; |
| 773 | bool isNaN; |
| 774 | if (!libwps::readDouble8(m_input, value, isNaN)) |
| 775 | { |
| 776 | m_input->seek(actPos+8, librevenge::RVNG_SEEK_SET); |
| 777 | f << "##value,"; |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | if (value<0 || value>0) |
| 782 | f << (i==0 ? "lowY2" : "highY2") << "=" << value << ","; |
| 783 | axisYSecond.m_scaling[i]=float(value); |
| 784 | } |
| 785 | } |
| 786 | val=int(libwps::readU8(m_input)); |
| 787 | if (val) |
| 788 | f << "fmtY2=" << std::hex << val << std::dec << ","; |
| 789 | val=int(libwps::readU8(m_input)); |
| 790 | if (val) |
| 791 | f << "f1=" << val << ","; |
| 792 | m_input->seek(pos+33, librevenge::RVNG_SEEK_SET); |
| 793 | ascii().addPos(pos); |
| 794 | ascii().addNote(f.str().c_str()); |
| 795 | |
| 796 | pos=m_input->tell(); |
| 797 | f.str(""); |
| 798 | f << "ChartDef-name2:"; |
| 799 | librevenge::RVNGString name(""); |
| 800 | if (!m_mainParser.readPString(name,39)) |
| 801 | f << "##sSz,"; |
| 802 | else if (!name.empty()) f << name.cstr() << ","; |
| 803 | m_input->seek(pos+40, librevenge::RVNG_SEEK_SET); |
| 804 | ascii().addPos(pos); |
| 805 | ascii().addNote(f.str().c_str()); |
| 806 | |
| 807 | pos=m_input->tell(); |
| 808 | f.str(""); |
| 809 | f << "ChartDef-D:"; |
| 810 | val=int(libwps::readU8(m_input)); |
| 811 | if (val==0) |
| 812 | f << "text[pos]=bottom,"; |
| 813 | else if (val==1) |
| 814 | f << "text[pos]=right,"; |
| 815 | else if (val==2) |
| 816 | f << "text[pos]=none,"; |
| 817 | else |
| 818 | f << "text[pos]=##" << val << ","; |
| 819 | for (int i=0; i<3; ++i) // USEME |
| 820 | { |
| 821 | val=int(libwps::readU8(m_input)); |
| 822 | if (val==6) continue; |
| 823 | f << (i==0 ? "title" : i==1 ? "legend" : "graph") << "[ouline]="; |
| 824 | if (val>=0 && val<=7) |
| 825 | { |
| 826 | char const *wh[]= {"box", "doubleLine", "thickLine", "shadow", "3d", "rndRect", "none","sculpted"}; |
| 827 | f << wh[val] << ","; |
| 828 | } |
| 829 | else |
| 830 | f << "###" << val << ","; |
| 831 | } |
| 832 | for (int i=0; i<3; ++i) |
| 833 | { |
| 834 | auto &axis = chart->getAxis(i); |
| 835 | f << "axis" << (i==0 ? "X" : i==1 ? "Y" : "Y2") << "=["; |
| 836 | val=int(libwps::readU16(m_input)); |
| 837 | if (val) f << "f0=" << std::hex << val << std::dec << ","; |
| 838 | val=int(libwps::readU8(m_input)); |
| 839 | if (val==0) |
| 840 | axis.m_type= WKSChart::Axis::A_Numeric; |
| 841 | else if (val==1 || val==255) |
| 842 | { |
| 843 | f << "log,"; // 0:normal |
| 844 | axis.m_type= WKSChart::Axis::A_Logarithmic; |
| 845 | } |
| 846 | else f << "scale=###" << val << ","; |
| 847 | long actPos=m_input->tell(); |
| 848 | double value; |
| 849 | bool isNaN; |
| 850 | if (!libwps::readDouble8(m_input, value, isNaN)) |
| 851 | { |
| 852 | m_input->seek(actPos+8, librevenge::RVNG_SEEK_SET); |
| 853 | f << "##value,"; |
| 854 | } |
| 855 | else if (value<0 || value>0) // USEME |
| 856 | f << "increment=" << value << ","; |
| 857 | f << "],"; |
| 858 | } |
| 859 | val=int(libwps::readU8(m_input)); |
| 860 | if (val && val<=7) |
| 861 | { |
| 862 | // USEME |
| 863 | char const *wh[]= {"solid", "dotted", "center-line", "dashed", "heavy solid", |
| 864 | "heavy dotted", "heavy centered", "heavy dashed" |
| 865 | }; |
| 866 | f << "style[gridline]=" << wh[val] << ","; |
| 867 | } |
| 868 | else if (val) |
| 869 | f << "##style[gridline]=" << val << ","; |
| 870 | cId=int(libwps::readU8(m_input)); |
| 871 | WPSColor gridColor(WPSColor::black()); |
| 872 | if (m_mainParser.getColor(cId, gridColor)) |
| 873 | { |
| 874 | chart->setGridColor(gridColor); |
| 875 | f << "color[line/grid]=" << gridColor << ","; |
| 876 | } |
| 877 | else |
| 878 | f << "##color[line/grid]=" << cId << ","; |
| 879 | ascii().addDelimiter(m_input->tell(),'|'); |
| 880 | m_input->seek(pos+49, librevenge::RVNG_SEEK_SET); |
| 881 | ascii().addPos(pos); |
| 882 | ascii().addNote(f.str().c_str()); |
| 883 | |
| 884 | // time to update the serie pattern |
| 885 | if (hasLegend) |
| 886 | { |
| 887 | chart->getLegend().m_show=true; |
| 888 | chart->getLegend().m_autoPosition=true; |
| 889 | chart->getLegend().m_relativePosition=WPSBorder::RightBit; |
| 890 | } |
| 891 | for (int i=0; i<6; ++i) |
| 892 | { |
| 893 | auto *serie=chart->getSerie(i, false); |
| 894 | if (!serie) continue; |
| 895 | WPSGraphicStyle::Pattern pattern; |
| 896 | if (!m_state->getPattern(patternSeriesId[i],pattern)) |
| 897 | { |
| 898 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: oops, can not find pattern %d\n", i)); |
| 899 | serie->setPrimaryColor(colorSeriesId[i]); |
| 900 | serie->setSecondaryColor(gridColor); |
| 901 | continue; |
| 902 | } |
| 903 | pattern.m_colors[0]=colorSeriesId[i]; |
| 904 | pattern.m_colors[1]=backColor; |
| 905 | serie->setPrimaryPattern(pattern); |
| 906 | serie->setSecondaryColor(gridColor); |
| 907 | } |
| 908 | |
| 909 | pos=m_input->tell(); |
| 910 | f.str(""); |
| 911 | f << "ChartDef-E:"; |
| 912 | val=int(libwps::readU8(m_input)); |
| 913 | if (val) f << "tickX[alternate],"; |
| 914 | val=int(libwps::readU16(m_input)); |
| 915 | if (val) f << "f0=" << std::hex << val << std::dec << ","; |
| 916 | val=int(libwps::readU8(m_input)); |
| 917 | if (val) f << "use[depth],"; |
| 918 | ascii().addDelimiter(m_input->tell(),'|'); |
| 919 | m_input->seek(24, librevenge::RVNG_SEEK_CUR); |
| 920 | ascii().addDelimiter(m_input->tell(),'|'); |
| 921 | val=int(libwps::read16(m_input)); |
| 922 | if (val) f << "bar[width]=" << val << "%,"; |
| 923 | for (int i=0; i<2; ++i) // 0: local graph position, 1: title/sub title frames |
| 924 | { |
| 925 | int dim[4]; |
| 926 | for (auto &d : dim) d = int(libwps::read16(m_input)); |
| 927 | if (dim[0]==-1) |
| 928 | continue; |
| 929 | WPSBox2i box(Vec2i(dim[0],dim[1]), Vec2i(dim[2],dim[3])); |
| 930 | if (box==WPSBox2i()) continue; |
| 931 | f << (i==0 ? "grid" : "title") << "[pos]=" << box << ","; |
| 932 | if (i==0) // the size seems to be the windows size 1024*??? |
| 933 | chart->m_plotAreaPosition= |
| 934 | WPSBox2f(Vec2f(float(dim[0])/1024.f,float(dim[1])/700.f),Vec2f(float(dim[2])/1024.f,float(dim[3])/700.f)); |
| 935 | } |
| 936 | f << "unk=["; |
| 937 | for (int i=0; i<9; ++i) |
| 938 | { |
| 939 | val=int(libwps::readU8(m_input)); |
| 940 | if (val==255) |
| 941 | f << "*,"; |
| 942 | else if (val) |
| 943 | f << val << ","; |
| 944 | else |
| 945 | f << "_,"; |
| 946 | } |
| 947 | f << "],"; |
| 948 | WPSColor color; |
| 949 | cId=int(libwps::readU8(m_input)); |
| 950 | if (m_mainParser.getColor(cId, color)) |
| 951 | { |
| 952 | chart->m_wallStyle.setSurfaceColor(color); |
| 953 | f << "col[fill]=" << color << ","; |
| 954 | } |
| 955 | else |
| 956 | f << "col[fill]=###" << color << ","; |
| 957 | val=int(libwps::readU8(m_input)); |
| 958 | if (val) f << "f1=" << std::hex << val << std::dec << ","; |
| 959 | for (auto &range : chart->m_ranges) |
| 960 | { |
| 961 | int dim[3]; |
| 962 | for (int d=0; d<(vers>=2 ? 3 : 2); ++d) dim[d] = int(libwps::read16(m_input)); |
| 963 | if (dim[0]==-1) |
| 964 | continue; |
| 965 | chart->m_spreadsheetId=vers<2 ? 0 : dim[2]; |
| 966 | range = WKSChart::Position(Vec2i(dim[0],dim[1])); |
| 967 | range.m_sheetId=chart->m_spreadsheetId; |
| 968 | } |
| 969 | if (chart->m_ranges[0].valid(chart->m_ranges[1])) |
| 970 | { |
| 971 | f << "position=" << chart->m_ranges[0] << ":" << chart->m_ranges[1] << ","; |
| 972 | if (chart->m_ranges[1].m_pos[0]>255 || chart->m_ranges[1].m_pos[1]>65535) |
| 973 | { |
| 974 | f << "###"; |
| 975 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: oops, the maximum position seems bad\n")); |
| 976 | } |
| 977 | else if (chart->m_ranges[0].m_sheetId!=chart->m_ranges[1].m_sheetId) |
| 978 | { |
| 979 | f << "###"; |
| 980 | WPS_DEBUG_MSG(("QuattroDosChart::readChart: oops, the position is on different sheet\n")); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | m_state->m_idToChartMap.insert(std::multimap<int,std::shared_ptr<QuattroDosChartInternal::Chart> >::value_type(chart->m_spreadsheetId, chart)); |
| 985 | } |
| 986 | } |
| 987 | if (sz != normalSz) ascii().addDelimiter(m_input->tell(),'#'); |
| 988 | ascii().addPos(pos); |
| 989 | ascii().addNote(f.str().c_str()); |
| 990 | |
| 991 | return true; |
| 992 | } |
| 993 | |
| 994 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |