]> git.bts.cx Git - aseprite-tools.git/blob - export-p8-font-metrics.lua
Initial commit
[aseprite-tools.git] / export-p8-font-metrics.lua
1 local pluginKey = "bts/p8-font-metrics"
2
3 local sprite = app.sprite
4
5 local name = app.fs.fileTitle(sprite.filename)
6
7 local previousExportFilename = sprite.properties(pluginKey).previousExportFilename
8
9 if previousExportFilename == nil then
10         local defaultPath = app.fs.filePath(sprite.filename)
11         local defaultFilename = name..".lua"
12         previousExportFilename = app.fs.joinPath(defaultPath, defaultFilename)
13 end
14
15 local dlg = Dialog()
16 dlg:file{ id="metrics_filename", label="Metrics output:", filename=previousExportFilename, filetypes=".lua", save=true }
17 dlg:button{ id="confirm", text="Confirm" }
18 dlg:button{ id="cancel", text="Cancel" }
19 dlg:show()
20
21 local data = dlg.data
22 if data.confirm then
23         name = app.fs.fileTitle(data.metrics_filename)
24
25         local basePath = app.fs.filePath(data.metrics_filename)
26         local imagePath = app.fs.joinPath(basePath, name)
27         
28         local characterDetails = {}
29
30         for cy=0,15 do
31                 for cx=0,15 do
32                         local x,y=cx*8,cy*8
33                         local width = 8
34                         for i, slice in ipairs(sprite.slices) do
35                                 if slice.bounds.x == x and slice.bounds.y == y then
36                                         width = slice.bounds.width+1
37                                         break
38                                 end
39                         end
40                         table.insert(characterDetails, width)
41                 end
42         end
43         
44         if true then
45                 --local metricsFilename = app.fs.joinPath(metricsPath, data.metrics_filename)
46                 local metricsFile = io.open(data.metrics_filename, "w")
47         
48                 metricsFile:write(name.."Widths = split[[")
49                 for k,v in ipairs(characterDetails) do
50                         metricsFile:write(v .. ",")
51                 end
52                 metricsFile:write("]]\n")
53                 metricsFile:write("\n")
54
55                 io.close(metricsFile)
56         end
57
58         sprite.properties(pluginKey).previousExportFilename = data.metrics_filename
59 end