gui_using_fonts.rst 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. .. _doc_gui_using_fonts:
  2. Using Fonts
  3. ===========
  4. Godot allows you to set specific fonts for different UI nodes.
  5. There are three different places where you can setup font usage.
  6. The first is the theme editor. Choose the node you want to set the
  7. font for and select the font tab. The second is in the inspector
  8. for control nodes under **Theme Overrides > Fonts**. Lastly, in
  9. the inspector settings for themes under **Default Font**.
  10. If no font override is specified anywhere,
  11. `Open Sans <https://fonts.google.com/specimen/Open+Sans>`__ SemiBold
  12. is used as the default project font.
  13. .. note::
  14. Since Godot 4.0, font sizes are no longer defined in the font itself but are
  15. instead defined in the node that uses the font. This is done in the
  16. **Theme Overrides > Font Sizes** section of the inspector.
  17. This allows changing the font size without having to duplicate the font
  18. resource for every different font size.
  19. There are 2 kinds of font files: *dynamic* (TTF/OTF/WOFF/WOFF2 formats) and
  20. *bitmap* (BMFont ``.fnt`` format or monospaced image). Dynamic fonts are the
  21. most commonly used option, as they can be resized and still look crisp at higher
  22. sizes. Thanks to their vector-based nature, they can also contain a lot more
  23. glyphs while keeping a reasonable file size compared to bitmap fonts. Dynamic
  24. fonts also support some advanced features that bitmap fonts cannot support, such
  25. as *ligatures* (several characters transforming into a single different design).
  26. .. tip::
  27. You can find freely licensed font files on websites such as `Google Fonts
  28. <https://fonts.google.com/>`__ and `Font Library
  29. <https://fontlibrary.org/>`__.
  30. Fonts are covered by copyright. Double-check the license of a font before
  31. using it, as not all fonts allow commercial use without purchasing a license.
  32. .. seealso::
  33. You can see how fonts work in action using the
  34. `BiDI and Font Features demo project <https://github.com/godotengine/godot-demo-projects/tree/master/gui/bidi_and_font_features>`__.
  35. Dynamic fonts
  36. -------------
  37. Godot supports the following dynamic font formats:
  38. - TrueType Font or Collection (``.ttf``, ``.ttc``)
  39. - OpenType Font or Collection (``.otf``, ``.otc``)
  40. - Web Open Font Format 1 (``.woff``)
  41. - Web Open Font Format 2 (``.woff2``, since Godot 3.5)
  42. While ``.woff`` and especially ``.woff2`` tend to result in smaller file sizes,
  43. there is no universally "better" font format. In most situations, it's
  44. recommended to use the font format that was shipped on the font developer's
  45. website.
  46. Bitmap fonts
  47. ------------
  48. Godot supports the BMFont (``.fnt``) bitmap font format. This is a format created
  49. by the `BMFont <https://www.angelcode.com/products/bmfont/>`__ program. Many
  50. BMFont-compatible programs also exist, like `BMGlyph <https://www.bmglyph.com/>`__ or web-based `fontcutter <https://github.com/fabienbk/fontcutter>`__.
  51. Alternatively, you can import any image to be used as a bitmap font.
  52. To do so, select the image in the FileSystem dock, go to the
  53. Import dock, change its import type to **Font Data (Image Font)** then
  54. click **Reimport**:
  55. .. figure:: img/using_fonts_bitmap_font_from_image_import_options.webp
  56. :align: center
  57. :alt: Changing import type to Font Data (Image Font)
  58. Changing import type to **Font Data (Image Font)**
  59. The font's character set layout can be in any order, but orders that match
  60. standard Unicode are recommended as they'll require far less configuration to
  61. import. For example, the bitmap font below contains
  62. `ASCII <https://en.wikipedia.org/wiki/ASCII>`__ characters and follows standard ASCII ordering:
  63. .. figure:: img/using_fonts_monospace_bitmap_font_example.webp
  64. :align: center
  65. :alt: Bitmap font example
  66. Credit: `LibreQuake <https://github.com/MissLav/LibreQuake/blob/master/lq1/gfx-wad/CONCHARS.png>`__
  67. (scaled and cropped to exclude extended range)
  68. The following import options can be used to import the above font image
  69. successfully:
  70. .. figure:: img/using_fonts_bitmap_font_from_image_example_configuration.webp
  71. :align: center
  72. :alt: Import options to use for the above example font
  73. Import options to use for the above example font
  74. The **Character Ranges** option is an array that maps each position on the image
  75. (in tile coordinates, not pixels). The font atlas is traversed from left to
  76. right and top to bottom. Characters can be specified with decimal numbers
  77. (``127``), hexadecimal numbers (``0x007f``) or between *single* quotes
  78. (``'~'``). Ranges can be specified with a hyphen between characters.
  79. For instance, ``0-127`` (or ``0x0000-0x007f``) denotes the full ASCII range.
  80. As another example, ``' '-'~'`` is equivalent to ``32-127`` and denotes the range
  81. of *printable* (visible) ASCII characters.
  82. Make sure the **Character Ranges** option doesn't exceed the number of
  83. **Columns** × **Rows** defined. Otherwise, the font will fail to import.
  84. If your font image contains margins not used for font glyphs (such as
  85. attribution information), try adjusting **Image Margin**. This is a margin
  86. applied only once around the whole image.
  87. If your font image contains guides (in the form of lines between glyphs) or
  88. if spacing between characters appears incorrect, try adjusting **Character
  89. Margin**. This margin is applied for every imported glyph.
  90. If you need finer control over character spacing than
  91. what the **Character Margin** options provide, you have more options.
  92. For one, **Character Ranges** supports 3 additional arguments after the
  93. specified range of characters.
  94. These additional arguments control their positioning and spacing.
  95. They represent space advance, X axis offset, and Y axis offset in that order.
  96. They will change the space advance and offset of each character
  97. by the amount of pixels written. Space advance is most useful if, for example,
  98. your lowercase letters are thinner than your uppercase letters.
  99. .. figure:: img/using_fonts_bitmap_font_advance_offsets_diagram.webp
  100. :align: center
  101. :alt: Diagram showing the advance and offset values being used in character ranges.
  102. Do note that the offsets can cause your text to be cropped off the edge of your label boundaries.
  103. Secondly, you can also set up **Kerning Pairs** for individual characters.
  104. Specify your kerning pair by typing two sets of characters separated by a space,
  105. then followed by another space, a number to specify how many extra/less pixels to
  106. space those two sets of characters when placed next to each other.
  107. .. figure:: img/using_fonts_bitmap_kerning_pairs_example.webp
  108. If needed, your kerning pair characters can be specified by Unicode character code
  109. by entering ``\uXXXX`` where XXXX is the hexadecimal value of the Unicode character.
  110. Loading a font file
  111. -------------------
  112. To load a font file (dynamic or bitmap), use the resource dropdown's
  113. **Quick Load** or **Load** option next to a font property, then navigate to the
  114. font file in question:
  115. .. figure:: img/using_fonts_load_font.webp
  116. :align: center
  117. Loading a font file
  118. You can also drag-and-drop a font file from the FileSystem dock to the inspector
  119. property that accepts a Font resource.
  120. .. warning::
  121. In Godot 4.0 and later, texture filter and repeat properties are defined in
  122. the location where the texture is used, rather than on the texture itself.
  123. This also applies to fonts (both dynamic fonts and bitmap fonts).
  124. Fonts that have a pixel art appearance should have bilinear filtering disabled
  125. by changing the **Rendering > Textures > Canvas Textures > Default Texture Filter**
  126. project setting to **Nearest**.
  127. The font size must also be an integer multiple of the design size (which
  128. varies on a per-font basis), and the Control node using the font must be
  129. scaled by an integer multiple as well. Otherwise, the font may look blurry.
  130. Font sizes in Godot are specified in pixels (px), not points (pt). Keep this
  131. in mind when comparing font sizes across different software.
  132. The texture filter mode can also be set on individual nodes that inherit from CanvasItem
  133. by setting :ref:`CanvasItem.texture_filter <class_CanvasItem_property_texture_filter>`.
  134. Font outlines and shadows
  135. -------------------------
  136. Font outlines and shadows can be used to improve readability when the background
  137. color isn't known in advance. For instance, this is the case for HUD elements
  138. that are drawn over a 2D/3D scene.
  139. Font outlines are available in most nodes that derive from Control, in addition
  140. to :ref:`class_Label3D`.
  141. To enable outline for a font on a given node, configure the theme overrides
  142. **Font Outline Color** and **Outline Size** in the inspector. The result should
  143. look like this:
  144. .. figure:: img/using_fonts_outline_example.webp
  145. :align: center
  146. :alt: Font outline example
  147. Font outline example
  148. .. note::
  149. If using a font with MSDF rendering, its **MSDF Pixel Range** import option
  150. be set to at least *twice* the value of the outline size for outline
  151. rendering to look correct. Otherwise, the outline may appear to be cut off
  152. earlier than intended.
  153. Support for font shadows is more limited: they are only available in
  154. :ref:`class_Label` and :ref:`class_RichTextLabel`. Additionally, font shadows
  155. always have a hard edge (but you can reduce their opacity to make them look more
  156. subtle). To enable font shadows on a given node, configure the **Font Shadow
  157. Color**, **Shadow Offset X**, and **Shadow Offset Y** theme overrides in a Label
  158. or RichTextLabel node accordingly:
  159. .. figure:: img/using_fonts_shadow.webp
  160. :align: center
  161. :alt: Configuring font shadow in a Label node
  162. Configuring font shadow in a Label node
  163. The result should look like this:
  164. .. figure:: img/using_fonts_shadow_example.webp
  165. :align: center
  166. :alt: Font shadow example
  167. Font shadow example
  168. .. tip::
  169. You can create local overrides to font display in Label nodes by creating a
  170. :ref:`class_LabelSettings` resource that you reuse across Label nodes. This
  171. resource takes priority over :ref:`theme properties <doc_gui_skinning>`.
  172. Advanced font features
  173. ----------------------
  174. .. _doc_using_fonts_antialiasing:
  175. Antialiasing
  176. ~~~~~~~~~~~~
  177. You can adjust how the font should be smoothed out when rendering by adjusting
  178. *antialiasing* and *hinting*. These are different properties, with different use
  179. cases.
  180. Antialiasing controls how glyph edges should be smoothed out when rasterizing
  181. the font. The default antialiasing method (**Grayscale**) works well on every
  182. display technology. However, at small sizes, grayscale antialiasing may result
  183. in fonts looking blurry.
  184. The antialiasing sharpness can be improved by using LCD subpixel optimization,
  185. which exploits the subpixel patterns of most LCD displays by offsetting the font
  186. antialiasing on a per-channel basis (red/green/blue). The downside is that this
  187. can introduce "fringing" on edges, especially on display technologies that don't
  188. use standard RGB subpixels (such as OLED displays).
  189. In most games, it's recommended to stick to the default **Grayscale**
  190. antialiasing. For non-game applications, LCD subpixel optimization is worth
  191. exploring.
  192. .. figure:: img/using_fonts_antialiasing_comparison.webp
  193. :align: center
  194. :alt: Font antialiasing comparison
  195. From top to bottom: Disabled, Grayscale, LCD Subpixel (RGB)
  196. .. note::
  197. Antialiasing cannot be changed on :ref:`MSDF-rendered fonts <doc_using_fonts_msdf>`
  198. – these are always rendered with grayscale antialiasing.
  199. .. _doc_using_fonts_hinting:
  200. Hinting
  201. ~~~~~~~
  202. Hinting controls how aggressively glyph edges should be snapped to pixels when
  203. rasterizing the font. **None** results in the smoothest appearance, which can
  204. make the font look blurry at small sizes. **Light** (default) is sharper by
  205. snapping glyph edges to pixels on the Y axis only, while **Full** is even sharper
  206. by snapping glyph edges to pixels on both X and Y axes. Depending on personal
  207. preference, you may prefer using one hinting mode over the other.
  208. .. figure:: img/using_fonts_hinting_comparison.webp
  209. :align: center
  210. :alt: Font hinting comparison
  211. From top to bottom: None, Light, Full hinting
  212. .. note::
  213. If changing the hinting mode has no visible effect after clicking
  214. **Reimport**, it's usually because the font doesn't include hinting
  215. instructions. This can be resolved by looking for a version of the font file
  216. that includes hinting instructions, or enabling **Force Autohinter** in the
  217. Import dock. This will use `FreeType <https://freetype.org/>`__'s autohinter
  218. to automatically add hinting instructions to the imported font.
  219. .. _doc_using_fonts_subpixel_positioning:
  220. Subpixel positioning
  221. ~~~~~~~~~~~~~~~~~~~~
  222. Subpixel positioning can be adjusted. This is a `FreeType <https://freetype.org/>`__
  223. feature that allows glyphs to be rendered more closely to their intended form.
  224. The default setting of **Auto** automatically enables subpixel positioning at
  225. small sizes, but disables it at large font sizes to improve rasterization
  226. performance.
  227. You can force the subpixel positioning mode to **Disabled**, **One half of a
  228. pixel** or **One quarter of a pixel**. **One quarter of a pixel** provides the
  229. best quality, at the cost of longer rasterization times.
  230. Changing antialiasing, hinting and subpixel positioning has the most visible
  231. effect at smaller font sizes.
  232. .. warning::
  233. Fonts that have a pixel art appearance should have their subpixel positioning
  234. mode set to **Disabled**. Otherwise, the font may appear to have uneven pixel
  235. sizes.
  236. This step is not required for bitmap fonts, as subpixel positioning is only
  237. relevant for dynamic fonts (which are usually made of vector elements).
  238. .. _doc_using_fonts_mipmaps:
  239. Mipmaps
  240. ~~~~~~~
  241. By default, fonts do not have mipmaps generated to reduce memory usage and speed
  242. up rasterization. However, this can cause downscaled fonts to become grainy. This
  243. can be especially noticeable with :ref:`doc_3d_text` that doesn't have **Fixed
  244. Size** enabled. This can also occur when displaying text with a traditional rasterized
  245. (non-:ref:`MSDF <doc_using_fonts_msdf>`) font in a Control node that has its scale
  246. lower than ``(1, 1)``.
  247. After selecting a font in the FileSystem dock, you can enable the **Mipmaps** in
  248. the Import dock to improve downscaled font rendering appearance.
  249. Mipmaps can be enabled on MSDF fonts as well. This can improve font rendering
  250. quality a little at smaller-than-default sizes, but MSDF fonts are already
  251. resistant to graininess out of the box.
  252. .. _doc_using_fonts_msdf:
  253. MSDF font rendering
  254. ~~~~~~~~~~~~~~~~~~~
  255. Multi-channel signed distance field (MSDF) font rendering allows rendering fonts
  256. at any size, without having to re-rasterize them when their size changes.
  257. MSDF font rendering has 2 upsides over traditional font rasterization, which
  258. Godot uses by default:
  259. - The font will always look crisp, even at huge sizes.
  260. - There is less stuttering when rendering characters *at large font sizes* for
  261. the first time, as there is no rasterization performed.
  262. The downsides of MSDF font rendering are:
  263. - Higher baseline cost for font rendering. This is usually not noticeable on
  264. desktop platforms, but it can have an impact on low-end mobile devices.
  265. - Fonts at small sizes will not look as clear as rasterized fonts, due to the
  266. lack of hinting.
  267. - Rendering new glyphs for the first time *at small font sizes* may be more
  268. expensive compared to traditional rasterized fonts.
  269. :ref:`doc_using_fonts_font_prerendering` can be used to alleviate this.
  270. - LCD subpixel optimization cannot be enabled for MSDF fonts.
  271. - Fonts with self-intersecting outlines will not render correctly in MSDF mode.
  272. If you notice rendering issues on fonts downloaded from websites such as
  273. `Google Fonts <https://fonts.google.com>`__, try downloading the font from the
  274. font author's official website instead.
  275. .. figure:: img/using_fonts_rasterized_vs_msdf_comparison.webp
  276. :align: center
  277. :alt: Comparison of font rasterization methods
  278. Comparison of font rasterization methods.
  279. From top to bottom: rasterized without oversampling, rasterized with oversampling, MSDF
  280. To enable MSDF rendering for a given font, select it in the FileSystem dock, go
  281. to the Import dock, enable **Multichannel Signed Distance Field**, then click
  282. **Reimport**:
  283. .. figure:: img/using_fonts_msdf_import_options.webp
  284. :align: center
  285. :alt: Enabling MSDF in the font's import options
  286. Enabling MSDF in the font's import options
  287. .. _doc_using_fonts_emoji:
  288. Using emoji
  289. ~~~~~~~~~~~
  290. Godot has limited support for emoji fonts:
  291. - CBDT/CBLC (embedded PNGs) and SVG emoji fonts are supported.
  292. - COLR/CPAL emoji fonts (custom vector format) are **not** supported.
  293. - EMJC bitmap image compression (used by iOS' system emoji font) is **not** supported.
  294. This means that to support emoji on iOS, you must use a custom font that
  295. uses SVG or PNG bitmap compression instead.
  296. For Godot to be able to display emoji, the font used (or one of its
  297. :ref:`fallbacks <doc_using_fonts_font_fallbacks>`) needs to include them.
  298. Otherwise, emoji won't be displayed and placeholder "tofu" characters will
  299. appear instead:
  300. .. figure:: img/using_fonts_emoji_placeholder_characters.webp
  301. :align: center
  302. :alt: Default appearance when trying to use emoji in a label
  303. Default appearance when trying to use emoji in a label
  304. After adding a font to display emoji such as
  305. `Noto Color Emoji <https://fonts.google.com/noto/specimen/Noto+Color+Emoji>`__,
  306. you get the expected result:
  307. .. figure:: img/using_fonts_emoji_correct_characters.webp
  308. :align: center
  309. :alt: Correct appearance after adding an emoji font to the label
  310. Correct appearance after adding an emoji font to the label
  311. To use a regular font alongside emoji, it's recommended to specify a
  312. :ref:`fallback font <doc_using_fonts_font_fallbacks>` that points to the
  313. emoji font in the regular font's advanced import options. If you wish to use
  314. the default project font while displaying emoji, leave the **Base Font**
  315. property in FontVariation empty while adding a font fallback pointing to the
  316. emoji font:
  317. .. tip::
  318. Emoji fonts are quite large in size, so you may want to :ref:`load a system
  319. font <doc_using_fonts_system_fonts>` to provide emoji glyphs rather than
  320. bundling it with your project. This allows providing full emoji support in
  321. your project without increasing the size of its exported PCK. The downside
  322. is that emoji will look different depending on the platform, and loading
  323. system fonts is not supported on all platforms.
  324. It's possible to use a system font as a fallback font too.
  325. Using icon fonts
  326. ~~~~~~~~~~~~~~~~
  327. Tools like `Fontello <https://fontello.com/>`__ can be used to generate font
  328. files containing vectors imported from SVG files. This can be used to render
  329. custom vector elements as part of your text, or to create extruded 3D icons
  330. with :ref:`doc_3d_text` and TextMesh.
  331. .. note::
  332. Fontello currently does not support creating multicolored fonts (which Godot
  333. can render). As of November 2022, support for multicolored fonts in icon
  334. font generation tools remains scarce.
  335. Depending on your use cases, this may lead to better results compared to using
  336. the ``img`` tag in :ref:`RichTextLabel <doc_bbcode_in_richtextlabel>`. Unlike
  337. bitmap images (including SVGs which are rasterized on import by Godot),
  338. true vector data can be resized to any size without losing quality.
  339. After downloading the generated font file, load it in your Godot project then
  340. specify it as a custom font for a Label, RichTextLabel or Label3D node. Switch
  341. over to the Fontello web interface, then copy the character by selecting it then
  342. pressing :kbd:`Ctrl + C` (:kbd:`Cmd + C` on macOS). Paste the character in the
  343. **Text** property of your Label node. The character will appear as a placeholder
  344. glyph in the inspector, but it should appear correctly in the 2D/3D viewport.
  345. To use an icon font alongside a traditional font in the same Control, you can
  346. specify the icon font as a :ref:`fallback <doc_using_fonts_font_fallbacks>`.
  347. This works because icon fonts use the Unicode *private use area*, which is
  348. reserved for use by custom fonts and doesn't contain standard glyphs by design.
  349. .. note::
  350. Several modern icon fonts such as `Font Awesome 6 <https://fontawesome.com/download>`__
  351. have a desktop variant that uses *ligatures* to specify icons. This allows you to
  352. specify icons by entering their name directly in the **Text** property of any
  353. node that can display fonts. Once the icon's name is fully entered as text
  354. (such as ``house``), it will be replaced by the icon.
  355. While easier to use, this approach cannot be used with font fallbacks as the main
  356. font's characters will take priority over the fallback font's ligatures.
  357. .. _doc_using_fonts_font_fallbacks:
  358. Font fallbacks
  359. ~~~~~~~~~~~~~~
  360. Godot supports defining one or more fallbacks when the main font lacks a glyph
  361. to be displayed. There are 2 main use cases for defining font fallbacks:
  362. - Use a font that only supports Latin character sets, but use another font to
  363. be able to display text another character set such as Cyrillic.
  364. - Use a font to render text, and another font to render emoji or icons.
  365. Open the Advanced Import Settings dialog by double-clicking the font file in the
  366. FileSystem dock. You can also select the font in the FileSystem dock, go to the
  367. Import dock then choose **Advanced…** at the bottom:
  368. .. figure:: img/using_fonts_advanced_import_settings.webp
  369. :align: center
  370. Import dock
  371. In the dialog that appears, look for **Fallbacks** section
  372. on the sidebar on the right, click the **Array[Font] (size 0)** text to expand
  373. the property, then click **Add Element**:
  374. .. figure:: img/using_fonts_font_fallbacks_add.webp
  375. :align: center
  376. Adding font fallback
  377. Click the dropdown arrow on the new element, then choose a font file using the
  378. **Quick Load** or **Load** options:
  379. .. figure:: img/using_fonts_font_fallbacks_load.webp
  380. :align: center
  381. Loading font fallback
  382. It is possible to add fallback fonts while using the default project font. To do
  383. so, leave the **Base Font** property empty while adding one or more font
  384. fallbacks.
  385. .. note::
  386. Font fallbacks can also be defined on a local basis similar to
  387. :ref:`doc_using_fonts_opentype_font_features`, but this is not covered here
  388. for brevity reasons.
  389. .. _doc_using_fonts_variable_fonts:
  390. Variable fonts
  391. ~~~~~~~~~~~~~~
  392. Godot has full support for `variable fonts <https://variablefonts.io/>`__, which
  393. allow you to use a single font file to represent various font weights and styles
  394. (regular, bold, italic, …). This must be supported by the font file you're using.
  395. To use a variable font, create a :ref:`class_FontVariation` resource in the
  396. location where you intend to use the font, then load a font file within the
  397. FontVariation resource:
  398. .. figure:: img/using_fonts_font_variation_create.webp
  399. :align: center
  400. Creating a FontVariation resource
  401. .. figure:: img/using_fonts_font_variation_load.webp
  402. :align: center
  403. Loading a font file into the FontVariation resource
  404. Scroll down to the FontVariation's **Variation** section, then click the
  405. **Variation Coordinates** text to expand the list of axes that can be adjusted:
  406. .. figure:: img/using_fonts_font_variation_variable_font.webp
  407. :align: center
  408. List of variation axes
  409. The set of axes you can adjust depends on the font loaded. Some variable fonts
  410. only support one axis of adjustment (typically *weight* or *slant*), while
  411. others may support multiple axes of adjustment.
  412. For example, here's the `Inter V <https://rsms.me/inter/>`__ font with a
  413. *weight* of ``900`` and a *slant* of ``-10``:
  414. .. figure:: img/using_fonts_font_variation_variable_font_example.webp
  415. :align: center
  416. Variable font example (Inter V)
  417. .. tip::
  418. While variable font axis names and scales aren't standardized,
  419. some common conventions are usually followed by font designers.
  420. The *weight* axis is standardized in OpenType to work as follows:
  421. +------------+--------------------------------+
  422. | Axis value | Effective font weight |
  423. +============+================================+
  424. | ``100`` | Thin (Hairline) |
  425. +------------+--------------------------------+
  426. | ``200`` | Extra Light (Ultra Light) |
  427. +------------+--------------------------------+
  428. | ``300`` | Light |
  429. +------------+--------------------------------+
  430. | ``400`` | **Regular (Normal)** |
  431. +------------+--------------------------------+
  432. | ``500`` | Medium |
  433. +------------+--------------------------------+
  434. | ``600`` | Semi-Bold (Demi-Bold) |
  435. +------------+--------------------------------+
  436. | ``700`` | **Bold** |
  437. +------------+--------------------------------+
  438. | ``800`` | Extra Bold (Ultra Bold) |
  439. +------------+--------------------------------+
  440. | ``900`` | Black (Heavy) |
  441. +------------+--------------------------------+
  442. | ``950`` | Extra Black (Ultra Black) |
  443. +------------+--------------------------------+
  444. You can save the FontVariation to a ``.tres`` resource file to reuse it in other
  445. places:
  446. .. figure:: img/using_fonts_font_variation_save_to_file.webp
  447. :align: center
  448. Saving FontVariation to an external resource file
  449. Faux bold and italic
  450. ~~~~~~~~~~~~~~~~~~~~
  451. When writing text in bold or italic, using font variants specifically designed
  452. for this looks better. Spacing between glyphs will be more consistent when using
  453. a bold font, and certain glyphs' shapes may change entirely in italic variants
  454. (compare "a" and *"a"*).
  455. However, real bold and italic fonts require shipping more font files, which
  456. increases distribution size. A single :ref:`variable font <doc_using_fonts_variable_fonts>`
  457. file can also be used, but this file will be larger than a single non-variable font.
  458. While file size is usually not an issue for desktop projects, it can be a concern
  459. for mobile/web projects that strive to keep distribution size as low as possible.
  460. To allow bold and italic fonts to be displayed without having to ship additional
  461. fonts (or use a variable font that is larger in size), Godot supports *faux*
  462. bold and italic.
  463. .. figure:: img/using_fonts_faux_bold_italic_vs_real_bold_italic.webp
  464. :align: center
  465. :alt: Faux bold/italic (top), real bold/italic (bottom). Normal font used: Open Sans SemiBold
  466. Faux bold/italic (top), real bold/italic (bottom). Normal font used: Open Sans SemiBold
  467. Faux bold and italic is automatically used in :ref:`class_RichTextLabel`'s bold
  468. and italic tags if no custom fonts are provided for bold and/or italic.
  469. To use faux bold, create a FontVariation resource in a property where a Font
  470. resource is expected. Set **Variation > Embolden** to a positive value to make a
  471. font bolder, or to a negative value to make it less bold. Recommended values are
  472. between ``0.5`` and ``1.2`` depending on the font.
  473. Faux italic is created by skewing the text, which is done by modifying the
  474. per-character transform. This is also provided in FontVariation using the
  475. **Variation > Transform** property. Setting the ``yx`` component of the
  476. character transform to a positive value will italicize the text. Recommended
  477. values are between ``0.2`` and ``0.4`` depending on the font.
  478. Adjusting font spacing
  479. ~~~~~~~~~~~~~~~~~~~~~~
  480. For stylistic purposes or for better readability, you may want to adjust how a
  481. font is presented in Godot.
  482. Create a FontVariation resource in a property where a Font resource is expected.
  483. There are 4 properties available in the **Variation > Extra Spacing** section,
  484. which accept positive and negative values:
  485. - **Glyph:** Additional spacing between every glyph.
  486. - **Space:** Additional spacing between words.
  487. - **Top:** Additional spacing above glyphs. This is used for multiline text,
  488. but also to calculate the minimum size of controls such as :ref:`class_Label`
  489. and :ref:`class_Button`.
  490. - **Bottom:** Additional spacing below glyphs. This is used for multiline text,
  491. but also to calculate the minimum size of controls such as :ref:`class_Label`
  492. and :ref:`class_Button`.
  493. The **Variation > Transform** property can also be adjusted to stretch
  494. characters horizontally or vertically. This is specifically done by adjusting
  495. the ``xx`` (horizontal scale) and ``yy`` (vertical scale) components. Remember
  496. to adjust glyph spacing to account for any changes, as glyph transform doesn't
  497. affect how much space each glyph takes in the text. Non-uniform scaling of this
  498. kind should be used sparingly, as fonts are generally not designed to be
  499. displayed with stretching.
  500. .. _doc_using_fonts_opentype_font_features:
  501. OpenType font features
  502. ~~~~~~~~~~~~~~~~~~~~~~
  503. Godot supports enabling OpenType font features, which are a standardized way to
  504. define alternate characters that can be toggled without having to swap font
  505. files entirely. Despite being named OpenType font features, these are also
  506. supported in TrueType (``.ttf``) and WOFF/WOFF2 font files.
  507. Support for OpenType features highly depends on the font used. Some fonts don't
  508. support any OpenType features, while other fonts can support dozens of
  509. toggleable features.
  510. There are 2 ways to use OpenType font features:
  511. **Globally on a font file**
  512. Open the Advanced Import Settings dialog by double-clicking the font file in the
  513. FileSystem dock. You can also select the font in the FileSystem dock, go to the
  514. Import dock then choose **Advanced…** at the bottom:
  515. .. figure:: img/using_fonts_advanced_import_settings.webp
  516. :align: center
  517. Import dock
  518. In the dialog that appears, look for the **Metadata Overrides > OpenType
  519. Features** section on the sidebar on the right, click the
  520. **Features (0 of N set)** text to expand the property, then click
  521. **Add Feature**:
  522. .. figure:: img/using_fonts_advanced_import_settings_opentype_features.webp
  523. :align: center
  524. OpenType feature overrides in Advanced Import Settings
  525. **In a specific font usage (FontVariation)**
  526. To use a font feature, create a FontVariation resource like you would do for a
  527. :ref:`variable font <doc_using_fonts_variable_fonts>`, then load a font file
  528. within the FontVariation resource:
  529. .. figure:: img/using_fonts_font_variation_create.webp
  530. :align: center
  531. Creating a FontVariation resource
  532. .. figure:: img/using_fonts_font_variation_load.webp
  533. :align: center
  534. Loading a font file into a FontVariation resource
  535. Scroll down to the FontVariation's **OpenType Features** section, click the
  536. **Features (0 of N set)** text to expand the property, then click **Add Feature**
  537. and select the desired feature in the dropdown:
  538. .. figure:: img/using_fonts_font_variation_opentype_features.webp
  539. :align: center
  540. Specifying OpenType features in a FontVariation resource
  541. For example, here's the `Inter <https://rsms.me/inter/>`__ font without the
  542. *Slashed Zero* feature (top), then with the *Slashed Zero* OpenType feature enabled
  543. (bottom):
  544. .. figure:: img/using_fonts_font_variation_slashed_zero.webp
  545. :align: center
  546. OpenType feature comparison (Inter)
  547. You can disable ligatures and/or kerning for a specific font by adding OpenType
  548. features, then unchecking them in the inspector:
  549. .. figure:: img/using_fonts_font_variation_disable_ligatures.webp
  550. :align: center
  551. Disabling ligatures and kerning for a font
  552. .. _doc_using_fonts_system_fonts:
  553. System fonts
  554. ~~~~~~~~~~~~
  555. .. warning::
  556. Loading system fonts is only supported on Windows, macOS, Linux, Android and iOS.
  557. However, loading system fonts on Android is unreliable as there is no
  558. official API for doing so. Godot has to rely on parsing system configuration
  559. files, which can be modified by third-party Android vendors. This may result
  560. in non-functional system font loading.
  561. System fonts are a different type of resource compared to imported fonts. They
  562. are never actually imported into the project, but are loaded at runtime. This
  563. has 2 benefits:
  564. - The fonts are not included within the exported PCK file, leading to a smaller
  565. file size for the exported project.
  566. - Since fonts are not included with the exported project, this avoids licensing
  567. issues that would occur if proprietary system fonts were distributed alongside
  568. the project.
  569. The engine automatically uses system fonts as fallback fonts, which makes it
  570. possible to display CJK characters and emoji without having to load a custom
  571. font. There are some restrictions that apply though, as mentioned in the
  572. :ref:`Using emoji <doc_using_fonts_emoji>` section.
  573. Create a :ref:`class_SystemFont` resource in the location where you desire to
  574. use the system font:
  575. .. figure:: img/using_fonts_system_font_create.webp
  576. :align: center
  577. Creating a SystemFont resource
  578. .. figure:: img/using_fonts_system_font_specify.webp
  579. :align: center
  580. Specifying a font name to use in a SystemFont resource
  581. You can either specify one or more font names explicitly (such as ``Arial``), or
  582. specify the name of a font *alias* that maps to a "standard" default font for
  583. the system:
  584. .. Android font information sourced from <https://android.googlesource.com/platform/frameworks/base/+/master/data/fonts/fonts.xml>
  585. +----------------+-----------------+----------------+-------------------------+-------------------------+
  586. | Font alias | Windows | macOS/iOS | Linux | Android |
  587. +================+=================+================+=========================+=========================+
  588. | ``sans-serif`` | Arial | Helvetica | *Handled by fontconfig* | Roboto / Noto Sans |
  589. +----------------+-----------------+----------------+-------------------------+-------------------------+
  590. | ``serif`` | Times New Roman | Times | *Handled by fontconfig* | Noto Serif |
  591. +----------------+-----------------+----------------+-------------------------+-------------------------+
  592. | ``monospace`` | Courier New | Courier | *Handled by fontconfig* | Droid Sans Mono |
  593. +----------------+-----------------+----------------+-------------------------+-------------------------+
  594. | ``cursive`` | Comic Sans MS | Apple Chancery | *Handled by fontconfig* | Dancing Script |
  595. +----------------+-----------------+----------------+-------------------------+-------------------------+
  596. | ``fantasy`` | Gabriola | Papyrus | *Handled by fontconfig* | Droid Sans Mono |
  597. +----------------+-----------------+----------------+-------------------------+-------------------------+
  598. On Android, Roboto is used for Latin/Cyrillic text and Noto Sans is used for
  599. other languages' glyphs such as CJK. On third-party Android distributions, the
  600. exact font selection may differ.
  601. If specifying more than one font, the first font that is found on the system
  602. will be used (from top to bottom). Font names and aliases are case-insensitive
  603. on all platforms.
  604. Like for font variations, you can save the SystemFont arrangement to a resource
  605. file to reuse it in other places.
  606. Remember that different system fonts have different metrics, which means that
  607. text that can fit within a rectangle on one platform may not be doing so on
  608. another platform. Always reserve some additional space during development so
  609. that labels can extend further if needed.
  610. .. note::
  611. Unlike Windows and macOS/iOS, the set of default fonts shipped on Linux
  612. depends on the distribution. This means that on different Linux
  613. distributions, different fonts may be displayed for a given system font name
  614. or alias.
  615. It is also possible to load fonts at runtime even if they aren't installed on the system.
  616. See :ref:`Runtime loading and saving <doc_runtime_file_loading_and_saving_fonts>`
  617. for details.
  618. .. _doc_using_fonts_font_prerendering:
  619. Font prerendering
  620. ~~~~~~~~~~~~~~~~~
  621. When using traditional rasterized fonts, Godot caches glyphs on a per-font and
  622. per-size basis. This reduces stuttering, but it can still occur the first time a
  623. glyph is displayed when running the project. This can be especially noticeable
  624. at higher font sizes or on mobile devices.
  625. When using MSDF fonts, they only need to be rasterized once to a special signed
  626. distance field texture. This means caching can be done purely on a per-font
  627. basis, without taking the font size into consideration. However, the initial
  628. rendering of MSDF fonts is slower compared to a traditional rasterized font at a
  629. medium size.
  630. To avoid stuttering issues related to font rendering, it is possible to
  631. *prerender* certain glyphs. This can be done for all glyphs you intend to use
  632. (for optimal results), or only for common glyphs that are most likely to appear
  633. during gameplay (to reduce file size). Glyphs that aren't pre-rendered will be
  634. rasterized on-the-fly as usual.
  635. .. note::
  636. In both cases (traditional and MSDF), font rasterization is done on the CPU.
  637. This means that the GPU performance doesn't affect how long it takes for fonts
  638. to be rasterized.
  639. Open the Advanced Import Settings dialog by double-clicking the font file in the
  640. FileSystem dock. You can also select the font in the FileSystem dock, go to the
  641. Import dock then choose **Advanced…** at the bottom:
  642. .. figure:: img/using_fonts_advanced_import_settings.webp
  643. :align: center
  644. Import dock
  645. Move to the **Pre-render Configurations** tab of the Advanced Import Settings dialog,
  646. then add a configuration by clicking the "plus" symbol:
  647. .. figure:: img/using_fonts_advanced_import_settings_prerender_new_configuration.webp
  648. :align: center
  649. :alt: Adding a new prerendering configuration in the Advanced Import Settings dialog
  650. Adding a new prerendering configuration in the Advanced Import Settings dialog
  651. After adding a configuration, make sure it is selected by clicking its name
  652. once. You can also rename the configuration by double-clicking it.
  653. There are 2 ways to add glyphs to be prerendered to a given configuration. It is
  654. possible to use both approaches in a cumulative manner:
  655. **Using text from translations**
  656. For most projects, this approach is the most convenient to use, as it
  657. automatically sources text from your language translations. The downside is that
  658. it can only be used if your project supports
  659. :ref:`internationalization <doc_internationalizing_games>`. Otherwise, stick to
  660. the "Using custom text" approach described below.
  661. After adding translations to the Project Settings, use the
  662. **Glyphs from the Translations** tab to check translations by double-clicking them,
  663. then click **Shape All Strings in the Translations and Add Glyphs** at the bottom:
  664. .. figure:: img/using_fonts_advanced_import_settings_prerender_translation.webp
  665. :align: center
  666. :alt: Enabling prerendering in the Advanced Import Settings dialog with the Glyphs from the Translations tab
  667. Enabling prerendering in the Advanced Import Settings dialog with the **Glyphs from the Translations** tab
  668. .. note::
  669. The list of prerendered glyphs is not automatically updated when
  670. translations are updated, so you need to repeat this process if your
  671. translations have changed significantly.
  672. **Using custom text**
  673. While it requires manually specifying text that will appear in the game, this is
  674. the most efficient approach for games which don't feature user text input. This
  675. approach is worth exploring for mobile games to reduce the file size of the
  676. distributed app.
  677. To use existing text as a baseline for prerendering, go to the **Glyphs from the
  678. Text** sub-tab of the Advanced Import Settings dialog, enter text in the window
  679. on the right, then click **Shape Text and Add Glyphs** at the bottom of the
  680. dialog:
  681. .. figure:: img/using_fonts_advanced_import_settings_prerender_text.webp
  682. :align: center
  683. :alt: Enabling prerendering in the Advanced Import Settings dialog, Glyphs from the Text tab
  684. Enabling prerendering in the Advanced Import Settings dialog with the **Glyphs from the Text** tab
  685. .. tip::
  686. If your project supports :ref:`internationalization <doc_internationalizing_games>`,
  687. you can paste the contents of your CSV or PO files in the above box to quickly
  688. prerender all possible characters that may be rendered during gameplay
  689. (excluding user-provided or non-translatable strings).
  690. **By enabling character sets**
  691. The second method requires less configuration and fewer updates if your game's
  692. text changes, and is more suited to text-heavy games or multiplayer games with
  693. chat. On the other hand, it may cause glyphs that never show up in the game to
  694. be prerendered, which is less efficient in terms of file size.
  695. To use existing text as a baseline for prerendering, go to the **Glyphs from the
  696. Character Map** sub-tab of the Advanced Import Settings dialog, then
  697. *double-click* character sets to be enabled on the right:
  698. .. figure:: img/using_fonts_advanced_import_settings_prerender_character_map.webp
  699. :align: center
  700. :alt: Enabling prerendering in the Advanced Import Settings dialog, Glyphs from the Character Map tab
  701. Enabling prerendering in the Advanced Import Settings dialog with the **Glyphs from the Character Map** tab
  702. To ensure full prerendering, the character sets you need to enable depend on
  703. which languages are supported in your game. For English, only **Basic Latin**
  704. needs to be enabled. Enabling **Latin-1 Supplement** as well allows fully
  705. covering many more languages, such as French, German and Spanish. For Russian,
  706. **Cyrillic** needs to be enabled, and so on.
  707. Default project font properties
  708. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  709. In the **GUI > Theme** section of the advanced Project Settings, you can choose
  710. how the default font should be rendered:
  711. - **Default Font Antialiasing:** Controls the
  712. :ref:`antialiasing <doc_using_fonts_antialiasing>` method used
  713. for the default project font.
  714. - **Default Font Hinting:** Controls the
  715. :ref:`hinting <doc_using_fonts_hinting>` method used for
  716. the default project font.
  717. - **Default Font Subpixel Positioning:** Controls the
  718. :ref:`subpixel positioning <doc_using_fonts_subpixel_positioning>`
  719. method for the default project font.
  720. - **Default Font Multichannel Signed Distance Field:** If ``true``, makes the
  721. default project font use :ref:`MSDF font rendering <doc_using_fonts_msdf>` instead
  722. of traditional rasterization.
  723. - **Default Font Generate Mipmaps:** If ``true``, enables
  724. :ref:`mipmap <doc_using_fonts_mipmaps>` generation and
  725. usage for the default project font.
  726. .. note::
  727. These project settings *only* affect the default project font (the one that
  728. is hardcoded in the engine binary).
  729. Custom fonts' properties are governed by their respective import options
  730. instead. You can use the **Import Defaults** section of the Project Settings
  731. dialog to override default import options for custom fonts.