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

Source for file aprint.php

Documentation is available at aprint.php

  1. <?php
  2. /**
  3. * APrint.php
  4. *
  5. * Simple class for printing text
  6. *
  7. * $Header: d:\cvs/classistd/aprint/aprint.php,v 1.15 2005/02/13 20:29:29 Darvin Exp $
  8. *
  9. * @author Andrioli Darvin
  10. * @version 1.4.0
  11. *
  12. * @copyright
  13. * Copyright (C) 2003-2005 Andrioli Darvin <darvin@andrioli.com>
  14. *
  15. * This library is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU Lesser General Public
  17. * License as published by the Free Software Foundation; either
  18. * version 2 of the License, or (at your option) any later version.
  19. *
  20. * This library is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  23. * Lesser General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public
  26. * License along with this library; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30.  
  31. /**
  32. * Print using courier new, with predefined size, weight 400 (normal)
  33. * @const AP_NORMAL_FONT
  34. * @access public
  35. */
  36. define('AP_NORMAL_FONT',0); // print using NORMAL FONT
  37. /**
  38. * Print using courier new, with predefined size, weight 800 (bold)
  39. * @const AP_BOLD_FONT
  40. * @access public
  41. */
  42. define('AP_BOLD_FONT',1); // print using bold font
  43. /**
  44. * Small font used to print the page's footer
  45. * @const AP_FOOTER_FONT
  46. * @access public
  47. */
  48. define('AP_FOOTER_FONT',2);
  49. /**
  50. * left text align.
  51. * @const AP_LEFT
  52. * @access public
  53. * @see text()
  54. */
  55. define('AP_LEFT',0);
  56. /**
  57. * center text align.
  58. * @const AP_CENTER
  59. * @access public
  60. * @see text()
  61. */
  62. define('AP_CENTER',1);
  63. /**
  64. * right text align.
  65. * @const AP_RIGHT
  66. * @access public
  67. * @see text()
  68. */
  69. define('AP_RIGHT',2);
  70.  
  71. /**
  72. * class APrint
  73. *
  74. */
  75. class APrint {
  76.  
  77. /**
  78. * Selected printer (Windows name)
  79. *
  80. * @var string $PrinterName
  81. */
  82. var $PrinterName;
  83. /**
  84. * I've already established a connection to the printer?
  85. * (TRUE/FALSE)
  86. * @var bool $ConnectionOpened
  87. */
  88. var $ConnectionOpened;
  89. /**
  90. * printer handler
  91. *
  92. * @var resource
  93. */
  94. var $hPrinter;
  95.  
  96. /**
  97. * Current row
  98. * @var integer
  99. */
  100. var $row;
  101.  
  102. /**
  103. * Orizontal resolution
  104. * @var integer
  105. */
  106. var $Res_X;
  107. /**
  108. * Vertical resolution
  109. * @var integer
  110. */
  111. var $Res_Y;
  112.  
  113. /**
  114. * Value for the font height
  115. * @var integer
  116. */
  117. var $FontH;
  118. /**
  119. * Value for the font width
  120. *
  121. */
  122. var $FontW;
  123. /**
  124. * Page counter
  125. *
  126. */
  127. var $NPag;
  128.  
  129. /**
  130. * handler of current selected font
  131. */
  132. var $CurrentFont;
  133. /**
  134. * Paper hight
  135. *
  136. */
  137. var $PaperDimX;
  138. /**
  139. * Paper width
  140. *
  141. */
  142. var $PaperDimY;
  143. /**
  144. * Margins, size of the top margin
  145. */
  146. var $MarginTop;
  147. /**
  148. * Margins, size of the bottom margin
  149. */
  150. var $MarginBottom;
  151. /**
  152. * Margins, size of the left margin
  153. */
  154. var $MarginLeft;
  155. /**
  156. * Margins, size of the right margin
  157. */
  158. var $MarginRight;
  159.  
  160. /**
  161. * array with all created font
  162. * @var array $FontTable
  163. * @see CreateFont()
  164. */
  165. var $FontTable;
  166.  
  167. /**
  168. * Job name
  169. * @var string $JobTitle
  170. * @see _OpenPrinter()
  171. */
  172. var $JobTitle;
  173.  
  174. var $ScriptId;
  175. /**
  176. * Callbacks array. Ths index holdsall callback defined.
  177. * Each index map to a particular function that supports the callback
  178. * The supported call back are:
  179. * 'newpage' -> Call the function each time just before execute the
  180. * 'NewPage' function code
  181. *
  182. * @var array
  183. * @see NewPage()
  184. * @see SetCallBack();
  185. * @see $CallBacksParm
  186. */
  187. var $CallBacks;
  188. /**
  189. * Parameters for the callbacks functions
  190. * @var array
  191. * @see $CallBacks
  192. */
  193. var $CallBacksParm;
  194. /**
  195. * Array holding the supported callbacks
  196. * @var array
  197. * @see SetCallBack()
  198. */
  199. var $ValidCallbacks=array('newpage');
  200. /**
  201. * Should this module print its own page footer. If set to
  202. * false, you may supply your custom footer using TextFooter
  203. * and 'newpage' callback
  204. * @var boolean
  205. * @see NewPage()
  206. * @see TextFooter()
  207. * @see SetPrintFooter()
  208. */
  209. var $PrintPageFooter=TRUE;
  210. /**
  211. * Array holding the supported unit of measure used by movement
  212. * function.
  213. * @var array
  214. * @see MvRelativePos()
  215. * @see MvAbsolutePos()
  216. */
  217. var $ValidMeasure=array('row','mm','inch','twip');
  218. /**
  219. * Array containing texts to print as header into each page
  220. * Also, into the array are stored the font id request to print the text
  221. * You may set different font to each row.
  222. * @var array
  223. * @see SetNewPageHeader()
  224. */
  225. var $NewPageHeader;
  226. /**
  227. * @var boolean
  228. * @see SetAutoNewPage()
  229. */
  230. var $AutoNewPage;
  231. /**
  232. * Initialize this class. It doesn not attempt to
  233. * establish the connection to the printer
  234. * This class will perform this job at the first time that you send any text
  235. * @param string $TitleText document title
  236. * @param string $ScriptId script identification
  237. * @access public
  238. */
  239. function APrint($TitleText="",$ScriptId="") {
  240. $this->PrinterName=""; // If no choice, use the default printer
  241. $this->ConnectionOpened=false;
  242. $this->row=10;
  243. $this->NPag=0;
  244. // Margins default settings
  245. $this->MarginTop=400;
  246. $this->MarginBottom=400;
  247. $this->MarginLeft=40;
  248. $this->MarginRight=40;
  249. // Start with an empty array
  250. $this->FontTable=array();
  251. // Initial value for paper size. I'll set the correct value after I'll open the connection
  252. // to the printer
  253. $this->PaperDimX=-1;
  254. $this->PaperDimY=-1;
  255. $this->JobTitle=$TitleText;
  256. $this->ScriptId=$ScriptId;
  257. $this->CallBacks=array();
  258. $this->CallBacksParm=array();
  259. $this->NewPageHeader=array();
  260. $this->SetAutoNewPage(TRUE);
  261. }
  262. /**
  263. * inizialize all parameter about the page size
  264. * Require a printer handler.
  265. * Unit used: twip,
  266. * 1 inch = 1440 twip
  267. * 1 mm = 56.7 twip
  268. * @access private
  269. */
  270. function _DefinePageSize()
  271. {
  272. $PaperType=printer_get_option($this->hPrinter, PRINTER_PAPER_FORMAT);
  273. switch($PaperType)
  274. {
  275. case PRINTER_FORMAT_CUSTOM:
  276. // custom paper width in mm
  277. $this->PaperDimX=printer_get_option($this->hPrinter, PRINTER_PAPER_WIDTH)*56.7;
  278. $this->PaperDimY=printer_get_option($this->hPrinter, PRINTER_PAPER_LENGTH)*56.7;
  279. break;
  280. case PRINTER_FORMAT_LETTER:
  281. $this->PaperDimX=8.5*1440;
  282. $this->PaperDimY=11*1440;
  283. break;
  284. case PRINTER_FORMAT_LEGAL:
  285. $this->PaperDimX=8.5*1440;
  286. $this->PaperDimY=14*1440;
  287. break;
  288. case PRINTER_FORMAT_A3:
  289. $this->PaperDimX=297*56.7;
  290. $this->PaperDimY=420*56.7;
  291. break;
  292. case PRINTER_FORMAT_A4:
  293. $this->PaperDimX=8*1440;
  294. $this->PaperDimY=11.69*1440;
  295. break;
  296. case PRINTER_FORMAT_A5:
  297. $this->PaperDimX=148*56.7;
  298. $this->PaperDimY=210*56.7;
  299. break;
  300. case PRINTER_FORMAT_B4:
  301. $this->PaperDimX=250*56.7;
  302. $this->PaperDimY=354*56.7;
  303. break;
  304. case PRINTER_FORMAT_B5:
  305. $this->PaperDimX=182*56.7;
  306. $this->PaperDimY=257*56.7;
  307. break;
  308. case PRINTER_FORMAT_FOLIO:
  309. $this->PaperDimX=8.5*1440;
  310. $this->PaperDimY=13*1440;
  311. break;
  312.  
  313. default:
  314. $this->PaperDimX=8.27*1440;
  315. $this->PaperDimY=11.69*1440;
  316. break;
  317. // trigger_error("Paper format type:".$PaperType." not supported",E_USER_ERROR);
  318. }
  319. // Now that I know the size of the page, I check tha value for margins
  320. $this->_CheckMarginValue();
  321. }
  322.  
  323. /**
  324. * Check the value for the margin. They should be >0 and
  325. * less then the page size
  326. *
  327. * @access private
  328. */
  329. function _CheckMarginValue()
  330. {
  331. if($this->MarginTop<0)
  332. trigger_error("Top margin is < 0. It should be greater or equal to 0",E_USER_ERROR);
  333. if($this->PaperDimY>0 && $this->MarginTop>$this->PaperDimY)
  334. trigger_error("Top margin bigger then paper size",E_USER_ERROR);
  335. if($this->MarginBottom<0)
  336. trigger_error("Bottom margin is < 0. It should be greater or equal to 0",E_USER_ERROR);
  337. if($this->PaperDimY>0 && $this->MarginBottom>$this->PaperDimY)
  338. trigger_error("Bottom margin bigger then paper size",E_USER_ERROR);
  339. if($this->PaperDimY>0 && ($this->MarginBottom+$this->MarginTop)>$this->PaperDimY)
  340. trigger_error("No rows to write left. Bottom margin + top margin bigger then paper size",E_USER_ERROR);
  341.  
  342. if($this->MarginLeft<0)
  343. trigger_error("Left margin is < 0. It should be greater or equal to 0",E_USER_ERROR);
  344. if($this->PaperDimX>0 && $this->MarginLeft>$this->PaperDimX)
  345. trigger_error("Left margin bigger then paper width",E_USER_ERROR);
  346.  
  347. if($this->MarginRight<0)
  348. trigger_error("Right margin is < 0. It should be greater or equal to 0",E_USER_ERROR);
  349. if($this->PaperDimX>0 && $this->MarginRight>$this->PaperDimX)
  350. trigger_error("Right margin bigger then paper width",E_USER_ERROR);
  351. if($this->PaperDimX>0 && ($this->MarginLeft+$this->MarginRight)>$this->PaperDimX)
  352. trigger_error("No columns to write left. Left margin + right margin bigger then paper width",E_USER_ERROR);
  353.  
  354. }
  355.  
  356. /**
  357. * Set the callback function
  358. * @param string $callback which callback apply the function
  359. * @param mixed $func function name or array holding object and method
  360. * (see call_uzer_func_array manual page for more detail)
  361. * @param array $parm parameters to pass to the function
  362. * @access public
  363. * @see $ValidCallbacks
  364. * @see $CallBacks
  365. */
  366. function SetCallBack($callback,$func,$parm)
  367. {
  368. if(!in_array($callback,$this->ValidCallbacks))
  369. trigger_error('Callback '. $callback. ' does not supported',E_USER_ERROR);
  370. $this->CallBacks[$callback]=$func;
  371. $this->CallBacksParm[$callback]=$parm;
  372. }
  373.  
  374. /**
  375. * Useful function to setup all margins with one function
  376. * use -1 to skip the parameter
  377. *
  378. * @param integer $top top margin size
  379. * @param integer $bottom bottom margin size
  380. * @param integer $left left margiclass
  381. * @param integer $right right margin size
  382. * @access public
  383. */
  384.  
  385. function SetMargin($top,$bottom=-1,$left=-1,$right=-1)
  386. {
  387. if(!($top<0))
  388. $this->MarginTop=$top;
  389. if(!($bottom<0))
  390. $this->MarginBottom=$bottom;
  391. if(!($left<0))
  392. $this->MarginLeft=$left;
  393. if(!($right<0))
  394. $this->MarginRight=$right;
  395. $this->_CheckMarginValue();
  396. }
  397.  
  398. /**
  399. * Set the size for the top margin
  400. * @param integer
  401. * @access public
  402. */
  403. function SetTopMargin($value)
  404. {
  405. $this->MarginTop=$value;
  406. $this->_CheckMarginValue();
  407. }
  408. /**
  409. * Set the size for the bottom margin
  410. * @param integer
  411. * @access public
  412. */
  413. function SetBottomMargin($value)
  414. {
  415. $this->MarginBottom=$value;
  416. $this->_CheckMarginValue();
  417. }
  418. /**
  419. * Set the size for the left margin
  420. * @param integer
  421. * @access public
  422. */
  423. function SetLeftMargin($value)
  424. {
  425. $this->MarginLeft=$value;
  426. $this->_CheckMarginValue();
  427. }
  428.  
  429. /**
  430. * Set the size for the right margin
  431. * @param integer
  432. * @access public
  433. */
  434. function SetRightMargin($value)
  435. {
  436. $this->MarginRight=$value;
  437. $this->_CheckMarginValue();
  438. }
  439.  
  440. /**
  441. * Specifiy which printer I'll use. If you don't give any printer, the default printer
  442. * will be used.
  443. * @param string $Name printer name as defined in Window
  444. * @access public
  445. */
  446. function SetPrinter($Name)
  447. {
  448. if($this->ConnectionOpened)
  449. trigger_error("Set the printer name before send any text (or title)",E_USER_ERROR);
  450. $this->PrinterName=$Name;
  451. }
  452.  
  453. /**
  454. * Set if the module should print its own footer or not.
  455. * @param bool $print TRUE=print its own page footer
  456. * @access public
  457. */
  458. function SetPrintFooter($print)
  459. {
  460. $this->PrintPageFooter=$print;
  461. }
  462.  
  463. /**
  464. * Setup the text to print on each page.
  465. * The text, passed as array, allow you to pass many rows
  466. * ( 1 row = 1 item).
  467. * @var array text to print. Due to compatibility toward previous version
  468. * text may be a string, in that case font will be ignored
  469. * @var array font to use to print each row
  470. * @var bool Should I print the header on the first page?
  471. * @access public
  472. */
  473. function SetPageHeader($Text,$fontArray=array(),$OnFirstPage=true)
  474. {
  475. $this->PageHeader=$Text;
  476. // Each call will destroy the previous values
  477. $this->NewPageHeader=array();
  478. if(is_array($Text))
  479. {
  480. // new parameters version aprint >= 1.2
  481. foreach($Text as $Key => $Value)
  482. {
  483. if(array_key_exists($Key,$fontArray))
  484. $txtFont=$fontArray[$Key];
  485. else
  486. $txtFont="";
  487. $this->NewPageHeader[]=array('Text'=>$Value,
  488. 'Font'=>$txtFont);
  489. }
  490. }
  491. else
  492. {
  493. // 1.0 version, $Text only one string
  494. $this->NewPageHeader[]=array('Text'=>$Text,
  495. 'Font'=>"");
  496. }
  497. if($OnFirstPage&&$this->NPag==1)
  498. $this->PrintPageHeader();
  499. }
  500.  
  501. /**
  502. * Set the AutoNewPage flag. If set to true, the module call the functionNewPage
  503. * automatically at the end of the page, if set to off, the user must call the
  504. * NewPage function
  505. * @param boolean
  506. * @return boolean The function returns the old value
  507. * @see NewPage()
  508. */
  509. function SetAutoNewPage($bool)
  510. {
  511. $old=$this->AutoNewPage;
  512. $this->AutoNewPage=$bool;
  513. return($old);
  514. }
  515.  
  516. /**
  517. * Write an header at the top of the first page.
  518. * @param string Text to print
  519. * @param integer $hFont font handle returned by CreateFont
  520. * @access public
  521. * @see CreateFont();
  522. */
  523. function HeaderText($TitleText,$hFont=-1)
  524. {
  525. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  526. $this->Text("----------------------------------------------------------",AP_NORMAL_FONT);
  527. $font=($hFont==-1)?AP_BOLD_FONT:$hFont;
  528. $this->Text($TitleText,$font);
  529. $this->Text("----------------------------------------------------------",AP_NORMAL_FONT);
  530. }
  531.  
  532. /**
  533. * Skip a row, left a blank row.
  534. * @access public
  535. *
  536. */
  537. function BlankRow()
  538. {
  539. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  540. $this->Text(" ");
  541. }
  542.  
  543. /**
  544. * printout the text
  545. * @param string $text text to printout
  546. * @param integer $hFont Handle to the font to use. Handle returned by CreateFont
  547. * @param integer $align text alignment
  548. * @param integer $col Orizontal position where start to print the text. This value
  549. * will be added to the left margin.
  550. * @access public
  551. * @see CreateFont()
  552. * @see TextFooter()
  553. */
  554. function Text($text,$hFont=-1,$align=AP_LEFT,$col=0)
  555. {
  556. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  557. // Select new font?
  558. if($hFont!=-1)
  559. {
  560. $this->_SetFont($hFont);
  561. }
  562. $this->FontH=$this->FontTable[$this->CurrentFont]['fontHeight'];
  563. $width=$this->FontTable[$this->CurrentFont]['fontWidth'];
  564. $textWidth=strlen($text)*$width;
  565. $StartCol=$this->_BeginCol($align,$textWidth,$col);
  566. printer_draw_text($this->hPrinter,$text,$this->ResConv_x($StartCol),$this->ResConv_y($this->row));
  567. $this->row+=$this->FontH;
  568. if($this->row>($this->PaperDimY-$this->MarginBottom-300-$this->FontH)&&$this->AutoNewPage)
  569. {
  570. $this->NewPage();
  571. }
  572. }
  573.  
  574. /**
  575. * printout the text at the exact position requested
  576. * @param integer orizontal position
  577. * @param integer vertical position where print the text
  578. * @param string $text text to printout
  579. * @param integer $hFont Handle to the font to use. Handle returned by CreateFont
  580. * @access public
  581. * @see CreateFont()
  582. * @see Text()
  583. */
  584. function TextXY($Xpos,$Ypos,$text,$hFont=-1)
  585. {
  586. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  587. // Select new font?
  588. if($hFont!=-1)
  589. {
  590. $this->_SetFont($hFont);
  591. }
  592. $this->FontH=$this->FontTable[$this->CurrentFont]['fontHeight'];
  593. $width=$this->FontTable[$this->CurrentFont]['fontWidth'];
  594. printer_draw_text($this->hPrinter,$text,$this->ResConv_x($Xpos),$this->ResConv_y($Ypos));
  595. $this->row=$Ypos+$this->FontH;
  596. if($this->row>($this->PaperDimY-$this->MarginBottom-300-$this->FontH)&&$this->AutoNewPage)
  597. {
  598. $this->NewPage();
  599. }
  600. }
  601.  
  602. /**
  603. * Print the page header
  604. * @access public
  605. * @see Text()
  606. */
  607. function PrintPageHeader()
  608. {
  609. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  610. $OriginalFont=-1;
  611. // var_dump($this->NewPageHeader);
  612. foreach($this->NewPageHeader as $Value)
  613. {
  614. $Text=$Value['Text'];
  615. $hFont=$Value['Font'];
  616. // Select new font?
  617. // var_dump($hFont);
  618. if($hFont!=-1 && $hFont!="")
  619. {
  620. $oldFont=$this->_SetFont($hFont);
  621. }
  622. if($OriginalFont<0) $OriginalFont=$oldFont;
  623. $width=$this->FontTable[$this->CurrentFont]['fontWidth'];
  624. $FontH=$this->FontTable[$this->CurrentFont]['fontHeight'];
  625. $textWidth=strlen($Text)*$width;
  626. $StartCol=$this->_BeginCol(AP_LEFT,$textWidth,0);
  627. printer_draw_text($this->hPrinter,$Text,$this->ResConv_x($StartCol),
  628. $this->ResConv_y($this->row));
  629. $this->row+=$FontH;
  630. } // Loop on each row
  631.  
  632. if($OriginalFont>-1)
  633. $this->_SetFont($OriginalFont);
  634. }
  635.  
  636.  
  637. /**
  638. * Print the given text as page footer
  639. * @param string $text text to printout
  640. * @param integer $hFont Handle to the font to use. Handle returned by CreateFont
  641. * @param integer $align text alignment
  642. * @param integer $col Orizontal position where start to print the text. This value
  643. * will be added to the left margin.
  644. * @access public
  645. * @see CreateFont()
  646. * @see Text()
  647. */
  648. function TextFooter($text,$hFont=-1,$align=AP_LEFT,$col=0)
  649. {
  650. if(!$this->ConnectionOpened) $this->_OpenPrinter();
  651. // Select new font?
  652. if($hFont!=-1)
  653. {
  654. $oldFont=$this->_SetFont($hFont);
  655. }
  656. else
  657. {
  658. $oldFont=$this->_SetFont(AP_FOOTER_FONT);
  659. }
  660. $width=$this->FontTable[$this->CurrentFont]['fontWidth'];
  661. $FontH=$this->FontTable[$this->CurrentFont]['fontHeight'];
  662. $textWidth=strlen($text)*$width;
  663. $StartCol=$this->_BeginCol($align,$textWidth,$col);
  664. printer_draw_text($this->hPrinter,$text,$this->ResConv_x($StartCol),
  665. $this->ResConv_y($this->PaperDimY-$this->MarginBottom-$FontH));
  666. printer_end_page($this->hPrinter);
  667. $this->_SetFont($oldFont);
  668. }
  669.  
  670. /**
  671. * This function compute the starting position given the
  672. * text alignment, the left margin and the starting column
  673. * @param integer $align text alignment
  674. * @param integer $textWidth text width
  675. * @param integer $col Orizontal position where start to print the text. This value
  676. * will be added to the left margin.
  677. * @access private
  678. * @see TextFooter()
  679. * @see Text()
  680. *
  681. */
  682. function _BeginCol($align,$textWidth,$col)
  683. {
  684. switch($align)
  685. {
  686. case AP_LEFT:
  687. $StartCol=$this->MarginLeft+$col;
  688. break;
  689. case AP_CENTER:
  690. if(($this->MarginLeft+$this->MarginRight+$textWidth+$col)>$this->PaperDimX)
  691. $StartCol=$this->MarginLeft;
  692. else
  693. $StartCol=$this->MarginLeft+$col+floor(($this->PaperDimX-$this->MarginLeft-$this->MarginRight-$textWidth-$col)/2);
  694. break;
  695. case AP_RIGHT:
  696. if(($this->MarginLeft+$this->MarginRight+$textWidth+$col)>$this->PaperDimX)
  697. $StartCol=$this->MarginLeft;
  698. else
  699. $StartCol=$this->PaperDimX-$this->MarginRight-$textWidth;
  700. break;
  701. }
  702. return($StartCol);
  703. }
  704. /**
  705. * Draw a dash line.
  706. * @access public
  707. */
  708. function Line()
  709. {
  710. $this->Text("----------------------------------------------------------");
  711. }
  712.  
  713. /**
  714. * Close the output to the printer and start to print
  715. * @access public
  716. */
  717. function run()
  718. {
  719. $this->_FooterNote();
  720. printer_end_doc($this->hPrinter);
  721. printer_close($this->hPrinter);
  722. }
  723.  
  724. /**
  725. * Close the current page and start a new one.
  726. *
  727. * @access public
  728. */
  729. function NewPage()
  730. {
  731. if(!$this->ConnectionOpened)
  732. trigger_error("Open the connection to the printer before run the function NewPage",E_USER_ERROR);
  733. // run the callback, if set
  734. if(array_key_exists('newpage',$this->CallBacks))
  735. call_user_func_array($this->CallBacks['newpage'],$this->CallBacksParm['newpage']);
  736.  
  737. if($this->NPag&&$this->PrintPageFooter) {
  738. $this->_FooterNote();
  739. }
  740. if($this->NPag) {
  741. printer_end_page($this->hPrinter);
  742. }
  743. $this->NPag++;
  744. printer_start_page($this->hPrinter);
  745. $this->row=$this->MarginTop;
  746. $this->PrintPageHeader();
  747. }
  748.  
  749. /**
  750. * Printout page number and script name as footer
  751. *
  752. * @access private
  753. */
  754. function _FooterNote()
  755. {
  756. $oldFont=$this->_SetFont(AP_FOOTER_FONT);
  757. printer_draw_text($this->hPrinter,$this->ScriptId,
  758. $this->ResConv_x($this->MarginLeft),
  759. $this->ResConv_y($this->PaperDimY-$this->MarginBottom-$this->FontTable[AP_FOOTER_FONT]['fontHeight']));
  760. printer_draw_text($this->hPrinter,$this->NPag,
  761. $this->ResConv_x($this->PaperDimX-$this->MarginRight-4000),
  762. $this->ResConv_y($this->PaperDimY-$this->MarginBottom-$this->FontTable[AP_FOOTER_FONT]['fontHeight']));
  763. $this->_SetFont($oldFont);
  764. }
  765.  
  766. /**
  767. * Create a new font
  768. *
  769. * @param string $fName Font Name, default: Courier New
  770. * @param integer $fHeight Font height, default: 336
  771. * @param integer $fWidth Font width, default: 168
  772. * @param integer $fWeight Font weight, default: 400. 0 -> thin, 800 -> bold
  773. * @param boolean $fItalic Italic script? True/False
  774. * @param boolean $fUnderline Text with underline? True/False
  775. * @param boolean $fStrikeout True/False
  776. * @param integer $fOrientation Should be always 3 digits, i.e 020. See printer_create_font in the note to the manual
  777. * @see _SetFont()
  778. * @see Text()
  779. * @access public
  780. *
  781. */
  782. function CreateFont($fName="Courier new",$fHeight=336,$fWidth=168,$fWeight=400,$fItalic=false,$fUnderline=false,$fStrikeout=false,$fOrientaton=0)
  783. {
  784. if(!$this->ConnectionOpened)
  785. trigger_error("Open the connection to the printer before define the font",E_USER_ERROR);
  786. $LastFontId=count($this->FontTable);
  787. $this->FontTable[$LastFontId]['fontName']=$fName;
  788. $this->FontTable[$LastFontId]['fontHeight']=$fHeight;
  789. $this->FontTable[$LastFontId]['fontWidth']=$fWidth;
  790. $this->FontTable[$LastFontId]['fontWeight']=$fWeight;
  791. $this->FontTable[$LastFontId]['fontItalic']=$fItalic;
  792. $this->FontTable[$LastFontId]['fontUnderline']=$fUnderline;
  793. $this->FontTable[$LastFontId]['fontStrikeout']=$fStrikeout;
  794. $this->FontTable[$LastFontId]['fontOrientaton']=$fOrientaton;
  795. // printer_create_font("Courier New",66,33,400,false,false,false,0);
  796. $this->FontTable[$LastFontId]['fontHandler']=printer_create_font($fName,$this->ResConv_y($fHeight),$this->ResConv_x($fWidth),$fWeight,$fItalic,$fUnderline,$fStrikeout,$fOrientaton);
  797. return($LastFontId);
  798. }
  799.  
  800. /**
  801. * Establish the connection to the printer
  802. * @access private
  803. */
  804. function _OpenPrinter()
  805. {
  806. if($this->PrinterName=="")
  807. $this->hPrinter=printer_open();
  808. else
  809. $this->hPrinter=printer_open($this->PrinterName);
  810.  
  811. //
  812. $this->ConnectionOpened=true;
  813. $this->_DefinePageSize(); // inizialize all parameter relate the margins, and page size
  814. if(!$this->Res_X)
  815. $this->Res_X=printer_get_option($this->hPrinter, PRINTER_RESOLUTION_X);
  816. if(!$this->Res_Y)
  817. $this->Res_Y=printer_get_option($this->hPrinter, PRINTER_RESOLUTION_Y);
  818. printer_start_doc($this->hPrinter, $this->JobTitle);
  819. $this->_DefaultFont();
  820. $this->_SetFont(AP_NORMAL_FONT);
  821. $this->NewPage();
  822. }
  823.  
  824.  
  825. /**
  826. * Start the connection to the printer. This is the public interface
  827. * to _OpenPrinter.
  828. * @param string Name of the printer to use. If not set use the system default
  829. * @access public
  830. * @see _OpenPrinter()
  831. */
  832. function OpenPrinter($PrinterName="")
  833. {
  834. if($PrinterName!="") $this->SetPrinter($PrinterName);
  835. $this->_OpenPrinter();
  836. }
  837.  
  838. /**
  839. * Define some dafault font
  840. * @access private
  841. */
  842. function _DefaultFont()
  843. {
  844. // create predefined font. Don't change the row order
  845. $this->CreateFont("Courier New",336,168,400,false,false,false,0);
  846. $this->CreateFont("Courier New",336,168,800,false,false,false,0);
  847. // echo $this->CreateFont("arial",163,84,200,false,false,false,0);
  848. $this->CreateFont("Courier new",163,50,200,false,false,false,0);
  849. }
  850.  
  851. /**
  852. * Convert the internal point size to the printer point size according to its resolution
  853. * As internal unit I use the twip. 1 inch = 1440 twip, 1 mm = 56,7 twip
  854. * @param integer
  855. * @return integer
  856. * @access private
  857. * @see ResConv_y()
  858. */
  859. function ResConv_x($value)
  860. {
  861. return(floor($value*$this->Res_X/1440));
  862. }
  863.  
  864. /**
  865. * @param integer
  866. * @return integer
  867. * @see ResConv_x()
  868. */
  869. function ResConv_y($value)
  870. {
  871. return(floor($value*$this->Res_Y/1440));
  872. }
  873.  
  874. /**
  875. * Convert the printer point size into internal point size according to its resolution
  876. * As internal unit I use the twip. 1 inch = 1440 twip, 1 mm = 56,7 twip
  877. * @param integer
  878. * @return integer
  879. * @access private
  880. * @see Conv2Twip_y()
  881. * @see ResConv_x()
  882. * @see ResConv_y()
  883. */
  884. function Conv2Twip_x($value)
  885. {
  886. return(floor($value*1440/$this->Res_X));
  887. }
  888.  
  889. /**
  890. * @param integer
  891. * @return integer
  892. * @see Conv2Twip_x()
  893. */
  894. function Conv2Twip_y($value)
  895. {
  896. return(floor($value*1440/$this->Res_Y));
  897. }
  898.  
  899. /**
  900. * Activate the specified font
  901. *
  902. * @param int $fnt font handle returned by CreateFont
  903. * @access private
  904. * @see CreateFont()
  905. */
  906. function _SetFont($fnt=-1)
  907. {
  908. if($fnt==-1)
  909. $fntHandler=$this->CurrentFont;
  910. else
  911. $fntHandler=$fnt;
  912. assert('array_key_exists($fntHandler,$this->FontTable)');
  913. printer_select_font($this->hPrinter,$this->FontTable[$fntHandler]['fontHandler']);
  914. $oldFont=$this->CurrentFont;
  915. $this->CurrentFont=$fntHandler;
  916. return($oldFont);
  917. }
  918.  
  919. /**
  920. * Move the currentline up or down of nRow rows. It return TRUE on success,
  921. * FALSE otherwise
  922. * @parameter integer $nUnit Numebr of 'unit' may be less or greater then zero
  923. * @parameter string $unit measurement used to set the movement
  924. * @access public
  925. * @return boolean
  926. */
  927. function MvRelativePos($nUnit,$unit='row')
  928. {
  929. $move=$this->_CalculatePos($nUnit,$unit);
  930. if($move<0&&abs($move)>$this->row)
  931. return(FALSE);
  932. if($move>0&&$this->row+$move>$this->PaperDimY)
  933. return(FALSE);
  934. $this->row+=$move;
  935. return(TRUE);
  936. }
  937.  
  938. /**
  939. * Move the currentline to nRow. It return TRUE on success,
  940. * FALSE otherwise
  941. * @parameter integer $nUnit Numebr of 'unit' it must be greater then zero
  942. * @parameter string $unit measurement used to set the movement
  943. * @access public
  944. * @return boolean
  945. */
  946. function MvAbsolutePos($nUnit,$unit='row')
  947. {
  948. $move=$this->_CalculatePos($nUnit,$unit);
  949. if($move<1||$move>$this->PaperDimY)
  950. return(FALSE);
  951. $this->row=$move;
  952. return(TRUE);
  953. }
  954.  
  955. /**
  956. * Move to the first row after the top margin It return TRUE on success,
  957. * FALSE otherwise
  958. * @access public
  959. * @return boolean
  960. */
  961. function MvTop()
  962. {
  963. $this->row=$this->MarginTop;
  964. return(TRUE);
  965. }
  966.  
  967. /**
  968. * Move to the last row before the bottom margin It return TRUE on success,
  969. * FALSE otherwise.
  970. * Notice: the bottom is calculated using as row hight the current font
  971. * @parameter integer $nRow New current line number
  972. * @access public
  973. * @return boolean
  974. */
  975. function MvBottom()
  976. {
  977. $this->PaperDimY-$this->MarginBottom-300-$this->FontH;
  978. return(TRUE);
  979. }
  980.  
  981. /**
  982. * Internal function used to calculate the movement.
  983. * @parameter integer $nUnit Numebr of 'unit' may be less or greater then zero
  984. * @parameter string $unit measurement used to set the movement
  985. * @access public
  986. * @return boolean
  987. */
  988. function _CalculatePos($nUnit,$unit)
  989. {
  990. if(!in_array($unit,$this->ValidMeasure))
  991. trigger_error('Unit of measure '.$unit.' not supported',E_USER_ERROR);
  992. switch($unit)
  993. {
  994. case 'row':
  995. $move=$this->FontH*$nUnit;
  996. break;
  997. case 'inch':
  998. $move=$nUnit*1440;
  999. break;
  1000. case 'mm':
  1001. $move=$nUnit*56.7;
  1002. break;
  1003. case 'twip':
  1004. $move=$nUnit;
  1005. break;
  1006. }
  1007. return($move);
  1008. }
  1009.  
  1010. /**
  1011. * Retrieve the page size
  1012. *
  1013. * @return array (width,length)
  1014. * @access public
  1015. */
  1016. function GetPageSize()
  1017. {
  1018. return(array($this->PaperDimX,$this->PaperDimY));
  1019. }
  1020.  
  1021. } // end class APrint
  1022.  
  1023. ?>

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