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