Page.php 52 KB

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