From 4bfc237ddc8d167dc6c36a7a08ccef2f34bd9680 Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Thu, 1 Feb 2018 16:54:55 +0100 Subject: [PATCH 01/27] Seperate signs API from signs definitions Change modnames from *_lib to *_api --- API.md | 110 ++++++++++++ LICENSE.txt | 166 +++++++++++++++++ README.md | 14 ++ copyright.txt | 3 + depends.txt | 0 init.lua | 343 ++++++++++++++++++++++++++++++++++++ settingtypes.txt | 1 + tools/make_font_lua.sh | 56 ++++++ tools/make_font_textures.sh | 111 ++++++++++++ 9 files changed, 804 insertions(+) create mode 100644 API.md create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 copyright.txt create mode 100644 depends.txt create mode 100644 init.lua create mode 100644 settingtypes.txt create mode 100755 tools/make_font_lua.sh create mode 100755 tools/make_font_textures.sh diff --git a/API.md b/API.md new file mode 100644 index 0000000..a4aee6d --- /dev/null +++ b/API.md @@ -0,0 +1,110 @@ +# Font Lib API +This document describes Font Lib API. Font Lib creates textures for font display on entities. + +## Settings +### default_font +Name of the font to be used when no font is given. The font should be registered. +If no default\_font given or if default\_font given but not registered, the first registered font will be used as default. + +## Provided methods +### get\_text\_size +**font\_lib.get\_text\_size(font\_name, text)** + +Computes size for a given font and text + +**font\_name**: Font name of registered font to use +**text**: Text to be rendered +**Returns**: rendered text width, height + +### make\_line\_texture +**font\_lib.make\_line\_texture(font\_name, text, width, x, y)** + +Builds texture part for a text line + +**font\_name**: Font name of registered font to use +**text**: Text to be rendered +**texturew**: Width of the texture (extra text is not rendered) +**x**: Starting x position in texture +**y**: Vertical position of the line in texture +**Returns**: Texture string + +### make\_multiline\_texture +**font\_lib.make\_multiline\_texture(font\_name, text, width, height, maxlines, halign, valign, color)** + +Builds texture for a multiline colored text + +**font\_name**: Font name of registered font to use +**text**: Text to be rendered +**texturew**: Width of the texture (extra text will be truncated) +**textureh**: Height of the texture +**maxlines**: Maximum number of lines +**halign**: Horizontal text align ("left", "right" or "center") (optional) +**valign**: Vertical text align ("top", "bottom" or "center") (optional) +**color**: Color of the text (optional) +**Returns**: Texture string + +### register\_font +**font\_lib.register_font(font\_name, height, widths)** + +Registers a new font in font_api. + +**font\_name**: Name of the font to register (this name will be used to address the font later) +If registering different sizes of the same font, add size in the font name (e.g. times\_10, times\_12...). +**height**: Font height in pixels (all font textures should have the same height) +**widths** : Array of character widths in pixels, indexed by UTF codepoints + +Font must have a char 0 which will be used to display any unknown char. + +All textures corresponding to the indexes in **widths** array should be present in textures directory with a name matching the pattern : + +**font\__.png** + +****: Name of the font as given in the first argument +****: UTF code of the char in 4 hexadecimal digits + +To ease that declaration, a shell is provided to build a .lua file from the texture files (see provided tools). + +## Provided tools + +Still in early stage of development, these tools are helpers to create font mods. + +### make_font_texture.sh + +This scripts takes a .ttf file as input and create one .png file per char, that can be used as font texture. Launch it from your future font mod directory. + +__Advice__ + +This script works much better with pixels font, providing the correct height. There is no antialiasing at all, vector fonts and bad heights gives very ugly results. + +__Syntax__ + +**make\_font\_texture.sh ** + +****: A TTF font file to use to create textures. +****: The font name to be used in font_api (should be simple, with no spaces). +****: Font height to be rendered. + +### make_font_lua.sh + +This script analyses textures in textures directory and creates a font\_.lua files with a call to register_font with images information. Launch it from your future font mod directory. + +Once the font\_.lua created, it can be included by a init.lua file or directly renamed to init.lua if you are creating a simple font mod. + +__Syntax__ + +**make\_font_lua.sh ** + +****: The font name to be used in font_api (same as given to make\_font\_texture.sh) + +### An exemple generating a font mod + + mkdir font_myfont + cd font_myfont + //tools/make_font_texture.sh myfont.ttf myfont 12 + //tools/make_font_lua.sh myfont + mv font_myfont.lua init.lua + + + + + diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..341c30b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7ae64b --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Font Lib + +This library for font display on entities (to be used with display_api for sign creation). + +**Dependancies**: default + +**License**: LGPL + +(Default font taken from VanessaE's homedecor/signs_lib, originally under WTFPL) + +**API**: See [API.md](https://github.com/pyrollo/display_modpack/blob/master/font_api/API.md) document please. + +For more information, see the [forum topic](https://forum.minetest.net/viewtopic.php?t=13563) at the Minetest forums. + diff --git a/copyright.txt b/copyright.txt new file mode 100644 index 0000000..ceb5446 --- /dev/null +++ b/copyright.txt @@ -0,0 +1,3 @@ +Code by Pierre-Yves Rollo (pyrollo) +Contributors: +Andrzej Pieńkowski (apienk): Unicode support and tool for creating texturess diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..e69de29 diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..f407f8b --- /dev/null +++ b/init.lua @@ -0,0 +1,343 @@ +--[[ + 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 . +--]] + +-- Global variables +------------------- + +font_api = {} +font_api.name = minetest.get_current_modname() +font_api.path = minetest.get_modpath(font_api.name) +font_api.registered_fonts = {} + +-- Local variables +------------------ + +local default_font = false + +-- Local functions +------------------ + +-- 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 + +-- Gets a default (settings or fist font) + +local function get_default_font() + -- First call + if default_font == false then + default_font = nil + + -- First, try with settings + local settings_font = minetest.settings:get("default_font") + + if settings_font ~= nil and settings_font ~= "" then + default_font = font_api.registered_fonts[settings_font] + + 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_api.registered_fonts) do + default_font = font + break + end + end + + -- 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 +end + +-- Returns font properties to be used according to font_name + +local function get_font(font_name) + local font = font_api.registered_fonts[font_name] + + if font == nil then + local message + + if font_name == nil then + message = "No font given" + else + message = "Font \""..font_name.."\" unregistered" + end + + font = get_default_font() + + if font ~= nil then + minetest.log("info", message..", using font \""..font.name.."\".") + end + end + + return font +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 + +-- 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 +-- @return Rendered text (width, height) + +function font_api.get_text_size(font_name, text) + local char + local width = 0 + local pos = 1 + local font = get_font(font_name) + + if font == nil then + return 0, 0 + else + while pos <= #text do + char, pos = get_next_char(text, pos) + -- Replace chars with no texture by the NULL(0) char + if font.widths[char] ~= nil then + width = width + font.widths[char] + else + width = width + font.widths[0] + end + end + end + + return width, font.height +end + +--- Builds texture part for a text line +-- @param font_name Font to be used +-- @param text Text to be rendered +-- @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 + +function font_api.make_line_texture(font_name, text, width, x, y) + local texture = "" + local char + local pos = 1 + local font = get_font(font_name) + + if font ~= nil then + while pos <= #text do + char, pos = get_next_char(text, pos) + + -- Replace chars with no texture by the NULL(0) char + if font.widths[char] == nil 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 + 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] + end + end + + return texture +end + +--- Builds texture for a multiline colored text +-- @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 +-- @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.make_multiline_texture(font_name, text, width, height, + maxlines, halign, valign, color) + local texture = "" + local lines = {} + local textheight = 0 + local y, w, h + + for num, line in pairs(split_lines(text, maxlines)) do + w, h = font_api.get_text_size(font_name, line) + lines[num] = { text = line, width = w, height = h, } + textheight = textheight + h + end + + if #lines then + if valign == "top" then + y = 0 + elseif valign == "bottom" then + y = height - textheight + else + y = (height - textheight) / 2 + end + end + + for _, line in pairs(lines) do + if halign == "left" then + texture = texture.. + font_api.make_line_texture(font_name, line.text, width, + 0, y) + elseif halign == "right" then + texture = texture.. + font_api.make_line_texture(font_name, line.text, width, + width - line.width, y) + else + texture = texture.. + font_api.make_line_texture(font_name, line.text, width, + (width - line.width) / 2, y) + end + y = y + line.height + end + + texture = string.format("[combine:%dx%d", width, height)..texture + if color then texture = texture.."^[colorize:"..color end + return texture +end + +--- Register a new font +-- Textures corresponding to the font should be named after following patern : +-- font__.png +-- : name of the font +-- : 4 digit hexadecimal unicode of the char +-- @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...). +-- @param height Font height in pixels +-- @param widths Array of character widths in pixels, indexed by UTF codepoints + +function font_api.register_font(font_name, height, widths) + + if font_api.registered_fonts[font_name] ~= nil then + minetest.log("error", "Font \""..font_name.."\" already registered.") + return + end + + if height == nil or height <= 0 then + minetest.log("error", "Font \""..font_name.. + "\" must have a positive height.") + return + end + + if type(widths) ~= "table" then + minetest.log("error", "Font \""..font_name.. + "\" must have a widths array.") + return + end + + if widths[0] == nil then + minetest.log("error", "Font \""..font_name.. + "\" must have a char with codepoint 0 (=unknown char).") + return + end + + font_api.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 +end + +--- Standard on_display_update entity callback. +-- 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_api.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() + + if entity and ndef.display_entities[entity.name] then + local def = ndef.display_entities[entity.name] + local font = get_font(def.font_name) + + objref:set_properties({ + textures={font_api.make_multiline_texture( + 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 + +-- Compatibility +font_lib = font_api + diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..d111159 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1 @@ +default_font(Default font) string diff --git a/tools/make_font_lua.sh b/tools/make_font_lua.sh new file mode 100755 index 0000000..ae24001 --- /dev/null +++ b/tools/make_font_lua.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +scriptname=$(basename $0) +identify="identify" + +font_name=$1 + +for f in textures/font_${font_name}_????.png +do + if [[ $f =~ textures/font_${font_name}_([0-9a-fA-F]{4}).png ]] + then + code=$((16#${BASH_REMATCH[1]})) + size=$(identify $f | cut -d " " -f 3) + w=$(echo $size | cut -d "x" -f 1) + h=$(echo $size | cut -d "x" -f 2) + + if [ -z "$font_height" ] + then + font_height=$h + else + if [ $font_height -ne $h ] + then + echo "Error : $f as height of $h pixels, previous textures have a height of $font_height pixels. All textures should have the same height." + fi + fi + + if [ -z "$font_widths" ] + then + font_widths="[$code]=$w" + else + font_widths="$font_widths, [$code]=$w" + fi + fi +done + +echo "--[[ + +$luafile generated by $scriptname $(LANG=en_US date) + +--]] + +font_api.register_font( + '$font_name', + $font_height, + { $font_widths } +); +" > font_$font_name.lua + +if grep -q font_api depends.txt &>/dev/null +then + echo "font_api already in depends.txt." +else + echo "adding font_api to depends.txt." + echo "font_api" >> depends.txt +fi + diff --git a/tools/make_font_textures.sh b/tools/make_font_textures.sh new file mode 100755 index 0000000..6f4959d --- /dev/null +++ b/tools/make_font_textures.sh @@ -0,0 +1,111 @@ +#!/bin/bash + +# This program generates a bitmap font for font_api mod for Minetest game. +# (c) Andrzej Pieńkowski +# (c) Pierre-Yves Rollo +# License: GPL + +usage() { + echo "Usage: $0 fontfile fontname fontsize" + echo "fontfile: A TTF font file to use to create textures." + echo "fontname: The font name to be used in font_api (should be simple, with no spaces)." + echo "fontsize: Font height to be rendered." +} + +if [ $# -ne 3 ] +then + usage + exit 1 +fi + +fontfile=$1 +fontname=$2 +fontsize=$3 + +if [ ! -r "$fontfile" ] +then + echo "$fontfile not readable." + exit 1 +fi + +# check imagemagick +hash convert &>/dev/null +if [ $? -eq 1 ]; then + echo -e "Error: This program requires convert from ImageMagick! Please install it by typing 'sudo apt-get install imagemagick' in terminal." + abort=1 +fi + +# check ttx +hash ttx &>/dev/null +if [ $? -eq 1 ]; then + echo -e "Error: This program requires ttx from FontTools! Please install it by typing 'sudo apt-get install fonttools' in terminal." + abort=1 +fi + +if [ $abort ] +then + exit 1 +fi + +generate() { + for i in $(seq $((0x$1)) $((0x$2))) + do + if echo "$codepoints" | grep -qi $(printf "0x%x" $i) + then + hex=$(printf "%04x" $i) + echo -e "Generating textures/font_${fontname}_$hex.png file for \"\\U$hex\" char." + if [[ "$hex" == "005c" ]] # Backslash char + then + convert -background none -fill black -font "$fontfile" -pointsize $fontsize label:"\\\\" -colorspace gray -channel alpha -threshold 50% textures/font_${fontname}_$hex.png + else + convert -background none -fill black -font "$fontfile" -pointsize $fontsize label:"$(echo -en "\\U$hex")" -colorspace gray -channel alpha -threshold 50% textures/font_${fontname}_$hex.png + fi + fi + done +} + +mkdir textures + +# Reads all available code points in the font. +codepoints=$(ttx -o - $fontfile | grep " Date: Sun, 8 Jul 2018 20:36:34 +0200 Subject: [PATCH 02/27] Creation of Font class and code update accordingly --- API.md | 133 ++++++++++------ font.lua | 270 +++++++++++++++++++++++++++++++++ init.lua | 295 +----------------------------------- registry.lua | 151 ++++++++++++++++++ tools/make_font_lua.sh | 19 ++- tools/make_font_textures.sh | 2 +- 6 files changed, 529 insertions(+), 341 deletions(-) create mode 100644 font.lua create mode 100644 registry.lua diff --git a/API.md b/API.md index a4aee6d..4c711be 100644 --- a/API.md +++ b/API.md @@ -4,65 +4,52 @@ This document describes Font Lib API. Font Lib creates textures for font display ## Settings ### default_font Name of the font to be used when no font is given. The font should be registered. + If no default\_font given or if default\_font given but not registered, the first registered font will be used as default. ## Provided methods -### get\_text\_size -**font\_lib.get\_text\_size(font\_name, text)** -Computes size for a given font and text +### font_api.get_default_font_name() +Returns de default font name. -**font\_name**: Font name of registered font to use -**text**: Text to be rendered -**Returns**: rendered text width, height +###font_api.register_font(font_name, font_def) +Register a new font. +**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...). +**font_def**: Font definition table (see **Font definition table** below). -### make\_line\_texture -**font\_lib.make\_line\_texture(font\_name, text, width, x, y)** +###font_api.on_display_update(pos, objref) +Standard on_display_update entity callback. -Builds texture part for a text line +**pos**: Node position -**font\_name**: Font name of registered font to use -**text**: Text to be rendered -**texturew**: Width of the texture (extra text is not rendered) -**x**: Starting x position in texture -**y**: Vertical position of the line in texture -**Returns**: Texture string +**objref**: Object reference of entity -### make\_multiline\_texture -**font\_lib.make\_multiline\_texture(font\_name, text, width, height, maxlines, halign, valign, color)** +Node should have a corresponding display_entity with size, resolution and maxlines fields and optionally halign, valign and color fields. -Builds texture for a multiline colored text +###Font definition table +Font definition table used by **font_api.register_font** and **font\_api.Font:new** may/can contain following elements: -**font\_name**: Font name of registered font to use -**text**: Text to be rendered -**texturew**: Width of the texture (extra text will be truncated) -**textureh**: Height of the texture -**maxlines**: Maximum number of lines -**halign**: Horizontal text align ("left", "right" or "center") (optional) -**valign**: Vertical text align ("top", "bottom" or "center") (optional) -**color**: Color of the text (optional) -**Returns**: Texture string +* **height** (required): Font height in pixels (all font textures should have the same height) . +* **widths** (required): Array of character widths in pixels, indexed by UTF codepoints. +* **margintop** (optional): Margin (in texture pixels) added on top of each char texture. +* **marginbottom** (optional): Margin (in texture pixels) added at bottom of each char texture. +* **linespacing** (optional): Spacing (in texture pixels) between each lines. -### register\_font -**font\_lib.register_font(font\_name, height, widths)** - -Registers a new font in font_api. - -**font\_name**: Name of the font to register (this name will be used to address the font later) -If registering different sizes of the same font, add size in the font name (e.g. times\_10, times\_12...). -**height**: Font height in pixels (all font textures should have the same height) -**widths** : Array of character widths in pixels, indexed by UTF codepoints +**margintop**, **marginbottom** and **linespacing** can be negative numbers (default 0) and are to be used to adjust various font styles to each other. Font must have a char 0 which will be used to display any unknown char. -All textures corresponding to the indexes in **widths** array should be present in textures directory with a name matching the pattern : +All textures corresponding to the indexes in widths array should be present in textures directory with a name matching the pattern : -**font\__.png** +> font\_**{font_name}**_**{utf_code}**.png -****: Name of the font as given in the first argument -****: UTF code of the char in 4 hexadecimal digits +**{font\_name}**: Name of the font as given in the first argument -To ease that declaration, a shell is provided to build a .lua file from the texture files (see provided tools). +**{utf\_code}**: UTF code of the char in 4 hexadecimal digits + +Example : font_courrier_0041.png is for the "A" char in the "courrier" font. + +To ease that declaration (specially to build the **widths** array), a shell is provided to build a {font\_name}.lua file from the texture files (see provided tools). ## Provided tools @@ -78,23 +65,23 @@ This script works much better with pixels font, providing the correct height. Th __Syntax__ -**make\_font\_texture.sh ** +**make\_font\_texture.sh {fontfile} {fontname} {fontsize}** -****: A TTF font file to use to create textures. -****: The font name to be used in font_api (should be simple, with no spaces). -****: Font height to be rendered. +**{fontfile}**: A TTF font file to use to create textures. +**{fontname}**: The font name to be used in font_api (should be simple, with no spaces). +**{fontsize}**: Font height to be rendered. ### make_font_lua.sh -This script analyses textures in textures directory and creates a font\_.lua files with a call to register_font with images information. Launch it from your future font mod directory. +This script analyses textures in textures directory and creates a font\_{font\_name}.lua files with a call to register_font with images information. Launch it from your future font mod directory. -Once the font\_.lua created, it can be included by a init.lua file or directly renamed to init.lua if you are creating a simple font mod. +Once the font\_{font\_name}.lua created, it can be included by a init.lua file or directly renamed to init.lua if you are creating a simple font mod. __Syntax__ -**make\_font_lua.sh ** +**make\_font_lua.sh {fontname}** -****: The font name to be used in font_api (same as given to make\_font\_texture.sh) +**{fontname}**: The font name to be used in font_api (same as given to make\_font\_texture.sh) ### An exemple generating a font mod @@ -104,7 +91,55 @@ __Syntax__ //tools/make_font_lua.sh myfont mv font_myfont.lua init.lua +## Font class +A font usable with font API. This class is supposed to be for internal use but who knows. +### font\_api.Font:new(def) +Create a new font object. +**def** is a table containing font definition. See **Font definition table** above. +### font:get_char_width(char) +Returns the width of char **char** in texture pixels. + +**char**: Unicode codepoint of char. + +### font:get_height(nb_of_lines) +Returns line(s) height. Takes care of top and bottom margins and line spacing. + +**nb_of_lines**: Number of lines in the text. + +### font:get_width(line) + +Returns the width of a text line. Beware, if line contains any new line char, they are ignored. + +**line**: Line of text which the width will be computed. + +### font:make_line_texture(line, texturew, x, y) +Create a texture for a text line. + +**line**: Line of text to be rendered in texture. + +**texturew**: Width of the texture (extra text is not rendered). + +**x**: Starting x position in texture. + +**y**: Vertical position of the line in texture. + +### font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) +Builds texture for a multiline colored text. + +**text**: Text to be rendered. + +**texturew**: Width of the texture (extra text will be truncated). + +**textureh**: Height of the texture. + +**maxlines**: Maximum number of lines. + +**halign**: Horizontal text align ("left"/"center"/"right") (optional). + +**valign**: Vertical text align ("top"/"center"/"bottom") (optional). + +**color**: Color of the text (optional). diff --git a/font.lua b/font.lua new file mode 100644 index 0000000..60563d8 --- /dev/null +++ b/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 + diff --git a/init.lua b/init.lua index f407f8b..2dc38ec 100644 --- a/init.lua +++ b/init.lua @@ -23,293 +23,12 @@ font_api = {} font_api.name = minetest.get_current_modname() font_api.path = minetest.get_modpath(font_api.name) -font_api.registered_fonts = {} --- Local variables ------------------- +-- Inclusions +------------- -local default_font = false - --- Local functions ------------------- - --- 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 - --- Gets a default (settings or fist font) - -local function get_default_font() - -- First call - if default_font == false then - default_font = nil - - -- First, try with settings - local settings_font = minetest.settings:get("default_font") - - if settings_font ~= nil and settings_font ~= "" then - default_font = font_api.registered_fonts[settings_font] - - 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_api.registered_fonts) do - default_font = font - break - end - end - - -- 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 -end - --- Returns font properties to be used according to font_name - -local function get_font(font_name) - local font = font_api.registered_fonts[font_name] - - if font == nil then - local message - - if font_name == nil then - message = "No font given" - else - message = "Font \""..font_name.."\" unregistered" - end - - font = get_default_font() - - if font ~= nil then - minetest.log("info", message..", using font \""..font.name.."\".") - end - end - - return font -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 - --- 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 --- @return Rendered text (width, height) - -function font_api.get_text_size(font_name, text) - local char - local width = 0 - local pos = 1 - local font = get_font(font_name) - - if font == nil then - return 0, 0 - else - while pos <= #text do - char, pos = get_next_char(text, pos) - -- Replace chars with no texture by the NULL(0) char - if font.widths[char] ~= nil then - width = width + font.widths[char] - else - width = width + font.widths[0] - end - end - end - - return width, font.height -end - ---- Builds texture part for a text line --- @param font_name Font to be used --- @param text Text to be rendered --- @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 - -function font_api.make_line_texture(font_name, text, width, x, y) - local texture = "" - local char - local pos = 1 - local font = get_font(font_name) - - if font ~= nil then - while pos <= #text do - char, pos = get_next_char(text, pos) - - -- Replace chars with no texture by the NULL(0) char - if font.widths[char] == nil 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 + 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] - end - end - - return texture -end - ---- Builds texture for a multiline colored text --- @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 --- @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.make_multiline_texture(font_name, text, width, height, - maxlines, halign, valign, color) - local texture = "" - local lines = {} - local textheight = 0 - local y, w, h - - for num, line in pairs(split_lines(text, maxlines)) do - w, h = font_api.get_text_size(font_name, line) - lines[num] = { text = line, width = w, height = h, } - textheight = textheight + h - end - - if #lines then - if valign == "top" then - y = 0 - elseif valign == "bottom" then - y = height - textheight - else - y = (height - textheight) / 2 - end - end - - for _, line in pairs(lines) do - if halign == "left" then - texture = texture.. - font_api.make_line_texture(font_name, line.text, width, - 0, y) - elseif halign == "right" then - texture = texture.. - font_api.make_line_texture(font_name, line.text, width, - width - line.width, y) - else - texture = texture.. - font_api.make_line_texture(font_name, line.text, width, - (width - line.width) / 2, y) - end - y = y + line.height - end - - texture = string.format("[combine:%dx%d", width, height)..texture - if color then texture = texture.."^[colorize:"..color end - return texture -end - ---- Register a new font --- Textures corresponding to the font should be named after following patern : --- font__.png --- : name of the font --- : 4 digit hexadecimal unicode of the char --- @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...). --- @param height Font height in pixels --- @param widths Array of character widths in pixels, indexed by UTF codepoints - -function font_api.register_font(font_name, height, widths) - - if font_api.registered_fonts[font_name] ~= nil then - minetest.log("error", "Font \""..font_name.."\" already registered.") - return - end - - if height == nil or height <= 0 then - minetest.log("error", "Font \""..font_name.. - "\" must have a positive height.") - return - end - - if type(widths) ~= "table" then - minetest.log("error", "Font \""..font_name.. - "\" must have a widths array.") - return - end - - if widths[0] == nil then - minetest.log("error", "Font \""..font_name.. - "\" must have a char with codepoint 0 (=unknown char).") - return - end - - font_api.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 -end +dofile(font_api.path.."/font.lua") +dofile(font_api.path.."/registry.lua") --- Standard on_display_update entity callback. -- Node should have a corresponding display_entity with size, resolution and @@ -325,11 +44,9 @@ function font_api.on_display_update(pos, objref) if entity and ndef.display_entities[entity.name] then local def = ndef.display_entities[entity.name] - local font = get_font(def.font_name) - + local font = font_api.get_font(meta:get_string("font") or def.font_name) objref:set_properties({ - textures={font_api.make_multiline_texture( - def.font_name, text, + textures={font:make_text_texture(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)}, diff --git a/registry.lua b/registry.lua new file mode 100644 index 0000000..62fbb52 --- /dev/null +++ b/registry.lua @@ -0,0 +1,151 @@ +--[[ + 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 . +--]] + +-- Global variables +------------------- + +font_api.registered_fonts = {} +font_api.registered_fonts_number = 0 + +-- Local variables +------------------ + +local default_font = false + +-- Local functions +------------------ + +-- Gets a default (settings or fist font) +local function get_default_font() + -- First call + if default_font == false then + default_font = nil + + -- First, try with settings + local settings_font = minetest.settings:get("default_font") + + if settings_font ~= nil and settings_font ~= "" then + default_font = font_api.registered_fonts[settings_font] + + 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_api.registered_fonts) do + default_font = font + break + end + end + + -- 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 +end + +--- Returns font object to be used according to font_name +-- @param font_name: Name of the font +-- @return Font object if font found (or default font) + +function font_api.get_font(font_name) + local font = font_api.registered_fonts[font_name] + + if font == nil then + local message + + if font_name == nil then + message = "No font given" + else + message = "Font \""..font_name.."\" unregistered" + end + + font = get_default_font() + + if font ~= nil then + minetest.log("info", message..", using font \""..font.name.."\".") + end + end + + return font +end + +-- API functions +---------------- + +--- Returns de default font name +-- @return Default font name + +function font_api.get_default_font_name() + return get_default_font().name +end + +--- Register a new font +-- Textures corresponding to the font should be named after following patern : +-- font__.png +-- : name of the font +-- : 4 digit hexadecimal unicode of the char +-- @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...). +-- @param def font definition. A associative array with following keys : +-- @key height (mandatory) Height in pixels of all font textures +-- @key widths (mandatory) Array of character widths in pixels, indexed by +-- UTF codepoints +-- @key margintop (optional) Margin (in texture pixels) added on top of each +-- char texture. +-- @key marginbottom (optional) dded at bottom of each char texture. +-- @key linespacing (optional) Spacing (in texture pixels) between each lines. +-- margintop, marginbottom and linespacing can be negative numbers (default 0) +-- and are to be used to adjust various font styles to each other. + +-- TODO: Add something to remove common accent if not defined in font + +function font_api.register_font(font_name, font_def) + + if font_api.registered_fonts[font_name] ~= nil then + minetest.log("error", "Font \""..font_name.."\" already registered.") + return + end + + local font = font_api.Font:new(font_def) + + if font == nil then + minetest.log("error", "Unable to register font \""..font_name.."\".") + return + end + + font.name = font_name + font_api.registered_fonts[font_name] = font + font_api.registered_fonts_number = font_api.registered_fonts_number + 1 + + -- Force to choose again default font + -- (allows use of fonts registered after start) + default_font = false + + minetest.log("action", "New font registered in font_api: "..font_name..".") +end + diff --git a/tools/make_font_lua.sh b/tools/make_font_lua.sh index ae24001..e858360 100755 --- a/tools/make_font_lua.sh +++ b/tools/make_font_lua.sh @@ -3,6 +3,17 @@ scriptname=$(basename $0) identify="identify" +usage() { + echo "Usage: $0 fontname" + echo "fontname: The name of the font. Must correspond to existing texture/font__????.png files" +} + +if [ $# -ne 1 ] +then + usage + exit 1 +fi + font_name=$1 for f in textures/font_${font_name}_????.png @@ -41,8 +52,12 @@ $luafile generated by $scriptname $(LANG=en_US date) font_api.register_font( '$font_name', - $font_height, - { $font_widths } + { + height = $font_height, + widths = { + $font_widths + }, + } ); " > font_$font_name.lua diff --git a/tools/make_font_textures.sh b/tools/make_font_textures.sh index 6f4959d..4a3191c 100755 --- a/tools/make_font_textures.sh +++ b/tools/make_font_textures.sh @@ -67,7 +67,7 @@ generate() { mkdir textures # Reads all available code points in the font. -codepoints=$(ttx -o - $fontfile | grep " Date: Sun, 8 Jul 2018 20:41:02 +0200 Subject: [PATCH 03/27] Creation of Font class and code update accordingly (fix) --- font.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/font.lua b/font.lua index 60563d8..6c848f6 100644 --- a/font.lua +++ b/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)) From 98272ebf63ab98bf740a8ba78e30adb8702b846c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 8 Jul 2018 21:27:39 +0200 Subject: [PATCH 04/27] Added two documentation schema --- doc/font.svg | 463 ++++++++++++++++++++++++++++++++++++++++++ doc/lines.svg | 544 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1007 insertions(+) create mode 100644 doc/font.svg create mode 100644 doc/lines.svg diff --git a/doc/font.svg b/doc/font.svg new file mode 100644 index 0000000..e4e8757 --- /dev/null +++ b/doc/font.svg @@ -0,0 +1,463 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + Texture Height + + Texture Width + + + + MarginTop + Margin Bottom + + Line Height + + + diff --git a/doc/lines.svg b/doc/lines.svg new file mode 100644 index 0000000..f75880f --- /dev/null +++ b/doc/lines.svg @@ -0,0 +1,544 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + Line Height + + + Text Height + Line Spacing + + + + + + + + + From 825557ff318f540487784ededbc577268fc0e6df Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Mon, 9 Jul 2018 07:19:27 +0000 Subject: [PATCH 05/27] Fix typos in API.md --- API.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index 4c711be..9900a80 100644 --- a/API.md +++ b/API.md @@ -12,12 +12,12 @@ If no default\_font given or if default\_font given but not registered, the firs ### font_api.get_default_font_name() Returns de default font name. -###font_api.register_font(font_name, font_def) +### font_api.register_font(font_name, font_def) Register a new font. **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...). **font_def**: Font definition table (see **Font definition table** below). -###font_api.on_display_update(pos, objref) +### font_api.on_display_update(pos, objref) Standard on_display_update entity callback. **pos**: Node position @@ -26,7 +26,7 @@ Standard on_display_update entity callback. Node should have a corresponding display_entity with size, resolution and maxlines fields and optionally halign, valign and color fields. -###Font definition table +### Font definition table Font definition table used by **font_api.register_font** and **font\_api.Font:new** may/can contain following elements: * **height** (required): Font height in pixels (all font textures should have the same height) . From 80b616ab9772116cde69b0a7686e5c3a1d9893b2 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 13 Jul 2018 20:41:53 +0200 Subject: [PATCH 06/27] Rework all nodes displaying text according to new font_api --- font.lua | 16 +++++++--------- init.lua | 12 ++++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/font.lua b/font.lua index 6c848f6..4619e7a 100644 --- a/font.lua +++ b/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 diff --git a/init.lua b/init.lua index 2dc38ec..06619be 100644 --- a/init.lua +++ b/init.lua @@ -45,10 +45,14 @@ function font_api.on_display_update(pos, objref) if entity and ndef.display_entities[entity.name] then local def = ndef.display_entities[entity.name] local font = font_api.get_font(meta:get_string("font") or def.font_name) - objref:set_properties({ - textures={font:make_text_texture(text, - def.size.x * def.resolution.x * font.height, - def.size.y * def.resolution.y * font.height, + + local maxlines = def.maxlines or 1 -- TODO:How to do w/o maxlines ? + + objref:set_properties({ + textures={font:make_text_texture(text, + font:get_height(maxlines) * def.size.x / def.size.y + / (def.aspect_ratio or 1), + font:get_height(maxlines), def.maxlines, def.halign, def.valign, def.color)}, visual_size = def.size }) From 076adbe8ab769e7da01092b80dc7442e25b18cfe Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 15 Jul 2018 09:40:18 +0200 Subject: [PATCH 07/27] Removed a debug trick and fixed indentation --- font.lua | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/font.lua b/font.lua index 4619e7a..580744d 100644 --- a/font.lua +++ b/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 From 35ead5a2a553af9362504b38c7c0ea042b3cc530 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Mon, 16 Jul 2018 10:18:08 +0200 Subject: [PATCH 08/27] Fix default font chosing when multiple fonts --- init.lua | 3 ++- registry.lua | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 06619be..dc3a3e0 100644 --- a/init.lua +++ b/init.lua @@ -44,7 +44,8 @@ function font_api.on_display_update(pos, objref) if entity and ndef.display_entities[entity.name] then local def = ndef.display_entities[entity.name] - local font = font_api.get_font(meta:get_string("font") or def.font_name) + local font = font_api.get_font(meta:get_string("font") ~= "" + and meta:get_string("font") or def.font_name) local maxlines = def.maxlines or 1 -- TODO:How to do w/o maxlines ? diff --git a/registry.lua b/registry.lua index 62fbb52..4b49b1e 100644 --- a/registry.lua +++ b/registry.lua @@ -49,7 +49,17 @@ local function get_default_font() end end - -- If failed, choose first font + -- If failed, choose first font without default = false + if default_font == nil then + for _, font in pairs(font_api.registered_fonts) do + if font.default then + default_font = font + break + end + end + end + + -- If failed, chose first font if default_font == nil then for _, font in pairs(font_api.registered_fonts) do default_font = font @@ -112,6 +122,7 @@ end -- If registering different sizes of the same font, add size in the font name -- (e.g. times_10, times_12...). -- @param def font definition. A associative array with following keys : +-- @key default True (by default) if this font may be used as default font -- @key height (mandatory) Height in pixels of all font textures -- @key widths (mandatory) Array of character widths in pixels, indexed by -- UTF codepoints From b74407e11e100deddfd99bc5aadccc60cbfb8d69 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Thu, 1 Nov 2018 10:47:39 +0100 Subject: [PATCH 09/27] Changed UTF8 routines and added char fallback mechanism --- fallbacks.lua | 176 +++++++++++++++++++++++++++++++++++++++++++++++++ font.lua | 178 ++++++++++++++++++++++++++------------------------ 2 files changed, 268 insertions(+), 86 deletions(-) create mode 100644 fallbacks.lua diff --git a/fallbacks.lua b/fallbacks.lua new file mode 100644 index 0000000..480d865 --- /dev/null +++ b/fallbacks.lua @@ -0,0 +1,176 @@ +--[[ + 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 . +--]] + +-- This is the unicode char fallback map. If a char is not present in +-- font, this maps indicates which char to try to use instead next. + +return { + -- Lowercase chars + ['a'] = 'A', ['b'] = 'B', ['c'] = 'C', ['d'] = 'D', + ['e'] = 'E', ['f'] = 'F', ['g'] = 'G', ['h'] = 'H', + ['i'] = 'I', ['j'] = 'J', ['k'] = 'K', ['l'] = 'L', + ['m'] = 'M', ['n'] = 'N', ['o'] = 'O', ['p'] = 'P', + ['q'] = 'Q', ['r'] = 'R', ['s'] = 'S', ['t'] = 'T', + ['u'] = 'U', ['v'] = 'V', ['w'] = 'W', ['x'] = 'X', + ['y'] = 'Y', ['z'] = 'Z', + + -- Special + ['¢'] = 'c', ['£'] = 'L', ['¥'] = 'Y', ['€'] = 'E', + ['©'] = '(C)', ['®'] = '(R)', ['™'] = 'TM', + ['ª'] = 'a', ['º'] = 'o', + ['«'] = '"', ['»'] = '"', ['´'] = '\'', + ['¹'] = '1', ['²'] = '2', ['³'] = '3', + ['µ'] = 'u', ['¤'] = 'o', + ['¼'] = '1/4', ['½'] = '1/2', ['¾'] = '3/4', + ['⅛'] = '1/8', ['⅜'] = '3/8', ['⅝'] = '5/8', ['⅞'] = '7/8', + ['¿'] = '?', + + -- Upper case accents + ['À'] = 'A', ['Á'] = 'A', ['Â'] = 'A', ['Ã'] = 'A', + ['Ä'] = 'A', ['Å'] = 'A', + ['Æ'] = 'AE', ['Ç'] = 'C', + ['È'] = 'E', ['É'] = 'E', ['Ê'] = 'E', ['Ë'] = 'E', + ['Ì'] = 'I', ['Í'] = 'I', ['Î'] = 'I', ['Ï'] = 'I', + ['Ð'] = 'D', ['Ñ'] = 'N', + ['Ò'] = 'O', ['Ó'] = 'O', ['Ô'] = 'O', ['Õ'] = 'O', + ['Ö'] = 'O', ['Ø'] = 'O', + ['Ú'] = 'U', ['Ù'] = 'U', ['Û'] = 'U', ['Ü'] = 'U', + ['×'] = 'x', ['Ý'] = 'Y', + + -- Lower case accents + ['à'] = 'a', ['à'] = 'a', ['á'] = 'a', ['â'] = 'a', + ['ã'] = 'a', ['ä'] = 'a', ['å'] = 'a', + ['æ'] = 'ae', ['ç'] = 'c', + ['è'] = 'e', ['é'] = 'e', ['ê'] = 'e', ['ë'] = 'e', + ['ì'] = 'i', ['í'] = 'i', ['î'] = 'i', ['ï'] = 'i', + ['ð'] = 'd', ['ñ'] = 'n', + ['ò'] = 'o', ['ó'] = 'o', ['ô'] = 'o', ['õ'] = 'o', + ['ö'] = 'o', ['ø'] = 'o', + ['ù'] = 'u', ['ú'] = 'u', ['û'] = 'u', ['ü'] = 'u', + ['ý'] = 'y', ['ÿ'] = 'y', + + -- Extended latin A + + ['Ā'] = 'A', ['ā'] = 'a', ['Ă'] = 'A', ['ă'] = 'a', + ['Ą'] = 'A', ['ą'] = 'a', ['Ć'] = 'C', ['ć'] = 'c', + ['Ĉ'] = 'C', ['ĉ'] = 'c', ['Ċ'] = 'C', ['ċ'] = 'c', + ['Č'] = 'C', ['č'] = 'c', ['Ď'] = 'D', ['ď'] = 'd', + ['Đ'] = 'D', ['đ'] = 'd', ['Ē'] = 'E', ['ē'] = 'e', + ['Ĕ'] = 'E', ['ĕ'] = 'e', ['Ė'] = 'E', ['ė'] = 'e', + ['Ę'] = 'E', ['ę'] = 'e', ['Ě'] = 'E', ['ě'] = 'e', + ['Ĝ'] = 'G', ['Ğ'] = 'G', ['ğ'] = 'g', ['ĝ'] = 'g', + ['Ġ'] = 'G', ['ġ'] = 'g', ['Ģ'] = 'G', ['ģ'] = 'g', + ['Ĥ'] = 'H', ['ĥ'] = 'h', ['Ħ'] = 'H', ['ħ'] = 'h', + ['Ĩ'] = 'I', ['ĩ'] = 'i', ['Ī'] = 'I', ['ī'] = 'i', + ['Ĭ'] = 'I', ['ĭ'] = 'i', ['Į'] = 'I', ['į'] = 'i', + ['ı'] = 'i', ['İ'] = 'I', ['IJ'] = 'IJ', ['ij'] = 'ij', + ['Ĵ'] = 'J', ['ĵ'] = 'j', ['ķ'] = 'k', ['Ķ'] = 'K', + ['ĸ'] = 'k', + ['Ĺ'] = 'L', ['ĺ'] = 'l', ['Ļ'] = 'L', ['ļ'] = 'l', + ['Ľ'] = 'L', ['ľ'] = 'l', ['Ŀ'] = 'L', ['ŀ'] = 'l', + ['Ł'] = 'L', ['ł'] = 'l', ['Ń'] = 'N', ['ń'] = 'n', + ['Ņ'] = 'N', ['ņ'] = 'n', ['Ň'] = 'N', ['ň'] = 'n', + ['ʼn'] = 'n', ['Ŋ'] = 'n', ['ŋ'] = 'n', + ['Ō'] = 'O', ['ō'] = 'o', ['Ŏ'] = 'O', ['ŏ'] = 'o', + ['ő'] = 'o', ['Ő'] = 'O', ['œ'] = 'oe', ['Œ'] = 'OE', + ['Ŕ'] = 'R', ['ŕ'] = 'r', ['Ŗ'] = 'R', ['ŗ'] = 'r', + ['Ř'] = 'R', ['ř'] = 'r', ['Ś'] = 'S', ['ś'] = 's', + ['Ŝ'] = 'S', ['ŝ'] = 's', ['Ş'] = 'S', ['ş'] = 's', + ['Š'] = 'S', ['š'] = 's', ['Ţ'] = 'T', ['ţ'] = 't', + ['ť'] = 't', ['Ŧ'] = 'T', ['Ť'] = 'T', ['ŧ'] = 't', + ['Ũ'] = 'U', ['ũ'] = 'u', ['Ū'] = 'U', ['ū'] = 'u', + ['Ŭ'] = 'U', ['ŭ'] = 'u', ['Ů'] = 'U', ['ů'] = 'u', + ['Ű'] = 'U', ['ű'] = 'u', ['Ų'] = 'U', ['ų'] = 'u', + ['Ŵ'] = 'W', ['ŵ'] = 'w', ['Ŷ'] = 'Y', ['ŷ'] = 'y', + ['Ÿ'] = 'Y', + ['Ź'] = 'Z', ['ź'] = 'z', ['Ż'] = 'Z', ['ż'] = 'z', + ['Ž'] = 'Z', ['ž'] = 'z', ['ſ'] = 's', + + -- Extended latin B + ['ƀ'] = 'b', ['Ɓ'] = 'B', ['Ƃ'] = 'B', ['ƃ'] = 'b', + ['Ɔ'] = 'O', + ['Ƈ'] = 'C', ['ƈ'] = 'c', ['Ɖ'] = 'D', ['Ɗ'] = 'D', + ['Ƌ'] = 'D', ['ƌ'] = 'd', ['Ǝ'] = 'E', ['Ə'] = 'e', + ['Ɛ'] = 'E', + ['Ƒ'] = 'F', ['ƒ'] = 'f', ['Ɠ'] = 'G', + ['ƕ'] = 'hv', ['Ɨ'] = 'I', ['Ƙ'] = 'K', ['ƙ'] = 'k', + ['ƚ'] = 'l', ['Ɯ'] = 'M', ['Ɲ'] = 'N', ['ƞ'] = 'n', + ['Ɵ'] = 'O', + ['Ơ'] = 'O', ['ơ'] = 'o', ['Ƣ'] = 'OI', ['ƣ'] = 'oi', + ['Ƥ'] = 'P', ['ƥ'] = 'p', ['Ʀ'] = 'YR', + ['Ƨ'] = 'S', ['ƨ'] = 's', ['ƫ'] = 't', + ['Ƭ'] = 'T', ['ƭ'] = 't', ['Ʈ'] = 'T', + ['Ư'] = 'U', ['ư'] = 'u', ['Ʋ'] = 'V', + ['Ƴ'] = 'Y', ['ƴ'] = 'y', ['Ƶ'] = 'Z', ['ƶ'] = 'z', + ['ƻ'] = '2', ['Ƽ'] = '5', ['ƽ'] = '5', + ['DŽ'] = 'DZ', ['Dž'] = 'Dz', ['dž'] = 'dz', + ['LJ'] = 'LJ', ['Lj'] = 'Lj', ['lj'] = 'lj', + ['NJ'] = 'NJ', ['Nj'] = 'Nj', ['nj'] = 'nj', + ['Ǎ'] = 'A', ['ǎ'] = 'a', ['Ǐ'] = 'I', ['ǐ'] = 'i', + ['Ǒ'] = 'O', ['ǒ'] = 'o', ['Ǔ'] = 'U', ['ǔ'] = 'u', + ['Ǖ'] = 'U', ['ǖ'] = 'u', ['Ǘ'] = 'U', ['ǘ'] = 'u', + ['Ǚ'] = 'U', ['ǚ'] = 'u', ['Ǜ'] = 'U', ['ǜ'] = 'u', + ['ǝ'] = 'e', + ['Ǟ'] = 'A', ['ǟ'] = 'a', ['Ǡ'] = 'A', ['ǡ'] = 'a', + ['Ǣ'] = 'Æ', ['ǣ'] = 'æ', ['Ǥ'] = 'G', ['ǥ'] = 'g', + ['Ǧ'] = 'G', ['ǧ'] = 'g', ['Ǩ'] = 'K', ['ǩ'] = 'k', + ['Ǫ'] = 'Q', ['ǫ'] = 'q', ['Ǭ'] = 'Q', ['ǭ'] = 'q', + ['ǰ'] = 'J', + ['DZ'] = 'DZ', ['Dz'] = 'Dz', ['dz'] = 'dz', + ['Ǵ'] = 'G', ['ǵ'] = 'g', ['Ƕ'] = 'H', + ['Ǹ'] = 'N', ['ǹ'] = 'n', ['Ǻ'] = 'A', ['ǻ'] = 'a', + ['Ǽ'] = 'Æ', ['ǽ'] = 'æ', ['Ǿ'] = 'Ø', ['ǿ'] = 'ø', + ['Ȁ'] = 'A', ['ȁ'] = 'a', ['Ȃ'] = 'A', ['ȃ'] = 'a', + ['Ȅ'] = 'E', ['ȅ'] = 'e', ['Ȇ'] = 'E', ['ȇ'] = 'e', + ['Ȉ'] = 'I', ['ȉ'] = 'i', ['Ȋ'] = 'I', ['ȋ'] = 'i', + ['Ȍ'] = 'O', ['ȍ'] = 'o', ['Ȏ'] = 'O', ['ȏ'] = 'o', + ['Ȑ'] = 'R', ['ȑ'] = 'r', ['Ȓ'] = 'R', ['ȓ'] = 'r', + ['Ȕ'] = 'U', ['ȕ'] = 'u', ['Ȗ'] = 'U', ['ȗ'] = 'u', + ['Ș'] = 'S', ['ș'] = 's', ['Ț'] = 'T', ['ț'] = 't', + ['Ȟ'] = 'H', ['ȟ'] = 'h', ['Ƞ'] = 'N', + ['ȡ'] = 'd', + ['Ȣ'] = 'OU', ['ȣ'] = 'ou', ['Ȥ'] = 'Z', ['ȥ'] = 'z', + ['Ȧ'] = 'A', ['ȧ'] = 'a', ['Ȩ'] = 'E', ['ȩ'] = 'e', + ['Ȫ'] = 'O', ['ȫ'] = 'o', ['Ȭ'] = 'O', ['ȭ'] = 'o', + ['Ȯ'] = 'O', ['ȯ'] = 'o', ['Ȱ'] = 'O', ['ȱ'] = 'o', + ['Ȳ'] = 'Y', ['ȳ'] = 'y', ['ȴ'] = 'l', + ['ȵ'] = 'n', ['ȶ'] = 't', ['ȷ'] = 'j', + ['ȸ'] = 'db', ['ȹ'] = 'qp', ['Ⱥ'] = 'A', + ['Ȼ'] = 'C', ['ȼ'] = 'c', ['Ƚ'] = 'L', + ['Ⱦ'] = 'T', ['ȿ'] = 's', ['ɀ'] = 'z', + ['Ƀ'] = 'B', ['Ʉ'] = 'U', ['Ʌ'] = 'V', + ['Ɇ'] = 'E', ['ɇ'] = 'e', ['Ɉ'] = 'J', ['ɉ'] = 'j', + ['Ɋ'] = 'Q', ['ɋ'] = 'q', ['Ɍ'] = 'R', ['ɍ'] = 'r', + ['Ɏ'] = 'Y', ['ɏ'] = 'y', ['ɐ'] = 'a', + ['ɓ'] = 'b', ['ɔ'] = 'o', + ['ɕ'] = 'c', ['ɖ'] = 'd', ['ɗ'] = 'd', + ['ɘ'] = 'e', ['ə'] = 'e', ['ɚ'] = 'e', + ['ɛ'] = 'e', ['ɜ'] = 'e', ['ɝ'] = 'e', ['ɞ'] = 'e', + ['ɟ'] = 'j', + ['ɠ'] = 'g', ['ɡ'] = 'g', ['ɢ'] = 'G', + ['ɥ'] = 'h', ['ɦ'] = 'h', ['ɧ'] = 'h', + ['ɨ'] = 'i', ['ɪ'] = 'I', + ['ɫ'] = 'l', ['ɬ'] = 'l', ['ɭ'] = 'l', + ['ɮ'] = 'lz', + ['ɯ'] = 'm', ['ɰ'] = 'm', ['ɱ'] = 'm', + ['ɲ'] = 'n', ['ɳ'] = 'n', ['ɴ'] = 'N', + ['ɵ'] = 'o', ['ɶ'] = 'Œ', + ['ɹ'] = 'r', ['ɺ'] = 'r', ['ɻ'] = 'r', + ['ɼ'] = 'r', ['ɽ'] = 'r', ['ɾ'] = 'r', ['ɿ'] = 'r', +} diff --git a/font.lua b/font.lua index 580744d..db12dba 100644 --- a/font.lua +++ b/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 +-- 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 - - -- 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 @@ -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 + line = line or '' - -- 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 + 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 - From 1a50b59995cc2637afa7cbf2ac2192eac845255f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Thu, 1 Nov 2018 12:25:47 +0100 Subject: [PATCH 10/27] Rewrited split_lines to avoid a string.split bug if first line empty --- font.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/font.lua b/font.lua index db12dba..e12bb4b 100644 --- a/font.lua +++ b/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 -------------------------------------------------------------------------------- From 11ede12680d2903215007b7b27137c832c6234da Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 9 Nov 2018 15:28:07 +0100 Subject: [PATCH 11/27] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c7ae64b..2c485d4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Font Lib +# Font API This library for font display on entities (to be used with display_api for sign creation). From 4a57d08f2b2b2e98a803cb010762862fdf800c72 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sat, 10 Nov 2018 22:35:47 +0100 Subject: [PATCH 12/27] New font selection formspec --- fontform.lua | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 8 +-- 2 files changed, 175 insertions(+), 4 deletions(-) create mode 100644 fontform.lua diff --git a/fontform.lua b/fontform.lua new file mode 100644 index 0000000..118d92d --- /dev/null +++ b/fontform.lua @@ -0,0 +1,171 @@ +--[[ + 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 . +--]] + +local modname = minetest.get_current_modname() + +-- Context management functions (surely many improvements to do) + +local contexts = {} + +local function get_player_name(player) + if type(player) == 'string' then return player end + if type(player) == 'userdata' and player.get_player_name then + return player:get_player_name() + end + minetest.log('warning', '['..modname..'] get_player_name could not identify player.') +end + +minetest.register_on_leaveplayer(function(player) + local playername = get_player_name(player) + if playername then contexts[playername] = nil end +end) + +local function new_context(player, context) + local playername = get_player_name(player) + if playername then + contexts[playername] = context + contexts[playername].playername = playername + return contexts[playername] + end +end + +local function get_context(player) + local playername = get_player_name(player) + if playername then + if contexts[playername] then + return contexts[playername] + else + minetest.log('warning', '['..modname..'] Context not found for player "'..playername..'"') + end + end +end + +local function update_context(player, changes) + local playername = get_player_name(player) + if playername then + if not contexts[playername] then + contexts[playername] = { playername = playername } + end + for key, value in pairs(changes) do + contexts[playername][key] = value + end + end +end + +-- Show node formspec functions + +local function show_node_formspec(player, pos) + local meta = minetest.get_meta(pos) + local playername = get_player_name(player) + + -- Decontextualize formspec + local fs = meta:get_string('formspec') + + -- Change context and currrent_name references to nodemeta references + fs = fs:gsub("current_name", "nodemeta:"..pos.x..","..pos.y..","..pos.z) + fs = fs:gsub("context", "nodemeta:"..pos.x..","..pos.y..","..pos.z) + + -- Change all ${} to their corresponding metadata values + local s, e + repeat + s, e = fs:find('%${.*}') + if s and e then + fs = fs:sub(1, s-1).. + minetest.formspec_escape(meta:get_string(fs:sub(s+2,e-1))).. + fs:sub(e+1) + end + until s == nil + + -- Find node on_receive_fields + local ndef = minetest.registered_nodes[minetest.get_node(pos).name] + + if ndef and ndef.on_receive_fields then + update_context(player, { on_receive_fields = ndef.on_receive_fields } ) + end + update_context(player, { node_pos = pos } ) + + -- Show formspec + minetest.show_formspec(playername, modname..':context_formspec', fs) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == modname..':context_formspec' then + local context = get_context(player) + if context == nil then return end + + if context.on_receive_fields then + context.on_receive_fields(context.pos, '', fields, player) + end + end +end) + +-- Specific functions + +local function font_list_prepare() + local list = {} + for name, _ in pairs(font_api.registered_fonts) do + list[#list+1] = name + end + table.sort(list) + return list +end + +local function show_fs(player) + local context = get_context(player) + if context == nil then return end + local fonts = font_list_prepare() + + local fs = 'size[4,'..(#fonts + 0.8)..']' + ..default.gui_bg..default.gui_bg_img..default.gui_slots + ..'button_exit[0,'..(#fonts)..';4,1;cancel;Cancel]' + + for line = 1, #fonts do + local font = font_api.get_font(fonts[line]) + fs = fs..'image[0.1,'..(line-0.9)..';4.5,0.8;' + ..font:make_text_texture(font.name, font:get_height()*5, + font:get_height()*1.2, 1, "center", "top", "#fff") + ..']button_exit[0,'..(line-1)..';4,1;font_'..font.name..';]' + end + minetest.show_formspec(context.playername, modname..':font_list', fs) +end + +minetest.register_on_player_receive_fields(function(player, formname, fields) + if formname == modname..':font_list' then + local context = get_context(player) + if context == nil then return end + + if fields.quit == 'true' then + for name, _ in pairs(font_api.registered_fonts) do + if fields['font_'..name] then + local meta = minetest.get_meta(context.pos) + meta:set_string("font", name) + display_api.update_entities(context.pos) + end + end + + -- Using after to avoid the "double close" bug + minetest.after(0, show_node_formspec, player, context.pos) + end + end +end) + +function font_api.show_font_list(player, pos) + new_context(player, { pos = pos }) + show_fs(player) +end diff --git a/init.lua b/init.lua index dc3a3e0..27cba99 100644 --- a/init.lua +++ b/init.lua @@ -29,9 +29,10 @@ font_api.path = minetest.get_modpath(font_api.name) dofile(font_api.path.."/font.lua") dofile(font_api.path.."/registry.lua") +dofile(font_api.path.."/fontform.lua") --- Standard on_display_update entity callback. --- Node should have a corresponding display_entity with size, resolution and +-- 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 @@ -49,8 +50,8 @@ function font_api.on_display_update(pos, objref) local maxlines = def.maxlines or 1 -- TODO:How to do w/o maxlines ? - objref:set_properties({ - textures={font:make_text_texture(text, + objref:set_properties({ + textures={font:make_text_texture(text, font:get_height(maxlines) * def.size.x / def.size.y / (def.aspect_ratio or 1), font:get_height(maxlines), @@ -62,4 +63,3 @@ end -- Compatibility font_lib = font_api - From 36f94f81a408f977bfa3ad41b27e505f8e0e492d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 13 Nov 2018 14:02:04 +0100 Subject: [PATCH 13/27] Update README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 2c485d4..33af92b 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,12 @@ This library for font display on entities (to be used with display_api for sign For more information, see the [forum topic](https://forum.minetest.net/viewtopic.php?t=13563) at the Minetest forums. +## Extra fonts + +You can add fonts by installing fonts mod. Be aware that each font comes with numerous textures. This can result in slowing media downloading and/or client display. + +Font mods can be found here: + + * [Metro](https://github.com/pyrollo/display_modpack/tree/master/font_metro): A multipurpose font with many chars (uppercase, lowercase and accentuated latin letters, usual signs, cyrillic and greek letters). + * [OldWizard](https://github.com/pyrollo/font_oldwizard): An old style gothic font. + * [Botic](https://github.com/pyrollo/font_botic): A scifi style font. From 81fa9f60cf567e108cb4cdfe43f2e9507a26fbfa Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 18 Nov 2018 15:29:59 +0100 Subject: [PATCH 14/27] Code simplification and securisation --- fontform.lua | 155 +++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 86 deletions(-) diff --git a/fontform.lua b/fontform.lua index 118d92d..01929dd 100644 --- a/fontform.lua +++ b/fontform.lua @@ -19,67 +19,37 @@ local modname = minetest.get_current_modname() --- Context management functions (surely many improvements to do) - local contexts = {} -local function get_player_name(player) - if type(player) == 'string' then return player end - if type(player) == 'userdata' and player.get_player_name then - return player:get_player_name() - end - minetest.log('warning', '['..modname..'] get_player_name could not identify player.') -end - minetest.register_on_leaveplayer(function(player) - local playername = get_player_name(player) - if playername then contexts[playername] = nil end + if minetest.is_player(player) then + contexts[player:get_player_name()] = nil + end end) -local function new_context(player, context) - local playername = get_player_name(player) - if playername then - contexts[playername] = context - contexts[playername].playername = playername - return contexts[playername] - end -end - -local function get_context(player) - local playername = get_player_name(player) - if playername then - if contexts[playername] then - return contexts[playername] - else - minetest.log('warning', '['..modname..'] Context not found for player "'..playername..'"') - end - end -end - -local function update_context(player, changes) - local playername = get_player_name(player) - if playername then - if not contexts[playername] then - contexts[playername] = { playername = playername } - end - for key, value in pairs(changes) do - contexts[playername][key] = value - end +local function get_context(playername) + if not contexts[playername] then + contexts[playername] = { playername = playername } end + return contexts[playername] end -- Show node formspec functions - -local function show_node_formspec(player, pos) +local function show_node_formspec(playername, pos) local meta = minetest.get_meta(pos) - local playername = get_player_name(player) -- Decontextualize formspec local fs = meta:get_string('formspec') + if not fs then + return + end + -- Change context and currrent_name references to nodemeta references - fs = fs:gsub("current_name", "nodemeta:"..pos.x..","..pos.y..","..pos.z) - fs = fs:gsub("context", "nodemeta:"..pos.x..","..pos.y..","..pos.z) + -- Change context and currrent_name references to nodemeta references + local nodemeta = string.format("nodemeta:%i,%i,%i", pos.x, pos.y ,pos.z) + fs = fs:gsub("current_name", nodemeta) + fs = fs:gsub("context", nodemeta) -- Change all ${} to their corresponding metadata values local s, e @@ -92,44 +62,44 @@ local function show_node_formspec(player, pos) end until s == nil + local context = get_context(playername) + context.node_pos = pos + -- Find node on_receive_fields local ndef = minetest.registered_nodes[minetest.get_node(pos).name] - if ndef and ndef.on_receive_fields then - update_context(player, { on_receive_fields = ndef.on_receive_fields } ) + context.on_receive_fields = ndef.on_receive_fields end - update_context(player, { node_pos = pos } ) -- Show formspec minetest.show_formspec(playername, modname..':context_formspec', fs) end minetest.register_on_player_receive_fields(function(player, formname, fields) - if formname == modname..':context_formspec' then - local context = get_context(player) - if context == nil then return end - - if context.on_receive_fields then - context.on_receive_fields(context.pos, '', fields, player) - end + if formname ~= modname..':context_formspec' then + return end + + if not minetest.is_player(player) then + return true + end + + local context = get_context(player:get_player_name()) + if context.on_receive_fields then + context.on_receive_fields(context.pos, '', fields, player) + end + return true end) -- Specific functions -local function font_list_prepare() - local list = {} +local function show_font_formspec(playername) + local context = get_context(playername) + local fonts = {} for name, _ in pairs(font_api.registered_fonts) do - list[#list+1] = name + fonts[#fonts+1] = name end - table.sort(list) - return list -end - -local function show_fs(player) - local context = get_context(player) - if context == nil then return end - local fonts = font_list_prepare() + table.sort(fonts) local fs = 'size[4,'..(#fonts + 0.8)..']' ..default.gui_bg..default.gui_bg_img..default.gui_slots @@ -146,26 +116,39 @@ local function show_fs(player) end minetest.register_on_player_receive_fields(function(player, formname, fields) - if formname == modname..':font_list' then - local context = get_context(player) - if context == nil then return end - - if fields.quit == 'true' then - for name, _ in pairs(font_api.registered_fonts) do - if fields['font_'..name] then - local meta = minetest.get_meta(context.pos) - meta:set_string("font", name) - display_api.update_entities(context.pos) - end - end - - -- Using after to avoid the "double close" bug - minetest.after(0, show_node_formspec, player, context.pos) - end + if formname ~= modname..':font_list' then + return end + + if not minetest.is_player(player) then + return true + end + + local playername = player:get_player_name() + local context = get_context(playername) + + if minetest.is_protected(context.pos, playername) then + return true + end + + if fields.quit == 'true' then + for name, _ in pairs(font_api.registered_fonts) do + if fields['font_'..name] then + local meta = minetest.get_meta(context.pos) + meta:set_string("font", name) + display_api.update_entities(context.pos) + end + end + -- Using after to avoid the "double close" bug + minetest.after(0, show_node_formspec, playername, context.pos) + end + return true end) -function font_api.show_font_list(player, pos) - new_context(player, { pos = pos }) - show_fs(player) +function font_api.show_font_list_from_pos(player, pos) + if minetest.is_player(player) then + local context = get_context(player:get_player_name()) + context.pos = pos + show_font_formspec(player:get_player_name()) + end end From 6d9e45f43deff645f05f07f350a505e7c321daeb Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sun, 18 Nov 2018 15:30:37 +0100 Subject: [PATCH 15/27] Added font formspec textures --- textures/font_api_center.png | Bin 0 -> 224 bytes textures/font_api_font.png | Bin 0 -> 238 bytes textures/font_api_left.png | Bin 0 -> 220 bytes textures/font_api_right.png | Bin 0 -> 222 bytes 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 textures/font_api_center.png create mode 100644 textures/font_api_font.png create mode 100644 textures/font_api_left.png create mode 100644 textures/font_api_right.png diff --git a/textures/font_api_center.png b/textures/font_api_center.png new file mode 100644 index 0000000000000000000000000000000000000000..967db6aded151e4f0d2e47878b4430e70164d795 GIT binary patch literal 224 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9oB=)|t_%$6K<5Ad|IeP;yA#M_ zED7=pW^j0RBMrn!@^*J&=wOxg04Xoqg z4pt^Mb~ZLQjg48NJq_KKYoo4mC{DPNxwb*Dz;Hq!gTEr5{xg|-tst{JUHx3vIVCg! E092tkF#rGn literal 0 HcmV?d00001 diff --git a/textures/font_api_font.png b/textures/font_api_font.png new file mode 100644 index 0000000000000000000000000000000000000000..e45033859af8885cabdca76db63acc5f1e228527 GIT binary patch literal 238 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9oB=)|t_%$6K<5Ad|IeP;yA#M_ zED7=pW^j0RBMrn!@^*J&=wOxg04XooU649-QVi6yBi3gww484B*6z5ywEsq8>;Q%@Ji5RU7~ z9IQ-i>}+gq3I|z~Ei9xOS;Qw?2nuKs>XwpFYzaQhq_C08qrx!5fpY_+!eWL`PQ31y Se3PbtO!jp3b6Mw<&;$TshdSW^ literal 0 HcmV?d00001 diff --git a/textures/font_api_left.png b/textures/font_api_left.png new file mode 100644 index 0000000000000000000000000000000000000000..727410bd23c9f1f4d337f8875f76d87eef8a5e58 GIT binary patch literal 220 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9oB=)|t_%$6K<5Ad|IeP;yA#M_ zED7=pW^j0RBMrn!@^*J&=wOxg04XoLV0FMhJw4NZ$OG(DmzeI+0(@_gyVWL z2P+dBI~$uD$HT2zj8h!MqFGfZ98F+x;zWVZS literal 0 HcmV?d00001 diff --git a/textures/font_api_right.png b/textures/font_api_right.png new file mode 100644 index 0000000000000000000000000000000000000000..16401d30ba6d6ad425a5a3a8f8e6655300f3d763 GIT binary patch literal 222 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!93?!50ihlx9oB=)|t_%$6K<5Ad|IeP;yA#M_ zED7=pW^j0RBMrn!@^*J&=wOxg04XooU649-QVi6yBi3gww484B*6z5ywEsq8>;RZkbk5RU7~ z9IQ-i>}+gq8V9##DNb?dSj$uxzOc)%VW&ot!eWLrfBxuq(L0xbO!9Q~b6Mw<&;$Sh Cb2vBv literal 0 HcmV?d00001 From 19f2da52ee6313778462a8708c3c617f47191c84 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Fri, 23 Nov 2018 11:34:55 +0100 Subject: [PATCH 16/27] Fonts configurable in signs, posters and steles --- fontform.lua | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/fontform.lua b/fontform.lua index 01929dd..e4952ed 100644 --- a/fontform.lua +++ b/fontform.lua @@ -101,16 +101,18 @@ local function show_font_formspec(playername) end table.sort(fonts) - local fs = 'size[4,'..(#fonts + 0.8)..']' - ..default.gui_bg..default.gui_bg_img..default.gui_slots - ..'button_exit[0,'..(#fonts)..';4,1;cancel;Cancel]' + local fs = string.format( + "size[4,%s]%s%s%sbutton_exit[0,%s;4,1;cancel;Cancel]", + #fonts + 0.8, default.gui_bg, default.gui_bg_img, default.gui_slots, + #fonts) for line = 1, #fonts do local font = font_api.get_font(fonts[line]) - fs = fs..'image[0.1,'..(line-0.9)..';4.5,0.8;' - ..font:make_text_texture(font.name, font:get_height()*5, + local texture = font:make_text_texture(font.name, font:get_height()*5, font:get_height()*1.2, 1, "center", "top", "#fff") - ..']button_exit[0,'..(line-1)..';4,1;font_'..font.name..';]' + fs = string.format( + "%simage[0.1,%s;4.5,0.8;%s]button_exit[0,%s;4,1;font_%s;]", + fs, line-0.9, texture, line-1, font.name) end minetest.show_formspec(context.playername, modname..':font_list', fs) end @@ -127,7 +129,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local playername = player:get_player_name() local context = get_context(playername) - if minetest.is_protected(context.pos, playername) then + if not context.pos + or minetest.is_protected(context.pos, playername) then return true end @@ -139,16 +142,26 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) display_api.update_entities(context.pos) end end - -- Using after to avoid the "double close" bug - minetest.after(0, show_node_formspec, playername, context.pos) + + if context.callback and type(context.callback) == "function" then + -- Using after to avoid the "double close" bug + minetest.after(0, context.callback, playername, context.pos) + else + -- Using after to avoid the "double close" bug + minetest.after(0, show_node_formspec, playername, context.pos) + end end return true end) -function font_api.show_font_list_from_pos(player, pos) +-- @param player Player viewing the form +-- @param pos Node pos +-- @param callback function(playername, pos) to be called on form close +function font_api.show_font_list(player, pos, callback) if minetest.is_player(player) then local context = get_context(player:get_player_name()) context.pos = pos + context.callback = callback show_font_formspec(player:get_player_name()) end end From 1d4c45e1309d12f186a7f657565d6aefe4200a10 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Wed, 5 Dec 2018 11:42:36 +0100 Subject: [PATCH 17/27] Fixed width fonts specific management --- API.md | 130 ++++++++++++++++++++++++++++++++++--------------------- font.lua | 26 +++++++---- init.lua | 27 +++++++++--- 3 files changed, 121 insertions(+), 62 deletions(-) diff --git a/API.md b/API.md index 9900a80..6c14ac4 100644 --- a/API.md +++ b/API.md @@ -7,35 +7,84 @@ Name of the font to be used when no font is given. The font should be registered If no default\_font given or if default\_font given but not registered, the first registered font will be used as default. -## Provided methods +## Use font_api with display_api (to display text on nodes) +### Base setup +Font_api offers a direct integration with display_api to display text on nodes. +First of all, create a display node with an entity. +To do this, refer to API.md in display_api mod, in particular "Howto register a display node". + +The only requirement then is to connect the `on_display_update` callback of the display entity to `font_api.on_display_update`: + +``` +minetest.register_node("mymod:test_text_node", { + ... + paramtype2 = "facedir", + ... + groups = { display_api = 1, ... }, + ... + display_entities = { + ["mymod:text"] = { + depth = -0.5 - display_api.entity_spacing, + on_display_update = font_api.on_display_update }, + } + ... + on_place = display_api.on_place, + on_construct = display_api.on_construct, + on_destruct = display_api.on_destruct, + on_rotate = display_api.on_rotate, + ... +}) +``` + +At this step, your node already displays text form "display_text" (hardcoded) node meta. + +But it uses defaults (default font, default size, default color). Likely you need something more. + +### Style your text +Font style and size can be chosen by adding some more entries to the display_entities definition table. + +#### Font size +Font size can be defined in various ways (maybe more in the future). +Start with a number of lines, and font_api will make it fit to the entity size. + * `maxlines` or `lines`: Number of maximum lines of text to be displayed. The font height will be adjusted accordingly. + +Then specify the char width. Two methods available: + * `aspect_ratio`: Defines the aspect ratio of chars. Works with all fonts. Should not be used if `columns` is specified. + * `columns`: Only if using a fixed width font, specifies the number of columns to display. + +#### Font style + * `font_name`: name of the font to use. Should correspond to a registered font (from a font mod). If not specified or font not found, default font is used. + * `color`: color to be used (default black). + * `halign`: Horizontal alignment: "left", "center" or "right" (default "center"). + * `valign`: Vertical alignement: "top", "middle" or "bottom" (default "middle"). + +## Provided methods ### font_api.get_default_font_name() Returns de default font name. ### font_api.register_font(font_name, font_def) Register a new font. -**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...). -**font_def**: Font definition table (see **Font definition table** below). + * `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...). + * `font_def`: Font definition table (see **Font definition table** below). ### font_api.on_display_update(pos, objref) Standard on_display_update entity callback. - -**pos**: Node position - -**objref**: Object reference of entity + * `pos`: Node position + * `objref`: Object reference of entity Node should have a corresponding display_entity with size, resolution and maxlines fields and optionally halign, valign and color fields. ### Font definition table Font definition table used by **font_api.register_font** and **font\_api.Font:new** may/can contain following elements: -* **height** (required): Font height in pixels (all font textures should have the same height) . -* **widths** (required): Array of character widths in pixels, indexed by UTF codepoints. -* **margintop** (optional): Margin (in texture pixels) added on top of each char texture. -* **marginbottom** (optional): Margin (in texture pixels) added at bottom of each char texture. -* **linespacing** (optional): Spacing (in texture pixels) between each lines. +* `height` (required): Font height in pixels (all font textures should have the same height) . +* `widths` (required): Array of character widths in pixels, indexed by UTF codepoints. +* `margintop` (optional): Margin (in texture pixels) added on top of each char texture. +* `marginbottom` (optional): Margin (in texture pixels) added at bottom of each char texture. +* `linespacing` (optional): Spacing (in texture pixels) between each lines. -**margintop**, **marginbottom** and **linespacing** can be negative numbers (default 0) and are to be used to adjust various font styles to each other. +`margintop`, `marginbottom` and `linespacing` can be negative numbers (default 0) and are to be used to adjust various font styles to each other. Font must have a char 0 which will be used to display any unknown char. @@ -57,7 +106,7 @@ Still in early stage of development, these tools are helpers to create font mods ### make_font_texture.sh -This scripts takes a .ttf file as input and create one .png file per char, that can be used as font texture. Launch it from your future font mod directory. +This scripts takes a .ttf file as input and create one .png file per char, that can be used as font texture. Launch it from your future font mod directory. __Advice__ @@ -95,51 +144,34 @@ __Syntax__ A font usable with font API. This class is supposed to be for internal use but who knows. ### font\_api.Font:new(def) -Create a new font object. +Create a new font object. + * `def` is a table containing font definition. See **Font definition table** above. -**def** is a table containing font definition. See **Font definition table** above. - -### font:get_char_width(char) -Returns the width of char **char** in texture pixels. - -**char**: Unicode codepoint of char. +### font:get_char_width(codepoint) +Returns the width of char `codepoint` in texture pixels. + * `codepoint`: Unicode codepoint of char. ### font:get_height(nb_of_lines) Returns line(s) height. Takes care of top and bottom margins and line spacing. - -**nb_of_lines**: Number of lines in the text. + * `nb_of_lines`: Number of lines in the text. ### font:get_width(line) - Returns the width of a text line. Beware, if line contains any new line char, they are ignored. - -**line**: Line of text which the width will be computed. + * `line`: Line of text which the width will be computed. ### font:make_line_texture(line, texturew, x, y) Create a texture for a text line. - -**line**: Line of text to be rendered in texture. - -**texturew**: Width of the texture (extra text is not rendered). - -**x**: Starting x position in texture. - -**y**: Vertical position of the line in texture. + * `line`: Line of text to be rendered in texture. + * `texturew`: Width of the texture (extra text is not rendered). + * `x`: Starting x position in texture. + * `y`: Vertical position of the line in texture. ### font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) Builds texture for a multiline colored text. - -**text**: Text to be rendered. - -**texturew**: Width of the texture (extra text will be truncated). - -**textureh**: Height of the texture. - -**maxlines**: Maximum number of lines. - -**halign**: Horizontal text align ("left"/"center"/"right") (optional). - -**valign**: Vertical text align ("top"/"center"/"bottom") (optional). - -**color**: Color of the text (optional). - + * `text`: Text to be rendered. + * `texturew`: Width of the texture (extra text will be truncated). + * `textureh`: Height of the texture. + * `maxlines`: Maximum number of lines. + * `halign`: Horizontal text align ("left"/"center"/"right") (optional). + * `valign`: Vertical text align ("top"/"center"/"bottom") (optional). + * `color`: Color of the text (optional). diff --git a/font.lua b/font.lua index e12bb4b..a69e049 100644 --- a/font.lua +++ b/font.lua @@ -103,6 +103,16 @@ function Font:new(def) local font = table.copy(def) setmetatable(font, self) self.__index = self + + -- Check if fixedwidth + for codepoint, width in pairs(font.widths) do + font.fixedwidth = font.fixedwidth or width + if width ~= font.fixedwidth then + font.fixedwidth = nil + break + end + end + return font end @@ -138,11 +148,11 @@ end --- Returns the width of a given char -- @param char : codepoint of the char -- @return Char width - -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] +function Font:get_char_width(codepoint) + if self.fixedwidth then + return self.fixedwidth + elseif self.widths[codepoint] then + return self.widths[codepoint] else return self.widths[0] end @@ -201,12 +211,12 @@ function Font:make_line_texture(line, texturew, x, y) codepoint, line = self:get_next_char(line) -- Add image only if it is visible (at least partly) - if x + self.widths[codepoint] >= 0 and x <= texturew then + if x + self:get_char_width(codepoint) >= 0 and x <= texturew then texture = texture.. string.format(":%d,%d=font_%s_%04x.png", x, y, self.name, codepoint) end - x = x + self.widths[codepoint] + x = x + self:get_char_width(codepoint) end return texture @@ -223,7 +233,7 @@ end -- @return Texture string function Font:make_text_texture(text, texturew, textureh, maxlines, - halign, valign, color) + halign, valign, color) local texture = "" local lines = {} local textheight = 0 diff --git a/init.lua b/init.lua index 27cba99..57a3f44 100644 --- a/init.lua +++ b/init.lua @@ -48,13 +48,30 @@ function font_api.on_display_update(pos, objref) local font = font_api.get_font(meta:get_string("font") ~= "" and meta:get_string("font") or def.font_name) - local maxlines = def.maxlines or 1 -- TODO:How to do w/o maxlines ? + -- Compute entity resolution accroding to given attributes + local texturew, textureh + textureh = font:get_height(def.lines or def.maxlines or 1) + + if def.columns then + if font.fixedwidth then + texturew = def.columns * font.fixedwidth + if def.aspect_ratio then + minetest.log('warning', "[font_api] 'aspect_ratio' ignored because 'columns' is specified") + end + else + minetest.log('warning', "[font_api] 'columns' ignored because '"..font.name.."' is not a fixed width font.") + end + end + + if not texturew then + if not def.aspect_ratio then + minetest.log('warning', "[font_api] No 'aspect_ratio' specified, using default 1.") + end + texturew = textureh * def.size.x / def.size.y / (def.aspect_ratio or 1) + end objref:set_properties({ - textures={font:make_text_texture(text, - font:get_height(maxlines) * def.size.x / def.size.y - / (def.aspect_ratio or 1), - font:get_height(maxlines), + textures={font:make_text_texture(text, texturew, textureh, def.maxlines, def.halign, def.valign, def.color)}, visual_size = def.size }) From 47cb8f2cd3520c795553d731ea91df9828d2d96e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Wed, 5 Dec 2018 15:41:56 +0100 Subject: [PATCH 18/27] New Font:render method --- API.md | 44 +++++++++++------ font.lua | 142 ++++++++++++++++++++++++------------------------------- init.lua | 72 +++++++++++++++------------- 3 files changed, 132 insertions(+), 126 deletions(-) diff --git a/API.md b/API.md index 6c14ac4..f3f4437 100644 --- a/API.md +++ b/API.md @@ -37,7 +37,7 @@ minetest.register_node("mymod:test_text_node", { }) ``` -At this step, your node already displays text form "display_text" (hardcoded) node meta. +At this step, your node already displays text form "display_text" (by default) node meta. If you want to store your text into another meta data field, add a `meta_text` field to display entity definition. But it uses defaults (default font, default size, default color). Likely you need something more. @@ -59,6 +59,28 @@ Then specify the char width. Two methods available: * `halign`: Horizontal alignment: "left", "center" or "right" (default "center"). * `valign`: Vertical alignement: "top", "middle" or "bottom" (default "middle"). +### Example +Using blue //botic// font, three lines height, aligned top left. Text stored in "text" node meta. +``` +minetest.register_node("mymod:test_text_node", { + ... + ... + display_entities = { + ["mymod:text"] = { + depth = -0.5 - display_api.entity_spacing, + on_display_update = font_api.on_display_update + meta_text = "text", + font_name = "botic", + color = "#0000FF", + maxlines = 3, + aspect_ratio = 0.5, + halign = "left", + valign = "top", + }, + } + ... +}) +``` ## Provided methods ### font_api.get_default_font_name() Returns de default font name. @@ -159,19 +181,13 @@ Returns line(s) height. Takes care of top and bottom margins and line spacing. Returns the width of a text line. Beware, if line contains any new line char, they are ignored. * `line`: Line of text which the width will be computed. -### font:make_line_texture(line, texturew, x, y) -Create a texture for a text line. - * `line`: Line of text to be rendered in texture. - * `texturew`: Width of the texture (extra text is not rendered). - * `x`: Starting x position in texture. - * `y`: Vertical position of the line in texture. - -### font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) +### font:renter(text, texturew, textureh, style) Builds texture for a multiline colored text. * `text`: Text to be rendered. * `texturew`: Width of the texture (extra text will be truncated). - * `textureh`: Height of the texture. - * `maxlines`: Maximum number of lines. - * `halign`: Horizontal text align ("left"/"center"/"right") (optional). - * `valign`: Vertical text align ("top"/"center"/"bottom") (optional). - * `color`: Color of the text (optional). + * `textureh`: Height of the texture (extra text will be truncated). + * `style`: A table with style indications: + - `lines` or `maxlines`: Maximum number of lines (default none). + - `halign`: Horizontal text align: "left"/"center"/"right" (default "center") + - `valign`: Vertical text align: "top"/"middle"/"bottom" (default "middle") + - `color`: Color of the text (default black) diff --git a/font.lua b/font.lua index a69e049..8eb43db 100644 --- a/font.lua +++ b/font.lua @@ -54,20 +54,6 @@ local function char_to_codepoint(str) end 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 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 - -------------------------------------------------------------------------------- --- Font class @@ -195,88 +181,86 @@ function Font:get_width(line) 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:make_line_texture(line, texturew, x, y) - local codepoint - local texture = "" - 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:get_char_width(codepoint) >= 0 and x <= texturew then - texture = texture.. - string.format(":%d,%d=font_%s_%04x.png", - x, y, self.name, codepoint) - end - x = x + self:get_char_width(codepoint) - 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 +--- Legacy make_text_texture method (replaced by "render" - Dec 2018) function Font:make_text_texture(text, texturew, textureh, maxlines, halign, valign, color) - local texture = "" - local lines = {} - local textheight = 0 - local y + return self:render(text, texturew, textureh, { + lines = maxlines, + valign = valign, + halign = halign, + color = color + }) +end - -- 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) } +--- Render text with the font in a view +-- @param text Text to be rendered +-- @param texturew Width (in pixels) of the texture (extra text will be truncated) +-- @param textureh Height (in pixels) of the texture (extra text will be truncated) +-- @param style Style of the rendering: +-- - lines: maximum number of text lines (if text is limited) +-- - halign: horizontal align ("left"/"center"/"right") +-- - valign: vertical align ("top"/"center"/"bottom") +-- - color: color of the text ("#rrggbb") +-- @return Texture string + +function Font:render(text, texturew, textureh, style) + local style = style or {} + + -- Split text into lines (and limit to style.lines # of lines) + local lines = {} + local pos = 1 + local found, line + repeat + found = string.find(text, "\n", pos) or (#text + 1) + line = string.sub(text, pos, found - 1) + lines[#lines + 1] = { text = line, width = self:get_width(line) } + pos = found + 1 + until (style.lines and (#lines >= style.lines)) or (pos > (#text + 1)) + + if not #lines then + return "" end - textheight = self:get_height(#lines) + local x, y, codepoint + local texture = "" + local 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 + if style.valign == "top" then + y = 0 + elseif style.valign == "bottom" then + y = textureh - textheight + else + y = (textureh - textheight) / 2 end y = y + (self.margintop or 0) 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) + if style.halign == "left" then + x = 0 + elseif style.halign == "right" then + x = texturew - line.width else - texture = texture.. - self:make_line_texture(line.text, texturew, - (texturew - line.width) / 2, y) + x = (texturew - line.width) / 2 + end + + while line.text ~= '' do + codepoint, line.text = self:get_next_char(line.text) + + -- Add image only if it is visible (at least partly) + if x + self.widths[codepoint] >= 0 and x <= texturew then + texture = texture.. + string.format(":%d,%d=font_%s_%04x.png", x, y, self.name, codepoint) + end + x = x + self.widths[codepoint] end y = y + self:get_height() + (self.linespacing or 0) end - texture = string.format("[combine:%dx%d", texturew, textureh)..texture - if color then texture = texture.."^[colorize:"..color end + if style.color then + texture = texture.."^[colorize:"..style.color + end return texture end diff --git a/init.lua b/init.lua index 57a3f44..307049b 100644 --- a/init.lua +++ b/init.lua @@ -39,43 +39,49 @@ dofile(font_api.path.."/fontform.lua") function font_api.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() - if entity and ndef.display_entities[entity.name] then - local def = ndef.display_entities[entity.name] - local font = font_api.get_font(meta:get_string("font") ~= "" - and meta:get_string("font") or def.font_name) - - -- Compute entity resolution accroding to given attributes - local texturew, textureh - textureh = font:get_height(def.lines or def.maxlines or 1) - - if def.columns then - if font.fixedwidth then - texturew = def.columns * font.fixedwidth - if def.aspect_ratio then - minetest.log('warning', "[font_api] 'aspect_ratio' ignored because 'columns' is specified") - end - else - minetest.log('warning', "[font_api] 'columns' ignored because '"..font.name.."' is not a fixed width font.") - end - end - - if not texturew then - if not def.aspect_ratio then - minetest.log('warning', "[font_api] No 'aspect_ratio' specified, using default 1.") - end - texturew = textureh * def.size.x / def.size.y / (def.aspect_ratio or 1) - end - - objref:set_properties({ - textures={font:make_text_texture(text, texturew, textureh, - def.maxlines, def.halign, def.valign, def.color)}, - visual_size = def.size - }) + if not entity or not ndef.display_entities[entity.name] then + return end + + local def = ndef.display_entities[entity.name] + local font = font_api.get_font(meta:get_string("font") ~= "" + and meta:get_string("font") or def.font_name) + + local text = meta:get_string(def.meta_text or "display_text") + + -- Compute entity resolution accroding to given attributes + local texturew, textureh + textureh = font:get_height(def.lines or def.maxlines or 1) + + if def.columns then + if font.fixedwidth then + texturew = def.columns * font.fixedwidth + if def.aspect_ratio then + minetest.log('warning', "[font_api] 'aspect_ratio' ignored because 'columns' is specified") + end + else + minetest.log('warning', "[font_api] 'columns' ignored because '"..font.name.."' is not a fixed width font.") + end + end + + if not texturew then + if not def.aspect_ratio then + minetest.log('warning', "[font_api] No 'aspect_ratio' specified, using default 1.") + end + texturew = textureh * def.size.x / def.size.y / (def.aspect_ratio or 1) + end + + objref:set_properties({ + textures={ font:render(text, texturew, textureh, { + lines = def.maxlines or def.lines, + halign = def.halign, + valign = def.valign, + color = def.color} ) }, + visual_size = def.size, + }) end -- Compatibility From 7793f6363fbbeaa89d8369d4aeee2d15dc28283e Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 11 Dec 2018 11:26:50 +0100 Subject: [PATCH 19/27] Better management of invalid UTF strings --- font.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/font.lua b/font.lua index 8eb43db..2304cd9 100644 --- a/font.lua +++ b/font.lua @@ -39,14 +39,15 @@ local function char_to_codepoint(str) local bytes = get_char_bytes(str) if bytes == 1 then return str:byte(1) - elseif bytes == 2 then + elseif bytes == 2 and str:byte(2) ~= nil then return (str:byte(1) - 0xC2) * 0x40 + str:byte(2) - elseif bytes == 3 then + elseif bytes == 3 and str:byte(2) ~= nil and str:byte(3) ~= nil then return (str:byte(1) - 0xE0) * 0x1000 + str:byte(2) % 0x40 * 0x40 + str:byte(3) % 0x40 - elseif bytes == 4 then -- Not tested + elseif bytes == 4 and str:byte(2) ~= nil and str:byte(3) ~= nil + and str:byte(4) ~= nil then -- Not tested return (str:byte(1) - 0xF0) * 0x40000 + str:byte(2) % 0x40 * 0x1000 + str:byte(3) % 0x40 * 0x40 @@ -117,6 +118,12 @@ function Font:get_next_char(text) local codepoint = char_to_codepoint(text) + if codepoint == nil then + minetest.log("warning", + "[font_api] Encountered a non UTF char, not displaying text.") + return nil, '' + end + -- Fallback mechanism if self.widths[codepoint] == nil then local char = text:sub(1, bytes) @@ -175,6 +182,7 @@ function Font:get_width(line) while line ~= "" do codepoint, line = self:get_next_char(line) + if codepoint == nil then return 0 end -- UTF Error width = width + self:get_char_width(codepoint) end @@ -247,6 +255,7 @@ function Font:render(text, texturew, textureh, style) while line.text ~= '' do codepoint, line.text = self:get_next_char(line.text) + if codepoint == nil then return '' end -- UTF Error -- Add image only if it is visible (at least partly) if x + self.widths[codepoint] >= 0 and x <= texturew then From 4c8eee06b2443e4758cd9595d1991393584b76b0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 11 Dec 2018 11:41:03 +0100 Subject: [PATCH 20/27] Better management of display_api integration --- depends.txt | 1 + display_api.lua | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 54 ++-------------------------------- 3 files changed, 80 insertions(+), 52 deletions(-) create mode 100644 display_api.lua diff --git a/depends.txt b/depends.txt index e69de29..88fa963 100644 --- a/depends.txt +++ b/depends.txt @@ -0,0 +1 @@ +display_api? diff --git a/display_api.lua b/display_api.lua new file mode 100644 index 0000000..2191dba --- /dev/null +++ b/display_api.lua @@ -0,0 +1,77 @@ +--[[ + font_api mod for Minetest - Library to create textures with fonts and text + (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 . +--]] + +-- Integration with display API + +if minetest.get_modpath("display_api") then + --- Standard on_display_update entity callback. + -- Node should have properly configured display_entity. + -- @param pos Node position + -- @param objref Object reference of entity + + font_api.on_display_update = function (pos, objref) + local meta = minetest.get_meta(pos) + local ndef = minetest.registered_nodes[minetest.get_node(pos).name] + local entity = objref:get_luaentity() + + if not entity or not ndef.display_entities[entity.name] then + return + end + + local def = ndef.display_entities[entity.name] + local font = font_api.get_font(meta:get_string("font") ~= "" + and meta:get_string("font") or def.font_name) + + local text = meta:get_string(def.meta_text or "display_text") + + -- Compute entity resolution accroding to given attributes + local texturew, textureh + textureh = font:get_height(def.lines or def.maxlines or 1) + + if def.columns then + if font.fixedwidth then + texturew = def.columns * font.fixedwidth + if def.aspect_ratio then + minetest.log('warning', "[font_api] 'aspect_ratio' ignored because 'columns' is specified") + end + else + minetest.log('warning', "[font_api] 'columns' ignored because '"..font.name.."' is not a fixed width font.") + end + end + + if not texturew then + if not def.aspect_ratio then + minetest.log('warning', "[font_api] No 'aspect_ratio' specified, using default 1.") + end + texturew = textureh * def.size.x / def.size.y / (def.aspect_ratio or 1) + end + + objref:set_properties({ + textures={ font:render(text, texturew, textureh, { + lines = def.maxlines or def.lines, + halign = def.halign, + valign = def.valign, + color = def.color} ) }, + visual_size = def.size, + }) + end +else + font_api.on_display_update = function (pos, objref) + minetest.log('error', '[font_api] font_api.on_display_update called but display_api mod not enabled.') + end +end diff --git a/init.lua b/init.lua index 307049b..7fbedf9 100644 --- a/init.lua +++ b/init.lua @@ -30,58 +30,8 @@ font_api.path = minetest.get_modpath(font_api.name) dofile(font_api.path.."/font.lua") dofile(font_api.path.."/registry.lua") dofile(font_api.path.."/fontform.lua") - ---- Standard on_display_update entity callback. --- 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_api.on_display_update(pos, objref) - local meta = minetest.get_meta(pos) - local ndef = minetest.registered_nodes[minetest.get_node(pos).name] - local entity = objref:get_luaentity() - - if not entity or not ndef.display_entities[entity.name] then - return - end - - local def = ndef.display_entities[entity.name] - local font = font_api.get_font(meta:get_string("font") ~= "" - and meta:get_string("font") or def.font_name) - - local text = meta:get_string(def.meta_text or "display_text") - - -- Compute entity resolution accroding to given attributes - local texturew, textureh - textureh = font:get_height(def.lines or def.maxlines or 1) - - if def.columns then - if font.fixedwidth then - texturew = def.columns * font.fixedwidth - if def.aspect_ratio then - minetest.log('warning', "[font_api] 'aspect_ratio' ignored because 'columns' is specified") - end - else - minetest.log('warning', "[font_api] 'columns' ignored because '"..font.name.."' is not a fixed width font.") - end - end - - if not texturew then - if not def.aspect_ratio then - minetest.log('warning', "[font_api] No 'aspect_ratio' specified, using default 1.") - end - texturew = textureh * def.size.x / def.size.y / (def.aspect_ratio or 1) - end - - objref:set_properties({ - textures={ font:render(text, texturew, textureh, { - lines = def.maxlines or def.lines, - halign = def.halign, - valign = def.valign, - color = def.color} ) }, - visual_size = def.size, - }) +if minetest.get_modpath("display_api") then + dofile(font_api.path.."/display_api.lua") end -- Compatibility From 4005b99c0583431beedb9dcdce3b1f3df9a57e1d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 11 Dec 2018 11:52:55 +0100 Subject: [PATCH 21/27] Added font_lib deprecation and corresponding notices in READMEs --- README.md | 10 ++++++++- deprecation.lua | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 4 +--- 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 deprecation.lua diff --git a/README.md b/README.md index 33af92b..13d287f 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,16 @@ For more information, see the [forum topic](https://forum.minetest.net/viewtopic You can add fonts by installing fonts mod. Be aware that each font comes with numerous textures. This can result in slowing media downloading and/or client display. -Font mods can be found here: +Font mods can be found here: * [Metro](https://github.com/pyrollo/display_modpack/tree/master/font_metro): A multipurpose font with many chars (uppercase, lowercase and accentuated latin letters, usual signs, cyrillic and greek letters). * [OldWizard](https://github.com/pyrollo/font_oldwizard): An old style gothic font. * [Botic](https://github.com/pyrollo/font_botic): A scifi style font. + + ## Deprecation notice (for modders) + + ### December 2018 + Following object is deprecate, shows a warning in log when used: + * `font_lib` global table (use `font_api` global table instead); + + This object will be removed in the future. diff --git a/deprecation.lua b/deprecation.lua new file mode 100644 index 0000000..5c0bed6 --- /dev/null +++ b/deprecation.lua @@ -0,0 +1,54 @@ +--[[ + font_api mod for Minetest - Library to create textures with fonts and text + (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 . +--]] + +-- Deprecation + +function deprecated_global_table(deprecated_global_name, replacement_global_name) + assert(type(deprecated_global_name) == 'string', "deprecated_global_name should be a string.") + assert(type(replacement_global_name) == 'string', "replacement_global_name should be a string.") + assert(deprecated_global_name ~= '', "deprecated_global_name should not be empty.") + assert(replacement_global_name ~= '', "replacement_global_name should not be empty.") + assert(rawget(_G, deprecated_global_name) == nil, "replacement global already exists.") + if _G[replacement_global_name] == nil then + print('warn_deprecated_functions: Warning, replacement global "'..replacement_global_name..'" does not exists.') + return + end + local meta = { + deprecated = deprecated_global_name, + replacement = replacement_global_name, + __index = function(table, key) + local meta = getmetatable(table) + local dbg = debug.getinfo(2, "lS") + minetest.log("warning", string.format('Warning: Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).', + meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'), (dbg.currentline or 0))) + return _G[meta.replacement][key] + end, + __newindex = function(table, key, value) + local meta = getmetatable(table) + local dbg = debug.getinfo(2, "lS") + minetest.log("warning", string.format('Warning: Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).', + meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'), (dbg.currentline or 0))) + _G[meta.replacement][key]=value + end, + } + rawset(_G, deprecated_global_name, {}) + setmetatable(_G[deprecated_global_name], meta) +end + +-- deprecated(2) -- December 2018 - Deprecation of font_lib +deprecated_global_table('font_lib', 'font_api') diff --git a/init.lua b/init.lua index 7fbedf9..80fc98a 100644 --- a/init.lua +++ b/init.lua @@ -33,6 +33,4 @@ dofile(font_api.path.."/fontform.lua") if minetest.get_modpath("display_api") then dofile(font_api.path.."/display_api.lua") end - --- Compatibility -font_lib = font_api +dofile(font_api.path.."/deprecation.lua") From 51a4a45a45b057198084d5b76a33bbb368a4810f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 11 Dec 2018 11:57:54 +0100 Subject: [PATCH 22/27] Updated font_api mod desctiption in headers --- deprecation.lua | 2 +- display_api.lua | 3 +-- fallbacks.lua | 25 ++++++++++++------------- fontform.lua | 25 ++++++++++++------------- init.lua | 25 ++++++++++++------------- registry.lua | 38 ++++++++++++++++++-------------------- 6 files changed, 56 insertions(+), 62 deletions(-) diff --git a/deprecation.lua b/deprecation.lua index 5c0bed6..b764d82 100644 --- a/deprecation.lua +++ b/deprecation.lua @@ -1,5 +1,5 @@ --[[ - font_api mod for Minetest - Library to create textures with fonts and text + font_api mod for Minetest - Library creating textures with fonts and text (c) Pierre-Yves Rollo This program is free software: you can redistribute it and/or modify diff --git a/display_api.lua b/display_api.lua index 2191dba..51a396d 100644 --- a/display_api.lua +++ b/display_api.lua @@ -1,5 +1,5 @@ --[[ - font_api mod for Minetest - Library to create textures with fonts and text + font_api mod for Minetest - Library creating textures with fonts and text (c) Pierre-Yves Rollo This program is free software: you can redistribute it and/or modify @@ -15,7 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . --]] - -- Integration with display API if minetest.get_modpath("display_api") then diff --git a/fallbacks.lua b/fallbacks.lua index 480d865..da3ff03 100644 --- a/fallbacks.lua +++ b/fallbacks.lua @@ -1,20 +1,19 @@ --[[ - font_api mod for Minetest - Library to add font display capability - to display_api mod. - (c) Pierre-Yves Rollo + font_api mod for Minetest - Library creating textures with fonts and text + (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 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. + 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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . --]] -- This is the unicode char fallback map. If a char is not present in diff --git a/fontform.lua b/fontform.lua index e4952ed..b7e6724 100644 --- a/fontform.lua +++ b/fontform.lua @@ -1,20 +1,19 @@ --[[ - font_api mod for Minetest - Library to add font display capability - to display_api mod. - (c) Pierre-Yves Rollo + font_api mod for Minetest - Library creating textures with fonts and text + (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 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. + 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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . --]] local modname = minetest.get_current_modname() diff --git a/init.lua b/init.lua index 80fc98a..c5858f4 100644 --- a/init.lua +++ b/init.lua @@ -1,20 +1,19 @@ --[[ - font_api mod for Minetest - Library to add font display capability - to display_api mod. - (c) Pierre-Yves Rollo + font_api mod for Minetest - Library creating textures with fonts and text + (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 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. + 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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . --]] -- Global variables diff --git a/registry.lua b/registry.lua index 4b49b1e..e183675 100644 --- a/registry.lua +++ b/registry.lua @@ -1,20 +1,19 @@ --[[ - font_api mod for Minetest - Library to add font display capability - to display_api mod. - (c) Pierre-Yves Rollo + font_api mod for Minetest - Library creating textures with fonts and text + (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 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. + 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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . --]] -- Global variables @@ -124,9 +123,9 @@ end -- @param def font definition. A associative array with following keys : -- @key default True (by default) if this font may be used as default font -- @key height (mandatory) Height in pixels of all font textures --- @key widths (mandatory) Array of character widths in pixels, indexed by +-- @key widths (mandatory) Array of character widths in pixels, indexed by -- UTF codepoints --- @key margintop (optional) Margin (in texture pixels) added on top of each +-- @key margintop (optional) Margin (in texture pixels) added on top of each -- char texture. -- @key marginbottom (optional) dded at bottom of each char texture. -- @key linespacing (optional) Spacing (in texture pixels) between each lines. @@ -144,19 +143,18 @@ function font_api.register_font(font_name, font_def) local font = font_api.Font:new(font_def) - if font == nil then + if font == nil then minetest.log("error", "Unable to register font \""..font_name.."\".") return end - + font.name = font_name font_api.registered_fonts[font_name] = font font_api.registered_fonts_number = font_api.registered_fonts_number + 1 - + -- Force to choose again default font -- (allows use of fonts registered after start) default_font = false - + minetest.log("action", "New font registered in font_api: "..font_name..".") end - From 8b896ab23f30b30df014727073304e4219260238 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 11 Dec 2018 12:00:32 +0100 Subject: [PATCH 23/27] Updated font_api mod desctiption in headers --- font.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/font.lua b/font.lua index 2304cd9..5794867 100644 --- a/font.lua +++ b/font.lua @@ -1,20 +1,19 @@ --[[ - font_api mod for Minetest - Library to add font display capability - to display_api mod. - (c) Pierre-Yves Rollo + font_api mod for Minetest - Library creating textures with fonts and text + (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 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. + 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 . + You should have received a copy of the GNU General Public License + along with this program. If not, see . --]] -- Fallback table From e2b597d09f856db6b4a7a7dd050106570770b02f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 11 Dec 2018 14:58:11 +0100 Subject: [PATCH 24/27] Update API documentation --- API.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index f3f4437..80b1121 100644 --- a/API.md +++ b/API.md @@ -1,5 +1,5 @@ -# Font Lib API -This document describes Font Lib API. Font Lib creates textures for font display on entities. +# Font API +This document describes Font API. Font API creates textures for font display on entities. ## Settings ### default_font @@ -97,7 +97,7 @@ Standard on_display_update entity callback. Node should have a corresponding display_entity with size, resolution and maxlines fields and optionally halign, valign and color fields. -### Font definition table +## Font definition table Font definition table used by **font_api.register_font** and **font\_api.Font:new** may/can contain following elements: * `height` (required): Font height in pixels (all font textures should have the same height) . @@ -108,6 +108,14 @@ Font definition table used by **font_api.register_font** and **font\_api.Font:ne `margintop`, `marginbottom` and `linespacing` can be negative numbers (default 0) and are to be used to adjust various font styles to each other. +Font attributes around a single char:\ +![Font attributes on a char](doc/font.svg) + +Font attributes effects on several lines:\ +![Font attributes on lines](doc/lines.svg) + +#### Additional requirements + Font must have a char 0 which will be used to display any unknown char. All textures corresponding to the indexes in widths array should be present in textures directory with a name matching the pattern : From ad6e62aa5b0fc42f83dd50cf1dec99084af83161 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sat, 9 Mar 2019 19:08:46 +0100 Subject: [PATCH 25/27] Added mod.conf --- mod.conf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 mod.conf diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..1aaccc0 --- /dev/null +++ b/mod.conf @@ -0,0 +1,3 @@ +name=font_api +description=A library for rendernig text on textures +optional_depends=display_api From ce65eb4e9b699a3e793eaf5a1b4951a7efef858f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Sat, 9 Mar 2019 19:09:27 +0100 Subject: [PATCH 26/27] Small update of README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 13d287f..ff1dd98 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Font API -This library for font display on entities (to be used with display_api for sign creation). +A library for rendernig text on textures (to be used with display_api for sign creation). **Dependancies**: default From 4c872e829d716014e601bdf0707edd11a9abbc17 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Thu, 14 Mar 2019 09:46:04 +0100 Subject: [PATCH 27/27] Replaced prints by minetest.log and code styling --- deprecation.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/deprecation.lua b/deprecation.lua index b764d82..1ffed11 100644 --- a/deprecation.lua +++ b/deprecation.lua @@ -23,9 +23,10 @@ function deprecated_global_table(deprecated_global_name, replacement_global_name assert(type(replacement_global_name) == 'string', "replacement_global_name should be a string.") assert(deprecated_global_name ~= '', "deprecated_global_name should not be empty.") assert(replacement_global_name ~= '', "replacement_global_name should not be empty.") - assert(rawget(_G, deprecated_global_name) == nil, "replacement global already exists.") + assert(rawget(_G, deprecated_global_name) == nil, "deprecated global does not exist.") if _G[replacement_global_name] == nil then - print('warn_deprecated_functions: Warning, replacement global "'..replacement_global_name..'" does not exists.') + minetest.log('warning', string.format( + 'Replacement global "%s" does not exists.', replacement_global_name)) return end local meta = { @@ -34,15 +35,19 @@ function deprecated_global_table(deprecated_global_name, replacement_global_name __index = function(table, key) local meta = getmetatable(table) local dbg = debug.getinfo(2, "lS") - minetest.log("warning", string.format('Warning: Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).', - meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'), (dbg.currentline or 0))) + minetest.log("warning", string.format( + 'Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).', + meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'), + (dbg.currentline or 0))) return _G[meta.replacement][key] end, __newindex = function(table, key, value) local meta = getmetatable(table) local dbg = debug.getinfo(2, "lS") - minetest.log("warning", string.format('Warning: Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).', - meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'), (dbg.currentline or 0))) + minetest.log("warning", string.format( + 'Accessing deprecated "%s" table, "%s" should be used instead (%s:%d).', + meta.deprecated, meta.replacement, (dbg.short_src or 'unknown'), + (dbg.currentline or 0))) _G[meta.replacement][key]=value end, }