NB. PDF Formatter for J Plot
NB.
NB.*plotpdf v main formatter
NB. [width height] plotpdf filename
NB.
NB. istallation
NB. copy to any folder
NB.
NB. loading
NB. require '<folder>/plotpdf.ijs'
NB.
NB. locale
NB. plotpdf_jdplot_ filename NB. from demo plot locale
NB. plotpdf__obj filename NB. from plot object
NB.
NB. running
NB. plot ?20#20 NB. any plot
NB. plotpdf 't.pdf' NB. from default plot locale
NB.
NB. pd ... NB. plot driver
NB. plotpdf 't.pdf' NB. from current locale
NB.
NB. test_pplotpdf_'' NB. batch test
NB.
NB. compatibily
NB. j406,j406 ce,j501,j501 ce
NB.
NB. author
NB. Oleg Kobchenko - 12/08/2002
require 'files strings format plot trig'
coclass'pplotpdf'
DEFAULTWH=: 240 300
TEST=: 0 : 0 NB. samples
'type area' plot (?10#10),:?10#10
'type bar' plot (?10#10),:?10#10
'type dot' plot (?100+i.60),:?<.^.4+i.100
'type hilo' plot (+/\1-?100#3),:(+/\1-?100#3)
'type line' plot ?10#10
'type linefit' plot (+/\1-?10#3);(+/\1-?10#3)
'type fbar' plot 10+(+/\1-?10#3),(+/\1-?10#3),(+/\1-?10#3),:(+/\1-?10#3)
'type sbar' plot 10+(+/\1-?10#3),(+/\1-?10#3),(+/\1-?10#3),:(+/\1-?10#3)
'type radar' plot |:6+(+/\1-?6#3),(+/\1-?6#3),:(+/\1-?6#3)
'type pie' plot 10 20 30 40
'type density' plot ?1+(,|.)"1(,|.)i.2#8
'type point' plot (?100+i.60),:?<.^.4+i.100
'type stick' plot (+/\1-?100#3);(+/\1-?100#3)
'type surface' plot 10+?1+(,|.)"1(,|.)i.2#8
'type wire' plot 10+?1+(,|.)"1(,|.)i.2#8
)
test=: 3 : 0
t=. <;._2 TEST
for_i. i.#t do.
try.
p=. ''''((E.#i.@#@]){.])1}.>i{t
if. -.1 e.'linefit'E.p do.
p=.'''title ',p,';' else. p=.'''' end.
". p,1}.>i{t
plotpdf_jwplot_ 'test',(,'z2'fmt i),'.pdf'
catch. smoutput p end.
end.
)
SC=: 1
getfontsz=: {.@:(#~ ~:&_1)@:(_1&".&>)@;:
citemize=: ,:^:(2: > #@$)
cexpand=: #^:(> #)
csc=:((($@] $ [) * ])&.> _1&{)`(_1:)`]}"_ 1
pdfcnt=: 3 : 0
y.=.toHOST y.
r=.'<< /Length ',(":#y.),' >>',CRLF
r,'stream',CRLF,y.,'endstream',CRLF
)
plotcnt=: 3 : 0
r=. ''
for_i. y. do.
r=. r,(('pdf',(>2{i))~i),LF
end.
)
makepdf=: 3 : 0
z=. ":1+#y.
r=. '%PDF-1.3',CRLF,'%',(171 167 182 187{a.),CRLF
s=. 'xref',CRLF,'0 ',z,CRLF
s=. s,(10#'0'),' 65535 f',CRLF
for_i. i.#y. do.
s=. s,(,'z10' fmt #r),' 00000 n',CRLF
c=. CRLF,(toCRLF>i{y.)
r=. r,(":1+i),' 0 obj',c,'endobj',CRLF
end.
tr=. toCRLF TR rplc '{z}';z
r,s,tr,(":#r),CRLF,'%%EOF'
)
plotpdf=: 3 : 0
DEFAULTWH plotpdf y.
:
PlotBase=:(0=4!:0<'Plot__PlotBase'){'jwplot';PlotBase
SC=:{:x.%1000
P=. (<x.%1000)csc Plot__PlotBase
r20=. R20 rplc'{d}';,'z4,5rz2'fmt 6{.6!:0''
r30=. R30 rplc'{x y}';":x.
r50=. pdfcnt plotcnt P
c=. R10;r20;r30;R40;r50
r=. makepdf c
r fwrite y.
)
PlotBase=:<'jwplot'
plotpdf_z_=: 3 : 0
PlotBase_pplotpdf_=:18!:5''
plotpdf_pplotpdf_ y.
:
PlotBase_pplotpdf_=:18!:5''
x. plotpdf_pplotpdf_ y.
)
pdfbox=: 3 : 0
'n m t w d s f c'=.y.
'% box ',":c
)
pdfrect=: 3 : 0
'n m t w d s f c'=.y.
z=. s >.&:(#@:citemize) f
s=. z cexpand citemize s
f=. z cexpand citemize f
c=. ,:@,^:(z<2)c
r=.'0 J 0 j ',(":SC*w),' w ',LF
for_i. i.z do.
r=.r,(,":(i{s)%255),' rg ',(,":(i{f)%255),' RG',LF
r=.r,,(":_4]\i{c),"1 ' re B',LF
end.
)
pdfpoly=: pdfline=: 3 : 0
'n m t w d s f c'=.y.
c=.citemize c
s=.(#c) cexpand citemize s
f=.(#c) cexpand citemize f
r=.'0 J 1 j ',(,":SC*2*w),' w '
if. t-:'line' do.f=.s end.
for_i. i.#c do.
r=.r,(,":(i{s)%255),' rg ',(,":(i{f)%255),' RG '
r=.r,(":2{.l=.i{c),' m '
r=.r,,(":_2]\2}.l),"1 ' l '
r=.r,((t-:'poly'){'SB'),LF
end.
)
pdfarc=: pdfpie=: 3 : 0
'n m t w d s f c'=.y.
z=. s >.&:(#@:citemize) f
s=. z cexpand citemize s
f=. z cexpand citemize f
c=. ,:@,^:(z<2)c
r=.'0 J 0 j ',(":SC*w),' w ',LF
for_i. i.#c do.
r=.r,(,":(i{s)%255),' rg ',(,":(i{f)%255),' RG',LF
z=.i{c
p=.bezierarc z
if. t-:'pie' do.
r=.r,(":center z),' m '
r=.r,(":2{.{.p),' l ',LF
else.
r=.r,(":2{.{.p),' m '
end.
r=.r,,(":2}."1 p),"1 ' c ',LF
r=.r,'b',LF
end.
)
center=: 2&{. + -:@(2 3&{)
angle2=: [: r.^:_1 *@(j./)"1
bezierarc=: 3 : 0
'x y w h'=.4{.y.
z=. center y.
't1 t2'=.2p1([|+)angle2 z-"1~(4+i.2 2){y.
th=. 6%~ t2(>.-<.)t1
'c c1 s s1'=. (cos , sin) th,t1
x0=.x1=.1 [ y0=.0
'x3 y3'=. c,s
x2=.3%~ (8*cos-:th)-x0+x3+3*x1
y2=.y3-(-x2-x3)%-tan th
y1=.3%~ (8*sin-:th)-y0+y3+3*y2
r=.(x0,x1,x2,x3),:y0,y1,y2,y3
r=.r +/ . *~ (c1,-s1),:s1,c1
r=.r (+/ . *)^:(i.6)~ (c,-s),:s,c
,@|:"2 z+"2(-:w,h)*"2 r
)
pdfcircle=: pdfdot=: 3 : 0
'n m t w d s f c'=.y.
c=.citemize c
r=.'1 J 1 j ',(":SC*(t-:'dot'){d,6),' w ',LF
r=.r,(,":s%255),' RG ',(,":s%255),' rg ',LF
for_i. i.#c do.
r=.r,,(":0 0,"1~_2]\i{c),"1 ' re B',LF
end.
)
pdftext=: 3 : 0
'n m t w d s f c'=.y.
r=. (,":s%255),' RG ',(,":s%255),' rg '
z=. 0.125*<.0.5+8*SC*|getfontsz d
r=.r,'/F1 ',(":z),' Tf '
if. 1=#$c do.
c=.,:c
w=.,<w
end.
x=.z*#@,&>w
if.t-:'textr'do.x=.0.6*x
elseif.t-:'textc'do.x=.0.25*x
elseif. 1 do.x=.0 end.
c=.c-"1]x,.z
for_i. i.#c do.
r=.r,'BT ',(":i{c),' Td ('
r=.r,(,>i{w), ') Tj ET '
end.
)
pdftextr=:pdftextc=:pdftext
R10=: 0 : 0
<< /Type /Catalog /Pages 3 0 R >>
)
R20=: 0 : 0
<< /Type /Info /Creator (plot for J)
/Producer (plotpdf by Oleg Kobchenko)
/Title (Plot) /Subject (Visualization)
/Author ()
/CreationDate (D:{d}) >>
)
R30=: 0 : 0
<< /Type /Pages
/MediaBox [0 0 {x y}] /Kids [4 0 R] /Count 1
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 << /Type /Font /Subtype /Type1
/Name /F1 /BaseFont /Helvetica >> >>
>> >>
)
R40=: 0 : 0
<< /Type /Page /Parent 3 0 R /Contents 5 0 R >>
)
TR=: 0 : 0
trailer
<< /Size {z}
/Root 1 0 R /Info 2 0 R >>
startxref
)