3 function string:split(sep, n)
4 local a, start = {}, 1;
7 local b, e = self:find(sep, start);
9 table.insert(a, self:sub(start));
12 a[#a+1] = self:sub(start, b - 1);
15 table.insert(a, self:sub(start));
22 function io.xopen(fn, mode)
24 if fn == nil then return io.stdin;
25 elseif fn == '-' then return (mode == 'r' and io.stdin) or io.stdout;
26 elseif fn:sub(-3) == '.gz' then return (mode == 'r' and io.popen('gzip -dc ' .. fn, 'r')) or io.popen('gzip > ' .. fn, 'w');
27 elseif fn:sub(-4) == '.bz2' then return (mode == 'r' and io.popen('bzip2 -dc ' .. fn, 'r')) or io.popen('bgzip2 > ' .. fn, 'w');
28 else return io.open(fn, mode) end
35 fp:write("/C { dup 255 and 255 div exch dup -8 bitshift 255 and 255 div 3 1 roll -16 bitshift 255 and 255 div 3 1 roll setrgbcolor } bind def\n")
36 fp:write("/L { 4 2 roll moveto lineto } bind def\n")
37 fp:write("/LX { dup 4 -1 roll exch moveto lineto } bind def\n")
38 fp:write("/LY { dup 4 -1 roll moveto exch lineto } bind def\n")
39 fp:write("/LS { 3 1 roll moveto show } bind def\n")
40 fp:write("/RS { dup stringwidth pop 4 -1 roll exch sub 3 -1 roll moveto show } bind def\n")
41 fp:write("/B { 4 copy 3 1 roll exch 6 2 roll 8 -2 roll moveto lineto lineto lineto closepath } bind def\n")
44 function eps.font(ft, size, fp)
46 fp:write(string.format('/FS %d def\n', size));
47 fp:write('/FS4 FS 4 div def\n');
48 fp:write('/' .. ft .. ' findfont FS scalefont setfont\n');
54 print("Usage: r2plot.lua <in.txt>");
58 local fp = io.xopen(arg[1]);
59 local n = tonumber(fp:read());
61 print('%!PS-Adobe-3.0 EPSF-3.0');
62 print('%%' .. string.format('BoundingBox: -%d -%d %.3f %.3f\n', 10*scale, scale, (n+1)*scale, (n+1)*scale));
63 print(string.format('%.3f setlinewidth', scale));
64 print(string.format('/plot { setgray moveto 0 %d rlineto } def', scale));
65 print(string.format('/plothalf { setgray moveto 0 %.2f rlineto } def', scale/2));
67 eps.font('Helvetica', scale-1);
70 for l in fp:lines() do
71 local t = l:split('\t');
72 print(string.format("%d %d FS4 add (%s) RS", (i-1)*scale-2, (i-1)*scale, t[1]));
74 if tonumber(t[j]) > 0.01 then
75 print(string.format('%.2f %.2f %.2f plot stroke', (i-1+.5)*scale, (j-2)*scale, 1.-t[j]));
81 print(string.format('%.2f %.2f %.2f plothalf stroke', -8*scale, (j-1) * scale/2, 1.-(j-1)/20));