| 1 |
{ |
| 2 |
Copyright 2004-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Convert font files (.ttf, .otf and other formats handled by FreeType) to Pascal units, |
| 17 |
to embed fonts inside source code. } |
| 18 |
|
| 19 |
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif} |
| 20 |
|
| 21 |
uses Classes, SysUtils, |
| 22 |
CastleFont2Pascal, CastleUtils, CastleClassUtils, CastleLog, |
| 23 |
CastleParameters, CastleTextureFontData, CastleStringUtils, |
| 24 |
CastleUriUtils, CastleUnicode, CastleFilesUtils, |
| 25 |
CastleImages, CastleApplicationProperties, CastleLocalizationGetText; |
| 26 |
|
| 27 |
var |
| 28 |
Size: Integer = 10; |
| 29 |
AntiAliasing: boolean = true; |
| 30 |
ParamUnitName, ParamFunctionName: string; |
| 31 |
OnlySampleText: boolean = false; |
| 32 |
DebugFontImage: boolean = false; |
| 33 |
Characters: TUnicodeCharList; |
| 34 |
|
| 35 |
const |
| 36 |
Options: array [0..12] of TOption = |
| 37 |
( |
| 38 |
(Short: 'h'; Long: 'help'; Argument: oaNone), |
| 39 |
(Short: 'v'; Long: 'version'; Argument: oaNone), |
| 40 |
(Short: #0; Long: 'size'; Argument: oaRequired), |
| 41 |
(Short: #0; Long: 'no-anti-alias'; Argument: oaNone), |
| 42 |
(Short: #0; Long: 'sample-text'; Argument: oaRequired), |
| 43 |
(Short: #0; Long: 'sample-text-file'; Argument: oaRequired), |
| 44 |
(Short: #0; Long: 'sample-code'; Argument: oaRequired), |
| 45 |
(Short: #0; Long: 'sample-get-text-mo'; Argument: oaRequired), |
| 46 |
(Short: #0; Long: 'function-name'; Argument: oaRequired), |
| 47 |
(Short: #0; Long: 'unit-name'; Argument: oaRequired), |
| 48 |
(Short: #0; Long: 'debug-log'; Argument: oaNone), |
| 49 |
(Short: #0; Long: 'debug-font-image'; Argument: oaNone), |
| 50 |
(Short: #0; Long: 'only-sample-text'; Argument: oaNone) |
| 51 |
); |
| 52 |
|
| 53 |
procedure OptionProc(OptionNum: Integer; HasArgument: boolean; |
| 54 |
const Argument: string; const SeparateArgs: TSeparateArgs; Data: Pointer); |
| 55 |
begin |
| 56 |
case OptionNum of |
| 57 |
0: begin |
| 58 |
Writeln( |
| 59 |
'texture-font-to-pascal: convert font file (like TTF or OTF)' +NL+ |
| 60 |
'to a Pascal source file, based on types' +NL+ |
| 61 |
'in Castle Game Engine CastleTextureFontData unit.' +NL+ |
| 62 |
NL+ |
| 63 |
'Usage:' +NL+ |
| 64 |
' texture-font-to-pascal [options...] MyFontFile.ttf' +NL+ |
| 65 |
NL+ |
| 66 |
'Available options:' +NL+ |
| 67 |
OptionDescription('-h / --help', |
| 68 |
'Print this help message and exit.') + NL + |
| 69 |
OptionDescription('--size=FONT-SIZE', '') + NL + |
| 70 |
OptionDescription('--no-anti-alias', '') + NL + |
| 71 |
OptionDescription('--sample-text=TEXT', |
| 72 |
'Load (if existing in the font file) all the listed characters. You can use this parameter multiple times.') + NL + |
| 73 |
OptionDescription('--sample-text-file=TEXT-FILE-NAME', |
| 74 |
'Load (if existing in the font file) all the characters used in given text file. You can use this parameter multiple times.') + NL + |
| 75 |
OptionDescription('--sample-code=TEXT', |
| 76 |
'Load (if existing in the font file) the listed character code. You can use this parameter multiple times.') + NL + |
| 77 |
OptionDescription('--sample-get-text-mo=URL', |
| 78 |
'Load (if existing in the font file) all the character codes present in translated strings in URL. URL must point to a GetText MO file, it can be a regular filename as well. You can use this parameter multiple times.') + NL + |
| 79 |
OptionDescription('--only-sample-text', |
| 80 |
'Load only characters from --sample-text and --sample-code, do not add standard ASCII chars. By default we add standard ASCII chars, regardless of --sample-text and --sample-code.') + NL + |
| 81 |
OptionDescription('--function-name=PASCAL-FUNCTION-NAME', |
| 82 |
'Set function name to access the font. By default we automatically calculate it based on font name and size.') + NL + |
| 83 |
OptionDescription('--unit-name=PASCAL-UNIT-NAME', |
| 84 |
'Set generated unit name. This also determines the output filename. By default we automatically calculate it based on function name (which in turn is automatically calculated based on font name and size).') + NL + |
| 85 |
OptionDescription('--debug-log', |
| 86 |
'See the log, showing e.g. the font image size.') + NL + |
| 87 |
OptionDescription('--debug-font-image', |
| 88 |
'Write to disk font images as png.') + NL + |
| 89 |
NL+ |
| 90 |
ApplicationProperties.Description); |
| 91 |
Halt; |
| 92 |
end; |
| 93 |
1: begin |
| 94 |
// include ApplicationName in version, good for help2man |
| 95 |
Writeln(ApplicationName + ' ' + ApplicationProperties.Version); |
| 96 |
Halt; |
| 97 |
end; |
| 98 |
2: Size := StrToInt(Argument); |
| 99 |
3: AntiAliasing := false; |
| 100 |
4: Characters.Add(Argument); |
| 101 |
5: Characters.Add(FileToString(Argument)); |
| 102 |
6: Characters.Add(StrToInt(Argument)); |
| 103 |
7: AddTranslatedCharacters(Argument, Characters); |
| 104 |
8: ParamFunctionName := Argument; |
| 105 |
9: ParamUnitName := Argument; |
| 106 |
10: InitializeLog; |
| 107 |
11: DebugFontImage := true; |
| 108 |
12: OnlySampleText := true; |
| 109 |
else raise EInternalError.Create('OptionProc'); |
| 110 |
end; |
| 111 |
end; |
| 112 |
|
| 113 |
var |
| 114 |
Font: TTextureFontData; |
| 115 |
PrecedingComment, UnitName, FontFunctionName, OutURL, FontURL, FontName: string; |
| 116 |
begin |
| 117 |
ApplicationProperties.ApplicationName := 'texture-font-to-pascal'; |
| 118 |
ApplicationProperties.Version := '1.0'; |
| 119 |
ApplicationProperties.OnWarning.Add(@ApplicationProperties.WriteWarningOnConsole); |
| 120 |
|
| 121 |
Characters := TUnicodeCharList.Create; |
| 122 |
try |
| 123 |
Parameters.Parse(Options, @OptionProc, nil); |
| 124 |
Parameters.CheckHigh(1); |
| 125 |
FontURL := Parameters[1]; |
| 126 |
if not OnlySampleText then |
| 127 |
Characters.Add(SimpleAsciiCharacters); |
| 128 |
if Characters.Count = 0 then |
| 129 |
raise EInvalidParams.Create('No font characters requested to be loaded'); |
| 130 |
|
| 131 |
FontName := DeleteURIExt(ExtractURIName(FontURL)); |
| 132 |
if ParamFunctionName <> '' then |
| 133 |
FontFunctionName := ParamFunctionName |
| 134 |
else |
| 135 |
FontFunctionName := 'TextureFont_' + |
| 136 |
SDeleteChars(FontName, AllChars - ['a'..'z', 'A'..'Z', '0'..'9']) + |
| 137 |
'_' + IntToStr(Size); |
| 138 |
|
| 139 |
if ParamUnitName <> '' then |
| 140 |
UnitName := ParamUnitName |
| 141 |
else |
| 142 |
UnitName := 'Castle' + FontFunctionName; |
| 143 |
|
| 144 |
PrecedingComment := Format( |
| 145 |
' Source font:' +NL+ |
| 146 |
' Name : %s' +NL+ |
| 147 |
' Size : %d' +NL+ |
| 148 |
' AntiAliasing : %s' +nl, |
| 149 |
[ FontName, Size, BoolToStr(AntiAliasing, true) ]); |
| 150 |
|
| 151 |
Font := TTextureFontData.Create(FontURL, Size, AntiAliasing, Characters); |
| 152 |
try |
| 153 |
OutURL := LowerCase(UnitName) + '.pas'; |
| 154 |
Font2Pascal(Font, UnitName, PrecedingComment, FontFunctionName, OutURL); |
| 155 |
Writeln('texture-font-to-pascal: "' + OutURL + '" generated, texture size ', |
| 156 |
Font.Image.Width, ' x ', |
| 157 |
Font.Image.Height); |
| 158 |
if DebugFontImage then |
| 159 |
begin |
| 160 |
OutURL := LowerCase(UnitName) + '.png'; |
| 161 |
SaveImage(Font.Image, OutURL); |
| 162 |
Writeln('texture-font-to-pascal: font image "' + OutURL + '" written'); |
| 163 |
end; |
| 164 |
finally FreeAndNil(Font) end; |
| 165 |
finally FreeAndNil(Characters) end; |
| 166 |
end. |