Page.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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. * @throws Zend_Pdf_Exception
  618. */
  619. public function extractFonts()
  620. {
  621. if ($this->_pageDictionary->Resources->Font === null) {
  622. // Page doesn't have any font attached
  623. // Return empty array
  624. return array();
  625. }
  626. $fontResources = $this->_pageDictionary->Resources->Font;
  627. $fontResourcesUnique = array();
  628. foreach ($fontResources->getKeys() as $fontResourceName) {
  629. $fontDictionary = $fontResources->$fontResourceName;
  630. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  631. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  632. require_once 'Zend/Pdf/Exception.php';
  633. throw new Zend_Pdf_Exception('Font dictionary has to be an indirect object or object reference.');
  634. }
  635. $fontResourcesUnique[spl_object_hash($fontDictionary->getObject())] = $fontDictionary;
  636. }
  637. $fonts = array();
  638. require_once 'Zend/Pdf/Exception.php';
  639. foreach ($fontResourcesUnique as $resourceId => $fontDictionary) {
  640. try {
  641. // Try to extract font
  642. $extractedFont = new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  643. $fonts[$resourceId] = $extractedFont;
  644. } catch (Zend_Pdf_Exception $e) {
  645. if ($e->getMessage() != 'Unsupported font type.') {
  646. throw $e;
  647. }
  648. }
  649. }
  650. return $fonts;
  651. }
  652. /**
  653. * Extract font attached to the page by specific font name
  654. *
  655. * $fontName should be specified in UTF-8 encoding
  656. *
  657. * @return Zend_Pdf_Resource_Font_Extracted|null
  658. * @throws Zend_Pdf_Exception
  659. */
  660. public function extractFont($fontName)
  661. {
  662. if ($this->_pageDictionary->Resources->Font === null) {
  663. // Page doesn't have any font attached
  664. return null;
  665. }
  666. $fontResources = $this->_pageDictionary->Resources->Font;
  667. $fontResourcesUnique = array();
  668. require_once 'Zend/Pdf/Exception.php';
  669. foreach ($fontResources->getKeys() as $fontResourceName) {
  670. $fontDictionary = $fontResources->$fontResourceName;
  671. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  672. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  673. require_once 'Zend/Pdf/Exception.php';
  674. throw new Zend_Pdf_Exception('Font dictionary has to be an indirect object or object reference.');
  675. }
  676. $resourceId = spl_object_hash($fontDictionary->getObject());
  677. if (isset($fontResourcesUnique[$resourceId])) {
  678. continue;
  679. } else {
  680. // Mark resource as processed
  681. $fontResourcesUnique[$resourceId] = 1;
  682. }
  683. if ($fontDictionary->BaseFont->value != $fontName) {
  684. continue;
  685. }
  686. try {
  687. // Try to extract font
  688. return new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  689. } catch (Zend_Pdf_Exception $e) {
  690. if ($e->getMessage() != 'Unsupported font type.') {
  691. throw $e;
  692. }
  693. // Continue searhing font with specified name
  694. }
  695. }
  696. return null;
  697. }
  698. /**
  699. * Get current font size
  700. *
  701. * @return float $fontSize
  702. */
  703. public function getFontSize()
  704. {
  705. return $this->_fontSize;
  706. }
  707. /**
  708. * Return the style, applied to the page.
  709. *
  710. * @return Zend_Pdf_Style|null
  711. */
  712. public function getStyle()
  713. {
  714. return $this->_style;
  715. }
  716. /**
  717. * Save the graphics state of this page.
  718. * This takes a snapshot of the currently applied style, position, clipping area and
  719. * any rotation/translation/scaling that has been applied.
  720. *
  721. * @todo check for the open paths
  722. * @throws Zend_Pdf_Exception - if a save is performed with an open path
  723. * @return Zend_Pdf_Page
  724. */
  725. public function saveGS()
  726. {
  727. $this->_saveCount++;
  728. $this->_addProcSet('PDF');
  729. $this->_contents .= " q\n";
  730. return $this;
  731. }
  732. /**
  733. * Restore the graphics state that was saved with the last call to saveGS().
  734. *
  735. * @throws Zend_Pdf_Exception - if there is no previously saved state
  736. * @return Zend_Pdf_Page
  737. */
  738. public function restoreGS()
  739. {
  740. if ($this->_saveCount-- <= 0) {
  741. require_once 'Zend/Pdf/Exception.php';
  742. throw new Zend_Pdf_Exception('Restoring graphics state which is not saved');
  743. }
  744. $this->_contents .= " Q\n";
  745. return $this;
  746. }
  747. /**
  748. * Intersect current clipping area with a circle.
  749. *
  750. * @param float $x
  751. * @param float $y
  752. * @param float $radius
  753. * @param float $startAngle
  754. * @param float $endAngle
  755. * @return Zend_Pdf_Page
  756. */
  757. public function clipCircle($x, $y, $radius, $startAngle = null, $endAngle = null)
  758. {
  759. $this->clipEllipse($x - $radius, $y - $radius,
  760. $x + $radius, $y + $radius,
  761. $startAngle, $endAngle);
  762. return $this;
  763. }
  764. /**
  765. * Intersect current clipping area with a polygon.
  766. *
  767. * Method signatures:
  768. * drawEllipse($x1, $y1, $x2, $y2);
  769. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  770. *
  771. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  772. *
  773. * @param float $x1
  774. * @param float $y1
  775. * @param float $x2
  776. * @param float $y2
  777. * @param float $startAngle
  778. * @param float $endAngle
  779. * @return Zend_Pdf_Page
  780. */
  781. public function clipEllipse($x1, $y1, $x2, $y2, $startAngle = null, $endAngle = null)
  782. {
  783. $this->_addProcSet('PDF');
  784. if ($x2 < $x1) {
  785. $temp = $x1;
  786. $x1 = $x2;
  787. $x2 = $temp;
  788. }
  789. if ($y2 < $y1) {
  790. $temp = $y1;
  791. $y1 = $y2;
  792. $y2 = $temp;
  793. }
  794. $x = ($x1 + $x2)/2.;
  795. $y = ($y1 + $y2)/2.;
  796. $xC = new Zend_Pdf_Element_Numeric($x);
  797. $yC = new Zend_Pdf_Element_Numeric($y);
  798. if ($startAngle !== null) {
  799. if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
  800. if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
  801. if ($startAngle > $endAngle) {
  802. $endAngle += M_PI*2;
  803. }
  804. $clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
  805. $clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
  806. $clipRadius = max($x2 - $x1, $y2 - $y1);
  807. for($count = 0; $count <= $clipSectors; $count++) {
  808. $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
  809. $pX = new Zend_Pdf_Element_Numeric($x + cos($pAngle)*$clipRadius);
  810. $pY = new Zend_Pdf_Element_Numeric($y + sin($pAngle)*$clipRadius);
  811. $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
  812. }
  813. $this->_contents .= $clipPath . "h\nW\nn\n";
  814. }
  815. $xLeft = new Zend_Pdf_Element_Numeric($x1);
  816. $xRight = new Zend_Pdf_Element_Numeric($x2);
  817. $yUp = new Zend_Pdf_Element_Numeric($y2);
  818. $yDown = new Zend_Pdf_Element_Numeric($y1);
  819. $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
  820. $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
  821. $xr = new Zend_Pdf_Element_Numeric($x + $xDelta);
  822. $xl = new Zend_Pdf_Element_Numeric($x - $xDelta);
  823. $yu = new Zend_Pdf_Element_Numeric($y + $yDelta);
  824. $yd = new Zend_Pdf_Element_Numeric($y - $yDelta);
  825. $this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
  826. . $xr->toString() . ' ' . $yUp->toString() . ' '
  827. . $xRight->toString() . ' ' . $yu->toString() . ' '
  828. . $xRight->toString() . ' ' . $yC->toString() . " c\n"
  829. . $xRight->toString() . ' ' . $yd->toString() . ' '
  830. . $xr->toString() . ' ' . $yDown->toString() . ' '
  831. . $xC->toString() . ' ' . $yDown->toString() . " c\n"
  832. . $xl->toString() . ' ' . $yDown->toString() . ' '
  833. . $xLeft->toString() . ' ' . $yd->toString() . ' '
  834. . $xLeft->toString() . ' ' . $yC->toString() . " c\n"
  835. . $xLeft->toString() . ' ' . $yu->toString() . ' '
  836. . $xl->toString() . ' ' . $yUp->toString() . ' '
  837. . $xC->toString() . ' ' . $yUp->toString() . " c\n"
  838. . "h\nW\nn\n";
  839. return $this;
  840. }
  841. /**
  842. * Intersect current clipping area with a polygon.
  843. *
  844. * @param array $x - array of float (the X co-ordinates of the vertices)
  845. * @param array $y - array of float (the Y co-ordinates of the vertices)
  846. * @param integer $fillMethod
  847. * @return Zend_Pdf_Page
  848. */
  849. public function clipPolygon($x, $y, $fillMethod = Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING)
  850. {
  851. $this->_addProcSet('PDF');
  852. $firstPoint = true;
  853. foreach ($x as $id => $xVal) {
  854. $xObj = new Zend_Pdf_Element_Numeric($xVal);
  855. $yObj = new Zend_Pdf_Element_Numeric($y[$id]);
  856. if ($firstPoint) {
  857. $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
  858. $firstPoint = false;
  859. } else {
  860. $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n";
  861. }
  862. }
  863. $this->_contents .= $path;
  864. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  865. $this->_contents .= " h\n W\nn\n";
  866. } else {
  867. // Even-Odd fill method.
  868. $this->_contents .= " h\n W*\nn\n";
  869. }
  870. return $this;
  871. }
  872. /**
  873. * Intersect current clipping area with a rectangle.
  874. *
  875. * @param float $x1
  876. * @param float $y1
  877. * @param float $x2
  878. * @param float $y2
  879. * @return Zend_Pdf_Page
  880. */
  881. public function clipRectangle($x1, $y1, $x2, $y2)
  882. {
  883. $this->_addProcSet('PDF');
  884. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  885. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  886. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  887. $height2Obj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  888. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' '
  889. . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n"
  890. . " W\nn\n";
  891. return $this;
  892. }
  893. /**
  894. * Draw a Zend_Pdf_ContentStream at the specified position on the page
  895. *
  896. * @param ZPdfContentStream $cs
  897. * @param float $x1
  898. * @param float $y1
  899. * @param float $x2
  900. * @param float $y2
  901. * @return Zend_Pdf_Page
  902. */
  903. public function drawContentStream($cs, $x1, $y1, $x2, $y2)
  904. {
  905. /** @todo implementation */
  906. return $this;
  907. }
  908. /**
  909. * Draw a circle centered on x, y with a radius of radius.
  910. *
  911. * Method signatures:
  912. * drawCircle($x, $y, $radius);
  913. * drawCircle($x, $y, $radius, $fillType);
  914. * drawCircle($x, $y, $radius, $startAngle, $endAngle);
  915. * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
  916. *
  917. *
  918. * It's not a really circle, because PDF supports only cubic Bezier curves.
  919. * But _very_ good approximation.
  920. * It differs from a real circle on a maximum 0.00026 radiuses
  921. * (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).
  922. * 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.
  923. *
  924. * @param float $x
  925. * @param float $y
  926. * @param float $radius
  927. * @param mixed $param4
  928. * @param mixed $param5
  929. * @param mixed $param6
  930. * @return Zend_Pdf_Page
  931. */
  932. public function drawCircle($x, $y, $radius, $param4 = null, $param5 = null, $param6 = null)
  933. {
  934. $this->drawEllipse($x - $radius, $y - $radius,
  935. $x + $radius, $y + $radius,
  936. $param4, $param5, $param6);
  937. return $this;
  938. }
  939. /**
  940. * Draw an ellipse inside the specified rectangle.
  941. *
  942. * Method signatures:
  943. * drawEllipse($x1, $y1, $x2, $y2);
  944. * drawEllipse($x1, $y1, $x2, $y2, $fillType);
  945. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  946. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  947. *
  948. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  949. *
  950. * @param float $x1
  951. * @param float $y1
  952. * @param float $x2
  953. * @param float $y2
  954. * @param mixed $param5
  955. * @param mixed $param6
  956. * @param mixed $param7
  957. * @return Zend_Pdf_Page
  958. */
  959. public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, $param7 = null)
  960. {
  961. if ($param5 === null) {
  962. // drawEllipse($x1, $y1, $x2, $y2);
  963. $startAngle = null;
  964. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE;
  965. } else if ($param6 === null) {
  966. // drawEllipse($x1, $y1, $x2, $y2, $fillType);
  967. $startAngle = null;
  968. $fillType = $param5;
  969. } else {
  970. // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  971. // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  972. $startAngle = $param5;
  973. $endAngle = $param6;
  974. if ($param7 === null) {
  975. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE;
  976. } else {
  977. $fillType = $param7;
  978. }
  979. }
  980. $this->_addProcSet('PDF');
  981. if ($x2 < $x1) {
  982. $temp = $x1;
  983. $x1 = $x2;
  984. $x2 = $temp;
  985. }
  986. if ($y2 < $y1) {
  987. $temp = $y1;
  988. $y1 = $y2;
  989. $y2 = $temp;
  990. }
  991. $x = ($x1 + $x2)/2.;
  992. $y = ($y1 + $y2)/2.;
  993. $xC = new Zend_Pdf_Element_Numeric($x);
  994. $yC = new Zend_Pdf_Element_Numeric($y);
  995. if ($startAngle !== null) {
  996. if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
  997. if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
  998. if ($startAngle > $endAngle) {
  999. $endAngle += M_PI*2;
  1000. }
  1001. $clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
  1002. $clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
  1003. $clipRadius = max($x2 - $x1, $y2 - $y1);
  1004. for($count = 0; $count <= $clipSectors; $count++) {
  1005. $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
  1006. $pX = new Zend_Pdf_Element_Numeric($x + cos($pAngle)*$clipRadius);
  1007. $pY = new Zend_Pdf_Element_Numeric($y + sin($pAngle)*$clipRadius);
  1008. $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
  1009. }
  1010. $this->_contents .= "q\n" . $clipPath . "h\nW\nn\n";
  1011. }
  1012. $xLeft = new Zend_Pdf_Element_Numeric($x1);
  1013. $xRight = new Zend_Pdf_Element_Numeric($x2);
  1014. $yUp = new Zend_Pdf_Element_Numeric($y2);
  1015. $yDown = new Zend_Pdf_Element_Numeric($y1);
  1016. $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
  1017. $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
  1018. $xr = new Zend_Pdf_Element_Numeric($x + $xDelta);
  1019. $xl = new Zend_Pdf_Element_Numeric($x - $xDelta);
  1020. $yu = new Zend_Pdf_Element_Numeric($y + $yDelta);
  1021. $yd = new Zend_Pdf_Element_Numeric($y - $yDelta);
  1022. $this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
  1023. . $xr->toString() . ' ' . $yUp->toString() . ' '
  1024. . $xRight->toString() . ' ' . $yu->toString() . ' '
  1025. . $xRight->toString() . ' ' . $yC->toString() . " c\n"
  1026. . $xRight->toString() . ' ' . $yd->toString() . ' '
  1027. . $xr->toString() . ' ' . $yDown->toString() . ' '
  1028. . $xC->toString() . ' ' . $yDown->toString() . " c\n"
  1029. . $xl->toString() . ' ' . $yDown->toString() . ' '
  1030. . $xLeft->toString() . ' ' . $yd->toString() . ' '
  1031. . $xLeft->toString() . ' ' . $yC->toString() . " c\n"
  1032. . $xLeft->toString() . ' ' . $yu->toString() . ' '
  1033. . $xl->toString() . ' ' . $yUp->toString() . ' '
  1034. . $xC->toString() . ' ' . $yUp->toString() . " c\n";
  1035. switch ($fillType) {
  1036. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1037. $this->_contents .= " B*\n";
  1038. break;
  1039. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1040. $this->_contents .= " f*\n";
  1041. break;
  1042. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1043. $this->_contents .= " S\n";
  1044. break;
  1045. }
  1046. if ($startAngle !== null) {
  1047. $this->_contents .= "Q\n";
  1048. }
  1049. return $this;
  1050. }
  1051. /**
  1052. * Draw an image at the specified position on the page.
  1053. *
  1054. * @param Zend_Pdf_Image $image
  1055. * @param float $x1
  1056. * @param float $y1
  1057. * @param float $x2
  1058. * @param float $y2
  1059. * @return Zend_Pdf_Page
  1060. */
  1061. public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2)
  1062. {
  1063. $this->_addProcSet('PDF');
  1064. $imageName = $this->_attachResource('XObject', $image);
  1065. $imageNameObj = new Zend_Pdf_Element_Name($imageName);
  1066. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1067. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1068. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  1069. $heightObj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  1070. $this->_contents .= "q\n"
  1071. . '1 0 0 1 ' . $x1Obj->toString() . ' ' . $y1Obj->toString() . " cm\n"
  1072. . $widthObj->toString() . ' 0 0 ' . $heightObj->toString() . " 0 0 cm\n"
  1073. . $imageNameObj->toString() . " Do\n"
  1074. . "Q\n";
  1075. return $this;
  1076. }
  1077. /**
  1078. * Draw a LayoutBox at the specified position on the page.
  1079. *
  1080. * @param Zend_Pdf_Element_LayoutBox $box
  1081. * @param float $x
  1082. * @param float $y
  1083. * @return Zend_Pdf_Page
  1084. */
  1085. public function drawLayoutBox($box, $x, $y)
  1086. {
  1087. /** @todo implementation */
  1088. return $this;
  1089. }
  1090. /**
  1091. * Draw a line from x1,y1 to x2,y2.
  1092. *
  1093. * @param float $x1
  1094. * @param float $y1
  1095. * @param float $x2
  1096. * @param float $y2
  1097. * @return Zend_Pdf_Page
  1098. */
  1099. public function drawLine($x1, $y1, $x2, $y2)
  1100. {
  1101. $this->_addProcSet('PDF');
  1102. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1103. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1104. $x2Obj = new Zend_Pdf_Element_Numeric($x2);
  1105. $y2Obj = new Zend_Pdf_Element_Numeric($y2);
  1106. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n"
  1107. . $x2Obj->toString() . ' ' . $y2Obj->toString() . " l\n S\n";
  1108. return $this;
  1109. }
  1110. /**
  1111. * Draw a polygon.
  1112. *
  1113. * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
  1114. * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
  1115. * See detailed description of these methods in a PDF documentation
  1116. * (section 4.4.2 Path painting Operators, Filling)
  1117. *
  1118. * @param array $x - array of float (the X co-ordinates of the vertices)
  1119. * @param array $y - array of float (the Y co-ordinates of the vertices)
  1120. * @param integer $fillType
  1121. * @param integer $fillMethod
  1122. * @return Zend_Pdf_Page
  1123. */
  1124. public function drawPolygon($x, $y,
  1125. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  1126. $fillMethod = Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING)
  1127. {
  1128. $this->_addProcSet('PDF');
  1129. $firstPoint = true;
  1130. foreach ($x as $id => $xVal) {
  1131. $xObj = new Zend_Pdf_Element_Numeric($xVal);
  1132. $yObj = new Zend_Pdf_Element_Numeric($y[$id]);
  1133. if ($firstPoint) {
  1134. $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
  1135. $firstPoint = false;
  1136. } else {
  1137. $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n";
  1138. }
  1139. }
  1140. $this->_contents .= $path;
  1141. switch ($fillType) {
  1142. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1143. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  1144. $this->_contents .= " b\n";
  1145. } else {
  1146. // Even-Odd fill method.
  1147. $this->_contents .= " b*\n";
  1148. }
  1149. break;
  1150. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1151. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  1152. $this->_contents .= " h\n f\n";
  1153. } else {
  1154. // Even-Odd fill method.
  1155. $this->_contents .= " h\n f*\n";
  1156. }
  1157. break;
  1158. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1159. $this->_contents .= " S\n";
  1160. break;
  1161. }
  1162. return $this;
  1163. }
  1164. /**
  1165. * Draw a rectangle.
  1166. *
  1167. * Fill types:
  1168. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle and stroke (default)
  1169. * Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke rectangle
  1170. * Zend_Pdf_Page::SHAPE_DRAW_FILL - fill rectangle
  1171. *
  1172. * @param float $x1
  1173. * @param float $y1
  1174. * @param float $x2
  1175. * @param float $y2
  1176. * @param integer $fillType
  1177. * @return Zend_Pdf_Page
  1178. */
  1179. public function drawRectangle($x1, $y1, $x2, $y2, $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE)
  1180. {
  1181. $this->_addProcSet('PDF');
  1182. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1183. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1184. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  1185. $height2Obj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  1186. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' '
  1187. . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n";
  1188. switch ($fillType) {
  1189. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1190. $this->_contents .= " B*\n";
  1191. break;
  1192. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1193. $this->_contents .= " f*\n";
  1194. break;
  1195. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1196. $this->_contents .= " S\n";
  1197. break;
  1198. }
  1199. return $this;
  1200. }
  1201. /**
  1202. * Draw a line of text at the specified position.
  1203. *
  1204. * @param string $text
  1205. * @param float $x
  1206. * @param float $y
  1207. * @param string $charEncoding (optional) Character encoding of source text.
  1208. * Defaults to current locale.
  1209. * @throws Zend_Pdf_Exception
  1210. * @return Zend_Pdf_Page
  1211. */
  1212. public function drawText($text, $x, $y, $charEncoding = '')
  1213. {
  1214. if ($this->_font === null) {
  1215. require_once 'Zend/Pdf/Exception.php';
  1216. throw new Zend_Pdf_Exception('Font has not been set');
  1217. }
  1218. $this->_addProcSet('Text');
  1219. $textObj = new Zend_Pdf_Element_String($this->_font->encodeString($text, $charEncoding));
  1220. $xObj = new Zend_Pdf_Element_Numeric($x);
  1221. $yObj = new Zend_Pdf_Element_Numeric($y);
  1222. $this->_contents .= "BT\n"
  1223. . $xObj->toString() . ' ' . $yObj->toString() . " Td\n"
  1224. . $textObj->toString() . " Tj\n"
  1225. . "ET\n";
  1226. return $this;
  1227. }
  1228. /**
  1229. * Return the height of this page in points.
  1230. *
  1231. * @return float
  1232. */
  1233. public function getHeight()
  1234. {
  1235. return $this->_pageDictionary->MediaBox->items[3]->value -
  1236. $this->_pageDictionary->MediaBox->items[1]->value;
  1237. }
  1238. /**
  1239. * Return the width of this page in points.
  1240. *
  1241. * @return float
  1242. */
  1243. public function getWidth()
  1244. {
  1245. return $this->_pageDictionary->MediaBox->items[2]->value -
  1246. $this->_pageDictionary->MediaBox->items[0]->value;
  1247. }
  1248. /**
  1249. * Close the path by drawing a straight line back to it's beginning.
  1250. *
  1251. * @throws Zend_Pdf_Exception - if a path hasn't been started with pathMove()
  1252. * @return Zend_Pdf_Page
  1253. */
  1254. public function pathClose()
  1255. {
  1256. /** @todo implementation */
  1257. return $this;
  1258. }
  1259. /**
  1260. * Continue the open path in a straight line to the specified position.
  1261. *
  1262. * @param float $x - the X co-ordinate to move to
  1263. * @param float $y - the Y co-ordinate to move to
  1264. * @return Zend_Pdf_Page
  1265. */
  1266. public function pathLine($x, $y)
  1267. {
  1268. /** @todo implementation */
  1269. return $this;
  1270. }
  1271. /**
  1272. * Start a new path at the specified position. If a path has already been started,
  1273. * move the cursor without drawing a line.
  1274. *
  1275. * @param float $x - the X co-ordinate to move to
  1276. * @param float $y - the Y co-ordinate to move to
  1277. * @return Zend_Pdf_Page
  1278. */
  1279. public function pathMove($x, $y)
  1280. {
  1281. /** @todo implementation */
  1282. return $this;
  1283. }
  1284. /**
  1285. * Writes the raw data to the page's content stream.
  1286. *
  1287. * Be sure to consult the PDF reference to ensure your syntax is correct. No
  1288. * attempt is made to ensure the validity of the stream data.
  1289. *
  1290. * @param string $data
  1291. * @param string $procSet (optional) Name of ProcSet to add.
  1292. * @return Zend_Pdf_Page
  1293. */
  1294. public function rawWrite($data, $procSet = null)
  1295. {
  1296. if (! empty($procSet)) {
  1297. $this->_addProcSet($procSet);
  1298. }
  1299. $this->_contents .= $data;
  1300. return $this;
  1301. }
  1302. /**
  1303. * Rotate the page.
  1304. *
  1305. * @param float $x - the X co-ordinate of rotation point
  1306. * @param float $y - the Y co-ordinate of rotation point
  1307. * @param float $angle - rotation angle
  1308. * @return Zend_Pdf_Page
  1309. */
  1310. public function rotate($x, $y, $angle)
  1311. {
  1312. $cos = new Zend_Pdf_Element_Numeric(cos($angle));
  1313. $sin = new Zend_Pdf_Element_Numeric(sin($angle));
  1314. $mSin = new Zend_Pdf_Element_Numeric(-$sin->value);
  1315. $xObj = new Zend_Pdf_Element_Numeric($x);
  1316. $yObj = new Zend_Pdf_Element_Numeric($y);
  1317. $mXObj = new Zend_Pdf_Element_Numeric(-$x);
  1318. $mYObj = new Zend_Pdf_Element_Numeric(-$y);
  1319. $this->_addProcSet('PDF');
  1320. $this->_contents .= '1 0 0 1 ' . $xObj->toString() . ' ' . $yObj->toString() . " cm\n"
  1321. . $cos->toString() . ' ' . $sin->toString() . ' ' . $mSin->toString() . ' ' . $cos->toString() . " 0 0 cm\n"
  1322. . '1 0 0 1 ' . $mXObj->toString() . ' ' . $mYObj->toString() . " cm\n";
  1323. return $this;
  1324. }
  1325. /**
  1326. * Scale coordination system.
  1327. *
  1328. * @param float $xScale - X dimention scale factor
  1329. * @param float $yScale - Y dimention scale factor
  1330. * @return Zend_Pdf_Page
  1331. */
  1332. public function scale($xScale, $yScale)
  1333. {
  1334. $xScaleObj = new Zend_Pdf_Element_Numeric($xScale);
  1335. $yScaleObj = new Zend_Pdf_Element_Numeric($yScale);
  1336. $this->_addProcSet('PDF');
  1337. $this->_contents .= $xScaleObj->toString() . ' 0 0 ' . $yScaleObj->toString() . " 0 0 cm\n";
  1338. return $this;
  1339. }
  1340. /**
  1341. * Translate coordination system.
  1342. *
  1343. * @param float $xShift - X coordinate shift
  1344. * @param float $yShift - Y coordinate shift
  1345. * @return Zend_Pdf_Page
  1346. */
  1347. public function translate($xShift, $yShift)
  1348. {
  1349. $xShiftObj = new Zend_Pdf_Element_Numeric($xShift);
  1350. $yShiftObj = new Zend_Pdf_Element_Numeric($yShift);
  1351. $this->_addProcSet('PDF');
  1352. $this->_contents .= '1 0 0 1 ' . $xShiftObj->toString() . ' ' . $yShiftObj->toString() . " cm\n";
  1353. return $this;
  1354. }
  1355. /**
  1356. * Translate coordination system.
  1357. *
  1358. * @param float $x - the X co-ordinate of axis skew point
  1359. * @param float $y - the Y co-ordinate of axis skew point
  1360. * @param float $xAngle - X axis skew angle
  1361. * @param float $yAngle - Y axis skew angle
  1362. * @return Zend_Pdf_Page
  1363. */
  1364. public function skew($x, $y, $xAngle, $yAngle)
  1365. {
  1366. $tanXObj = new Zend_Pdf_Element_Numeric(tan($xAngle));
  1367. $tanYObj = new Zend_Pdf_Element_Numeric(-tan($yAngle));
  1368. $xObj = new Zend_Pdf_Element_Numeric($x);
  1369. $yObj = new Zend_Pdf_Element_Numeric($y);
  1370. $mXObj = new Zend_Pdf_Element_Numeric(-$x);
  1371. $mYObj = new Zend_Pdf_Element_Numeric(-$y);
  1372. $this->_addProcSet('PDF');
  1373. $this->_contents .= '1 0 0 1 ' . $xObj->toString() . ' ' . $yObj->toString() . " cm\n"
  1374. . '1 ' . $tanXObj->toString() . ' ' . $tanYObj->toString() . " 1 0 0 cm\n"
  1375. . '1 0 0 1 ' . $mXObj->toString() . ' ' . $mYObj->toString() . " cm\n";
  1376. return $this;
  1377. }
  1378. }