| 1 | unit BGRAFontGL;
|
|---|
| 2 |
|
|---|
| 3 | {$mode objfpc}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, BGRAGraphics, BGRAOpenGLType, BGRABitmapTypes,
|
|---|
| 9 | AvgLvlTree;
|
|---|
| 10 |
|
|---|
| 11 | type
|
|---|
| 12 | { TRenderedGlyph }
|
|---|
| 13 |
|
|---|
| 14 | TRenderedGlyph = class
|
|---|
| 15 | private
|
|---|
| 16 | FIdentifier: UTF8String;
|
|---|
| 17 | FTexture: IBGLTexture;
|
|---|
| 18 | FHorizontalOverflowPx, FVerticalOverflowPx, FAdvancePx: integer;
|
|---|
| 19 | public
|
|---|
| 20 | constructor Create(AIdentifier: UTF8String; ATexture: IBGLTexture;
|
|---|
| 21 | AHorizontalOverflowPx, AVerticalOverflowPx: integer);
|
|---|
| 22 | procedure Draw(x,y,Scale: single; AColor: TBGRAPixel); overload;
|
|---|
| 23 | procedure Draw(x,y,Scale: single; AGradTopLeft, AGradTopRight, AGradBottomRight, AGradBottomLeft: TBGRAPixel); overload;
|
|---|
| 24 | property Identifier: UTF8String read FIdentifier;
|
|---|
| 25 | property AdvancePx: integer read FAdvancePx;
|
|---|
| 26 | end;
|
|---|
| 27 |
|
|---|
| 28 | { IBGLRenderedFont }
|
|---|
| 29 |
|
|---|
| 30 | IBGLRenderedFont = interface(IBGLFont)
|
|---|
| 31 | function GetBackgroundColor: TBGRAPixel;
|
|---|
| 32 | function GetColor: TBGRAPixel;
|
|---|
| 33 | function GetFontEmHeight: integer;
|
|---|
| 34 | function GetFontFullHeight: integer;
|
|---|
| 35 | function GetHorizontalOverflow: single;
|
|---|
| 36 | function GetName: string;
|
|---|
| 37 | function GetQuality: TBGRAFontQuality;
|
|---|
| 38 | function GetStyle: TFontStyles;
|
|---|
| 39 | function GetVerticalOverflow: single;
|
|---|
| 40 | procedure SetBackgroundColor(AValue: TBGRAPixel);
|
|---|
| 41 | procedure SetColor(AValue: TBGRAPixel);
|
|---|
| 42 | procedure SetFontEmHeight(AValue: integer);
|
|---|
| 43 | procedure SetFontFullHeight(AValue: integer);
|
|---|
| 44 | procedure SetHorizontalOverflow(AValue: single);
|
|---|
| 45 | procedure SetName(AValue: string);
|
|---|
| 46 | procedure SetQuality(AValue: TBGRAFontQuality);
|
|---|
| 47 | procedure SetStyle(AValue: TFontStyles);
|
|---|
| 48 | procedure SetVerticalOverflow(AValue: single);
|
|---|
| 49 |
|
|---|
| 50 | property Name: string read GetName write SetName;
|
|---|
| 51 | property Style: TFontStyles read GetStyle write SetStyle;
|
|---|
| 52 | property Quality: TBGRAFontQuality read GetQuality write SetQuality;
|
|---|
| 53 | property EmHeight: integer read GetFontEmHeight write SetFontEmHeight;
|
|---|
| 54 | property FullHeight: integer read GetFontFullHeight write SetFontFullHeight;
|
|---|
| 55 | property Color: TBGRAPixel read GetColor write SetColor;
|
|---|
| 56 | property HorizontalOverflow: single read GetHorizontalOverflow write SetHorizontalOverflow;
|
|---|
| 57 | property VerticalOverflow: single read GetVerticalOverflow write SetVerticalOverflow;
|
|---|
| 58 | property BackgroundColor: TBGRAPixel read GetBackgroundColor write SetBackgroundColor;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | { TBGLRenderedFont }
|
|---|
| 62 |
|
|---|
| 63 | TBGLRenderedFont = class(TBGLCustomFont,IBGLRenderedFont)
|
|---|
| 64 | private
|
|---|
| 65 | FGlyphs: TAvgLvlTree;
|
|---|
| 66 |
|
|---|
| 67 | FName: string;
|
|---|
| 68 | FColor: TBGRAPixel;
|
|---|
| 69 | FBackgroundColor: TBGRAPixel;
|
|---|
| 70 | FEmHeight: integer;
|
|---|
| 71 | FHorizontalOverflow: single;
|
|---|
| 72 | FVerticalOverflow: single;
|
|---|
| 73 | FQuality: TBGRAFontQuality;
|
|---|
| 74 | FStyle: TFontStyles;
|
|---|
| 75 | FGradTopLeft, FGradTopRight, FGradBottomRight, FGradBottomLeft: TBGRAPixel;
|
|---|
| 76 | FUseGradientColor: boolean;
|
|---|
| 77 | FClipped: boolean;
|
|---|
| 78 | FWordBreakHandler: TWordBreakHandler;
|
|---|
| 79 |
|
|---|
| 80 | function CompareGlyph({%H-}Tree: TAvgLvlTree; Data1, Data2: Pointer): integer;
|
|---|
| 81 | function FindGlyph(AIdentifier: string): TAvgLvlTreeNode;
|
|---|
| 82 | function GetBackgroundColor: TBGRAPixel;
|
|---|
| 83 | function GetColor: TBGRAPixel;
|
|---|
| 84 | function GetFontEmHeight: integer;
|
|---|
| 85 | function GetGlyph(AIdentifier: string): TRenderedGlyph;
|
|---|
| 86 | function GetHorizontalOverflow: single;
|
|---|
| 87 | function GetName: string;
|
|---|
| 88 | function GetQuality: TBGRAFontQuality;
|
|---|
| 89 | function GetStyle: TFontStyles;
|
|---|
| 90 | function GetVerticalOverflow: single;
|
|---|
| 91 | procedure SetGlyph(AIdentifier: string; AValue: TRenderedGlyph);
|
|---|
| 92 | function GetFontFullHeight: integer;
|
|---|
| 93 | procedure SetBackgroundColor(AValue: TBGRAPixel);
|
|---|
| 94 | procedure SetColor(AValue: TBGRAPixel);
|
|---|
| 95 | procedure SetFontEmHeight(AValue: integer);
|
|---|
| 96 | procedure SetFontFullHeight(AValue: integer);
|
|---|
| 97 | procedure SetHorizontalOverflow(AValue: single);
|
|---|
| 98 | procedure SetName(AValue: string);
|
|---|
| 99 | procedure SetQuality(AValue: TBGRAFontQuality);
|
|---|
| 100 | procedure SetStyle(AValue: TFontStyles);
|
|---|
| 101 | procedure SetVerticalOverflow(AValue: single);
|
|---|
| 102 | protected
|
|---|
| 103 | FRenderer: TBGRACustomFontRenderer;
|
|---|
| 104 | FRendererOwned: boolean;
|
|---|
| 105 | function LoadFromFile({%H-}AFilename: UTF8String): boolean; override;
|
|---|
| 106 | procedure FreeMemoryOnDestroy; override;
|
|---|
| 107 | function CreateGlyph(AIdentifier: string): TRenderedGlyph; virtual;
|
|---|
| 108 | procedure CopyFontToRenderer; virtual;
|
|---|
| 109 | procedure DoTextOut(X, Y: Single; const Text : UTF8String; AColor: TBGRAPixel; AHorizontalAlign: TAlignment; AVerticalAlign: TTextLayout); overload; virtual;
|
|---|
| 110 | procedure DoTextOut(X, Y: Single; const Text : UTF8String; AColor: TBGRAPixel); overload; override;
|
|---|
| 111 | procedure DoTextRect(X, Y, Width, Height: Single; const Text : UTF8String; AColor: TBGRAPixel); override;
|
|---|
| 112 | function GetClipped: boolean; override;
|
|---|
| 113 | function GetUseGradientColors: boolean; override;
|
|---|
| 114 | procedure SetClipped(AValue: boolean); override;
|
|---|
| 115 | procedure SetUseGradientColors(AValue: boolean); override;
|
|---|
| 116 | procedure DiscardGlyphs; virtual;
|
|---|
| 117 | procedure DefaultWordBreakHandler(var ABefore, AAfter: string);
|
|---|
| 118 | procedure SplitText(var ATextUTF8: string; AMaxWidth: single; out ARemainsUTF8: string);
|
|---|
| 119 | function GetWrappedLines(ATextUTF8: string; AWidth: single): TStringList;
|
|---|
| 120 | public
|
|---|
| 121 | constructor Create(ARenderer: TBGRACustomFontRenderer; ARendererOwned: boolean = true);
|
|---|
| 122 | procedure FreeMemory; override;
|
|---|
| 123 | function TextWidth(const Text: UTF8String): single; override;
|
|---|
| 124 | function TextHeight(const {%H-}Text: UTF8String): single; override;
|
|---|
| 125 | function TextHeight(const Text: UTF8String; AWidth: single): single; override;
|
|---|
| 126 | procedure SetGradientColors(ATopLeft, ATopRight, ABottomRight, ABottomLeft: TBGRAPixel); override;
|
|---|
| 127 | property Name: string read GetName write SetName;
|
|---|
| 128 | property Style: TFontStyles read GetStyle write SetStyle;
|
|---|
| 129 | property Quality: TBGRAFontQuality read GetQuality write SetQuality;
|
|---|
| 130 | property EmHeight: integer read GetFontEmHeight write SetFontEmHeight;
|
|---|
| 131 | property FullHeight: integer read GetFontFullHeight write SetFontFullHeight;
|
|---|
| 132 | property Color: TBGRAPixel read GetColor write SetColor;
|
|---|
| 133 | property HorizontalOverflow: single read GetHorizontalOverflow write SetHorizontalOverflow;
|
|---|
| 134 | property VerticalOverflow: single read GetVerticalOverflow write SetVerticalOverflow;
|
|---|
| 135 | property BackgroundColor: TBGRAPixel read GetBackgroundColor write SetBackgroundColor;
|
|---|
| 136 | property WordBreakHandler: TWordBreakHandler read FWordBreakHandler write FWordBreakHandler;
|
|---|
| 137 | property Glyph[AIdentifier: string]: TRenderedGlyph read GetGlyph;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | implementation
|
|---|
| 141 |
|
|---|
| 142 | uses BGRAUTF8;
|
|---|
| 143 |
|
|---|
| 144 | { TRenderedGlyph }
|
|---|
| 145 |
|
|---|
| 146 | constructor TRenderedGlyph.Create(AIdentifier: UTF8String; ATexture: IBGLTexture;
|
|---|
| 147 | AHorizontalOverflowPx, AVerticalOverflowPx: integer);
|
|---|
| 148 | begin
|
|---|
| 149 | FIdentifier := AIdentifier;
|
|---|
| 150 | FTexture := ATexture;
|
|---|
| 151 | FHorizontalOverflowPx:= AHorizontalOverflowPx;
|
|---|
| 152 | FVerticalOverflowPx:= AVerticalOverflowPx;
|
|---|
| 153 | FAdvancePx := ATexture.Width - 2*FHorizontalOverflowPx;
|
|---|
| 154 | end;
|
|---|
| 155 |
|
|---|
| 156 | procedure TRenderedGlyph.Draw(x, y, Scale: single; AColor: TBGRAPixel);
|
|---|
| 157 | begin
|
|---|
| 158 | FTexture.StretchDraw(x-FHorizontalOverflowPx*Scale,y-FVerticalOverflowPx*Scale, FTexture.Width*Scale, FTexture.Height*Scale, AColor);
|
|---|
| 159 | end;
|
|---|
| 160 |
|
|---|
| 161 | procedure TRenderedGlyph.Draw(x, y, Scale: single; AGradTopLeft, AGradTopRight,
|
|---|
| 162 | AGradBottomRight, AGradBottomLeft: TBGRAPixel);
|
|---|
| 163 | begin
|
|---|
| 164 | FTexture.SetGradientColors(AGradTopLeft,AGradTopRight, AGradBottomRight,AGradBottomLeft);
|
|---|
| 165 | FTexture.StretchDraw(x-FHorizontalOverflowPx*Scale,y-FVerticalOverflowPx*Scale, FTexture.Width*Scale, FTexture.Height*Scale);
|
|---|
| 166 | FTexture.GradientColors := false;
|
|---|
| 167 | end;
|
|---|
| 168 |
|
|---|
| 169 | { TBGLRenderedFont }
|
|---|
| 170 |
|
|---|
| 171 | function TBGLRenderedFont.CompareGlyph(Tree: TAvgLvlTree; Data1, Data2: Pointer): integer;
|
|---|
| 172 | begin
|
|---|
| 173 | result := CompareStr(TRenderedGlyph(Data1).Identifier,TRenderedGlyph(Data2).Identifier);
|
|---|
| 174 | end;
|
|---|
| 175 |
|
|---|
| 176 | function TBGLRenderedFont.FindGlyph(AIdentifier: string): TAvgLvlTreeNode;
|
|---|
| 177 | var Comp: integer;
|
|---|
| 178 | Node: TAvgLvlTreeNode;
|
|---|
| 179 | begin
|
|---|
| 180 | Node:=FGlyphs.Root;
|
|---|
| 181 | while (Node<>nil) do begin
|
|---|
| 182 | Comp:=CompareStr(AIdentifier,TRenderedGlyph(Node.Data).Identifier);
|
|---|
| 183 | if Comp=0 then break;
|
|---|
| 184 | if Comp<0 then begin
|
|---|
| 185 | Node:=Node.Left
|
|---|
| 186 | end else begin
|
|---|
| 187 | Node:=Node.Right
|
|---|
| 188 | end;
|
|---|
| 189 | end;
|
|---|
| 190 | result := Node;
|
|---|
| 191 | end;
|
|---|
| 192 |
|
|---|
| 193 | function TBGLRenderedFont.GetBackgroundColor: TBGRAPixel;
|
|---|
| 194 | begin
|
|---|
| 195 | result := FBackgroundColor;
|
|---|
| 196 | end;
|
|---|
| 197 |
|
|---|
| 198 | function TBGLRenderedFont.GetColor: TBGRAPixel;
|
|---|
| 199 | begin
|
|---|
| 200 | result := FColor;
|
|---|
| 201 | end;
|
|---|
| 202 |
|
|---|
| 203 | function TBGLRenderedFont.GetFontEmHeight: integer;
|
|---|
| 204 | begin
|
|---|
| 205 | result := FEmHeight;
|
|---|
| 206 | end;
|
|---|
| 207 |
|
|---|
| 208 | function TBGLRenderedFont.CreateGlyph(AIdentifier: string): TRenderedGlyph;
|
|---|
| 209 | var b: TBGLCustomBitmap;
|
|---|
| 210 | hOverflow, vOverflow: integer;
|
|---|
| 211 | begin
|
|---|
| 212 | CopyFontToRenderer;
|
|---|
| 213 | with FRenderer.TextSize(AIdentifier) do
|
|---|
| 214 | begin
|
|---|
| 215 | hOverflow := round(cx*HorizontalOverflow)+1;
|
|---|
| 216 | vOverflow:= round(cy*VerticalOverflow)+1;
|
|---|
| 217 | b:= BGLBitmapFactory.Create(cx+2*hOverflow,cy+2*vOverflow,BackgroundColor);
|
|---|
| 218 | FRenderer.TextOut(b, hOverflow,vOverflow, AIdentifier, Color, taLeftJustify);
|
|---|
| 219 | result:= TRenderedGlyph.Create(AIdentifier,b.MakeTextureAndFree,hOverflow,vOverflow);
|
|---|
| 220 | end;
|
|---|
| 221 | end;
|
|---|
| 222 |
|
|---|
| 223 | procedure TBGLRenderedFont.CopyFontToRenderer;
|
|---|
| 224 | begin
|
|---|
| 225 | FRenderer.FontName := FName;
|
|---|
| 226 | FRenderer.FontEmHeight := FEmHeight;
|
|---|
| 227 | FRenderer.FontOrientation := 0;
|
|---|
| 228 | FRenderer.FontQuality := FQuality;
|
|---|
| 229 | FRenderer.FontStyle := FStyle;
|
|---|
| 230 | end;
|
|---|
| 231 |
|
|---|
| 232 | procedure TBGLRenderedFont.DoTextOut(X, Y: Single; const Text: UTF8String;
|
|---|
| 233 | AColor: TBGRAPixel; AHorizontalAlign: TAlignment; AVerticalAlign: TTextLayout);
|
|---|
| 234 | var
|
|---|
| 235 | pstr: pchar;
|
|---|
| 236 | left,charlen: integer;
|
|---|
| 237 | nextchar: string;
|
|---|
| 238 | g: TRenderedGlyph;
|
|---|
| 239 | begin
|
|---|
| 240 | if Text = '' then exit;
|
|---|
| 241 |
|
|---|
| 242 | pstr := @Text[1];
|
|---|
| 243 | left := length(Text);
|
|---|
| 244 | case AHorizontalAlign of
|
|---|
| 245 | taCenter: x -= round(TextWidth(Text)/2);
|
|---|
| 246 | taRightJustify: x -= TextWidth(Text);
|
|---|
| 247 | end;
|
|---|
| 248 | case AVerticalAlign of
|
|---|
| 249 | tlCenter: y -= round(TextHeight(Text)/2);
|
|---|
| 250 | tlBottom: y -= TextHeight(Text)*Scale;
|
|---|
| 251 | end;
|
|---|
| 252 | while left > 0 do
|
|---|
| 253 | begin
|
|---|
| 254 | charlen := UTF8CharacterLength(pstr);
|
|---|
| 255 | setlength(nextchar, charlen);
|
|---|
| 256 | move(pstr^, nextchar[1], charlen);
|
|---|
| 257 | inc(pstr,charlen);
|
|---|
| 258 | dec(left,charlen);
|
|---|
| 259 |
|
|---|
| 260 | g := GetGlyph(nextchar);
|
|---|
| 261 | if g <> nil then
|
|---|
| 262 | begin
|
|---|
| 263 | if FUseGradientColor then
|
|---|
| 264 | g.Draw(x,y,Scale,FGradTopLeft,FGradTopRight,FGradBottomRight,FGradBottomLeft)
|
|---|
| 265 | else
|
|---|
| 266 | g.Draw(x,y,Scale,AColor);
|
|---|
| 267 | x += (g.AdvancePx + StepX) * Scale;
|
|---|
| 268 | end;
|
|---|
| 269 | end;
|
|---|
| 270 | end;
|
|---|
| 271 |
|
|---|
| 272 | procedure TBGLRenderedFont.DoTextOut(X, Y: Single; const Text: UTF8String;
|
|---|
| 273 | AColor: TBGRAPixel);
|
|---|
| 274 | begin
|
|---|
| 275 | if Justify then
|
|---|
| 276 | DoTextOut(X,Y,Text,AColor,taLeftJustify,VerticalAlign)
|
|---|
| 277 | else
|
|---|
| 278 | DoTextOut(X,Y,Text,AColor,HorizontalAlign,VerticalAlign);
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | procedure TBGLRenderedFont.DoTextRect(X, Y, Width, Height: Single;
|
|---|
| 282 | const Text: UTF8String; AColor: TBGRAPixel);
|
|---|
| 283 |
|
|---|
| 284 | procedure DoDrawTextLine(LineY, LineWidth: Single; ALine: string; AJustify: boolean);
|
|---|
| 285 | var CurX: single;
|
|---|
| 286 | words: TStringList;
|
|---|
| 287 | wordStart: integer;
|
|---|
| 288 | i: Integer;
|
|---|
| 289 | begin
|
|---|
| 290 | if AJustify then
|
|---|
| 291 | begin
|
|---|
| 292 | words := TStringList.Create;
|
|---|
| 293 | wordStart := 1;
|
|---|
| 294 | for i := 1 to length(ALine) do
|
|---|
| 295 | begin
|
|---|
| 296 | if ALine[i]=' ' then
|
|---|
| 297 | begin
|
|---|
| 298 | words.Add(copy(ALine,wordStart,i-wordStart));
|
|---|
| 299 | wordStart := i+1;
|
|---|
| 300 | end;
|
|---|
| 301 | end;
|
|---|
| 302 | words.add(copy(ALine,wordStart,length(ALine)+1-wordStart));
|
|---|
| 303 | CurX := X;
|
|---|
| 304 | LineWidth := 0;
|
|---|
| 305 | for i := 0 to words.Count-1 do
|
|---|
| 306 | LineWidth += TextWidth(words[i]);
|
|---|
| 307 |
|
|---|
| 308 | for i := 0 to words.Count-1 do
|
|---|
| 309 | begin
|
|---|
| 310 | DoTextOut(CurX+round((Width-LineWidth)/(words.Count-1)*i),LineY,words[i],AColor,taLeftJustify,tlTop);
|
|---|
| 311 | CurX += TextWidth(words[i]);
|
|---|
| 312 | end;
|
|---|
| 313 | words.Free;
|
|---|
| 314 | end else
|
|---|
| 315 | begin
|
|---|
| 316 | Case HorizontalAlign of
|
|---|
| 317 | taCenter: CurX := round(X+(Width-LineWidth)/2);
|
|---|
| 318 | taRightJustify: CurX := X+Width-LineWidth;
|
|---|
| 319 | else
|
|---|
| 320 | CurX := X;
|
|---|
| 321 | end;
|
|---|
| 322 | DoTextOut(CurX,LineY,ALine,AColor,taLeftJustify,tlTop);
|
|---|
| 323 | end;
|
|---|
| 324 | end;
|
|---|
| 325 |
|
|---|
| 326 | var CurY: Single;
|
|---|
| 327 | lineHeight: Single;
|
|---|
| 328 | lines: TStringList;
|
|---|
| 329 | i,originalNbLines: Integer;
|
|---|
| 330 | maxLineCount: int64;
|
|---|
| 331 | begin
|
|---|
| 332 | If Text='' then exit;
|
|---|
| 333 | lines := GetWrappedLines(Text,Width);
|
|---|
| 334 | lineHeight := FullHeight * Scale;
|
|---|
| 335 | originalNbLines := lines.Count;
|
|---|
| 336 |
|
|---|
| 337 | if Clipped then
|
|---|
| 338 | begin
|
|---|
| 339 | if lineHeight = 0 then exit;
|
|---|
| 340 | maxLineCount := trunc(Height/lineHeight);
|
|---|
| 341 | if maxLineCount <= 0 then exit;
|
|---|
| 342 | while lines.Count > maxLineCount do
|
|---|
| 343 | lines.Delete(lines.Count-1);
|
|---|
| 344 | end;
|
|---|
| 345 |
|
|---|
| 346 | case VerticalAlign of
|
|---|
| 347 | tlCenter: CurY := round(Y+( Height - lines.Count*lineHeight )/2);
|
|---|
| 348 | tlBottom: CurY := Y + Height - lines.Count*lineHeight;
|
|---|
| 349 | else CurY := Y;
|
|---|
| 350 | end;
|
|---|
| 351 |
|
|---|
| 352 | for i := 0 to lines.Count-1 do
|
|---|
| 353 | begin
|
|---|
| 354 | DoDrawTextLine(CurY,TextWidth(lines[i]),lines[i],Justify and (i<>originalNbLines-1));
|
|---|
| 355 | CurY += lineHeight;
|
|---|
| 356 | end;
|
|---|
| 357 | lines.Free;
|
|---|
| 358 | end;
|
|---|
| 359 |
|
|---|
| 360 | function TBGLRenderedFont.GetGlyph(AIdentifier: string): TRenderedGlyph;
|
|---|
| 361 | var Node: TAvgLvlTreeNode;
|
|---|
| 362 | begin
|
|---|
| 363 | Node := FindGlyph(AIdentifier);
|
|---|
| 364 | if Node = nil then
|
|---|
| 365 | begin
|
|---|
| 366 | if UTF8Length(AIdentifier)<>1 then
|
|---|
| 367 | result := nil
|
|---|
| 368 | else
|
|---|
| 369 | begin
|
|---|
| 370 | result := CreateGlyph(AIdentifier);
|
|---|
| 371 | SetGlyph(AIdentifier, result);
|
|---|
| 372 | end;
|
|---|
| 373 | end
|
|---|
| 374 | else
|
|---|
| 375 | result := TRenderedGlyph(Node.Data);
|
|---|
| 376 | end;
|
|---|
| 377 |
|
|---|
| 378 | function TBGLRenderedFont.GetHorizontalOverflow: single;
|
|---|
| 379 | begin
|
|---|
| 380 | result := FHorizontalOverflow;
|
|---|
| 381 | end;
|
|---|
| 382 |
|
|---|
| 383 | function TBGLRenderedFont.GetName: string;
|
|---|
| 384 | begin
|
|---|
| 385 | result := FName;
|
|---|
| 386 | end;
|
|---|
| 387 |
|
|---|
| 388 | function TBGLRenderedFont.GetQuality: TBGRAFontQuality;
|
|---|
| 389 | begin
|
|---|
| 390 | result := FQuality;
|
|---|
| 391 | end;
|
|---|
| 392 |
|
|---|
| 393 | function TBGLRenderedFont.GetStyle: TFontStyles;
|
|---|
| 394 | begin
|
|---|
| 395 | result := FStyle;
|
|---|
| 396 | end;
|
|---|
| 397 |
|
|---|
| 398 | function TBGLRenderedFont.GetVerticalOverflow: single;
|
|---|
| 399 | begin
|
|---|
| 400 | result := FVerticalOverflow;
|
|---|
| 401 | end;
|
|---|
| 402 |
|
|---|
| 403 | procedure TBGLRenderedFont.SetGlyph(AIdentifier: string; AValue: TRenderedGlyph);
|
|---|
| 404 | var Node: TAvgLvlTreeNode;
|
|---|
| 405 | begin
|
|---|
| 406 | if AValue.Identifier <> AIdentifier then
|
|---|
| 407 | raise exception.Create('Identifier mismatch');
|
|---|
| 408 | Node := FindGlyph(AIdentifier);
|
|---|
| 409 | if Node <> nil then
|
|---|
| 410 | begin
|
|---|
| 411 | if pointer(AValue) <> Node.Data then
|
|---|
| 412 | TRenderedGlyph(Node.Data).Free;
|
|---|
| 413 | Node.Data := AValue;
|
|---|
| 414 | end else
|
|---|
| 415 | FGlyphs.Add(pointer(AValue));
|
|---|
| 416 | end;
|
|---|
| 417 |
|
|---|
| 418 | procedure TBGLRenderedFont.SetStyle(AValue: TFontStyles);
|
|---|
| 419 | begin
|
|---|
| 420 | if FStyle=AValue then Exit;
|
|---|
| 421 | FStyle:=AValue;
|
|---|
| 422 | DiscardGlyphs;
|
|---|
| 423 | end;
|
|---|
| 424 |
|
|---|
| 425 | procedure TBGLRenderedFont.SetVerticalOverflow(AValue: single);
|
|---|
| 426 | begin
|
|---|
| 427 | if FVerticalOverflow=AValue then Exit;
|
|---|
| 428 | FVerticalOverflow:=AValue;
|
|---|
| 429 | DiscardGlyphs;
|
|---|
| 430 | end;
|
|---|
| 431 |
|
|---|
| 432 | function TBGLRenderedFont.GetClipped: boolean;
|
|---|
| 433 | begin
|
|---|
| 434 | result := FClipped;
|
|---|
| 435 | end;
|
|---|
| 436 |
|
|---|
| 437 | function TBGLRenderedFont.GetUseGradientColors: boolean;
|
|---|
| 438 | begin
|
|---|
| 439 | result := FUseGradientColor;
|
|---|
| 440 | end;
|
|---|
| 441 |
|
|---|
| 442 | procedure TBGLRenderedFont.SetClipped(AValue: boolean);
|
|---|
| 443 | begin
|
|---|
| 444 | FClipped:= AValue;
|
|---|
| 445 | end;
|
|---|
| 446 |
|
|---|
| 447 | procedure TBGLRenderedFont.SetUseGradientColors(AValue: boolean);
|
|---|
| 448 | begin
|
|---|
| 449 | FUseGradientColor:= AValue;
|
|---|
| 450 | end;
|
|---|
| 451 |
|
|---|
| 452 | procedure TBGLRenderedFont.DiscardGlyphs;
|
|---|
| 453 | begin
|
|---|
| 454 | FGlyphs.FreeAndClear;
|
|---|
| 455 | end;
|
|---|
| 456 |
|
|---|
| 457 | procedure TBGLRenderedFont.DefaultWordBreakHandler(var ABefore, AAfter: string);
|
|---|
| 458 | begin
|
|---|
| 459 | BGRADefaultWordBreakHandler(ABefore,AAfter);
|
|---|
| 460 | end;
|
|---|
| 461 |
|
|---|
| 462 | function TBGLRenderedFont.GetWrappedLines(ATextUTF8: string; AWidth: single
|
|---|
| 463 | ): TStringList;
|
|---|
| 464 | var
|
|---|
| 465 | ARemains: string;
|
|---|
| 466 | begin
|
|---|
| 467 | result := TStringList.Create;
|
|---|
| 468 | repeat
|
|---|
| 469 | SplitText(ATextUTF8, AWidth, ARemains);
|
|---|
| 470 | result.Add(ATextUTF8);
|
|---|
| 471 | ATextUTF8 := ARemains;
|
|---|
| 472 | until ARemains = '';
|
|---|
| 473 | end;
|
|---|
| 474 |
|
|---|
| 475 | procedure TBGLRenderedFont.SplitText(var ATextUTF8: string; AMaxWidth: single;
|
|---|
| 476 | out ARemainsUTF8: string);
|
|---|
| 477 | var
|
|---|
| 478 | pstr: pchar;
|
|---|
| 479 | p,left,charlen: integer;
|
|---|
| 480 | totalWidth: single;
|
|---|
| 481 | firstChar: boolean;
|
|---|
| 482 | nextchar: string;
|
|---|
| 483 | g: TRenderedGlyph;
|
|---|
| 484 | begin
|
|---|
| 485 | totalWidth := 0;
|
|---|
| 486 | if ATextUTF8 = '' then
|
|---|
| 487 | begin
|
|---|
| 488 | ARemainsUTF8 := '';
|
|---|
| 489 | exit;
|
|---|
| 490 | end else
|
|---|
| 491 | begin
|
|---|
| 492 | p := 1;
|
|---|
| 493 | pstr := @ATextUTF8[1];
|
|---|
| 494 | left := length(ATextUTF8);
|
|---|
| 495 | firstChar := true;
|
|---|
| 496 | while left > 0 do
|
|---|
| 497 | begin
|
|---|
| 498 | if RemoveLineEnding(ATextUTF8,p) then
|
|---|
| 499 | begin
|
|---|
| 500 | ARemainsUTF8 := copy(ATextUTF8,p,length(ATextUTF8)-p+1);
|
|---|
| 501 | ATextUTF8 := copy(ATextUTF8,1,p-1);
|
|---|
| 502 | exit;
|
|---|
| 503 | end;
|
|---|
| 504 |
|
|---|
| 505 | charlen := UTF8CharacterLength(pstr);
|
|---|
| 506 | setlength(nextchar, charlen);
|
|---|
| 507 | move(pstr^, nextchar[1], charlen);
|
|---|
| 508 | inc(pstr,charlen);
|
|---|
| 509 |
|
|---|
| 510 | g := GetGlyph(nextchar);
|
|---|
| 511 | if g <> nil then
|
|---|
| 512 | begin
|
|---|
| 513 | if not firstChar then totalWidth += StepX*Scale;
|
|---|
| 514 | totalWidth += g.AdvancePx*Scale;
|
|---|
| 515 | if not firstChar and (totalWidth > AMaxWidth) then
|
|---|
| 516 | begin
|
|---|
| 517 | ARemainsUTF8:= copy(ATextUTF8,p,length(ATextUTF8)-p+1);
|
|---|
| 518 | ATextUTF8 := copy(ATextUTF8,1,p-1);
|
|---|
| 519 | if Assigned(FWordBreakHandler) then
|
|---|
| 520 | FWordBreakHandler(ATextUTF8,ARemainsUTF8) else
|
|---|
| 521 | DefaultWordBreakHandler(ATextUTF8,ARemainsUTF8);
|
|---|
| 522 | exit;
|
|---|
| 523 | end;
|
|---|
| 524 | end;
|
|---|
| 525 |
|
|---|
| 526 | dec(left,charlen);
|
|---|
| 527 | inc(p,charlen);
|
|---|
| 528 | firstChar := false;
|
|---|
| 529 | end;
|
|---|
| 530 | end;
|
|---|
| 531 | ARemainsUTF8 := ''; //no split
|
|---|
| 532 | end;
|
|---|
| 533 |
|
|---|
| 534 | procedure TBGLRenderedFont.SetName(AValue: string);
|
|---|
| 535 | begin
|
|---|
| 536 | if FName=AValue then Exit;
|
|---|
| 537 | FName:=AValue;
|
|---|
| 538 | DiscardGlyphs;
|
|---|
| 539 | end;
|
|---|
| 540 |
|
|---|
| 541 | procedure TBGLRenderedFont.SetFontEmHeight(AValue: integer);
|
|---|
| 542 | begin
|
|---|
| 543 | if FEmHeight=AValue then Exit;
|
|---|
| 544 | FEmHeight:=AValue;
|
|---|
| 545 | DiscardGlyphs;
|
|---|
| 546 | end;
|
|---|
| 547 |
|
|---|
| 548 | function TBGLRenderedFont.GetFontFullHeight: integer;
|
|---|
| 549 | begin
|
|---|
| 550 | if FEmHeight < 0 then
|
|---|
| 551 | result := -EmHeight
|
|---|
| 552 | else
|
|---|
| 553 | result := FRenderer.TextSize('Hg').cy;
|
|---|
| 554 | end;
|
|---|
| 555 |
|
|---|
| 556 | procedure TBGLRenderedFont.SetBackgroundColor(AValue: TBGRAPixel);
|
|---|
| 557 | begin
|
|---|
| 558 | if FBackgroundColor=AValue then Exit;
|
|---|
| 559 | FBackgroundColor:=AValue;
|
|---|
| 560 | DiscardGlyphs;
|
|---|
| 561 | end;
|
|---|
| 562 |
|
|---|
| 563 | procedure TBGLRenderedFont.SetColor(AValue: TBGRAPixel);
|
|---|
| 564 | begin
|
|---|
| 565 | if FColor=AValue then Exit;
|
|---|
| 566 | FColor:=AValue;
|
|---|
| 567 | DiscardGlyphs;
|
|---|
| 568 | end;
|
|---|
| 569 |
|
|---|
| 570 | procedure TBGLRenderedFont.SetFontFullHeight(AValue: integer);
|
|---|
| 571 | begin
|
|---|
| 572 | EmHeight:= -AValue;
|
|---|
| 573 | end;
|
|---|
| 574 |
|
|---|
| 575 | procedure TBGLRenderedFont.SetHorizontalOverflow(AValue: single);
|
|---|
| 576 | begin
|
|---|
| 577 | if FHorizontalOverflow=AValue then Exit;
|
|---|
| 578 | FHorizontalOverflow:=AValue;
|
|---|
| 579 | DiscardGlyphs;
|
|---|
| 580 | end;
|
|---|
| 581 |
|
|---|
| 582 | procedure TBGLRenderedFont.SetQuality(AValue: TBGRAFontQuality);
|
|---|
| 583 | begin
|
|---|
| 584 | if FQuality=AValue then Exit;
|
|---|
| 585 | FQuality:=AValue;
|
|---|
| 586 | DiscardGlyphs;
|
|---|
| 587 | end;
|
|---|
| 588 |
|
|---|
| 589 | function TBGLRenderedFont.LoadFromFile(AFilename: UTF8String): boolean;
|
|---|
| 590 | begin
|
|---|
| 591 | result := false;
|
|---|
| 592 | end;
|
|---|
| 593 |
|
|---|
| 594 | procedure TBGLRenderedFont.FreeMemoryOnDestroy;
|
|---|
| 595 | begin
|
|---|
| 596 | FreeMemory;
|
|---|
| 597 | if FRendererOwned then FreeAndNil(FRenderer);
|
|---|
| 598 | FreeAndNil(FGlyphs);
|
|---|
| 599 | end;
|
|---|
| 600 |
|
|---|
| 601 | constructor TBGLRenderedFont.Create(ARenderer: TBGRACustomFontRenderer;
|
|---|
| 602 | ARendererOwned: boolean);
|
|---|
| 603 | begin
|
|---|
| 604 | Init;
|
|---|
| 605 | FRenderer := ARenderer;
|
|---|
| 606 | FRendererOwned := ARendererOwned;
|
|---|
| 607 |
|
|---|
| 608 | FName := 'Arial';
|
|---|
| 609 | FColor := BGRAWhite;
|
|---|
| 610 | FBackgroundColor := BGRAPixelTransparent;
|
|---|
| 611 | FEmHeight := 20;
|
|---|
| 612 | FStyle := [];
|
|---|
| 613 | FHorizontalOverflow := 0.33;
|
|---|
| 614 | FVerticalOverflow := 0;
|
|---|
| 615 | FQuality := fqFineAntialiasing;
|
|---|
| 616 |
|
|---|
| 617 | FGradTopLeft := BGRAWhite;
|
|---|
| 618 | FGradTopRight := BGRAWhite;
|
|---|
| 619 | FGradBottomLeft := BGRAWhite;
|
|---|
| 620 | FGradBottomRight := BGRAWhite;
|
|---|
| 621 | FUseGradientColor:= false;
|
|---|
| 622 | FClipped:= false;
|
|---|
| 623 |
|
|---|
| 624 | FGlyphs := TAvgLvlTree.CreateObjectCompare(@CompareGlyph);
|
|---|
| 625 | FWordBreakHandler:= nil;
|
|---|
| 626 | end;
|
|---|
| 627 |
|
|---|
| 628 | procedure TBGLRenderedFont.FreeMemory;
|
|---|
| 629 | begin
|
|---|
| 630 | DiscardGlyphs;
|
|---|
| 631 | inherited FreeMemory;
|
|---|
| 632 | end;
|
|---|
| 633 |
|
|---|
| 634 | function TBGLRenderedFont.TextWidth(const Text: UTF8String): single;
|
|---|
| 635 | var
|
|---|
| 636 | pstr: pchar;
|
|---|
| 637 | left,charlen: integer;
|
|---|
| 638 | nextchar: string;
|
|---|
| 639 | g: TRenderedGlyph;
|
|---|
| 640 | firstChar: boolean;
|
|---|
| 641 | begin
|
|---|
| 642 | result := 0;
|
|---|
| 643 | if Text = '' then exit;
|
|---|
| 644 |
|
|---|
| 645 | firstChar := true;
|
|---|
| 646 | pstr := @Text[1];
|
|---|
| 647 | left := length(Text);
|
|---|
| 648 | while left > 0 do
|
|---|
| 649 | begin
|
|---|
| 650 | charlen := UTF8CharacterLength(pstr);
|
|---|
| 651 | setlength(nextchar, charlen);
|
|---|
| 652 | move(pstr^, nextchar[1], charlen);
|
|---|
| 653 | inc(pstr,charlen);
|
|---|
| 654 | dec(left,charlen);
|
|---|
| 655 |
|
|---|
| 656 | g := GetGlyph(nextchar);
|
|---|
| 657 | if g <> nil then
|
|---|
| 658 | begin
|
|---|
| 659 | if firstChar then
|
|---|
| 660 | firstchar := false
|
|---|
| 661 | else
|
|---|
| 662 | result += StepX * Scale;
|
|---|
| 663 | result += g.AdvancePx * Scale;
|
|---|
| 664 | end;
|
|---|
| 665 | end;
|
|---|
| 666 | end;
|
|---|
| 667 |
|
|---|
| 668 | function TBGLRenderedFont.TextHeight(const Text: UTF8String): single;
|
|---|
| 669 | begin
|
|---|
| 670 | result := FullHeight * Scale;
|
|---|
| 671 | end;
|
|---|
| 672 |
|
|---|
| 673 | function TBGLRenderedFont.TextHeight(const Text: UTF8String; AWidth: single
|
|---|
| 674 | ): single;
|
|---|
| 675 | var
|
|---|
| 676 | lines: TStringList;
|
|---|
| 677 | begin
|
|---|
| 678 | lines := GetWrappedLines(Text, AWidth);
|
|---|
| 679 | result := lines.Count * (FullHeight * Scale);
|
|---|
| 680 | lines.Free;
|
|---|
| 681 | end;
|
|---|
| 682 |
|
|---|
| 683 | procedure TBGLRenderedFont.SetGradientColors(ATopLeft, ATopRight, ABottomRight,
|
|---|
| 684 | ABottomLeft: TBGRAPixel);
|
|---|
| 685 | begin
|
|---|
| 686 | FGradTopLeft := ATopLeft;
|
|---|
| 687 | FGradTopRight := ATopRight;
|
|---|
| 688 | FGradBottomLeft := ABottomLeft;
|
|---|
| 689 | FGradBottomRight := ABottomRight;
|
|---|
| 690 | GradientColors := true;
|
|---|
| 691 | end;
|
|---|
| 692 |
|
|---|
| 693 | end.
|
|---|
| 694 |
|
|---|