DrawingTest.php 25 KB

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