-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd2dStaticText.pas
397 lines (356 loc) · 9.65 KB
/
d2dStaticText.pas
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//---------------------------------------------------------------------------
unit d2dStaticText;
interface
uses
d2dTypes,
d2dInterfaces,
d2dFormattedText,
d2dDocRoot;
type
Td2dStaticText = class(TInterfacedObject, Id2dTextAddingTool)
private
f_Align: Td2dTextAlignType;
f_AutoWidth: Boolean;
f_TextColor: Td2dColor;
f_Font: Id2dFont;
f_Formatter: Td2dFormatter;
f_Root: Td2dDocRoot;
f_Text: string;
f_LinkColor: Td2dColor;
f_LinkHColor: Td2dColor;
f_OnLinkClick: Td2dOnLinkClickEvent;
f_TextSource: Td2dTextSource;
f_X: Single;
f_Y: Single;
function pm_GetHeight: Single;
function pm_GetLineSpacing: Single;
function pm_GetParaSpacing: Single;
function pm_GetWidth: Single;
procedure pm_SetAlign(const Value: Td2dTextAlignType);
procedure pm_SetTextColor(const Value: Td2dColor);
procedure pm_SetFont(const Value: Id2dFont);
procedure pm_SetLineSpacing(const Value: Single);
procedure pm_SetParaSpacing(const Value: Single);
procedure pm_SetText(const Value: string);
procedure pm_SetWidth(const Value: Single);
procedure ApplyColors;
procedure ApplyFont;
procedure ReFormat;
procedure _ApplyChunkColors(const aChunk: Td2dCustomTextChunk);
procedure _ApplyFontToChunk(const aChunk: Td2dCustomTextChunk);
procedure _ApplySliceColors(const aSlice: Td2dCustomSlice);
//
procedure AddText(const aText: string);
procedure AddLink(const aText, aTarget: string);
procedure CheckAutowidth;
function pm_GetLinksEnabled: Boolean;
procedure pm_SetAutoWidth(const Value: Boolean);
procedure pm_SetLinkColor(const Value: Td2dColor);
procedure pm_SetLinkHColor(const Value: Td2dColor);
procedure pm_SetLinksEnabled(const Value: Boolean);
procedure pm_SetX(const Value: Single);
procedure pm_SetY(const Value: Single);
procedure RenewLinkState;
public
constructor Create(const aFont: Id2dFont; const aColor: Td2dColor);
destructor Destroy; override;
procedure EndText;
procedure Render;
procedure ProcessEvent(var theEvent: Td2dInputEvent);
procedure StartText;
property Align: Td2dTextAlignType read f_Align write pm_SetAlign;
property AutoWidth: Boolean read f_AutoWidth write pm_SetAutoWidth;
property TextColor: Td2dColor read f_TextColor write pm_SetTextColor;
property Font: Id2dFont read f_Font write pm_SetFont;
property Height: Single read pm_GetHeight;
property LineSpacing: Single read pm_GetLineSpacing write pm_SetLineSpacing;
property ParaSpacing: Single read pm_GetParaSpacing write pm_SetParaSpacing;
property Text: string read f_Text write pm_SetText;
property LinkColor: Td2dColor read f_LinkColor write pm_SetLinkColor;
property LinkHColor: Td2dColor read f_LinkHColor write pm_SetLinkHColor;
property LinksEnabled: Boolean read pm_GetLinksEnabled write pm_SetLinksEnabled;
property Width: Single read pm_GetWidth write pm_SetWidth;
property X: Single read f_X write pm_SetX;
property Y: Single read f_Y write pm_SetY;
property OnLinkClick: Td2dOnLinkClickEvent read f_OnLinkClick write f_OnLinkClick;
end;
implementation
uses
SysUtils,
d2dCore,
d2dUtils;
const
c_BadLinkColor = $FFB52929;
c_BadLinkHColor = $FFFF3A3A;
constructor Td2dStaticText.Create(const aFont: Id2dFont; const aColor: Td2dColor);
begin
inherited Create;
f_TextSource := Td2dTextSource.Create;
f_Root := Td2dDocRoot.Create(0);
f_Formatter := Td2dFormatter.Create(f_TextSource, f_Root);
f_TextColor := aColor;
f_LinkColor := $FF0019FF;
f_LinkHColor := $FF4F60FF;
f_Font := aFont;
f_AutoWidth := True;
end;
destructor Td2dStaticText.Destroy;
begin
FreeAndNil(f_Formatter);
FreeAndNil(f_TextSource);
FreeAndNil(f_Root);
inherited;
end;
procedure Td2dStaticText.AddLink(const aText, aTarget: string);
begin
f_Text := f_Text + aText;
if aTarget <> '' then
f_TextSource.AddLink(aText, aTarget, f_Font, f_TextColor, f_LinkColor, f_LinkHColor, f_Align)
else
f_TextSource.AddLink(aText, aTarget, f_Font, f_TextColor, c_BadLinkColor, c_BadLinkHColor, f_Align)
end;
procedure Td2dStaticText.AddText(const aText: string);
begin
f_Text := f_Text + aText;
f_TextSource.AddText(aText, f_Font, f_TextColor, f_Align);
end;
procedure Td2dStaticText.ApplyColors;
begin
f_TextSource.IterateChunks(_ApplyChunkColors);
f_Root.IterateLeafSlices(_ApplySliceColors);
end;
procedure Td2dStaticText.ApplyFont;
begin
f_TextSource.IterateChunks(_ApplyFontToChunk);
ReFormat;
end;
procedure Td2dStaticText.CheckAutowidth;
var
l_Size: Td2dPoint;
begin
if f_AutoWidth then
begin
f_Font.CalcSize(f_Text, l_Size);
f_Root.Width := l_Size.X;
end;
end;
procedure Td2dStaticText.EndText;
begin
CheckAutowidth;
ReFormat;
end;
function Td2dStaticText.pm_GetHeight: Single;
begin
Result := f_Root.Height;
end;
function Td2dStaticText.pm_GetLineSpacing: Single;
begin
Result := f_Formatter.FormatParams.rLineSpacing;
end;
function Td2dStaticText.pm_GetLinksEnabled: Boolean;
begin
Result := f_Root.LinksAllowed;
end;
function Td2dStaticText.pm_GetParaSpacing: Single;
begin
Result := f_Formatter.FormatParams.rParaSpacing;
end;
function Td2dStaticText.pm_GetWidth: Single;
begin
Result := f_Root.Width;
end;
procedure Td2dStaticText.pm_SetAlign(const Value: Td2dTextAlignType);
var
I: Integer;
begin
if f_Align <> Value then
begin
f_Align := Value;
with f_TextSource do
for I := 0 to Count - 1 do
Paragraphs[I].ParaType := f_Align;
ReFormat;
end;
end;
procedure Td2dStaticText.pm_SetAutoWidth(const Value: Boolean);
begin
f_AutoWidth := Value;
if f_AutoWidth then
begin
CheckAutowidth;
ReFormat;
end;
end;
procedure Td2dStaticText.pm_SetTextColor(const Value: Td2dColor);
begin
if f_TextColor <> Value then
begin
f_TextColor := Value;
ApplyColors;
end;
end;
procedure Td2dStaticText.pm_SetFont(const Value: Id2dFont);
begin
if f_Font <> Value then
begin
f_Font := Value;
ApplyFont;
end;
end;
procedure Td2dStaticText.pm_SetLineSpacing(const Value: Single);
var
l_FP: Td2dFormatParamsRec;
begin
l_FP := f_Formatter.FormatParams;
l_FP.rLineSpacing := Value;
f_Formatter.FormatParams := l_FP;
ReFormat;
end;
procedure Td2dStaticText.pm_SetParaSpacing(const Value: Single);
var
l_FP: Td2dFormatParamsRec;
begin
l_FP := f_Formatter.FormatParams;
l_FP.rParaSpacing := Value;
f_Formatter.FormatParams := l_FP;
ReFormat;
end;
procedure Td2dStaticText.pm_SetText(const Value: string);
begin
if f_Text <> Value then
begin
StartText;
try
AddText(Value);
finally
EndText;
end;
end;
end;
procedure Td2dStaticText.pm_SetLinkColor(const Value: Td2dColor);
begin
if f_LinkColor <> Value then
begin
f_LinkColor := Value;
ApplyColors;
end;
end;
procedure Td2dStaticText.pm_SetLinkHColor(const Value: Td2dColor);
begin
if f_LinkHColor <> Value then
begin
f_LinkHColor := Value;
ApplyColors;
end;
end;
procedure Td2dStaticText.pm_SetLinksEnabled(const Value: Boolean);
begin
if Value <> f_Root.LinksAllowed then
begin
f_Root.LinksAllowed := Value;
f_Root.DropLinkHighlight;
f_Root.DropLinkCache;
if Value then
gD2DE.Input_TouchMousePos;
end;
end;
procedure Td2dStaticText.pm_SetWidth(const Value: Single);
begin
if (not f_AutoWidth) and (Width <> Value) then
begin
f_Root.Width := Value;
ReFormat;
end;
end;
procedure Td2dStaticText.pm_SetX(const Value: Single);
begin
if f_X <> Value then
begin
f_X := Value;
RenewLinkState;
end;
end;
procedure Td2dStaticText.pm_SetY(const Value: Single);
begin
if f_Y <> Value then
begin
f_Y := Value;
RenewLinkState;
end;
end;
procedure Td2dStaticText.ProcessEvent(var theEvent: Td2dInputEvent);
begin
if (theEvent.EventType = INPUT_MOUSEMOVE) and LinksEnabled then
begin
f_Root.FindLinkHighlight(X, Y, theEvent);
end;
if (theEvent.EventType = INPUT_MBUTTONDOWN) and (theEvent.KeyCode = D2DK_LBUTTON) and (f_Root.HighlightedLink <> nil) then
begin
if Assigned(f_OnLinkClick) then
f_OnLinkClick(Self, D2DMoveRect(f_Root.HighlightedLink.Rect, X, Y), f_Root.HighlightedLink.Slice.Target);
Processed(theEvent);
end;
end;
procedure Td2dStaticText.ReFormat;
begin
f_Formatter.ReformatAll;
f_Root.DropLinkCache;
f_Root.ForceLinkAllowance;
end;
procedure Td2dStaticText.Render;
begin
f_Root.Draw(f_X, f_Y);
end;
procedure Td2dStaticText.RenewLinkState;
begin
f_Root.DropLinkCache;
gD2DE.Input_TouchMousePos;
end;
procedure Td2dStaticText.StartText;
begin
f_Root.Clear;
f_TextSource.Clear;
f_Text := '';
end;
procedure Td2dStaticText._ApplyChunkColors(const aChunk: Td2dCustomTextChunk);
begin
if aChunk.ChunkType in [ctText, ctLink] then
begin
Td2dStringChunk(aChunk).Color := f_TextColor;
if aChunk.ChunkType = ctLink then
begin
Td2dLinkChunk(aChunk).LinkColor := f_LinkColor;
Td2dLinkChunk(aChunk).HighlightColor := f_LinkHColor;
end;
end;
end;
procedure Td2dStaticText._ApplyFontToChunk(const aChunk: Td2dCustomTextChunk);
begin
if aChunk.ChunkType in [ctText, ctLink] then
begin
Td2dStringChunk(aChunk).Font := f_Font;
end;
end;
procedure Td2dStaticText._ApplySliceColors(const aSlice: Td2dCustomSlice);
begin
if aSlice.SliceType in [stText, stLink] then
begin
Td2dTextSlice(aSlice).Color := f_TextColor;
if aSlice.SliceType = stLink then
begin
Td2dLinkSlice(aSlice).LinkColor := f_LinkColor;
Td2dLinkSlice(aSlice).HighlightColor := f_LinkHColor;
end;
end;
end;
end.