Debian Codegraph @

src/lib/FullWrtStruct.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 <sstream>
35 #include <string>
36
37 #include "MWAWEntry.hxx"
38 #include "MWAWGraphicStyle.hxx"
39 #include "MWAWInputStream.hxx"
40
41 #include "FullWrtStruct.hxx"
42
43 namespace FullWrtStruct
44 {
45 bool getColor(int color, MWAWColor &col)
46 {
47 if (color==0xFFFF) return false;
48 if (color&0x8000) // true color
49 col=MWAWColor(static_cast<unsigned char>(((color>>10)&0x1F)<<3),
50 static_cast<unsigned char>(((color>>5)&0x1F)<<3),
51 static_cast<unsigned char>((color&0x1F)<<3));
52 else if ((color&0x6000)==0x6000) // black
53 col=MWAWColor(0,0,0);
54 else if ((color&0x4000) || (color>=0&&color <= 100)) { // gray in %
55 int val = ((color&0x7F)*255)/100;
56 if (val > 255) val = 0;
57 else val = 255-val;
58 auto c = static_cast<unsigned char>(val);
59 col=MWAWColor(c,c,c);
60 }
61 else
62 return false;
63 return true;
64 }
65
66 std::string getTypeName(int type)
67 {
68 switch (type) {
69 case 0:
70 return "columns,";
71 case 1:
72 return "tabs,";
73 case 2:
74 return "item,";
75 case 3:
76 return "style,";
77 case 0xa:
78 return "main,";
79 case 0xb:
80 return "comment,";
81 case 0xc:
82 return "footnote,";
83 case 0xd:
84 return "endnote,";
85 case 0x10: // checkme
86 return "index,";
87 case 0x11: // checkme
88 return "header,";
89 case 0x13:
90 return "sidebar,";
91 case 0x14:
92 return "sidebar[simple],";
93 case 0x15:
94 return "graphic,";
95 case 0x18: // in general empty
96 return "variableText,";
97 // 13-14: always find with child
98 // 0xb, 11-12 can also have child...
99 case 0x19:
100 return "reference,";
101 case 0x1a:
102 return "referenceRedirect,";
103 case 0x1e:
104 return "variableRedirect,";
105 case 0x1f:
106 return "dataMod,";
107 default:
108 break;
109 }
110 std::stringstream s;
111 s << "type=" << std::hex << type << std::dec << ",";
112 return s.str();
113 }
114
115 ////////////////////////////////////////////////////////////
116 // Border
117 ////////////////////////////////////////////////////////////
118 MWAWBorder Border::getBorder(int type)
119 {
120 MWAWBorder res;
121 if ((type%2)==0)
122 res.m_type=MWAWBorder::Double;
123 res.m_width=double(type/2);
124 return res;
125 }
126
127 void Border::addTo(MWAWGraphicStyle &style) const
128 {
129 if (!m_backColor.isWhite())
130 style.setBackgroundColor(m_backColor);
131 if (hasShadow()) {
132 style.m_shadowOffset=MWAWVec2f(m_shadowDepl);
133 style.setShadowColor(m_shadowColor);
134 }
135 if (m_frameBorder.isEmpty())
136 return;
137 MWAWBorder bord=m_frameBorder;
138 bord.m_color = m_color[0];
139 style.setBorders(15, bord);
140 }
141
142 bool Border::read(std::shared_ptr<FullWrtStruct::Entry> zone, int fSz)
143 {
144 *this=FullWrtStruct::Border();
145 if (fSz < 26) {
146 MWAW_DEBUG_MSG(("FullWrtStruct::Border::read: find unexpected size\n"));
147 return false;
148 }
149 MWAWInputStreamPtr input = zone->m_input;
150 libmwaw::DebugStream f;
151 long pos = input->tell();
152
153 int width[3];
154 int totalW = 0;
155 for (auto &w : width) totalW += (w=static_cast<int>(input->readLong(1)));
156 if (width[0]&&width[2]) {
157 m_frameBorder.m_style=MWAWBorder::Simple;
158 m_frameBorder.m_type=MWAWBorder::Double;
159 m_frameBorder.m_width=0.5*double(totalW);
160 m_frameBorder.m_widthsList.resize(3);
161 for (size_t i=0; i < 3; ++i)
162 m_frameBorder.m_widthsList[i]=0.5*double(width[i]);
163 }
164 else if (!width[0] && !width[1] && width[2]) {
165 m_frameBorder.m_style=MWAWBorder::Simple;
166 m_frameBorder.m_width=0.5*double(totalW);
167 }
168 else if (totalW) {
169 MWAW_DEBUG_MSG(("FullWrtStruct::Border::read: frame border width seems odd\n"));
170 f << "###frame[w]=[";
171 for (auto w : width) f << w << ",";
172 f << "],";
173 }
174 auto val = static_cast<int>(input->readLong(1));
175 if (val)
176 m_shadowDepl=MWAWVec2i(val,val);
177 val = static_cast<int>(input->readLong(1));
178 if (val) f << "frame[rectCorner]=" << val << ",";
179 m_type[0] = static_cast<int>(input->readLong(1));
180 MWAWColor col;
181 for (int j = 0; j < 7; j++) {
182 val = static_cast<int>(input->readULong(2));
183 if (getColor(val,col)) {
184 switch (j) {
185 case 1: // border
186 m_color[0] = col;
187 break;
188 case 2:
189 m_shadowColor=col;
190 break;
191 case 3: // separator
192 m_color[1] = col;
193 break;
194 case 4: // = border?
195 if (m_color[0] != col)
196 f << "#col[border2]=" << col << ",";
197 break;
198 case 5:
199 m_frontColor=col;
200 break;
201 case 6:
202 m_backColor=col;
203 break;
204 default:
205 if (!col.isBlack())
206 f << "col" << j << "=" << col << ",";
207 }
208 }
209 else
210 f << "#col" << j << "=" << std::hex << val << std::dec << ",";
211 }
212 for (int j = 0; j < 2; j++) { // g0=g1=0
213 val = static_cast<int>(input->readLong(1));
214 if (val) f << "g" << j << "=" << val << ",";
215 }
216 m_type[1] = static_cast<int>(input->readLong(1)); // sepH
217 m_type[2] = static_cast<int>(input->readLong(1)); // sepV
218 m_flags = static_cast<int>(input->readULong(2));
219 m_extra = f.str();
220 input->seek(pos+fSz, librevenge::RVNG_SEEK_SET);
221 return true;
222 }
223
224 std::vector<MWAWVariable<MWAWBorder> > Border::getParagraphBorders() const
225 {
226 std::vector<MWAWVariable<MWAWBorder> > res;
227 int wh=-1;
228 if (m_type[0]>0 && m_type[0]<=8) wh=0;
229 else if (m_type[1]>0 && m_type[1]<=8) wh=1;
230 if (wh == -1)
231 return res;
232 MWAWVariable<MWAWBorder> border(getBorder(m_type[wh]));
233 border->m_color=m_color[wh];
234 if (wh==0)
235 res.resize(4,border);
236 else {
237 res.resize(4);
238 res[libmwaw::Bottom]=border;
239 }
240 return res;
241 }
242
243 std::ostream &operator<<(std::ostream &o, Border const &p)
244 {
245 if (!p.m_frontColor.isBlack())
246 o << "frontColor=" << p.m_frontColor << ",";
247 if (!p.m_backColor.isWhite())
248 o << "backColor=" << p.m_backColor << ",";
249 if (p.hasShadow())
250 o << "shadow=" << p.m_shadowDepl << "[" << p.m_shadowColor << "],";
251 for (int w=0; w < 3; w++) {
252 if (!p.m_type[w]) continue;
253 if (w==0)
254 o << "border=";
255 else if (w==1)
256 o << "sep[H]=";
257 else
258 o << "sep[V]=";
259 switch (p.m_type[w]) {
260 case 0: // none
261 break;
262 case 1:
263 o << "hairline:";
264 break;
265 case 2:
266 o << "hairline double:";
267 break;
268 case 3:
269 o << "normal:";
270 break;
271 case 4:
272 o << "normal double:";
273 break;
274 case 5:
275 o << "2pt:";
276 break;
277 case 7:
278 o << "3pt:";
279 break;
280 default:
281 o << "#type[" << p.m_type[w] << "]:";
282 }
283 if (w!=2 && !p.m_color[w].isBlack())
284 o << "col=" << p.m_color[w] << ",";
285 else
286 o << ",";
287 }
288 if (!p.m_frameBorder.isEmpty())
289 o << "border[frame]=" << p.m_frameBorder << ",";
290 if (p.m_flags & 0x4000)
291 o << "setBorder,";
292 if (p.m_flags & 0x8000)
293 o << "setSeparator,";
294 if (p.m_flags & 0x3FFF)
295 o << "flags=" << std::hex << (p.m_flags & 0x3FFF) << std::dec << ",";
296 o << p.m_extra;
297 return o;
298 }
299 ////////////////////////////////////////////////////////////
300 // Entry
301 ////////////////////////////////////////////////////////////
302 Entry::Entry(MWAWInputStreamPtr const &input)
303 : MWAWEntry()
304 , m_input(input)
305 , m_nextId(-2)
306 , m_fileType(-1)
307 , m_typeId(-3)
308 , m_data()
309 , m_asciiFile()
310 {
311 for (auto &val : m_values) val=0;
312 }
313 Entry::~Entry()
314 {
315 closeDebugFile();
316 }
317
318 std::ostream &operator<<(std::ostream &o, Entry const &entry)
319 {
320 if (entry.type().length()) {
321 o << entry.type();
322 if (entry.id() >= 0) o << "[" << entry.id() << "]";
323 o << ",";
324 }
325 if (entry.m_id != -1) {
326 o << "fId=" << entry.m_id << ",";
327 }
328 switch (entry.m_fileType) {
329 case -1:
330 break;
331 case 0xa:
332 o << "main,";
333 break;
334 case 0x11:
335 o << "header,";
336 break;
337 case 0x12:
338 o << "footer,";
339 break;
340 case 0x13:
341 o << "textbox,";
342 break;
343 default:
344 o << "zType=" << std::hex << entry.m_fileType << std::dec << ",";
345 }
346 if (entry.m_typeId != -3) {
347 if (entry.m_typeId >= 0) o << "text/graphic,";
348 else if (entry.m_typeId == -2)
349 o << "null,";
350 else if (entry.m_typeId == -1)
351 o << "main,";
352 else
353 o << "#type=" << entry.m_typeId << ",";
354 }
355 for (int i = 0; i < 3; i++)
356 if (entry.m_values[i])
357 o << "e" << i << "=" << entry.m_values[i] << ",";
358 if (entry.m_extra.length())
359 o << entry.m_extra << ",";
360 return o;
361 }
362
363 bool Entry::valid() const
364 {
365 return m_input && MWAWEntry::valid() && m_input->checkPosition(begin()) && m_input->checkPosition(end());
366 }
367
368 void Entry::update()
369 {
370 if (!m_data.size()) return;
371
372 setBegin(0);
373 setLength(long(m_data.size()));
374 m_input=MWAWInputStream::get(m_data, false);
375 if (!m_input) {
376 MWAW_DEBUG_MSG(("Entry::update: problem the input size is bad!!!\n"));
377 return;
378 }
379 m_asciiFile.reset(new libmwaw::DebugFile(m_input));
380 std::stringstream s;
381 if (m_typeId == -1)
382 s << "MainZoneM" << m_id;
383 else
384 s << "DataZone" << m_id;
385 m_asciiFile->open(s.str());
386 }
387
388 void Entry::closeDebugFile()
389 {
390 if (!m_data.size()) return;
391 m_asciiFile->reset();
392 }
393
394 libmwaw::DebugFile &Entry::getAsciiFile()
395 {
396 return *m_asciiFile;
397 }
398
399 bool Entry::operator==(const Entry &a) const
400 {
401 if (MWAWEntry::operator!=(a)) return false;
402 if (m_input.get() != a.m_input.get()) return false;
403 if (id() != a.id()) return false;
404 if (m_nextId != a.m_nextId) return false;
405 if (m_fileType != a.m_fileType) return false;
406 if (m_typeId != a.m_typeId) return false;
407 if (m_id != a.m_id) return false;
408 for (int i = 0; i < 3; i++)
409 if (m_values[i] != a.m_values[i]) return false;
410 return true;
411 }
412
413 ////////////////////////////////////////////////////////////
414 // read the zone data header
415 ZoneHeader::~ZoneHeader()
416 {
417 }
418
419 bool ZoneHeader::read(std::shared_ptr<FullWrtStruct::Entry> zone)
420 {
421 MWAWInputStreamPtr input = zone->m_input;
422 libmwaw::DebugFile &asciiFile = zone->getAsciiFile();
423 libmwaw::DebugStream f;
424 bool typedDoc = m_type > 0;
425 long pos = input->tell();
426 if (pos+73 > zone->end())
427 return false;
428
429 auto val = static_cast<int>(input->readULong(1));
430 if (!typedDoc && val)
431 return false;
432 if (val) f << "#type[high]" << std::hex << val << std::dec << ",";
433 auto type = static_cast<int>(input->readULong(1));
434 if (!(type >= 0x18 && type <=0x1f) && !(type >= 0xc && type <= 0xe)
435 &&!(typedDoc && type==0x5a))
436 return false;
437 f << "type=" << std::hex << type << std::dec << ",";
438
439 val = static_cast<int>(input->readULong(2));
440 if (val) {
441 if (!typedDoc) return false;
442 f << "#f0=" << val << ",";
443 }
444 val = static_cast<int>(input->readULong(1)); // 0, 6 or 0x10, 0x1e
445 if (val) f << "f1=" << std::hex << val << std::dec << ",";
446 val = static_cast<int>(input->readLong(1)); // 0 or 0x1 or -10
447 if (val != 1) f << "f2=" << val << ",";
448 auto N = static_cast<int>(input->readLong(2));
449 if (N) // can be a big number, but some time 0, 1, 3, 4, ...
450 f << "N0=" << N << ",";
451 // small number between 1 and 0x1f
452 val = static_cast<int>(input->readLong(2));
453 if (val) f << "N1=" << val << ",";
454
455 val = static_cast<int>(input->readLong(1)); // 0, 1, 2, -1, -2
456 if (val) f << "f3=" << val << ",";
457 val = static_cast<int>(input->readULong(1)); // 12, 1f, 22, 23, 25, 2d, 32, 60, 62, 66, 67, ...
458 if (val) f << "f4=" << std::hex << val << std::dec << ",";
459
460 // small number, g0, g2 often negative
461 for (int i = 0; i < 4; i++) {
462 val = static_cast<int>(input->readLong(2));
463 if (val) f << "g" << i << "=" << val << ",";
464 }
465
466 val = static_cast<int>(input->readLong(2)); // alway -2
467 if (val != -2) {
468 if (val > 0 || val < -2) {
469 input->seek(pos, librevenge::RVNG_SEEK_SET);
470 return false;
471 }
472 f << "#g4=" << val << ",";
473 }
474 for (int i = 0; i < 3; i++) {
475 // first a small number < 3e1, g6,g7 almost always 0 expected one time g6=-1a9
476 val = static_cast<int>(input->readLong(4));
477 if (!val) continue;
478 if (i==2 && !typedDoc)
479 return false;
480 f << "g" << i+5 << "=" << val << ",";
481 }
482 m_fileId = static_cast<int>(input->readULong(2));
483 m_docId = static_cast<int>(input->readULong(2));
484 for (int i=0; i < 3; ++i) { // h0: a small number 0..be, h1=0|1, h2=0..7
485 val = static_cast<int>(input->readLong(2));
486 if (val)
487 f << "h" << i << "=" << val << ",";
488 }
489 // now probably dependent of the type
490 switch (m_type) {
491 case 0x13: // sidebar
492 case 0x14: // sidebar simple
493 for (int i=0; i < 3; ++i) { // h3=0..e, h4=0..d, h5=0..a8
494 val = static_cast<int>(input->readLong(2));
495 if (val)
496 f << "h" << i+3 << "=" << val << ",";
497 }
498 f << "PTR=[";
499 for (int i=0; i < 2; ++i)
500 f << std::hex << input->readULong(4) << std::dec << ",";
501 f << "],";
502 m_wrapping=static_cast<int>(input->readLong(1));
503 val = static_cast<int>(input->readLong(1));
504 if (val) f << "#h6=" << val << ",";
505 for (int i=0; i<6; ++i) { // 0
506 val = static_cast<int>(input->readLong(2));
507 if (val)
508 f << "h" << i+7 << "=" << val << ",";
509 }
510 break;
511 default:
512 break;
513 }
514 m_extra = f.str();
515 if (input->tell()!=pos+72)
516 asciiFile.addDelimiter(input->tell(),'|');
517 asciiFile.addPos(pos);
518 input->seek(pos+72, librevenge::RVNG_SEEK_SET);
519 f.str("");
520 return true;
521 }
522
523 std::ostream &operator<<(std::ostream &o, ZoneHeader const &dt)
524 {
525 if (dt.m_type >= 0) o << FullWrtStruct::getTypeName(dt.m_type);
526 if (dt.m_fileId >= 0) o << "fileId=" << dt.m_fileId << ",";
527 if (dt.m_docId >= 0) o << "docId=" << dt.m_docId << ",";
528 switch (dt.m_wrapping) {
529 case -1:
530 break;
531 case 0:
532 o << "wrapToShape,";
533 break;
534 case 1:
535 o << "wrap[rect],";
536 break;
537 case 2:
538 o << "wrap[shrinkToFit],";
539 break;
540 case 3:
541 o << "wrap[background],";
542 break;
543 default:
544 o << "#wrap=" << dt.m_wrapping << ",";
545 break;
546 }
547 o << dt.m_extra;
548 return o;
549 }
550
551 }
552
553 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: