2023-03-17 03:39:41 +01:00
|
|
|
|
#!/usr/bin/env lua5.1
|
|
|
|
|
-- -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
dofile("init.lua")
|
2023-09-04 18:48:24 +02:00
|
|
|
|
dofile("../tga_encoder/init.lua")
|
2023-03-18 22:33:18 +01:00
|
|
|
|
|
2023-03-19 00:42:05 +01:00
|
|
|
|
font_1 = unicode_text.hexfont(
|
|
|
|
|
{
|
|
|
|
|
kerning = true,
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-03-18 22:33:18 +01:00
|
|
|
|
font_1:load_glyphs(
|
2023-09-04 19:18:38 +02:00
|
|
|
|
io.lines("unifont.hex")
|
2023-03-18 22:33:18 +01:00
|
|
|
|
)
|
2023-03-19 00:42:05 +01:00
|
|
|
|
font_1:load_glyphs(
|
|
|
|
|
io.lines("U+FFFD.hex")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
-- The following text purposely contains glyphs not in unifont.hex, to
|
|
|
|
|
-- show that these are rendered like U+FFFD REPLACEMENT CHARACTER (<28>).
|
2023-03-20 00:53:52 +01:00
|
|
|
|
local text = "ABC... 123!!! (*ˊᗜˋ*)/ᵗᑋᵃᐢᵏ ᵞᵒᵘ* 😁\
|
2023-03-17 03:39:41 +01:00
|
|
|
|
\
|
2023-03-19 00:42:05 +01:00
|
|
|
|
U+0077 w LATIN SMALL LETTER W\
|
|
|
|
|
U+00F0 ð LATIN SMALL LETTER ETH\
|
|
|
|
|
U+2665 ♥ BLACK HEART SUIT\
|
|
|
|
|
U+10348 𐍈 GOTHIC LETTER HWAIR"
|
2023-03-18 22:33:18 +01:00
|
|
|
|
local pixels = font_1:render_text(text)
|
2023-03-17 03:39:41 +01:00
|
|
|
|
local image = tga_encoder.image(pixels)
|
|
|
|
|
image:save("test.tga")
|
|
|
|
|
|
2023-03-18 22:33:18 +01:00
|
|
|
|
font_2 = unicode_text.hexfont(
|
|
|
|
|
{
|
2023-03-19 00:42:05 +01:00
|
|
|
|
background_color = { 1 },
|
|
|
|
|
foreground_color = { 0 },
|
2023-03-18 22:33:18 +01:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
font_2:load_glyphs(
|
2023-09-04 19:18:38 +02:00
|
|
|
|
io.lines("unifont.hex")
|
2023-03-18 22:33:18 +01:00
|
|
|
|
)
|
|
|
|
|
font_2:load_glyphs(
|
|
|
|
|
io.lines("unifont_upper.hex")
|
|
|
|
|
)
|
2023-03-17 03:39:41 +01:00
|
|
|
|
local file = io.open("UTF-8-demo.txt")
|
2023-03-19 00:42:05 +01:00
|
|
|
|
|
|
|
|
|
-- encode image with colormap (palette)
|
2023-03-17 03:39:41 +01:00
|
|
|
|
tga_encoder.image(
|
2023-03-18 22:33:18 +01:00
|
|
|
|
font_2:render_text(
|
2023-03-17 03:39:41 +01:00
|
|
|
|
file:read("*all")
|
|
|
|
|
)
|
2023-03-19 00:42:05 +01:00
|
|
|
|
):save(
|
|
|
|
|
"UTF-8-demo.tga",
|
|
|
|
|
{
|
|
|
|
|
colormap = {
|
|
|
|
|
{ 0xFF, 0xFF, 0xFF },
|
|
|
|
|
{ 0x00, 0x00, 0xFF },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
file:close()
|
|
|
|
|
|
|
|
|
|
font_2.background_color = { 0, 0, 0 }
|
|
|
|
|
font_2.foreground_color = { 255, 255, 255 }
|
|
|
|
|
font_2.tabulator_size = 4 * 8
|
|
|
|
|
local file = io.open("example.txt")
|
|
|
|
|
local text = file:read("*all")
|
2023-03-17 03:39:41 +01:00
|
|
|
|
file:close()
|
2023-03-19 00:42:05 +01:00
|
|
|
|
local pixels = font_2:render_text(text)
|
|
|
|
|
|
|
|
|
|
-- colorize text pixels
|
|
|
|
|
for h = 1, #pixels do
|
|
|
|
|
for w = 1, #pixels[h] do
|
|
|
|
|
local pixel = pixels[h][w]
|
|
|
|
|
if (
|
|
|
|
|
255 == pixel[1] and
|
|
|
|
|
255 == pixel[2] and
|
|
|
|
|
255 == pixel[3]
|
|
|
|
|
) then
|
|
|
|
|
pixel = {
|
|
|
|
|
( h + w ) % 128 + 128,
|
|
|
|
|
( h % w ) % 128 + 128,
|
|
|
|
|
( h - w ) % 128 + 128,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
pixels[h][w] = pixel
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- encode image with 15-bit colors
|
|
|
|
|
tga_encoder.image(pixels):save(
|
|
|
|
|
"example.tga",
|
|
|
|
|
{
|
|
|
|
|
color_format="A1R5G5B5",
|
|
|
|
|
compression = "RAW",
|
|
|
|
|
}
|
|
|
|
|
)
|