GlistEngine
Loading...
Searching...
No Matches
gGUIBBCodeParser.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2014 Nitra Games Ltd., Istanbul, Turkey *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice should not be *
13 * deleted from the source form of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29/*
30 * gGUIBBCodeParser.h
31 *
32 * Lightweight BBCode parser and rich-text layout helper for GlistEngine GUI controls.
33 */
34
35#ifndef UI_GGUIBBCODEPARSER_H_
36#define UI_GGUIBBCODEPARSER_H_
37
38#include <functional>
39#include <string>
40#include <vector>
41
43 float r = 1.0f;
44 float g = 1.0f;
45 float b = 1.0f;
46 float a = 1.0f;
47
48 bool operator==(const gGUIBBCodeColor& other) const {
49 return r == other.r && g == other.g && b == other.b && a == other.a;
50 }
51 bool operator!=(const gGUIBBCodeColor& other) const {
52 return !(*this == other);
53 }
54};
55
57 bool bold = false;
58 bool italic = false;
59 bool underline = false;
60 bool strikethrough = false;
61 bool code = false;
62 bool hasColor = false;
64 bool hasBgColor = false;
66 int fontSize = 0; // 0 = inherit base font size
67 bool hasUrl = false;
68 std::string url;
69 int linkId = 0;
70
71 bool operator==(const gGUIBBCodeStyle& other) const {
72 return bold == other.bold &&
73 italic == other.italic &&
74 underline == other.underline &&
76 code == other.code &&
77 hasColor == other.hasColor &&
78 (!hasColor || color == other.color) &&
79 hasBgColor == other.hasBgColor &&
80 (!hasBgColor || bgColor == other.bgColor) &&
81 fontSize == other.fontSize &&
82 hasUrl == other.hasUrl &&
83 linkId == other.linkId &&
84 (!hasUrl || url == other.url);
85 }
86 bool operator!=(const gGUIBBCodeStyle& other) const {
87 return !(*this == other);
88 }
89};
90
92 std::string text;
94 int rawStartByte = 0;
95 int rawEndByte = 0;
97 int plainEndByte = 0;
98};
99
101 std::string plainText;
102 std::vector<gGUIBBCodeSpan> spans;
103 // Size == plainText.size() + 1; maps each plain-text UTF-8 byte index to a raw markup byte index.
104 std::vector<int> plainToRawByte;
105 // Byte lengths of each UTF-8 character in plainText.
106 std::vector<short> plainCharLengths;
107};
108
110 float ascent = 11.0f;
111 float descent = 3.0f;
112};
113
115 std::string text;
117 float x = 0.0f;
118 float width = 0.0f;
119 float ascent = 11.0f;
120 float descent = 3.0f;
125};
126
128 std::vector<gGUIBBCodeLayoutRun> runs;
129 float width = 0.0f;
130 float ascent = 11.0f;
131 float descent = 3.0f;
132 float lineHeight = 18.0f;
133 float topY = 0.0f;
134 float baselineY = 13.0f;
139 bool endedWithHardBreak = false;
140};
141
143 int charIndex = 0;
144 int byteOffset = 0;
145 int lineIndex = 0;
146 float cursorX = 0.0f;
147};
148
150 bool hit = false;
151 int linkId = 0;
152 std::string url;
153 std::string displayText;
154 int lineIndex = -1;
155 int runIndex = -1;
156};
157
159 float x = 0.0f;
160 float y = 0.0f;
161 float w = 0.0f;
162 float h = 0.0f;
163 int lineIndex = 0;
164};
165
186public:
187 static constexpr int MIN_FONT_SIZE = 6;
188 static constexpr int MAX_FONT_SIZE = 96;
189
190 using MeasureWidthFunc = std::function<float(const std::string& text, const gGUIBBCodeStyle& style)>;
191 using FontMetricsFunc = std::function<gGUIBBCodeFontMetrics(const gGUIBBCodeStyle& style)>;
192
193 static gGUIBBCodeDocument parse(const std::string& input, int baseFontSize = 11);
194
195 static bool parseColor(const std::string& value, gGUIBBCodeColor& outColor);
196 static bool parseSize(const std::string& value, int baseFontSize, int& outSize);
197 static bool parseUrl(const std::string& value, std::string& outUrl);
198
199 static std::vector<gGUIBBCodeLayoutLine> layout(
200 const gGUIBBCodeDocument& doc,
201 float maxWidth,
202 bool multiline,
203 float lineTopMargin,
204 const MeasureWidthFunc& measureWidth,
205 const FontMetricsFunc& fontMetrics);
206
208 const std::vector<gGUIBBCodeLayoutLine>& lines,
209 float localX,
210 float localY,
211 const MeasureWidthFunc& measureWidth,
212 float lineOffsetX = 0.0f);
213
215 const gGUIBBCodeLayoutLine& line,
216 int lineIndex,
217 float localX,
218 const MeasureWidthFunc& measureWidth);
219
221 const std::vector<gGUIBBCodeLayoutLine>& lines,
222 float localX,
223 float localY,
224 const std::vector<float>& lineOffsetsX = {});
225
226 static std::vector<gGUIBBCodeSelectionRect> computeSelectionRects(
227 const std::vector<gGUIBBCodeLayoutLine>& lines,
228 int selStartByte,
229 int selEndByte,
230 const MeasureWidthFunc& measureWidth);
231
232 static int utf8CharLength(const std::string& s, int bytePos);
233 static int countUtf8Chars(const std::string& s, int startByte, int endByte);
234 static int charIndexToByteOffset(const gGUIBBCodeDocument& doc, int charIndex);
235 static int byteOffsetToCharIndex(const gGUIBBCodeDocument& doc, int byteOffset);
236};
237
238#endif /* UI_GGUIBBCODEPARSER_H_ */
Definition gGUIBBCodeParser.h:185
static bool parseColor(const std::string &value, gGUIBBCodeColor &outColor)
static int charIndexToByteOffset(const gGUIBBCodeDocument &doc, int charIndex)
static gGUIBBCodeLinkHit hitTestLink(const std::vector< gGUIBBCodeLayoutLine > &lines, float localX, float localY, const std::vector< float > &lineOffsetsX={})
std::function< gGUIBBCodeFontMetrics(const gGUIBBCodeStyle &style)> FontMetricsFunc
Definition gGUIBBCodeParser.h:191
static gGUIBBCodeHitResult hitTest(const std::vector< gGUIBBCodeLayoutLine > &lines, float localX, float localY, const MeasureWidthFunc &measureWidth, float lineOffsetX=0.0f)
static int countUtf8Chars(const std::string &s, int startByte, int endByte)
static gGUIBBCodeHitResult hitTestLine(const gGUIBBCodeLayoutLine &line, int lineIndex, float localX, const MeasureWidthFunc &measureWidth)
static int utf8CharLength(const std::string &s, int bytePos)
static std::vector< gGUIBBCodeLayoutLine > layout(const gGUIBBCodeDocument &doc, float maxWidth, bool multiline, float lineTopMargin, const MeasureWidthFunc &measureWidth, const FontMetricsFunc &fontMetrics)
static int byteOffsetToCharIndex(const gGUIBBCodeDocument &doc, int byteOffset)
std::function< float(const std::string &text, const gGUIBBCodeStyle &style)> MeasureWidthFunc
Definition gGUIBBCodeParser.h:190
static bool parseSize(const std::string &value, int baseFontSize, int &outSize)
static constexpr int MIN_FONT_SIZE
Definition gGUIBBCodeParser.h:187
static std::vector< gGUIBBCodeSelectionRect > computeSelectionRects(const std::vector< gGUIBBCodeLayoutLine > &lines, int selStartByte, int selEndByte, const MeasureWidthFunc &measureWidth)
static bool parseUrl(const std::string &value, std::string &outUrl)
static constexpr int MAX_FONT_SIZE
Definition gGUIBBCodeParser.h:188
static gGUIBBCodeDocument parse(const std::string &input, int baseFontSize=11)
Definition gGUIBBCodeParser.h:42
float b
Definition gGUIBBCodeParser.h:45
bool operator!=(const gGUIBBCodeColor &other) const
Definition gGUIBBCodeParser.h:51
float g
Definition gGUIBBCodeParser.h:44
float a
Definition gGUIBBCodeParser.h:46
bool operator==(const gGUIBBCodeColor &other) const
Definition gGUIBBCodeParser.h:48
float r
Definition gGUIBBCodeParser.h:43
Definition gGUIBBCodeParser.h:100
std::string plainText
Definition gGUIBBCodeParser.h:101
std::vector< short > plainCharLengths
Definition gGUIBBCodeParser.h:106
std::vector< gGUIBBCodeSpan > spans
Definition gGUIBBCodeParser.h:102
std::vector< int > plainToRawByte
Definition gGUIBBCodeParser.h:104
Definition gGUIBBCodeParser.h:109
float ascent
Definition gGUIBBCodeParser.h:110
float descent
Definition gGUIBBCodeParser.h:111
Definition gGUIBBCodeParser.h:142
int byteOffset
Definition gGUIBBCodeParser.h:144
float cursorX
Definition gGUIBBCodeParser.h:146
int lineIndex
Definition gGUIBBCodeParser.h:145
int charIndex
Definition gGUIBBCodeParser.h:143
Definition gGUIBBCodeParser.h:127
float lineHeight
Definition gGUIBBCodeParser.h:132
int plainEndByte
Definition gGUIBBCodeParser.h:136
float ascent
Definition gGUIBBCodeParser.h:130
float baselineY
Definition gGUIBBCodeParser.h:134
int plainEndChar
Definition gGUIBBCodeParser.h:138
int plainStartChar
Definition gGUIBBCodeParser.h:137
float descent
Definition gGUIBBCodeParser.h:131
int plainStartByte
Definition gGUIBBCodeParser.h:135
float width
Definition gGUIBBCodeParser.h:129
bool endedWithHardBreak
Definition gGUIBBCodeParser.h:139
float topY
Definition gGUIBBCodeParser.h:133
std::vector< gGUIBBCodeLayoutRun > runs
Definition gGUIBBCodeParser.h:128
Definition gGUIBBCodeParser.h:114
std::string text
Definition gGUIBBCodeParser.h:115
gGUIBBCodeStyle style
Definition gGUIBBCodeParser.h:116
float x
Definition gGUIBBCodeParser.h:117
float width
Definition gGUIBBCodeParser.h:118
int plainEndChar
Definition gGUIBBCodeParser.h:124
int plainStartChar
Definition gGUIBBCodeParser.h:123
float ascent
Definition gGUIBBCodeParser.h:119
int plainStartByte
Definition gGUIBBCodeParser.h:121
float descent
Definition gGUIBBCodeParser.h:120
int plainEndByte
Definition gGUIBBCodeParser.h:122
Definition gGUIBBCodeParser.h:149
bool hit
Definition gGUIBBCodeParser.h:150
std::string displayText
Definition gGUIBBCodeParser.h:153
std::string url
Definition gGUIBBCodeParser.h:152
int linkId
Definition gGUIBBCodeParser.h:151
int runIndex
Definition gGUIBBCodeParser.h:155
int lineIndex
Definition gGUIBBCodeParser.h:154
Definition gGUIBBCodeParser.h:158
int lineIndex
Definition gGUIBBCodeParser.h:163
float y
Definition gGUIBBCodeParser.h:160
float h
Definition gGUIBBCodeParser.h:162
float w
Definition gGUIBBCodeParser.h:161
float x
Definition gGUIBBCodeParser.h:159
Definition gGUIBBCodeParser.h:91
gGUIBBCodeStyle style
Definition gGUIBBCodeParser.h:93
std::string text
Definition gGUIBBCodeParser.h:92
int rawStartByte
Definition gGUIBBCodeParser.h:94
int plainStartByte
Definition gGUIBBCodeParser.h:96
int plainEndByte
Definition gGUIBBCodeParser.h:97
int rawEndByte
Definition gGUIBBCodeParser.h:95
Definition gGUIBBCodeParser.h:56
gGUIBBCodeColor color
Definition gGUIBBCodeParser.h:63
bool italic
Definition gGUIBBCodeParser.h:58
bool code
Definition gGUIBBCodeParser.h:61
bool hasUrl
Definition gGUIBBCodeParser.h:67
bool hasColor
Definition gGUIBBCodeParser.h:62
bool strikethrough
Definition gGUIBBCodeParser.h:60
bool operator==(const gGUIBBCodeStyle &other) const
Definition gGUIBBCodeParser.h:71
int linkId
Definition gGUIBBCodeParser.h:69
bool bold
Definition gGUIBBCodeParser.h:57
std::string url
Definition gGUIBBCodeParser.h:68
bool operator!=(const gGUIBBCodeStyle &other) const
Definition gGUIBBCodeParser.h:86
bool hasBgColor
Definition gGUIBBCodeParser.h:64
gGUIBBCodeColor bgColor
Definition gGUIBBCodeParser.h:65
int fontSize
Definition gGUIBBCodeParser.h:66
bool underline
Definition gGUIBBCodeParser.h:59