File indexing completed on 2024-05-12 16:33:18

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #ifndef SVGTEXTHELPER_H
0021 #define SVGTEXTHELPER_H
0022 
0023 #include <QVector>
0024 #include <QString>
0025 #include <QList>
0026 #include <QPointF>
0027 
0028 typedef QList<qreal> CharTransforms;
0029 
0030 class KoXmlElement;
0031 class SvgGraphicsContext;
0032 
0033 class ArtisticTextLoadingContext
0034 {
0035 public:
0036     enum OffsetType {
0037         None,
0038         Absolute,
0039         Relative
0040     };
0041 
0042     ArtisticTextLoadingContext();
0043 
0044     static QString simplifyText(const QString &text, bool preserveWhiteSpace = false);
0045 
0046     /// Parses current character transforms (x,y,dx,dy,rotate)
0047     void parseCharacterTransforms(const KoXmlElement &element, SvgGraphicsContext *gc);
0048 
0049     /// Pushes the current character transforms to the stack
0050     void pushCharacterTransforms();
0051 
0052     /// Pops last character transforms from the stack
0053     void popCharacterTransforms();
0054 
0055     /// Checks current x-offset type
0056     OffsetType xOffsetType() const;
0057 
0058     /// Checks current y-offset type
0059     OffsetType yOffsetType() const;
0060 
0061     /// Returns x-offsets from stack
0062     CharTransforms xOffsets(int count);
0063 
0064     /// Returns y-offsets from stack
0065     CharTransforms yOffsets(int count);
0066 
0067     /// Returns rotations from stack
0068     CharTransforms rotations(int count);
0069 
0070     /// Returns the text position
0071     QPointF textPosition() const;
0072 
0073 private:
0074     void printDebug();
0075 
0076     struct CharTransformState {
0077         CharTransformState()
0078             : hasData(false), lastTransform(0.0)
0079         {
0080         }
0081 
0082         CharTransformState(const CharTransforms &initialData)
0083             : data(initialData), hasData(!initialData.isEmpty())
0084             , lastTransform(initialData.isEmpty() ? 0.0 : initialData.last())
0085         {
0086         }
0087 
0088         CharTransforms extract(int count)
0089         {
0090             const int copyCount = qMin(data.count(), count);
0091             CharTransforms extracted = data.mid(0, copyCount);
0092             data = data.mid(copyCount);
0093             return extracted;
0094         }
0095 
0096         CharTransforms data;
0097         bool hasData;
0098         qreal lastTransform;
0099     };
0100 
0101     typedef QList<CharTransformState> CharTransformStack;
0102 
0103     enum ValueType {
0104         Number,
0105         XLength,
0106         YLength
0107     };
0108 
0109     /// Parses offset values from the given string
0110     CharTransforms parseList(const QString &listString, SvgGraphicsContext *gc, ValueType type);
0111 
0112     /// Collects number of specified transforms values from the stack
0113     CharTransforms collectValues(int count, CharTransformState &current, CharTransformStack &stack);
0114 
0115     CharTransformState m_currentAbsolutePosX; ///< current absolute character x-positions
0116     CharTransformState m_currentAbsolutePosY; ///< current absolute character y-positions
0117     CharTransformState m_currentRelativePosX; ///< current relative character x-positions
0118     CharTransformState m_currentRelativePosY; ///< current relative character y-positions
0119     CharTransformState m_currentRotations;    ///< current character rotations
0120     CharTransformStack m_absolutePosX; ///< stack of absolute character x-positions
0121     CharTransformStack m_absolutePosY; ///< stack of absolute character y-positions
0122     CharTransformStack m_relativePosX; ///< stack of relative character x-positions
0123     CharTransformStack m_relativePosY; ///< stack of relative character y-positions
0124     CharTransformStack m_rotations;    ///< stack of character rotations
0125 
0126     QPointF m_textPosition;
0127 };
0128 
0129 #endif // SVGTEXTHELPER_H