odf.cpp revision 76addc201c409e81eaaa73fe27cc0f79c4db097c
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * OpenDocument <drawing> input and output
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * This is an an entry in the extensions mechanism to begin to enable
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * the inputting and outputting of OpenDocument Format (ODF) files from
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * within Inkscape. Although the initial implementations will be very lossy
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * due to the differences in the models of SVG and ODF, they will hopefully
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * improve greatly with time. People should consider this to be a framework
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * that can be continously upgraded for ever improving fidelity. Potential
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * developers should especially look in preprocess() and writeTree() to see how
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * the SVG tree is scanned, read, translated, and then written to ODF.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Authors:
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Bob Jamison
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Abhishek Sharma
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Kris De Gussem
6656f193fdace606d1b162d6dea0223bc295f0a6cilix * Copyright (C) 2006, 2007 Bob Jamison
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Copyright (C) 2013 Kris De Gussem
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * This library is free software; you can redistribute it and/or
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * modify it under the terms of the GNU Lesser General Public
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * License as published by the Free Software Foundation; either
d9c673867f424647c1586c356cc0ac1d34d0a98ajohanengelen * version 2.1 of the License, or (at your option) any later version.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * This library is distributed in the hope that it will be useful,
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * but WITHOUT ANY WARRANTY; without even the implied warranty of
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Lesser General Public License for more details.
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * You should have received a copy of the GNU Lesser General Public
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * License along with this library; if not, write to the Free Software
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
70eb1fc448cb08acf3468f80fa2296c03b32afd2cilix//# System includes
ab99111a42436818e6902e044c8f3af2b724263bcilix//# Inkscape includes
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraud//# Shorthand notation
92fe3142613d000eff89db8a983b3b18b14eee79johanengelentypedef Inkscape::IO::BufferOutputStream BufferOutputStream;
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraudtypedef Inkscape::IO::OutputStreamWriter OutputStreamWriter;
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraudtypedef Inkscape::IO::StringOutputStream StringOutputStream;
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraud//########################################################################
92fe3142613d000eff89db8a983b3b18b14eee79johanengelen//# C L A S S SingularValueDecomposition
dc98accfae7a38326b92d74fa4330ac8ccb5b778jfbarraud//########################################################################
0563fd55cbad59e8a878e6d4cbbdd8e47f74488djohanengelen SVDMatrix(unsigned int rowSize, unsigned int colSize)
8d9f5d586a04809427ce1df284a5720112177991cilix d = new double[size];
92fe3142613d000eff89db8a983b3b18b14eee79johanengelen for (unsigned int i=0 ; i<size ; i++)
8d9f5d586a04809427ce1df284a5720112177991cilix d[i] = 0.0;
70eb1fc448cb08acf3468f80fa2296c03b32afd2cilix SVDMatrix(double *vals, unsigned int rowSize, unsigned int colSize)
6f4a90e526af850ffc36064f58f09c190f3b633fjohanengelen d = new double[size];
f4db63be4e929f4706410914295deccaceea19cdcilix for (unsigned int i=0 ; i<size ; i++)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm return *this;
6656f193fdace606d1b162d6dea0223bc295f0a6cilix delete[] d;
6656f193fdace606d1b162d6dea0223bc295f0a6cilix double& operator() (unsigned int row, unsigned int col)
6656f193fdace606d1b162d6dea0223bc295f0a6cilix double operator() (unsigned int row, unsigned int col) const
6656f193fdace606d1b162d6dea0223bc295f0a6cilix unsigned int getRows()
6656f193fdace606d1b162d6dea0223bc295f0a6cilix unsigned int getCols()
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for (unsigned int i=0 ; i<rows ; i++)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for (unsigned int k=0 ; k<cols ; k++)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm //sum += a[i][k] * b[k][j];
262d0c3f05130d86368d95f110aa8ccab5f83e5ccilix for (unsigned int i=0 ; i<rows ; i++){
262d0c3f05130d86368d95f110aa8ccab5f83e5ccilix for (unsigned int j=0 ; j<cols ; j++){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm virtual void init()
dda97aeba7480d08320ebceecae13b8531db1b81johanengelen d = new double[size];
dda97aeba7480d08320ebceecae13b8531db1b81johanengelen for (unsigned int i=0 ; i<size ; i++){
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm d[i] = other.d[i];
46c4893a7458eda6edcd064121bc000634af7a09johanengelen unsigned int rows;
46c4893a7458eda6edcd064121bc000634af7a09johanengelen unsigned int cols;
46c4893a7458eda6edcd064121bc000634af7a09johanengelen unsigned int size;
46c4893a7458eda6edcd064121bc000634af7a09johanengelen * ====================================================
46c4893a7458eda6edcd064121bc000634af7a09johanengelen * This class is ported almost verbatim from the public domain
46c4893a7458eda6edcd064121bc000634af7a09johanengelen * JAMA Matrix package. It is modified to handle only 3x3 matrices
46c4893a7458eda6edcd064121bc000634af7a09johanengelen * and our Geom::Affine affine transform class. We give full
46c4893a7458eda6edcd064121bc000634af7a09johanengelen * attribution to them, along with many thanks. JAMA can be found at:
46c4893a7458eda6edcd064121bc000634af7a09johanengelen * ====================================================
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Singular Value Decomposition.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * For an m-by-n matrix A with m >= n, the singular value decomposition is
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * an n-by-n orthogonal matrix V so that A = U*S*V'.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * The singular values, sigma[k] = S[k][k], are ordered so that
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * sigma[0] >= sigma[1] >= ... >= sigma[n-1].
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * The singular value decompostion always exists, so the constructor will
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * never fail. The matrix condition number and the effective numerical
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * rank can be computed from this decomposition.
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen /** Construct the singular value decomposition
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm @param A Rectangular matrix
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen @return Structure to access U, S and V.
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen SingularValueDecomposition (const SVDMatrix &mat) :
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm delete[] s;
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen * Return the left singular vectors
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @return U
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Return the right singular vectors
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @return V
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * Return the s[index] value
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm * @return max(S)
73d455c08e8062e257dd052d2d690b9300434351cilix * Two norm condition number
73d455c08e8062e257dd052d2d690b9300434351cilix * @return max(S)/min(S)
73d455c08e8062e257dd052d2d690b9300434351cilix * Effective numerical matrix rank
73d455c08e8062e257dd052d2d690b9300434351cilix * @return Number of nonnegligible singular values.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm double *s;
c0cd5511d3b975ebe07d019c1f5528108725e438johanengelen unsigned int s_size;
c0cd5511d3b975ebe07d019c1f5528108725e438johanengelenstatic double svd_hypot(double a, double b)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm else if (b != 0)
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm // Initialize.
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm int m = A.getRows();
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm int n = A.getCols();
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm int nu = (m > n) ? m : n;
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen s = new double[s_size];
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen double *e = new double[n];
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen double *work = new double[m];
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen // Reduce A to bidiagonal form, storing the diagonal elements
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen // in s and the super-diagonal elements in e.
93bb287e28a818fd5ba61b99d012e0500a49ccf6johanengelen for (int k = 0; k < 2; k++) {
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen // Compute the transformation for the k-th column and
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // place the k-th diagonal in s[k].
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // Compute 2-norm of k-th column without under/overflow.
af8d25189f88abf89cdbe0e180e271c94079624fbuliabyak for (int i = k; i < m; i++) {
af8d25189f88abf89cdbe0e180e271c94079624fbuliabyak s[k] = svd_hypot(s[k],A(i, k));
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen if (s[k] != 0.0) {
af8d25189f88abf89cdbe0e180e271c94079624fbuliabyak if (A(k, k) < 0.0) {
a0334366488989ef25fb812d7030d298c0917c96johanengelen s[k] = -s[k];
a0334366488989ef25fb812d7030d298c0917c96johanengelen for (int i = k; i < m; i++) {
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen A(i, k) /= s[k];
a0334366488989ef25fb812d7030d298c0917c96johanengelen A(k, k) += 1.0;
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen s[k] = -s[k];
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen for (int j = k+1; j < n; j++) {
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // Apply the transformation.
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen double t = 0;
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen for (int i = k; i < m; i++) {
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen t += A(i, k) * A(i, j);
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen t = -t/A(k, k);
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen for (int i = k; i < m; i++) {
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen A(i, j) += t*A(i, k);
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // Place the k-th row of A into e for the
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // subsequent calculation of the row transformation.
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen e[j] = A(k, j);
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // Place the transformation in U for subsequent back
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen // multiplication.
fb5a72174252e0e79107dcad3bf5a2bbd73e349cjohanengelen for (int i = k; i < m; i++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm U(i, k) = A(i, k);
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen // Compute the k-th row transformation and place the
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen // k-th super-diagonal in e[k].
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen // Compute 2-norm without under/overflow.
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen for (int i = k+1; i < n; i++) {
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen e[k] = svd_hypot(e[k],e[i]);
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen if (e[k] != 0.0) {
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen e[k] = -e[k];
56542e2b97ec8826cc692153b0e2d4f5ac8ef913johanengelen for (int i = k+1; i < n; i++) {
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen e[i] /= e[k];
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen e[k] = -e[k];
ddc251b3cf95b0097b6a5ee39ea132bd4d7d5cbcjohanengelen // Apply the transformation.
42e99769805c14a5cc01c805faa3c3b03f9dd1c0johanengelen for (int i = k+1; i < m; i++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for (int j = k+1; j < n; j++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for (int i = k+1; i < m; i++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm work[i] += e[j]*A(i, j);
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for (int j = k+1; j < n; j++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm double t = -e[j]/e[k+1];
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm for (int i = k+1; i < m; i++) {
f07bfd5a05d43a6d11f7cd442f085149092dea88pjrm A(i, j) += t*work[i];
if (wantv) {
if (nct < n) {
if (wantu) {
if (wantv) {
int k,kase;
if (fabs(e[k]) <=
int ks;
if (ks == k) {
if (ks == k) {
k = ks;
switch (kase) {
double t = svd_hypot(s[j],f);
double cs = s[j]/t;
double sn = f/t;
if (wantv) {
double t = svd_hypot(s[j],f);
double cs = s[j]/t;
double sn = f/t;
f = -sn*e[j];
e[j] = cs*e[j];
if (wantu) {
d = fabs(s[k]);
d = fabs(e[k]);
double t = svd_hypot(f,g);
double cs = f/t;
double sn = g/t;
if (wantv) {
t = svd_hypot(f,g);
cs = f/t;
sn = g/t;
if (wantv) {
for (int i = 0; i <= pp; i++) {
while (k < pp) {
delete [] work;
return s[index];
if (s[i] > tol)
if (valstr)
return val;
return ext;
return str;
Geom::Affine doc2dt_tf = Geom::Affine(Geom::Scale(1.0, -1.0)); /// @fixme hardcoded desktop transform
return tf;
if (bbox) {
return bbox;
return itemTransform;
if (!reprobj)
if (ze)
outs.writeString("<!DOCTYPE manifest:manifest PUBLIC \"-//OpenOffice.org//DTD Manifest 1.0//EN\" \"Manifest.dtd\">\n");
outs.writeString("<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n");
outs.writeString(" <manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.graphics\" manifest:full-path=\"/\"/>\n");
outs.writeString(" <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>\n");
outs.writeString(" <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"styles.xml\"/>\n");
outs.writeString(" <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>\n");
* This writes the document meta information to meta.xml
outs.writeString("xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
Glib::ustring tmp = Glib::ustring::compose(" <meta:generator>%1</meta:generator>\n", InkscapeVersion);
using Geom::X;
using Geom::Y;
int nrPoints = 0;
Geom::PathVector pv = pathv_to_linear_and_cubic_beziers(pathv * tf * Geom::Translate(xoff, yoff) * Geom::Scale(1000.));
nrPoints++;
if (fabs(points[i][X])<1.0) points[i][X] = 0.0; // Why is this needed? Shouldn't we just round all numbers then?
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]);
nrPoints++;
return nrPoints;
bool OdfOutput::processStyle(SPItem *item, const Glib::ustring &id, const Glib::ustring &gradientNameFill, const Glib::ustring &gradientNameStroke, Glib::ustring& output)
if (!item)
if (!style)
if (gradient)
if (gradient)
bool styleMatch = false;
styleMatch = true;
if (styleMatch)
output = Glib::ustring::compose ("<style:style style:name=\"%1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n", si.name);
output += Glib::ustring::compose (" draw:fill=\"gradient\" draw:fill-gradient-name=\"%1\"", gradientNameFill);
output += Glib::ustring::compose (" draw:stroke=\"gradient\" draw:stroke-gradient-name=\"%1\"", gradientNameStroke);
output += Glib::ustring::compose (" svg:stroke-width=\"%1\" svg:stroke-color=\"%2\" ", si.strokeWidth, si.strokeColor);
if (!item)
if (!style)
SPGradient *gradient = SP_GRADIENT((checkFillGradient?(SP_STYLE_FILL_SERVER(style)) :(SP_STYLE_STROKE_SERVER(style))));
bool gradientMatch = false;
gradientMatch = true;
if (gradientMatch)
// int gradientCount = gradientTable.size();
snprintf(buf, 127, " draw:start-color=\"#%06lx\" draw:end-color=\"#%06lx\"", gi.stops[0].rgb, gi.stops[1].rgb);
//TODO: apply maths, to define begin of gradient, taking gradient begin and end, as well as object boundary into account
output += Glib::ustring::compose(" draw:start-intensity=\"%1\" draw:end-intensity=\"%2\" draw:angle=\"%3\"/>\n",
output += Glib::ustring::compose("<draw:gradient draw:name=\"%1\" draw:display-name=\"%1\" ", gi.name);
snprintf(buf, 127, "draw:start-color=\"#%06lx\" draw:end-color=\"#%06lx\" ", gi.stops[0].rgb, gi.stops[1].rgb);
snprintf(buf, 127, "draw:start-intensity=\"%f%%\" draw:end-intensity=\"%f%%\" ", gi.stops[0].opacity*100.0, gi.stops[1].opacity*100.0);
if (!reprobj)
if (!bbox) {
double rotate;
double xskew;
double yskew;
double xscale;
double yscale;
if (curve)
* Write the header for the content.xml file
outs.writeString(" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
* Write the footer for the style.xml file
outs.writeString("<style:style style:name=\"gr1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
outs.writeString(" draw:luminance=\"0%\" draw:contrast=\"0%\" draw:gamma=\"100%\" draw:red=\"0%\"\n");
* Write the header for the content.xml file
outs.writeString(" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
* Write the footer for the content.xml file
* Write the content.xml file. Writes the namesspace headers, then
//Content.xml stream
//Style.xml stream
reset();
"<mimetype>text/x-povray-script</mimetype>\n"
new OdfOutput());
return TRUE;