Coverage Report

Created: 2026-10-28 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmwaw/src/lib/WriterPlsParser.cxx
Line
Count
Source
1
/* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3
/* libmwaw
4
* Version: MPL 2.0 / LGPLv2+
5
*
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 2.0 (the "License"); you may not use this file except in compliance with
8
* the License or as specified alternatively below. You may obtain a copy of
9
* the License at http://www.mozilla.org/MPL/
10
*
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
14
* License.
15
*
16
* Major Contributor(s):
17
* Copyright (C) 2002 William Lachance (wrlach@gmail.com)
18
* Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net)
19
* Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
20
* Copyright (C) 2006, 2007 Andrew Ziem
21
* Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr)
22
*
23
*
24
* All Rights Reserved.
25
*
26
* For minor contributions see the git repository.
27
*
28
* Alternatively, the contents of this file may be used under the terms of
29
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30
* in which case the provisions of the LGPLv2+ are applicable
31
* instead of those above.
32
*/
33
34
#include <cstring>
35
#include <iomanip>
36
#include <iostream>
37
#include <limits>
38
#include <sstream>
39
40
#include <librevenge/librevenge.h>
41
42
#include "MWAWCell.hxx"
43
#include "MWAWTextListener.hxx"
44
#include "MWAWFont.hxx"
45
#include "MWAWFontConverter.hxx"
46
#include "MWAWHeader.hxx"
47
#include "MWAWParagraph.hxx"
48
#include "MWAWPosition.hxx"
49
#include "MWAWPictMac.hxx"
50
#include "MWAWPrinter.hxx"
51
#include "MWAWSection.hxx"
52
#include "MWAWSubDocument.hxx"
53
#include "MWAWTable.hxx"
54
55
#include "WriterPlsParser.hxx"
56
57
/** Internal: the structures of a WriterPlsParser */
58
namespace WriterPlsParserInternal
59
{
60
//! Page informations
61
struct PageInfo {
62
  PageInfo()
63
4.87k
    : m_firstLine(0)
64
4.87k
    , m_height(0)
65
4.87k
    , m_heightFromBegin(0)
66
4.87k
  {
67
4.87k
    std::memset(m_unknown, 0, sizeof(m_unknown));
68
4.87k
  }
69
  friend std::ostream &operator<<(std::ostream &o, PageInfo const &p)
70
0
  {
71
0
    o << "firstLine=" << p.m_firstLine
72
0
      << ", height=" << p.m_height
73
0
      << ", height[fromStart]=" << p.m_heightFromBegin;
74
0
    if (p.m_unknown[0] != 1) o << ", unkn0=" << p.m_unknown[0];
75
0
    if (p.m_unknown[1]) o << ", unkn1=" << p.m_unknown[1];
76
0
    return o;
77
0
  }
78
79
  int m_firstLine;
80
  int m_unknown[2]; // 1 0
81
  int m_height, m_heightFromBegin;
82
};
83
84
//! Column informations
85
struct ColumnInfo {
86
  ColumnInfo()
87
4.92k
    : m_firstLine(0)
88
4.92k
    , m_height(0)
89
4.92k
    , m_col(0)
90
4.92k
    , m_numCol(1)
91
4.92k
  {
92
4.92k
    std::memset(m_unknown, 0, sizeof(m_unknown));
93
4.92k
  }
94
  friend std::ostream &operator<<(std::ostream &o, ColumnInfo const &c)
95
0
  {
96
0
    o << "firstLine=" << c.m_firstLine
97
0
      << ", col=" << c.m_col << "/" << c.m_numCol
98
0
      << ", height=" << c.m_height
99
0
      << ", dim?=" << c.m_unknown[3];
100
0
    if (c.m_unknown[0]) o << ", unkn0=" << c.m_unknown[0];
101
0
    if (c.m_unknown[1] != 1) o << ", unkn1=" << c.m_unknown[1];
102
0
    if (c.m_unknown[2]) o << ", unkn2=" << c.m_unknown[2];
103
0
    return o;
104
0
  }
105
106
  int m_firstLine;
107
  int m_unknown[4]; // 0 1 0
108
  int m_height, m_col, m_numCol;
109
};
110
111
//! Column informations in a table
112
struct ColumnTableInfo {
113
  ColumnTableInfo()
114
274k
    : m_height(0)
115
274k
    , m_numData(0)
116
274k
    , m_flags(0)
117
274k
  {
118
549k
    for (auto &col : m_colX) col=0;
119
824k
    for (auto &text : m_textX) text=0;
120
274k
  }
121
  friend std::ostream &operator<<(std::ostream &o, ColumnTableInfo const &c)
122
0
  {
123
0
    o << "height=" << c.m_height
124
0
      << ", numData=" << c.m_numData
125
0
      << ", colX=" <<  c.m_colX[0] << "<->" << c.m_colX[1]
126
0
      << ", textX=" <<  c.m_textX[0] << "<->" << c.m_textX[1];
127
0
    if (c.m_textX[0] !=  c.m_textX[2])
128
0
      o << ", textX[begin?]=" << c.m_textX[2];
129
0
    if (c.m_flags) o << ", flags=" << c.m_flags;
130
0
    return o;
131
0
  }
132
133
  int m_height;
134
  int m_numData;
135
  int m_colX[2]; // x : begin and end pos
136
  int m_textX[3]; // x : begin|end|begin pos
137
  int m_flags; // 0
138
};
139
140
//! Paragraph informations
141
struct ParagraphInfo {
142
  ParagraphInfo()
143
174k
    : m_pos(0)
144
174k
    , m_type(-2)
145
174k
    , m_height(0)
146
174k
    , m_height2(0)
147
174k
    , m_width(0)
148
174k
    , m_numLines(0)
149
174k
    , m_linesHeight()
150
174k
    , m_unknowns()
151
174k
  {
152
1.04M
    for (auto &fl : m_flags) fl=0;
153
174k
  }
154
  int getType() const
155
564k
  {
156
564k
    if (m_type >= 8) return (m_type & 0x7);
157
355k
    return m_type;
158
564k
  }
159
  friend std::ostream &operator<<(std::ostream &o, ParagraphInfo const &p)
160
0
  {
161
0
    int type = p.m_type;
162
0
    bool typeFlag = false;
163
0
    if (type >= 8) {
164
0
      typeFlag = true;
165
0
      type &= 7;
166
0
    }
167
0
    switch (type) {
168
0
    case 0:
169
0
      o << "text";
170
0
      break;
171
0
    case 1:
172
0
      o << "section";
173
0
      break;
174
0
    case 2:
175
0
      o << "text2";
176
0
      break;
177
0
    case 3:
178
0
      o << "colBreak";
179
0
      break;
180
0
    case 4:
181
0
      o << "graphics";
182
0
      break;
183
0
    case 5:
184
0
      o << "table";
185
0
      break;
186
0
187
0
    case -1:
188
0
      o << "empty";
189
0
      break;
190
0
    case -2:
191
0
      break;
192
0
    default:
193
0
      o << "type=" << type;
194
0
      break;
195
0
    }
196
0
    if (typeFlag) o << "[in table],";
197
0
    else o << ",";
198
0
199
0
    if (p.m_pos) o << "pos=" << std::hex << p.m_pos << std::dec << ",";
200
0
    o << "h=" << p.m_height << ",";
201
0
    if (p.m_height2 != p.m_height) o << "h[next]=" << p.m_height2 << ",";
202
0
    if (p.m_width) o << "w=" << p.m_width << ",";
203
0
    if (type == 5) {
204
0
      o << "numCols=" << p.m_numLines << ",";
205
0
      if (!p.m_linesHeight.empty()) {
206
0
        o << "numDataByCols=[";
207
0
        for (auto i : p.m_linesHeight)
208
0
          o << i << ",";
209
0
        o << "],";
210
0
      }
211
0
    }
212
0
    else {
213
0
      if (p.m_numLines) o << "numLines=" << p.m_numLines << ",";
214
0
      if (!p.m_linesHeight.empty()) {
215
0
        o << "lineH=[";
216
0
        for (auto i : p.m_linesHeight)
217
0
          o << i << ",";
218
0
        o << "],";
219
0
      }
220
0
    }
221
0
    for (int i= 0; i < 6; i++) {
222
0
      if (!p.m_flags[i]) continue;
223
0
      o << "f" << i << "=" << std::hex << p.m_flags[i] << std::dec << ",";
224
0
    }
225
0
    if (p.m_unknowns.size()) {
226
0
      o << "unkn=[";
227
0
      for (auto unk : p.m_unknowns) {
228
0
        if (unk)
229
0
          o << unk << ",";
230
0
        else
231
0
          o << "_,";
232
0
      }
233
0
      o << "],";
234
0
    }
235
0
    return o;
236
0
  }
237
238
  long m_pos;
239
  int m_type;
240
241
  int m_height, m_height2, m_width, m_numLines;
242
  std::vector<int> m_linesHeight;
243
  int m_flags[6];
244
  std::vector<int> m_unknowns;
245
};
246
247
//! Windows informations
248
struct WindowsInfo {
249
  struct Zone {
250
    Zone()
251
1.69M
      : m_number(0)
252
1.69M
      , m_size(0)
253
1.69M
      , m_width(0)
254
1.69M
    {
255
5.09M
      for (auto &unk : m_unknown) unk = 0;
256
1.69M
    }
257
    bool empty() const
258
0
    {
259
0
      return m_number==0 && m_size==0;
260
0
    }
261
    friend std::ostream &operator<<(std::ostream &o, Zone const &z)
262
0
    {
263
0
      o << "N=" << z.m_number << ", sz=" << std::hex << z.m_size << std::dec;
264
0
      o << ", w=" << z.m_width;
265
0
      for (int i = 0; i < 3; i++) {
266
0
        if (!z.m_unknown[i]) continue;
267
0
        o << ", f" << i << "=" << z.m_unknown[i];
268
0
      }
269
0
      return o;
270
0
    }
271
    int m_number;
272
    int m_size;
273
    int m_width;
274
    int m_unknown[3];
275
  };
276
277
  friend std::ostream &operator<<(std::ostream &o, WindowsInfo const &w);
278
279
  WindowsInfo()
280
202k
    : m_pageDim()
281
202k
    , m_headerY(0)
282
202k
    , m_footerY(0)
283
202k
    , m_pages()
284
202k
    , m_columns()
285
202k
    , m_paragraphs()
286
202k
  {
287
202k
  }
288
289
  bool dimensionInvalid() const
290
40.3k
  {
291
40.3k
    return (m_pageDim.x() < 0 || m_pageDim.y() < 0 ||
292
39.7k
            m_headerY < 0 || m_footerY < 0 ||
293
39.2k
            m_headerY+m_footerY > m_pageDim.y());
294
40.3k
  }
295
296
  bool getColumnLimitsFor(int line, std::vector<int> &listPos);
297
298
  MWAWVec2i m_pageDim;
299
  int m_headerY, m_footerY;
300
  std::vector<PageInfo> m_pages;
301
  std::vector<ColumnInfo> m_columns;
302
  std::vector<ParagraphInfo> m_paragraphs;
303
304
  // ????, pages, columns, parag, ???, ???, ???
305
  Zone m_zone[7];
306
};
307
308
bool WindowsInfo::getColumnLimitsFor(int line, std::vector<int> &listPos)
309
32.9k
{
310
32.9k
  listPos.resize(0);
311
312
32.9k
  size_t numColumns = m_columns.size();
313
32.9k
  size_t firstColumn = 0; // initialized to make clang analyzer happier
314
32.9k
  int numCols = 0;
315
36.2k
  for (size_t i = 0; i < numColumns; i++) {
316
4.29k
    if (m_columns[i].m_firstLine == line+2) {
317
188
      numCols=m_columns[i].m_numCol;
318
188
      firstColumn = i;
319
188
      if (numCols > int(unsigned(numColumns - firstColumn)))
320
119
        numCols = int(unsigned(numColumns - firstColumn));
321
188
      if (numCols <= 1 || m_columns[i].m_col != 1) return false;
322
89
      break;
323
188
    }
324
4.10k
    if (m_columns[i].m_firstLine > line+2)
325
842
      return true;
326
4.10k
  }
327
32.0k
  if (numCols <= 1)
328
31.9k
    return true;
329
330
89
  size_t numPara = m_paragraphs.size();
331
89
  listPos.resize(size_t(numCols));
332
224
  for (size_t i = 0; i < size_t(numCols); i++) {
333
223
    ColumnInfo const &colInfo =  m_columns[firstColumn++];
334
223
    int l = colInfo.m_firstLine-1;
335
223
    if (l < 0 || l >= static_cast<int>(numPara)) {
336
62
      MWAW_DEBUG_MSG(("WindowsInfo::getColumnLimitsFor: pb with line position\n"));
337
62
      return false;
338
62
    }
339
161
    if (i && m_paragraphs[size_t(l)].getType() != 3) {
340
26
      MWAW_DEBUG_MSG(("WindowsInfo::getColumnLimitsFor: can not find cols break\n"));
341
26
      return false;
342
26
    }
343
344
135
    listPos[i] = (i == 0) ? l-1 : l;
345
135
  }
346
1
  return true;
347
89
}
348
349
std::ostream &operator<<(std::ostream &o, WindowsInfo const &w)
350
0
{
351
0
  if (w.m_pageDim.x() || w.m_pageDim.y())
352
0
    o << "pagesDim=" << w.m_pageDim << ",";
353
0
  if (w.m_headerY) o << "header[Height]=" << w.m_headerY << ",";
354
0
  if (w.m_footerY) o << "footer[Height]=" << w.m_footerY << ",";
355
0
  for (int i = 0; i < 7; i++) {
356
0
    if (w.m_zone[i].empty()) continue;
357
0
    switch (i) {
358
0
    case 1:
359
0
      o << "zonePages";
360
0
      break;
361
0
    case 2:
362
0
      o << "zoneCols?";
363
0
      break;
364
0
    case 3:
365
0
      o << "zoneParag";
366
0
      break;
367
0
    default:
368
0
      o << "unkZone" << i;
369
0
      break;
370
0
    }
371
0
    o << "=[" << w.m_zone[i] << "], ";
372
0
  }
373
0
  return o;
374
0
}
375
376
////////////////////////////////////////
377
/** Internal: class to store the font properties */
378
struct Font {
379
  Font()
380
747k
    : m_font()
381
747k
    , m_firstChar(0)
382
747k
  {
383
747k
  }
384
385
  //! operator<<
386
  friend std::ostream &operator<<(std::ostream &o, Font const &f)
387
0
  {
388
0
    if (f.m_firstChar) o << "firstChar=" << f.m_firstChar << ",";
389
0
    return o;
390
0
  }
391
  //! the font
392
  MWAWFont m_font;
393
  //! the first character
394
  int m_firstChar;
395
};
396
397
////////////////////////////////////////
398
/** Internal: class to store the line  properties */
399
struct Line {
400
  Line()
401
174M
    : m_firstChar(0)
402
174M
    , m_height(0)
403
174M
    , m_width(0)
404
174M
    , m_maxFontSize(0)
405
174M
  {
406
697M
    for (auto &fl : m_flags) fl=0;
407
174M
  }
408
409
  //! operator<<
410
  friend std::ostream &operator<<(std::ostream &o, Line const &l)
411
0
  {
412
0
    if (l.m_firstChar) o << "firstChar=" << l.m_firstChar << ",";
413
0
    o << "height=" << l.m_height << ", width=" << l.m_width;
414
0
    for (int i = 0; i < 4; i++) {
415
0
      if (!l.m_flags[i]) continue;
416
0
      o << ", lF" << i << "=" << std::hex << l.m_flags[i] << std::dec;
417
0
    }
418
0
    return o;
419
0
  }
420
  //! the first character
421
  int m_firstChar;
422
  int m_height/** the height */, m_width /** the width */;
423
  int m_maxFontSize; /** the maximum font size */
424
  //! some flag
425
  int m_flags[4]; // flags[0]  a small number : a,b,c
426
};
427
428
////////////////////////////////////////
429
/** Internal: class to store the Graphic properties */
430
struct GraphicInfo {
431
  GraphicInfo()
432
14.6M
    : m_width(0)
433
14.6M
    , m_graphicWidth(0)
434
14.6M
  {
435
102M
    for (auto &fl : m_flags) fl=0;
436
14.6M
  }
437
438
  //! operator<<
439
  friend std::ostream &operator<<(std::ostream &o, GraphicInfo const &g)
440
0
  {
441
0
    o << "width=" << g.m_graphicWidth << ", width[line]=" << g.m_width;
442
0
    for (int i = 0; i < 6; i++) { // m_flags[6] seems to be junk
443
0
      if (!g.m_flags[i]) continue;
444
0
      o << ", gF" << i << "=" << std::hex << g.m_flags[i] << std::dec;
445
0
    }
446
0
    return o;
447
0
  }
448
  //! the first character
449
  int m_width/** the line width */, m_graphicWidth /** the graphic width */;
450
  //! some flag
451
  int m_flags[7];
452
};
453
454
////////////////////////////////////////
455
/** Internal: class to store the Section properties */
456
struct SectionInfo {
457
  SectionInfo()
458
26.8M
    : m_numCol(0)
459
26.8M
  {
460
80.4M
    for (auto &dim : m_dim) dim = 0;
461
107M
    for (auto &fl : m_flags) fl = 0;
462
26.8M
  }
463
464
  bool empty() const
465
26.8M
  {
466
26.8M
    if (m_numCol) return false;
467
26.7M
    for (auto dim : m_dim)
468
80.2M
      if (dim) return false;
469
26.7M
    for (auto fl : m_flags)
470
106M
      if (fl) return false;
471
26.7M
    return true;
472
26.7M
  }
473
  //! operator<<
474
  friend std::ostream &operator<<(std::ostream &o, SectionInfo const &s)
475
0
  {
476
0
    if (s.m_numCol) o << "numCols?=" << s.m_numCol << ",";
477
0
    o << "dim?=[";
478
0
    for (auto dim : s.m_dim)
479
0
      o << dim << ",";
480
0
    o << "],";
481
0
    for (int i = 0; i < 4; i++) {
482
0
      if (!s.m_flags[i]) continue;
483
0
      o << ", sF" << i << "=" << std::hex << s.m_flags[i] << std::dec;
484
0
    }
485
0
    return o;
486
0
  }
487
  //! the number of columns(?)
488
  int m_numCol;
489
  //! unknown dimension
490
  int m_dim[3];
491
  //! some flag
492
  int m_flags[4];
493
};
494
495
////////////////////////////////////////
496
/** Internal: class to store the beginning of all paragraph data */
497
struct ParagraphData {
498
  //! Constructor
499
  ParagraphData()
500
950k
    : m_type(-1)
501
950k
    , m_typeFlag(0)
502
950k
    , m_height(0)
503
950k
    , m_width(0)
504
950k
    , m_unknown(0)
505
950k
    , m_text("")
506
950k
    , m_fonts()
507
950k
    , m_endPos(0)
508
950k
  {
509
1.90M
    for (auto &indent : m_indent) indent=0;
510
1.90M
    for (auto &numData : m_numData) numData=0;
511
950k
  }
512
  //! operator<<
513
  friend std::ostream &operator<<(std::ostream &o, ParagraphData const &p)
514
0
  {
515
0
    switch (p.m_type) {
516
0
    case 0:
517
0
      o << "text";
518
0
      break;
519
0
    case 1:
520
0
      o << "section";
521
0
      break; // checkme find only one time before a colbreak
522
0
    case 2:
523
0
      o << "text2";
524
0
      break;  // what is the difference with 1
525
0
    case 3:
526
0
      o << "colBreak";
527
0
      break;  // find only one time to change column
528
0
    case 4:
529
0
      o << "graphic";
530
0
      break;
531
0
    case 5:
532
0
      o << "table";
533
0
      break;
534
0
    default:
535
0
      o << "type=" << p.m_type;
536
0
      break;
537
0
    }
538
0
    switch (p.m_typeFlag) {
539
0
    case 0:
540
0
      break;
541
0
    case 0x80:
542
0
      o << "[in table]";
543
0
      break;
544
0
    default:
545
0
      o << "[" << std::hex << p.m_typeFlag << std::dec << "],";
546
0
    }
547
0
    o << ",";
548
0
549
0
    o << "height=" << p.m_height << ",";
550
0
    o << "witdh=" << p.m_width << ",";
551
0
    if (p.m_indent[0]) o << "indent[left]=" << p.m_indent[0] << ",";
552
0
    if (p.m_indent[1] != p.m_indent[0])
553
0
      o << "indent[firstPos]=" << p.m_indent[1] << ",";
554
0
    if (p.m_text.length()) o << "text='" << p.m_text << "',";
555
0
    if (p.m_type==5) o << "numData[total]=" << p.m_unknown << ",";
556
0
    else o << "unkn=" << p.m_unknown << ","; /* in text2: often 1, but can be 5|13|25|29 */
557
0
    return o;
558
0
  }
559
560
  int m_type, m_typeFlag;
561
  int m_height, m_width;
562
  int m_indent[2]; // left indent and ?
563
  int m_unknown;
564
565
  std::string m_text;
566
  std::vector<Font> m_fonts;
567
568
  long m_endPos; // end of the data ( except if there is auxilliary data )
569
  int m_numData[2]; // number of data[1] ( always font?), data[2]
570
};
571
572
////////////////////////////////////////
573
//! Internal: the state of a WriterPlsParser
574
struct State {
575
  //! constructor
576
  State()
577
53.9k
    : m_actPage(0)
578
53.9k
    , m_numPages(0)
579
53.9k
    , m_headerHeight(0)
580
53.9k
    , m_footerHeight(0)
581
53.9k
  {
582
53.9k
  }
583
584
  int m_actPage /** the actual page */, m_numPages /** the number of page of the final document */;
585
586
  //! the information ( 0: main, 1: header, 2: footer)
587
  WindowsInfo m_windows[3];
588
589
  int m_headerHeight /** the header height if known */,
590
      m_footerHeight /** the footer height if known */;
591
};
592
593
////////////////////////////////////////
594
//! Internal: the subdocument of a WriterPlsParser
595
class SubDocument final : public MWAWSubDocument
596
{
597
public:
598
  SubDocument(WriterPlsParser &pars, MWAWInputStreamPtr const &input, int zoneId)
599
108
    : MWAWSubDocument(&pars, input, MWAWEntry()), m_id(zoneId)
600
108
  {
601
108
  }
602
603
  //! destructor
604
0
  ~SubDocument() final {}
605
606
  //! operator!=
607
  bool operator!=(MWAWSubDocument const &doc) const final
608
0
  {
609
0
    if (MWAWSubDocument::operator!=(doc)) return true;
610
0
    auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
611
0
    if (!sDoc) return true;
612
0
    if (m_id != sDoc->m_id) return true;
613
0
    return false;
614
0
  }
615
616
  //! the parser function
617
  void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
618
619
protected:
620
  //! the subdocument id
621
  int m_id;
622
};
623
624
void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
625
108
{
626
108
  if (!listener.get()) {
627
0
    MWAW_DEBUG_MSG(("WriterPlsParserInternal::SubDocument::parse: no listener\n"));
628
0
    return;
629
0
  }
630
108
  if (m_id != 1 && m_id != 2) {
631
0
    MWAW_DEBUG_MSG(("WriterPlsParserInternal::SubDocument::parse: unknown zone\n"));
632
0
    return;
633
0
  }
634
108
  auto *parser=dynamic_cast<WriterPlsParser *>(m_parser);
635
108
  if (!parser) {
636
0
    MWAW_DEBUG_MSG(("WriterPlsParserInternal::SubDocument::parse: no parser\n"));
637
0
    return;
638
0
  }
639
640
108
  long pos = m_input->tell();
641
108
  parser->sendWindow(m_id);
642
108
  m_input->seek(pos, librevenge::RVNG_SEEK_SET);
643
108
}
644
}
645
646
////////////////////////////////////////////////////////////
647
// constructor/destructor, ...
648
////////////////////////////////////////////////////////////
649
WriterPlsParser::WriterPlsParser(MWAWInputStreamPtr const &input, MWAWRSRCParserPtr const &rsrcParser, MWAWHeader *header)
650
22.5k
  : MWAWTextParser(input, rsrcParser, header)
