File indexing completed on 2025-02-02 04:14:57

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2009 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef SVGUTIL_H
0008 #define SVGUTIL_H
0009 
0010 #include "kritaflake_export.h"
0011 #include <QRectF>
0012 
0013 class QString;
0014 class QTransform;
0015 class QStringList;
0016 class KoXmlWriter;
0017 
0018 #include <QDomDocument>
0019 
0020 class SvgGraphicsContext;
0021 
0022 class KRITAFLAKE_EXPORT SvgUtil
0023 {
0024 public:
0025 
0026     // remove later! pixels *are* user coordinates
0027     static double fromUserSpace(double value);
0028     static double toUserSpace(double value);
0029 
0030     static double ptToPx(SvgGraphicsContext *gc, double value);
0031 
0032     /// Converts given point from points to userspace units.
0033     static QPointF toUserSpace(const QPointF &point);
0034 
0035     /// Converts given rectangle from points to userspace units.
0036     static QRectF toUserSpace(const QRectF &rect);
0037 
0038     /// Converts given rectangle from points to userspace units.
0039     static QSizeF toUserSpace(const QSizeF &size);
0040 
0041     /**
0042      * Parses the given float percentage.
0043      * @param value the input number containing float percentage (0..1)
0044      * @return the percentage string (0%..100%), not normalized
0045      */
0046     static QString toPercentage(qreal value);
0047 
0048     /**
0049      * Parses the given string containing a percentage.
0050      * @param s the input string containing the percentage, float (0..1) or integer (0%..100%)
0051      * @param ok optional failure indicator
0052      * @return the float percentage (0..1), not normalized
0053      */
0054     static double fromPercentage(QString s, bool *ok=nullptr);
0055 
0056     /**
0057      * Converts position from objectBoundingBox units to userSpace units.
0058      */
0059     static QPointF objectToUserSpace(const QPointF &position, const QRectF &objectBound);
0060 
0061     /**
0062      * Converts size from objectBoundingBox units to userSpace units.
0063      */
0064     static QSizeF objectToUserSpace(const QSizeF &size, const QRectF &objectBound);
0065 
0066     /**
0067      * Converts position from userSpace units to objectBoundingBox units.
0068      */
0069     static QPointF userSpaceToObject(const QPointF &position, const QRectF &objectBound);
0070 
0071     /**
0072      * Converts size from userSpace units to objectBoundingBox units.
0073      */
0074     static QSizeF userSpaceToObject(const QSizeF &size, const QRectF &objectBound);
0075 
0076     /// Converts specified transformation to a string
0077     static QString transformToString(const QTransform &transform);
0078 
0079     /// Writes a \p transform as an attribute \p name iff the transform is not empty
0080     static void writeTransformAttributeLazy(const QString &name, const QTransform &transform, KoXmlWriter &shapeWriter);
0081 
0082     /// Parses a viewbox attribute into an rectangle
0083     static bool parseViewBox(const QDomElement &e, const QRectF &elementBounds, QRectF *_viewRect, QTransform *_viewTransform);
0084 
0085     struct PreserveAspectRatioParser;
0086     static void parseAspectRatio(const PreserveAspectRatioParser &p, const QRectF &elementBounds, const QRectF &viewRect, QTransform *_viewTransform);
0087 
0088     /// Parses a length attribute
0089     static qreal parseUnit(SvgGraphicsContext *gc, const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF());
0090 
0091     /// parses a length attribute in x-direction
0092     static qreal parseUnitX(SvgGraphicsContext *gc, const QString &unit);
0093 
0094     /// parses a length attribute in y-direction
0095     static qreal parseUnitY(SvgGraphicsContext *gc, const QString &unit);
0096 
0097     /// parses a length attribute in xy-direction
0098     static qreal parseUnitXY(SvgGraphicsContext *gc, const QString &unit);
0099 
0100     /// parses angle, result in *radians*!
0101     static qreal parseUnitAngular(SvgGraphicsContext *gc, const QString &unit);
0102 
0103     /// parses the number into parameter number
0104     static const char * parseNumber(const char *ptr, qreal &number);
0105 
0106     static qreal parseNumber(const QString &string);
0107 
0108     static QString mapExtendedShapeTag(const QString &tagName, const QDomElement &element);
0109 
0110     static QStringList simplifyList(const QString &str);
0111 
0112     struct KRITAFLAKE_EXPORT PreserveAspectRatioParser
0113     {
0114         PreserveAspectRatioParser(const QString &str);
0115 
0116         enum Alignment {
0117             Min,
0118             Middle,
0119             Max
0120         };
0121 
0122         bool defer = false;
0123         Qt::AspectRatioMode mode = Qt::IgnoreAspectRatio;
0124         Alignment xAlignment = Min;
0125         Alignment yAlignment = Min;
0126 
0127         QPointF rectAnchorPoint(const QRectF &rc) const;
0128 
0129         QString toString() const;
0130 
0131     private:
0132         Alignment alignmentFromString(const QString &str) const;
0133         QString alignmentToString(Alignment alignment) const;
0134         static qreal alignedValue(qreal min, qreal max, Alignment alignment);
0135     };
0136 };
0137 
0138 #endif // SVGUTIL_H