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