File: KoText.cpp

package info (click to toggle)
calligra 1%3A25.04.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 309,164 kB
  • sloc: cpp: 667,890; xml: 126,105; perl: 2,724; python: 2,497; yacc: 1,817; ansic: 1,326; sh: 1,223; lex: 1,107; javascript: 495; makefile: 24
file content (183 lines) | stat: -rw-r--r-- 5,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
 * SPDX-FileCopyrightText: 2008 Girish Ramakrishnan <girish@forwardbias.in>
 * SPDX-FileCopyrightText: 2011 Pierre Ducroquet <pinaraf@pinaraf.info>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */
#include "KoText.h"

#include <KoUnit.h>

#include <KLocalizedString>

using namespace KoText;

QStringList KoText::underlineTypeList()
{
    QStringList lst;
    lst << i18nc("Underline Style", "None");
    lst << i18nc("Underline Style", "Single");
    lst << i18nc("Underline Style", "Double");
    return lst;
}

QStringList KoText::underlineStyleList()
{
    QStringList lst;
    lst << "_________"; // solid
    lst << "___ ___ __"; // dash
    lst << "_ _ _ _ _ _"; // dot
    lst << "___ _ ___ _"; // dash_dot
    lst << "___ _ _ ___"; // dash_dot_dot
    lst << "~~~~~~~"; // wavy lines
    return lst;
}

KoText::Tab::Tab()
    : position(0.)
    , type(QTextOption::LeftTab)
    , leaderType(KoCharacterStyle::NoLineType)
    , leaderStyle(KoCharacterStyle::NoLineStyle)
    , leaderWeight(KoCharacterStyle::AutoLineWeight)
    , leaderWidth(0)
{
}

bool KoText::Tab::operator==(const Tab &other) const
{
    return other.position == position && other.type == type && other.delimiter == delimiter && other.leaderStyle == leaderStyle
        && other.leaderColor == leaderColor && other.leaderText == leaderText;
}

Qt::Alignment KoText::alignmentFromString(const QString &align)
{
    Qt::Alignment alignment = Qt::AlignLeft;
    if (align == "left")
        alignment = Qt::AlignLeft | Qt::AlignAbsolute;
    else if (align == "right")
        alignment = Qt::AlignRight | Qt::AlignAbsolute;
    else if (align == "start")
        alignment = Qt::AlignLeading;
    else if (align == "end")
        alignment = Qt::AlignTrailing;
    else if (align == "center")
        alignment = Qt::AlignHCenter;
    else if (align == "justify")
        alignment = Qt::AlignJustify;
    else if (align == "margins") // in tables this is effectively the same as justify
        alignment = Qt::AlignJustify;
    return alignment;
}

QString KoText::alignmentToString(Qt::Alignment alignment)
{
    QString align;

    alignment &= Qt::AlignHorizontal_Mask;
    if (alignment == (Qt::AlignLeft | Qt::AlignAbsolute))
        align = "left";
    else if (alignment == (Qt::AlignRight | Qt::AlignAbsolute))
        align = "right";
    else if (alignment == Qt::AlignLeading)
        align = "start";
    else if (alignment == Qt::AlignTrailing)
        align = "end";
    else if (alignment == Qt::AlignHCenter)
        align = "center";
    else if (alignment == Qt::AlignJustify)
        align = "justify";
    return align;
}

Qt::Alignment KoText::valignmentFromString(const QString &align)
{
    Qt::Alignment alignment = Qt::AlignTop;
    if (align == "top")
        alignment = Qt::AlignTop;
    else if (align == "middle")
        alignment = Qt::AlignVCenter;
    else if (align == "bottom")
        alignment = Qt::AlignBottom;
    return alignment;
}

QString KoText::valignmentToString(Qt::Alignment alignment)
{
    QString align;
    alignment &= Qt::AlignVertical_Mask;
    if (alignment == (Qt::AlignTop))
        align = "top";
    else if (alignment == Qt::AlignVCenter)
        align = "middle";
    else if (alignment == Qt::AlignBottom)
        align = "bottom";
    else
        align = "automatic";
    return align;
}

KoText::Direction KoText::directionFromString(const QString &writingMode)
{
    // LTR is lr-tb. RTL is rl-tb
    if (writingMode == "lr" || writingMode == "lr-tb")
        return KoText::LeftRightTopBottom;
    if (writingMode == "rl" || writingMode == "rl-tb")
        return KoText::RightLeftTopBottom;
    if (writingMode == "tb" || writingMode == "tb-rl")
        return KoText::TopBottomRightLeft;
    if (writingMode == "tb-lr")
        return KoText::TopBottomLeftRight;
    if (writingMode == "page")
        return KoText::InheritDirection;
    return KoText::AutoDirection;
}

QString KoText::directionToString(KoText::Direction direction)
{
    if (direction == KoText::LeftRightTopBottom)
        return "lr";
    if (direction == KoText::RightLeftTopBottom)
        return "rl";
    if (direction == KoText::TopBottomRightLeft)
        return "tb-rl";
    if (direction == KoText::TopBottomLeftRight)
        return "tb-lr";
    if (direction == KoText::InheritDirection)
        return "page";

    return "auto";
}

KoText::KoTextBreakProperty KoText::textBreakFromString(const QString &textBreak)
{
    if (textBreak == "page")
        return KoText::PageBreak;
    if (textBreak == "column")
        return KoText::ColumnBreak;
    return KoText::NoBreak;
}

QString KoText::textBreakToString(KoText::KoTextBreakProperty textBreak)
{
    if (textBreak == KoText::PageBreak)
        return "page";
    if (textBreak == KoText::ColumnBreak)
        return "column";
    return "auto";
}

QTextLength KoText::parseLength(const QString &length)
{
    if (length.contains('%')) {
        QString lengthValue = length.left(length.indexOf('%'));
        bool ok = false;
        qreal realLength = lengthValue.toDouble(&ok);
        if (ok)
            return QTextLength(QTextLength::PercentageLength, realLength);
        else
            return QTextLength(QTextLength::PercentageLength, 0);
    } else {
        return QTextLength(QTextLength::FixedLength, KoUnit::parseValue(length));
    }
}