Debian Codegraph @

src/lib/ActaText.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
34 #include <cstring>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWTextListener.hxx"
44 #include "MWAWDebug.hxx"
45 #include "MWAWFont.hxx"
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWPageSpan.hxx"
48 #include "MWAWParagraph.hxx"
49 #include "MWAWPictData.hxx"
50 #include "MWAWPosition.hxx"
51 #include "MWAWRSRCParser.hxx"
52
53 #include "ActaParser.hxx"
54
55 #include "ActaText.hxx"
56
57 /** Internal: the structures of a ActaText */
58 namespace ActaTextInternal
59 {
60 ////////////////////////////////////////
61 //! Internal: a topic of a ActaText
62 struct Topic {
63 //! constructor
64 Topic()
65 : m_depth(0)
66 , m_type(0)
67 , m_hidden(0)
68 , m_pageBreak(false)
69 , m_font()
70 , m_labelColor(MWAWColor::black())
71 , m_data()
72 , m_fonts()
73 , m_auxi()
74 , m_extra("")
75 {
76 }
77 //! return true if the topic is valid
78 bool valid() const
79 {
80 return m_depth>0 && (m_type==1 || m_type==2);
81 }
82 //! operator<<
83 friend std::ostream &operator<<(std::ostream &o, Topic const &topic)
84 {
85 if (topic.m_depth > 0)
86 o << "depth=" << topic.m_depth << ",";
87 switch (topic.m_type) {
88 case 1:
89 o << "text,";
90 break;
91 case 2:
92 o << "graphic,";
93 break;
94 default:
95 o << "#type=" << topic.m_type << ",";
96 break;
97 }
98 if (topic.m_pageBreak) o << "pagebreak,";
99 if (topic.m_hidden) o << "hidden[" << topic.m_hidden << "],";
100 if (!topic.m_labelColor.isBlack())
101 o << "labelColor=" << topic.m_labelColor << ",";
102 o << topic.m_extra << ",";
103 return o;
104 }
105 //! the node depth
106 int m_depth;
107 //! the node type: 1=text, 2=graphic
108 int m_type;
109 //! the number of time a topic is hidden by its parents
110 int m_hidden;
111 //! true if a page break exists before the topic
112 bool m_pageBreak;
113 //! the line font
114 MWAWFont m_font;
115 //! the label color
116 MWAWColor m_labelColor;
117 //! the data entries(text or graphic)
118 MWAWEntry m_data;
119 //! the fonts entries(for text)
120 MWAWEntry m_fonts;
121 //! an auxialliary entry(unknown)
122 MWAWEntry m_auxi;
123 //! extra
124 std::string m_extra;
125 };
126
127 ////////////////////////////////////////
128 //! Internal: the state of a ActaText
129 struct State {
130 //! constructor
131 State()
132 : m_topicList()
133 , m_listId(-1)
134 , m_colorList()
135 , m_version(-1)
136 , m_numPages(-1)
137 , m_actualPage(1)
138 {
139 }
140
141 //! set the default color map
142 void setDefaultColorList(int version);
143 //! the topic list
144 std::vector<Topic> m_topicList;
145 //! the list id
146 int m_listId;
147 //! a list colorId -> color
148 std::vector<MWAWColor> m_colorList;
149 //! the file version
150 mutable int m_version;
151 int m_numPages /* the number of pages */, m_actualPage /* the actual page */;
152 };
153
154 void State::setDefaultColorList(int version)
155 {
156 if (m_colorList.size()) return;
157 if (version==3) {
158 uint32_t const defCol[20] = {
159 0x000000, 0xff0000, 0x00ff00, 0x0000ff, 0x00ffff, 0xff00db, 0xffff00, 0x8d02ff,
160 0xff9200, 0x7f7f7f, 0x994914, 0x000000, 0x484848, 0x880000, 0x008600, 0x838300,
161 0xff9200, 0x7f7f7f, 0x994914, 0xfffff
162 };
163 m_colorList.resize(20);
164 for (size_t i = 0; i < 20; i++)
165 m_colorList[i] = defCol[i];
166 return;
167 }
168 }
169 }
170
171 ////////////////////////////////////////////////////////////
172 // constructor/destructor, ...
173 ////////////////////////////////////////////////////////////
174 ActaText::ActaText(ActaParser &parser)
175 : m_parserState(parser.getParserState())
176 , m_state(new ActaTextInternal::State)
177 , m_mainParser(&parser)
178 {
179 }
180
181 ActaText::~ActaText()
182 { }
183
184 int ActaText::version() const
185 {
186 if (m_state->m_version < 0)
187 m_state->m_version = m_parserState->m_version;
188 return m_state->m_version;
189 }
190
191 int ActaText::numPages() const
192 {
193 if (m_state->m_numPages >= 0)
194 return m_state->m_numPages;
195
196 int nPages=1;
197 for (auto const &topic : m_state->m_topicList) {
198 if (topic.m_pageBreak)
199 nPages++;
200 }
201
202 return m_state->m_numPages = nPages;
203 }
204
205 bool ActaText::getColor(int id, MWAWColor &col) const
206 {
207 auto numColor = static_cast<int>(m_state->m_colorList.size());
208 if (!numColor) {
209 m_state->setDefaultColorList(version());
210 numColor = int(m_state->m_colorList.size());
211 }
212 if (id < 0 || id >= numColor)
213 return false;
214 col = m_state->m_colorList[size_t(id)];
215 return true;
216 }
217
218 ////////////////////////////////////////////////////////////
219 // Intermediate level
220 ////////////////////////////////////////////////////////////
221
222 //
223 // find/send the different zones
224 //
225 bool ActaText::createZones()
226 {
227 int vers=version();
228 MWAWInputStreamPtr &input= m_parserState->m_input;
229 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
230 input->seek(vers>=3 ? 2 : 0, librevenge::RVNG_SEEK_SET);
231 while (!input->isEnd()) {
232 if (!readTopic())
233 break;
234 }
235
236 long pos = input->tell();
237 auto val=static_cast<int>(input->readLong(2));
238 if (val || (vers<3 && !input->isEnd())) {
239 MWAW_DEBUG_MSG(("ActaText::createZones: find unexpected end data\n"));
240 ascFile.addPos(pos);
241 ascFile.addNote("Entries(Loose):###");
242 }
243 else {
244 ascFile.addPos(pos);
245 ascFile.addNote("_");
246 }
247 return m_state->m_topicList.size();
248 }
249
250 bool ActaText::sendMainText()
251 {
252 // create the main list
253 auto list = m_mainParser->getMainList();
254 if (!list) {
255 MWAW_DEBUG_MSG(("ActaText::sendMainText: can retrieve the main list\n"));
256 }
257 else
258 m_state->m_listId = list->getId();
259
260 for (auto const &topic : m_state->m_topicList)
261 sendTopic(topic);
262 return true;
263 }
264
265 //
266 // read/send a topic
267 //
268 bool ActaText::readTopic()
269 {
270 MWAWInputStreamPtr &input= m_parserState->m_input;
271 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
272 libmwaw::DebugStream f;
273
274 int vers=version();
275 long pos = input->tell();
276 if (!input->checkPosition(pos+18+4+((vers>=3) ? 4 : 0)))
277 return false;
278 ActaTextInternal::Topic topic;
279 topic.m_depth=static_cast<int>(input->readLong(2)); // checkme
280 topic.m_type=static_cast<int>(input->readLong(2));
281 if (!topic.valid()) {
282 input->seek(pos, librevenge::RVNG_SEEK_SET);
283 return false;
284 }
285 auto flag = static_cast<int>(input->readULong(2)); // 0|1|2|c01c|2002|
286 if (flag & 0x100)
287 f << "current,";
288 if (flag & 0x2000)
289 topic.m_pageBreak=true;
290 flag &= 0xFEFF;
291 if (flag)
292 f << "fl=" << std::hex << flag << std::dec << ",";
293 if (!readFont(topic.m_font, false))
294 f << "foont###,";
295 auto color=static_cast<int>(input->readLong(1));
296 if (color) {
297 MWAWColor fCol;
298 if (getColor(color, fCol))
299 topic.m_labelColor=fCol;
300 else
301 f << "#col=" << color << ",";
302 static bool first=true;
303 if (first) {
304 MWAW_DEBUG_MSG(("ActaText::readTopic: sending color label is not implemented\n"));
305 first = false;
306 }
307 }
308 for (int i = 0; i < 5; i++) {
309 auto val=static_cast<int>(input->readLong(1));
310 if (!val) continue;
311 if (val==1 && i==2) f << "showChild|check,"; // also g2=-1
312 else f << "g" << i << "=" << val << ",";
313 }
314 topic.m_hidden=static_cast<int>(input->readLong(1));
315 topic.m_extra=f.str();
316 f.str("");
317 f << "Entries(Topic):" << topic;
318 #ifdef DEBUG
319 f << "font=[" << topic.m_font.getDebugString(m_parserState->m_fontConverter) << "],";
320 #endif
321
322 ascFile.addPos(pos);
323 ascFile.addNote(f.str().c_str());
324 input->seek(pos+18, librevenge::RVNG_SEEK_SET);
325
326 int numZones= vers<3 ? 1 : topic.m_type==2 ? 2 : 3;
327 for (int z=0; z < numZones; z++) {
328 pos = input->tell();
329 auto sz=long(input->readULong(4));
330 if (sz < 0 || !input->checkPosition(pos+4+sz)) {
331 MWAW_DEBUG_MSG(("ActaText::readTopic: can not read a topic zone\n"));
332 ascFile.addPos(pos);
333 ascFile.addNote("###");
334
335 input->seek(pos, librevenge::RVNG_SEEK_SET);
336 return false;
337 }
338 if (sz==0) {
339 ascFile.addPos(pos);
340 ascFile.addNote("_");
341 }
342
343 MWAWEntry &entry=(z==0) ? topic.m_data : (z==1&&topic.m_type==1) ? topic.m_fonts : topic.m_auxi;
344 entry.setBegin(pos+4);
345 entry.setLength(sz);
346 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
347 }
348
349 m_state->m_topicList.push_back(topic);
350 return true;
351 }
352
353 bool ActaText::sendTopic(ActaTextInternal::Topic const &topic)
354 {
355 MWAWTextListenerPtr listener=m_parserState->m_textListener;
356 if (!listener) {
357 MWAW_DEBUG_MSG(("ActaText::sendTopic: can not find a listener\n"));
358 return false;
359 }
360 if (topic.m_pageBreak)
361 m_mainParser->newPage(++m_state->m_actualPage);
362
363 // useme: find always here a field of size 6 with 3 int=0...
364 if (topic.m_auxi.valid()) {
365 MWAWInputStreamPtr &input= m_parserState->m_input;
366 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
367 libmwaw::DebugStream f;
368 input->seek(topic.m_auxi.begin(), librevenge::RVNG_SEEK_SET);
369 f.str("");
370 f << "Entries(Data1):";
371 if (topic.m_auxi.length()!=6) {
372 MWAW_DEBUG_MSG(("ActaText::readTopic: find unexpected size for data1\n"));
373 f << "###";
374 }
375 else {
376 for (int i=0; i<3; i++) {
377 auto val=static_cast<int>(input->readLong(2));
378 if (val) f << "#f" << i << "=" << val << ",";
379 }
380 }
381 ascFile.addPos(topic.m_auxi.begin()-4);
382 ascFile.addNote(f.str().c_str());
383 }
384
385 // ok, let update the data
386 MWAWParagraph para = listener->getParagraph();
387 if (m_state->m_listId >= 0) {
388 para.m_listLevelIndex=topic.m_depth;
389 para.m_listId = m_state->m_listId;
390 }
391 para.m_margins[1]=0.2*double(topic.m_depth-1);
392 listener->setParagraph(para);
393 listener->setFont(topic.m_font);
394
395 if (topic.m_data.length()==0) {
396 listener->insertEOL();
397 return true;
398 }
399
400 if (topic.m_type==1)
401 return sendText(topic);
402 else
403 return sendGraphic(topic);
404 }
405
406 //
407 // send the text
408 //
409 bool ActaText::sendText(ActaTextInternal::Topic const &topic)
410 {
411 MWAWTextListenerPtr listener=m_parserState->m_textListener;
412 if (!listener) {
413 MWAW_DEBUG_MSG(("ActaText::sendText: can not find a listener\n"));
414 return false;
415 }
416 if (!topic.m_data.valid()) {
417 MWAW_DEBUG_MSG(("ActaText::sendText: can not find my data\n"));
418 listener->insertEOL();
419 return false;
420 }
421 MWAWInputStreamPtr &input= m_parserState->m_input;
422 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
423 libmwaw::DebugStream f;
424
425 // first read the font list if it exists
426 std::map<long,MWAWFont> fontMap;
427 if (topic.m_fonts.valid()) {
428 input->seek(topic.m_fonts.begin(), librevenge::RVNG_SEEK_SET);
429 auto n= static_cast<int>(input->readULong(2));
430
431 f.str("");
432 f << "Entries(CharPLC):n=" << n << ",";
433 if (2+20*n!=long(topic.m_fonts.length())) {
434 MWAW_DEBUG_MSG(("ActaText::sendText: find unexpected number of font\n"));
435 f << "###";
436 ascFile.addPos(topic.m_fonts.begin()-4);
437 ascFile.addNote(f.str().c_str());
438 }
439 else {
440 ascFile.addPos(topic.m_fonts.begin()-4);
441 ascFile.addNote(f.str().c_str());
442
443 for (int i = 0; i < n; i++) {
444 long pPos = input->tell();
445 f.str("");
446 f << "CharPLC-" << i << ":";
447
448 auto cPos = static_cast<long>(input->readULong(4));
449 if (cPos) f << "cPos=" << cPos << ",";
450 int dim[2];
451 for (int &j : dim)
452 j=static_cast<int>(input->readLong(2));
453 f << "h=" << dim[0] << ","; // checkme, simillar to font.size()+2
454 f << "f0=" << dim[1] << ","; // seems slightly less than font.size()
455
456 MWAWFont font;
457 if (!readFont(font, true))
458 f << "###";
459 else
460 fontMap[cPos]=font;
461 #ifdef DEBUG
462 f << "font=[" << font.getDebugString(m_parserState->m_fontConverter) << "],";
463 #endif
464
465 for (int j = 0; j < 3; j++) { // always 0
466 auto val = static_cast<int>(input->readLong(2));
467 if (val)
468 f << "f" << j+1 << "=" << val << ",";
469 }
470 input->seek(pPos+20, librevenge::RVNG_SEEK_SET);
471 ascFile.addPos(pPos);
472 ascFile.addNote(f.str().c_str());
473 }
474 }
475 }
476
477 input->seek(topic.m_data.begin(), librevenge::RVNG_SEEK_SET);
478 long sz=topic.m_data.length();
479 f.str("");
480 f << "Entries(Text):";
481 for (long i=0; i < sz; i++) {
482 auto fIt = fontMap.find(i);
483 if (fIt!=fontMap.end())
484 listener->setFont(fIt->second);
485 auto c=static_cast<char>(input->readULong(1));
486 switch (c) {
487 case 0x9:
488 listener->insertTab();
489 break;
490 case 0xd:
491 listener->insertEOL(true);
492 break;
493 default:
494 listener->insertCharacter(static_cast<unsigned char>(c));
495 }
496 f << c;
497 }
498 listener->insertEOL();
499 ascFile.addPos(topic.m_data.begin()-4);
500 ascFile.addNote(f.str().c_str());
501 return true;
502 }
503
504 //
505 // send a graphic
506 //
507 bool ActaText::sendGraphic(ActaTextInternal::Topic const &topic)
508 {
509 MWAWTextListenerPtr listener=m_parserState->m_textListener;
510 if (!listener) {
511 MWAW_DEBUG_MSG(("ActaText::sendGraphic: can not find a listener\n"));
512 return false;
513 }
514 if (!topic.m_data.valid()) {
515 MWAW_DEBUG_MSG(("ActaText::sendGraphic: can not find my data\n"));
516 listener->insertEOL();
517 return false;
518 }
519 long dataSz=topic.m_data.length();
520 MWAWInputStreamPtr &input= m_parserState->m_input;
521 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
522 long pos = topic.m_data.begin();
523
524 ascFile.addPos(pos-4);
525 ascFile.addNote("Entries(Graphic):");
526 ascFile.skipZone(pos, pos+dataSz-1);
527
528 MWAWBox2f box;
529 input->seek(pos, librevenge::RVNG_SEEK_SET);
530 auto res = MWAWPictData::check(input, static_cast<int>(dataSz), box);
531 if (res == MWAWPict::MWAW_R_BAD) {
532 MWAW_DEBUG_MSG(("ActaText::sendGraphic: can not find the picture\n"));
533 ascFile.addPos(pos);
534 ascFile.addNote("###");
535
536 return true;
537 }
538
539 librevenge::RVNGBinaryData file;
540 input->seek(pos, librevenge::RVNG_SEEK_SET);
541 input->readDataBlock(dataSz, file);
542
543 MWAWPosition posi(MWAWVec2f(0,0), box.size(), librevenge::RVNG_POINT);
544 posi.setRelativePosition(MWAWPosition::Char);
545 listener->insertPicture(posi, MWAWEmbeddedObject(file, "image/pict"));
546 listener->insertEOL();
547 #ifdef DEBUG_WITH_FILES
548 static int volatile pictName = 0;
549 libmwaw::DebugStream f;
550 f << "DATA-" << ++pictName;
551 libmwaw::Debug::dumpFile(file, f.str().c_str());
552 #endif
553
554 return true;
555 }
556
557 //////////////////////////////////////////////
558 // Fonts
559 //////////////////////////////////////////////
560 bool ActaText::readFont(MWAWFont &font, bool inPLC)
561 {
562 font = MWAWFont();
563
564 MWAWInputStreamPtr &input= m_parserState->m_input;
565 libmwaw::DebugStream f;
566
567 font.setId(static_cast<int>(input->readLong(2)));
568 int flag[2];
569 for (int i = 0; i < 2; i++)
570 flag[inPLC ? i : 1-i] = static_cast<int>(input->readULong(1));
571 uint32_t flags = 0;
572 if (flag[0]&0x1) flags |= MWAWFont::boldBit;
573 if (flag[0]&0x2) flags |= MWAWFont::italicBit;
574 if (flag[0]&0x4) font.setUnderlineStyle(MWAWFont::Line::Simple);
575 if (flag[0]&0x8) flags |= MWAWFont::embossBit;
576 if (flag[0]&0x10) flags |= MWAWFont::shadowBit;
577 flag[0] &= 0xE0;
578 for (int i = 0; i < 2; i++) {
579 if (flag[i])
580 f << "#fl" << i << "=" << std::hex << flag << std::dec << ",";
581 }
582 font.setFlags(flags);
583 font.setSize(static_cast<float>(input->readLong(2)));
584
585 font.m_extra=f.str();
586 return true;
587 }
588
589 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: