1 /**
2 * OpenDocument <drawing> input and output
3 *
4 * This is an an entry in the extensions mechanism to begin to enable
5 * the inputting and outputting of OpenDocument Format (ODF) files from
6 * within Inkscape. Although the initial implementations will be very lossy
7 * do to the differences in the models of SVG and ODF, they will hopefully
8 * improve greatly with time. People should consider this to be a framework
9 * that can be continously upgraded for ever improving fidelity. Potential
10 * developers should especially look in preprocess() and writeTree() to see how
11 * the SVG tree is scanned, read, translated, and then written to ODF.
12 *
13 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
14 *
15 * Authors:
16 * Bob Jamison
17 *
18 * Copyright (C) 2006, 2007 Bob Jamison
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 */
37 #ifdef HAVE_CONFIG_H
38 # include <config.h>
39 #endif
41 #include "odf.h"
43 //# System includes
44 #include <stdio.h>
45 #include <time.h>
46 #include <vector>
49 //# Inkscape includes
50 #include "clear-n_.h"
51 #include "inkscape.h"
52 #include <style.h>
53 #include "display/curve.h"
54 #include <2geom/pathvector.h>
55 #include <2geom/bezier-curve.h>
56 #include <2geom/hvlinesegment.h>
57 #include <2geom/transforms.h>
58 #include <helper/geom.h>
59 #include "helper/geom-curves.h"
60 #include "extension/system.h"
62 #include "xml/repr.h"
63 #include "xml/attribute-record.h"
64 #include "sp-image.h"
65 #include "sp-gradient.h"
66 #include "sp-stop.h"
67 #include "gradient-chemistry.h"
68 #include "sp-linear-gradient.h"
69 #include "sp-radial-gradient.h"
70 #include "sp-path.h"
71 #include "sp-text.h"
72 #include "sp-flowtext.h"
73 #include "svg/svg.h"
74 #include "text-editing.h"
77 //# DOM-specific includes
78 #include "dom/dom.h"
79 #include "dom/util/ziptool.h"
80 #include "dom/io/domstream.h"
81 #include "dom/io/bufferstream.h"
82 #include "dom/io/stringstream.h"
89 namespace Inkscape
90 {
91 namespace Extension
92 {
93 namespace Internal
94 {
98 //# Shorthand notation
99 typedef org::w3c::dom::DOMString DOMString;
100 typedef org::w3c::dom::XMLCh XMLCh;
101 typedef org::w3c::dom::io::OutputStreamWriter OutputStreamWriter;
102 typedef org::w3c::dom::io::BufferOutputStream BufferOutputStream;
103 typedef org::w3c::dom::io::StringOutputStream StringOutputStream;
105 //########################################################################
106 //# C L A S S SingularValueDecomposition
107 //########################################################################
108 #include <math.h>
110 class SVDMatrix
111 {
112 public:
114 SVDMatrix()
115 {
116 init();
117 }
119 SVDMatrix(unsigned int rowSize, unsigned int colSize)
120 {
121 init();
122 rows = rowSize;
123 cols = colSize;
124 size = rows * cols;
125 d = new double[size];
126 for (unsigned int i=0 ; i<size ; i++)
127 d[i] = 0.0;
128 }
130 SVDMatrix(double *vals, unsigned int rowSize, unsigned int colSize)
131 {
132 init();
133 rows = rowSize;
134 cols = colSize;
135 size = rows * cols;
136 d = new double[size];
137 for (unsigned int i=0 ; i<size ; i++)
138 d[i] = vals[i];
139 }
142 SVDMatrix(const SVDMatrix &other)
143 {
144 init();
145 assign(other);
146 }
148 SVDMatrix &operator=(const SVDMatrix &other)
149 {
150 assign(other);
151 return *this;
152 }
154 virtual ~SVDMatrix()
155 {
156 delete[] d;
157 }
159 double& operator() (unsigned int row, unsigned int col)
160 {
161 if (row >= rows || col >= cols)
162 return badval;
163 return d[cols*row + col];
164 }
166 double operator() (unsigned int row, unsigned int col) const
167 {
168 if (row >= rows || col >= cols)
169 return badval;
170 return d[cols*row + col];
171 }
173 unsigned int getRows()
174 {
175 return rows;
176 }
178 unsigned int getCols()
179 {
180 return cols;
181 }
183 SVDMatrix multiply(const SVDMatrix &other)
184 {
185 if (cols != other.rows)
186 {
187 SVDMatrix dummy;
188 return dummy;
189 }
190 SVDMatrix result(rows, other.cols);
191 for (unsigned int i=0 ; i<rows ; i++)
192 {
193 for (unsigned int j=0 ; j<other.cols ; j++)
194 {
195 double sum = 0.0;
196 for (unsigned int k=0 ; k<cols ; k++)
197 {
198 //sum += a[i][k] * b[k][j];
199 sum += d[i*cols +k] * other(k, j);
200 }
201 result(i, j) = sum;
202 }
204 }
205 return result;
206 }
208 SVDMatrix transpose()
209 {
210 SVDMatrix result(cols, rows);
211 for (unsigned int i=0 ; i<rows ; i++)
212 for (unsigned int j=0 ; j<cols ; j++)
213 result(j, i) = d[i*cols + j];
214 return result;
215 }
217 private:
220 virtual void init()
221 {
222 badval = 0.0;
223 d = NULL;
224 rows = 0;
225 cols = 0;
226 size = 0;
227 }
229 void assign(const SVDMatrix &other)
230 {
231 if (d)
232 {
233 delete[] d;
234 d = 0;
235 }
236 rows = other.rows;
237 cols = other.cols;
238 size = other.size;
239 d = new double[size];
240 for (unsigned int i=0 ; i<size ; i++)
241 d[i] = other.d[i];
242 }
244 double badval;
246 double *d;
247 unsigned int rows;
248 unsigned int cols;
249 unsigned int size;
250 };
254 /**
255 *
256 * ====================================================
257 *
258 * NOTE:
259 * This class is ported almost verbatim from the public domain
260 * JAMA Matrix package. It is modified to handle only 3x3 matrices
261 * and our NR::Matrix affine transform class. We give full
262 * attribution to them, along with many thanks. JAMA can be found at:
263 * http://math.nist.gov/javanumerics/jama
264 *
265 * ====================================================
266 *
267 * Singular Value Decomposition.
268 * <P>
269 * For an m-by-n matrix A with m >= n, the singular value decomposition is
270 * an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and
271 * an n-by-n orthogonal matrix V so that A = U*S*V'.
272 * <P>
273 * The singular values, sigma[k] = S[k][k], are ordered so that
274 * sigma[0] >= sigma[1] >= ... >= sigma[n-1].
275 * <P>
276 * The singular value decompostion always exists, so the constructor will
277 * never fail. The matrix condition number and the effective numerical
278 * rank can be computed from this decomposition.
279 */
280 class SingularValueDecomposition
281 {
282 public:
284 /** Construct the singular value decomposition
285 @param A Rectangular matrix
286 @return Structure to access U, S and V.
287 */
289 SingularValueDecomposition (const SVDMatrix &mat)
290 {
291 A = mat;
292 s = NULL;
293 s_size = 0;
294 calculate();
295 }
297 virtual ~SingularValueDecomposition()
298 {
299 delete[] s;
300 }
302 /**
303 * Return the left singular vectors
304 * @return U
305 */
306 SVDMatrix &getU();
308 /**
309 * Return the right singular vectors
310 * @return V
311 */
312 SVDMatrix &getV();
314 /**
315 * Return the s[index] value
316 */ double getS(unsigned int index);
318 /**
319 * Two norm
320 * @return max(S)
321 */
322 double norm2();
324 /**
325 * Two norm condition number
326 * @return max(S)/min(S)
327 */
328 double cond();
330 /**
331 * Effective numerical matrix rank
332 * @return Number of nonnegligible singular values.
333 */
334 int rank();
336 private:
338 void calculate();
340 SVDMatrix A;
341 SVDMatrix U;
342 double *s;
343 unsigned int s_size;
344 SVDMatrix V;
346 };
349 static double svd_hypot(double a, double b)
350 {
351 double r;
353 if (fabs(a) > fabs(b))
354 {
355 r = b/a;
356 r = fabs(a) * sqrt(1+r*r);
357 }
358 else if (b != 0)
359 {
360 r = a/b;
361 r = fabs(b) * sqrt(1+r*r);
362 }
363 else
364 {
365 r = 0.0;
366 }
367 return r;
368 }
372 void SingularValueDecomposition::calculate()
373 {
374 // Initialize.
375 int m = A.getRows();
376 int n = A.getCols();
378 int nu = (m > n) ? m : n;
379 s_size = (m+1 < n) ? m+1 : n;
380 s = new double[s_size];
381 U = SVDMatrix(m, nu);
382 V = SVDMatrix(n, n);
383 double *e = new double[n];
384 double *work = new double[m];
385 bool wantu = true;
386 bool wantv = true;
388 // Reduce A to bidiagonal form, storing the diagonal elements
389 // in s and the super-diagonal elements in e.
391 int nct = (m-1<n) ? m-1 : n;
392 int nrtx = (n-2<m) ? n-2 : m;
393 int nrt = (nrtx>0) ? nrtx : 0;
394 for (int k = 0; k < 2; k++) {
395 if (k < nct) {
397 // Compute the transformation for the k-th column and
398 // place the k-th diagonal in s[k].
399 // Compute 2-norm of k-th column without under/overflow.
400 s[k] = 0;
401 for (int i = k; i < m; i++) {
402 s[k] = svd_hypot(s[k],A(i, k));
403 }
404 if (s[k] != 0.0) {
405 if (A(k, k) < 0.0) {
406 s[k] = -s[k];
407 }
408 for (int i = k; i < m; i++) {
409 A(i, k) /= s[k];
410 }
411 A(k, k) += 1.0;
412 }
413 s[k] = -s[k];
414 }
415 for (int j = k+1; j < n; j++) {
416 if ((k < nct) & (s[k] != 0.0)) {
418 // Apply the transformation.
420 double t = 0;
421 for (int i = k; i < m; i++) {
422 t += A(i, k) * A(i, j);
423 }
424 t = -t/A(k, k);
425 for (int i = k; i < m; i++) {
426 A(i, j) += t*A(i, k);
427 }
428 }
430 // Place the k-th row of A into e for the
431 // subsequent calculation of the row transformation.
433 e[j] = A(k, j);
434 }
435 if (wantu & (k < nct)) {
437 // Place the transformation in U for subsequent back
438 // multiplication.
440 for (int i = k; i < m; i++) {
441 U(i, k) = A(i, k);
442 }
443 }
444 if (k < nrt) {
446 // Compute the k-th row transformation and place the
447 // k-th super-diagonal in e[k].
448 // Compute 2-norm without under/overflow.
449 e[k] = 0;
450 for (int i = k+1; i < n; i++) {
451 e[k] = svd_hypot(e[k],e[i]);
452 }
453 if (e[k] != 0.0) {
454 if (e[k+1] < 0.0) {
455 e[k] = -e[k];
456 }
457 for (int i = k+1; i < n; i++) {
458 e[i] /= e[k];
459 }
460 e[k+1] += 1.0;
461 }
462 e[k] = -e[k];
463 if ((k+1 < m) & (e[k] != 0.0)) {
465 // Apply the transformation.
467 for (int i = k+1; i < m; i++) {
468 work[i] = 0.0;
469 }
470 for (int j = k+1; j < n; j++) {
471 for (int i = k+1; i < m; i++) {
472 work[i] += e[j]*A(i, j);
473 }
474 }
475 for (int j = k+1; j < n; j++) {
476 double t = -e[j]/e[k+1];
477 for (int i = k+1; i < m; i++) {
478 A(i, j) += t*work[i];
479 }
480 }
481 }
482 if (wantv) {
484 // Place the transformation in V for subsequent
485 // back multiplication.
487 for (int i = k+1; i < n; i++) {
488 V(i, k) = e[i];
489 }
490 }
491 }
492 }
494 // Set up the final bidiagonal matrix or order p.
496 int p = (n < m+1) ? n : m+1;
497 if (nct < n) {
498 s[nct] = A(nct, nct);
499 }
500 if (m < p) {
501 s[p-1] = 0.0;
502 }
503 if (nrt+1 < p) {
504 e[nrt] = A(nrt, p-1);
505 }
506 e[p-1] = 0.0;
508 // If required, generate U.
510 if (wantu) {
511 for (int j = nct; j < nu; j++) {
512 for (int i = 0; i < m; i++) {
513 U(i, j) = 0.0;
514 }
515 U(j, j) = 1.0;
516 }
517 for (int k = nct-1; k >= 0; k--) {
518 if (s[k] != 0.0) {
519 for (int j = k+1; j < nu; j++) {
520 double t = 0;
521 for (int i = k; i < m; i++) {
522 t += U(i, k)*U(i, j);
523 }
524 t = -t/U(k, k);
525 for (int i = k; i < m; i++) {
526 U(i, j) += t*U(i, k);
527 }
528 }
529 for (int i = k; i < m; i++ ) {
530 U(i, k) = -U(i, k);
531 }
532 U(k, k) = 1.0 + U(k, k);
533 for (int i = 0; i < k-1; i++) {
534 U(i, k) = 0.0;
535 }
536 } else {
537 for (int i = 0; i < m; i++) {
538 U(i, k) = 0.0;
539 }
540 U(k, k) = 1.0;
541 }
542 }
543 }
545 // If required, generate V.
547 if (wantv) {
548 for (int k = n-1; k >= 0; k--) {
549 if ((k < nrt) & (e[k] != 0.0)) {
550 for (int j = k+1; j < nu; j++) {
551 double t = 0;
552 for (int i = k+1; i < n; i++) {
553 t += V(i, k)*V(i, j);
554 }
555 t = -t/V(k+1, k);
556 for (int i = k+1; i < n; i++) {
557 V(i, j) += t*V(i, k);
558 }
559 }
560 }
561 for (int i = 0; i < n; i++) {
562 V(i, k) = 0.0;
563 }
564 V(k, k) = 1.0;
565 }
566 }
568 // Main iteration loop for the singular values.
570 int pp = p-1;
571 int iter = 0;
572 //double eps = pow(2.0,-52.0);
573 //double tiny = pow(2.0,-966.0);
574 //let's just calculate these now
575 //a double can be e ± 308.25, so this is safe
576 double eps = 2.22e-16;
577 double tiny = 1.6e-291;
578 while (p > 0) {
579 int k,kase;
581 // Here is where a test for too many iterations would go.
583 // This section of the program inspects for
584 // negligible elements in the s and e arrays. On
585 // completion the variables kase and k are set as follows.
587 // kase = 1 if s(p) and e[k-1] are negligible and k<p
588 // kase = 2 if s(k) is negligible and k<p
589 // kase = 3 if e[k-1] is negligible, k<p, and
590 // s(k), ..., s(p) are not negligible (qr step).
591 // kase = 4 if e(p-1) is negligible (convergence).
593 for (k = p-2; k >= -1; k--) {
594 if (k == -1) {
595 break;
596 }
597 if (fabs(e[k]) <=
598 tiny + eps*(fabs(s[k]) + fabs(s[k+1]))) {
599 e[k] = 0.0;
600 break;
601 }
602 }
603 if (k == p-2) {
604 kase = 4;
605 } else {
606 int ks;
607 for (ks = p-1; ks >= k; ks--) {
608 if (ks == k) {
609 break;
610 }
611 double t = (ks != p ? fabs(e[ks]) : 0.) +
612 (ks != k+1 ? fabs(e[ks-1]) : 0.);
613 if (fabs(s[ks]) <= tiny + eps*t) {
614 s[ks] = 0.0;
615 break;
616 }
617 }
618 if (ks == k) {
619 kase = 3;
620 } else if (ks == p-1) {
621 kase = 1;
622 } else {
623 kase = 2;
624 k = ks;
625 }
626 }
627 k++;
629 // Perform the task indicated by kase.
631 switch (kase) {
633 // Deflate negligible s(p).
635 case 1: {
636 double f = e[p-2];
637 e[p-2] = 0.0;
638 for (int j = p-2; j >= k; j--) {
639 double t = svd_hypot(s[j],f);
640 double cs = s[j]/t;
641 double sn = f/t;
642 s[j] = t;
643 if (j != k) {
644 f = -sn*e[j-1];
645 e[j-1] = cs*e[j-1];
646 }
647 if (wantv) {
648 for (int i = 0; i < n; i++) {
649 t = cs*V(i, j) + sn*V(i, p-1);
650 V(i, p-1) = -sn*V(i, j) + cs*V(i, p-1);
651 V(i, j) = t;
652 }
653 }
654 }
655 }
656 break;
658 // Split at negligible s(k).
660 case 2: {
661 double f = e[k-1];
662 e[k-1] = 0.0;
663 for (int j = k; j < p; j++) {
664 double t = svd_hypot(s[j],f);
665 double cs = s[j]/t;
666 double sn = f/t;
667 s[j] = t;
668 f = -sn*e[j];
669 e[j] = cs*e[j];
670 if (wantu) {
671 for (int i = 0; i < m; i++) {
672 t = cs*U(i, j) + sn*U(i, k-1);
673 U(i, k-1) = -sn*U(i, j) + cs*U(i, k-1);
674 U(i, j) = t;
675 }
676 }
677 }
678 }
679 break;
681 // Perform one qr step.
683 case 3: {
685 // Calculate the shift.
687 double scale = fabs(s[p-1]);
688 double d = fabs(s[p-2]);
689 if (d>scale) scale=d;
690 d = fabs(e[p-2]);
691 if (d>scale) scale=d;
692 d = fabs(s[k]);
693 if (d>scale) scale=d;
694 d = fabs(e[k]);
695 if (d>scale) scale=d;
696 double sp = s[p-1]/scale;
697 double spm1 = s[p-2]/scale;
698 double epm1 = e[p-2]/scale;
699 double sk = s[k]/scale;
700 double ek = e[k]/scale;
701 double b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/2.0;
702 double c = (sp*epm1)*(sp*epm1);
703 double shift = 0.0;
704 if ((b != 0.0) | (c != 0.0)) {
705 shift = sqrt(b*b + c);
706 if (b < 0.0) {
707 shift = -shift;
708 }
709 shift = c/(b + shift);
710 }
711 double f = (sk + sp)*(sk - sp) + shift;
712 double g = sk*ek;
714 // Chase zeros.
716 for (int j = k; j < p-1; j++) {
717 double t = svd_hypot(f,g);
718 double cs = f/t;
719 double sn = g/t;
720 if (j != k) {
721 e[j-1] = t;
722 }
723 f = cs*s[j] + sn*e[j];
724 e[j] = cs*e[j] - sn*s[j];
725 g = sn*s[j+1];
726 s[j+1] = cs*s[j+1];
727 if (wantv) {
728 for (int i = 0; i < n; i++) {
729 t = cs*V(i, j) + sn*V(i, j+1);
730 V(i, j+1) = -sn*V(i, j) + cs*V(i, j+1);
731 V(i, j) = t;
732 }
733 }
734 t = svd_hypot(f,g);
735 cs = f/t;
736 sn = g/t;
737 s[j] = t;
738 f = cs*e[j] + sn*s[j+1];
739 s[j+1] = -sn*e[j] + cs*s[j+1];
740 g = sn*e[j+1];
741 e[j+1] = cs*e[j+1];
742 if (wantu && (j < m-1)) {
743 for (int i = 0; i < m; i++) {
744 t = cs*U(i, j) + sn*U(i, j+1);
745 U(i, j+1) = -sn*U(i, j) + cs*U(i, j+1);
746 U(i, j) = t;
747 }
748 }
749 }
750 e[p-2] = f;
751 iter = iter + 1;
752 }
753 break;
755 // Convergence.
757 case 4: {
759 // Make the singular values positive.
761 if (s[k] <= 0.0) {
762 s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
763 if (wantv) {
764 for (int i = 0; i <= pp; i++) {
765 V(i, k) = -V(i, k);
766 }
767 }
768 }
770 // Order the singular values.
772 while (k < pp) {
773 if (s[k] >= s[k+1]) {
774 break;
775 }
776 double t = s[k];
777 s[k] = s[k+1];
778 s[k+1] = t;
779 if (wantv && (k < n-1)) {
780 for (int i = 0; i < n; i++) {
781 t = V(i, k+1); V(i, k+1) = V(i, k); V(i, k) = t;
782 }
783 }
784 if (wantu && (k < m-1)) {
785 for (int i = 0; i < m; i++) {
786 t = U(i, k+1); U(i, k+1) = U(i, k); U(i, k) = t;
787 }
788 }
789 k++;
790 }
791 iter = 0;
792 p--;
793 }
794 break;
795 }
796 }
798 delete e;
799 delete work;
801 }
805 /**
806 * Return the left singular vectors
807 * @return U
808 */
809 SVDMatrix &SingularValueDecomposition::getU()
810 {
811 return U;
812 }
814 /**
815 * Return the right singular vectors
816 * @return V
817 */
819 SVDMatrix &SingularValueDecomposition::getV()
820 {
821 return V;
822 }
824 /**
825 * Return the s[0] value
826 */
827 double SingularValueDecomposition::getS(unsigned int index)
828 {
829 if (index >= s_size)
830 return 0.0;
831 return s[index];
832 }
834 /**
835 * Two norm
836 * @return max(S)
837 */
838 double SingularValueDecomposition::norm2()
839 {
840 return s[0];
841 }
843 /**
844 * Two norm condition number
845 * @return max(S)/min(S)
846 */
848 double SingularValueDecomposition::cond()
849 {
850 return s[0]/s[2];
851 }
853 /**
854 * Effective numerical matrix rank
855 * @return Number of nonnegligible singular values.
856 */
857 int SingularValueDecomposition::rank()
858 {
859 double eps = pow(2.0,-52.0);
860 double tol = 3.0*s[0]*eps;
861 int r = 0;
862 for (int i = 0; i < 3; i++)
863 {
864 if (s[i] > tol)
865 r++;
866 }
867 return r;
868 }
870 //########################################################################
871 //# E N D C L A S S SingularValueDecomposition
872 //########################################################################
878 #define pi 3.14159
879 //#define pxToCm 0.0275
880 #define pxToCm 0.03
881 #define piToRad 0.0174532925
882 #define docHeightCm 22.86
885 //########################################################################
886 //# O U T P U T
887 //########################################################################
889 /**
890 * Get the value of a node/attribute pair
891 */
892 static Glib::ustring getAttribute( Inkscape::XML::Node *node, char const *attrName)
893 {
894 Glib::ustring val;
895 char const *valstr = node->attribute(attrName);
896 if (valstr)
897 val = valstr;
898 return val;
899 }
903 /**
904 * Get the extension suffix from the end of a file name
905 */
906 static Glib::ustring getExtension(const Glib::ustring &fname)
907 {
908 Glib::ustring ext;
910 std::string::size_type pos = fname.rfind('.');
911 if (pos == fname.npos)
912 {
913 ext = "";
914 }
915 else
916 {
917 ext = fname.substr(pos);
918 }
919 return ext;
920 }
923 static Glib::ustring formatTransform(NR::Matrix &tf)
924 {
925 Glib::ustring str;
926 if (!tf.test_identity())
927 {
928 StringOutputStream outs;
929 OutputStreamWriter out(outs);
930 out.printf("matrix(%.3f %.3f %.3f %.3f %.3f %.3f)",
931 tf[0], tf[1], tf[2], tf[3], tf[4], tf[5]);
932 str = outs.getString();
933 }
934 return str;
935 }
941 /**
942 * Get the general transform from SVG pixels to
943 * ODF cm
944 */
945 static NR::Matrix getODFTransform(const SPItem *item)
946 {
947 //### Get SVG-to-ODF transform
948 NR::Matrix tf (sp_item_i2d_affine(item));
949 //Flip Y into document coordinates
950 double doc_height = sp_document_height(SP_ACTIVE_DOCUMENT);
951 NR::Matrix doc2dt_tf = NR::Matrix(NR::scale(1.0, -1.0));
952 doc2dt_tf = doc2dt_tf * NR::Matrix(NR::translate(0, doc_height));
953 tf = tf * doc2dt_tf;
954 tf = tf * NR::Matrix(NR::scale(pxToCm));
955 return tf;
956 }
961 /**
962 * Get the bounding box of an item, as mapped onto
963 * an ODF document, in cm.
964 */
965 static boost::optional<NR::Rect> getODFBoundingBox(const SPItem *item)
966 {
967 boost::optional<NR::Rect> bbox = sp_item_bbox_desktop((SPItem *)item);
968 if (bbox) {
969 double doc_height = sp_document_height(SP_ACTIVE_DOCUMENT);
970 NR::Matrix doc2dt_tf = NR::Matrix(NR::scale(1.0, -1.0));
971 doc2dt_tf = doc2dt_tf * NR::Matrix(NR::translate(0, doc_height));
972 bbox = *bbox * doc2dt_tf;
973 bbox = *bbox * NR::Matrix(NR::scale(pxToCm));
974 }
975 return bbox;
976 }
980 /**
981 * Get the transform for an item, correcting for
982 * handedness reversal
983 */
984 static NR::Matrix getODFItemTransform(const SPItem *item)
985 {
986 NR::Matrix itemTransform = NR::Matrix(NR::scale(1, -1));
987 itemTransform = itemTransform * item->transform;
988 itemTransform = itemTransform * NR::Matrix(NR::scale(1, -1));
989 return itemTransform;
990 }
994 /**
995 * Get some fun facts from the transform
996 */
997 static void analyzeTransform(NR::Matrix &tf,
998 double &rotate, double &/*xskew*/, double &/*yskew*/,
999 double &xscale, double &yscale)
1000 {
1001 SVDMatrix mat(2, 2);
1002 mat(0, 0) = tf[0];
1003 mat(0, 1) = tf[1];
1004 mat(1, 0) = tf[2];
1005 mat(1, 1) = tf[3];
1007 SingularValueDecomposition svd(mat);
1009 SVDMatrix U = svd.getU();
1010 SVDMatrix V = svd.getV();
1011 SVDMatrix Vt = V.transpose();
1012 SVDMatrix UVt = U.multiply(Vt);
1013 double s0 = svd.getS(0);
1014 double s1 = svd.getS(1);
1015 xscale = s0;
1016 yscale = s1;
1017 //g_message("## s0:%.3f s1:%.3f", s0, s1);
1018 //g_message("## u:%.3f %.3f %.3f %.3f", U(0,0), U(0,1), U(1,0), U(1,1));
1019 //g_message("## v:%.3f %.3f %.3f %.3f", V(0,0), V(0,1), V(1,0), V(1,1));
1020 //g_message("## vt:%.3f %.3f %.3f %.3f", Vt(0,0), Vt(0,1), Vt(1,0), Vt(1,1));
1021 //g_message("## uvt:%.3f %.3f %.3f %.3f", UVt(0,0), UVt(0,1), UVt(1,0), UVt(1,1));
1022 rotate = UVt(0,0);
1023 }
1027 static void gatherText(Inkscape::XML::Node *node, Glib::ustring &buf)
1028 {
1029 if (node->type() == Inkscape::XML::TEXT_NODE)
1030 {
1031 char *s = (char *)node->content();
1032 if (s)
1033 buf.append(s);
1034 }
1036 for (Inkscape::XML::Node *child = node->firstChild() ;
1037 child != NULL; child = child->next())
1038 {
1039 gatherText(child, buf);
1040 }
1042 }
1044 /**
1045 * FIRST PASS.
1046 * Method descends into the repr tree, converting image, style, and gradient info
1047 * into forms compatible in ODF.
1048 */
1049 void
1050 OdfOutput::preprocess(ZipFile &zf, Inkscape::XML::Node *node)
1051 {
1053 Glib::ustring nodeName = node->name();
1054 Glib::ustring id = getAttribute(node, "id");
1056 //### First, check for metadata
1057 if (nodeName == "metadata" || nodeName == "svg:metadata")
1058 {
1059 Inkscape::XML::Node *mchild = node->firstChild() ;
1060 if (!mchild || strcmp(mchild->name(), "rdf:RDF"))
1061 return;
1062 Inkscape::XML::Node *rchild = mchild->firstChild() ;
1063 if (!rchild || strcmp(rchild->name(), "cc:Work"))
1064 return;
1065 for (Inkscape::XML::Node *cchild = rchild->firstChild() ;
1066 cchild ; cchild = cchild->next())
1067 {
1068 Glib::ustring ccName = cchild->name();
1069 Glib::ustring ccVal;
1070 gatherText(cchild, ccVal);
1071 //g_message("ccName: %s ccVal:%s", ccName.c_str(), ccVal.c_str());
1072 metadata[ccName] = ccVal;
1073 }
1074 return;
1075 }
1077 //Now consider items.
1078 SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(node);
1079 if (!reprobj)
1080 return;
1081 if (!SP_IS_ITEM(reprobj))
1082 {
1083 return;
1084 }
1085 SPItem *item = SP_ITEM(reprobj);
1086 //### Get SVG-to-ODF transform
1087 NR::Matrix tf = getODFTransform(item);
1089 if (nodeName == "image" || nodeName == "svg:image")
1090 {
1091 //g_message("image");
1092 Glib::ustring href = getAttribute(node, "xlink:href");
1093 if (href.size() > 0)
1094 {
1095 Glib::ustring oldName = href;
1096 Glib::ustring ext = getExtension(oldName);
1097 if (ext == ".jpeg")
1098 ext = ".jpg";
1099 if (imageTable.find(oldName) == imageTable.end())
1100 {
1101 char buf[64];
1102 snprintf(buf, sizeof(buf), "Pictures/image%u%s",
1103 static_cast<unsigned int>(imageTable.size()), ext.c_str());
1104 Glib::ustring newName = buf;
1105 imageTable[oldName] = newName;
1106 Glib::ustring comment = "old name was: ";
1107 comment.append(oldName);
1108 URI oldUri(oldName);
1109 //g_message("oldpath:%s", oldUri.getNativePath().c_str());
1110 //# if relative to the documentURI, get proper path
1111 URI resUri = documentUri.resolve(oldUri);
1112 DOMString pathName = resUri.getNativePath();
1113 //g_message("native path:%s", pathName.c_str());
1114 ZipEntry *ze = zf.addFile(pathName, comment);
1115 if (ze)
1116 {
1117 ze->setFileName(newName);
1118 }
1119 else
1120 {
1121 g_warning("Could not load image file '%s'", pathName.c_str());
1122 }
1123 }
1124 }
1125 }
1127 for (Inkscape::XML::Node *child = node->firstChild() ;
1128 child ; child = child->next())
1129 preprocess(zf, child);
1130 }
1134 /**
1135 * Writes the manifest. Currently it only changes according to the
1136 * file names of images packed into the zip file.
1137 */
1138 bool OdfOutput::writeManifest(ZipFile &zf)
1139 {
1140 BufferOutputStream bouts;
1141 OutputStreamWriter outs(bouts);
1143 time_t tim;
1144 time(&tim);
1146 outs.printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1147 outs.printf("<!DOCTYPE manifest:manifest PUBLIC \"-//OpenOffice.org//DTD Manifest 1.0//EN\" \"Manifest.dtd\">\n");
1148 outs.printf("\n");
1149 outs.printf("\n");
1150 outs.printf("<!--\n");
1151 outs.printf("*************************************************************************\n");
1152 outs.printf(" file: manifest.xml\n");
1153 outs.printf(" Generated by Inkscape: %s", ctime(&tim)); //ctime has its own <cr>
1154 outs.printf(" http://www.inkscape.org\n");
1155 outs.printf("*************************************************************************\n");
1156 outs.printf("-->\n");
1157 outs.printf("\n");
1158 outs.printf("\n");
1159 outs.printf("<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n");
1160 outs.printf(" <manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.graphics\" manifest:full-path=\"/\"/>\n");
1161 outs.printf(" <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>\n");
1162 outs.printf(" <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>\n");
1163 outs.printf(" <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>\n");
1164 outs.printf(" <!--List our images here-->\n");
1165 std::map<Glib::ustring, Glib::ustring>::iterator iter;
1166 for (iter = imageTable.begin() ; iter!=imageTable.end() ; iter++)
1167 {
1168 Glib::ustring oldName = iter->first;
1169 Glib::ustring newName = iter->second;
1171 Glib::ustring ext = getExtension(oldName);
1172 if (ext == ".jpeg")
1173 ext = ".jpg";
1174 outs.printf(" <manifest:file-entry manifest:media-type=\"");
1175 if (ext == ".gif")
1176 outs.printf("image/gif");
1177 else if (ext == ".png")
1178 outs.printf("image/png");
1179 else if (ext == ".jpg")
1180 outs.printf("image/jpeg");
1181 outs.printf("\" manifest:full-path=\"");
1182 outs.printf(newName.c_str());
1183 outs.printf("\"/>\n");
1184 }
1185 outs.printf("</manifest:manifest>\n");
1187 outs.close();
1189 //Make our entry
1190 ZipEntry *ze = zf.newEntry("META-INF/manifest.xml", "ODF file manifest");
1191 ze->setUncompressedData(bouts.getBuffer());
1192 ze->finish();
1194 return true;
1195 }
1198 /**
1199 * This writes the document meta information to meta.xml
1200 */
1201 bool OdfOutput::writeMeta(ZipFile &zf)
1202 {
1203 BufferOutputStream bouts;
1204 OutputStreamWriter outs(bouts);
1206 time_t tim;
1207 time(&tim);
1209 std::map<Glib::ustring, Glib::ustring>::iterator iter;
1210 Glib::ustring creator = "unknown";
1211 iter = metadata.find("dc:creator");
1212 if (iter != metadata.end())
1213 creator = iter->second;
1214 Glib::ustring date = "";
1215 iter = metadata.find("dc:date");
1216 if (iter != metadata.end())
1217 date = iter->second;
1219 outs.printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1220 outs.printf("\n");
1221 outs.printf("\n");
1222 outs.printf("<!--\n");
1223 outs.printf("*************************************************************************\n");
1224 outs.printf(" file: meta.xml\n");
1225 outs.printf(" Generated by Inkscape: %s", ctime(&tim)); //ctime has its own <cr>
1226 outs.printf(" http://www.inkscape.org\n");
1227 outs.printf("*************************************************************************\n");
1228 outs.printf("-->\n");
1229 outs.printf("\n");
1230 outs.printf("\n");
1231 outs.printf("<office:document-meta\n");
1232 outs.printf("xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
1233 outs.printf("xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
1234 outs.printf("xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
1235 outs.printf("xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
1236 outs.printf("xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
1237 outs.printf("xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
1238 outs.printf("xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
1239 outs.printf("xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
1240 outs.printf("office:version=\"1.0\">\n");
1241 outs.printf("<office:meta>\n");
1242 outs.printf(" <meta:generator>Inkscape.org - 0.45</meta:generator>\n");
1243 outs.printf(" <meta:initial-creator>%#s</meta:initial-creator>\n",
1244 creator.c_str());
1245 outs.printf(" <meta:creation-date>%#s</meta:creation-date>\n", date.c_str());
1246 for (iter = metadata.begin() ; iter != metadata.end() ; iter++)
1247 {
1248 Glib::ustring name = iter->first;
1249 Glib::ustring value = iter->second;
1250 if (name.size() > 0 && value.size()>0)
1251 {
1252 outs.printf(" <%#s>%#s</%#s>\n",
1253 name.c_str(), value.c_str(), name.c_str());
1254 }
1255 }
1256 outs.printf(" <meta:editing-cycles>2</meta:editing-cycles>\n");
1257 outs.printf(" <meta:editing-duration>PT56S</meta:editing-duration>\n");
1258 outs.printf(" <meta:user-defined meta:name=\"Info 1\"/>\n");
1259 outs.printf(" <meta:user-defined meta:name=\"Info 2\"/>\n");
1260 outs.printf(" <meta:user-defined meta:name=\"Info 3\"/>\n");
1261 outs.printf(" <meta:user-defined meta:name=\"Info 4\"/>\n");
1262 outs.printf(" <meta:document-statistic meta:object-count=\"2\"/>\n");
1263 outs.printf("</office:meta>\n");
1264 outs.printf("</office:document-meta>\n");
1265 outs.printf("\n");
1266 outs.printf("\n");
1269 outs.close();
1271 //Make our entry
1272 ZipEntry *ze = zf.newEntry("meta.xml", "ODF info file");
1273 ze->setUncompressedData(bouts.getBuffer());
1274 ze->finish();
1276 return true;
1277 }
1282 /**
1283 * This is called just before writeTree(), since it will write style and
1284 * gradient information above the <draw> tag in the content.xml file
1285 */
1286 bool OdfOutput::writeStyle(ZipFile &zf)
1287 {
1288 BufferOutputStream bouts;
1289 OutputStreamWriter outs(bouts);
1291 /*
1292 ==========================================================
1293 Dump our style table. Styles should have a general layout
1294 something like the following. Look in:
1295 http://books.evc-cit.info/odbook/ch06.html#draw-style-file-section
1296 for style and gradient information.
1297 <style:style style:name="gr13"
1298 style:family="graphic" style:parent-style-name="standard">
1299 <style:graphic-properties draw:stroke="solid"
1300 svg:stroke-width="0.1cm"
1301 svg:stroke-color="#ff0000"
1302 draw:fill="solid" draw:fill-color="#e6e6ff"/>
1303 </style:style>
1304 ==========================================================
1305 */
1306 outs.printf("<!-- ####### Styles from Inkscape document ####### -->\n");
1307 std::vector<StyleInfo>::iterator iter;
1308 for (iter = styleTable.begin() ; iter != styleTable.end() ; iter++)
1309 {
1310 outs.printf("<style:style style:name=\"%s\"", iter->name.c_str());
1311 StyleInfo s(*iter);
1312 outs.printf(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
1313 outs.printf(" <style:graphic-properties");
1314 outs.printf(" draw:fill=\"%s\" ", s.fill.c_str());
1315 if (s.fill != "none")
1316 {
1317 outs.printf(" draw:fill-color=\"%s\" ", s.fillColor.c_str());
1318 outs.printf(" draw:fill-opacity=\"%s\" ", s.fillOpacity.c_str());
1319 }
1320 outs.printf(" draw:stroke=\"%s\" ", s.stroke.c_str());
1321 if (s.stroke != "none")
1322 {
1323 outs.printf(" svg:stroke-width=\"%s\" ", s.strokeWidth.c_str());
1324 outs.printf(" svg:stroke-color=\"%s\" ", s.strokeColor.c_str());
1325 outs.printf(" svg:stroke-opacity=\"%s\" ", s.strokeOpacity.c_str());
1326 }
1327 outs.printf("/>\n");
1328 outs.printf("</style:style>\n");
1329 }
1331 //## Dump our gradient table
1332 int gradientCount = 0;
1333 outs.printf("\n");
1334 outs.printf("<!-- ####### Gradients from Inkscape document ####### -->\n");
1335 std::vector<GradientInfo>::iterator giter;
1336 for (giter = gradientTable.begin() ; giter != gradientTable.end() ; giter++)
1337 {
1338 GradientInfo gi(*giter);
1339 if (gi.style == "linear")
1340 {
1341 /*
1342 ===================================================================
1343 LINEAR gradient. We need something that looks like this:
1344 <draw:gradient draw:name="Gradient_20_7"
1345 draw:display-name="Gradient 7"
1346 draw:style="linear"
1347 draw:start-color="#008080" draw:end-color="#993366"
1348 draw:start-intensity="100%" draw:end-intensity="100%"
1349 draw:angle="150" draw:border="0%"/>
1350 ===================================================================
1351 */
1352 if (gi.stops.size() < 2)
1353 {
1354 g_warning("Need at least 2 tops for a linear gradient");
1355 continue;
1356 }
1357 outs.printf("<svg:linearGradient ");
1358 outs.printf("id=\"%#s_g\" ", gi.name.c_str());
1359 outs.printf("draw:name=\"%#s_g\"\n", gi.name.c_str());
1360 outs.printf(" draw:display-name=\"imported linear %u\"\n",
1361 gradientCount);
1362 outs.printf(" svg:x1=\"%05.3fcm\" svg:y1=\"%05.3fcm\"\n",
1363 gi.x1, gi.y1);
1364 outs.printf(" svg:x2=\"%05.3fcm\" svg:y2=\"%05.3fcm\"\n",
1365 gi.x2, gi.y2);
1366 outs.printf(" svg:gradientUnits=\"objectBoundingBox\">\n");
1367 outs.printf(" <svg:stop\n");
1368 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1369 gi.stops[0].rgb);
1370 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1371 gi.stops[0].opacity * 100.0);
1372 outs.printf(" svg:offset=\"0\"/>\n");
1373 outs.printf(" <svg:stop\n");
1374 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1375 gi.stops[1].rgb);
1376 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1377 gi.stops[1].opacity * 100.0);
1378 outs.printf(" svg:offset=\"1\"/>\n");
1379 outs.printf("</svg:linearGradient>\n");
1380 }
1381 else if (gi.style == "radial")
1382 {
1383 /*
1384 ===================================================================
1385 RADIAL gradient. We need something that looks like this:
1386 <!-- radial gradient, light gray to white, centered, 0% border -->
1387 <draw:gradient draw:name="radial_20_borderless"
1388 draw:display-name="radial borderless"
1389 draw:style="radial"
1390 draw:cx="50%" draw:cy="50%"
1391 draw:start-color="#999999" draw:end-color="#ffffff"
1392 draw:border="0%"/>
1393 ===================================================================
1394 */
1395 if (gi.stops.size() < 2)
1396 {
1397 g_warning("Need at least 2 tops for a radial gradient");
1398 continue;
1399 }
1400 outs.printf("<svg:radialGradient ");
1401 outs.printf("id=\"%#s_g\" ", gi.name.c_str());
1402 outs.printf("draw:name=\"%#s_g\"\n", gi.name.c_str());
1403 outs.printf(" draw:display-name=\"imported radial %d\"\n",
1404 gradientCount);
1405 outs.printf(" svg:cx=\"%05.3f\" svg:cy=\"%05.3f\"\n",
1406 gi.cx, gi.cy);
1407 outs.printf(" svg:fx=\"%05.3f\" svg:fy=\"%05.3f\"\n",
1408 gi.fx, gi.fy);
1409 outs.printf(" svg:r=\"%05.3f\"\n",
1410 gi.r);
1411 outs.printf(" svg:gradientUnits=\"objectBoundingBox\">\n");
1412 outs.printf(" <svg:stop\n");
1413 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1414 gi.stops[0].rgb);
1415 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1416 gi.stops[0].opacity * 100.0);
1417 outs.printf(" svg:offset=\"0\"/>\n");
1418 outs.printf(" <svg:stop\n");
1419 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1420 gi.stops[1].rgb);
1421 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1422 gi.stops[1].opacity * 100.0);
1423 outs.printf(" svg:offset=\"1\"/>\n");
1424 outs.printf("</svg:radialGradient>\n");
1425 }
1426 else
1427 {
1428 g_warning("unsupported gradient style '%s'", gi.style.c_str());
1429 }
1430 outs.printf("<style:style style:name=\"%#s\" style:family=\"graphic\" ",
1431 gi.name.c_str());
1432 outs.printf("style:parent-style-name=\"standard\">\n");
1433 outs.printf(" <style:graphic-properties draw:fill=\"gradient\" ");
1434 outs.printf("draw:fill-gradient-name=\"%#s_g\"\n",
1435 gi.name.c_str());
1436 outs.printf(" draw:textarea-horizontal-align=\"center\" ");
1437 outs.printf("draw:textarea-vertical-align=\"middle\"/>\n");
1438 outs.printf("</style:style>\n\n");
1440 gradientCount++;
1441 }
1443 outs.printf("\n");
1444 outs.printf("</office:automatic-styles>\n");
1445 outs.printf("\n");
1446 outs.printf("\n");
1447 outs.printf("<office:master-styles>\n");
1448 outs.printf("<draw:layer-set>\n");
1449 outs.printf(" <draw:layer draw:name=\"layout\"/>\n");
1450 outs.printf(" <draw:layer draw:name=\"background\"/>\n");
1451 outs.printf(" <draw:layer draw:name=\"backgroundobjects\"/>\n");
1452 outs.printf(" <draw:layer draw:name=\"controls\"/>\n");
1453 outs.printf(" <draw:layer draw:name=\"measurelines\"/>\n");
1454 outs.printf("</draw:layer-set>\n");
1455 outs.printf("\n");
1456 outs.printf("<style:master-page style:name=\"Default\"\n");
1457 outs.printf(" style:page-master-name=\"PM1\" draw:style-name=\"dp1\"/>\n");
1458 outs.printf("</office:master-styles>\n");
1459 outs.printf("\n");
1460 outs.printf("\n");
1461 outs.printf("\n");
1462 outs.printf("</office:document-styles>\n");
1463 outs.printf("\n");
1464 outs.printf("<!--\n");
1465 outs.printf("*************************************************************************\n");
1466 outs.printf(" E N D O F F I L E\n");
1467 outs.printf(" Have a nice day - ishmal\n");
1468 outs.printf("*************************************************************************\n");
1469 outs.printf("-->\n");
1470 outs.printf("\n");
1472 //Make our entry
1473 ZipEntry *ze = zf.newEntry("styles.xml", "ODF style file");
1474 ze->setUncompressedData(bouts.getBuffer());
1475 ze->finish();
1477 return true;
1478 }
1482 /**
1483 * Writes an SVG path as an ODF <draw:path> and returns the number of points written
1484 */
1485 static int
1486 writePath(Writer &outs, Geom::PathVector const &pathv,
1487 Geom::Matrix const &tf, double xoff, double yoff)
1488 {
1489 using Geom::X;
1490 using Geom::Y;
1492 int nrPoints = 0;
1494 // convert the path to only lineto's and cubic curveto's:
1495 Geom::PathVector pv = pathv_to_linear_and_cubic_beziers(pathv * tf * Geom::Translate(xoff, yoff) * Geom::Scale(1000.));
1497 for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
1499 double destx = pit->initialPoint()[X];
1500 double desty = pit->initialPoint()[Y];
1501 if (fabs(destx)<1.0) destx = 0.0; // Why is this needed? Shouldn't we just round all numbers then?
1502 if (fabs(desty)<1.0) desty = 0.0;
1503 outs.printf("M %.3f %.3f ", destx, desty);
1504 nrPoints++;
1506 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit) {
1508 if( is_straight_curve(*cit) )
1509 {
1510 double destx = cit->finalPoint()[X];
1511 double desty = cit->finalPoint()[Y];
1512 if (fabs(destx)<1.0) destx = 0.0; // Why is this needed? Shouldn't we just round all numbers then?
1513 if (fabs(desty)<1.0) desty = 0.0;
1514 outs.printf("L %.3f %.3f ", destx, desty);
1515 }
1516 else if(Geom::CubicBezier const *cubic = dynamic_cast<Geom::CubicBezier const*>(&*cit)) {
1517 std::vector<Geom::Point> points = cubic->points();
1518 for (unsigned i = 1; i <= 3; i++) {
1519 if (fabs(points[i][X])<1.0) points[i][X] = 0.0; // Why is this needed? Shouldn't we just round all numbers then?
1520 if (fabs(points[i][Y])<1.0) points[i][Y] = 0.0;
1521 }
1522 outs.printf("C %.3f %.3f %.3f %.3f %.3f %.3f ", points[1][X],points[1][Y], points[2][X],points[2][Y], points[3][X],points[3][Y]);
1523 }
1524 else {
1525 g_error ("logical error, because pathv_to_linear_and_cubic_beziers was used");
1526 }
1528 nrPoints++;
1529 }
1531 if (pit->closed()) {
1532 outs.printf("Z");
1533 }
1534 }
1536 return nrPoints;
1537 }
1541 bool OdfOutput::processStyle(Writer &outs, SPItem *item,
1542 const Glib::ustring &id)
1543 {
1544 SPStyle *style = item->style;
1546 StyleInfo si;
1548 //## FILL
1549 if (style->fill.isColor())
1550 {
1551 guint32 fillCol = style->fill.value.color.toRGBA32( 0 );
1552 char buf[16];
1553 int r = (fillCol >> 24) & 0xff;
1554 int g = (fillCol >> 16) & 0xff;
1555 int b = (fillCol >> 8) & 0xff;
1556 //g_message("## %s %lx", id.c_str(), (unsigned int)fillCol);
1557 snprintf(buf, 15, "#%02x%02x%02x", r, g, b);
1558 si.fillColor = buf;
1559 si.fill = "solid";
1560 double opacityPercent = 100.0 *
1561 (SP_SCALE24_TO_FLOAT(style->fill_opacity.value));
1562 snprintf(buf, 15, "%.3f%%", opacityPercent);
1563 si.fillOpacity = buf;
1564 }
1566 //## STROKE
1567 if (style->stroke.isColor())
1568 {
1569 guint32 strokeCol = style->stroke.value.color.toRGBA32( 0 );
1570 char buf[16];
1571 int r = (strokeCol >> 24) & 0xff;
1572 int g = (strokeCol >> 16) & 0xff;
1573 int b = (strokeCol >> 8) & 0xff;
1574 snprintf(buf, 15, "#%02x%02x%02x", r, g, b);
1575 si.strokeColor = buf;
1576 snprintf(buf, 15, "%.3fpt", style->stroke_width.value);
1577 si.strokeWidth = buf;
1578 si.stroke = "solid";
1579 double opacityPercent = 100.0 *
1580 (SP_SCALE24_TO_FLOAT(style->stroke_opacity.value));
1581 snprintf(buf, 15, "%.3f%%", opacityPercent);
1582 si.strokeOpacity = buf;
1583 }
1585 //Look for existing identical style;
1586 bool styleMatch = false;
1587 std::vector<StyleInfo>::iterator iter;
1588 for (iter=styleTable.begin() ; iter!=styleTable.end() ; iter++)
1589 {
1590 if (si.equals(*iter))
1591 {
1592 //map to existing styleTable entry
1593 Glib::ustring styleName = iter->name;
1594 //g_message("found duplicate style:%s", styleName.c_str());
1595 styleLookupTable[id] = styleName;
1596 styleMatch = true;
1597 break;
1598 }
1599 }
1601 //## Dont need a new style
1602 if (styleMatch)
1603 return false;
1605 char buf[16];
1606 snprintf(buf, 15, "style%d", (int)styleTable.size());
1607 Glib::ustring styleName = buf;
1608 si.name = styleName;
1609 styleTable.push_back(si);
1610 styleLookupTable[id] = styleName;
1612 outs.printf("<style:style style:name=\"%s\"", si.name.c_str());
1613 outs.printf(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
1614 outs.printf(" <style:graphic-properties");
1615 outs.printf(" draw:fill=\"%s\" ", si.fill.c_str());
1616 if (si.fill != "none")
1617 {
1618 outs.printf(" draw:fill-color=\"%s\" ", si.fillColor.c_str());
1619 outs.printf(" draw:fill-opacity=\"%s\" ", si.fillOpacity.c_str());
1620 }
1621 outs.printf(" draw:stroke=\"%s\" ", si.stroke.c_str());
1622 if (si.stroke != "none")
1623 {
1624 outs.printf(" svg:stroke-width=\"%s\" ", si.strokeWidth.c_str());
1625 outs.printf(" svg:stroke-color=\"%s\" ", si.strokeColor.c_str());
1626 outs.printf(" svg:stroke-opacity=\"%s\" ", si.strokeOpacity.c_str());
1627 }
1628 outs.printf("/>\n");
1629 outs.printf("</style:style>\n");
1631 return true;
1632 }
1637 bool OdfOutput::processGradient(Writer &outs, SPItem *item,
1638 const Glib::ustring &id, NR::Matrix &/*tf*/)
1639 {
1640 if (!item)
1641 return false;
1643 SPStyle *style = item->style;
1645 if (!style)
1646 return false;
1648 if (!style->fill.isPaintserver())
1649 return false;
1651 //## Gradient. Look in writeStyle() below to see what info
1652 // we need to read into GradientInfo.
1653 if (!SP_IS_GRADIENT(SP_STYLE_FILL_SERVER(style)))
1654 return false;
1656 SPGradient *gradient = SP_GRADIENT(SP_STYLE_FILL_SERVER(style));
1658 GradientInfo gi;
1660 SPGradient *grvec = sp_gradient_get_vector(gradient, FALSE);
1661 for (SPStop *stop = sp_first_stop(grvec) ;
1662 stop ; stop = sp_next_stop(stop))
1663 {
1664 unsigned long rgba = sp_stop_get_rgba32(stop);
1665 unsigned long rgb = (rgba >> 8) & 0xffffff;
1666 double opacity = ((double)(rgba & 0xff)) / 256.0;
1667 GradientStop gs(rgb, opacity);
1668 gi.stops.push_back(gs);
1669 }
1671 if (SP_IS_LINEARGRADIENT(gradient))
1672 {
1673 gi.style = "linear";
1674 SPLinearGradient *linGrad = SP_LINEARGRADIENT(gradient);
1675 /*
1676 NR::Point p1(linGrad->x1.value, linGrad->y1.value);
1677 p1 = p1 * tf;
1678 gi.x1 = p1[NR::X];
1679 gi.y1 = p1[NR::Y];
1680 NR::Point p2(linGrad->x2.value, linGrad->y2.value);
1681 p2 = p2 * tf;
1682 gi.x2 = p2[NR::X];
1683 gi.y2 = p2[NR::Y];
1684 */
1685 gi.x1 = linGrad->x1.value;
1686 gi.y1 = linGrad->y1.value;
1687 gi.x2 = linGrad->x2.value;
1688 gi.y2 = linGrad->y2.value;
1689 }
1690 else if (SP_IS_RADIALGRADIENT(gradient))
1691 {
1692 gi.style = "radial";
1693 SPRadialGradient *radGrad = SP_RADIALGRADIENT(gradient);
1694 gi.cx = radGrad->cx.computed * 100.0;//ODG cx is percentages
1695 gi.cy = radGrad->cy.computed * 100.0;
1696 }
1697 else
1698 {
1699 g_warning("not a supported gradient type");
1700 return false;
1701 }
1703 //Look for existing identical style;
1704 bool gradientMatch = false;
1705 std::vector<GradientInfo>::iterator iter;
1706 for (iter=gradientTable.begin() ; iter!=gradientTable.end() ; iter++)
1707 {
1708 if (gi.equals(*iter))
1709 {
1710 //map to existing gradientTable entry
1711 Glib::ustring gradientName = iter->name;
1712 //g_message("found duplicate style:%s", gradientName.c_str());
1713 gradientLookupTable[id] = gradientName;
1714 gradientMatch = true;
1715 break;
1716 }
1717 }
1719 if (gradientMatch)
1720 return true;
1722 //## No match, let us write a new entry
1723 char buf[16];
1724 snprintf(buf, 15, "gradient%d", (int)gradientTable.size());
1725 Glib::ustring gradientName = buf;
1726 gi.name = gradientName;
1727 gradientTable.push_back(gi);
1728 gradientLookupTable[id] = gradientName;
1730 int gradientCount = gradientTable.size();
1732 if (gi.style == "linear")
1733 {
1734 /*
1735 ===================================================================
1736 LINEAR gradient. We need something that looks like this:
1737 <draw:gradient draw:name="Gradient_20_7"
1738 draw:display-name="Gradient 7"
1739 draw:style="linear"
1740 draw:start-color="#008080" draw:end-color="#993366"
1741 draw:start-intensity="100%" draw:end-intensity="100%"
1742 draw:angle="150" draw:border="0%"/>
1743 ===================================================================
1744 */
1745 if (gi.stops.size() < 2)
1746 {
1747 g_warning("Need at least 2 stops for a linear gradient");
1748 return false;;
1749 }
1750 outs.printf("<svg:linearGradient ");
1751 outs.printf("id=\"%#s_g\" ", gi.name.c_str());
1752 outs.printf("draw:name=\"%#s_g\"\n", gi.name.c_str());
1753 outs.printf(" draw:display-name=\"imported linear %d\"\n",
1754 gradientCount);
1755 outs.printf(" svg:gradientUnits=\"objectBoundingBox\"\n");
1756 outs.printf(" svg:x1=\"%05.3fcm\" svg:y1=\"%05.3fcm\"\n",
1757 gi.x1 * pxToCm, gi.y1 * pxToCm);
1758 outs.printf(" svg:x2=\"%05.3fcm\" svg:y2=\"%05.3fcm\">\n",
1759 gi.x2 * pxToCm, gi.y2 * pxToCm);
1760 outs.printf(" <svg:stop\n");
1761 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1762 gi.stops[0].rgb);
1763 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1764 gi.stops[0].opacity * 100.0);
1765 outs.printf(" svg:offset=\"0\"/>\n");
1766 outs.printf(" <svg:stop\n");
1767 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1768 gi.stops[1].rgb);
1769 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1770 gi.stops[1].opacity * 100.0);
1771 outs.printf(" svg:offset=\"1\"/>\n");
1772 outs.printf("</svg:linearGradient>\n");
1773 }
1774 else if (gi.style == "radial")
1775 {
1776 /*
1777 ===================================================================
1778 RADIAL gradient. We need something that looks like this:
1779 <!-- radial gradient, light gray to white, centered, 0% border -->
1780 <draw:gradient draw:name="radial_20_borderless"
1781 draw:display-name="radial borderless"
1782 draw:style="radial"
1783 draw:cx="50%" draw:cy="50%"
1784 draw:start-color="#999999" draw:end-color="#ffffff"
1785 draw:border="0%"/>
1786 ===================================================================
1787 */
1788 if (gi.stops.size() < 2)
1789 {
1790 g_warning("Need at least 2 stops for a radial gradient");
1791 return false;
1792 }
1793 outs.printf("<svg:radialGradient ");
1794 outs.printf("id=\"%#s_g\" ", gi.name.c_str());
1795 outs.printf("draw:name=\"%#s_g\"\n", gi.name.c_str());
1796 outs.printf(" draw:display-name=\"imported radial %d\"\n",
1797 gradientCount);
1798 outs.printf(" svg:gradientUnits=\"objectBoundingBox\"\n");
1799 outs.printf(" svg:cx=\"%05.3f\" svg:cy=\"%05.3f\"\n",
1800 gi.cx, gi.cy);
1801 outs.printf(" svg:fx=\"%05.3f\" svg:fy=\"%05.3f\"\n",
1802 gi.fx, gi.fy);
1803 outs.printf(" svg:r=\"%05.3f\">\n",
1804 gi.r);
1805 outs.printf(" <svg:stop\n");
1806 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1807 gi.stops[0].rgb);
1808 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1809 gi.stops[0].opacity * 100.0);
1810 outs.printf(" svg:offset=\"0\"/>\n");
1811 outs.printf(" <svg:stop\n");
1812 outs.printf(" svg:stop-color=\"#%06lx\"\n",
1813 gi.stops[1].rgb);
1814 outs.printf(" svg:stop-opacity=\"%f%%\"\n",
1815 gi.stops[1].opacity * 100.0);
1816 outs.printf(" svg:offset=\"1\"/>\n");
1817 outs.printf("</svg:radialGradient>\n");
1818 }
1819 else
1820 {
1821 g_warning("unsupported gradient style '%s'", gi.style.c_str());
1822 return false;
1823 }
1824 outs.printf("<style:style style:name=\"%#s\" style:family=\"graphic\" ",
1825 gi.name.c_str());
1826 outs.printf("style:parent-style-name=\"standard\">\n");
1827 outs.printf(" <style:graphic-properties draw:fill=\"gradient\" ");
1828 outs.printf("draw:fill-gradient-name=\"%#s_g\"\n",
1829 gi.name.c_str());
1830 outs.printf(" draw:textarea-horizontal-align=\"center\" ");
1831 outs.printf("draw:textarea-vertical-align=\"middle\"/>\n");
1832 outs.printf("</style:style>\n\n");
1834 return true;
1835 }
1840 /**
1841 * SECOND PASS.
1842 * This is the main SPObject tree output to ODF. preprocess()
1843 * must be called prior to this, as elements will often reference
1844 * data parsed and tabled in preprocess().
1845 */
1846 bool OdfOutput::writeTree(Writer &couts, Writer &souts,
1847 Inkscape::XML::Node *node)
1848 {
1849 //# Get the SPItem, if applicable
1850 SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(node);
1851 if (!reprobj)
1852 return true;
1853 if (!SP_IS_ITEM(reprobj))
1854 {
1855 return true;
1856 }
1857 SPItem *item = SP_ITEM(reprobj);
1860 Glib::ustring nodeName = node->name();
1861 Glib::ustring id = getAttribute(node, "id");
1863 //### Get SVG-to-ODF transform
1864 NR::Matrix tf = getODFTransform(item);
1866 //### Get ODF bounding box params for item
1867 boost::optional<NR::Rect> bbox = getODFBoundingBox(item);
1868 if (!bbox) {
1869 return true;
1870 }
1872 double bbox_x = bbox->min()[NR::X];
1873 double bbox_y = bbox->min()[NR::Y];
1874 double bbox_width = bbox->extent(NR::X);
1875 double bbox_height = bbox->extent(NR::Y);
1877 double rotate;
1878 double xskew;
1879 double yskew;
1880 double xscale;
1881 double yscale;
1882 analyzeTransform(tf, rotate, xskew, yskew, xscale, yscale);
1884 //# Do our stuff
1885 SPCurve *curve = NULL;
1889 if (nodeName == "svg" || nodeName == "svg:svg")
1890 {
1891 //# Iterate through the children
1892 for (Inkscape::XML::Node *child = node->firstChild() ;
1893 child ; child = child->next())
1894 {
1895 if (!writeTree(couts, souts, child))
1896 return false;
1897 }
1898 return true;
1899 }
1900 else if (nodeName == "g" || nodeName == "svg:g")
1901 {
1902 if (id.size() > 0)
1903 couts.printf("<draw:g id=\"%s\">\n", id.c_str());
1904 else
1905 couts.printf("<draw:g>\n");
1906 //# Iterate through the children
1907 for (Inkscape::XML::Node *child = node->firstChild() ;
1908 child ; child = child->next())
1909 {
1910 if (!writeTree(couts, souts, child))
1911 return false;
1912 }
1913 if (id.size() > 0)
1914 couts.printf("</draw:g> <!-- id=\"%s\" -->\n", id.c_str());
1915 else
1916 couts.printf("</draw:g>\n");
1917 return true;
1918 }
1920 //######################################
1921 //# S T Y L E
1922 //######################################
1923 processStyle(souts, item, id);
1925 //######################################
1926 //# G R A D I E N T
1927 //######################################
1928 processGradient(souts, item, id, tf);
1933 //######################################
1934 //# I T E M D A T A
1935 //######################################
1936 //g_message("##### %s #####", nodeName.c_str());
1937 if (nodeName == "image" || nodeName == "svg:image")
1938 {
1939 if (!SP_IS_IMAGE(item))
1940 {
1941 g_warning("<image> is not an SPImage. Why? ;-)");
1942 return false;
1943 }
1945 SPImage *img = SP_IMAGE(item);
1946 double ix = img->x.value;
1947 double iy = img->y.value;
1948 double iwidth = img->width.value;
1949 double iheight = img->height.value;
1951 NR::Rect ibbox(NR::Point(ix, iy), NR::Point(ix+iwidth, iy+iheight));
1952 ibbox = ibbox * tf;
1953 ix = ibbox.min()[NR::X];
1954 iy = ibbox.min()[NR::Y];
1955 //iwidth = ibbox.max()[NR::X] - ibbox.min()[NR::X];
1956 //iheight = ibbox.max()[NR::Y] - ibbox.min()[NR::Y];
1957 iwidth = xscale * iwidth;
1958 iheight = yscale * iheight;
1960 NR::Matrix itemTransform = getODFItemTransform(item);
1962 Glib::ustring itemTransformString = formatTransform(itemTransform);
1964 Glib::ustring href = getAttribute(node, "xlink:href");
1965 std::map<Glib::ustring, Glib::ustring>::iterator iter = imageTable.find(href);
1966 if (iter == imageTable.end())
1967 {
1968 g_warning("image '%s' not in table", href.c_str());
1969 return false;
1970 }
1971 Glib::ustring newName = iter->second;
1973 couts.printf("<draw:frame ");
1974 if (id.size() > 0)
1975 couts.printf("id=\"%s\" ", id.c_str());
1976 couts.printf("draw:style-name=\"gr1\" draw:text-style-name=\"P1\" draw:layer=\"layout\" ");
1977 //no x or y. make them the translate transform, last one
1978 couts.printf("svg:width=\"%.3fcm\" svg:height=\"%.3fcm\" ",
1979 iwidth, iheight);
1980 if (itemTransformString.size() > 0)
1981 {
1982 couts.printf("draw:transform=\"%s translate(%.3fcm, %.3fcm)\" ",
1983 itemTransformString.c_str(), ix, iy);
1984 }
1985 else
1986 {
1987 couts.printf("draw:transform=\"translate(%.3fcm, %.3fcm)\" ",
1988 ix, iy);
1989 }
1991 couts.printf(">\n");
1992 couts.printf(" <draw:image xlink:href=\"%s\" xlink:type=\"simple\"\n",
1993 newName.c_str());
1994 couts.printf(" xlink:show=\"embed\" xlink:actuate=\"onLoad\">\n");
1995 couts.printf(" <text:p/>\n");
1996 couts.printf(" </draw:image>\n");
1997 couts.printf("</draw:frame>\n");
1998 return true;
1999 }
2000 else if (SP_IS_SHAPE(item))
2001 {
2002 //g_message("### %s is a shape", nodeName.c_str());
2003 curve = sp_shape_get_curve(SP_SHAPE(item));
2004 }
2005 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
2006 {
2007 curve = te_get_layout(item)->convertToCurves();
2008 }
2010 if (curve)
2011 {
2012 //### Default <path> output
2014 couts.printf("<draw:path ");
2015 if (id.size()>0)
2016 couts.printf("id=\"%s\" ", id.c_str());
2018 std::map<Glib::ustring, Glib::ustring>::iterator siter;
2019 siter = styleLookupTable.find(id);
2020 if (siter != styleLookupTable.end())
2021 {
2022 Glib::ustring styleName = siter->second;
2023 couts.printf("draw:style-name=\"%s\" ", styleName.c_str());
2024 }
2026 std::map<Glib::ustring, Glib::ustring>::iterator giter;
2027 giter = gradientLookupTable.find(id);
2028 if (giter != gradientLookupTable.end())
2029 {
2030 Glib::ustring gradientName = giter->second;
2031 couts.printf("draw:fill-gradient-name=\"%s\" ",
2032 gradientName.c_str());
2033 }
2035 couts.printf("draw:layer=\"layout\" svg:x=\"%.3fcm\" svg:y=\"%.3fcm\" ",
2036 bbox_x, bbox_y);
2037 couts.printf("svg:width=\"%.3fcm\" svg:height=\"%.3fcm\" ",
2038 bbox_width, bbox_height);
2039 couts.printf("svg:viewBox=\"0.0 0.0 %.3f %.3f\"\n",
2040 bbox_width * 1000.0, bbox_height * 1000.0);
2042 couts.printf(" svg:d=\"");
2043 int nrPoints = writePath(couts, curve->get_pathvector(),
2044 tf, bbox_x, bbox_y);
2045 couts.printf("\"");
2047 couts.printf(">\n");
2048 couts.printf(" <!-- %d nodes -->\n", nrPoints);
2049 couts.printf("</draw:path>\n\n");
2052 curve->unref();
2053 }
2055 return true;
2056 }
2060 /**
2061 * Write the header for the content.xml file
2062 */
2063 bool OdfOutput::writeStyleHeader(Writer &outs)
2064 {
2065 time_t tim;
2066 time(&tim);
2068 outs.printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2069 outs.printf("\n");
2070 outs.printf("\n");
2071 outs.printf("<!--\n");
2072 outs.printf("*************************************************************************\n");
2073 outs.printf(" file: styles.xml\n");
2074 outs.printf(" Generated by Inkscape: %s", ctime(&tim)); //ctime has its own <cr>
2075 outs.printf(" http://www.inkscape.org\n");
2076 outs.printf("*************************************************************************\n");
2077 outs.printf("-->\n");
2078 outs.printf("\n");
2079 outs.printf("\n");
2080 outs.printf("<office:document-styles\n");
2081 outs.printf(" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
2082 outs.printf(" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"\n");
2083 outs.printf(" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"\n");
2084 outs.printf(" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"\n");
2085 outs.printf(" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"\n");
2086 outs.printf(" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"\n");
2087 outs.printf(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
2088 outs.printf(" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
2089 outs.printf(" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
2090 outs.printf(" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"\n");
2091 outs.printf(" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
2092 outs.printf(" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"\n");
2093 outs.printf(" xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"\n");
2094 outs.printf(" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"\n");
2095 outs.printf(" xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n");
2096 outs.printf(" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"\n");
2097 outs.printf(" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"\n");
2098 outs.printf(" xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
2099 outs.printf(" xmlns:ooow=\"http://openoffice.org/2004/writer\"\n");
2100 outs.printf(" xmlns:oooc=\"http://openoffice.org/2004/calc\"\n");
2101 outs.printf(" xmlns:dom=\"http://www.w3.org/2001/xml-events\"\n");
2102 outs.printf(" xmlns:xforms=\"http://www.w3.org/2002/xforms\"\n");
2103 outs.printf(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
2104 outs.printf(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
2105 outs.printf(" xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
2106 outs.printf(" xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
2107 outs.printf(" office:version=\"1.0\">\n");
2108 outs.printf("\n");
2109 outs.printf("\n");
2110 outs.printf("<!--\n");
2111 outs.printf("*************************************************************************\n");
2112 outs.printf(" S T Y L E S\n");
2113 outs.printf(" Style entries have been pulled from the svg style and\n");
2114 outs.printf(" representation attributes in the SVG tree. The tree elements\n");
2115 outs.printf(" then refer to them by name, in the ODF manner\n");
2116 outs.printf("*************************************************************************\n");
2117 outs.printf("-->\n");
2118 outs.printf("\n");
2119 outs.printf("<office:styles>\n");
2120 outs.printf("\n");
2122 return true;
2123 }
2126 /**
2127 * Write the footer for the style.xml file
2128 */
2129 bool OdfOutput::writeStyleFooter(Writer &outs)
2130 {
2131 outs.printf("\n");
2132 outs.printf("</office:styles>\n");
2133 outs.printf("\n");
2134 outs.printf("\n");
2135 outs.printf("<office:automatic-styles>\n");
2136 outs.printf("<!-- ####### 'Standard' styles ####### -->\n");
2137 outs.printf("<style:style style:name=\"dp1\" style:family=\"drawing-page\"/>\n");
2138 outs.printf("<style:style style:name=\"gr1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
2139 outs.printf(" <style:graphic-properties draw:stroke=\"none\" draw:fill=\"none\"\n");
2140 outs.printf(" draw:textarea-horizontal-align=\"center\"\n");
2141 outs.printf(" draw:textarea-vertical-align=\"middle\" draw:color-mode=\"standard\"\n");
2142 outs.printf(" draw:luminance=\"0%%\" draw:contrast=\"0%%\" draw:gamma=\"100%%\" draw:red=\"0%%\"\n");
2143 outs.printf(" draw:green=\"0%%\" draw:blue=\"0%%\" fo:clip=\"rect(0cm 0cm 0cm 0cm)\"\n");
2144 outs.printf(" draw:image-opacity=\"100%%\" style:mirror=\"none\"/>\n");
2145 outs.printf("</style:style>\n");
2146 outs.printf("<style:style style:name=\"P1\" style:family=\"paragraph\">\n");
2147 outs.printf(" <style:paragraph-properties fo:text-align=\"center\"/>\n");
2148 outs.printf("</style:style>\n");
2149 outs.printf("</office:automatic-styles>\n");
2150 outs.printf("\n");
2151 outs.printf("\n");
2152 outs.printf("<office:master-styles>\n");
2153 outs.printf("<draw:layer-set>\n");
2154 outs.printf(" <draw:layer draw:name=\"layout\"/>\n");
2155 outs.printf(" <draw:layer draw:name=\"background\"/>\n");
2156 outs.printf(" <draw:layer draw:name=\"backgroundobjects\"/>\n");
2157 outs.printf(" <draw:layer draw:name=\"controls\"/>\n");
2158 outs.printf(" <draw:layer draw:name=\"measurelines\"/>\n");
2159 outs.printf("</draw:layer-set>\n");
2160 outs.printf("\n");
2161 outs.printf("<style:master-page style:name=\"Default\"\n");
2162 outs.printf(" style:page-master-name=\"PM1\" draw:style-name=\"dp1\"/>\n");
2163 outs.printf("</office:master-styles>\n");
2164 outs.printf("\n");
2165 outs.printf("\n");
2166 outs.printf("\n");
2167 outs.printf("</office:document-styles>\n");
2168 outs.printf("\n");
2169 outs.printf("<!--\n");
2170 outs.printf("*************************************************************************\n");
2171 outs.printf(" E N D O F F I L E\n");
2172 outs.printf(" Have a nice day - ishmal\n");
2173 outs.printf("*************************************************************************\n");
2174 outs.printf("-->\n");
2175 outs.printf("\n");
2177 return true;
2178 }
2183 /**
2184 * Write the header for the content.xml file
2185 */
2186 bool OdfOutput::writeContentHeader(Writer &outs)
2187 {
2188 time_t tim;
2189 time(&tim);
2191 outs.printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2192 outs.printf("\n");
2193 outs.printf("\n");
2194 outs.printf("<!--\n");
2195 outs.printf("*************************************************************************\n");
2196 outs.printf(" file: content.xml\n");
2197 outs.printf(" Generated by Inkscape: %s", ctime(&tim)); //ctime has its own <cr>
2198 outs.printf(" http://www.inkscape.org\n");
2199 outs.printf("*************************************************************************\n");
2200 outs.printf("-->\n");
2201 outs.printf("\n");
2202 outs.printf("\n");
2203 outs.printf("<office:document-content\n");
2204 outs.printf(" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
2205 outs.printf(" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"\n");
2206 outs.printf(" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"\n");
2207 outs.printf(" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"\n");
2208 outs.printf(" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"\n");
2209 outs.printf(" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"\n");
2210 outs.printf(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
2211 outs.printf(" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
2212 outs.printf(" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
2213 outs.printf(" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"\n");
2214 outs.printf(" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
2215 outs.printf(" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"\n");
2216 outs.printf(" xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"\n");
2217 outs.printf(" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"\n");
2218 outs.printf(" xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n");
2219 outs.printf(" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"\n");
2220 outs.printf(" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"\n");
2221 outs.printf(" xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
2222 outs.printf(" xmlns:ooow=\"http://openoffice.org/2004/writer\"\n");
2223 outs.printf(" xmlns:oooc=\"http://openoffice.org/2004/calc\"\n");
2224 outs.printf(" xmlns:dom=\"http://www.w3.org/2001/xml-events\"\n");
2225 outs.printf(" xmlns:xforms=\"http://www.w3.org/2002/xforms\"\n");
2226 outs.printf(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
2227 outs.printf(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
2228 outs.printf(" xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
2229 outs.printf(" xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
2230 outs.printf(" office:version=\"1.0\">\n");
2231 outs.printf("\n");
2232 outs.printf("\n");
2233 outs.printf("<office:scripts/>\n");
2234 outs.printf("\n");
2235 outs.printf("\n");
2236 outs.printf("<!--\n");
2237 outs.printf("*************************************************************************\n");
2238 outs.printf(" D R A W I N G\n");
2239 outs.printf(" This section is the heart of SVG-ODF conversion. We are\n");
2240 outs.printf(" starting with simple conversions, and will slowly evolve\n");
2241 outs.printf(" into a 'smarter' translation as time progresses. Any help\n");
2242 outs.printf(" in improving .odg export is welcome.\n");
2243 outs.printf("*************************************************************************\n");
2244 outs.printf("-->\n");
2245 outs.printf("\n");
2246 outs.printf("\n");
2247 outs.printf("<office:body>\n");
2248 outs.printf("<office:drawing>\n");
2249 outs.printf("<draw:page draw:name=\"page1\" draw:style-name=\"dp1\"\n");
2250 outs.printf(" draw:master-page-name=\"Default\">\n");
2251 outs.printf("\n");
2252 outs.printf("\n");
2254 return true;
2255 }
2258 /**
2259 * Write the footer for the content.xml file
2260 */
2261 bool OdfOutput::writeContentFooter(Writer &outs)
2262 {
2263 outs.printf("\n");
2264 outs.printf("\n");
2266 outs.printf("</draw:page>\n");
2267 outs.printf("</office:drawing>\n");
2269 outs.printf("\n");
2270 outs.printf("\n");
2271 outs.printf("<!-- ######### CONVERSION FROM SVG ENDS ######## -->\n");
2272 outs.printf("\n");
2273 outs.printf("\n");
2275 outs.printf("</office:body>\n");
2276 outs.printf("</office:document-content>\n");
2277 outs.printf("\n");
2278 outs.printf("\n");
2279 outs.printf("\n");
2280 outs.printf("<!--\n");
2281 outs.printf("*************************************************************************\n");
2282 outs.printf(" E N D O F F I L E\n");
2283 outs.printf(" Have a nice day - ishmal\n");
2284 outs.printf("*************************************************************************\n");
2285 outs.printf("-->\n");
2286 outs.printf("\n");
2287 outs.printf("\n");
2289 return true;
2290 }
2294 /**
2295 * Write the content.xml file. Writes the namesspace headers, then
2296 * calls writeTree().
2297 */
2298 bool OdfOutput::writeContent(ZipFile &zf, Inkscape::XML::Node *node)
2299 {
2300 //Content.xml stream
2301 BufferOutputStream cbouts;
2302 OutputStreamWriter couts(cbouts);
2304 if (!writeContentHeader(couts))
2305 return false;
2307 //Style.xml stream
2308 BufferOutputStream sbouts;
2309 OutputStreamWriter souts(sbouts);
2311 if (!writeStyleHeader(souts))
2312 return false;
2315 //# Descend into the tree, doing all of our conversions
2316 //# to both files as the same time
2317 if (!writeTree(couts, souts, node))
2318 {
2319 g_warning("Failed to convert SVG tree");
2320 return false;
2321 }
2325 //# Finish content file
2326 if (!writeContentFooter(couts))
2327 return false;
2329 ZipEntry *ze = zf.newEntry("content.xml", "ODF master content file");
2330 ze->setUncompressedData(cbouts.getBuffer());
2331 ze->finish();
2335 //# Finish style file
2336 if (!writeStyleFooter(souts))
2337 return false;
2339 ze = zf.newEntry("styles.xml", "ODF style file");
2340 ze->setUncompressedData(sbouts.getBuffer());
2341 ze->finish();
2343 return true;
2344 }
2347 /**
2348 * Resets class to its pristine condition, ready to use again
2349 */
2350 void
2351 OdfOutput::reset()
2352 {
2353 metadata.clear();
2354 styleTable.clear();
2355 styleLookupTable.clear();
2356 gradientTable.clear();
2357 gradientLookupTable.clear();
2358 imageTable.clear();
2361 }
2364 /**
2365 * Descends into the SVG tree, mapping things to ODF when appropriate
2366 */
2367 void
2368 OdfOutput::save(Inkscape::Extension::Output */*mod*/, SPDocument *doc, gchar const *uri)
2369 {
2370 reset();
2372 //g_message("native file:%s\n", uri);
2373 documentUri = URI(uri);
2375 ZipFile zf;
2376 preprocess(zf, doc->rroot);
2378 if (!writeManifest(zf))
2379 {
2380 g_warning("Failed to write manifest");
2381 return;
2382 }
2384 if (!writeContent(zf, doc->rroot))
2385 {
2386 g_warning("Failed to write content");
2387 return;
2388 }
2390 if (!writeMeta(zf))
2391 {
2392 g_warning("Failed to write metafile");
2393 return;
2394 }
2396 if (!zf.writeFile(uri))
2397 {
2398 return;
2399 }
2400 }
2403 /**
2404 * This is the definition of PovRay output. This function just
2405 * calls the extension system with the memory allocated XML that
2406 * describes the data.
2407 */
2408 void
2409 OdfOutput::init()
2410 {
2411 Inkscape::Extension::build_from_mem(
2412 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
2413 "<name>" N_("OpenDocument Drawing Output") "</name>\n"
2414 "<id>org.inkscape.output.odf</id>\n"
2415 "<output>\n"
2416 "<extension>.odg</extension>\n"
2417 "<mimetype>text/x-povray-script</mimetype>\n"
2418 "<filetypename>" N_("OpenDocument drawing (*.odg)") "</filetypename>\n"
2419 "<filetypetooltip>" N_("OpenDocument drawing file") "</filetypetooltip>\n"
2420 "</output>\n"
2421 "</inkscape-extension>",
2422 new OdfOutput());
2423 }
2425 /**
2426 * Make sure that we are in the database
2427 */
2428 bool
2429 OdfOutput::check (Inkscape::Extension::Extension */*module*/)
2430 {
2431 /* We don't need a Key
2432 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
2433 return FALSE;
2434 */
2436 return TRUE;
2437 }
2441 //########################################################################
2442 //# I N P U T
2443 //########################################################################
2447 //#######################
2448 //# L A T E R !!! :-)
2449 //#######################
2463 } //namespace Internal
2464 } //namespace Extension
2465 } //namespace Inkscape
2468 //########################################################################
2469 //# E N D O F F I L E
2470 //########################################################################
2472 /*
2473 Local Variables:
2474 mode:c++
2475 c-file-style:"stroustrup"
2476 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2477 indent-tabs-mode:nil
2478 fill-column:99
2479 End:
2480 */
2481 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :