phpDocumentor Aprint
[ Back ] [ class tree: Aprint ] [ index: Aprint ] [ all elements ]

Source for file psprint.php

Documentation is available at psprint.php

  1. <?php
  2. /**
  3. * PSPrint.php
  4. *
  5. * Simple class for printing text (PostScript version)
  6. * @author Andrioli Darvin
  7. * @version $Header: d:\cvs/classistd/aprint/psprint.php,v 1.4 2005/02/13 21:02:39 Darvin Exp $
  8. *
  9. * @copyright
  10. * Copyright (C) 2005 Andrioli Darvin <darvin@andrioli.com>
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2 of the License, or (at your option) any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27.  
  28. require_once('aprint.php');
  29.  
  30. /**
  31. * Paper Orientation
  32. * @const AP_LANDSCAPE
  33. * @access public
  34. */
  35. define('AP_LANDSCAPE',0);
  36. /**
  37. * Paper Orientation
  38. * @const AP_PORTRAIT
  39. * @access public
  40. */
  41. define('AP_PORTRAIT',1);
  42.  
  43. /**
  44. *
  45. *
  46. */
  47. class PSPrint extends APrint {
  48.  
  49. /**
  50. * File Postscript to write
  51. *
  52. * @var string
  53. */
  54. var $FileName;
  55. /**
  56. * Document handler
  57. *
  58. * @var resource
  59. */
  60. var $hDoc;
  61. /**
  62. * Paper orientation, supported only by ps module
  63. * @var integer
  64. */
  65. var $Orientation;
  66. /**
  67. * Is the page size set?
  68. * @var boolean
  69. */
  70. var $IsSizeSet;
  71. /**
  72. * Space between two rows
  73. * @var integer
  74. */
  75. var $Interlinea;
  76. /**
  77. * Path to fonts file
  78. * @var array
  79. */
  80. var $FontPath;
  81. /**
  82. * Initialize this class. It does not attempt to
  83. * open the output file
  84. * This class will perform this job at the first time that you send any text
  85. * @param string $TitleText document title
  86. * @param string $ScriptId script identification
  87. * @access public
  88. */
  89. function PSPrint($TitleText="",$ScriptId="") {
  90. APrint::APrint($TitleText,$ScriptId);
  91. $this->FileName="";
  92. $this->IsSizeSet=false;
  93. $this->CurrentFont=AP_NORMAL_FONT;
  94.  
  95. // Margins default settings
  96. $this->MarginTop=400;
  97. $this->MarginBottom=400;
  98. $this->MarginLeft=40;
  99. $this->MarginRight=40;
  100. $this->Interlinea=5;
  101.  
  102. $this->FontPath=array('.');
  103. }
  104.  
  105. /**
  106. * The Aprint module take the page size from the printer. Here the user must provide the
  107. * page size
  108. * Unit used: twip,
  109. * 1 inch = 1440 twip
  110. * 1 mm = 56.7 twip
  111. * @access private
  112. * @see SetPageSize()
  113. * @see SetCustomPageSize()
  114. */
  115. function _DefinePageSize()
  116. {
  117. }
  118.  
  119. /**
  120. * The page size. This function accept the well known page forms
  121. * LEGAL,LETTER, A3,A4,A5, B4, B5
  122. * @param string
  123. * @access public
  124. * @see SetCustomPageSize()
  125. */
  126. function SetPageSize($PageType='A4',$orientation)
  127. {
  128. switch($PageType)
  129. {
  130. case 'LETTER':
  131. $this->PaperDimX=8.5*1440;
  132. $this->PaperDimY=11*1440;
  133. break;
  134. case 'LEGAL':
  135. $this->PaperDimX=8.5*1440;
  136. $this->PaperDimY=14*1440;
  137. break;
  138. case 'A3':
  139. $this->PaperDimX=297*56.7;
  140. $this->PaperDimY=420*56.7;
  141. break;
  142. case 'A4':
  143. $this->PaperDimX=8*1440;
  144. $this->PaperDimY=11.69*1440;
  145. break;
  146. case 'A5':
  147. $this->PaperDimX=148*56.7;
  148. $this->PaperDimY=210*56.7;
  149. break;
  150. case 'B4':
  151. $this->PaperDimX=250*56.7;
  152. $this->PaperDimY=354*56.7;
  153. break;
  154. case 'B5':
  155. $this->PaperDimX=182*56.7;
  156. $this->PaperDimY=257*56.7;
  157. break;
  158. default: // Unknown format, use A4
  159. $this->PaperDimX=8.27*1440;
  160. $this->PaperDimY=11.69*1440;
  161. break;
  162. }
  163. $this->Orientation=$orientation;
  164. $this->IsSizeSet=true;
  165. $this->_CheckMarginValue();
  166. }
  167.  
  168. /**
  169. * Set custom page size. The dimension must be points.
  170. * 1 inch = 1440 points
  171. * 1 mm = 56.7 twip
  172. * @param integer
  173. * @param integer
  174. * @param integer
  175. * @see AP_PORTRAIT
  176. * @see AP_LANDSCAPE
  177. * @access public
  178. */
  179. function SetCustomPageSize($DimX,$DimY,$Orientation)
  180. {
  181. $this->PaperDimX=$DimX;
  182. $this->PaperDimY=$DimY;
  183. $this->IsSizeSet=true;
  184. $this->Orientation=$Orientation;
  185. $this->_CheckMarginValue();
  186. }
  187.  
  188. /**
  189. * Useful function to setup all margins with one function
  190. * use -1 to skip the parameter
  191. *
  192. * @param integer $top top margin size
  193. * @param integer $bottom bottom margin size
  194. * @param integer $left left margiclass
  195. * @param integer $right right margin size
  196. * @access public
  197. */
  198.  
  199. function SetMargin($top,$bottom=-1,$left=-1,$right=-1)
  200. {
  201. APrint::SetMargin($top,$bottom,$left,$right);
  202. $this->row=$this->MarginTop;
  203. }
  204.  
  205. /**
  206. * Set the size for the top margin
  207. * @param integer
  208. * @access public
  209. */
  210. function SetTopMargin($value)
  211. {
  212. APrint::SetTopMargin($value);
  213. $this->row=$this->MarginTop;
  214. }
  215. /**
  216. * Check the value for the margin. They should be >0 and
  217. * less then the page size. The PSPrint version, before to call the APrint method
  218. * check whether the page size is set.
  219. *
  220. * @access private
  221. */
  222. function _CheckMarginValue()
  223. {
  224. if(!$this->IsSizeSet)
  225. trigger_error('Set the page size before to set the margins size',E_USER_ERROR);
  226. APrint::_CheckMarginValue();
  227. }
  228. /**
  229. * Specify which printer I'll use. PSPrint does not use any printer, but
  230. * write its content into file, therefore this function is meaningless. Call
  231. * SetFileName instead. It exists to avoid to broke the interface.
  232. * @param string $Name printer name as defined in Window
  233. * @access public $Name
  234. * @return void
  235. * @see SetFileName()
  236. */
  237. function SetPrinter($Name)
  238. {
  239. APrint::SetPrinter($Name);
  240. $this->FileName=$Name;
  241. }
  242.  
  243. /**
  244. * Set the name of the file to write
  245. * @param string $Name
  246. * @access public
  247. * @return void
  248. */
  249. function SetFileName($Name)
  250. {
  251. APrint::SetPrinter($Name);
  252. $this->FileName=$Name;
  253. }
  254.  
  255. /**
  256. * printout the text
  257. * @param string $text text to printout
  258. * @param integer $hFont Handle to the font to use. Handle returned by CreateFont. If set to -1
  259. * or not provided, it use the last used font.
  260. * @param integer $align text alignment
  261. * @param integer $col Orizontal position where start to print the text. This value
  262. * will be added to the left margin.
  263. * @access public
  264. * @see CreateFont()
  265. * @see TextFooter()
  266. */
  267. function Text($text,$hFont=-1,$align=AP_LEFT,$col=0)
  268. {
  269. if(!$this->IsSizeSet)
  270. trigger_error('Set the page size before write some text',E_USER_ERROR);
  271. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  272. // Select new font?
  273. if($hFont!=-1)
  274. {
  275. $this->_SetFont($hFont);
  276. }
  277. $size=ps_string_geometry($this->hDoc,$text);
  278. $txtHeight=abs($size['descender'])+$size['ascender'];
  279. if($txtHeight>0)
  280. $this->FontH=$this->Conv2Twip_y($txtHeight+$this->Interlinea);
  281. $textWidth=$this->Conv2Twip_x($size['width']);
  282. $StartCol=$this->_BeginCol($align,$textWidth,$col);
  283. $this->row+=$this->FontH;
  284. if($this->row>($this->PaperDimY-$this->MarginBottom-300-$this->FontH)&&$this->AutoNewPage)
  285. {
  286. $this->NewPage();
  287. $this->row+=$this->FontH;
  288. }
  289. ps_show_xy($this->hDoc,$text,$this->ResConv_x($StartCol),$this->_VPoint($this->ResConv_y($this->row)));
  290. }
  291.  
  292. /**
  293. * printout the text at the exact position requested
  294. * @param integer orizontal position
  295. * @param integer vertical position where print the text
  296. * @param string $text text to printout
  297. * @param integer $hFont Handle to the font to use. Handle returned by CreateFont
  298. * @access public
  299. * @see CreateFont()
  300. * @see Text()
  301. */
  302. function TextXY($Xpos,$Ypos,$text,$hFont=-1)
  303. {
  304. if(!$this->IsSizeSet)
  305. trigger_error('Set the page size before write some text',E_USER_ERROR);
  306. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  307. // Select new font?
  308. if($hFont!=-1)
  309. {
  310. $this->_SetFont($hFont);
  311. }
  312. $size=ps_string_geometry($this->hDoc,$text);
  313. $txtHeight=abs($size['descender'])+$size['ascender'];
  314. if($txtHeight>0)
  315. $this->FontH=$this->Conv2Twip_y($txtHeight+$this->Interlinea);
  316. $textWidth=$this->Conv2Twip_y($size['width']);
  317. $this->row=$Ypos+$this->FontH;
  318. if($this->row>($this->PaperDimY-$this->MarginBottom-300-$this->FontH)&&$this->AutoNewPage)
  319. {
  320. $this->NewPage();
  321. $this->row=$Ypos+$this->FontH;
  322. }
  323. ps_show_xy($this->hDoc,$text,$this->ResConv_x($Xpos),$this->_VPoint($this->ResConv_y($Ypos)));
  324. }
  325.  
  326. /**
  327. * Print the page header
  328. * @access public
  329. * @see Text()
  330. */
  331. function PrintPageHeader()
  332. {
  333. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  334. $OriginalFont=-1;
  335. foreach($this->NewPageHeader as $Value)
  336. {
  337. $Text=$Value['Text'];
  338. $hFont=$Value['Font'];
  339. if($hFont!=-1 && $hFont!="")
  340. {
  341. $oldFont=$this->_SetFont($hFont);
  342. }
  343. if($OriginalFont<0) $OriginalFont=$oldFont;
  344. $size=ps_string_geometry($this->hDoc,$Text);
  345. $txtHeight=$this->Conv2Twip_y(abs($size['descender'])+$size['ascender']+$this->Interlinea);
  346. if($txtHeight>0)
  347. $FontH=$txtHeight;
  348. $textWidth=$this->Conv2Twip_x($size['width']);
  349.  
  350. $StartCol=$this->_BeginCol(AP_LEFT,$textWidth,0);
  351. $this->row+=$FontH;
  352. ps_show_xy($this->hDoc,$Text,$this->ResConv_x($StartCol),
  353. $this->_VPoint($this->ResConv_y($this->row)));
  354. } // Loop on each row
  355.  
  356. if($OriginalFont>-1)
  357. $this->_SetFont($OriginalFont);
  358. }
  359.  
  360.  
  361. /**
  362. * Print the given text as page footer
  363. * @param string $text text to printout
  364. * @param integer $hFont Handle to the font to use. Handle returned by CreateFont
  365. * @param integer $align text alignment
  366. * @param integer $col Orizontal position where start to print the text. This value
  367. * will be added to the left margin.
  368. * @access public
  369. * @see CreateFont()
  370. * @see Text()
  371. */
  372. function TextFooter($text,$hFont=-1,$align=AP_LEFT,$col=0)
  373. {
  374. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  375. // Select new font?
  376. if($hFont!=-1)
  377. {
  378. $oldFont=$this->_SetFont($hFont);
  379. }
  380. else
  381. {
  382. $oldFont=$this->_SetFont(AP_FOOTER_FONT);
  383. }
  384. $size=ps_string_geometry($this->hDoc,$text);
  385. $txtHeight=$this->Conv2Twip_y(abs($size['descender'])+$size['ascender']+$this->Interlinea);
  386. $FontH=$txtHeight;
  387. $textWidth=$this->Conv2Twip_x($size['width']);
  388. $StartCol=$this->_BeginCol($align,$textWidth,$col);
  389. ps_show_xy($this->hDoc,$text,$this->ResConv_x($StartCol),
  390. $this->_VPoint($this->ResConv_y($this->PaperDimY-$this->MarginBottom)));
  391. ps_end_page($this->hDoc);
  392. $this->_SetFont($oldFont);
  393. }
  394.  
  395. /**
  396. * Close the output.
  397. * @access public
  398. */
  399. function run()
  400. {
  401. $this->_FooterNote();
  402. ps_end_page($this->hDoc);
  403. ps_close($this->hDoc);
  404. ps_delete($this->hDoc);
  405. }
  406.  
  407. /**
  408. * Close the current page and start the new one.
  409. *
  410. * @access public
  411. */
  412. function NewPage()
  413. {
  414. if(!$this->IsSizeSet)
  415. trigger_error('Set the page size before start the new page',E_USER_ERROR);
  416. if(!$this->ConnectionOpened)
  417. trigger_error("Open the connection to the printer before run the function NewPage",E_USER_ERROR);
  418. // run the callback, if set
  419. if(array_key_exists('newpage',$this->CallBacks))
  420. call_user_func_array($this->CallBacks['newpage'],$this->CallBacksParm['newpage']);
  421.  
  422. if($this->NPag&&$this->PrintPageFooter) {
  423. $this->_FooterNote();
  424. }
  425. if($this->NPag) {
  426. ps_end_page($this->hDoc);
  427. }
  428. $this->NPag++;
  429. ps_begin_page($this->hDoc,$this->ResConv_x($this->PaperDimX),$this->ResConv_y($this->PaperDimY));
  430. // enable the last used font on the current page
  431. $this->_SetFont();
  432. $this->row=$this->MarginTop;
  433. $this->PrintPageHeader();
  434. }
  435.  
  436. /**
  437. * Printout page number and script name as footer
  438. *
  439. * @access private
  440. */
  441. function _FooterNote()
  442. {
  443. $oldFont=$this->_SetFont(AP_FOOTER_FONT);
  444. $size=ps_string_geometry($this->hDoc,$this->ScriptId);
  445. $txtHeight=$this->Conv2Twip_y($size['descender']+$size['ascender']+$this->Interlinea);
  446. ps_show_xy($this->hDoc,$this->ScriptId,
  447. $this->ResConv_x($this->MarginLeft),
  448. $this->_VPoint($this->ResConv_y($this->PaperDimY-$this->MarginBottom)));
  449. $size=ps_string_geometry($this->hDoc,$this->NPag);
  450. // $txtHeight=$size['descender']+$size['ascender']*$this->Res_Y;
  451. $width=$this->Conv2Twip_x($size['width']);
  452. ps_show_xy($this->hDoc,$this->NPag,
  453. $this->ResConv_x($this->PaperDimX-$this->MarginRight-$width),
  454. $this->_VPoint($this->ResConv_y($this->PaperDimY-$this->MarginBottom)));
  455. $this->_SetFont($oldFont);
  456. }
  457.  
  458. /**
  459. * Create new font
  460. *
  461. * @param string $fName Font Name. Set the path to the font file. The ps extension
  462. * should take the path from the function set_pathname, but
  463. * on my system it doesn't work (ps 1.3.0 + pslib 0.2.5). So I must
  464. * provide here the full pathname (absolute or relative) to the font file
  465. * @param integer $fHeight Font height, default: 336
  466. * @param integer $fWidth Font width, default: 168 (PsPrint unused)
  467. * @param integer $fWeight Font weight, default: 400. 0 -> thin, 800 -> bold (PsPrint unused)
  468. * @param boolean $fItalic Italic script? True/False (PsPrint unused)
  469. * @param boolean $fUnderline Text with underline? True/False
  470. * @param boolean $fStrikeout True/False (PsPrint unused)
  471. * @param integer $fOrientation Should be always 3 digits, i.e 020. See printer_create_font in the note to the manual (PsPrint unused)
  472. * @see _SetFont()
  473. * @see Text()
  474. * @see SetFontPath()
  475. * @access public
  476. *
  477. */
  478. function CreateFont($fName="Courier",$fHeight=240,$fWidth=120,$fWeight=400,$fItalic=false,$fUnderline=false,$fStrikeout=false,$fOrientaton=0)
  479. {
  480. if(!$this->ConnectionOpened)
  481. trigger_error("Open the output file before define the font",E_USER_ERROR);
  482. $LastFontId=count($this->FontTable);
  483. $this->FontTable[$LastFontId]['fontName']=$fName;
  484. $this->FontTable[$LastFontId]['fontHeight']=ceil($fHeight/20);
  485. $this->FontTable[$LastFontId]['fontWidth']=ceil($fWidth/20);
  486. $this->FontTable[$LastFontId]['fontWeight']=$fWeight;
  487. $this->FontTable[$LastFontId]['fontItalic']=$fItalic;
  488. $this->FontTable[$LastFontId]['fontUnderline']=$fUnderline;
  489. $this->FontTable[$LastFontId]['fontStrikeout']=$fStrikeout;
  490. $this->FontTable[$LastFontId]['fontOrientaton']=$fOrientaton;
  491. // if(!$this->FontTable[$LastFontId]['fontHandler'] = ps_findfont($this->hDoc, $this->FontTable[$LastFontId]['fontName'], "builtin", 1))
  492. if(!$this->FontTable[$LastFontId]['fontHandler'] = ps_findfont($this->hDoc, $this->FontTable[$LastFontId]['fontName'], "", 0))
  493.  
  494. trigger_error("Could not find font.\n",E_USER_ERROR);
  495. return($LastFontId);
  496. }
  497.  
  498. /**
  499. * Open the output printer (APrint) or the output file (PSPrint)
  500. * @access private
  501. * @see OpenFileName()
  502. */
  503. function _OpenPrinter()
  504. {
  505. if($this->FileName=="")
  506. trigger_error('Set the filename to open',E_USER_ERROR);
  507. else
  508. {
  509. $this->hDoc=ps_new();
  510. if (!ps_open_file($this->hDoc, $this->FileName))
  511. trigger_error('Error open file '.$this->FileName,E_USER_ERROR);
  512. }
  513. if($this->Orientation==AP_PORTRAIT)
  514. ps_set_info($this->hDoc, "Orientation", "Portrait");
  515. else
  516. ps_set_info($this->hDoc, "Orientation", "Landscape");
  517. $this->ConnectionOpened=true;
  518. $this->_DefinePageSize(); // inizialize all parameter relate the margins, and page size
  519. $this->Res_X=72;
  520. $this->Res_Y=72;
  521. // set the font path. It must be done after the creation of the new document
  522. for($i=0;$i<count($this->FontPath);$i++)
  523. ps_set_parameter($this->hDoc,'SearchPath',$this->FontPath[$i]);
  524. $this->_DefaultFont();
  525. $this->NewPage();
  526. $this->_SetFont(AP_NORMAL_FONT);
  527. }
  528.  
  529.  
  530. /**
  531. * Start the connection to the printer. This is the public interface
  532. * to _OpenPrinter.
  533. * @param string $FileName Name of the printer to use. If not set use the system default
  534. * @access public
  535. * @see _OpenPrinter()
  536. */
  537. function OpenFileName($FileName="")
  538. {
  539. if($FileName!="") $this->SetPrinter($FileName);
  540. $this->_OpenPrinter();
  541. }
  542.  
  543. /**
  544. * Activate the specified font
  545. *
  546. * @param int $fnt font handle returned by CreateFont
  547. * @access private
  548. * @see CreateFont()
  549. */
  550. function _SetFont($fnt=-1)
  551. {
  552. if($fnt==-1)
  553. $fntHandler=$this->CurrentFont;
  554. else
  555. $fntHandler=$fnt;
  556. assert('array_key_exists($fntHandler,$this->FontTable)');
  557. ps_set_parameter($this->hDoc, "underline", $this->FontTable[$fntHandler]['fontUnderline']);
  558. //ps_set_parameter($ps, "strikeout", $this->FontTable[$LastFontId]['fontStrikeout']);
  559. ps_setfont($this->hDoc, $this->FontTable[$fntHandler]['fontHandler'], $this->FontTable[$fntHandler]['fontHeight']);
  560. $oldFont=$this->CurrentFont;
  561. $this->CurrentFont=$fntHandler;
  562. return($oldFont);
  563. }
  564.  
  565. /**
  566. * Set the path where the font file .afm are located. The Postscript module require
  567. * Notice: if the ps module is not created, the class store the information until it will be created
  568. * and then set the given path.
  569. * The ps extension should take the path from the function set_pathname, but
  570. * on my system it doesn't work (ps 1.3.0 + pslib 0.2.5). So I must
  571. * provide here the full pathname (absolute or relative) to the font file as parameter to
  572. * CreateFont()
  573. * @access public
  574. * @param string $Path
  575. * @see CreateFont()
  576. */
  577. function SetFontPath($Path)
  578. {
  579. $this->FontPath[]=$Path;
  580. if($this->ConnectionOpened)
  581. ps_set_parameter($this->hDoc,'SearchPath',$Path);
  582. }
  583.  
  584. /**
  585. * Define some dafault font
  586. * @access private
  587. */
  588. function _DefaultFont()
  589. {
  590. // create predefined font. Don't change the row order
  591. $this->CreateFont("Dustismo",320,8,400,false,false,false,0);
  592. $this->CreateFont("Dustismo",320,8,800,false,false,false,0);
  593. $this->CreateFont("Dustismo",240,6,200,false,false,false,0);
  594. }
  595.  
  596. /**
  597. * The ps module set the point zero to the bottom edge of the page, the
  598. * module has its point zero on the top edge, the function converts the
  599. * vertical value from both systems.
  600. * @param integer $PosY Vertical position (Aprint system)
  601. * @return integer Vertical position (Ps system)
  602. * @access private
  603. */
  604. function _VPoint($PosY)
  605. {
  606. $SizeY=$this->ResConv_y($this->PaperDimY);
  607. if($PosY>$SizeY)
  608. trigger_error('Vertical position out of the page size',E_USER_ERROR);
  609. return($SizeY-$PosY);
  610. }
  611.  
  612. } // end class PSPrint
  613. ?>

Documentation generated on Sun, 13 Feb 2005 22:02:55 +0100 by phpDocumentor 1.3.0RC3