1 /* This file is part of the KDE project
2 * Copyright (C) 2006 Thomas Zander <zander@kde.org>
3 * Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
4 * Copyright (C) 2011 Pierre Ducroquet <pinaraf@pinaraf.info>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 #include "KoText.h"
22
23 #include <KoUnit.h>
24
25 #include <klocalizedstring.h>
26
27 using namespace KoText;
28
underlineTypeList()29 QStringList KoText::underlineTypeList()
30 {
31 QStringList lst;
32 lst << i18nc("Underline Style", "None");
33 lst << i18nc("Underline Style", "Single");
34 lst << i18nc("Underline Style", "Double");
35 return lst;
36 }
37
underlineStyleList()38 QStringList KoText::underlineStyleList()
39 {
40 QStringList lst;
41 lst << "_________"; // solid
42 lst << "___ ___ __"; // dash
43 lst << "_ _ _ _ _ _"; // dot
44 lst << "___ _ ___ _"; // dash_dot
45 lst << "___ _ _ ___"; // dash_dot_dot
46 lst << "~~~~~~~"; // wavy lines
47 return lst;
48 }
49
Tab()50 KoText::Tab::Tab()
51 : position(0.),
52 type(QTextOption::LeftTab),
53 leaderType(KoCharacterStyle::NoLineType),
54 leaderStyle(KoCharacterStyle::NoLineStyle),
55 leaderWeight(KoCharacterStyle::AutoLineWeight),
56 leaderWidth(0)
57 {
58 }
59
operator ==(const Tab & other) const60 bool KoText::Tab::operator==(const Tab &other) const
61 {
62 return other.position == position &&
63 other.type == type &&
64 other.delimiter == delimiter &&
65 other.leaderStyle == leaderStyle &&
66 other.leaderColor == leaderColor &&
67 other.leaderText == leaderText ;
68 }
69
alignmentFromString(const QString & align)70 Qt::Alignment KoText::alignmentFromString(const QString &align)
71 {
72 Qt::Alignment alignment = Qt::AlignLeft;
73 if (align == "left")
74 alignment = Qt::AlignLeft | Qt::AlignAbsolute;
75 else if (align == "right")
76 alignment = Qt::AlignRight | Qt::AlignAbsolute;
77 else if (align == "start")
78 alignment = Qt::AlignLeading;
79 else if (align == "end")
80 alignment = Qt::AlignTrailing;
81 else if (align == "center")
82 alignment = Qt::AlignHCenter;
83 else if (align == "justify")
84 alignment = Qt::AlignJustify;
85 else if (align == "margins") // in tables this is effectively the same as justify
86 alignment = Qt::AlignJustify;
87 return alignment;
88 }
89
alignmentToString(Qt::Alignment alignment)90 QString KoText::alignmentToString(Qt::Alignment alignment)
91 {
92 QString align;
93
94 alignment &= Qt::AlignHorizontal_Mask;
95 if (alignment == (Qt::AlignLeft | Qt::AlignAbsolute))
96 align = "left";
97 else if (alignment == (Qt::AlignRight | Qt::AlignAbsolute))
98 align = "right";
99 else if (alignment == Qt::AlignLeading)
100 align = "start";
101 else if (alignment == Qt::AlignTrailing)
102 align = "end";
103 else if (alignment == Qt::AlignHCenter)
104 align = "center";
105 else if (alignment == Qt::AlignJustify)
106 align = "justify";
107 return align;
108 }
109
valignmentFromString(const QString & align)110 Qt::Alignment KoText::valignmentFromString(const QString &align)
111 {
112 Qt::Alignment alignment = Qt::AlignTop;
113 if (align == "top")
114 alignment = Qt::AlignTop;
115 else if (align == "middle")
116 alignment = Qt::AlignVCenter;
117 else if (align == "bottom")
118 alignment = Qt::AlignBottom;
119 return alignment;
120 }
121
valignmentToString(Qt::Alignment alignment)122 QString KoText::valignmentToString(Qt::Alignment alignment)
123 {
124 QString align;
125 alignment &= Qt::AlignVertical_Mask;
126 if (alignment == (Qt::AlignTop))
127 align = "top";
128 else if (alignment == Qt::AlignVCenter)
129 align = "middle";
130 else if (alignment == Qt::AlignBottom)
131 align = "bottom";
132 else
133 align = "automatic";
134 return align;
135 }
136
directionFromString(const QString & writingMode)137 KoText::Direction KoText::directionFromString(const QString &writingMode)
138 {
139 // LTR is lr-tb. RTL is rl-tb
140 if (writingMode == "lr" || writingMode == "lr-tb")
141 return KoText::LeftRightTopBottom;
142 if (writingMode == "rl" || writingMode == "rl-tb")
143 return KoText::RightLeftTopBottom;
144 if (writingMode == "tb" || writingMode == "tb-rl")
145 return KoText::TopBottomRightLeft;
146 if (writingMode == "tb-lr")
147 return KoText::TopBottomLeftRight;
148 if (writingMode == "page")
149 return KoText::InheritDirection;
150 return KoText::AutoDirection;
151 }
152
directionToString(KoText::Direction direction)153 QString KoText::directionToString(KoText::Direction direction)
154 {
155 if (direction == KoText::LeftRightTopBottom)
156 return "lr";
157 if (direction == KoText::RightLeftTopBottom)
158 return "rl";
159 if (direction == KoText::TopBottomRightLeft)
160 return "tb-rl";
161 if (direction == KoText::TopBottomLeftRight)
162 return "tb-lr";
163 if (direction == KoText::InheritDirection)
164 return "page";
165
166 return "auto";
167 }
168
textBreakFromString(const QString & textBreak)169 KoText::KoTextBreakProperty KoText::textBreakFromString(const QString& textBreak)
170 {
171 if (textBreak == "page")
172 return KoText::PageBreak;
173 if (textBreak == "column")
174 return KoText::ColumnBreak;
175 return KoText::NoBreak;
176 }
177
textBreakToString(KoText::KoTextBreakProperty textBreak)178 QString KoText::textBreakToString(KoText::KoTextBreakProperty textBreak)
179 {
180 if (textBreak == KoText::PageBreak)
181 return "page";
182 if (textBreak == KoText::ColumnBreak)
183 return "column";
184 return "auto";
185 }
186
parseLength(const QString & length)187 QTextLength KoText::parseLength(const QString &length)
188 {
189 if (length.contains('%'))
190 {
191 QString lengthValue = length.left(length.indexOf('%'));
192 bool ok = false;
193 qreal realLength = lengthValue.toDouble(&ok);
194 if (ok)
195 return QTextLength(QTextLength::PercentageLength, realLength);
196 else
197 return QTextLength(QTextLength::PercentageLength, 0);
198 }
199 else
200 {
201 return QTextLength(QTextLength::FixedLength, KoUnit::parseValue(length));
202 }
203 }
204
205