| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 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 |
{ Base node type for all font style nodes. } |
| 19 |
TAbstractFontStyleNode = class(TAbstractNode) |
| 20 |
{$I auto_generated_node_helpers/x3dnodes_x3dfontstylenode.inc} |
| 21 |
end; |
| 22 |
|
| 23 |
{ Font family that can be specified by a TFontStyleNode. } |
| 24 |
TX3DFontFamily = (ffSerif, ffSans, ffTypeWriter); |
| 25 |
|
| 26 |
{ Font justification that can be specified by a TFontStyleNode. } |
| 27 |
TX3DFontJustify = (fjFirst, fjBegin, fjMiddle, fjEnd); |
| 28 |
|
| 29 |
{ Font style that can be specified by a @link(TFontStyleNode.Style). } |
| 30 |
TX3DFontStyle = (fsPlain, fsBold, fsItalic, fsBoldItalic); |
| 31 |
|
| 32 |
{ Defines the size, family, style and other properties used for @link(TTextNode). } |
| 33 |
TFontStyleNode = class(TAbstractFontStyleNode) |
| 34 |
strict private |
| 35 |
JustifyWarningUppercaseDone, JustifyWarningObsoleteDone, |
| 36 |
JustifyWarningNotSupportedDone: boolean; |
| 37 |
FCustomFont: TCastleFont; |
| 38 |
FCustomFontObserver: TFreeNotificationObserver; |
| 39 |
function StringToJustify(const S: string; |
| 40 |
const DefaultValue: TX3DFontJustify): TX3DFontJustify; |
| 41 |
function GetFamily: TX3DFontFamily; |
| 42 |
procedure SetFamily(const Value: TX3DFontFamily); |
| 43 |
function GetBold: boolean; |
| 44 |
procedure SetBold(const Value: boolean); |
| 45 |
function GetItalic: boolean; |
| 46 |
procedure SetItalic(const Value: boolean); |
| 47 |
function GetJustify: TX3DFontJustify; |
| 48 |
procedure SetJustify(const Value: TX3DFontJustify); |
| 49 |
function GetJustifyMinor: TX3DFontJustify; |
| 50 |
procedure SetJustifyMinor(const Value: TX3DFontJustify); |
| 51 |
procedure SetCustomFont(const Value: TCastleFont); |
| 52 |
procedure CustomFontFreeNotification(const Sender: TFreeNotificationObserver); |
| 53 |
private |
| 54 |
class function GetDefaultFont(const Family: TX3DFontFamily; const Bold, Italic: Boolean): TCastleFont; |
| 55 |
public |
| 56 |
const |
| 57 |
DefaultSize = 1.0; |
| 58 |
DefaultSpacing = 1.0; |
| 59 |
DefaultFamily = ffSerif; |
| 60 |
DefaultBold = false; |
| 61 |
DefaultItalic = false; |
| 62 |
DefaultJustify = fjBegin; |
| 63 |
DefaultJustifyMinor = fjFirst; |
| 64 |
DefaultBlending = true; |
| 65 |
|
| 66 |
constructor Create(const AX3DName: string = ''; const ABaseUrl: String = ''); override; |
| 67 |
destructor Destroy; override; |
| 68 |
|
| 69 |
property Family: TX3DFontFamily read GetFamily write SetFamily; |
| 70 |
property Justify: TX3DFontJustify read GetJustify write SetJustify; |
| 71 |
property JustifyMinor: TX3DFontJustify read GetJustifyMinor write SetJustifyMinor; |
| 72 |
property Bold: boolean read GetBold write SetBold; |
| 73 |
property Italic: boolean read GetItalic write SetItalic; |
| 74 |
|
| 75 |
class function ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 76 |
override; |
| 77 |
|
| 78 |
{ Font used by this node, determined by our fields (like @link(Bold)) |
| 79 |
and @link(TFontStyleNode.CustomFont). } |
| 80 |
function Font: TCastleFont; |
| 81 |
|
| 82 |
{ Adjust the font. |
| 83 |
|
| 84 |
TODO: Currently this only allows TCastleFont. |
| 85 |
In the future it should allow any TCastleAbstractFont, |
| 86 |
like TCastleFontFamily (thus have different options for bold/italic variants) |
| 87 |
or TCastleBitmapFont. } |
| 88 |
property CustomFont: TCastleFont read FCustomFont write SetCustomFont; |
| 89 |
|
| 90 |
{$I auto_generated_node_helpers/x3dnodes_fontstyle.inc} |
| 91 |
end; |
| 92 |
TFontStyleNode_2 = TFontStyleNode deprecated 'use TFontStyleNode'; |
| 93 |
|
| 94 |
{ Multiline text, visualized as a set of flat and textured polygons. } |
| 95 |
TTextNode = class(TAbstractGeometryNode) |
| 96 |
strict private |
| 97 |
FFontTextureNode: TPixelTextureNode; |
| 98 |
function Font: TCastleFont; |
| 99 |
public |
| 100 |
destructor Destroy; override; |
| 101 |
function TexCoordField: TSFNode; override; |
| 102 |
|
| 103 |
function Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; override; |
| 104 |
function LocalBoundingBox(State: TX3DGraphTraverseState; |
| 105 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override; |
| 106 |
function BoundingBox(State: TX3DGraphTraverseState; |
| 107 |
ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D; override; |
| 108 |
|
| 109 |
function SolidField: TSFBool; override; |
| 110 |
function FontTextureNode: TAbstractTexture2DNode; override; |
| 111 |
|
| 112 |
{ Force recalculating the shape and font texture. |
| 113 |
Call this when TFontStyleNode.CustomFont returns something different. } |
| 114 |
procedure FontChanged; |
| 115 |
|
| 116 |
{ } |
| 117 |
procedure SetString(const Value: array of string); overload; deprecated 'use SetText'; |
| 118 |
{ } |
| 119 |
procedure SetString(const Value: TCastleStringList); overload; deprecated 'use SetText'; |
| 120 |
|
| 121 |
{$I auto_generated_node_helpers/x3dnodes_text.inc} |
| 122 |
end; |
| 123 |
|
| 124 |
{$endif read_interface} |
| 125 |
|
| 126 |
{$ifdef read_implementation} |
| 127 |
|
| 128 |
{ internal routine to create proxy ------------------------------------------- } |
| 129 |
|
| 130 |
{ Create a simple mesh (filling OutCoords, OutTexCoords fields |
| 131 |
to initialize X3D node like QuadSet) that represents input Text node. |
| 132 |
The interface is suitable for both VRML 2.0 / X3D Text node, |
| 133 |
as well as VRML 1.0 AsciiText node. } |
| 134 |
procedure TextProxy(const OriginalNode: TX3DNode; |
| 135 |
const Size, Spacing: Single; |
| 136 |
const Justify, JustifyMinor: TX3DFontJustify; |
| 137 |
const Strings: TCastleStringList; |
| 138 |
const MaxDisplayChars: Integer; |
| 139 |
const Font: TCastleFont; |
| 140 |
const Solid: boolean; |
| 141 |
const EventLineBounds: TMFVec2fEvent; |
| 142 |
const EventOrigin: TSFVec3fEvent; |
| 143 |
const EventTextBounds: TSFVec2fEvent; |
| 144 |
const OutCoord: TMFVec3f; const OutTexCoord, OutExtraTexCoord: TMFVec2f); |
| 145 |
var |
| 146 |
TextWidths: TSingleList; |
| 147 |
FontData: TTextureFontData; |
| 148 |
|
| 149 |
{ StringPos* calculate position to start the given line of text. |
| 150 |
They ignore the Size parameter. } |
| 151 |
|
| 152 |
function StringPosX(I: Integer): Single; |
| 153 |
begin |
| 154 |
case Justify of |
| 155 |
fjBegin, fjFirst : |
| 156 |
Result := 0; |
| 157 |
fjMiddle: Result := - TextWidths.Items[I] / 2; |
| 158 |
fjEnd : Result := - TextWidths.Items[I]; |
| 159 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 160 |
else raise EInternalError.Create('StringPosX: Invalid font Justify value'); |
| 161 |
{$endif} |
| 162 |
end; |
| 163 |
end; |
| 164 |
|
| 165 |
function StringPosY(I: Integer): Single; |
| 166 |
begin |
| 167 |
case JustifyMinor of |
| 168 |
fjFirst : Result := - I; |
| 169 |
fjBegin : Result := - (I + 1); |
| 170 |
fjMiddle: Result := Strings.Count / 2 - (I + 1); |
| 171 |
fjEnd : Result := Strings.Count - (I + 1); |
| 172 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 173 |
else raise EInternalError.Create('StringPosY: Invalid font JustifyMinor value'); |
| 174 |
{$endif} |
| 175 |
end; |
| 176 |
Result := Result * (Font.Height * Spacing); |
| 177 |
|
| 178 |
{ For JustifyMinor <> fjFirst add font descend, |
| 179 |
to account for the fact that baseline is a bit higher than 0. |
| 180 |
|
| 181 |
This means that (for JustifyMinor <> fjFirst) vertical level = 0 is not "baseline". |
| 182 |
Vertical level = 0 is "where bounding box starts". This has advantages: |
| 183 |
|
| 184 |
- It is similar behavior to TCastleLabel. So user has consistent behavior, |
| 185 |
and you can even easily match TCastleLabel coordinates with TCastleText coordinates |
| 186 |
in the viewport. |
| 187 |
- It is more comfortable when you want to fit the text somewhere. |
| 188 |
No text geometry goes underneath vertical level = 0, ever. |
| 189 |
- This makes the fjBegin and fjEnd behave consistently, |
| 190 |
and for both horizontal and vertical cases. |
| 191 |
They all mean "this is the edge of the bounding box". |
| 192 |
|
| 193 |
In contrast, fjFirst logic is a bit special. |
| 194 |
It means that vertical level = 0 is "baseline". |
| 195 |
This makes the bottom of the font special: |
| 196 |
|
| 197 |
- Font may have some geometry below 0. |
| 198 |
- This is unlike the horizontal fjFirst (that determines just "left edge of bbox" |
| 199 |
and is equal to fjBegin). |
| 200 |
|
| 201 |
Of course fjFirst (vertical level = 0 is "baseline") has one big advantage: |
| 202 |
making larger/smaller Descend of the font doesn't shift where the baseline is rendered. |
| 203 |
|
| 204 |
It matches Text_LocalBoundingBox logic, that also accounts for |
| 205 |
JustifyMinor=fjFirst as a special case. } |
| 206 |
if JustifyMinor <> fjFirst then |
| 207 |
Result := Result + Font.DescenderHeight * Spacing; |
| 208 |
end; |
| 209 |
|
| 210 |
var |
| 211 |
YScale, XScale, MaxTextWidth: Single; |
| 212 |
ExtraTexOrigin: TVector2; |
| 213 |
|
| 214 |
procedure SendEvents; |
| 215 |
var |
| 216 |
Origin: TVector3; |
| 217 |
LineBounds: TMFVec2f; |
| 218 |
FinalHeight: Single; |
| 219 |
I: Integer; |
| 220 |
begin |
| 221 |
if (EventOrigin <> nil) and |
| 222 |
(OriginalNode.Scene <> nil) then |
| 223 |
begin |
| 224 |
{ TODO: We ignore topToBottom, leftToRight. } |
| 225 |
|
| 226 |
FinalHeight := Font.Height * Spacing * YScale; |
| 227 |
|
| 228 |
{ calculate and send Origin } |
| 229 |
Origin := TVector3.Zero; |
| 230 |
case Justify of |
| 231 |
fjBegin, fjFirst : |
| 232 |
Origin.X := 0; |
| 233 |
fjMiddle: Origin.X := -MaxTextWidth * XScale / 2; |
| 234 |
fjEnd : Origin.X := -MaxTextWidth * XScale ; |
| 235 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 236 |
else raise EInternalError.Create('SendEvents: Invalid font justify value'); |
| 237 |
{$endif} |
| 238 |
end; |
| 239 |
case JustifyMinor of |
| 240 |
fjFirst : Origin.Y := -FinalHeight; |
| 241 |
fjBegin : Origin.Y := 0; |
| 242 |
fjMiddle: Origin.Y := -FinalHeight * Strings.Count / 2; |
| 243 |
fjEnd : Origin.Y := -FinalHeight * Strings.Count; |
| 244 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 245 |
else raise EInternalError.Create('SendEvents: Invalid font justify value'); |
| 246 |
{$endif} |
| 247 |
end; |
| 248 |
EventOrigin.Send(Origin, OriginalNode.Scene.NextEventTime); |
| 249 |
|
| 250 |
if EventLineBounds.SendNeeded then |
| 251 |
begin |
| 252 |
LineBounds := TMFVec2f.CreateUndefined(OriginalNode, false, EventLineBounds.X3DName); |
| 253 |
try |
| 254 |
LineBounds.Items.Count := TextWidths.Count; |
| 255 |
|
| 256 |
case Justify of |
| 257 |
fjBegin, fjFirst: |
| 258 |
for I := 0 to TextWidths.Count - 1 do |
| 259 |
LineBounds.Items.L[I] := Vector2( |
| 260 |
TextWidths[I] * XScale, FinalHeight); |
| 261 |
fjMiddle: |
| 262 |
for I := 0 to TextWidths.Count - 1 do |
| 263 |
LineBounds.Items.L[I] := Vector2( |
| 264 |
(MaxTextWidth + TextWidths[I]) * XScale / 2, FinalHeight); |
| 265 |
fjEnd : |
| 266 |
for I := 0 to TextWidths.Count - 1 do |
| 267 |
LineBounds.Items.L[I] := Vector2( |
| 268 |
MaxTextWidth * XScale, FinalHeight); |
| 269 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 270 |
else raise EInternalError.Create('Invalid font justify value'); |
| 271 |
{$endif} |
| 272 |
end; |
| 273 |
|
| 274 |
EventLineBounds.Send(LineBounds, OriginalNode.Scene.NextEventTime); |
| 275 |
finally FreeAndNil(LineBounds) end; |
| 276 |
end; |
| 277 |
|
| 278 |
EventTextBounds.Send(Vector2( |
| 279 |
MaxTextWidth * XScale, FinalHeight * Strings.Count), OriginalNode.Scene.NextEventTime); |
| 280 |
end; |
| 281 |
end; |
| 282 |
|
| 283 |
{ Print the string, by adding rectangles to current QuadSet. |
| 284 |
This is analogous to printing straight in 2D done by TCastleFont.Print. |
| 285 |
Some pieces of code below are deliberately similar, and we prepare to make outlines |
| 286 |
too. } |
| 287 |
procedure Print(const X, Y: Single; const S: string; const XScale, YScale: Single); |
| 288 |
|
| 289 |
function ConvertCoordToExtraTex(const CoordIndex: Integer; const Coord: Single): Single; |
| 290 |
begin |
| 291 |
Result := MapRange(Coord, |
| 292 |
ExtraTexOrigin[CoordIndex], |
| 293 |
ExtraTexOrigin[CoordIndex] + Font.Height * Spacing * YScale, |
| 294 |
0, 1); |
| 295 |
end; |
| 296 |
|
| 297 |
// TODO: Outline not done yet. |
| 298 |
// Outline requires a different X3D shape for different color, or using ColorRGBA. |
| 299 |
const |
| 300 |
Outline = 0; |
| 301 |
OutlineHighQuality = true; |
| 302 |
var |
| 303 |
ScreenX, ScreenY: Single; |
| 304 |
G: TTextureFontData.TGlyph; |
| 305 |
|
| 306 |
procedure GlyphDraw(const OutlineMoveX, OutlineMoveY: Integer); |
| 307 |
var |
| 308 |
ScreenRect, ImageRect, ExtraTexRect: TFloatRectangle; |
| 309 |
begin |
| 310 |
{ Use a small margin around every glyph to allow bilinear |
| 311 |
filtering to smoothly go from opaque to fully transparent |
| 312 |
at glyph border. This prevents glyph border from ending suddenly, |
| 313 |
it looks much better in the case of blending. |
| 314 |
|
| 315 |
This cooperates with TTextureFontData.Create (used by |
| 316 |
texture-font-to-pascal) that makes sure that each letter is |
| 317 |
surrounded with a padding that allows such border, |
| 318 |
see GlyphPadding in castletexturefontdata.pas. } |
| 319 |
{$define EXTRA_GLYPH_SPACE} |
| 320 |
|
| 321 |
ScreenRect := FloatRectangle( |
| 322 |
XScale * (ScreenX - G.X + OutlineMoveX * Outline {$ifdef EXTRA_GLYPH_SPACE} - 0.5 {$endif}), |
| 323 |
YScale * (ScreenY - G.Y + OutlineMoveY * Outline {$ifdef EXTRA_GLYPH_SPACE} - 0.5 {$endif}), |
| 324 |
XScale * (G.Width {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}), |
| 325 |
YScale * (G.Height {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif})); |
| 326 |
|
| 327 |
ImageRect := FloatRectangle( |
| 328 |
(G.ImageX {$ifdef EXTRA_GLYPH_SPACE} - 0.5 {$endif}) / FontData.Image.Width, |
| 329 |
(G.ImageY {$ifdef EXTRA_GLYPH_SPACE} - 0.5 {$endif}) / FontData.Image.Height, |
| 330 |
(G.Width {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}) / FontData.Image.Width, |
| 331 |
(G.Height {$ifdef EXTRA_GLYPH_SPACE} + 1 {$endif}) / FontData.Image.Height); |
| 332 |
|
| 333 |
ScreenRect.AddToCoords(OutCoord.Items, 0); |
| 334 |
ImageRect.AddToCoords(OutTexCoord.Items); |
| 335 |
|
| 336 |
{ if we want extra texture coordinates, generate them knowing |
| 337 |
ExtraTexOrigin and current Coord* values. This is enough, |
| 338 |
see the specification: |
| 339 |
|
| 340 |
The texture origin is at the origin of the first string, |
| 341 |
as determined by the justification. The texture is scaled |
| 342 |
equally in both S and T dimensions, with the font height |
| 343 |
representing 1 unit. S increases to the right, and T increases up. } |
| 344 |
|
| 345 |
if OutExtraTexCoord <> nil then |
| 346 |
begin |
| 347 |
ExtraTexRect := FloatRectangle( |
| 348 |
ConvertCoordToExtraTex(0, ScreenRect.Left ), |
| 349 |
ConvertCoordToExtraTex(1, ScreenRect.Bottom), |
| 350 |
ConvertCoordToExtraTex(0, ScreenRect.Right ) - ConvertCoordToExtraTex(0, ScreenRect.Left), |
| 351 |
ConvertCoordToExtraTex(1, ScreenRect.Top ) - ConvertCoordToExtraTex(1, ScreenRect.Bottom) |
| 352 |
); |
| 353 |
ExtraTexRect.AddToCoords(OutExtraTexCoord.Items); |
| 354 |
end; |
| 355 |
end; |
| 356 |
|
| 357 |
var |
| 358 |
Iter: TCastleStringIterator; |
| 359 |
begin |
| 360 |
{ first pass, to render Outline. } |
| 361 |
|
| 362 |
// TODO: Outline not done yet. |
| 363 |
// Outline requires a different X3D shape for different color, or using ColorRGBA. |
| 364 |
(* |
| 365 |
if Outline <> 0 then |
| 366 |
begin |
| 367 |
... |
| 368 |
end; |
| 369 |
*) |
| 370 |
|
| 371 |
ScreenX := X; |
| 372 |
ScreenY := Y; |
| 373 |
|
| 374 |
Iter.Start(S); |
| 375 |
while Iter.GetNext do |
| 376 |
begin |
| 377 |
G := FontData.Glyph(Iter.Current); |
| 378 |
if G <> nil then |
| 379 |
begin |
| 380 |
if (G.Width <> 0) and (G.Height <> 0) then |
| 381 |
// TODO: Outline not done yet. |
| 382 |
// Outline requires a different X3D shape for different color, or using ColorRGBA. |
| 383 |
(* |
| 384 |
if Outline <> 0 then |
| 385 |
GlyphDraw(1, 1) |
| 386 |
else |
| 387 |
*) |
| 388 |
GlyphDraw(0, 0); |
| 389 |
ScreenX := ScreenX + G.AdvanceX + Outline * 2; |
| 390 |
ScreenY := ScreenY + G.AdvanceY; |
| 391 |
end; |
| 392 |
end; |
| 393 |
end; |
| 394 |
|
| 395 |
var |
| 396 |
I: Integer; |
| 397 |
DisplayChars: Integer; |
| 398 |
begin |
| 399 |
{$warnings off} // using FontData, will be internal in the future |
| 400 |
FontData := Font.FontData; |
| 401 |
{$warnings on} |
| 402 |
if FontData = nil then |
| 403 |
begin |
| 404 |
WritelnWarning('Cannot render font that is not loaded (has FontData = nil)'); |
| 405 |
Exit; |
| 406 |
end; |
| 407 |
|
| 408 |
YScale := Size / Font.Height; |
| 409 |
{ TODO: Use maxEntent, length for VRML 2.0. Use width for VRML 1.0. } |
| 410 |
XScale := YScale; |
| 411 |
|
| 412 |
TextWidths := TSingleList.Create; |
| 413 |
try |
| 414 |
TextWidths.Count := Strings.Count; |
| 415 |
MaxTextWidth := 0; |
| 416 |
for I := 0 to TextWidths.Count - 1 do |
| 417 |
begin |
| 418 |
TextWidths.L[I] := Font.TextWidth(Strings[I]); |
| 419 |
MaxVar(MaxTextWidth, TextWidths.L[I]); |
| 420 |
end; |
| 421 |
|
| 422 |
if Strings.Count <> 0 then |
| 423 |
begin |
| 424 |
ExtraTexOrigin := Vector2(StringPosX(0) * XScale, StringPosY(0) * YScale); |
| 425 |
if MaxDisplayChars < 0 then |
| 426 |
for I := 0 to Strings.Count - 1 do |
| 427 |
Print(StringPosX(I), StringPosY(I), Strings[I], XScale, YScale) //optimize in case MaxDisplayChars = -1 |
| 428 |
else |
| 429 |
begin |
| 430 |
DisplayChars := MaxDisplayChars; |
| 431 |
for I := 0 to Strings.Count - 1 do |
| 432 |
begin |
| 433 |
if StringLength(Strings[I]) < DisplayChars then |
| 434 |
Print(StringPosX(I), StringPosY(I), Strings[I], XScale, YScale) |
| 435 |
else |
| 436 |
Print(StringPosX(I), StringPosY(I), StringCopy(Strings[I], 1, DisplayChars), XScale, YScale); |
| 437 |
Dec(DisplayChars, StringLength(Strings[I])); |
| 438 |
if DisplayChars <= 0 then |
| 439 |
Break; |
| 440 |
end; |
| 441 |
end; |
| 442 |
end; |
| 443 |
|
| 444 |
{ These events should be generated only when |
| 445 |
the default values of length and maxExtent are used. |
| 446 |
For now, we ignore length and maxExtent, so these events are |
| 447 |
simply always generated. } |
| 448 |
SendEvents; |
| 449 |
finally FreeAndNil(TextWidths) end; |
| 450 |
end; |
| 451 |
|
| 452 |
function TTextNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 453 |
var |
| 454 |
Size, Spacing: Single; |
| 455 |
Justify, JustifyMinor: TX3DFontJustify; |
| 456 |
FaceSet: TQuadSetNode; |
| 457 |
CoordNode: TCoordinateNode; |
| 458 |
MultiTexCoordNode: TMultiTextureCoordinateNode; |
| 459 |
TexCoordNode, ExtraTexCoordNode: TTextureCoordinateNode; |
| 460 |
I: Integer; |
| 461 |
ExtraTexCoordField: TMFVec2f; |
| 462 |
begin |
| 463 |
if FontStyle is TFontStyleNode then // also checks FontStyle <> nil |
| 464 |
begin |
| 465 |
Size := TFontStyleNode(FontStyle).FdSize.Value; |
| 466 |
Spacing := TFontStyleNode(FontStyle).FdSpacing.Value; |
| 467 |
Justify := TFontStyleNode(FontStyle).Justify; |
| 468 |
JustifyMinor := TFontStyleNode(FontStyle).JustifyMinor; |
| 469 |
end else |
| 470 |
begin |
| 471 |
Size := TFontStyleNode.DefaultSize; |
| 472 |
Spacing := TFontStyleNode.DefaultSpacing; |
| 473 |
Justify := TFontStyleNode.DefaultJustify; |
| 474 |
JustifyMinor := TFontStyleNode.DefaultJustifyMinor; |
| 475 |
end; |
| 476 |
|
| 477 |
FaceSet := TQuadSetNode.Create(X3DName, BaseUrl); |
| 478 |
try |
| 479 |
FaceSet.Solid := Solid; |
| 480 |
|
| 481 |
CoordNode := TCoordinateNode.Create('', BaseUrl); |
| 482 |
FaceSet.Coord := CoordNode; |
| 483 |
|
| 484 |
MultiTexCoordNode := TMultiTextureCoordinateNode.Create('', BaseUrl); |
| 485 |
FaceSet.TexCoord := MultiTexCoordNode; |
| 486 |
|
| 487 |
TexCoordNode := TTextureCoordinateNode.Create('', BaseUrl); |
| 488 |
MultiTexCoordNode.FdTexCoord.Add(TexCoordNode); |
| 489 |
|
| 490 |
if (FdTexCoord.Value <> nil) and FdTexCoord.CurrentChildAllowed then |
| 491 |
begin |
| 492 |
{ If you specify explicit Text.texCoord value, we assume you want |
| 493 |
to use it for Appearance.texture, instead of automatic texture |
| 494 |
coordinates that would be placed inside ExtraTexCoordNode. |
| 495 |
So we don't use ExtraTexCoordNode then. |
| 496 |
|
| 497 |
Note: don't worry about CastleInternalShadowMaps processing here. |
| 498 |
CastleInternalShadowMaps processing happens on the resulting geometry node, |
| 499 |
it *does not* modify source FdTexCoord field. So shadow maps |
| 500 |
will work on both textured and untextured text, |
| 501 |
because CastleInternalShadowMaps processing will actually cut off ExtraTexCoordNode |
| 502 |
in case they are unused (because text is not textured), |
| 503 |
before adding ProjectedTextureCoordinate. } |
| 504 |
ExtraTexCoordNode := nil; |
| 505 |
if FdTexCoord.Value is TMultiTextureCoordinateNode then |
| 506 |
begin |
| 507 |
for I := 0 to TMultiTextureCoordinateNode(FdTexCoord.Value).FdTexCoord.Count - 1 do |
| 508 |
MultiTexCoordNode.FdTexCoord.Add( |
| 509 |
TMultiTextureCoordinateNode(FdTexCoord.Value).FdTexCoord[I]); |
| 510 |
end else |
| 511 |
MultiTexCoordNode.FdTexCoord.Add(FdTexCoord.Value); |
| 512 |
end else |
| 513 |
begin |
| 514 |
ExtraTexCoordNode := TTextureCoordinateNode.Create('', BaseUrl); |
| 515 |
MultiTexCoordNode.FdTexCoord.Add(ExtraTexCoordNode); |
| 516 |
end; |
| 517 |
if ExtraTexCoordNode <> nil then |
| 518 |
ExtraTexCoordField := ExtraTexCoordNode.FdPoint else |
| 519 |
ExtraTexCoordField := nil; |
| 520 |
|
| 521 |
TextProxy(Self, Size, Spacing, Justify, JustifyMinor, FdString.Items, |
| 522 |
FdMaxDisplayChars.Value, Font, |
| 523 |
Solid, EventLineBounds, EventOrigin, EventTextBounds, |
| 524 |
CoordNode.FdPoint, TexCoordNode.FdPoint, ExtraTexCoordField); |
| 525 |
|
| 526 |
Result := FaceSet; |
| 527 |
except FreeAndNil(FaceSet); raise end; |
| 528 |
end; |
| 529 |
|
| 530 |
{ constants ------------------------------------------------------------------ } |
| 531 |
|
| 532 |
const |
| 533 |
FontStyleNames: array [TX3DFontStyle] of String = |
| 534 |
('PLAIN', 'BOLD', 'ITALIC', 'BOLDITALIC'); |
| 535 |
|
| 536 |
FontFamilyNames: array [TX3DFontFamily] of string = |
| 537 |
('SERIF', 'SANS', 'TYPEWRITER'); |
| 538 |
|
| 539 |
JustifyToString: array[TX3DFontJustify] of string = |
| 540 |
('FIRST', 'BEGIN', 'MIDDLE', 'END'); |
| 541 |
|
| 542 |
{ TFontStyleNode ------------------------------------------------------------- } |
| 543 |
|
| 544 |
constructor TFontStyleNode.Create(const AX3DName: string = ''; const ABaseUrl: String = ''); |
| 545 |
begin |
| 546 |
inherited; |
| 547 |
FCustomFontObserver := TFreeNotificationObserver.Create(nil); |
| 548 |
FCustomFontObserver.OnFreeNotification := {$ifdef FPC}@{$endif}CustomFontFreeNotification; |
| 549 |
end; |
| 550 |
|
| 551 |
destructor TFontStyleNode.Destroy; |
| 552 |
begin |
| 553 |
FreeAndNil(FCustomFontObserver); |
| 554 |
inherited; |
| 555 |
end; |
| 556 |
|
| 557 |
procedure TFontStyleNode.SetCustomFont(const Value: TCastleFont); |
| 558 |
begin |
| 559 |
if FCustomFont <> Value then |
| 560 |
begin |
| 561 |
FCustomFont := Value; |
| 562 |
FCustomFontObserver.Observed := Value; |
| 563 |
end; |
| 564 |
end; |
| 565 |
|
| 566 |
procedure TFontStyleNode.CustomFontFreeNotification(const Sender: TFreeNotificationObserver); |
| 567 |
begin |
| 568 |
FCustomFont := nil; |
| 569 |
end; |
| 570 |
|
| 571 |
class function TFontStyleNode.ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 572 |
begin |
| 573 |
Result := Version.Major >= 2; |
| 574 |
end; |
| 575 |
|
| 576 |
function TFontStyleNode.GetFamily: TX3DFontFamily; |
| 577 |
var |
| 578 |
I: Integer; |
| 579 |
begin |
| 580 |
for I := 0 to FdFamily.Items.Count - 1 do |
| 581 |
if FdFamily.Items[I] = 'SERIF' then |
| 582 |
Exit(ffSerif) |
| 583 |
else |
| 584 |
if FdFamily.Items[I] = 'SANS' then |
| 585 |
Exit(ffSans) |
| 586 |
else |
| 587 |
if FdFamily.Items[I] = 'TYPEWRITER' then |
| 588 |
Exit(ffTypeWriter) |
| 589 |
else |
| 590 |
WritelnWarning('VRML/X3D', 'Font family "' + FdFamily.Items[I] + '" not supported'); |
| 591 |
|
| 592 |
{ If no supported values on FdFamily.Items then fall back to serif } |
| 593 |
Result := ffSerif; |
| 594 |
end; |
| 595 |
|
| 596 |
procedure TFontStyleNode.SetFamily(const Value: TX3DFontFamily); |
| 597 |
begin |
| 598 |
FdFamily.Send(FontFamilyNames[Value]); |
| 599 |
end; |
| 600 |
|
| 601 |
const |
| 602 |
StyleEnum: array[boolean { bold ? }, boolean { italic ? }] of TX3DFontStyle = |
| 603 |
( (fsPlain, fsItalic), |
| 604 |
(fsBold, fsBoldItalic) ); |
| 605 |
|
| 606 |
function TFontStyleNode.GetBold: boolean; |
| 607 |
begin |
| 608 |
Result := Style in [fsBold, fsBoldItalic]; |
| 609 |
end; |
| 610 |
|
| 611 |
procedure TFontStyleNode.SetBold(const Value: boolean); |
| 612 |
begin |
| 613 |
Style := StyleEnum[Value, Italic]; |
| 614 |
end; |
| 615 |
|
| 616 |
function TFontStyleNode.GetItalic: boolean; |
| 617 |
begin |
| 618 |
Result := Style in [fsItalic, fsBoldItalic]; |
| 619 |
end; |
| 620 |
|
| 621 |
procedure TFontStyleNode.SetItalic(const Value: boolean); |
| 622 |
begin |
| 623 |
Style := StyleEnum[Bold, Value]; |
| 624 |
end; |
| 625 |
|
| 626 |
function TFontStyleNode.StringToJustify(const S: string; |
| 627 |
const DefaultValue: TX3DFontJustify): TX3DFontJustify; |
| 628 |
const |
| 629 |
SJustifyObsolete = 'Font justify "%s" should not be used in VRML >= 2.0, use "%s" instead'; |
| 630 |
var |
| 631 |
J: string; |
| 632 |
begin |
| 633 |
{ Some X3D models use lowercase names, like [http://instant-reality.com/] |
| 634 |
test models. } |
| 635 |
J := UpperCase(S); |
| 636 |
if (J <> S) and not JustifyWarningUppercaseDone then |
| 637 |
begin |
| 638 |
WritelnWarning('VRML/X3D', Format('Font justify value "%s" should be specified in uppercase', |
| 639 |
[S])); |
| 640 |
JustifyWarningUppercaseDone := true; |
| 641 |
end; |
| 642 |
|
| 643 |
if J = '' then Result := DefaultValue else |
| 644 |
if J = 'BEGIN' then Result := fjBegin else |
| 645 |
if J = 'FIRST' then Result := fjFirst else |
| 646 |
if J = 'MIDDLE' then Result := fjMiddle else |
| 647 |
if J = 'END' then Result := fjEnd else |
| 648 |
|
| 649 |
{ Some X3D models use old justify names, like cic.nist.gov X3D demos. } |
| 650 |
if J = 'LEFT' then |
| 651 |
begin |
| 652 |
if not JustifyWarningObsoleteDone then |
| 653 |
begin |
| 654 |
WritelnWarning('VRML/X3D', Format(SJustifyObsolete, ['LEFT', 'BEGIN'])); |
| 655 |
JustifyWarningObsoleteDone := true; |
| 656 |
end; |
| 657 |
Result := fjBegin; |
| 658 |
end else |
| 659 |
if J = 'CENTER' then |
| 660 |
begin |
| 661 |
if not JustifyWarningObsoleteDone then |
| 662 |
begin |
| 663 |
WritelnWarning('VRML/X3D', Format(SJustifyObsolete, ['CENTER', 'MIDDLE'])); |
| 664 |
JustifyWarningObsoleteDone := true; |
| 665 |
end; |
| 666 |
Result := fjMiddle; |
| 667 |
end else |
| 668 |
if J = 'RIGHT' then |
| 669 |
begin |
| 670 |
if not JustifyWarningObsoleteDone then |
| 671 |
begin |
| 672 |
WritelnWarning('VRML/X3D', Format(SJustifyObsolete, ['RIGHT', 'END'])); |
| 673 |
JustifyWarningObsoleteDone := true; |
| 674 |
end; |
| 675 |
Result := fjEnd; |
| 676 |
end else |
| 677 |
|
| 678 |
begin |
| 679 |
Result := DefaultValue; |
| 680 |
if not JustifyWarningNotSupportedDone then |
| 681 |
begin |
| 682 |
WritelnWarning('VRML/X3D', 'Font justify "' + S + '" not supported'); |
| 683 |
JustifyWarningNotSupportedDone := true; |
| 684 |
end; |
| 685 |
end; |
| 686 |
end; |
| 687 |
|
| 688 |
function TFontStyleNode.GetJustify: TX3DFontJustify; |
| 689 |
begin |
| 690 |
if FdJustify.Items.Count > 0 then |
| 691 |
Result := StringToJustify(FdJustify.Items[0], DefaultJustify) |
| 692 |
else |
| 693 |
Result := DefaultJustify; |
| 694 |
end; |
| 695 |
|
| 696 |
function TFontStyleNode.GetJustifyMinor: TX3DFontJustify; |
| 697 |
begin |
| 698 |
if FdJustify.Items.Count > 1 then |
| 699 |
Result := StringToJustify(FdJustify.Items[1], DefaultJustifyMinor) |
| 700 |
else |
| 701 |
Result := DefaultJustifyMinor; |
| 702 |
end; |
| 703 |
|
| 704 |
procedure TFontStyleNode.SetJustify(const Value: TX3DFontJustify); |
| 705 |
begin |
| 706 |
FdJustify.Send([JustifyToString[Value], JustifyToString[JustifyMinor]]); |
| 707 |
end; |
| 708 |
|
| 709 |
procedure TFontStyleNode.SetJustifyMinor(const Value: TX3DFontJustify); |
| 710 |
begin |
| 711 |
FdJustify.Send([JustifyToString[Justify], JustifyToString[Value]]); |
| 712 |
end; |
| 713 |
|
| 714 |
function TFontStyleNode.Font: TCastleFont; |
| 715 |
begin |
| 716 |
{ Check CustomFont.FontLoaded below, because this checks CustomFont.Data <> nil, |
| 717 |
and TX3DFontTexturesCache.LoadFontTexture requires Data <> nil. } |
| 718 |
if (CustomFont <> nil) and CustomFont.FontLoaded then |
| 719 |
Result := CustomFont |
| 720 |
else |
| 721 |
Result := TFontStyleNode.GetDefaultFont(Family, Bold, Italic); |
| 722 |
end; |
| 723 |
|
| 724 |
var |
| 725 |
DefaultFont: array [ |
| 726 |
TX3DFontFamily, |
| 727 |
{ bold } Boolean, |
| 728 |
{ italic } Boolean |
| 729 |
] of TCastleFont; |
| 730 |
|
| 731 |
class function TFontStyleNode.GetDefaultFont(const Family: TX3DFontFamily; const Bold, Italic: Boolean): TCastleFont; |
| 732 |
begin |
| 733 |
{ Create TCastleFont on-demand, as it creates a texture, |
| 734 |
and we don't want to create all 3 * 2 * 2 font textures always, |
| 735 |
when most CGE applications will not use all X3D font versions. } |
| 736 |
if DefaultFont[Family, Bold, Italic] = nil then |
| 737 |
begin |
| 738 |
DefaultFont[Family, Bold, Italic] := TCastleFont.Create(nil); |
| 739 |
DefaultFont[Family, Bold, Italic].Load(DefaultFontData[Family, Bold, Italic]); |
| 740 |
end; |
| 741 |
Result := DefaultFont[Family, Bold, Italic]; |
| 742 |
end; |
| 743 |
|
| 744 |
{ TTextNode ------------------------------------------------------------------ } |
| 745 |
|
| 746 |
destructor TTextNode.Destroy; |
| 747 |
begin |
| 748 |
if X3DCache <> nil then |
| 749 |
begin |
| 750 |
X3DCache.FreeFontTexture(FFontTextureNode); |
| 751 |
// X3DCache.FreeFontTexture must be set it to nil |
| 752 |
Assert(FFontTextureNode = nil); |
| 753 |
end; |
| 754 |
inherited; |
| 755 |
end; |
| 756 |
|
| 757 |
procedure TTextNode.FontChanged; |
| 758 |
begin |
| 759 |
if X3DCache <> nil then |
| 760 |
begin |
| 761 |
X3DCache.FreeFontTexture(FFontTextureNode); |
| 762 |
// X3DCache.FreeFontTexture must be set it to nil |
| 763 |
Assert(FFontTextureNode = nil); |
| 764 |
end; |
| 765 |
end; |
| 766 |
|
| 767 |
function TTextNode.TexCoordField: TSFNode; |
| 768 |
begin |
| 769 |
Result := FdTexCoord; |
| 770 |
end; |
| 771 |
|
| 772 |
function TTextNode.SolidField: TSFBool; |
| 773 |
begin |
| 774 |
Result := FdSolid; |
| 775 |
end; |
| 776 |
|
| 777 |
function TTextNode.Font: TCastleFont; |
| 778 |
begin |
| 779 |
if FontStyle is TFontStyleNode then // also checks FontStyle <> nil |
| 780 |
Result := TFontStyleNode(FontStyle).Font |
| 781 |
else |
| 782 |
Result := TFontStyleNode.GetDefaultFont( |
| 783 |
TFontStyleNode.DefaultFamily, |
| 784 |
TFontStyleNode.DefaultBold, |
| 785 |
TFontStyleNode.DefaultItalic); |
| 786 |
end; |
| 787 |
|
| 788 |
function TTextNode.FontTextureNode: TAbstractTexture2DNode; |
| 789 |
var |
| 790 |
Blending: boolean; |
| 791 |
begin |
| 792 |
if FFontTextureNode = nil then |
| 793 |
begin |
| 794 |
if FontStyle is TFontStyleNode then // also checks FontStyle <> nil |
| 795 |
Blending := TFontStyleNode(FontStyle).Blending |
| 796 |
else |
| 797 |
Blending := TFontStyleNode.DefaultBlending; |
| 798 |
FFontTextureNode := X3DCache.LoadFontTexture(Font, Blending); |
| 799 |
end; |
| 800 |
Result := FFontTextureNode; |
| 801 |
end; |
| 802 |
|
| 803 |
procedure TTextNode.SetString(const Value: array of string); |
| 804 |
begin |
| 805 |
SetText(Value); |
| 806 |
end; |
| 807 |
|
| 808 |
procedure TTextNode.SetString(const Value: TCastleStringList); |
| 809 |
begin |
| 810 |
SetText(Value); |
| 811 |
end; |
| 812 |
|
| 813 |
{ registration --------------------------------------------------------------- } |
| 814 |
|
| 815 |
procedure RegisterTextNodes; |
| 816 |
begin |
| 817 |
NodesManager.RegisterNodeClasses([ |
| 818 |
TFontStyleNode, |
| 819 |
TTextNode |
| 820 |
]); |
| 821 |
end; |
| 822 |
|
| 823 |
procedure FontsInitialization; |
| 824 |
begin |
| 825 |
{ We do not embed in CGE now all 12 font variants, |
| 826 |
see castle-model-viewer/embedded_data/fonts/README.md for explanation. |
| 827 |
So just set all fonts to use default Font_Default3d_Sans. } |
| 828 |
DefaultFontData[ffTypeWriter, true , true ] := Font_Default3d_Sans; |
| 829 |
DefaultFontData[ffTypeWriter, true , false] := Font_Default3d_Sans; |
| 830 |
DefaultFontData[ffTypeWriter, false, true ] := Font_Default3d_Sans; |
| 831 |
DefaultFontData[ffTypeWriter, false, false] := Font_Default3d_Sans; |
| 832 |
|
| 833 |
DefaultFontData[ffSerif, true , true ] := Font_Default3d_Sans; |
| 834 |
DefaultFontData[ffSerif, true , false] := Font_Default3d_Sans; |
| 835 |
DefaultFontData[ffSerif, false, true ] := Font_Default3d_Sans; |
| 836 |
DefaultFontData[ffSerif, false, false] := Font_Default3d_Sans; |
| 837 |
|
| 838 |
DefaultFontData[ffSans, true , true ] := Font_Default3d_Sans; |
| 839 |
DefaultFontData[ffSans, true , false] := Font_Default3d_Sans; |
| 840 |
DefaultFontData[ffSans, false, true ] := Font_Default3d_Sans; |
| 841 |
DefaultFontData[ffSans, false, false] := Font_Default3d_Sans; |
| 842 |
end; |
| 843 |
|
| 844 |
procedure FontsFinalization; |
| 845 |
var |
| 846 |
Family: TX3DFontFamily; |
| 847 |
Bold, Italic: Boolean; |
| 848 |
begin |
| 849 |
for Family := Low(Family) to High(Family) do |
| 850 |
for Bold := Low(Bold) to High(Bold) do |
| 851 |
for Italic := Low(Italic) to High(Italic) do |
| 852 |
FreeAndNil(DefaultFont[Family, Bold, Italic]); |
| 853 |
end; |
| 854 |
|
| 855 |
{$endif read_implementation} |