forked from fedetft/mxgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextbox.cpp
204 lines (188 loc) · 8.5 KB
/
textbox.cpp
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
/***************************************************************************
* Copyright (C) 2022,2024 by Daniele Cattaneo *
* *
* 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. *
* *
* 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. *
* *
* As a special exception, if other files instantiate templates or use *
* macros or inline functions from this file, or you compile this file *
* and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with the GNU General *
* Public License. This exception does not invalidate any other reasons *
* why a work based on this file might be covered by the GNU General *
* Public License. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include "textbox.h"
#include "misc_inst.h"
#include <utility>
#include <cctype>
#include "../miosix-kernel/miosix/util/unicode.h"
using namespace mxgui;
typedef short (*GlyphWidthFP)(const Font& f, char32_t c);
static short getFWFGlyphWidth(const Font& f, char32_t c)
{
return f.getWidth();
}
static short getVWFGlyphWidth(const Font& f, char32_t c)
{
if(f.isInRange(c))
return f.getWidths()[f.getVirtualCodepoint(c)];
// width of fallback glyph
return f.getWidths()[f.getNumGlyphs()-1];
}
static inline std::pair<int, short> computeLineEnd_charWrap(const Font& font, GlyphWidthFP getGlyphWidth, const char *p, short maxWidth)
{
short lineWidth=0;
int i;
char32_t c;
for(i=0; (c=miosix::Unicode::nextUtf8(p))!='\0'; i++)
{
if(c=='\n') { i++; break; }
short width=getGlyphWidth(font, c);
if (lineWidth+width>maxWidth) break;
lineWidth+=width;
}
return std::make_pair(i, lineWidth);
}
static inline std::pair<int, short> computeLineEnd_wordWrap(const Font& font, GlyphWidthFP getGlyphWidth, const char *p, short maxWidth)
{
const short spaceWidth=getGlyphWidth(font, ' ');
short lineWidth=0;
int i=0,j;
bool firstWord=true;
short lastTrailingSpaceWidth=0;
const char *prv=p;
char32_t c;
while((c=miosix::Unicode::nextUtf8(p))!='\0')
{
if(c=='\n') { i++; break; }
short wordWidth=0;
j=i;
while(c!='\0' && c!=' ' && c!='\n')
{
wordWidth+=getGlyphWidth(font, c);
prv=p;
c=miosix::Unicode::nextUtf8(p);
j++;
}
p=prv;
if(lineWidth+lastTrailingSpaceWidth+wordWidth>maxWidth) break;
firstWord=false;
lineWidth+=wordWidth+lastTrailingSpaceWidth;
lastTrailingSpaceWidth=0;
i=j;
while(miosix::Unicode::nextUtf8(p)==' ')
{
lastTrailingSpaceWidth+=spaceWidth;
i++;
// with nextUtf8 we actually exit the loop with
// the iterator positioned one postion after the
// necessary, so save previous iterator
prv=p;
}
p=prv;
if(lineWidth+lastTrailingSpaceWidth>maxWidth) break;
}
if(firstWord && i==0) return computeLineEnd_charWrap(font, getGlyphWidth, p, maxWidth);
return std::make_pair(i, lineWidth);
}
int TextBox::draw(DrawingContext& dc, Point p0, Point p1,
const char *str, unsigned int options,
unsigned short topMargin, unsigned short leftMargin,
unsigned short rightMargin, unsigned short bottomMargin,
int scrollY)
{
unsigned short left=p0.x(), top=p0.y();
unsigned short right=p1.x(), btm=p1.y();
const Color bgColor = dc.getBackground();
if (topMargin>0) dc.clear(Point(left,top),Point(right,top+topMargin-1), bgColor);
top+=topMargin;
if (leftMargin>0) dc.clear(Point(left,top), Point(left+leftMargin-1,btm), bgColor);
left+=leftMargin;
if (rightMargin>0) dc.clear(Point(right-rightMargin+1,top), Point(right,btm), bgColor);
right-=rightMargin;
if (bottomMargin>0) dc.clear(Point(left,btm-bottomMargin+1), Point(right,btm), bgColor);
btm-=bottomMargin;
return TextBox::draw(dc, Point(left,top), Point(right,btm), str, options, scrollY);
}
int TextBox::draw(DrawingContext& dc, Point p0, Point p1,
const char *str, unsigned int options, int scrollY)
{
int left=p0.x(), top=p0.y(), right=p1.x(), btm=p1.y();
const Font font = dc.getFont();
const Color bgColor = dc.getBackground();
const int lineHeight = font.getHeight();
const bool withBG = (options & BackgroundMask)==BoxBackground;
const bool withClip = (options & PartialLinesMask)==ClipPartialLines;
GlyphWidthFP getGlyphWidth;
if (font.isFixedWidth()) getGlyphWidth=getFWFGlyphWidth;
else getGlyphWidth=getVWFGlyphWidth;
int lineTop=top-scrollY;
if (withBG && lineTop>top) dc.clear(Point(left,top), Point(right,std::min(lineTop-1,btm)), bgColor);
const char *p=str, *prv=p;
char32_t c=miosix::Unicode::nextUtf8(p);
p=prv;
int stopY = withClip ? btm : btm-lineHeight;
while(c!=0 && lineTop<=stopY)
{
std::pair<int, short> end;
if ((options&WrapMask)==WordWrap) end=computeLineEnd_wordWrap(font, getGlyphWidth, p, right-left+1);
else end = computeLineEnd_charWrap(font, getGlyphWidth, p, right-left+1);
// printf("nchar=%d lineWidth=%d\n", end.first, end.second);
const int lineBottom=std::min(lineTop+lineHeight, btm+1);
if(!withClip && lineTop<top && lineBottom>top)
{
if (withBG) dc.clear(Point(left,top), Point(right,lineBottom-1), bgColor);
}
else if(lineBottom>lineTop && lineBottom>top)
{
int realLineTop = std::max(lineTop,top);
if ((options&AlignmentMask) == LeftAlignment)
{
const short textRight=left+end.second-1;
dc.clippedWrite(Point(left,lineTop), Point(left,realLineTop), Point(textRight,lineBottom-1), p);
if (withBG && textRight+1<=right) dc.clear(Point(textRight+1,realLineTop), Point(right,lineBottom-1), bgColor);
}
if ((options&AlignmentMask) == CenterAlignment)
{
const short leftPadding=((right-left)-end.second)/2;
const short textLeft=left+leftPadding;
const short rightBoxLeft=textLeft+end.second;
if (withBG && leftPadding>0) dc.clear(Point(left,realLineTop), Point(textLeft-1,lineBottom-1), bgColor);
dc.clippedWrite(Point(textLeft,lineTop), Point(textLeft,realLineTop), Point(rightBoxLeft-1,lineBottom-1), p);
if (withBG && rightBoxLeft<=right) dc.clear(Point(rightBoxLeft,realLineTop), Point(right,lineBottom-1), bgColor);
}
if ((options&AlignmentMask) == RightAlignment)
{
const short textLeft=right-end.second+1;
if (withBG && textLeft>left) dc.clear(Point(left,realLineTop), Point(textLeft-1,lineBottom-1), bgColor);
dc.clippedWrite(Point(textLeft,lineTop), Point(textLeft,realLineTop), Point(right,lineBottom-1), p);
}
}
lineTop=lineBottom;
for(int i=0; i<end.first+1; i++)
{
prv=p;
c=miosix::Unicode::nextUtf8(p);
}
p=prv;
}
if (withBG && lineTop<=btm) dc.clear(Point(left,std::max(top,lineTop)), Point(right,btm), bgColor);
// compute offset inside str
int offset=0;
char32_t c2;
while((c2=miosix::Unicode::nextUtf8(str))==c) offset++;
return offset;
}