Debian Codegraph @

src/Gui/SoFCVectorizeSVGAction.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #include <qglobal.h>
25 #include <iomanip>
26 #include <ios>
27 #include <Inventor/SbBSPTree.h>
28
29
30 #include <Base/FileInfo.h>
31
32 #include "SoFCVectorizeSVGAction.h"
33
34
35 using namespace Gui;
36
37 class SoVectorizeItem
38 {
39 public:
40 SoVectorizeItem()
41 {
42 this->type = UNDEFINED;
43 this->depth = 0.0f;
44 }
45 // quick and easy type system
46 enum Type
47 {
48 UNDEFINED,
49 LINE,
50 TRIANGLE,
51 TEXT,
52 POINT,
53 IMAGE
54 };
55 int type;
56 float depth; // for depth sorting
57 };
58
59 class SoVectorizePoint: public SoVectorizeItem
60 {
61 public:
62 SoVectorizePoint()
63 {
64 this->type = POINT;
65 this->vidx = 0;
66 this->size = 1.0f;
67 this->col = 0;
68 }
69 int vidx; // index to BSPtree coordinate
70 float size; // Coin size (pixels)
71 uint32_t col;
72 };
73
74 class SoVectorizeTriangle: public SoVectorizeItem
75 {
76 public:
77 SoVectorizeTriangle()
78 {
79 this->type = TRIANGLE;
80 }
81 int vidx[3]; // indices to BSPtree coordinates
82 uint32_t col[3];
83 };
84
85 class SoVectorizeLine: public SoVectorizeItem
86 {
87 public:
88 SoVectorizeLine()
89 {
90 this->type = LINE;
91 vidx[0] = 0;
92 vidx[1] = 0;
93 col[0] = 0;
94 col[1] = 0;
95 this->pattern = 0xffff;
96 this->width = 1.0f;
97 }
98 int vidx[2]; // indices to BSPtree coordinates
99 uint32_t col[2];
100 uint16_t pattern; // Coin line pattern
101 float width; // Coin line width (pixels)
102 };
103
104 class SoVectorizeText: public SoVectorizeItem
105 {
106 public:
107 SoVectorizeText()
108 {
109 this->type = TEXT;
110 this->fontsize = 10;
111 this->col = 0;
112 this->justification = LEFT;
113 }
114
115 enum Justification
116 {
117 LEFT,
118 RIGHT,
119 CENTER
120 };
121
122 SbName fontname;
123 float fontsize; // size in normalized coordinates
124 SbString string;
125 SbVec2f pos; // pos in normalized coordinates
126 uint32_t col;
127 Justification justification;
128 };
129
130 class SoVectorizeImage: public SoVectorizeItem
131 {
132 public:
133 SoVectorizeImage()
134 {
135 this->type = IMAGE;
136 this->image.data = nullptr;
137 this->image.nc = 0;
138 }
139
140 SbVec2f pos; // pos in normalized coordinates
141 SbVec2f size; // size in normalized coordinates
142
143 struct Image
144 {
145 const unsigned char* data;
146 SbVec2s size;
147 int nc;
148 } image;
149 };
150
151 // ----------------------------------------------------------------
152
153 SoSVGVectorOutput::SoSVGVectorOutput() = default;
154
155 SoSVGVectorOutput::~SoSVGVectorOutput()
156 {
157 closeFile();
158 }
159
160 SbBool SoSVGVectorOutput::openFile(const char* filename)
161 {
162 Base::FileInfo fi(filename);
163 #ifdef _MSC_VER
164 this->file.open(fi.toStdWString().c_str(), std::ios::out | std::ios::binary);
165 #else
166 this->file.open(fi.filePath().c_str(), std::ios::out | std::ios::binary);
167 #endif
168
169 return this->file.is_open();
170 }
171
172 void SoSVGVectorOutput::closeFile()
173 {
174 if (this->file.is_open()) {
175 this->file.close();
176 }
177 }
178
179 std::fstream& SoSVGVectorOutput::getFileStream()
180 {
181 return this->file;
182 }
183
184 // ----------------------------------------------------------------
185
186 namespace Gui
187 {
188 class SoFCVectorizeSVGActionP
189 {
190 public:
191 SoFCVectorizeSVGActionP(SoFCVectorizeSVGAction* p)
192 {
193 this->publ = p;
194 }
195
196 void printCircle(const SbVec3f& v, const SbColor& c, const float radius) const;
197 void printSquare(const SbVec3f& v, const SbColor& c, const float size) const;
198 void printTriangle(const SbVec3f* v, const SbColor* c) const;
199 void printTriangle(const SoVectorizeTriangle* item) const;
200 void printLine(const SoVectorizeLine* item) const;
201 void printPoint(const SoVectorizePoint* item) const;
202 void printText(const SoVectorizeText* item) const;
203 void printImage(const SoVectorizeImage* item) const;
204
205 private:
206 SoFCVectorizeSVGAction* publ;
207 };
208 } // namespace Gui
209
210 void SoFCVectorizeSVGActionP::printText(const SoVectorizeText* item) const
211 {
212 SbVec2f mul = publ->getRotatedViewportSize();
213 SbVec2f add = publ->getRotatedViewportStartpos();
214 float posx = item->pos[0] * mul[0] + add[0];
215 float posy = item->pos[1] * mul[1] + add[1];
216
217 std::ostream& str = publ->getSVGOutput()->getFileStream();
218 str << "<text x=\"" << posx << "\" y=\"" << posy
219 << "\" "
220 "font-size=\""
221 << item->fontsize * mul[1] << "px\">" << item->string.getString() << "</text>" << std::endl;
222 }
223
224 void SoFCVectorizeSVGActionP::printTriangle(const SoVectorizeTriangle* item) const
225 {
226 SbVec2f mul = publ->getRotatedViewportSize();
227 SbVec2f add = publ->getRotatedViewportStartpos();
228
229 const SbBSPTree& bsp = publ->getBSPTree();
230
231 SbVec3f v[3];
232 SbColor c[3];
233 float t[3];
234
235 for (int i = 0; i < 3; i++) {
236 v[i] = bsp.getPoint(item->vidx[i]);
237 v[i][0] = (v[i][0] * mul[0]) + add[0];
238 v[i][1] = ((1.0f - v[i][1]) * mul[1]) + add[1];
239 c[i].setPackedValue(item->col[i], t[i]);
240 }
241 this->printTriangle((SbVec3f*)v, (SbColor*)c);
242 }
243 /* TODO: Support gradients, e.g.
244 <defs>
245 <linearGradient id="verlauf" x1="0%" y1="100%" x2="100%" y2="100%">
246 <stop offset="0%" stop-color="black" stop-opacity="100%" />
247 <stop offset="100%" stop-color="white" stop-opacity="50%" />
248 </linearGradient>
249 </defs>
250 */
251 /* Example: color per vertex
252 #Inventor V2.1 ascii
253 Separator {
254
255 Coordinate3 {
256 point [ 0.000000 0.000000 0.000000,
257 100.000000 50.000000 0.000000,
258 0.000000 100.000000 0.000000 ]
259
260 }
261 Material {
262 diffuseColor [ 1 1 0, 0 0 1, 1 0 0 ]
263 }
264
265 MaterialBinding {
266 value PER_VERTEX
267 }
268 IndexedFaceSet {
269 coordIndex [ 0, 1, 2, -1 ]
270 }
271 }
272
273 <?xml version="1.0"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
274 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
275 <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"
276 xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <linearGradient id="red"
277 gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="35" y2="80"> <stop offset="0"
278 stop-color="rgb(255,0,0)" stop-opacity="1"/> <stop offset="1" stop-color="rgb(255,0,0)"
279 stop-opacity="0"/>
280 </linearGradient>
281 <linearGradient id="blue" gradientUnits="userSpaceOnUse" x1="100" y1="50" x2="0" y2="50">
282 <stop offset="0" stop-color="rgb(0,0,255)" stop-opacity="1"/>
283 <stop offset="1" stop-color="rgb(0,0,255)" stop-opacity="0"/>
284 </linearGradient>
285 <linearGradient id="yellow" gradientUnits="userSpaceOnUse" x1="0" y1="100" x2="40" y2="20">
286 <stop offset="0" stop-color="rgb(255,255,0)" stop-opacity="1"/>
287 <stop offset="1" stop-color="rgb(255,255,0)" stop-opacity="0"/>
288 </linearGradient>
289 <path id="triangle1" d="M0 0 L100 50 L0 100 z"/>
290 <filter id="colorAdd">
291 <feComposite in="SourceGraphic" in2="BackgroundImage" operator="arithmetic" k2="1" k3="1"/>
292 </filter>
293 <filter id="Matrix1">
294 <feColorMatrix type="matrix" values="
295 1 0 0 0 0
296 0 1 0 0 0
297 0 0 1 0 0
298 1 1 1 1 0
299 0 0 0 0 1
300 "/>
301 </filter>
302 </defs>
303 <g filter="url(#Matrix1)">
304 <use xlink:href="#triangle1" fill="url(#blue)"/>
305 <use xlink:href="#triangle1" fill="url(#yellow)" filter="url(#colorAdd)"/>
306 <use xlink:href="#triangle1" fill="url(#red)" filter="url(#colorAdd)"/>
307 </g>
308 </svg>
309 */
310 void SoFCVectorizeSVGActionP::printTriangle(const SbVec3f* v, const SbColor* c) const
311 {
312 if (v[0] == v[1] || v[1] == v[2] || v[0] == v[2]) {
313 return;
314 }
315 uint32_t cc = c->getPackedValue();
316
317 std::ostream& str = publ->getSVGOutput()->getFileStream();
318 str << "<path d=\"M " << v[2][0] << "," << v[2][1] << " L " << v[1][0] << "," << v[1][1] << " "
319 << v[0][0] << "," << v[0][1] << " z\"" << std::endl
320 << " style=\"fill:#" << std::hex << std::setw(6) << std::setfill('0') << (cc >> 8)
321 << "; stroke:#" << std::hex << std::setw(6) << std::setfill('0') << (cc >> 8) << ";"
322 << std::endl
323 << " stroke-width:" << publ->getLineWidth() << ";" << std::endl
324 << " stroke-linecap:round;stroke-linejoin:round\"/>" << std::endl;
325 }
326
327 void SoFCVectorizeSVGActionP::printCircle(const SbVec3f& v, const SbColor& c, const float radius) const
328 {
329 // todo
330 Q_UNUSED(v);
331 Q_UNUSED(c);
332 Q_UNUSED(radius);
333 }
334
335 void SoFCVectorizeSVGActionP::printSquare(const SbVec3f& v, const SbColor& c, const float size) const
336 {
337 // todo
338 Q_UNUSED(v);
339 Q_UNUSED(c);
340 Q_UNUSED(size);
341 }
342
343 void SoFCVectorizeSVGActionP::printLine(const SoVectorizeLine* item) const
344 {
345 SbVec2f mul = publ->getRotatedViewportSize();
346 SbVec2f add = publ->getRotatedViewportStartpos();
347
348 const SbBSPTree& bsp = publ->getBSPTree();
349
350 SbVec3f v[2];
351 SbColor c[2];
352 float t[2];
353
354 for (int i = 0; i < 2; i++) {
355 v[i] = bsp.getPoint(item->vidx[i]);
356 v[i][0] = (v[i][0] * mul[0]) + add[0];
357 v[i][1] = ((1.0f - v[i][1]) * mul[1]) + add[1];
358 c[i].setPackedValue(item->col[i], t[i]);
359 }
360 uint32_t cc = c[0].getPackedValue();
361
362 std::ostream& str = publ->getSVGOutput()->getFileStream();
363 str << "<line "
364 << "x1=\"" << v[0][0] << "\" y1=\"" << v[0][1] << "\" "
365 << "x2=\"" << v[1][0] << "\" y2=\"" << v[1][1] << "\" "
366 << "stroke=\"#" << std::hex << std::setw(6) << std::setfill('0') << (cc >> 8) << "\""
367 << " stroke-linecap=\"square\" "
368 << " stroke-width=\"" << publ->getLineWidth() << "\" />\n";
369 }
370
371 void SoFCVectorizeSVGActionP::printPoint(const SoVectorizePoint* item) const
372 {
373 // todo
374 Q_UNUSED(item);
375 }
376
377 void SoFCVectorizeSVGActionP::printImage(const SoVectorizeImage* item) const
378 {
379 // todo
380 Q_UNUSED(item);
381 }
382
383 // -------------------------------------------------------
384
385 SO_ACTION_SOURCE(SoFCVectorizeSVGAction)
386
387 void SoFCVectorizeSVGAction::initClass()
388 {
389 SO_ACTION_INIT_CLASS(SoFCVectorizeSVGAction, SoVectorizeAction);
390 }
391
392 SoFCVectorizeSVGAction::SoFCVectorizeSVGAction()
393 {
394 SO_ACTION_CONSTRUCTOR(SoFCVectorizeSVGAction);
395 this->setOutput(new SoSVGVectorOutput);
396 this->p = new SoFCVectorizeSVGActionP(this);
397 }
398
399 SoFCVectorizeSVGAction::~SoFCVectorizeSVGAction()
400 {
401 delete this->p;
402 }
403
404 SoSVGVectorOutput* SoFCVectorizeSVGAction::getSVGOutput() const
405 {
406 return static_cast<SoSVGVectorOutput*>(SoVectorizeAction::getOutput());
407 }
408
409 void SoFCVectorizeSVGAction::printHeader() const
410 {
411 std::ostream& str = this->getSVGOutput()->getFileStream();
412 str << R"(<?xml version="1.0" encoding="UTF-8" standalone="no"?>)" << std::endl;
413 str << "<!-- Created with FreeCAD (https://www.freecad.org) -->" << std::endl;
414 str << "<svg xmlns=\"http://www.w3.org/2000/svg\"" << std::endl;
415 str << R"( xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events")"
416 << std::endl;
417 str << R"( version="1.1" baseProfile="full")" << std::endl;
418
419 SbVec2f size = getPageSize();
420 if (this->getOrientation() == LANDSCAPE) {
421 SbSwap<float>(size[0], size[1]);
422 }
423 if (getUseMM()) {
424 str << " width=\"" << size[0] << "mm\" height=\"" << size[1] << "mm\"" << std::endl;
425 str << " viewBox=\"0 0 " << size[0] << " " << size[1] << "\">" << std::endl;
426 }
427 else { // original code used px
428 str << " width=\"" << size[0] << "\" height=\"" << size[1] << "\">" << std::endl;
429 }
430 str << "<g>" << std::endl;
431 }
432
433 void SoFCVectorizeSVGAction::printFooter() const
434 {
435 std::ostream& str = this->getSVGOutput()->getFileStream();
436 str << "</g>" << std::endl;
437 str << "</svg>";
438 }
439
440 void SoFCVectorizeSVGAction::printViewport() const
441 {}
442
443 void SoFCVectorizeSVGAction::printBackground() const
444 {
445 if (!getBackgroundState()) {
446 return;
447 }
448 SbVec2f mul = getRotatedViewportSize();
449 SbVec2f add = getRotatedViewportStartpos();
450
451 float x[2], y[2];
452 x[0] = add[0];
453 x[1] = mul[0] - add[0];
454 y[0] = add[1];
455 y[1] = mul[1] - add[1];
456
457 SbColor bg;
458 (void)this->getBackgroundColor(bg);
459 uint32_t cc = bg.getPackedValue();
460
461 std::ostream& str = this->getSVGOutput()->getFileStream();
462 str << "</g>" << std::endl;
463 str << "<path" << std::endl;
464 str << " d=\"M " << x[0] << "," << y[0] << " L " << x[1] << "," << y[0] << " L " << x[1]
465 << "," << y[1] << " L " << x[0] << "," << y[1] << " L " << x[0] << "," << y[0] << " z \""
466 << std::endl;
467 str << " style=\"fill:#" << std::hex << std::setw(6) << std::setfill('0') << (cc >> 8)
468 << ";fill-opacity:1;fill-rule:evenodd;stroke:none;"
469 "stroke-width:"
470 << getLineWidth()
471 << ";stroke-linecap:butt;stroke-linejoin:"
472 "miter;stroke-opacity:1\" />\n";
473 str << "<g>" << std::endl;
474 }
475
476 void SoFCVectorizeSVGAction::printItem(const SoVectorizeItem* item) const
477 {
478 switch (item->type) {
479 case SoVectorizeItem::TRIANGLE:
480 this->p->printTriangle(static_cast<const SoVectorizeTriangle*>(item));
481 break;
482 case SoVectorizeItem::LINE:
483 this->p->printLine(static_cast<const SoVectorizeLine*>(item));
484 break;
485 case SoVectorizeItem::POINT:
486 this->p->printPoint(static_cast<const SoVectorizePoint*>(item));
487 break;
488 case SoVectorizeItem::TEXT:
489 this->p->printText(static_cast<const SoVectorizeText*>(item));
490 break;
491 case SoVectorizeItem::IMAGE:
492 this->p->printImage(static_cast<const SoVectorizeImage*>(item));
493 break;
494 default:
495 assert(0 && "unsupported item");
496 break;
497 }
498 }