NB. write package
NB. built from project: ~Source\packages\print\write\write
NB. write z definitions

script_z_ '~system\packages\print\print.ijs'

coclass 'jprint'


NB.*startprint      v initialize print job
NB.*endprint        v end print job
NB.*startpage       v start page
NB.*endpage         v end page
NB.*write           v write text (see syntax below)
NB.*drawbox         v draw box (outline or solid)
NB.*drawline        v draw line
NB.*drawgraph       v draw graph (from wmf file)
NB.*makeframe       v make a frame
NB.*preview         v preview page

startprint_z_=: startprint_jprint_
endprint_z_=: endprint_jprint_
startpage_z_=: startpage_jprint_
endpage_z_=: endpage_jprint_
write_z_=: write_jprint_
drawbox_z_=: drawbox_jprint_
drawline_z_=: drawline_jprint_
drawgraph_z_=: drawgraph_jprint_
makeframe_z_=: makeframe_jprint_
preview_z_=: preview_jprint_

NB. writefns.ijs        - utilities for user-defined printing

endprint=: wd bind 'pclose' @ glzenddoc
startpage=: glclear @ glzstartpage
endpage=: glzendpage @ glzprint @ glshow

NB. =========================================================
NB. drawbox         opt drawbox frame
NB.
NB. opt has up to 3 values:
NB.   0{opt       pensize (default 10 twips)
NB.   1{opt       0=border only (default)  1=solid
NB.   2{opt       RGB color number (white) - only used if solid
drawbox=: 3 : 0
'' drawbox y.
:
'x y w h'=. getframe y.
'size solid clr'=. 3{.x.,(#x.)}.10 0 16777215
if. solid do.
  glbrush glrgb 256 256 256#:clr
  glrect rnd0 x,y,w,h
else.
  glpen size,0
  gllines rnd0 x,y,x,(y+h),(x+w),(y+h),(x+w),y,x,y
end.
)

NB. =========================================================
NB. drawgraph         wmf_file drawgraph frame
drawgraph=: 4 : 0
glplayxywh getframe y.
glplay x.
)

NB. =========================================================
NB. drawline      opt drawline frame
NB.
NB. opt is pensize (default 10 twips)
drawline=: 3 : 0
10 drawline y.
:
x0=. getpos 2{.y.
x1=. getpos 2}.y.
glpen x.,0
gllines rnd0 x0,x1
)

NB. =========================================================
NB. makeframe      - outerframe makeframe innerframe
NB.
NB. makes a new frame relative to the page edges,
NB. where innerframe is relative to outerframe,
NB. and outerframe is relative to the page edges.
NB.
NB. if only topleft position is given, the frame goes to the
NB. right and bottom edges
NB.
NB. e.g.   1 1 4 3 makeframe 0.5 0.25 2 _0.25
NB.     1.5 1.25 3 2.75
makeframe=: 4 : 0
'px py pw ph'=. x.
'x y w h'=. 4{.y.
'sw sh'=. 1440%~ (<1;2 3){PRINTPAGE
if. pw <: 0 do. pw=. sw + pw - px end.
if. ph <: 0 do. ph=. sh + ph - py end.
if. w <: 0 do. w=. pw + w - x end.
if. h <: 0 do. h=. ph + h - y end.
(x+px),(y+py),w,h
)

NB. =========================================================
NB. [orientation] startprint [job [;printfile]]
NB.
NB.   startprint ''             - initialize printer
NB.   startprint jobname        - with jobname (default: print)
NB.   startprint jobname;file   - gives output file
NB.
NB. orientation is 1=portrait (default) 2=landscape
startprint=: 3 : 0
1 startprint y.
:
'job file'=. y.,(#y.)}.'print';''
if. -.(<'print') e. <;._2 wd 'qp' do.
  wd 'pc print closeok'
  vga=. 640 >: {. 0 ". wd 'qm'
  wd 'xywh 0 0 ',": vga { 2 2$250 180 210 150
  wd 'cc p0 isigraph rightmove bottommove'
  wd 'pas 0 0;pmove ',(":vga{130 90),' 5 -1 -1'
else.
  wd 'psel print'
end.

glmap MM_TWIPS
glzcreate''
glzdevmode 'orientation ',":x.
getprintpage''
glzstartdoc EAV,job,EAV,EAV,file,EAV
)

NB. =========================================================
NB. write     - opt write dat
write=: 3 : 0
'' write y.
:
'just frame font txt'=. y.
'lines paras foreclr backclr'=. x.,(#x.)}.1.2 0.5 0,WHITE

xywh=. getframe frame
setfont font2twips font
paras=. paras*{.glqtextmetrics''

NB. character matrix:
if. (2=3!:0 txt) *. 2=#$txt do.
  dtb=. #~ +./\.@(' '&~:)
  txt=. < <@dtb"1 txt
  'xywh txt'=. place just;xywh;'';foreclr;backclr;lines;txt
  (1 getframe xywh);>txt
  return.
end.

NB. cut character string into paras:
if. 2=3!:0 txt do. txt=. cutpara txt end.

while. #txt do.

  bit=. 0 pick txt
  txt=. }.txt
  lf=. +/*./\.bit=LF
  bit=. (2{xywh) wrappara (-lf)}.bit
  'xywh bit'=. place just;xywh;'';foreclr;backclr;lines;bit
  if. #bit do.
    bit=. < (}: ; ,&'  ' each bit),lf # LF
    0 0 0 0;<bit,txt return.
  end.

  xywh=. xywh - 0 0 0, paras * lf

end.

(1 getframe xywh);''
)
