| 1 |
{ |
| 2 |
Copyright 2016-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Render "rich text", a small subset of HTML available in CGE controls like TCastleLabel. } |
| 17 |
unit CastleInternalRichText; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, Generics.Collections, |
| 24 |
CastleColors, CastleVectors, CastleRectangles, CastleFonts; |
| 25 |
|
| 26 |
type |
| 27 |
{ @exclude Internal type for TRichText } |
| 28 |
TTextCommand = ( |
| 29 |
tcBold, tcBoldEnd, |
| 30 |
tcItalic, tcItalicEnd, |
| 31 |
tcFontColor, tcFontSize, tcFontEnd, |
| 32 |
tcSmall, tcSmallEnd |
| 33 |
); |
| 34 |
|
| 35 |
TTextProperty = class; |
| 36 |
|
| 37 |
{ Line of text with processing commands. |
| 38 |
@exclude Internal type for TRichText. } |
| 39 |
TTextLine = class({$ifdef FPC}specialize{$endif} TObjectList<TTextProperty>) |
| 40 |
strict private |
| 41 |
FWidthKnown: boolean; |
| 42 |
FWidth: Single; |
| 43 |
FFont: TCastleFontFamily; |
| 44 |
public |
| 45 |
type |
| 46 |
TFontState = class |
| 47 |
Color: TCastleColor; |
| 48 |
Size: Single; |
| 49 |
end; |
| 50 |
TFontStateList = {$ifdef FPC}specialize{$endif} TObjectList<TFontState>; |
| 51 |
|
| 52 |
TPrintState = class |
| 53 |
Color: TCastleColor; |
| 54 |
DefaultSize: Single; |
| 55 |
{ Created on demand, only when some tcFontXxx command is seen. } |
| 56 |
FontStack: TFontStateList; |
| 57 |
{ Note that Bold and Italic are expressed as integers, |
| 58 |
to smoothly handle <b> embeded in <b>. The value <> 0 indicates |
| 59 |
to use the given variant. } |
| 60 |
Bold, Italic: Cardinal; |
| 61 |
{ The default Font.Bold, Font.Italic before whole TRichText processing. } |
| 62 |
RestoreBold, RestoreItalic: boolean; |
| 63 |
destructor Destroy; override; |
| 64 |
end; |
| 65 |
|
| 66 |
constructor Create(const AFont: TCastleFontFamily); |
| 67 |
function Width(const State: TPrintState): Single; |
| 68 |
function DisplayChars(const State: TPrintState): Cardinal; |
| 69 |
function KnownWidth: Single; |
| 70 |
{ Render line of text at given position. } |
| 71 |
procedure Print(const State: TPrintState; X0, Y0: Single; |
| 72 |
var MaxDisplayChars: Integer); |
| 73 |
end; |
| 74 |
|
| 75 |
{ @exclude Internal type for TRichText } |
| 76 |
TTextProperty = class abstract |
| 77 |
procedure Print(const Font: TCastleFontFamily; |
| 78 |
const State: TTextLine.TPrintState; var X0: Single; const Y0: Single; |
| 79 |
var MaxDisplayChars: Integer); virtual; abstract; |
| 80 |
function Wrap(const Font: TCastleFontFamily; const State: TTextLine.TPrintState; |
| 81 |
var CurrentWidth: Single; const MaxWidth: Single; |
| 82 |
const CurrentLine: TTextLine; const CurrentPropertyIndex: Integer): TTextLine; virtual; abstract; |
| 83 |
function Width(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Single; virtual; abstract; |
| 84 |
function DisplayChars(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Cardinal; virtual; abstract; |
| 85 |
end; |
| 86 |
|
| 87 |
{ @exclude Internal type for TRichText } |
| 88 |
TTextPropertyString = class(TTextProperty) |
| 89 |
S: string; |
| 90 |
procedure Print(const Font: TCastleFontFamily; |
| 91 |
const State: TTextLine.TPrintState; var X0: Single; const Y0: Single; |
| 92 |
var MaxDisplayChars: Integer); override; |
| 93 |
{ If there's a need to break, then: |
| 94 |
- this property is modified (cut), |
| 95 |
- CurrentLine is modified (cut), |
| 96 |
- new line is returned, |
| 97 |
- CurrentWidth is undefined. |
| 98 |
Otherwise, this moves forward, increasing CurrentWidth. } |
| 99 |
function Wrap(const Font: TCastleFontFamily; const State: TTextLine.TPrintState; |
| 100 |
var CurrentWidth: Single; const MaxWidth: Single; |
| 101 |
const CurrentLine: TTextLine; const CurrentPropertyIndex: Integer): TTextLine; override; |
| 102 |
function Width(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Single; override; |
| 103 |
function DisplayChars(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Cardinal; override; |
| 104 |
end; |
| 105 |
|
| 106 |
{ @exclude Internal type for TRichText } |
| 107 |
TTextPropertyCommand = class(TTextProperty) |
| 108 |
strict private |
| 109 |
procedure UpdateState(const Font: TCastleFontFamily; |
| 110 |
const State: TTextLine.TPrintState); |
| 111 |
public |
| 112 |
Command: TTextCommand; |
| 113 |
Color: TCastleColor; |
| 114 |
HtmlSize: Integer; |
| 115 |
PercentSize: Single; |
| 116 |
procedure Print(const Font: TCastleFontFamily; |
| 117 |
const State: TTextLine.TPrintState; var X0: Single; const Y0: Single; |
| 118 |
var MaxDisplayChars: Integer); override; |
| 119 |
function Wrap(const Font: TCastleFontFamily; const State: TTextLine.TPrintState; |
| 120 |
var CurrentWidth: Single; const MaxWidth: Single; |
| 121 |
const CurrentLine: TTextLine; const CurrentPropertyIndex: Integer): TTextLine; override; |
| 122 |
function Width(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Single; override; |
| 123 |
function DisplayChars(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Cardinal; override; |
| 124 |
end; |
| 125 |
|
| 126 |
{ Multi-line text with processing commands |
| 127 |
(like "change font to bold now"). Used to render "rich text", |
| 128 |
which is text that may contain (a subset of) HTML. |
| 129 |
|
| 130 |
Note that TRichText instance is always tied to a corresponding |
| 131 |
TCastleFontFamily used to render it. @bold(Through the lifetime of TRichText, |
| 132 |
we assume that size and other properties of this font remain constant.) } |
| 133 |
TRichText = class({$ifdef FPC}specialize{$endif} TObjectList<TTextLine>) |
| 134 |
strict private |
| 135 |
FWidthKnown: boolean; |
| 136 |
{ Known max line width, e.g. calculated by @link(Wrap). |
| 137 |
Using this allows to avoid recalculating this many times, |
| 138 |
e.g. @link(Wrap) always calculates this as a by-product of it's work. } |
| 139 |
FWidth: Single; |
| 140 |
FFont: TCastleFontFamily; |
| 141 |
FOwnsFont: boolean; |
| 142 |
procedure SetTextWithoutHtml(Text: TStrings); |
| 143 |
procedure SetTextWithHtml(Text: TStrings); |
| 144 |
procedure AddTextWithHtml(const S: string); |
| 145 |
function BeginProcessing(const InitialColor: TCastleColor): TTextLine.TPrintState; |
| 146 |
procedure EndProcessing(var State: TTextLine.TPrintState); |
| 147 |
public |
| 148 |
constructor Create(const AFont: TCastleAbstractFont; |
| 149 |
const Text: TStrings; const Html: boolean); overload; |
| 150 |
constructor Create(const AFont: TCastleAbstractFont; |
| 151 |
const S: string; const Html: boolean); overload; |
| 152 |
destructor Destroy; override; |
| 153 |
function Width: Single; |
| 154 |
procedure Wrap(const MaxWidth: Single); |
| 155 |
procedure Print(const X, Y: Single; const Color: TCastleColor; |
| 156 |
const ALineSpacing: Single; |
| 157 |
const TextHorizontalAlignment: THorizontalPosition = hpLeft; |
| 158 |
MaxDisplayChars: Integer = -1); |
| 159 |
function DisplayChars: Cardinal; |
| 160 |
end; |
| 161 |
|
| 162 |
implementation |
| 163 |
|
| 164 |
// TODO: Delphi Serialize |
| 165 |
|
| 166 |
uses SysUtils, StrUtils, Math, {$ifndef FPC} Character,{$endif} |
| 167 |
CastleUtils, CastleStringUtils, CastleLog, CastleUnicode, CastleComponentSerialize; |
| 168 |
|
| 169 |
{ TPrintState ---------------------------------------------------------------- } |
| 170 |
|
| 171 |
destructor TTextLine.TPrintState.Destroy; |
| 172 |
begin |
| 173 |
FreeAndNil(FontStack); |
| 174 |
inherited; |
| 175 |
end; |
| 176 |
|
| 177 |
{ TTextPropertyString -------------------------------------------------------- } |
| 178 |
|
| 179 |
procedure TTextPropertyString.Print(const Font: TCastleFontFamily; |
| 180 |
const State: TTextLine.TPrintState; var X0: Single; const Y0: Single; |
| 181 |
var MaxDisplayChars: Integer); |
| 182 |
var |
| 183 |
L: Integer; |
| 184 |
begin |
| 185 |
if MaxDisplayChars <> -1 then |
| 186 |
begin |
| 187 |
L := StringLength(S); |
| 188 |
if MaxDisplayChars >= L then |
| 189 |
begin |
| 190 |
MaxDisplayChars := MaxDisplayChars - L; |
| 191 |
Font.Print(X0, Y0, State.Color, S); |
| 192 |
end else |
| 193 |
begin |
| 194 |
Font.Print(X0, Y0, State.Color, StringCopy(S, 1, MaxDisplayChars)); |
| 195 |
MaxDisplayChars := 0; |
| 196 |
end; |
| 197 |
end else |
| 198 |
Font.Print(X0, Y0, State.Color, S); |
| 199 |
X0 := X0 + Font.TextWidth(S); |
| 200 |
end; |
| 201 |
|
| 202 |
function TTextPropertyString.Wrap(const Font: TCastleFontFamily; const State: TTextLine.TPrintState; |
| 203 |
var CurrentWidth: Single; const MaxWidth: Single; |
| 204 |
const CurrentLine: TTextLine; const CurrentPropertyIndex: Integer): TTextLine; |
| 205 |
|
| 206 |
{ Split line in the middle of this TTextPropertyString. } |
| 207 |
procedure BreakLine(const MaximumUnicodeChars: Cardinal); |
| 208 |
var |
| 209 |
NewProp: TTextPropertyString; |
| 210 |
ExtractedProp: TTextProperty; |
| 211 |
HardBreakOutput, BreakOutput1, BreakOutput2: String; |
| 212 |
P: Integer; |
| 213 |
begin |
| 214 |
{ HardBreakOutput is BreakOutput1 in case we don't find a whitespace |
| 215 |
on which to break } |
| 216 |
HardBreakOutput := StringCopy(S, 1, MaximumUnicodeChars); |
| 217 |
|
| 218 |
{ We have to break this line now. |
| 219 |
|
| 220 |
Note: To split with space, we use regular |
| 221 |
BackCharsPos followed by Copy + SEnding. These are not aware of |
| 222 |
multi-character sequences that in UTF-8 (FPC) or UTF-16 (Delphi) may mean |
| 223 |
one Unicode character. But it's OK, the space is detected correctly |
| 224 |
(since it's a single character in UTF-8 and UTF-16 and cannot be mistaken |
| 225 |
for the middle of a multi-character sequence due to how UTF-8 and UTF-16 |
| 226 |
are made). } |
| 227 |
P := BackCharsPos(WhiteSpaces, HardBreakOutput); |
| 228 |
if P > 0 then |
| 229 |
begin |
| 230 |
BreakOutput1 := Copy(S, 1, P - 1); |
| 231 |
BreakOutput2 := SEnding(S, P + 1) { break at pos p, delete p-th char } |
| 232 |
end else |
| 233 |
begin |
| 234 |
{ Code in case we don't find a whitespace on which to break. |
| 235 |
|
| 236 |
Note: we used to have here logic like this: |
| 237 |
|
| 238 |
if LineIsNonEmptyAlready then // calculated by looking at CurrentWidth <> 0 when TTextPropertyString.Wrap starts |
| 239 |
BreakOutput1 := '' |
| 240 |
else |
| 241 |
BreakOutput1 := Copy(S, 1, MaximumChars); |
| 242 |
|
| 243 |
In effect, if we have "blablah<b>foobar</b>", we would prefer to break |
| 244 |
"blablah" + newline + "<b>foobar</b>" (if this would make both lines fit in MaxWidth) |
| 245 |
instead of "blablah<b>foo" + newline + "bar</b>". |
| 246 |
But it seemed incorrect: in any case we are breaking in the middle of |
| 247 |
the word (not at space), |
| 248 |
so why do we favor the place where font style (bold/non-bold) changes? |
| 249 |
In particular it looked incorrect in Japanese, where there are no spaces |
| 250 |
between words, and any place is a valid breaking point. |
| 251 |
So the line should should be typically long in Japanese and it should |
| 252 |
be broken where it doesn't fit (not earlier, when bold/non-bold changes). |
| 253 |
} |
| 254 |
BreakOutput1 := HardBreakOutput; |
| 255 |
BreakOutput2 := StringEnding(S, MaximumUnicodeChars + 1); |
| 256 |
end; |
| 257 |
|
| 258 |
{ now leave BreakOutput1 in this line, and add BreakOutput2 |
| 259 |
(along with remaining properties on this line) to new line } |
| 260 |
S := BreakOutput1; |
| 261 |
|
| 262 |
NewProp := TTextPropertyString.Create; |
| 263 |
NewProp.S := BreakOutput2; |
| 264 |
|
| 265 |
Result := TTextLine.Create(Font); |
| 266 |
Result.Add(NewProp); |
| 267 |
|
| 268 |
while CurrentLine.Count > CurrentPropertyIndex + 1 do |
| 269 |
begin |
| 270 |
ExtractedProp := CurrentLine.Items[CurrentPropertyIndex + 1]; |
| 271 |
CurrentLine.Extract(ExtractedProp); |
| 272 |
Result.Add(ExtractedProp); |
| 273 |
end; |
| 274 |
end; |
| 275 |
|
| 276 |
var |
| 277 |
SIterator: TCastleStringIterator; |
| 278 |
PropUnicodeChars: Cardinal; |
| 279 |
BreakNeeded: Boolean; |
| 280 |
NewCurrentWidth: Single; |
| 281 |
begin |
| 282 |
Result := nil; |
| 283 |
SIterator.Start(S); |
| 284 |
{ If S is empty, there's no need to break it. } |
| 285 |
if SIterator.GetNext then |
| 286 |
begin |
| 287 |
{ the first character C is always considered to fit into BreakOutput1, |
| 288 |
regardless of MaxWidth --- after all, we have to place this character |
| 289 |
somewhere (otherwise we would have to reject this character |
| 290 |
or raise an error). } |
| 291 |
CurrentWidth := CurrentWidth + Font.TextWidth(UnicodeCharToString(SIterator.Current)); |
| 292 |
PropUnicodeChars := 1; |
| 293 |
BreakNeeded := false; |
| 294 |
|
| 295 |
while SIterator.GetNext do |
| 296 |
begin |
| 297 |
NewCurrentWidth := CurrentWidth + Font.TextWidth(UnicodeCharToString(SIterator.Current)); |
| 298 |
if NewCurrentWidth > MaxWidth then |
| 299 |
begin |
| 300 |
{ More characters available, but we need to break } |
| 301 |
BreakNeeded := true; |
| 302 |
Break; |
| 303 |
end; |
| 304 |
CurrentWidth := NewCurrentWidth; |
| 305 |
PropUnicodeChars := PropUnicodeChars + 1; |
| 306 |
end; |
| 307 |
|
| 308 |
if BreakNeeded then |
| 309 |
BreakLine(PropUnicodeChars); |
| 310 |
end; |
| 311 |
end; |
| 312 |
|
| 313 |
function TTextPropertyString.Width(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Single; |
| 314 |
begin |
| 315 |
Result := Font.TextWidth(S); |
| 316 |
end; |
| 317 |
|
| 318 |
function TTextPropertyString.DisplayChars(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Cardinal; |
| 319 |
begin |
| 320 |
Result := StringLength(S); |
| 321 |
end; |
| 322 |
|
| 323 |
{ TTextPropertyCommand -------------------------------------------------------- } |
| 324 |
|
| 325 |
procedure TTextPropertyCommand.Print(const Font: TCastleFontFamily; |
| 326 |
const State: TTextLine.TPrintState; var X0: Single; const Y0: Single; |
| 327 |
var MaxDisplayChars: Integer); |
| 328 |
begin |
| 329 |
UpdateState(Font, State); |
| 330 |
end; |
| 331 |
|
| 332 |
function TTextPropertyCommand.Wrap(const Font: TCastleFontFamily; const State: TTextLine.TPrintState; |
| 333 |
var CurrentWidth: Single; const MaxWidth: Single; |
| 334 |
const CurrentLine: TTextLine; const CurrentPropertyIndex: Integer): TTextLine; |
| 335 |
begin |
| 336 |
UpdateState(Font, State); |
| 337 |
Result := nil; |
| 338 |
end; |
| 339 |
|
| 340 |
function TTextPropertyCommand.Width(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Single; |
| 341 |
begin |
| 342 |
UpdateState(Font, State); |
| 343 |
Result := 0; |
| 344 |
end; |
| 345 |
|
| 346 |
function TTextPropertyCommand.DisplayChars(const Font: TCastleFontFamily; const State: TTextLine.TPrintState): Cardinal; |
| 347 |
begin |
| 348 |
UpdateState(Font, State); |
| 349 |
Result := 0; |
| 350 |
end; |
| 351 |
|
| 352 |
procedure TTextPropertyCommand.UpdateState(const Font: TCastleFontFamily; |
| 353 |
const State: TTextLine.TPrintState); |
| 354 |
const |
| 355 |
{ Chosen constant seems to match Google Chrome's <small> sizing. |
| 356 |
Firefox seems to have more complex equation for <small> |
| 357 |
(effect of <small> seems to depend non-linearly on the parent size). } |
| 358 |
SizeSmaller = 0.825; |
| 359 |
SizeLarger = 1 / SizeSmaller; |
| 360 |
|
| 361 |
{ Convert HTML size as for <font> |
| 362 |
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font to our font size. } |
| 363 |
function HtmlSizeToMySize(const HtmlSize: Integer; const DefaultSize: Single): Single; |
| 364 |
begin |
| 365 |
if HtmlSize < 3 then |
| 366 |
Result := DefaultSize * Power(SizeSmaller, 3 - HtmlSize) else |
| 367 |
if HtmlSize > 3 then |
| 368 |
Result := DefaultSize * Power(SizeLarger, HtmlSize - 3) else |
| 369 |
Result := DefaultSize; |
| 370 |
end; |
| 371 |
|
| 372 |
var |
| 373 |
FontState: TTextLine.TFontState; |
| 374 |
begin |
| 375 |
case Command of |
| 376 |
tcBold: |
| 377 |
begin |
| 378 |
Inc(State.Bold); |
| 379 |
Font.InternalBold := State.Bold <> 0; |
| 380 |
end; |
| 381 |
tcBoldEnd: |
| 382 |
if State.Bold = 0 then |
| 383 |
WritelnWarning('HTML', 'Mismatched </b>') else |
| 384 |
begin |
| 385 |
Dec(State.Bold); |
| 386 |
Font.InternalBold := State.Bold <> 0; |
| 387 |
end; |
| 388 |
tcItalic: |
| 389 |
begin |
| 390 |
Inc(State.Italic); |
| 391 |
Font.InternalItalic := State.Italic <> 0; |
| 392 |
end; |
| 393 |
tcItalicEnd: |
| 394 |
if State.Italic = 0 then |
| 395 |
WritelnWarning('HTML', 'Mismatched </i>') else |
| 396 |
begin |
| 397 |
Dec(State.Italic); |
| 398 |
Font.InternalItalic := State.Italic <> 0; |
| 399 |
end; |
| 400 |
tcFontColor: |
| 401 |
begin |
| 402 |
if State.FontStack = nil then |
| 403 |
State.FontStack := TTextLine.TFontStateList.Create; |
| 404 |
FontState := TTextLine.TFontState.Create; |
| 405 |
FontState.Color := State.Color; |
| 406 |
FontState.Size := Font.EffectiveSize; |
| 407 |
State.FontStack.Add(FontState); |
| 408 |
|
| 409 |
State.Color := Color; |
| 410 |
end; |
| 411 |
tcFontSize: |
| 412 |
begin |
| 413 |
if State.FontStack = nil then |
| 414 |
State.FontStack := TTextLine.TFontStateList.Create; |
| 415 |
FontState := TTextLine.TFontState.Create; |
| 416 |
FontState.Color := State.Color; |
| 417 |
FontState.Size := Font.EffectiveSize; |
| 418 |
State.FontStack.Add(FontState); |
| 419 |
|
| 420 |
if HtmlSize <> 0 then |
| 421 |
Font.Size := HtmlSizeToMySize(HtmlSize, State.DefaultSize) else |
| 422 |
Font.Size := PercentSize * State.DefaultSize; |
| 423 |
end; |
| 424 |
tcSmall: |
| 425 |
begin |
| 426 |
if State.FontStack = nil then |
| 427 |
State.FontStack := TTextLine.TFontStateList.Create; |
| 428 |
FontState := TTextLine.TFontState.Create; |
| 429 |
FontState.Color := State.Color; |
| 430 |
FontState.Size := Font.EffectiveSize; |
| 431 |
State.FontStack.Add(FontState); |
| 432 |
|
| 433 |
Font.Size := Font.EffectiveSize * SizeSmaller; |
| 434 |
end; |
| 435 |
tcFontEnd, tcSmallEnd: |
| 436 |
if (State.FontStack = nil) or (State.FontStack.Count = 0) then |
| 437 |
WritelnWarning('HTML', 'Mismatched </font> or </small>') else |
| 438 |
begin |
| 439 |
FontState := State.FontStack.Last; |
| 440 |
State.Color := FontState.Color; |
| 441 |
Font.Size := FontState.Size; |
| 442 |
State.FontStack.Delete(State.FontStack.Count - 1); // remove last, popping from stack |
| 443 |
end; |
| 444 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 445 |
else raise EInternalError.Create('TTextPropertyCommand.Print unknown enum'); |
| 446 |
{$endif} |
| 447 |
end; |
| 448 |
end; |
| 449 |
|
| 450 |
{ TTextLine ------------------------------------------------------------------ } |
| 451 |
|
| 452 |
constructor TTextLine.Create(const AFont: TCastleFontFamily); |
| 453 |
begin |
| 454 |
inherited Create(true); |
| 455 |
FFont := AFont; |
| 456 |
end; |
| 457 |
|
| 458 |
function TTextLine.Width(const State: TPrintState): Single; |
| 459 |
var |
| 460 |
I: Integer; |
| 461 |
begin |
| 462 |
{ note that this cannot just return FWidth, even if FWidthKnown, |
| 463 |
because we still have to iterate over our properties to update State. } |
| 464 |
FWidthKnown := true; |
| 465 |
FWidth := 0; |
| 466 |
for I := 0 to Count - 1 do |
| 467 |
FWidth := FWidth + Items[I].Width(FFont, State); |
| 468 |
Result := FWidth; |
| 469 |
end; |
| 470 |
|
| 471 |
function TTextLine.DisplayChars(const State: TPrintState): Cardinal; |
| 472 |
var |
| 473 |
I: Integer; |
| 474 |
begin |
| 475 |
Result := 0; |
| 476 |
for I := 0 to Count - 1 do |
| 477 |
Result := Result + Items[I].DisplayChars(FFont, State); |
| 478 |
end; |
| 479 |
|
| 480 |
function TTextLine.KnownWidth: Single; |
| 481 |
begin |
| 482 |
if not FWidthKnown then |
| 483 |
raise Exception.Create('Line width not known yet'); |
| 484 |
Result := FWidth; |
| 485 |
end; |
| 486 |
|
| 487 |
procedure TTextLine.Print(const State: TPrintState; X0, Y0: Single; |
| 488 |
var MaxDisplayChars: Integer); |
| 489 |
var |
| 490 |
I: Integer; |
| 491 |
begin |
| 492 |
for I := 0 to Count - 1 do |
| 493 |
Items[I].Print(FFont, State, X0, Y0, MaxDisplayChars); |
| 494 |
end; |
| 495 |
|
| 496 |
{ TRichText ------------------------------------------------------------------ } |
| 497 |
|
| 498 |
constructor TRichText.Create(const AFont: TCastleAbstractFont; |
| 499 |
const Text: TStrings; const Html: boolean); |
| 500 |
begin |
| 501 |
inherited Create(true); |
| 502 |
|
| 503 |
if AFont is TCastleFontFamily then |
| 504 |
begin |
| 505 |
FFont := AFont as TCastleFontFamily; |
| 506 |
FOwnsFont := false; |
| 507 |
end else |
| 508 |
begin |
| 509 |
FFont := TCastleFontFamily.Create(nil); |
| 510 |
FFont.Regular := AFont; |
| 511 |
FOwnsFont := true; |
| 512 |
{ Do not make this warning by default? Too talkative sometimes, |
| 513 |
esp. at every Print call, |
| 514 |
and as HTML may be useful without TCastleFontFamily sometimes. |
| 515 |
|
| 516 |
if Html then |
| 517 |
WritelnWarning('HTML', 'Rendering HTML text with simple font (' + |
| 518 |
AFont.ClassName + ':' + AFont.Name + |
| 519 |
'), without bold/italic variants. <b> and <i> will not have any effect'); |
| 520 |
} |
| 521 |
end; |
| 522 |
|
| 523 |
if Html then |
| 524 |
SetTextWithHtml(Text) else |
| 525 |
SetTextWithoutHtml(Text); |
| 526 |
end; |
| 527 |
|
| 528 |
constructor TRichText.Create(const AFont: TCastleAbstractFont; |
| 529 |
const S: string; const Html: boolean); |
| 530 |
var |
| 531 |
Text: TStringList; |
| 532 |
begin |
| 533 |
Text := TStringList.Create; |
| 534 |
try |
| 535 |
Text.Text := S; |
| 536 |
Create(AFont, Text, Html); |
| 537 |
finally FreeAndNil(Text) end; |
| 538 |
end; |
| 539 |
|
| 540 |
destructor TRichText.Destroy; |
| 541 |
begin |
| 542 |
if FOwnsFont then |
| 543 |
FreeAndNil(FFont); |
| 544 |
inherited; |
| 545 |
end; |
| 546 |
|
| 547 |
procedure TRichText.SetTextWithoutHtml(Text: TStrings); |
| 548 |
var |
| 549 |
PropS: TTextPropertyString; |
| 550 |
I: Integer; |
| 551 |
begin |
| 552 |
Count := Text.Count; |
| 553 |
for I := 0 to Text.Count - 1 do |
| 554 |
begin |
| 555 |
PropS := TTextPropertyString.Create; |
| 556 |
PropS.S := Text[I]; |
| 557 |
Items[I] := TTextLine.Create(FFont); |
| 558 |
Items[I].Add(PropS); |
| 559 |
end; |
| 560 |
end; |
| 561 |
|
| 562 |
procedure TRichText.SetTextWithHtml(Text: TStrings); |
| 563 |
var |
| 564 |
I: Integer; |
| 565 |
begin |
| 566 |
Clear; |
| 567 |
Capacity := Text.Count; |
| 568 |
for I := 0 to Text.Count - 1 do |
| 569 |
AddTextWithHtml(Text[I]); |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TRichText.AddTextWithHtml(const S: string); |
| 573 |
var |
| 574 |
TextLine: TTextLine; |
| 575 |
|
| 576 |
{ Test is given substring at position I within larger string S. |
| 577 |
Does it fast (without creating a temporary copy for a subtring from S). |
| 578 |
Case-sensitive. } |
| 579 |
function SubstringStartsHere(const S: string; const I: Integer; |
| 580 |
const Substring: string; out NextChar: Integer): boolean; |
| 581 |
var |
| 582 |
J, SubstringLength, SIndex: Integer; |
| 583 |
begin |
| 584 |
SubstringLength := Length(Substring); |
| 585 |
NextChar := I + SubstringLength; |
| 586 |
if NextChar > Length(S) + 1 then |
| 587 |
Exit(false); // Substring too long |
| 588 |
SIndex := I; |
| 589 |
for J := 1 to SubstringLength do |
| 590 |
begin |
| 591 |
if Substring[J] <> S[SIndex] then |
| 592 |
Exit(false); |
| 593 |
Inc(SIndex); |
| 594 |
end; |
| 595 |
Result := true; |
| 596 |
end; |
| 597 |
|
| 598 |
function EntityFound(const S: string; const I: Integer; |
| 599 |
out NextChar: Integer): TTextPropertyString; |
| 600 |
begin |
| 601 |
if SubstringStartsHere(S, I, '&', NextChar) then |
| 602 |
begin |
| 603 |
Result := TTextPropertyString.Create; |
| 604 |
Result.S := '&'; |
| 605 |
end else |
| 606 |
if SubstringStartsHere(S, I, '<', NextChar) then |
| 607 |
begin |
| 608 |
Result := TTextPropertyString.Create; |
| 609 |
Result.S := '<'; |
| 610 |
end else |
| 611 |
if SubstringStartsHere(S, I, '>', NextChar) then |
| 612 |
begin |
| 613 |
Result := TTextPropertyString.Create; |
| 614 |
Result.S := '>'; |
| 615 |
end else |
| 616 |
if SubstringStartsHere(S, I, ''', NextChar) then |
| 617 |
begin |
| 618 |
Result := TTextPropertyString.Create; |
| 619 |
Result.S := ''''; |
| 620 |
end else |
| 621 |
if SubstringStartsHere(S, I, '"', NextChar) then |
| 622 |
begin |
| 623 |
Result := TTextPropertyString.Create; |
| 624 |
Result.S := '"'; |
| 625 |
end else |
| 626 |
Result := nil; |
| 627 |
end; |
| 628 |
|
| 629 |
{ @raises EConvertError In case of invalid color hexadecimal value. } |
| 630 |
function ReadFontColor(const S: string; const I: Integer; |
| 631 |
out NextChar: Integer; out Color: TCastleColor): boolean; |
| 632 |
var |
| 633 |
EndPos: Integer; |
| 634 |
const |
| 635 |
HexDigits = ['0'..'9', 'a'..'f', 'A'..'F']; |
| 636 |
begin |
| 637 |
EndPos := PosEx('">', S, I); |
| 638 |
NextChar := EndPos + 2; |
| 639 |
{ we have to find ">, and have exactly 6 or 8 characters for color } |
| 640 |
Result := (EndPos = I + 6) or (EndPos = I + 8); |
| 641 |
|
| 642 |
Color.X := StrHexToInt(Copy(S, I , 2)) / 255; |
| 643 |
Color.Y := StrHexToInt(Copy(S, I + 2, 2)) / 255; |
| 644 |
Color.Z := StrHexToInt(Copy(S, I + 4, 2)) / 255; |
| 645 |
if EndPos = I + 8 then |
| 646 |
Color.W := StrHexToInt(Copy(S, I + 6, 2)) / 255 else |
| 647 |
Color.W := 1.0; |
| 648 |
end; |
| 649 |
|
| 650 |
function ReadFontSize(const S: string; const I: Integer; |
| 651 |
out NextChar: Integer; out HtmlSize: Integer; out PercentSize: Single): boolean; |
| 652 |
var |
| 653 |
EndPos: Integer; |
| 654 |
SizeRead: Int64; |
| 655 |
NumStr: string; |
| 656 |
begin |
| 657 |
EndPos := PosEx('">', S, I); |
| 658 |
NextChar := EndPos + 2; |
| 659 |
Result := EndPos <> 0; |
| 660 |
SizeRead := 0; |
| 661 |
PercentSize := 0; |
| 662 |
|
| 663 |
NumStr := Copy(S, I, EndPos - I); |
| 664 |
|
| 665 |
if IsSuffix('%', NumStr, false) then |
| 666 |
begin |
| 667 |
if not TryStrToFloatDot(SuffixRemove('%', NumStr, false), PercentSize) then |
| 668 |
Exit(false); |
| 669 |
PercentSize := PercentSize / 100; |
| 670 |
end else |
| 671 |
begin |
| 672 |
if not TryStrToInt64(NumStr, SizeRead) then |
| 673 |
Exit(false); |
| 674 |
if CharInSet(S[I], ['+', '-']) then |
| 675 |
{ size is relative to 3. |
| 676 |
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font } |
| 677 |
HtmlSize := Clamped(3 + SizeRead, 1, 7) |
| 678 |
else |
| 679 |
HtmlSize := Clamped(SizeRead, 1, 7); |
| 680 |
end; |
| 681 |
end; |
| 682 |
|
| 683 |
function CommandFound(const S: string; const I: Integer; |
| 684 |
out NextChar: Integer): TTextPropertyCommand; |
| 685 |
begin |
| 686 |
if SubstringStartsHere(S, I, '<b>', NextChar) then |
| 687 |
begin |
| 688 |
Result := TTextPropertyCommand.Create; |
| 689 |
Result.Command := tcBold; |
| 690 |
end else |
| 691 |
if SubstringStartsHere(S, I, '</b>', NextChar) then |
| 692 |
begin |
| 693 |
Result := TTextPropertyCommand.Create; |
| 694 |
Result.Command := tcBoldEnd; |
| 695 |
end else |
| 696 |
if SubstringStartsHere(S, I, '<i>', NextChar) then |
| 697 |
begin |
| 698 |
Result := TTextPropertyCommand.Create; |
| 699 |
Result.Command := tcItalic; |
| 700 |
end else |
| 701 |
if SubstringStartsHere(S, I, '</i>', NextChar) then |
| 702 |
begin |
| 703 |
Result := TTextPropertyCommand.Create; |
| 704 |
Result.Command := tcItalicEnd; |
| 705 |
end else |
| 706 |
if SubstringStartsHere(S, I, '<font color="#', NextChar) then |
| 707 |
begin |
| 708 |
Result := TTextPropertyCommand.Create; |
| 709 |
Result.Command := tcFontColor; |
| 710 |
if not ReadFontColor(S, NextChar, NextChar, Result.Color) then |
| 711 |
FreeAndNil(Result); // resign, not correct |
| 712 |
end else |
| 713 |
if SubstringStartsHere(S, I, '<font size="', NextChar) then |
| 714 |
begin |
| 715 |
Result := TTextPropertyCommand.Create; |
| 716 |
Result.Command := tcFontSize; |
| 717 |
if not ReadFontSize(S, NextChar, NextChar, Result.HtmlSize, Result.PercentSize) then |
| 718 |
FreeAndNil(Result); // resign, not correct |
| 719 |
end else |
| 720 |
if SubstringStartsHere(S, I, '</font>', NextChar) then |
| 721 |
begin |
| 722 |
Result := TTextPropertyCommand.Create; |
| 723 |
Result.Command := tcFontEnd; |
| 724 |
end else |
| 725 |
if SubstringStartsHere(S, I, '<small>', NextChar) then |
| 726 |
begin |
| 727 |
Result := TTextPropertyCommand.Create; |
| 728 |
Result.Command := tcSmall; |
| 729 |
end else |
| 730 |
if SubstringStartsHere(S, I, '</small>', NextChar) then |
| 731 |
begin |
| 732 |
Result := TTextPropertyCommand.Create; |
| 733 |
Result.Command := tcSmallEnd; |
| 734 |
end else |
| 735 |
Result := nil; |
| 736 |
end; |
| 737 |
|
| 738 |
function CommentFound(const S: string; const I: Integer; |
| 739 |
out NextChar: Integer): TTextPropertyString; |
| 740 |
var |
| 741 |
EndPos: Integer; |
| 742 |
begin |
| 743 |
Result := nil; |
| 744 |
if SubstringStartsHere(S, I, '<!--', NextChar) then |
| 745 |
begin |
| 746 |
EndPos := PosEx('-->', S, NextChar); |
| 747 |
if EndPos <> 0 then |
| 748 |
begin |
| 749 |
NextChar := EndPos + 3; |
| 750 |
Result := TTextPropertyString.Create; // with empty S |
| 751 |
end; |
| 752 |
end; |
| 753 |
end; |
| 754 |
|
| 755 |
function SpecialFound(const S: string; const I: Integer; |
| 756 |
out NextChar: Integer): TTextProperty; |
| 757 |
begin |
| 758 |
if S[I] = '&' then |
| 759 |
Result := EntityFound(S, I, NextChar) else |
| 760 |
if S[I] = '<' then |
| 761 |
begin |
| 762 |
if SCharIs(S, I + 1, '!') then |
| 763 |
Result := CommentFound(S, I, NextChar) else |
| 764 |
{ simply ignore the </p>, we handle <p> correctly already } |
| 765 |
if SubstringStartsHere(S, I, '</p>', NextChar) then |
| 766 |
begin |
| 767 |
Result := TTextPropertyString.Create; // with empty S |
| 768 |
end else |
| 769 |
Result := CommandFound(S, I, NextChar); |
| 770 |
end else |
| 771 |
Result := nil; |
| 772 |
end; |
| 773 |
|
| 774 |
procedure AddRemainingString(const I, Done: Integer); |
| 775 |
var |
| 776 |
PropS: TTextPropertyString; |
| 777 |
begin |
| 778 |
if Done + 1 < I then |
| 779 |
begin |
| 780 |
PropS := TTextPropertyString.Create; |
| 781 |
PropS.S := Copy(S, Done + 1, I - 1 - Done); |
| 782 |
TextLine.Add(PropS); |
| 783 |
end; |
| 784 |
end; |
| 785 |
|
| 786 |
var |
| 787 |
I, Done, NextChar, SLength: Integer; |
| 788 |
SLowerCase: string; |
| 789 |
PropSpecial: TTextProperty; |
| 790 |
begin |
| 791 |
TextLine := TTextLine.Create(FFont); |
| 792 |
|
| 793 |
{ we search lowercase substrings inside this, thus we ignore case automatically |
| 794 |
in the rest of the processing code } |
| 795 |
SLowerCase := AnsiLowerCase(S); |
| 796 |
|
| 797 |
Done := 0; |
| 798 |
I := 1; |
| 799 |
SLength := Length(S); |
| 800 |
|
| 801 |
while I <= SLength do |
| 802 |
begin |
| 803 |
{ for speed, quickly test and move on if this isn't any special character } |
| 804 |
if CharInSet(S[I], ['<', '&']) then |
| 805 |
begin |
| 806 |
{ handle line-breaking elements specially here } |
| 807 |
if SubstringStartsHere(SLowerCase, I, '<p>', NextChar) then |
| 808 |
begin |
| 809 |
AddRemainingString(I, Done); |
| 810 |
Add(TextLine); TextLine := TTextLine.Create(FFont); |
| 811 |
Add(TextLine); TextLine := TTextLine.Create(FFont); |
| 812 |
Done := NextChar - 1; |
| 813 |
I := NextChar; |
| 814 |
end else |
| 815 |
if SubstringStartsHere(SLowerCase, I, '<br>', NextChar) or |
| 816 |
SubstringStartsHere(SLowerCase, I, '<br/>', NextChar) or |
| 817 |
SubstringStartsHere(SLowerCase, I, '<br />', NextChar) then |
| 818 |
begin |
| 819 |
AddRemainingString(I, Done); |
| 820 |
Add(TextLine); TextLine := TTextLine.Create(FFont); |
| 821 |
Done := NextChar - 1; |
| 822 |
I := NextChar; |
| 823 |
end else |
| 824 |
{ handle entities and commands, |
| 825 |
that break a text into multiple TTextProperty instances } |
| 826 |
begin |
| 827 |
PropSpecial := SpecialFound(SLowerCase, I, NextChar); |
| 828 |
if PropSpecial <> nil then |
| 829 |
begin |
| 830 |
AddRemainingString(I, Done); |
| 831 |
TextLine.Add(PropSpecial); |
| 832 |
Done := NextChar - 1; |
| 833 |
I := NextChar; |
| 834 |
end else |
| 835 |
Inc(I); |
| 836 |
end; |
| 837 |
end else |
| 838 |
Inc(I); |
| 839 |
end; |
| 840 |
|
| 841 |
AddRemainingString(I, Done); |
| 842 |
Add(TextLine); |
| 843 |
end; |
| 844 |
|
| 845 |
function TRichText.BeginProcessing(const InitialColor: TCastleColor): TTextLine.TPrintState; |
| 846 |
begin |
| 847 |
Result := TTextLine.TPrintState.Create; |
| 848 |
|
| 849 |
{ set FFont into Bold = Italic = false state } |
| 850 |
Result.RestoreBold := FFont.InternalBold; |
| 851 |
Result.RestoreItalic := FFont.InternalItalic; |
| 852 |
FFont.InternalBold := false; |
| 853 |
FFont.InternalItalic := false; |
| 854 |
|
| 855 |
Result.Color := InitialColor; |
| 856 |
Result.DefaultSize := FFont.EffectiveSize; |
| 857 |
end; |
| 858 |
|
| 859 |
procedure TRichText.EndProcessing(var State: TTextLine.TPrintState); |
| 860 |
begin |
| 861 |
FFont.InternalBold := State.RestoreBold; |
| 862 |
FFont.InternalItalic := State.RestoreItalic; |
| 863 |
|
| 864 |
FreeAndNil(State); |
| 865 |
end; |
| 866 |
|
| 867 |
procedure TRichText.Print(const X, Y: Single; const Color: TCastleColor; |
| 868 |
const ALineSpacing: Single; |
| 869 |
const TextHorizontalAlignment: THorizontalPosition; |
| 870 |
MaxDisplayChars: Integer); |
| 871 |
var |
| 872 |
X0, Y0: Single; |
| 873 |
LineSpacing: Single; |
| 874 |
|
| 875 |
function XPos(const Line: Integer): Single; |
| 876 |
begin |
| 877 |
case TextHorizontalAlignment of |
| 878 |
hpLeft : Result := X0; |
| 879 |
hpMiddle: Result := X0 - Items[Line].KnownWidth / 2; |
| 880 |
hpRight : Result := X0 - Items[Line].KnownWidth; |
| 881 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 882 |
else raise EInternalError.Create('TRichText.Print: TextHorizontalAlignment unknown'); |
| 883 |
{$endif} |
| 884 |
end; |
| 885 |
end; |
| 886 |
|
| 887 |
var |
| 888 |
Height: Single; |
| 889 |
|
| 890 |
function YPos(const Line: Integer): Single; |
| 891 |
begin |
| 892 |
Result := (Count - 1 - Line) * (Height + LineSpacing) + Y0; |
| 893 |
end; |
| 894 |
|
| 895 |
var |
| 896 |
State: TTextLine.TPrintState; |
| 897 |
I: Integer; |
| 898 |
begin |
| 899 |
X0 := X; |
| 900 |
Y0 := Y; |
| 901 |
LineSpacing := ALineSpacing; |
| 902 |
|
| 903 |
{ force calculating FWidth for all lines } |
| 904 |
if TextHorizontalAlignment in [hpMiddle, hpRight] then |
| 905 |
Width; |
| 906 |
|
| 907 |
State := BeginProcessing(Color); |
| 908 |
try |
| 909 |
Height := FFont.Height; |
| 910 |
for I := 0 to Count - 1 do |
| 911 |
Items[I].Print(State, XPos(I), YPos(I), MaxDisplayChars); |
| 912 |
finally EndProcessing(State) end; |
| 913 |
end; |
| 914 |
|
| 915 |
function TRichText.Width: Single; |
| 916 |
var |
| 917 |
I: Integer; |
| 918 |
State: TTextLine.TPrintState; |
| 919 |
begin |
| 920 |
if not FWidthKnown then |
| 921 |
begin |
| 922 |
FWidthKnown := true; |
| 923 |
FWidth := 0; |
| 924 |
State := BeginProcessing(Black { any color }); |
| 925 |
try |
| 926 |
for I := 0 to Count - 1 do |
| 927 |
MaxVar(FWidth, Items[I].Width(State)); |
| 928 |
finally EndProcessing(State) end; |
| 929 |
end; |
| 930 |
Result := FWidth; |
| 931 |
end; |
| 932 |
|
| 933 |
function TRichText.DisplayChars: Cardinal; |
| 934 |
var |
| 935 |
I: Integer; |
| 936 |
State: TTextLine.TPrintState; |
| 937 |
begin |
| 938 |
Result := 0; |
| 939 |
State := BeginProcessing(Black { any color }); |
| 940 |
try |
| 941 |
for I := 0 to Count - 1 do |
| 942 |
Result := Result + Items[I].DisplayChars(State); |
| 943 |
finally EndProcessing(State) end; |
| 944 |
end; |
| 945 |
|
| 946 |
procedure TRichText.Wrap(const MaxWidth: Single); |
| 947 |
var |
| 948 |
I, J: Integer; |
| 949 |
LineWidth: Single; |
| 950 |
Line, NewLine: TTextLine; |
| 951 |
State: TTextLine.TPrintState; |
| 952 |
begin |
| 953 |
State := BeginProcessing(Black { any color }); |
| 954 |
try |
| 955 |
I := 0; |
| 956 |
{ note that the current Count will be changing, as we will split our own strings } |
| 957 |
while I < Count do |
| 958 |
begin |
| 959 |
Line := Items[I]; |
| 960 |
LineWidth := 0; |
| 961 |
for J := 0 to Line.Count - 1 do |
| 962 |
begin |
| 963 |
NewLine := Line[J].Wrap(FFont, State, LineWidth, Ceil(MaxWidth), Line, J); |
| 964 |
if NewLine <> nil then |
| 965 |
begin |
| 966 |
{ next I loop iteration will break the next line, |
| 967 |
which is the remainder of current line } |
| 968 |
Insert(I + 1, NewLine); |
| 969 |
Break; // Break from for J loop |
| 970 |
end; |
| 971 |
end; |
| 972 |
Inc(I); |
| 973 |
end; |
| 974 |
finally EndProcessing(State) end; |
| 975 |
end; |
| 976 |
|
| 977 |
end. |