Page.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @package Zend_Pdf
  16. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /** Zend_Pdf_Resource_Font */
  20. require_once 'Zend/Pdf/Resource/Font.php';
  21. /** Zend_Pdf_Style */
  22. require_once 'Zend/Pdf/Style.php';
  23. /** Zend_Pdf_Element_Dictionary */
  24. require_once 'Zend/Pdf/Element/Dictionary.php';
  25. /** Zend_Pdf_Element_Reference */
  26. require_once 'Zend/Pdf/Element/Reference.php';
  27. /** Zend_Pdf_ElementFactory */
  28. require_once 'Zend/Pdf/ElementFactory.php';
  29. /** Zend_Pdf_Color */
  30. require_once 'Zend/Pdf/Color.php';
  31. /** Zend_Pdf_Color_GrayScale */
  32. require_once 'Zend/Pdf/Color/GrayScale.php';
  33. /** Zend_Pdf_Color_Rgb */
  34. require_once 'Zend/Pdf/Color/Rgb.php';
  35. /** Zend_Pdf_Color_Cmyk */
  36. require_once 'Zend/Pdf/Color/Cmyk.php';
  37. /**
  38. * PDF Page
  39. *
  40. * @package Zend_Pdf
  41. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Pdf_Page
  45. {
  46. /**** Class Constants ****/
  47. /* Page Sizes */
  48. /**
  49. * Size representing an A4 page in portrait (tall) orientation.
  50. */
  51. const SIZE_A4 = '595:842:';
  52. /**
  53. * Size representing an A4 page in landscape (wide) orientation.
  54. */
  55. const SIZE_A4_LANDSCAPE = '842:595:';
  56. /**
  57. * Size representing a US Letter page in portrait (tall) orientation.
  58. */
  59. const SIZE_LETTER = '612:792:';
  60. /**
  61. * Size representing a US Letter page in landscape (wide) orientation.
  62. */
  63. const SIZE_LETTER_LANDSCAPE = '792:612:';
  64. /* Shape Drawing */
  65. /**
  66. * Stroke the path only. Do not fill.
  67. */
  68. const SHAPE_DRAW_STROKE = 0;
  69. /**
  70. * Fill the path only. Do not stroke.
  71. */
  72. const SHAPE_DRAW_FILL = 1;
  73. /**
  74. * Fill and stroke the path.
  75. */
  76. const SHAPE_DRAW_FILL_AND_STROKE = 2;
  77. /* Shape Filling Methods */
  78. /**
  79. * Fill the path using the non-zero winding rule.
  80. */
  81. const FILL_METHOD_NON_ZERO_WINDING = 0;
  82. /**
  83. * Fill the path using the even-odd rule.
  84. */
  85. const FILL_METHOD_EVEN_ODD = 1;
  86. /* Line Dash Types */
  87. /**
  88. * Solid line dash.
  89. */
  90. const LINE_DASHING_SOLID = 0;
  91. /**
  92. * Reference to the object with page dictionary.
  93. *
  94. * @var Zend_Pdf_Element_Reference
  95. */
  96. protected $_pageDictionary;
  97. /**
  98. * PDF objects factory.
  99. *
  100. * @var Zend_Pdf_ElementFactory_Interface
  101. */
  102. protected $_objFactory = null;
  103. /**
  104. * Flag which signals, that page is created separately from any PDF document or
  105. * attached to anyone.
  106. *
  107. * @var boolean
  108. */
  109. protected $_attached;
  110. /**
  111. * Stream of the drawing instractions.
  112. *
  113. * @var string
  114. */
  115. protected $_contents = '';
  116. /**
  117. * Current style
  118. *
  119. * @var Zend_Pdf_Style
  120. */
  121. protected $_style = null;
  122. /**
  123. * Counter for the "Save" operations
  124. *
  125. * @var integer
  126. */
  127. protected $_saveCount = 0;
  128. /**
  129. * Safe Graphics State semafore
  130. *
  131. * If it's false, than we can't be sure Graphics State is restored withing
  132. * context of previous contents stream (ex. drawing coordinate system may be rotated).
  133. * We should encompass existing content with save/restore GS operators
  134. *
  135. * @var boolean
  136. */
  137. protected $_safeGS;
  138. /**
  139. * Current font
  140. *
  141. * @var Zend_Pdf_Resource_Font
  142. */
  143. protected $_font = null;
  144. /**
  145. * Current font size
  146. *
  147. * @var float
  148. */
  149. protected $_fontSize;
  150. /**
  151. * Object constructor.
  152. * Constructor signatures:
  153. *
  154. * 1. Load PDF page from a parsed PDF file.
  155. * Object factory is created by PDF parser.
  156. * ---------------------------------------------------------
  157. * new Zend_Pdf_Page(Zend_Pdf_Element_Dictionary $pageDict,
  158. * Zend_Pdf_ElementFactory_Interface $factory);
  159. * ---------------------------------------------------------
  160. *
  161. * 2. Clone PDF page.
  162. * New page is created in the same context as source page. Object factory is shared.
  163. * Thus it will be attached to the document, but need to be placed into Zend_Pdf::$pages array
  164. * to be included into output.
  165. * ---------------------------------------------------------
  166. * new Zend_Pdf_Page(Zend_Pdf_Page $page);
  167. * ---------------------------------------------------------
  168. *
  169. * 3. Create new page with a specified pagesize.
  170. * If $factory is null then it will be created and page must be attached to the document to be
  171. * included into output.
  172. * ---------------------------------------------------------
  173. * new Zend_Pdf_Page(string $pagesize, Zend_Pdf_ElementFactory_Interface $factory = null);
  174. * ---------------------------------------------------------
  175. *
  176. * 4. Create new page with a specified pagesize (in default user space units).
  177. * If $factory is null then it will be created and page must be attached to the document to be
  178. * included into output.
  179. * ---------------------------------------------------------
  180. * new Zend_Pdf_Page(numeric $width, numeric $height, Zend_Pdf_ElementFactory_Interface $factory = null);
  181. * ---------------------------------------------------------
  182. *
  183. *
  184. * @param mixed $param1
  185. * @param mixed $param2
  186. * @param mixed $param3
  187. * @throws Zend_Pdf_Exception
  188. */
  189. public function __construct($param1, $param2 = null, $param3 = null)
  190. {
  191. if ($param1 instanceof Zend_Pdf_Element_Reference &&
  192. $param1->getType() == Zend_Pdf_Element::TYPE_DICTIONARY &&
  193. $param2 instanceof Zend_Pdf_ElementFactory_Interface &&
  194. $param3 === null
  195. ) {
  196. $this->_pageDictionary = $param1;
  197. $this->_objFactory = $param2;
  198. $this->_attached = true;
  199. $this->_safeGS = false;
  200. return;
  201. } else if ($param1 instanceof Zend_Pdf_Page && $param2 === null && $param3 === null) {
  202. // Clone existing page.
  203. // Let already existing content and resources to be shared between pages
  204. // We don't give existing content modification functionality, so we don't need "deep copy"
  205. $this->_objFactory = $param1->_objFactory;
  206. $this->_attached = &$param1->_attached;
  207. $this->_safeGS = false;
  208. $this->_pageDictionary = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  209. foreach ($param1->_pageDictionary->getKeys() as $key) {
  210. if ($key == 'Contents') {
  211. // Clone Contents property
  212. $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array();
  213. if ($param1->_pageDictionary->Contents->getType() != Zend_Pdf_Element::TYPE_ARRAY) {
  214. // Prepare array of content streams and add existing stream
  215. $this->_pageDictionary->Contents->items[] = $param1->_pageDictionary->Contents;
  216. } else {
  217. // Clone array of the content streams
  218. foreach ($param1->_pageDictionary->Contents->items as $srcContentStream) {
  219. $this->_pageDictionary->Contents->items[] = $srcContentStream;
  220. }
  221. }
  222. } else {
  223. $this->_pageDictionary->$key = $param1->_pageDictionary->$key;
  224. }
  225. }
  226. return;
  227. } else if (is_string($param1) &&
  228. ($param2 === null || $param2 instanceof Zend_Pdf_ElementFactory_Interface) &&
  229. $param3 === null) {
  230. $this->_objFactory = ($param2 !== null)? $param2 : Zend_Pdf_ElementFactory::createFactory(1);
  231. $this->_attached = false;
  232. $this->_safeGS = true; /** New page created. That's users App responsibility to track GS changes */
  233. switch (strtolower($param1)) {
  234. case 'a4':
  235. $param1 = Zend_Pdf_Page::SIZE_A4;
  236. break;
  237. case 'a4-landscape':
  238. $param1 = Zend_Pdf_Page::SIZE_A4_LANDSCAPE;
  239. break;
  240. case 'letter':
  241. $param1 = Zend_Pdf_Page::SIZE_LETTER;
  242. break;
  243. case 'letter-landscape':
  244. $param1 = Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE;
  245. break;
  246. default:
  247. // should be in "x:y" form
  248. }
  249. $pageDim = explode(':', $param1);
  250. if(count($pageDim) == 3) {
  251. $pageWidth = $pageDim[0];
  252. $pageHeight = $pageDim[1];
  253. } else {
  254. /**
  255. * @todo support of user defined pagesize notations, like:
  256. * "210x297mm", "595x842", "8.5x11in", "612x792"
  257. */
  258. require_once 'Zend/Pdf/Exception.php';
  259. throw new Zend_Pdf_Exception('Wrong pagesize notation.');
  260. }
  261. /**
  262. * @todo support of pagesize recalculation to "default user space units"
  263. */
  264. } else if (is_numeric($param1) && is_numeric($param2) &&
  265. ($param3 === null || $param3 instanceof Zend_Pdf_ElementFactory_Interface)) {
  266. $this->_objFactory = ($param3 !== null)? $param3 : Zend_Pdf_ElementFactory::createFactory(1);
  267. $this->_attached = false;
  268. $this->_safeGS = true; /** New page created. That's users App responsibility to track GS changes */
  269. $pageWidth = $param1;
  270. $pageHeight = $param2;
  271. } else {
  272. require_once 'Zend/Pdf/Exception.php';
  273. throw new Zend_Pdf_Exception('Unrecognized method signature, wrong number of arguments or wrong argument types.');
  274. }
  275. $this->_pageDictionary = $this->_objFactory->newObject(new Zend_Pdf_Element_Dictionary());
  276. $this->_pageDictionary->Type = new Zend_Pdf_Element_Name('Page');
  277. $this->_pageDictionary->LastModified = new Zend_Pdf_Element_String(Zend_Pdf::pdfDate());
  278. $this->_pageDictionary->Resources = new Zend_Pdf_Element_Dictionary();
  279. $this->_pageDictionary->MediaBox = new Zend_Pdf_Element_Array();
  280. $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric(0);
  281. $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric(0);
  282. $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric($pageWidth);
  283. $this->_pageDictionary->MediaBox->items[] = new Zend_Pdf_Element_Numeric($pageHeight);
  284. $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array();
  285. }
  286. /**
  287. * Clone operator
  288. *
  289. * @throws Zend_Pdf_Exception
  290. */
  291. public function __clone()
  292. {
  293. require_once 'Zend/Pdf/Exception.php';
  294. throw new Zend_Pdf_Exception('Cloning Zend_Pdf_Page object using \'clone\' keyword is not supported. Use \'new Zend_Pdf_Page($srcPage)\' syntax');
  295. }
  296. /**
  297. * Attach resource to the page
  298. *
  299. * @param string $type
  300. * @param Zend_Pdf_Resource $resource
  301. * @return string
  302. */
  303. protected function _attachResource($type, Zend_Pdf_Resource $resource)
  304. {
  305. // Check that Resources dictionary contains appropriate resource set
  306. if ($this->_pageDictionary->Resources->$type === null) {
  307. $this->_pageDictionary->Resources->touch();
  308. $this->_pageDictionary->Resources->$type = new Zend_Pdf_Element_Dictionary();
  309. } else {
  310. $this->_pageDictionary->Resources->$type->touch();
  311. }
  312. // Check, that resource is already attached to resource set.
  313. $resObject = $resource->getResource();
  314. foreach ($this->_pageDictionary->Resources->$type->getKeys() as $ResID) {
  315. if ($this->_pageDictionary->Resources->$type->$ResID === $resObject) {
  316. return $ResID;
  317. }
  318. }
  319. $idCounter = 1;
  320. do {
  321. $newResName = $type[0] . $idCounter++;
  322. } while ($this->_pageDictionary->Resources->$type->$newResName !== null);
  323. $this->_pageDictionary->Resources->$type->$newResName = $resObject;
  324. $this->_objFactory->attach($resource->getFactory());
  325. return $newResName;
  326. }
  327. /**
  328. * Add procedureSet to the Page description
  329. *
  330. * @param string $procSetName
  331. */
  332. protected function _addProcSet($procSetName)
  333. {
  334. // Check that Resources dictionary contains ProcSet entry
  335. if ($this->_pageDictionary->Resources->ProcSet === null) {
  336. $this->_pageDictionary->Resources->touch();
  337. $this->_pageDictionary->Resources->ProcSet = new Zend_Pdf_Element_Array();
  338. } else {
  339. $this->_pageDictionary->Resources->ProcSet->touch();
  340. }
  341. foreach ($this->_pageDictionary->Resources->ProcSet->items as $procSetEntry) {
  342. if ($procSetEntry->value == $procSetName) {
  343. // Procset is already included into a ProcSet array
  344. return;
  345. }
  346. }
  347. $this->_pageDictionary->Resources->ProcSet->items[] = new Zend_Pdf_Element_Name($procSetName);
  348. }
  349. /**
  350. * Retrive PDF file reference to the page
  351. *
  352. * @return Zend_Pdf_Element_Dictionary
  353. */
  354. public function getPageDictionary()
  355. {
  356. return $this->_pageDictionary;
  357. }
  358. /**
  359. * Dump current drawing instructions into the content stream.
  360. *
  361. * @todo Don't forget to close all current graphics operations (like path drawing)
  362. *
  363. * @throws Zend_Pdf_Exception
  364. */
  365. public function flush()
  366. {
  367. if ($this->_saveCount != 0) {
  368. require_once 'Zend/Pdf/Exception.php';
  369. throw new Zend_Pdf_Exception('Saved graphics state is not restored');
  370. }
  371. if ($this->_contents == '') {
  372. return;
  373. }
  374. if ($this->_pageDictionary->Contents->getType() != Zend_Pdf_Element::TYPE_ARRAY) {
  375. /**
  376. * It's a stream object.
  377. * Prepare Contents page attribute for update.
  378. */
  379. $this->_pageDictionary->touch();
  380. $currentPageContents = $this->_pageDictionary->Contents;
  381. $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array();
  382. $this->_pageDictionary->Contents->items[] = $currentPageContents;
  383. } else {
  384. $this->_pageDictionary->Contents->touch();
  385. }
  386. if ((!$this->_safeGS) && (count($this->_pageDictionary->Contents->items) != 0)) {
  387. /**
  388. * Page already has some content which is not treated as safe.
  389. *
  390. * Add save/restore GS operators
  391. */
  392. $this->_addProcSet('PDF');
  393. $newContentsArray = new Zend_Pdf_Element_Array();
  394. $newContentsArray->items[] = $this->_objFactory->newStreamObject(" q\n");
  395. foreach ($this->_pageDictionary->Contents->items as $contentStream) {
  396. $newContentsArray->items[] = $contentStream;
  397. }
  398. $newContentsArray->items[] = $this->_objFactory->newStreamObject(" Q\n");
  399. $this->_pageDictionary->touch();
  400. $this->_pageDictionary->Contents = $newContentsArray;
  401. $this->_safeGS = true;
  402. }
  403. $this->_pageDictionary->Contents->items[] =
  404. $this->_objFactory->newStreamObject($this->_contents);
  405. $this->_contents = '';
  406. }
  407. /**
  408. * Prepare page to be rendered into PDF.
  409. *
  410. * @todo Don't forget to close all current graphics operations (like path drawing)
  411. *
  412. * @param Zend_Pdf_ElementFactory_Interface $objFactory
  413. * @throws Zend_Pdf_Exception
  414. */
  415. public function render(Zend_Pdf_ElementFactory_Interface $objFactory)
  416. {
  417. $this->flush();
  418. if ($objFactory === $this->_objFactory) {
  419. // Page is already attached to the document.
  420. return;
  421. }
  422. if ($this->_attached) {
  423. require_once 'Zend/Pdf/Exception.php';
  424. throw new Zend_Pdf_Exception('Page is attached to one documen, but rendered in context of another.');
  425. /**
  426. * @todo Page cloning must be implemented here instead of exception.
  427. * PDF objects (ex. fonts) can be shared between pages.
  428. * Thus all referenced objects, which can be modified, must be cloned recursively,
  429. * to avoid producing wrong object references in a context of source PDF.
  430. */
  431. //...
  432. } else {
  433. $objFactory->attach($this->_objFactory);
  434. }
  435. }
  436. /**
  437. * Set fill color.
  438. *
  439. * @param Zend_Pdf_Color $color
  440. * @return Zend_Pdf_Page
  441. */
  442. public function setFillColor(Zend_Pdf_Color $color)
  443. {
  444. $this->_addProcSet('PDF');
  445. $this->_contents .= $color->instructions(false);
  446. return $this;
  447. }
  448. /**
  449. * Set line color.
  450. *
  451. * @param Zend_Pdf_Color $color
  452. * @return Zend_Pdf_Page
  453. */
  454. public function setLineColor(Zend_Pdf_Color $color)
  455. {
  456. $this->_addProcSet('PDF');
  457. $this->_contents .= $color->instructions(true);
  458. return $this;
  459. }
  460. /**
  461. * Set line width.
  462. *
  463. * @param float $width
  464. * @return Zend_Pdf_Page
  465. */
  466. public function setLineWidth($width)
  467. {
  468. $this->_addProcSet('PDF');
  469. $widthObj = new Zend_Pdf_Element_Numeric($width);
  470. $this->_contents .= $widthObj->toString() . " w\n";
  471. return $this;
  472. }
  473. /**
  474. * Set line dashing pattern
  475. *
  476. * Pattern is an array of floats: array(on_length, off_length, on_length, off_length, ...)
  477. * Phase is shift from the beginning of line.
  478. *
  479. * @param array $pattern
  480. * @param array $phase
  481. * @return Zend_Pdf_Page
  482. */
  483. public function setLineDashingPattern($pattern, $phase = 0)
  484. {
  485. $this->_addProcSet('PDF');
  486. if ($pattern === Zend_Pdf_Page::LINE_DASHING_SOLID) {
  487. $pattern = array();
  488. $phase = 0;
  489. }
  490. $dashPattern = new Zend_Pdf_Element_Array();
  491. $phaseEleemnt = new Zend_Pdf_Element_Numeric($phase);
  492. foreach ($pattern as $dashItem) {
  493. $dashElement = new Zend_Pdf_Element_Numeric($dashItem);
  494. $dashPattern->items[] = $dashElement;
  495. }
  496. $this->_contents .= $dashPattern->toString() . ' '
  497. . $phaseEleemnt->toString() . " d\n";
  498. return $this;
  499. }
  500. /**
  501. * Set current font.
  502. *
  503. * @param Zend_Pdf_Resource_Font $font
  504. * @param float $fontSize
  505. * @return Zend_Pdf_Page
  506. */
  507. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize)
  508. {
  509. $this->_addProcSet('Text');
  510. $fontName = $this->_attachResource('Font', $font);
  511. $this->_font = $font;
  512. $this->_fontSize = $fontSize;
  513. $fontNameObj = new Zend_Pdf_Element_Name($fontName);
  514. $fontSizeObj = new Zend_Pdf_Element_Numeric($fontSize);
  515. $this->_contents .= $fontNameObj->toString() . ' ' . $fontSizeObj->toString() . " Tf\n";
  516. return $this;
  517. }
  518. /**
  519. * Set the style to use for future drawing operations on this page
  520. *
  521. * @param Zend_Pdf_Style $style
  522. * @return Zend_Pdf_Page
  523. */
  524. public function setStyle(Zend_Pdf_Style $style)
  525. {
  526. $this->_style = $style;
  527. $this->_addProcSet('Text');
  528. $this->_addProcSet('PDF');
  529. if ($style->getFont() !== null) {
  530. $this->setFont($style->getFont(), $style->getFontSize());
  531. }
  532. $this->_contents .= $style->instructions($this->_pageDictionary->Resources);
  533. return $this;
  534. }
  535. /**
  536. * Set the transparancy
  537. *
  538. * $alpha == 0 - transparent
  539. * $alpha == 1 - opaque
  540. *
  541. * Transparency modes, supported by PDF:
  542. * Normal (default), Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight,
  543. * SoftLight, Difference, Exclusion
  544. *
  545. * @param float $alpha
  546. * @param string $mode
  547. * @throws Zend_Pdf_Exception
  548. * @return Zend_Pdf_Page
  549. */
  550. public function setAlpha($alpha, $mode = 'Normal')
  551. {
  552. if (!in_array($mode, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge',
  553. 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion'))) {
  554. require_once 'Zend/Pdf/Exception.php';
  555. throw new Zend_Pdf_Exception('Unsupported transparency mode.');
  556. }
  557. if (!is_numeric($alpha) || $alpha < 0 || $alpha > 1) {
  558. require_once 'Zend/Pdf/Exception.php';
  559. throw new Zend_Pdf_Exception('Alpha value must be numeric between 0 (transparent) and 1 (opaque).');
  560. }
  561. $this->_addProcSet('Text');
  562. $this->_addProcSet('PDF');
  563. $resources = $this->_pageDictionary->Resources;
  564. // Check if Resources dictionary contains ExtGState entry
  565. if ($resources->ExtGState === null) {
  566. $resources->touch();
  567. $resources->ExtGState = new Zend_Pdf_Element_Dictionary();
  568. } else {
  569. $resources->ExtGState->touch();
  570. }
  571. $idCounter = 1;
  572. do {
  573. $gStateName = 'GS' . $idCounter++;
  574. } while ($resources->ExtGState->$gStateName !== null);
  575. $gStateDictionary = new Zend_Pdf_Element_Dictionary();
  576. $gStateDictionary->Type = new Zend_Pdf_Element_Name('ExtGState');
  577. $gStateDictionary->BM = new Zend_Pdf_Element_Name($mode);
  578. $gStateDictionary->CA = new Zend_Pdf_Element_Numeric($alpha);
  579. $gStateDictionary->ca = new Zend_Pdf_Element_Numeric($alpha);
  580. $resources->ExtGState->$gStateName = $this->_objFactory->newObject($gStateDictionary);
  581. $gStateNameObj = new Zend_Pdf_Element_Name($gStateName);
  582. $this->_contents .= $gStateNameObj->toString() . " gs\n";
  583. return $this;
  584. }
  585. /**
  586. * Get current font.
  587. *
  588. * @return Zend_Pdf_Resource_Font $font
  589. */
  590. public function getFont()
  591. {
  592. return $this->_font;
  593. }
  594. /**
  595. * Extract resources attached to the page
  596. *
  597. * This method is not intended to be used in userland, but helps to optimize some document wide operations
  598. *
  599. * returns array of Zend_Pdf_Element_Dictionary objects
  600. *
  601. * @internal
  602. * @return array
  603. */
  604. public function extractResources()
  605. {
  606. return $this->_pageDictionary->Resources;
  607. }
  608. /**
  609. * Extract fonts attached to the page
  610. *
  611. * returns array of Zend_Pdf_Resource_Font_Extracted objects
  612. *
  613. * @return array
  614. */
  615. public function extractFonts()
  616. {
  617. if ($this->_pageDictionary->Resources->Font === null) {
  618. // Page doesn't have any font attached
  619. // Return empty array
  620. return array();
  621. }
  622. $fontResources = $this->_pageDictionary->Resources->Font;
  623. $fontResourcesUnique = array();
  624. foreach ($fontResources->getKeys() as $fontResourceName) {
  625. $fontDictionary = $fontResources->$fontResourceName;
  626. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  627. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  628. // Font dictionary has to be an indirect object or object reference
  629. continue;
  630. }
  631. $fontResourcesUnique[$fontDictionary->toString($this->_objFactory)] = $fontDictionary;
  632. }
  633. $fonts = array();
  634. require_once 'Zend/Pdf/Exception.php';
  635. foreach ($fontResourcesUnique as $resourceReference => $fontDictionary) {
  636. try {
  637. // Try to extract font
  638. $extractedFont = new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  639. $fonts[$resourceReference] = $extractedFont;
  640. } catch (Zend_Pdf_Exception $e) {
  641. if ($e->getMessage() != 'Unsupported font type.') {
  642. throw $e;
  643. }
  644. }
  645. }
  646. return $fonts;
  647. }
  648. /**
  649. * Extract font attached to the page by specific font name
  650. *
  651. * $fontName should be specified in UTF-8 encoding
  652. *
  653. * @return Zend_Pdf_Resource_Font_Extracted|null
  654. */
  655. public function extractFont($fontName)
  656. {
  657. if ($this->_pageDictionary->Resources->Font === null) {
  658. // Page doesn't have any font attached
  659. return null;
  660. }
  661. $fontResources = $this->_pageDictionary->Resources->Font;
  662. require_once 'Zend/Pdf/Exception.php';
  663. foreach ($fontResources->getKeys() as $fontResourceName) {
  664. $fontDictionary = $fontResources->$fontResourceName;
  665. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  666. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  667. // Font dictionary has to be an indirect object or object reference
  668. continue;
  669. }
  670. if ($fontDictionary->BaseFont->value != $fontName) {
  671. continue;
  672. }
  673. try {
  674. // Try to extract font
  675. return new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  676. } catch (Zend_Pdf_Exception $e) {
  677. if ($e->getMessage() != 'Unsupported font type.') {
  678. throw $e;
  679. }
  680. // Continue searhing font with specified name
  681. }
  682. }
  683. return null;
  684. }
  685. /**
  686. * Get current font size
  687. *
  688. * @return float $fontSize
  689. */
  690. public function getFontSize()
  691. {
  692. return $this->_fontSize;
  693. }
  694. /**
  695. * Return the style, applied to the page.
  696. *
  697. * @return Zend_Pdf_Style|null
  698. */
  699. public function getStyle()
  700. {
  701. return $this->_style;
  702. }
  703. /**
  704. * Save the graphics state of this page.
  705. * This takes a snapshot of the currently applied style, position, clipping area and
  706. * any rotation/translation/scaling that has been applied.
  707. *
  708. * @todo check for the open paths
  709. * @throws Zend_Pdf_Exception - if a save is performed with an open path
  710. * @return Zend_Pdf_Page
  711. */
  712. public function saveGS()
  713. {
  714. $this->_saveCount++;
  715. $this->_addProcSet('PDF');
  716. $this->_contents .= " q\n";
  717. return $this;
  718. }
  719. /**
  720. * Restore the graphics state that was saved with the last call to saveGS().
  721. *
  722. * @throws Zend_Pdf_Exception - if there is no previously saved state
  723. * @return Zend_Pdf_Page
  724. */
  725. public function restoreGS()
  726. {
  727. if ($this->_saveCount-- <= 0) {
  728. require_once 'Zend/Pdf/Exception.php';
  729. throw new Zend_Pdf_Exception('Restoring graphics state which is not saved');
  730. }
  731. $this->_contents .= " Q\n";
  732. return $this;
  733. }
  734. /**
  735. * Intersect current clipping area with a circle.
  736. *
  737. * @param float $x
  738. * @param float $y
  739. * @param float $radius
  740. * @param float $startAngle
  741. * @param float $endAngle
  742. * @return Zend_Pdf_Page
  743. */
  744. public function clipCircle($x, $y, $radius, $startAngle = null, $endAngle = null)
  745. {
  746. $this->clipEllipse($x - $radius, $y - $radius,
  747. $x + $radius, $y + $radius,
  748. $startAngle, $endAngle);
  749. return $this;
  750. }
  751. /**
  752. * Intersect current clipping area with a polygon.
  753. *
  754. * Method signatures:
  755. * drawEllipse($x1, $y1, $x2, $y2);
  756. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  757. *
  758. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  759. *
  760. * @param float $x1
  761. * @param float $y1
  762. * @param float $x2
  763. * @param float $y2
  764. * @param float $startAngle
  765. * @param float $endAngle
  766. * @return Zend_Pdf_Page
  767. */
  768. public function clipEllipse($x1, $y1, $x2, $y2, $startAngle = null, $endAngle = null)
  769. {
  770. $this->_addProcSet('PDF');
  771. if ($x2 < $x1) {
  772. $temp = $x1;
  773. $x1 = $x2;
  774. $x2 = $temp;
  775. }
  776. if ($y2 < $y1) {
  777. $temp = $y1;
  778. $y1 = $y2;
  779. $y2 = $temp;
  780. }
  781. $x = ($x1 + $x2)/2.;
  782. $y = ($y1 + $y2)/2.;
  783. $xC = new Zend_Pdf_Element_Numeric($x);
  784. $yC = new Zend_Pdf_Element_Numeric($y);
  785. if ($startAngle !== null) {
  786. if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
  787. if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
  788. if ($startAngle > $endAngle) {
  789. $endAngle += M_PI*2;
  790. }
  791. $clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
  792. $clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
  793. $clipRadius = max($x2 - $x1, $y2 - $y1);
  794. for($count = 0; $count <= $clipSectors; $count++) {
  795. $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
  796. $pX = new Zend_Pdf_Element_Numeric($x + cos($pAngle)*$clipRadius);
  797. $pY = new Zend_Pdf_Element_Numeric($y + sin($pAngle)*$clipRadius);
  798. $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
  799. }
  800. $this->_contents .= $clipPath . "h\nW\nn\n";
  801. }
  802. $xLeft = new Zend_Pdf_Element_Numeric($x1);
  803. $xRight = new Zend_Pdf_Element_Numeric($x2);
  804. $yUp = new Zend_Pdf_Element_Numeric($y2);
  805. $yDown = new Zend_Pdf_Element_Numeric($y1);
  806. $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
  807. $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
  808. $xr = new Zend_Pdf_Element_Numeric($x + $xDelta);
  809. $xl = new Zend_Pdf_Element_Numeric($x - $xDelta);
  810. $yu = new Zend_Pdf_Element_Numeric($y + $yDelta);
  811. $yd = new Zend_Pdf_Element_Numeric($y - $yDelta);
  812. $this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
  813. . $xr->toString() . ' ' . $yUp->toString() . ' '
  814. . $xRight->toString() . ' ' . $yu->toString() . ' '
  815. . $xRight->toString() . ' ' . $yC->toString() . " c\n"
  816. . $xRight->toString() . ' ' . $yd->toString() . ' '
  817. . $xr->toString() . ' ' . $yDown->toString() . ' '
  818. . $xC->toString() . ' ' . $yDown->toString() . " c\n"
  819. . $xl->toString() . ' ' . $yDown->toString() . ' '
  820. . $xLeft->toString() . ' ' . $yd->toString() . ' '
  821. . $xLeft->toString() . ' ' . $yC->toString() . " c\n"
  822. . $xLeft->toString() . ' ' . $yu->toString() . ' '
  823. . $xl->toString() . ' ' . $yUp->toString() . ' '
  824. . $xC->toString() . ' ' . $yUp->toString() . " c\n"
  825. . "h\nW\nn\n";
  826. return $this;
  827. }
  828. /**
  829. * Intersect current clipping area with a polygon.
  830. *
  831. * @param array $x - array of float (the X co-ordinates of the vertices)
  832. * @param array $y - array of float (the Y co-ordinates of the vertices)
  833. * @param integer $fillMethod
  834. * @return Zend_Pdf_Page
  835. */
  836. public function clipPolygon($x, $y, $fillMethod = Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING)
  837. {
  838. $this->_addProcSet('PDF');
  839. $firstPoint = true;
  840. foreach ($x as $id => $xVal) {
  841. $xObj = new Zend_Pdf_Element_Numeric($xVal);
  842. $yObj = new Zend_Pdf_Element_Numeric($y[$id]);
  843. if ($firstPoint) {
  844. $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
  845. $firstPoint = false;
  846. } else {
  847. $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n";
  848. }
  849. }
  850. $this->_contents .= $path;
  851. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  852. $this->_contents .= " h\n W\nn\n";
  853. } else {
  854. // Even-Odd fill method.
  855. $this->_contents .= " h\n W*\nn\n";
  856. }
  857. return $this;
  858. }
  859. /**
  860. * Intersect current clipping area with a rectangle.
  861. *
  862. * @param float $x1
  863. * @param float $y1
  864. * @param float $x2
  865. * @param float $y2
  866. * @return Zend_Pdf_Page
  867. */
  868. public function clipRectangle($x1, $y1, $x2, $y2)
  869. {
  870. $this->_addProcSet('PDF');
  871. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  872. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  873. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  874. $height2Obj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  875. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' '
  876. . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n"
  877. . " W\nn\n";
  878. return $this;
  879. }
  880. /**
  881. * Draw a Zend_Pdf_ContentStream at the specified position on the page
  882. *
  883. * @param ZPdfContentStream $cs
  884. * @param float $x1
  885. * @param float $y1
  886. * @param float $x2
  887. * @param float $y2
  888. * @return Zend_Pdf_Page
  889. */
  890. public function drawContentStream($cs, $x1, $y1, $x2, $y2)
  891. {
  892. /** @todo implementation */
  893. return $this;
  894. }
  895. /**
  896. * Draw a circle centered on x, y with a radius of radius.
  897. *
  898. * Method signatures:
  899. * drawCircle($x, $y, $radius);
  900. * drawCircle($x, $y, $radius, $fillType);
  901. * drawCircle($x, $y, $radius, $startAngle, $endAngle);
  902. * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
  903. *
  904. *
  905. * It's not a really circle, because PDF supports only cubic Bezier curves.
  906. * But _very_ good approximation.
  907. * It differs from a real circle on a maximum 0.00026 radiuses
  908. * (at PI/8, 3*PI/8, 5*PI/8, 7*PI/8, 9*PI/8, 11*PI/8, 13*PI/8 and 15*PI/8 angles).
  909. * At 0, PI/4, PI/2, 3*PI/4, PI, 5*PI/4, 3*PI/2 and 7*PI/4 it's exactly a tangent to a circle.
  910. *
  911. * @param float $x
  912. * @param float $y
  913. * @param float $radius
  914. * @param mixed $param4
  915. * @param mixed $param5
  916. * @param mixed $param6
  917. * @return Zend_Pdf_Page
  918. */
  919. public function drawCircle($x, $y, $radius, $param4 = null, $param5 = null, $param6 = null)
  920. {
  921. $this->drawEllipse($x - $radius, $y - $radius,
  922. $x + $radius, $y + $radius,
  923. $param4, $param5, $param6);
  924. return $this;
  925. }
  926. /**
  927. * Draw an ellipse inside the specified rectangle.
  928. *
  929. * Method signatures:
  930. * drawEllipse($x1, $y1, $x2, $y2);
  931. * drawEllipse($x1, $y1, $x2, $y2, $fillType);
  932. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  933. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  934. *
  935. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  936. *
  937. * @param float $x1
  938. * @param float $y1
  939. * @param float $x2
  940. * @param float $y2
  941. * @param mixed $param5
  942. * @param mixed $param6
  943. * @param mixed $param7
  944. * @return Zend_Pdf_Page
  945. */
  946. public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, $param7 = null)
  947. {
  948. if ($param5 === null) {
  949. // drawEllipse($x1, $y1, $x2, $y2);
  950. $startAngle = null;
  951. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE;
  952. } else if ($param6 === null) {
  953. // drawEllipse($x1, $y1, $x2, $y2, $fillType);
  954. $startAngle = null;
  955. $fillType = $param5;
  956. } else {
  957. // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  958. // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  959. $startAngle = $param5;
  960. $endAngle = $param6;
  961. if ($param7 === null) {
  962. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE;
  963. } else {
  964. $fillType = $param7;
  965. }
  966. }
  967. $this->_addProcSet('PDF');
  968. if ($x2 < $x1) {
  969. $temp = $x1;
  970. $x1 = $x2;
  971. $x2 = $temp;
  972. }
  973. if ($y2 < $y1) {
  974. $temp = $y1;
  975. $y1 = $y2;
  976. $y2 = $temp;
  977. }
  978. $x = ($x1 + $x2)/2.;
  979. $y = ($y1 + $y2)/2.;
  980. $xC = new Zend_Pdf_Element_Numeric($x);
  981. $yC = new Zend_Pdf_Element_Numeric($y);
  982. if ($startAngle !== null) {
  983. if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
  984. if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
  985. if ($startAngle > $endAngle) {
  986. $endAngle += M_PI*2;
  987. }
  988. $clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
  989. $clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
  990. $clipRadius = max($x2 - $x1, $y2 - $y1);
  991. for($count = 0; $count <= $clipSectors; $count++) {
  992. $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
  993. $pX = new Zend_Pdf_Element_Numeric($x + cos($pAngle)*$clipRadius);
  994. $pY = new Zend_Pdf_Element_Numeric($y + sin($pAngle)*$clipRadius);
  995. $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
  996. }
  997. $this->_contents .= "q\n" . $clipPath . "h\nW\nn\n";
  998. }
  999. $xLeft = new Zend_Pdf_Element_Numeric($x1);
  1000. $xRight = new Zend_Pdf_Element_Numeric($x2);
  1001. $yUp = new Zend_Pdf_Element_Numeric($y2);
  1002. $yDown = new Zend_Pdf_Element_Numeric($y1);
  1003. $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
  1004. $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
  1005. $xr = new Zend_Pdf_Element_Numeric($x + $xDelta);
  1006. $xl = new Zend_Pdf_Element_Numeric($x - $xDelta);
  1007. $yu = new Zend_Pdf_Element_Numeric($y + $yDelta);
  1008. $yd = new Zend_Pdf_Element_Numeric($y - $yDelta);
  1009. $this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
  1010. . $xr->toString() . ' ' . $yUp->toString() . ' '
  1011. . $xRight->toString() . ' ' . $yu->toString() . ' '
  1012. . $xRight->toString() . ' ' . $yC->toString() . " c\n"
  1013. . $xRight->toString() . ' ' . $yd->toString() . ' '
  1014. . $xr->toString() . ' ' . $yDown->toString() . ' '
  1015. . $xC->toString() . ' ' . $yDown->toString() . " c\n"
  1016. . $xl->toString() . ' ' . $yDown->toString() . ' '
  1017. . $xLeft->toString() . ' ' . $yd->toString() . ' '
  1018. . $xLeft->toString() . ' ' . $yC->toString() . " c\n"
  1019. . $xLeft->toString() . ' ' . $yu->toString() . ' '
  1020. . $xl->toString() . ' ' . $yUp->toString() . ' '
  1021. . $xC->toString() . ' ' . $yUp->toString() . " c\n";
  1022. switch ($fillType) {
  1023. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1024. $this->_contents .= " B*\n";
  1025. break;
  1026. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1027. $this->_contents .= " f*\n";
  1028. break;
  1029. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1030. $this->_contents .= " S\n";
  1031. break;
  1032. }
  1033. if ($startAngle !== null) {
  1034. $this->_contents .= "Q\n";
  1035. }
  1036. return $this;
  1037. }
  1038. /**
  1039. * Draw an image at the specified position on the page.
  1040. *
  1041. * @param Zend_Pdf_Image $image
  1042. * @param float $x1
  1043. * @param float $y1
  1044. * @param float $x2
  1045. * @param float $y2
  1046. * @return Zend_Pdf_Page
  1047. */
  1048. public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2)
  1049. {
  1050. $this->_addProcSet('PDF');
  1051. $imageName = $this->_attachResource('XObject', $image);
  1052. $imageNameObj = new Zend_Pdf_Element_Name($imageName);
  1053. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1054. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1055. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  1056. $heightObj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  1057. $this->_contents .= "q\n"
  1058. . '1 0 0 1 ' . $x1Obj->toString() . ' ' . $y1Obj->toString() . " cm\n"
  1059. . $widthObj->toString() . ' 0 0 ' . $heightObj->toString() . " 0 0 cm\n"
  1060. . $imageNameObj->toString() . " Do\n"
  1061. . "Q\n";
  1062. return $this;
  1063. }
  1064. /**
  1065. * Draw a LayoutBox at the specified position on the page.
  1066. *
  1067. * @param Zend_Pdf_Element_LayoutBox $box
  1068. * @param float $x
  1069. * @param float $y
  1070. * @return Zend_Pdf_Page
  1071. */
  1072. public function drawLayoutBox($box, $x, $y)
  1073. {
  1074. /** @todo implementation */
  1075. return $this;
  1076. }
  1077. /**
  1078. * Draw a line from x1,y1 to x2,y2.
  1079. *
  1080. * @param float $x1
  1081. * @param float $y1
  1082. * @param float $x2
  1083. * @param float $y2
  1084. * @return Zend_Pdf_Page
  1085. */
  1086. public function drawLine($x1, $y1, $x2, $y2)
  1087. {
  1088. $this->_addProcSet('PDF');
  1089. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1090. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1091. $x2Obj = new Zend_Pdf_Element_Numeric($x2);
  1092. $y2Obj = new Zend_Pdf_Element_Numeric($y2);
  1093. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n"
  1094. . $x2Obj->toString() . ' ' . $y2Obj->toString() . " l\n S\n";
  1095. return $this;
  1096. }
  1097. /**
  1098. * Draw a polygon.
  1099. *
  1100. * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
  1101. * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
  1102. * See detailed description of these methods in a PDF documentation
  1103. * (section 4.4.2 Path painting Operators, Filling)
  1104. *
  1105. * @param array $x - array of float (the X co-ordinates of the vertices)
  1106. * @param array $y - array of float (the Y co-ordinates of the vertices)
  1107. * @param integer $fillType
  1108. * @param integer $fillMethod
  1109. * @return Zend_Pdf_Page
  1110. */
  1111. public function drawPolygon($x, $y,
  1112. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  1113. $fillMethod = Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING)
  1114. {
  1115. $this->_addProcSet('PDF');
  1116. $firstPoint = true;
  1117. foreach ($x as $id => $xVal) {
  1118. $xObj = new Zend_Pdf_Element_Numeric($xVal);
  1119. $yObj = new Zend_Pdf_Element_Numeric($y[$id]);
  1120. if ($firstPoint) {
  1121. $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
  1122. $firstPoint = false;
  1123. } else {
  1124. $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n";
  1125. }
  1126. }
  1127. $this->_contents .= $path;
  1128. switch ($fillType) {
  1129. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1130. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  1131. $this->_contents .= " b\n";
  1132. } else {
  1133. // Even-Odd fill method.
  1134. $this->_contents .= " b*\n";
  1135. }
  1136. break;
  1137. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1138. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  1139. $this->_contents .= " h\n f\n";
  1140. } else {
  1141. // Even-Odd fill method.
  1142. $this->_contents .= " h\n f*\n";
  1143. }
  1144. break;
  1145. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1146. $this->_contents .= " S\n";
  1147. break;
  1148. }
  1149. return $this;
  1150. }
  1151. /**
  1152. * Draw a rectangle.
  1153. *
  1154. * Fill types:
  1155. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle and stroke (default)
  1156. * Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke rectangle
  1157. * Zend_Pdf_Page::SHAPE_DRAW_FILL - fill rectangle
  1158. *
  1159. * @param float $x1
  1160. * @param float $y1
  1161. * @param float $x2
  1162. * @param float $y2
  1163. * @param integer $fillType
  1164. * @return Zend_Pdf_Page
  1165. */
  1166. public function drawRectangle($x1, $y1, $x2, $y2, $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE)
  1167. {
  1168. $this->_addProcSet('PDF');
  1169. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1170. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1171. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  1172. $height2Obj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  1173. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' '
  1174. . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n";
  1175. switch ($fillType) {
  1176. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1177. $this->_contents .= " B*\n";
  1178. break;
  1179. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1180. $this->_contents .= " f*\n";
  1181. break;
  1182. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1183. $this->_contents .= " S\n";
  1184. break;
  1185. }
  1186. return $this;
  1187. }
  1188. /**
  1189. * Draw a line of text at the specified position.
  1190. *
  1191. * @param string $text
  1192. * @param float $x
  1193. * @param float $y
  1194. * @param string $charEncoding (optional) Character encoding of source text.
  1195. * Defaults to current locale.
  1196. * @throws Zend_Pdf_Exception
  1197. * @return Zend_Pdf_Page
  1198. */
  1199. public function drawText($text, $x, $y, $charEncoding = '')
  1200. {
  1201. if ($this->_font === null) {
  1202. require_once 'Zend/Pdf/Exception.php';
  1203. throw new Zend_Pdf_Exception('Font has not been set');
  1204. }
  1205. $this->_addProcSet('Text');
  1206. $textObj = new Zend_Pdf_Element_String($this->_font->encodeString($text, $charEncoding));
  1207. $xObj = new Zend_Pdf_Element_Numeric($x);
  1208. $yObj = new Zend_Pdf_Element_Numeric($y);
  1209. $this->_contents .= "BT\n"
  1210. . $xObj->toString() . ' ' . $yObj->toString() . " Td\n"
  1211. . $textObj->toString() . " Tj\n"
  1212. . "ET\n";
  1213. return $this;
  1214. }
  1215. /**
  1216. * Return the height of this page in points.
  1217. *
  1218. * @return float
  1219. */
  1220. public function getHeight()
  1221. {
  1222. return $this->_pageDictionary->MediaBox->items[3]->value -
  1223. $this->_pageDictionary->MediaBox->items[1]->value;
  1224. }
  1225. /**
  1226. * Return the width of this page in points.
  1227. *
  1228. * @return float
  1229. */
  1230. public function getWidth()
  1231. {
  1232. return $this->_pageDictionary->MediaBox->items[2]->value -
  1233. $this->_pageDictionary->MediaBox->items[0]->value;
  1234. }
  1235. /**
  1236. * Close the path by drawing a straight line back to it's beginning.
  1237. *
  1238. * @throws Zend_Pdf_Exception - if a path hasn't been started with pathMove()
  1239. * @return Zend_Pdf_Page
  1240. */
  1241. public function pathClose()
  1242. {
  1243. /** @todo implementation */
  1244. return $this;
  1245. }
  1246. /**
  1247. * Continue the open path in a straight line to the specified position.
  1248. *
  1249. * @param float $x - the X co-ordinate to move to
  1250. * @param float $y - the Y co-ordinate to move to
  1251. * @return Zend_Pdf_Page
  1252. */
  1253. public function pathLine($x, $y)
  1254. {
  1255. /** @todo implementation */
  1256. return $this;
  1257. }
  1258. /**
  1259. * Start a new path at the specified position. If a path has already been started,
  1260. * move the cursor without drawing a line.
  1261. *
  1262. * @param float $x - the X co-ordinate to move to
  1263. * @param float $y - the Y co-ordinate to move to
  1264. * @return Zend_Pdf_Page
  1265. */
  1266. public function pathMove($x, $y)
  1267. {
  1268. /** @todo implementation */
  1269. return $this;
  1270. }
  1271. /**
  1272. * Writes the raw data to the page's content stream.
  1273. *
  1274. * Be sure to consult the PDF reference to ensure your syntax is correct. No
  1275. * attempt is made to ensure the validity of the stream data.
  1276. *
  1277. * @param string $data
  1278. * @param string $procSet (optional) Name of ProcSet to add.
  1279. * @return Zend_Pdf_Page
  1280. */
  1281. public function rawWrite($data, $procSet = null)
  1282. {
  1283. if (! empty($procSet)) {
  1284. $this->_addProcSet($procSet);
  1285. }
  1286. $this->_contents .= $data;
  1287. return $this;
  1288. }
  1289. /**
  1290. * Rotate the page.
  1291. *
  1292. * @param float $x - the X co-ordinate of rotation point
  1293. * @param float $y - the Y co-ordinate of rotation point
  1294. * @param float $angle - rotation angle
  1295. * @return Zend_Pdf_Page
  1296. */
  1297. public function rotate($x, $y, $angle)
  1298. {
  1299. $cos = new Zend_Pdf_Element_Numeric(cos($angle));
  1300. $sin = new Zend_Pdf_Element_Numeric(sin($angle));
  1301. $mSin = new Zend_Pdf_Element_Numeric(-$sin->value);
  1302. $xObj = new Zend_Pdf_Element_Numeric($x);
  1303. $yObj = new Zend_Pdf_Element_Numeric($y);
  1304. $mXObj = new Zend_Pdf_Element_Numeric(-$x);
  1305. $mYObj = new Zend_Pdf_Element_Numeric(-$y);
  1306. $this->_addProcSet('PDF');
  1307. $this->_contents .= '1 0 0 1 ' . $xObj->toString() . ' ' . $yObj->toString() . " cm\n"
  1308. . $cos->toString() . ' ' . $sin->toString() . ' ' . $mSin->toString() . ' ' . $cos->toString() . " 0 0 cm\n"
  1309. . '1 0 0 1 ' . $mXObj->toString() . ' ' . $mYObj->toString() . " cm\n";
  1310. return $this;
  1311. }
  1312. /**
  1313. * Scale coordination system.
  1314. *
  1315. * @param float $xScale - X dimention scale factor
  1316. * @param float $yScale - Y dimention scale factor
  1317. * @return Zend_Pdf_Page
  1318. */
  1319. public function scale($xScale, $yScale)
  1320. {
  1321. $xScaleObj = new Zend_Pdf_Element_Numeric($xScale);
  1322. $yScaleObj = new Zend_Pdf_Element_Numeric($yScale);
  1323. $this->_addProcSet('PDF');
  1324. $this->_contents .= $xScaleObj->toString() . ' 0 0 ' . $yScaleObj->toString() . " 0 0 cm\n";
  1325. return $this;
  1326. }
  1327. /**
  1328. * Translate coordination system.
  1329. *
  1330. * @param float $xShift - X coordinate shift
  1331. * @param float $yShift - Y coordinate shift
  1332. * @return Zend_Pdf_Page
  1333. */
  1334. public function translate($xShift, $yShift)
  1335. {
  1336. $xShiftObj = new Zend_Pdf_Element_Numeric($xShift);
  1337. $yShiftObj = new Zend_Pdf_Element_Numeric($yShift);
  1338. $this->_addProcSet('PDF');
  1339. $this->_contents .= '1 0 0 1 ' . $xShiftObj->toString() . ' ' . $yShiftObj->toString() . " cm\n";
  1340. return $this;
  1341. }
  1342. /**
  1343. * Translate coordination system.
  1344. *
  1345. * @param float $x - the X co-ordinate of axis skew point
  1346. * @param float $y - the Y co-ordinate of axis skew point
  1347. * @param float $xAngle - X axis skew angle
  1348. * @param float $yAngle - Y axis skew angle
  1349. * @return Zend_Pdf_Page
  1350. */
  1351. public function skew($x, $y, $xAngle, $yAngle)
  1352. {
  1353. $tanXObj = new Zend_Pdf_Element_Numeric(tan($xAngle));
  1354. $tanYObj = new Zend_Pdf_Element_Numeric(-tan($yAngle));
  1355. $xObj = new Zend_Pdf_Element_Numeric($x);
  1356. $yObj = new Zend_Pdf_Element_Numeric($y);
  1357. $mXObj = new Zend_Pdf_Element_Numeric(-$x);
  1358. $mYObj = new Zend_Pdf_Element_Numeric(-$y);
  1359. $this->_addProcSet('PDF');
  1360. $this->_contents .= '1 0 0 1 ' . $xObj->toString() . ' ' . $yObj->toString() . " cm\n"
  1361. . '1 ' . $tanXObj->toString() . ' ' . $tanYObj->toString() . " 1 0 0 cm\n"
  1362. . '1 0 0 1 ' . $mXObj->toString() . ' ' . $mYObj->toString() . " cm\n";
  1363. return $this;
  1364. }
  1365. }