Debian Codegraph @

src/lib/WPS4Text.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * Major Contributor(s):
10 * Copyright (C) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 #ifdef DEBUG_WITH_FILES
27 // set to 1 to debug the font property
28 # define DEBUG_FP 1
29 // set to 1 to debug the paragraph property
30 # define DEBUG_PP 1
31 // set to 1 to print the plc position
32 # define DEBUG_PLC_POS 1
33 #else
34 # define DEBUG_FP 0
35 # define DEBUG_PP 0
36 # define DEBUG_PLC_POS 0
37 #endif
38
39 #include <iomanip>
40 #include <iostream>
41
42 #include <map>
43 #include <vector>
44
45 #include <librevenge/librevenge.h>
46
47 #include "libwps_internal.h"
48 #include "libwps_tools_win.h"
49
50 #include "WPSContentListener.h"
51 #include "WPSFont.h"
52 #include "WPSPosition.h"
53 #include "WPSParagraph.h"
54
55 #include "WPS4.h"
56
57 #include "WPS4Text.h"
58
59 /** Internal and low level: the structures of a WPS4Text used to parse PLC*/
60 namespace WPS4PLCInternal
61 {
62 /** Internal and low level: the PLC different types and their structures */
63 struct PLC;
64
65 //! a map of known plc
66 struct KnownPLC
67 {
68 public:
69 //! constructor
70 KnownPLC();
71 //! destructor
72 ~KnownPLC();
73 //! returns the PLC corresponding to a name
74 PLC get(std::string const &name);
75 protected:
76 //! creates the map of known PLC
77 void createMapping();
78 //! map name -> known PLC
79 std::map<std::string, PLC> m_knowns;
80 };
81 }
82
83 //////////////////////////////////////////////////////////////////////////////
84 // general enum
85 //////////////////////////////////////////////////////////////////////////////
86 namespace WPS4TextInternal
87 {
88 /** a enum used to type a zone */
89 enum ZoneType { Z_String=-1, Z_Header=0, Z_Footer=1, Z_Main=2, Z_Note, Z_Bookmark, Z_DLink, Z_Unknown};
90 /** Internal: class to store a font name: name with encoding type */
91 struct FontName
92 {
93 //! constructor with file's version to define the default encoding */
94 explicit FontName(libwps_tools_win::Font::Type type)
95 : m_name("")
96 , m_type(type)
97 {
98 }
99 //! operator<<
100 friend std::ostream &operator<<(std::ostream &o, FontName const &ft);
101 /** returns the default dos name corresponding to \a id th font */
102 static librevenge::RVNGString getDosName(int id);
103
104 //! font name
105 librevenge::RVNGString m_name;
106 //! font encoding type
107 libwps_tools_win::Font::Type m_type;
108 };
109 //! operator<< for a font name
110 std::ostream &operator<<(std::ostream &o, FontName const &ft)
111 {
112 if (!ft.m_name.empty()) o << "name='" << ft.m_name.cstr() << "'";
113 else o << "name='Unknown'";
114 if (ft.m_type!=libwps_tools_win::Font::WIN3_WEUROPE &&
115 ft.m_type!=libwps_tools_win::Font::DOS_850)
116 o << ",type=" << libwps_tools_win::Font::getTypeName(ft.m_type).cstr() << ",";
117 return o;
118 }
119
120 librevenge::RVNGString FontName::getDosName(int id)
121 {
122 switch (id)
123 {
124 case 0:
125 return "Courier";
126 case 1:
127 return "Courier PC";
128 case 3:
129 return "Univers_Scale";
130 case 4:
131 return "Universe";
132 case 6:
133 return "LinePrinterPC";
134 case 7:
135 return "LinePrinter";
136 case 16:
137 return "CGTimes_Scale";
138 case 24:
139 return "CGTimes";
140 default:
141 break;
142 }
143
144 WPS_DEBUG_MSG(("WPS4TextInternal::FontName::getDosName: encountered unknown font %i\n", id));
145 return "Courier";
146 }
147 /** Internal: class to store font properties */
148 struct Font final : public WPSFont
149 {
150 //! constructor with file's version to define the default encoding */
151 explicit Font(libwps_tools_win::Font::Type type)
152 : WPSFont()
153 , m_type(type)
154 , m_backColor(WPSColor::white())
155 , m_special(false)
156 , m_dlinkId(-1)
157 {
158 }
159 Font(Font const &)=default;
160 Font &operator=(Font const &)=default;
161 //! destructor
162 ~Font() final;
163 //! returns a default font (Courier12) with file's version to define the default encoding */
164 static Font getDefault(libwps_tools_win::Font::Type type, int version)
165 {
166 Font res(type);
167 if (version <= 2)
168 res.m_name="Courier";
169 else
170 res.m_name="Times New Roman";
171 res.m_size=12;
172 return res;
173 }
174
175 //! operator<<
176 friend std::ostream &operator<<(std::ostream &o, Font const &ft);
177
178 //! the font encoding type
179 libwps_tools_win::Font::Type m_type;
180 //! background color index
181 WPSColor m_backColor;
182 //! a flag to know if we have a special field (a note), ...
183 bool m_special;
184 //! a id to retrieve a file name ( dos )
185 int m_dlinkId;
186 };
187
188 Font::~Font()
189 {
190 }
191
192 //! operator<< for font properties
193 std::ostream &operator<<(std::ostream &o, Font const &ft)
194 {
195 o << static_cast<WPSFont const &>(ft) << ",";
196
197 if (ft.m_special)
198 {
199 if (ft.m_dlinkId >= 0)
200 o << "spec[" << ft.m_dlinkId << "],";
201 else
202 o << "spec,";
203 }
204
205 if (!ft.m_backColor.isWhite())
206 o << "bgCol=" << ft.m_backColor << ",";
207 return o;
208 }
209
210 /** Internal: class to store an note type */
211 struct Note final : public WPSEntry
212 {
213 //! constructor
214 Note()
215 : WPSEntry()
216 , m_label("")
217 , m_error("") {}
218 Note(Note const &)=default;
219 Note &operator=(Note const &)=default;
220 //! destructor
221 ~Note() final;
222 bool isNumeric() const
223 {
224 return m_label.len()==0;
225 }
226 //! operator <<
227 friend std::ostream &operator<<(std::ostream &o, Note const &note)
228 {
229 if (note.m_label.len())
230 o << "lab=" << note.m_label.cstr() << ",";
231 else
232 o << "numeric,";
233 if (!note.m_error.empty()) o << note.m_error << ",";
234 return o;
235 }
236 //! the label if not numeric
237 librevenge::RVNGString m_label;
238 //! a string used to store the parsing errors
239 std::string m_error;
240 };
241
242 Note::~Note()
243 {
244 }
245 /** Internal: class to store an object definition */
246 struct Object
247 {
248 //! constructor
249 Object()
250 : m_id(-1)
251 , m_page(0)
252 , m_origin()
253 , m_size()
254 , m_pos()
255 , m_unknown(0)
256 , m_extra("") {}
257 //! operator <<
258 friend std::ostream &operator<<(std::ostream &o, Object const &obj);
259
260 //! the object identificator
261 int m_id;
262 //! the page
263 int m_page;
264 //! the origin
265 Vec2f m_origin;
266 //! the object size in the document
267 Vec2f m_size;
268 //! an entry which indicates where the object is defined in the file
269 WPSEntry m_pos;
270 //! unknown data
271 long m_unknown;
272 //! a string used to store the parsing errors
273 std::string m_extra;
274 };
275 //! operator<< for an object
276 std::ostream &operator<<(std::ostream &o, Object const &obj)
277 {
278 if (obj.m_id > -1) o << "ole" << obj.m_id << ",";
279 if (obj.m_page) o << "page=" << obj.m_page << ",";
280 if (obj.m_origin!=Vec2f(0,0)) o << "orig=" << obj.m_origin << ",";
281 o <<"size=" << obj.m_size << ",";
282 if (obj.m_pos.valid()) o << std::hex << "def=(0x" << obj.m_pos.begin() << "->" << obj.m_pos.end() << ")," << std::dec;
283 if (obj.m_unknown) o << std::hex << "unkn=" << obj.m_unknown << std::dec << ",";
284 if (!obj.m_extra.empty()) o << obj.m_extra;
285 return o;
286 }
287
288 /** Internal: class to store an object definition */
289 struct DosLink
290 {
291 //! constructor
292 DosLink()
293 : m_type(-1)
294 , m_width(-1)
295 , m_size()
296 , m_name("")
297 , m_pos()
298 , m_extra("") {}
299 //! operator <<
300 friend std::ostream &operator<<(std::ostream &o, DosLink const &dlink);
301
302 //! the type
303 int m_type;
304 //! the width
305 float m_width;
306 //! the object size in the document
307 Vec2f m_size;
308 //! the file name
309 std::string m_name;
310 //! an entry which indicates where the object is defined in the file
311 WPSEntry m_pos;
312 //! a string used to store the parsing errors
313 std::string m_extra;
314 };
315 //! operator<< for an object
316 std::ostream &operator<<(std::ostream &o, DosLink const &dlink)
317 {
318 switch (dlink.m_type)
319 {
320 case -1:
321 break;
322 case 1:
323 o << "chart,";
324 break;
325 case 0x81:
326 o << "pict,";
327 break;
328 case 0x40:
329 o << "spreadsheet,";
330 break;
331 default:
332 o << "#type=" << dlink.m_type << ",";
333 break;
334 }
335 if (dlink.m_width >= 0) o << "width?=" << dlink.m_width << ",";
336 if (dlink.m_size.x() >= 0 && (dlink.m_size.y()<0 || dlink.m_size.y()>0))
337 o <<"size=" << dlink.m_size << ",";
338 if (dlink.m_name.length()) o << "name='" << dlink.m_name << "',";
339 if (!dlink.m_extra.empty()) o << ", err=" << dlink.m_extra;
340 return o;
341 }
342
343 /** Internal: class to store a date/time format */
344 struct DateTime
345 {
346 //! constructor
347 DateTime()
348 : m_type(-1)
349 , m_extra("") {}
350 //! returns a format to used with strftime
351 std::string format() const;
352 //! operator <<
353 friend std::ostream &operator<<(std::ostream &o, DateTime const &dtime);
354
355 //! the type
356 int m_type;
357 //! a string used to store the parsing errors
358 std::string m_extra;
359 };
360
361 std::string DateTime::format() const
362 {
363 switch (m_type)
364 {
365 case 0:
366 return "%m/%d/%Y";
367 case 1:
368 return "%m/%Y";
369 case 2:
370 return "%d %B %Y";
371 case 3:
372 return "%A %d %B %Y";
373 case 4:
374 return "%B %Y";
375 case 5:
376 return "%m/%d/%Y %I:%M";
377 case 6:
378 return "%m/%d/%Y %I:%M:%S";
379 case 7:
380 return "%I:%M:%S";
381 case 8:
382 return "%I:%M";
383 case 9:
384 return "%H:%M:%S";
385 case 10:
386 return "%H:%M";
387 default:
388 break;
389 }
390 return "";
391 }
392
393 //! operator<< for an object
394 std::ostream &operator<<(std::ostream &o, DateTime const &dtime)
395 {
396 switch (dtime.m_type)
397 {
398 case -1:
399 break;
400 case 0:
401 case 1:
402 case 2:
403 case 3:
404 case 4:
405 o << "date[F"<<dtime.m_type<<"],";
406 break;
407 case 5:
408 case 6:
409 o << "date&time[F"<<dtime.m_type-5<<"],";
410 break;
411 case 7:
412 case 8:
413 case 9:
414 case 10:
415 o << "time[F"<<dtime.m_type-7<<"],";
416 break;
417 default:
418 o << "#type=" << dtime.m_type << ",";
419 break;
420 }
421 if (!dtime.m_extra.empty()) o << ", err=" << dtime.m_extra;
422 return o;
423 }
424
425 /** different types
426 *
427 * - BTE: font/paragraph properties
428 * - OBJECT: object properties
429 * - FTNp, FTNd: footnote position in text and footnote content
430 * - BKMK: comment field
431 * - DTTM: field type: date/time/..
432 */
433 enum PLCType { BTE=0, OBJECT, FTNp, FTNd, BKMK, DTTM, Unknown};
434
435 /** Internal: class to store the PLC: Pointer List Content ? */
436 struct DataPLC
437 {
438 //! constructor
439 DataPLC()
440 : m_name("")
441 , m_type(Unknown)
442 , m_value(-1)
443 , m_extra() {}
444 //! operator<<
445 friend std::ostream &operator<<(std::ostream &o, DataPLC const &plc);
446 //! the entry field name
447 std::string m_name;
448 //! the plc type
449 PLCType m_type;
450 //! a potential value
451 long m_value;
452 //! a string used to store the parsing errors
453 std::string m_extra;
454 };
455 //! operator<< for a dataPLC
456 std::ostream &operator<<(std::ostream &o, DataPLC const &plc)
457 {
458 o << "type=" << plc.m_name << ",";
459 if (plc.m_value != -1) o << "val=" << std::hex << plc.m_value << std::dec << ", ";
460 if (!plc.m_extra.empty()) o << "errors=(" << plc.m_extra << ")";
461 return o;
462 }
463
464 /** Internal: the state of a WPS4Text */
465 struct State
466 {
467 //! constructor
468 State()
469 : m_fontNames()
470 , m_fontList()
471 , m_paragraphList()
472 , m_FDPCs()
473 , m_FDPPs()
474 , m_footnoteList()
475 , m_footnoteMap()
476 , m_bookmarkMap()
477 , m_dosLinkList()
478 , m_main()
479 , m_header()
480 , m_footer()
481 , m_otherZones()
482 , m_objectMap()
483 , m_dateTimeMap()
484 , m_plcList()
485 , m_knownPLC()
486 {}
487
488 //! the list of fonts names
489 std::map<int,FontName> m_fontNames;
490 //! the list of all font properties
491 std::vector<Font> m_fontList;
492 //! the list of all paragraph properties
493 std::vector<WPSParagraph> m_paragraphList;
494
495 //! the list of FDPC entries (ie list to find the font properties lists )
496 std::vector<WPSEntry> m_FDPCs;
497 //! the list of FDPP entries (ie list to find the paragraph properties lists )
498 std::vector<WPSEntry> m_FDPPs;
499
500 //! the footnote entries
501 std::vector<Note> m_footnoteList;
502 //! map: footnote in text -> footnote entry
503 std::map<long,Note const *> m_footnoteMap;
504 //! map: bookmark in text -> bookmark
505 std::map<long, WPSEntry> m_bookmarkMap;
506 //! the dos file links
507 std::vector<DosLink> m_dosLinkList;
508
509 WPSEntry m_main /** the main text zone entry*/,
510 m_header /** the header text entry*/, m_footer /** the footer text entry*/;
511
512 //! the entries which are not in main/header/footer text and in the footnotes
513 std::vector<WPSEntry> m_otherZones;
514 //! map: object in text -> object
515 std::map<long, Object> m_objectMap;
516 //! map: date field in text -> date time format
517 std::map<long, DateTime> m_dateTimeMap;
518 //! a list of all PLCs
519 std::vector<DataPLC> m_plcList;
520 //! the known plc
521 WPS4PLCInternal::KnownPLC m_knownPLC;
522 };
523 }
524
525 //////////////////////////////////////////////////////////////////////////////
526 //
527 // MAIN CODE
528 //
529 //////////////////////////////////////////////////////////////////////////////
530
531 // constructor/destructor
532 WPS4Text::WPS4Text(WPS4Parser &parser, RVNGInputStreamPtr &input)
533 : WPSTextParser(parser, input)
534 , m_listener()
535 , m_state()
536 {
537 m_state.reset(new WPS4TextInternal::State);
538 }
539
540 WPS4Text::~WPS4Text()
541 {
542 }
543
544 // number of page
545 int WPS4Text::numPages() const
546 {
547 int numPage = 1;
548 m_input->seek(m_textPositions.begin(), librevenge::RVNG_SEEK_SET);
549 while (!m_input->isEnd() && m_input->tell() != m_textPositions.end())
550 {
551 if (libwps::readU8(m_input.get()) == 0x0C) numPage++;
552 }
553 for (auto const &it : m_state->m_objectMap)
554 {
555 if (it.second.m_page<=numPage) continue;
556 if (it.second.m_page<=numPage+10)
557 numPage=it.second.m_page;
558 else
559 {
560 WPS_DEBUG_MSG(("WPS4Text::numPages: the number of pages seems bad\n"));
561 }
562 }
563 return numPage;
564 }
565
566 // page object
567 void WPS4Text::sendObjects(int page)
568 {
569 for (auto const &it : m_state->m_objectMap)
570 {
571 auto const &obj=it.second;
572 if (obj.m_page<=0 || obj.m_id<0) continue;
573 if (page>=0 && obj.m_page!=page+1) continue;
574 WPSPosition position(obj.m_origin, obj.m_size);
575 if (page<0) position.setPage(obj.m_page);
576 position.setRelativePosition(WPSPosition::Page);
577 position.m_wrapping = WPSPosition::WDynamic;
578 mainParser().sendObject(position, obj.m_id);
579 }
580 }
581
582 // return main/header/footer/all entry
583 WPSEntry WPS4Text::getHeaderEntry() const
584 {
585 if (m_state->m_header.valid()) return m_state->m_header;
586 auto const &nameMultiMap = getNameEntryMap();
587 auto pos = nameMultiMap.find("SHdr");
588 if (pos == nameMultiMap.end()) return WPSEntry();
589 WPSEntry res = pos->second;
590 res.setType("TEXT");
591 res.setId(WPS4TextInternal::Z_String);
592 return res;
593 }
594
595 WPSEntry WPS4Text::getFooterEntry() const
596 {
597 if (m_state->m_footer.valid()) return m_state->m_footer;
598 auto const &nameMultiMap = getNameEntryMap();
599 auto pos = nameMultiMap.find("SFtr");
600 if (pos == nameMultiMap.end()) return WPSEntry();
601 WPSEntry res = pos->second;
602 res.setType("TEXT");
603 res.setId(WPS4TextInternal::Z_String);
604 return res;
605 }
606
607 WPSEntry WPS4Text::getMainTextEntry() const
608 {
609 return m_state->m_main;
610 }
611
612 WPS4TextInternal::Font WPS4Text::getDefaultFont() const
613 {
614 return WPS4TextInternal::Font::getDefault(mainParser().getDefaultFontType(), version());
615 }
616
617 ////////////////////////////////////////////////////////////
618 // send the data
619 ////////////////////////////////////////////////////////////
620 void WPS4Text::flushExtra()
621 {
622 if (!m_listener)
623 {
624 WPS_DEBUG_MSG(("WPS4Text::flushExtra can not find the listener\n"));
625 return;
626 }
627 size_t numExtra = m_state->m_otherZones.size();
628 if (numExtra == 0) return;
629
630 m_listener->setFont(getDefaultFont());
631 m_listener->setParagraph(WPSParagraph());
632 m_listener->insertEOL();
633 #ifdef DEBUG
634 librevenge::RVNGString message = "--------- extra text zone -------- ";
635 m_listener->insertUnicodeString(message);
636 #endif
637 for (size_t i = 0; i < numExtra; ++i)
638 readText(m_state->m_otherZones[i]);
639 }
640
641 bool WPS4Text::readText(WPSEntry const &zone)
642 {
643 bool bookmark = zone.id() == WPS4TextInternal::Z_Bookmark;
644 bool dlink = zone.id() == WPS4TextInternal::Z_DLink;
645 bool simpleString = zone.id() == WPS4TextInternal::Z_String || bookmark || dlink;
646 bool mainZone = zone.id() == WPS4TextInternal::Z_Main;
647
648 if (m_listener.get() == nullptr)
649 {
650 WPS_DEBUG_MSG(("WPS4Text::readText can not find the listener\n"));
651 return false;
652 }
653 if (!zone.valid())
654 {
655 WPS_DEBUG_MSG(("WPS4Text::readText invalid zone, must not happen\n"));
656 m_listener->insertCharacter(' ');
657 return false;
658 }
659 if (mainZone)
660 {
661 int numCols = mainParser().numColumns();
662 if (numCols > 1)
663 {
664 if (m_listener->isSectionOpened())
665 {
666 WPS_DEBUG_MSG(("WPS4Text::readText the section is already open\n"));
667 }
668 else
669 {
670 int w = int(72*mainParser().pageWidth())/numCols;
671 std::vector<int> width;
672 width.resize(size_t(numCols), w);
673 m_listener->openSection(width,librevenge::RVNG_POINT);
674 }
675 }
676 }
677 auto FODs_iter = m_FODList.begin();
678
679 // update the property to correspond to the text
680 int prevFId = -1, prevPId = -1;
681 if (simpleString) FODs_iter = m_FODList.end();
682 else if (FODs_iter == m_FODList.end() && mainZone)
683 {
684 WPS_DEBUG_MSG(("WPS4Text::readText: CAN NOT FIND any FODs for main zone, REVERT to basic string!!!!!!!!!\n"));
685 simpleString = true;
686 }
687
688 for (; FODs_iter!= m_FODList.end(); ++FODs_iter)
689 {
690 auto const &fod = *(FODs_iter);
691 if (fod.m_pos >= zone.begin()) break;
692
693 int id = (*FODs_iter).m_id;
694 if (fod.m_type == DataFOD::ATTR_TEXT) prevFId = id;
695 else if (fod.m_type == DataFOD::ATTR_PARAG) prevPId = id;
696 }
697
698 WPS4TextInternal::Font defaultFont(getDefaultFont());
699 WPS4TextInternal::Font actFont(defaultFont);
700 if (prevFId != -1)
701 actFont = m_state->m_fontList[size_t(prevFId)];
702 m_listener->setFont(actFont);
703
704 if (prevPId != -1)
705 m_listener->setParagraph(m_state->m_paragraphList[size_t(prevPId)]);
706 else
707 m_listener->setParagraph(WPSParagraph());
708
709 if (dlink)
710 {
711 m_listener->insertUnicodeString("include ");
712 m_listener->insertUnicode(0x226a);
713 }
714 bool first = true;
715 int actPage = 1;
716 for (; simpleString || FODs_iter!= m_FODList.end(); ++FODs_iter)
717 {
718 long actPos;
719 long lastPos;
720
721
722 libwps::DebugStream f;
723 f << "Text";
724
725 if (simpleString)
726 {
727 actPos = zone.begin();
728 lastPos = zone.end();
729 }
730 else
731 {
732 auto fod = *(FODs_iter);
733 actPos = first ? zone.begin() : fod.m_pos;
734 if (long(actPos) >= zone.end()) break;
735 first = false;
736
737 if (++FODs_iter!= m_FODList.end())
738 {
739 lastPos = (*FODs_iter).m_pos;
740 if (long(lastPos) >= zone.end()) lastPos = zone.end();
741 }
742 else
743 lastPos = zone.end();
744 --FODs_iter;
745 int fId = fod.m_id;
746 switch (fod.m_type)
747 {
748 case DataFOD::ATTR_TEXT:
749 if (fId >= 0)
750 actFont = m_state->m_fontList[size_t(fId)];
751 else
752 actFont = defaultFont;
753 m_listener->setFont(actFont);
754 #if DEBUG_FP
755 f << "[";
756 if (fId >= 0) f << "C" << fId << ":" << actFont << "]";
757 else f << "_]";
758 #endif
759 break;
760 case DataFOD::ATTR_PARAG:
761 if (fId >= 0)
762 m_listener->setParagraph(m_state->m_paragraphList[size_t(fId)]);
763 else
764 m_listener->setParagraph(WPSParagraph());
765 #if DEBUG_PP
766 f << "[";
767 if (fId >= 0) f << "P" << fId << ":" << m_state->m_paragraphList[size_t(fId)] << "]";
768 else f << "_]";
769 #endif
770 break;
771 case DataFOD::ATTR_PLC:
772 if (fId >= 0 && m_state->m_plcList[size_t(fId)].m_type == WPS4TextInternal::BKMK)
773 {
774 WPSEntry bkmk;
775 if (m_state->m_bookmarkMap.find(actPos) == m_state->m_bookmarkMap.end())
776 {
777 WPS_DEBUG_MSG(("WPS4Text::readText: can not find the bookmark entry\n"));
778 }
779 else
780 bkmk = m_state->m_bookmarkMap.find(actPos)->second;
781 bkmk.setType("TEXT");
782 bkmk.setId(WPS4TextInternal::Z_Bookmark);
783 mainParser().createDocument(bkmk, libwps::DOC_COMMENT_ANNOTATION);
784 }
785 #if DEBUG_PLC_POS
786 f << "[PLC";
787 if (fId>= 0) f << m_state->m_plcList[size_t(fId)] << "]";
788 else f << "_]";
789 #endif
790 break;
791 case DataFOD::ATTR_UNKN:
792 default:
793 WPS_DEBUG_MSG(("WPS4Text::readText: find unknown plc\n"));
794 #if DEBUG_PLC_POS
795 f << "[DataFOD(###Unknown)]";
796 #endif
797 break;
798 }
799 }
800 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
801 std::string chaine("");
802 long len = lastPos-actPos;
803 std::string text;
804 for (long i = len; i>=0; i--)
805 {
806 long pos = m_input->tell();
807 uint8_t readVal = i==0 ? uint8_t('\0') : libwps::readU8(m_input);
808 bool isSimpleField=false;
809 if (readVal=='&' && simpleString && pos+2 <= lastPos)
810 {
811 int nextVal = libwps::readU8(m_input);
812 m_input->seek(-1, librevenge::RVNG_SEEK_CUR);
813 switch (nextVal) // check me
814 {
815 case 'p':
816 case 'P':
817 case 'd':
818 case 'D':
819 case 't':
820 case 'T':
821 case 'f':
822 case 'F':
823 isSimpleField=true;
824 break;
825 default:
826 break;
827 }
828 }
829 if ((readVal<0x10 || readVal==0x11 || readVal==0x12 || readVal==0x1f || (version()<=2 && readVal==0xca) || isSimpleField) && !text.empty())
830 {
831 m_listener->insertUnicodeString(libwps_tools_win::Font::unicodeString(text, actFont.m_type));
832 text.clear();
833 }
834 if (0x00 == readVal)
835 {
836 if (i > 1)
837 {
838 WPS_DEBUG_MSG(("WPS4Text::readText: find some unexpected 0 character\n"));
839 // probably an error, but we can ignore id
840 chaine += '#';
841 }
842 continue;
843 }
844 chaine += char(readVal);
845 switch (readVal)
846 {
847 case 0x01: // chart ?
848 case 0x08: // spreadsheet range
849 case 0x0e: // picture
850 {
851 if (!actFont.m_special || m_state->m_dosLinkList.empty() || actFont.m_dlinkId >= int(m_state->m_dosLinkList.size()))
852 {
853 WPS_DEBUG_MSG(("WPS4Text::readText: send DLINK can not find id\n"));
854 break;
855 }
856 int id = actFont.m_dlinkId >= 0 ? actFont.m_dlinkId : 0;
857 WPSEntry ent = m_state->m_dosLinkList[size_t(id)].m_pos;
858 ent.setType("TEXT");
859 ent.setId(WPS4TextInternal::Z_DLink);
860 WPSPosition pos_(Vec2f(),Vec2f(3.0f,0.2f));
861 pos_.setRelativePosition(WPSPosition::Paragraph, WPSPosition::XCenter);
862 pos_.m_wrapping = WPSPosition::WNone;
863 librevenge::RVNGPropertyList extras;
864 mainParser().createTextBox(ent, pos_, extras);
865 m_listener->insertEOL();
866 break;
867 }
868 case 0x02:
869 m_listener->insertField(WPSField(WPSField::PageNumber));
870 break;
871 case 0x03:
872 m_listener->insertField(WPSField(WPSField::Date));
873 break;
874 case 0x04:
875 m_listener->insertField(WPSField(WPSField::Time));
876 break;
877 case 0x05:
878 m_listener->insertField(WPSField(WPSField::Title));
879 break;
880 case 0x07:
881 {
882 if (m_state->m_objectMap.find(actPos) == m_state->m_objectMap.end())
883 {
884 WPS_DEBUG_MSG(("WPS4Text::readText: can not find object for position : %lX\n", static_cast<unsigned long>(actPos)));
885 }
886 else
887 {
888 WPS4TextInternal::Object const &obj = m_state->m_objectMap[actPos];
889 if (obj.m_id < 0 || obj.m_page > 0) break;
890 WPSPosition position(Vec2f(0,0), obj.m_size);
891 position.setRelativePosition(WPSPosition::CharBaseLine);
892 position.m_wrapping = WPSPosition::WDynamic;
893 mainParser().sendObject(position, obj.m_id);
894 }
895 break;
896 }
897 case 0x0f:
898 {
899 if (m_state->m_dateTimeMap.find(actPos) == m_state->m_dateTimeMap.end())
900 {
901 WPS_DEBUG_MSG(("WPS4Text::readText: can not find date/time for position : %lX\n", static_cast<unsigned long>(actPos)));
902 }
903 else
904 {
905 auto const &form = m_state->m_dateTimeMap[actPos];
906 std::string format = form.format();
907 if (format.length())
908 {
909 WPSField field(WPSField::Date);
910 field.m_DTFormat=format;
911 m_listener->insertField(field);
912 }
913 else
914 {
915 WPS_DEBUG_MSG(("WPS4Text::readText: unknown date/time format for position : %lX\n", static_cast<unsigned long>(actPos)));
916 }
917 }
918 break;
919 }
920 case 0x06: // footnote
921 {
922 // ok if this is the first character of the footnote definition
923 if (zone.id() == WPS4TextInternal::Z_Note) break;
924 if (m_state->m_footnoteMap.find(pos) == m_state->m_footnoteMap.end() ||
925 m_state->m_footnoteMap[pos] == nullptr)
926 {
927 WPS_DEBUG_MSG(("WPS4Text::readText:do not find the footnote zone\n"));
928 break;
929 }
930 auto const &note = *m_state->m_footnoteMap[pos];
931 mainParser().createNote(note, note.m_label);
932 break;
933 }
934 case 0x09:
935 m_listener->insertTab();
936 break;
937 case 0x0C:
938 if (mainZone) mainParser().newPage(++actPage);
939 break;
940 case 0x0d:
941 break; // 0d0a = end of line
942 case 0x0a:
943 m_listener->insertEOL();
944 break;
945 case 0x0b: // check me
946 m_listener->insertEOL(true);
947 break;
948 case 0x11: // insecable hyphen
949 m_listener->insertUnicode(0x2011);
950 break;
951 case 0x12: // insecable space
952 m_listener->insertUnicode(0xA0);
953 break;
954 case 0x1F: // optional hyphen
955 break;
956 case '&':
957 if (isSimpleField)
958 {
959 int nextVal = libwps::readU8(m_input);
960 bool done = true;
961 switch (nextVal) // check me
962 {
963 case 'p':
964 case 'P':
965 m_listener->insertField(WPSField(WPSField::PageNumber));
966 break;
967 case 'd':
968 case 'D':
969 m_listener->insertField(WPSField(WPSField::Date));
970 break;
971 case 't':
972 case 'T':
973 m_listener->insertField(WPSField(WPSField::Time));
974 break;
975 case 'f':
976 case 'F':
977 m_listener->insertField(WPSField(WPSField::Title));
978 break;
979 // case '&': check me does '&&'->'&' ?
980 default:
981 done = false;
982 break;
983 }
984 if (done)
985 {
986 i--;
987 break;
988 }
989 m_input->seek(-1, librevenge::RVNG_SEEK_CUR);
990 }
991 WPS_FALLTHROUGH;
992 default:
993 if (version()<=2)
994 {
995 // special caracter
996 if (readVal==0xca) // not breaking space
997 {
998 m_listener->insertCharacter(0xA0);
999 break;
1000 }
1001 }
1002 if (readVal)
1003 text.push_back(char(readVal));
1004 break;
1005 }
1006 }
1007
1008 if (simpleString) break;
1009
1010 f << "='"<<chaine<<"'";
1011 ascii().addPos(actPos);
1012 ascii().addNote(f.str().c_str());
1013 }
1014
1015 if (dlink)
1016 m_listener->insertUnicode(0x226b);
1017
1018 return true;
1019 }
1020
1021 ////////////////////////////////////////////////////////////
1022 // find all the text entries
1023 ////////////////////////////////////////////////////////////
1024 bool WPS4Text::readEntries()
1025 {
1026 auto &nameMultiMap = getNameEntryMap();
1027
1028 libwps::DebugStream f;
1029 long actPos = m_input->tell();
1030 f << "ZZHeader-Text:Limit(";
1031
1032 int textLimits[4];
1033 // look like begin of text : end of header/end of footer/end text
1034 // but sometimes the zones overlaps !!!
1035 for (int &textLimit : textLimits) textLimit = libwps::read32(m_input);
1036
1037 bool first = true, ok = true;
1038 long lastPos = textLimits[0] < 0x100 ? 0x100 : textLimits[0];
1039 for (int i = 0; i < 3; ++i)
1040 {
1041 long newPos = textLimits[i+1];
1042 WPSEntry zone;
1043 zone.setBegin(lastPos);
1044 zone.setEnd(newPos);
1045 zone.setType("TEXT");
1046 zone.setId(i);
1047
1048 if (newPos >= lastPos)
1049 lastPos = newPos;
1050 if (!zone.valid() || zone.begin() < 0x100)
1051 {
1052 if (newPos != 0x100 && newPos != -1)
1053 {
1054 WPS_DEBUG_MSG(("WPS4Text::readEntries: find odd text limit\n"));
1055 ok = false;
1056 }
1057 f << "_, ";
1058 continue;
1059 }
1060
1061 if (first)
1062 {
1063 m_textPositions.setBegin(zone.begin());
1064 first = false;
1065 }
1066
1067 m_textPositions.setEnd(zone.end());
1068 nameMultiMap.insert(WPS4Parser::NameMultiMap::value_type(zone.type(), zone));
1069
1070 switch (i)
1071 {
1072 case 0:
1073 m_state->m_header = zone;
1074 break;
1075 case 1:
1076 m_state->m_footer = zone;
1077 break;
1078 case 2:
1079 m_state->m_main = zone;
1080 break;
1081 default:
1082 break;
1083 }
1084
1085 f << "Text"<<i << "=" << std::hex << zone.begin() << "x" << zone.end() << ",";
1086 ascii().addPos(zone.begin());
1087 std::string name = "ZZ";
1088 name+= zone.type();
1089 name+=char('0'+i);
1090 ascii().addNote(name.c_str());
1091 ascii().addPos(zone.end());
1092 ascii().addNote("_");
1093 }
1094 f << ")";
1095 if (!ok)
1096 {
1097 m_state->m_header = m_state->m_footer = WPSEntry();
1098 m_state->m_main = m_textPositions;
1099 }
1100 if (!m_textPositions.valid())
1101 {
1102 WPS_DEBUG_MSG(("WPS4Text::readEntries: textPosition is not valid"));
1103 return false;
1104 }
1105
1106 /* stream offset to end of file */
1107 auto eof = long(libwps::readU32(m_input));
1108
1109 if (m_textPositions.end() > eof)
1110 {
1111 WPS_DEBUG_MSG(("WPS4Text:readEntries: can not find text positions\n"));
1112 return false;
1113 }
1114
1115 // check if fPositions.offset_eos
1116 long newPos = m_input->tell();
1117 if (m_input->seek(eof-1, librevenge::RVNG_SEEK_SET) != 0 || m_input->tell() != eof-1)
1118 {
1119 eof = m_input->tell();
1120 WPS_DEBUG_MSG(("WPS4Text:readEntries: incomplete file\n"));
1121 if (eof < m_textPositions.end()) return false;
1122 }
1123 mainParser().setSizeFile(eof);
1124
1125 f << ", endFile=" << eof;
1126 ascii().addPos(actPos);
1127 ascii().addNote(f.str().c_str());
1128
1129 m_input->seek(newPos, librevenge::RVNG_SEEK_SET);
1130
1131 static char const *zName[] =
1132 { "BTEC", "BTEP", "SHdr", "SFtr", "DLINK", "FTNp", "FTNd", "BKMK", "FONT" };
1133
1134 for (auto &i : zName)
1135 mainParser().parseEntry(i);
1136
1137 return true;
1138 }
1139
1140 ////////////////////////////////////////////////////////////
1141 // find all the text structures
1142 ////////////////////////////////////////////////////////////
1143 bool WPS4Text::readStructures()
1144 {
1145 auto &nameMultiMap = getNameEntryMap();
1146
1147 // first find the font name
1148 auto pos = nameMultiMap.find("FONT");
1149 if (pos != nameMultiMap.end()) readFontNames(pos->second);
1150
1151 // now find the character and paragraph properties
1152 for (int i = 0; i < 2; ++i)
1153 {
1154 // we begin by i = 1 to create firsts the fdpc structure
1155 if (findFDPStructures(1-i)) continue;
1156 findFDPStructuresByHand(1-i);
1157 }
1158
1159 /* read character FODs (FOrmatting Descriptors) */
1160 std::vector<DataFOD> fdps;
1161 for (auto const &fdp : m_state->m_FDPCs)
1162 readFDP(fdp, fdps, static_cast<FDPParser>(&WPS4Text::readFont));
1163 m_FODList = mergeSortedFODLists(fdps, m_FODList);
1164
1165
1166 /* read paragraphs FODs (FOrmatting Descriptors) */
1167 fdps.resize(0);
1168 for (auto const &fdp : m_state->m_FDPPs)
1169 readFDP(fdp, fdps, static_cast<FDPParser>(&WPS4Text::readParagraph));
1170 m_FODList = mergeSortedFODLists(fdps, m_FODList);
1171
1172 /* read the object structures */
1173 pos = nameMultiMap.find("EOBJ");
1174 if (pos != nameMultiMap.end())
1175 {
1176 std::vector<long> textPtrs, listValues;
1177 readPLC(pos->second, textPtrs, listValues, &WPS4Text::objectDataParser);
1178 }
1179
1180 // update the footnote
1181 WPSEntry ftnD, ftnP;
1182 pos = nameMultiMap.find("FTNd");
1183 if (pos != nameMultiMap.end()) ftnD = pos->second;
1184 pos = nameMultiMap.find("FTNp");
1185 if (pos != nameMultiMap.end()) ftnP = pos->second;
1186 readFootNotes(ftnD, ftnP);
1187
1188 // bookmark
1189 pos = nameMultiMap.find("BKMK");
1190 if (pos != nameMultiMap.end())
1191 {
1192 std::vector<long> textPtrs, listValues;
1193 readPLC(pos->second, textPtrs, listValues, &WPS4Text::bkmkDataParser);
1194 }
1195
1196 // the list of file
1197 pos = nameMultiMap.find("DLINK");
1198 if (pos != nameMultiMap.end())
1199 readDosLink(pos->second);
1200
1201 // date/time format
1202 pos = nameMultiMap.find("DTTM");
1203 if (pos != nameMultiMap.end())
1204 {
1205 WPSEntry const &zone = pos->second;
1206 std::vector<long> textPtrs, listValues;
1207 readPLC(zone, textPtrs, listValues, &WPS4Text::dttmDataParser);
1208 }
1209
1210 // finally, we must remove the footnote of textposition...
1211 long bot = m_state->m_main.begin();
1212 long endPos = m_state->m_main.end();
1213 size_t numFootNotes = m_state->m_footnoteList.size(), actNote = 0;
1214 bool textPosUpdated = false;
1215 while (bot < endPos)
1216 {
1217 if (actNote < numFootNotes &&
1218 m_state->m_footnoteList[actNote].begin()==bot)
1219 {
1220 bot = m_state->m_footnoteList[actNote].end();
1221 actNote++;
1222 continue;
1223 }
1224 long lastPos = actNote < numFootNotes ?
1225 m_state->m_footnoteList[actNote].begin() : endPos;
1226 if (lastPos > endPos) lastPos = endPos;
1227 WPSEntry mZone;
1228 mZone.setBegin(bot);
1229 mZone.setEnd(lastPos);
1230 mZone.setType("TEXT");
1231 if (!textPosUpdated)
1232 {
1233 mZone.setId(WPS4TextInternal::Z_Main);
1234 m_state->m_main = mZone;
1235 textPosUpdated = true;
1236 }
1237 else
1238 {
1239 if (m_state->m_otherZones.size() == 0)
1240 {
1241 WPS_DEBUG_MSG(("WPS4Text::readStructures: find unknown text zone\n"));
1242 }
1243 mZone.setId(WPS4TextInternal::Z_Unknown);
1244 m_state->m_otherZones.push_back(mZone);
1245 }
1246 bot = lastPos;
1247 }
1248
1249
1250 return true;
1251 }
1252
1253 ////////////////////////////////////////////////////////////
1254 // find FDP zones ( normal method followed by another method
1255 // which may works for some bad files )
1256 ////////////////////////////////////////////////////////////
1257 bool WPS4Text::findFDPStructures(int which)
1258 {
1259 std::vector<WPSEntry> &zones = which ? m_state->m_FDPCs : m_state->m_FDPPs;
1260 zones.resize(0);
1261
1262 char const *indexName = which ? "BTEC" : "BTEP";
1263 char const *sIndexName = which ? "FDPC" : "FDPP";
1264
1265 auto const &nameMultiMap =getNameEntryMap();
1266 auto pos = nameMultiMap.find(indexName);
1267 if (pos == nameMultiMap.end()) return false;
1268
1269 std::vector<long> textPtrs;
1270 std::vector<long> listValues;
1271
1272 if (!readPLC(pos->second, textPtrs, listValues)) return false;
1273
1274 size_t numV = listValues.size();
1275 if (textPtrs.size() != numV+1) return false;
1276
1277 WPSEntry zone;
1278 zone.setType(sIndexName);
1279
1280 for (size_t i = 0; i < numV; ++i)
1281 {
1282 long bPos = listValues[i];
1283 if (bPos <= 0) return false;
1284 zone.setBegin(bPos);
1285 zone.setLength(0x80);
1286
1287 zones.push_back(zone);
1288 }
1289
1290 return true;
1291 }
1292
1293 bool WPS4Text::findFDPStructuresByHand(int which)
1294 {
1295 char const *indexName = which ? "FDPC" : "FDPP";
1296 WPS_DEBUG_MSG(("WPS4Text::findFDPStructuresByHand: need to create %s list by hand \n", indexName));
1297
1298 auto &zones = which ? m_state->m_FDPCs : m_state->m_FDPPs;
1299 zones.resize(0);
1300
1301 long debPos;
1302 if (which == 1)
1303 {
1304 // hack: each fdp block is aligned with 0x80,
1305 // and appears consecutively just after the text
1306 auto pnChar = uint32_t((m_textPositions.end()+127)>>7);
1307 /* sanity check */
1308 if (0 == pnChar)
1309 {
1310 WPS_DEBUG_MSG(("WPS4Text::findFDPStructuresByHand: pnChar is 0, so file may be corrupt\n"));
1311 throw libwps::ParseException();
1312 }
1313 debPos = 0x80 * long(pnChar);
1314 }
1315 else
1316 {
1317 size_t nFDPC = m_state->m_FDPCs.size();
1318 if (!nFDPC)
1319 {
1320 WPS_DEBUG_MSG(("WPS4Text::findFDPStructuresByHand: can not find last fdpc pos\n"));
1321 return false;
1322 }
1323 debPos = m_state->m_FDPCs[nFDPC-1].end();
1324 }
1325
1326 WPSEntry fdp;
1327 fdp.setType(indexName);
1328
1329 long lastPos = m_textPositions.begin();
1330 while (1)
1331 {
1332 m_input->seek(debPos+0x7f, librevenge::RVNG_SEEK_SET);
1333 if (m_input->tell() != debPos+0x7f)
1334 {
1335 WPS_DEBUG_MSG(("WPS4Text: find EOF while parsing the %s\n", indexName));
1336 return false;
1337 }
1338 int nbElt = libwps::readU8(m_input);
1339 if (5*nbElt+4 > 0x80)
1340 {
1341 WPS_DEBUG_MSG(("WPS4Text: find too big number of data while parsing the %s\n", indexName));
1342 return false;
1343 }
1344 m_input->seek(debPos, librevenge::RVNG_SEEK_SET);
1345 if (long(libwps::readU32(m_input)) != lastPos)
1346 {
1347 WPS_DEBUG_MSG(("WPS4Text: find incorrect linking while parsing the %s\n", indexName));
1348 return false;
1349 }
1350 if (nbElt != 1)
1351 m_input->seek(4*nbElt-4, librevenge::RVNG_SEEK_CUR);
1352
1353 auto newPos = long(libwps::readU32(m_input));
1354 if (newPos < lastPos || newPos > m_textPositions.end())
1355 {
1356 WPS_DEBUG_MSG(("WPS4Text: find incorrect linking while parsing the %s\n", indexName));
1357 return false;
1358 }
1359 fdp.setBegin(debPos);
1360 fdp.setLength(0x80);
1361 zones.push_back(fdp);
1362
1363 if (newPos == m_textPositions.end()) break;
1364
1365 lastPos = newPos;
1366 debPos = fdp.end();
1367 }
1368
1369 return true;
1370 }
1371
1372 // PLC Data: default parser
1373 bool WPS4Text::defDataParser(long, long, int, long endPos, std::string &mess)
1374 {
1375 mess = "";
1376 libwps::DebugStream f;
1377
1378 long actPos = m_input->tell();
1379 long length = endPos+1-actPos;
1380 int sz = (length%4)==0 ? 4 : (length%2)==0 ? 2 : 1;
1381 f << "unk["<< sz << "]=";
1382 while (m_input->tell() <= endPos+1-sz)
1383 {
1384 long val = 0;
1385 switch (sz)
1386 {
1387 case 1:
1388 val = libwps::readU8(m_input);
1389 break;
1390 case 2:
1391 val = libwps::readU16(m_input);
1392 break;
1393 case 4:
1394 val = long(libwps::readU32(m_input));
1395 break;
1396 default:
1397 break;
1398 }
1399 f << std::hex << val << std::dec << ",";
1400 }
1401 mess = f.str();
1402 return true;
1403 }
1404
1405 ////////////////////////////////////////////////////////////
1406 // the fonts name zone (zone8)
1407 ////////////////////////////////////////////////////////////
1408 bool WPS4Text::readFontNames(WPSEntry const &entry)
1409 {
1410 if (!entry.valid()) return false;
1411
1412 m_input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1413
1414 long endPos = entry.end();
1415 int nFonts = 0;
1416 auto docType=mainParser().getDefaultFontType();
1417 while (m_input->tell() < endPos)
1418 {
1419 long actPos;
1420 actPos = m_input->tell();
1421 libwps::DebugStream f;
1422
1423 /* Sometimes the font numbers start at 0 and increment nicely.
1424 However, other times the font numbers jump around. */
1425 uint8_t font_number = libwps::readU8(m_input);
1426 if (m_state->m_fontNames.find(font_number) != m_state->m_fontNames.end())
1427 {
1428 WPS_DEBUG_MSG(("WPS4Text::readFontNames: at position 0x%lx: font number %i duplicated\n",
1429 static_cast<unsigned long>(m_input->tell()-2), font_number));
1430 throw libwps::ParseException();
1431 }
1432
1433 f << "Font" << nFonts++ << ": id=" << int(font_number) << ", ";
1434 //fixme: what is this byte? maybe a font class
1435 uint8_t unknown_byte = libwps::readU8(m_input);
1436 f << "unk=" << int(unknown_byte) << ", ";
1437
1438 librevenge::RVNGString s;
1439 uint8_t nChar = libwps::readU8(m_input);
1440 for (uint8_t i = nChar; i>0; i--)
1441 {
1442 if (m_input->isEnd())
1443 {
1444 WPS_DEBUG_MSG(("WPS4Text::readFontNames: can not read the font number %i (end of file)\n",
1445 font_number));
1446 throw libwps::ParseException();
1447 }
1448 unsigned char val = libwps::readU8(m_input);
1449 // sanity check (because sometimes contains char > 0x80 .. )
1450 if (val >= ' ' && val <= 'z') s.append(char(val));
1451 else
1452 {
1453 static bool first = true;
1454 if (first)
1455 {
1456 first = false;
1457 WPS_DEBUG_MSG(("WPS4Text:readFontNames find odd caracters in font name : %d\n", int(val)));
1458 }
1459 f << "##oddC=" << int(val) << ", ";
1460 }
1461 }
1462 auto fType=libwps_tools_win::Font::getFontType(s);
1463 if (fType==libwps_tools_win::Font::UNKNOWN)
1464 fType=docType;
1465 WPS4TextInternal::FontName font(fType);
1466 font.m_name = s;
1467 f << font;
1468
1469 m_state->m_fontNames.insert(std::pair<int,WPS4TextInternal::FontName>(font_number,font));
1470
1471 ascii().addPos(actPos);
1472 ascii().addNote(f.str().c_str());
1473 ascii().addPos(m_input->tell());
1474 }
1475
1476 return true;
1477 }
1478
1479 ////////////////////////////////////////////////////////////
1480 // the font:
1481 ////////////////////////////////////////////////////////////
1482 bool WPS4Text::readFont(long endPos, int &id, std::string &mess)
1483 {
1484 WPS4TextInternal::Font font(getDefaultFont());
1485 font.m_size = 12;
1486
1487 libwps::DebugStream f;
1488
1489 int fl[4] = { 0, 0, 0, 0};
1490 if (m_input->tell() < endPos) fl[0] = libwps::readU8(m_input);
1491
1492 /* set difference from default properties */
1493 uint32_t attributes = 0;
1494 if (fl[0] & 0x01) attributes |= WPS_BOLD_BIT;
1495 if (fl[0] & 0x02) attributes |= WPS_ITALICS_BIT;
1496 if (fl[0] & 0x04) attributes |= WPS_STRIKEOUT_BIT;
1497 fl[0] &= 0xf8;
1498
1499 // what & 0x01 -> ???
1500 // what & 0x02 -> note
1501 // what & 0x04 -> ???
1502 // what & 0x08 -> fName
1503 // what & 0x10 -> size
1504 // what & 0x20 -> underline (fl[2])
1505 // what & 0x40 -> decalage
1506 // what & 0x80 -> color
1507 int what = 0;
1508 if (m_input->tell() < endPos) what = libwps::readU8(m_input);
1509
1510 font.m_special = ((what & 2) != 0);
1511 what &= 0xfd;
1512
1513 if (m_input->tell() < endPos)
1514 {
1515 // the fonts
1516 // FIXME: find some properties with size=3,
1517 // for which this character seems
1518 // related to size, not font
1519 uint8_t font_n = libwps::readU8(m_input);
1520
1521 if (m_state->m_fontNames.find(font_n) != m_state->m_fontNames.end())
1522 {
1523 auto const &fontName=m_state->m_fontNames.find(font_n)->second;
1524 font.m_name=fontName.m_name;
1525 font.m_type=fontName.m_type;
1526 }
1527 else if (version() <= 2)
1528 {
1529 font.m_name=WPS4TextInternal::FontName::getDosName(font_n);
1530 font.m_type=mainParser().getDefaultFontType();
1531 }
1532 else
1533 {
1534 WPS_DEBUG_MSG(("WPS4Text: error: encountered font %i which is not indexed\n",
1535 font_n));
1536 }
1537
1538 if (font.m_name.empty()) f << "###nameId=" << int(font_n) << ",";
1539 }
1540
1541 if (m_input->tell() < endPos)
1542 {
1543 // underline, ...
1544 int underlinePos = libwps::readU8(m_input);
1545 if (underlinePos)
1546 {
1547 if (!(what & 0x20)) f << "undFl,";
1548 else what &= 0xdf;
1549 attributes |= WPS_UNDERLINE_BIT;
1550 }
1551 }
1552
1553 if (m_input->tell() < endPos) // font size * 2
1554 {
1555 int fSize = libwps::readU8(m_input);
1556 if (fSize)
1557 {
1558 if (!(what & 0x10)) f << "szFl,";
1559 else what &= 0xef;
1560 font.m_size = (fSize/2);
1561 }
1562 }
1563
1564 if (m_input->tell() < endPos) // height decalage -> sub/superscript
1565 {
1566 int fDec = libwps::read8(m_input);
1567 if (fDec)
1568 {
1569 if (!(what & 0x40)) f << "sub/supFl(val=" << fDec<<"),";
1570 else what &= 0xbf;
1571
1572 if (fDec > 0) attributes |= WPS_SUPERSCRIPT_BIT;
1573 else attributes |= WPS_SUBSCRIPT_BIT;
1574 }
1575 }
1576 if (m_input->tell()+2 <= endPos) // color field
1577 {
1578 int bkColor = libwps::readU8(m_input);
1579 int ftColor = libwps::readU8(m_input);
1580 bool setColor = !!(what & 0x80);
1581 what &= 0x7F;
1582
1583 if ((bkColor || ftColor) && !setColor)
1584 {
1585 setColor = true;
1586 f << "colorFl,";
1587 }
1588 if (setColor)
1589 {
1590 WPSColor color;
1591 if (mainParser().getColor(bkColor, color))
1592 font.m_backColor = color;
1593 if (mainParser().getColor(ftColor, color))
1594 font.m_color = color;
1595 }
1596 }
1597 if (m_input->tell() < endPos)
1598 font.m_dlinkId = libwps::readU8(m_input);
1599 if (what) f << "#what=" << std::hex << what << std::dec << ",";
1600 if (fl[0]) f << "unkn0=" << std::hex << fl[0] << std::dec << ",";
1601
1602 if (m_input->tell() != endPos)
1603 {
1604 f << "#unknEnd=(";
1605 while (m_input->tell() < endPos) f << std::hex << libwps::readU8(m_input) <<",";
1606 f << ")";
1607 }
1608
1609 font.m_attributes = attributes;
1610 font.m_extra = f.str();
1611
1612 id = int(m_state->m_fontList.size());
1613 m_state->m_fontList.push_back(font);
1614 f.str("");
1615 f << font;
1616 mess = f.str();
1617
1618 return true;
1619 }
1620
1621 ////////////////////////////////////////////////////////////
1622 // the file list: only in dos3 ?
1623 ////////////////////////////////////////////////////////////
1624 bool WPS4Text::readDosLink(WPSEntry const &entry)
1625 {
1626 if (!entry.valid()) return false;
1627
1628 long length = entry.length();
1629 if (length%44)
1630 {
1631 WPS_DEBUG_MSG(("WPS4Text::readDosLink: length::=%ld seem odd\n", length));
1632 return false;
1633 }
1634
1635 m_input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1636 libwps::DebugStream f;
1637 long numElt = length/44;
1638 long val;
1639 for (long n = 0; n < numElt; ++n)
1640 {
1641 WPS4TextInternal::DosLink link;
1642 long pos = m_input->tell();
1643 long endPos = pos+44;
1644 f.str("");
1645 for (int i = 0; i < 2; ++i) // always 0, 0
1646 {
1647 val = libwps::readU16(m_input);
1648 if (val) f << "unkn" << i << "=" << std::hex << val << std::dec << ",";
1649 }
1650 link.m_width = float(libwps::readU16(m_input)/1440.);
1651 for (int i = 2; i < 4; ++i) // always f0, f0
1652 {
1653 val = libwps::readU16(m_input);
1654 if (val != 0xf0) f << "unkn" << i << "=" << std::hex << val << std::dec << ",";
1655 }
1656 link.m_type = libwps::readU8(m_input);
1657 val = libwps::readU8(m_input);
1658 if (val) // find 0x18 for a spreadsheet
1659 f << "unk4=" << std::hex << val << std::dec << ",";
1660 switch (link.m_type)
1661 {
1662 case 0x81: // picture ?
1663 {
1664 long dim[2];
1665 for (long &i : dim) i = libwps::readU16(m_input);
1666 link.m_size = Vec2f(float(dim[0])/1440.f, float(dim[1])/1440.f);
1667 val = libwps::readU16(m_input); // always 0
1668 if (val) f << "g0=" << val << ",";
1669 val = libwps::readU16(m_input); // always 4
1670 if (val != 4) f << "g1=" << val << ",";
1671 }
1672 WPS_FALLTHROUGH;
1673 case 0x40: // spreadsheet range
1674 case 0x01: // char ?
1675 {
1676 std::string name("");
1677 link.m_pos.setBegin(m_input->tell());
1678 while (!m_input->isEnd() && long(m_input->tell()) < endPos)
1679 {
1680 auto c = char(libwps::readU8(m_input));
1681 if (!c)
1682 {
1683 m_input->seek(-1, librevenge::RVNG_SEEK_CUR);
1684 break;
1685 }
1686 name += c;
1687 }
1688 link.m_pos.setEnd(m_input->tell());
1689 link.m_pos.setId(WPS4TextInternal::Z_DLink);
1690 link.m_name = name;
1691 break;
1692 }
1693 default:
1694 break;
1695 }
1696 link.m_extra = f.str();
1697 m_state->m_dosLinkList.push_back(link);
1698 f.str("");
1699 f << "ZZDLINK-" << n << ":" << link;
1700 if (long(m_input->tell()) != endPos)
1701 ascii().addDelimiter(m_input->tell(),'|');
1702 ascii().addPos(pos);
1703 ascii().addNote(f.str().c_str());
1704 m_input->seek(endPos, librevenge::RVNG_SEEK_SET);
1705 }
1706 return true;
1707 }
1708
1709 ////////////////////////////////////////////////////////////
1710 // the paragraph properties:
1711 ////////////////////////////////////////////////////////////
1712 bool WPS4Text::readParagraph(long endPos, int &id, std::string &mess)
1713 {
1714 long actPos = m_input->tell();
1715 long size = endPos - actPos;
1716
1717 WPSParagraph pp;
1718 if (size && size < 3)
1719 {
1720 WPS_DEBUG_MSG(("WPS4Text:readParagraph:(sz=%ld)\n", size));
1721 return false;
1722 }
1723
1724 libwps::DebugStream f;
1725 for (int i = 0; i < 3; ++i)
1726 {
1727 int v = libwps::readU8(m_input);
1728 if (v != 0) f << "unkn"<<i<< "=" << v;
1729 }
1730
1731 while (m_input->tell() < endPos)
1732 {
1733 int v = libwps::readU8(m_input);
1734 long pos = m_input->tell();
1735 bool ok = true, done = true;
1736 int arg = -1;
1737 switch (v)
1738 {
1739 case 0x2:
1740 {
1741 if (pos+1 > endPos)
1742 {
1743 ok = false;
1744 break;
1745 }
1746 arg = libwps::readU8(m_input);
1747 f << "f2=" << arg << ",";
1748 break;
1749 }
1750 case 0x5:
1751 {
1752 if (pos+1 > endPos)
1753 {
1754 ok = false;
1755 break;
1756 }
1757 arg = libwps::readU8(m_input);
1758 switch (arg)
1759 {
1760 case 0:
1761 pp.m_justify = libwps::JustificationLeft;
1762 break;
1763 case 1:
1764 pp.m_justify = libwps::JustificationCenter;
1765 break;
1766 case 2:
1767 pp.m_justify = libwps::JustificationRight;
1768 break;
1769 case 3:
1770 pp.m_justify = libwps::JustificationFull;
1771 break;
1772 default:
1773 f << "#just=" << arg << ",";
1774 pp.m_justify = libwps::JustificationLeft;
1775 }
1776 break;
1777 }
1778 case 0x7: // 1: marked don't break paragraph
1779 case 0x8: // 1: marked keep paragraph with next
1780 {
1781 if (pos+1 > endPos)
1782 {
1783 ok = false;
1784 break;
1785 }
1786 arg = libwps::readU8(m_input);
1787 if (arg == 0) break;
1788 if (arg == 1) pp.m_breakStatus |= ((v == 7) ? libwps::NoBreakBit : libwps::NoBreakWithNextBit);
1789 else f << "#status=" << arg << ",";
1790 break;
1791 }
1792
1793 // BORDER
1794 case 0x9:
1795 {
1796 if (pos+1 > endPos)
1797 {
1798 ok = false;
1799 break;
1800 }
1801 arg = libwps::readU8(m_input);
1802 pp.m_borderStyle.m_style = WPSBorder::Simple;
1803 pp.m_borderStyle.m_type = WPSBorder::Single;
1804 pp.m_borderStyle.m_width = 1;
1805 int style = (arg&0xf);
1806 switch (style)
1807 {
1808 case 0:
1809 break;
1810 case 1:
1811 pp.m_borderStyle.m_width = 2;
1812 break;
1813 case 2:
1814 pp.m_borderStyle.m_type = WPSBorder::Double;
1815 break;
1816 case 3:
1817 pp.m_borderStyle.m_style = WPSBorder::Dot;
1818 break;
1819 case 4:
1820 pp.m_borderStyle.m_style = WPSBorder::LargeDot;
1821 break;
1822 case 5:
1823 pp.m_borderStyle.m_style = WPSBorder::Dash;
1824 break;
1825 case 6:
1826 case 7:
1827 case 8:
1828 pp.m_borderStyle.m_width = style-3;
1829 break;
1830 case 9:
1831 case 10:
1832 pp.m_borderStyle.m_width = style-7;
1833 pp.m_borderStyle.m_type = WPSBorder::Double;
1834 break;
1835 default:
1836 f << "#borderStyle=" << style << ",";
1837 WPS_DEBUG_MSG(("WPS4Text:readParagraph: unknown border style\n"));
1838 break;
1839 }
1840 int high = (arg>>4);
1841 if (version() < 3)
1842 {
1843 WPSColor color;
1844 if (high && mainParser().getColor(high, color))
1845 pp.m_borderStyle.m_color = color;
1846 else if (high)
1847 f << "#borderColor=" << high << ",";
1848 }
1849 else
1850 {
1851 switch (high)
1852 {
1853 case 0:
1854 break;
1855 case 4:
1856 pp.m_border = 0xf;
1857 break;
1858 case 8:
1859 pp.m_border = 0xf;
1860 f << "borderShaded,";
1861 break;
1862 default:
1863 f << "#borderStyle[high]=" << high << ",";
1864 break;
1865 }
1866 }
1867 break;
1868 }
1869 case 0xa: // 1: top border
1870 case 0xb: // : bottom border
1871 case 0xc: // : left border
1872 case 0xd: // : right border
1873 {
1874 if (pos+1 > endPos)
1875 {
1876 ok = false;
1877 break;
1878 }
1879 arg = libwps::readU8(m_input);
1880 if (arg == 0) break;
1881 if (arg == 1)
1882 {
1883 switch (v)
1884 {
1885 case 0xa:
1886 pp.m_border |= WPSBorder::TopBit;
1887 break;
1888 case 0xb:
1889 pp.m_border |= WPSBorder::BottomBit;
1890 break;
1891 case 0xc:
1892 pp.m_border |= WPSBorder::LeftBit;
1893 break;
1894 case 0xd:
1895 pp.m_border |= WPSBorder::RightBit;
1896 break;
1897 default:
1898 break;
1899 }
1900 }
1901 else f << "#border=" << arg << ",";
1902 break;
1903 }
1904 case 0x18: // border color
1905 {
1906 if (long(pos)==endPos)
1907 {
1908 ok = false;
1909 break;
1910 }
1911 int colorId = libwps::readU8(m_input);
1912 WPSColor color;
1913 if (mainParser().getColor(colorId, color))
1914 pp.m_borderStyle.m_color = color;
1915 else
1916 f << "#colorId=" << colorId << ",";
1917 break;
1918 }
1919 case 0xe: // 1: bullet
1920 {
1921 if (pos+1 > endPos)
1922 {
1923 ok = false;
1924 break;
1925 }
1926 arg = libwps::readU8(m_input);
1927 if (arg == 0) break;
1928
1929 pp.m_listLevelIndex = 1;
1930 pp.m_listLevel.m_type = libwps::BULLET;
1931 static const uint32_t bulletList[]=
1932 {
1933 0x2022, 0x3e, 0x25c6, 0x21d2, 0x25c7, 0x2605, /* 1-6 */
1934 0, 0, 0, 0, 0, 0, /* 7-12 unknown */
1935 0, 0, 0, 0, 0, 0x2750, /* 13-17 unknown and document... */
1936 0x2713, 0x261e, 0x2704, 0x2611, 0x2612, 0x270e /* 18-24 */
1937 };
1938 if (arg <= 24 && bulletList[arg-1])
1939 libwps::appendUnicode(bulletList[arg-1], pp.m_listLevel.m_bullet);
1940 else
1941 libwps::appendUnicode(0x2022, pp.m_listLevel.m_bullet);
1942 break;
1943 }
1944 case 0x1b:
1945 case 0x1a:
1946 case 0x10: // the bullet char : 0x18
1947 {
1948 if (pos+1 > endPos)
1949 {
1950 ok = false;
1951 break;
1952 }
1953 arg = libwps::readU8(m_input);
1954 done = true;
1955 switch (v)
1956 {
1957 case 0x1a:
1958 if (arg) f << "backPattern=" << arg << ",";
1959 break;
1960 case 0x1b:
1961 {
1962 if (arg==0) break;
1963 WPSColor color;
1964 if (mainParser().getColor(arg>>4, color))
1965 f << "backPatternBackColor=" << color << ";";
1966 else
1967 f << "#backPatternBackColor=" << (arg>>4) << ",";
1968 if (mainParser().getColor(arg&0xf, color))
1969 f << "backPatternFrontColor=" << color << ";";
1970 else
1971 f << "#backPatternFrontColor=" << (arg&0xf) << ",";
1972 break;
1973 }
1974 case 0x10:
1975 if (arg!=0x18) f << "bullet?=" << arg << ",";
1976 break;
1977 default:
1978 done = false;
1979 break;
1980 }
1981 break;
1982 }
1983 case 0xf: // tabs:
1984 {
1985 if (pos+1 > endPos)
1986 {
1987 ok = false;
1988 break;
1989 }
1990 int nVal = libwps::read8(m_input);
1991 if (nVal < 2 || pos + 1 + nVal > endPos)
1992 {
1993 ok = false;
1994 break;
1995 }
1996 int flag = libwps::readU8(m_input);
1997 if (flag) f << "#tabsFl=" << flag << ",";
1998 size_t nItem = libwps::readU8(m_input);
1999 if (size_t(nVal) != 2 + 3*nItem)
2000 {
2001 ok = false;
2002 break;
2003 }
2004 pp.m_tabs.resize(nItem);
2005 for (size_t i = 0; i < nItem; ++i)
2006 pp.m_tabs[i].m_position = libwps::read16(m_input)/1440.;
2007 for (size_t i = 0; i < nItem; ++i)
2008 {
2009 enum WPSTabStop::Alignment align = WPSTabStop::LEFT;
2010 int val = libwps::readU8(m_input);
2011 switch ((val & 0x3))
2012 {
2013 case 0:
2014 align = WPSTabStop::LEFT;
2015 break;
2016 case 1:
2017 align = WPSTabStop::CENTER;
2018 break;
2019 case 2:
2020 align = WPSTabStop::RIGHT;
2021 break;
2022 case 3:
2023 align = WPSTabStop::DECIMAL;
2024 break;
2025 default:
2026 break;
2027 }
2028 pp.m_tabs[i].m_alignment = align;
2029
2030 if (val&4) f << "#Tabbits3";
2031 val = (val>>3);
2032
2033 switch (val)
2034 {
2035 case 0:
2036 break;
2037 case 1:
2038 pp.m_tabs[i].m_leaderCharacter = '.';
2039 break;
2040 case 2:
2041 pp.m_tabs[i].m_leaderCharacter = '-';
2042 break;
2043 case 3:
2044 pp.m_tabs[i].m_leaderCharacter = '_';
2045 break;
2046 case 4:
2047 pp.m_tabs[i].m_leaderCharacter = '=';
2048 break;
2049 default:
2050 f << "#TabSep=" << val;
2051 }
2052 }
2053
2054 break;
2055 }
2056 case 0x11: // right margin : 1440*inches
2057 case 0x12: // left margin
2058 case 0x13: // another margin ( check me )
2059 case 0x14: // left text indent (relative to left margin)
2060 case 0x15: // line spacing (inter line) 240
2061 case 0x16: // line spacing before 240 = 1 line spacing
2062 case 0x17: // line spacing after
2063 {
2064 if (pos+2 > endPos)
2065 {
2066 ok = false;
2067 break;
2068 }
2069
2070 arg = libwps::read16(m_input);
2071 switch (v)
2072 {
2073 case 0x11:
2074 pp.m_margins[2] = arg/1440.;
2075 break;
2076 case 0x13: // seems another way to define the left margin
2077 f << "#left,";
2078 WPS_FALLTHROUGH;
2079 case 0x12:
2080 pp.m_margins[1] = arg/1440.;
2081 break;
2082 case 0x14:
2083 pp.m_margins[0] = arg/1440.;
2084 break;
2085 case 0x15:
2086 {
2087 double lines = arg ? arg/240. : 1.0;
2088 if (lines < 1.0 || lines > 2.0)
2089 {
2090 f << "##interLineSpacing=" << lines << ",";
2091 lines = (lines < 1.0) ? 1.0 : 2.0;
2092 }
2093 pp.setInterline(lines, librevenge::RVNG_PERCENT);
2094 break;
2095 }
2096 case 0x16:
2097 pp.m_spacings[1] = arg/240.;
2098 break;
2099 case 0x17:
2100 pp.m_spacings[2] = arg/240.;
2101 break;
2102 default:
2103 done = false;
2104 }
2105 break;
2106 }
2107 default:
2108 ok = false;
2109 }
2110 if (!ok)
2111 {
2112 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
2113 f << "###v" << v<<"=" <<std::hex;
2114 while (m_input->tell() < endPos)
2115 f << int(libwps::readU8(m_input)) << ",";
2116 break;
2117 }
2118
2119 if (done) continue;
2120
2121 f << "f" << v << "=" << std::hex << arg << std::dec << ",";
2122 }
2123 if (pp.m_listLevelIndex >= 1)
2124 pp.m_listLevel.m_labelIndent = pp.m_margins[1];
2125 else if (pp.m_margins[0] + pp.m_margins[1] < 0.0)
2126 {
2127 // sanity check
2128 if (pp.m_margins[1] < 0.0) pp.m_margins[1] = 0.0;
2129 pp.m_margins[0] = -pp.m_margins[1];
2130 }
2131 pp.m_extra = f.str();
2132
2133 id = int(m_state->m_paragraphList.size());
2134 m_state->m_paragraphList.push_back(pp);
2135
2136 f.str("");
2137 f << pp;
2138 mess = f.str();
2139 return true;
2140 }
2141
2142 ////////////////////////////////////////////////////////////
2143 // the foot note properties:
2144 ////////////////////////////////////////////////////////////
2145 bool WPS4Text::readFootNotes(WPSEntry const &ftnD, WPSEntry const &ftnP)
2146 {
2147 if (!ftnD.valid() && !ftnP.valid()) return true;
2148 if (!ftnD.valid() || !ftnP.valid())
2149 {
2150 WPS_DEBUG_MSG(("WPS4Text::readFootNotes: one of the two entry is not valid, footnote will be ignored\n"));
2151 return false;
2152 }
2153
2154 std::vector<long> footNotePos,footNoteDef, listValues;
2155 if (!readPLC(ftnP, footNotePos, listValues, &WPS4Text::footNotesDataParser))
2156 {
2157 WPS_DEBUG_MSG(("WPS4Text::readFootNotes: can not read positions\n"));
2158 return false;
2159 }
2160
2161 if (!readPLC(ftnD, footNoteDef, listValues))
2162 {
2163 WPS_DEBUG_MSG(("WPS4Text::readFootNotes: can not read definitions\n"));
2164 return false;
2165 }
2166
2167 int numFootNotes = int(footNotePos.size())-1;
2168 if (numFootNotes <= 0 || int(footNoteDef.size())-1 != numFootNotes)
2169 {
2170 WPS_DEBUG_MSG(("WPS4Text::readFootNotes: no footnotes\n"));
2171 return false;
2172 }
2173
2174 // save the actual type and create a list of footnote entries
2175 std::vector<WPS4TextInternal::Note> noteTypes=m_state->m_footnoteList;
2176 m_state->m_footnoteList.resize(0);
2177
2178 std::vector<int> corresp;
2179 for (size_t i = 0; i < size_t(numFootNotes); ++i)
2180 {
2181 WPS4TextInternal::Note fZone;
2182 fZone.setBegin(footNoteDef[i]);
2183 fZone.setEnd(footNoteDef[i+1]);
2184 fZone.setType("TEXT");
2185 fZone.setId(WPS4TextInternal::Z_Note);
2186 m_state->m_footnoteList.push_back(fZone);
2187 corresp.push_back(int(i));
2188
2189 // sort the footnote
2190 for (size_t j = i; j > 0; j--)
2191 {
2192 if (m_state->m_footnoteList[j].begin() >=
2193 m_state->m_footnoteList[j-1].end()) break;
2194
2195 if (m_state->m_footnoteList[j].end() >
2196 m_state->m_footnoteList[j-1].begin())
2197 {
2198 WPS_DEBUG_MSG
2199 (("WPS4Text: error: can not create footnotes zone, found %lx and %lx\n",
2200 static_cast<unsigned long>(m_state->m_footnoteList[j].end()),
2201 static_cast<unsigned long>(m_state->m_footnoteList[j-1].begin())));
2202
2203 m_state->m_footnoteList.resize(0);
2204 return false;
2205 }
2206
2207 std::swap(m_state->m_footnoteList[j], m_state->m_footnoteList[j-1]);
2208
2209 int pos = corresp[j];
2210 corresp[j] = corresp[j-1];
2211 corresp[j-1] = pos;
2212 }
2213 }
2214 // ok, we can create the map, ...
2215 for (size_t i = 0; i < size_t(numFootNotes); ++i)
2216 {
2217 auto id = size_t(corresp[i]);
2218 WPS4TextInternal::Note &z = m_state->m_footnoteList[id];
2219 if (id < noteTypes.size())
2220 {
2221 z.m_label = noteTypes[id].m_label;
2222 z.m_error = noteTypes[id].m_error;
2223 }
2224 m_state->m_footnoteMap[footNotePos[id]] = &z;
2225 }
2226 return true;
2227 }
2228
2229 bool WPS4Text::footNotesDataParser(long /*bot*/, long /*eot*/, int id,
2230 long endPos, std::string &mess)
2231 {
2232 mess = "";
2233
2234 long actPos = m_input->tell();
2235 long length = endPos+1-actPos;
2236 if (length != 12)
2237 {
2238 WPS_DEBUG_MSG(("WPS4Text::footNotesDataParser: unknown size %ld for footdata data\n", length));
2239 return false;
2240 }
2241 libwps::DebugStream f;
2242 WPS4TextInternal::Note note;
2243 auto type = libwps::readU16(m_input);
2244 if (type & 1)
2245 {
2246 if (type != 1)
2247 f << "###numeric=" << std::hex << type << std::dec << ",";
2248 }
2249 else if (type == 0 || type > 20)
2250 f << "###char,";
2251 else
2252 note.m_label=libwps_tools_win::Font::unicodeString(m_input.get(), type/2, mainParser().getDefaultFontType());
2253 note.m_error=f.str();
2254 if (id >= int(m_state->m_footnoteList.size()))
2255 m_state->m_footnoteList.resize(size_t(id+1));
2256 m_state->m_footnoteList[size_t(id)]=note;
2257 f.str("");
2258 f << note;
2259 mess = f.str();
2260 m_input->seek(endPos+1, librevenge::RVNG_SEEK_SET);
2261 return true;
2262 }
2263
2264 ////////////////////////////////////////////////////////////
2265 // the bookmark properties:
2266 ////////////////////////////////////////////////////////////
2267 bool WPS4Text::bkmkDataParser(long bot, long /*eot*/, int /*id*/,
2268 long endPos, std::string &mess)
2269 {
2270 mess = "";
2271 if (m_state->m_bookmarkMap.find(bot) != m_state->m_bookmarkMap.end())
2272 {
2273 WPS_DEBUG_MSG(("WPS4Text:bkmkDataParser: bookmark already exists in this position\n"));
2274 return true;
2275 }
2276
2277 long actPos = m_input->tell();
2278 long length = endPos+1-actPos;
2279 if (length != 16)
2280 {
2281 WPS_DEBUG_MSG(("WPS4Text::bkmkDataParser: unknown size %ld for bkmkdata data\n", length));
2282 return false;
2283 }
2284
2285 for (int i = 0; i < 16; ++i)
2286 {
2287 auto c = char(libwps::readU8(m_input));
2288 if (c == '\0') break;
2289 mess += c;
2290 }
2291 WPSEntry ent;
2292 ent.setBegin(actPos);
2293 ent.setEnd(m_input->tell());
2294 ent.setId(WPS4TextInternal::Z_String);
2295 m_state->m_bookmarkMap[bot] = ent;
2296 m_input->seek(endPos+1, librevenge::RVNG_SEEK_SET);
2297 return true;
2298 }
2299
2300 ////////////////////////////////////////////////////////////
2301 // the object properties:
2302 ////////////////////////////////////////////////////////////
2303 bool WPS4Text::objectDataParser(long bot, long /*eot*/, int id,
2304 long endPos, std::string &mess)
2305 {
2306 mess = "";
2307 if (m_state->m_objectMap.find(bot) != m_state->m_objectMap.end())
2308 {
2309 WPS_DEBUG_MSG(("WPS4Text:objectDataParser: object already exists in this position\n"));
2310 return true;
2311 }
2312
2313 libwps::DebugStream f;
2314
2315 long actPos = m_input->tell();
2316 long length = endPos+1-actPos;
2317 if (length != 36)
2318 {
2319 WPS_DEBUG_MSG(("WPS4Text:objectDataParser unknown size %ld for object data\n", length));
2320 return false;
2321 }
2322 // 3->08 4->4f4d or 68->list?
2323 f << "type?=" << int(libwps::read16(m_input)) << ",";
2324 for (int i = 0; i < 2; ++i)
2325 {
2326 int v =libwps::read16(m_input);
2327 if (v) f << "unkn1:" << i << "=" << v << ",";
2328 }
2329 float dim[4];
2330 for (float &i : dim)
2331 i =float(libwps::read16(m_input)/1440.);
2332
2333 // CHECKME: the next two sizes are often simillar,
2334 // maybe the first one is the original size and the second
2335 // size in the document...
2336 f << "origSz?=[" << dim[0] << "," << dim[1] << "],";
2337
2338 WPS4TextInternal::Object obj;
2339 obj.m_size = Vec2f(dim[2], dim[3]); // CHECKME: unit
2340
2341 auto size = long(libwps::readU32(m_input));
2342 auto pos = long(libwps::readU32(m_input));
2343
2344 actPos = m_input->tell();
2345 if (pos >= 0 && size > 0 && (long)((unsigned long) pos+(unsigned long) size)>pos && mainParser().checkFilePosition(pos+size))
2346 {
2347 obj.m_pos.setBegin(pos);
2348 obj.m_pos.setLength(size);
2349 obj.m_pos.setId(id);
2350
2351 int objectId = mainParser().readObject(m_input, obj.m_pos);
2352 if (objectId == -1)
2353 {
2354 WPS_DEBUG_MSG(("WPS4Text::objectDataParser: can not find the object %d\n", id));
2355 }
2356 obj.m_id = objectId;
2357 }
2358 else
2359 {
2360 WPS_DEBUG_MSG(("WPS4Text::objectDataParser: bad object position\n"));
2361 }
2362
2363 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
2364 auto val =int(libwps::read16(m_input)); // small number, probably an id
2365 if (val) f << "id=" << val << ",";
2366 for (int i = 0; i < 2; ++i)
2367 {
2368 val =int(libwps::read16(m_input));
2369 if (val) f << "f" << i << "=" << val << ",";
2370 }
2371 obj.m_page =int(libwps::read16(m_input));
2372 int origin[2];
2373 for (int &i : origin) i =int(libwps::read16(m_input));
2374 obj.m_origin=Vec2f(float(origin[0]/1440.),float(origin[1]/1440.));
2375 val =int(libwps::read16(m_input)); // can be big
2376 if (val) f << "f2=" << val << ",";
2377 obj.m_extra = f.str();
2378 if (obj.m_id>=0)
2379 m_state->m_objectMap[bot] = obj;
2380 f.str("");
2381 f << obj;
2382
2383 mess = f.str();
2384 return true;
2385 }
2386
2387 ////////////////////////////////////////////////////////////
2388 // the dttm properties:
2389 ////////////////////////////////////////////////////////////
2390 bool WPS4Text::dttmDataParser(long bot, long /*eot*/, int /*id*/,
2391 long endPos, std::string &mess)
2392 {
2393 mess = "";
2394 if (m_state->m_dateTimeMap.find(bot) != m_state->m_dateTimeMap.end())
2395 {
2396 WPS_DEBUG_MSG(("WPS4Text:dttmDataParser: dttm already exists in this position\n"));
2397 return true;
2398 }
2399
2400 libwps::DebugStream f;
2401
2402 long actPos = m_input->tell();
2403 long length = endPos+1-actPos;
2404 if (length != 42)
2405 {
2406 WPS_DEBUG_MSG(("WPS4Text:dttmDataParser unknown size %ld for dttm data\n", length));
2407 return false;
2408 }
2409
2410 WPS4TextInternal::DateTime form;
2411 int val;
2412 for (int i = 0; i < 3; ++i) // always 0, 0, 0 ?
2413 {
2414 val =libwps::read16(m_input);
2415 if (val) f << "f" << i << "=" << val << ",";
2416 }
2417 form.m_type=libwps::read16(m_input);
2418 val =libwps::read16(m_input); // alway 0 ?
2419 if (val) f << "f3=" << val << ",";
2420 // end unknown
2421 for (int i = 0; i < 16; ++i)
2422 {
2423 val =libwps::readU16(m_input);
2424 if (val) f << "g" << i << "=" << std::hex << val << std::dec << ",";
2425 }
2426 form.m_extra = f.str();
2427 m_state->m_dateTimeMap[bot] = form;
2428 f.str("");
2429 f << form;
2430 mess = f.str();
2431 return true;
2432 }
2433
2434 ////////////////////////////////////////
2435 // VERY LOW LEVEL ( plc )
2436 ////////////////////////////////////////
2437 /** Internal and low level: the structures of a WPS4Text used to parse PLC*/
2438 namespace WPS4PLCInternal
2439 {
2440 /** Internal and low level: the PLC different types and their structures */
2441 struct PLC
2442 {
2443 /** the PLC types */
2444 typedef enum WPS4TextInternal::PLCType PLCType;
2445 /** the way to define the text positions
2446 *
2447 * - P_ABS: absolute position,
2448 * - P_REL: position are relative to the beginning text offset */
2449 typedef enum { P_ABS=0, P_REL, P_UNKNOWN} Position;
2450 /** the type of the content
2451 *
2452 * - T_CST: size is constant
2453 * - T_STRUCT: a structured type ( which unknown size) */
2454 typedef enum { T_CST=0, T_COMPLEX, T_UNKNOWN} Type;
2455
2456 //! constructor
2457 explicit PLC(PLCType w= WPS4TextInternal::Unknown, Position p=P_UNKNOWN, Type t=T_UNKNOWN, unsigned char tChar='\0', int f=1) :
2458 m_type(w), m_pos(p), m_contentType(t), m_textChar(tChar), m_cstFactor(f) {}
2459 PLC(PLC const &orig)=default;
2460 PLC(PLC &&orig)=default;
2461 PLC &operator=(PLC const &orig)=default;
2462 PLC &operator=(PLC &&orig)=default;
2463 //! PLC type
2464 PLCType m_type;
2465 //! the way to define the text positions
2466 Position m_pos;
2467 //! the type of the content
2468 Type m_contentType;
2469 /** the character which appears in the text when this PLC is found
2470 *
2471 * '\\0' means that there is not default character */
2472 unsigned char m_textChar;
2473 //! some data are stored divided by some unit
2474 int m_cstFactor;
2475 };
2476
2477 KnownPLC::KnownPLC()
2478 : m_knowns()
2479 {
2480 createMapping();
2481 }
2482
2483 KnownPLC::~KnownPLC()
2484 {
2485 }
2486
2487 PLC KnownPLC::get(std::string const &name)
2488 {
2489 auto pos = m_knowns.find(name);
2490 if (pos == m_knowns.end()) return PLC();
2491 return pos->second;
2492 }
2493
2494 void KnownPLC::createMapping()
2495 {
2496 m_knowns["BTEP"] =
2497 PLC(WPS4TextInternal::BTE, PLC::P_ABS, PLC::T_CST, '\0', 0x80);
2498 m_knowns["BTEC"] =
2499 PLC(WPS4TextInternal::BTE,PLC::P_ABS, PLC::T_CST, '\0', 0x80);
2500 m_knowns["EOBJ"] =
2501 PLC(WPS4TextInternal::OBJECT,PLC::P_UNKNOWN, PLC::T_COMPLEX, 0x7);
2502 m_knowns["FTNp"] =
2503 PLC(WPS4TextInternal::FTNp,PLC::P_REL, PLC::T_CST, 0x6);
2504 m_knowns["FTNd"] =
2505 PLC(WPS4TextInternal::FTNd,PLC::P_REL, PLC::T_COMPLEX, 0x6);
2506 m_knowns["BKMK"] =
2507 PLC(WPS4TextInternal::BKMK,PLC::P_REL, PLC::T_COMPLEX);
2508 m_knowns["DTTM"] =
2509 PLC(WPS4TextInternal::DTTM,PLC::P_REL, PLC::T_COMPLEX, 0xf);
2510 }
2511 }
2512
2513 bool WPS4Text::readPLC
2514 (WPSEntry const &zone,
2515 std::vector<long> &textPtrs, std::vector<long> &listValues, WPS4Text::DataParser parser)
2516 {
2517 textPtrs.resize(0);
2518 listValues.resize(0);
2519 long size = zone.length();
2520 if (zone.begin() <= 0 || size < 8) return false;
2521 auto plcType = m_state->m_knownPLC.get(zone.type());
2522
2523 libwps::DebugStream f;
2524 ascii().addPos(zone.begin());
2525 m_input->seek(zone.begin(), librevenge::RVNG_SEEK_SET);
2526
2527 long lastPos = 0;
2528 std::vector<DataFOD> fods;
2529 unsigned numElt = 0;
2530 f << "pos=(";
2531 while (numElt*4+4 <= unsigned(size))
2532 {
2533 auto newPos = long(libwps::readU32(m_input));
2534 if (plcType.m_pos == WPS4PLCInternal::PLC::P_UNKNOWN)
2535 {
2536 if (newPos < m_textPositions.begin())
2537 plcType.m_pos = WPS4PLCInternal::PLC::P_REL;
2538 else if (newPos+m_textPositions.begin() > m_textPositions.end())
2539 plcType.m_pos = WPS4PLCInternal::PLC::P_ABS;
2540 else if (plcType.m_textChar=='\0')
2541 {
2542 WPS_DEBUG_MSG(("WPS4Text:readPLC Can not decide position for PLC: %s\n", zone.type().c_str()));
2543 plcType.m_pos = WPS4PLCInternal::PLC::P_REL;
2544 }
2545 else
2546 {
2547 long actPos = m_input->tell();
2548 m_input->seek(newPos, librevenge::RVNG_SEEK_SET);
2549 if (libwps::readU8(m_input) == plcType.m_textChar)
2550 plcType.m_pos = WPS4PLCInternal::PLC::P_ABS;
2551 else plcType.m_pos = WPS4PLCInternal::PLC::P_REL;
2552 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
2553 }
2554 }
2555
2556 if (plcType.m_pos == WPS4PLCInternal::PLC::P_REL)
2557 newPos += m_textPositions.begin();
2558
2559 if (newPos < lastPos ||
2560 newPos > m_textPositions.end())
2561 {
2562 // sometimes the convertissor do not their jobs correctly
2563 // for the last element
2564 if (plcType.m_pos == WPS4PLCInternal::PLC::P_REL &&
2565 newPos == m_textPositions.end()+m_textPositions.begin())
2566 newPos = m_textPositions.end();
2567 else
2568 return false;
2569 }
2570
2571 textPtrs.push_back(newPos);
2572
2573 DataFOD fod;
2574 fod.m_type = DataFOD::ATTR_PLC;
2575 fod.m_pos = newPos;
2576
2577 f << std::hex << newPos << ", ";
2578 if (newPos == m_textPositions.end()) break;
2579
2580 numElt++;
2581 lastPos = newPos;
2582 fods.push_back(fod);
2583 }
2584 f << ")";
2585
2586 if (long(numElt) < 1) return false;
2587
2588 long dataSize = (size-4*long(numElt)-4)/long(numElt);
2589 if (dataSize > 100) return false;
2590 if (size!= long(numElt)*(4+dataSize)+4) return false;
2591
2592 ascii().addNote(f.str().c_str());
2593
2594 if (!dataSize)
2595 {
2596 for (size_t i = 0; i < numElt; ++i)
2597 {
2598 listValues.push_back(-1);
2599 fods[i].m_id = int(m_state->m_plcList.size());
2600 }
2601 WPS4TextInternal::DataPLC plc;
2602 plc.m_name = zone.type();
2603 plc.m_type = plcType.m_type;
2604 m_state->m_plcList.push_back(plc);
2605 m_FODList = mergeSortedFODLists(fods, m_FODList);
2606 return true;
2607 }
2608
2609 // ok we have some data
2610 bool ok = true;
2611 long pos = m_input->tell();
2612 auto pars = parser;
2613 if ((dataSize == 3 || dataSize > 4) && !pars)
2614 pars = &WPS4Text::defDataParser;
2615
2616 for (size_t i = 0; i < numElt; ++i)
2617 {
2618 WPS4TextInternal::DataPLC plc;
2619
2620 if (!pars && dataSize <= 4)
2621 {
2622 switch (dataSize)
2623 {
2624 case 1:
2625 plc.m_value = libwps::readU8(m_input);
2626 break;
2627 case 2:
2628 plc.m_value = libwps::readU16(m_input);
2629 break;
2630 case 4:
2631 plc.m_value = long(libwps::readU32(m_input));
2632 break;
2633 default:
2634 WPS_DEBUG_MSG(("WPS4Text:readPLC: unexpected PLC size\n"));
2635 WPS_FALLTHROUGH;
2636 case 0:
2637 plc.m_value = 0;
2638 }
2639 plc.m_value *=plcType.m_cstFactor;
2640 }
2641 else if (pars)
2642 {
2643 std::string mess;
2644 if (!(this->*pars)(textPtrs[i], textPtrs[i+1], int(i), pos+dataSize-1, mess))
2645 {
2646 ok = false;
2647 break;
2648 }
2649 plc.m_extra = mess;
2650 m_input->seek(pos+dataSize, librevenge::RVNG_SEEK_SET);
2651 }
2652
2653 listValues.push_back(plc.m_value);
2654
2655 fods[i].m_id = int(m_state->m_plcList.size());
2656 fods[i].m_defPos = pos;
2657
2658 plc.m_name = zone.type();
2659 plc.m_type = plcType.m_type;
2660 m_state->m_plcList.push_back(plc);
2661
2662 f.str("");
2663 f << "ZZ" << zone.type() << i << ":" << plc;
2664 ascii().addPos(pos);
2665 ascii().addNote(f.str().c_str());
2666
2667 pos += dataSize;
2668 }
2669
2670 if (ok) m_FODList = mergeSortedFODLists(fods, m_FODList);
2671 return true;
2672 }
2673
2674 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */