Debian Codegraph @

src/lib/CanvasStyleManager.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 <set>
40 #include <sstream>
41 #include <stack>
42
43 #include <librevenge/librevenge.h>
44
45 #include "MWAWFont.hxx"
46 #include "MWAWFontConverter.hxx"
47 #include "MWAWGraphicListener.hxx"
48 #include "MWAWGraphicStyle.hxx"
49 #include "MWAWParagraph.hxx"
50 #include "MWAWParser.hxx"
51
52 #include "CanvasParser.hxx"
53
54 #include "CanvasStyleManager.hxx"
55
56 #include "libmwaw_internal.hxx"
57
58 /** Internal: the structures of a CanvasStyleManager */
59 namespace CanvasStyleManagerInternal
60 {
61
62 ////////////////////////////////////////
63 //! Internal: the state of a CanvasStyleManager
64 struct State {
65 //! constructor
66 State()
67 : m_input()
68
69 , m_colors()
70 , m_patterns()
71 {
72 }
73
74 //! the main input
75 MWAWInputStreamPtr m_input;
76
77 //! the colors
78 std::vector<MWAWColor> m_colors;
79 //! the patterns
80 std::vector<MWAWGraphicStyle::Pattern> m_patterns;
81 };
82
83 }
84
85 ////////////////////////////////////////////////////////////
86 // constructor/destructor, ...
87 ////////////////////////////////////////////////////////////
88 CanvasStyleManager::CanvasStyleManager(CanvasParser &parser)
89 : m_parserState(parser.getParserState())
90 , m_state(new CanvasStyleManagerInternal::State)
91 , m_mainParser(&parser)
92 {
93 }
94
95 CanvasStyleManager::~CanvasStyleManager()
96 { }
97
98 int CanvasStyleManager::version() const
99 {
100 return m_parserState->m_version;
101 }
102
103 void CanvasStyleManager::setInput(MWAWInputStreamPtr &input)
104 {
105 m_state->m_input=input;
106 }
107
108 MWAWInputStreamPtr &CanvasStyleManager::getInput()
109 {
110 return m_state->m_input;
111 }
112
113 bool CanvasStyleManager::get(int index, MWAWColor &color) const
114 {
115 if (index<0 || index>=int(m_state->m_colors.size())) {
116 MWAW_DEBUG_MSG(("CanvasStyleManager::getColor: can find color with index=%d\n", index));
117 return false;
118 }
119 color=m_state->m_colors[size_t(index)];
120 return true;
121 }
122
123 bool CanvasStyleManager::get(int index, MWAWGraphicStyle::Pattern &pattern) const
124 {
125 if (index<0 || index>=int(m_state->m_patterns.size())) {
126 MWAW_DEBUG_MSG(("CanvasStyleManager::getPattern: can find pattern with index=%d\n", index));
127 return false;
128 }
129 pattern=m_state->m_patterns[size_t(index)];
130 return true;
131 }
132
133 std::vector<MWAWColor> const &CanvasStyleManager::getColorsList() const
134 {
135 return m_state->m_colors;
136 }
137
138 ////////////////////////////////////////////////////////////
139 //
140 // Intermediate level
141 //
142 ////////////////////////////////////////////////////////////
143
144 bool CanvasStyleManager::readArrows()
145 {
146 auto input=getInput();
147 long pos=input ? input->tell() : 0;
148 if (!input || !input->checkPosition(pos+180)) {
149 MWAW_DEBUG_MSG(("CanvasStyleManager::readArrows: file is too short\n"));
150 return false;
151 }
152
153 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
154 libmwaw::DebugStream f;
155 f << "Entries(Arrow):";
156 ascFile.addPos(pos);
157 ascFile.addNote(f.str().c_str());
158
159 for (int u=0; u<6; ++u) {
160 pos=input->tell();
161 f.str("");
162 f << "Arrow-" << u << ":";
163 for (int i=0; i<2; ++i) { // 0
164 int val=int(input->readLong(2));
165 if (val) f << "f" << i << "=" << val << ",";
166 }
167 MWAWGraphicStyle::Arrow arrow;
168 std::string extra;
169 if (readArrow(arrow, extra))
170 f << "arrow=[" << arrow << extra << "],";
171 else
172 f << "###";
173 input->seek(pos+30, librevenge::RVNG_SEEK_SET);
174 ascFile.addPos(pos);
175 ascFile.addNote(f.str().c_str());
176 }
177 return true;
178 }
179
180 bool CanvasStyleManager::readArrow(MWAWGraphicStyle::Arrow &arrow, std::string &extra)
181 {
182 arrow=MWAWGraphicStyle::Arrow::plain();
183 extra="";
184 auto input=getInput();
185 long pos=input ? input->tell() : 0;
186 if (!input || !input->checkPosition(pos+26)) {
187 MWAW_DEBUG_MSG(("CanvasStyleManager::readArrow: file is too short\n"));
188 return false;
189 }
190 libmwaw::DebugStream f;
191 int type=int(input->readULong(2));
192 if (type&0x100) f << "hasBar,";
193 if (type&0x200) f << "use[surf,color],";
194 // type&0x800 rare ?
195 if (type&0x1000) f << "circle,";
196 f << "type=" << (type&3) << ",";
197 if (type&0xECFC)
198 f << "fl=" << std::hex << (type&0xECFC) << std::dec << ",";
199 int dim[2];
200 for (auto &d: dim) d=int(input->readLong(2));
201 f << "pt0=" << MWAWVec2i(dim[1], dim[0]) << ",";
202 int val=int(input->readULong(2));
203 if (val!=3) f << "f2=" << val << ",";
204 val=int(input->readULong(4));
205 if (val!=0x20000)
206 f << "scale=" << double(val)/65536 << ",";
207 for (int i=0; i<7; ++i) {
208 int const expected[]= {90,1,0,2,0,3,12};
209 val=int(input->readLong(2));
210 if (val!=expected[i])
211 f << "g" << i << "=" << val << ",";
212 }
213 switch (type&0xfdff) {
214 case 1:
215 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,33)), "m10 0l-10 30 l10 3 l10 -3z", false);
216 break;
217 case 2:
218 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(20,30)), "m10 0l-10 30h20z", false);
219 break;
220 case 3:
221 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1131,1580)),
222 "M1013 1491l118 89-567-1580-564 1580 114-85 136-68 148-46 161-17 161 13 153 46z", false);
223 break;
224 case 0x100:
225 arrow=MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(-100,0),MWAWVec2i(100, 30)),
226 "M 0,0 L -100,0 -100,30 100,30 100,0 0,0 Z", false);
227 break;
228 case 0x101:
229 arrow=MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(40,38)), "m20 0l-20 0 l0 4 l20 0 l-10 30 l10 3 l10 -3 l-10 -30 l20 0 l0 -4z", false);
230 break;
231 case 0x102:
232 arrow=MWAWGraphicStyle::Arrow(10, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(40,35)), "m20 0l-20 0 l0 4 l20 0 l-10 30 l20 0 l-10 -30 l20 0 l0 -4z", false);
233 break;
234 case 0x1000:
235 arrow=MWAWGraphicStyle::Arrow(5, MWAWBox2i(MWAWVec2i(0,0),MWAWVec2i(1131,1131)), "M462 1118l-102-29-102-51-93-72-72-93-51-102-29-102-13-105 13-102 29-106 51-102 72-89 93-72 102-50 102-34 106-9 101 9 106 34 98 50 93 72 72 89 51 102 29 106 13 102-13 105-29 102-51 102-72 93-93 72-98 51-106 29-101 13z", false);
236 break;
237 default:
238 MWAW_DEBUG_MSG(("CanvasStyleManager::readArrow: find unexpected type\n"));
239 f << "###";
240 break;
241 }
242 extra=f.str();
243 return true;
244 }
245
246 bool CanvasStyleManager::readColors(int numColors)
247 {
248 if (!m_mainParser->decode(6*numColors)) {
249 MWAW_DEBUG_MSG(("CanvasParser::readColors: can not decode the input\n"));
250 return false;
251 }
252 auto input=getInput();
253 long pos=input ? input->tell() : 0;
254 if (!input || numColors<1 || !input->checkPosition(pos+numColors*6)) {
255 MWAW_DEBUG_MSG(("CanvasStyleManager::readColors: file is too short\n"));
256 return false;
257 }
258
259 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
260 libmwaw::DebugStream f;
261 f << "Entries(Color):";
262 ascFile.addPos(pos);
263 ascFile.addNote(f.str().c_str());
264
265 unsigned char col[3];
266 for (int i=0; i<numColors; ++i) {
267 pos=input->tell();
268 f.str("");
269 f << "Color-" << i << ":";
270 for (auto &c : col) c=(unsigned char)(input->readULong(2)>>8);
271 m_state->m_colors.push_back(MWAWColor(col[0], col[1], col[2]));
272 f << m_state->m_colors.back() << ",";
273 ascFile.addPos(pos);
274 ascFile.addNote(f.str().c_str());
275 }
276 return true;
277 }
278
279 bool CanvasStyleManager::readDashes(int numDashes, bool user)
280 {
281 auto input=getInput();
282 long pos=input ? input->tell() : 0;
283 int const dataSize=user ? 60 : 58;
284 if (!user) {
285 if (!m_mainParser->decode(2+numDashes*dataSize)) {
286 MWAW_DEBUG_MSG(("CanvasParser::readDashes: can not decode the input\n"));
287 return false;
288 }
289 }
290 if (!input || numDashes<1 || !input->checkPosition(pos+(user?0:2)+numDashes*dataSize)) {
291 MWAW_DEBUG_MSG(("CanvasStyleManager::readDashes: file is too short\n"));
292 return false;
293 }
294
295 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
296 libmwaw::DebugStream f;
297 f << "Entries(Dash):";
298 int val;
299 if (!user) {
300 val=int(input->readULong(2));
301 if (val)
302 f << "f0=" << val << ",";
303 }
304 ascFile.addPos(pos);
305 ascFile.addNote(f.str().c_str());
306
307 for (int i=0; i<numDashes; ++i) {
308 pos=input->tell();
309 f.str("");
310 f << "Dash-" << i << (user ? "U" : "") << ":";
311 int N=int(input->readULong(2));
312 if (N<=0 || N>12) {
313 MWAW_DEBUG_MSG(("CanvasStyleManager::readDashes: the number of dashes seems bad\n"));
314 f << "###N=" << N << ",";
315 ascFile.addPos(pos);
316 ascFile.addNote(f.str().c_str());
317 return false;
318 }
319 f << "dashes=[";
320 for (int p=0; p<N; ++p) f << float(input->readLong(4))/65536.f << ",";
321 f << "],";
322 input->seek(pos+50, librevenge::RVNG_SEEK_SET);
323 for (int j=0; j<(user ? 5 : 4); ++j) {
324 val=int(input->readLong(2));
325 if (val)
326 f << "f" << j+1 << "=" << val << ",";
327 }
328 ascFile.addPos(pos);
329 ascFile.addNote(f.str().c_str());
330 }
331 return true;
332 }
333
334 bool CanvasStyleManager::readGradient(MWAWEntry const &entry, MWAWGraphicStyle::Gradient &gradient)
335 {
336 auto input=getInput();
337 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
338 libmwaw::DebugStream f;
339 f << "Entries(Gradient):";
340
341 if (entry.length()<126 || !input || !input->checkPosition(entry.end())) {
342 MWAW_DEBUG_MSG(("CanvasStyleManager::readGradient: can not find the gradient data\n"));
343 if (input)
344 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
345 f << "###";
346 ascFile.addPos(entry.begin());
347 ascFile.addNote(f.str().c_str());
348 return false;
349 }
350
351 ascFile.addPos(entry.begin());
352 ascFile.addNote(f.str().c_str());
353 input->seek(entry.begin(), librevenge::RVNG_SEEK_SET);
354
355 MWAWColor colors[2];
356 int val;
357 for (int st=0; st<2; ++st) {
358 long pos=input->tell();
359 f.str("");
360 f << "Gradient-col" << st << ":";
361 val=int(input->readULong(2));
362 if (val!=0x8000) f << "fl=" << std::hex << val << std::dec << ",";
363 for (int wh=0; wh<2; ++wh) {
364 unsigned char col[3];
365 for (auto &c : col) c=(unsigned char)(input->readULong(2)>>8);
366 MWAWColor color(col[0],col[1],col[2]) ;
367 if (wh==0) colors[st]=color;
368 f << "c" << wh << "=" << color << ",";
369 }
370 ascFile.addDelimiter(input->tell(),'|');
371 input->seek(pos+46, librevenge::RVNG_SEEK_SET);
372 f << "],";
373 ascFile.addPos(pos);
374 ascFile.addNote(f.str().c_str());
375 }
376 long pos=input->tell();
377 f.str("");
378 f << "Gradient-data:";
379 int method=int(input->readLong(2));
380 f << "method=" << method << ","; // TB, BT, LR, RL, circ, ellip, rect, directional, shape
381 f << "using=" << input->readLong(2) << ","; // palette, rgb, dithered
382 val=int(input->readLong(2)); // center, mouse
383 bool useCenterPoint=val==2;
384 if (val!=1 && val!=2)
385 f << "##center[flag]=" << val << ",";
386 int rate=int(input->readLong(2));
387 if (rate!=1)
388 f << "rate=" << rate << ","; // constant, dual, accelerating
389 ascFile.addDelimiter(input->tell(),'|');
390 input->seek(pos+12, librevenge::RVNG_SEEK_SET);
391 ascFile.addDelimiter(input->tell(),'|');
392 int dir[2];
393 for (auto &d : dir) d=int(input->readLong(2));
394 if (method==8)
395 f << "dir=" << MWAWVec2i(dir[0],dir[1]) << ",";
396 int center[2];
397 for (auto &d : center) d=int(input->readLong(2));
398 if (useCenterPoint) {
399 f << "center=" << MWAWVec2i(center[0],center[1]) << ",";
400 gradient.m_percentCenter=MWAWVec2f(float(center[0])/100,float(center[1])/100);
401 }
402 switch (method) {
403 case 1:
404 case 2:
405 case 3:
406 case 4: {
407 gradient.m_type=MWAWGraphicStyle::Gradient::G_Linear;
408 int const angles[]= {90, 270, 0, 180};
409 gradient.m_angle=float(angles[method-1]+90);
410 break;
411 }
412 case 5:
413 gradient.m_type=MWAWGraphicStyle::Gradient::G_Radial;
414 break;
415 case 6:
416 gradient.m_type=MWAWGraphicStyle::Gradient::G_Ellipsoid;
417 break;
418 case 7:
419 case 9: // shape
420 gradient.m_type=MWAWGraphicStyle::Gradient::G_Square;
421 break;
422 case 8:
423 gradient.m_type=MWAWGraphicStyle::Gradient::G_Linear;
424 if (dir[0]<0 || dir[0]>0 || dir[1]<0 || dir[1]>0) // checkme: this is probably bad
425 gradient.m_angle=float(std::atan2(dir[1],dir[0])*180/M_PI)+180;
426 break;
427 default:
428 gradient.m_type=MWAWGraphicStyle::Gradient::G_Linear;
429 MWAW_DEBUG_MSG(("CanvasStyleManager::readGradient: unknown method=%d\n", method));
430 f << "##method,";
431 break;
432 }
433 if (rate==2) {
434 gradient.m_stopList.resize(3);
435 for (size_t i=0; i<3; ++i)
436 gradient.m_stopList[i]=MWAWGraphicStyle::Gradient::Stop(float(i)/2,colors[i<2 ? i : 0]);
437 }
438 else {
439 gradient.m_stopList.resize(2);
440 for (size_t i=0; i<2; ++i)
441 gradient.m_stopList[i]=MWAWGraphicStyle::Gradient::Stop(float(i),colors[1-i]);
442 }
443 ascFile.addDelimiter(input->tell(),'|');
444 ascFile.addPos(pos);
445 ascFile.addNote(f.str().c_str());
446
447 input->seek(entry.end(), librevenge::RVNG_SEEK_SET);
448 return true;
449 }
450
451 bool CanvasStyleManager::readPatterns(int numPatterns)
452 {
453 if (!m_mainParser->decode(8*numPatterns)) {
454 MWAW_DEBUG_MSG(("CanvasParser::readPatterns: can not decode the input\n"));
455 return false;
456 }
457 auto input=getInput();
458 long pos=input ? input->tell() : 0;
459 if (!input || !input->checkPosition(pos+8*numPatterns)) {
460 MWAW_DEBUG_MSG(("CanvasStyleManager::readPatterns: file is too short\n"));
461 return false;
462 }
463
464 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
465 libmwaw::DebugStream f;
466 f << "Entries(Pattern):";
467 int N=int(input->readULong(2));
468 if (N!=120)
469 f << "f0=" << N << ",";
470 ascFile.addPos(pos);
471 ascFile.addNote(f.str().c_str());
472
473 m_state->m_patterns.resize(size_t(numPatterns));
474 for (size_t i=0; i<size_t(numPatterns)-1; ++i) {
475 pos=input->tell();
476 f.str("");
477 f << "Pattern-" << i << ":";
478 auto &pat = m_state->m_patterns[i];
479 pat.m_dim=MWAWVec2i(8,8);
480 pat.m_data.resize(8);
481 for (auto &c : pat.m_data) c=(unsigned char) input->readULong(1);
482 f << pat << ",";
483 ascFile.addPos(pos);
484 ascFile.addNote(f.str().c_str());
485 }
486 ascFile.addPos(input->tell());
487 ascFile.addNote("Pattern-end:");
488 // in general some 0 but sometimes junk?
489 input->seek(6, librevenge::RVNG_SEEK_CUR);
490 return true;
491 }
492
493 bool CanvasStyleManager::readPenSize()
494 {
495 auto input=getInput();
496 long pos=input ? input->tell() : 0;
497 if (!input || !input->checkPosition(pos+20)) {
498 MWAW_DEBUG_MSG(("CanvasStyleManager::readPenSize: file is too short\n"));
499 return false;
500 }
501
502 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
503 libmwaw::DebugStream f;
504 f << "Entries(PenSize):sz=[";
505 for (int i=0; i<10; ++i)
506 f << double(input->readULong(2))/256. << ",";
507 f << "],";
508 ascFile.addPos(pos);
509 ascFile.addNote(f.str().c_str());
510 return true;
511 }
512
513 bool CanvasStyleManager::readFonts(int numFonts)
514 {
515 MWAWInputStreamPtr input = getInput();
516 long pos=input->tell();
517 if (numFonts<=0 || !input->checkPosition(pos+132*numFonts)) {
518 MWAW_DEBUG_MSG(("CanvasStyleManager::readFonts: zone seems too short\n"));
519 return false;
520 }
521
522 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
523 libmwaw::DebugStream f;
524 f << "Entries(Font):";
525 ascFile.addPos(pos);
526 ascFile.addNote(f.str().c_str());
527
528 bool const isWindows=m_mainParser->isWindowsFile();
529 auto fontConverter=m_parserState->m_fontConverter;
530 std::string const family=isWindows ? "CP1252" : "";
531 for (int fo=0; fo<numFonts; ++fo) {
532 pos=input->tell();
533 f.str("");
534 f << "Font-" << fo << ":";
535 int id=int(input->readULong(2));
536 f << "id=" << id << ",";
537 int val=int(input->readLong(2)); // 0
538 if (val) f << "f0=" << val << ",";
539 int dSz=int(input->readULong(1));
540 if (dSz>=127) {
541 MWAW_DEBUG_MSG(("CanvasStyleManager::readFonts: can not read a name\n"));
542 f << "###name";
543 }
544 else {
545 std::string name;
546 for (int s=0; s<dSz; ++s) name+=char(input->readULong(1));
547 if (!name.empty())
548 fontConverter->setCorrespondance(isWindows ? fo+1 : id, name, family);
549 f << name << ",";
550 }
551 ascFile.addPos(pos);
552 ascFile.addNote(f.str().c_str());
553 input->seek(pos+132, librevenge::RVNG_SEEK_SET);
554 }
555 return true;
556 }
557
558 ////////////////////////////////////////////////////////////
559 //
560 // Windows resource
561 //
562 ////////////////////////////////////////////////////////////
563 bool CanvasStyleManager::readColorValues(MWAWEntry const &entry)
564 {
565 auto input=getInput();
566 if (!input || !entry.valid() || !input->checkPosition(entry.end()))
567 return false;
568 if (entry.length()<32*16) {
569 MWAW_DEBUG_MSG(("CanvasStyleManager::readColorValues: the zone seems too small\n"));
570 return false;
571 }
572
573 libmwaw::DebugFile &ascFile = m_parserState->m_asciiFile;
574 libmwaw::DebugStream f;
575 f << "Entries(CVal)[" << entry.id() << "]:";
576 input->seek(entry.begin(),librevenge::RVNG_SEEK_SET);
577 ascFile.addPos(entry.begin());
578 ascFile.addNote(f.str().c_str());
579
580 int N=int(entry.length()/16); // normally 256
581 unsigned char col[4];
582 for (int i=0; i<N; ++i) {
583 long pos=input->tell();
584 f.str("");
585 f << "CVal-" << i << ":";
586 int val=int(input->readULong(2));
587 if (val!=0x8000) f << "f0=" << std::hex << val << std::dec << ",";
588 for (int c=0; c<3; ++c) col[c]=(unsigned char)(input->readULong(2)>>8);
589 f << MWAWColor(col[0], col[1], col[2]) << ",";
590 for (auto &c : col) c=(unsigned char)(input->readULong(2)>>8); // CMYK?
591 f << "col2=" << MWAWColor(col[0], col[1], col[2], col[3]) << ",";
592 input->seek(pos+16,librevenge::RVNG_SEEK_SET);
593 ascFile.addPos(pos);
594 ascFile.addNote(f.str().c_str());
595 }
596 return true;
597 }
598
599
600 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: