| 1 |
{ |
| 2 |
This file is based on the Free Pascal run time library, |
| 3 |
remade a lot for Castle Game Engine. |
| 4 |
|
| 5 |
Copyright by the Free Pascal development team and Michalis Kamburelis. |
| 6 |
|
| 7 |
License: |
| 8 |
This file is adapted from the FPC RTL source code, as such |
| 9 |
the license and copyright information of FPC RTL applies here. |
| 10 |
That said, the license of FPC RTL happens to be *exactly* |
| 11 |
the same as used by the "Castle Game Engine": LGPL (version 2.1) |
| 12 |
with "static linking exception" (with exactly the same wording |
| 13 |
of the "static linking exception"). |
| 14 |
See the file COPYING.txt, included in this distribution, for details about |
| 15 |
the copyright of "Castle Game Engine". |
| 16 |
See http://www.freepascal.org/faq.var#general-license about the copyright |
| 17 |
of FPC RTL. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 22 |
|
| 23 |
---------------------------------------------------------------------------- |
| 24 |
} |
| 25 |
|
| 26 |
{ Manager for FreeType fonts, serving as a middle-man between FreeType library API |
| 27 |
(in CastleInternalFreeTypeH) and CGE code loading font into image |
| 28 |
(in CastleTextureFontData). |
| 29 |
|
| 30 |
This is based on a similar FPC concept, but |
| 31 |
|
| 32 |
- We simplified this unit a lot to better suit our purpose. |
| 33 |
We don't need platform-specific searching for fonts, |
| 34 |
or platform-specific resolutions etc. |
| 35 |
We only want to open font files from URLs. |
| 36 |
|
| 37 |
- Loading fonts using from any URL supported by CGE, |
| 38 |
thanks to using Download and FT_New_Memory_Face underneath. |
| 39 |
|
| 40 |
- raise exception from TFontManager.Create when FreeType library not found |
| 41 |
|
| 42 |
- Use UTF-8 encoding for string and Cardinal for character type, |
| 43 |
following David Emerson patch from |
| 44 |
http://free-pascal-general.1045716.n5.nabble.com/freetype-unit-unicode-td4866273.html |
| 45 |
adjusted to use our CastleUnicode unit. |
| 46 |
} |
| 47 |
unit CastleInternalFreeType; |
| 48 |
|
| 49 |
{$I castleconf.inc} |
| 50 |
|
| 51 |
interface |
| 52 |
|
| 53 |
uses SysUtils, Classes, Types, |
| 54 |
CastleInternalFreeTypeH, CastleUnicode, CastleUtils; |
| 55 |
|
| 56 |
// determine if file comparison need to be case sensitive or not |
| 57 |
{$ifdef WIN32} |
| 58 |
{$undef CaseSense} |
| 59 |
{$else} |
| 60 |
{$define CaseSense} |
| 61 |
{$endif} |
| 62 |
|
| 63 |
type |
| 64 |
FreeTypeException = class (exception); |
| 65 |
|
| 66 |
{ Raised by TFontManager.Create when the freetype library is not available. } |
| 67 |
EFreeTypeLibraryNotFound = class (FreeTypeException); |
| 68 |
|
| 69 |
TBitmapType = (btBlackWhite, bt256Gray); |
| 70 |
TFontBitmap = record |
| 71 |
height, width, pitch, |
| 72 |
x,y, advanceX, advanceY : integer; |
| 73 |
{ for some reason, default FPC TByteArray has limited length (0..32767). |
| 74 |
We sometimes need longer (if you try to generate some huge font size, like 300). } |
| 75 |
data : CastleUtils.PByteArray; |
| 76 |
end; |
| 77 |
PFontBitmap = ^TFontBitmap; |
| 78 |
|
| 79 |
TStringBitMaps = class |
| 80 |
private |
| 81 |
FList : TList; |
| 82 |
FBounds : TRect; |
| 83 |
FText : string; |
| 84 |
FMode : TBitmapType; |
| 85 |
function GetCount : integer; |
| 86 |
function GetBitmap (index:integer) : PFontBitmap; |
| 87 |
procedure CalculateGlobals; |
| 88 |
public |
| 89 |
constructor Create (ACount : integer); |
| 90 |
destructor destroy; override; |
| 91 |
procedure GetBoundRect (out aRect : TRect); |
| 92 |
property Text : string read FText; |
| 93 |
property Mode : TBitmapType read FMode; |
| 94 |
property Count : integer read GetCount; |
| 95 |
property Bitmaps[index:integer] : PFontBitmap read GetBitmap; |
| 96 |
end; |
| 97 |
|
| 98 |
TFontManager = class; |
| 99 |
|
| 100 |
PMgrGlyph = ^TMgrGlyph; |
| 101 |
TMgrGlyph = record |
| 102 |
Character : TUnicodeChar; |
| 103 |
GlyphIndex : FT_UInt; |
| 104 |
Glyph : PFT_Glyph; |
| 105 |
end; |
| 106 |
|
| 107 |
PMgrSize = ^TMgrSize; |
| 108 |
TMgrSize = record |
| 109 |
Size : integer; |
| 110 |
Glyphs : TList; |
| 111 |
end; |
| 112 |
|
| 113 |
TMgrFont = class |
| 114 |
private |
| 115 |
Mgr : TFontManager; |
| 116 |
Font : PFT_Face; |
| 117 |
FSizes : TList; |
| 118 |
Filename : string; |
| 119 |
LastSize : PMgrSize; |
| 120 |
MemoryStream: TCustomMemoryStream; |
| 121 |
procedure FreeGlyphs; |
| 122 |
public |
| 123 |
constructor Create (aMgr:TFontManager; afilename:string; anindex:integer); |
| 124 |
destructor destroy; override; |
| 125 |
end; |
| 126 |
|
| 127 |
TFontManager = class |
| 128 |
private |
| 129 |
FTLib : PFT_Library; |
| 130 |
FList : TList; |
| 131 |
FPaths : TStringList; |
| 132 |
CurFont : TMgrFont; |
| 133 |
CurSize : PMgrSize; |
| 134 |
CurRenderMode : FT_Render_Mode; |
| 135 |
UseKerning : boolean; |
| 136 |
protected |
| 137 |
function GetFontId (afilename:string; anindex:integer) : integer; |
| 138 |
function CreateFont (afilename:string; anindex:integer) : integer; |
| 139 |
//function SearchFont (afilename:string) : string; |
| 140 |
function GetFont (FontID:integer) : TMgrFont; |
| 141 |
procedure GetSize (aSize : integer); |
| 142 |
function CreateSize (aSize : integer) : PMgrSize; |
| 143 |
procedure SetPixelSize (aSize : integer); |
| 144 |
function GetGlyph (c : TUnicodeChar) : PMgrGlyph; |
| 145 |
function CreateGlyph (c : TUnicodeChar) : PMgrGlyph; |
| 146 |
procedure InitMakeString (FontID, Size:integer); |
| 147 |
function MakeString (FontId:integer; Text:string; Size:integer) : TStringBitmaps; overload; |
| 148 |
public |
| 149 |
{ @raises EFreeTypeLibraryNotFound } |
| 150 |
constructor Create; |
| 151 |
destructor destroy; override; |
| 152 |
function RequestFont (afilename:string) : integer; overload; |
| 153 |
function RequestFont (afilename:string; anindex:integer) : integer; overload; |
| 154 |
function GetFreeTypeFont (aFontID:integer) : PFT_Face; |
| 155 |
function GetString (FontId:integer; Text:string; Size:integer) : TStringBitmaps; overload; |
| 156 |
// Black and white, following the direction of the font (left to right, top to bottom, ...) |
| 157 |
function GetStringGray (FontId:integer; Text:string; Size:integer) : TStringBitmaps; overload; |
| 158 |
// Anti Aliased gray scale, following the direction of the font (left to right, top to bottom, ...) |
| 159 |
end; |
| 160 |
|
| 161 |
const |
| 162 |
sErrErrorsInCleanup : string = '%d errors detected while freeing a Font Manager object'; |
| 163 |
sErrFontFileNotFound : string = 'Font file "%s" not found'; |
| 164 |
sErrFreeType : string = 'Error %d while %s'; |
| 165 |
sInitializing : string = 'initializing font engine'; |
| 166 |
sDestroying : string = 'destroying font engine'; |
| 167 |
sErrErrorInCleanup : string = 'freeing Font Manager object'; |
| 168 |
sErrSetPixelSize : string = 'setting pixel size %d'; |
| 169 |
sErrSetCharSize : string = 'setting char size %d'; |
| 170 |
sErrLoadingGlyph : string = 'loading glyph'; |
| 171 |
sErrKerning : string = 'determining kerning distance'; |
| 172 |
sErrMakingString1 : string = 'making string bitmaps step 1'; |
| 173 |
sErrMakingString2 : string = 'making string bitmaps step 2'; |
| 174 |
sErrMakingString3 : string = 'making string bitmaps step 3'; |
| 175 |
sErrMakingString4 : string = 'making string bitmaps step 4'; |
| 176 |
sErrLoadFont : string = 'loading font %d from file %s'; |
| 177 |
sErrInitializing : string = 'initializing FreeType'; |
| 178 |
sErrDestroying : string = 'finalizing FreeType'; |
| 179 |
|
| 180 |
var |
| 181 |
FontMgr : TFontManager; |
| 182 |
|
| 183 |
{ @raises EFreeTypeLibraryNotFound } |
| 184 |
procedure InitFontMgr; |
| 185 |
procedure DoneFontMgr; |
| 186 |
|
| 187 |
implementation |
| 188 |
|
| 189 |
uses {$ifdef FPC}{$IFDEF win32}dos, {$ENDIF}{$endif} |
| 190 |
CastleDownload; |
| 191 |
|
| 192 |
procedure FTError (Event:string; Err:integer); |
| 193 |
begin |
| 194 |
raise FreeTypeException.CreateFmt (sErrFreeType, [Err,Event]); |
| 195 |
end; |
| 196 |
|
| 197 |
Function FTCheck (Res: Integer; Msg:string) : Integer; |
| 198 |
|
| 199 |
begin |
| 200 |
Result:=Res; |
| 201 |
If (Result<>0) then |
| 202 |
FTError(Msg,Result); |
| 203 |
end; |
| 204 |
|
| 205 |
{procedure WriteFT_Face(CurFont: PFT_Face); |
| 206 |
var |
| 207 |
i: Integer; |
| 208 |
begin |
| 209 |
writeln(' num_faces=',CurFont^.num_faces); |
| 210 |
writeln(' face_index=',CurFont^.face_index); |
| 211 |
writeln(' face_flags=',CurFont^.face_flags); |
| 212 |
writeln(' style_flags=',CurFont^.style_flags); |
| 213 |
writeln(' num_glyphs=',CurFont^.num_glyphs); |
| 214 |
writeln(' family_name=',CurFont^.family_name<>nil); |
| 215 |
writeln(' style_name=',CurFont^.style_name<>nil); |
| 216 |
// if CurFont^.style_name<>nil then begin |
| 217 |
// writeln(' ',CurFont^.style_name^); |
| 218 |
// end; |
| 219 |
writeln(' num_fixed_sizes=',CurFont^.num_fixed_sizes); |
| 220 |
writeln(' available_sizes=',CurFont^.available_sizes<>nil); |
| 221 |
for i:=1 to CurFont^.num_fixed_sizes do begin |
| 222 |
writeln(' ',i,' ',CurFont^.available_sizes^[i-1].width,'x',CurFont^.available_sizes^[i-1].height); |
| 223 |
end; |
| 224 |
writeln(' num_charmaps=',CurFont^.num_charmaps); |
| 225 |
writeln(' charmaps=',CurFont^.charmaps<>nil); |
| 226 |
writeln(' generic.data=',CurFont^.generic.data<>nil); |
| 227 |
writeln(' generic.finalizer=',CurFont^.generic.finalizer<>nil); |
| 228 |
writeln(' bbox.xMin=',CurFont^.bbox.xMin, |
| 229 |
' bbox.xMax=',CurFont^.bbox.xMax, |
| 230 |
' bbox.yMin=',CurFont^.bbox.yMin, |
| 231 |
' bbox.yMax=',CurFont^.bbox.yMax, |
| 232 |
' units_per_EM=',CurFont^.units_per_EM, |
| 233 |
' ascender=',CurFont^.ascender, |
| 234 |
' descender=',CurFont^.descender, |
| 235 |
' height=',CurFont^.height, |
| 236 |
' max_advance_width=',CurFont^.max_advance_width, |
| 237 |
' max_advance_height=',CurFont^.max_advance_height, |
| 238 |
' underline_position=',CurFont^.underline_position, |
| 239 |
' underline_thickness=',CurFont^.underline_thickness, |
| 240 |
' glyph=',CurFont^.glyph<>nil, |
| 241 |
' size=',CurFont^.size<>nil, |
| 242 |
' charmap=',CurFont^.charmap<>nil, |
| 243 |
''); |
| 244 |
end;} |
| 245 |
|
| 246 |
{ TMgrFont } |
| 247 |
|
| 248 |
constructor TMgrFont.Create (aMgr:TFontManager; afilename:string; anindex:integer); |
| 249 |
begin |
| 250 |
inherited create; |
| 251 |
Filename := afilename; |
| 252 |
Mgr := aMgr; |
| 253 |
FSizes := TList.create; |
| 254 |
LastSize := nil; |
| 255 |
|
| 256 |
Try |
| 257 |
{ Old code: |
| 258 |
FTCheck(FT_New_Face (aMgr.FTLib, PAnsiChar(AnsiString(afilename)), anindex, font),format (sErrLoadFont,[anindex,afilename])); |
| 259 |
|
| 260 |
This mostly worked OK, except on Windows where it fails reading |
| 261 |
a filename with non-ASCII characters. |
| 262 |
Also it required a temporary file when accessing non-file URLs. |
| 263 |
So now we make it simpler: just use CGE Download() to open the URL |
| 264 |
(afilename can be any URL), and pass memory chunk to FreeType using |
| 265 |
FT_New_Memory_Face. |
| 266 |
} |
| 267 |
|
| 268 |
MemoryStream := Download(afilename, [soForceMemoryStream]) as TCustomMemoryStream; |
| 269 |
FTCheck(FT_New_Memory_Face(aMgr.FTLib, MemoryStream.Memory, MemoryStream.Size, anindex, font), |
| 270 |
format (sErrLoadFont,[anindex,afilename])); |
| 271 |
|
| 272 |
{ We will free MemoryStream only in our destructor. |
| 273 |
Otherwise weirdest errors can occur when reading the existing font 2nd time |
| 274 |
(autotests see weird Height), |
| 275 |
this is also said in docs |
| 276 |
https://freetype.org/freetype2/docs/reference/ft2-base_interface.html#ft_new_memory_face : |
| 277 |
"You must not deallocate the memory before calling FT_Done_Face." |
| 278 |
} |
| 279 |
|
| 280 |
//WriteFT_Face(font); |
| 281 |
except |
| 282 |
Font:=Nil; |
| 283 |
Raise; |
| 284 |
end; |
| 285 |
end; |
| 286 |
|
| 287 |
destructor TMgrFont.destroy; |
| 288 |
begin |
| 289 |
try |
| 290 |
FreeGlyphs; |
| 291 |
finally |
| 292 |
FreeAndNil(MemoryStream); |
| 293 |
FSizes.Free; |
| 294 |
inherited Destroy; |
| 295 |
end; |
| 296 |
end; |
| 297 |
|
| 298 |
procedure TMgrFont.FreeGlyphs; |
| 299 |
var r,t : integer; |
| 300 |
S : PMgrSize; |
| 301 |
G : PMgrGlyph; |
| 302 |
begin |
| 303 |
for r := FSizes.count-1 downto 0 do |
| 304 |
begin |
| 305 |
with PMgrSize(FSizes[r])^ do |
| 306 |
begin |
| 307 |
for t := Glyphs.count-1 downto 0 do |
| 308 |
begin |
| 309 |
with PMgrGlyph(Glyphs[t])^ do |
| 310 |
FT_Done_Glyph (Glyph); |
| 311 |
G := PMgrGlyph(Glyphs[t]); |
| 312 |
dispose (G); |
| 313 |
end; |
| 314 |
Glyphs.Free; |
| 315 |
end; |
| 316 |
S := PMgrSize(FSizes[r]); |
| 317 |
dispose (S); |
| 318 |
end; |
| 319 |
end; |
| 320 |
|
| 321 |
|
| 322 |
{ TFontManager } |
| 323 |
|
| 324 |
constructor TFontManager.Create; |
| 325 |
var r : integer; |
| 326 |
begin |
| 327 |
inherited create; |
| 328 |
FList := Tlist.Create; |
| 329 |
FPaths := TStringList.Create; |
| 330 |
|
| 331 |
if not FreeTypeLibraryInitialized then |
| 332 |
raise EFreeTypeLibraryNotFound.Create('Cannot load FreeType library, loading font files not possible'); |
| 333 |
|
| 334 |
r := FT_Init_FreeType(FTLib); |
| 335 |
if r <> 0 then |
| 336 |
begin |
| 337 |
FTLib := nil; |
| 338 |
FTError (sErrInitializing, r); |
| 339 |
end; |
| 340 |
end; |
| 341 |
|
| 342 |
destructor TFontManager.Destroy; |
| 343 |
procedure FreeFontObjects; |
| 344 |
var r : integer; |
| 345 |
begin |
| 346 |
for r := FList.Count-1 downto 0 do |
| 347 |
begin |
| 348 |
GetFont(r).Free; |
| 349 |
end; |
| 350 |
end; |
| 351 |
procedure FreeLibrary; |
| 352 |
var r : integer; |
| 353 |
begin |
| 354 |
r := FT_Done_FreeType (FTlib); |
| 355 |
if r <> 0 then |
| 356 |
FTError (sErrDestroying, r); |
| 357 |
end; |
| 358 |
begin |
| 359 |
FreeFontObjects; |
| 360 |
FList.Free; |
| 361 |
FPaths.Free; |
| 362 |
try |
| 363 |
if assigned(FTLib) then |
| 364 |
FreeLibrary; |
| 365 |
finally |
| 366 |
inherited Destroy; |
| 367 |
end; |
| 368 |
end; |
| 369 |
|
| 370 |
function TFontManager.GetFontId (afilename:string; anindex:integer) : integer; |
| 371 |
begin |
| 372 |
result := FList.count-1; |
| 373 |
while (result >= 0) and |
| 374 |
( ({$ifdef CaseSense}CompareText{$else}CompareStr{$endif} |
| 375 |
(TMgrFont(FList[Result]).Filename, afilename) <> 0) or |
| 376 |
(anIndex <> TMgrFont(FList[Result]).font^.face_index) |
| 377 |
) do |
| 378 |
dec (result); |
| 379 |
end; |
| 380 |
|
| 381 |
function TFontManager.CreateFont (afilename:string; anindex:integer) : integer; |
| 382 |
var f : TMgrFont; |
| 383 |
begin |
| 384 |
// writeln ('creating font ',afilename,' (',anindex,')'); |
| 385 |
f := TMgrFont.Create (self, afilename, anindex); |
| 386 |
result := FList.Count; |
| 387 |
Flist.Add (f); |
| 388 |
end; |
| 389 |
|
| 390 |
function TFontManager.GetFont (FontID:integer) : TMgrFont; |
| 391 |
begin |
| 392 |
if (FontID >= 0) and (FontID < FList.Count) then |
| 393 |
begin |
| 394 |
result := TMgrFont(FList[FontID]); |
| 395 |
|
| 396 |
if result <> CurFont then // set last used size of the font as current size |
| 397 |
begin |
| 398 |
CurSize := result.LastSize; |
| 399 |
end; |
| 400 |
end |
| 401 |
else |
| 402 |
Result := nil; |
| 403 |
end; |
| 404 |
|
| 405 |
procedure TFontManager.GetSize (aSize : integer); |
| 406 |
var r : integer; |
| 407 |
begin |
| 408 |
if not ( assigned(CurSize) and |
| 409 |
(CurSize^.Size = aSize) ) then |
| 410 |
begin |
| 411 |
r := CurFont.FSizes.count; |
| 412 |
repeat |
| 413 |
dec (r) |
| 414 |
until (r < 0) or ( (PMgrSize(CurFont.FSizes[r])^.size = aSize) ); |
| 415 |
if r < 0 then |
| 416 |
CurSize := CreateSize (aSize) |
| 417 |
else |
| 418 |
CurSize := PMgrSize(CurFont.FSizes[r]); |
| 419 |
CurFont.LastSize := CurSize; |
| 420 |
end; |
| 421 |
end; |
| 422 |
|
| 423 |
function TFontManager.CreateSize (aSize : integer) : PMgrSize; |
| 424 |
begin |
| 425 |
new (result); |
| 426 |
result^.Size := aSize; |
| 427 |
result^.Glyphs := Tlist.Create; |
| 428 |
SetPixelSize (aSize); |
| 429 |
CurFont.FSizes.Add (result); |
| 430 |
end; |
| 431 |
|
| 432 |
procedure TFontManager.SetPixelSize (aSize : integer); |
| 433 |
|
| 434 |
procedure CheckSize; |
| 435 |
var r : integer; |
| 436 |
begin |
| 437 |
with Curfont.Font^ do |
| 438 |
begin |
| 439 |
r := Num_fixed_sizes; |
| 440 |
repeat |
| 441 |
dec (r); |
| 442 |
until (r < 0) or |
| 443 |
( (available_sizes^[r].height=asize) and |
| 444 |
(available_sizes^[r].width=asize) ); |
| 445 |
if r >= 0 then |
| 446 |
raise FreeTypeException.CreateFmt ('Size %d not available for %s %s', |
| 447 |
[aSize, style_name, family_name]); |
| 448 |
end; |
| 449 |
end; |
| 450 |
|
| 451 |
var s : Integer; |
| 452 |
Err : integer; |
| 453 |
|
| 454 |
const |
| 455 |
{ Use 0, letting FreeType library use good default, |
| 456 |
http://www.freetype.org/freetype2/docs/tutorial/step1.html , |
| 457 |
and in effect Size is in nice pixels by default. } |
| 458 |
Resolution = 0; |
| 459 |
begin |
| 460 |
with Curfont, Font^ do |
| 461 |
if (face_flags and FT_Face_Flag_Fixed_Sizes) <> 0 then |
| 462 |
begin |
| 463 |
CheckSize; |
| 464 |
Err := FT_Set_pixel_sizes (Font, aSize, aSize); |
| 465 |
if Err <> 0 then |
| 466 |
FTError (format(sErrSetPixelSize,[aSize]), Err); |
| 467 |
end |
| 468 |
else |
| 469 |
begin |
| 470 |
s := aSize shl 6; |
| 471 |
Err := FT_Set_char_size (Font, s, s, Resolution, Resolution); |
| 472 |
if Err <> 0 then |
| 473 |
FTError (format(sErrSetCharSize,[aSize]), Err); |
| 474 |
end; |
| 475 |
end; |
| 476 |
|
| 477 |
function TFontManager.CreateGlyph (c : TUnicodeChar) : PMgrGlyph; |
| 478 |
var e : integer; |
| 479 |
begin |
| 480 |
new (result); |
| 481 |
FillByte(Result^,SizeOf(Result),0); |
| 482 |
result^.character := c; |
| 483 |
result^.GlyphIndex := FT_Get_Char_Index (CurFont.font, c); |
| 484 |
//WriteFT_Face(CurFont.Font); |
| 485 |
e := FT_Load_Glyph (CurFont.font, result^.GlyphIndex, FT_Load_Default); |
| 486 |
if e <> 0 then |
| 487 |
begin |
| 488 |
FTError (sErrLoadingGlyph, e); |
| 489 |
end; |
| 490 |
e := FT_Get_Glyph (Curfont.font^.glyph, result^.glyph); |
| 491 |
if e <> 0 then |
| 492 |
begin |
| 493 |
FTError (sErrLoadingGlyph, e); |
| 494 |
end; |
| 495 |
CurSize^.Glyphs.Add (result); |
| 496 |
end; |
| 497 |
|
| 498 |
function TFontManager.GetGlyph (c : TUnicodeChar) : PMgrGlyph; |
| 499 |
var r : integer; |
| 500 |
begin |
| 501 |
With CurSize^ do |
| 502 |
begin |
| 503 |
r := Glyphs.Count; |
| 504 |
repeat |
| 505 |
dec (r) |
| 506 |
until (r < 0) or (PMgrGlyph(Glyphs[r])^.character = c); |
| 507 |
if r < 0 then |
| 508 |
result := CreateGlyph (c) |
| 509 |
else |
| 510 |
result := PMgrGlyph(Glyphs[r]); |
| 511 |
end; |
| 512 |
end; |
| 513 |
|
| 514 |
procedure TFontManager.InitMakeString (FontID, Size:integer); |
| 515 |
begin |
| 516 |
GetSize (size); |
| 517 |
UseKerning := ((Curfont.font^.face_flags and FT_FACE_FLAG_KERNING) <> 0); |
| 518 |
end; |
| 519 |
|
| 520 |
function TFontManager.MakeString(FontId: Integer; Text: String; Size: Integer): TStringBitmaps; |
| 521 |
var g : PMgrGlyph; |
| 522 |
bm : PFT_BitmapGlyph; |
| 523 |
gl : PFT_Glyph; |
| 524 |
e, prevIndex, prevx, r, rx : integer; |
| 525 |
Iter: TCastleStringIterator; |
| 526 |
uc : TUnicodeChar; |
| 527 |
pos, kern : FT_Vector; |
| 528 |
buf : CastleUtils.PByteArray; |
| 529 |
reverse : boolean; |
| 530 |
begin |
| 531 |
CurFont := GetFont(FontID); |
| 532 |
InitMakeString (FontID, Size); |
| 533 |
result := TStringBitmaps.Create(StringLength(Text)); |
| 534 |
if (CurRenderMode = FT_RENDER_MODE_MONO) then |
| 535 |
result.FMode := btBlackWhite |
| 536 |
else |
| 537 |
result.FMode := bt256Gray; |
| 538 |
prevIndex := 0; |
| 539 |
prevx := 0; |
| 540 |
pos.x := 0; |
| 541 |
pos.y := 0; |
| 542 |
r := -1; |
| 543 |
Iter.Start(Text); |
| 544 |
while Iter.GetNext do |
| 545 |
begin |
| 546 |
uc := Iter.Current; |
| 547 |
// retrieve loaded glyph |
| 548 |
g := GetGlyph (uc); |
| 549 |
inc (r); |
| 550 |
// check kerning |
| 551 |
if UseKerning and (g^.glyphindex <>0) and (PrevIndex <> 0) then |
| 552 |
begin |
| 553 |
prevx := pos.x; |
| 554 |
e := FT_Get_Kerning (Curfont.Font, prevIndex, g^.GlyphIndex, ft_kerning_default, kern); |
| 555 |
if e <> 0 then |
| 556 |
FTError (sErrKerning, e); |
| 557 |
pos.x := pos.x + kern.x; |
| 558 |
end; |
| 559 |
// render the glyph |
| 560 |
FTCheck(FT_Glyph_Copy (g^.glyph, gl),sErrMakingString1); |
| 561 |
FTCheck(FT_Glyph_To_Bitmap (gl, CurRenderMode, @pos, true),sErrMakingString4); |
| 562 |
// Copy what is needed to record |
| 563 |
bm := PFT_BitmapGlyph(gl); |
| 564 |
with result.Bitmaps[r]^ do |
| 565 |
begin |
| 566 |
with gl^.advance do |
| 567 |
begin |
| 568 |
advanceX := x shr 6; |
| 569 |
advanceY := y shr 6; |
| 570 |
end; |
| 571 |
with bm^ do |
| 572 |
begin |
| 573 |
height := bitmap.rows; |
| 574 |
width := bitmap.width; |
| 575 |
x := (pos.x shr 6) + left; // transformed bitmap has correct x,y |
| 576 |
y := (pos.y shr 6) - top; // not transformed has only a relative correction |
| 577 |
buf := CastleUtils.PByteArray(bitmap.buffer); |
| 578 |
reverse := (bitmap.pitch < 0); |
| 579 |
if reverse then |
| 580 |
begin |
| 581 |
pitch := -bitmap.pitch; |
| 582 |
{$ifndef FPC}System.{$endif}getmem (data, pitch*height); |
| 583 |
for rx := height-1 downto 0 do |
| 584 |
move (buf^[rx*pitch], data^[(height-rx-1)*pitch], pitch); |
| 585 |
end |
| 586 |
else |
| 587 |
begin |
| 588 |
pitch := bitmap.pitch; |
| 589 |
rx := pitch*height; |
| 590 |
{$ifndef FPC}System.{$endif}getmem (data, rx); |
| 591 |
move (buf^[0], data^[0], rx); |
| 592 |
end; |
| 593 |
end; |
| 594 |
end; |
| 595 |
// place position for next glyph |
| 596 |
// The previous code in this place used shr 10, which |
| 597 |
// produces wrongly spaced text and looks very ugly |
| 598 |
// for more information see: http://bugs.freepascal.org/view.php?id=17156 |
| 599 |
pos.x := pos.x + (gl^.advance.x shr 11); |
| 600 |
// pos.y := pos.y + (gl^.advance.y shr 6); // for angled texts also |
| 601 |
if prevx > pos.x then |
| 602 |
pos.x := prevx; |
| 603 |
// finish rendered glyph |
| 604 |
FT_Done_Glyph (gl); |
| 605 |
// Get the next unicode |
| 606 |
end; // while |
| 607 |
result.FText := Text; |
| 608 |
result.CalculateGlobals; |
| 609 |
end; |
| 610 |
|
| 611 |
function TFontManager.GetString (FontId:integer; Text:string; Size:integer) : TStringBitmaps; |
| 612 |
// Black and white, following the direction of the font (left to right, top to bottom, ...) |
| 613 |
begin |
| 614 |
CurRenderMode := FT_RENDER_MODE_MONO; |
| 615 |
result := MakeString (FontID, text, Size); |
| 616 |
end; |
| 617 |
|
| 618 |
function TFontManager.GetStringGray (FontId:integer; Text:string; Size:integer) : TStringBitmaps; |
| 619 |
// Anti Aliased gray scale, following the direction of the font (left to right, top to bottom, ...) |
| 620 |
begin |
| 621 |
CurRenderMode := FT_RENDER_MODE_NORMAL; |
| 622 |
result := MakeString (FontID, text, Size); |
| 623 |
end; |
| 624 |
|
| 625 |
function TFontManager.RequestFont (afilename:string) : integer; |
| 626 |
begin |
| 627 |
result := RequestFont (afilename,0); |
| 628 |
end; |
| 629 |
|
| 630 |
function TFontManager.RequestFont (afilename:string; anindex:integer) : integer; |
| 631 |
begin |
| 632 |
if afilename = '' then |
| 633 |
result := -1 |
| 634 |
else |
| 635 |
begin |
| 636 |
result := GetFontID (afilename,anindex); |
| 637 |
if result < 0 then |
| 638 |
result := CreateFont (afilename,anindex); |
| 639 |
end; |
| 640 |
end; |
| 641 |
|
| 642 |
function TFontManager.GetFreeTypeFont (aFontID:integer) : PFT_Face; |
| 643 |
begin |
| 644 |
result := GetFont(aFontID).font; |
| 645 |
end; |
| 646 |
|
| 647 |
{ TStringBitmaps } |
| 648 |
|
| 649 |
function TStringBitmaps.GetCount : integer; |
| 650 |
begin |
| 651 |
result := FList.Count; |
| 652 |
end; |
| 653 |
|
| 654 |
function TStringBitmaps.GetBitmap (index:integer) : PFontBitmap; |
| 655 |
begin |
| 656 |
result := PFontBitmap(FList[index]); |
| 657 |
end; |
| 658 |
|
| 659 |
constructor TStringBitmaps.Create (ACount : integer); |
| 660 |
var r : integer; |
| 661 |
bm : PFontBitmap; |
| 662 |
begin |
| 663 |
inherited create; |
| 664 |
FList := Tlist.Create; |
| 665 |
FList.Capacity := ACount; |
| 666 |
for r := 0 to ACount-1 do |
| 667 |
begin |
| 668 |
new (bm); |
| 669 |
FList.Add (bm); |
| 670 |
end; |
| 671 |
end; |
| 672 |
|
| 673 |
destructor TStringBitmaps.destroy; |
| 674 |
var r : integer; |
| 675 |
bm : PFontBitmap; |
| 676 |
begin |
| 677 |
for r := 0 to Flist.count-1 do |
| 678 |
begin |
| 679 |
bm := PFontBitmap(FList[r]); |
| 680 |
freemem (bm^.data); |
| 681 |
dispose (bm); |
| 682 |
end; |
| 683 |
FList.Free; |
| 684 |
inherited; |
| 685 |
end; |
| 686 |
|
| 687 |
procedure TStringBitmaps.CalculateGlobals; |
| 688 |
var r : integer; |
| 689 |
begin |
| 690 |
if count = 0 then |
| 691 |
Exit; |
| 692 |
// check first 2 bitmaps for left side |
| 693 |
// check last 2 bitmaps for right side |
| 694 |
with BitMaps[0]^ do |
| 695 |
begin |
| 696 |
FBounds.left := x; |
| 697 |
FBounds.top := y + height; |
| 698 |
FBounds.bottom := y; |
| 699 |
end; |
| 700 |
with Bitmaps[count-1]^ do |
| 701 |
FBounds.right := x + width; |
| 702 |
if count > 1 then |
| 703 |
begin |
| 704 |
with Bitmaps[1]^ do |
| 705 |
r := x; |
| 706 |
if r < FBounds.left then |
| 707 |
FBounds.left := r; |
| 708 |
with Bitmaps[count-2]^ do |
| 709 |
r := x + width; |
| 710 |
if r > FBounds.right then |
| 711 |
FBounds.right := r; |
| 712 |
end; |
| 713 |
// check top/bottom of other bitmaps |
| 714 |
for r := 1 to count-1 do |
| 715 |
with Bitmaps[r]^ do |
| 716 |
begin |
| 717 |
if FBounds.top < y + height then |
| 718 |
FBounds.top := y + height; |
| 719 |
if FBounds.bottom > y then |
| 720 |
FBounds.bottom := y; |
| 721 |
end; |
| 722 |
end; |
| 723 |
|
| 724 |
procedure TStringBitmaps.GetBoundRect (out aRect : TRect); |
| 725 |
begin |
| 726 |
aRect := FBounds; |
| 727 |
end; |
| 728 |
|
| 729 |
procedure InitFontMgr; |
| 730 |
begin |
| 731 |
if not assigned (FontMgr) then |
| 732 |
FontMgr := TFontManager.create; |
| 733 |
end; |
| 734 |
|
| 735 |
procedure DoneFontMgr; |
| 736 |
begin |
| 737 |
if assigned (FontMgr) then |
| 738 |
FontMgr.Free; |
| 739 |
end; |
| 740 |
|
| 741 |
initialization |
| 742 |
finalization |
| 743 |
DoneFontMgr; |
| 744 |
end. |