DrawingTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php
  2. /**
  3. * @package Zend_Pdf
  4. * @subpackage UnitTests
  5. */
  6. /** Zend_Pdf */
  7. require_once 'Zend/Pdf.php';
  8. /** PHPUnit Test Case */
  9. require_once 'PHPUnit/Framework/TestCase.php';
  10. /**
  11. * @package Zend_Pdf
  12. * @subpackage UnitTests
  13. */
  14. class Zend_Pdf_DrawingTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function testDrawing()
  17. {
  18. $pdf = new Zend_Pdf();
  19. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  20. $pdf->pages[] = ($page1 = $pdf->newPage('A4'));
  21. // Add new page generated by Zend_Pdf_Page object (page is not attached to the document)
  22. $pdf->pages[] = ($page2 = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE));
  23. // Add new page generated by Zend_Pdf_Page object (page is attached to the document)
  24. $pdf->pages[] = ($page3 = $pdf->newPage('A4'));
  25. // Create new font
  26. $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  27. // Apply font and draw text
  28. $page1->setFont($font, 36)
  29. ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
  30. ->drawText('Helvetica 36 text string', 60, 500);
  31. // Use font object for another page
  32. $page2->setFont($font, 24)
  33. ->drawText('Helvetica 24 text string', 60, 500);
  34. // Use another font
  35. $page2->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES), 32)
  36. ->drawText('Times-Roman 32 text string', 60, 450);
  37. // Draw rectangle
  38. $page2->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
  39. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
  40. ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
  41. ->drawRectangle(60, 400, 400, 350);
  42. // Draw circle
  43. $page2->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
  44. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  45. ->drawCircle(85, 375, 25);
  46. // Draw sectors
  47. $page2->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
  48. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  49. ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
  50. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  51. ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
  52. // Draw ellipse
  53. $page2->setFillColor(new Zend_Pdf_Color_Html('Red'))
  54. ->drawEllipse(250, 400, 400, 350)
  55. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  56. ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
  57. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  58. ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);
  59. // Draw and fill polygon
  60. $page2->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
  61. $x = array();
  62. $y = array();
  63. for ($count = 0; $count < 8; $count++) {
  64. $x[] = 140 + 25*cos(3*M_PI_4*$count);
  65. $y[] = 375 + 25*sin(3*M_PI_4*$count);
  66. }
  67. $page2->drawPolygon($x, $y,
  68. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  69. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
  70. // Draw line
  71. $page2->setLineWidth(0.5)
  72. ->drawLine(60, 375, 400, 375);
  73. // -----------------------------------------------------------------------------------
  74. $page3->translate(200, 10)
  75. ->rotate(10, 10, M_PI_2/9)
  76. ->scale(0.7, 1.2)
  77. ->skew(60, 350, M_PI_2/9, -M_PI_2/9);
  78. // Use font object for another page
  79. $page3->setFont($font, 24)
  80. ->drawText('Helvetica 24 text string', 60, 500);
  81. // Use another font
  82. $page3->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES), 32)
  83. ->drawText('Times-Roman 32 text string', 60, 450);
  84. // Draw rectangle
  85. $page3->setFillColor(new Zend_Pdf_Color_GrayScale(0.8))
  86. ->setLineColor(new Zend_Pdf_Color_GrayScale(0.2))
  87. ->setLineDashingPattern(array(3, 2, 3, 4), 1.6)
  88. ->drawRectangle(60, 400, 400, 350);
  89. // Draw circle
  90. $page3->setLineDashingPattern(Zend_Pdf_Page::LINE_DASHING_SOLID)
  91. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 0))
  92. ->drawCircle(85, 375, 25);
  93. // Draw sectors
  94. $page3->drawCircle(200, 375, 25, 2*M_PI/3, -M_PI/6)
  95. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  96. ->drawCircle(200, 375, 25, M_PI/6, 2*M_PI/3)
  97. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  98. ->drawCircle(200, 375, 25, -M_PI/6, M_PI/6);
  99. // Draw ellipse
  100. $page3->setFillColor(new Zend_Pdf_Color_Html('Red'))
  101. ->drawEllipse(250, 400, 400, 350)
  102. ->setFillColor(new Zend_Pdf_Color_Cmyk(1, 0, 0, 0))
  103. ->drawEllipse(250, 400, 400, 350, M_PI/6, 2*M_PI/3)
  104. ->setFillColor(new Zend_Pdf_Color_Rgb(1, 1, 0))
  105. ->drawEllipse(250, 400, 400, 350, -M_PI/6, M_PI/6);
  106. // Draw and fill polygon
  107. $page3->setFillColor(new Zend_Pdf_Color_Rgb(1, 0, 1));
  108. $x = array();
  109. $y = array();
  110. for ($count = 0; $count < 8; $count++) {
  111. $x[] = 140 + 25*cos(3*M_PI_4*$count);
  112. $y[] = 375 + 25*sin(3*M_PI_4*$count);
  113. }
  114. $page3->drawPolygon($x, $y,
  115. Zend_Pdf_Page::SHAPE_DRAW_FILL_AND_STROKE,
  116. Zend_Pdf_Page::FILL_METHOD_EVEN_ODD);
  117. // Draw line
  118. $page3->setLineWidth(0.5)
  119. ->drawLine(60, 375, 400, 375);
  120. $pdf->save(dirname(__FILE__) . '/_files/output.pdf');
  121. unset($pdf);
  122. $pdf1 = Zend_Pdf::load(dirname(__FILE__) . '/_files/output.pdf');
  123. $this->assertTrue($pdf1 instanceof Zend_Pdf);
  124. unset($pdf1);
  125. unlink(dirname(__FILE__) . '/_files/output.pdf');
  126. }
  127. public function testImageDrawing()
  128. {
  129. $pdf = new Zend_Pdf();
  130. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  131. $pdf->pages[] = ($page = $pdf->newPage('A4'));
  132. $stampImagePNG = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/_files/stamp.png');
  133. $this->assertTrue($stampImagePNG instanceof Zend_Pdf_Resource_Image);
  134. $page->saveGS()
  135. ->clipCircle(250, 500, 50)
  136. ->drawImage($stampImagePNG, 200, 450, 300, 550)
  137. ->restoreGS();
  138. $stampImageTIFF = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/_files/stamp.tif');
  139. $this->assertTrue($stampImageTIFF instanceof Zend_Pdf_Resource_Image);
  140. $page->saveGS()
  141. ->clipCircle(325, 500, 50)
  142. ->drawImage($stampImagePNG, 275, 450, 375, 550)
  143. ->restoreGS();
  144. if (function_exists('gd_info')) {
  145. $info = gd_info();
  146. $jpegSupported = $info['JPG Support'];
  147. } else {
  148. $jpegSupported = false;
  149. }
  150. if ($jpegSupported) {
  151. $stampImageJPG = Zend_Pdf_Image::imageWithPath(dirname(__FILE__) . '/_files/stamp.jpg');
  152. $this->assertTrue($stampImageJPG instanceof Zend_Pdf_Resource_Image);
  153. $page->saveGS()
  154. ->clipCircle(287.5, 440, 50)
  155. ->drawImage($stampImageJPG, 237.5, 390, 337.5, 490)
  156. ->restoreGS();
  157. $page->saveGS()
  158. ->clipCircle(250, 500, 50)
  159. ->clipCircle(287.5, 440, 50)
  160. ->drawImage($stampImagePNG, 200, 450, 300, 550)
  161. ->restoreGS();
  162. }
  163. $pdf->save(dirname(__FILE__) . '/_files/output.pdf');
  164. unset($pdf);
  165. $pdf1 = Zend_Pdf::load(dirname(__FILE__) . '/_files/output.pdf');
  166. $this->assertTrue($pdf1 instanceof Zend_Pdf);
  167. unset($pdf1);
  168. unlink(dirname(__FILE__) . '/_files/output.pdf');
  169. }
  170. public function testFontDrawing()
  171. {
  172. if (PHP_OS == 'AIX') {
  173. $this->markTestSkipped('Not supported on AIX');
  174. }
  175. $pdf = new Zend_Pdf();
  176. $fontsList = array(Zend_Pdf_Font::FONT_COURIER,
  177. Zend_Pdf_Font::FONT_COURIER_BOLD,
  178. Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC,
  179. Zend_Pdf_Font::FONT_COURIER_BOLD_OBLIQUE,
  180. Zend_Pdf_Font::FONT_COURIER_ITALIC,
  181. Zend_Pdf_Font::FONT_COURIER_OBLIQUE,
  182. Zend_Pdf_Font::FONT_HELVETICA,
  183. Zend_Pdf_Font::FONT_HELVETICA_BOLD,
  184. Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC,
  185. Zend_Pdf_Font::FONT_HELVETICA_BOLD_OBLIQUE,
  186. Zend_Pdf_Font::FONT_HELVETICA_ITALIC,
  187. Zend_Pdf_Font::FONT_HELVETICA_OBLIQUE,
  188. Zend_Pdf_Font::FONT_TIMES,
  189. Zend_Pdf_Font::FONT_TIMES_BOLD,
  190. Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC,
  191. Zend_Pdf_Font::FONT_TIMES_ITALIC,
  192. Zend_Pdf_Font::FONT_TIMES_ROMAN);
  193. $titleFont = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER_BOLD_OBLIQUE);
  194. foreach ($fontsList as $fontName) {
  195. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  196. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE));
  197. $font = Zend_Pdf_Font::fontWithName($fontName);
  198. $this->assertTrue($font instanceof Zend_Pdf_Resource_Font);
  199. $page->setFont($titleFont, 10)
  200. ->drawText($font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en') . ':', 100, 400);
  201. $page->setFont($font, 20);
  202. $page->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360);
  203. $ascent = $font->getAscent();
  204. $this->assertTrue( abs(1 - $font->getCoveredPercentage('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxwz')) < 0.00001 );
  205. $descent = $font->getDescent();
  206. $font->getFontName(Zend_Pdf_Font::NAME_FULL, 'en');
  207. $font->getFontName(Zend_Pdf_Font::NAME_FAMILY, 'en');
  208. $font->getFontName(Zend_Pdf_Font::NAME_PREFERRED_FAMILY, 'en');
  209. $font->getFontName(Zend_Pdf_Font::NAME_STYLE, 'en');
  210. $font->getFontName(Zend_Pdf_Font::NAME_PREFERRED_STYLE, 'en');
  211. $font->getFontName(Zend_Pdf_Font::NAME_DESCRIPTION, 'en');
  212. $font->getFontName(Zend_Pdf_Font::NAME_SAMPLE_TEXT, 'en');
  213. $font->getFontName(Zend_Pdf_Font::NAME_ID, 'en');
  214. $font->getFontName(Zend_Pdf_Font::NAME_VERSION, 'en');
  215. $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en');
  216. $font->getFontName(Zend_Pdf_Font::NAME_CID_NAME, 'en');
  217. $font->getFontName(Zend_Pdf_Font::NAME_DESIGNER, 'en');
  218. $font->getFontName(Zend_Pdf_Font::NAME_DESIGNER_URL, 'en');
  219. $font->getFontName(Zend_Pdf_Font::NAME_MANUFACTURER, 'en');
  220. $font->getFontName(Zend_Pdf_Font::NAME_VENDOR_URL, 'en');
  221. $font->getFontName(Zend_Pdf_Font::NAME_COPYRIGHT, 'en');
  222. $font->getFontName(Zend_Pdf_Font::NAME_TRADEMARK, 'en');
  223. $font->getFontName(Zend_Pdf_Font::NAME_LICENSE, 'en');
  224. $font->getFontName(Zend_Pdf_Font::NAME_LICENSE_URL, 'en');
  225. $type = $font->getFontType();
  226. $lineGap = $font->getLineGap();
  227. $lineHeight = $font->getLineHeight();
  228. $this->assertTrue($font->getResource() instanceof Zend_Pdf_Element_Object);
  229. $font->getStrikePosition();
  230. $font->getStrikeThickness();
  231. $font->getUnderlinePosition();
  232. $font->getUnitsPerEm();
  233. $font->widthForGlyph(10);
  234. }
  235. $nonAlphabeticalPhonts =
  236. array(Zend_Pdf_Font::FONT_SYMBOL =>
  237. "\x00\x20\x00\x21\x22\x00\x00\x23\x22\x03\x00\x25\x00\x26\x22\x0b\x00\x28\x00\x29\x22\x17\x00\x2b\x00\x2c\x22\x12\x00\x2e\x00\x2f\x00\x30\x00\x31\x00\x32\x00\x33\x00\x34\x00\x35\x00\x36\x00\x37\x00\x38\x00\x39\x00\x3a\x00\x3b\x00\x3c\x00\x3d\x00\x3e\x00\x3f\x22\x45\x03\x91\x03\x92\x03\xa7\x22\x06\x03\x95\x03\xa6",
  238. Zend_Pdf_Font::FONT_ZAPFDINGBATS =>
  239. "\x00\x20\x27\x01\x27\x02\x27\x03\x27\x04\x26\x0e\x27\x06\x27\x07\x27\x08\x27\x09\x26\x1b\x26\x1e\x27\x0c\x27\x0d\x27\x0e\x27\x0f\x27\x10\x27\x11\x27\x12\x27\x13\x27\x14\x27\x15\x27\x16\x27\x17\x27\x18\x27\x19\x27\x1a");
  240. foreach ($nonAlphabeticalPhonts as $fontName => $example) {
  241. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  242. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE));
  243. $font = Zend_Pdf_Font::fontWithName($fontName);
  244. $this->assertTrue($font instanceof Zend_Pdf_Resource_Font);
  245. $page->setFont($titleFont, 10)
  246. ->drawText($font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en') . ':', 100, 400);
  247. $page->setFont($font, 20)
  248. ->drawText($example, 100, 360, 'UTF-16BE');
  249. $ascent = $font->getAscent();
  250. $this->assertTrue( abs(1 - $font->getCoveredPercentage($example, 'UTF-16BE')) < 0.00001 );
  251. $descent = $font->getDescent();
  252. $font->getFontName(Zend_Pdf_Font::NAME_FULL, 'en');
  253. $font->getFontName(Zend_Pdf_Font::NAME_FAMILY, 'en');
  254. $font->getFontName(Zend_Pdf_Font::NAME_PREFERRED_FAMILY, 'en');
  255. $font->getFontName(Zend_Pdf_Font::NAME_STYLE, 'en');
  256. $font->getFontName(Zend_Pdf_Font::NAME_PREFERRED_STYLE, 'en');
  257. $font->getFontName(Zend_Pdf_Font::NAME_DESCRIPTION, 'en');
  258. $font->getFontName(Zend_Pdf_Font::NAME_SAMPLE_TEXT, 'en');
  259. $font->getFontName(Zend_Pdf_Font::NAME_ID, 'en');
  260. $font->getFontName(Zend_Pdf_Font::NAME_VERSION, 'en');
  261. $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en');
  262. $font->getFontName(Zend_Pdf_Font::NAME_CID_NAME, 'en');
  263. $font->getFontName(Zend_Pdf_Font::NAME_DESIGNER, 'en');
  264. $font->getFontName(Zend_Pdf_Font::NAME_DESIGNER_URL, 'en');
  265. $font->getFontName(Zend_Pdf_Font::NAME_MANUFACTURER, 'en');
  266. $font->getFontName(Zend_Pdf_Font::NAME_VENDOR_URL, 'en');
  267. $font->getFontName(Zend_Pdf_Font::NAME_COPYRIGHT, 'en');
  268. $font->getFontName(Zend_Pdf_Font::NAME_TRADEMARK, 'en');
  269. $font->getFontName(Zend_Pdf_Font::NAME_LICENSE, 'en');
  270. $font->getFontName(Zend_Pdf_Font::NAME_LICENSE_URL, 'en');
  271. $type = $font->getFontType();
  272. $lineGap = $font->getLineGap();
  273. $lineHeight = $font->getLineHeight();
  274. $this->assertTrue($font->getResource() instanceof Zend_Pdf_Element_Object);
  275. $font->getStrikePosition();
  276. $font->getStrikeThickness();
  277. $font->getUnderlinePosition();
  278. $font->getUnitsPerEm();
  279. $font->widthForGlyph(10);
  280. }
  281. $TTFFontsList = array('VeraBd.ttf',
  282. 'VeraBI.ttf',
  283. 'VeraIt.ttf',
  284. 'VeraMoBd.ttf',
  285. 'VeraMoBI.ttf',
  286. 'VeraMoIt.ttf',
  287. 'VeraMono.ttf',
  288. 'VeraSeBd.ttf',
  289. 'VeraSe.ttf',
  290. 'Vera.ttf');
  291. foreach ($TTFFontsList as $fontName) {
  292. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  293. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE));
  294. $font = Zend_Pdf_Font::fontWithPath(dirname(__FILE__) . '/_fonts/' . $fontName);
  295. $this->assertTrue($font instanceof Zend_Pdf_Resource_Font);
  296. $page->setFont($titleFont, 10)
  297. ->drawText($font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en') . ':', 100, 400);
  298. $page->setFont($font, 20)
  299. ->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360);
  300. $ascent = $font->getAscent();
  301. $this->assertTrue( abs(1 - $font->getCoveredPercentage('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxwz')) < 0.00001 );
  302. $descent = $font->getDescent();
  303. $font->getFontName(Zend_Pdf_Font::NAME_FULL, 'en');
  304. $font->getFontName(Zend_Pdf_Font::NAME_FAMILY, 'en');
  305. $font->getFontName(Zend_Pdf_Font::NAME_PREFERRED_FAMILY, 'en');
  306. $font->getFontName(Zend_Pdf_Font::NAME_STYLE, 'en');
  307. $font->getFontName(Zend_Pdf_Font::NAME_PREFERRED_STYLE, 'en');
  308. $font->getFontName(Zend_Pdf_Font::NAME_DESCRIPTION, 'en');
  309. $font->getFontName(Zend_Pdf_Font::NAME_SAMPLE_TEXT, 'en');
  310. $font->getFontName(Zend_Pdf_Font::NAME_ID, 'en');
  311. $font->getFontName(Zend_Pdf_Font::NAME_VERSION, 'en');
  312. $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en');
  313. $font->getFontName(Zend_Pdf_Font::NAME_CID_NAME, 'en');
  314. $font->getFontName(Zend_Pdf_Font::NAME_DESIGNER, 'en');
  315. $font->getFontName(Zend_Pdf_Font::NAME_DESIGNER_URL, 'en');
  316. $font->getFontName(Zend_Pdf_Font::NAME_MANUFACTURER, 'en');
  317. $font->getFontName(Zend_Pdf_Font::NAME_VENDOR_URL, 'en');
  318. $font->getFontName(Zend_Pdf_Font::NAME_COPYRIGHT, 'en');
  319. $font->getFontName(Zend_Pdf_Font::NAME_TRADEMARK, 'en');
  320. $font->getFontName(Zend_Pdf_Font::NAME_LICENSE, 'en');
  321. $font->getFontName(Zend_Pdf_Font::NAME_LICENSE_URL, 'en');
  322. $type = $font->getFontType();
  323. $lineGap = $font->getLineGap();
  324. $lineHeight = $font->getLineHeight();
  325. $this->assertTrue($font->getResource() instanceof Zend_Pdf_Element_Object);
  326. $font->getStrikePosition();
  327. $font->getStrikeThickness();
  328. $font->getUnderlinePosition();
  329. $font->getUnitsPerEm();
  330. $font->widthForGlyph(10);
  331. }
  332. $pdf->save(dirname(__FILE__) . '/_files/output.pdf');
  333. unset($pdf);
  334. $pdf1 = Zend_Pdf::load(dirname(__FILE__) . '/_files/output.pdf');
  335. $this->assertTrue($pdf1 instanceof Zend_Pdf);
  336. unset($pdf1);
  337. unlink(dirname(__FILE__) . '/_files/output.pdf');
  338. }
  339. public function testFontExtracting()
  340. {
  341. if (PHP_OS == 'AIX') {
  342. $this->markTestSkipped('Not supported on AIX');
  343. }
  344. $pdf = new Zend_Pdf();
  345. $fontsList = array(Zend_Pdf_Font::FONT_COURIER,
  346. Zend_Pdf_Font::FONT_HELVETICA_BOLD,
  347. Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC);
  348. foreach ($fontsList as $fontName) {
  349. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  350. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE));
  351. $font = Zend_Pdf_Font::fontWithName($fontName);
  352. $page->setFont($font, 10)
  353. ->drawText($font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en') . ':', 100, 400);
  354. $page->setFont($font, 20)
  355. ->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360);
  356. $type = $font->getFontType();
  357. }
  358. $TTFFontsList = array('VeraBd.ttf',
  359. 'VeraBI.ttf',
  360. 'VeraIt.ttf',
  361. 'VeraMoBd.ttf',
  362. 'VeraMoBI.ttf',
  363. 'VeraMoIt.ttf',
  364. 'VeraMono.ttf',
  365. 'VeraSeBd.ttf',
  366. 'VeraSe.ttf',
  367. 'Vera.ttf');
  368. foreach ($TTFFontsList as $fontName) {
  369. // Add new page generated by Zend_Pdf object (page is attached to the specified the document)
  370. $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4_LANDSCAPE));
  371. $font = Zend_Pdf_Font::fontWithPath(dirname(__FILE__) . '/_fonts/' . $fontName);
  372. $page->setFont($font, 10)
  373. ->drawText($font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'CP1252') . ':', 100, 400);
  374. $page->setFont($font, 20)
  375. ->drawText("'The quick brown fox jumps over the lazy dog'", 100, 360);
  376. $type = $font->getFontType();
  377. }
  378. $pdf->save(dirname(__FILE__) . '/_files/output.pdf');
  379. unset($pdf);
  380. $pdf1 = Zend_Pdf::load(dirname(__FILE__) . '/_files/output.pdf');
  381. $newPages = array();
  382. $fontList = array();
  383. $fontNames = array();
  384. foreach ($pdf1->pages as $page) {
  385. $pageFonts = $page->extractFonts();
  386. foreach ($pageFonts as $font) {
  387. $fontList[] = $font;
  388. $fontNames[] = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
  389. }
  390. }
  391. $this->assertEquals(array(Zend_Pdf_Font::FONT_COURIER,
  392. Zend_Pdf_Font::FONT_HELVETICA_BOLD,
  393. Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC,
  394. 'BitstreamVeraSans-Bold',
  395. 'BitstreamVeraSans-BoldOblique',
  396. 'BitstreamVeraSans-Oblique',
  397. 'BitstreamVeraSansMono-Bold',
  398. 'BitstreamVeraSansMono-BoldOb',
  399. 'BitstreamVeraSansMono-Oblique',
  400. 'BitstreamVeraSansMono-Roman',
  401. 'BitstreamVeraSerif-Bold',
  402. 'BitstreamVeraSerif-Roman',
  403. 'BitstreamVeraSans-Roman'),
  404. $fontNames);
  405. $pdf1->pages[] = ($page = $pdf1->newPage(Zend_Pdf_Page::SIZE_A4));
  406. $yPosition = 700;
  407. foreach ($fontList as $font) {
  408. $page->setFont($font, 15)
  409. ->drawText("The quick brown fox jumps over the lazy dog", 100, $yPosition);
  410. $yPosition -= 30;
  411. }
  412. $fontNames1 = array();
  413. foreach ($pdf1->extractFonts() as $font) {
  414. $fontNames1[] = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
  415. }
  416. $this->assertEquals(array(Zend_Pdf_Font::FONT_COURIER,
  417. Zend_Pdf_Font::FONT_HELVETICA_BOLD,
  418. Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC,
  419. 'BitstreamVeraSans-Bold',
  420. 'BitstreamVeraSans-BoldOblique',
  421. 'BitstreamVeraSans-Oblique',
  422. 'BitstreamVeraSansMono-Bold',
  423. 'BitstreamVeraSansMono-BoldOb',
  424. 'BitstreamVeraSansMono-Oblique',
  425. 'BitstreamVeraSansMono-Roman',
  426. 'BitstreamVeraSerif-Bold',
  427. 'BitstreamVeraSerif-Roman',
  428. 'BitstreamVeraSans-Roman'),
  429. $fontNames1);
  430. $page = reset($pdf1->pages);
  431. $font = $page->extractFont(Zend_Pdf_Font::FONT_COURIER);
  432. $this->assertTrue($font instanceof Zend_Pdf_Resource_Font_Extracted);
  433. $font = $page->extractFont(Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC);
  434. $this->assertNull($font);
  435. $font = $pdf1->extractFont(Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC);
  436. $this->assertTrue($font instanceof Zend_Pdf_Resource_Font_Extracted);
  437. $font = $pdf1->extractFont(Zend_Pdf_Font::FONT_TIMES_ROMAN);
  438. $this->assertNull($font);
  439. $pdf1->save(dirname(__FILE__) . '/_files/output1.pdf');
  440. unset($pdf1);
  441. $pdf2 = Zend_Pdf::load(dirname(__FILE__) . '/_files/output1.pdf');
  442. $this->assertTrue($pdf2 instanceof Zend_Pdf);
  443. unset($pdf2);
  444. unlink(dirname(__FILE__) . '/_files/output.pdf');
  445. unlink(dirname(__FILE__) . '/_files/output1.pdf');
  446. }
  447. }