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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2008,2011 Jan Hambrecht <jaham@gmx.net>
0003  * Copyright (C) 2008 Rob Buis <buis@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #ifndef ARTISITICTEXTRANGE_H
0022 #define ARTISITICTEXTRANGE_H
0023 
0024 #include <QString>
0025 #include <QPointF>
0026 #include <QFont>
0027 
0028 /// Represents a range of characters with the same text properties
0029 class ArtisticTextRange
0030 {
0031 public:
0032     enum OffsetType {
0033         AbsoluteOffset,
0034         RelativeOffset
0035     };
0036 
0037     enum BaselineShift {
0038         None,     ///< no baseline shift
0039         Sub,      ///< subscript baseline shift
0040         Super,    ///< superscript baseline shift
0041         Percent,  ///< percentage baseline shift
0042         Length    ///< absolute baseline shift
0043     };
0044 
0045     ArtisticTextRange(const QString &text, const QFont &font);
0046     ~ArtisticTextRange();
0047 
0048     /// Returns the text content
0049     QString text() const;
0050 
0051     /// Sets the text to display
0052     void setText(const QString &text);
0053 
0054     /// Inserts new text at the given position
0055     void insertText(int charIndex, const QString &text);
0056 
0057     /// Appends text to the text range
0058     void appendText(const QString &text);
0059 
0060     /**
0061      * Sets the font used for drawing
0062      * Note that it is expected that the font has its point size set
0063      * in postscript points.
0064      */
0065     void setFont(const QFont &font);
0066 
0067     /// Returns the font
0068     QFont font() const;
0069 
0070     /// Extracts specified part of the text range
0071     ArtisticTextRange extract(int from, int count = -1);
0072 
0073     /// Checks if specified text range has the same style as this text range
0074     bool hasEqualStyle(const ArtisticTextRange &other) const;
0075 
0076     /// Sets individual character x-offsets
0077     void setXOffsets(const QList<qreal> &offsets, OffsetType type);
0078 
0079     /// Sets individual character y-offsets
0080     void setYOffsets(const QList<qreal> &offsets, OffsetType type);
0081 
0082     /// Returns the character x-offset for the specified character
0083     qreal xOffset(int charIndex) const;
0084 
0085     /// Returns the character y-offset for the specified character
0086     qreal yOffset(int charIndex) const;
0087 
0088     /// Checks if specified character has an x-offset value
0089     bool hasXOffset(int charIndex) const;
0090 
0091     /// Checks if specified character has an y-offset value
0092     bool hasYOffset(int charIndex) const;
0093 
0094     /// Checks if range has x-offsets
0095     bool hasXOffsets() const;
0096 
0097     /// Checks if range has y-offsets
0098     bool hasYOffsets() const;
0099 
0100     /// Returns the type of the x-offsets
0101     OffsetType xOffsetType() const;
0102 
0103     /// Returns the type of the y-offsets
0104     OffsetType yOffsetType() const;
0105 
0106     /// Sets individual character rotations
0107     void setRotations(const QList<qreal> &rotations);
0108 
0109     /// Checks if specified character has a rotation
0110     bool hasRotation(int charIndex) const;
0111 
0112     /// Checks if range has character rotations
0113     bool hasRotations() const;
0114 
0115     /// Returns the character rotation for the specified character
0116     qreal rotation(int charIndex) const;
0117 
0118     /// Sets additional spacing between characters
0119     void setLetterSpacing(qreal letterSpacing);
0120 
0121     /// Returns the letter spacing
0122     qreal letterSpacing() const;
0123 
0124     /// Sets additional spacing between words
0125     void setWordSpacing(qreal wordSpacing);
0126 
0127     /// Returns the word spacing
0128     qreal wordSpacing() const;
0129 
0130     /// Returns baseline shift mode
0131     BaselineShift baselineShift() const;
0132 
0133     /// Returns the optional baseline shift value
0134     qreal baselineShiftValue() const;
0135 
0136     /// Returns the normalized baseline shift value in point
0137     qreal baselineShiftValueNormalized() const;
0138 
0139     /// Sets baseline shift mode and optional value
0140     void setBaselineShift(BaselineShift mode, qreal value = 0.0);
0141 
0142     /// Returns the factor to calculate sub and super script font size
0143     static qreal subAndSuperScriptSizeFactor();
0144 
0145     /// Prints debug output
0146     void printDebug() const;
0147 
0148 private:
0149     QString m_text; ///< the text of the text range
0150     QFont m_font; ///< the font of the text range
0151     QList<qreal> m_xOffsets; ///< character x-offsets
0152     QList<qreal> m_yOffsets; ///< character y-offsets
0153     OffsetType m_xOffsetType; ///< character x-offset type
0154     OffsetType m_yOffsetType; ///< character y-offset type
0155     QList<qreal> m_rotations; ///< character rotations
0156     qreal m_letterSpacing; ///< additional inter character spacing
0157     qreal m_wordSpacing; ///< additional inter word spacing
0158     BaselineShift m_baselineShift; ///< baseline shift mode
0159     qreal m_baselineShiftValue; ///< optional baseline shift value
0160 };
0161 
0162 #endif // ARTISITICTEXTRANGE_H