Debian Codegraph @

src/lib/libmwaw_internal.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 <cstdarg>
36 #include <cstdio>
37 #include <iomanip>
38 #include <string>
39 #include <sstream>
40 #include <time.h>
41
42 #include <ctype.h>
43 #include <locale.h>
44
45 #include <librevenge-stream/librevenge-stream.h>
46
47 #include "libmwaw_internal.hxx"
48
49 /** namespace used to regroup all libwpd functions, enumerations which we have redefined for internal usage */
50 namespace libmwaw
51 {
52 uint8_t readU8(librevenge::RVNGInputStream *input)
53 {
54 unsigned long numBytesRead;
55 uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead);
56
57 if (!p || numBytesRead != sizeof(uint8_t))
58 throw libmwaw::FileException();
59
60 return *p;
61 }
62
63 void appendUnicode(uint32_t val, librevenge::RVNGString &buffer)
64 {
65 uint8_t first;
66 int len;
67 if (val < 0x80) {
68 first = 0;
69 len = 1;
70 }
71 else if (val < 0x800) {
72 first = 0xc0;
73 len = 2;
74 }
75 else if (val < 0x10000) {
76 first = 0xe0;
77 len = 3;
78 }
79 else if (val < 0x200000) {
80 first = 0xf0;
81 len = 4;
82 }
83 else if (val < 0x4000000) {
84 first = 0xf8;
85 len = 5;
86 }
87 else {
88 first = 0xfc;
89 len = 6;
90 }
91
92 char outbuf[7];
93 int i;
94 for (i = len - 1; i > 0; --i) {
95 outbuf[i] = char((val & 0x3f) | 0x80);
96 val >>= 6;
97 }
98 outbuf[0] = char(val | first);
99 outbuf[len] = 0;
100 buffer.append(outbuf);
101 }
102 }
103
104 namespace libmwaw
105 {
106 std::string numberingTypeToString(NumberingType type)
107 {
108 switch (type) {
109 case ARABIC:
110 return "1";
111 case LOWERCASE:
112 return "a";
113 case UPPERCASE:
114 return "A";
115 case LOWERCASE_ROMAN:
116 return "i";
117 case UPPERCASE_ROMAN:
118 return "I";
119 case NONE:
120 case BULLET:
121 #if !defined(__clang__)
122 default:
123 #endif
124 break;
125 }
126 MWAW_DEBUG_MSG(("libmwaw::numberingTypeToString: must not be called with type %d\n", int(type)));
127 return "1";
128 }
129
130 std::string numberingValueToString(NumberingType type, int value)
131 {
132 std::stringstream ss;
133 std::string s("");
134 switch (type) {
135 case ARABIC:
136 ss << value;
137 return ss.str();
138 case LOWERCASE:
139 case UPPERCASE:
140 if (value <= 0) {
141 MWAW_DEBUG_MSG(("libmwaw::numberingValueToString: value can not be negative or null for type %d\n", int(type)));
142 return (type == LOWERCASE) ? "a" : "A";
143 }
144 while (value > 0) {
145 s = char((type == LOWERCASE ? 'a' : 'A')+((value-1)%26))+s;
146 value = (value-1)/26;
147 }
148 return s;
149 case LOWERCASE_ROMAN:
150 case UPPERCASE_ROMAN: {
151 static char const *romanS[] = {"M", "CM", "D", "CD", "C", "XC", "L",
152 "XL", "X", "IX", "V", "IV", "I"
153 };
154 static char const *romans[] = {"m", "cm", "d", "cd", "c", "xc", "l",
155 "xl", "x", "ix", "v", "iv", "i"
156 };
157 static int const romanV[] = {1000, 900, 500, 400, 100, 90, 50,
158 40, 10, 9, 5, 4, 1
159 };
160 if (value <= 0 || value >= 4000) {
161 MWAW_DEBUG_MSG(("libmwaw::numberingValueToString: out of range value for type %d\n", int(type)));
162 return (type == LOWERCASE_ROMAN) ? "i" : "I";
163 }
164 for (int p = 0; p < 13; p++) {
165 while (value >= romanV[p]) {
166 ss << ((type == LOWERCASE_ROMAN) ? romans[p] : romanS[p]);
167 value -= romanV[p];
168 }
169 }
170 return ss.str();
171 }
172 case NONE:
173 return "";
174 case BULLET:
175 #if !defined(__clang__)
176 default:
177 #endif
178 MWAW_DEBUG_MSG(("libmwaw::numberingValueToString: must not be called with type %d\n", int(type)));
179 break;
180 }
181 return "";
182 }
183
184 std::string writingModeToString(WritingMode mode)
185 {
186 switch (mode) {
187 case WritingLeftTop:
188 return "lt-rb";
189 case WritingLeftBottom:
190 return "lb-rt";
191 case WritingRightTop:
192 return "rt-lb";
193 case WritingRightBottom:
194 return "rb-lt";
195 case WritingInherited:
196 #if !defined(__clang__)
197 default:
198 #endif
199 break;
200 }
201 return "";
202 }
203 }
204
205 // color function
206 MWAWColor MWAWColor::barycenter(float alpha, MWAWColor const &colA,
207 float beta, MWAWColor const &colB)
208 {
209 uint32_t res = 0;
210 for (int i=0, depl=0; i<4; i++, depl+=8) {
211 float val=alpha*float((colA.m_value>>depl)&0xFF)+beta*float((colB.m_value>>depl)&0xFF);
212 if (val < 0) val=0;
213 if (val > 256) val=256;
214 auto comp= static_cast<unsigned char>(val);
215 res+=uint32_t(comp<<depl);
216 }
217 return MWAWColor(res);
218 }
219
220 std::ostream &operator<< (std::ostream &o, MWAWColor const &c)
221 {
222 auto const width = o.width();
223 auto const fill = o.fill();
224 o << "#" << std::hex << std::setfill('0') << std::setw(6)
225 << (c.m_value&0xFFFFFF)
226 // std::ios::width() takes/returns std::streamsize (long), but
227 // std::setw() takes int. Go figure...
228 << std::dec << std::setfill(fill) << std::setw(static_cast<int>(width));
229 return o;
230 }
231
232 std::string MWAWColor::str() const
233 {
234 std::stringstream stream;
235 stream << *this;
236 return stream.str();
237 }
238
239 // field function
240 bool MWAWField::addTo(librevenge::RVNGPropertyList &propList) const
241 {
242 switch (m_type) {
243 case Date: {
244 propList.insert("librevenge:field-type", "text:date");
245
246 librevenge::RVNGPropertyListVector pVect;
247 if (m_DTFormat.empty() || !libmwaw::convertDTFormat(m_DTFormat, pVect))
248 break;
249
250 propList.insert("librevenge:value-type", "date");
251 propList.insert("number:automatic-order", "true");
252 propList.insert("librevenge:format", pVect);
253 break;
254 }
255 case PageCount:
256 propList.insert("librevenge:field-type", "text:page-count");
257 propList.insert("style:num-format", numberingTypeToString(m_numberingType).c_str());
258 break;
259 case PageNumber:
260 propList.insert("librevenge:field-type", "text:page-number");
261 propList.insert("style:num-format", numberingTypeToString(m_numberingType).c_str());
262 break;
263 case Title:
264 propList.insert("librevenge:field-type", "text:title");
265 break;
266 case Time: {
267 propList.insert("librevenge:field-type", "text:time");
268
269 librevenge::RVNGPropertyListVector pVect;
270 if (m_DTFormat.empty() || !libmwaw::convertDTFormat(m_DTFormat, pVect))
271 break;
272
273 propList.insert("librevenge:value-type", "time");
274 propList.insert("number:automatic-order", "true");
275 propList.insert("librevenge:format", pVect);
276 break;
277 }
278 case BookmarkStart:
279 case BookmarkEnd:
280 propList.insert("librevenge:field-type", m_type==BookmarkStart ? "text:bookmark-start" : "text:bookmark-end");
281 if (!m_data.empty())
282 propList.insert("text:name",m_data.c_str());
283 break;
284 case Database:
285 case None:
286 #if !defined(__clang__)
287 default:
288 #endif
289 return false;
290 }
291 return true;
292 }
293
294 librevenge::RVNGString MWAWField::getString() const
295 {
296 librevenge::RVNGString res;
297 switch (m_type) {
298 case Database:
299 if (m_data.length())
300 res=librevenge::RVNGString(m_data.c_str());
301 else
302 res=librevenge::RVNGString("#DATAFIELD#");
303 break;
304 case BookmarkStart:
305 case BookmarkEnd:
306 case Date:
307 case PageCount:
308 case PageNumber:
309 case Title:
310 case Time:
311 case None:
312 #if !defined(__clang__)
313 default:
314 #endif
315 break;
316 }
317
318 return res;
319 }
320
321 // format function
322 namespace libmwaw
323 {
324 bool convertDTFormat(std::string const &dtFormat, librevenge::RVNGPropertyListVector &propVect)
325 {
326 propVect.clear();
327 std::string text("");
328 librevenge::RVNGPropertyList list;
329 size_t len=dtFormat.size();
330 for (size_t c=0; c < len; ++c) {
331 if (dtFormat[c]!='%' || c+1==len) {
332 text+=dtFormat[c];
333 continue;
334 }
335 char ch=dtFormat[++c];
336 if (ch=='%') {
337 text += '%';
338 continue;
339 }
340 if (!text.empty()) {
341 list.clear();
342 list.insert("librevenge:value-type", "text");
343 list.insert("librevenge:text", text.c_str());
344 propVect.append(list);
345 text.clear();
346 }
347 list.clear();
348 switch (ch) {
349 case 'Y':
350 list.insert("number:style", "long");
351 MWAW_FALLTHROUGH;
352 case 'y':
353 list.insert("librevenge:value-type", "year");
354 propVect.append(list);
355 break;
356 case 'B':
357 list.insert("number:style", "long");
358 MWAW_FALLTHROUGH;
359 case 'b':
360 case 'h':
361 list.insert("librevenge:value-type", "month");
362 list.insert("number:textual", true);
363 propVect.append(list);
364 break;
365 case 'm':
366 list.insert("librevenge:value-type", "month");
367 propVect.append(list);
368 break;
369 case 'e':
370 list.insert("number:style", "long");
371 MWAW_FALLTHROUGH;
372 case 'd':
373 list.insert("librevenge:value-type", "day");
374 propVect.append(list);
375 break;
376 case 'A':
377 list.insert("number:style", "long");
378 MWAW_FALLTHROUGH;
379 case 'a':
380 list.insert("librevenge:value-type", "day-of-week");
381 propVect.append(list);
382 break;
383
384 case 'H':
385 list.insert("number:style", "long");
386 MWAW_FALLTHROUGH;
387 case 'I':
388 list.insert("librevenge:value-type", "hours");
389 propVect.append(list);
390 break;
391 case 'M':
392 list.insert("librevenge:value-type", "minutes");
393 list.insert("number:style", "long");
394 propVect.append(list);
395 break;
396 case 'S':
397 list.insert("librevenge:value-type", "seconds");
398 list.insert("number:style", "long");
399 propVect.append(list);
400 break;
401 case 'p':
402 list.clear();
403 list.insert("librevenge:value-type", "am-pm");
404 propVect.append(list);
405 break;
406 #if !defined(__clang__)
407 default:
408 MWAW_DEBUG_MSG(("convertDTFormat: find unimplement command %c(ignored)\n", ch));
409 #endif
410 }
411 }
412 if (!text.empty()) {
413 list.clear();
414 list.insert("librevenge:value-type", "text");
415 list.insert("librevenge:text", text.c_str());
416 propVect.append(list);
417 }
418 return propVect.count()!=0;
419 }
420 }
421
422 // link function
423 bool MWAWLink::addTo(librevenge::RVNGPropertyList &propList) const
424 {
425 propList.insert("xlink:type","simple");
426 if (!m_HRef.empty())
427 propList.insert("xlink:href",m_HRef.c_str());
428 return true;
429 }
430
431 // border function
432 int MWAWBorder::compare(MWAWBorder const &orig) const
433 {
434 int diff = int(m_style)-int(orig.m_style);
435 if (diff) return diff;
436 diff = int(m_type)-int(orig.m_type);
437 if (diff) return diff;
438 if (m_width < orig.m_width) return -1;
439 if (m_width > orig.m_width) return 1;
440 if (m_color < orig.m_color) return -1;
441 if (m_color > orig.m_color) return 1;
442 return 0;
443 }
444 bool MWAWBorder::addTo(librevenge::RVNGPropertyList &propList, std::string const which) const
445 {
446 std::stringstream stream, field;
447 stream << m_width << "pt ";
448 if (m_type==MWAWBorder::Double || m_type==MWAWBorder::Triple) {
449 static bool first = true;
450 if (first && m_style!=Simple) {
451 MWAW_DEBUG_MSG(("MWAWBorder::addTo: find double or tripe border with complex style\n"));
452 first = false;
453 }
454 stream << "double";
455 }
456 else {
457 switch (m_style) {
458 case Dot:
459 case LargeDot:
460 stream << "dotted";
461 break;
462 case Dash:
463 stream << "dashed";
464 break;
465 case Simple:
466 stream << "solid";
467 break;
468 case None:
469 #if !defined(__clang__)
470 default:
471 #endif
472 stream << "none";
473 break;
474 }
475 }
476 stream << " " << m_color;
477 field << "fo:border";
478 if (which.length())
479 field << "-" << which;
480 propList.insert(field.str().c_str(), stream.str().c_str());
481 size_t numRelWidth=m_widthsList.size();
482 if (numRelWidth==0)
483 return true;
484 if (m_type!=MWAWBorder::Double || numRelWidth!=3) {
485 static bool first = true;
486 if (first) {
487 MWAW_DEBUG_MSG(("MWAWBorder::addTo: relative width is only implemented with double style\n"));
488 first = false;
489 }
490 return true;
491 }
492 double totalWidth=0;
493 for (auto const &w : m_widthsList)
494 totalWidth+=w;
495 if (totalWidth <= 0) {
496 MWAW_DEBUG_MSG(("MWAWBorder::addTo: can not compute total width\n"));
497 return true;
498 }
499 double factor=m_width/totalWidth;
500 stream.str("");
501 for (size_t w=0; w < numRelWidth; w++) {
502 stream << factor *m_widthsList[w]<< "pt";
503 if (w+1!=numRelWidth)
504 stream << " ";
505 }
506 field.str("");
507 field << "style:border-line-width";
508 if (which.length())
509 field << "-" << which;
510 propList.insert(field.str().c_str(), stream.str().c_str());
511 return true;
512 }
513
514 std::ostream &operator<< (std::ostream &o, MWAWBorder::Style const &style)
515 {
516 switch (style) {
517 case MWAWBorder::None:
518 o << "none";
519 break;
520 case MWAWBorder::Simple:
521 break;
522 case MWAWBorder::Dot:
523 o << "dot";
524 break;
525 case MWAWBorder::LargeDot:
526 o << "large dot";
527 break;
528 case MWAWBorder::Dash:
529 o << "dash";
530 break;
531 #if !defined(__clang__)
532 default:
533 MWAW_DEBUG_MSG(("MWAWBorder::operator<<: find unknown style\n"));
534 o << "#style=" << int(style);
535 break;
536 #endif
537 }
538 return o;
539 }
540
541 std::ostream &operator<< (std::ostream &o, MWAWBorder const &border)
542 {
543 o << border.m_style << ":";
544 switch (border.m_type) {
545 case MWAWBorder::Single:
546 break;
547 case MWAWBorder::Double:
548 o << "double:";
549 break;
550 case MWAWBorder::Triple:
551 o << "triple:";
552 break;
553 #if !defined(__clang__)
554 default:
555 MWAW_DEBUG_MSG(("MWAWBorder::operator<<: find unknown type\n"));
556 o << "#type=" << int(border.m_type) << ":";
557 break;
558 #endif
559 }
560 if (border.m_width > 1 || border.m_width < 1) o << "w=" << border.m_width << ":";
561 if (!border.m_color.isBlack())
562 o << "col=" << border.m_color << ":";
563 o << ",";
564 if (!border.m_widthsList.empty()) {
565 o << "bordW[rel]=[";
566 for (auto const &w : border.m_widthsList)
567 o << w << ",";
568 o << "]:";
569 }
570 o << border.m_extra;
571 return o;
572 }
573
574 // picture function
575 MWAWEmbeddedObject::~MWAWEmbeddedObject()
576 {
577 }
578
579 bool MWAWEmbeddedObject::addTo(librevenge::RVNGPropertyList &propList) const
580 {
581 bool firstSet=false;
582 librevenge::RVNGPropertyListVector auxiliarVector;
583 for (size_t i=0; i<m_dataList.size(); ++i) {
584 if (m_dataList[i].empty()) continue;
585 std::string type=m_typeList.size() ? m_typeList[i] : "image/pict";
586 if (!firstSet) {
587 propList.insert("librevenge:mime-type", type.c_str());
588 propList.insert("office:binary-data", m_dataList[i]);
589 firstSet=true;
590 continue;
591 }
592 librevenge::RVNGPropertyList auxiList;
593 auxiList.insert("librevenge:mime-type", type.c_str());
594 auxiList.insert("office:binary-data", m_dataList[i]);
595 auxiliarVector.append(auxiList);
596 }
597 if (!auxiliarVector.empty())
598 propList.insert("librevenge:replacement-objects", auxiliarVector);
599 if (!firstSet) {
600 MWAW_DEBUG_MSG(("MWAWEmbeddedObject::addTo: called without picture\n"));
601 return false;
602 }
603 return true;
604 }
605
606 int MWAWEmbeddedObject::cmp(MWAWEmbeddedObject const &pict) const
607 {
608 if (m_typeList.size()!=pict.m_typeList.size())
609 return m_typeList.size()<pict.m_typeList.size() ? -1 : 1;
610 for (size_t i=0; i<m_typeList.size(); ++i) {
611 if (m_typeList[i]<pict.m_typeList[i]) return -1;
612 if (m_typeList[i]>pict.m_typeList[i]) return 1;
613 }
614 if (m_dataList.size()!=pict.m_dataList.size())
615 return m_dataList.size()<pict.m_dataList.size() ? -1 : 1;
616 for (size_t i=0; i<m_dataList.size(); ++i) {
617 if (m_dataList[i].size() < pict.m_dataList[i].size()) return 1;
618 if (m_dataList[i].size() > pict.m_dataList[i].size()) return -1;
619
620 const unsigned char *ptr=m_dataList[i].getDataBuffer();
621 const unsigned char *aPtr=pict.m_dataList[i].getDataBuffer();
622 if (!ptr || !aPtr) continue; // must only appear if the two buffers are empty
623 for (unsigned long h=0; h < m_dataList[i].size(); ++h, ++ptr, ++aPtr) {
624 if (*ptr < *aPtr) return 1;
625 if (*ptr > *aPtr) return -1;
626 }
627 }
628 return 0;
629 }
630
631 std::ostream &operator<<(std::ostream &o, MWAWEmbeddedObject const &pict)
632 {
633 if (pict.isEmpty()) return o;
634 o << "[";
635 for (auto const &type : pict.m_typeList) {
636 if (type.empty())
637 o << "_,";
638 else
639 o << type << ",";
640 }
641 o << "],";
642 return o;
643 }
644
645 // a little geometry
646 MWAWTransformation MWAWTransformation::rotation(float angle, MWAWVec2f const &center)
647 {
648 auto angl=float(double(angle)*M_PI/180);
649 auto cosA=float(std::cos(angl));
650 auto sinA=float(std::sin(angl));
651 return MWAWTransformation(MWAWVec3f(cosA, -sinA, center[0]-cosA*center[0]+sinA*center[1]),
652 MWAWVec3f(sinA, cosA, center[1]-sinA*center[0]-cosA*center[1]));
653 }
654
655 bool MWAWTransformation::decompose(float &rot, MWAWVec2f &shearing, MWAWTransformation &transform, MWAWVec2f const &origCenter) const
656 {
657 if (m_isIdentity) return false;
658 MWAWVec3f const &xRow=(*this)[0];
659 MWAWVec3f const &yRow=(*this)[1];
660 MWAWVec2f const &center=*this * origCenter;
661 // first check shearing
662 float shearY=0;
663 float val1=xRow[0]*xRow[1];
664 float val2=yRow[0]*yRow[1];
665 float diff=val2-val1;
666 if (diff<-0.01f || diff>0.01f) {
667 float const &A=val1;
668 float const B=xRow[1]*yRow[0]+xRow[0]*yRow[1];
669 float const &C=diff;
670 if (A>=0 && A<=0) {
671 if (B>=0 && A<=0) {
672 MWAW_DEBUG_MSG(("MWAWTransformation::decompose: can not determine the shearing\n"));
673 return false;
674 }
675 shearY=C/B;
676 }
677 else {
678 float const &delta=B*B-4*A*C;
679 if (delta<0) {
680 MWAW_DEBUG_MSG(("MWAWTransformation::decompose: can not determine the shearing\n"));
681 return false;
682 }
683 shearY=(B-float(std::sqrt(delta)))/2.f/A;
684 }
685 transform=MWAWTransformation::shear(MWAWVec2f(0,-shearY), center) **this;
686 }
687 else
688 transform=*this;
689 shearing=MWAWVec2f(0,shearY);
690 // fixme: we must first check for symetry here...
691 // now the rotation
692 rot=-std::atan2(-transform[1][0],transform[1][1]);
693 rot *= float(180/M_PI);
694 transform=MWAWTransformation::rotation(-rot, center) * transform;
695 return true;
696 }
697
698 namespace libmwaw
699 {
700 MWAWVec2f rotatePointAroundCenter(MWAWVec2f const &point, MWAWVec2f const &center, float angle)
701 {
702 float angl=float(M_PI/180.)*angle;
703 MWAWVec2f pt = point-center;
704 return center + MWAWVec2f(std::cos(angl)*pt[0]-std::sin(angl)*pt[1],
705 std::sin(angl)*pt[0]+std::cos(angl)*pt[1]);
706 }
707
708 MWAWBox2f rotateBoxFromCenter(MWAWBox2f const &box, float angle)
709 {
710 MWAWVec2f center=box.center();
711 MWAWVec2f minPt, maxPt;
712 for (int p=0; p<4; ++p) {
713 MWAWVec2f pt=rotatePointAroundCenter(MWAWVec2f(box[p<2?0:1][0],box[(p%2)?0:1][1]), center, angle);
714 if (p==0) {
715 minPt=maxPt=pt;
716 continue;
717 }
718 for (int c=0; c<2; ++c) {
719 if (pt[c]<minPt[c])
720 minPt[c]=pt[c];
721 else if (pt[c]>maxPt[c])
722 maxPt[c]=pt[c];
723 }
724 }
725 return MWAWBox2f(minPt,maxPt);
726 }
727
728 // debug message
729 #ifdef DEBUG
730 void printDebugMsg(const char *format, ...)
731 {
732 va_list args;
733 va_start(args, format);
734 std::vfprintf(stderr, format, args);
735 va_end(args);
736 }
737 #endif
738 }
739
740 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: