importing_fonts.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. .. _doc_importing_fonts:
  2. Importing fonts
  3. ===============
  4. What is a font?
  5. ---------------
  6. Fonts in modern operating systems are created as scalable vector
  7. graphics. They are stored as a collection of curves (usually one for
  8. each character), which are independent of the screen resolution, and
  9. stored in standardized file formats, such as TTF (TrueType) or OTF
  10. (OpenType).
  11. Rendering such fonts to bitmaps is a complex process, which employs
  12. different methods to convert curves to pixels depending on context and
  13. target size. Due to this, this rendering process must be done by using
  14. the CPU. Game engines use the GPU to render, and 3D APIs don't really
  15. support the means to do this efficiently, so fonts have to be converted
  16. to a format that is friendly to the GPU when imported to a project.
  17. Converting fonts
  18. ----------------
  19. This conversion process consists of rendering a vector font to a given
  20. point size and storing all the resulting characters in a bitmap texture.
  21. The bitmap texture is then used by the GPU to draw a small quad for each
  22. character and form readable strings.
  23. .. image:: /img/bitmapfont.png
  24. The drawback of this process is that fonts must be pre-imported in the
  25. specific sizes that they will use in the project. However, given that
  26. that bitmap fonts compress really well, this is not as bad as it sounds.
  27. Importing a font
  28. ----------------
  29. Fonts are imported via the Font import dialog. The dialog will ask for a
  30. font, a size, some options and a target resource fie to save.
  31. .. image:: /img/fontimport.png
  32. The dialog is fully dynamic, which means that any change will be
  33. reflected in the font preview window. The user ccan tweak almost every
  34. parameter and get instant feedback on how the font will look.
  35. Since the resulting font is a bitmap, a few more options were added to
  36. make the imported font look even nicer. These options were added to
  37. please graphic designers, who love putting gradients, outlines and
  38. shadows in fonts, as well as changing all the inter-spaces available :).
  39. The options which will be explained in the next section.
  40. Extra spacing
  41. ~~~~~~~~~~~~~
  42. It is possible to add more space for:
  43. - **Characters**, the space between them can be varied.
  44. - **"space" character**, so the distance between words is bigger.
  45. - **Top and Bottom margins**, this changes the spacing between lines as
  46. well as the space between the top and bottom lines and the borders.
  47. .. image:: /img/fontspacing.png
  48. Shadows & outline
  49. ~~~~~~~~~~~~~~~~~
  50. Fonts can be added a shadow. For this, the font is drawn again below on
  51. a different color and the blurred with a gaussian kernel of different
  52. sizes. The resulting shadow can be adjusted with an exponential function
  53. to make it softer or more like an outline. A second shadow is also
  54. provided to create some added effects, like a bump or outline+shadow.
  55. .. image:: /img/shadowoutline.png
  56. Gradients
  57. ~~~~~~~~~
  58. Gradients are also another of the visual effects that graphic designers
  59. often use. To show how much we love them, we added those too. Gradients
  60. can be provided as a simple curve between two colors, or a special png
  61. file with a hand drawn gradient.
  62. .. image:: /img/fontgradients.png
  63. Internationalization
  64. --------------------
  65. Colors, shadows and gradients are beautiful, but it's time we get to
  66. serious business. Developing games for Asian markets is a common
  67. practice in today's globalized world and app stores.
  68. Here's when things get tricky with using bitmap fonts. Asian alphabets
  69. (Chinese, Japanese and Korean) contains dozens of thousands of
  70. characters. Generating bitmap fonts with every single of them is pretty
  71. expensive, as the resulting textures are huge. If the font size is small
  72. enough, it can be done without much trouble, but when the fonts become
  73. bigger, we run out of video ram pretty quickly!
  74. To solve this, Godot allows the user to specify a text file (in UTF-8
  75. format) where it expects to find all the characters that will be used in
  76. the project. This seems difficult to provide at first, and more to keep
  77. up to date, but it becomes rather easy when one realizes that the .csv
  78. with the translations can be used as such source file (see the
  79. :ref:`doc_importing_translations` section). As Godot re-imports assets when
  80. their dependencies change, both the translation and font files will be
  81. updated and re-imported automatically if the translation csv changes.
  82. Another cool trick for using a text file as limit of which characters
  83. can be imported is when using really large fonts. For example, the user
  84. might want to use a super large font, but only to show numbers. For
  85. this, he or she writes a numbers.txt file that contains "1234567890",
  86. and Godot will only limit itself to import data, thus saving a lot of
  87. video memory.