File indexing completed on 2025-07-06 04:48:05

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <config-kitinerary.h>
0008 
0009 #include "popplerutils_p.h"
0010 
0011 #include <QBrush>
0012 #include <QPainterPath>
0013 #include <QPen>
0014 
0015 #include <memory>
0016 
0017 #include <GfxState.h>
0018 
0019 using namespace KItinerary;
0020 
0021 QPen PopplerUtils::currentPen(GfxState *state)
0022 {
0023     QPen pen;
0024     pen.setStyle(Qt::SolidLine);
0025     pen.setWidthF(state->getLineWidth());
0026 
0027     GfxRGB rgb;
0028     state->getStrokeRGB(&rgb);
0029     QColor c;
0030     c.setRgbF(colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b), 1.0f);
0031     pen.setColor(c);
0032 
0033     switch (state->getLineCap()) {
0034         case 0: pen.setCapStyle(Qt::FlatCap); break;
0035         case 1: pen.setCapStyle(Qt::RoundCap); break;
0036         case 2: pen.setCapStyle(Qt::SquareCap); break;
0037     }
0038 
0039     switch (state->getLineJoin()) {
0040         case 0: pen.setJoinStyle(Qt::SvgMiterJoin); break;
0041         case 1: pen.setJoinStyle(Qt::RoundJoin); break;
0042         case 2: pen.setJoinStyle(Qt::BevelJoin); break;
0043     }
0044 
0045     pen.setMiterLimit(state->getMiterLimit());
0046 
0047     return pen;
0048 }
0049 
0050 QBrush PopplerUtils::currentBrush(GfxState* state)
0051 {
0052     QBrush brush;
0053     brush.setStyle(Qt::SolidPattern);
0054 
0055     GfxRGB rgb;
0056     state->getFillRGB(&rgb);
0057     QColor c;
0058     c.setRgbF(colToDbl(rgb.r), colToDbl(rgb.g), colToDbl(rgb.b), 1.0f);
0059     brush.setColor(c);
0060 
0061     return brush;
0062 }
0063 
0064 QTransform KItinerary::PopplerUtils::currentTransform(GfxState *state)
0065 {
0066     const auto ctm = state->getCTM();
0067     return QTransform(ctm[0], ctm[1], ctm[2], ctm[3], ctm[4], ctm[5]);
0068 }
0069 
0070 QPainterPath PopplerUtils::convertPath(const GfxPath *path, Qt::FillRule fillRule)
0071 {
0072     QPainterPath qpp;
0073     qpp.setFillRule(fillRule);
0074 
0075     for (auto i = 0; i < path->getNumSubpaths(); ++i) {
0076 #if KPOPPLER_VERSION >= QT_VERSION_CHECK(0, 83, 0)
0077         const auto subpath = path->getSubpath(i);
0078 #else
0079         const auto subpath = const_cast<GfxPath*>(path)->getSubpath(i);
0080 #endif
0081         if (subpath->getNumPoints() > 0) {
0082             qpp.moveTo(subpath->getX(0), subpath->getY(0));
0083             for (auto j = 1;j < subpath->getNumPoints();) {
0084                 if (subpath->getCurve(j)) {
0085                     qpp.cubicTo(subpath->getX(j),   subpath->getY(j), subpath->getX(j+1), subpath->getY(j+1), subpath->getX(j+2), subpath->getY(j+2));
0086                     j += 3;
0087                 } else {
0088                     qpp.lineTo(subpath->getX(j), subpath->getY(j));
0089                     ++j;
0090                 }
0091             }
0092             if (subpath->isClosed()) {
0093                 qpp.closeSubpath();
0094             }
0095         }
0096     }
0097     return qpp;
0098 }