2
0

Page.php 53 KB

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