Page.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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. * Reference to the object with page dictionary.
  95. *
  96. * @var Zend_Pdf_Element_Reference
  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 instractions.
  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. * @return Zend_Pdf_Element_Dictionary
  355. */
  356. public function getPageDictionary()
  357. {
  358. return $this->_pageDictionary;
  359. }
  360. /**
  361. * Dump current drawing instructions into the content stream.
  362. *
  363. * @todo Don't forget to close all current graphics operations (like path drawing)
  364. *
  365. * @throws Zend_Pdf_Exception
  366. */
  367. public function flush()
  368. {
  369. if ($this->_saveCount != 0) {
  370. require_once 'Zend/Pdf/Exception.php';
  371. throw new Zend_Pdf_Exception('Saved graphics state is not restored');
  372. }
  373. if ($this->_contents == '') {
  374. return;
  375. }
  376. if ($this->_pageDictionary->Contents->getType() != Zend_Pdf_Element::TYPE_ARRAY) {
  377. /**
  378. * It's a stream object.
  379. * Prepare Contents page attribute for update.
  380. */
  381. $this->_pageDictionary->touch();
  382. $currentPageContents = $this->_pageDictionary->Contents;
  383. $this->_pageDictionary->Contents = new Zend_Pdf_Element_Array();
  384. $this->_pageDictionary->Contents->items[] = $currentPageContents;
  385. } else {
  386. $this->_pageDictionary->Contents->touch();
  387. }
  388. if ((!$this->_safeGS) && (count($this->_pageDictionary->Contents->items) != 0)) {
  389. /**
  390. * Page already has some content which is not treated as safe.
  391. *
  392. * Add save/restore GS operators
  393. */
  394. $this->_addProcSet('PDF');
  395. $newContentsArray = new Zend_Pdf_Element_Array();
  396. $newContentsArray->items[] = $this->_objFactory->newStreamObject(" q\n");
  397. foreach ($this->_pageDictionary->Contents->items as $contentStream) {
  398. $newContentsArray->items[] = $contentStream;
  399. }
  400. $newContentsArray->items[] = $this->_objFactory->newStreamObject(" Q\n");
  401. $this->_pageDictionary->touch();
  402. $this->_pageDictionary->Contents = $newContentsArray;
  403. $this->_safeGS = true;
  404. }
  405. $this->_pageDictionary->Contents->items[] =
  406. $this->_objFactory->newStreamObject($this->_contents);
  407. $this->_contents = '';
  408. }
  409. /**
  410. * Prepare page to be rendered into PDF.
  411. *
  412. * @todo Don't forget to close all current graphics operations (like path drawing)
  413. *
  414. * @param Zend_Pdf_ElementFactory_Interface $objFactory
  415. * @throws Zend_Pdf_Exception
  416. */
  417. public function render(Zend_Pdf_ElementFactory_Interface $objFactory)
  418. {
  419. $this->flush();
  420. if ($objFactory === $this->_objFactory) {
  421. // Page is already attached to the document.
  422. return;
  423. }
  424. if ($this->_attached) {
  425. require_once 'Zend/Pdf/Exception.php';
  426. throw new Zend_Pdf_Exception('Page is attached to one documen, but rendered in context of another.');
  427. /**
  428. * @todo Page cloning must be implemented here instead of exception.
  429. * PDF objects (ex. fonts) can be shared between pages.
  430. * Thus all referenced objects, which can be modified, must be cloned recursively,
  431. * to avoid producing wrong object references in a context of source PDF.
  432. */
  433. //...
  434. } else {
  435. $objFactory->attach($this->_objFactory);
  436. }
  437. }
  438. /**
  439. * Set fill color.
  440. *
  441. * @param Zend_Pdf_Color $color
  442. * @return Zend_Pdf_Page
  443. */
  444. public function setFillColor(Zend_Pdf_Color $color)
  445. {
  446. $this->_addProcSet('PDF');
  447. $this->_contents .= $color->instructions(false);
  448. return $this;
  449. }
  450. /**
  451. * Set line color.
  452. *
  453. * @param Zend_Pdf_Color $color
  454. * @return Zend_Pdf_Page
  455. */
  456. public function setLineColor(Zend_Pdf_Color $color)
  457. {
  458. $this->_addProcSet('PDF');
  459. $this->_contents .= $color->instructions(true);
  460. return $this;
  461. }
  462. /**
  463. * Set line width.
  464. *
  465. * @param float $width
  466. * @return Zend_Pdf_Page
  467. */
  468. public function setLineWidth($width)
  469. {
  470. $this->_addProcSet('PDF');
  471. $widthObj = new Zend_Pdf_Element_Numeric($width);
  472. $this->_contents .= $widthObj->toString() . " w\n";
  473. return $this;
  474. }
  475. /**
  476. * Set line dashing pattern
  477. *
  478. * Pattern is an array of floats: array(on_length, off_length, on_length, off_length, ...)
  479. * Phase is shift from the beginning of line.
  480. *
  481. * @param array $pattern
  482. * @param array $phase
  483. * @return Zend_Pdf_Page
  484. */
  485. public function setLineDashingPattern($pattern, $phase = 0)
  486. {
  487. $this->_addProcSet('PDF');
  488. if ($pattern === Zend_Pdf_Page::LINE_DASHING_SOLID) {
  489. $pattern = array();
  490. $phase = 0;
  491. }
  492. $dashPattern = new Zend_Pdf_Element_Array();
  493. $phaseEleemnt = new Zend_Pdf_Element_Numeric($phase);
  494. foreach ($pattern as $dashItem) {
  495. $dashElement = new Zend_Pdf_Element_Numeric($dashItem);
  496. $dashPattern->items[] = $dashElement;
  497. }
  498. $this->_contents .= $dashPattern->toString() . ' '
  499. . $phaseEleemnt->toString() . " d\n";
  500. return $this;
  501. }
  502. /**
  503. * Set current font.
  504. *
  505. * @param Zend_Pdf_Resource_Font $font
  506. * @param float $fontSize
  507. * @return Zend_Pdf_Page
  508. */
  509. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize)
  510. {
  511. $this->_addProcSet('Text');
  512. $fontName = $this->_attachResource('Font', $font);
  513. $this->_font = $font;
  514. $this->_fontSize = $fontSize;
  515. $fontNameObj = new Zend_Pdf_Element_Name($fontName);
  516. $fontSizeObj = new Zend_Pdf_Element_Numeric($fontSize);
  517. $this->_contents .= $fontNameObj->toString() . ' ' . $fontSizeObj->toString() . " Tf\n";
  518. return $this;
  519. }
  520. /**
  521. * Set the style to use for future drawing operations on this page
  522. *
  523. * @param Zend_Pdf_Style $style
  524. * @return Zend_Pdf_Page
  525. */
  526. public function setStyle(Zend_Pdf_Style $style)
  527. {
  528. $this->_style = $style;
  529. $this->_addProcSet('Text');
  530. $this->_addProcSet('PDF');
  531. if ($style->getFont() !== null) {
  532. $this->setFont($style->getFont(), $style->getFontSize());
  533. }
  534. $this->_contents .= $style->instructions($this->_pageDictionary->Resources);
  535. return $this;
  536. }
  537. /**
  538. * Set the transparancy
  539. *
  540. * $alpha == 0 - transparent
  541. * $alpha == 1 - opaque
  542. *
  543. * Transparency modes, supported by PDF:
  544. * Normal (default), Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight,
  545. * SoftLight, Difference, Exclusion
  546. *
  547. * @param float $alpha
  548. * @param string $mode
  549. * @throws Zend_Pdf_Exception
  550. * @return Zend_Pdf_Page
  551. */
  552. public function setAlpha($alpha, $mode = 'Normal')
  553. {
  554. if (!in_array($mode, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge',
  555. 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion'))) {
  556. require_once 'Zend/Pdf/Exception.php';
  557. throw new Zend_Pdf_Exception('Unsupported transparency mode.');
  558. }
  559. if (!is_numeric($alpha) || $alpha < 0 || $alpha > 1) {
  560. require_once 'Zend/Pdf/Exception.php';
  561. throw new Zend_Pdf_Exception('Alpha value must be numeric between 0 (transparent) and 1 (opaque).');
  562. }
  563. $this->_addProcSet('Text');
  564. $this->_addProcSet('PDF');
  565. $resources = $this->_pageDictionary->Resources;
  566. // Check if Resources dictionary contains ExtGState entry
  567. if ($resources->ExtGState === null) {
  568. $resources->touch();
  569. $resources->ExtGState = new Zend_Pdf_Element_Dictionary();
  570. } else {
  571. $resources->ExtGState->touch();
  572. }
  573. $idCounter = 1;
  574. do {
  575. $gStateName = 'GS' . $idCounter++;
  576. } while ($resources->ExtGState->$gStateName !== null);
  577. $gStateDictionary = new Zend_Pdf_Element_Dictionary();
  578. $gStateDictionary->Type = new Zend_Pdf_Element_Name('ExtGState');
  579. $gStateDictionary->BM = new Zend_Pdf_Element_Name($mode);
  580. $gStateDictionary->CA = new Zend_Pdf_Element_Numeric($alpha);
  581. $gStateDictionary->ca = new Zend_Pdf_Element_Numeric($alpha);
  582. $resources->ExtGState->$gStateName = $this->_objFactory->newObject($gStateDictionary);
  583. $gStateNameObj = new Zend_Pdf_Element_Name($gStateName);
  584. $this->_contents .= $gStateNameObj->toString() . " gs\n";
  585. return $this;
  586. }
  587. /**
  588. * Get current font.
  589. *
  590. * @return Zend_Pdf_Resource_Font $font
  591. */
  592. public function getFont()
  593. {
  594. return $this->_font;
  595. }
  596. /**
  597. * Extract resources attached to the page
  598. *
  599. * This method is not intended to be used in userland, but helps to optimize some document wide operations
  600. *
  601. * returns array of Zend_Pdf_Element_Dictionary objects
  602. *
  603. * @internal
  604. * @return array
  605. */
  606. public function extractResources()
  607. {
  608. return $this->_pageDictionary->Resources;
  609. }
  610. /**
  611. * Extract fonts attached to the page
  612. *
  613. * returns array of Zend_Pdf_Resource_Font_Extracted objects
  614. *
  615. * @return array
  616. */
  617. public function extractFonts()
  618. {
  619. if ($this->_pageDictionary->Resources->Font === null) {
  620. // Page doesn't have any font attached
  621. // Return empty array
  622. return array();
  623. }
  624. $fontResources = $this->_pageDictionary->Resources->Font;
  625. $fontResourcesUnique = array();
  626. foreach ($fontResources->getKeys() as $fontResourceName) {
  627. $fontDictionary = $fontResources->$fontResourceName;
  628. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  629. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  630. // Font dictionary has to be an indirect object or object reference
  631. continue;
  632. }
  633. $fontResourcesUnique[$fontDictionary->toString($this->_objFactory)] = $fontDictionary;
  634. }
  635. $fonts = array();
  636. require_once 'Zend/Pdf/Exception.php';
  637. foreach ($fontResourcesUnique as $resourceReference => $fontDictionary) {
  638. try {
  639. // Try to extract font
  640. $extractedFont = new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  641. $fonts[$resourceReference] = $extractedFont;
  642. } catch (Zend_Pdf_Exception $e) {
  643. if ($e->getMessage() != 'Unsupported font type.') {
  644. throw $e;
  645. }
  646. }
  647. }
  648. return $fonts;
  649. }
  650. /**
  651. * Extract font attached to the page by specific font name
  652. *
  653. * $fontName should be specified in UTF-8 encoding
  654. *
  655. * @return Zend_Pdf_Resource_Font_Extracted|null
  656. */
  657. public function extractFont($fontName)
  658. {
  659. if ($this->_pageDictionary->Resources->Font === null) {
  660. // Page doesn't have any font attached
  661. return null;
  662. }
  663. $fontResources = $this->_pageDictionary->Resources->Font;
  664. require_once 'Zend/Pdf/Exception.php';
  665. foreach ($fontResources->getKeys() as $fontResourceName) {
  666. $fontDictionary = $fontResources->$fontResourceName;
  667. if (! ($fontDictionary instanceof Zend_Pdf_Element_Reference ||
  668. $fontDictionary instanceof Zend_Pdf_Element_Object) ) {
  669. // Font dictionary has to be an indirect object or object reference
  670. continue;
  671. }
  672. if ($fontDictionary->BaseFont->value != $fontName) {
  673. continue;
  674. }
  675. try {
  676. // Try to extract font
  677. return new Zend_Pdf_Resource_Font_Extracted($fontDictionary);
  678. } catch (Zend_Pdf_Exception $e) {
  679. if ($e->getMessage() != 'Unsupported font type.') {
  680. throw $e;
  681. }
  682. // Continue searhing font with specified name
  683. }
  684. }
  685. return null;
  686. }
  687. /**
  688. * Get current font size
  689. *
  690. * @return float $fontSize
  691. */
  692. public function getFontSize()
  693. {
  694. return $this->_fontSize;
  695. }
  696. /**
  697. * Return the style, applied to the page.
  698. *
  699. * @return Zend_Pdf_Style|null
  700. */
  701. public function getStyle()
  702. {
  703. return $this->_style;
  704. }
  705. /**
  706. * Save the graphics state of this page.
  707. * This takes a snapshot of the currently applied style, position, clipping area and
  708. * any rotation/translation/scaling that has been applied.
  709. *
  710. * @todo check for the open paths
  711. * @throws Zend_Pdf_Exception - if a save is performed with an open path
  712. * @return Zend_Pdf_Page
  713. */
  714. public function saveGS()
  715. {
  716. $this->_saveCount++;
  717. $this->_addProcSet('PDF');
  718. $this->_contents .= " q\n";
  719. return $this;
  720. }
  721. /**
  722. * Restore the graphics state that was saved with the last call to saveGS().
  723. *
  724. * @throws Zend_Pdf_Exception - if there is no previously saved state
  725. * @return Zend_Pdf_Page
  726. */
  727. public function restoreGS()
  728. {
  729. if ($this->_saveCount-- <= 0) {
  730. require_once 'Zend/Pdf/Exception.php';
  731. throw new Zend_Pdf_Exception('Restoring graphics state which is not saved');
  732. }
  733. $this->_contents .= " Q\n";
  734. return $this;
  735. }
  736. /**
  737. * Intersect current clipping area with a circle.
  738. *
  739. * @param float $x
  740. * @param float $y
  741. * @param float $radius
  742. * @param float $startAngle
  743. * @param float $endAngle
  744. * @return Zend_Pdf_Page
  745. */
  746. public function clipCircle($x, $y, $radius, $startAngle = null, $endAngle = null)
  747. {
  748. $this->clipEllipse($x - $radius, $y - $radius,
  749. $x + $radius, $y + $radius,
  750. $startAngle, $endAngle);
  751. return $this;
  752. }
  753. /**
  754. * Intersect current clipping area with a polygon.
  755. *
  756. * Method signatures:
  757. * drawEllipse($x1, $y1, $x2, $y2);
  758. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  759. *
  760. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  761. *
  762. * @param float $x1
  763. * @param float $y1
  764. * @param float $x2
  765. * @param float $y2
  766. * @param float $startAngle
  767. * @param float $endAngle
  768. * @return Zend_Pdf_Page
  769. */
  770. public function clipEllipse($x1, $y1, $x2, $y2, $startAngle = null, $endAngle = null)
  771. {
  772. $this->_addProcSet('PDF');
  773. if ($x2 < $x1) {
  774. $temp = $x1;
  775. $x1 = $x2;
  776. $x2 = $temp;
  777. }
  778. if ($y2 < $y1) {
  779. $temp = $y1;
  780. $y1 = $y2;
  781. $y2 = $temp;
  782. }
  783. $x = ($x1 + $x2)/2.;
  784. $y = ($y1 + $y2)/2.;
  785. $xC = new Zend_Pdf_Element_Numeric($x);
  786. $yC = new Zend_Pdf_Element_Numeric($y);
  787. if ($startAngle !== null) {
  788. if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
  789. if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
  790. if ($startAngle > $endAngle) {
  791. $endAngle += M_PI*2;
  792. }
  793. $clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
  794. $clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
  795. $clipRadius = max($x2 - $x1, $y2 - $y1);
  796. for($count = 0; $count <= $clipSectors; $count++) {
  797. $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
  798. $pX = new Zend_Pdf_Element_Numeric($x + cos($pAngle)*$clipRadius);
  799. $pY = new Zend_Pdf_Element_Numeric($y + sin($pAngle)*$clipRadius);
  800. $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
  801. }
  802. $this->_contents .= $clipPath . "h\nW\nn\n";
  803. }
  804. $xLeft = new Zend_Pdf_Element_Numeric($x1);
  805. $xRight = new Zend_Pdf_Element_Numeric($x2);
  806. $yUp = new Zend_Pdf_Element_Numeric($y2);
  807. $yDown = new Zend_Pdf_Element_Numeric($y1);
  808. $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
  809. $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
  810. $xr = new Zend_Pdf_Element_Numeric($x + $xDelta);
  811. $xl = new Zend_Pdf_Element_Numeric($x - $xDelta);
  812. $yu = new Zend_Pdf_Element_Numeric($y + $yDelta);
  813. $yd = new Zend_Pdf_Element_Numeric($y - $yDelta);
  814. $this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
  815. . $xr->toString() . ' ' . $yUp->toString() . ' '
  816. . $xRight->toString() . ' ' . $yu->toString() . ' '
  817. . $xRight->toString() . ' ' . $yC->toString() . " c\n"
  818. . $xRight->toString() . ' ' . $yd->toString() . ' '
  819. . $xr->toString() . ' ' . $yDown->toString() . ' '
  820. . $xC->toString() . ' ' . $yDown->toString() . " c\n"
  821. . $xl->toString() . ' ' . $yDown->toString() . ' '
  822. . $xLeft->toString() . ' ' . $yd->toString() . ' '
  823. . $xLeft->toString() . ' ' . $yC->toString() . " c\n"
  824. . $xLeft->toString() . ' ' . $yu->toString() . ' '
  825. . $xl->toString() . ' ' . $yUp->toString() . ' '
  826. . $xC->toString() . ' ' . $yUp->toString() . " c\n"
  827. . "h\nW\nn\n";
  828. return $this;
  829. }
  830. /**
  831. * Intersect current clipping area with a polygon.
  832. *
  833. * @param array $x - array of float (the X co-ordinates of the vertices)
  834. * @param array $y - array of float (the Y co-ordinates of the vertices)
  835. * @param integer $fillMethod
  836. * @return Zend_Pdf_Page
  837. */
  838. public function clipPolygon($x, $y, $fillMethod = Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING)
  839. {
  840. $this->_addProcSet('PDF');
  841. $firstPoint = true;
  842. foreach ($x as $id => $xVal) {
  843. $xObj = new Zend_Pdf_Element_Numeric($xVal);
  844. $yObj = new Zend_Pdf_Element_Numeric($y[$id]);
  845. if ($firstPoint) {
  846. $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
  847. $firstPoint = false;
  848. } else {
  849. $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n";
  850. }
  851. }
  852. $this->_contents .= $path;
  853. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  854. $this->_contents .= " h\n W\nn\n";
  855. } else {
  856. // Even-Odd fill method.
  857. $this->_contents .= " h\n W*\nn\n";
  858. }
  859. return $this;
  860. }
  861. /**
  862. * Intersect current clipping area with a rectangle.
  863. *
  864. * @param float $x1
  865. * @param float $y1
  866. * @param float $x2
  867. * @param float $y2
  868. * @return Zend_Pdf_Page
  869. */
  870. public function clipRectangle($x1, $y1, $x2, $y2)
  871. {
  872. $this->_addProcSet('PDF');
  873. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  874. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  875. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  876. $height2Obj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  877. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' '
  878. . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n"
  879. . " W\nn\n";
  880. return $this;
  881. }
  882. /**
  883. * Draw a Zend_Pdf_ContentStream at the specified position on the page
  884. *
  885. * @param ZPdfContentStream $cs
  886. * @param float $x1
  887. * @param float $y1
  888. * @param float $x2
  889. * @param float $y2
  890. * @return Zend_Pdf_Page
  891. */
  892. public function drawContentStream($cs, $x1, $y1, $x2, $y2)
  893. {
  894. /** @todo implementation */
  895. return $this;
  896. }
  897. /**
  898. * Draw a circle centered on x, y with a radius of radius.
  899. *
  900. * Method signatures:
  901. * drawCircle($x, $y, $radius);
  902. * drawCircle($x, $y, $radius, $fillType);
  903. * drawCircle($x, $y, $radius, $startAngle, $endAngle);
  904. * drawCircle($x, $y, $radius, $startAngle, $endAngle, $fillType);
  905. *
  906. *
  907. * It's not a really circle, because PDF supports only cubic Bezier curves.
  908. * But _very_ good approximation.
  909. * It differs from a real circle on a maximum 0.00026 radiuses
  910. * (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).
  911. * 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.
  912. *
  913. * @param float $x
  914. * @param float $y
  915. * @param float $radius
  916. * @param mixed $param4
  917. * @param mixed $param5
  918. * @param mixed $param6
  919. * @return Zend_Pdf_Page
  920. */
  921. public function drawCircle($x, $y, $radius, $param4 = null, $param5 = null, $param6 = null)
  922. {
  923. $this->drawEllipse($x - $radius, $y - $radius,
  924. $x + $radius, $y + $radius,
  925. $param4, $param5, $param6);
  926. return $this;
  927. }
  928. /**
  929. * Draw an ellipse inside the specified rectangle.
  930. *
  931. * Method signatures:
  932. * drawEllipse($x1, $y1, $x2, $y2);
  933. * drawEllipse($x1, $y1, $x2, $y2, $fillType);
  934. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  935. * drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  936. *
  937. * @todo process special cases with $x2-$x1 == 0 or $y2-$y1 == 0
  938. *
  939. * @param float $x1
  940. * @param float $y1
  941. * @param float $x2
  942. * @param float $y2
  943. * @param mixed $param5
  944. * @param mixed $param6
  945. * @param mixed $param7
  946. * @return Zend_Pdf_Page
  947. */
  948. public function drawEllipse($x1, $y1, $x2, $y2, $param5 = null, $param6 = null, $param7 = null)
  949. {
  950. if ($param5 === null) {
  951. // drawEllipse($x1, $y1, $x2, $y2);
  952. $startAngle = null;
  953. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE;
  954. } else if ($param6 === null) {
  955. // drawEllipse($x1, $y1, $x2, $y2, $fillType);
  956. $startAngle = null;
  957. $fillType = $param5;
  958. } else {
  959. // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle);
  960. // drawEllipse($x1, $y1, $x2, $y2, $startAngle, $endAngle, $fillType);
  961. $startAngle = $param5;
  962. $endAngle = $param6;
  963. if ($param7 === null) {
  964. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE;
  965. } else {
  966. $fillType = $param7;
  967. }
  968. }
  969. $this->_addProcSet('PDF');
  970. if ($x2 < $x1) {
  971. $temp = $x1;
  972. $x1 = $x2;
  973. $x2 = $temp;
  974. }
  975. if ($y2 < $y1) {
  976. $temp = $y1;
  977. $y1 = $y2;
  978. $y2 = $temp;
  979. }
  980. $x = ($x1 + $x2)/2.;
  981. $y = ($y1 + $y2)/2.;
  982. $xC = new Zend_Pdf_Element_Numeric($x);
  983. $yC = new Zend_Pdf_Element_Numeric($y);
  984. if ($startAngle !== null) {
  985. if ($startAngle != 0) { $startAngle = fmod($startAngle, M_PI*2); }
  986. if ($endAngle != 0) { $endAngle = fmod($endAngle, M_PI*2); }
  987. if ($startAngle > $endAngle) {
  988. $endAngle += M_PI*2;
  989. }
  990. $clipPath = $xC->toString() . ' ' . $yC->toString() . " m\n";
  991. $clipSectors = (int)ceil(($endAngle - $startAngle)/M_PI_4);
  992. $clipRadius = max($x2 - $x1, $y2 - $y1);
  993. for($count = 0; $count <= $clipSectors; $count++) {
  994. $pAngle = $startAngle + ($endAngle - $startAngle)*$count/(float)$clipSectors;
  995. $pX = new Zend_Pdf_Element_Numeric($x + cos($pAngle)*$clipRadius);
  996. $pY = new Zend_Pdf_Element_Numeric($y + sin($pAngle)*$clipRadius);
  997. $clipPath .= $pX->toString() . ' ' . $pY->toString() . " l\n";
  998. }
  999. $this->_contents .= "q\n" . $clipPath . "h\nW\nn\n";
  1000. }
  1001. $xLeft = new Zend_Pdf_Element_Numeric($x1);
  1002. $xRight = new Zend_Pdf_Element_Numeric($x2);
  1003. $yUp = new Zend_Pdf_Element_Numeric($y2);
  1004. $yDown = new Zend_Pdf_Element_Numeric($y1);
  1005. $xDelta = 2*(M_SQRT2 - 1)*($x2 - $x1)/3.;
  1006. $yDelta = 2*(M_SQRT2 - 1)*($y2 - $y1)/3.;
  1007. $xr = new Zend_Pdf_Element_Numeric($x + $xDelta);
  1008. $xl = new Zend_Pdf_Element_Numeric($x - $xDelta);
  1009. $yu = new Zend_Pdf_Element_Numeric($y + $yDelta);
  1010. $yd = new Zend_Pdf_Element_Numeric($y - $yDelta);
  1011. $this->_contents .= $xC->toString() . ' ' . $yUp->toString() . " m\n"
  1012. . $xr->toString() . ' ' . $yUp->toString() . ' '
  1013. . $xRight->toString() . ' ' . $yu->toString() . ' '
  1014. . $xRight->toString() . ' ' . $yC->toString() . " c\n"
  1015. . $xRight->toString() . ' ' . $yd->toString() . ' '
  1016. . $xr->toString() . ' ' . $yDown->toString() . ' '
  1017. . $xC->toString() . ' ' . $yDown->toString() . " c\n"
  1018. . $xl->toString() . ' ' . $yDown->toString() . ' '
  1019. . $xLeft->toString() . ' ' . $yd->toString() . ' '
  1020. . $xLeft->toString() . ' ' . $yC->toString() . " c\n"
  1021. . $xLeft->toString() . ' ' . $yu->toString() . ' '
  1022. . $xl->toString() . ' ' . $yUp->toString() . ' '
  1023. . $xC->toString() . ' ' . $yUp->toString() . " c\n";
  1024. switch ($fillType) {
  1025. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1026. $this->_contents .= " B*\n";
  1027. break;
  1028. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1029. $this->_contents .= " f*\n";
  1030. break;
  1031. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1032. $this->_contents .= " S\n";
  1033. break;
  1034. }
  1035. if ($startAngle !== null) {
  1036. $this->_contents .= "Q\n";
  1037. }
  1038. return $this;
  1039. }
  1040. /**
  1041. * Draw an image at the specified position on the page.
  1042. *
  1043. * @param Zend_Pdf_Image $image
  1044. * @param float $x1
  1045. * @param float $y1
  1046. * @param float $x2
  1047. * @param float $y2
  1048. * @return Zend_Pdf_Page
  1049. */
  1050. public function drawImage(Zend_Pdf_Resource_Image $image, $x1, $y1, $x2, $y2)
  1051. {
  1052. $this->_addProcSet('PDF');
  1053. $imageName = $this->_attachResource('XObject', $image);
  1054. $imageNameObj = new Zend_Pdf_Element_Name($imageName);
  1055. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1056. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1057. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  1058. $heightObj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  1059. $this->_contents .= "q\n"
  1060. . '1 0 0 1 ' . $x1Obj->toString() . ' ' . $y1Obj->toString() . " cm\n"
  1061. . $widthObj->toString() . ' 0 0 ' . $heightObj->toString() . " 0 0 cm\n"
  1062. . $imageNameObj->toString() . " Do\n"
  1063. . "Q\n";
  1064. return $this;
  1065. }
  1066. /**
  1067. * Draw a LayoutBox at the specified position on the page.
  1068. *
  1069. * @param Zend_Pdf_Element_LayoutBox $box
  1070. * @param float $x
  1071. * @param float $y
  1072. * @return Zend_Pdf_Page
  1073. */
  1074. public function drawLayoutBox($box, $x, $y)
  1075. {
  1076. /** @todo implementation */
  1077. return $this;
  1078. }
  1079. /**
  1080. * Draw a line from x1,y1 to x2,y2.
  1081. *
  1082. * @param float $x1
  1083. * @param float $y1
  1084. * @param float $x2
  1085. * @param float $y2
  1086. * @return Zend_Pdf_Page
  1087. */
  1088. public function drawLine($x1, $y1, $x2, $y2)
  1089. {
  1090. $this->_addProcSet('PDF');
  1091. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1092. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1093. $x2Obj = new Zend_Pdf_Element_Numeric($x2);
  1094. $y2Obj = new Zend_Pdf_Element_Numeric($y2);
  1095. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . " m\n"
  1096. . $x2Obj->toString() . ' ' . $y2Obj->toString() . " l\n S\n";
  1097. return $this;
  1098. }
  1099. /**
  1100. * Draw a polygon.
  1101. *
  1102. * If $fillType is Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE or
  1103. * Zend_Pdf_Page::SHAPE_DRAW_FILL, then polygon is automatically closed.
  1104. * See detailed description of these methods in a PDF documentation
  1105. * (section 4.4.2 Path painting Operators, Filling)
  1106. *
  1107. * @param array $x - array of float (the X co-ordinates of the vertices)
  1108. * @param array $y - array of float (the Y co-ordinates of the vertices)
  1109. * @param integer $fillType
  1110. * @param integer $fillMethod
  1111. * @return Zend_Pdf_Page
  1112. */
  1113. public function drawPolygon($x, $y,
  1114. $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  1115. $fillMethod = Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING)
  1116. {
  1117. $this->_addProcSet('PDF');
  1118. $firstPoint = true;
  1119. foreach ($x as $id => $xVal) {
  1120. $xObj = new Zend_Pdf_Element_Numeric($xVal);
  1121. $yObj = new Zend_Pdf_Element_Numeric($y[$id]);
  1122. if ($firstPoint) {
  1123. $path = $xObj->toString() . ' ' . $yObj->toString() . " m\n";
  1124. $firstPoint = false;
  1125. } else {
  1126. $path .= $xObj->toString() . ' ' . $yObj->toString() . " l\n";
  1127. }
  1128. }
  1129. $this->_contents .= $path;
  1130. switch ($fillType) {
  1131. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1132. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  1133. $this->_contents .= " b\n";
  1134. } else {
  1135. // Even-Odd fill method.
  1136. $this->_contents .= " b*\n";
  1137. }
  1138. break;
  1139. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1140. if ($fillMethod == Zend_Pdf_Page::FILL_METHOD_NON_ZERO_WINDING) {
  1141. $this->_contents .= " h\n f\n";
  1142. } else {
  1143. // Even-Odd fill method.
  1144. $this->_contents .= " h\n f*\n";
  1145. }
  1146. break;
  1147. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1148. $this->_contents .= " S\n";
  1149. break;
  1150. }
  1151. return $this;
  1152. }
  1153. /**
  1154. * Draw a rectangle.
  1155. *
  1156. * Fill types:
  1157. * Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE - fill rectangle and stroke (default)
  1158. * Zend_Pdf_Page::SHAPE_DRAW_STROKE - stroke rectangle
  1159. * Zend_Pdf_Page::SHAPE_DRAW_FILL - fill rectangle
  1160. *
  1161. * @param float $x1
  1162. * @param float $y1
  1163. * @param float $x2
  1164. * @param float $y2
  1165. * @param integer $fillType
  1166. * @return Zend_Pdf_Page
  1167. */
  1168. public function drawRectangle($x1, $y1, $x2, $y2, $fillType = Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE)
  1169. {
  1170. $this->_addProcSet('PDF');
  1171. $x1Obj = new Zend_Pdf_Element_Numeric($x1);
  1172. $y1Obj = new Zend_Pdf_Element_Numeric($y1);
  1173. $widthObj = new Zend_Pdf_Element_Numeric($x2 - $x1);
  1174. $height2Obj = new Zend_Pdf_Element_Numeric($y2 - $y1);
  1175. $this->_contents .= $x1Obj->toString() . ' ' . $y1Obj->toString() . ' '
  1176. . $widthObj->toString() . ' ' . $height2Obj->toString() . " re\n";
  1177. switch ($fillType) {
  1178. case Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE:
  1179. $this->_contents .= " B*\n";
  1180. break;
  1181. case Zend_Pdf_Page::SHAPE_DRAW_FILL:
  1182. $this->_contents .= " f*\n";
  1183. break;
  1184. case Zend_Pdf_Page::SHAPE_DRAW_STROKE:
  1185. $this->_contents .= " S\n";
  1186. break;
  1187. }
  1188. return $this;
  1189. }
  1190. /**
  1191. * Draw a line of text at the specified position.
  1192. *
  1193. * @param string $text
  1194. * @param float $x
  1195. * @param float $y
  1196. * @param string $charEncoding (optional) Character encoding of source text.
  1197. * Defaults to current locale.
  1198. * @throws Zend_Pdf_Exception
  1199. * @return Zend_Pdf_Page
  1200. */
  1201. public function drawText($text, $x, $y, $charEncoding = '')
  1202. {
  1203. if ($this->_font === null) {
  1204. require_once 'Zend/Pdf/Exception.php';
  1205. throw new Zend_Pdf_Exception('Font has not been set');
  1206. }
  1207. $this->_addProcSet('Text');
  1208. $textObj = new Zend_Pdf_Element_String($this->_font->encodeString($text, $charEncoding));
  1209. $xObj = new Zend_Pdf_Element_Numeric($x);
  1210. $yObj = new Zend_Pdf_Element_Numeric($y);
  1211. $this->_contents .= "BT\n"
  1212. . $xObj->toString() . ' ' . $yObj->toString() . " Td\n"
  1213. . $textObj->toString() . " Tj\n"
  1214. . "ET\n";
  1215. return $this;
  1216. }
  1217. /**
  1218. * Return the height of this page in points.
  1219. *
  1220. * @return float
  1221. */
  1222. public function getHeight()
  1223. {
  1224. return $this->_pageDictionary->MediaBox->items[3]->value -
  1225. $this->_pageDictionary->MediaBox->items[1]->value;
  1226. }
  1227. /**
  1228. * Return the width of this page in points.
  1229. *
  1230. * @return float
  1231. */
  1232. public function getWidth()
  1233. {
  1234. return $this->_pageDictionary->MediaBox->items[2]->value -
  1235. $this->_pageDictionary->MediaBox->items[0]->value;
  1236. }
  1237. /**
  1238. * Close the path by drawing a straight line back to it's beginning.
  1239. *
  1240. * @throws Zend_Pdf_Exception - if a path hasn't been started with pathMove()
  1241. * @return Zend_Pdf_Page
  1242. */
  1243. public function pathClose()
  1244. {
  1245. /** @todo implementation */
  1246. return $this;
  1247. }
  1248. /**
  1249. * Continue the open path in a straight line to the specified position.
  1250. *
  1251. * @param float $x - the X co-ordinate to move to
  1252. * @param float $y - the Y co-ordinate to move to
  1253. * @return Zend_Pdf_Page
  1254. */
  1255. public function pathLine($x, $y)
  1256. {
  1257. /** @todo implementation */
  1258. return $this;
  1259. }
  1260. /**
  1261. * Start a new path at the specified position. If a path has already been started,
  1262. * move the cursor without drawing a line.
  1263. *
  1264. * @param float $x - the X co-ordinate to move to
  1265. * @param float $y - the Y co-ordinate to move to
  1266. * @return Zend_Pdf_Page
  1267. */
  1268. public function pathMove($x, $y)
  1269. {
  1270. /** @todo implementation */
  1271. return $this;
  1272. }
  1273. /**
  1274. * Writes the raw data to the page's content stream.
  1275. *
  1276. * Be sure to consult the PDF reference to ensure your syntax is correct. No
  1277. * attempt is made to ensure the validity of the stream data.
  1278. *
  1279. * @param string $data
  1280. * @param string $procSet (optional) Name of ProcSet to add.
  1281. * @return Zend_Pdf_Page
  1282. */
  1283. public function rawWrite($data, $procSet = null)
  1284. {
  1285. if (! empty($procSet)) {
  1286. $this->_addProcSet($procSet);
  1287. }
  1288. $this->_contents .= $data;
  1289. return $this;
  1290. }
  1291. /**
  1292. * Rotate the page.
  1293. *
  1294. * @param float $x - the X co-ordinate of rotation point
  1295. * @param float $y - the Y co-ordinate of rotation point
  1296. * @param float $angle - rotation angle
  1297. * @return Zend_Pdf_Page
  1298. */
  1299. public function rotate($x, $y, $angle)
  1300. {
  1301. $cos = new Zend_Pdf_Element_Numeric(cos($angle));
  1302. $sin = new Zend_Pdf_Element_Numeric(sin($angle));
  1303. $mSin = new Zend_Pdf_Element_Numeric(-$sin->value);
  1304. $xObj = new Zend_Pdf_Element_Numeric($x);
  1305. $yObj = new Zend_Pdf_Element_Numeric($y);
  1306. $mXObj = new Zend_Pdf_Element_Numeric(-$x);
  1307. $mYObj = new Zend_Pdf_Element_Numeric(-$y);
  1308. $this->_addProcSet('PDF');
  1309. $this->_contents .= '1 0 0 1 ' . $xObj->toString() . ' ' . $yObj->toString() . " cm\n"
  1310. . $cos->toString() . ' ' . $sin->toString() . ' ' . $mSin->toString() . ' ' . $cos->toString() . " 0 0 cm\n"
  1311. . '1 0 0 1 ' . $mXObj->toString() . ' ' . $mYObj->toString() . " cm\n";
  1312. return $this;
  1313. }
  1314. /**
  1315. * Scale coordination system.
  1316. *
  1317. * @param float $xScale - X dimention scale factor
  1318. * @param float $yScale - Y dimention scale factor
  1319. * @return Zend_Pdf_Page
  1320. */
  1321. public function scale($xScale, $yScale)
  1322. {
  1323. $xScaleObj = new Zend_Pdf_Element_Numeric($xScale);
  1324. $yScaleObj = new Zend_Pdf_Element_Numeric($yScale);
  1325. $this->_addProcSet('PDF');
  1326. $this->_contents .= $xScaleObj->toString() . ' 0 0 ' . $yScaleObj->toString() . " 0 0 cm\n";
  1327. return $this;
  1328. }
  1329. /**
  1330. * Translate coordination system.
  1331. *
  1332. * @param float $xShift - X coordinate shift
  1333. * @param float $yShift - Y coordinate shift
  1334. * @return Zend_Pdf_Page
  1335. */
  1336. public function translate($xShift, $yShift)
  1337. {
  1338. $xShiftObj = new Zend_Pdf_Element_Numeric($xShift);
  1339. $yShiftObj = new Zend_Pdf_Element_Numeric($yShift);
  1340. $this->_addProcSet('PDF');
  1341. $this->_contents .= '1 0 0 1 ' . $xShiftObj->toString() . ' ' . $yShiftObj->toString() . " cm\n";
  1342. return $this;
  1343. }
  1344. /**
  1345. * Translate coordination system.
  1346. *
  1347. * @param float $x - the X co-ordinate of axis skew point
  1348. * @param float $y - the Y co-ordinate of axis skew point
  1349. * @param float $xAngle - X axis skew angle
  1350. * @param float $yAngle - Y axis skew angle
  1351. * @return Zend_Pdf_Page
  1352. */
  1353. public function skew($x, $y, $xAngle, $yAngle)
  1354. {
  1355. $tanXObj = new Zend_Pdf_Element_Numeric(tan($xAngle));
  1356. $tanYObj = new Zend_Pdf_Element_Numeric(-tan($yAngle));
  1357. $xObj = new Zend_Pdf_Element_Numeric($x);
  1358. $yObj = new Zend_Pdf_Element_Numeric($y);
  1359. $mXObj = new Zend_Pdf_Element_Numeric(-$x);
  1360. $mYObj = new Zend_Pdf_Element_Numeric(-$y);
  1361. $this->_addProcSet('PDF');
  1362. $this->_contents .= '1 0 0 1 ' . $xObj->toString() . ' ' . $yObj->toString() . " cm\n"
  1363. . '1 ' . $tanXObj->toString() . ' ' . $tanYObj->toString() . " 1 0 0 cm\n"
  1364. . '1 0 0 1 ' . $mXObj->toString() . ' ' . $mYObj->toString() . " cm\n";
  1365. return $this;
  1366. }
  1367. }