From 9bd40bd31f5262b237a9854b5c38b1d070eb081f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Wed, 8 Oct 2025 21:36:16 +0200 Subject: [PATCH] wip --- font_api/font.lua | 10 +++-- font_api/fontform.lua | 2 +- font_api/tools/make_font.lua | 79 ++++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/font_api/font.lua b/font_api/font.lua index 202c198..c19edee 100644 --- a/font_api/font.lua +++ b/font_api/font.lua @@ -126,6 +126,10 @@ function Font:new(def) return nil end + if self.charspacing == nil then + self.charspacing = 0 + end + setmetatable(font, self) return font @@ -172,7 +176,7 @@ end function Font:get_char_width(codepoint) -- [1] is char width - return (self.glyphs[codepoint] or self.glyphs[0])[1] + return (self.glyphs[codepoint] or self.glyphs[0])[1] + self.charspacing end --- Returns texture for a given glyph @@ -303,13 +307,13 @@ function Font:render(text, width, height, style) local glyph = self.glyphs[codepoint] -- Add image only if it is visible (at least partly) - if x + glyph[1] >= 0 and x <= width then + if x + glyph[1] + self.charspacing >= 0 and x <= width then local glyph_texture = self:get_glyph_texture(glyph):gsub("[\\^:]", "\\%0") if glyph_texture ~= '' then texture = string.format("%s:%d,%d=%s", texture, x, y, glyph_texture) end end - x = x + glyph[1] + x = x + glyph[1] + self.charspacing end y = y + self:get_height() + (self.linespacing or 0) diff --git a/font_api/fontform.lua b/font_api/fontform.lua index 04e19b3..6c7169f 100644 --- a/font_api/fontform.lua +++ b/font_api/fontform.lua @@ -113,7 +113,7 @@ local function show_font_formspec(playername) ) 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) + fs, line - 0.9, texture:gsub("[\\^]", "\\%0"), line - 1, font.name) end minetest.show_formspec(context.playername, modname..':font_list', fs) end diff --git a/font_api/tools/make_font.lua b/font_api/tools/make_font.lua index d2f4ad5..9b41e19 100644 --- a/font_api/tools/make_font.lua +++ b/font_api/tools/make_font.lua @@ -6,6 +6,32 @@ -- TODO : detect and manage fixed width fonts +-- TODO: HAVE A PARAM FILE FOR EACH FONT WITH WANTED SETTINGS +-- Par exemple: +-- activation du trim +-- valeur de linespacing / charspacing .. + +-- +-- Dependancies check +-- + +local function check(cmd, msg) + if os.execute(cmd .. " > /dev/null 2>&1") then + return true + else + print(msg) + end +end + +if + not check("convert --version", "Error: This program requires convert from ImageMagick!") or + not check("identify --version", "Error: This program requires identify from ImageMagick!") or + not check("ttx --version", "Error: This program requires ttx from FontTools!") +then + print("Please fix above problem and retry.") + os.exit(1) +end + -- -- Argument management @@ -29,27 +55,6 @@ local fontsize=arg[3] local modname = fontname --- --- Dependancies check --- - -local function check(cmd, msg) - if os.execute(cmd .. " > /dev/null 2>&1") then - return true - else - print(msg) - end -end - -if - not check("convert --version", "Error: This program requires convert from ImageMagick!") or - not check("identify --version", "Error: This program requires identify from ImageMagick!") or - not check("ttx --version", "Error: This program requires ttx from FontTools!") -then - print("Please fix above problem and retry.") - os.exit(1) -end - -- -- Prepare output directory -- @@ -124,11 +129,12 @@ local function measure(codepoint) local char = utf8.char(codepoint) local cmd = string.format( - "convert -font \"%s\" -pointsize %d label:\"%s\" -identify NULL:", - fontfile, fontsize, escape(char) - ) + "convert -font \"%s\" -pointsize %d label:\"%s\" -define trim:edges=east,west -trim info:", + fontfile, fontsize, escape(char) + ) + + local _, _, w, h = string.find(command(cmd), "([0-9]+)x([0-9]+)" ) - _, _, w, h = string.find(command(cmd), "([0-9]+)x([0-9]+)" ) return tonumber(w), tonumber(h) end @@ -191,7 +197,7 @@ local function make_final_texture(filename) -- Compute positions for _, tile_width in ipairs(tile_widths) do for _, codepoint in ipairs(by_width[tile_width]) do - local glyph_x = x // tile_width + local glyph_x = math.ceil(x / tile_width) x = glyph_x * tile_width if x + tile_width > texture_width then -- no space left on current line x = 0 @@ -201,7 +207,7 @@ local function make_final_texture(filename) end glyph_xs[codepoint] = glyph_x glyph_ys[codepoint] = glyph_y - glyph_ns[codepoint] = texture_width // tile_width + glyph_ns[codepoint] = math.floor(texture_width / tile_width) x = x + tile_width end end @@ -213,7 +219,7 @@ local function make_final_texture(filename) )) for codepoint, n in pairs(glyph_ns) do - local w = texture_width // n + local w = math.floor(texture_width / n) local x = w * glyph_xs[codepoint] local y = font_height * glyph_ys[codepoint] @@ -223,11 +229,11 @@ local function make_final_texture(filename) if codepoint == 0 then -- The "unknown" char cmd = string.format("xc:transparent[%dx%d] -background none -colorspace gray -stroke black -fill transparent -strokewidth 1 -draw \"rectangle 0,0 %d,%d\"", - w, font_height, w - 1, font_height - 1 + w, font_height - 1, w - 1, font_height - 2 ) else -- Other glyhp chars - cmd = string.format("-channel alpha -background none -colorspace gray -fill black -font \"%s\" -pointsize %d label:\"%s\"", + cmd = string.format("-channel alpha -background none -colorspace gray -fill black -font \"%s\" -pointsize %d label:\"%s\" -define trim:edges=east,west -trim", fontfile, fontsize, escape(utf8.char(codepoint)) ) end @@ -264,22 +270,22 @@ add_codepoints(0x0021, 0x007f) add_codepoints(0x00a0, 0x00ff) -- 0100-017f Latin Extended-A (full) ---add_codepoints(0x0100, 0x017f) +add_codepoints(0x0100, 0x017f) -- 0370-03ff Greek (full) ---add_codepoints(0x0370, 0x03ff) +add_codepoints(0x0370, 0x03ff) -- 0400-04ff Cyrilic (full) ---add_codepoints(0x0400, 0x04ff) +add_codepoints(0x0400, 0x04ff) -- 2000-206f General Punctuation (Limited to Dashes) ---add_codepoints(0x2010, 0x2015) +add_codepoints(0x2010, 0x2015) -- 2000-206f General Punctuation (Limited to Quotes) ---add_codepoints(0x2018, 0x201F) +add_codepoints(0x2018, 0x201F) -- 20a0-20cf Currency Symbols (Limited to Euro symbol) ---add_codepoints(0x20ac, 0x20ac) +add_codepoints(0x20ac, 0x20ac) print("Prepare final texture") @@ -311,6 +317,7 @@ font_api.register_font( default = true, margintop = 3, linespacing = -2, + charspacing = 2, texture_height = %d, glyphs_height = %d, glyphs = {