Code

* Removing the bitmap files that aren't being used.
[inkscape.git] / src / extension / internal / wpg-input.cpp
index 13335cf796d173d315a6188cb95dcd92bd201375..c37d5705b4bc0b0077fc2506a9b129ed0b6bd79f 100644 (file)
-/* \r
- *  This file came from libwpg as a source, their utility wpg2svg\r
- *  specifically.  It has been modified to work as an Inkscape extension.\r
- *  The Inkscape extension code is covered by this copyright, but the\r
- *  rest is covered by the one bellow.\r
- *\r
- * Authors:\r
- *   Ted Gould <ted@gould.cx>\r
- *\r
- * Copyright (C) 2006 Authors\r
- *\r
- * Released under GNU GPL, read the file 'COPYING' for more information\r
- *\r
- */\r
-\r
-/* libwpg\r
- * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)\r
- * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Library General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * Library General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Library General Public\r
- * License along with this library; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA\r
- *\r
- * For further information visit http://libwpg.sourceforge.net\r
- */\r
-\r
-/* "This product is not manufactured, approved, or supported by\r
- * Corel Corporation or Corel Corporation Limited."\r
- */\r
-\r
-#include <stdio.h>\r
-\r
-#include "wpg-input.h"\r
-#include "extension/system.h"\r
-#include "extension/input.h"\r
-\r
-#include "libwpg/libwpg.h"\r
-#include "libwpg/WPGStreamImplementation.h"\r
-\r
-using namespace libwpg;\r
-\r
-namespace Inkscape {\r
-namespace Extension {\r
-namespace Internal {\r
-\r
-class InkscapePainter : public libwpg::WPGPaintInterface {\r
-public:\r
-       InkscapePainter();\r
-\r
-       void startDocument(double imageWidth, double imageHeight);\r
-       void endDocument();\r
-       void startLayer(unsigned int id);\r
-       void endLayer(unsigned int id);\r
-\r
-       void setPen(const WPGPen& pen);\r
-       void setBrush(const WPGBrush& brush);\r
-       void setFillRule(FillRule rule);\r
-\r
-       void drawRectangle(const WPGRect& rect, double rx, double ry);\r
-       void drawEllipse(const WPGPoint& center, double rx, double ry);\r
-       void drawPolygon(const WPGPointArray& vertices);\r
-       void drawPath(const WPGPath& path);\r
-\r
-private:\r
-       WPGPen m_pen;\r
-       WPGBrush m_brush;\r
-       FillRule m_fillRule;\r
-       int m_gradientIndex;\r
-       void writeStyle();\r
-\r
-public:\r
-        std::string document;\r
-};\r
-\r
-InkscapePainter::InkscapePainter(): m_fillRule(AlternatingFill), m_gradientIndex(1)\r
-{\r
-}\r
-\r
-void InkscapePainter::startDocument(double width, double height) \r
-{\r
-       printf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");\r
-       printf("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"");\r
-       printf(" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");\r
-\r
-//     printf("<!-- Created with wpg2svg/libwpg %s -->\n", LIBWPG_VERSION_STRING);\r
-\r
-       printf("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" ");\r
-       printf("xmlns:xlink=\"http://www.w3.org/1999/xlink\" ");        \r
-       printf("width=\"%g\" height=\"%f\" >\n", 72*width, 72*height);\r
-       \r
-       m_gradientIndex = 1;\r
-}\r
-\r
-void InkscapePainter::endDocument()\r
-{\r
-       printf("</svg>\n");\r
-}\r
-\r
-void InkscapePainter::setPen(const WPGPen& pen)\r
-{\r
-       m_pen = pen;\r
-}\r
-\r
-void InkscapePainter::setBrush(const WPGBrush& brush)\r
-{\r
-       m_brush = brush;\r
-       \r
-       if(m_brush.style == WPGBrush::Gradient)\r
-       {\r
-               double angle = m_brush.gradient.angle();\r
-\r
-               printf("<defs>\n");\r
-               printf("  <linearGradient id=\"grad%d\" >\n", m_gradientIndex++);\r
-               for(unsigned c = 0; c < m_brush.gradient.count(); c++)\r
-               {\r
-                       // round to nearest percentage\r
-                       int ofs = (int)(100.0*m_brush.gradient.stopOffset(c)+0.5);\r
-\r
-                       WPGColor color = m_brush.gradient.stopColor(c);\r
-                       printf("    <stop offset=\"%d%%\" stop-color=\"#%02x%02x%02x\" />\n",\r
-                               ofs, color.red, color.green, color.blue);\r
-               }\r
-               printf("  </linearGradient>\n");\r
-               \r
-               // not a simple horizontal gradient\r
-               if(angle != -90.0)\r
-               {\r
-                       printf("  <linearGradient xlink:href=\"#grad%d\"", m_gradientIndex-1);\r
-                       printf(" id=\"grad%d\" ", m_gradientIndex++);\r
-                       printf("x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\" "); \r
-                       printf("gradientTransform=\"rotate(%f)\" ", angle);\r
-                       printf("gradientUnits=\"objectBoundingBox\" >\n");\r
-                       printf("  </linearGradient>\n");\r
-               }\r
-               \r
-               printf("</defs>\n");\r
-       }\r
-}\r
-\r
-void InkscapePainter::setFillRule(FillRule rule)\r
-{\r
-       m_fillRule = rule;\r
-}\r
-\r
-void InkscapePainter::startLayer(unsigned int id)\r
-{\r
-       printf("<g id=\"Layer%d\" >\n", id);\r
-}\r
-\r
-void InkscapePainter::endLayer(unsigned int)\r
-{\r
-       printf("</g>\n");\r
-}\r
-\r
-void InkscapePainter::drawRectangle(const WPGRect& rect, double rx, double ry)\r
-{\r
-       printf("<rect ");\r
-       printf("x=\"%f\" y=\"%f\" ", 72*rect.x1, 72*rect.y1);\r
-       printf("width=\"%f\" height=\"%f\" ", 72*rect.width(), 72*rect.height());\r
-       if((rx !=0) || (ry !=0))\r
-               printf("rx=\"%f\" ry=\"%f\" ", 72*rx, 72*ry);\r
-       writeStyle();\r
-       printf("/>\n");\r
-}\r
-\r
-void InkscapePainter::drawEllipse(const WPGPoint& center, double rx, double ry)\r
-{\r
-       printf("<ellipse ");\r
-       printf("cx=\"%f\" cy=\"%f\" ", 72*center.x, 72*center.y);\r
-       printf("rx=\"%f\" ry=\"%f\" ", 72*rx, 72*ry);\r
-       writeStyle();\r
-       printf("/>\n");\r
-}\r
-\r
-void InkscapePainter::drawPolygon(const WPGPointArray& vertices)\r
-{\r
-       if(vertices.count() < 2)\r
-               return;\r
-\r
-       if(vertices.count() == 2)\r
-       {\r
-               const WPGPoint& p1 = vertices[0];\r
-               const WPGPoint& p2 = vertices[1];\r
-               printf("<line ");\r
-               printf("x1=\"%f\"  y1=\"%f\" ", 72*p1.x, 72*p1.y);\r
-               printf("x2=\"%f\"  y2=\"%f\"\n", 72*p2.x, 72*p2.y);\r
-               writeStyle();\r
-               printf("/>\n");\r
-       }\r
-       else\r
-       {\r
-               printf("<polyline ");\r
-               printf("points=\"");\r
-               for(unsigned i = 0; i < vertices.count(); i++)\r
-               {\r
-                       printf("%f %f", 72*vertices[i].x, 72*vertices[i].y);\r
-                       if(i < vertices.count()-1) printf(", ");\r
-               }\r
-               printf("\"\n");\r
-               writeStyle();\r
-               printf("/>\n");\r
-       }\r
-}\r
-\r
-void InkscapePainter::drawPath(const WPGPath& path)\r
-{\r
-       printf("<path d=\"");\r
-       for(unsigned i = 0; i < path.count(); i++)\r
-       {\r
-               WPGPathElement element = path.element(i);\r
-               WPGPoint point = element.point;\r
-               switch(element.type)\r
-               {\r
-                       case WPGPathElement::MoveToElement:\r
-                               printf("\n M%f,%f ", 72*point.x, 72*point.y );\r
-                               break;\r
-                               \r
-                       case WPGPathElement::LineToElement:\r
-                               printf("\n L%f,%f ", 72*point.x, 72*point.y );\r
-                               break;\r
-                       \r
-                       case WPGPathElement::CurveToElement:\r
-                               printf("C");\r
-                               printf("%f,%f ", 72*element.extra1.x, 72*element.extra1.y );\r
-                               printf("%f,%f ", 72*element.extra2.x, 72*element.extra2.y );\r
-                               printf("%f,%f", 72*point.x, 72*point.y );\r
-                               break;\r
-                       \r
-                       default:\r
-                               break;\r
-               }\r
-       }\r
-       printf("\" \n");\r
-       writeStyle();\r
-       printf("/>\n");\r
-}\r
-\r
-// create "style" attribute based on current pen and brush\r
-void InkscapePainter::writeStyle()\r
-{\r
-       printf("style=\"");\r
-\r
-       const WPGColor& color = m_pen.foreColor;\r
-       printf("stroke-width: %f; ", 72*m_pen.width);\r
-       if(m_pen.width > 0.0)\r
-       {\r
-               printf("stroke: rgb(%d,%d,%d); ", color.red, color.green, color.blue);\r
-               if(color.alpha != 0)\r
-                       // alpha = 0 means opacity = 1.0, alpha = 256 means opacity = 0\r
-                       printf("stroke-opacity: %f; ", 1.0-(color.alpha/256.0));\r
-       }\r
-\r
-       if(!m_pen.solid)\r
-       {\r
-               printf("stroke-dasharray: ");\r
-               for(unsigned i = 0; i < m_pen.dashArray.count(); i++)\r
-               {\r
-                       printf("%f", 72*m_pen.dashArray.at(i)*m_pen.width);\r
-                       if(i < m_pen.dashArray.count()-1) \r
-                               printf(", ");\r
-               }\r
-               printf("; ");\r
-       }\r
-       \r
-       if(m_brush.style == WPGBrush::NoBrush)\r
-               printf("fill: none; ");\r
-\r
-       if(m_fillRule == InkscapePainter::WindingFill)\r
-               printf("fill-rule: nonzero; ");\r
-       else if(m_fillRule == InkscapePainter::AlternatingFill)\r
-               printf("fill-rule: evenodd; ");\r
-\r
-       if(m_brush.style == WPGBrush::Gradient)\r
-               printf("fill: url(#grad%d); ", m_gradientIndex-1);\r
-\r
-       if(m_brush.style == WPGBrush::Solid)\r
-               printf("fill: rgb(%d,%d,%d); ", m_brush.foreColor.red, \r
-                       m_brush.foreColor.green, m_brush.foreColor.blue);\r
-\r
-       printf("\""); // style\r
-}\r
-\r
-SPDocument *\r
-WpgInput::open(Inkscape::Extension::Input * mod, const gchar * uri) {\r
-    WPGInputStream* input = new WPGFileStream(uri);\r
-    if (input->isOle()) {\r
-        WPGInputStream* olestream = input->getWPGOleStream();\r
-        if (olestream) {\r
-            delete input;\r
-            input = olestream;\r
-        }\r
-    }\r
-\r
-    if (!WPGraphics::isSupported(input)) {\r
-        //! \todo Dialog here\r
-        // fprintf(stderr, "ERROR: Unsupported file format (unsupported version) or file is encrypted!\n");\r
-        return NULL;\r
-    }\r
-\r
-    InkscapePainter painter;\r
-    WPGraphics::parse(input, &painter);\r
-\r
-    return 0;\r
-}\r
-\r
-#include "clear-n_.h"\r
-\r
-void\r
-WpgInput::init(void) {\r
-    Inkscape::Extension::Extension * ext;\r
-\r
-    ext = Inkscape::Extension::build_from_mem(\r
-        "<inkscape-extension>\n"\r
-            "<name>" N_("WPG Input") "</name>\n"\r
-            "<id>org.inkscape.input.wpg</id>\n"\r
-            "<input>\n"\r
-                "<extension>.wpg</extension>\n"\r
-                "<mimetype>image/x-wpg</mimetype>\n"\r
-                "<filetypename>" N_("WordPerfect Graphics (*.wpg)") "</filetypename>\n"\r
-                "<filetypetooltip>" N_("Vector graphics format used by Corel WordPerfect") "</filetypetooltip>\n"\r
-            "</input>\n"\r
-        "</inkscape-extension>", new WpgInput());\r
-} // init\r
-\r
-} } }  /* namespace Inkscape, Extension, Implementation */\r
-\r
-/*\r
-  Local Variables:\r
-  mode:c++\r
-  c-file-style:"stroustrup"\r
-  c-file-offsets:((innamespace . 0)(inline-open . 0))\r
-  indent-tabs-mode:nil\r
-  fill-column:99\r
-  End:\r
-*/\r
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r
+/* 
+ *  This file came from libwpg as a source, their utility wpg2svg
+ *  specifically.  It has been modified to work as an Inkscape extension.
+ *  The Inkscape extension code is covered by this copyright, but the
+ *  rest is covered by the one bellow.
+ *
+ * Authors:
+ *   Ted Gould <ted@gould.cx>
+ *
+ * Copyright (C) 2006 Authors
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ *
+ */
+
+/* libwpg
+ * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
+ * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *
+ * For further information visit http://libwpg.sourceforge.net
+ */
+
+/* "This product is not manufactured, approved, or supported by
+ * Corel Corporation or Corel Corporation Limited."
+ */
+
+#include <stdio.h>
+#include "config.h"
+
+#ifdef WITH_LIBWPG
+
+#include "wpg-input.h"
+#include "extension/system.h"
+#include "extension/input.h"
+#include "document.h"
+
+#include "libwpg/libwpg.h"
+#include "libwpg/WPGStreamImplementation.h"
+
+
+using namespace libwpg;
+
+namespace Inkscape {
+namespace Extension {
+namespace Internal {
+
+
+SPDocument *
+WpgInput::open(Inkscape::Extension::Input * mod, const gchar * uri) {
+    WPXInputStream* input = new libwpg::WPGFileStream(uri);
+    if (input->isOLEStream()) {
+        WPXInputStream* olestream = input->getDocumentOLEStream();
+        if (olestream) {
+            delete input;
+            input = olestream;
+        }
+    }
+
+    if (!WPGraphics::isSupported(input)) {
+        //! \todo Dialog here
+        // fprintf(stderr, "ERROR: Unsupported file format (unsupported version) or file is encrypted!\n");
+        // printf("I'm giving up not supported\n");
+        delete input;
+        return NULL;
+    }
+
+    libwpg::WPGString output;
+    if (!libwpg::WPGraphics::generateSVG(input, output)) {
+        delete input;
+        return NULL;
+    }
+
+    //printf("I've got a doc: \n%s", painter.document.c_str());
+
+    SPDocument * doc = sp_document_new_from_mem(output.cstr(), strlen(output.cstr()), TRUE);
+    delete input;
+    return doc;
+}
+
+#include "clear-n_.h"
+
+void
+WpgInput::init(void) {
+    Inkscape::Extension::Extension * ext;
+
+    ext = Inkscape::Extension::build_from_mem(
+        "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
+            "<name>" N_("WPG Input") "</name>\n"
+            "<id>org.inkscape.input.wpg</id>\n"
+            "<input>\n"
+                "<extension>.wpg</extension>\n"
+                "<mimetype>image/x-wpg</mimetype>\n"
+                "<filetypename>" N_("WordPerfect Graphics (*.wpg)") "</filetypename>\n"
+                "<filetypetooltip>" N_("Vector graphics format used by Corel WordPerfect") "</filetypetooltip>\n"
+            "</input>\n"
+        "</inkscape-extension>", new WpgInput());
+} // init
+
+} } }  /* namespace Inkscape, Extension, Implementation */
+#endif /* WITH_LIBWPG */
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :