| 1 |
--[[ Copyright (c) 2010 Peter "Corsix" Cawley |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
-- Creates a dat, tab and palette file from a single BMP image of multiple glyphs |
| 22 |
-- to be used for a font spritesheet |
| 23 |
-- bitmap_name: the target BMP image |
| 24 |
-- cell_width: width per character glyph |
| 25 |
-- cell_height: height per character glyph |
| 26 |
-- space_width: spacing between glyphs |
| 27 |
-- Output goes to the same location of the BMP image |
| 28 |
|
| 29 |
function makeFont(bitmap_name, cell_width, cell_height, space_width) |
| 30 |
if not (bitmap_name or cell_width or cell_height or space_width) then |
| 31 |
print("Usage: lua mkfont.lua <bitmap-file> <cell-width> <cell-height> ".. |
| 32 |
"<space-width>") |
| 33 |
print("Converts a bitmap which contains multiple glyphs (arranged in a ".. |
| 34 |
"grid) into a sprite sheet (.tab and .dat) and a palette (.pal).") |
| 35 |
return |
| 36 |
end |
| 37 |
|
| 38 |
package.path = (debug.getinfo(1, "S").source:match("@(.*[" .. package.config |
| 39 |
:sub(1, 1) .. "])") or "") .. "lib_" .. package.config:sub(5, 5) |
| 40 |
.. ".lua" .. package.config:sub(3, 3) .. package.path |
| 41 |
local bmp = require("lib_bmp") |
| 42 |
local spritesheet = require("lib_spritesheet") |
| 43 |
|
| 44 |
local bitmap_name, cell_width, cell_height, space_width = bitmap_name, cell_width, cell_height, space_width |
| 45 |
cell_width = assert(tonumber(cell_width), "cell width must be a number") |
| 46 |
cell_height = assert(tonumber(cell_height), "cell height must be a number") |
| 47 |
space_width = tonumber(space_width) or cell_width |
| 48 |
|
| 49 |
local bitmap = assert(bmp.open(bitmap_name)) |
| 50 |
local ncells_x = bitmap.width / cell_width |
| 51 |
assert(ncells_x % 1 == 0, "Bitmap width must be a multiple of the cell width") |
| 52 |
local ncells_y = bitmap.height / cell_height |
| 53 |
assert(ncells_y % 1 == 0, "Bitmap height must be a multiple of the cell height") |
| 54 |
|
| 55 |
local filename_base = bitmap_name:match"^(.*%.)[^.]*$" |
| 56 |
local ss = spritesheet.open(filename_base .. "tab", filename_base .. "dat", true) |
| 57 |
|
| 58 |
local pal = assert(io.open(filename_base .. "pal", "wb")) |
| 59 |
pal:write(bitmap.palette) |
| 60 |
pal:close() |
| 61 |
|
| 62 |
spritesheet.writeDummy(ss) |
| 63 |
for y = 0, ncells_y - 1 do |
| 64 |
for x = 0, ncells_x - 1 do |
| 65 |
if x == 0 and y == 0 then |
| 66 |
spritesheet.write(ss, space_width, 1, bmp.getSubPixels(bitmap, 0, 0, space_width, 1)) |
| 67 |
else |
| 68 |
local x, y = x * cell_width, y * cell_height |
| 69 |
local w, h = cell_width, cell_height |
| 70 |
while h > 1 do |
| 71 |
local is_empty = true |
| 72 |
for d = 0, w - 1 do |
| 73 |
if bmp.getPixel(bitmap, x + d, y + h - 1) ~= "\255" then |
| 74 |
is_empty = false |
| 75 |
end |
| 76 |
end |
| 77 |
if is_empty then |
| 78 |
h = h - 1 |
| 79 |
else |
| 80 |
break |
| 81 |
end |
| 82 |
end |
| 83 |
while w > 1 do |
| 84 |
local is_empty = true |
| 85 |
for d = 0, h - 1 do |
| 86 |
if bmp.getPixel(bitmap, x + w - 1, y + d) ~= "\255" then |
| 87 |
is_empty = false |
| 88 |
end |
| 89 |
end |
| 90 |
if is_empty then |
| 91 |
w = w - 1 |
| 92 |
else |
| 93 |
break |
| 94 |
end |
| 95 |
end |
| 96 |
if w == 1 and h == 1 and bmp.getPixel(bitmap, x, y) == "\255" then |
| 97 |
spritesheet.writeDummy(ss) |
| 98 |
else |
| 99 |
spritesheet.write(ss, w, cell_height, bmp.getSubPixels(bitmap, x, y, w, cell_height)) |
| 100 |
end |
| 101 |
end |
| 102 |
end |
| 103 |
end |
| 104 |
spritesheet.close(ss) |
| 105 |
end |