651
22.5k
  , m_state()
652
22.5k
{
653
22.5k
  init();
654
22.5k
}
655
656
WriterPlsParser::~WriterPlsParser()
657
22.5k
{
658
22.5k
}
659
660
void WriterPlsParser::init()
661
22.5k
{
662
22.5k
  resetTextListener();
663
22.5k
  setAsciiName("main-1");
664
665
22.5k
  m_state.reset(new WriterPlsParserInternal::State);
666
667
  // reduce the margin (in case, the page is not defined)
668
22.5k
  getPageSpan().setMargins(0.1);
669
22.5k
}
670
671
////////////////////////////////////////////////////////////
672
// position and height
673
////////////////////////////////////////////////////////////
674
double WriterPlsParser::getTextHeight() const
675
4.66k
{
676
4.66k
  return getPageSpan().getPageLength()-m_state->m_headerHeight/72.0-m_state->m_footerHeight/72.0;
677
4.66k
}
678
679
////////////////////////////////////////////////////////////
680
// new page
681
////////////////////////////////////////////////////////////
682
void WriterPlsParser::newPage(int number)
683
8.41k
{
684
8.41k
  if (number <= m_state->m_actPage || number > m_state->m_numPages)
685
5.78k
    return;
686
687
5.25k
  while (m_state->m_actPage < number) {
688
2.62k
    m_state->m_actPage++;
689
2.62k
    if (!getTextListener() || m_state->m_actPage == 1)
690
2.62k
      continue;
691
4
    getTextListener()->insertBreak(MWAWTextListener::PageBreak);
692
4
  }
693
2.62k
}
694
695
696
697
////////////////////////////////////////////////////////////
698
// the parser
699
////////////////////////////////////////////////////////////
700
void WriterPlsParser::parse(librevenge::RVNGTextInterface *docInterface)
701
8.88k
{
702
8.88k
  if (!getInput().get() || !checkHeader(nullptr))  throw(libmwaw::ParseException());
703
8.88k
  bool ok = true;
704
8.88k
  try {
705
    // create the asciiFile
706
8.88k
    ascii().setStream(getInput());
707
8.88k
    ascii().open(asciiName());
708
709
8.88k
    checkHeader(nullptr);
710
8.88k
    ok = createZones();
711
8.88k
    ascii().addPos(getInput()->tell());
712
8.88k
    ascii().addNote("_");
713
8.88k
    if (ok) {
714
8.40k
      createDocument(docInterface);
715
8.40k
      sendWindow(0);
716
8.40k
    }
717
718
8.88k
    ascii().reset();
719
8.88k
  }
720
8.88k
  catch (...) {
721
0
    MWAW_DEBUG_MSG(("WriterPlsParser::parse: exception catched when parsing\n"));
722
0
    ok = false;
723
0
  }
724
725
8.88k
  resetTextListener();
726
8.88k
  if (!ok) throw(libmwaw::ParseException());
727
8.88k
}
728
729
////////////////////////////////////////////////////////////
730
// create the document
731
////////////////////////////////////////////////////////////
732
void WriterPlsParser::createDocument(librevenge::RVNGTextInterface *documentInterface)
733
8.40k
{
734
8.40k
  if (!documentInterface) return;
735
8.40k
  if (getTextListener()) {
736
0
    MWAW_DEBUG_MSG(("WriterPlsParser::createDocument: listener already exist\n"));
737
0
    return;
738
0
  }
739
740
  // update the page
741
8.40k
  m_state->m_actPage = 0;
742
743
  // create the page list
744
8.40k
  MWAWPageSpan ps(getPageSpan());
745
25.2k
  for (int i = 1; i < 3; i++) {
746
16.8k
    if (m_state->m_windows[i].m_paragraphs.size() == 0)
747
16.7k
      continue;
748
108
    MWAWHeaderFooter hF((i==1) ? MWAWHeaderFooter::HEADER : MWAWHeaderFooter::FOOTER, MWAWHeaderFooter::ALL);
749
108
    hF.m_subDocument.reset(new WriterPlsParserInternal::SubDocument(*this, getInput(), i));
750
108
    ps.setHeaderFooter(hF);
751
108
  }
752
753
8.40k
  m_state->m_numPages = int(m_state->m_windows[0].m_pages.size());
754
8.40k
  ps.setPageSpan(m_state->m_numPages+1);
755
8.40k
  std::vector<MWAWPageSpan> pageList(1,ps);
756
  //
757
8.40k
  MWAWTextListenerPtr listen(new MWAWTextListener(*getParserState(), pageList, documentInterface));
758
8.40k
  setTextListener(listen);
759
8.40k
  listen->startDocument();
760
8.40k
}
761
762
763
////////////////////////////////////////////////////////////
764
//
765
// Intermediate level
766
//
767
////////////////////////////////////////////////////////////
768
bool WriterPlsParser::createZones()
769
8.88k
{
770
8.88k
  if (!readWindowsInfo(0) || !readPrintInfo())
771
0
    return false;
772
26.1k
  for (int st = 1; st < 4; st++) {
773
25.8k
    bool ok = true;
774
25.8k
    switch (st) {
775
8.88k
    case 1:
776
8.88k
      ok = m_state->m_headerHeight > 0;
777
8.88k
      break;
778
8.60k
    case 2:
779
8.60k
      ok = m_state->m_footerHeight > 0;
780
8.60k
      break;
781
8.40k
    default:
782
8.40k
      break;
783
25.8k
    }
784
25.8k
    if (!ok) continue;
785
9.38k
    if (st !=3 && !readWindowsInfo(st))
786
269
      return false;
787
9.11k
    if (!readWindowsZone(st==3 ? 0 : st))
788
8.37k
      return (st==3);
789
9.11k
  }
790
791
234
  return true;
792
8.88k
}
793
794
////////////////////////////////////////////////////////////
795
//
796
// Low level
797
//
798
////////////////////////////////////////////////////////////
799
800
////////////////////////////////////////////////////////////
801
// read the header
802
////////////////////////////////////////////////////////////
803
bool WriterPlsParser::checkHeader(MWAWHeader *header, bool /*strict*/)
804
31.4k
{
805
31.4k
  *m_state = WriterPlsParserInternal::State();
806
807
31.4k
  MWAWInputStreamPtr input = getInput();
808
31.4k
  if (!input || !input->hasDataFork())
809
0
    return false;
810
811
31.4k
  int const headerSize=2;
812
31.4k
  if (!input->checkPosition(headerSize)) {
813
0
    MWAW_DEBUG_MSG(("WriterPlsParser::checkHeader: file is too short\n"));
814
0
    return false;
815
0
  }
816
31.4k
  input->seek(0,librevenge::RVNG_SEEK_SET);
817
31.4k
  if (input->readULong(2) != 0x110)
818
170
    return false;
819
31.2k
  ascii().addPos(0);
820
31.2k
  ascii().addNote("FileHeader");
821
822
31.2k
  if (!readWindowsInfo(0) || !readPrintInfo())
823
3.02k
    return false;
824
28.2k
  input->seek(2,librevenge::RVNG_SEEK_SET);
825
28.2k
  if (header)
826
10.4k
    header->reset(MWAWDocument::MWAW_T_WRITERPLUS, 1);
827
828
28.2k
  return true;
829
31.2k
}
830
831
bool WriterPlsParser::readWindowsInfo(int zone)
832
41.1k
{
833
41.1k
  if (zone<0 || zone>=3) {
834
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readWindowsInfo:the zone seems bad\n"));
835
0
    return false;
836
0
  }
837
41.1k
  MWAWInputStreamPtr input = getInput();
838
839
41.1k
  long debPos = input->tell();
840
41.1k
  if (!input->checkPosition(debPos+0xf4)) {
841
762
    MWAW_DEBUG_MSG(("WriterPlsParser::readWindowsInfo: file is too short\n"));
842
762
    return false;
843
762
  }
844
845
40.3k
  WriterPlsParserInternal::WindowsInfo info;
846
40.3k
  libmwaw::DebugStream f;
847
40.3k
  f << "Entries(WindowsZone)";
848
40.3k
  switch (zone) {
849
39.6k
  case 0:
850
39.6k
    break;
851
443
  case 1:
852
443
    f << "[Header]";
853
443
    break;
854
302
  case 2:
855
302
    f << "[Footer]";
856
302
    break;
857
0
  default:
858
0
    f << "[Unknown]";
859
0
    break;
860
40.3k
  }
861
40.3k
  f << ":";
862
121k
  for (int i = 0; i < 2; i++) {
863
80.6k
    auto val = static_cast<int>(input->readLong(1));
864
80.6k
    f << "f" << i << "=" << val << ",";
865
80.6k
  }
866
40.3k
  f << "unkn=" << input->readLong(2);
867
868
40.3k
  long pos;
869
282k
  for (auto &i : info.m_zone) {
870
282k
    WriterPlsParserInternal::WindowsInfo::Zone infoZone;
871
282k
    infoZone.m_unknown[0] = static_cast<int>(input->readULong(1));
872
282k
    infoZone.m_width = static_cast<int>(input->readULong(2));
873
282k
    infoZone.m_unknown[1] = static_cast<int>(input->readULong(1));
874
282k
    infoZone.m_unknown[2] = static_cast<int>(input->readULong(2));
875
282k
    infoZone.m_size = static_cast<int>(input->readULong(2));
876
282k
    infoZone.m_number = static_cast<int>(input->readULong(2));
877
282k
    i = infoZone;
878
282k
  }
879
40.3k
  f << "," << info;
880
881
40.3k
  ascii().addPos(debPos);
882
40.3k
  ascii().addNote(f.str().c_str());
883
884
40.3k
  pos = input->tell();
885
40.3k
  ascii().addPos(pos);
886
40.3k
  ascii().addNote("WindowsZone(A-1)");
887
40.3k
  ascii().addPos(pos+12);
888
40.3k
  ascii().addNote("WindowsZone(A-2)");
889
40.3k
  ascii().addPos(pos+30);
890
40.3k
  ascii().addNote("WindowsZone(A-3)");
891
40.3k
  ascii().addPos(pos+60);
892
40.3k
  ascii().addNote("WindowsZone(A-4)");
893
40.3k
  ascii().addPos(pos+60+14);
894
40.3k
  ascii().addNote("WindowsZone(A-5)");
895
40.3k
  ascii().addPos(pos+60+14*2);
896
40.3k
  ascii().addNote("WindowsZone(A-6)");
897
898
40.3k
  pos = debPos+0xc2;
899
40.3k
  input->seek(pos, librevenge::RVNG_SEEK_SET);
900
40.3k
  f.str("");
901
40.3k
  f << "WindowsZone(A-7):";
902
40.3k
  auto val = static_cast<int>(input->readLong(2));
903
40.3k
  if (val) f << "unkn=" << val << ",";
904
40.3k
  auto width = static_cast<int>(input->readLong(2));
905
40.3k
  info.m_footerY = static_cast<int>(input->readLong(2));
906
40.3k
  info.m_headerY = static_cast<int>(input->readLong(2));
907
40.3k
  auto height = static_cast<int>(input->readLong(2));
908
40.3k
  info.m_pageDim = MWAWVec2i(width, height);
909
40.3k
  f << "page=" << info.m_pageDim << ",";
910
40.3k
  if (info.m_headerY)
911
4.22k
    f << "header[height]=" << info.m_headerY << ",";
912
40.3k
  if (info.m_footerY)
913
4.30k
    f << "footer[height]=" << info.m_footerY << ",";
914
161k
  for (int i = 0; i < 3; i++) // always 17 12 0 left|right ?
915
121k
    f << "f" << i << "=" << static_cast<int>(input->readLong(2)) << ",";
916
40.3k
  ascii().addPos(pos);
917
40.3k
  ascii().addNote(f.str().c_str());
918
40.3k
  if (info.dimensionInvalid())
919
1.70k
    return false;
920
38.6k
  if (zone == 0) {
921
37.9k
    m_state->m_headerHeight = info.m_headerY;
922
37.9k
    m_state->m_footerHeight = info.m_footerY;
923
37.9k
  }
924
38.6k
  pos = input->tell();
925
38.6k
  f.str("");
926
38.6k
  f << "WindowsZone(B):";
927
38.6k
  int dim[4];
928
154k
  for (auto &d : dim) d = static_cast<int>(input->readLong(2));
929
38.6k
  f << "dim(?)=" << dim[1] << "x" << dim[0] << "-" << dim[3] << "x" << dim[2] << ",";
930
115k
  for (int i = 0; i < 2; i++) {
931
77.2k
    auto fl = static_cast<int>(input->readLong(1)); // almost always 0 except some time 1
932
77.2k
    if (fl) f << "fl" << i << "=" << fl << ",";
933
77.2k
  }
934
270k
  for (int i = 0; i < 6; i++) {
935
231k
    int values[3];
936
231k
    values[0] = static_cast<int>(input->readULong(1));
937
231k
    values[1] = static_cast<int>(input->readLong(2));
938
231k
    values[2] = static_cast<int>(input->readULong(1));
939
231k
    if (values[0] == 0 && values[1] == 0 && values[2] == 0) continue;
940
158k
    f << "f" << i << "=[" << values[0] << ", w=" << values[1]
941
158k
      << ", " << std::hex << values[2] << std::dec << "],";
942
158k
  }
943
944
38.6k
  m_state->m_windows[zone]=info;
945
946
38.6k
  ascii().addPos(pos);
947
38.6k
  ascii().addNote(f.str().c_str());
948
949
38.6k
  return true;
950
40.3k
}
951
952
////////////////////////////////////////////////////////////
953
// read all the windows zone info
954
////////////////////////////////////////////////////////////
955
bool WriterPlsParser::readWindowsZone(int zone)
956
9.11k
{
957
9.11k
  if (zone<0 || zone>=3) {
958
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readWindowsZone:the zone seems bad\n"));
959
0
    return false;
960
0
  }
961
962
9.11k
  MWAWInputStreamPtr input = getInput();
963
9.11k
  auto &wInfo = m_state->m_windows[zone];
964
965
9.11k
  libmwaw::DebugStream f;
966
31.9k
  for (int wh=1; wh < 7; wh++) {
967
30.7k
    auto const &z = wInfo.m_zone[wh];
968
30.7k
    int length = z.m_size;
969
30.7k
    if (!length) continue;
970
971
16.3k
    long pos = input->tell();
972
16.3k
    input->seek(length, librevenge::RVNG_SEEK_CUR);
973
16.3k
    if (long(input->tell()) != pos+length) {
974
1.00k
      MWAW_DEBUG_MSG(("WriterPlsParser::readWindowsZone: zone is too short\n"));
975
1.00k
      return false;
976
1.00k
    }
977
15.3k
    input->seek(pos, librevenge::RVNG_SEEK_SET);
978
979
15.3k
    bool ok = false;
980
15.3k
    switch (wh) {
981
5.51k
    case 1:
982
5.51k
      ok=readPageInfo(zone);
983
5.51k
      break;
984
1.17k
    case 2:
985
1.17k
      ok=readColInfo(zone);
986
1.17k
      break;
987
7.85k
    case 3:
988
      // need to get next block
989
7.85k
      ok = readParagraphInfo(zone);
990
7.85k
      if (!ok) return false;
991
896
      break;
992
896
    default:
993
775
      break;
994
15.3k
    }
995
8.35k
    if (ok) continue;
996
997
4.21k
    input->seek(pos, librevenge::RVNG_SEEK_SET);
998
4.21k
    if (z.m_number && (length % z.m_number) == 0) {
999
2.71k
      int dataSz = length / z.m_number;
1000
498k
      for (int i = 0; i < z.m_number; i++) {
1001
495k
        f.str("");
1002
495k
        f << "Entries(Zone" << wh << ")-" << i << ":";
1003
495k
        ascii().addPos(input->tell());
1004
495k
        ascii().addNote(f.str().c_str());
1005
495k
        input->seek(dataSz, librevenge::RVNG_SEEK_CUR);
1006
495k
      }
1007
2.71k
    }
1008
1.50k
    else {
1009
1.50k
      f.str("");
1010
1.50k
      f << "Entries(Zone" << wh << "):";
1011
1.50k
      ascii().addPos(input->tell());
1012
1.50k
      ascii().addNote(f.str().c_str());
1013
1.50k
      input->seek(length, librevenge::RVNG_SEEK_CUR);
1014
1.50k
    }
1015
4.21k
  }
1016
1017
1.41k
  for (int i = int(wInfo.m_paragraphs.size())-1; i >= 0; i--) {
1018
828
    auto const &pInfo = wInfo.m_paragraphs[size_t(i)];
1019
828
    if (!pInfo.m_pos)  continue;
1020
1021
573
    input->seek(pInfo.m_pos, librevenge::RVNG_SEEK_SET);
1022
573
    auto length = long(input->readULong(2));
1023
573
    auto length2 = long(input->readULong(2));
1024
573
    long endPos = pInfo.m_pos+4+length+length2;
1025
573
    input->seek(endPos, librevenge::RVNG_SEEK_SET);
1026
573
    if (long(input->tell()) != endPos) {
1027
391
      MWAW_DEBUG_MSG(("WriterPlsParser::readWindowsZone: data zone is too short\n"));
1028
391
      return false;
1029
391
    }
1030
182
    switch (pInfo.getType()) {
1031
48
    case 4:
1032
48
      length = long(input->readULong(4));
1033
48
      input->seek(length, librevenge::RVNG_SEEK_CUR);
1034
48
      if (long(input->tell()) != endPos+length+4) {
1035
25
        MWAW_DEBUG_MSG(("WriterPlsParser::readWindowsZone: graphics zone is too short\n"));
1036
25
        return false;
1037
25
      }
1038
23
      break;
1039
134
    default: // 0,1,2,3, 5 : ok, other ?
1040
134
      break;
1041
182
    }
1042
157
    return true;
1043
182
  }
1044
583
  return true;
1045
1.15k
}
1046
1047
////////////////////////////////////////////////////////////
1048
// send the windows zone info
1049
////////////////////////////////////////////////////////////
1050
bool WriterPlsParser::sendWindow(int zone, MWAWVec2i limits)
1051
245k
{
1052
245k
  MWAWTextListenerPtr listener=getTextListener();
1053
245k
  if (!listener) {
1054
0
    MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: can not find a listener\n"));
1055
0
    return false;
1056
0
  }
1057
245k
  if (zone<0 || zone>=3) {
1058
0
    MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow:the zone seems bad\n"));
1059
0
    return false;
1060
0
  }
1061
245k
  auto &wInfo = m_state->m_windows[zone];
1062
1063
245k
  bool sendAll = limits[0] < 0;
1064
1065
245k
  auto maxPages = int(wInfo.m_pages.size());
1066
245k
  if (maxPages == 0 || zone || !sendAll) maxPages = 1;
1067
1068
245k
  int actParag = 0;
1069
245k
  int actCol = 0, numCols = 0;
1070
459k
  for (int pg = 0; pg < maxPages; pg++) {
1071
245k
    int endParag = 0;
1072
245k
    if (!sendAll) {
1073
237k
      actParag = limits[0];
1074
237k
      endParag = limits[1];
1075
237k
      if (endParag <= actParag) {
1076
31.9k
        MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: pb2 with limits\n"));
1077
31.9k
        return true;
1078
31.9k
      }
1079
237k
    }
1080
8.51k
    else {
1081
8.51k
      if (zone == 0) {
1082
8.41k
        newPage(pg+1);
1083
8.41k
        actCol = numCols ? 1 : 0;
1084
8.41k
      }
1085
8.51k
      if (pg == maxPages-1 || wInfo.m_pages.size() == 0)
1086
8.51k
        endParag = int(wInfo.m_paragraphs.size());
1087
4
      else {
1088
4
        endParag = wInfo.m_pages[size_t(pg)+1].m_firstLine-1;
1089
4
        if (endParag == -1 || endParag < actParag) {
1090
0
          MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: pb with page zone\n"));
1091
0
          continue;
1092
0
        }
1093
4
      }
1094
8.51k
    }
1095
213k
    if (endParag > int(wInfo.m_paragraphs.size())) {
1096
98.7k
      MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: pb with limits\n"));
1097
98.7k
      endParag = int(wInfo.m_paragraphs.size());
1098
98.7k
    }
1099
1100
703k
    for (int i = actParag; i < endParag; i++) {
1101
489k
      auto const &pInfo = wInfo.m_paragraphs[size_t(i)];
1102
489k
      if (!pInfo.m_pos) {
1103
4.86k
        readText(pInfo);
1104
4.86k
        continue;
1105
4.86k
      }
1106
484k
      bool ok = true;
1107
484k
      switch (pInfo.getType()) {
1108
4.78k
      case 3: // col break: seems similar to an entry data (with a text zone which does not contain any character)
1109
4.78k
        if (numCols) {
1110
0
          if (actCol >numCols) {
1111
0
            MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: pb with col break\n"));
1112
0
          }
1113
0
          else {
1114
0
            actCol++;
1115
0
            listener->insertBreak(MWAWTextListener::ColumnBreak);
1116
0
          }
1117
0
        }
1118
4.78k
        MWAW_FALLTHROUGH;
1119
126k
      case 0:
1120
181k
      case 2:
1121
181k
        ok = readText(pInfo);
1122
181k
        break;
1123
32.9k
      case 1: {
1124
32.9k
        MWAWSection section;
1125
32.9k
        bool canCreateSection = sendAll && zone == 0 && actCol == numCols;
1126
32.9k
        if (findSection(zone, MWAWVec2i(i, endParag), section)) {
1127
32.8k
          if (!canCreateSection) {
1128
20.1k
            if (section.numColumns()>1) {
1129
0
              MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: find a section in auxilliary zone\n"));
1130
0
            }
1131
20.1k
          }
1132
12.6k
          else {
1133
12.6k
            if (listener->isSectionOpened())
1134
7.71k
              listener->closeSection();
1135
12.6k
            listener->openSection(section);
1136
12.6k
            numCols = listener->getSection().numColumns();
1137
12.6k
            if (numCols<=1) numCols=0;
1138
12.6k
            actCol = numCols ? 1 : 0;
1139
12.6k
            canCreateSection = false;
1140
12.6k
          }
1141
32.8k
        }
1142
1143
32.9k
        ok = readSection(pInfo, canCreateSection);
1144
32.9k
        break;
1145
126k
      }
1146
15.8k
      case 4:
1147
15.8k
        ok = readGraphic(pInfo);
1148
15.8k
        break;
1149
212k
      case 5:
1150
212k
        if (pInfo.m_numLines + i <= endParag) {
1151
200k
          if ((ok = readTable(pInfo))) {
1152
141k
            listener->openTableRow(float(pInfo.m_height), librevenge::RVNG_POINT);
1153
1154
379k
            for (size_t j = 0; j < pInfo.m_linesHeight.size(); j++) {
1155
237k
              int numData = pInfo.m_linesHeight[j];
1156
237k
              MWAWCell cell;
1157
237k
              cell.setPosition(MWAWVec2i(int(j), 0));
1158
237k
              listener->openTableCell(cell);
1159
237k
              sendWindow(zone, MWAWVec2i(i+1, i+1+numData));
1160
237k
              i += numData;
1161
237k
              listener->closeTableCell();
1162
237k
            }
1163
1164
141k
            listener->closeTableRow();
1165
141k
            listener->closeTable();
1166
141k
          }
1167
200k
        }
1168
11.4k
        else {
1169
11.4k
          MWAW_DEBUG_MSG(("WriterPlsParser::sendWindow: table across a page\n"));
1170
11.4k
        }
1171
212k
        break;
1172
41.7k
      default:
1173
41.7k
        ok = readUnknown(pInfo);
1174
41.7k
        break;
1175
484k
      }
1176
484k
      if (!ok) {
1177
215k
        libmwaw::DebugStream f;
1178
215k
        f << "Entries(Unknown):" << pInfo;
1179
215k
        ascii().addPos(pInfo.m_pos);
1180
215k
        ascii().addNote(f.str().c_str());
1181
215k
      }
1182
484k
    }
1183
213k
    actParag = endParag;
1184
213k
  }
1185
213k
  return true;
1186
245k
}
1187
1188
1189
/*
1190
 * find the column size which correspond to a limit
1191
 *
1192
 * Note: complex because we need to read the file in order to find the limit
1193
 */
