DrawingTest.php 24 KB

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