From 06ac1870418034c02ecd073a5a85b674a728d892 Mon Sep 17 00:00:00 2001 From: ywang Date: Sun, 8 Aug 2021 15:36:58 +0200 Subject: Fix newline bug --- advtrains/spec/textrender_spec.lua | 2 ++ advtrains/textrender.lua | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/advtrains/spec/textrender_spec.lua b/advtrains/spec/textrender_spec.lua index 063d9ee..f02afec 100644 --- a/advtrains/spec/textrender_spec.lua +++ b/advtrains/spec/textrender_spec.lua @@ -1,5 +1,7 @@ package.path = "../?.lua" .. package.path +-- This unittest file only makes sure that extremely trivial things do not break. It is not intended to be complete + local tr = require("textrender") describe("string to unicode codepoint sequence converter", function() diff --git a/advtrains/textrender.lua b/advtrains/textrender.lua index f23cd3f..de9deac 100644 --- a/advtrains/textrender.lua +++ b/advtrains/textrender.lua @@ -146,16 +146,20 @@ end local function split_lines(cs, width, height, options) local lines = {} local nlines = options.lines - local lwidth = width+1 -- for newline for the first line + local lwidth = width+1 -- force newline for the first line local wrap_overflow = options.wrap_overflow local align = options.align local sl = split_into_segments(cs) for i = 1, #sl do seg = sl[i] local seq = seg.seq + if not seg.bchar then + lwidth = width+1 --force newline + end if lwidth + seg.width <= width then local line = lines[#lines] - line.width = (line.width or 0) + seg.width + lwidth = (line.width or 0) + seg.width + line.width = lwidth if seg.bchar then line[#line+1] = seg.bchar end @@ -168,6 +172,7 @@ local function split_lines(cs, width, height, options) local line = seq line.width = seg.size[#line] lines[#lines+1] = line + lwidth = 0 else -- now we need to deal with segments longer than the given width if wrap_overflow then -- forcibly break the sequence @@ -182,7 +187,8 @@ local function split_lines(cs, width, height, options) j = j+1 end if l[1] then - l.width = st[j-1]-tw + lwidth = st[j-1]-tw + l.width = lwidth lines[#lines+1] = l end ci = j @@ -202,6 +208,7 @@ local function split_lines(cs, width, height, options) l.width = l.width + charwidth(seq[i]) end lines[#lines+1] = l + lwidth = l.width end end if #lines > nlines then @@ -280,12 +287,15 @@ local function render(str, ...) end if minetest then + local tilefmt = "advtrains_hud_bg.png^[resize:128x128^[colorize:#0000ff^(advtrains_hud_bg.png^[resize:128x64)^%s" + -- expected: Hello worldáéḯṍǘ + local sampletext = "Hello world\n\náe"..string.char(0xcc,0x81,0xc3,0xaf,0xcc,0x81,0x6f,0xcc,0x83,0xcc,0x81,0xc7,0x98) minetest.register_node("advtrains:textrender_demo",{ description = "Text rendering demo", - tiles = {"advtrains_hud_bg.png^[resize:128x128^[colorize:#0000ff^(advtrains_hud_bg.png^[resize:128x64)^"..render("Hello\n\n World", 128, 128, {color="#00ff00"})}, + tiles = {string.format(tilefmt, render(sampletext, 128, 128, {color="#00ff00"}))}, groups = {cracky=3, not_in_creative_inventory=1} }) - minetest.register_on_mods_loaded(function() + minetest.register_on_mods_loaded(function() -- wait until the trainhud module is loaded minetest.register_craft{ output = "advtrains:textrender_demo", recipe = { @@ -299,5 +309,6 @@ end return { mbstoucs = mbstoucs, + render = render, texture_escape = texture_escape, } \ No newline at end of file -- cgit v1.2.3