font.php
来自「Bug tracker, and reporter.」· PHP 代码 · 共 776 行 · 第 1/2 页
PHP
776 行
*/ const WEIGHT_BOLD = 700; /** * Extra-bold (Ultra-bold) font weight. */ const WEIGHT_EXTRA_BOLD = 800; /** * Black (Heavy) font weight. */ const WEIGHT_BLACK = 900; /* Font Widths */ /** * Ultra-condensed font width. Typically 50% of normal. */ const WIDTH_ULTRA_CONDENSED = 1; /** * Extra-condensed font width. Typically 62.5% of normal. */ const WIDTH_EXTRA_CONDENSED = 2; /** * Condensed font width. Typically 75% of normal. */ const WIDTH_CONDENSED = 3; /** * Semi-condensed font width. Typically 87.5% of normal. */ const WIDTH_SEMI_CONDENSED = 4; /** * Normal (Medium) font width. */ const WIDTH_NORMAL = 5; /** * Semi-expanded font width. Typically 112.5% of normal. */ const WIDTH_SEMI_EXPANDED = 6; /** * Expanded font width. Typically 125% of normal. */ const WIDTH_EXPANDED = 7; /** * Extra-expanded font width. Typically 150% of normal. */ const WIDTH_EXTRA_EXPANDED = 8; /** * Ultra-expanded font width. Typically 200% of normal. */ const WIDTH_ULTRA_EXPANDED = 9; /* Font Embedding Options */ /** * Do not embed the font in the PDF document. */ const EMBED_DONT_EMBED = 0x01; /** * Embed, but do not subset the font in the PDF document. */ const EMBED_DONT_SUBSET = 0x02; /** * Embed, but do not compress the font in the PDF document. */ const EMBED_DONT_COMPRESS = 0x04; /** * Suppress the exception normally thrown if the font cannot be embedded * due to its copyright bits being set. */ const EMBED_SUPPRESS_EMBED_EXCEPTION = 0x08; /**** Class Variables ****/ /** * Array whose keys are the unique PostScript names of instantiated fonts. * The values are the font objects themselves. * @var array */ private static $_fontNames = array(); /** * Array whose keys are the md5 hash of the full paths on disk for parsed * fonts. The values are the font objects themselves. * @var array */ private static $_fontFilePaths = array(); /**** Public Interface ****/ /* Factory Methods */ /** * Returns a {@link Zend_Pdf_Resource_Font} object by full name. * * This is the preferred method to obtain one of the standard 14 PDF fonts. * * The result of this method is cached, preventing unnecessary duplication * of font objects. Repetitive calls for a font with the same name will * return the same object. * * The $embeddingOptions parameter allows you to set certain flags related
* to font embedding. You may combine options by OR-ing them together. See
* the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of
* available options and their descriptions. Note that this value is only
* used when creating a font for the first time. If a font with the same
* name already exists, you will get that object and the options you specify
* here will be ignored. This is because fonts are only embedded within the
* PDF file once.
*
* If the font name supplied does not match the name of a previously
* instantiated object and it is not one of the 14 standard PDF fonts, an
* exception will be thrown.
*
* @param string $name Full PostScript name of font.
* @param integer $embeddingOptions (optional) Options for font embedding.
* @return Zend_Pdf_Resource_Font
* @throws Zend_Pdf_Exception
*/ public static function fontWithName($name, $embeddingOptions = 0)
{ /* First check the cache. Don't duplicate font objects. */ if (isset(Zend_Pdf_Font::$_fontNames[$name])) { return Zend_Pdf_Font::$_fontNames[$name]; } /** * @todo It would be cool to be able to have a mapping of font names to * file paths in a configuration file for frequently used custom * fonts. This would allow a user to use custom fonts without having * to hard-code file paths all over the place. Table this idea until * {@link Zend_Config} is ready. */ /* Not an existing font and no mapping in the config file. Check to see * if this is one of the standard 14 PDF fonts. */ switch ($name) { case Zend_Pdf_Font::FONT_COURIER: $font = new Zend_Pdf_Resource_Font_Simple_Standard_Courier(); break; case Zend_Pdf_Font::FONT_COURIER_BOLD: $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBold(); break; case Zend_Pdf_Font::FONT_COURIER_OBLIQUE: $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierOblique(); break; case Zend_Pdf_Font::FONT_COURIER_BOLD_OBLIQUE: $font = new Zend_Pdf_Resource_Font_Simple_Standard_CourierBoldOblique(); break; case Zend_Pdf_Font::FONT_HELVETICA: $font = new Zend_Pdf_Resource_Font_Simple_Standard_Helvetica(); break; case Zend_Pdf_Font::FONT_HELVETICA_BOLD: $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBold(); break; case Zend_Pdf_Font::FONT_HELVETICA_OBLIQUE: $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaOblique(); break; case Zend_Pdf_Font::FONT_HELVETICA_BOLD_OBLIQUE: $font = new Zend_Pdf_Resource_Font_Simple_Standard_HelveticaBoldOblique(); break; case Zend_Pdf_Font::FONT_SYMBOL: $font = new Zend_Pdf_Resource_Font_Simple_Standard_Symbol(); break; case Zend_Pdf_Font::FONT_TIMES_ROMAN: $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesRoman(); break; case Zend_Pdf_Font::FONT_TIMES_BOLD: $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBold(); break; case Zend_Pdf_Font::FONT_TIMES_ITALIC: $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesItalic(); break; case Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC: $font = new Zend_Pdf_Resource_Font_Simple_Standard_TimesBoldItalic(); break; case Zend_Pdf_Font::FONT_ZAPFDINGBATS: $font = new Zend_Pdf_Resource_Font_Simple_Standard_ZapfDingbats(); break; default: throw new Zend_Pdf_Exception("Unknown font name: $name", Zend_Pdf_Exception::BAD_FONT_NAME); } /* Add this new font to the cache array and return it for use. */ Zend_Pdf_Font::$_fontNames[$name] = $font; return $font; } /** * Returns a {@link Zend_Pdf_Resource_Font} object by file path. * * The result of this method is cached, preventing unnecessary duplication * of font objects. Repetitive calls for the font with the same path will * return the same object. * * The $embeddingOptions parameter allows you to set certain flags related * to font embedding. You may combine options by OR-ing them together. See * the EMBED_ constants defined in {@link Zend_Pdf_Font} for the list of * available options and their descriptions. Note that this value is only * used when creating a font for the first time. If a font with the same * name already exists, you will get that object and the options you specify * here will be ignored. This is because fonts are only embedded within the * PDF file once. * * If the file path supplied does not match the path of a previously * instantiated object or the font type cannot be determined, an exception * will be thrown. * * @param string $filePath Full path to the font file. * @param integer $embeddingOptions (optional) Options for font embedding. * @return Zend_Pdf_Resource_Font * @throws Zend_Pdf_Exception */ public static function fontWithPath($filePath, $embeddingOptions = 0) { /* First check the cache. Don't duplicate font objects. */ $filePathKey = md5($filePath); if (isset(Zend_Pdf_Font::$_fontFilePaths[$filePathKey])) { return Zend_Pdf_Font::$_fontFilePaths[$filePathKey]; } /* Create a file parser data source object for this file. File path and * access permission checks are handled here. */ $dataSource = new Zend_Pdf_FileParserDataSource_File($filePath); /* Attempt to determine the type of font. We can't always trust file * extensions, but try that first since it's fastest. */ $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); /* If it turns out that the file is named improperly and we guess the * wrong type, we'll get null instead of a font object. */ switch ($fileExtension) { case 'ttf': $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions); break; default: /* Unrecognized extension. Try to determine the type by actually * parsing it below. */ $font = null; break; } if (is_null($font)) { /* There was no match for the file extension or the extension was * wrong. Attempt to detect the type of font by actually parsing it. * We'll do the checks in order of most likely format to try to * reduce the detection time. */ // OpenType // TrueType if ((is_null($font)) && ($fileExtension != 'ttf')) { $font = Zend_Pdf_Font::_extractTrueTypeFont($dataSource, $embeddingOptions); } // Type 1 PostScript // Mac OS X dfont // others? } /* Done with the data source object. */ $dataSource = null; if (! is_null($font)) { /* Parsing was successful. Add this font instance to the cache arrays * and return it for use. */ $fontName = $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, '', ''); Zend_Pdf_Font::$_fontNames[$fontName] = $font; $filePathKey = md5($filePath); Zend_Pdf_Font::$_fontFilePaths[$filePathKey] = $font; return $font; } else { /* The type of font could not be determined. Give up. */ throw new Zend_Pdf_Exception("Cannot determine font type: $filePath", Zend_Pdf_Exception::CANT_DETERMINE_FONT_TYPE); } } /**** Internal Methods ****/ /* Font Extraction Methods */ /** * Attempts to extract a TrueType font from the data source. * * If the font parser throws an exception that suggests the data source * simply doesn't contain a TrueType font, catches it and returns null. If * an exception is thrown that suggests the TrueType font is corrupt or * otherwise unusable, throws that exception. If successful, returns the * font object. * * @param Zend_Pdf_FileParserDataSource $dataSource * @param integer $embeddingOptions Options for font embedding. * @return Zend_Pdf_Resource_Font_OpenType_TrueType May also return null if * the data source does not appear to contain a TrueType font. * @throws Zend_Pdf_Exception */ protected static function _extractTrueTypeFont($dataSource, $embeddingOptions) { try { $fontParser = new Zend_Pdf_FileParser_Font_OpenType_TrueType($dataSource);
$fontParser->parse();
if ($fontParser->isAdobeLatinSubset) { $font = new Zend_Pdf_Resource_Font_Simple_Parsed_TrueType($fontParser, $embeddingOptions);
} else {
/* Use Composite Type 0 font which supports Unicode character mapping */
$cidFont = new Zend_Pdf_Resource_Font_CidFont_TrueType($fontParser, $embeddingOptions);
$font = new Zend_Pdf_Resource_Font_Type0($cidFont);
}
} catch (Zend_Pdf_Exception $exception) { /* The following exception codes suggest that this isn't really a * TrueType font. If we caught such an exception, simply return * null. For all other cases, it probably is a TrueType font but has * a problem; throw the exception again. */ $fontParser = null; switch ($exception->getCode()) { case Zend_Pdf_Exception::WRONG_FONT_TYPE: // break intentionally omitted case Zend_Pdf_Exception::BAD_TABLE_COUNT: // break intentionally omitted case Zend_Pdf_Exception::BAD_MAGIC_NUMBER: return null; default: throw $exception; } } return $font;
}}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?