Capabilities file I/O from Lua scripts
[jpcrr.git] / lua / subtitles.lua
blobd38c74a9bb91e203db389251b4cc3d7b5146d2bb
1 do
2 local old_set_font_file = set_font_file;
3 local old_text_metrics = text_metrics;
4 local old_render_text = render_text;
7 dofile("textrender.lua");
9 local new_set_font_file = set_font_file;
10 local new_text_metrics = text_metrics;
11 local new_render_text = render_text;
13 font, err = io.open_arch_read("subtitle-font");
14 if not font then
15 error("Can't open font: " .. err);
16 end
17 new_set_font_file(font);
20 set_font_file = old_set_font_file;
21 text_metrics = old_text_metrics;
22 render_text = old_render_text;
24 tasvideos_subtitle_this_is_TAS = function()
25 return "This is a tool-assisted recording.\nFor details, visit http://TASVideos.org/";
26 end
28 tasvideos_subtitle_this_is_TAS_fast = function()
29 return "This was a tool-assisted recording.\nFor details, visit http://TASVideos.org/";
30 end
32 for_each_header = function(callback)
33 local headers = jpcrr.movie_headers();
34 local hdrnum, hdr, ret;
36 for hdrnum, hdr in ipairs(headers) do
37 ret = callback(hdr);
38 if(ret ~= nil) then
39 return ret;
40 end
41 end
42 end
44 get_gamename = function()
45 return for_each_header(function(header)
46 if header[1] == "GAMENAME" then
47 return header[2];
48 else
49 return nil;
50 end
51 end);
52 end
54 get_authors = function(long_form)
55 local ret = {};
56 local i;
58 for_each_header(function(header)
59 if header[1] == "AUTHORS" or header[1] == "AUTHORNICKS" then
60 for i = 2, #header do
61 ret[#ret + 1] = header[i];
62 end
63 elseif header[1] == "AUTHORFULL" then
64 if long_form then
65 ret[#ret + 1] = header[2];
66 else
67 ret[#ret + 1] = header[3];
68 end
69 end
70 end);
71 return ret;
72 end
74 get_long_authors = function()
75 return get_authors(true);
76 end
78 get_short_authors = function()
79 return get_authors(false);
80 end
82 format_runtime = function()
83 local length = jpcrr.movie_length();
84 local subseconds = length % 1000000000;
85 local seconds = (length - subseconds) / 1000000000;
86 subseconds = math.ceil(subseconds / 1000000);
87 local minutes = (seconds - seconds % 60) / 60;
88 seconds = seconds % 60;
89 local hours = (minutes - minutes % 60) / 60;
90 minutes = minutes % 60;
91 if hours > 0 then
92 return tostring(hours) .. ":" .. tostring(minutes) .. ":" .. tostring(seconds) .. "." .. tostring(subseconds);
93 else
94 return tostring(minutes) .. ":" .. tostring(seconds) .. "." .. tostring(subseconds);
95 end
96 end
98 subtitle_runinfo = function()
100 local w, h, x, y, wr, hr, old_w;
101 wr, hr = jpcrr.vga_resolution();
103 ret = (get_gamename() or "<unknown>") .. " in " .. format_runtime() .. "\n";
104 ret = ret .. "by ";
105 local k, v, rettmp;
106 for k, v in ipairs(get_short_authors()) do
107 if k > 1 then
108 ret = ret .. " & ";
110 rettmp = ret .. v;
111 old_w = w;
112 w, h = new_text_metrics(rettmp);
113 if w > 550 and w > old_w then
114 ret = ret .. "\n" .. v;
115 else
116 ret = rettmp;
119 ret = ret .. "\nRerecord count: " .. tostring(jpcrr.movie_rerecords());
120 return ret;
123 render_subtitle_text = function(text, to_top, fr, fg, fb, fa, br, bg, bb, ba)
125 local w, h, x, y, wr, hr;
127 w, h = new_text_metrics(text);
128 wr, hr = jpcrr.vga_resolution();
129 x = math.floor((wr - w) / 2);
130 if to_top then
131 y = 0;
132 else
133 y = hr - h;
136 new_render_text(3, x, y, text, false, fr, fg, fb, fa, br, bg, bb, ba);
142 --- Title in hh:mm:ss.ss
143 --- by PlayerName
144 --- Rerecord count: number