1194
bool WriterPlsParser::findSection(int zone, MWAWVec2i limits, MWAWSection &sec)
1195
32.9k
{
1196
32.9k
  if (zone<0 || zone>=3) {
1197
0
    MWAW_DEBUG_MSG(("WriterPlsParser::findSection:the zone seems bad\n"));
1198
0
    return false;
1199
0
  }
1200
32.9k
  auto &wInfo = m_state->m_windows[zone];
1201
1202
32.9k
  sec=MWAWSection();
1203
32.9k
  std::vector<int> listPos;
1204
32.9k
  if (!wInfo.getColumnLimitsFor(limits[0], listPos))
1205
187
    return false;
1206
1207
32.8k
  size_t numPos = listPos.size();
1208
32.8k
  if (!numPos)
1209
32.8k
    return true;
1210
1
  if (listPos[numPos-1] >= limits[1]) {
1211
0
    MWAW_DEBUG_MSG(("WriterPlsParser::findSection: columns across a page\n"));
1212
0
    return false;
1213
0
  }
1214
1215
1
  MWAWInputStreamPtr input = getInput();
1216
1
  int totalSize = 0;
1217
1
  for (auto &line : listPos) {
1218
1
    long pos = wInfo.m_paragraphs[size_t(line)].m_pos;
1219
1
    if (!pos) {
1220
0
      MWAW_DEBUG_MSG(("WriterPlsParser::findSection: bad data pos\n"));
1221
0
      return false;
1222
0
    }
1223
1
    input->seek(pos, librevenge::RVNG_SEEK_SET);
1224
1
    if (input->readLong(2)) {
1225
0
      MWAW_DEBUG_MSG(("WriterPlsParser::findSection: find a text size\n"));
1226
0
      return false;
1227
0
    }
1228
1
    input->seek(8, librevenge::RVNG_SEEK_CUR); // sz2 and type, h, indent
1229
1
    auto val = static_cast<int>(input->readLong(2));
1230
1
    if (val <= 0 || long(input->tell()) != pos + 12) {
1231
1
      MWAW_DEBUG_MSG(("WriterPlsParser::findSection: file is too short\n"));
1232
1
      return false;
1233
1
    }
1234
0
    totalSize += val;
1235
0
    MWAWSection::Column col;
1236
0
    col.m_width=val;
1237
0
    col.m_widthUnit=librevenge::RVNG_POINT;
1238
0
    sec.m_columns.push_back(col);
1239
0
  }
1240
0
  if (sec.m_columns.size()==1)
1241
0
    sec.m_columns.resize(0);
1242
0
  if (totalSize >= int(72.*getPageWidth())) {
1243
0
    MWAW_DEBUG_MSG(("WriterPlsParser::findSection: total size is too big\n"));
1244
0
    return false;
1245
0
  }
1246
0
  return true;
1247
0
}
1248
1249
////////////////////////////////////////////////////////////
1250
// read all the windows zone info
1251
////////////////////////////////////////////////////////////
1252
bool WriterPlsParser::readPageInfo(int zone)
1253
5.51k
{
1254
5.51k
  if (zone<0 || zone>=3) {
1255
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readPageInfo:the zone seems bad\n"));
1256
0
    return false;
1257
0
  }
1258
1259
5.51k
  MWAWInputStreamPtr input = getInput();
1260
5.51k
  libmwaw::DebugStream f;
1261
1262
5.51k
  auto &wInfo = m_state->m_windows[zone];
1263
5.51k
  int numPages = wInfo.m_zone[1].m_number;
1264
5.51k
  if (wInfo.m_zone[1].m_size != numPages * 10) {
1265
848
    MWAW_DEBUG_MSG(("WriterPlsParser::readPageInfo: odd page size\n"));
1266
848
    return false;
1267
848
  }
1268
1269
4.66k
  int actNumLine = 0;
1270
4.66k
  auto maxHeight = int(72.*getTextHeight()+20.);
1271
4.66k
  if (maxHeight < 1000) maxHeight = 1000;
1272
4.66k
  int prevTotalHeight = 0;
1273
1274
7.50k
  for (int page = 0; page < numPages; page++) {
1275
4.87k
    long pos = input->tell();
1276
4.87k
    WriterPlsParserInternal::PageInfo pInfo;
1277
4.87k
    pInfo.m_firstLine = static_cast<int>(input->readLong(2));
1278
4.87k
    if ((page == 0 && pInfo.m_firstLine != 1) || pInfo.m_firstLine < actNumLine)
1279
1.32k
      return false;
1280
3.55k
    actNumLine=pInfo.m_firstLine;
1281
7.11k
    for (auto &unkn : pInfo.m_unknown) unkn = static_cast<int>(input->readLong(2));
1282
3.55k
    pInfo.m_heightFromBegin = static_cast<int>(input->readULong(2));
1283
3.55k
    if (pInfo.m_heightFromBegin < prevTotalHeight) return false;
1284
3.52k
    prevTotalHeight = pInfo.m_heightFromBegin;
1285
3.52k
    pInfo.m_height = static_cast<int>(input->readULong(2));
1286
3.52k
    if (pInfo.m_height > maxHeight) return false;
1287
1288
2.83k
    wInfo.m_pages.push_back(pInfo);
1289
2.83k
    f.str("");
1290
2.83k
    f << "Entries(PageInfo)-"<< page+1 << ":" << pInfo;
1291
1292
2.83k
    ascii().addPos(pos);
1293
2.83k
    ascii().addNote(f.str().c_str());
1294
2.83k
  }
1295
1296
2.62k
  return true;
1297
4.66k
}
1298
1299
////////////////////////////////////////////////////////////
1300
// read a windows paragraph info
1301
////////////////////////////////////////////////////////////
1302
MWAWParagraph WriterPlsParser::getParagraph(WriterPlsParserInternal::ParagraphData const &data)
1303
79.9k
{
1304
79.9k
  MWAWParagraph para;
1305
1306
79.9k
  para.m_marginsUnit=librevenge::RVNG_POINT;
1307
  // decrease a little left indent to avoid some page width pb
1308
79.9k
  double left=double(data.m_indent[0])-20.-72.*getPageSpan().getMarginLeft();
1309
79.9k
  if (left > 0)
1310
19.5k
    para.m_margins[1]=left;
1311
79.9k
  para.m_margins[0]=double(data.m_indent[1]-data.m_indent[0]);
1312
79.9k
  if (getTextListener() && getTextListener()->getSection().numColumns() > 1)
1313
0
    return para; // too dangerous to set the paragraph width in this case...
1314
79.9k
  double right=getPageWidth()*72.-double(data.m_width);
1315
79.9k
  if (right > 0)
1316
67.7k
    para.m_margins[2]=right;
1317
79.9k
  return para;
1318
79.9k
}
1319
1320
bool WriterPlsParser::readParagraphInfo(int zone)
1321
7.85k
{
1322
7.85k
  if (zone<0 || zone>=3) {
1323
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readParagraphInfo:the zone seems bad\n"));
1324
0
    return false;
1325
0
  }
1326
1327
7.85k
  libmwaw::DebugStream f;
1328
1329
7.85k
  MWAWInputStreamPtr input = getInput();
1330
7.85k
  WriterPlsParserInternal::WindowsInfo &wInfo = m_state->m_windows[zone];
1331
7.85k
  int numPara = wInfo.m_zone[3].m_number;
1332
7.85k
  long endPos = long(input->tell()) + wInfo.m_zone[3].m_size;
1333
1334
7.85k
  int para = 0;
1335
178k
  while (para <= numPara) {
1336
177k
    long pos = input->tell();
1337
177k
    if (pos == endPos) break;
1338
177k
    if (pos > endPos) return false;
1339
174k
    WriterPlsParserInternal::ParagraphInfo pInfo;
1340
1341
174k
    f.str("");
1342
174k
    f << "Entries(ParaInfo)-"<< para+1 << ":";
1343
174k
    auto wh = static_cast<int>(input->readLong(1));
1344
174k
    if ((wh%2) == 0) {
1345
46.9k
      if (wh < 4) return false;
1346
180k
      for (int i = 0; i < (wh-4)/2; i++)
1347
137k
        pInfo.m_unknowns.push_back(static_cast<int>(input->readULong(2)));
1348
43.1k
      pInfo.m_type = -1;
1349
43.1k
      pInfo.m_numLines = static_cast<int>(input->readULong(1)); // probably numLine
1350
43.1k
      pInfo.m_height = static_cast<int>(input->readULong(2));
1351
43.1k
      f << pInfo;
1352
43.1k
      ascii().addPos(pos);
1353
43.1k
      ascii().addNote(f.str().c_str());
1354
43.1k
      continue;
1355
46.9k
    }
1356
127k
    para++;
1357
127k
    pInfo.m_flags[0] = (wh>>1);
1358
127k
    pInfo.m_flags[1] = static_cast<int>(input->readULong(1)); // almost always 0
1359
127k
    pInfo.m_type = static_cast<int>(input->readULong(1));
1360
127k
    pInfo.m_numLines = static_cast<int>(input->readULong(1)); // or numColumns if type==5
1361
127k
    pInfo.m_height = static_cast<int>(input->readULong(2));
1362
127k
    pInfo.m_pos = long(input->readULong(4));
1363
127k
    pInfo.m_flags[2] = static_cast<int>(input->readULong(1)); // almost always 0
1364
127k
    pInfo.m_width = static_cast<int>(input->readULong(2));
1365
382k
    for (int i = 3; i < 5; i++)
1366
254k
      pInfo.m_flags[i] = static_cast<int>(input->readULong(1));
1367
127k
    if (pInfo.m_numLines!=1) {
1368
1.01M
      for (int i = 0; i < pInfo.m_numLines; i++)
1369
958k
        pInfo.m_linesHeight.push_back(static_cast<int>(input->readULong(1)));
1370
61.0k
    }
1371
127k
    pInfo.m_height2 = static_cast<int>(input->readULong(1));
1372
127k
    wInfo.m_paragraphs.push_back(pInfo);
1373
127k
    f << pInfo;
1374
1375
127k
    ascii().addPos(pos);
1376
127k
    ascii().addNote(f.str().c_str());
1377
1378
127k
  }
1379
1380
896
  return true;
1381
7.85k
}
1382
1383
////////////////////////////////////////////////////////////
1384
// read all the windows col info ?
1385
////////////////////////////////////////////////////////////
1386
bool WriterPlsParser::readColInfo(int zone)
1387
1.17k
{
1388
1.17k
  if (zone<0 || zone>=3) {
1389
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readColInfo:the zone seems bad\n"));
1390
0
    return false;
1391
0
  }
1392
1393
1.17k
  libmwaw::DebugStream f;
1394
1395
1.17k
  WriterPlsParserInternal::WindowsInfo &wInfo = m_state->m_windows[zone];
1396
1.17k
  int numCols = wInfo.m_zone[2].m_number;
1397
1.17k
  if (wInfo.m_zone[2].m_size != numCols * 16) {
1398
555
    MWAW_DEBUG_MSG(("WriterPlsParser::readColInfo: odd col size\n"));
1399
555
    return false;
1400
555
  }
1401
1402
615
  MWAWInputStreamPtr input = getInput();
1403
5.53k
  for (int col = 0; col < numCols; col++) {
1404
4.92k
    long pos = input->tell();
1405
4.92k
    WriterPlsParserInternal::ColumnInfo cInfo;
1406
4.92k
    cInfo.m_col = static_cast<int>(input->readLong(2));
1407
4.92k
    cInfo.m_unknown[0] = static_cast<int>(input->readLong(2));
1408
4.92k
    cInfo.m_numCol = static_cast<int>(input->readLong(2));
1409
4.92k
    cInfo.m_firstLine = static_cast<int>(input->readLong(2));
1410
19.6k
    for (int i = 1; i < 4; i++)
1411
14.7k
      cInfo.m_unknown[i] = static_cast<int>(input->readLong(2));
1412
4.92k
    cInfo.m_height = static_cast<int>(input->readLong(2));
1413
4.92k
    wInfo.m_columns.push_back(cInfo);
1414
1415
4.92k
    f.str("");
1416
4.92k
    f << "Entries(ColInfo):" << cInfo;
1417
4.92k
    ascii().addPos(pos);
1418
4.92k
    ascii().addNote(f.str().c_str());
1419
4.92k
  }
1420
1421
615
  return true;
1422
1.17k
}
1423
1424
bool WriterPlsParser::readText(WriterPlsParserInternal::ParagraphInfo const &info)
1425
186k
{
1426
186k
  WriterPlsParserInternal::ParagraphData data;
1427
186k
  std::vector<WriterPlsParserInternal::Line> lines;
1428
186k
  if (!info.m_pos) {
1429
4.86k
    MWAW_DEBUG_MSG(("WriterPlsParser::readText: pb with pos\n"));
1430
4.86k
    return false;
1431
4.86k
  }
1432
1433
181k
  if (!readParagraphData(info, true, data))
1434
101k
    return false;
1435
1436
79.9k
  libmwaw::DebugStream f;
1437
1438
79.9k
  MWAWInputStreamPtr input = getInput();
1439
79.9k
  long pos = input->tell();
1440
79.9k
  f.str("");
1441
79.9k
  f << "Paragraph" << data.m_type << "(II):";
1442
1443
79.9k
  int numLines = data.m_numData[1];
1444
79.9k
  if (!readLines(info, numLines, lines)) {
1445
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readText: pb with the lines\n"));
1446
0
    lines.resize(0);
1447
0
    input->seek(pos+numLines*16, librevenge::RVNG_SEEK_SET);
1448
0
    f << "###lines,";
1449
0
  }
1450
174M
  for (int i = 0; i < numLines; i++)
1451
174M
    f << "line" << i << "=[" << lines[size_t(i)] << "],";
1452
1453
79.9k
  if (long(input->tell()) != data.m_endPos) {
1454
75.2k
    ascii().addDelimiter(input->tell(), '|');
1455
75.2k
    input->seek(data.m_endPos, librevenge::RVNG_SEEK_SET);
1456
75.2k
    f << "#endPos,";
1457
75.2k
  }
1458
1459
79.9k
  ascii().addPos(pos);
1460
79.9k
  ascii().addNote(f.str().c_str());
1461
1462
79.9k
  ascii().addPos(input->tell());
1463
79.9k
  ascii().addNote("_");
1464
1465
79.9k
  if (!getTextListener())
1466
0
    return true;
1467
79.9k
  std::string const &text = data.m_text;
1468
79.9k
  auto const &fonts = data.m_fonts;
1469
79.9k
  auto numChars = long(text.length());
1470
79.9k
  size_t actFont = 0, numFonts = fonts.size();
1471
79.9k
  int actLine = 0;
1472
79.9k
  numLines=int(lines.size());
1473
79.9k
  MWAWParagraph para=getParagraph(data);
1474
1475
79.9k
  if (numLines == 0 && info.m_height > 0) {
1476
52.3k
    para.setInterline(info.m_height, librevenge::RVNG_POINT);
1477
52.3k
    getTextListener()->setParagraph(para);
1478
52.3k
  }
1479
3.25M
  for (long c = 0; c < numChars; c++) {
1480
3.17M
    if (actFont < numFonts && c ==  fonts[actFont].m_firstChar)
1481
6.90k
      getTextListener()->setFont(fonts[actFont++].m_font);
1482
3.17M
    if (actLine < numLines && c == lines[size_t(actLine)].m_firstChar) {
1483
6.84k
      if (actLine) getTextListener()->insertEOL();
1484
6.84k
      if (numLines == 1 && info.m_height > lines[0].m_height) {
1485
301
        para.setInterline(info.m_height, librevenge::RVNG_POINT);
1486
301
        getTextListener()->setParagraph(para);
1487
301
      }
1488
6.54k
      else if (lines[size_t(actLine)].m_height) {
1489
4.70k
        para.setInterline(lines[size_t(actLine)].m_height, librevenge::RVNG_POINT);
1490
4.70k
        getTextListener()->setParagraph(para);
1491
4.70k
      }
1492
6.84k
      actLine++;
1493
6.84k
    }
1494
1495
3.17M
    auto ch = static_cast<unsigned char>(text[size_t(c)]);
1496
3.17M
    if (ch == 0x9)
1497
18.9k
      getTextListener()->insertTab();
1498
3.15M
    else
1499
3.15M
      getTextListener()->insertCharacter(ch);
1500
3.17M
  }
1501
79.9k
  if (info.getType() != 3)
1502
77.2k
    getTextListener()->insertEOL();
1503
1504
79.9k
  return true;
1505
79.9k
}
1506
1507
bool WriterPlsParser::readSection(WriterPlsParserInternal::ParagraphInfo const &info, bool mainBlock)
1508
32.9k
{
1509
32.9k
  WriterPlsParserInternal::ParagraphData data;
1510
1511
32.9k
  if (!info.m_pos) {
1512
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readSection: can not find the beginning pos\n"));
1513
0
    return false;
1514
0
  }
1515
1516
32.9k
  if (!readParagraphData(info, true, data))
1517
24.9k
    return false;
1518
1519
8.03k
  libmwaw::DebugStream f;
1520
1521
8.03k
  MWAWInputStreamPtr input = getInput();
1522
8.03k
  long pos = input->tell();
1523
8.03k
  f.str("");
1524
8.03k
  f << "Paragraph" << data.m_type << "(II):";
1525
1526
8.03k
  int numData = data.m_numData[1];
1527
8.03k
  if (numData != 1) {
1528
7.43k
    MWAW_DEBUG_MSG(("WriterPlsParser::readSection: unexpected num of data: %d \n", numData));
1529
7.43k
  }
1530
1531
8.03k
  std::vector<WriterPlsParserInternal::SectionInfo> sections;
1532
26.8M
  for (int i = 0; i < numData; i++) {
1533
26.8M
    WriterPlsParserInternal::SectionInfo section;
1534
80.4M
    for (int j = 0; j < 2; j++)
1535
53.6M
      section.m_flags[j] = static_cast<int>(input->readLong(2));
1536
26.8M
    section.m_numCol = static_cast<int>(input->readLong(2)); // checkme
1537
80.4M
    for (auto &dim : section.m_dim) dim = static_cast<int>(input->readLong(2));
1538
80.4M
    for (int j = 2; j < 4; j++)
1539
53.6M
      section.m_flags[j] = static_cast<int>(input->readLong(2));
1540
26.8M
    sections.push_back(section);
1541
26.8M
    if (!section.empty())
1542
71.5k
      f << "section" << i << "=[" << section << "],";
1543
26.8M
  }
1544
1545
8.03k
  if (long(input->tell()) != data.m_endPos) {
1546
7.42k
    ascii().addDelimiter(input->tell(), '|');
1547
7.42k
    input->seek(data.m_endPos, librevenge::RVNG_SEEK_SET);
1548
7.42k
    f << "#endPos,";
1549
7.42k
  }
1550
1551
8.03k
  if (getTextListener() && mainBlock) {
1552
20
    if (!getTextListener()->isSectionOpened())
1553
17
      getTextListener()->openSection(MWAWSection());
1554
20
  }
1555
8.03k
  ascii().addPos(pos);
1556
8.03k
  ascii().addNote(f.str().c_str());
1557
1558
8.03k
  ascii().addPos(input->tell());
1559
8.03k
  ascii().addNote("_");
1560
8.03k
  return true;
1561
32.9k
}
1562
1563
bool WriterPlsParser::readTable(WriterPlsParserInternal::ParagraphInfo const &info)
1564
200k
{
1565
200k
  WriterPlsParserInternal::ParagraphData data;
1566
1567
200k
  if (!info.m_pos) {
1568
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readTable: can not find the beginning pos\n"));
1569
0
    return false;
1570
0
  }
1571
1572
200k
  if (!readParagraphData(info, true, data))
1573
41.6k
    return false;
1574
1575
159k
  libmwaw::DebugStream f;
1576
1577
159k
  MWAWInputStreamPtr input = getInput();
1578
159k
  long pos = input->tell();
1579
159k
  f.str("");
1580
159k
  f << "Paragraph" << data.m_type << "(II):";
1581
1582
159k
  int numData = data.m_numData[1];
1583
159k
  if (numData <= 1) {
1584
134k
    MWAW_DEBUG_MSG(("WriterPlsParser::readTable: unexpected num of data: %d \n", numData));
1585
134k
  }
1586
159k
  if (numData < 0 || !input->checkPosition(pos+16*numData)) {
1587
17.1k
    MWAW_DEBUG_MSG(("WriterPlsParser::readTable: the columns do not fit in the file\n"));
1588
17.1k
    return false;
1589
17.1k
  }
1590
1591
141k
  std::vector<WriterPlsParserInternal::ColumnTableInfo> columns;
1592
416k
  for (int i = 0; i < numData; i++) {
1593
274k
    WriterPlsParserInternal::ColumnTableInfo cols;
1594
274k
    cols.m_height = static_cast<int>(input->readLong(2));
1595
549k
    for (auto &colX : cols.m_colX) colX = static_cast<int>(input->readLong(2));
1596
274k
    cols.m_numData = static_cast<int>(input->readLong(2));
1597
274k
    cols.m_flags  = static_cast<int>(input->readLong(2));
1598
824k
    for (auto &textX : cols.m_textX) textX = static_cast<int>(input->readLong(2));
1599
1600
274k
    columns.push_back(cols);
1601
274k
    f << "col" << i << "=[" << cols << "],";
1602
274k
  }
1603
1604
141k
  if (getTextListener()) {
1605
141k
    std::vector<float> colSize(static_cast<size_t>(numData));
1606
416k
    for (int i = 0; i < numData; i++) {
1607
274k
      auto const &cols = columns[size_t(i)];
1608
274k
      colSize[size_t(i)] = float(cols.m_colX[1]-cols.m_colX[0]);
1609
274k
    }
1610
141k
    MWAWTable table(MWAWTable::TableDimBit);
1611
141k
    table.setColsSize(colSize);
1612
    // use the same function than getParagraph to respect alignment
1613
141k
    int left=columns.empty() ? 0 : columns[0].m_colX[0]-20-int(72.*getPageSpan().getMarginLeft());
1614
141k
    if (left)
1615
14.1k
      table.setAlignment(MWAWTable::Left, float(left));
1616
141k
    getTextListener()->openTable(table);
1617
141k
  }
1618
1619
141k
  if (long(input->tell()) != data.m_endPos) {
1620
137k
    ascii().addDelimiter(input->tell(), '|');
1621
137k
    input->seek(data.m_endPos, librevenge::RVNG_SEEK_SET);
1622
137k
    f << "#endPos,";
1623
137k
  }
1624
1625
141k
  ascii().addPos(pos);
1626
141k
  ascii().addNote(f.str().c_str());
1627
1628
141k
  ascii().addPos(input->tell());
1629
141k
  ascii().addNote("_");
1630
1631
141k
  return true;
1632
159k
}
1633
1634
bool WriterPlsParser::readGraphic(WriterPlsParserInternal::ParagraphInfo const &info)
1635
15.8k
{
1636
15.8k
  WriterPlsParserInternal::ParagraphData data;
1637
1638
15.8k
  if (!info.m_pos) {
1639
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readGraphic: can not find the beginning pos\n"));
1640
0
    return false;
1641
0
  }
1642
1643
15.8k
  if (!readParagraphData(info, true, data))
1644
7.89k
    return false;
1645
1646
7.97k
  libmwaw::DebugStream f;
1647
1648
7.97k
  MWAWInputStreamPtr input = getInput();
1649
7.97k
  long pos = input->tell();
1650
7.97k
  f.str("");
1651
7.97k
  f << "Paragraph" << data.m_type << "(II):";
1652
1653
7.97k
  int numData = data.m_numData[1];
1654
7.97k
  if (numData != 1) {
1655
7.64k
    MWAW_DEBUG_MSG(("WriterPlsParser::readGraphic: unexpected num of data: %d \n", numData));
1656
7.64k
  }
1657
1658
7.97k
  std::vector<WriterPlsParserInternal::GraphicInfo> graphicsInfos;
1659
14.7M
  for (int i = 0; i < numData; i++) {
1660
14.6M
    WriterPlsParserInternal::GraphicInfo gInfo;
1661
14.6M
    gInfo.m_flags[0] = static_cast<int>(input->readLong(1));
1662
14.6M
    gInfo.m_width = static_cast<int>(input->readLong(2));
1663
14.6M
    gInfo.m_flags[1] = static_cast<int>(input->readULong(1)); //
1664
14.6M
    gInfo.m_graphicWidth = static_cast<int>(input->readLong(2)); // total width
1665
88.1M
    for (int j = 2; j < 7; j++)
1666
73.4M
      gInfo.m_flags[j] = static_cast<int>(input->readLong(2));
1667
14.6M
    f << "data" << i << "=[" << gInfo << "],";
1668
14.6M
    graphicsInfos.push_back(gInfo);
1669
14.6M
  }
1670
7.97k
  if (long(input->tell()) != data.m_endPos) {
1671
7.57k
    ascii().addDelimiter(input->tell(), '|');
1672
7.57k
    input->seek(data.m_endPos, librevenge::RVNG_SEEK_SET);
1673
7.57k
    f << "#endPos,";
1674
7.57k
  }
1675
1676
7.97k
  ascii().addPos(pos);
1677
7.97k
  ascii().addNote(f.str().c_str());
1678
1679
  // read the graphic:
1680
7.97k
  pos = input->tell();
1681
7.97k
  auto length = long(input->readULong(4));
1682
7.97k
  if (!length) {
1683
3.72k
    MWAW_DEBUG_MSG(("WriterPlsParser::readGraphic: find a zero size graphics\n"));
1684
3.72k
    ascii().addPos(pos);
1685
3.72k
    ascii().addNote("Entries(Graphic):#sz=0");
1686
3.72k
    return true;
1687
3.72k
  }
1688
4.25k
  long endPos = pos+4+length;
1689
4.25k
  input->seek(length, librevenge::RVNG_SEEK_CUR);
1690
4.25k
  if (long(input->tell()) != endPos) {
1691
2.50k
    MWAW_DEBUG_MSG(("WriterPlsParser::readGraphic: file is too short\n"));
1692
2.50k
    input->seek(pos, librevenge::RVNG_SEEK_SET);
1693
2.50k
    return false;
1694
2.50k
  }
1695
1696
1.75k
  f.str("");
1697
1.75k
  f << "Paragraph" << data.m_type << "(III):";
1698
1699
1.75k
  MWAWBox2f box;
1700
1.75k
  input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1701
1.75k
  auto res = MWAWPictData::check(input, static_cast<int>(length), box);
1702
1.75k
  if (res == MWAWPict::MWAW_R_BAD) {
1703
1.00k
    MWAW_DEBUG_MSG(("WriterPlsParser::readGraphic: can not find the picture\n"));
1704
1.00k
    input->seek(endPos, librevenge::RVNG_SEEK_SET);
1705
1.00k
    return false;
1706
1.00k
  }
1707
1708
745
  MWAWVec2f actualSize(0,0), naturalSize(actualSize);
1709
745
  if (box.size().x() > 0 && box.size().y()  > 0) {
1710
79
    if (actualSize.x() <= 0 || actualSize.y() <= 0) actualSize = box.size();
1711
79
    naturalSize = box.size();
1712
79
  }
1713
666
  else {
1714
666
    MWAW_DEBUG_MSG(("WriterPlsParser::readGraphic: can not find the picture size\n"));
1715
666
    actualSize = MWAWVec2f(100,100);
1716
666
  }
1717
1718
745
  MWAWPosition pictPos=MWAWPosition(MWAWVec2f(0,0),actualSize, librevenge::RVNG_POINT);
1719
745
  pictPos.setRelativePosition(MWAWPosition::Char);
1720
745
  pictPos.setNaturalSize(naturalSize);
1721
745
  f << pictPos;
1722
1723
  // get the picture
1724
745
  input->seek(pos+4, librevenge::RVNG_SEEK_SET);
1725
745
  std::shared_ptr<MWAWPict> pict(MWAWPictData::get(input, static_cast<int>(length)));
1726
745
  if (getTextListener()) {
1727
745
    auto para=getTextListener()->getParagraph();
1728
745
    para.setInterline(info.m_height, librevenge::RVNG_POINT);
1729
745
    getTextListener()->setParagraph(para);
1730
745
    MWAWEmbeddedObject picture;
1731
745
    if (pict && pict->getBinary(picture))
1732
738
      getTextListener()->insertPicture(pictPos, picture);
1733
745
    getTextListener()->insertEOL();
1734
745
    para.setInterline(1.0, librevenge::RVNG_PERCENT);
1735
745
    getTextListener()->setParagraph(para);
1736
745
  }
1737
745
  if (pict)
1738
738
    ascii().skipZone(pos+4, pos+4+length-1);
1739
1740
745
  input->seek(endPos, librevenge::RVNG_SEEK_SET);
1741
1742
745
  ascii().addPos(pos);
1743
745
  ascii().addNote(f.str().c_str());
1744
1745
745
  ascii().addPos(endPos);
1746
745
  ascii().addNote("_");
1747
1748
745
  return true;
1749
1.75k
}
1750
1751
////////////////////////////////////////////////////////////
1752
// read a paragraph
1753
////////////////////////////////////////////////////////////
1754
bool WriterPlsParser::readUnknown(WriterPlsParserInternal::ParagraphInfo const &info)
1755
41.7k
{
1756
41.7k
  WriterPlsParserInternal::ParagraphData data;
1757
41.7k
  if (!readParagraphData(info, true, data))
1758
18.8k
    return false;
1759
1760
22.8k
  libmwaw::DebugStream f;
1761
1762
22.8k
  MWAWInputStreamPtr input = getInput();
1763
22.8k
  long pos = input->tell();
1764
22.8k
  f.str("");
1765
22.8k
  f << "Paragraph" << data.m_type << "(II):";
1766
1767
22.8k
  int numData = data.m_numData[1];
1768
37.9M
  for (int i = 0; i < numData; i++) {
1769
37.9M
    f << "data" << i << "=[";
1770
341M
    for (int j = 0; j < 8; j++) {
1771
303M
      auto val = static_cast<int>(input->readLong(2));
1772
303M
      if (!val) f << "_,";
1773
662k
      else f << val << ",";
1774
303M
    }
1775
37.9M
    f << "],";
1776
37.9M
  }
1777
22.8k
  if (long(input->tell()) != data.m_endPos) {
1778
22.2k
    ascii().addDelimiter(input->tell(), '|');
1779
22.2k
    input->seek(data.m_endPos, librevenge::RVNG_SEEK_SET);
1780
22.2k
    f << "#";
1781
22.2k
  }
1782
1783
22.8k
  ascii().addPos(pos);
1784
22.8k
  ascii().addNote(f.str().c_str());
1785
1786
22.8k
  ascii().addPos(input->tell());
1787
22.8k
  ascii().addNote("_");
1788
1789
22.8k
  return true;
1790
41.7k
}
1791
1792
////////////////////////////////////////////////////////////
1793
// read the beginning of a paragraph data
1794
////////////////////////////////////////////////////////////
1795
bool WriterPlsParser::readParagraphData(WriterPlsParserInternal::ParagraphInfo const &info, bool hasFonts,
1796
                                        WriterPlsParserInternal::ParagraphData &data)
