Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
wpg-input.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * This file came from libwpg as a source, their utility wpg2svg
4 * specifically. It has been modified to work as an Inkscape extension.
5 * The Inkscape extension code is covered by this copyright, but the
6 * rest is covered by the one below.
7 *
8 * Authors:
9 * Ted Gould <ted@gould.cx>
10 * Abhishek Sharma
11 *
12 * Copyright (C) 2006 Authors
13 *
14 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 *
16 */
17
18/* libwpg
19 * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
20 * Copyright (C) 2005 Fridrich Strba (fridrich.strba@bluewin.ch)
21 *
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Library General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Library General Public License for more details.
31 *
32 * You should have received a copy of the GNU Library General Public
33 * License along with this library; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 *
36 * For further information visit http://libwpg.sourceforge.net
37 */
38
39/* "This product is not manufactured, approved, or supported by
40 * Corel Corporation or Corel Corporation Limited."
41 */
42
43#include "wpg-input.h"
44#include "extension/system.h"
45#include "extension/input.h"
46#include "document.h"
47#include "object/sp-root.h"
48#include "util/units.h"
49
50#include <libwpg/libwpg.h>
51#include <librevenge-stream/librevenge-stream.h>
52
53using librevenge::RVNGString;
54using librevenge::RVNGFileStream;
55using librevenge::RVNGInputStream;
56
57using namespace libwpg;
58
60
61static std::span<char const> as_span(RVNGString const &str)
62{
63 return {str.cstr(), str.size()};
64}
65
66std::unique_ptr<SPDocument> WpgInput::open(Inkscape::Extension::Input *, char const *uri, bool)
67{
68 std::unique_ptr<RVNGInputStream> input;
69
70 #ifdef _WIN32
71 // RVNGFileStream uses fopen() internally which unfortunately only uses ANSI encoding on Windows
72 // therefore attempt to convert uri to the system codepage
73 // even if this is not possible the alternate short (8.3) file name will be used if available
74 auto converted_uri = g_win32_locale_filename_from_utf8(uri);
75 input = std::make_unique<RVNGFileStream>(converted_uri);
76 g_free(converted_uri);
77 #else
78 input = std::make_unique<RVNGFileStream>(uri);
79 #endif
80
81 if (input->isStructured()) {
82 if (auto olestream = input->getSubStreamByName("PerfectOffice_MAIN")) {
83 input.reset(olestream);
84 }
85 }
86
87 if (!WPGraphics::isSupported(input.get())) {
89 // fprintf(stderr, "ERROR: Unsupported file format (unsupported version) or file is encrypted!\n");
90 // printf("I'm giving up not supported\n");
91 return nullptr;
92 }
93
94 librevenge::RVNGStringVector vec;
95 librevenge::RVNGSVGDrawingGenerator generator(vec, "");
96
97 if (!libwpg::WPGraphics::parse(input.get(), &generator) || vec.empty() || vec[0].empty()) {
98 return nullptr;
99 }
100
101 RVNGString output("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
102 output.append(vec[0]);
103
104 auto doc = SPDocument::createNewDocFromMem(as_span(output), true);
105
106 // Set viewBox if it doesn't exist
107 if (doc && !doc->getRoot()->viewBox_set) {
108 // Scales the document to account for 72dpi scaling in librevenge(<=0.0.4)
109 doc->setWidth(Inkscape::Util::Quantity(doc->getWidth().quantity, "pt"), false);
110 doc->setHeight(Inkscape::Util::Quantity(doc->getHeight().quantity, "pt"), false);
111 doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value("pt"), doc->getHeight().value("pt")));
112 }
113
114 return doc;
115}
116
117#include "clear-n_.h"
118
120 // clang-format off
122 "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
123 "<name>" N_("WPG Input") "</name>\n"
124 "<id>org.inkscape.input.wpg</id>\n"
125 "<input>\n"
126 "<extension>.wpg</extension>\n"
127 "<mimetype>image/x-wpg</mimetype>\n"
128 "<filetypename>" N_("WordPerfect Graphics (*.wpg)") "</filetypename>\n"
129 "<filetypetooltip>" N_("Vector graphics format used by Corel WordPerfect") "</filetypetooltip>\n"
130 "</input>\n"
131 "</inkscape-extension>", std::make_unique<WpgInput>());
132 // clang-format on
133}
134
135} // namespace Inkscape::Extension::Internal
136
137/*
138 Local Variables:
139 mode:c++
140 c-file-style:"stroustrup"
141 c-file-offsets:((innamespace . 0)(inline-open . 0))
142 indent-tabs-mode:nil
143 fill-column:99
144 End:
145*/
146// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
static CRect from_xywh(Coord x, Coord y, Coord w, Coord h)
Create rectangle from origin and dimensions.
std::unique_ptr< SPDocument > open(Inkscape::Extension::Input *mod, char const *uri, bool is_importing) override
Open a file.
Definition wpg-input.cpp:66
static std::unique_ptr< SPDocument > createNewDocFromMem(std::span< char const > buffer, bool keepalive, std::string const &filename="")
Definition document.cpp:708
A way to clear the N_ macro, which is defined as an inline function.
std::span< char const > as_span(librevenge::RVNGString const &str)
void build_from_mem(gchar const *buffer, std::unique_ptr< Implementation::Implementation > in_imp)
Create a module from a buffer holding an XML description.
Definition system.cpp:459
SPRoot: SVG <svg> implementation.