mapprint.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. ###############################################################
  2. # mapprint.tcl - GRASS GIS Manager procedures for postscript and
  3. # lpr printing, and pdf and eps output
  4. # January 2006 Michael Barton, Arizona State University
  5. # COPYRIGHT: (C) 1999 - 2006 by the GRASS Development Team
  6. #
  7. # This program is free software under the GNU General Public
  8. # License (>=v2). Read the file COPYING that comes with GRASS
  9. # for details.
  10. #
  11. ##########################################################################
  12. namespace eval psprint {
  13. variable docht
  14. variable docwd
  15. variable epsfile
  16. variable format
  17. variable gsdevices
  18. variable gsexists
  19. variable gspresent
  20. variable gsstate
  21. variable ldevice
  22. variable mbottom
  23. variable mleft
  24. variable mright
  25. variable mtop
  26. variable orient
  27. variable paper
  28. variable paper_preset
  29. variable pdffile
  30. variable pght
  31. variable pgwd
  32. variable printer
  33. variable printmode
  34. variable res
  35. variable tmppngfile
  36. variable tmpppmfile
  37. variable tmppsfile
  38. variable tmpscript
  39. variable PPap
  40. variable PVar
  41. variable PView
  42. variable PWid
  43. global array can # mon
  44. }
  45. #initialize variables
  46. proc psprint::init { } {
  47. variable pgwd
  48. variable pght
  49. variable docwd
  50. variable docht
  51. variable paper
  52. variable paper_preset
  53. variable printmode
  54. variable printer
  55. variable gsexists
  56. variable orient
  57. variable mleft
  58. variable mright
  59. variable mtop
  60. variable mbottom
  61. variable gsstate
  62. variable ldevice
  63. variable gsdevices
  64. variable res
  65. global mon
  66. global mingw
  67. set psprint::pgwd 8.5
  68. set psprint::pght 11
  69. set psprint::docwd 7.5
  70. set psprint::docht 10
  71. set psprint::paper "preset"
  72. set psprint::paper_preset "letter"
  73. set psprint::printer ""
  74. set psprint::gsexists 1
  75. set psprint::orient "landscape"
  76. set psprint::res 300
  77. set psprint::mleft 1
  78. set psprint::mright 1
  79. set psprint::mtop 1
  80. set psprint::mbottom 1
  81. # default is lpr printing
  82. set printmode "lpr"
  83. # check for ghostscript
  84. # switch to native windows version of ghostscript if runing wingrass
  85. if { $mingw == 1 } {
  86. set cmd "gswin32c"
  87. } else {
  88. set cmd "gs"
  89. }
  90. #enable additional printing options if Ghostscript available
  91. if {![catch {set input [exec $cmd -help]} error]} {
  92. regexp ".*Available devices:(.*)Search path:" $input string gsdevices
  93. set gsstate "normal"
  94. regsub -all { } $gsdevices { } gsdevices
  95. regsub -all { } $gsdevices \n gsdevices
  96. regsub -all \n\n $gsdevices \n gsdevices
  97. } else {
  98. set gsdevices "none available"
  99. set gsstate "disabled"
  100. #set printmode "eps"
  101. tk_messageBox -type ok -icon error -message [G_msg "Ghostscript not available"]
  102. }
  103. }
  104. # calculate paper size and document size on paper (all in inches)
  105. proc psprint::paper { } {
  106. variable paper
  107. variable paper_preset
  108. variable printmode
  109. variable pgwd
  110. variable pght
  111. variable mleft
  112. variable mright
  113. variable mtop
  114. variable mbottom
  115. variable docwd
  116. variable docht
  117. variable orient
  118. # set paper dimensions
  119. if { $paper == "preset" } {
  120. switch $paper_preset {
  121. "11x17" {
  122. set pgwd 11
  123. set pght 17
  124. }
  125. "ledger" {
  126. set pgwd 17
  127. set pght 11
  128. }
  129. "legal" {
  130. set pgwd 8.5
  131. set pght 14
  132. }
  133. "letter" {
  134. set pgwd 8.5
  135. set pght 11
  136. }
  137. "a0" {
  138. set pgwd 33.0556
  139. set pght 46.7778
  140. }
  141. "a1" {
  142. set pgwd 23.3889
  143. set pght 33.055
  144. }
  145. "a2" {
  146. set pgwd 16.5278
  147. set pght 23.3889
  148. }
  149. "a3" {
  150. set pgwd 11.6944
  151. set pght 16.5278
  152. }
  153. "a4" {
  154. set pgwd 8.26389
  155. set pght 11.6944
  156. }
  157. }
  158. }
  159. set docwd [expr $pgwd - $mright - $mleft]
  160. set docht [expr $pght - $mtop - $mbottom]
  161. if { $orient == "landscape" && $printmode == "pdf" } {
  162. set docwd [expr $docwd + 2]
  163. set docht [expr $docht + 2]
  164. } else {
  165. set docwd [expr $docwd + 1]
  166. set docht [expr $docht + 1]
  167. }
  168. update
  169. }
  170. # initialize tmpfiles for poscript printing
  171. proc psprint::init_tmpfiles { } {
  172. variable tmpscript
  173. variable tmppsfile
  174. variable tmppngfile
  175. # get temporary file for postscript printing
  176. set pid [ pid ]
  177. if {[catch {set tmppsfile [ exec g.tempfile pid=$pid ]} error]} {
  178. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  179. }
  180. append tmppsfile ".ps"
  181. set pid [ pid ]
  182. if {[catch {set tmppngfile [ exec g.tempfile pid=$pid ]} error]} {
  183. GmLib::errmsg $error [G_msg "Error creating tempfile"]
  184. }
  185. append tmppngfile ".png"
  186. }
  187. # show gs printer devices in output window
  188. proc psprint::show_devices { } {
  189. variable gsdevices
  190. set ah [monitor_annotation_start {} "Ghostscript Output Devices" {}]
  191. monitor_annotate $ah $gsdevices
  192. }
  193. # create printer options window
  194. proc psprint::window { cm cv cx cy } {
  195. variable pgwd
  196. variable pght
  197. variable paper
  198. variable paper_preset
  199. variable printmode
  200. variable printer
  201. variable gsexists
  202. variable epsfile
  203. variable pdffile
  204. variable orient
  205. variable res
  206. variable pgwd
  207. variable pght
  208. variable mleft
  209. variable mright
  210. variable mtop
  211. variable mbottom
  212. variable gspresent
  213. variable ldevice
  214. variable gsdevices
  215. variable gsstate
  216. global mon
  217. set mon $cm
  218. # check if opened
  219. if { [winfo exists .printwin] } {
  220. wm deiconify .printwin
  221. raise .printwin
  222. return
  223. }
  224. set PW [toplevel .printwin]
  225. wm title $PW [G_msg "Postscript and LPR printing of map display"]
  226. # Left part paper + output
  227. set PWid(left) [ frame $PW.left -padx 5 -pady 5]
  228. pack $PWid(left) -side left -anchor w
  229. # paper size, scale
  230. set PWid(paper) [ frame $PWid(left).paper]
  231. pack $PWid(paper) -side top -anchor w
  232. # preset paper sizes (from ghostscript)
  233. set row [ frame $PWid(paper).row1 ]
  234. radiobutton $row.a -variable psprint::paper -value "preset" \
  235. -highlightthickness 0
  236. Label $row.b -anchor w -text [G_msg "Preset paper type"]
  237. ComboBox $row.c -label "" -width 20 -textvariable psprint::paper_preset \
  238. -values {"letter" "a4" "legal" "11x17" "a3" "ledger" "a0" "a1" "a2" } \
  239. -modifycmd psprint::paper
  240. pack $row.a $row.b $row.c -side left;
  241. pack $row -side top -fill x -expand no -anchor n
  242. # custom paper sizes
  243. set row [ frame $PWid(paper).row2 ]
  244. radiobutton $row.a -variable psprint::paper -value "custom" \
  245. -highlightthickness 0
  246. Label $row.b -anchor w -text [G_msg "Custom paper size"]
  247. Label $row.c -anchor w -text [G_msg "width:"]
  248. Entry $row.d -width 10 -textvariable psprint::pgwd
  249. Label $row.e -anchor w -text [G_msg " height:"]
  250. Entry $row.f -width 10 -textvariable psprint::pght
  251. pack $row.a $row.b $row.c $row.d $row.e $row.f -side left;
  252. pack $row -side top -fill x -expand no -anchor n
  253. #margins
  254. set row [ frame $PWid(paper).row3]
  255. Label $row.a -anchor w -text [G_msg "Margins left:"]
  256. Entry $row.b -width 10 -textvariable psprint::mleft
  257. Label $row.c -anchor w -text [G_msg " right:"]
  258. Entry $row.d -width 10 -textvariable psprint::mright
  259. Label $row.e -anchor w -text [G_msg " top:"]
  260. Entry $row.f -width 10 -textvariable psprint::mtop
  261. Label $row.g -anchor w -text [G_msg " bottom:"]
  262. Entry $row.h -width 10 -textvariable psprint::mbottom
  263. pack $row.a $row.b $row.c $row.d $row.e $row.f $row.g $row.h -side left;
  264. pack $row -side top -fill x -expand no -anchor n
  265. # portrait or landscape
  266. set row [ frame $PWid(paper).row4 ]
  267. LabelEntry $row.a -label [G_msg "Resolution (dpi) for printing and PDF "] \
  268. -textvariable psprint::res -width 4
  269. Label $row.b -anchor w -text " "
  270. radiobutton $row.c -variable psprint::orient -value "landscape" \
  271. -text "landscape mode" -highlightthickness 0
  272. radiobutton $row.d -variable psprint::orient -value "portrait" \
  273. -text "portrait mode " -highlightthickness 0
  274. pack $row.a $row.b $row.c $row.d -side left;
  275. pack $row -side top -fill x -expand no -anchor n
  276. # output options
  277. set PWid(output) [ frame $PWid(left).output ]
  278. pack $PWid(output) -side top -anchor w
  279. # LPR printer
  280. set row [ frame $PWid(output).lpr ]
  281. radiobutton $row.a -variable psprint::printmode -value "lpr" \
  282. -highlightthickness 0
  283. Label $row.b -anchor w -text [G_msg "Print on LPR printer"]
  284. pack $row.a $row.b -side left;
  285. pack $row -side top -fill x -expand no -anchor n
  286. # Postscript printer
  287. set row [ frame $PWid(output).psprinter ]
  288. radiobutton $row.a -variable psprint::printmode -value "psprint" \
  289. -state $psprint::gsstate -highlightthickness 0
  290. Label $row.b -anchor w -text [G_msg "Print on postscript device* "] \
  291. -state $psprint::gsstate
  292. ComboBox $row.c -width 20 -textvariable psprint::printer \
  293. -values $psprint::gsdevices -editable 0 -entrybg white
  294. pack $row.a $row.b $row.c -side left;
  295. pack $row -side top -fill x -expand no -anchor n
  296. # PDF file
  297. set row [ frame $PWid(output).pdffile]
  298. radiobutton $row.a -variable psprint::printmode -value "pdf" \
  299. -state $psprint::gsstate -highlightthickness 0
  300. Label $row.b -anchor w -text [G_msg "Save to PDF file* "] \
  301. -state $psprint::gsstate
  302. Entry $row.c -width 30 -textvariable psprint::pdffile -state $gsstate
  303. Button $row.d -text [G_msg "Browse"] -command { set psprint::pdffile \
  304. [tk_getSaveFile -title "Output PDF file" -defaultextension ".pdf"]} \
  305. -state $psprint::gsstate
  306. pack $row.a $row.b $row.c $row.d -side left;
  307. pack $row -side top -fill x -expand no -anchor n
  308. # EPS file
  309. set row [ frame $PWid(output).epsfile ]
  310. radiobutton $row.a -variable psprint::printmode -value "eps" \
  311. -highlightthickness 0
  312. Label $row.b -anchor w -text [G_msg "Save to EPS file "]
  313. Entry $row.c -width 30 -textvariable psprint::epsfile
  314. Button $row.d -text [G_msg "Browse"] -command { set psprint::epsfile \
  315. [ tk_getSaveFile -title "Output EPS file" -defaultextension ".eps"] }
  316. pack $row.a $row.b $row.c $row.d -side left;
  317. pack $row -side top -fill x -expand no -anchor n
  318. set row [ frame $PWid(output).gsmessage ]
  319. Label $row.a -anchor w -text [G_msg "*requires ghostscript to be installed and in path"]
  320. pack $row.a -side bottom;
  321. pack $row -side top -fill x -expand yes -anchor center
  322. # Buttons
  323. set but [ frame $PWid(left).buttons ]
  324. pack $but -side top
  325. Button $but.print -text [G_msg "Print"] -command "update; psprint::print $cv"
  326. Button $but.close -text [G_msg "Close"] -command { destroy .printwin }
  327. pack $but.print $but.close -side left
  328. }
  329. proc psprint::print { cv } {
  330. variable paper
  331. variable paper_preset
  332. variable printmode
  333. variable printer
  334. variable gsexists
  335. variable res
  336. variable format
  337. variable orient
  338. variable epsfile
  339. variable pdffile
  340. variable tmppsfile
  341. variable tmppngfile
  342. variable pgwd
  343. variable pght
  344. variable docwd
  345. variable docht
  346. variable mright
  347. variable mleft
  348. variable mtop
  349. variable mbottom
  350. global gmpath
  351. global mon
  352. global mingw
  353. psprint::init_tmpfiles
  354. psprint::paper
  355. update
  356. set landscape $gmpath/landscap.ps
  357. #change doc size to points
  358. set cdocwd [expr $docwd * 72]
  359. set cdocht [expr $docht * 72]
  360. # set paper size for postscript printer and pdf files
  361. set w [expr round($pgwd * $res)]
  362. set h [expr round($pght * $res)]
  363. set format "-g$w"
  364. append format "x$h"
  365. # switch to native windows version of ghostscript if runing wingrass
  366. if { $mingw == 1 } {
  367. set cmd "gswin32c"
  368. } else {
  369. set cmd "gs"
  370. }
  371. if { $orient == "portrait" } {
  372. $cv postscript -pageheight $cdocht -pagewidth $cdocwd \
  373. -file $tmppsfile
  374. } else {
  375. $cv postscript -rotate 1 -pageheight $cdocht -pagewidth $cdocwd \
  376. -file $tmppsfile
  377. }
  378. after 500
  379. # lpr printing
  380. if { $printmode == "lpr" } {
  381. if {[catch {exec lpr -o position=center $tmppsfile } error]} {
  382. GmLib::errmsg $error
  383. }
  384. }
  385. # postsript printing via ghostsript
  386. if { $printmode == "psprint" && $printer != "" } {
  387. if {[catch {exec $cmd $format -sDEVICE#$printer -r$res -sNOPAUSE -dBATCH -- $tmppsfile} error]} {
  388. GmLib::errmsg $error
  389. }
  390. }
  391. # output to pdf file via ghostscript
  392. if { $printmode == "pdf" && $pdffile != "" } {
  393. if {[catch {exec $cmd $format -sDEVICE#pdfwrite -r$res -sNOPAUSE -sOutputFile#$pdffile -dBATCH -- $tmppsfile} error]} {
  394. GmLib::errmsg $error
  395. }
  396. }
  397. # output to eps file
  398. if { $printmode == "eps" && $epsfile != "" } {
  399. if { $orient == "portrait" } {
  400. $cv postscript -file "$epsfile"
  401. } else {
  402. $cv postscript -file "$epsfile" -rotate 1
  403. }
  404. }
  405. psprint::clean
  406. }
  407. proc psprint::set_option { key value } {
  408. variable PWid
  409. variable PVar
  410. variable PPap
  411. variable PView
  412. set PVar($key) $value
  413. }
  414. # Delete temporary files
  415. proc psprint::clean { } {
  416. variable tmppsfile
  417. variable tmppngfile
  418. catch {file delete $tmppsfile}
  419. catch {file delete $tmppngfile}
  420. }