registry.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. --[[
  2. font_api mod for Minetest - Library creating textures with fonts and text
  3. (c) Pierre-Yves Rollo
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. --]]
  15. -- Global variables
  16. -------------------
  17. font_api.registered_fonts = {}
  18. font_api.registered_fonts_number = 0
  19. -- Local variables
  20. ------------------
  21. local default_font = false
  22. -- Local functions
  23. ------------------
  24. -- Gets a default (settings or fist font)
  25. local function get_default_font()
  26. -- First call
  27. if default_font == false then
  28. default_font = nil
  29. -- First, try with settings
  30. local settings_font = minetest.settings:get("default_font")
  31. if settings_font ~= nil and settings_font ~= "" then
  32. default_font = font_api.registered_fonts[settings_font]
  33. if default_font == nil then
  34. minetest.log("warning", "Default font in settings (\""..
  35. settings_font.."\") is not registered.")
  36. end
  37. end
  38. -- If failed, choose first font without default = false
  39. if default_font == nil then
  40. for _, font in pairs(font_api.registered_fonts) do
  41. if font.default then
  42. default_font = font
  43. break
  44. end
  45. end
  46. end
  47. -- If failed, chose first font
  48. if default_font == nil then
  49. for _, font in pairs(font_api.registered_fonts) do
  50. default_font = font
  51. break
  52. end
  53. end
  54. -- Error, no font registered
  55. if default_font == nil then
  56. minetest.log("error",
  57. "No font registred, unable to choose a default font.")
  58. end
  59. end
  60. return default_font
  61. end
  62. --- Returns font object to be used according to font_name
  63. -- @param font_name: Name of the font
  64. -- @return Font object if font found (or default font)
  65. function font_api.get_font(font_name)
  66. local font = font_api.registered_fonts[font_name]
  67. if font == nil then
  68. local message
  69. if font_name == nil then
  70. message = "No font given"
  71. else
  72. message = "Font \""..font_name.."\" unregistered"
  73. end
  74. font = get_default_font()
  75. if font ~= nil then
  76. minetest.log("info", message..", using font \""..font.name.."\".")
  77. end
  78. end
  79. return font
  80. end
  81. -- API functions
  82. ----------------
  83. --- Returns de default font name
  84. -- @return Default font name
  85. function font_api.get_default_font_name()
  86. return get_default_font().name
  87. end
  88. --- Register a new font
  89. -- Textures corresponding to the font should be named after following patern :
  90. -- font_<name>_<code>.png
  91. -- <name> : name of the font
  92. -- <code> : 4 digit hexadecimal unicode of the char
  93. -- @param font_name Name of the font to register
  94. -- If registering different sizes of the same font, add size in the font name
  95. -- (e.g. times_10, times_12...).
  96. -- @param def font definition. A associative array with following keys :
  97. -- @key default True (by default) if this font may be used as default font
  98. -- @key height (mandatory) Height in pixels of all font textures
  99. -- @key widths (mandatory) Array of character widths in pixels, indexed by
  100. -- UTF codepoints
  101. -- @key margintop (optional) Margin (in texture pixels) added on top of each
  102. -- char texture.
  103. -- @key marginbottom (optional) dded at bottom of each char texture.
  104. -- @key linespacing (optional) Spacing (in texture pixels) between each lines.
  105. -- margintop, marginbottom and linespacing can be negative numbers (default 0)
  106. -- and are to be used to adjust various font styles to each other.
  107. -- TODO: Add something to remove common accent if not defined in font
  108. function font_api.register_font(font_name, font_def)
  109. if font_api.registered_fonts[font_name] ~= nil then
  110. minetest.log("error", "Font \""..font_name.."\" already registered.")
  111. return
  112. end
  113. local font = font_api.Font:new(font_def)
  114. if font == nil then
  115. minetest.log("error", "Unable to register font \""..font_name.."\".")
  116. return
  117. end
  118. font.name = font_name
  119. font_api.registered_fonts[font_name] = font
  120. font_api.registered_fonts_number = font_api.registered_fonts_number + 1
  121. -- Force to choose again default font
  122. -- (allows use of fonts registered after start)
  123. default_font = false
  124. minetest.log("action", "New font registered in font_api: "..font_name..".")
  125. end