Example #1
0
void SvgParser::applyFillStyle(KoShape *shape)
{
    SvgGraphicsContext *gc = m_context.currentGC();
    if (! gc)
        return;

    if (gc->fillType == SvgGraphicsContext::None) {
        shape->setBackground(QSharedPointer<KoShapeBackground>(0));
    } else if (gc->fillType == SvgGraphicsContext::Solid) {
        shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(gc->fillColor)));
    } else if (gc->fillType == SvgGraphicsContext::Complex) {
        // try to find referenced gradient
        SvgGradientHelper *gradient = findGradient(gc->fillId);
        if (gradient) {
            // great, we have a gradient fill
            QSharedPointer<KoGradientBackground> bg;
            if (gradient->gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
                bg = QSharedPointer<KoGradientBackground>(new KoGradientBackground(*gradient->gradient()));
                bg->setTransform(gradient->transform());
            } else {
                QGradient *convertedGradient = SvgGradientHelper::convertGradient(gradient->gradient(), shape->size());
                bg = QSharedPointer<KoGradientBackground>(new KoGradientBackground(convertedGradient));
                QTransform invShapematrix = shape->transformation().inverted();
                bg->setTransform(gradient->transform() * gc->matrix * invShapematrix);
            }
            shape->setBackground(bg);
        } else {
            // try to find referenced pattern
            SvgPatternHelper *pattern = findPattern(gc->fillId);
            KoImageCollection *imageCollection = m_documentResourceManager->imageCollection();
            if (pattern && imageCollection) {
                // great we have a pattern fill
                QRectF objectBound = QRectF(QPoint(), shape->size());
                QRectF currentBoundbox = gc->currentBoundbox;

                // properties from the object are not inherited
                // so we are creating a new context without copying
                SvgGraphicsContext *gc = m_context.pushGraphicsContext(pattern->content(), false);

                // the pattern establishes a new coordinate system with its
                // origin at the patterns x and y attributes
                gc->matrix = QTransform();
                // object bounding box units are relative to the object the pattern is applied
                if (pattern->patternContentUnits() == SvgPatternHelper::ObjectBoundingBox) {
                    gc->currentBoundbox = objectBound;
                    gc->forcePercentage = true;
                } else {
                    // inherit the current bounding box
                    gc->currentBoundbox = currentBoundbox;
                }

                applyStyle(0, pattern->content());

                // parse the pattern content elements
                QList<KoShape*> patternContent = parseContainer(pattern->content());

                // generate the pattern image from the shapes and the object bounding rect
                QImage image = pattern->generateImage(objectBound, patternContent);

                m_context.popGraphicsContext();

                // delete the shapes created from the pattern content
                qDeleteAll(patternContent);

                if (!image.isNull()) {
                    QSharedPointer<KoPatternBackground> bg(new KoPatternBackground(imageCollection));
                    bg->setPattern(image);

                    QPointF refPoint = shape->documentToShape(pattern->position(objectBound));
                    QSizeF tileSize = pattern->size(objectBound);

                    bg->setPatternDisplaySize(tileSize);
                    if (pattern->patternUnits() == SvgPatternHelper::ObjectBoundingBox) {
                        if (tileSize == objectBound.size())
                            bg->setRepeat(KoPatternBackground::Stretched);
                    }

                    // calculate pattern reference point offset in percent of tileSize
                    // and relative to the topleft corner of the shape
                    qreal fx = refPoint.x() / tileSize.width();
                    qreal fy = refPoint.y() / tileSize.height();
                    if (fx < 0.0)
                        fx = floor(fx);
                    else if (fx > 1.0)
                        fx = ceil(fx);
                    else
                        fx = 0.0;
                    if (fy < 0.0)
                        fy = floor(fy);
                    else if (fx > 1.0)
                        fy = ceil(fy);
                    else
                        fy = 0.0;
                    qreal offsetX = 100.0 * (refPoint.x() - fx * tileSize.width()) / tileSize.width();
                    qreal offsetY = 100.0 * (refPoint.y() - fy * tileSize.height()) / tileSize.height();
                    bg->setReferencePointOffset(QPointF(offsetX, offsetY));

                    shape->setBackground(bg);
                }
            } else {
                // no referenced fill found, use fallback color
                shape->setBackground(QSharedPointer<KoColorBackground>(new KoColorBackground(gc->fillColor)));
            }
        }
    }

    KoPathShape *path = dynamic_cast<KoPathShape*>(shape);
    if (path)
        path->setFillRule(gc->fillRule);
}