Debian Codegraph @

src/lib/MsWrdTextStyles.cxx

raw
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 ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
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 #include <map>
34
35 #include "MWAWTextListener.hxx"
36 #include "MWAWSection.hxx"
37
38 #include "MsWrdParser.hxx"
39 #include "MsWrdStruct.hxx"
40 #include "MsWrdText.hxx"
41
42 #include "MsWrdTextStyles.hxx"
43
44 /** Internal: the structures of a MsWrdTextStyles */
45 namespace MsWrdTextStylesInternal
46 {
47 ////////////////////////////////////////
48 //! Internal: the state of a MsWrdTextStylesInternal
49 struct State {
50 //! constructor
51 State()
52 : m_version(-1)
53 , m_defaultFont(2,12)
54 , m_nextStyleMap()
55 , m_fontList()
56 , m_paragraphList()
57 , m_sectionList()
58 , m_textstructParagraphList()
59 , m_styleFontMap()
60 , m_styleParagraphMap()
61 {
62 }
63 //! the file version
64 int m_version;
65
66 //! the default font ( NewYork 12pt)
67 MWAWFont m_defaultFont;
68
69 //! a map styleId to next styleId
70 std::map<int,int> m_nextStyleMap;
71
72 //! the list of fonts
73 std::vector<MsWrdStruct::Font> m_fontList;
74
75 //! the list of paragraph
76 std::vector<MsWrdStruct::Paragraph> m_paragraphList;
77
78 //! the list of section
79 std::vector<MsWrdStruct::Section> m_sectionList;
80
81 //! the list of paragraph in textstruct
82 std::vector<MsWrdStruct::Paragraph> m_textstructParagraphList;
83
84 //! the list of fonts in style
85 std::map<int, MsWrdStruct::Font> m_styleFontMap;
86
87 //! the list of paragraph in style
88 std::map<int, MsWrdStruct::Paragraph> m_styleParagraphMap;
89 };
90 }
91
92 ////////////////////////////////////////////////////////////
93 // constructor/destructor, ...
94 ////////////////////////////////////////////////////////////
95 MsWrdTextStyles::MsWrdTextStyles(MsWrdText &textParser)
96 : m_parserState(textParser.getParserState())
97 , m_state(new MsWrdTextStylesInternal::State)
98 , m_mainParser(textParser.m_mainParser)
99 , m_textParser(&textParser)
100 {
101 }
102
103 MsWrdTextStyles::~MsWrdTextStyles()
104 { }
105
106 int MsWrdTextStyles::version() const
107 {
108 if (m_state->m_version < 0)
109 m_state->m_version = m_parserState->m_version;
110 return m_state->m_version;
111 }
112
113 MWAWFont const &MsWrdTextStyles::getDefaultFont() const
114 {
115 return m_state->m_defaultFont;
116 }
117
118 ////////////////////////////////////////////////////////////
119 // try to read a font
120 ////////////////////////////////////////////////////////////
121 bool MsWrdTextStyles::readFont(MsWrdStruct::Font &font, MsWrdTextStyles::ZoneType type)
122 {
123 bool mainZone = type==TextZone;
124 libmwaw::DebugStream f;
125
126 MWAWInputStreamPtr &input= m_parserState->m_input;
127 long pos = input->tell();
128 auto sz = static_cast<int>(input->readULong(1));
129 if (sz > 20 || sz == 3) {
130 input->seek(pos, librevenge::RVNG_SEEK_SET);
131 return false;
132 }
133
134 if (sz == 0) return true;
135
136 auto flag = static_cast<int>(input->readULong(1));
137 uint32_t flags = font.m_font->flags();
138 if (flag&0x80) flags ^= MWAWFont::boldBit;
139 if (flag&0x40) flags ^= MWAWFont::italicBit;
140 if (flag&0x20) {
141 if (font.m_font->getStrikeOut().m_style==MWAWFont::Line::Simple)
142 font.m_font->setStrikeOutStyle(MWAWFont::Line::None);
143 else
144 font.m_font->setStrikeOutStyle(MWAWFont::Line::Simple);
145 }
146 if (flag&0x10) flags ^= MWAWFont::outlineBit;
147 if (flag&0x8) flags ^= MWAWFont::shadowBit;
148 if (flag&0x4) flags ^= MWAWFont::smallCapsBit;
149 if (flag&0x2) flags ^= MWAWFont::uppercaseBit;
150 if (flag&0x1) flags ^= MWAWFont::hiddenBit;
151
152 int what = 0;
153 /* 01: horizontal decal, 2: vertical decal, 4; underline, 08: fSize, 10: set font, 20: font color, 40: ???(maybe reset)
154 */
155 if (sz >= 2) what = static_cast<int>(input->readULong(1));
156
157 if (sz >= 4) {
158 auto fId = static_cast<int>(input->readULong(2));
159 if (fId) {
160 if (mainZone && (what & 0x50)==0) f << "#fId,";
161 font.m_font->setId(fId);
162 }
163 else if (what & 0x10) {
164 }
165 what &= 0xEF;
166 }
167 else if (what & 0x10) {
168 }
169 if (sz >= 5) {
170 float fSz = float(input->readULong(1))/2.0f;
171 if (fSz>0) {
172 if (mainZone && (what & 0x48)==0) f << "#fSz,";
173 font.m_font->setSize(fSz);
174 }
175 what &= 0xF7;
176 }
177
178 if (sz >= 6) {
179 auto decal = static_cast<int>(input->readLong(1)); // unit point
180 if (decal) {
181 if (what & 0x2)
182 font.m_font->set(MWAWFont::Script(float(decal)/2.f, librevenge::RVNG_POINT));
183 else
184 f << "#vDecal=" << decal;
185 }
186 what &= 0xFD;
187 }
188 if (sz >= 7) {
189 auto decal = static_cast<int>(input->readLong(1)); // unit point > 0 -> expand < 0: condensed
190 if (decal) {
191 if ((what & 0x1) == 0) f << "#hDecal=" << decal <<",";
192 else font.m_font->setDeltaLetterSpacing(float(decal)/16.0f);
193 }
194 what &= 0xFE;
195 }
196
197 if (sz >= 8) {
198 auto val = static_cast<int>(input->readULong(1));
199 if (val & 0xF0) {
200 if (what & 0x20) {
201 MWAWColor col;
202 if (m_mainParser->getColor((val>>4),col))
203 font.m_font->setColor(col);
204 else
205 f << "#fColor=" << (val>>4) << ",";
206 }
207 else
208 f << "#fColor=" << (val>>4) << ",";
209 }
210 what &= 0xDF;
211
212 if (val && (what & 0x4)) {
213 auto style=MWAWFont::Line::Simple;
214 switch ((val>>1)&0x7) {
215 case 4:
216 style=MWAWFont::Line::Dot;
217 break;
218 case 3:
219 font.m_font->setUnderlineType(MWAWFont::Line::Double);
220 break;
221 case 2:
222 font.m_font->setUnderlineWordFlag(true);
223 break;
224 case 1:
225 break;
226 default:
227 f << "#underline=" << ((val>>1) &0x7) << ",";
228 break;
229 }
230 if (font.m_font->getUnderline().m_style==style)
231 style=MWAWFont::Line::None;
232 font.m_font->setUnderlineStyle(style);
233 what &= 0xFB;
234 }
235 else if (val & 0xe)
236 f << "#underline?=" << ((val>>1) &0x7) << ",";
237 if (val & 0xF1)
238 f << "#underline[unkn]=" << std::hex << (val & 0xF1) << std::dec << ",";
239 }
240 if (what & 0x20) {
241 font.m_font->setColor(MWAWColor::black());
242 what &= 0xDF;
243 }
244 if (what & 0x4) {
245 font.m_font->setUnderlineStyle(MWAWFont::Line::None);
246 what &= 0xFB;
247 }
248 if (what & 0x2) {
249 font.m_font->set(MWAWFont::Script(0, librevenge::RVNG_POINT));
250 what &= 0xFD;
251 }
252 if (what & 0x1) {
253 font.m_font->setDeltaLetterSpacing(0);
254 what &= 0xFE;
255 }
256 font.m_unknown =what;
257 font.m_font->setFlags(flags);
258
259 bool ok = false;
260 if (mainZone && sz >= 10 && sz <= 12) {
261 auto wh = static_cast<int>(input->readULong(1));
262 long pictPos = 0;
263 for (int i = 10; i < 13; i++) {
264 pictPos <<= 8;
265 if (i <= sz) pictPos += input->readULong(1);
266 }
267 long actPos = input->tell();
268 if (m_mainParser->checkPicturePos(pictPos, wh)) {
269 ok = true;
270 input->seek(actPos, librevenge::RVNG_SEEK_SET);
271 font.m_picturePos = pictPos;
272 f << "pictWh=" << wh << ",";
273 }
274 else
275 input->seek(pos+1+8, librevenge::RVNG_SEEK_SET);
276 }
277 if (!ok && sz >= 9) {
278 auto wh = static_cast<int>(input->readLong(1));
279 switch (wh) {
280 case -1:
281 ok = true;
282 break;
283 case 0: // line height ?
284 if (sz < 10) break;
285 font.m_size=float(input->readULong(1))/2.f;
286 ok = true;
287 break;
288 default:
289 break;
290 }
291 }
292 if (!ok && sz >= 9) {
293 input->seek(pos+1+8, librevenge::RVNG_SEEK_SET);
294 f << "#";
295 }
296 if (long(input->tell()) != pos+1+sz)
297 m_parserState->m_asciiFile.addDelimiter(input->tell(), '|');
298
299 input->seek(pos+1+sz, librevenge::RVNG_SEEK_SET);
300 font.m_extra = f.str();
301 return true;
302 }
303
304 bool MsWrdTextStyles::getFont(ZoneType type, int id, MsWrdStruct::Font &font)
305 {
306 MsWrdStruct::Font *fFont = nullptr;
307 switch (type) {
308 case TextZone:
309 if (id < 0 || id >= int(m_state->m_fontList.size()))
310 break;
311 fFont = &m_state->m_fontList[size_t(id)];
312 break;
313 case StyleZone:
314 if (m_state->m_styleFontMap.find(id) == m_state->m_styleFontMap.end())
315 break;
316 fFont = &m_state->m_styleFontMap.find(id)->second;
317 break;
318 case TextStructZone:
319 case InParagraphDefinition:
320 #if !defined(__clang__)
321 default:
322 #endif
323 MWAW_DEBUG_MSG(("MsWrdTextStyles::getFont: do not know how to send this type of font\n"));
324 return false;
325 }
326 if (!fFont) {
327 MWAW_DEBUG_MSG(("MsWrdTextStyles::getFont: can not find font with %d[type=%d]\n", id, int(type)));
328 return false;
329 }
330 int fId = font.m_font->id();
331 float fSz = font.m_font->size();
332 font = *fFont;
333 if (font.m_font->id() < 0)
334 font.m_font->setId(fId);
335 if (font.m_font->size() <= 0)
336 font.m_font->setSize(fSz);
337 return true;
338 }
339
340 void MsWrdTextStyles::setProperty(MsWrdStruct::Font const &font)
341 {
342 if (!m_parserState->m_textListener) return;
343 MsWrdStruct::Font tmp = font;
344 if (tmp.m_font->id() < 0) tmp.m_font->setId(m_state->m_defaultFont.id());
345 if (tmp.m_font->size() <= 0) tmp.m_font->setSize(m_state->m_defaultFont.size());
346 tmp.updateFontToFinalState();
347 m_parserState->m_textListener->setFont(*tmp.m_font);
348 }
349
350 ////////////////////////////////////////////////////////////
351 // read/send the paragraph zone
352 ////////////////////////////////////////////////////////////
353 bool MsWrdTextStyles::getParagraph(ZoneType type, int id, MsWrdStruct::Paragraph &para)
354 {
355 switch (type) {
356 case TextZone:
357 if (id < 0 || id >= int(m_state->m_paragraphList.size()))
358 break;
359 para = m_state->m_paragraphList[size_t(id)];
360 return true;
361 case StyleZone:
362 if (m_state->m_styleParagraphMap.find(id) == m_state->m_styleParagraphMap.end())
363 break;
364 para = m_state->m_styleParagraphMap.find(id)->second;
365 return true;
366 case TextStructZone:
367 if (id < 0 || id >= int(m_state->m_textstructParagraphList.size()))
368 break;
369 para = m_state->m_textstructParagraphList[size_t(id)];
370 return true;
371 case InParagraphDefinition:
372 #if !defined(__clang__)
373 default:
374 #endif
375 MWAW_DEBUG_MSG(("MsWrdTextStyles::getParagraph: do not know how to send this type of font\n"));
376 return false;
377 }
378
379 MWAW_DEBUG_MSG(("MsWrdTextStyles::getParagraph: can not find paragraph with %d[type=%d]\n", id, int(type)));
380 return false;
381 }
382
383 void MsWrdTextStyles::sendDefaultParagraph()
384 {
385 if (!m_parserState->m_textListener) return;
386 m_parserState->m_textListener->setParagraph(MsWrdStruct::Paragraph(version()));
387 }
388
389 bool MsWrdTextStyles::readParagraph(MsWrdStruct::Paragraph &para, int dataSz)
390 {
391 int sz;
392 MWAWInputStreamPtr &input= m_parserState->m_input;
393 if (dataSz >= 0)
394 sz = dataSz;
395 else
396 sz = static_cast<int>(input->readULong(2));
397
398 long pos = input->tell();
399 long endPos = pos+sz;
400
401 if (sz == 0) return true;
402 if (!input->checkPosition(endPos)) return false;
403
404 int const vers = version();
405 libmwaw::DebugStream f;
406 int numFont=0;
407 while (long(input->tell()) < endPos) {
408 long actPos = input->tell();
409 /* 5-16: basic paragraph properties
410 75-84: basic section properties
411 other
412 */
413 if (para.read(input,endPos)) continue;
414 input->seek(actPos, librevenge::RVNG_SEEK_SET);
415
416 int wh = static_cast<int>(input->readULong(1)), val;
417 if (vers <= 3 && wh >= 0x36 && wh <= 0x45) {
418 // this section data has different meaning in v3 and after...
419 input->seek(actPos, librevenge::RVNG_SEEK_SET);
420 break;
421 }
422 bool done = false;
423 long dSz = endPos-actPos;
424 switch (wh) {
425 case 0:
426 done = (actPos+1==endPos||(dataSz==2 && actPos+2==endPos));
427 break;
428 case 0x38:
429 if (dSz < 4) break;
430 val = static_cast<int>(input->readLong(1));
431 if (val != 2) f << "#shadType=" << val << ",";
432 f << "shad=" << float(input->readLong(2))/100.f << "%,";
433 done = true;
434 break;
435 case 0x3a: // checkme: maybe plain
436 f << "f3a,";
437 done = true;
438 break;
439 case 0x4d: {
440 if (dSz < 2) break;
441 val = static_cast<int>(input->readLong(1));
442 para.m_modFont->m_font->set(MWAWFont::Script(float(val)/2.f,librevenge::RVNG_POINT));
443 if (val==0) f << "pos[y]=0,";
444 done = true;
445 break;
446 }
447 case 0x3c: // bold
448 case 0x3d: // italic?
449 case 0x3e: // strikeout (chekme)
450 case 0x3f: // outline (chekme)
451 case 0x40: // shadow (chekme)
452 case 0x41: // small caps (chekme)
453 case 0x42: // all caps (chekme)
454 case 0x43: // hidden (chekme)
455 case 0x45: // underline
456 case 0x4a: {
457 if (dSz < 2) break;
458 done = true;
459 val = static_cast<int>(input->readULong(1));
460 if (wh == 0x4a) {
461 if (val > 4 && val < 40)
462 para.m_modFont->m_font->setSize(float(val)/2.0f);
463 else
464 f << "#fSize=" << val << ",";
465 break;
466 }
467 switch (wh) {
468 case 0x3c:
469 case 0x3d:
470 case 0x3e:
471 case 0x3f:
472 case 0x40:
473 case 0x41:
474 case 0x42:
475 case 0x43:
476 para.m_modFont->m_flags[wh-0x3c]=val;
477 break;
478 case 0x45:
479 para.m_modFont->m_flags[8]=val;
480 break;
481 default:
482 break;
483 }
484 break;
485 }
486 case 0x44:
487 if (dSz < 3) break;
488 done = true;
489 val = static_cast<int>(input->readULong(2));
490 para.m_modFont->m_font->setId(val);
491 break;
492 case 0x2: // a small number between 0 and 4
493 case 0x34: // 0 ( one time)
494 case 0x39: // 0 ( one time)
495 case 0x47: // 0 one time
496 case 0x49: // 0 ( one time)
497 case 0x4c: // 0, 6, -12
498 case 0x5e: // 0
499 if (dSz < 2) break;
500 done = true;
501 val = static_cast<int>(input->readLong(1));
502 f << "f" << std::hex << wh << std::dec << "=" << val << ",";
503 break;
504 case 0x23: // alway 0 ?
505 if (dSz < 3) break;
506 done = true;
507 val = static_cast<int>(input->readLong(2));
508 f << "f" << std::hex << wh << std::dec << "=" << val << ",";
509 break;
510 case 0x9f: // two small number: table range?
511 if (dSz < 3) break;
512 done = true;
513 f << "f" << std::hex << wh << "=[";
514 for (int i = 0; i < 2; i++)
515 f << input->readULong(1) << ",";
516 f << std::dec << "],";
517 break;
518 case 0x50: // two small number
519 if (dSz < 4) break;
520 done = true;
521 f << "f" << std::hex << wh << std::dec << "=[";
522 f << input->readLong(1) << ",";
523 f << input->readLong(2) << ",";
524 f << "],";
525 break;
526 case 0x4f: // a small int and a pos?
527 if (dSz < 4) break;
528 done = true;
529 f << "f" << std::hex << wh << std::dec << "=[";
530 f << input->readLong(1) << ",";
531 f << std::hex << input->readULong(2) << std::dec << "],";
532 break;
533 case 0x9e: // two small number + a pos?
534 if (dSz < 5) break;
535 done = true;
536 f << "f" << std::hex << wh << std::dec << "=[";
537 for (int i = 0; i < 2; i++)
538 f << input->readLong(1) << ",";
539 f << std::hex << input->readULong(2) << std::dec << "],";
540 break;
541 case 0x4e:
542 case 0x53: { // same as 4e ( new format )
543 // checkme: sometimes, we can have a list of font, do we need to
544 // use only the last one?
545 if (numFont++)
546 f << "#font" << numFont-1 << "=["
547 << para.m_font->m_font->getDebugString(m_parserState->m_fontConverter)
548 << "," << *para.m_font << "],";
549 done = true;
550 para.m_font = MsWrdStruct::Font();
551 if (!readFont(*para.m_font, InParagraphDefinition) || long(input->tell()) > endPos) {
552 done = false;
553 f << "#";
554 break;
555 }
556 break;
557 }
558 case 0x5f: { // 4 index
559 if (dSz < 10) break;
560 done = true;
561 sz = static_cast<int>(input->readULong(1));
562 if (sz != 8) f << "#sz=" << sz << ",";
563 f << "f5f=[";
564 for (int i = 0; i < 4; i++) f << input->readLong(2) << ",";
565 f << "],";
566 break;
567 }
568 case 0x94: // checkme space between column divided by 2 (in table) ?
569 if (dSz < 3) break;
570 done = true;
571 val = static_cast<int>(input->readLong(2));
572 f << "colSep[table]=" << 2*val/1440. << ",";
573 break;
574 default:
575 break;
576 }
577 if (!done) {
578 input->seek(actPos, librevenge::RVNG_SEEK_SET);
579 break;
580 }
581 }
582 if (long(input->tell()) != endPos) {
583 static bool first = true;
584 if (first) {
585 MWAW_DEBUG_MSG(("MsWrdTextStyles::readParagraph: can not read end of paragraph\n"));
586 first = false;
587 }
588 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
589 ascFile.addDelimiter(input->tell(),'|');
590 f << "####";
591 input->seek(endPos, librevenge::RVNG_SEEK_SET);
592 }
593 para.m_extra += f.str();
594
595 return true;
596 }
597
598 ////////////////////////////////////////////////////////////
599 // read the char/parag plc
600 ////////////////////////////////////////////////////////////
601 bool MsWrdTextStyles::readPLCList(MsWrdEntry const &entry)
602 {
603 if (entry.length() < 10 || (entry.length()%6) != 4) {
604 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLCList: the zone size seems odd\n"));
605 return false;
606 }
607 long pos = entry.begin();
608 entry.setParsed(true);
609 MWAWInputStreamPtr &input= m_parserState->m_input;
610 input->seek(pos, librevenge::RVNG_SEEK_SET);
611 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
612 libmwaw::DebugStream f;
613 f << entry.type() << ":";
614 auto N=int(entry.length()/6);
615 std::vector<long> textPos; // limit of the text in the file
616 textPos.resize(size_t(N)+1);
617 for (auto &tPos : textPos) tPos = long(input->readULong(4));
618 int const expectedSize = (version() <= 3) ? 0x80 : 0x200;
619 for (int i = 0; i < N; i++) {
620 if (!input->checkPosition(textPos[size_t(i)])) f << "#";
621
622 auto defPos = long(input->readULong(2));
623 f << std::hex << "[filePos?=" << textPos[size_t(i)] << ",dPos=" << defPos << std::dec << ",";
624 f << "],";
625
626 MsWrdEntry plc;
627 plc.setType(entry.id() ? "ParagPLC" : "CharPLC");
628 plc.setId(i);
629 plc.setBegin(defPos*expectedSize);
630 plc.setLength(expectedSize);
631 if (!input->checkPosition(plc.end())) {
632 f << "#PLC,";
633 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLCList: plc def is outside the file\n"));
634 }
635 else {
636 long actPos = input->tell();
637 MWAWVec2<long> fLimit(textPos[size_t(i)], textPos[size_t(i)+1]);
638 readPLC(plc, entry.id(), fLimit);
639 input->seek(actPos, librevenge::RVNG_SEEK_SET);
640 }
641 }
642 f << std::hex << "end?=" << textPos[size_t(N)] << ",";
643
644 ascFile.addPos(pos);
645 ascFile.addNote(f.str().c_str());
646
647 ascFile.addPos(entry.end());
648 ascFile.addNote("_");
649 return true;
650 }
651
652 bool MsWrdTextStyles::readPLC(MsWrdEntry &entry, int type, MWAWVec2<long> const &fLimit)
653 {
654 int const vers = version();
655 int const expectedSize = (vers <= 3) ? 0x80 : 0x200;
656 int const posFactor = (vers <= 3) ? 1 : 2;
657 if (entry.length() != expectedSize) {
658 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLC: the zone size seems odd\n"));
659 return false;
660 }
661 MWAWInputStreamPtr &input= m_parserState->m_input;
662 input->seek(entry.end()-1, librevenge::RVNG_SEEK_SET);
663 auto N=static_cast<int>(input->readULong(1));
664 if (N==0 || 5*(N+1) > entry.length()) {
665 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLC: the number of plc seems odd\n"));
666 return false;
667 }
668
669 long pos = entry.begin();
670 entry.setParsed(true);
671 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
672 libmwaw::DebugStream f;
673 f << "Entries("<< entry.type() << ")[" << entry.id() << "]:N=" << N << ",";
674
675 input->seek(pos, librevenge::RVNG_SEEK_SET);
676 std::vector<long> filePos;
677 filePos.resize(size_t(N)+1);
678 for (auto &fPos : filePos) fPos = long(input->readULong(4));
679 if (filePos[0] != fLimit[0]) {
680 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLC: bad first limit\n"));
681 return false;
682 }
683 std::map<int, int> mapPosId;
684 std::vector<int> decal;
685 decal.resize(size_t(N));
686 size_t numData = type == 0 ? m_state->m_fontList.size() :
687 m_state->m_paragraphList.size();
688 auto plcType = type == 0 ? MsWrdText::PLC::Font : MsWrdText::PLC::Paragraph;
689 auto &plcMap = m_textParser->getFilePLCMap();
690
691 for (size_t i = 0; i < size_t(N); i++) {
692 decal[i] = static_cast<int>(input->readULong(1));
693 int id = -1;
694 if (decal[i]) {
695 if (mapPosId.find(decal[i]) != mapPosId.end())
696 id = mapPosId.find(decal[i])->second;
697 else {
698 id = int(numData++);
699 mapPosId[decal[i]] = id;
700
701 long actPos = input->tell();
702 libmwaw::DebugStream f2;
703 f2 << entry.type() << "-";
704
705 long dataPos = entry.begin()+posFactor*decal[i];
706 if (type == 0) {
707 input->seek(dataPos, librevenge::RVNG_SEEK_SET);
708 f2 << "F" << id << ":";
709 MsWrdStruct::Font font;
710 if (!readFont(font, TextZone)) {
711 font = MsWrdStruct::Font();
712 f2 << "#";
713 }
714 else
715 f2 << font.m_font->getDebugString(m_parserState->m_fontConverter) << font << ",";
716 m_state->m_fontList.push_back(font);
717 }
718 else {
719 MsWrdStruct::Paragraph para(vers);
720 f2 << "P" << id << ":";
721
722 input->seek(dataPos, librevenge::RVNG_SEEK_SET);
723 auto sz = static_cast<int>(input->readLong(1));
724 long endPos;
725 if (vers <= 3) {
726 sz++;
727 endPos = dataPos+sz;
728 }
729 else
730 endPos = dataPos+2*sz+1;
731 if (sz < 4 || endPos > entry.end()) {
732 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLC: can not read plcSz\n"));
733 f2 << "#";
734 }
735 else {
736 auto stId = static_cast<int>(input->readLong(1));
737 if (m_state->m_styleParagraphMap.find(stId)==m_state->m_styleParagraphMap.end()) {
738 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLC: can not find parent paragraph\n"));
739 f2 << "#";
740 }
741 else
742 para.m_styleId = stId;
743 f2 << "sP" << stId << ",";
744 if (vers > 3) {
745 if (!para.m_info->read(input, endPos, vers)) {
746 f2 << "###info,";
747 input->seek(dataPos+2+6, librevenge::RVNG_SEEK_SET);
748 }
749 // osnole: do we need to check here if the paragraph is empty ?
750 // ie. if (para.m_info->isEmpty()&&stId==0)
751 }
752 else { // always 0 ?
753 auto val = static_cast<int>(input->readLong(2));
754 if (val) f << "g0=" << val << ",";
755 }
756 ascFile.addDelimiter(input->tell(),'|');
757 if (readParagraph(para, int(endPos-input->tell()))) {
758 #ifdef DEBUG_WITH_FILES
759 para.print(f2, m_parserState->m_fontConverter);
760 #endif
761 }
762 else {
763 para = MsWrdStruct::Paragraph(vers);
764 f2 << "#";
765 }
766 }
767 m_state->m_paragraphList.push_back(para);
768 }
769 input->seek(actPos, librevenge::RVNG_SEEK_SET);
770 ascFile.addPos(dataPos);
771 ascFile.addNote(f2.str().c_str());
772 }
773 }
774 f << std::hex << filePos[i] << std::dec;
775 MsWrdText::PLC plc(plcType, id);
776 plcMap.insert(std::multimap<long,MsWrdText::PLC>::value_type
777 (filePos[type == 0 ? i : i+1], plc));
778 if (id >= 0) {
779 if (type==0) f << ":F" << id;
780 else f << ":P" << id;
781 }
782 f << ",";
783 }
784
785 ascFile.addPos(pos);
786 ascFile.addNote(f.str().c_str());
787
788 ascFile.addPos(entry.end());
789 ascFile.addNote("_");
790 if (filePos[size_t(N)] != fLimit[1]) {
791 if (input->isEnd()) {
792 MWAW_DEBUG_MSG(("MsWrdTextStyles::readPLC: we are already at the end of the stream\n"));
793 return false;
794 }
795 MsWrdEntry nextEntry(entry);
796 nextEntry.setBegin(entry.begin()+expectedSize);
797 MWAWVec2<long> newLimit(filePos[size_t(N)], fLimit[1]);
798 readPLC(nextEntry,type,newLimit);
799 }
800 return true;
801 }
802
803 ////////////////////////////////////////////////////////////
804 // read the text structure
805 ////////////////////////////////////////////////////////////
806 bool MsWrdTextStyles::readTextStructList(MsWrdEntry &entry)
807 {
808 if (entry.length() < 19) {
809 MWAW_DEBUG_MSG(("MsWrdTextStyles::readTextStructList: the zone seems to short\n"));
810 return false;
811 }
812 int const vers = version();
813 long pos = entry.begin();
814 MWAWInputStreamPtr &input= m_parserState->m_input;
815 input->seek(pos, librevenge::RVNG_SEEK_SET);
816 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
817 libmwaw::DebugStream f;
818 auto type = static_cast<int>(input->readLong(1));
819 if (type != 1 && type != 2) {
820 MWAW_DEBUG_MSG(("MsWrdTextStyles::readTextStructList: find odd type %d\n", type));
821 return false;
822 }
823
824 int num = 0;
825 while (type == 1) {
826 /* probably a paragraph definition. Fixme: create a function */
827 auto length = static_cast<int>(input->readULong(2));
828 long endPos = pos+3+length;
829 if (endPos > entry.end()) {
830 ascFile.addPos(pos);
831 ascFile.addNote("TextStruct[paragraph]#");
832 MWAW_DEBUG_MSG(("MsWrdTextStyles::readTextStructList: zone(paragraph) is too big\n"));
833 return false;
834 }
835 f.str("");
836 f << "ParagPLC:tP" << num++<< "]:";
837 MsWrdStruct::Paragraph para(vers);
838 input->seek(-2,librevenge::RVNG_SEEK_CUR);
839 if (readParagraph(para) && long(input->tell()) <= endPos) {
840 #ifdef DEBUG_WITH_FILES
841 para.print(f, m_parserState->m_fontConverter);
842 #endif
843 }
844 else {
845 para = MsWrdStruct::Paragraph(vers);
846 f << "#";
847 }
848 m_state->m_textstructParagraphList.push_back(para);
849 ascFile.addPos(pos);
850 ascFile.addNote(f.str().c_str());
851 input->seek(endPos, librevenge::RVNG_SEEK_SET);
852
853 pos = input->tell();
854 type = static_cast<int>(input->readULong(1));
855 if (type == 2) break;
856 if (type != 1) {
857 MWAW_DEBUG_MSG(("MsWrdTextStyles::readTextStructList: find odd type %d\n", type));
858 return false;
859 }
860 }
861 input->seek(-1,librevenge::RVNG_SEEK_CUR);
862 return true;
863 }
864
865 int MsWrdTextStyles::readPropertyModifier(bool &complex, std::string &extra)
866 {
867 MWAWInputStreamPtr &input= m_parserState->m_input;
868 long pos = input->tell();
869 auto c = static_cast<int>(input->readULong(1));
870 complex = false;
871 if (c&0x80) { // complex data, let get the id
872 complex=true;
873 return ((c&0x7F)<<8)|static_cast<int>(input->readULong(1));
874 }
875 if (c==0) {
876 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
877 return -1;
878 }
879 int id = -1;
880 libmwaw::DebugStream f;
881 MsWrdStruct::Paragraph para(version());
882 input->seek(-1, librevenge::RVNG_SEEK_CUR);
883 if (readParagraph(para, 2)) {
884 id = int(m_state->m_textstructParagraphList.size());
885 m_state->m_textstructParagraphList.push_back(para);
886 #ifdef DEBUG_WITH_FILES
887 f << "[";
888 para.print(f, m_parserState->m_fontConverter);
889 f << "]";
890 #endif
891 }
892 else {
893 input->seek(pos+1, librevenge::RVNG_SEEK_SET);
894 f << "#f" << std::hex << c << std::dec << "=" << static_cast<int>(input->readULong(1));
895 }
896 extra = f.str();
897 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
898 return id;
899 }
900
901 ////////////////////////////////////////////////////////////
902 // read/send the section zone
903 ////////////////////////////////////////////////////////////
904 bool MsWrdTextStyles::getSection(ZoneType type, int id, MsWrdStruct::Section &section)
905 {
906 switch (type) {
907 case TextZone:
908 if (id < 0 || id >= int(m_state->m_sectionList.size()))
909 break;
910 section = m_state->m_sectionList[size_t(id)];
911 return true;
912 case StyleZone:
913 case TextStructZone:
914 case InParagraphDefinition:
915 #if !defined(__clang__)
916 default:
917 #endif
918 MWAW_DEBUG_MSG(("MsWrdTextStyles::getSection do not know how to get this type of section\n"));
919 return false;
920 }
921 MWAW_DEBUG_MSG(("MsWrdTextStyles::getSection can not find this section\n"));
922 return false;
923 }
924
925 bool MsWrdTextStyles::getSectionParagraph(ZoneType type, int id, MsWrdStruct::Paragraph &para)
926 {
927 MsWrdStruct::Section sec;
928 if (!getSection(type, id, sec)) return false;
929 if (!sec.m_paragraphId.isSet()) return false;
930 return getParagraph(StyleZone, *sec.m_paragraphId, para);
931 }
932
933 bool MsWrdTextStyles::getSectionFont(ZoneType type, int id, MsWrdStruct::Font &font)
934 {
935 MsWrdStruct::Section sec;
936 if (!getSection(type, id, sec)) return false;
937
938 if (!sec.m_paragraphId.isSet()) return false;
939 MsWrdStruct::Paragraph para(version());
940 if (!getParagraph(StyleZone, *sec.m_paragraphId, para))
941 return false;
942
943 if (para.m_font.isSet())
944 font = para.m_font.get();
945 else
946 return false;
947 return true;
948 }
949
950
951 bool MsWrdTextStyles::readSection(MsWrdEntry &entry, std::vector<long> &cLimits)
952 {
953 if (entry.length() < 14 || (entry.length()%10) != 4) {
954 MWAW_DEBUG_MSG(("MsWrdTextStyles::readSection: the zone size seems odd\n"));
955 return false;
956 }
957 long pos = entry.begin();
958 entry.setParsed(true);
959 MWAWInputStreamPtr &input= m_parserState->m_input;
960 input->seek(pos, librevenge::RVNG_SEEK_SET);
961 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
962 libmwaw::DebugStream f;
963 f << "Section:";
964 auto N=size_t(entry.length()/10);
965 cLimits.resize(N+1);
966 for (auto &limit : cLimits) limit = long(input->readULong(4));
967
968 MsWrdText::PLC plc(MsWrdText::PLC::Section);
969 auto &plcMap = m_textParser->getTextPLCMap();
970 long textLength = m_textParser->getMainTextLength();
971 for (size_t i = 0; i < N; i++) {
972 MsWrdStruct::Section sec;
973 sec.m_type = static_cast<int>(input->readULong(1)); // 0|40|80|C0
974 sec.m_flag = static_cast<int>(input->readULong(1)); // number between 2 and 7
975 sec.m_id = int(i);
976 unsigned long filePos = input->readULong(4);
977 if (textLength && cLimits[i] > textLength) {
978 MWAW_DEBUG_MSG(("MsWrdTextStyles::readSection: text positions is bad...\n"));
979 f << "#";
980 }
981 else {
982 plc.m_id = int(i);
983 plcMap.insert(std::multimap<long,MsWrdText::PLC>::value_type(cLimits[i],plc));
984 }
985 f << std::hex << "cPos=" << cLimits[i] << ":[" << sec << ",";
986 if (filePos != 0xFFFFFFFFL) {
987 f << "pos=" << std::hex << filePos << std::dec << ",";
988 long actPos = input->tell();
989 readSection(sec,long(filePos));
990 input->seek(actPos, librevenge::RVNG_SEEK_SET);
991 }
992 f << "],";
993
994 m_state->m_sectionList.push_back(sec);
995 }
996 ascFile.addPos(pos);
997 ascFile.addNote(f.str().c_str());
998
999 ascFile.addPos(entry.end());
1000 ascFile.addNote("_");
1001 return true;
1002 }
1003
1004 bool MsWrdTextStyles::readSection(MsWrdStruct::Section &sec, long debPos)
1005 {
1006 MWAWInputStreamPtr &input= m_parserState->m_input;
1007 if (!input->checkPosition(debPos)) {
1008 MWAW_DEBUG_MSG(("MsWrdTextStyles::readSection: can not find section data...\n"));
1009 return false;
1010 }
1011 int const vers = version();
1012 input->seek(debPos, librevenge::RVNG_SEEK_SET);
1013 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1014 libmwaw::DebugStream f;
1015 auto sz = static_cast<int>(input->readULong(1));
1016 long endPos = debPos+sz+1;
1017 if (sz < 1 || sz >= 255) {
1018 MWAW_DEBUG_MSG(("MsWrdTextStyles::readSection: data section size seems bad...\n"));
1019 f << "Section-" << sec.m_id.get() << ":#" << sec;
1020 ascFile.addPos(debPos);
1021 ascFile.addNote(f.str().c_str());
1022 return false;
1023 }
1024 while (input->tell() < endPos) {
1025 long pos = input->tell();
1026 bool ok;
1027 if (vers <= 3)
1028 ok = sec.readV3(input, endPos);
1029 else
1030 ok = sec.read(input, endPos);
1031 if (ok) continue;
1032 f << "#";
1033 ascFile.addDelimiter(pos,'|');
1034 break;
1035 }
1036 f << "Section-S" << sec.m_id.get() << ":" << sec;
1037 ascFile.addPos(debPos);
1038 ascFile.addNote(f.str().c_str());
1039
1040 ascFile.addPos(endPos);
1041 ascFile.addNote("_");
1042 return true;
1043 }
1044
1045 void MsWrdTextStyles::setProperty(MsWrdStruct::Section const &sec)
1046 {
1047 MWAWTextListenerPtr listener=m_parserState->m_textListener;
1048 if (!listener) return;
1049 if (listener->isHeaderFooterOpened()) {
1050 MWAW_DEBUG_MSG(("MsWrdTextStyles::setProperty: can not open a section in header/footer\n"));
1051 }
1052 else {
1053 int numCols = sec.m_col.get();
1054 int actCols = listener->getSection().numColumns();
1055 if (numCols >= 1 && actCols > 1 && sec.m_colBreak.get()) {
1056 if (!listener->isSectionOpened()) {
1057 MWAW_DEBUG_MSG(("MsWrdTextStyles::setProperty: section is not opened\n"));
1058 }
1059 else
1060 listener->insertBreak(MWAWTextListener::ColumnBreak);
1061 }
1062 else {
1063 if (listener->isSectionOpened())
1064 listener->closeSection();
1065 listener->openSection(sec.getSection(m_mainParser->getPageWidth()));
1066 }
1067 }
1068 }
1069
1070 bool MsWrdTextStyles::sendSection(int id, int textStructId)
1071 {
1072 if (!m_parserState->m_textListener) return true;
1073
1074 if (id < 0 || id >= int(m_state->m_sectionList.size())) {
1075 MWAW_DEBUG_MSG(("MsWrdTextStyles::sendText: can not find new section\n"));
1076 return false;
1077 }
1078 auto section=m_state->m_sectionList[size_t(id)];
1079 MsWrdStruct::Paragraph para(version());
1080 if (textStructId >= 0 &&
1081 getParagraph(MsWrdTextStyles::TextStructZone, textStructId, para) &&
1082 para.m_section.isSet()) {
1083 section.insert(*para.m_section);
1084 }
1085 setProperty(section);
1086 return true;
1087 }
1088
1089 ////////////////////////////////////////////////////////////
1090 // read the styles
1091 ////////////////////////////////////////////////////////////
1092 std::map<int,int> const &MsWrdTextStyles::getNextStyleMap() const
1093 {
1094 return m_state->m_nextStyleMap;
1095 }
1096
1097 bool MsWrdTextStyles::readStyles(MsWrdEntry &entry)
1098 {
1099 if (entry.length() < 6) {
1100 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStyles: zone seems to short...\n"));
1101 return false;
1102 }
1103 m_state->m_styleFontMap.clear();
1104 m_state->m_styleParagraphMap.clear();
1105 m_state->m_nextStyleMap.clear();
1106 entry.setParsed(true);
1107 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1108 ascFile.addPos(entry.end());
1109 ascFile.addNote("_");
1110
1111 long pos = entry.begin();
1112 libmwaw::DebugStream f;
1113 MWAWInputStreamPtr &input= m_parserState->m_input;
1114 input->seek(pos, librevenge::RVNG_SEEK_SET);
1115 f << entry << ":";
1116 auto N = static_cast<int>(input->readLong(2));
1117 if (N) f << "N?=" << N;
1118 ascFile.addPos(pos);
1119 ascFile.addNote(f.str().c_str());
1120
1121 // first find the different zone
1122 long debPos[4];
1123 int const overOk[3]= {0,30,100}; // name, font, paragraph
1124 for (int st = 0; st < 3; st++) {
1125 debPos[st] = input->tell();
1126 auto dataSz = static_cast<int>(input->readULong(2));
1127 long endPos = debPos[st]+dataSz;
1128 if (dataSz < 2+N || endPos > entry.end()+overOk[st]) {
1129 ascFile.addPos(pos);
1130 ascFile.addNote("###Styles(bad)");
1131 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStyles: can not read styles(%d)...\n", st));
1132 return false;
1133 }
1134 if (endPos > entry.end()) {
1135 entry.setEnd(endPos+1);
1136 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStyles(%d): size seems incoherent...\n", st));
1137 f.str("");
1138 f << "#sz=" << dataSz << ",";
1139 ascFile.addPos(debPos[st]);
1140 ascFile.addNote(f.str().c_str());
1141 }
1142 input->seek(endPos, librevenge::RVNG_SEEK_SET);
1143 }
1144 debPos[3] = input->tell();
1145 // read the styles parents
1146 std::vector<int> orig, order;
1147 if (readStylesHierarchy(entry, N, orig))
1148 order=orderStyles(orig);
1149
1150 int N1=0;
1151 MsWrdEntry zone;
1152 zone.setBegin(debPos[0]);
1153 zone.setEnd(debPos[1]);
1154 if (!readStylesNames(zone, N, N1)) {
1155 N1=int(orig.size())-N;
1156 if (N1 < 0)
1157 return false;
1158 }
1159 // ok, repair orig, and order if need
1160 if (int(orig.size()) < N+N1)
1161 orig.resize(size_t(N+N1), -1000);
1162 if (int(order.size()) < N+N1) {
1163 for (auto i = int(order.size()); i < N+N1; i++)
1164 order.push_back(i);
1165 }
1166 zone.setBegin(debPos[1]);
1167 zone.setEnd(debPos[2]);
1168 readStylesFont(zone, N, orig, order);
1169
1170 zone.setBegin(debPos[2]);
1171 zone.setEnd(debPos[3]);
1172 readStylesParagraph(zone, N, orig, order);
1173 return true;
1174 }
1175
1176 bool MsWrdTextStyles::readStylesNames(MsWrdEntry const &zone, int N, int &Nnamed)
1177 {
1178 long pos = zone.begin();
1179 MWAWInputStreamPtr &input= m_parserState->m_input;
1180 if (!zone.valid() || !input || !input->checkPosition(zone.end())) {
1181 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesNames: zone(names) seems to short...\n"));
1182 return false;
1183 }
1184 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1185 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1186 libmwaw::DebugStream f;
1187 f << "Styles(names):";
1188 int actN=0;
1189 while (long(input->tell()) < zone.end()) {
1190 auto sz = static_cast<int>(input->readULong(1));
1191 if (sz == 0) {
1192 f << "*";
1193 actN++;
1194 continue;
1195 }
1196 if (sz == 0xFF) {
1197 f << "_";
1198 actN++;
1199 continue;
1200 }
1201 pos = input->tell();
1202 if (pos+sz > zone.end()) {
1203 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesNames: zone(names) seems to short...\n"));
1204 f << "#";
1205 ascFile.addNote(f.str().c_str());
1206 input->seek(pos-1, librevenge::RVNG_SEEK_SET);
1207 break;
1208 }
1209 std::string s("");
1210 for (int i = 0; i < sz; i++) s += char(input->readULong(1));
1211 f << "N" << actN-N << "=" ;
1212 f << s << ",";
1213 actN++;
1214 }
1215 Nnamed=actN-N;
1216 if (Nnamed < 0) {
1217 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesNames: zone(names) seems to short: stop...\n"));
1218 f << "#";
1219 }
1220 ascFile.addPos(zone.begin());
1221 ascFile.addNote(f.str().c_str());
1222 return Nnamed >= 0;
1223 }
1224
1225 bool MsWrdTextStyles::readStylesFont
1226 (MsWrdEntry &zone, int N, std::vector<int> const &previous, std::vector<int> const &order)
1227 {
1228 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1229 libmwaw::DebugStream f;
1230 long pos = zone.begin();
1231 ascFile.addPos(pos);
1232 ascFile.addNote("Styles(font):");
1233
1234 MWAWInputStreamPtr &input= m_parserState->m_input;
1235 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1236 size_t numElt = order.size();
1237 std::vector<long> debPos;
1238 std::vector<int> dataSize;
1239 debPos.resize(numElt, 0);
1240 dataSize.resize(numElt, 0);
1241 for (size_t i = 0; i < numElt; i++) {
1242 pos = input->tell();
1243 debPos[i] = pos;
1244 int sz = dataSize[i] = static_cast<int>(input->readULong(1));
1245 if (sz == 0xFF)
1246 sz = 0;
1247 else if (pos+1+sz > zone.end()) {
1248 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesFont: can not read a font\n"));
1249 if (i == 0)
1250 return false;
1251 numElt = i-1;
1252 break;
1253 }
1254 if (sz)
1255 input->seek(sz, librevenge::RVNG_SEEK_CUR);
1256 else {
1257 f.str("");
1258 f << "CharPLC(sF" << int(i)-N << "):";
1259 ascFile.addPos(pos);
1260 ascFile.addNote(f.str().c_str());
1261 }
1262 }
1263
1264 for (auto id : order) {
1265 if (id < 0 || id >= int(numElt)) continue;
1266 int prevId = previous[size_t(id)];
1267 MsWrdStruct::Font font;
1268 // osnola:what is the difference between dataSize[(size_t)id]=0|0xFF
1269 if (prevId >= 0 && m_state->m_styleFontMap.find(prevId-N) != m_state->m_styleFontMap.end())
1270 font = m_state->m_styleFontMap.find(prevId-N)->second;
1271 if (dataSize[size_t(id)] && dataSize[size_t(id)] != 0xFF) {
1272 input->seek(debPos[size_t(id)], librevenge::RVNG_SEEK_SET);
1273
1274 f.str("");
1275 f << "CharPLC(sF" << id-int(N) << "):";
1276 if (!readFont(font, StyleZone)) f << "#";
1277 else if (id==int(N)) m_state->m_defaultFont=font.m_font.get();
1278 f << "font=[" << font.m_font->getDebugString(m_parserState->m_fontConverter) << font << "],";
1279 ascFile.addPos(debPos[size_t(id)]);
1280 ascFile.addNote(f.str().c_str());
1281 }
1282 m_state->m_styleFontMap.insert(std::map<int,MsWrdStruct::Font>::value_type(id-N,font));
1283 }
1284 return true;
1285 }
1286
1287 bool MsWrdTextStyles::readStylesParagraph(MsWrdEntry &zone, int N, std::vector<int> const &previous,
1288 std::vector<int> const &order)
1289 {
1290 int const vers=version();
1291 int minSz = vers <= 3 ? 3 : 7;
1292 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1293 libmwaw::DebugStream f;
1294 long pos = zone.begin();
1295 ascFile.addPos(pos);
1296 ascFile.addNote("Styles(paragraph):");
1297
1298 MWAWInputStreamPtr &input= m_parserState->m_input;
1299 input->seek(pos+2, librevenge::RVNG_SEEK_SET);
1300 size_t numElt = order.size();
1301 std::vector<long> debPos;
1302 std::vector<int> dataSize;
1303 debPos.resize(numElt, 0);
1304 dataSize.resize(numElt, 0);
1305 for (size_t i = 0; i < numElt; i++) {
1306 pos = input->tell();
1307 debPos[i] = pos;
1308 int sz = dataSize[i] = static_cast<int>(input->readULong(1));
1309 if (sz != 0xFF && pos+1+sz > zone.end()) {
1310 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesParagraph: can not read a paragraph\n"));
1311 if (i == 0)
1312 return false;
1313 numElt = i-1;
1314 break;
1315 }
1316 if (sz && sz != 0xFF)
1317 input->seek(sz, librevenge::RVNG_SEEK_CUR);
1318 else {
1319 f.str("");
1320 f << "ParagPLC(sP" << int(i)-N << "):";
1321 ascFile.addPos(pos);
1322 ascFile.addNote(f.str().c_str());
1323 }
1324 }
1325 for (auto id : order) {
1326 if (id < 0 || id >= int(numElt)) continue;
1327 int prevId = previous[size_t(id)];
1328 MsWrdStruct::Paragraph para(vers);
1329 if (prevId >= 0 && m_state->m_styleParagraphMap.find(prevId-N) != m_state->m_styleParagraphMap.end())
1330 para = m_state->m_styleParagraphMap.find(prevId-N)->second;
1331 /** osnola: update the font style here or after reading data ? */
1332 if (m_state->m_styleFontMap.find(id-N) != m_state->m_styleFontMap.end())
1333 para.m_font = m_state->m_styleFontMap.find(id-N)->second;
1334 if (dataSize[size_t(id)] != 0xFF) {
1335 f.str("");
1336 f << "ParagPLC(sP" << id-N << "):";
1337 if (dataSize[size_t(id)] < minSz) {
1338 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesParagraph: zone(paragraph) the id seems bad...\n"));
1339 f << "#";
1340 }
1341 else {
1342 input->seek(debPos[size_t(id)]+1, librevenge::RVNG_SEEK_SET);
1343 auto pId = static_cast<int>(input->readLong(1));
1344 if (id >= N && pId != id-N) {
1345 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesParagraph: zone(paragraph) the id seems bad...\n"));
1346 f << "#id=" << pId << ",";
1347 }
1348 auto val = static_cast<int>(input->readLong(2)); // always 0?
1349 if (val) f << "g0=" << val << ",";
1350
1351 if (vers > 3) {
1352 for (int j = 1; j < 3; j++) { // 0|90|c0,0|1
1353 val = static_cast<int>(input->readLong(2));
1354 if (val) f << "g" << j << "=" << std::hex << val << std::dec << ",";
1355 }
1356 }
1357
1358 if (dataSize[size_t(id)] != minSz && !readParagraph(para, dataSize[size_t(id)]-minSz))
1359 f << "#";
1360 #ifdef DEBUG_WITH_FILES
1361 para.print(f, m_parserState->m_fontConverter);
1362 #endif
1363 }
1364 ascFile.addPos(debPos[size_t(id)]);
1365 ascFile.addNote(f.str().c_str());
1366 }
1367 para.m_modFont.setSet(false);
1368 m_state->m_styleParagraphMap.insert
1369 (std::map<int,MsWrdStruct::Paragraph>::value_type(id-N,para));
1370 }
1371 return true;
1372 }
1373
1374 bool MsWrdTextStyles::readStylesHierarchy(MsWrdEntry &entry, int N, std::vector<int> &orig)
1375 {
1376 MWAWInputStreamPtr &input= m_parserState->m_input;
1377 long pos = input->tell();
1378 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1379 libmwaw::DebugStream f;
1380 f << "Styles(hierarchy):";
1381
1382 auto N2 = static_cast<int>(input->readULong(2));
1383 if (N2 < N) {
1384 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesHierarchy: N seems too small...\n"));
1385 f << "#N=" << N2 << ",";
1386 ascFile.addPos(pos);
1387 ascFile.addNote("Styles(hierarchy):#"); // big problem
1388 return false;
1389 }
1390 if (pos+(N2+1)*2 > entry.end()) {
1391 if (N2>40) {
1392 MWAW_DEBUG_MSG(("MsWrdTextStyles::readStylesHierarchy: N seems very big...\n"));
1393 ascFile.addPos(pos);
1394 ascFile.addNote("Styles(hierarchy):#"); // big problem
1395 }
1396 f << "#";
1397 }
1398 orig.resize(0);
1399 orig.resize(size_t(N2), -1000);
1400 for (int i = 0; i < N2; i++) {
1401 auto v0 = static_cast<int>(input->readLong(1)); // often 0 or i-N
1402 auto v1 = static_cast<int>(input->readLong(1));
1403 f << "prev(sP"<< i-N << ")";
1404 if (v1 == -34) {
1405 }
1406 else if (v1 < -N || v1+N >= N2)
1407 f << "=###" << v1;
1408 else {
1409 orig[size_t(i)] = v1+N;
1410 f << "=sP" << v1;
1411 }
1412 if (v0 < -N || v0+N >= N2) {
1413 f << "[###next" << v0 << "]";
1414 m_state->m_nextStyleMap[i-N]=i-N;
1415 }
1416 else {
1417 m_state->m_nextStyleMap[i-N]=v0;
1418 if (v0==i-N)
1419 f << "*";
1420 else if (v0)
1421 f << "[next" << v0 << "]";
1422 }
1423 f << ",";
1424 }
1425
1426 ascFile.addPos(pos);
1427 ascFile.addNote(f.str().c_str());
1428
1429 pos = input->tell();
1430 if (pos < entry.end()) {
1431 ascFile.addPos(pos);
1432 ascFile.addNote("_");
1433 }
1434 else if (pos > entry.end())
1435 entry.setEnd(pos);
1436
1437 return true;
1438 }
1439
1440 std::vector<int> MsWrdTextStyles::orderStyles(std::vector<int> const &previous)
1441 {
1442 std::vector<int> order, numChild;
1443 size_t N = previous.size();
1444 numChild.resize(N, 0);
1445 for (auto id : previous) {
1446 if (id == -1000) continue;
1447 if (id < 0 || id >= int(N)) {
1448 MWAW_DEBUG_MSG(("MsWrdTextStyles::orderStyles: find a bad previous %d\n", id));
1449 continue;
1450 }
1451 numChild[size_t(id)]++;
1452 }
1453 order.resize(N);
1454 size_t numElt = 0;
1455 while (numElt < N) {
1456 bool read = false;
1457 for (size_t i = 0; i < N; i++) {
1458 if (numChild[i]) continue;
1459 order[N-(++numElt)]=int(i);
1460 if (previous[i] >= 0 && previous[i] < int(N))
1461 numChild[size_t(previous[i])]--;
1462 read = true;
1463 numChild[i]=-1;
1464 }
1465 if (read) continue;
1466 MWAW_DEBUG_MSG(("MsWrdTextStyles::orderStyles: find a loop, stop...\n"));
1467 for (size_t i = 0; i < N; i++) {
1468 if (numChild[i] != -1)
1469 order[N-(++numElt)]=int(i);
1470 }
1471 break;
1472 }
1473 return order;
1474 }
1475
1476 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: