Debian Codegraph @

src/lib/MWAWGraphicShape.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 /* This header contains code specific to a pict mac file
35 */
36 #include <string.h>
37
38 #include <algorithm>
39 #include <cmath>
40 #include <cstring>
41 #include <iomanip>
42 #include <iostream>
43 #include <sstream>
44 #include <string>
45 #include <utility>
46
47 #include <librevenge/librevenge.h>
48
49 #include "libmwaw_internal.hxx"
50
51 #include "MWAWGraphicEncoder.hxx"
52 #include "MWAWGraphicStyle.hxx"
53
54 #include "MWAWGraphicShape.hxx"
55
56 ////////////////////////////////////////////////////////////
57 // MWAWGraphicShape::PathData
58 ////////////////////////////////////////////////////////////
59 std::ostream &operator<<(std::ostream &o, MWAWGraphicShape::PathData const &path)
60 {
61 o << path.m_type;
62 switch (path.m_type) {
63 case 'H':
64 o << ":" << path.m_x[0];
65 break;
66 case 'V':
67 o << ":" << path.m_x[1];
68 break;
69 case 'M':
70 case 'L':
71 case 'T':
72 o << ":" << path.m_x;
73 break;
74 case 'Q':
75 case 'S':
76 o << ":" << path.m_x << ":" << path.m_x1;
77 break;
78 case 'C':
79 o << ":" << path.m_x << ":" << path.m_x1 << ":" << path.m_x2;
80 break;
81 case 'A':
82 o << ":" << path.m_x << ":r=" << path.m_r;
83 if (path.m_largeAngle) o << ":largeAngle";
84 if (path.m_sweep) o << ":sweep";
85 if (path.m_rotate<0 || path.m_rotate>0) o << ":rot=" << path.m_rotate;
86 break;
87 case 'Z':
88 break;
89 default:
90 o << "###";
91 }
92 return o;
93 }
94
95 void MWAWGraphicShape::PathData::translate(MWAWVec2f const &decal)
96 {
97 if (m_type=='Z')
98 return;
99 m_x += decal;
100 if (m_type=='H' || m_type=='V' || m_type=='M' || m_type=='L' || m_type=='T' || m_type=='A')
101 return;
102 m_x1 += decal;
103 if (m_type=='Q' || m_type=='S')
104 return;
105 m_x2 += decal;
106 }
107
108 void MWAWGraphicShape::PathData::scale(MWAWVec2f const &scaling)
109 {
110 if (m_type=='Z')
111 return;
112 m_x = MWAWVec2f(m_x[0]*scaling[0], m_x[1]*scaling[1]);
113 if (m_type=='H' || m_type=='V' || m_type=='M' || m_type=='L' || m_type=='T' || m_type=='A')
114 return;
115 m_x1 = MWAWVec2f(m_x1[0]*scaling[0], m_x1[1]*scaling[1]);
116 if (m_type=='Q' || m_type=='S')
117 return;
118 m_x2 = MWAWVec2f(m_x2[0]*scaling[0], m_x2[1]*scaling[1]);
119 }
120
121 void MWAWGraphicShape::PathData::rotate(float angle, MWAWVec2f const &decal)
122 {
123 if (m_type=='Z')
124 return;
125 float angl=angle*float(M_PI/180.);
126 m_x = MWAWVec2f(std::cos(angl)*m_x[0]-std::sin(angl)*m_x[1],
127 std::sin(angl)*m_x[0]+std::cos(angl)*m_x[1])+decal;
128 if (m_type=='A') {
129 m_rotate += angle;
130 return;
131 }
132 if (m_type=='H' || m_type=='V' || m_type=='M' || m_type=='L' || m_type=='T')
133 return;
134 m_x1 = MWAWVec2f(std::cos(angl)*m_x1[0]-std::sin(angl)*m_x1[1],
135 std::sin(angl)*m_x1[0]+std::cos(angl)*m_x1[1])+decal;
136 if (m_type=='Q' || m_type=='S')
137 return;
138 m_x2 = MWAWVec2f(std::cos(angl)*m_x2[0]-std::sin(angl)*m_x2[1],
139 std::sin(angl)*m_x2[0]+std::cos(angl)*m_x2[1])+decal;
140 }
141
142 void MWAWGraphicShape::PathData::transform(MWAWTransformation const &matrix, float rotation)
143 {
144 if (m_type=='Z')
145 return;
146 m_x = matrix*m_x;
147 if (m_type=='A') {
148 m_rotate += rotation;
149 return;
150 }
151 if (m_type=='H' || m_type=='V' || m_type=='M' || m_type=='L' || m_type=='T')
152 return;
153 m_x1 = matrix*m_x1;
154 if (m_type=='Q' || m_type=='S')
155 return;
156 m_x2 = matrix*m_x2;
157 }
158
159 bool MWAWGraphicShape::PathData::get(librevenge::RVNGPropertyList &list, MWAWVec2f const &orig) const
160 {
161 list.clear();
162 std::string type("");
163 type += m_type;
164 list.insert("librevenge:path-action", type.c_str());
165 if (m_type=='Z')
166 return true;
167 if (m_type=='H') {
168 list.insert("svg:x",double(m_x[0]-orig[0]), librevenge::RVNG_POINT);
169 return true;
170 }
171 if (m_type=='V') {
172 list.insert("svg:y",double(m_x[1]-orig[1]), librevenge::RVNG_POINT);
173 return true;
174 }
175 list.insert("svg:x",double(m_x[0]-orig[0]), librevenge::RVNG_POINT);
176 list.insert("svg:y",double(m_x[1]-orig[1]), librevenge::RVNG_POINT);
177 if (m_type=='M' || m_type=='L' || m_type=='T')
178 return true;
179 if (m_type=='A') {
180 list.insert("svg:rx",double(m_r[0]), librevenge::RVNG_POINT);
181 list.insert("svg:ry",double(m_r[1]), librevenge::RVNG_POINT);
182 list.insert("librevenge:large-arc", m_largeAngle);
183 list.insert("librevenge:sweep", m_sweep);
184 list.insert("librevenge:rotate", double(m_rotate), librevenge::RVNG_GENERIC);
185 return true;
186 }
187 list.insert("svg:x1",double(m_x1[0]-orig[0]), librevenge::RVNG_POINT);
188 list.insert("svg:y1",double(m_x1[1]-orig[1]), librevenge::RVNG_POINT);
189 if (m_type=='Q' || m_type=='S')
190 return true;
191 list.insert("svg:x2",double(m_x2[0]-orig[0]), librevenge::RVNG_POINT);
192 list.insert("svg:y2",double(m_x2[1]-orig[1]), librevenge::RVNG_POINT);
193 if (m_type=='C')
194 return true;
195 MWAW_DEBUG_MSG(("MWAWGraphicShape::PathData::get: unknown command %c\n", m_type));
196 list.clear();
197 return false;
198 }
199
200 int MWAWGraphicShape::PathData::cmp(MWAWGraphicShape::PathData const &a) const
201 {
202 if (m_type < a.m_type) return 1;
203 if (m_type > a.m_type) return 1;
204 int diff = m_x.cmp(a.m_x);
205 if (diff) return diff;
206 diff = m_x1.cmp(a.m_x1);
207 if (diff) return diff;
208 diff = m_x2.cmp(a.m_x2);
209 if (diff) return diff;
210 diff = m_r.cmp(a.m_r);
211 if (diff) return diff;
212 if (m_rotate < a.m_rotate) return 1;
213 if (m_rotate > a.m_rotate) return -1;
214 if (m_largeAngle != a.m_largeAngle)
215 return m_largeAngle ? 1 : -1;
216 if (m_sweep != a.m_sweep)
217 return m_sweep ? 1 : -1;
218 return 0;
219 }
220
221 ////////////////////////////////////////////////////////////
222 // MWAWGraphicShape
223 ////////////////////////////////////////////////////////////
224 MWAWGraphicShape::~MWAWGraphicShape()
225 {
226 }
227
228 MWAWGraphicShape MWAWGraphicShape::line(MWAWVec2f const &orig, MWAWVec2f const &dest)
229 {
230 MWAWGraphicShape res;
231 res.m_type = MWAWGraphicShape::Line;
232 res.m_vertices.resize(2);
233 res.m_vertices[0]=orig;
234 res.m_vertices[1]=dest;
235 MWAWVec2f minPt(orig), maxPt(orig);
236 for (int c=0; c<2; ++c) {
237 if (orig[c] < dest[c])
238 maxPt[c]=dest[c];
239 else
240 minPt[c]=dest[c];
241 }
242 res.m_bdBox=MWAWBox2f(minPt,maxPt);
243 return res;
244 }
245
246 MWAWGraphicShape MWAWGraphicShape::measure(MWAWVec2f const &orig, MWAWVec2f const &dest)
247 {
248 MWAWGraphicShape res=line(orig,dest);
249 res.m_type= MWAWGraphicShape::Measure;
250 return res;
251 }
252
253 std::ostream &operator<<(std::ostream &o, MWAWGraphicShape const &sh)
254 {
255 o << "box=" << sh.m_bdBox << ",";
256 switch (sh.m_type) {
257 case MWAWGraphicShape::Line:
258 o << "line,";
259 if (sh.m_vertices.size()!=2)
260 o << "###pts,";
261 else
262 o << "pts=" << sh.m_vertices[0] << "<->" << sh.m_vertices[1] << ",";
263 break;
264 case MWAWGraphicShape::Measure:
265 o << "measure,";
266 if (sh.m_vertices.size()!=2)
267 o << "###pts,";
268 else
269 o << "pts=" << sh.m_vertices[0] << "<->" << sh.m_vertices[1] << ",";
270 break;
271 case MWAWGraphicShape::Rectangle:
272 o << "rect,";
273 if (sh.m_formBox!=sh.m_bdBox)
274 o << "box[rect]=" << sh.m_formBox << ",";
275 if (sh.m_cornerWidth!=MWAWVec2f(0,0))
276 o << "corners=" << sh.m_cornerWidth << ",";
277 break;
278 case MWAWGraphicShape::Circle:
279 o << "circle,";
280 break;
281 case MWAWGraphicShape::Arc:
282 case MWAWGraphicShape::Pie:
283 o << (sh.m_type == MWAWGraphicShape::Arc ? "arc," : "pie,");
284 o << "box[ellipse]=" << sh.m_formBox << ",";
285 o << "angle=" << sh.m_arcAngles << ",";
286 break;
287 case MWAWGraphicShape::Polygon:
288 case MWAWGraphicShape::Polyline:
289 if (sh.m_type==MWAWGraphicShape::Polygon)
290 o << "polygon,pts=[";
291 else
292 o << "polyline,pts=[";
293 for (auto const &pt : sh.m_vertices)
294 o << pt << ",";
295 o << "],";
296 break;
297 case MWAWGraphicShape::Path:
298 o << "path,pts=[";
299 for (auto const &pt : sh.m_path)
300 o << pt << ",";
301 o << "],";
302 break;
303 case MWAWGraphicShape::ShapeUnknown:
304 #if !defined(__clang__)
305 default:
306 #endif
307 o << "###unknown[shape],";
308 break;
309 }
310 o << sh.m_extra;
311 return o;
312 }
313
314 int MWAWGraphicShape::cmp(MWAWGraphicShape const &a) const
315 {
316 if (m_type < a.m_type) return 1;
317 if (m_type > a.m_type) return -1;
318 if (m_bdBox < a.m_bdBox) return 1;
319 if (m_bdBox > a.m_bdBox) return -1;
320 if (m_formBox < a.m_formBox) return 1;
321 if (m_formBox > a.m_formBox) return -1;
322 int diff = m_cornerWidth.cmp(a.m_cornerWidth);
323 if (diff) return diff;
324 diff = m_arcAngles.cmp(a.m_arcAngles);
325 if (diff) return diff;
326 if (m_vertices.size()<a.m_vertices.size()) return 1;
327 if (m_vertices.size()>a.m_vertices.size()) return -1;
328 for (size_t pt=0; pt < m_vertices.size(); ++pt) {
329 diff = m_vertices[pt].cmp(a.m_vertices[pt]);
330 if (diff) return diff;
331 }
332 if (m_path.size()<a.m_path.size()) return 1;
333 if (m_path.size()>a.m_path.size()) return -1;
334 for (size_t pt=0; pt < m_path.size(); ++pt) {
335 diff = m_path[pt].cmp(a.m_path[pt]);
336 if (diff) return diff;
337 }
338 return 0;
339 }
340
341 MWAWBox2f MWAWGraphicShape::getBdBox(MWAWGraphicStyle const &style, bool moveToO) const
342 {
343 MWAWBox2f bdBox=m_bdBox;
344 if (moveToO)
345 bdBox=MWAWBox2f(MWAWVec2f(0,0),m_bdBox.size());
346 if (style.hasLine())
347 bdBox.extend(style.m_lineWidth/2.f);
348 if (m_type==Line) {
349 // fixme: add 4pt for each arrows
350 int numArrows=(style.m_arrows[0].isEmpty() ? 0 : 1)+(style.m_arrows[1].isEmpty() ? 0 : 1);
351 if (numArrows) bdBox.extend(float(2*numArrows));
352 }
353 return bdBox;
354 }
355
356 void MWAWGraphicShape::translate(MWAWVec2f const &decal)
357 {
358 if (decal==MWAWVec2f(0,0))
359 return;
360 m_bdBox=MWAWBox2f(m_bdBox.min()+decal, m_bdBox.max()+decal);
361 m_formBox=MWAWBox2f(m_formBox.min()+decal, m_formBox.max()+decal);
362 for (auto &pt : m_vertices)
363 pt+=decal;
364 for (auto &pt : m_path)
365 pt.translate(decal);
366 }
367
368 void MWAWGraphicShape::scale(MWAWVec2f const &scaling)
369 {
370 // checkme: does not work for symetry if shape is an arc...
371 m_bdBox=MWAWBox2f(MWAWVec2f(scaling[0]*m_bdBox.min()[0],scaling[1]*m_bdBox.min()[1]),
372 MWAWVec2f(scaling[0]*m_bdBox.max()[0],scaling[1]*m_bdBox.max()[1]));
373 m_formBox=MWAWBox2f(MWAWVec2f(scaling[0]*m_formBox.min()[0],scaling[1]*m_formBox.min()[1]),
374 MWAWVec2f(scaling[0]*m_formBox.max()[0],scaling[1]*m_formBox.max()[1]));
375 for (auto &pt : m_vertices)
376 pt=MWAWVec2f(scaling[0]*pt[0], scaling[1]*pt[1]);
377 for (auto &pt : m_path)
378 pt.scale(scaling);
379 }
380
381 MWAWGraphicShape MWAWGraphicShape::rotate(float angle, MWAWVec2f const &center) const
382 {
383 while (angle >= 360) angle -= 360;
384 while (angle <= -360) angle += 360;
385 if (angle >= -1.e-3f && angle <= 1.e-3f) return *this;
386 float angl=angle*float(M_PI/180.);
387 MWAWVec2f decal=center-MWAWVec2f(std::cos(angl)*center[0]-std::sin(angl)*center[1],
388 std::sin(angl)*center[0]+std::cos(angl)*center[1]);
389 MWAWBox2f fBox;
390 for (int i=0; i < 4; ++i) {
391 MWAWVec2f pt=MWAWVec2f(m_bdBox[i%2][0],m_bdBox[i/2][1]);
392 pt = MWAWVec2f(std::cos(angl)*pt[0]-std::sin(angl)*pt[1],
393 std::sin(angl)*pt[0]+std::cos(angl)*pt[1])+decal;
394 if (i==0) fBox=MWAWBox2f(pt,pt);
395 else fBox=fBox.getUnion(MWAWBox2f(pt,pt));
396 }
397 MWAWGraphicShape res = path(fBox);
398 res.m_path=getPath(false);
399 for (auto &pt : res.m_path)
400 pt.rotate(angle, decal);
401 return res;
402 }
403
404 MWAWGraphicShape MWAWGraphicShape::transform(MWAWTransformation const &matrix) const
405 {
406 if (matrix.isIdentity()) return *this;
407 if (matrix[0][1]<=0 && matrix[0][1]>=0 && matrix[1][0]<=0 && matrix[1][0]>=0) {
408 MWAWGraphicShape res=*this;
409 if (matrix[0][0]<1 || matrix[0][0]>1 || matrix[1][1]<1 || matrix[1][1]>1)
410 res.scale(MWAWVec2f(matrix[0][0], matrix[1][1]));
411 res.translate(MWAWVec2f(matrix[0][2],matrix[1][2]));
412 return res;
413 }
414
415 MWAWBox2f fBox;
416 for (int i=0; i < 4; ++i) {
417 MWAWVec2f pt = matrix*MWAWVec2f(m_bdBox[i%2][0],m_bdBox[i/2][1]);
418 if (i==0) fBox=MWAWBox2f(pt,pt);
419 else fBox=fBox.getUnion(MWAWBox2f(pt,pt));
420 }
421 MWAWGraphicShape res = path(fBox);
422 res.m_path=getPath(true);
423
424 MWAWTransformation transf;
425 float rotation=0;
426 MWAWVec2f shearing;
427 if (!matrix.decompose(rotation,shearing,transf,fBox.center()))
428 rotation=0;
429 for (auto &pt : res.m_path)
430 pt.transform(matrix, rotation);
431 return res;
432 }
433
434 bool MWAWGraphicShape::addPathTo(MWAWVec2f const &orig, librevenge::RVNGPropertyListVector &vect) const
435 {
436 MWAWVec2f decal=orig-m_bdBox[0];
437 std::vector<MWAWGraphicShape::PathData> fPath=getPath(false);
438 size_t n=fPath.size();
439 if (!n) {
440 MWAW_DEBUG_MSG(("MWAWGraphicShape::addPathTo: can not find the path\n"));
441 return false;
442 }
443 librevenge::RVNGPropertyList list;
444 for (auto const &pt : fPath) {
445 list.clear();
446 if (pt.get(list, -1.0f*decal))
447 vect.append(list);
448 }
449 if (fPath[n-1].m_type != 'Z') {
450 // odg need a closed path to draw surface, so ...
451 list.clear();
452 list.insert("librevenge:path-action", "Z");
453 vect.append(list);
454 }
455 return true;
456 }
457
458 MWAWGraphicShape::Command MWAWGraphicShape::addTo(MWAWVec2f const &orig, bool asSurface, librevenge::RVNGPropertyList &propList) const
459 {
460 MWAWVec2f pt;
461 librevenge::RVNGPropertyList list;
462 librevenge::RVNGPropertyListVector vect;
463 MWAWVec2f decal=orig-m_bdBox[0];
464 switch (m_type) {
465 case Line:
466 case Measure:
467 if (m_vertices.size()!=2) break;
468 if (m_type==Measure)
469 propList.insert("draw:show-unit", true);
470 pt=m_vertices[0]+decal;
471 list.insert("svg:x",double(pt.x()), librevenge::RVNG_POINT);
472 list.insert("svg:y",double(pt.y()), librevenge::RVNG_POINT);
473 vect.append(list);
474 pt=m_vertices[1]+decal;
475 list.insert("svg:x",double(pt.x()), librevenge::RVNG_POINT);
476 list.insert("svg:y",double(pt.y()), librevenge::RVNG_POINT);
477 vect.append(list);
478 propList.insert("svg:points", vect);
479 return C_Polyline;
480 case Rectangle:
481 if (m_cornerWidth[0] > 0 && m_cornerWidth[1] > 0) {
482 propList.insert("svg:rx",double(m_cornerWidth[0]), librevenge::RVNG_POINT);
483 propList.insert("svg:ry",double(m_cornerWidth[1]), librevenge::RVNG_POINT);
484 }
485 pt=m_formBox[0]+decal;
486 propList.insert("svg:x",double(pt.x()), librevenge::RVNG_POINT);
487 propList.insert("svg:y",double(pt.y()), librevenge::RVNG_POINT);
488 pt=m_formBox.size();
489 propList.insert("svg:width",double(pt.x()), librevenge::RVNG_POINT);
490 propList.insert("svg:height",double(pt.y()), librevenge::RVNG_POINT);
491 return C_Rectangle;
492 case Circle:
493 pt=0.5f*(m_formBox[0]+m_formBox[1])+decal;
494 propList.insert("svg:cx",double(pt.x()), librevenge::RVNG_POINT);
495 propList.insert("svg:cy",double(pt.y()), librevenge::RVNG_POINT);
496 pt=0.5f*(m_formBox[1]-m_formBox[0]);
497 propList.insert("svg:rx",double(pt.x()), librevenge::RVNG_POINT);
498 propList.insert("svg:ry",double(pt.y()), librevenge::RVNG_POINT);
499 return C_Ellipse;
500 case Arc:
501 case Pie: {
502 MWAWVec2f center=0.5f*(m_formBox[0]+m_formBox[1])+decal;
503 MWAWVec2f rad=0.5f*(m_formBox[1]-m_formBox[0]);
504 float angl0=m_arcAngles[0];
505 float angl1=m_arcAngles[1];
506 if (rad[1]<0) {
507 static bool first=true;
508 if (first) {
509 MWAW_DEBUG_MSG(("MWAWGraphicShape::addTo: oops radiusY for arc is negative, inverse it\n"));
510 first=false;
511 }
512 rad[1]=-rad[1];
513 }
514 while (angl1<angl0)
515 angl1+=360.f;
516 while (angl1>angl0+360.f)
517 angl1-=360.f;
518 if (angl1-angl0>=180.f && angl1-angl0<=180.f)
519 angl1+=0.01f;
520 float angl=angl0*float(M_PI/180.);
521 bool addCenter=m_type==Pie && asSurface;
522 if (addCenter) {
523 pt=center;
524 list.insert("librevenge:path-action", "M");
525 list.insert("svg:x",double(pt.x()), librevenge::RVNG_POINT);
526 list.insert("svg:y",double(pt.y()), librevenge::RVNG_POINT);
527 vect.append(list);
528 }
529 list.clear();
530 pt=center+MWAWVec2f(std::cos(angl)*rad[0],-std::sin(angl)*rad[1]);
531 list.insert("librevenge:path-action", addCenter ? "L" : "M");
532 list.insert("svg:x",double(pt.x()), librevenge::RVNG_POINT);
533 list.insert("svg:y",double(pt.y()), librevenge::RVNG_POINT);
534 vect.append(list);
535
536 list.clear();
537 angl=angl1*float(M_PI/180.);
538 pt=center+MWAWVec2f(std::cos(angl)*rad[0],-std::sin(angl)*rad[1]);
539 list.insert("librevenge:path-action", "A");
540 list.insert("librevenge:large-arc", !(angl1-angl0<180.f));
541 list.insert("librevenge:sweep", false);
542 list.insert("svg:rx",double(rad.x()), librevenge::RVNG_POINT);
543 list.insert("svg:ry",double(rad.y()), librevenge::RVNG_POINT);
544 list.insert("svg:x",double(pt.x()), librevenge::RVNG_POINT);
545 list.insert("svg:y",double(pt.y()), librevenge::RVNG_POINT);
546 vect.append(list);
547 if (asSurface) {
548 list.clear();
549 list.insert("librevenge:path-action", "Z");
550 vect.append(list);
551 }
552
553 propList.insert("svg:d", vect);
554 return C_Path;
555 }
556 case Polygon:
557 case Polyline: {
558 size_t n=m_vertices.size();
559 if (n<2) break;
560 for (auto point : m_vertices) {
561 list.clear();
562 point += decal;
563 list.insert("svg:x", double(point.x()), librevenge::RVNG_POINT);
564 list.insert("svg:y", double(point.y()), librevenge::RVNG_POINT);
565 vect.append(list);
566 }
567 propList.insert("svg:points", vect);
568 return (asSurface && m_type==Polygon) ? C_Polygon : C_Polyline;
569 }
570 case Path: {
571 size_t n=m_path.size();
572 if (!n) break;
573 for (auto const &point : m_path) {
574 list.clear();
575 if (point.get(list, -1.0f*decal))
576 vect.append(list);
577 }
578 if (asSurface && m_path[n-1].m_type != 'Z') {
579 // odg need a closed path to draw surface, so ...
580 list.clear();
581 list.insert("librevenge:path-action", "Z");
582 vect.append(list);
583 }
584 propList.insert("svg:d", vect);
585 return C_Path;
586 }
587 case ShapeUnknown:
588 #if !defined(__clang__)
589 default:
590 #endif
591 break;
592 }
593 MWAW_DEBUG_MSG(("MWAWGraphicShape::addTo: can not send a shape with type=%d\n", int(m_type)));
594 return C_Bad;
595 }
596
597 std::vector<MWAWGraphicShape::PathData> MWAWGraphicShape::getPath(bool forTransformation) const
598 {
599 std::vector<MWAWGraphicShape::PathData> res;
600 float const delta=0.55228f;
601 switch (m_type) {
602 case Measure:
603 MWAW_DEBUG_MSG(("MWAWGraphicShape::getPath: called on a measure, transform it in line\n"));
604 MWAW_FALLTHROUGH;
605 case Line:
606 case Polygon:
607 case Polyline: {
608 size_t n=m_vertices.size();
609 if (n<2) break;
610 res.push_back(PathData('M',m_vertices[0]));
611 for (size_t i = 1; i < n; ++i)
612 res.push_back(PathData('L', m_vertices[i]));
613 break;
614 }
615 case Rectangle:
616 if (m_cornerWidth[0] > 0 && m_cornerWidth[1] > 0) {
617 MWAWBox2f box=m_formBox;
618 if (box.min()[0]>box.max()[0]) std::swap(box.min()[0],box.max()[0]);
619 if (box.min()[1]>box.max()[1]) std::swap(box.min()[1],box.max()[1]);
620 MWAWVec2f c=m_cornerWidth;
621 if (2*c[0]>box.size()[0]) c[0]=0.5f*box.size()[0];
622 if (2*c[1]>box.size()[1]) c[1]=0.5f*box.size()[1];
623 if (forTransformation) {
624 MWAWVec2f pt0(box[1][0]-c[0],box[0][1]);
625 res.push_back(PathData('M',pt0));
626 MWAWVec2f pt1(box[1][0],box[0][1]+c[1]);
627 res.push_back(PathData('C',pt1,pt0+MWAWVec2f(delta*c[0],0),pt1-MWAWVec2f(0,delta*c[1])));
628 pt0=MWAWVec2f(box[1][0],box[1][1]-c[1]);
629 res.push_back(PathData('L',pt0));
630 pt1=MWAWVec2f(box[1][0]-c[0],box[1][1]);
631 res.push_back(PathData('C',pt1,pt0+MWAWVec2f(0,delta*c[1]),pt1+MWAWVec2f(delta*c[0],0)));
632 pt0=MWAWVec2f(box[0][0]+c[0],box[1][1]);
633 res.push_back(PathData('L',pt0));
634 pt1=MWAWVec2f(box[0][0],box[1][1]-c[1]);
635 res.push_back(PathData('C',pt1,pt0-MWAWVec2f(delta*c[0],0),pt1+MWAWVec2f(0,delta*c[1])));
636 pt0=MWAWVec2f(box[0][0],box[0][1]+c[1]);
637 res.push_back(PathData('L',pt0));
638 pt1=MWAWVec2f(box[0][0]+c[0],box[0][1]);
639 res.push_back(PathData('C',pt1,pt0-MWAWVec2f(0,delta*c[1]),pt1-MWAWVec2f(delta*c[0],0)));
640 }
641 else {
642 res.push_back(PathData('M',MWAWVec2f(box[1][0]-c[0],box[0][1])));
643 PathData data('A',MWAWVec2f(box[1][0],box[0][1]+c[1]));
644 data.m_r=c;
645 data.m_sweep=true;
646 res.push_back(data);
647 res.push_back(PathData('L',MWAWVec2f(box[1][0],box[1][1]-c[1])));
648 data.m_x=MWAWVec2f(box[1][0]-c[0],box[1][1]);
649 res.push_back(data);
650 res.push_back(PathData('L',MWAWVec2f(box[0][0]+c[0],box[1][1])));
651 data.m_x=MWAWVec2f(box[0][0],box[1][1]-c[1]);
652 res.push_back(data);
653 res.push_back(PathData('L',MWAWVec2f(box[0][0],box[0][1]+c[1])));
654 data.m_x=MWAWVec2f(box[0][0]+c[0],box[0][1]);
655 res.push_back(data);
656 }
657 res.push_back(PathData('Z'));
658 break;
659 }
660 res.push_back(PathData('M',m_formBox[0]));
661 res.push_back(PathData('L',MWAWVec2f(m_formBox[0][0],m_formBox[1][1])));
662 res.push_back(PathData('L',m_formBox[1]));
663 res.push_back(PathData('L',MWAWVec2f(m_formBox[1][0],m_formBox[0][1])));
664 res.push_back(PathData('Z'));
665 break;
666 case Circle: {
667 if (forTransformation) {
668 MWAWVec2f center=m_formBox.center();
669 MWAWVec2f dir=0.5f*delta*(m_formBox[1]-m_formBox[0]);
670 MWAWVec2f pt0(m_formBox[0][0],center[1]);
671 res.push_back(PathData('M',pt0));
672 MWAWVec2f pt1(center[0],m_formBox[0][1]);
673 res.push_back(PathData('C',pt1, pt0-MWAWVec2f(0,dir[1]), pt1-MWAWVec2f(dir[0],0)));
674 pt0=MWAWVec2f(m_formBox[1][0],center[1]);
675 res.push_back(PathData('C',pt0, pt1+MWAWVec2f(dir[0],0), pt0-MWAWVec2f(0,dir[1])));
676 pt1=MWAWVec2f(center[0],m_formBox[1][1]);
677 res.push_back(PathData('C',pt1, pt0+MWAWVec2f(0,dir[1]), pt1+MWAWVec2f(dir[0],0)));
678 pt0=MWAWVec2f(m_formBox[0][0],center[1]);
679 res.push_back(PathData('C',pt0, pt1-MWAWVec2f(dir[0],0), pt0+MWAWVec2f(0,dir[1])));
680 res.push_back(PathData('Z'));
681 }
682 else {
683 MWAWVec2f pt0 = MWAWVec2f(m_formBox[0][0],0.5f*(m_formBox[0][1]+m_formBox[1][1]));
684 MWAWVec2f pt1 = MWAWVec2f(m_formBox[1][0],pt0[1]);
685 res.push_back(PathData('M',pt0));
686 PathData data('A',pt1);
687 data.m_r=0.5f*(m_formBox[1]-m_formBox[0]);
688 data.m_largeAngle=true;
689 res.push_back(data);
690 data.m_x=pt0;
691 res.push_back(data);
692 }
693 break;
694 }
695 case Arc:
696 case Pie: {
697 MWAWVec2f center=0.5f*(m_formBox[0]+m_formBox[1]);
698 MWAWVec2f rad=0.5f*(m_formBox[1]-m_formBox[0]);
699 float angl0=m_arcAngles[0];
700 float angl1=m_arcAngles[1];
701 if (rad[1]<0) {
702 static bool first=true;
703 if (first) {
704 MWAW_DEBUG_MSG(("MWAWGraphicShape::getPath: oops radiusY for arc is negative, inverse it\n"));
705 first=false;
706 }
707 rad[1]=-rad[1];
708 }
709 while (angl1<angl0)
710 angl1+=360.f;
711 while (angl1>angl0+360.f)
712 angl1-=360.f;
713 if (angl1-angl0>=180.f && angl1-angl0<=180.f)
714 angl1+=0.01f;
715 float angl=angl0*float(M_PI/180.);
716 bool addCenter=m_type==Pie;
717 if (addCenter)
718 res.push_back(PathData('M', center));
719 MWAWVec2f pt=center+MWAWVec2f(std::cos(angl)*rad[0],-std::sin(angl)*rad[1]);
720 res.push_back(PathData(addCenter ? 'L' : 'M', pt));
721 if (!forTransformation) {
722 angl=angl1*float(M_PI/180.);
723 pt=center+MWAWVec2f(std::cos(angl)*rad[0],-std::sin(angl)*rad[1]);
724 PathData data('A',pt);
725 data.m_largeAngle=(angl1-angl0>=180.f);
726 data.m_r=rad;
727 res.push_back(data);
728 }
729 else {
730 int N=int(angl1-angl0)/90;
731 float dAngle=float(angl1-angl0)/float(N+1);
732 for (int i=0; i<=N; ++i) {
733 float newAngl= i==N ? angl1 : angl0+float(i+1)*dAngle;
734 newAngl*=float(M_PI/180.);
735 MWAWVec2f newPt=center+MWAWVec2f(std::cos(angl1)*rad[0],-std::sin(angl1)*rad[1]);
736 MWAWVec2f dir(-std::sin(angl)*rad[0],-std::cos(angl)*rad[1]);
737 MWAWVec2f newDir(-std::sin(newAngl)*rad[0],-std::cos(newAngl)*rad[1]);
738 float deltaDir=4/3.f*std::tan((newAngl-angl)/4);
739 res.push_back(PathData('C',newPt,pt+deltaDir*dir,newPt-deltaDir*newDir));
740 pt=newPt;
741 angl=newAngl;
742 }
743 if (m_type==Pie) res.push_back(PathData('Z'));
744 }
745 break;
746 }
747 case Path:
748 return m_path;
749 case ShapeUnknown:
750 #if !defined(__clang__)
751 default:
752 #endif
753 MWAW_DEBUG_MSG(("MWAWGraphicShape::getPath: unexpected type\n"));
754 break;
755 }
756 return res;
757 }
758
759 std::vector<MWAWGraphicShape::PathData> MWAWGraphicShape::offsetVertices(std::vector<MWAWGraphicShape::PathData> const &path, float offset, MWAWBox2f &finalBox)
760 {
761 size_t N=path.size();
762
763 MWAWVec2f prevPoint(0,0);
764 std::vector<MWAWVec2f> listPoints;
765 listPoints.reserve(N);
766 size_t begComponent=0;
767 std::vector<size_t> componentsSize;
768 for (size_t i=0; i<N; ++i) {
769 auto const &p=path[i];
770 if (p.m_type=='Z') {
771 listPoints.push_back(prevPoint);
772 componentsSize.push_back(i+1-begComponent);
773 begComponent=i+1;
774 continue;
775 }
776 auto pt=p.m_x;
777 if (p.m_type=='H')
778 pt[1]=prevPoint[1];
779 else if (p.m_type=='V')
780 pt[0]=prevPoint[0];
781 listPoints.push_back(pt);
782 prevPoint=pt;
783 }
784 if (begComponent!=N)
785 componentsSize.push_back(N-begComponent);
786
787 std::vector<MWAWGraphicShape::PathData> res;
788 res.reserve(N);
789
790 size_t first=0;
791 bool firstPtInBox=true;
792 for (auto n : componentsSize) {
793 if (!n) continue;
794 bool const endZ=path[first+n-1].m_type=='Z';
795 size_t nPt=endZ ? n-1 : n;
796 if (nPt<=1) continue;
797
798 std::vector<MWAWVec2f> decal;
799 decal.reserve(nPt);
800 bool const endEquiv = endZ ? listPoints[first] == listPoints[first+nPt-1] : false;
801 size_t nMod=endEquiv ? nPt-1 : nPt;
802 for (size_t i=0; i<nMod; ++i) {
803 MWAWVec2f dir=listPoints[first+((i+1!=nMod || endZ) ? (i+1)%nMod : i)] - listPoints[first+(i+nMod-1)%nMod];
804 if (dir==MWAWVec2f(0,0))
805 dir=listPoints[first+(i+1==nMod ? i : i+1)] - listPoints[first+(i+1==nMod ? i-1 : i)];
806 float len=std::sqrt(dir[0]*dir[0]+dir[1]*dir[1]);
807 if (len<=0) {
808 decal.push_back(MWAWVec2f(0,0));
809 continue;
810 }
811 decal.push_back(MWAWVec2f(-dir[1]*offset/len, dir[0]*offset/len));
812 }
813 for (size_t i=0; i<nMod; ++i) {
814 if (firstPtInBox) {
815 finalBox=MWAWBox2f(listPoints[first+i]+decal[i], listPoints[first+i]+decal[i]);
816 firstPtInBox=false;
817 }
818 else
819 finalBox=finalBox.getUnion(MWAWBox2f(listPoints[first+i]+decal[i], listPoints[first+i]+decal[i]));
820 }
821 if (endEquiv) decal.push_back(decal[0]);
822
823
824 for (size_t i=0; i<n; ++i) {
825 auto p=path[first+i];
826 if (p.m_type=='Z') {
827 res.push_back(p);
828 continue;
829 }
830 p.m_x+=decal[i];
831 if (p.m_type=='H' || p.m_type=='V' || p.m_type=='M' || p.m_type=='L' || p.m_type=='T' || p.m_type=='A') {
832 res.push_back(p);
833 continue;
834 }
835 if (p.m_type=='Q') {
836 p.m_x1 += 0.5f*(decal[i]+decal[(i+nPt-1)%nPt]);
837 res.push_back(p);
838 continue;
839 }
840 if (p.m_type=='S') {
841 p.m_x1 += decal[i];
842 res.push_back(p);
843 continue;
844 }
845 p.m_x1 += decal[(i+nPt-1)%nPt];
846 p.m_x2 += decal[i];
847 res.push_back(p);
848 }
849
850 first+=n;
851 }
852 return res;
853 }
854
855 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab:
856