display_modpack/font_lib/init.lua

341 lines
9.0 KiB
Lua
Raw Normal View History

--[[
font_lib mod for Minetest - Library to add font display capability
to display_lib mod.
(c) Pierre-Yves Rollo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
-- Global variables
2018-01-13 12:38:55 +01:00
-------------------
font_lib = {}
2018-01-13 12:38:55 +01:00
font_lib.name = minetest.get_current_modname()
font_lib.path = minetest.get_modpath(font_lib.name)
2017-12-19 21:52:49 +01:00
font_lib.registered_fonts = {}
2018-01-13 12:38:55 +01:00
-- Local variables
------------------
local default_font = false
-- Local functions
2017-12-19 21:52:49 +01:00
------------------
2017-12-19 21:52:49 +01:00
-- Split multiline text into array of lines, with <maxlines> maximum lines.
local function split_lines(text, maxlines)
local splits = text:split("\n")
if maxlines then
local lines = {}
for num = 1,maxlines do
lines[num] = splits[num]
end
return lines
else
return splits
end
end
2018-01-13 12:38:55 +01:00
-- Gets a default (settings or fist font)
2017-12-19 21:52:49 +01:00
2018-01-13 12:38:55 +01:00
local function get_default_font()
-- First call
if default_font == false then
default_font = nil
2017-12-19 21:52:49 +01:00
2018-01-13 12:38:55 +01:00
-- First, try with settings
local settings_font = minetest.settings:get("default_font")
2017-12-21 21:47:16 +01:00
2018-01-13 12:38:55 +01:00
if settings_font ~= nil and settings_font ~= "" then
default_font = font_lib.registered_fonts[settings_font]
2017-12-21 21:47:16 +01:00
2018-01-13 12:38:55 +01:00
if default_font == nil then
minetest.log("warning", "Default font in settings (\""..
settings_font.."\") is not registered.")
end
end
-- If failed, choose first font
if default_font == nil then
for _, font in pairs(font_lib.registered_fonts) do
default_font = font
break
end
end
2017-12-21 21:47:16 +01:00
2018-01-13 12:38:55 +01:00
-- Error, no font registered
if default_font == nil then
minetest.log("error",
"No font registred, unable to choose a default font.")
end
end
return default_font
2017-12-19 21:52:49 +01:00
end
-- Returns font properties to be used according to font_name
local function get_font(font_name)
local font = font_lib.registered_fonts[font_name]
if font == nil then
2018-01-13 12:38:55 +01:00
local message
2017-12-19 21:52:49 +01:00
if font_name == nil then
message = "No font given"
else
message = "Font \""..font_name.."\" unregistered"
end
2018-01-13 12:38:55 +01:00
font = get_default_font()
if font ~= nil then
minetest.log("info", message..", using font \""..font.name.."\".")
2017-12-19 21:52:49 +01:00
end
end
return font
end
2018-01-13 12:38:55 +01:00
-- Returns next char, managing ascii and unicode plane 0 (0000-FFFF).
local function get_next_char(text, pos)
local msb = text:byte(pos)
-- 1 byte char, ascii equivalent codepoints
if msb < 0x80 then
return msb, pos + 1
end
-- 4 bytes char not managed (Only 16 bits codepoints are managed)
if msb >= 0xF0 then
return 0, pos + 4
end
-- 3 bytes char
if msb >= 0xE0 then
return (msb - 0xE0) * 0x1000
+ text:byte(pos + 1) % 0x40 * 0x40
+ text:byte(pos + 2) % 0x40,
pos + 3
end
-- 2 bytes char (little endian)
if msb >= 0xC2 then
return (msb - 0xC2) * 0x40 + text:byte(pos + 1),
pos + 2
end
-- Not an UTF char
return 0, pos + 1
end
2017-12-19 21:52:49 +01:00
-- API functions
----------------
-- Computes text size for a given font and text (ignores new lines)
-- @param font_name Font to be used
-- @param text Text to be rendered
2017-12-19 21:52:49 +01:00
-- @return Rendered text (width, height)
2017-12-19 21:52:49 +01:00
function font_lib.get_text_size(font_name, text)
local char
local width = 0
2018-01-13 12:38:55 +01:00
local pos = 1
2017-12-19 21:52:49 +01:00
local font = get_font(font_name)
2017-12-19 21:52:49 +01:00
if font == nil then
return 0, 0
else
2018-01-13 12:38:55 +01:00
while pos <= #text do
2017-12-19 21:52:49 +01:00
char, pos = get_next_char(text, pos)
2018-01-13 12:38:55 +01:00
-- Replace chars with no texture by the NULL(0) char
2017-12-19 21:52:49 +01:00
if font.widths[char] ~= nil then
width = width + font.widths[char]
2018-01-13 12:38:55 +01:00
else
width = width + font.widths[0]
2017-12-19 21:52:49 +01:00
end
end
end
2018-01-13 12:38:55 +01:00
2017-12-19 21:52:49 +01:00
return width, font.height
end
--- Builds texture part for a text line
2017-12-19 21:52:49 +01:00
-- @param font_name Font to be used
-- @param text Text to be rendered
2017-12-19 21:52:49 +01:00
-- @param width Width of the texture (extra text is not rendered)
-- @param x Starting x position in texture
-- @param y Vertical position of the line in texture
-- @return Texture string
2017-12-19 21:52:49 +01:00
function font_lib.make_line_texture(font_name, text, width, x, y)
local texture = ""
2017-12-19 21:52:49 +01:00
local char
2018-01-13 12:38:55 +01:00
local pos = 1
2017-12-19 21:52:49 +01:00
local font = get_font(font_name)
2017-12-19 21:52:49 +01:00
if font ~= nil then
2018-01-13 12:38:55 +01:00
while pos <= #text do
2017-12-19 21:52:49 +01:00
char, pos = get_next_char(text, pos)
2018-01-13 12:38:55 +01:00
-- Replace chars with no texture by the NULL(0) char
if font.widths[char] == nil then
print(string.format("["..font_lib.name
.."] Missing char %d (%04x)",char,char))
char = 0
2017-12-19 21:52:49 +01:00
end
2018-01-13 12:38:55 +01:00
-- Add image only if it is visible (at least partly)
if x + font.widths[char] >= 0 and x <= width then
texture = texture..
string.format(":%d,%d=font_%s_%04x.png",
x, y, font.name, char)
end
x = x + font.widths[char]
2017-12-19 21:52:49 +01:00
end
end
2018-01-13 12:38:55 +01:00
return texture
end
--- Builds texture for a multiline colored text
2017-12-19 21:52:49 +01:00
-- @param font_name Font to be used
-- @param text Text to be rendered
-- @param texturew Width of the texture (extra text will be truncated)
-- @param textureh Height of the texture
-- @param maxlines Maximum number of lines
2018-01-13 12:38:55 +01:00
-- @param halign Horizontal text align ("left"/"center"/"right") (optional)
-- @param valign Vertical text align ("top"/"center"/"bottom") (optional)
-- @param color Color of the text (optional)
-- @return Texture string
2017-12-19 21:52:49 +01:00
function font_lib.make_multiline_texture(font_name, text, width, height,
2018-01-13 12:38:55 +01:00
maxlines, halign, valign, color)
local texture = ""
2017-12-19 21:52:49 +01:00
local lines = {}
local textheight = 0
local y, w, h
2018-01-13 12:38:55 +01:00
2017-12-19 21:52:49 +01:00
for num, line in pairs(split_lines(text, maxlines)) do
w, h = font_lib.get_text_size(font_name, line)
lines[num] = { text = line, width = w, height = h, }
textheight = textheight + h
end
2018-01-13 12:38:55 +01:00
2017-12-19 21:52:49 +01:00
if #lines then
if valign == "top" then
y = 0
elseif valign == "bottom" then
y = height - textheight
2018-01-13 12:38:55 +01:00
else
2017-12-19 21:52:49 +01:00
y = (height - textheight) / 2
end
end
2018-01-13 12:38:55 +01:00
for _, line in pairs(lines) do
2018-01-13 12:38:55 +01:00
if halign == "left" then
texture = texture..
font_lib.make_line_texture(font_name, line.text, width,
0, y)
elseif halign == "right" then
texture = texture..
font_lib.make_line_texture(font_name, line.text, width,
width - line.width, y)
else
texture = texture..
font_lib.make_line_texture(font_name, line.text, width,
(width - line.width) / 2, y)
end
2017-12-19 21:52:49 +01:00
y = y + line.height
end
2017-12-19 21:52:49 +01:00
texture = string.format("[combine:%dx%d", width, height)..texture
if color then texture = texture.."^[colorize:"..color end
return texture
end
2017-12-19 21:52:49 +01:00
--- Register a new font
-- Textures corresponding to the font should be named after following patern :
-- font_<name>_<code>.png
-- <name> : name of the font
-- <code> : 4 digit hexadecimal unicode of the char
2018-01-13 12:38:55 +01:00
-- @param font_name Name of the font to register
-- If registering different sizes of the same font, add size in the font name
-- (e.g. times_10, times_12...).
2017-12-19 21:52:49 +01:00
-- @param height Font height in pixels
2018-01-13 12:38:55 +01:00
-- @param widths Array of character widths in pixels, indexed by UTF codepoints
2017-12-19 21:52:49 +01:00
function font_lib.register_font(font_name, height, widths)
2018-01-13 12:38:55 +01:00
2017-12-19 21:52:49 +01:00
if font_lib.registered_fonts[font_name] ~= nil then
minetest.log("error", "Font \""..font_name.."\" already registered.")
return
end
2018-01-13 12:38:55 +01:00
if height == nil or height <= 0 then
minetest.log("error", "Font \""..font_name..
"\" must have a positive height.")
return
end
2017-12-19 21:52:49 +01:00
2018-01-13 12:38:55 +01:00
if type(widths) ~= "table" then
minetest.log("error", "Font \""..font_name..
"\" must have a widths array.")
return
2017-12-19 21:52:49 +01:00
end
2018-01-13 12:38:55 +01:00
if widths[0] == nil then
minetest.log("error", "Font \""..font_name..
"\" must have a char with codepoint 0 (=unknown char).")
return
end
font_lib.registered_fonts[font_name] =
{ name = font_name, height = height, widths = widths }
-- Force to choose again default font
-- (allows use of fonts registered after start)
default_font = false
2017-12-19 21:52:49 +01:00
end
--- Standard on_display_update entity callback.
2018-01-13 12:38:55 +01:00
-- Node should have a corresponding display_entity with size, resolution and
-- maxlines fields and optionally halign, valign and color fields
-- @param pos Node position
-- @param objref Object reference of entity
function font_lib.on_display_update(pos, objref)
local meta = minetest.get_meta(pos)
local text = meta:get_string("display_text")
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
local entity = objref:get_luaentity()
2017-12-19 21:52:49 +01:00
if entity and ndef.display_entities[entity.name] then
local def = ndef.display_entities[entity.name]
2018-01-13 12:38:55 +01:00
local font = get_font(def.font_name)
objref:set_properties({
textures={font_lib.make_multiline_texture(
2018-01-13 12:38:55 +01:00
def.font_name, text,
def.size.x * def.resolution.x * font.height,
def.size.y * def.resolution.y * font.height,
def.maxlines, def.halign, def.valign, def.color)},
visual_size = def.size
})
end
end