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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2009 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 #include "ArtisticTextRange.h"
0022 #include <QDebug>
0023 
0024 ArtisticTextRange::ArtisticTextRange(const QString &text, const QFont &font)
0025     : m_text(text), m_font(font), m_letterSpacing(0.0), m_wordSpacing(0.0)
0026     , m_baselineShift(None), m_baselineShiftValue(0.0)
0027 {
0028 }
0029 
0030 ArtisticTextRange::~ArtisticTextRange()
0031 {
0032 }
0033 
0034 void ArtisticTextRange::setText(const QString &text)
0035 {
0036     m_text = text;
0037 }
0038 
0039 QString ArtisticTextRange::text() const
0040 {
0041     return m_text;
0042 }
0043 
0044 void ArtisticTextRange::insertText(int charIndex, const QString &text)
0045 {
0046     m_text.insert(charIndex, text);
0047 }
0048 
0049 void ArtisticTextRange::appendText(const QString &text)
0050 {
0051     m_text += text;
0052 }
0053 
0054 void ArtisticTextRange::setFont( const QFont & font )
0055 {
0056     if( m_font == font )
0057         return;
0058 
0059     m_font = font;
0060 }
0061 
0062 QFont ArtisticTextRange::font() const
0063 {
0064     return m_font;
0065 }
0066 
0067 ArtisticTextRange ArtisticTextRange::extract(int from, int count)
0068 {
0069     // copy text and font
0070     ArtisticTextRange extracted(m_text.mid(from, count), m_font);
0071     // copy corresponding character transformations
0072     if (from < m_xOffsets.count())
0073         extracted.setXOffsets(m_xOffsets.mid(from, count), m_xOffsetType);
0074     if (from < m_yOffsets.count())
0075         extracted.setYOffsets(m_yOffsets.mid(from, count), m_yOffsetType);
0076     if (from < m_rotations.count())
0077         extracted.setRotations(m_rotations.mid(from, count));
0078 
0079     extracted.setLetterSpacing(m_letterSpacing);
0080     extracted.setWordSpacing(m_wordSpacing);
0081     extracted.setBaselineShift(m_baselineShift, m_baselineShiftValue);
0082 
0083     // remove text
0084     m_text.remove(from, count < 0 ? m_text.length()-from : count);
0085     // remove character transformations
0086     m_xOffsets = m_xOffsets.mid(0, from);
0087     m_yOffsets = m_yOffsets.mid(0, from);
0088     m_rotations = m_rotations.mid(0, from);
0089 
0090     return extracted;
0091 }
0092 
0093 bool ArtisticTextRange::hasEqualStyle(const ArtisticTextRange &other) const
0094 {
0095     return m_font == other.m_font;
0096 }
0097 
0098 void ArtisticTextRange::setXOffsets(const QList<qreal> &offsets, OffsetType type)
0099 {
0100     m_xOffsets = offsets;
0101     m_xOffsetType = type;
0102 }
0103 
0104 void ArtisticTextRange::setYOffsets(const QList<qreal> &offsets, OffsetType type)
0105 {
0106     m_yOffsets = offsets;
0107     m_yOffsetType = type;
0108 }
0109 
0110 qreal ArtisticTextRange::xOffset(int charIndex) const
0111 {
0112     return m_xOffsets.value(charIndex);
0113 }
0114 
0115 qreal ArtisticTextRange::yOffset(int charIndex) const
0116 {
0117     return m_yOffsets.value(charIndex);
0118 }
0119 
0120 bool ArtisticTextRange::hasXOffset(int charIndex) const
0121 {
0122     return charIndex >= 0 && charIndex < m_xOffsets.count();
0123 }
0124 
0125 bool ArtisticTextRange::hasYOffset(int charIndex) const
0126 {
0127     return charIndex >= 0 && charIndex < m_yOffsets.count();
0128 }
0129 
0130 bool ArtisticTextRange::hasXOffsets() const
0131 {
0132     return !m_xOffsets.isEmpty();
0133 }
0134 
0135 bool ArtisticTextRange::hasYOffsets() const
0136 {
0137     return !m_yOffsets.isEmpty();
0138 }
0139 
0140 ArtisticTextRange::OffsetType ArtisticTextRange::xOffsetType() const
0141 {
0142     return m_xOffsetType;
0143 }
0144 
0145 ArtisticTextRange::OffsetType ArtisticTextRange::yOffsetType() const
0146 {
0147     return m_yOffsetType;
0148 }
0149 
0150 void ArtisticTextRange::setRotations(const QList<qreal> &rotations)
0151 {
0152     m_rotations = rotations;
0153 }
0154 
0155 bool ArtisticTextRange::hasRotation(int charIndex) const
0156 {
0157     return charIndex >= 0 && charIndex < m_rotations.count();
0158 }
0159 
0160 bool ArtisticTextRange::hasRotations() const
0161 {
0162     return !m_rotations.isEmpty();
0163 }
0164 
0165 qreal ArtisticTextRange::rotation(int charIndex) const
0166 {
0167     return m_rotations.value(charIndex);
0168 }
0169 
0170 void ArtisticTextRange::setLetterSpacing(qreal letterSpacing)
0171 {
0172     m_letterSpacing = letterSpacing;
0173 }
0174 
0175 qreal ArtisticTextRange::letterSpacing() const
0176 {
0177     return m_letterSpacing;
0178 }
0179 
0180 void ArtisticTextRange::setWordSpacing(qreal wordSpacing)
0181 {
0182     m_wordSpacing = wordSpacing;
0183 }
0184 
0185 qreal ArtisticTextRange::wordSpacing() const
0186 {
0187     return m_wordSpacing;
0188 }
0189 
0190 ArtisticTextRange::BaselineShift ArtisticTextRange::baselineShift() const
0191 {
0192     return m_baselineShift;
0193 }
0194 
0195 qreal ArtisticTextRange::baselineShiftValue() const
0196 {
0197     return m_baselineShiftValue;
0198 }
0199 
0200 void ArtisticTextRange::setBaselineShift(BaselineShift mode, qreal value)
0201 {
0202     m_baselineShift = mode;
0203     m_baselineShiftValue = value;
0204 }
0205 
0206 qreal ArtisticTextRange::subAndSuperScriptSizeFactor()
0207 {
0208     return 0.58; // taken from wikipedia
0209 }
0210 
0211 void ArtisticTextRange::printDebug() const
0212 {
0213     qDebug() << "text:" << m_text;
0214     qDebug() << "font:" << m_font;
0215     switch(m_xOffsetType) {
0216     case AbsoluteOffset:
0217         qDebug() << "x:" << m_xOffsets;
0218         break;
0219     case RelativeOffset:
0220         qDebug() << "dx:" << m_xOffsets;
0221         break;
0222     }
0223     switch(m_yOffsetType) {
0224     case AbsoluteOffset:
0225         qDebug() << "y:" << m_yOffsets;
0226         break;
0227     case RelativeOffset:
0228         qDebug() << "dy:" << m_yOffsets;
0229         break;
0230     }
0231     qDebug() << "rotate:" << m_rotations;
0232 }