From c6cad702bcea7f7836153b9b7f6ad847e3bd605e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 8 Jul 2018 20:36:34 +0200 Subject: Creation of Font class and code update accordingly --- font_api/font.lua | 270 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 font_api/font.lua (limited to 'font_api/font.lua') diff --git a/font_api/font.lua b/font_api/font.lua new file mode 100644 index 0000000..60563d8 --- /dev/null +++ b/font_api/font.lua @@ -0,0 +1,270 @@ +--[[ + font_api mod for Minetest - Library to add font display capability + to display_api 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 . +--]] + + +--[[ + Margins, spacings, can be negative numbers +]]-- + +-- Local functions +------------------ + +-- Table deep copy + +local function deep_copy(input) + local output = {} + local key, value + for key, value in pairs(input) do + if type(value) == 'table' then + output[key] = deep_copy(value) + else + output[key] = value + end + end + return output +end + +-- 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 + +-- Split multiline text into array of lines, with 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 + +-------------------------------------------------------------------------------- +--- Font class + +font_api.Font = {} + +function font_api.Font:new(def) + + if type(def) ~= "table" then + minetest.log("error", "Font definition must be a table.") + return nil + end + + if def.height == nil or def.height <= 0 then + minetest.log("error", "Font definition must have a positive height.") + return nil + end + + if type(def.widths) ~= "table" then + minetest.log("error", "Font definition must have a widths array.") + return nil + end + + if def.widths[0] == nil then + minetest.log("error", + "Font must have a char with codepoint 0 (=unknown char).") + return nil + end + + local font = deep_copy(def) + setmetatable(font, self) + self.__index = self + return font +end + +--- Returns the width of a given char +-- @param char : codepoint of the char +-- @return Char width + +function font_api.Font:get_char_width(char) + -- Replace chars with no texture by the NULL(0) char + if self.widths[char] ~= nil then + return self.widths[char] + else + return self.widths[0] + end +end + +--- Text height for multiline text including margins and line spacing +-- @param nb_of_lines : number of text lines (default 1) +-- @return Text height + +function font_api.Font:get_height(nb_of_lines) + if nb_of_lines == nil then nb_of_lines = 1 end + + if nb_of_lines > 0 then + return + ( + (self.height or 0) + + (self.margin_top or 0) + + (self.margin_bottom or 0) + ) * nb_of_lines + + (self.line_spacing or 0) * (nb_of_lines -1) + else + return nb_of_lines == 0 and 0 or nil + end +end + +--- Computes text width for a given text (ignores new lines) +-- @param line Line of text which the width will be computed. +-- @return Text width + +function font_api.Font:get_width(line) + + local char + local width = 0 + local pos = 1 + + -- TODO: Use iterator + while pos <= #line do + char, pos = get_next_char(line, pos) + width = width + self:get_char_width(char) + end + + return width +end + +--- Builds texture part for a text line +-- @param line Text line to be rendered +-- @param texturew 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 + +function font_api.Font:make_line_texture(line, texturew, x, y) + local texture = "" + local char + local pos = 1 + + -- TODO: Use iterator + while pos <= #text do + char, pos = get_next_char(line, pos) + + -- Replace chars with no texture by the NULL(0) char + if self.widths[char] == nil +or char == 88 --DEBUG + then + print(string.format("["..font_api.name + .."] Missing char %d (%04x)",char,char)) + char = 0 + end + + -- Add image only if it is visible (at least partly) + if x + self.widths[char] >= 0 and x <= texturew then + texture = texture.. + string.format(":%d,%d=font_%s_%04x.png", + x, y, self.name, char) + end + x = x + self.widths[char] + end + + return texture +end + +--- Builds texture for a multiline colored text +-- @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 +-- @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 + +function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, + halign, valign, color) + local texture = "" + local lines = {} + local textheight = 0 + local y + + -- Split text into lines (limited to maxlines fist lines) + for num, line in pairs(split_lines(text, maxlines)) do + lines[num] = { text = line, width = self:get_width(line) } + end + + textheight = self:get_height(#lines) + + if #lines then + if valign == "top" then + y = 0 + elseif valign == "bottom" then + y = textureh - textheight + else + y = (textureh - textheight) / 2 + end + end + + for _, line in pairs(lines) do + if halign == "left" then + texture = texture.. + self:make_line_texture(line.text, texturew, + 0, y) + elseif halign == "right" then + texture = texture.. + self:make_line_texture(line.text, texturew, + texturew - line.width, y) + else + texture = texture.. + self:make_line_texture(line.text, texturew, + (texturew - line.width) / 2, y) + end + + y = y + self:get_height() + (self.line_spacing or 0) + end + + texture = string.format("[combine:%dx%d", texturew, textureh)..texture + if color then texture = texture.."^[colorize:"..color end + return texture +end + -- cgit v1.2.3 From b96550ab247f53c84d52fe7bbfbccbd2f672d7d1 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 8 Jul 2018 20:41:02 +0200 Subject: Creation of Font class and code update accordingly (fix) --- font_api/font.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'font_api/font.lua') diff --git a/font_api/font.lua b/font_api/font.lua index 60563d8..6c848f6 100644 --- a/font_api/font.lua +++ b/font_api/font.lua @@ -187,12 +187,11 @@ function font_api.Font:make_line_texture(line, texturew, x, y) local pos = 1 -- TODO: Use iterator - while pos <= #text do + while pos <= #line do char, pos = get_next_char(line, pos) -- Replace chars with no texture by the NULL(0) char if self.widths[char] == nil -or char == 88 --DEBUG then print(string.format("["..font_api.name .."] Missing char %d (%04x)",char,char)) -- cgit v1.2.3 From 8c7557e45d4744fe35ad058950062cf771640126 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 13 Jul 2018 20:41:53 +0200 Subject: Rework all nodes displaying text according to new font_api --- font_api/font.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'font_api/font.lua') diff --git a/font_api/font.lua b/font_api/font.lua index 6c848f6..4619e7a 100644 --- a/font_api/font.lua +++ b/font_api/font.lua @@ -17,11 +17,6 @@ along with this program. If not, see . --]] - ---[[ - Margins, spacings, can be negative numbers -]]-- - -- Local functions ------------------ @@ -146,10 +141,10 @@ function font_api.Font:get_height(nb_of_lines) return ( (self.height or 0) + - (self.margin_top or 0) + - (self.margin_bottom or 0) + (self.margintop or 0) + + (self.marginbottom or 0) ) * nb_of_lines + - (self.line_spacing or 0) * (nb_of_lines -1) + (self.linespacing or 0) * (nb_of_lines -1) else return nb_of_lines == 0 and 0 or nil end @@ -192,6 +187,7 @@ function font_api.Font:make_line_texture(line, texturew, x, y) -- Replace chars with no texture by the NULL(0) char if self.widths[char] == nil +or char == 88 then print(string.format("["..font_api.name .."] Missing char %d (%04x)",char,char)) @@ -243,6 +239,8 @@ function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, y = (textureh - textheight) / 2 end end + + y = y + (self.margintop or 0) for _, line in pairs(lines) do if halign == "left" then @@ -259,7 +257,7 @@ function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, (texturew - line.width) / 2, y) end - y = y + self:get_height() + (self.line_spacing or 0) + y = y + self:get_height() + (self.linespacing or 0) end texture = string.format("[combine:%dx%d", texturew, textureh)..texture -- cgit v1.2.3 From fac6dfe1f896b9c8a59c6f0416759c3dc8d715ed Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 15 Jul 2018 09:40:18 +0200 Subject: Removed a debug trick and fixed indentation --- font_api/font.lua | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'font_api/font.lua') diff --git a/font_api/font.lua b/font_api/font.lua index 4619e7a..580744d 100644 --- a/font_api/font.lua +++ b/font_api/font.lua @@ -43,7 +43,7 @@ local function get_next_char(text, pos) -- 1 byte char, ascii equivalent codepoints if msb < 0x80 then return msb, pos + 1 - end + end -- 4 bytes char not managed (Only 16 bits codepoints are managed) if msb >= 0xF0 then @@ -64,7 +64,7 @@ local function get_next_char(text, pos) pos + 2 end - -- Not an UTF char + -- Not an UTF char return 0, pos + 1 end @@ -158,7 +158,7 @@ function font_api.Font:get_width(line) local char local width = 0 - local pos = 1 + local pos = 1 -- TODO: Use iterator while pos <= #line do @@ -187,11 +187,10 @@ function font_api.Font:make_line_texture(line, texturew, x, y) -- Replace chars with no texture by the NULL(0) char if self.widths[char] == nil -or char == 88 then - print(string.format("["..font_api.name + print(string.format("["..font_api.name .."] Missing char %d (%04x)",char,char)) - char = 0 + char = 0 end -- Add image only if it is visible (at least partly) @@ -220,27 +219,27 @@ function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) local texture = "" local lines = {} - local textheight = 0 - local y + local textheight = 0 + local y -- Split text into lines (limited to maxlines fist lines) - for num, line in pairs(split_lines(text, maxlines)) do - lines[num] = { text = line, width = self:get_width(line) } - end + for num, line in pairs(split_lines(text, maxlines)) do + lines[num] = { text = line, width = self:get_width(line) } + end textheight = self:get_height(#lines) - if #lines then - if valign == "top" then - y = 0 - elseif valign == "bottom" then - y = textureh - textheight - else - y = (textureh - textheight) / 2 - end - end + if #lines then + if valign == "top" then + y = 0 + elseif valign == "bottom" then + y = textureh - textheight + else + y = (textureh - textheight) / 2 + end + end - y = y + (self.margintop or 0) + y = y + (self.margintop or 0) for _, line in pairs(lines) do if halign == "left" then -- cgit v1.2.3 From 95c9da849d98ecee9b040761683e86de81303ccf Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Thu, 1 Nov 2018 10:47:39 +0100 Subject: Changed UTF8 routines and added char fallback mechanism --- font_api/font.lua | 180 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 93 insertions(+), 87 deletions(-) (limited to 'font_api/font.lua') diff --git a/font_api/font.lua b/font_api/font.lua index 580744d..db12dba 100644 --- a/font_api/font.lua +++ b/font_api/font.lua @@ -17,59 +17,44 @@ along with this program. If not, see . --]] +-- Fallback table +local fallbacks = dofile(font_api.path.."/fallbacks.lua") + -- Local functions ------------------ --- Table deep copy - -local function deep_copy(input) - local output = {} - local key, value - for key, value in pairs(input) do - if type(value) == 'table' then - output[key] = deep_copy(value) - else - output[key] = value - end +-- Returns number of UTF8 bytes of the first char of the string +local function get_char_bytes(str) + local msb = str:byte(1) + if msb ~= nil then + if msb < 0x80 then return 1 end + if msb >= 0xF0 then return 4 end + if msb >= 0xE0 then return 3 end + if msb >= 0xC2 then return 2 end end - return output end --- 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 +-- Returns the unicode codepoint of the first char of the string +local function char_to_codepoint(str) + local bytes = get_char_bytes(str) + if bytes == 1 then + return str:byte(1) + elseif bytes == 2 then + return (str:byte(1) - 0xC2) * 0x40 + + str:byte(2) + elseif bytes == 3 then + return (str:byte(1) - 0xE0) * 0x1000 + + str:byte(2) % 0x40 * 0x40 + + str:byte(3) % 0x40 + elseif bytes == 4 then -- Not tested + return (str:byte(1) - 0xF0) * 0x40000 + + str:byte(2) % 0x40 * 0x1000 + + str:byte(3) % 0x40 * 0x40 + + str:byte(4) % 0x40 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 -- Split multiline text into array of lines, with maximum lines. - local function split_lines(text, maxlines) local splits = text:split("\n") if maxlines then @@ -86,42 +71,75 @@ end -------------------------------------------------------------------------------- --- Font class -font_api.Font = {} +local Font = {} +font_api.Font = Font -function font_api.Font:new(def) +function Font:new(def) if type(def) ~= "table" then - minetest.log("error", "Font definition must be a table.") + minetest.log("error", + "[font_api] Font definition must be a table.") return nil end - + if def.height == nil or def.height <= 0 then - minetest.log("error", "Font definition must have a positive height.") + minetest.log("error", + "[font_api] Font definition must have a positive height.") return nil end if type(def.widths) ~= "table" then - minetest.log("error", "Font definition must have a widths array.") + minetest.log("error", + "[font_api] Font definition must have a widths array.") return nil end if def.widths[0] == nil then - minetest.log("error", - "Font must have a char with codepoint 0 (=unknown char).") + minetest.log("error", + "[font_api] Font must have a char with codepoint 0 (=unknown char).") return nil end - local font = deep_copy(def) + local font = table.copy(def) setmetatable(font, self) self.__index = self return font end +--- Gets the next char of a text +-- @return Codepoint of first char, +-- @return Remaining string without this first char + +function Font:get_next_char(text) + local bytes = get_char_bytes(text) + + if bytes == nil then + minetest.log("warning", + "[font_api] Encountered a non UTF char, not displaying text.") + return nil, '' + end + + local codepoint = char_to_codepoint(text) + + -- Fallback mechanism + if self.widths[codepoint] == nil then + local char = text:sub(1, bytes) + + if fallbacks[char] then + return self:get_next_char(fallbacks[char]..text:sub(bytes+1)) + else + return 0, text:sub(bytes+1) -- Ultimate fallback + end + else + return codepoint, text:sub(bytes+1) + end +end + --- Returns the width of a given char -- @param char : codepoint of the char -- @return Char width -function font_api.Font:get_char_width(char) +function Font:get_char_width(char) -- Replace chars with no texture by the NULL(0) char if self.widths[char] ~= nil then return self.widths[char] @@ -134,13 +152,13 @@ end -- @param nb_of_lines : number of text lines (default 1) -- @return Text height -function font_api.Font:get_height(nb_of_lines) +function Font:get_height(nb_of_lines) if nb_of_lines == nil then nb_of_lines = 1 end - + if nb_of_lines > 0 then - return + return ( - (self.height or 0) + + (self.height or 0) + (self.margintop or 0) + (self.marginbottom or 0) ) * nb_of_lines + @@ -154,16 +172,14 @@ end -- @param line Line of text which the width will be computed. -- @return Text width -function font_api.Font:get_width(line) - - local char +function Font:get_width(line) + local codepoint local width = 0 - local pos = 1 + line = line or '' - -- TODO: Use iterator - while pos <= #line do - char, pos = get_next_char(line, pos) - width = width + self:get_char_width(char) + while line ~= "" do + codepoint, line = self:get_next_char(line) + width = width + self:get_char_width(codepoint) end return width @@ -176,30 +192,21 @@ end -- @param y Vertical position of the line in texture -- @return Texture string -function font_api.Font:make_line_texture(line, texturew, x, y) +function Font:make_line_texture(line, texturew, x, y) + local codepoint local texture = "" - local char - local pos = 1 - - -- TODO: Use iterator - while pos <= #line do - char, pos = get_next_char(line, pos) - - -- Replace chars with no texture by the NULL(0) char - if self.widths[char] == nil - then - print(string.format("["..font_api.name - .."] Missing char %d (%04x)",char,char)) - char = 0 - end + line = line or '' + + while line ~= '' do + codepoint, line = self:get_next_char(line) -- Add image only if it is visible (at least partly) - if x + self.widths[char] >= 0 and x <= texturew then + if x + self.widths[codepoint] >= 0 and x <= texturew then texture = texture.. string.format(":%d,%d=font_%s_%04x.png", - x, y, self.name, char) + x, y, self.name, codepoint) end - x = x + self.widths[char] + x = x + self.widths[codepoint] end return texture @@ -215,13 +222,13 @@ end -- @param color Color of the text (optional) -- @return Texture string -function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, +function Font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) local texture = "" local lines = {} local textheight = 0 local y - + -- Split text into lines (limited to maxlines fist lines) for num, line in pairs(split_lines(text, maxlines)) do lines[num] = { text = line, width = self:get_width(line) } @@ -238,7 +245,7 @@ function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, y = (textureh - textheight) / 2 end end - + y = y + (self.margintop or 0) for _, line in pairs(lines) do @@ -263,4 +270,3 @@ function font_api.Font:make_text_texture(text, texturew, textureh, maxlines, if color then texture = texture.."^[colorize:"..color end return texture end - -- cgit v1.2.3 From 06d35ec9bf48e5fd96952cca5264d92742cf31db Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Thu, 1 Nov 2018 12:25:47 +0100 Subject: Rewrited split_lines to avoid a string.split bug if first line empty --- font_api/font.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'font_api/font.lua') diff --git a/font_api/font.lua b/font_api/font.lua index db12dba..e12bb4b 100644 --- a/font_api/font.lua +++ b/font_api/font.lua @@ -55,17 +55,17 @@ local function char_to_codepoint(str) end -- Split multiline text into array of lines, with maximum lines. +-- Can not use minetest string.split as it has bug if first line(s) empty 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 + local lines = {} + local pos = 1 + repeat + local found = string.find(text, "\n", pos) + found = found or #text + 1 + lines[#lines + 1] = string.sub(text, pos, found - 1) + pos = found + 1 + until (maxlines and (#lines >= maxlines)) or (pos > (#text + 1)) + return lines end -------------------------------------------------------------------------------- -- cgit v1.2.3