PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helpers/dompdf/load_font.php

https://bitbucket.org/hlevine/myclientbase-south-african-version
PHP | 256 lines | 123 code | 58 blank | 75 comment | 38 complexity | 84ecf1a7596f69b07dcc732fcded9248 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * DOMPDF - PHP5 HTML to PDF renderer
  5. *
  6. * File: $RCSfile: load_font.php,v $
  7. * Created on: 2004-06-23
  8. *
  9. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library in the file LICENSE.LGPL; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  24. * 02111-1307 USA
  25. *
  26. * Alternatively, you may distribute this software under the terms of the
  27. * PHP License, version 3.0 or later. A copy of this license should have
  28. * been distributed with this file in the file LICENSE.PHP . If this is not
  29. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  30. *
  31. * The latest version of DOMPDF might be available at:
  32. * http://www.digitaljunkies.ca/dompdf
  33. *
  34. * @link http://www.digitaljunkies.ca/dompdf
  35. * @copyright 2004 Benj Carson
  36. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  37. * @package dompdf
  38. * @version 0.5.1
  39. */
  40. require_once("dompdf_config.inc.php");
  41. /**
  42. * @access private
  43. */
  44. define("_TTF2AFM", escapeshellarg(TTF2AFM) . " -a -GAef -OW ");
  45. if ( !file_exists(TTF2AFM) ) {
  46. die("Unable to locate the ttf2afm / ttf2pt1 executable (checked " . TTF2AFM . ").\n");
  47. }
  48. /**
  49. * Display command line usage
  50. *
  51. */
  52. function usage() {
  53. echo <<<EOD
  54. Usage: {$_SERVER["argv"][0]} font_family n_file [b_file] [i_file] [bi_file]
  55. font_family: the name of the font, e.g. Verdana, 'Times New Roman',
  56. monospace, sans-serif.
  57. n_file: the .pfb or .ttf file for the normal, non-bold, non-italic
  58. face of the font.
  59. {b|i|bi}_file: the files for each of the respective (bold, italic,
  60. bold-italic) faces.
  61. If the optional b|i|bi files are not specified, load_font.php will search
  62. the directory containing normal font file (n_file) for additional files that
  63. it thinks might be the correct ones (e.g. that end in _Bold or b or B). If
  64. it finds the files they will also be processed. All files will be
  65. automatically copied to the DOMPDF font directory, and afm files will be
  66. generated using ttf2afm.
  67. Examples:
  68. ./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf
  69. ./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttf
  70. EOD;
  71. }
  72. if ( $_SERVER["argc"] < 3 ) {
  73. usage();
  74. die();
  75. }
  76. /**
  77. * Installs a new font family
  78. *
  79. * This function maps a font-family name to a font. It tries to locate the
  80. * bold, italic, and bold italic versions of the font as well. Once the
  81. * files are located, ttf versions of the font are copied to the fonts
  82. * directory. Changes to the font lookup table are saved to the cache.
  83. *
  84. * @param string $fontname the font-family name
  85. * @param string $normal the filename of the normal face font subtype
  86. * @param string $bold the filename of the bold face font subtype
  87. * @param string $italic the filename of the italic face font subtype
  88. * @param string $bold_italic the filename of the bold italic face font subtype
  89. */
  90. function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {
  91. // Check if the base filename is readable
  92. if ( !is_readable($normal) )
  93. throw new DOMPDF_Exception("Unable to read '$normal'.");
  94. $dir = dirname($normal);
  95. list($file, $ext) = explode(".", basename($normal), 2); // subtract extension
  96. // Try $file_Bold.$ext etc.
  97. $ext = ".$ext";
  98. if ( !isset($bold) || !is_readable($bold) ) {
  99. $bold = $dir . "/" . $file . "_Bold" . $ext;
  100. if ( !is_readable($bold) ) {
  101. // Try $file . "b"
  102. $bold = $dir . "/" . $file . "b" . $ext;
  103. if ( !is_readable($bold) ) {
  104. // Try $file . "B"
  105. $bold = $dir . "/" . $file . "B" . $ext;
  106. if ( !is_readable($bold) )
  107. $bold = null;
  108. }
  109. }
  110. }
  111. if ( is_null($bold) )
  112. echo ("Unable to find bold face file.\n");
  113. if ( !isset($italic) || !is_readable($italic) ) {
  114. $italic = $dir . "/" . $file . "_Italic" . $ext;
  115. if ( !is_readable($italic) ) {
  116. // Try $file . "i"
  117. $italic = $dir . "/" . $file . "i" . $ext;
  118. if ( !is_readable($italic) ) {
  119. // Try $file . "I"
  120. $italic = $dir . "/" . $file . "I" . $ext;
  121. if ( !is_readable($italic) )
  122. $italic = null;
  123. }
  124. }
  125. }
  126. if ( is_null($italic) )
  127. echo ("Unable to find italic face file.\n");
  128. if ( !isset($bold_italic) || !is_readable($bold_italic) ) {
  129. $bold_italic = $dir . "/" . $file . "_Bold_Italic" . $ext;
  130. if ( !is_readable($bold_italic) ) {
  131. // Try $file . "bi"
  132. $bold_italic = $dir . "/" . $file . "bi" . $ext;
  133. if ( !is_readable($bold_italic) ) {
  134. // Try $file . "BI"
  135. $bold_italic = $dir . "/" . $file . "BI" . $ext;
  136. if ( !is_readable($bold_italic) ) {
  137. // Try $file . "ib"
  138. $bold_italic = $dir . "/" . $file . "ib" . $ext;
  139. if ( !is_readable($bold_italic) ) {
  140. // Try $file . "IB"
  141. $bold_italic = $dir . "/" . $file . "IB" . $ext;
  142. if ( !is_readable($bold_italic) )
  143. $bold_italic = null;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if ( is_null($bold_italic) )
  150. echo ("Unable to find bold italic face file.\n");
  151. $fonts = compact("normal", "bold", "italic", "bold_italic");
  152. $entry = array();
  153. if ( strtolower($ext) === ".pfb" || strtolower($ext) === ".ttf" || strtolower($ext) === ".otf" ) {
  154. // Copy the files to the font directory.
  155. foreach ($fonts as $var => $src) {
  156. if ( is_null($src) ) {
  157. $entry[$var] = DOMPDF_FONT_DIR . basename($normal);
  158. continue;
  159. }
  160. // Verify that the fonts exist and are readable
  161. if ( !is_readable($src) )
  162. throw new User_DOMPDF_Exception("Requested font '$pathname' is not readable");
  163. $dest = DOMPDF_FONT_DIR . basename($src);
  164. if ( !is_writeable(dirname($dest)) )
  165. throw new User_DOMPDF_Exception("Unable to write to destination '$dest'.");
  166. echo "Copying $src to $dest...\n";
  167. if ( !copy($src, $dest) )
  168. throw new DOMPDF_Exception("Unable to copy '$src' to '" . DOMPDF_FONT_DIR . "$dest'.");
  169. $entry[$var] = $dest;
  170. }
  171. } else
  172. throw new DOMPDF_Exception("Unable to process fonts of type '$ext'.");
  173. // If the extension is a ttf, try and convert the fonts to afm too
  174. if ( mb_strtolower($ext) === ".ttf" || strtolower($ext) == ".otf" ) {
  175. foreach ($fonts as $var => $font) {
  176. if ( is_null($font) ) {
  177. $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4);
  178. continue;
  179. }
  180. $dest = DOMPDF_FONT_DIR . mb_substr(basename($font),0, -4);
  181. echo "Generating .afm for $font...\n";
  182. echo "Command: " . _TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . "\n";
  183. exec( _TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . " &> /dev/null", $output, $ret );
  184. $entry[$var] = $dest;
  185. }
  186. }
  187. // FIXME: how to generate afms from pfb?
  188. // Store the fonts in the lookup table
  189. Font_Metrics::set_font_family(strtolower($fontname), $entry);
  190. // Save the changes
  191. Font_Metrics::save_font_families();
  192. }
  193. $normal = $_SERVER["argv"][2];
  194. $bold = isset($_SERVER["argv"][3]) ? $_SERVER["argv"][3] : null;
  195. $italic = isset($_SERVER["argv"][4]) ? $_SERVER["argv"][4] : null;
  196. $bold_italic = isset($_SERVER["argv"][5]) ? $_SERVER["argv"][5] : null;
  197. install_font_family($_SERVER["argv"][1], $normal, $bold, $italic, $bold_italic);
  198. ?>