From 68664e00e2372534b4df2fdc5f54f836bafece18 Mon Sep 17 00:00:00 2001 From: miklosh Date: Sat, 28 Jul 2007 14:31:25 +0000 Subject: [PATCH] Set top-level groups as layers with the name of the PDF document --- src/extension/internal/pdfinput/pdf-input.cpp | 8 +++++- .../internal/pdfinput/svg-builder.cpp | 25 ++++++++++++++++++- src/extension/internal/pdfinput/svg-builder.h | 4 ++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/extension/internal/pdfinput/pdf-input.cpp b/src/extension/internal/pdfinput/pdf-input.cpp index 9adf1958f..4a9377b9a 100644 --- a/src/extension/internal/pdfinput/pdf-input.cpp +++ b/src/extension/internal/pdfinput/pdf-input.cpp @@ -69,7 +69,12 @@ PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) { sp_document_set_undo_sensitive(doc, false); // No need to undo in this temporary document // Create builder and parser - SvgBuilder *builder = new SvgBuilder(doc, pdf_doc->getXRef()); + gchar *docname = g_path_get_basename(uri); + gchar *dot = g_strrstr(docname, "."); + if (dot) { + *dot = 0; + } + SvgBuilder *builder = new SvgBuilder(doc, docname, pdf_doc->getXRef()); PdfParser *pdf_parser = new PdfParser(pdf_doc->getXRef(), builder, page_num-1, page->getRotate(), page->getResourceDict(), page->getCropBox()); @@ -84,6 +89,7 @@ PdfInput::open(::Inkscape::Extension::Input * mod, const gchar * uri) { obj.free(); delete pdf_parser; delete builder; + g_free(docname); delete pdf_doc; // Restore undo diff --git a/src/extension/internal/pdfinput/svg-builder.cpp b/src/extension/internal/pdfinput/svg-builder.cpp index f640ded35..b0c6c8fea 100644 --- a/src/extension/internal/pdfinput/svg-builder.cpp +++ b/src/extension/internal/pdfinput/svg-builder.cpp @@ -69,8 +69,9 @@ SvgBuilder::SvgBuilder() { _current_state = NULL; } -SvgBuilder::SvgBuilder(SPDocument *document, XRef *xref) { +SvgBuilder::SvgBuilder(SPDocument *document, gchar *docname, XRef *xref) { _doc = document; + _docname = docname; _xref = xref; _xml_doc = sp_document_repr_doc(_doc); _container = _root = _doc->rroot; @@ -80,6 +81,7 @@ SvgBuilder::SvgBuilder(SPDocument *document, XRef *xref) { SvgBuilder::SvgBuilder(SvgBuilder *parent, Inkscape::XML::Node *root) { _doc = parent->_doc; + _docname = parent->_docname; _xref = parent->_xref; _xml_doc = parent->_xml_doc; _container = this->_root = root; @@ -96,6 +98,16 @@ void SvgBuilder::setDocumentSize(double width, double height) { this->_height = height; } +/** + * \brief Sets groupmode of the current container to 'layer' and sets its label if given + */ +void SvgBuilder::setAsLayer(char *layer_name) { + _container->setAttribute("inkscape:groupmode", "layer"); + if (layer_name) { + _container->setAttribute("inkscape:label", layer_name); + } +} + void SvgBuilder::saveState() { _group_depth.push_back(0); pushGroup(); @@ -114,6 +126,17 @@ Inkscape::XML::Node *SvgBuilder::pushGroup() { _container = node; Inkscape::GC::release(node); _group_depth.back()++; + // Set as a layer if this is a top-level group + if ( _container->parent() == _root ) { + static int layer_count = 1; + if ( layer_count > 1 ) { + gchar *layer_name = g_strdup_printf("%s%d", _docname, layer_count); + setAsLayer(layer_name); + g_free(layer_name); + } else { + setAsLayer(_docname); + } + } return _container; } diff --git a/src/extension/internal/pdfinput/svg-builder.h b/src/extension/internal/pdfinput/svg-builder.h index 47a79a0f4..a329e3020 100644 --- a/src/extension/internal/pdfinput/svg-builder.h +++ b/src/extension/internal/pdfinput/svg-builder.h @@ -79,12 +79,13 @@ struct SvgGlyph { */ class SvgBuilder { public: - SvgBuilder(SPDocument *document, XRef *xref); + SvgBuilder(SPDocument *document, gchar *docname, XRef *xref); SvgBuilder(SvgBuilder *parent, Inkscape::XML::Node *root); ~SvgBuilder(); // Property setting void setDocumentSize(double width, double height); // Document size in px + void setAsLayer(char *layer_name=NULL); // Handling the node stack Inkscape::XML::Node *pushGroup(); @@ -166,6 +167,7 @@ private: GfxState *_current_state; SPDocument *_doc; + gchar *_docname; // Basename of the URI from which this document is created XRef *_xref; // Cross-reference table from the PDF doc we're converting from Inkscape::XML::Document *_xml_doc; Inkscape::XML::Node *_root; // Root node from the point of view of this SvgBuilder -- 2.30.2