From 372160020acd94509b0cecce8bfa9276f2feb48a Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Wed, 26 Sep 2012 15:42:58 +0300 Subject: [PATCH 1/2] Implemented import groups and strokes from CFF. --- src/adaptors/UBCFFSubsetAdaptor.cpp | 118 +++++++++++++++------------- src/adaptors/UBCFFSubsetAdaptor.h | 2 +- 2 files changed, 66 insertions(+), 54 deletions(-) diff --git a/src/adaptors/UBCFFSubsetAdaptor.cpp b/src/adaptors/UBCFFSubsetAdaptor.cpp index f156e4c5..7228f5b2 100644 --- a/src/adaptors/UBCFFSubsetAdaptor.cpp +++ b/src/adaptors/UBCFFSubsetAdaptor.cpp @@ -350,13 +350,6 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolygon(const QDomElement &e } } - //bounding rect lef top corner coordinates - qreal x1 = polygon.boundingRect().topLeft().x(); - qreal y1 = polygon.boundingRect().topLeft().y(); - //bounding rect dimensions - qreal width = polygon.boundingRect().width(); - qreal height = polygon.boundingRect().height(); - QString strokeColorText = element.attribute(aStroke); QString fillColorText = element.attribute(aFill); QString strokeWidthText = element.attribute(aStrokewidth); @@ -373,37 +366,22 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolygon(const QDomElement &e brush.setColor(fillColor); brush.setStyle(Qt::SolidPattern); - QSvgGenerator *generator = createSvgGenerator(width + pen.width(), height + pen.width()); - QPainter painter; - - painter.begin(generator); //drawing to svg tmp file + UBGraphicsPolygonItem *graphicsPolygon = new UBGraphicsPolygonItem(polygon); + graphicsPolygon->setBrush(brush); - painter.translate(pen.widthF() / 2 - x1, pen.widthF() / 2 - y1); - painter.setBrush(brush); - painter.setPen(pen); - painter.drawPolygon(polygon); - - painter.end(); - - //add resulting svg file to scene - UBGraphicsSvgItem *svgItem = mCurrentScene->addSvg(QUrl::fromLocalFile(generator->fileName())); QTransform transform; QString textTransform = element.attribute(aTransform); - svgItem->resetTransform(); + graphicsPolygon->resetTransform(); if (!textTransform.isNull()) { - transform = transformFromString(textTransform, svgItem); + transform = transformFromString(textTransform, graphicsPolygon); } - repositionSvgItem(svgItem, width +strokeWidth, height + strokeWidth, x1 - strokeWidth/2 + transform.m31(), y1 + strokeWidth/2 + transform.m32(), transform); - hashSceneItem(element, svgItem); + mCurrentScene->addItem(graphicsPolygon); if (mGSectionContainer) { - addItemToGSection(svgItem); + addItemToGSection(graphicsPolygon); } - - delete generator; - return true; } bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolyline(const QDomElement &element) @@ -438,9 +416,6 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolyline(const QDomElement & } } - //bounding rect lef top corner coordinates - qreal x1 = polygon.boundingRect().topLeft().x(); - qreal y1 = polygon.boundingRect().topLeft().y(); //bounding rect dimensions qreal width = polygon.boundingRect().width(); qreal height = polygon.boundingRect().height(); @@ -458,35 +433,25 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolyline(const QDomElement & pen.setColor(strokeColor); pen.setWidth(strokeWidth); - QSvgGenerator *generator = createSvgGenerator(width + pen.width(), height + pen.width()); - QPainter painter; - - painter.begin(generator); //drawing to svg tmp file - - painter.translate(pen.widthF()/2 - x1, pen.widthF()/2- y1); - painter.setPen(pen); - painter.drawPolyline(polygon); - - painter.end(); - - //add resulting svg file to scene - UBGraphicsSvgItem *svgItem = mCurrentScene->addSvg(QUrl::fromLocalFile(generator->fileName())); + QBrush brush; + brush.setColor(strokeColor); + brush.setStyle(Qt::SolidPattern); + UBGraphicsPolygonItem *graphicsPolygon = new UBGraphicsPolygonItem(polygon); + graphicsPolygon->setBrush(brush); QTransform transform; QString textTransform = element.attribute(aTransform); - svgItem->resetTransform(); + graphicsPolygon->resetTransform(); if (!textTransform.isNull()) { - transform = transformFromString(textTransform, svgItem); + transform = transformFromString(textTransform, graphicsPolygon); } - repositionSvgItem(svgItem, width +strokeWidth, height + strokeWidth, x1 + transform.m31() - strokeWidth/2, y1 + transform.m32() + strokeWidth/2, transform); - hashSceneItem(element, svgItem); - + mCurrentScene->addItem(graphicsPolygon); + if (mGSectionContainer) { - addItemToGSection(svgItem); + addItemToGSection(graphicsPolygon); } - delete generator; return true; } @@ -1052,7 +1017,7 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvg(const QDomElement &svgSecti return true; } -bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbGroup(QDomElement &parent) +UBGraphicsGroupContainerItem *UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbGroup(QDomElement &parent) { //TODO. Create groups from elements parsed by parseIwbElement() function if (parent.namespaceURI() != iwbNS) { @@ -1060,7 +1025,54 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbGroup(QDomElement &parent) return false; } - return true; + UBGraphicsGroupContainerItem *group = new UBGraphicsGroupContainerItem(); + QList strokesContainer; + QList groupContainer; + QDomElement currentStrokeElement = parent.firstChildElement(); + while (!currentStrokeElement.isNull()) + { + if (tGroup == currentStrokeElement.tagName()) + group->addToGroup(parseIwbGroup(currentStrokeElement)); + else + { + QString uuid = currentStrokeElement.attribute(aRef); + if (!uuid.isEmpty()) + { + if (uuid.contains("stroke")) // create stroke group + strokesContainer.append(qgraphicsitem_cast(mCurrentScene->itemForUuid(uuid))); + else // single elements in group + groupContainer.append(mCurrentScene->itemForUuid(uuid)); + } + } + currentStrokeElement = currentStrokeElement.nextSiblingElement(); + } + UBGraphicsStrokesGroup* pStrokesGroup = new UBGraphicsStrokesGroup(); + UBGraphicsStroke *currentStroke = new UBGraphicsStroke(); + foreach(UBGraphicsPolygonItem* poly, strokesContainer) + { + if (poly) + { + mCurrentScene->removeItem(poly); + mCurrentScene->removeItemFromDeletion(poly); + poly->setStrokesGroup(pStrokesGroup); + poly->setStroke(currentStroke); + pStrokesGroup->addToGroup(poly); + } + } + if (currentStroke->polygons().empty()) + delete currentStroke; + if (pStrokesGroup->childItems().count()) + mCurrentScene->addItem(pStrokesGroup); + else + delete pStrokesGroup; + foreach(QGraphicsItem* item, groupContainer) + group->addToGroup(item); + if (pStrokesGroup) + group->addToGroup(pStrokesGroup); + if (group->childItems().count()) + mCurrentScene->addItem(group); + + return group; } bool UBCFFSubsetAdaptor::UBCFFSubsetReader::strToBool(QString str) diff --git a/src/adaptors/UBCFFSubsetAdaptor.h b/src/adaptors/UBCFFSubsetAdaptor.h index acebc151..ca389a49 100644 --- a/src/adaptors/UBCFFSubsetAdaptor.h +++ b/src/adaptors/UBCFFSubsetAdaptor.h @@ -102,7 +102,7 @@ private: inline bool parseSvgFlash(const QDomElement &element); inline bool parseSvgAudio(const QDomElement &element); inline bool parseSvgVideo(const QDomElement &element); - inline bool parseIwbGroup(QDomElement &parent); + inline UBGraphicsGroupContainerItem *parseIwbGroup(QDomElement &parent); inline bool parseIwbElement(QDomElement &element); inline void parseTSpan(const QDomElement &parent, QPainter &painter , qreal &curX, qreal &curY, qreal &width, qreal &height, qreal &linespacing, QRectF &lastDrawnTextBoundingRect From 925efac633a9bfcc95b6fd76d735c01dec0fb316 Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Thu, 27 Sep 2012 13:11:29 +0300 Subject: [PATCH 2/2] Implemented import UBZ strokes from CFF (means strokes who was exported from UBZ to CFF and imported back). Compatible with groups. --- src/adaptors/UBCFFSubsetAdaptor.cpp | 219 +++++++++++++++++++++------- 1 file changed, 166 insertions(+), 53 deletions(-) diff --git a/src/adaptors/UBCFFSubsetAdaptor.cpp b/src/adaptors/UBCFFSubsetAdaptor.cpp index 7228f5b2..6fa04c34 100644 --- a/src/adaptors/UBCFFSubsetAdaptor.cpp +++ b/src/adaptors/UBCFFSubsetAdaptor.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "core/UBPersistenceManager.h" @@ -350,6 +351,13 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolygon(const QDomElement &e } } + //bounding rect lef top corner coordinates + qreal x1 = polygon.boundingRect().topLeft().x(); + qreal y1 = polygon.boundingRect().topLeft().y(); + //bounding rect dimensions + qreal width = polygon.boundingRect().width(); + qreal height = polygon.boundingRect().height(); + QString strokeColorText = element.attribute(aStroke); QString fillColorText = element.attribute(aFill); QString strokeWidthText = element.attribute(aStrokewidth); @@ -366,21 +374,59 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolygon(const QDomElement &e brush.setColor(fillColor); brush.setStyle(Qt::SolidPattern); - UBGraphicsPolygonItem *graphicsPolygon = new UBGraphicsPolygonItem(polygon); - graphicsPolygon->setBrush(brush); + + QUuid itemUuid(element.attribute(aId).right(QUuid().toString().length())); + QUuid itemGroupUuid(element.attribute(aId).left(QUuid().toString().length()-1)); + if (!itemUuid.isNull() && (itemGroupUuid!=itemUuid)) // reimported from UBZ + { + UBGraphicsPolygonItem *graphicsPolygon = new UBGraphicsPolygonItem(polygon); - QTransform transform; - QString textTransform = element.attribute(aTransform); + graphicsPolygon->setBrush(brush); - graphicsPolygon->resetTransform(); - if (!textTransform.isNull()) { - transform = transformFromString(textTransform, graphicsPolygon); - } - mCurrentScene->addItem(graphicsPolygon); + QTransform transform; + QString textTransform = element.attribute(aTransform); - if (mGSectionContainer) + graphicsPolygon->resetTransform(); + if (!textTransform.isNull()) { + transform = transformFromString(textTransform, graphicsPolygon); + } + mCurrentScene->addItem(graphicsPolygon); + + graphicsPolygon->setUuid(itemUuid); + + } + else // single CFF { - addItemToGSection(graphicsPolygon); + QSvgGenerator *generator = createSvgGenerator(width + pen.width(), height + pen.width()); + QPainter painter; + + painter.begin(generator); //drawing to svg tmp file + + painter.translate(pen.widthF() / 2 - x1, pen.widthF() / 2 - y1); + painter.setBrush(brush); + painter.setPen(pen); + painter.drawPolygon(polygon); + + painter.end(); + + //add resulting svg file to scene + UBGraphicsSvgItem *svgItem = mCurrentScene->addSvg(QUrl::fromLocalFile(generator->fileName())); + QTransform transform; + QString textTransform = element.attribute(aTransform); + + svgItem->resetTransform(); + if (!textTransform.isNull()) { + transform = transformFromString(textTransform, svgItem); + } + repositionSvgItem(svgItem, width +strokeWidth, height + strokeWidth, x1 - strokeWidth/2 + transform.m31(), y1 + strokeWidth/2 + transform.m32(), transform); + hashSceneItem(element, svgItem); + + if (mGSectionContainer) + { + addItemToGSection(svgItem); + } + + delete generator; } return true; } @@ -416,6 +462,10 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolyline(const QDomElement & } } + //bounding rect lef top corner coordinates + qreal x1 = polygon.boundingRect().topLeft().x(); + qreal y1 = polygon.boundingRect().topLeft().y(); + //bounding rect dimensions qreal width = polygon.boundingRect().width(); qreal height = polygon.boundingRect().height(); @@ -436,20 +486,59 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPolyline(const QDomElement & QBrush brush; brush.setColor(strokeColor); brush.setStyle(Qt::SolidPattern); - UBGraphicsPolygonItem *graphicsPolygon = new UBGraphicsPolygonItem(polygon); - graphicsPolygon->setBrush(brush); - QTransform transform; - QString textTransform = element.attribute(aTransform); - graphicsPolygon->resetTransform(); - if (!textTransform.isNull()) { - transform = transformFromString(textTransform, graphicsPolygon); + QUuid itemUuid(element.attribute(aId).right(QUuid().toString().length())); + QUuid itemGroupUuid(element.attribute(aId).left(QUuid().toString().length()-1)); + if (!itemUuid.isNull() && (itemGroupUuid!=itemUuid)) // reimported from UBZ + { + UBGraphicsPolygonItem *graphicsPolygon = new UBGraphicsPolygonItem(polygon); + + UBGraphicsStroke *stroke = new UBGraphicsStroke(); + graphicsPolygon->setStroke(stroke); + + graphicsPolygon->setBrush(brush); + QTransform transform; + QString textTransform = element.attribute(aTransform); + + graphicsPolygon->resetTransform(); + if (!textTransform.isNull()) { + transform = transformFromString(textTransform, graphicsPolygon); + } + mCurrentScene->addItem(graphicsPolygon); + } - mCurrentScene->addItem(graphicsPolygon); - - if (mGSectionContainer) + else // simple CFF { - addItemToGSection(graphicsPolygon); + QSvgGenerator *generator = createSvgGenerator(width + pen.width(), height + pen.width()); + QPainter painter; + + painter.begin(generator); //drawing to svg tmp file + + painter.translate(pen.widthF() / 2 - x1, pen.widthF() / 2 - y1); + painter.setBrush(brush); + painter.setPen(pen); + painter.drawPolygon(polygon); + + painter.end(); + + //add resulting svg file to scene + UBGraphicsSvgItem *svgItem = mCurrentScene->addSvg(QUrl::fromLocalFile(generator->fileName())); + QTransform transform; + QString textTransform = element.attribute(aTransform); + + svgItem->resetTransform(); + if (!textTransform.isNull()) { + transform = transformFromString(textTransform, svgItem); + } + repositionSvgItem(svgItem, width +strokeWidth, height + strokeWidth, x1 - strokeWidth/2 + transform.m31(), y1 + strokeWidth/2 + transform.m32(), transform); + hashSceneItem(element, svgItem); + + if (mGSectionContainer) + { + addItemToGSection(svgItem); + } + + delete generator; } @@ -945,8 +1034,8 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgElement(const QDomElement &p { QString tagName = parent.tagName(); if (parent.namespaceURI() != svgNS) { - qDebug() << "Incorrect namespace, error at content file, line number" << parent.lineNumber(); - return false; + qWarning() << "Incorrect namespace, error at content file, line number" << parent.lineNumber(); + //return false; } if (tagName == tG && !parseGSection(parent)) return false; @@ -992,8 +1081,8 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvgPageset(const QDomElement &p bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbMeta(const QDomElement &element) { if (element.namespaceURI() != iwbNS) { - qDebug() << "incorrect meta namespace, incorrect document"; - return false; + qWarning() << "incorrect meta namespace, incorrect document"; + //return false; } return true; @@ -1001,8 +1090,8 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbMeta(const QDomElement &elem bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseSvg(const QDomElement &svgSection) { if (svgSection.namespaceURI() != svgNS) { - qDebug() << "incorrect svg namespace, incorrect document"; - return false; + qWarning() << "incorrect svg namespace, incorrect document"; + // return false; } parseSvgSectionAttr(svgSection); @@ -1021,13 +1110,15 @@ UBGraphicsGroupContainerItem *UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbGro { //TODO. Create groups from elements parsed by parseIwbElement() function if (parent.namespaceURI() != iwbNS) { - qDebug() << "incorrect iwb group namespace, incorrect document"; - return false; + qWarning() << "incorrect iwb group namespace, incorrect document"; + // return false; } UBGraphicsGroupContainerItem *group = new UBGraphicsGroupContainerItem(); - QList strokesContainer; + QMultiMap strokesGroupsContainer; QList groupContainer; + QString currentStrokeIdentifier; + QDomElement currentStrokeElement = parent.firstChildElement(); while (!currentStrokeElement.isNull()) { @@ -1038,40 +1129,62 @@ UBGraphicsGroupContainerItem *UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbGro QString uuid = currentStrokeElement.attribute(aRef); if (!uuid.isEmpty()) { - if (uuid.contains("stroke")) // create stroke group - strokesContainer.append(qgraphicsitem_cast(mCurrentScene->itemForUuid(uuid))); + if (uuid.size() > QUuid().toString().length()) // create stroke group + { + currentStrokeIdentifier = uuid.left(QUuid().toString().length()-1); + UBGraphicsPolygonItem *strokeByUuid = qgraphicsitem_cast(mCurrentScene->itemForUuid(QUuid(uuid.right(QUuid().toString().length())))); + + if (strokeByUuid) + strokesGroupsContainer.insert(currentStrokeIdentifier, strokeByUuid); + } else // single elements in group - groupContainer.append(mCurrentScene->itemForUuid(uuid)); + groupContainer.append(mCurrentScene->itemForUuid(QUuid(uuid))); } } currentStrokeElement = currentStrokeElement.nextSiblingElement(); } - UBGraphicsStrokesGroup* pStrokesGroup = new UBGraphicsStrokesGroup(); - UBGraphicsStroke *currentStroke = new UBGraphicsStroke(); - foreach(UBGraphicsPolygonItem* poly, strokesContainer) + + + + foreach (QString key, strokesGroupsContainer.keys()) { - if (poly) + UBGraphicsStrokesGroup* pStrokesGroup = new UBGraphicsStrokesGroup(); + UBGraphicsStroke *currentStroke = new UBGraphicsStroke(); + foreach(UBGraphicsPolygonItem* poly, strokesGroupsContainer.values(key)) { - mCurrentScene->removeItem(poly); - mCurrentScene->removeItemFromDeletion(poly); - poly->setStrokesGroup(pStrokesGroup); - poly->setStroke(currentStroke); - pStrokesGroup->addToGroup(poly); + if (poly) + { + mCurrentScene->removeItem(poly); + mCurrentScene->removeItemFromDeletion(poly); + poly->setStrokesGroup(pStrokesGroup); + poly->setStroke(currentStroke); + pStrokesGroup->addToGroup(poly); + } } + if (currentStroke->polygons().empty()) + delete currentStroke; + if (pStrokesGroup->childItems().count()) + mCurrentScene->addItem(pStrokesGroup); + else + delete pStrokesGroup; + + if (pStrokesGroup) + group->addToGroup(pStrokesGroup); } - if (currentStroke->polygons().empty()) - delete currentStroke; - if (pStrokesGroup->childItems().count()) - mCurrentScene->addItem(pStrokesGroup); - else - delete pStrokesGroup; + foreach(QGraphicsItem* item, groupContainer) group->addToGroup(item); - if (pStrokesGroup) - group->addToGroup(pStrokesGroup); + if (group->childItems().count()) + { mCurrentScene->addItem(group); + if (!groupContainer.count()) + { + group->destroy(false); + } + } + return group; } @@ -1083,8 +1196,8 @@ bool UBCFFSubsetAdaptor::UBCFFSubsetReader::strToBool(QString str) bool UBCFFSubsetAdaptor::UBCFFSubsetReader::parseIwbElement(QDomElement &element) { if (element.namespaceURI() != iwbNS) { - qDebug() << "incorrect iwb element namespace, incorrect document"; - return false; + qWarning() << "incorrect iwb element namespace, incorrect document"; + // return false; } bool locked = false;