From 4bfc237ddc8d167dc6c36a7a08ccef2f34bd9680 Mon Sep 17 00:00:00 2001 From: Thomas--S Date: Thu, 1 Feb 2018 16:54:55 +0100 Subject: [PATCH] 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 "