1797
472k
{
1798
472k
  libmwaw::DebugStream f;
1799
1800
472k
  MWAWInputStreamPtr input = getInput();
1801
472k
  long pos = info.m_pos;
1802
472k
  input->seek(pos, librevenge::RVNG_SEEK_SET);
1803
1804
472k
  data = WriterPlsParserInternal::ParagraphData();
1805
472k
  auto textLength = static_cast<int>(input->readLong(2));
1806
472k
  auto length2 = static_cast<int>(input->readLong(2));
1807
472k
  data.m_endPos = pos+4+textLength+length2;
1808
1809
472k
  if (textLength < 0 || length2 < 0 || !input->checkPosition(data.m_endPos)) {
1810
178k
    MWAW_DEBUG_MSG(("WriterPlsParser::readParagraphData:  paragraph is too short\n"));
1811
178k
    return false;
1812
178k
  }
1813
294k
  if (textLength) {
1814
25.9k
    std::string &text = data.m_text;
1815
6.35M
    for (int i = 0; i < textLength; i++) {
1816
6.34M
      auto c = char(input->readULong(1));
1817
6.34M
      if (c == '\0') return false;
1818
6.32M
      text += c;
1819
6.32M
    }
1820
25.9k
  }
1821
277k
  auto type = static_cast<int>(input->readULong(2));
1822
277k
  data.m_type = (type & 7);
1823
277k
  data.m_typeFlag = (type & 0xFFF8);
1824
1825
277k
  f << "Entries(Paragraph" << data.m_type << "):";
1826
1827
  // format type
1828
277k
  if (info.m_type != data.m_type + (data.m_typeFlag!=0 ? 8 : 0)) {
1829
238k
    MWAW_DEBUG_MSG(("WriterPlsParser::readParagraph: I find an unexpected type\n"));
1830
238k
    f << "#diffType=" << info.m_type << ",";
1831
238k
  }
1832
1833
277k
  data.m_height = static_cast<int>(input->readLong(2));
1834
277k
  data.m_indent[0] = static_cast<int>(input->readLong(2)); // left indent ?
1835
277k
  data.m_width = static_cast<int>(input->readLong(2));
1836
277k
  data.m_indent[1] = static_cast<int>(input->readLong(2)); // first pos indent ?
1837
277k
  data.m_unknown = static_cast<int>(input->readLong(2));
1838
1839
555k
  for (auto &numData : data.m_numData) numData = static_cast<int>(input->readLong(2));
1840
1841
277k
  auto &fonts = data.m_fonts;
1842
277k
  if (hasFonts) {
1843
277k
    long actPos = input->tell();
1844
277k
    if (data.m_numData[0]<0 || !input->checkPosition(actPos+data.m_numData[0]*16)) {
1845
65.6k
      MWAW_DEBUG_MSG(("WriterPlsParser::readParagraph: pb reading the number of fonts\n"));
1846
65.6k
      f << "###numFonts=" << data.m_numData[0] << ",";
1847
65.6k
    }
1848
212k
    else if (!readFonts(data.m_numData[0], data.m_type, fonts)) {
1849
0
      MWAW_DEBUG_MSG(("WriterPlsParser::readParagraph: pb with the fonts\n"));
1850
0
      input->seek(actPos+data.m_numData[0]*16, librevenge::RVNG_SEEK_SET);
1851
0
    }
1852
277k
  }
1853
1854
277k
  f << data;
1855
1.02M
  for (size_t i = 0; i < fonts.size(); i++) {
1856
747k
    f << "font" << i << "=[";
1857
#ifdef DEBUG
1858
    f << fonts[i].m_font.getDebugString(getFontConverter());
1859
#endif
1860
747k
    f << fonts[i] << "],";
1861
747k
  }
1862
277k
  ascii().addPos(pos);
1863
277k
  ascii().addNote(f.str().c_str());
1864
1865
277k
  return true;
1866
294k
}
1867
1868
////////////////////////////////////////////////////////////
1869
// read a series of fonts
1870
////////////////////////////////////////////////////////////
1871
bool WriterPlsParser::readFonts
1872
(int nFonts, int type, std::vector<WriterPlsParserInternal::Font> &fonts)
1873
212k
{
1874
212k
  fonts.resize(0);
1875
212k
  MWAWInputStreamPtr input = getInput();
1876
212k
  bool hasFontExtra = true;
1877
212k
  switch (type) {
1878
173k
  case 0: // find in these case junk in the last part of font
1879
175k
  case 2:
1880
180k
  case 4:
1881
180k
    hasFontExtra = false;
1882
180k
    break;
1883
31.6k
  default:
1884
31.6k
    break;
1885
212k
  }
1886
212k
  int actPos = 0;
1887
212k
  libmwaw::DebugStream f;
1888
959k
  for (int i = 0; i < nFonts; i++) {
1889
747k
    if (!input->checkPosition(input->tell()+16)) {
1890
0
      MWAW_DEBUG_MSG(("WriterPlsParser::readFonts: the zone seems too short\n"));
1891
0
      break;
1892
0
    }
1893
747k
    WriterPlsParserInternal::Font fInfo;
1894
747k
    f.str("");
1895
747k
    auto val = static_cast<int>(input->readLong(2)); // 65|315
1896
747k
    if (val) f << "dim?=" << val << ",";
1897
2.99M
    for (int j = 0; j < 3; j++) { // always 0: a color ?
1898
2.24M
      val = static_cast<int>(input->readLong(1));
1899
2.24M
      if (val) f << "f" << j << "=" << val << ",";
1900
2.24M
    }
1901
747k
    MWAWFont &font = fInfo.m_font;
1902
747k
    font.setId(static_cast<int>(input->readULong(1)));
1903
747k
    auto flag = static_cast<int>(input->readULong(1));
1904
747k
    uint32_t flags = 0;
1905
747k
    if (flag&0x1) flags |= MWAWFont::boldBit;
1906
747k
    if (flag&0x2) flags |= MWAWFont::italicBit;
1907
747k
    if (flag&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
1908
747k
    if (flag&0x8) flags |= MWAWFont::embossBit;
1909
747k
    if (flag&0x10) flags |= MWAWFont::shadowBit;
1910
747k
    if (flag&0x60)
1911
174k
      f << "#fl=" << std::hex << (flag&0x60) << std::dec << ",";
1912
747k
    if (flag&0x80) f << "fl80,"; // frequent, find on complete line,
1913
1914
747k
    flag= static_cast<int>(input->readULong(1));
1915
747k
    if (flag&2) font.set(MWAWFont::Script::super100());
1916
747k
    if (flag&4) font.set(MWAWFont::Script::sub100());
1917
747k
    if (flag&0x10) f << "flA10,";// also frequent, find on complete line
1918
747k
    if (flag&0xE9) f << "#flA=" << std::hex << (flag&0xE9) << std::dec << ",";
1919
747k
    font.setFlags(flags);
1920
747k
    val = static_cast<int>(input->readLong(1));// always 0
1921
747k
    if (val)
1922
397k
      f << "#g0=" << val << ",";
1923
747k
    font.setSize(float(input->readLong(1)));
1924
747k
    fInfo.m_firstChar = actPos;
1925
747k
    auto nChar = static_cast<int>(input->readULong(2));
1926
747k
    actPos += nChar;
1927
747k
    if (!hasFontExtra)
1928
455k
      input->seek(4, librevenge::RVNG_SEEK_CUR);
1929
292k
    else { // always 0
1930
876k
      for (int j = 0; j < 2; j++) {
1931
584k
        val = static_cast<int>(input->readLong(2));
1932
584k
        if (val) f << "g" << j+1 << "=" << val << ",";
1933
584k
      }
1934
292k
    }
1935
747k
    font.m_extra+=f.str();
1936
747k
    fonts.push_back(fInfo);
1937
747k
  }
1938
1939
212k
  return true;
1940
212k
}
1941
1942
////////////////////////////////////////////////////////////
1943
// read a series of lines
1944
////////////////////////////////////////////////////////////
1945
bool WriterPlsParser::readLines
1946
(WriterPlsParserInternal::ParagraphInfo const &/*info*/,
1947
 int nLines, std::vector<WriterPlsParserInternal::Line> &lines)
1948
79.9k
{
1949
79.9k
  lines.resize(0);
1950
79.9k
  MWAWInputStreamPtr input = getInput();
1951
1952
79.9k
  int actPos = 0;
1953
174M
  for (int i = 0; i < nLines; i++) {
1954
174M
    WriterPlsParserInternal::Line lInfo;
1955
174M
    lInfo.m_height = static_cast<int>(input->readLong(2));
1956
174M
    lInfo.m_maxFontSize = static_cast<int>(input->readLong(2)); // checkMe
1957
174M
    lInfo.m_width = static_cast<int>(input->readLong(2));
1958
174M
    auto nChar = static_cast<int>(input->readLong(2));
1959
174M
    lInfo.m_firstChar = actPos;
1960
174M
    actPos += nChar;
1961
    /*
1962
       f0 always 0
1963
       f1 almost always 0, if not 1
1964
       f2 almost always 0, if not 2, 3, 4, c
1965
       f3 almost always 0, if not 200, 400, 6465, 7600, dfc, e03, e04, e06 : junk?
1966
     */
1967
697M
    for (auto &fl : lInfo.m_flags) fl = static_cast<int>(input->readLong(2));
1968
174M
    lines.push_back(lInfo);
1969
174M
  }
1970
79.9k
  return true;
1971
79.9k
}
1972
1973
////////////////////////////////////////////////////////////
1974
// read the print info
1975
////////////////////////////////////////////////////////////
1976
bool WriterPlsParser::readPrintInfo()
1977
37.9k
{
1978
37.9k
  MWAWInputStreamPtr input = getInput();
1979
37.9k
  long pos = input->tell();
1980
37.9k
  libmwaw::DebugStream f;
1981
  // print info
1982
37.9k
  libmwaw::PrinterInfo info;
1983
37.9k
  if (!info.read(input)) return false;
1984
37.5k
  f << "Entries(PrintInfo):"<< info;
1985
1986
37.5k
  MWAWVec2i paperSize = info.paper().size();
1987
37.5k
  MWAWVec2i pageSize = info.page().size();
1988
37.5k
  if (pageSize.x() <= 0 || pageSize.y() <= 0 ||
1989
37.3k
      paperSize.x() <= 0 || paperSize.y() <= 0) return false;
1990
1991
  // define margin from print info
1992
37.1k
  MWAWVec2i lTopMargin= -1 * info.paper().pos(0);
1993
37.1k
  MWAWVec2i rBotMargin=info.paper().size() - info.page().size();
1994
1995
  // move margin left | top
1996
37.1k
  int decalX = lTopMargin.x() > 14 ? lTopMargin.x()-14 : 0;
1997
37.1k
  int decalY = lTopMargin.y() > 14 ? lTopMargin.y()-14 : 0;
1998
37.1k
  lTopMargin -= MWAWVec2i(decalX, decalY);
1999
37.1k
  rBotMargin += MWAWVec2i(decalX, decalY);
2000
2001
  // decrease right | bottom
2002
37.1k
  int rightMarg = rBotMargin.x() -50;
2003
37.1k
  if (rightMarg < 0) rightMarg=0;
2004
37.1k
  int botMarg = rBotMargin.y() -50;
2005
37.1k
  if (botMarg < 0) botMarg=0;
2006
2007
37.1k
  getPageSpan().setMarginTop(lTopMargin.y()/72.0);
2008
37.1k
  getPageSpan().setMarginBottom(botMarg/72.0);
2009
37.1k
  getPageSpan().setMarginLeft(lTopMargin.x()/72.0);
2010
37.1k
  getPageSpan().setMarginRight(rightMarg/72.0);
2011
37.1k
  getPageSpan().setFormLength(paperSize.y()/72.);
2012
37.1k
  getPageSpan().setFormWidth(paperSize.x()/72.);
2013
2014
37.1k
  ascii().addPos(pos);
2015
37.1k
  ascii().addNote(f.str().c_str());
2016
2017
37.1k
  input->seek(pos+0x78, librevenge::RVNG_SEEK_SET);
2018
37.1k
  if (long(input->tell()) != pos+0x78) {
2019
0
    MWAW_DEBUG_MSG(("WriterPlsParser::readPrintInfo: file is too short\n"));
2020
0
    return false;
2021
0
  }
2022
2023
37.1k
  return true;
2024
37.1k
}
2025
2026
// vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: