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