display_api.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. -- Integration with display API
  16. if minetest.get_modpath("display_api") then
  17. --- Standard on_display_update entity callback.
  18. -- Node should have properly configured display_entity.
  19. -- @param pos Node position
  20. -- @param objref Object reference of entity
  21. font_api.on_display_update = function (pos, objref)
  22. local meta = minetest.get_meta(pos)
  23. local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
  24. local entity = objref:get_luaentity()
  25. if not entity or not ndef.display_entities[entity.name] then
  26. return
  27. end
  28. local def = ndef.display_entities[entity.name]
  29. local font = font_api.get_font(meta:get_string("font") ~= ""
  30. and meta:get_string("font") or def.font_name)
  31. local text = meta:get_string(def.meta_text or "display_text")
  32. -- Compute entity resolution accroding to given attributes
  33. local texturew, textureh
  34. textureh = font:get_height(def.lines or def.maxlines or 1)
  35. if def.columns then
  36. if font.fixedwidth then
  37. texturew = def.columns * font.fixedwidth
  38. if def.aspect_ratio then
  39. minetest.log('warning', "[font_api] 'aspect_ratio' ignored because 'columns' is specified")
  40. end
  41. else
  42. minetest.log('warning', "[font_api] 'columns' ignored because '"..font.name.."' is not a fixed width font.")
  43. end
  44. end
  45. if not texturew then
  46. if not def.aspect_ratio then
  47. minetest.log('warning', "[font_api] No 'aspect_ratio' specified, using default 1.")
  48. end
  49. texturew = textureh * def.size.x / def.size.y / (def.aspect_ratio or 1)
  50. end
  51. objref:set_properties({
  52. textures={ font:render(text, texturew, textureh, {
  53. lines = def.maxlines or def.lines,
  54. halign = def.halign,
  55. valign = def.valign,
  56. color = def.color} ) },
  57. visual_size = def.size,
  58. })
  59. end
  60. else
  61. font_api.on_display_update = function (pos, objref)
  62. minetest.log('error', '[font_api] font_api.on_display_update called but display_api mod not enabled.')
  63. end
  64. end