{
Copyright 2016-2023 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" 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.
----------------------------------------------------------------------------
}
{ Render "rich text", a small subset of HTML available in CGE controls like TCastleLabel. }
unit CastleInternalRichText;
{$I castleconf.inc}
interface
uses Classes, Generics.Collections,
CastleColors, CastleVectors, CastleRectangles, CastleFonts;
type
{ @exclude Internal type for TRichText }
TTextCommand = (
tcBold, tcBoldEnd,
tcItalic, tcItalicEnd,
tcFontColor, tcFontSize, tcFontEnd,
tcSmall, tcSmallEnd
);
TTextProperty = class;
{ Line of text with processing commands.
@exclude Internal type for TRichText. }
TTextLine = class({$ifdef FPC}specialize{$endif} TObjectList
correctly already } if SubstringStartsHere(S, I, '
', NextChar) then begin Result := TTextPropertyString.Create; // with empty S end else Result := CommandFound(S, I, NextChar); end else Result := nil; end; procedure AddRemainingString(const I, Done: Integer); var PropS: TTextPropertyString; begin if Done + 1 < I then begin PropS := TTextPropertyString.Create; PropS.S := Copy(S, Done + 1, I - 1 - Done); TextLine.Add(PropS); end; end; var I, Done, NextChar, SLength: Integer; SLowerCase: string; PropSpecial: TTextProperty; begin TextLine := TTextLine.Create(FFont); { we search lowercase substrings inside this, thus we ignore case automatically in the rest of the processing code } SLowerCase := AnsiLowerCase(S); Done := 0; I := 1; SLength := Length(S); while I <= SLength do begin { for speed, quickly test and move on if this isn't any special character } if CharInSet(S[I], ['<', '&']) then begin { handle line-breaking elements specially here } if SubstringStartsHere(SLowerCase, I, '', NextChar) then
begin
AddRemainingString(I, Done);
Add(TextLine); TextLine := TTextLine.Create(FFont);
Add(TextLine); TextLine := TTextLine.Create(FFont);
Done := NextChar - 1;
I := NextChar;
end else
if SubstringStartsHere(SLowerCase, I, '
', NextChar) or
SubstringStartsHere(SLowerCase, I, '
', NextChar) or
SubstringStartsHere(SLowerCase, I, '
', NextChar) then
begin
AddRemainingString(I, Done);
Add(TextLine); TextLine := TTextLine.Create(FFont);
Done := NextChar - 1;
I := NextChar;
end else
{ handle entities and commands,
that break a text into multiple TTextProperty instances }
begin
PropSpecial := SpecialFound(SLowerCase, I, NextChar);
if PropSpecial <> nil then
begin
AddRemainingString(I, Done);
TextLine.Add(PropSpecial);
Done := NextChar - 1;
I := NextChar;
end else
Inc(I);
end;
end else
Inc(I);
end;
AddRemainingString(I, Done);
Add(TextLine);
end;
function TRichText.BeginProcessing(const InitialColor: TCastleColor): TTextLine.TPrintState;
begin
Result := TTextLine.TPrintState.Create;
{ set FFont into Bold = Italic = false state }
Result.RestoreBold := FFont.InternalBold;
Result.RestoreItalic := FFont.InternalItalic;
FFont.InternalBold := false;
FFont.InternalItalic := false;
Result.Color := InitialColor;
Result.DefaultSize := FFont.EffectiveSize;
end;
procedure TRichText.EndProcessing(var State: TTextLine.TPrintState);
begin
FFont.InternalBold := State.RestoreBold;
FFont.InternalItalic := State.RestoreItalic;
FreeAndNil(State);
end;
procedure TRichText.Print(const X, Y: Single; const Color: TCastleColor;
const ALineSpacing: Single;
const TextHorizontalAlignment: THorizontalPosition;
MaxDisplayChars: Integer);
var
X0, Y0: Single;
LineSpacing: Single;
function XPos(const Line: Integer): Single;
begin
case TextHorizontalAlignment of
hpLeft : Result := X0;
hpMiddle: Result := X0 - Items[Line].KnownWidth / 2;
hpRight : Result := X0 - Items[Line].KnownWidth;
{$ifndef COMPILER_CASE_ANALYSIS}
else raise EInternalError.Create('TRichText.Print: TextHorizontalAlignment unknown');
{$endif}
end;
end;
var
Height: Single;
function YPos(const Line: Integer): Single;
begin
Result := (Count - 1 - Line) * (Height + LineSpacing) + Y0;
end;
var
State: TTextLine.TPrintState;
I: Integer;
begin
X0 := X;
Y0 := Y;
LineSpacing := ALineSpacing;
{ force calculating FWidth for all lines }
if TextHorizontalAlignment in [hpMiddle, hpRight] then
Width;
State := BeginProcessing(Color);
try
Height := FFont.Height;
for I := 0 to Count - 1 do
Items[I].Print(State, XPos(I), YPos(I), MaxDisplayChars);
finally EndProcessing(State) end;
end;
function TRichText.Width: Single;
var
I: Integer;
State: TTextLine.TPrintState;
begin
if not FWidthKnown then
begin
FWidthKnown := true;
FWidth := 0;
State := BeginProcessing(Black { any color });
try
for I := 0 to Count - 1 do
MaxVar(FWidth, Items[I].Width(State));
finally EndProcessing(State) end;
end;
Result := FWidth;
end;
function TRichText.DisplayChars: Cardinal;
var
I: Integer;
State: TTextLine.TPrintState;
begin
Result := 0;
State := BeginProcessing(Black { any color });
try
for I := 0 to Count - 1 do
Result := Result + Items[I].DisplayChars(State);
finally EndProcessing(State) end;
end;
procedure TRichText.Wrap(const MaxWidth: Single);
var
I, J: Integer;
LineWidth: Single;
Line, NewLine: TTextLine;
State: TTextLine.TPrintState;
begin
State := BeginProcessing(Black { any color });
try
I := 0;
{ note that the current Count will be changing, as we will split our own strings }
while I < Count do
begin
Line := Items[I];
LineWidth := 0;
for J := 0 to Line.Count - 1 do
begin
NewLine := Line[J].Wrap(FFont, State, LineWidth, Ceil(MaxWidth), Line, J);
if NewLine <> nil then
begin
{ next I loop iteration will break the next line,
which is the remainder of current line }
Insert(I + 1, NewLine);
Break; // Break from for J loop
end;
end;
Inc(I);
end;
finally EndProcessing(State) end;
end;
end.