Page.php 53 KB

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