wxMaxima
Loading...
Searching...
No Matches
TextFormat.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2026 Gunter Königsmann <wxMaxima@physikbuch.de>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
55#ifndef TEXTFORMAT_H
56#define TEXTFORMAT_H
57
58#include <cstdint>
59#include <cstddef>
60#include <vector>
61#include <wx/string.h>
62
63class wxXmlNode;
64
65namespace TextFormat {
66
68using Format = std::uint8_t;
69
70enum : Format {
71 None = 0,
72 Bold = 1,
73 Italic = 2,
74 Underline = 4,
75 Strikethrough = 8,
79 Subscript = 32
80};
81
84constexpr Format WidthAffecting = Bold | Italic | Superscript | Subscript;
85
87constexpr std::size_t FontVariants = 16;
88
91constexpr std::size_t FontIndex(Format format) {
92 return static_cast<std::size_t>((format & (Bold | Italic)) |
93 ((format & (Superscript | Subscript)) >> 2));
94}
95
98
104constexpr Format WithFlag(Format format, Format flag) {
105 if (flag & VerticalPosition)
106 format &= static_cast<Format>(~VerticalPosition);
107 return static_cast<Format>(format | flag);
108}
109
111using Formats = std::vector<Format>;
112
114bool IsPlain(const Formats &formats);
115
117inline Format At(const Formats &formats, std::size_t pos) {
118 return (pos < formats.size()) ? formats[pos] : Format(None);
119}
120
127Format FormatForInsertionAt(const wxString &text, const Formats &formats,
128 std::size_t pos);
129
150Formats Reconcile(const wxString &oldText, const Formats &oldFormats,
151 const wxString &newText, bool hasPending = false,
152 std::size_t pendingPos = 0, Format pendingFormat = None);
153
155struct Run {
157 std::size_t start;
159 std::size_t end;
160 Format format;
161};
162
171std::vector<Run> Runs(const wxString &text, const Formats &formats,
172 std::size_t start, std::size_t end);
173
179wxString LineAttributes(const wxString &text, const Formats &formats,
180 std::size_t lineStart, std::size_t lineEnd);
181
193void ReadLine(const wxXmlNode *line, wxString &text, Formats &formats,
194 wxString *unknownAttributes = nullptr);
195
215void ReadHTML(const wxString &html, wxString &text, Formats &formats);
216
221std::vector<std::pair<std::size_t, std::size_t>> ParseRanges(const wxString &ranges);
222
226std::size_t CodePointsBefore(const wxString &text, std::size_t start, std::size_t index);
227
229std::size_t IndexOfCodePoint(const wxString &text, std::size_t start,
230 std::size_t codePoints);
231} // namespace TextFormat
232
233#endif // TEXTFORMAT_H
std::vector< Run > Runs(const wxString &text, const Formats &formats, std::size_t start, std::size_t end)
Splits [start, end) of text into runs of equally formatted characters.
Definition: TextFormat.cpp:314
std::size_t CodePointsBefore(const wxString &text, std::size_t start, std::size_t index)
How many Unicode code points text[0, index) holds - which differs from index where wxString stores UT...
Definition: TextFormat.cpp:289
std::size_t IndexOfCodePoint(const wxString &text, std::size_t start, std::size_t codePoints)
The wxString index of the codePoints'th code point after start.
Definition: TextFormat.cpp:301
bool IsPlain(const Formats &formats)
Does this formatting vector describe plain text?
Definition: TextFormat.cpp:201
void ReadLine(const wxXmlNode *line, wxString &text, Formats &formats, wxString *unknownAttributes)
Append the text and formatting of a <line> element.
Definition: TextFormat.cpp:398
wxString LineAttributes(const wxString &text, const Formats &formats, std::size_t lineStart, std::size_t lineEnd)
The attributes that store the formatting of [lineStart, lineEnd) of text.
Definition: TextFormat.cpp:330
std::vector< std::pair< std::size_t, std::size_t > > ParseRanges(const wxString &ranges)
Parse a range list like "0-4,10-12" (half-open, in code points).
Definition: TextFormat.cpp:360
void ReadHTML(const wxString &html, wxString &text, Formats &formats)
The text and formatting of an HTML fragment other programs put on the clipboard.
Definition: TextFormat.cpp:584
constexpr std::size_t FontVariants
How many different fonts the WidthAffecting flags can ask for.
Definition: TextFormat.h:87
std::vector< Format > Formats
One Format per character, indexed like the text it belongs to; empty = plain.
Definition: TextFormat.h:111
Format At(const Formats &formats, std::size_t pos)
The format of character pos, None past the end (or for plain text).
Definition: TextFormat.h:117
constexpr Format VerticalPosition
The flags that move a character up or down. A character is never both.
Definition: TextFormat.h:97
constexpr Format WidthAffecting
The flags that change a character's width, and therefore the font it is measured and drawn with....
Definition: TextFormat.h:84
constexpr std::size_t FontIndex(Format format)
A number below FontVariants that tells the fonts the WidthAffecting flags ask for apart.
Definition: TextFormat.h:91
std::uint8_t Format
The formatting of one character: a combination of the flags below.
Definition: TextFormat.h:68
@ Superscript
Smaller, and raised above the baseline.
Definition: TextFormat.h:77
@ Subscript
Smaller, and lowered below the baseline.
Definition: TextFormat.h:79
constexpr Format WithFlag(Format format, Format flag)
The format that results from switching flag on in format.
Definition: TextFormat.h:104
@ TextFormat
Bold, italic, underline and strikethrough for text cells.
A stretch of text whose characters all have the same format.
Definition: TextFormat.h:155
std::size_t end
The index after its last character.
Definition: TextFormat.h:159
std::size_t start
The index of its first character.
Definition: TextFormat.h:157