| 1 |
{%MainUnit castlefonts.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
type |
| 20 |
{ Abstract class for a font that can be used to render text. } |
| 21 |
TCastleAbstractFont = class abstract(TCastleComponent) |
| 22 |
strict private |
| 23 |
type |
| 24 |
TSavedProperties = class |
| 25 |
Size: Single; |
| 26 |
Outline: Cardinal; |
| 27 |
OutlineColor: TCastleColor; |
| 28 |
OutlineHighQuality: boolean; |
| 29 |
TargetImage: TCastleImage; |
| 30 |
end; |
| 31 |
TSavedPropertiesList = {$ifdef FPC}specialize{$endif} TObjectList<TSavedProperties>; |
| 32 |
var |
| 33 |
FMeasuredSize, FMeasuredHeight, FMeasuredCapHeight, FMeasuredDescenderHeight: Single; |
| 34 |
FMeasureHeight, FMeasureCapHeight, FMeasureDescenderHeight: String; |
| 35 |
FOutline: Cardinal; |
| 36 |
FOutlineColor: TCastleColor; |
| 37 |
FOutlineHighQuality: boolean; |
| 38 |
FTargetImage: TCastleImage; |
| 39 |
FPropertiesStack: TSavedPropertiesList; |
| 40 |
FSize: Single; |
| 41 |
FFontSizeNotifications: TNotifyEventList; |
| 42 |
procedure MakeMeasure; |
| 43 |
procedure GLContextCloseEvent(Sender: TObject); |
| 44 |
{$ifdef FPC}strict{$endif} protected |
| 45 |
{ Calculate properties based on measuring the current font |
| 46 |
(with current @link(Size)). |
| 47 |
The default implementation in TCastleAbstractFont looks at TextHeight of sample texts |
| 48 |
to determine the parameter values. } |
| 49 |
procedure Measure(out AHeight, ACapHeight, ADescenderHeight: Single); virtual; |
| 50 |
procedure GLContextClose; virtual; |
| 51 |
procedure SetSize(const Value: Single); virtual; |
| 52 |
{ Call this when the measurement of font possibly changed, and anything cached |
| 53 |
should be recalculated by calling TextWidth, TextHeight etc. again. } |
| 54 |
procedure FontSizesChanged; |
| 55 |
procedure SetOutline(const Value: Cardinal); |
| 56 |
procedure SetMeasureHeight(const Value: String); |
| 57 |
procedure SetMeasureCapHeight(const Value: String); |
| 58 |
procedure SetMeasureDescenderHeight(const Value: String); |
| 59 |
function StoreMeasureHeight: Boolean; |
| 60 |
function StoreMeasureCapHeight: Boolean; |
| 61 |
function StoreMeasureDescenderHeight: Boolean; |
| 62 |
public |
| 63 |
const |
| 64 |
DefaultMeasureHeight = 'AOHIypq'; |
| 65 |
DefaultMeasureCapHeight = 'HI'; |
| 66 |
DefaultMeasureDescenderHeight = 'ya'; |
| 67 |
|
| 68 |
constructor Create(AOwner: TComponent); override; |
| 69 |
destructor Destroy; override; |
| 70 |
|
| 71 |
{ If the font is loaded, it can actually display and measure some characters. } |
| 72 |
function FontLoaded: Boolean; virtual; abstract; |
| 73 |
|
| 74 |
{ Desired font size to use when rendering (@link(Print)) and measuring |
| 75 |
(@link(TextWidth), @link(TextHeight) and related). |
| 76 |
Should be always > 0. |
| 77 |
|
| 78 |
The font size should correspond to the font height, which you can query using @link(Height). |
| 79 |
So in general @link(Size) and @link(Height) should be equal. |
| 80 |
That said, it is up to each specific font file to have correct size that satisfies above. |
| 81 |
This property, @name, gets and sets the size, as understood by FreeType font size. |
| 82 |
On the other hand, the @link(CapHeight) and @link(Height) properties |
| 83 |
actually query the letters height. } |
| 84 |
property Size: Single read FSize write SetSize; |
| 85 |
|
| 86 |
{ Draw text at the current WindowPos, and move |
| 87 |
the WindowPos at the end. This way you can immediately |
| 88 |
call another PrintAndMove again, to add something at the end. |
| 89 |
|
| 90 |
It is not adviced to use it, as using the global WindowPos leads |
| 91 |
sooner or later to messy in code, that has to deal with global state. |
| 92 |
If you need to know how to move after printing text, use TextMove. |
| 93 |
|
| 94 |
May require 1 free slot on the attributes stack. |
| 95 |
May only be called when current matrix is modelview. |
| 96 |
Doesn't modify any OpenGL state or matrix, except it moves raster position. } |
| 97 |
procedure PrintAndMove(const s: string); deprecated 'use Print(X, Y, ...), and move the (X, Y) yourself based on TextMove, instead of this'; |
| 98 |
|
| 99 |
{ Draw text at the given position with given color. |
| 100 |
If the last Color component is not 1, the text is rendered |
| 101 |
with blending. |
| 102 |
|
| 103 |
Overloaded version without X, Y uses WindowPos (but doesn't modify |
| 104 |
it, in contrast to PrintAndMove). |
| 105 |
Overloaded version without Color uses CurrentColor, |
| 106 |
last color set by glColorv. |
| 107 |
It is not adviced to use overloaded versions without X, Y or Color |
| 108 |
--- using global state leads to messy code. |
| 109 |
You should upgrade your code to use the version that gets X,Y,Color |
| 110 |
explicitly. |
| 111 |
|
| 112 |
May require 1 free slot on the attributes stack. |
| 113 |
May only be called when current matrix is modelview. |
| 114 |
Doesn't modify any OpenGL state or matrix, except it moves raster position. } |
| 115 |
procedure Print(const X, Y: Single; const Color: TCastleColor; |
| 116 |
const S: string); overload; virtual; abstract; |
| 117 |
procedure Print(const Pos: TVector2Integer; const Color: TCastleColor; |
| 118 |
const S: string); overload; |
| 119 |
procedure Print(const Pos: TVector2; const Color: TCastleColor; |
| 120 |
const S: string); overload; |
| 121 |
|
| 122 |
procedure Print(const X, Y: Single; const S: string); overload; deprecated 'instead of this, use Print overload that takes explicit X,Y,Color parameters'; |
| 123 |
procedure Print(const s: string); overload; deprecated 'instead of this, use Print overload that takes explicit X,Y,Color parameters'; |
| 124 |
|
| 125 |
{ Print text, aligning within given rectangle. |
| 126 |
|
| 127 |
Hint: Use TRectangle.Grow(-10) or similar to align within a rectangle |
| 128 |
with padding. } |
| 129 |
procedure PrintRect(const Rect: TRectangle; const Color: TCastleColor; |
| 130 |
const S: string; |
| 131 |
const HorizontalAlignment: THorizontalPosition; |
| 132 |
const VerticalAlignment: TVerticalPosition); overload; |
| 133 |
procedure PrintRect(const Rect: TFloatRectangle; const Color: TCastleColor; |
| 134 |
const S: string; |
| 135 |
const HorizontalAlignment: THorizontalPosition; |
| 136 |
const VerticalAlignment: TVerticalPosition); overload; |
| 137 |
|
| 138 |
{ Print text, aligning within given rectangle. |
| 139 |
Newlines within the text will be automatically honored, |
| 140 |
the text will be rendered as multiple lines. |
| 141 |
See @link(PrintStrings) for description of parameters Html, |
| 142 |
LineSpacing, TextHorizontalAlignment. } |
| 143 |
procedure PrintRectMultiline(const Rect: TRectangle; const Color: TCastleColor; |
| 144 |
const S: string; |
| 145 |
const HorizontalAlignment: THorizontalPosition; |
| 146 |
const VerticalAlignment: TVerticalPosition; |
| 147 |
const Html: boolean; |
| 148 |
const LineSpacing: Integer; |
| 149 |
const TextHorizontalAlignment: THorizontalPosition = hpLeft); overload; |
| 150 |
procedure PrintRectMultiline(const Rect: TFloatRectangle; const Color: TCastleColor; |
| 151 |
const S: string; |
| 152 |
const HorizontalAlignment: THorizontalPosition; |
| 153 |
const VerticalAlignment: TVerticalPosition; |
| 154 |
const Html: boolean; |
| 155 |
const LineSpacing: Integer; |
| 156 |
const TextHorizontalAlignment: THorizontalPosition = hpLeft); overload; |
| 157 |
|
| 158 |
{ The font may require some OpenGL resources for drawing. |
| 159 |
You can explicitly create them using PrepareResources (although it is never |
| 160 |
needed, resources will be automatically created if needed). |
| 161 |
There's no public method to explicitly destroy them, |
| 162 |
they are always destroyed automatically. |
| 163 |
@groupBegin } |
| 164 |
procedure PrepareResources; virtual; |
| 165 |
{ @groupEnd } |
| 166 |
|
| 167 |
function TextWidth(const S: string): Single; virtual; abstract; |
| 168 |
function TextHeight(const S: string): Single; virtual; abstract; |
| 169 |
{ The height (above the baseline) of the text. |
| 170 |
This doesn't take into account height of the text below the baseline |
| 171 |
(for example letter "y" has the tail below the baseline in most fonts). } |
| 172 |
function TextHeightBase(const S: string): Single; virtual; abstract; |
| 173 |
function TextMove(const S: string): TVector2; virtual; abstract; |
| 174 |
function TextSize(const S: string): TVector2; |
| 175 |
|
| 176 |
function RowHeight: Single; deprecated 'use Height'; |
| 177 |
function RowHeightBase: Single; deprecated 'use CapHeight'; |
| 178 |
function Descend: Single; deprecated 'use DescenderHeight'; |
| 179 |
|
| 180 |
{ Maximum height of font characters, taking into account whole descent and ascent. |
| 181 |
|
| 182 |
By default calculated as height of "AOHIypq", see the @link(MeasureHeight). |
| 183 |
|
| 184 |
See about standard font measurements: |
| 185 |
https://en.wikipedia.org/wiki/Typeface#Font_metrics , |
| 186 |
https://en.wikipedia.org/wiki/Metric_typographic_units . } |
| 187 |
function Height: Single; |
| 188 |
|
| 189 |
{ Height above the baseline of font characters. |
| 190 |
|
| 191 |
Compared to @link(Height), this does not take into account the characters |
| 192 |
size below the baseline, and it doesn't take into account some possible "overflow" |
| 193 |
on the top of some characters. |
| 194 |
|
| 195 |
By default calculated as height of "HI", see the MeasureCapHeight. |
| 196 |
|
| 197 |
The intention is that, while this doesn't represent the whole possible font height, |
| 198 |
but it often represents the perceived by user height of the block of text, |
| 199 |
as fonts are designed to make descents / overflows not stand out. |
| 200 |
|
| 201 |
See about standard font measurements: |
| 202 |
https://en.wikipedia.org/wiki/Typeface#Font_metrics , |
| 203 |
https://en.wikipedia.org/wiki/Metric_typographic_units . |
| 204 |
About "cap height" in particular: |
| 205 |
https://en.wikipedia.org/wiki/Cap_height |
| 206 |
https://fonts.google.com/knowledge/glossary/cap_height } |
| 207 |
function CapHeight: Single; |
| 208 |
|
| 209 |
{ How low the font characters may go below the baseline. |
| 210 |
By default calculated as a difference between "y" and "a" characters height, |
| 211 |
see the MeasureDescenderHeight. |
| 212 |
|
| 213 |
See about standard font measurements: |
| 214 |
https://en.wikipedia.org/wiki/Typeface#Font_metrics , |
| 215 |
https://en.wikipedia.org/wiki/Metric_typographic_units . } |
| 216 |
function DescenderHeight: Single; |
| 217 |
|
| 218 |
{ Break lines (possibly break one long string into more strings) |
| 219 |
to fit the text with given MaxLineWidth. |
| 220 |
|
| 221 |
This takes into account current font information (works also |
| 222 |
for non-monospace fonts, of course), and converts your Unbroken |
| 223 |
text into Broken text, such that TextWidth of the longest Broken |
| 224 |
line fits within MaxLineWidth. |
| 225 |
|
| 226 |
Tries to break on white characters. If not possible (there's |
| 227 |
a long stream of non-white characters that really has to be broken), |
| 228 |
it will break in the middle of normal (non-white) characters. |
| 229 |
The only situation when we have to fail, and the resulting |
| 230 |
Broken text is wider than required MaxLineWidth, is when |
| 231 |
@italic(a single character in your font) is wider than MaxLineWidth. |
| 232 |
In such case, there's really no solution, and we'll just let such |
| 233 |
character stay. |
| 234 |
|
| 235 |
If you use the overloaded version where Unbroken is just a string, |
| 236 |
then note that already existing newlines (NL) inside Unbroken |
| 237 |
will be correctly preserved. |
| 238 |
|
| 239 |
If you use the overloaded version with separate Unbroken and |
| 240 |
Broken parameters, then the previous Broken contents are not modified. |
| 241 |
We only append to Broken new strings, coming from Unbroken text. |
| 242 |
The overloaded version that takes only Broken parameter |
| 243 |
(no Unbroken parameter) simply modifies it's Broken parameter |
| 244 |
(from the line FirstToBreak). |
| 245 |
|
| 246 |
@groupBegin } |
| 247 |
procedure BreakLines(const unbroken: string; broken: TStrings; MaxLineWidth: Single); overload; |
| 248 |
procedure BreakLines(unbroken, broken: TStrings; MaxLineWidth: Single); overload; |
| 249 |
procedure BreakLines(broken: TStrings; MaxLineWidth: Single; FirstToBreak: integer); overload; |
| 250 |
{ @groupEnd } |
| 251 |
|
| 252 |
{ Largest width of the line of text in given list. |
| 253 |
|
| 254 |
@param(Html Indicates that strings inside SList use a subset of HTML, |
| 255 |
the same ones as interpreted by PrintStrings. |
| 256 |
If your SList uses these elements (for example, you plan to call later |
| 257 |
PrintStrings with the same SList and Html = @true) then make |
| 258 |
sure you pass Html = @true to this method. |
| 259 |
Otherwise, MaxTextWidth will treat HTML markup (like @code(<font ...>)) |
| 260 |
like a normal text, usually making the width incorrectly large.) |
| 261 |
} |
| 262 |
function MaxTextWidth(SList: TStrings; const Html: boolean = false): Single; |
| 263 |
|
| 264 |
{ Print all strings from the list. |
| 265 |
|
| 266 |
@param(X0 The X position of the whole text block. |
| 267 |
It's exact interpretation depends on TextHorizontalAlignment value.) |
| 268 |
|
| 269 |
@param(Y0 The bottom position of the whole text block. |
| 270 |
That is, this is the bottom position of the last string.) |
| 271 |
|
| 272 |
@param(Color The color of the text. Alpha value of the color is honored, |
| 273 |
value < 1 renders partially-transparent text. |
| 274 |
|
| 275 |
Overloaded and deprecated versions without |
| 276 |
explicit Color parameter use CurrentColor.) |
| 277 |
|
| 278 |
@param(Strs The text to display. Can be given as either TStringList, |
| 279 |
or a simple array of strings.) |
| 280 |
|
| 281 |
@param(Html Enable a subset of HTML to mark font changes inside the text. |
| 282 |
See the example examples/fonts/html_text_demo.html for a demo, supported |
| 283 |
HTML constructs now are: |
| 284 |
@unorderedList( |
| 285 |
@item <b> (bold) |
| 286 |
@item <i> (italic) |
| 287 |
@item <font color="#rrggbb">, <font color="#rrggbbaa"> (change color, with or without alpha) |
| 288 |
@item <font size="xxx">, <small> (change size) |
| 289 |
@item <br> <br/> <br /> <p> (newlines; paragraph makes 2 newlines) |
| 290 |
@item & < > ' " (entities) |
| 291 |
@item <!-- xxx --> (comments) |
| 292 |
) |
| 293 |
) |
| 294 |
|
| 295 |
@param(LineSpacing Extra space between lines. |
| 296 |
Distance between each line is determined by Height + LineSpacing |
| 297 |
pixels. |
| 298 |
|
| 299 |
Note that LineSpacing can be < 0 (as well as > 0), |
| 300 |
this may be sometimes useful if you really want to squeeze |
| 301 |
more text into the available space. Still, make sure that |
| 302 |
(Height + LineSpacing) is > 0.) |
| 303 |
|
| 304 |
@groupBegin } |
| 305 |
procedure PrintStrings(const X0, Y0: Single; const Color: TCastleColor; |
| 306 |
const Strs: TStrings; const Html: boolean; |
| 307 |
const LineSpacing: Single; |
| 308 |
const TextHorizontalAlignment: THorizontalPosition = hpLeft); overload; |
| 309 |
procedure PrintStrings(const X0, Y0: Single; const Color: TCastleColor; |
| 310 |
const Strs: array of string; const Html: boolean; |
| 311 |
const LineSpacing: Single; |
| 312 |
const TextHorizontalAlignment: THorizontalPosition = hpLeft); overload; |
| 313 |
procedure PrintStrings(const Strs: TStrings; |
| 314 |
const Html: boolean; const LineSpacing: Single; |
| 315 |
const X0: Single = 0; const Y0: Single = 0); overload; deprecated 'instead of this, use PrintStrings version that takes explicit Color parameter'; |
| 316 |
procedure PrintStrings(const Strs: array of string; |
| 317 |
const Html: boolean; const LineSpacing: Single; |
| 318 |
const X0: Single = 0; const Y0: Single = 0); overload; deprecated 'instead of this, use PrintStrings version that takes explicit Color parameter'; |
| 319 |
{ @groupEnd } |
| 320 |
|
| 321 |
{ Print the string, broken such that it fits within MaxLineWidth. |
| 322 |
The string is broken into many lines using BreakLines, |
| 323 |
so the original newlines insides are correctly used, |
| 324 |
and the length of lines fits inside MaxLineWidth. |
| 325 |
|
| 326 |
The strings are printed on the screen, just like by PrintStrings. |
| 327 |
If PositionsFirst then the X0, Y0 determine |
| 328 |
the position of the first (top) line, otherwise they determine |
| 329 |
the position of the last (bottom) line. |
| 330 |
|
| 331 |
LineSpacing has the same meaning as for PrintStrings: |
| 332 |
it adds an additional space between lines (if positive) or forces |
| 333 |
the lines to be more tightly squeezed (if negative). Always make |
| 334 |
sure that (Height + LineSpacing) > 0. |
| 335 |
|
| 336 |
Returns the number of lines printed, that is the number of lines |
| 337 |
after breaking the text into lines. This may be useful e.g. to calculate |
| 338 |
the height of the printed text. |
| 339 |
|
| 340 |
May require 1 free slot on the attributes stack. |
| 341 |
May only be called when current matrix is modelview. |
| 342 |
Doesn't modify any OpenGL state or matrix. |
| 343 |
|
| 344 |
Overloaded and deprecated version without |
| 345 |
explicit Color parameter uses CurrentColor. |
| 346 |
|
| 347 |
Overloaded version that takes rectangle as a parameter can |
| 348 |
align the resulting string box within the rectangle. |
| 349 |
|
| 350 |
@groupBegin } |
| 351 |
function PrintBrokenString(const Rect: TRectangle; const Color: TCastleColor; |
| 352 |
const S: string; |
| 353 |
const LineSpacing: Single; |
| 354 |
const AlignHorizontal: THorizontalPosition; |
| 355 |
const AlignVertical: TVerticalPosition; |
| 356 |
const Html: boolean = false): Integer; overload; |
| 357 |
function PrintBrokenString(const Rect: TFloatRectangle; const Color: TCastleColor; |
| 358 |
const S: string; |
| 359 |
const LineSpacing: Single; |
| 360 |
const AlignHorizontal: THorizontalPosition; |
| 361 |
const AlignVertical: TVerticalPosition; |
| 362 |
const Html: boolean = false): Integer; overload; |
| 363 |
function PrintBrokenString(X0, Y0: Single; const Color: TCastleColor; |
| 364 |
const S: string; const MaxLineWidth: Single; |
| 365 |
const PositionsFirst: boolean; |
| 366 |
const LineSpacing: Single; |
| 367 |
const Html: boolean = false): Integer; overload; |
| 368 |
function PrintBrokenString(const S: string; |
| 369 |
const MaxLineWidth, X0, Y0: Single; |
| 370 |
const PositionsFirst: boolean; |
| 371 |
const LineSpacing: Single): Integer; overload; deprecated 'instead of this, use PrintBrokenString that takes explicit Color parameter'; |
| 372 |
{ @groupEnd } |
| 373 |
|
| 374 |
{ Outline size around the normal text. |
| 375 |
Note that the current implementation is very simple, it will only |
| 376 |
look sensible for small outline values (like 1 or 2). |
| 377 |
|
| 378 |
Note that outline size, in pixels, is not scaled along with font size. |
| 379 |
Which makes sense: whether you load TTF with size 10 and then set size 20, |
| 380 |
or you load TTF with size 40 and then set size 20 |
| 381 |
--- the font is internally scaled differently, but the resulting sizes |
| 382 |
and outline sizes remain the same. |
| 383 |
|
| 384 |
@seealso OutlineHighQuality } |
| 385 |
property Outline: Cardinal read FOutline write SetOutline default 0; |
| 386 |
|
| 387 |
{ Optionally force better outline quality. Used only if Outline <> 0. |
| 388 |
High quality outline looks better, but is about 2x more expensive to draw. |
| 389 |
@seealso Outline } |
| 390 |
property OutlineHighQuality: boolean |
| 391 |
read FOutlineHighQuality write FOutlineHighQuality default false; |
| 392 |
|
| 393 |
{ Outline color, used only if Outline <> 0. Default is black. |
| 394 |
@seealso Outline } |
| 395 |
property OutlineColor: TCastleColor read FOutlineColor write FOutlineColor; |
| 396 |
|
| 397 |
{ Save draw properties to a stack. Saves: |
| 398 |
@link(Size), @link(Outline), @link(OutlineColor), @link(OutlineHighQuality), |
| 399 |
@link(TargetImage). } |
| 400 |
procedure PushProperties; |
| 401 |
procedure PopProperties; |
| 402 |
|
| 403 |
{ Non-zero font size. Usually same thing as @link(Size), but in case of proxy |
| 404 |
font classes (like TCustomizedFont and TCastleFontFamily) it makes sure to |
| 405 |
never return zero (which, in case of font proxies, |
| 406 |
is allowed value for @link(Size) and means "use underlying font size"). } |
| 407 |
function EffectiveSize: Single; virtual; |
| 408 |
|
| 409 |
function RealSize: Single; deprecated 'use EffectiveSize'; |
| 410 |
|
| 411 |
{ The image where we render the font. |
| 412 |
Usually (when this is @nil) our rendering routines render to the screen |
| 413 |
(or the current FBO, if you use @link(TGLRenderToTexture)). |
| 414 |
By setting this to non-nil, you make the rendering by done on CPU |
| 415 |
(without libraries like OpenGL/OpenGLES), and the text is drawn on the given image. |
| 416 |
|
| 417 |
The PushProperties and PopProperties methods save/restore this. |
| 418 |
|
| 419 |
TODO: Font scaling (normally done by TCastleFont and TCastleBitmapFont |
| 420 |
if you change @link(Size) from default) |
| 421 |
is not done when drawing font to an image. |
| 422 |
So don't touch the @link(Size) |
| 423 |
if you plan on rendering to image. Also, when using HTML tags, |
| 424 |
do not change the font size by them. |
| 425 |
Otherwise TextWidth / TextHeight will be unsynchronized |
| 426 |
with what @link(Print) actually does --- so not only your font will |
| 427 |
remain constant size, also it will overlap with itself. |
| 428 |
This will get fixed one day --- TCastleImage.Draw just needs to support scaling. |
| 429 |
} |
| 430 |
property TargetImage: TCastleImage read FTargetImage write FTargetImage; |
| 431 |
|
| 432 |
{ Add notification when FontSizesChanged occurs. } |
| 433 |
procedure AddFontSizeChangeNotification(const Notify: TNotifyEvent); |
| 434 |
{ Remove notification when FontSizesChanged occurs. } |
| 435 |
procedure RemoveFontSizeChangeNotification(const Notify: TNotifyEvent); |
| 436 |
published |
| 437 |
{ Use these characters to measure font @link(Height). } |
| 438 |
property MeasureHeight: String read FMeasureHeight write SetMeasureHeight |
| 439 |
stored StoreMeasureHeight; |
| 440 |
|
| 441 |
{ Use these characters to measure font @link(CapHeight). |
| 442 |
Note that we only measure the height above baseline (Y = 0), |
| 443 |
so even when MeasureHeight and MeasureCapHeight are equal, |
| 444 |
the resulting @link(Height) and @link(CapHeight) may differ. } |
| 445 |
property MeasureCapHeight: String read FMeasureCapHeight write SetMeasureCapHeight |
| 446 |
stored StoreMeasureCapHeight; |
| 447 |
|
| 448 |
{ Use these characters to measure font @link(DescenderHeight). |
| 449 |
|
| 450 |
This string must be empty (meaning that descend is zero) |
| 451 |
or has exactly 2 characters. |
| 452 |
The descender height is measured as height of 1st char - height of 2nd char. |
| 453 |
|
| 454 |
See DefaultMeasureDescenderHeight . } |
| 455 |
property MeasureDescenderHeight: String read FMeasureDescenderHeight write SetMeasureDescenderHeight |
| 456 |
stored StoreMeasureDescenderHeight; |
| 457 |
end; |
| 458 |
|
| 459 |
TGLBitmapFontAbstract = TCastleAbstractFont deprecated 'use TCastleAbstractFont'; |
| 460 |
|
| 461 |
{$endif read_interface} |
| 462 |
|
| 463 |
{$ifdef read_implementation} |
| 464 |
|
| 465 |
{ TCastleAbstractFont ------------------------------------------------------} |
| 466 |
|
| 467 |
constructor TCastleAbstractFont.Create(AOwner: TComponent); |
| 468 |
begin |
| 469 |
inherited; |
| 470 |
FMeasuredSize := -1; // not measured at all |
| 471 |
FOutlineColor := Black; |
| 472 |
FMeasureHeight := DefaultMeasureHeight; |
| 473 |
FMeasureCapHeight := DefaultMeasureCapHeight; |
| 474 |
FMeasureDescenderHeight := DefaultMeasureDescenderHeight; |
| 475 |
ApplicationProperties.OnGLContextCloseObject.Add({$ifdef FPC}@{$endif}GLContextCloseEvent); |
| 476 |
end; |
| 477 |
|
| 478 |
destructor TCastleAbstractFont.Destroy; |
| 479 |
begin |
| 480 |
if ApplicationProperties <> nil then |
| 481 |
ApplicationProperties.OnGLContextCloseObject.Remove({$ifdef FPC}@{$endif}GLContextCloseEvent); |
| 482 |
GLContextClose; |
| 483 |
FreeAndNil(FPropertiesStack); |
| 484 |
FreeAndNil(FFontSizeNotifications); |
| 485 |
inherited; |
| 486 |
end; |
| 487 |
|
| 488 |
procedure TCastleAbstractFont.SetSize(const Value: Single); |
| 489 |
begin |
| 490 |
if FSize <> Value then |
| 491 |
begin |
| 492 |
Assert(not IsInfinite(Value)); |
| 493 |
FSize := Value; |
| 494 |
end; |
| 495 |
end; |
| 496 |
|
| 497 |
procedure TCastleAbstractFont.GLContextCloseEvent(Sender: TObject); |
| 498 |
begin |
| 499 |
GLContextClose; |
| 500 |
end; |
| 501 |
|
| 502 |
procedure TCastleAbstractFont.PrepareResources; |
| 503 |
begin |
| 504 |
end; |
| 505 |
|
| 506 |
procedure TCastleAbstractFont.GLContextClose; |
| 507 |
begin |
| 508 |
end; |
| 509 |
|
| 510 |
function TCastleAbstractFont.TextSize(const S: string): TVector2; |
| 511 |
begin |
| 512 |
Result := Vector2(TextWidth(S), TextHeight(S)); |
| 513 |
end; |
| 514 |
|
| 515 |
procedure TCastleAbstractFont.Print(const Pos: TVector2Integer; |
| 516 |
const Color: TCastleColor; const S: string); |
| 517 |
begin |
| 518 |
Print(Pos[0], Pos[1], Color, S); |
| 519 |
end; |
| 520 |
|
| 521 |
procedure TCastleAbstractFont.Print(const Pos: TVector2; |
| 522 |
const Color: TCastleColor; const S: string); |
| 523 |
begin |
| 524 |
Print(Pos[0], Pos[1], Color, S); |
| 525 |
end; |
| 526 |
|
| 527 |
procedure TCastleAbstractFont.Print(const s: string); |
| 528 |
begin |
| 529 |
{ Deprecated method uses other deprecated method here, don't warn } |
| 530 |
{$warnings off} |
| 531 |
Print(WindowPos[0], WindowPos[1], CurrentColor, S); |
| 532 |
{$warnings on} |
| 533 |
end; |
| 534 |
|
| 535 |
procedure TCastleAbstractFont.PrintAndMove(const s: string); |
| 536 |
var |
| 537 |
M: TVector2; |
| 538 |
begin |
| 539 |
{ Deprecated method uses other deprecated method here, don't warn } |
| 540 |
{$warnings off} |
| 541 |
Print(S); |
| 542 |
M := TextMove(S); |
| 543 |
WindowPos := WindowPos + Vector2Integer(Round(M.X), Round(M.Y)); |
| 544 |
{$warnings on} |
| 545 |
end; |
| 546 |
|
| 547 |
procedure TCastleAbstractFont.Print(const X, Y: Single; const S: string); |
| 548 |
begin |
| 549 |
{ Deprecated method uses other deprecated method here, don't warn } |
| 550 |
{$warnings off} |
| 551 |
Print(X, Y, CurrentColor, S); |
| 552 |
{$warnings on} |
| 553 |
end; |
| 554 |
|
| 555 |
procedure TCastleAbstractFont.PrintRectMultiline(const Rect: TFloatRectangle; const Color: TCastleColor; |
| 556 |
const S: string; |
| 557 |
const HorizontalAlignment: THorizontalPosition; |
| 558 |
const VerticalAlignment: TVerticalPosition; |
| 559 |
const Html: boolean; |
| 560 |
const LineSpacing: Integer; |
| 561 |
const TextHorizontalAlignment: THorizontalPosition); |
| 562 |
var |
| 563 |
Strings: TStringList; |
| 564 |
ThisRect: TFloatRectangle; |
| 565 |
X: Single; |
| 566 |
begin |
| 567 |
Strings := TStringList.Create; |
| 568 |
try |
| 569 |
Strings.Text := S; |
| 570 |
if Strings.Count <> 0 then |
| 571 |
begin |
| 572 |
ThisRect := FloatRectangle(0, 0, MaxTextWidth(Strings, Html), |
| 573 |
Strings.Count * (LineSpacing + Height) - LineSpacing); |
| 574 |
ThisRect := ThisRect. |
| 575 |
Align(HorizontalAlignment, Rect, HorizontalAlignment). |
| 576 |
Align(VerticalAlignment, Rect, VerticalAlignment); |
| 577 |
case TextHorizontalAlignment of |
| 578 |
hpLeft : X := ThisRect.Left; |
| 579 |
hpMiddle : X := ThisRect.Center[0]; |
| 580 |
hpRight : X := ThisRect.Right; |
| 581 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 582 |
else raise EInternalError.Create('TextHorizontalAlignment? in TCastleAbstractFont.PrintRectMultiline'); |
| 583 |
{$endif} |
| 584 |
end; |
| 585 |
PrintStrings(X, ThisRect.Bottom, Color, Strings, |
| 586 |
Html, LineSpacing, TextHorizontalAlignment); |
| 587 |
end; |
| 588 |
finally FreeAndNil(Strings) end; |
| 589 |
end; |
| 590 |
|
| 591 |
procedure TCastleAbstractFont.PrintRectMultiline(const Rect: TRectangle; const Color: TCastleColor; |
| 592 |
const S: string; |
| 593 |
const HorizontalAlignment: THorizontalPosition; |
| 594 |
const VerticalAlignment: TVerticalPosition; |
| 595 |
const Html: boolean; |
| 596 |
const LineSpacing: Integer; |
| 597 |
const TextHorizontalAlignment: THorizontalPosition); |
| 598 |
begin |
| 599 |
PrintRectMultiline(FloatRectangle(Rect), Color, S, |
| 600 |
HorizontalAlignment, VerticalAlignment, Html, LineSpacing, TextHorizontalAlignment); |
| 601 |
end; |
| 602 |
|
| 603 |
procedure TCastleAbstractFont.PrintRect( |
| 604 |
const Rect: TFloatRectangle; const Color: TCastleColor; |
| 605 |
const S: string; |
| 606 |
const HorizontalAlignment: THorizontalPosition; |
| 607 |
const VerticalAlignment: TVerticalPosition); |
| 608 |
var |
| 609 |
ThisRect: TFloatRectangle; |
| 610 |
begin |
| 611 |
ThisRect := |
| 612 |
FloatRectangle(0, 0, TextWidth(S), TextHeight(S)). |
| 613 |
Align(HorizontalAlignment, Rect, HorizontalAlignment). |
| 614 |
Align(VerticalAlignment, Rect, VerticalAlignment); |
| 615 |
Print(ThisRect.Left, ThisRect.Bottom, Color, S); |
| 616 |
end; |
| 617 |
|
| 618 |
procedure TCastleAbstractFont.PrintRect( |
| 619 |
const Rect: TRectangle; const Color: TCastleColor; |
| 620 |
const S: string; |
| 621 |
const HorizontalAlignment: THorizontalPosition; |
| 622 |
const VerticalAlignment: TVerticalPosition); |
| 623 |
begin |
| 624 |
PrintRect(FloatRectangle(Rect), Color, S, HorizontalAlignment, VerticalAlignment); |
| 625 |
end; |
| 626 |
|
| 627 |
procedure TCastleAbstractFont.BreakLines(const unbroken: string; |
| 628 |
broken: TStrings; MaxLineWidth: Single); |
| 629 |
var |
| 630 |
unbrokenlist: TStringList; |
| 631 |
begin |
| 632 |
unbrokenlist := TStringList.Create; |
| 633 |
try |
| 634 |
Strings_SetText(unbrokenlist, unbroken); |
| 635 |
BreakLines(unbrokenlist, broken, MaxLineWidth); |
| 636 |
finally unbrokenlist.Free end; |
| 637 |
end; |
| 638 |
|
| 639 |
procedure TCastleAbstractFont.BreakLines(unbroken, broken: TStrings; |
| 640 |
MaxLineWidth: Single); |
| 641 |
var |
| 642 |
i, FirstToBreak: Integer; |
| 643 |
begin |
| 644 |
FirstToBreak := broken.count; |
| 645 |
for I := 0 to unbroken.count-1 do broken.Append(unbroken[i]); |
| 646 |
BreakLines(broken, MaxLineWidth, FirstToBreak); |
| 647 |
end; |
| 648 |
|
| 649 |
procedure TCastleAbstractFont.BreakLines(broken: TStrings; |
| 650 |
MaxLineWidth: Single; FirstToBreak: integer); |
| 651 |
var |
| 652 |
I: Integer; |
| 653 |
LineWidth, NewLineWidth: Single; |
| 654 |
LineUnicodeChars: Cardinal; |
| 655 |
P: Integer; |
| 656 |
BreakInput, BreakOutput1, BreakOutput2, HardBreakOutput: string; |
| 657 |
BreakInputIterator: TCastleStringIterator; |
| 658 |
BreakNeeded: Boolean; |
| 659 |
begin |
| 660 |
{ break the strings on Broken list. |
| 661 |
We look at MaxLineWidth in pixels, taking into account that font |
| 662 |
does not have to be mono-spaced. } |
| 663 |
|
| 664 |
I := FirstToBreak; |
| 665 |
{ instead of "for" use "while" because Broken.Count will be changing } |
| 666 |
while I < Broken.Count do |
| 667 |
begin |
| 668 |
BreakInput := Broken[I]; |
| 669 |
BreakInputIterator.Start(BreakInput); |
| 670 |
{ Try breaking line number I. If it's empty, there's no need to break it. } |
| 671 |
if BreakInputIterator.GetNext then |
| 672 |
begin |
| 673 |
{ the first character C is always considered to fit into BreakOutput1, |
| 674 |
regardless of MaxLineWidth --- after all, we have to place this character |
| 675 |
somewhere (otherwise we would have to reject this character |
| 676 |
or raise an error). } |
| 677 |
LineWidth := TextWidth(UnicodeCharToString(BreakInputIterator.Current)); |
| 678 |
LineUnicodeChars := 1; |
| 679 |
BreakNeeded := false; |
| 680 |
|
| 681 |
while BreakInputIterator.GetNext do |
| 682 |
begin |
| 683 |
NewLineWidth := LineWidth + TextWidth(UnicodeCharToString(BreakInputIterator.Current)); |
| 684 |
if NewLineWidth > MaxLineWidth then |
| 685 |
begin |
| 686 |
{ More characters available, but we need to break } |
| 687 |
BreakNeeded := true; |
| 688 |
Break; |
| 689 |
end; |
| 690 |
LineWidth := NewLineWidth; |
| 691 |
LineUnicodeChars := LineUnicodeChars + 1; |
| 692 |
end; |
| 693 |
|
| 694 |
if BreakNeeded then |
| 695 |
begin |
| 696 |
{ HardBreakOutput is BreakOutput1 in case we don't find a whitespace |
| 697 |
on which to break } |
| 698 |
HardBreakOutput := StringCopy(BreakInput, 1, LineUnicodeChars); |
| 699 |
|
| 700 |
{ We have to break this line now. |
| 701 |
|
| 702 |
Note: To split with space, we use regular |
| 703 |
BackCharsPos followed by Copy + SEnding. These are not aware of |
| 704 |
multi-character sequences that in UTF-8 (FPC) or UTF-16 (Delphi) may mean |
| 705 |
one Unicode character. But it's OK, the space is detected correctly |
| 706 |
(since it's a single character in UTF-8 and UTF-16 and cannot be mistaken |
| 707 |
for the middle of a multi-character sequence due to how UTF-8 and UTF-16 |
| 708 |
are made). } |
| 709 |
P := BackCharsPos(WhiteSpaces, HardBreakOutput); |
| 710 |
if P > 0 then |
| 711 |
begin |
| 712 |
BreakOutput1 := Copy(BreakInput, 1, P - 1); |
| 713 |
BreakOutput2 := SEnding(BreakInput, P + 1) { break at pos p, delete p-th char } |
| 714 |
end else |
| 715 |
begin |
| 716 |
BreakOutput1 := HardBreakOutput; |
| 717 |
BreakOutput2 := StringEnding(BreakInput, LineUnicodeChars + 1); |
| 718 |
end; |
| 719 |
Broken[I] := BreakOutput1; |
| 720 |
Broken.Insert(I + 1, BreakOutput2); // next iteration will break BreakOutput2 |
| 721 |
end; |
| 722 |
end; |
| 723 |
|
| 724 |
Inc(I); |
| 725 |
end; |
| 726 |
end; |
| 727 |
|
| 728 |
function TCastleAbstractFont.MaxTextWidth(SList: TStrings; const Html: boolean): Single; |
| 729 |
var |
| 730 |
I: Integer; |
| 731 |
Text: TRichText; |
| 732 |
begin |
| 733 |
if not Html then |
| 734 |
begin |
| 735 |
{ simple and fast implementation in case TRichText not needed } |
| 736 |
Result := 0; |
| 737 |
for I := 0 to SList.Count-1 do |
| 738 |
MaxVar(Result, TextWidth(SList[I])); |
| 739 |
end else |
| 740 |
begin |
| 741 |
Text := TRichText.Create(Self, SList, Html); |
| 742 |
try |
| 743 |
Result := Text.Width; |
| 744 |
finally FreeAndNil(Text) end; |
| 745 |
end; |
| 746 |
end; |
| 747 |
|
| 748 |
procedure TCastleAbstractFont.PrintStrings(const X0, Y0: Single; |
| 749 |
const Color: TCastleColor; const Strs: TStrings; |
| 750 |
const Html: boolean; const LineSpacing: Single; |
| 751 |
const TextHorizontalAlignment: THorizontalPosition); |
| 752 |
|
| 753 |
function XPos(const Line: Integer; const S: string): Single; |
| 754 |
begin |
| 755 |
case TextHorizontalAlignment of |
| 756 |
hpLeft : Result := X0; |
| 757 |
hpMiddle: Result := X0 - TextWidth(S) / 2; |
| 758 |
hpRight : Result := X0 - TextWidth(S); |
| 759 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 760 |
else raise EInternalError.Create('TCastleAbstractFont.PrintStrings: TextHorizontalAlignment unknown'); |
| 761 |
{$endif} |
| 762 |
end; |
| 763 |
end; |
| 764 |
|
| 765 |
function YPos(const Line: Integer): Single; |
| 766 |
begin |
| 767 |
Result := (Strs.Count - 1 - Line) * (Height + LineSpacing) + Y0; |
| 768 |
end; |
| 769 |
|
| 770 |
var |
| 771 |
Line: Integer; |
| 772 |
Text: TRichText; |
| 773 |
S: String; |
| 774 |
Y: Single; |
| 775 |
HasScissorRect: Boolean; |
| 776 |
ScissorRect: TRectangle; |
| 777 |
begin |
| 778 |
if not Html then |
| 779 |
begin |
| 780 |
{ simple and fast implementation in case TRichText not needed } |
| 781 |
|
| 782 |
{ Check RenderContext <> nil, as this may be used for TCastleImage |
| 783 |
drawing, testcase: font_draw_over_image. |
| 784 |
TODO: better account for TCastleImage drawing, |
| 785 |
it may be possible even when RenderContext <> nil, |
| 786 |
and we should ignore scissors then. } |
| 787 |
HasScissorRect := (RenderContext <> nil) and |
| 788 |
RenderContext.FinalScissor(ScissorRect) and |
| 789 |
(not ScissorRect.IsEmpty); |
| 790 |
|
| 791 |
for Line := 0 to Strs.Count - 1 do |
| 792 |
begin |
| 793 |
Y := YPos(Line); |
| 794 |
{ Reject entire line, if it would be clipped anyway. } |
| 795 |
if HasScissorRect and |
| 796 |
( (Y > ScissorRect.Top) or |
| 797 |
(Y + Height < ScissorRect.Bottom) |
| 798 |
) then |
| 799 |
Continue; |
| 800 |
S := Strs[Line]; |
| 801 |
Print(XPos(Line, S), Y, Color, S); |
| 802 |
end; |
| 803 |
end else |
| 804 |
begin |
| 805 |
Text := TRichText.Create(Self, Strs, Html); |
| 806 |
try |
| 807 |
Text.Print(X0, Y0, Color, LineSpacing, TextHorizontalAlignment); |
| 808 |
finally FreeAndNil(Text) end; |
| 809 |
end; |
| 810 |
end; |
| 811 |
|
| 812 |
procedure TCastleAbstractFont.PrintStrings(const X0, Y0: Single; |
| 813 |
const Color: TCastleColor; const Strs: array of string; |
| 814 |
const Html: boolean; const LineSpacing: Single; |
| 815 |
const TextHorizontalAlignment: THorizontalPosition); |
| 816 |
var |
| 817 |
SList: TStringList; |
| 818 |
begin |
| 819 |
SList := TStringList.Create; |
| 820 |
try |
| 821 |
AddStrArrayToStrings(Strs, SList); |
| 822 |
PrintStrings(X0, Y0, Color, SList, Html, LineSpacing, TextHorizontalAlignment); |
| 823 |
finally FreeAndNil(SList) end; |
| 824 |
end; |
| 825 |
|
| 826 |
procedure TCastleAbstractFont.PrintStrings(const Strs: TStrings; |
| 827 |
const Html: boolean; const LineSpacing: Single; |
| 828 |
const X0: Single; const Y0: Single); |
| 829 |
begin |
| 830 |
{ Deprecated stuff uses other deprecated stuff here, don't warn } |
| 831 |
{$warnings off} |
| 832 |
PrintStrings(X0, Y0, CurrentColor, Strs, Html, LineSpacing); |
| 833 |
{$warnings on} |
| 834 |
end; |
| 835 |
|
| 836 |
procedure TCastleAbstractFont.PrintStrings(const Strs: array of string; |
| 837 |
const Html: boolean; const LineSpacing: Single; const X0: Single; |
| 838 |
const Y0: Single); |
| 839 |
var |
| 840 |
SList: TStringList; |
| 841 |
begin |
| 842 |
{ Deprecated stuff uses other deprecated stuff here, don't warn } |
| 843 |
{$warnings off} |
| 844 |
SList := TStringList.Create; |
| 845 |
try |
| 846 |
AddStrArrayToStrings(Strs, SList); |
| 847 |
PrintStrings(X0, Y0, CurrentColor, SList, Html, LineSpacing); |
| 848 |
finally SList.Free end; |
| 849 |
{$warnings on} |
| 850 |
end; |
| 851 |
|
| 852 |
function TCastleAbstractFont.PrintBrokenString(X0, Y0: Single; |
| 853 |
const Color: TCastleColor; const S: string; const MaxLineWidth: Single; |
| 854 |
const PositionsFirst: boolean; const LineSpacing: Single; |
| 855 |
const Html: boolean): Integer; |
| 856 |
var |
| 857 |
Text: TRichText; |
| 858 |
begin |
| 859 |
Text := TRichText.Create(Self, S, Html); |
| 860 |
try |
| 861 |
Text.Wrap(MaxLineWidth); |
| 862 |
if PositionsFirst then |
| 863 |
Y0 := Y0 - (Text.Count-1) * (Height + LineSpacing); |
| 864 |
Text.Print(X0, Y0, Color, LineSpacing); |
| 865 |
Result := Text.Count; |
| 866 |
finally FreeAndNil(Text) end; |
| 867 |
end; |
| 868 |
|
| 869 |
function TCastleAbstractFont.PrintBrokenString(const Rect: TFloatRectangle; |
| 870 |
const Color: TCastleColor; const S: string; |
| 871 |
const LineSpacing: Single; |
| 872 |
const AlignHorizontal: THorizontalPosition; |
| 873 |
const AlignVertical: TVerticalPosition; |
| 874 |
const Html: boolean): Integer; |
| 875 |
var |
| 876 |
Text: TRichText; |
| 877 |
X0, Y0, BrokenHeight: Single; |
| 878 |
begin |
| 879 |
Text := TRichText.Create(Self, S, Html); |
| 880 |
try |
| 881 |
Text.Wrap(Rect.Width); |
| 882 |
{ calculate X0 based on Rect and Text.Width } |
| 883 |
case AlignHorizontal of |
| 884 |
hpLeft : X0 := Rect.Left; |
| 885 |
hpMiddle: X0 := Rect.Left + (Rect.Width - Text.Width) / 2; |
| 886 |
hpRight : X0 := Rect.Right - Text.Width; |
| 887 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 888 |
else raise EInternalError.Create('PrintBrokenString.AlignHorizontal?'); |
| 889 |
{$endif} |
| 890 |
end; |
| 891 |
{ calculate Y0 based on Rect and BrokenHeight } |
| 892 |
BrokenHeight := Text.Count * (LineSpacing + Height); |
| 893 |
case AlignVertical of |
| 894 |
vpBottom: Y0 := Rect.Bottom; |
| 895 |
vpMiddle: Y0 := Rect.Bottom + (Rect.Height - BrokenHeight) / 2; |
| 896 |
vpTop : Y0 := Rect.Top - BrokenHeight; |
| 897 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 898 |
else raise EInternalError.Create('PrintBrokenString.AlignVertical?'); |
| 899 |
{$endif} |
| 900 |
end; |
| 901 |
Text.Print(X0, Y0, Color, LineSpacing); |
| 902 |
Result := Text.Count; |
| 903 |
finally FreeAndNil(Text) end; |
| 904 |
end; |
| 905 |
|
| 906 |
function TCastleAbstractFont.PrintBrokenString(const Rect: TRectangle; const Color: TCastleColor; |
| 907 |
const S: string; |
| 908 |
const LineSpacing: Single; |
| 909 |
const AlignHorizontal: THorizontalPosition; |
| 910 |
const AlignVertical: TVerticalPosition; |
| 911 |
const Html: boolean = false): Integer; |
| 912 |
begin |
| 913 |
Result := PrintBrokenString(FloatRectangle(Rect), Color, S, LineSpacing, |
| 914 |
AlignHorizontal, AlignVertical, Html); |
| 915 |
end; |
| 916 |
|
| 917 |
function TCastleAbstractFont.PrintBrokenString(const S: string; |
| 918 |
const MaxLineWidth, X0, Y0: Single; |
| 919 |
const PositionsFirst: boolean; |
| 920 |
const LineSpacing: Single): Integer; deprecated; |
| 921 |
begin |
| 922 |
{ Deprecated stuff uses other deprecated stuff here, don't warn } |
| 923 |
{$warnings off} |
| 924 |
Result := PrintBrokenString(X0, Y0, CurrentColor, S, MaxLineWidth, |
| 925 |
PositionsFirst, LineSpacing); |
| 926 |
{$warnings on} |
| 927 |
end; |
| 928 |
|
| 929 |
procedure TCastleAbstractFont.Measure(out AHeight, ACapHeight, ADescenderHeight: Single); |
| 930 |
var |
| 931 |
MeasureDescenderHeightLen: Integer; |
| 932 |
begin |
| 933 |
AHeight := TextHeight(MeasureHeight); |
| 934 |
ACapHeight := TextHeightBase(MeasureCapHeight); |
| 935 |
|
| 936 |
MeasureDescenderHeightLen := StringLength(MeasureDescenderHeight); |
| 937 |
if MeasureDescenderHeightLen = 2 then |
| 938 |
begin |
| 939 |
ADescenderHeight := Max(0, |
| 940 |
TextHeight(StringCopy(MeasureDescenderHeight, 1, 1)) - |
| 941 |
TextHeight(StringCopy(MeasureDescenderHeight, 2, 1)) |
| 942 |
); |
| 943 |
end else |
| 944 |
begin |
| 945 |
ADescenderHeight := 0; |
| 946 |
if MeasureDescenderHeightLen <> 0 then |
| 947 |
WritelnWarning('MeasureDescenderHeight must have 0 or 2 Unicode characters, but has %d: "%s"', [ |
| 948 |
MeasureDescenderHeightLen, |
| 949 |
MeasureDescenderHeight |
| 950 |
]); |
| 951 |
end; |
| 952 |
end; |
| 953 |
|
| 954 |
procedure TCastleAbstractFont.MakeMeasure; |
| 955 |
begin |
| 956 |
if FMeasuredSize <> Size then |
| 957 |
begin |
| 958 |
Measure(FMeasuredHeight, FMeasuredCapHeight, FMeasuredDescenderHeight); |
| 959 |
FMeasuredSize := Size; |
| 960 |
end; |
| 961 |
end; |
| 962 |
|
| 963 |
function TCastleAbstractFont.RowHeight: Single; |
| 964 |
begin |
| 965 |
Result := Height; |
| 966 |
end; |
| 967 |
|
| 968 |
function TCastleAbstractFont.RowHeightBase: Single; |
| 969 |
begin |
| 970 |
Result := CapHeight; |
| 971 |
end; |
| 972 |
|
| 973 |
function TCastleAbstractFont.Descend: Single; |
| 974 |
begin |
| 975 |
Result := DescenderHeight; |
| 976 |
end; |
| 977 |
|
| 978 |
function TCastleAbstractFont.Height: Single; |
| 979 |
begin |
| 980 |
MakeMeasure; |
| 981 |
Result := FMeasuredHeight; |
| 982 |
end; |
| 983 |
|
| 984 |
function TCastleAbstractFont.CapHeight: Single; |
| 985 |
begin |
| 986 |
MakeMeasure; |
| 987 |
Result := FMeasuredCapHeight; |
| 988 |
end; |
| 989 |
|
| 990 |
function TCastleAbstractFont.DescenderHeight: Single; |
| 991 |
begin |
| 992 |
MakeMeasure; |
| 993 |
Result := FMeasuredDescenderHeight; |
| 994 |
end; |
| 995 |
|
| 996 |
procedure TCastleAbstractFont.PushProperties; |
| 997 |
var |
| 998 |
SavedProperites: TSavedProperties; |
| 999 |
begin |
| 1000 |
if FPropertiesStack = nil then |
| 1001 |
FPropertiesStack := TSavedPropertiesList.Create; |
| 1002 |
|
| 1003 |
SavedProperites := TSavedProperties.Create; |
| 1004 |
SavedProperites.Size := Size; |
| 1005 |
SavedProperites.Outline := Outline; |
| 1006 |
SavedProperites.OutlineColor := OutlineColor; |
| 1007 |
SavedProperites.OutlineHighQuality := OutlineHighQuality; |
| 1008 |
SavedProperites.TargetImage := TargetImage; |
| 1009 |
FPropertiesStack.Add(SavedProperites); |
| 1010 |
end; |
| 1011 |
|
| 1012 |
procedure TCastleAbstractFont.PopProperties; |
| 1013 |
var |
| 1014 |
SavedProperites: TSavedProperties; |
| 1015 |
begin |
| 1016 |
if (FPropertiesStack = nil) or (FPropertiesStack.Count = 0) then |
| 1017 |
raise Exception.Create('Cannot do TCastleAbstractFont.PopProperties, stack empty. Every PopProperties should match previous PushProperties'); |
| 1018 |
|
| 1019 |
SavedProperites := FPropertiesStack.Last; |
| 1020 |
Size := SavedProperites.Size; |
| 1021 |
Outline := SavedProperites.Outline; |
| 1022 |
OutlineColor := SavedProperites.OutlineColor; |
| 1023 |
OutlineHighQuality := SavedProperites.OutlineHighQuality; |
| 1024 |
TargetImage := SavedProperites.TargetImage; |
| 1025 |
FPropertiesStack.Delete(FPropertiesStack.Count - 1); |
| 1026 |
end; |
| 1027 |
|
| 1028 |
function TCastleAbstractFont.EffectiveSize: Single; |
| 1029 |
begin |
| 1030 |
Result := Size; |
| 1031 |
end; |
| 1032 |
|
| 1033 |
function TCastleAbstractFont.RealSize: Single; |
| 1034 |
begin |
| 1035 |
Result := EffectiveSize; |
| 1036 |
end; |
| 1037 |
|
| 1038 |
procedure TCastleAbstractFont.SetOutline(const Value: Cardinal); |
| 1039 |
begin |
| 1040 |
if FOutline <> Value then |
| 1041 |
begin |
| 1042 |
FOutline := Value; |
| 1043 |
// invalidate measurements, like Height, as they depend on Outline |
| 1044 |
FMeasuredSize := -1; |
| 1045 |
end; |
| 1046 |
end; |
| 1047 |
|
| 1048 |
procedure TCastleAbstractFont.FontSizesChanged; |
| 1049 |
begin |
| 1050 |
FMeasuredSize := -1; // not measured at all |
| 1051 |
if FFontSizeNotifications <> nil then |
| 1052 |
FFontSizeNotifications.ExecuteAll(Self); |
| 1053 |
end; |
| 1054 |
|
| 1055 |
procedure TCastleAbstractFont.AddFontSizeChangeNotification(const Notify: TNotifyEvent); |
| 1056 |
begin |
| 1057 |
if FFontSizeNotifications = nil then |
| 1058 |
FFontSizeNotifications := TNotifyEventList.Create; |
| 1059 |
FFontSizeNotifications.Add(Notify); |
| 1060 |
end; |
| 1061 |
|
| 1062 |
procedure TCastleAbstractFont.RemoveFontSizeChangeNotification(const Notify: TNotifyEvent); |
| 1063 |
begin |
| 1064 |
if FFontSizeNotifications <> nil then // may be nil when destroying, and we make notification |
| 1065 |
FFontSizeNotifications.Remove(Notify); |
| 1066 |
end; |
| 1067 |
|
| 1068 |
procedure TCastleAbstractFont.SetMeasureHeight(const Value: String); |
| 1069 |
begin |
| 1070 |
if FMeasureHeight <> Value then |
| 1071 |
begin |
| 1072 |
FMeasureHeight := Value; |
| 1073 |
FMeasuredSize := -1; // invalidate measurements |
| 1074 |
end; |
| 1075 |
end; |
| 1076 |
|
| 1077 |
procedure TCastleAbstractFont.SetMeasureCapHeight(const Value: String); |
| 1078 |
begin |
| 1079 |
if FMeasureCapHeight <> Value then |
| 1080 |
begin |
| 1081 |
FMeasureCapHeight := Value; |
| 1082 |
FMeasuredSize := -1; // invalidate measurements |
| 1083 |
end; |
| 1084 |
end; |
| 1085 |
|
| 1086 |
procedure TCastleAbstractFont.SetMeasureDescenderHeight(const Value: String); |
| 1087 |
begin |
| 1088 |
if FMeasureDescenderHeight <> Value then |
| 1089 |
begin |
| 1090 |
FMeasureDescenderHeight := Value; |
| 1091 |
FMeasuredSize := -1; // invalidate measurements |
| 1092 |
end; |
| 1093 |
end; |
| 1094 |
|
| 1095 |
function TCastleAbstractFont.StoreMeasureHeight: Boolean; |
| 1096 |
begin |
| 1097 |
Result := FMeasureHeight <> DefaultMeasureHeight; |
| 1098 |
end; |
| 1099 |
|
| 1100 |
function TCastleAbstractFont.StoreMeasureCapHeight: Boolean; |
| 1101 |
begin |
| 1102 |
Result := FMeasureCapHeight <> DefaultMeasureCapHeight; |
| 1103 |
end; |
| 1104 |
|
| 1105 |
function TCastleAbstractFont.StoreMeasureDescenderHeight: Boolean; |
| 1106 |
begin |
| 1107 |
Result := FMeasureDescenderHeight <> DefaultMeasureDescenderHeight; |
| 1108 |
end; |
| 1109 |
|
| 1110 |
{$endif read_implementation} |