Debian Codegraph @

src/lib/MarinerWrtGraph.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 <cmath>
35 #include <iomanip>
36 #include <iostream>
37 #include <limits>
38 #include <map>
39 #include <sstream>
40
41 #include <librevenge/librevenge.h>
42
43 #include "MWAWCell.hxx"
44 #include "MWAWTextListener.hxx"
45 #include "MWAWFont.hxx"
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWGraphicEncoder.hxx"
48 #include "MWAWGraphicListener.hxx"
49 #include "MWAWGraphicShape.hxx"
50 #include "MWAWGraphicStyle.hxx"
51 #include "MWAWParagraph.hxx"
52 #include "MWAWPictMac.hxx"
53 #include "MWAWPosition.hxx"
54 #include "MWAWSubDocument.hxx"
55
56 #include "MarinerWrtParser.hxx"
57
58 #include "MarinerWrtGraph.hxx"
59
60 /** Internal: the structures of a MarinerWrtGraph */
61 namespace MarinerWrtGraphInternal
62 {
63 ////////////////////////////////////////
64 //! Internal: the struct use to store a pattern in MarinerWrtGraph
65 struct Pattern {
66 //! constructor with default pattern
67 Pattern()
68 : m_uniform(true)
69 , m_pattern()
70 , m_percent(1)
71 {
72 }
73 //! constructor ( 4 uint16_t by pattern )
74 Pattern(uint16_t const *pat, bool uniform)
75 : m_uniform(uniform)
76 , m_pattern()
77 , m_percent(1)
78 {
79 m_pattern.m_dim=MWAWVec2i(8,8);
80 m_pattern.m_colors[0]=MWAWColor::white();
81 m_pattern.m_colors[1]=MWAWColor::black();
82 for (size_t i=0; i < 4; ++i) {
83 uint16_t val=pat[i];
84 m_pattern.m_data.push_back(static_cast<unsigned char>(val>>8));
85 m_pattern.m_data.push_back(static_cast<unsigned char>(val&0xFF));
86 }
87 int numOnes=0;
88 for (size_t j=0; j < 8; ++j) {
89 uint8_t val=m_pattern.m_data[j];
90 for (int b=0; b < 8; b++) {
91 if (val&1) ++numOnes;
92 val = uint8_t(val>>1);
93 }
94 }
95 m_percent=float(numOnes)/64.f;
96 }
97 //! a flag to know if the pattern is uniform or not
98 bool m_uniform;
99 //! the graphic style pattern
100 MWAWGraphicStyle::Pattern m_pattern;
101 //! the percent color
102 float m_percent;
103 };
104
105 ////////////////////////////////////////
106 //! Internal: the struct use to store a token entry
107 struct Token {
108 //! constructor
109 Token()
110 : m_type(-1)
111 , m_highType(-1)
112 , m_dim(0,0)
113 , m_refType(0)
114 , m_refId(0)
115 , m_fieldType(0)
116 , m_value("")
117 , m_pictData()
118 , m_pictId(0)
119 , m_valPictId(0)
120 , m_pictBorderColor(MWAWColor::black())
121 , m_ruleType(0)
122 , m_rulePattern(0)
123 , m_parsed(true)
124 , m_extra("")
125 {
126 for (auto &id : m_id) id=0;
127 for (auto &type : m_pictBorderType) type=0;
128 for (auto &width : m_pictBorderWidth) width=0;
129 }
130 //! return true if the picture has some border
131 bool hasPictBorders() const
132 {
133 for (auto type : m_pictBorderType)
134 if (type) return true;
135 return false;
136 }
137 //! add border properties
138 void addPictBorder(MWAWGraphicStyle &style) const
139 {
140 if (!hasPictBorders()) return;
141 for (int i = 0; i < 4; i++) {
142 if (m_pictBorderType[i] <=0)
143 continue;
144 MWAWBorder border;
145 border.m_color=m_pictBorderColor;
146 switch (m_pictBorderType[i]) {
147 case 1: // single[w=0.5]
148 border.m_width = 0.5;
149 MWAW_FALLTHROUGH;
150 case 2:
151 border.m_style = MWAWBorder::Simple;
152 break;
153 case 3:
154 border.m_style = MWAWBorder::Dot;
155 break;
156 case 4:
157 border.m_style = MWAWBorder::Dash;
158 break;
159 case 5:
160 border.m_width = 2;
161 break;
162 case 6:
163 border.m_width = 3;
164 break;
165 case 7:
166 border.m_width = 6;
167 break;
168 case 8:
169 border.m_type = MWAWBorder::Double;
170 break;
171 case 10:
172 border.m_type = MWAWBorder::Double;
173 border.m_widthsList.resize(3,1.);
174 border.m_widthsList[0]=2.0;
175 break;
176 case 11:
177 border.m_type = MWAWBorder::Double;
178 border.m_widthsList.resize(3,1.);
179 border.m_widthsList[2]=2.0;
180 break;
181 case 9:
182 border.m_type = MWAWBorder::Double;
183 border.m_width = 2;
184 break;
185 default:
186 border.m_style = MWAWBorder::None;
187 break;
188 }
189 static int const wh[] = { libmwaw::LeftBit, libmwaw::TopBit, libmwaw::RightBit, libmwaw::BottomBit };
190 style.setBorders(wh[i], border);
191 }
192 }
193 //! operator<<
194 friend std::ostream &operator<<(std::ostream &o, Token const &tkn);
195 //! the token id and the graph? id
196 long m_id[2];
197 //! the field type
198 int m_type;
199 //! the high byte of the field type
200 int m_highType;
201 //! the dimension
202 MWAWVec2i m_dim;
203 // for footnote
204 //! the zone to used type
205 int m_refType;
206 //! the zone to used id
207 uint32_t m_refId;
208 // for field
209 //! the field type
210 int m_fieldType;
211 //! the token value
212 std::string m_value;
213 // for picture
214 //! the picture data
215 MWAWEntry m_pictData;
216 //! a picture id
217 long m_pictId;
218 //! a optional picture id
219 long m_valPictId;
220 //! the pict border color
221 MWAWColor m_pictBorderColor;
222 //! the pict border type
223 int m_pictBorderType[4];
224 //! the pict border width
225 float m_pictBorderWidth[4];
226 // for rule
227 //! the rule type
228 int m_ruleType;
229 //! the rule pattern
230 int m_rulePattern;
231 //! true if the token has been send to a listener
232 mutable bool m_parsed;
233 //! some extra data
234 std::string m_extra;
235 };
236
237 std::ostream &operator<<(std::ostream &o, Token const &tkn)
238 {
239 if (tkn.m_id[0]) o << "id=" << std::hex << tkn.m_id[0] << std::dec << ",";
240 if (tkn.m_id[1]) o << "id2=" << std::hex << tkn.m_id[1] << std::dec << ",";
241 switch (tkn.m_type) {
242 case -1:
243 break;
244 case 0x14:
245 o << "graph";
246 if (tkn.m_highType) o << "[" << tkn.m_highType << "]";
247 o << ",";
248 break;
249 case 0x17:
250 o << "date";
251 if (tkn.m_highType!=1) o << "[" << tkn.m_highType << "]";
252 o << ",";
253 break;
254 case 0x18:
255 o << "time";
256 if (tkn.m_highType!=1) o << "[" << tkn.m_highType << "]";
257 o << ",";
258 break;
259 case 0x19:
260 o << "pagenumber";
261 if (tkn.m_highType!=1) o << "[" << tkn.m_highType << "]";
262 o << ",";
263 break;
264 case 0x1e:
265 o << "footnote[mark]";
266 if (tkn.m_highType!=9) o << "[" << tkn.m_highType << "]";
267 o << ",";
268 break;
269 case 0x1f:
270 o << "footnote[content]";
271 if (tkn.m_highType!=1) o << "[" << tkn.m_highType << "]";
272 o << ",";
273 break;
274 case 0x23:
275 o << "rule";
276 if (tkn.m_highType!=1) o << "[" << tkn.m_highType << "]";
277 o << ",";
278 break;
279 case 0x24:
280 o << "field[formula]";
281 if (tkn.m_highType!=9) o << "[" << tkn.m_highType << "]";
282 o << ",";
283 break;
284 default:
285 o << "#type=" << tkn.m_type << "[" << tkn.m_highType << "],";
286 }
287 if (tkn.m_fieldType)
288 o << "field[type/val]=" << tkn.m_fieldType << ",";
289 if (tkn.m_dim[0] || tkn.m_dim[1])
290 o << "dim=" << tkn.m_dim << ",";
291 if (tkn.m_value.length())
292 o << "val=" << tkn.m_value << ",";
293 if (tkn.m_pictId)
294 o << "pictId=" << std::hex << tkn.m_pictId << std::dec << ",";
295 if (tkn.m_valPictId && tkn.m_valPictId != tkn.m_pictId)
296 o << "pictId[inValue]=" << std::hex << tkn.m_valPictId << std::dec << ",";
297 if (!tkn.m_pictBorderColor.isBlack())
298 o << "pict[color]=" << tkn.m_pictBorderColor << ",";
299 if (tkn.hasPictBorders()) {
300 o << "pict[borders]=[";
301 for (int i=0; i < 4; ++i)
302 o << tkn.m_pictBorderType[i] << ":" << tkn.m_pictBorderWidth[i] << ",";
303 o << "],";
304 }
305 if (tkn.m_refId) {
306 o << "zone[ref]=";
307 if (tkn.m_refType==0xe)
308 o << "footnote[" << std::hex << (tkn.m_refId&0xFFFFFFF) << std::dec << "],";
309 else
310 o << "#type" << tkn.m_refType << "[" << std::hex << (tkn.m_refId&0xFFFFFFF) << std::dec << "],";
311 }
312 switch (tkn.m_ruleType) {
313 case 0:
314 break; // no
315 case 1:
316 o << "rule[hairline],";
317 break;
318 case 2:
319 break; // single
320 case 3:
321 o << "rule[w=2],";
322 break;
323 case 4:
324 o << "rule[w=3],";
325 break;
326 case 5:
327 o << "rule[w=4],";
328 break;
329 case 6:
330 o << "rule[double],";
331 break;
332 case 7:
333 o << "rule[double,w=2],";
334 break;
335 default:
336 o << "#rule[type=" << tkn.m_ruleType << "],";
337 break;
338 }
339 if (tkn.m_rulePattern)
340 o << "rule[pattern]=" << tkn.m_rulePattern << ",";
341 o << tkn.m_extra;
342 return o;
343 }
344
345 ////////////////////////////////////////
346 //! Internal: the struct use to store a ps zone of a MarinerWrtGraph
347 struct PSZone {
348 //! constructor
349 PSZone()
350 : m_pos()
351 , m_type(0)
352 , m_id(0)
353 , m_parsed(false)
354 , m_extra("")
355 {
356 }
357 //! operator<<
358 friend std::ostream &operator<<(std::ostream &o, PSZone const &file)
359 {
360 if (file.m_type) o << "type=" << file.m_type << ",";
361 if (file.m_id > 0) o << "id=" << std::hex << file.m_id << std::dec << ",";
362 o << file.m_extra;
363 return o;
364 }
365 //! the file position
366 MWAWEntry m_pos;
367 //! a local type?
368 int m_type;
369 //! an id
370 long m_id;
371 //! a flag to know if the data has been sent
372 mutable bool m_parsed;
373 //! some extra data
374 std::string m_extra;
375 };
376
377 ////////////////////////////////////////
378 //! Internal: the struct use to store a zone of a MarinerWrtGraph
379 struct Zone {
380 //! constructor
381 Zone()
382 : m_tokenMap()
383 , m_psZoneMap()
384 {
385 }
386 //! the map id->token
387 std::map<long, Token> m_tokenMap;
388 //! the map id->entry to a psfile
389 std::map<long, PSZone> m_psZoneMap;
390 };
391
392 ////////////////////////////////////////
393 //! Internal: the state of a MarinerWrtGraph
394 struct State {
395 //! constructor
396 State()
397 : m_zoneMap()
398 , m_patternList()
399 , m_numPages(0)
400 {
401 }
402
403 //! set the default pattern map
404 void setDefaultPatternList(int version);
405 //! return a reference to a textzone ( if zone not exists, created it )
406 Zone &getZone(int id)
407 {
408 auto it = m_zoneMap.find(id);
409 if (it != m_zoneMap.end())
410 return it->second;
411 it = m_zoneMap.insert
412 (std::map<int,Zone>::value_type(id,Zone())).first;
413 return it->second;
414 }
415
416 //! a map id -> textZone
417 std::map<int,Zone> m_zoneMap;
418 //! a list patternId -> percent
419 std::vector<Pattern> m_patternList;
420
421 int m_numPages /* the number of pages */;
422 };
423
424 void State::setDefaultPatternList(int /*version*/)
425 {
426 if (m_patternList.size()) return;
427 static uint16_t const dataV1[4*29] = {
428 0x0000,0x0000,0x0000,0x0000,0x8800,0x0200,0x8800,0x2000,
429 0x8800,0x2200,0x8800,0x2200,0x8800,0xaa00,0x8800,0xaa00,
430 0x8822,0x8822,0x8822,0x8822,0x10aa,0x00aa,0x01aa,0x00aa,
431 0xaa11,0xaa44,0xaa11,0xaa44,0xaa11,0xaa44,0xaa11,0xaa44,
432 0xaa55,0xaa55,0xaa55,0xaa55,0x55aa,0x55bb,0x55aa,0x55bb,
433 0x55ee,0x55bb,0x55ee,0x55bb,0x55ee,0x55ff,0x55ee,0x55ff,
434 0xdd77,0xdd77,0xdd77,0xdd77,0xdd7f,0xddf7,0xdd7f,0xddf7,
435 0xddff,0x77ff,0xddff,0x77ff,0xf7ff,0xddff,0x7fff,0xddff,
436 0xffff,0xffff,0xffff,0xffff,
437 0xffff,0x0000,0xffff,0x0000,0xcccc,0xcccc,0xcccc,0xcccc,
438 0xcc66,0x3399,0xcc66,0x3399,0x9933,0x66cc,0x9933,0x66cc,
439 0xcccc,0x3333,0xcccc,0x3333,0x33ff,0xccff,0x33ff,0xccff,
440 0xff00,0x0000,0xff00,0x0000,0x8888,0x8888,0x8888,0x8888,
441 0x8844,0x2211,0x8844,0x2211,0x1122,0x4488,0x1122,0x4488,
442 0xff88,0x8888,0xff88,0x8888,0x5522,0x5588,0x5522,0x5588
443 };
444 for (size_t i=0; i < 29; ++i)
445 m_patternList.push_back(Pattern(dataV1+4*i, i<17));
446 }
447
448 ////////////////////////////////////////
449 //! Internal: the subdocument of a MarinerWrtGraph
450 class SubDocument final : public MWAWSubDocument
451 {
452 public:
453 //! constructor
454 SubDocument(MarinerWrtGraph &pars, MWAWInputStreamPtr const &input, int id)
455 : MWAWSubDocument(pars.m_mainParser, input, MWAWEntry())
456 , m_graphParser(&pars)
457 , m_id(id)
458 {
459 }
460
461 //! destructor
462 ~SubDocument() final {}
463
464 //! operator!=
465 bool operator!=(MWAWSubDocument const &doc) const final;
466
467 //! the parser function
468 void parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType type) final;
469
470 protected:
471 /** the graph parser */
472 MarinerWrtGraph *m_graphParser;
473 //! the zone id
474 int m_id;
475
476 private:
477 SubDocument(SubDocument const &orig) = delete;
478 SubDocument &operator=(SubDocument const &orig) = delete;
479 };
480
481 void SubDocument::parse(MWAWListenerPtr &listener, libmwaw::SubDocumentType /*type*/)
482 {
483 if (!listener.get()) {
484 MWAW_DEBUG_MSG(("MarinerWrtGraphInternal::SubDocument::parse: no listener\n"));
485 return;
486 }
487 if (!m_graphParser) {
488 MWAW_DEBUG_MSG(("MarinerWrtGraphInternal::SubDocument::parse: no parser\n"));
489 return;
490 }
491
492 long pos = m_input->tell();
493 m_graphParser->sendText(m_id);
494 m_input->seek(pos, librevenge::RVNG_SEEK_SET);
495 }
496
497 bool SubDocument::operator!=(MWAWSubDocument const &doc) const
498 {
499 if (MWAWSubDocument::operator!=(doc)) return true;
500 auto const *sDoc = dynamic_cast<SubDocument const *>(&doc);
501 if (!sDoc) return true;
502 if (m_graphParser != sDoc->m_graphParser) return true;
503 if (m_id != sDoc->m_id) return true;
504 return false;
505 }
506 }
507
508 ////////////////////////////////////////////////////////////
509 // constructor/destructor, ...
510 ////////////////////////////////////////////////////////////
511 MarinerWrtGraph::MarinerWrtGraph(MarinerWrtParser &parser)
512 : m_parserState(parser.getParserState())
513 , m_state(new MarinerWrtGraphInternal::State)
514 , m_mainParser(&parser)
515 {
516 }
517
518 MarinerWrtGraph::~MarinerWrtGraph()
519 {
520 }
521
522 int MarinerWrtGraph::version() const
523 {
524 return m_parserState->m_version;
525 }
526
527 int MarinerWrtGraph::numPages() const
528 {
529 if (m_state->m_numPages)
530 return m_state->m_numPages;
531 int nPages = 0;
532 m_state->m_numPages = nPages;
533 return nPages;
534 }
535
536 float MarinerWrtGraph::getPatternPercent(int id) const
537 {
538 auto numPattern = static_cast<int>(m_state->m_patternList.size());
539 if (!numPattern) {
540 m_state->setDefaultPatternList(version());
541 numPattern = int(m_state->m_patternList.size());
542 }
543 if (id < 0 || id >= numPattern)
544 return -1.;
545 return m_state->m_patternList[size_t(id)].m_percent;
546 }
547
548 void MarinerWrtGraph::sendText(int zoneId)
549 {
550 if (zoneId)
551 m_mainParser->sendText(zoneId);
552 }
553
554 void MarinerWrtGraph::sendToken(int zoneId, long tokenId)
555 {
556 MWAWTextListenerPtr listener=m_parserState->m_textListener;
557 if (!listener) {
558 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendToken: can not the listener\n"));
559 return;
560 }
561 if (m_state->m_zoneMap.find(zoneId)==m_state->m_zoneMap.end()) {
562 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendToken: can not find zone %d\n", zoneId));
563 return;
564 }
565 auto &zone = m_state->getZone(zoneId);
566 if (zone.m_tokenMap.find(tokenId)== zone.m_tokenMap.end()) {
567 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendToken: can not find token id %ld\n", tokenId));
568 return;
569 }
570 auto const &token = zone.m_tokenMap.find(tokenId)->second;
571 token.m_parsed = true;
572 switch (token.m_type) {
573 case 0x14:
574 sendPicture(token);
575 return;
576 case 0x17:
577 if (token.m_value.length()) {
578 for (char c : token.m_value)
579 listener->insertCharacter(static_cast<unsigned char>(c));
580 }
581 else
582 listener->insertField(MWAWField(MWAWField::Date));
583 return;
584 case 0x18:
585 if (token.m_value.length()) {
586 for (char c : token.m_value)
587 listener->insertCharacter(static_cast<unsigned char>(c));
588 }
589 else
590 listener->insertField(MWAWField(MWAWField::Time));
591 return;
592 case 0x19: // fixme this can also be page count
593 switch (token.m_fieldType) {
594 case 0:
595 case 4: // big roman
596 case 6: // small roman
597 listener->insertField(MWAWField(MWAWField::PageNumber));
598 break;
599 case 1:
600 case 5: // big roman
601 case 7: // small roman
602 listener->insertField(MWAWField(MWAWField::PageCount));
603 break;
604 case 2:
605 listener->insertField(MWAWField(MWAWField::PageNumber));
606 listener->insertUnicodeString(librevenge::RVNGString(" of "));
607 listener->insertField(MWAWField(MWAWField::PageCount));
608 break;
609 case 3:
610 listener->insertField(MWAWField(MWAWField::PageNumber));
611 listener->insertChar('/');
612 listener->insertField(MWAWField(MWAWField::PageCount));
613 break;
614 default:
615 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendToken: find unknown pagenumber style\n"));
616 listener->insertField(MWAWField(MWAWField::PageNumber));
617 break;
618 }
619 return;
620 case 0x1e: {
621 bool endNote=true;
622 int fZoneId = m_mainParser->getZoneId(token.m_refId, endNote);
623 MWAWSubDocumentPtr subdoc(new MarinerWrtGraphInternal::SubDocument(*this, m_parserState->m_input, fZoneId));
624 listener->insertNote(MWAWNote(endNote ? MWAWNote::EndNote : MWAWNote::FootNote), subdoc);
625 return;
626 }
627 case 0x1f: // footnote content, ok to ignore
628 return;
629 case 0x23:
630 sendRule(token);
631 return;
632 case 0x24: // field
633 listener->insertChar('[');
634 if (token.m_value.length()) {
635 for (char c : token.m_value)
636 listener->insertCharacter(static_cast<unsigned char>(c));
637 }
638 else
639 listener->insertUnicodeString(librevenge::RVNGString("Merge Field"));
640 listener->insertChar(']');
641 return;
642 default:
643 break;
644 }
645
646 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendToken: sending type %x is not unplemented\n", static_cast<unsigned int>(token.m_type)));
647 }
648
649 void MarinerWrtGraph::sendRule(MarinerWrtGraphInternal::Token const &tkn)
650 {
651 MWAWListenerPtr listener=m_parserState->m_textListener;
652 if (!listener) {
653 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendRule: can not find the listener\n"));
654 return;
655 }
656 MWAWVec2i const &sz=tkn.m_dim;
657 if (sz[0] < 0 || sz[1] < 0 || (sz[0]==0 && sz[1]==0)) {
658 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendRule: the rule size seems bad\n"));
659 return;
660 }
661 std::vector<float> listW;
662 switch (tkn.m_ruleType) {
663 case 0: // no width
664 return;
665 case 1:
666 listW.resize(1,0.5f);
667 break;
668 default:
669 case 2:
670 listW.resize(1,1);
671 break;
672 case 6:
673 listW.resize(3,1);
674 break;
675 case 3:
676 listW.resize(1,2);
677 break;
678 case 7:
679 listW.resize(3,2);
680 break;
681 case 4:
682 listW.resize(1,3);
683 break;
684 case 5:
685 listW.resize(1,4);
686 break;
687 }
688
689 MarinerWrtGraphInternal::Pattern pat;
690 if (tkn.m_rulePattern >= 0 && tkn.m_rulePattern < static_cast<int>(m_state->m_patternList.size()))
691 pat = m_state->m_patternList[size_t(tkn.m_rulePattern)];
692 else {
693 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendRule: can not find pattern\n"));
694 }
695 // retrieve the actual font to get the ruler color + a basic estimation of the line height
696 MWAWFont actFont=listener->getFont();
697 MWAWColor col;
698 actFont.getColor(col);
699 float lineH = actFont.size() > 0 ? actFont.size() : 12.f;
700 float totalWidth=0;
701 for (float l : listW) totalWidth += l;
702 if (lineH<totalWidth) lineH=totalWidth;
703 MWAWBox2f box(MWAWVec2f(0,0), MWAWVec2f(sz)+MWAWVec2f(0,lineH));
704 MWAWPosition pos(box[0], box[1], librevenge::RVNG_POINT);
705 pos.setRelativePosition(MWAWPosition::Char);
706 MWAWGraphicStyle pStyle;
707 MWAWGraphicShape shape;
708 if (pat.m_uniform) {
709 pStyle.m_lineWidth=listW[0];
710 pStyle.m_lineColor=MWAWColor::barycenter(pat.m_percent,col,1.f-pat.m_percent,MWAWColor::white());
711 shape=MWAWGraphicShape::line(MWAWVec2f(0,0), MWAWVec2f(sz));
712 }
713 else {
714 pStyle.m_lineWidth=0;
715 pat.m_pattern.m_colors[1]=col;
716 pStyle.setPattern(pat.m_pattern);
717 shape=MWAWGraphicShape::rectangle(MWAWBox2f(MWAWVec2f(0,0), MWAWVec2f(float(sz[0]),listW[0])));
718 }
719
720 if (listW.size()==1) {
721 shape.m_bdBox=box;
722 listener->insertShape(pos,shape, pStyle);
723 return;
724 }
725 MWAWGraphicEncoder graphicEncoder;
726 MWAWGraphicListener graphicListener(*m_parserState, MWAWBox2f(MWAWVec2f(0,0), MWAWVec2f(sz)+MWAWVec2f(0,lineH)), &graphicEncoder);
727 graphicListener.startDocument();
728 float actH = (lineH-totalWidth)/2.f;
729 for (size_t l=0; l < listW.size(); ++l) {
730 if ((l%2)==0) {
731 MWAWPosition linePos(MWAWVec2f(0,actH), MWAWVec2f(sz)+MWAWVec2f(0,listW[l]),librevenge::RVNG_POINT);
732 linePos.m_anchorTo=MWAWPosition::Page;
733 graphicListener.insertShape(linePos, shape, pStyle);
734 }
735 actH += listW[l];
736 }
737 graphicListener.endDocument();
738 MWAWEmbeddedObject picture;
739 if (graphicEncoder.getBinaryResult(picture))
740 listener->insertPicture(pos,picture);
741 }
742
743 void MarinerWrtGraph::sendPicture(MarinerWrtGraphInternal::Token const &tkn)
744 {
745 if (!tkn.m_pictData.valid()) {
746 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendPicture: can not find the graph\n"));
747 return;
748 }
749
750 MWAWInputStreamPtr &input= m_parserState->m_input;
751 long pos = input->tell();
752 input->seek(tkn.m_pictData.begin(), librevenge::RVNG_SEEK_SET);
753 librevenge::RVNGBinaryData data;
754 input->readDataBlock(tkn.m_pictData.length(), data);
755
756 #ifdef DEBUG_WITH_FILES
757 static int volatile pictName = 0;
758 std::stringstream fName;
759 fName << "Pict" << ++pictName << ".pct";
760 libmwaw::Debug::dumpFile(data, fName.str().c_str());
761
762 m_parserState->m_asciiFile.skipZone(tkn.m_pictData.begin(),tkn.m_pictData.end()-1);
763 #endif
764 MWAWVec2i dim(tkn.m_dim);
765 if (dim[0] <= 0 || dim[1] <= 0) {
766 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendPicture: can not find the picture dim\n"));
767 dim = MWAWVec2i(100,100);
768 }
769 MWAWPosition posi(MWAWVec2f(0,0),MWAWVec2f(dim),librevenge::RVNG_POINT);
770 posi.setRelativePosition(MWAWPosition::Char);
771 MWAWGraphicStyle style;
772 tkn.addPictBorder(style);
773 if (m_parserState->m_textListener)
774 m_parserState->m_textListener->insertPicture(posi, MWAWEmbeddedObject(data, "image/pict"), style);
775 input->seek(pos, librevenge::RVNG_SEEK_SET);
776 }
777
778 void MarinerWrtGraph::sendPSZone(MarinerWrtGraphInternal::PSZone const &ps, MWAWPosition const &pos)
779 {
780 ps.m_parsed = true;
781
782 if (!ps.m_pos.valid()) {
783 MWAW_DEBUG_MSG(("MarinerWrtGraph::sendPicture: can not find the graph\n"));
784 return;
785 }
786
787 MWAWInputStreamPtr &input= m_parserState->m_input;
788 long actPos = input->tell();
789 input->seek(ps.m_pos.begin(), librevenge::RVNG_SEEK_SET);
790 librevenge::RVNGBinaryData data;
791 input->readDataBlock(ps.m_pos.length(), data);
792
793 #ifdef DEBUG_WITH_FILES
794 static int volatile pictName = 0;
795 std::stringstream fName;
796 fName << "PS" << ++pictName << ".ps";
797 libmwaw::Debug::dumpFile(data, fName.str().c_str());
798
799 m_parserState->m_asciiFile.skipZone(ps.m_pos.begin(),ps.m_pos.end()-1);
800 #endif
801 MWAWPosition pictPos(pos);
802 if (pos.size()[0] <= 0 || pos.size()[1] <= 0)
803 pictPos.setSize(MWAWVec2f(100,100));
804 if (m_parserState->m_textListener)
805 m_parserState->m_textListener->insertPicture(pictPos, MWAWEmbeddedObject(data, "image/ps"));
806 input->seek(actPos, librevenge::RVNG_SEEK_SET);
807 }
808
809 ////////////////////////////////////////////////////////////
810 //
811 // Intermediate level
812 //
813 ////////////////////////////////////////////////////////////
814
815 bool MarinerWrtGraph::readToken(MarinerWrtEntry const &entry, int zoneId)
816 {
817 if (entry.length() < 3) {
818 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken: data seems to short\n"));
819 return false;
820 }
821 MWAWInputStreamPtr &input= m_parserState->m_input;
822 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
823 input->pushLimit(entry.end());
824 std::vector<MarinerWrtStruct> dataList;
825 m_mainParser->decodeZone(dataList, 100);
826 input->popLimit();
827
828 size_t numData = dataList.size();
829 if (numData < 16) {
830 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken: find unexpected number of data\n"));
831 return false;
832 }
833
834 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
835 libmwaw::DebugStream f;
836 size_t d = 0;
837 long val;
838
839 auto &zone = m_state->getZone(zoneId);
840 MarinerWrtGraphInternal::Token tkn;
841 for (int j = 0; j < 14; j++) {
842 auto &dt = dataList[d++];
843 if (!dt.isBasic()) {
844 f << "#f" << j << "=" << dt << ",";
845 static bool first=true;
846 if (first) {
847 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken: find some struct block\n"));
848 first = false;
849 }
850 continue;
851 }
852 int dim[2];
853 switch (j) {
854 case 0:
855 tkn.m_id[0] = dt.value(0);
856 break;
857 case 1: // always 5
858 if (dt.value(0) != 5)
859 f << "f0=" << dt.value(0) << ",";
860 break;
861 case 2:
862 val = dt.value(0);
863 tkn.m_highType = int(val >> 16);
864 tkn.m_type = int(val&0xFFFF);
865 break;
866 case 3:
867 case 4:
868 dim[0]= dim[1]= 0;
869 dim[j-3]=static_cast<int>(dt.value(0));
870 if (j!=4)
871 dim[++j-3]=static_cast<int>(dataList[d++].value(0));
872 tkn.m_dim = MWAWVec2i(dim[0],dim[1]);
873 break;
874 case 6:
875 if (dt.value(0)) {
876 auto v = uint32_t(dt.value(0));
877 tkn.m_refType = int(v>>28);
878 tkn.m_refId = v;
879 }
880 break;
881 case 8:
882 tkn.m_fieldType = static_cast<int>(dt.value(0));
883 break;
884 case 9:
885 tkn.m_id[1] = dt.value(0);
886 break;
887 case 10:
888 case 11:
889 case 12:
890 case 13: // 0 or 1 for graph : link to border ?
891 tkn.m_pictBorderWidth[j-10]=float(dt.value(0));
892 while (j!=13)
893 tkn.m_pictBorderWidth[++j-10]=float(dataList[d++].value(0));
894 break;
895 default:
896 if (dt.value(0))
897 f << "#f" << j << "=" << dt.value(0) << ",";
898 break;
899 }
900 }
901 tkn.m_extra = f.str();
902 f.str("");
903 f << entry.name() << ":" << tkn;
904 ascFile.addPos(entry.begin());
905 ascFile.addNote(f.str().c_str());
906
907 ascFile.addPos(dataList[d].m_filePos);
908 f.str("");
909 f << entry.name() << "(II):type=" << std::hex << tkn.m_type << std::dec << ",";
910 for (int i = 0; i < 2; i++) {
911 auto const &data = dataList[d++];
912 std::string str;
913 if (i==0 && readTokenBlock0(data, tkn, str)) {
914 if (str.length())
915 f << "block0=[" << str << "],";
916 continue;
917 }
918 if (data.m_type != 0) {
919 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken(II): can not read block%d\n", i));
920 f << "###bl" << i << "=" << data << ",";
921 }
922 else {
923 f << "bl" << i << "=[";
924 input->seek(data.m_pos.begin(), librevenge::RVNG_SEEK_SET);
925 for (int j = 0; j < int(data.m_pos.length()/2); j++) {
926 if (i==1 && j == 1 && data.m_pos.length() >= 12) {
927 for (auto &type : tkn.m_pictBorderType) type=static_cast<int>(input->readULong(1));
928 // checkme: only for picture or always ?
929 unsigned char col[]= {0,0,0};
930 for (auto &c : col) c=static_cast<unsigned char>(input->readULong(2)>>8);
931 j += 5;
932 tkn.m_pictBorderColor = MWAWColor(col[0],col[1],col[2]);
933 if (!tkn.m_pictBorderColor.isBlack()) f << "bordColor=" << tkn.m_pictBorderColor << ",";
934 continue;
935 }
936 val = long(input->readULong(2));
937 if (val) f << "f" << j << "=" << std::hex << val << std::dec << ",";
938 }
939 f << "],";
940 }
941 }
942 if (tkn.m_type==0x24 && d < numData && tkn.m_value.empty()) {
943 std::string str;
944 if (readTokenBlock0(dataList[d], tkn, str)) {
945 d++;
946 f << str;
947 }
948 }
949 if (tkn.m_type != 0x14 || numData < 32) {
950 for (; d < numData; d++) {
951 MarinerWrtStruct const &data = dataList[d];
952 f << "#" << data << ",";
953 static bool first = true;
954 if (first) {
955 first = false;
956 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken(II): find some extra data \n"));
957 }
958 }
959 zone.m_tokenMap[tkn.m_id[0]] = tkn;
960 ascFile.addNote(f.str().c_str());
961
962 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
963 return true;
964 }
965
966 // ok now read the picture data
967 ascFile.addNote(f.str().c_str());
968 ascFile.addPos(dataList[d].m_filePos);
969 f.str("");
970 for (int j = 0; j < 15; j++) {
971 MarinerWrtStruct const &dt = dataList[d++];
972 if (!dt.isBasic()) {
973 f << "#f" << j << "=" << dt << ",";
974 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken(III): find some struct block\n"));
975 continue;
976 }
977 switch (j) {
978 case 8:
979 if (!dt.value(0))
980 break;
981 tkn.m_pictId = dt.value(0);
982 f << "pictId=" << std::hex << tkn.m_pictId << std::dec << ",";
983 break;
984 default:
985 if (dt.value(0))
986 f << "#f" << j << "=" << dt.value(0) << ",";
987 }
988 }
989
990 MarinerWrtStruct dt = dataList[d++];
991 if (dt.m_type != 0 || !dt.m_pos.length()) {
992 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken: can not find the picture data\n"));
993 f << "###pictData=" << dt << ",";
994 }
995 else
996 tkn.m_pictData = dt.m_pos;
997
998 for (; d < numData; d++) {
999 MarinerWrtStruct const &data = dataList[d];
1000 f << "#" << data << ",";
1001 static bool first = true;
1002 if (first) {
1003 first = false;
1004 MWAW_DEBUG_MSG(("MarinerWrtGraph::readToken(III): find some extra data \n"));
1005 }
1006 }
1007 tkn.m_extra += f.str();
1008 zone.m_tokenMap[tkn.m_id[0]] = tkn;
1009
1010 std::stringstream f2;
1011 f2 << entry.name() << "(III):" << f.str();
1012 ascFile.addNote(f2.str().c_str());
1013
1014 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1015 return true;
1016 }
1017
1018 bool MarinerWrtGraph::readTokenBlock0(MarinerWrtStruct const &data, MarinerWrtGraphInternal::Token &tkn, std::string &res)
1019 {
1020 res = "";
1021 if (data.m_type != 0 || !data.m_pos.valid()) {
1022 MWAW_DEBUG_MSG(("MarinerWrtGraph::readTokenBlock0: called without data\n"));
1023 return false;
1024 }
1025 if (data.m_pos.length()<0x2c) {
1026 MWAW_DEBUG_MSG(("MarinerWrtGraph::readTokenBlock0: the data seems very short\n"));
1027 return false;
1028 }
1029
1030 std::stringstream f;
1031
1032 MWAWInputStreamPtr &input= m_parserState->m_input;
1033 long pos = data.m_pos.begin(), endPos = data.m_pos.end();
1034 input->seek(pos, librevenge::RVNG_SEEK_SET);
1035 // fixme: this depends on the token type
1036 long val;
1037 int firstExpectedVal= (tkn.m_type==0x14) ? 28 :
1038 (tkn.m_type==0x17||tkn.m_type==0x18) ? 6 : 0;
1039 for (int i = 0; i < firstExpectedVal/2; i++) {
1040 val = long(input->readLong(2));
1041 if (val) f << "#f" << i << "=" << val << ",";
1042 }
1043 input->seek(pos+firstExpectedVal, librevenge::RVNG_SEEK_SET);
1044 std::string fValue("");
1045 switch (tkn.m_type) {
1046 case 0x14:
1047 tkn.m_valPictId = input->readLong(4);
1048 if (tkn.m_valPictId) f << "pId=" << std::hex << tkn.m_valPictId << ",";
1049 break;
1050 case 0x17:
1051 case 0x18:
1052 val = input->readLong(2);
1053 if (val) f << "f0=" << val << ","; // fieldType?
1054 MWAW_FALLTHROUGH;
1055 case 0x19:
1056 case 0x1e:
1057 case 0x1f:
1058 case 0x24:
1059 while (!input->isEnd()) {
1060 if (input->tell() >= endPos)
1061 break;
1062 val = long(input->readULong(1));
1063 if (!val) {
1064 input->seek(-1, librevenge::RVNG_SEEK_CUR);
1065 break;
1066 }
1067 fValue += char(val);
1068 }
1069 break;
1070 case 0x23:
1071 // either big or small endian
1072 tkn.m_ruleType = static_cast<int>(input->readULong(2));
1073 if ((tkn.m_ruleType&0xFF)==0) tkn.m_ruleType>>=8;
1074 tkn.m_rulePattern = static_cast<int>(input->readULong(2));
1075 if ((tkn.m_rulePattern&0xFF)==0) tkn.m_rulePattern>>=8;
1076 switch (tkn.m_ruleType) {
1077 case 0:
1078 break; // no
1079 case 1:
1080 f << "rule[hairline],";
1081 break;
1082 case 2:
1083 f << "rule[single],";
1084 break;
1085 case 3:
1086 f << "rule[w=2],";
1087 break;
1088 case 4:
1089 f << "rule[w=3],";
1090 break;
1091 case 5:
1092 f << "rule[w=4],";
1093 break;
1094 case 6:
1095 f << "rule[double],";
1096 break;
1097 case 7:
1098 f << "rule[double,w=2],";
1099 break;
1100 default:
1101 f << "#rule[type=" << tkn.m_ruleType << "],";
1102 break;
1103 }
1104 if (tkn.m_rulePattern)
1105 f << "rule[pattern]=" << tkn.m_rulePattern << ",";
1106 break;
1107 default:
1108 break;
1109 }
1110 if (fValue.length()) {
1111 tkn.m_value = fValue;
1112 f << "val=" << fValue << ",";
1113 }
1114 int numRemains = int(endPos-input->tell())/2;
1115 for (int i = 0; i < numRemains; i++) { // always 0
1116 val = input->readLong(2);
1117 if (val) f << "#g" << i << "=" << val << ",";
1118 }
1119 res = f.str();
1120 tkn.m_extra += res;
1121 return true;
1122 }
1123
1124 bool MarinerWrtGraph::readPostscript(MarinerWrtEntry const &entry, int zoneId)
1125 {
1126 if (entry.length() < 3) {
1127 MWAW_DEBUG_MSG(("MarinerWrtGraph::readPostscript: data seems to short\n"));
1128 return false;
1129 }
1130 MWAWInputStreamPtr &input= m_parserState->m_input;
1131 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
1132 input->pushLimit(entry.end());
1133 std::vector<MarinerWrtStruct> dataList;
1134 m_mainParser->decodeZone(dataList, 1+3);
1135 input->popLimit();
1136
1137 if (int(dataList.size()) != 3) {
1138 MWAW_DEBUG_MSG(("MarinerWrtGraph::readPostscript: find unexpected number of data\n"));
1139 return false;
1140 }
1141
1142 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
1143 libmwaw::DebugStream f;
1144 size_t d = 0;
1145 MarinerWrtGraphInternal::Zone &zone = m_state->getZone(zoneId);
1146 MarinerWrtGraphInternal::PSZone psFile;
1147 for (int i = 0; i < 2; i++) {
1148 MarinerWrtStruct const &data = dataList[d++];
1149 if (!data.isBasic()) {
1150 MWAW_DEBUG_MSG(("MarinerWrtGraph::readPostscript: find unexpected type for f0\n"));
1151 f << "###f" << i << "=" << data << ",";
1152 }
1153 else if (i==0)
1154 psFile.m_type = static_cast<int>(data.value(0));
1155 else
1156 psFile.m_id = data.value(0);
1157 }
1158 MarinerWrtStruct const &data = dataList[d++];
1159 if (data.m_type != 0) {
1160 MWAW_DEBUG_MSG(("MarinerWrtGraph::readPostscript: can not find my file\n"));
1161 f << "###";
1162 psFile.m_extra=f.str();
1163 }
1164 else if (data.m_pos.valid()) {
1165 psFile.m_extra=f.str();
1166 psFile.m_pos = data.m_pos;
1167 zone.m_psZoneMap[psFile.m_id] = psFile;
1168 #ifdef DEBUG_WITH_FILES
1169 input->seek(data.m_pos.begin(), librevenge::RVNG_SEEK_SET);
1170 librevenge::RVNGBinaryData file;
1171 input->readDataBlock(data.m_pos.length(), file);
1172
1173 static int volatile psName = 0;
1174 std::stringstream fName;
1175 fName << "PS" << ++psName << ".ps";
1176 libmwaw::Debug::dumpFile(file, fName.str().c_str());
1177
1178 ascFile.skipZone(data.m_pos.begin(),data.m_pos.end()-1);
1179 #endif
1180 }
1181 f.str("");
1182 f << entry.name() << ":" << psFile;
1183 ascFile.addPos(entry.begin());
1184 ascFile.addNote(f.str().c_str());
1185 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
1186 return true;
1187 }
1188
1189 ////////////////////////////////////////////////////////////
1190 // send data to a listener
1191 ////////////////////////////////////////////////////////////
1192
1193 ////////////////////////////////////////////////////////////
1194 // low level
1195 ////////////////////////////////////////////////////////////
1196
1197 ////////////////////////////////////////////////////////////
1198 // send data
1199 ////////////////////////////////////////////////////////////
1200 bool MarinerWrtGraph::sendPageGraphics()
1201 {
1202 return true;
1203 }
1204
1205 void MarinerWrtGraph::flushExtra()
1206 {
1207 #ifdef DEBUG
1208 MWAWPosition pictPos(MWAWVec2f(0,0),MWAWVec2f(0,0),librevenge::RVNG_POINT);
1209 pictPos.setRelativePosition(MWAWPosition::Char);
1210
1211 for (auto const &it : m_state->m_zoneMap) {
1212 int zId = it.first;
1213 auto const &zone = it.second;
1214
1215 for (auto const &tIt : zone.m_tokenMap) {
1216 auto const &tkn = tIt.second;
1217 if (tkn.m_parsed) continue;
1218 sendToken(zId, tIt.first);
1219 }
1220 for (auto const &psIt : zone.m_psZoneMap) {
1221 auto const &psZone = psIt.second;
1222 if (psZone.m_parsed) continue;
1223 sendPSZone(psZone, pictPos);
1224 }
1225 }
1226 #endif
1227 }
1228 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: