2
0

DrawingTest.php 25 KB

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