Add built-in VGA font
[jpcrr.git] / lua / subtitles.lua
blob3c601174b58ae9a413cd71dd03dea9233501f497
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 local tostring2 = function(num)
83 if num < 10 then
84 return "0" .. tostring(num);
85 else
86 return tostring(num);
87 end
88 end
90 local tostring3 = function(num)
91 if num < 10 then
92 return "00" .. tostring(num);
93 elseif num < 100 then
94 return "0" .. tostring(num);
95 else
96 return tostring(num);
97 end
98 end
100 format_runtime = function()
101 local length = jpcrr.movie_length();
102 local subseconds = length % 1000000000;
103 local seconds = (length - subseconds) / 1000000000;
104 subseconds = math.ceil(subseconds / 1000000);
105 local minutes = (seconds - seconds % 60) / 60;
106 seconds = seconds % 60;
107 local hours = (minutes - minutes % 60) / 60;
108 minutes = minutes % 60;
109 if hours > 0 then
110 return tostring(hours) .. ":" .. tostring2(minutes) .. ":" .. tostring2(seconds) .. "." .. tostring3(subseconds);
111 else
112 return tostring2(minutes) .. ":" .. tostring2(seconds) .. "." .. tostring3(subseconds);
116 subtitle_runinfo = function()
118 local w, h, x, y, wr, hr, old_w;
119 wr, hr = jpcrr.vga_resolution();
121 ret = (get_gamename() or "<unknown>") .. " in " .. format_runtime() .. "\n";
122 ret = ret .. "by ";
123 local k, v, rettmp;
124 for k, v in ipairs(get_short_authors()) do
125 if k > 1 then
126 ret = ret .. " & ";
128 rettmp = ret .. v;
129 old_w = w;
130 w, h = new_text_metrics(rettmp);
131 if w > 550 and w > old_w then
132 ret = ret .. "\n" .. v;
133 else
134 ret = rettmp;
137 ret = ret .. "\nRerecord count: " .. tostring(jpcrr.movie_rerecords());
138 return ret;
141 render_subtitle_text = function(text, to_top, fr, fg, fb, fa, br, bg, bb, ba)
143 local w, h, x, y, wr, hr;
145 w, h = new_text_metrics(text);
146 wr, hr = jpcrr.vga_resolution();
147 x = math.floor((wr - w) / 2);
148 if to_top then
149 y = 0;
150 else
151 y = hr - h;
154 new_render_text(3, x, y, text, false, fr, fg, fb, fa, br, bg, bb, ba);
160 --- Title in hh:mm:ss.ss
161 --- by PlayerName
162 --- Rerecord count: number