You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -5121,6 +5120,27 @@ Fonts added to your project are automatically converted into a texture format th
5121
5120
- Bitmap
5122
5121
- Distance field
5123
5122
5123
+
## Offline or Runtime fonts
5124
+
5125
+
By default, the conversion to rasterized glyph images happens at build time (offline). This has the drawback that each font needs to rasterize all possible glyphs in the build stage, producing potentially very large textures that consume memory and also increase the bundle size.
5126
+
5127
+
By using "runtime fonts", the .ttf fonts will be bundled as-is, and the rasterization will happen on-demand at runtime. This minimizes both runtime memory usage and the bundle size.
5128
+
5129
+
## Text layout support (e.g. Right-to-left)
5130
+
5131
+
The runtime fonts also have the benefit of supporting full text layout, e.g. right-to-left.
5132
+
We currently use the libraries [HarfBuzz](https://github.com/harfbuzz/harfbuzz), [SheenBidi](https://github.com/Tehreer/SheenBidi), [libunibreak](https://github.com/adah1972/libunibreak) and [SkriBidi](https://github.com/memononen/Skribidi).
5133
+
5134
+
See [Enabling Runtime Fonts](#manuals:font#enabling-runtime-fonts)
5135
+
5136
+
## Font collection
5137
+
5138
+
The `.fontc` file format is also known as a font collection. In offline mode, only one font is associated with it.
5139
+
When using runtime fonts, you can associate more than one font file (.ttf) with the font collection.
5140
+
5141
+
This allows for using the a font collection when rendering multiple texts in different languages, while also keeping the memory footprint low.
5142
+
E.g. loading a collection with the Japanese font, then associate that font with the current main font, followed by unloading the Japanese font collection.
5143
+
5124
5144
## Creating a font
5125
5145
5126
5146
To create a font for use in Defold, create a new Font file by selecting <kbd>File ▸ New...</kbd> from the menu, then select <kbd>Font</kbd>. You can also <kbd>right click</kbd> a location in the *Assets* browser and select <kbd>New... ▸ Font</kbd>.
@@ -5186,6 +5206,8 @@ Shadow support is enabled by the built-in font material shaders and handles both
5186
5206
*Characters*
5187
5207
: Which characters to include in the font. By default this field include the ASCII printable characters (character codes 32-126). You can add or remove characters from this field to include more or less characters in the font..
5188
5208
5209
+
For runtime fonts, this text acts as a cache prewarming with the correct glyphs. This happens during load time. See `font.prewarm_text()`.
5210
+
5189
5211
<div class='sidenote' markdown='1'>
5190
5212
The ASCII printable characters are:
5191
5213
space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ \` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
@@ -5268,23 +5290,29 @@ For example - to generate a gradient in a shader fragment, simply write:
5268
5290
5269
5291
For more information about shader uniforms, see the [Shader manual](#manuals:shader).
5270
5292
5271
-
## Runtime generation
5293
+
## Enabling Runtime Fonts
5272
5294
5273
5295
It is possible to use runtime generation for SDF type fonts, when using TrueType (.ttf) fonts.
5274
5296
This approach can greatly reduce the download size and runtime memory consumption of a Defold game.
5275
-
The small downside is a very small delay for each glyph generated at runtime.
5297
+
The small downside is the asynchronous nature of generating each glyph.
5276
5298
5277
-
Enable the feature by setting `font.runtime_generation` in game.project.
5299
+
* Enable the feature by setting `font.runtime_generation` in game.project.
5300
+
5301
+
* Add an [App Manifest](#manuals:app-manifest) and enable the `Use full text layout system` option.
5302
+
This builds a custom engine that has this feature enabled.
5278
5303
5279
5304
<div class='sidenote' markdown='1'>
5280
5305
This feature is currently experimental, but with the intention to be used as the default workflow in the future.
5281
5306
</div>
5282
5307
5283
5308
<div class='important' markdown='1'>
5284
-
This setting affects all .ttf fonts in the project.
5309
+
The `font.runtime_generation` setting affects all .ttf fonts in the project.
5285
5310
</div>
5286
5311
5287
-
### Prewarming glyph cache
5312
+
5313
+
### Font Scripting
5314
+
5315
+
#### Prewarming glyph cache
5288
5316
5289
5317
In order to make the runtime fonts easier to use, they support prewarming of the glyph cache.
5290
5318
This means the font will generate the glyphs listed in *Characters* in the font.
@@ -5293,54 +5321,54 @@ This means the font will generate the glyphs listed in *Characters* in the font.
5293
5321
If `All Chars` is selected, there will be no prewarming as it defeats the purpose of not having to generate all glyphs at the same time.
5294
5322
</div>
5295
5323
5296
-
### Font Scripting
5324
+
If the `Characters` field of the `.fontc` file is set, this is used as a text, to figure out which glyphs needs to be updated in the glyph cache.
5297
5325
5298
-
For runtime fonts, it's possible to add or removed sub fonts.
5326
+
It is also possible to manually update the glyph cache by calling `font.prewarm_text(font_collection, text, callback)`. It provides a callback to let you know when all the missing glyphs have been added to the glyph cache, and it's safe to present the text on screen.
5327
+
5328
+
### Adding/removing fonts to a font collection
5329
+
5330
+
For runtime fonts, it's possible to add or remove fonts (.ttf) to a font collection.
5299
5331
This is useful when a large font has been split up into multiple files for different character sets (e.g. CJK)
5300
5332
5301
5333
<div class='important' markdown='1'>
5302
-
Adding a subfont doesn't automatically load or render all the glyphs.
5334
+
Adding a font to a font collection doesn't automatically load or render all the glyphs.
5303
5335
</div>
5304
5336
5305
5337
```lua
5306
-
-- Add the range A-Z to the .fontc
5307
-
local font_hash = hash("/assets/fonts/roboto.fontc")
5308
-
local ttf_hash = hash("/assets/fonts/Roboto/Roboto-Bold.ttf")
To load the glyphs to the font, you will need to call the `font.add_glyphs()`.
5322
-
It is an asynchronous operation, and once it's done, it's safe to progress to show any message containing the glyphs.
5354
+
### Prewarming glyphs
5323
5355
5324
-
```lua
5325
-
local function add_glyph_callback(self, id, result, errmsg)
5326
-
if not result then
5327
-
print("Request " .. id .." finished with error:", errmsg)
5328
-
else
5329
-
msg.post(some_url, "show_dialog")
5330
-
end
5331
-
end
5356
+
To properly show a text with a runtime font, the glyphs need to be resolved. The `font.prewarm_text()` does this for you.
5357
+
It is an asynchronous operation, and once it's done and you get the callback, it's safe to progress to show any message containing the glyphs.
5332
5358
5333
-
-- Load glyphs into the font
5334
-
local font_hash = hash("/assets/fonts/roboto.fontc")
5335
-
local glyphs = "Some text to be shown!" -- for optimal performance, make this a list of unique glyphs
5336
-
local request_id = font.add_glyphs(font_hash, ttf_hash, add_glyph_callback)
5337
-
```
5359
+
<div class='important' markdown='1'>
5360
+
If the glyph cache gets full, the oldest glyph in the cache will be evicted.
5361
+
</div>
5338
5362
5339
-
And, once the characters aren't needed anymore, you can discard that memory:
5340
5363
```lua
5341
-
-- Remove the associated ttf resource
5342
-
local font_hash = hash("/assets/fonts/roboto.fontc")
5343
-
font.remove_glyphs(font_hash, "All the characters in the set")
5364
+
font.prewarm_text(self.font_collection, info.text, function (self, request_id, result, err)
5365
+
if result then
5366
+
print("PREWARMING OK!")
5367
+
label.set_text(self.label, info.text)
5368
+
else
5369
+
print("Error prewarming text:", err)
5370
+
end
5371
+
end)
5344
5372
```
5345
5373
5346
5374
<!-- /manuals/resource -->
@@ -7668,7 +7696,12 @@ Apart from the properties *Id*, *Position* and *Rotation* the following componen
7668
7696
: This property should refer to the glTF *.gltf* or Collada *.dae* file that contains the mesh to use. If the file contains multiple meshes, only the first one is read.
7669
7697
7670
7698
*Material*
7671
-
: Set this property to a material you have created that is suitable for a textured 3D object. There is a built-in *model.material* file that you can use as a starting point.
7699
+
: Set this property to a material you have created that is suitable for a textured 3D object. There are a number of built-in materials that you can use as a starting point:
7700
+
7701
+
* Use *model.material* for static non-instanced models
7702
+
* Use *model_instances.material* for static instanced models
7703
+
* Use *model_skinned.material* for skinned (animated) non-instanced models
7704
+
* Use *model_skinned_instances.material* for skinned (animated) instanced models
7672
7705
7673
7706
*Texture*
7674
7707
: This property should point to the texture image file that you want applied to the object.
@@ -7734,7 +7767,14 @@ A model also has a number of different properties that can be manipulated using
7734
7767
7735
7768
3D software commonly allows you to set properties on your object vertices, like coloring and texturing. This information goes into the glTF *.gltf* or Collada *.dae* file that you export from your 3D software. Depending on the requirements of your game you will have to select and/or create appropriate and _performant_ materials for your objects. A material combines _shader programs_ with a set of parameters for rendering of the object.
7736
7769
7737
-
There is a simple 3D model material available in the built-in materials folder. If you need to create custom materials for your models, see the [Material documentation](#manuals:material) for information. The [Shader manual](#manuals:shader) contains information on how shader programs work.
7770
+
There are a number of built-in materials that you can use as a starting point:
7771
+
7772
+
* Use *model.material* for static non-instanced models
7773
+
* Use *model_instances.material* for static instanced models
7774
+
* Use *model_skinned.material* for skinned (animated) non-instanced models
7775
+
* Use *model_skinned_instances.material* for skinned (animated) instanced models
7776
+
7777
+
If you need to create custom materials for your models, see the [Material documentation](#manuals:material) for information. The [Shader manual](#manuals:shader) contains information on how shader programs work.
Copy file name to clipboardExpand all lines: manuals/font.md
+73-40Lines changed: 73 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,19 @@ layout: manual
6
6
title: Fonts in Defold manual
7
7
toc:
8
8
- Font files
9
+
- Offline or Runtime fonts
10
+
- Text layout support (e.g. Right-to-left)
11
+
- Font collection
9
12
- Creating a font
10
13
- Properties
11
14
- Distance field fonts
12
15
- Bitmap BMFonts
13
16
- Artifacts and best practices
14
17
- Font Cache
15
-
- Runtime generation
16
-
- Prewarming glyph cache
18
+
- Enabling Runtime Fonts
17
19
- Font Scripting
20
+
- Adding/removing fonts to a font collection
21
+
- Prewarming glyphs
18
22
---
19
23
20
24
# Font files
@@ -30,6 +34,27 @@ Fonts added to your project are automatically converted into a texture format th
30
34
- Bitmap
31
35
- Distance field
32
36
37
+
## Offline or Runtime fonts
38
+
39
+
By default, the conversion to rasterized glyph images happens at build time (offline). This has the drawback that each font needs to rasterize all possible glyphs in the build stage, producing potentially very large textures that consume memory and also increase the bundle size.
40
+
41
+
By using "runtime fonts", the .ttf fonts will be bundled as-is, and the rasterization will happen on-demand at runtime. This minimizes both runtime memory usage and the bundle size.
42
+
43
+
## Text layout support (e.g. Right-to-left)
44
+
45
+
The runtime fonts also have the benefit of supporting full text layout, e.g. right-to-left.
46
+
We currently use the libraries [HarfBuzz](https://github.com/harfbuzz/harfbuzz), [SheenBidi](https://github.com/Tehreer/SheenBidi), [libunibreak](https://github.com/adah1972/libunibreak) and [SkriBidi](https://github.com/memononen/Skribidi).
47
+
48
+
See [Enabling Runtime Fonts](/manuals/font#enabling-runtime-fonts)
49
+
50
+
## Font collection
51
+
52
+
The `.fontc` file format is also known as a font collection. In offline mode, only one font is associated with it.
53
+
When using runtime fonts, you can associate more than one font file (.ttf) with the font collection.
54
+
55
+
This allows for using the a font collection when rendering multiple texts in different languages, while also keeping the memory footprint low.
56
+
E.g. loading a collection with the Japanese font, then associate that font with the current main font, followed by unloading the Japanese font collection.
57
+
33
58
## Creating a font
34
59
35
60
To create a font for use in Defold, create a new Font file by selecting <kbd>File ▸ New...</kbd> from the menu, then select <kbd>Font</kbd>. You can also <kbd>right click</kbd> a location in the *Assets* browser and select <kbd>New... ▸ Font</kbd>.
@@ -95,6 +120,8 @@ Shadow support is enabled by the built-in font material shaders and handles both
95
120
*Characters*
96
121
: Which characters to include in the font. By default this field include the ASCII printable characters (character codes 32-126). You can add or remove characters from this field to include more or less characters in the font..
97
122
123
+
For runtime fonts, this text acts as a cache prewarming with the correct glyphs. This happens during load time. See `font.prewarm_text()`.
124
+
98
125
<divclass='sidenote'markdown='1'>
99
126
The ASCII printable characters are:
100
127
space ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ \` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
@@ -177,23 +204,29 @@ For example - to generate a gradient in a shader fragment, simply write:
177
204
178
205
For more information about shader uniforms, see the [Shader manual](/manuals/shader).
179
206
180
-
## Runtime generation
207
+
## Enabling Runtime Fonts
181
208
182
209
It is possible to use runtime generation for SDF type fonts, when using TrueType (.ttf) fonts.
183
210
This approach can greatly reduce the download size and runtime memory consumption of a Defold game.
184
-
The small downside is a very small delay for each glyph generated at runtime.
211
+
The small downside is the asynchronous nature of generating each glyph.
212
+
213
+
* Enable the feature by setting `font.runtime_generation` in game.project.
185
214
186
-
Enable the feature by setting `font.runtime_generation` in game.project.
215
+
* Add an [App Manifest](/manuals/app-manifest) and enable the `Use full text layout system` option.
216
+
This builds a custom engine that has this feature enabled.
187
217
188
218
<divclass='sidenote'markdown='1'>
189
219
This feature is currently experimental, but with the intention to be used as the default workflow in the future.
190
220
</div>
191
221
192
222
<divclass='important'markdown='1'>
193
-
This setting affects all .ttf fonts in the project.
223
+
The `font.runtime_generation` setting affects all .ttf fonts in the project.
194
224
</div>
195
225
196
-
### Prewarming glyph cache
226
+
227
+
### Font Scripting
228
+
229
+
#### Prewarming glyph cache
197
230
198
231
In order to make the runtime fonts easier to use, they support prewarming of the glyph cache.
199
232
This means the font will generate the glyphs listed in *Characters* in the font.
@@ -202,52 +235,52 @@ This means the font will generate the glyphs listed in *Characters* in the font.
202
235
If `All Chars` is selected, there will be no prewarming as it defeats the purpose of not having to generate all glyphs at the same time.
203
236
</div>
204
237
205
-
### Font Scripting
238
+
If the `Characters` field of the `.fontc` file is set, this is used as a text, to figure out which glyphs needs to be updated in the glyph cache.
239
+
240
+
It is also possible to manually update the glyph cache by calling `font.prewarm_text(font_collection, text, callback)`. It provides a callback to let you know when all the missing glyphs have been added to the glyph cache, and it's safe to present the text on screen.
241
+
242
+
### Adding/removing fonts to a font collection
206
243
207
-
For runtime fonts, it's possible to add or removed sub fonts.
244
+
For runtime fonts, it's possible to add or remove fonts (.ttf) to a font collection.
208
245
This is useful when a large font has been split up into multiple files for different character sets (e.g. CJK)
209
246
210
247
<divclass='important'markdown='1'>
211
-
Adding a subfont doesn't automatically load or render all the glyphs.
248
+
Adding a font to a font collection doesn't automatically load or render all the glyphs.
0 commit comments