File: format.cpp

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (268 lines) | stat: -rw-r--r-- 9,568 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Qt User Interface                                                     *
 *                                                                        *
 *  Copyright (c) 1999-2025, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This file is modified from the KDE syntax-highlighting framework,     *
 *  which is copyright (C) 2016 Volker Krause <vkrause@kde.org>           *
 *  and is distributed under the GNU Library General Public License as    *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  This program is free software; you can redistribute it and/or         *
 *  modify it under the terms of the GNU General Public License as        *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  As an exception, when this program is distributed through (i) the     *
 *  App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or     *
 *  (iii) Google Play by Google Inc., then that store may impose any      *
 *  digital rights management, device limits and/or redistribution        *
 *  restrictions that are required by its terms of service.               *
 *                                                                        *
 *  This program is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public License     *
 *  along with this program. If not, see <https://www.gnu.org/licenses/>. *
 *                                                                        *
 **************************************************************************/

#include "syntax/format.h"
#include "syntax/format_p.h"
#include "syntax/definition.h"
#include "syntax/definitionref.h"
#include "syntax/textstyledata_p.h"
#include "syntax/theme.h"
#include "syntax/themedata_p.h"

#include <cassert>

#include "utilities/stringutils.h"
#include "file/xml/xmlreader.h"

namespace regina::syntax {

static inline unsigned readColour(const std::string& str) {
    if (str.empty() || str.front() != '#')
        return 0;
    return static_cast<unsigned>(strtoul(str.c_str() + 1, 0, 16));
}

static Theme::TextStyle stringToDefaultFormat(const std::string& str)
{
    if (!regina::startsWith(str, "ds"))
        return Theme::Normal;

    // TODO: Make this log-time in the enumerator size.
    if (str == "dsNormal") return Theme::Normal;
    if (str == "dsKeyword") return Theme::Keyword;
    if (str == "dsFunction") return Theme::Function;
    if (str == "dsVariable") return Theme::Variable;
    if (str == "dsControlFlow") return Theme::ControlFlow;
    if (str == "dsOperator") return Theme::Operator;
    if (str == "dsBuiltIn") return Theme::BuiltIn;
    if (str == "dsExtension") return Theme::Extension;
    if (str == "dsPreprocessor") return Theme::Preprocessor;
    if (str == "dsAttribute") return Theme::Attribute;
    if (str == "dsChar") return Theme::Char;
    if (str == "dsSpecialChar") return Theme::SpecialChar;
    if (str == "dsString") return Theme::String;
    if (str == "dsVerbatimString") return Theme::VerbatimString;
    if (str == "dsSpecialString") return Theme::SpecialString;
    if (str == "dsImport") return Theme::Import;
    if (str == "dsDataType") return Theme::DataType;
    if (str == "dsDecVal") return Theme::DecVal;
    if (str == "dsBaseN") return Theme::BaseN;
    if (str == "dsFloat") return Theme::Float;
    if (str == "dsConstant") return Theme::Constant;
    if (str == "dsComment") return Theme::Comment;

    return Theme::Normal;
}

FormatPrivate::FormatPrivate()
    : defaultStyle(Theme::Normal)
    , id(0)
    , spellCheck(true)
{
}

FormatPrivate* FormatPrivate::get(const Format &format)
{
    return format.d.get();
}

Format::Format() : d(new FormatPrivate)
{
}

Format::Format(const Format &other) :
    d(other.d)
{
}

Format::~Format()
{
}

Format& Format::operator=(const Format& other)
{
    d = other.d;
    return *this;
}

bool Format::isValid() const
{
    return !d->name.empty();
}

const std::string& Format::name() const
{
    return d->name;
}

uint16_t Format::id() const
{
    return d->id;
}

bool Format::isDefaultTextStyle(const Theme &theme) const
{
    return (!hasTextColor(theme))
        && (!hasBackgroundColor(theme))
        && (selectedTextColor(theme) == theme.selectedTextColor(Theme::Normal))
        && (selectedBackgroundColor(theme) == theme.selectedBackgroundColor(Theme::Normal))
        && (isBold(theme) == theme.isBold(Theme::Normal))
        && (isItalic(theme) == theme.isItalic(Theme::Normal))
        && (isUnderline(theme) == theme.isUnderline(Theme::Normal))
        && (isStrikeThrough(theme) == theme.isStrikeThrough(Theme::Normal));
}

bool Format::hasTextColor(const Theme &theme) const
{
    return textColor(theme) != theme.textColor(Theme::Normal)
        && (d->style.textColor || theme.textColor(d->defaultStyle));
}

unsigned Format::textColor(const Theme &theme) const
{
    return d->style.textColor ? d->style.textColor : theme.textColor(d->defaultStyle);
}

unsigned Format::selectedTextColor(const Theme &theme) const
{
    return d->style.selectedTextColor ? d->style.selectedTextColor : theme.selectedTextColor(d->defaultStyle);
}

bool Format::hasBackgroundColor(const Theme &theme) const
{
    return backgroundColor(theme) != theme.backgroundColor(Theme::Normal)
         && (d->style.backgroundColor || theme.backgroundColor(d->defaultStyle));
}

unsigned Format::backgroundColor(const Theme &theme) const
{
    return d->style.backgroundColor ? d->style.backgroundColor : theme.backgroundColor(d->defaultStyle);
}

unsigned Format::selectedBackgroundColor(const Theme &theme) const
{
    return d->style.selectedBackgroundColor ? d->style.selectedBackgroundColor
                            : theme.selectedBackgroundColor(d->defaultStyle);
}

bool Format::isBold(const Theme &theme) const
{
    return d->style.hasBold ? d->style.bold : theme.isBold(d->defaultStyle);
}

bool Format::isItalic(const Theme &theme) const
{
    return d->style.hasItalic ? d->style.italic : theme.isItalic(d->defaultStyle);
}

bool Format::isUnderline(const Theme &theme) const
{
    return d->style.hasUnderline ? d->style.underline : theme.isUnderline(d->defaultStyle);
}

bool Format::isStrikeThrough(const Theme &theme) const
{
    return d->style.hasStrikeThrough ? d->style.strikeThrough : theme.isStrikeThrough(d->defaultStyle);
}

bool Format::spellCheck() const
{
    return d->spellCheck;
}


void FormatPrivate::load(xmlTextReaderPtr reader)
{
    name = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"name"));
    defaultStyle = stringToDefaultFormat(
        regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"defStyleNum")));

    std::string ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"color"));
    if (!ref.empty()) {
        style.textColor = readColour(ref);
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"selColor"));
    if (!ref.empty()) {
        style.selectedTextColor = readColour(ref);
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"backgroundColor"));
    if (!ref.empty()) {
        style.backgroundColor = readColour(ref);
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"selBackgroundColor"));
    if (!ref.empty()) {
        style.selectedBackgroundColor = readColour(ref);
    }

    bool b;
    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"italic"));
    if (!ref.empty()) {
        regina::valueOf(ref, b);
        style.hasItalic = true;
        style.italic = b;
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"bold"));
    if (!ref.empty()) {
        regina::valueOf(ref, b);
        style.hasBold = true;
        style.bold = b;
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"underline"));
    if (!ref.empty()) {
        regina::valueOf(ref, b);
        style.hasUnderline = true;
        style.underline = b;
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"strikeOut"));
    if (!ref.empty()) {
        regina::valueOf(ref, b);
        style.hasStrikeThrough = true;
        style.strikeThrough = b;
    }

    ref = regina::xml::xmlString(xmlTextReaderGetAttribute(reader, (const xmlChar*)"spellChecking"));
    if (!ref.empty()) {
        regina::valueOf(ref, spellCheck);
    }
}

} // namespace regina::syntax