/** * Returns the total width in points of the string using the specified font and * size. * * This is not the most efficient way to perform this calculation. I'm * concentrating optimization efforts on the upcoming layout manager class. * Similar calculations exist inside the layout manager class, but widths are * generally calculated only after determining line fragments. * * @param string $string * @param Zend_Pdf_Resource_Font $font * @param float $fontSize Font size in points * @return float */ public function widthForStringUsingFontSize($string, $font, $fontSize) { $drawingString = '"libiconv"' == ICONV_IMPL ? iconv('UTF-8', 'UTF-16BE//IGNORE', $string) : @iconv('UTF-8', 'UTF-16BE', $string); $characters = array(); for ($i = 0; $i < strlen($drawingString); $i++) { $characters[] = ord($drawingString[$i++]) << 8 | ord($drawingString[$i]); } $glyphs = $font->glyphNumbersForCharacters($characters); $widths = $font->widthsForGlyphs($glyphs); $stringWidth = array_sum($widths) / $font->getUnitsPerEm() * $fontSize; return $stringWidth; }
/** * Calculate the width of given text in points taking into account current font and font-size * * @param string $text * @param Zend_Pdf_Resource_Font $font * @param float $font_size * @return float */ public function getTextWidth($text, Zend_Pdf_Resource_Font $font, $font_size) { $drawing_text = iconv('', 'UTF-16BE', $text); $characters = array(); for ($i = 0; $i < strlen($drawing_text); $i++) { $characters[] = ord($drawing_text[$i++]) << 8 | ord($drawing_text[$i]); } $glyphs = $font->glyphNumbersForCharacters($characters); $widths = $font->widthsForGlyphs($glyphs); $text_width = array_sum($widths) / $font->getUnitsPerEm() * $font_size; return $text_width; }
/** * Object constructor * */ public function __construct(Zend_Pdf_Resource_Font_CidFont $descendantFont) { parent::__construct(); $this->_objectFactory->attach($descendantFont->getFactory()); $this->_fontType = Zend_Pdf_Font::TYPE_TYPE_0; $this->_descendantFont = $descendantFont; $this->_fontNames = $descendantFont->getFontNames(); $this->_isBold = $descendantFont->isBold(); $this->_isItalic = $descendantFont->isItalic(); $this->_isMonospaced = $descendantFont->isMonospace(); $this->_underlinePosition = $descendantFont->getUnderlinePosition(); $this->_underlineThickness = $descendantFont->getUnderlineThickness(); $this->_strikePosition = $descendantFont->getStrikePosition(); $this->_strikeThickness = $descendantFont->getStrikeThickness(); $this->_unitsPerEm = $descendantFont->getUnitsPerEm(); $this->_ascent = $descendantFont->getAscent(); $this->_descent = $descendantFont->getDescent(); $this->_lineGap = $descendantFont->getLineGap(); $this->_resource->Subtype = new Zend_Pdf_Element_Name('Type0'); $this->_resource->BaseFont = new Zend_Pdf_Element_Name($descendantFont->getResource()->BaseFont->value); $this->_resource->DescendantFonts = new Zend_Pdf_Element_Array(array($descendantFont->getResource())); $this->_resource->Encoding = new Zend_Pdf_Element_Name('Identity-H'); $toUnicode = $this->_objectFactory->newStreamObject(self::getToUnicodeCMapData()); $this->_resource->ToUnicode = $toUnicode; }
private function setLineHeight($numLines = 1) { if (empty($numLines)) { $numLines = 1; } $lineLeading = intval($this->_font->getLineHeight() / $this->_font->getUnitsPerEm()); $this->_currentHeight = $this->getHeight() * $numLines + $lineLeading; return $this; }
/** * Draw a line of text at the specified position. * * @param string $text * @param float $x * @param float $y * @param string $charEncoding (optional) Character encoding of source text. * Defaults to current locale. * @throws Zend_Pdf_Exception * @return Zend_Pdf_Canvas_Interface */ public function drawText($text, $x, $y, $charEncoding = '') { if ($this->_font === null) { require_once 'Zend/Pdf/Exception.php'; throw new Zend_Pdf_Exception('Font has not been set'); } $this->_addProcSet('Text'); $textObj = new Zend_Pdf_Element_String($this->_font->encodeString($text, $charEncoding)); $xObj = new Zend_Pdf_Element_Numeric($x); $yObj = new Zend_Pdf_Element_Numeric($y); $this->_contents .= "BT\n" . $xObj->toString() . ' ' . $yObj->toString() . " Td\n" . $textObj->toString() . " Tj\n" . "ET\n"; return $this; }
/** * Object constructor * * 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 it is not requried that fonts be embedded within the PDF file * to use them. If the recipient of the PDF has the font installed on their * computer, they will see the correct fonts in the document. If they don't, * the PDF viewer will substitute or synthesize a replacement. * * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object * containing OpenType file. * @param integer $embeddingOptions Options for font embedding. * @throws Zend_Pdf_Exception */ public function __construct(Zend_Pdf_FileParser_Font_OpenType $fontParser, $embeddingOptions) { $fontParser->parse(); parent::__construct($embeddingOptions); /* Object properties */ $this->_fontNames = $fontParser->names; $this->_isBold = $fontParser->isBold; $this->_isItalic = $fontParser->isItalic; $this->_isMonospaced = $fontParser->isMonospaced; $this->_underlinePosition = $fontParser->underlinePosition; $this->_underlineThickness = $fontParser->underlineThickness; $this->_strikePosition = $fontParser->strikePosition; $this->_strikeThickness = $fontParser->strikeThickness; $this->_unitsPerEm = $fontParser->unitsPerEm; $this->_ascent = $fontParser->ascent; $this->_descent = $fontParser->descent; $this->_lineGap = $fontParser->lineGap; $this->_glyphWidths = $fontParser->glyphWidths; $this->_glyphMaxIndex = count($this->_glyphWidths) - 1; $this->cmap = $fontParser->cmap; /* Resource dictionary */ $baseFont = $this->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8'); $this->_resource->BaseFont = new Zend_Pdf_Element_Name($baseFont); $this->_resource->FirstChar = new Zend_Pdf_Element_Numeric(0); $this->_resource->LastChar = new Zend_Pdf_Element_Numeric(255); /* Build up the widths array and add it as an indirect object. The * character codes contained in this array are the Unicode characters * representing the WinAnsi (CP1252) character set. This corresponds to * to encoding method specified below. */ $characterCodes = array(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x20ac, 0x0, 0x201a, 0x192, 0x201e, 0x2026, 0x2020, 0x2021, 0x2c6, 0x2030, 0x160, 0x2039, 0x152, 0x0, 0x17d, 0x0, 0x0, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x2dc, 0x2122, 0x161, 0x203a, 0x153, 0x0, 0x17e, 0x178, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff); /* Convert characters to glyphs and then get the widths. */ $glyphNumbers = $this->cmap->glyphNumbersForCharacters($characterCodes); $glyphWidths = $this->widthsForGlyphs($glyphNumbers); /* Now convert the scalar glyph widths to Zend_Pdf_Element_Numeric objects. */ $pdfWidths = array(); foreach ($glyphWidths as $width) { $pdfWidths[] = new Zend_Pdf_Element_Numeric($this->_toEmSpace($width)); } /* Create the Zend_Pdf_Element_Array object and add it to the font's * object factory and resource dictionary. */ $widthsArrayElement = new Zend_Pdf_Element_Array($pdfWidths); $widthsObject = $this->_objectFactory->newObject($widthsArrayElement); $this->_resource->Widths = $widthsObject; $this->_resource->Encoding = new Zend_Pdf_Element_Name('WinAnsiEncoding'); }
/** * Object constructor * */ public function __construct() { parent::__construct(); /** * @todo * It's easy to add other encodings support now (Standard-Encoding, MacRomanEncoding, * PDFDocEncoding, MacExpertEncoding, Symbol, and ZapfDingbats). * Steps for the implementation: * - completely describe all PDF single byte encodings in the documentation * - implement non-WinAnsi encodings processing into encodeString()/decodeString() methods * * These encodings will be automatically supported for standard builtin PDF fonts as well * as for external fonts. */ $this->_resource->Encoding = new Zend_Pdf_Element_Name('WinAnsiEncoding'); }
/** * Object constructor */ public function __construct() { parent::__construct(); $this->_resource->Subtype = new Zend_Pdf_Element_Name('Type1'); }
/** * Object constructor * * 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 it is not requried that fonts be embedded within the PDF file * to use them. If the recipient of the PDF has the font installed on their * computer, they will see the correct fonts in the document. If they don't, * the PDF viewer will substitute or synthesize a replacement. * * * @param Zend_Pdf_Resource_Font $font Font * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object containing parsed TrueType file. * @param integer $embeddingOptions Options for font embedding. * @return Zend_Pdf_Element_Dictionary * @throws Zend_Pdf_Exception */ public static function factory(Zend_Pdf_Resource_Font $font, Zend_Pdf_FileParser_Font_OpenType $fontParser, $embeddingOptions) { /* The font descriptor object contains the rest of the font metrics and * the information about the embedded font program (if applicible). */ $fontDescriptor = new Zend_Pdf_Element_Dictionary(); $fontDescriptor->Type = new Zend_Pdf_Element_Name('FontDescriptor'); $fontDescriptor->FontName = new Zend_Pdf_Element_Name($font->getResource()->BaseFont->value); /* The font flags value is a bitfield that describes the stylistic * attributes of the font. We will set as many of the bits as can be * determined from the font parser. */ $flags = 0; if ($fontParser->isMonospaced) { // bit 1: FixedPitch $flags |= 1 << 0; } if ($fontParser->isSerifFont) { // bit 2: Serif $flags |= 1 << 1; } if (!$fontParser->isAdobeLatinSubset) { // bit 3: Symbolic $flags |= 1 << 2; } if ($fontParser->isScriptFont) { // bit 4: Script $flags |= 1 << 3; } if ($fontParser->isAdobeLatinSubset) { // bit 6: Nonsymbolic $flags |= 1 << 5; } if ($fontParser->isItalic) { // bit 7: Italic $flags |= 1 << 6; } // bits 17-19: AllCap, SmallCap, ForceBold; not available $fontDescriptor->Flags = new Zend_Pdf_Element_Numeric($flags); $fontBBox = array(new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->xMin)), new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->yMin)), new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->xMax)), new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->yMax))); $fontDescriptor->FontBBox = new Zend_Pdf_Element_Array($fontBBox); $fontDescriptor->ItalicAngle = new Zend_Pdf_Element_Numeric($fontParser->italicAngle); $fontDescriptor->Ascent = new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->ascent)); $fontDescriptor->Descent = new Zend_Pdf_Element_Numeric($font->toEmSpace($fontParser->descent)); $fontDescriptor->CapHeight = new Zend_Pdf_Element_Numeric($fontParser->capitalHeight); /** * The vertical stem width is not yet extracted from the OpenType font * file. For now, record zero which is interpreted as 'unknown'. * @todo Calculate value for StemV. */ $fontDescriptor->StemV = new Zend_Pdf_Element_Numeric(0); $fontDescriptor->MissingWidth = new Zend_Pdf_Element_Numeric($fontParser->glyphWidths[0]); /* Set up font embedding. This is where the actual font program itself * is embedded within the PDF document. * * Note that it is not requried that fonts be embedded within the PDF * document to use them. If the recipient of the PDF has the font * installed on their computer, they will see the correct fonts in the * document. If they don't, the PDF viewer will substitute or synthesize * a replacement. * * There are several guidelines for font embedding: * * First, the developer might specifically request not to embed the font. */ if (!($embeddingOptions & Zend_Pdf_Font::EMBED_DONT_EMBED)) { /* Second, the font author may have set copyright bits that prohibit * the font program from being embedded. Yes this is controversial, * but it's the rules: * http://partners.adobe.com/public/developer/en/acrobat/sdk/FontPolicies.pdf * * To keep the developer in the loop, and to prevent surprising bug * reports of "your PDF doesn't have the right fonts," throw an * exception if the font cannot be embedded. */ if (!$fontParser->isEmbeddable) { /* This exception may be suppressed if the developer decides that * it's not a big deal that the font program can't be embedded. */ if (!($embeddingOptions & Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION)) { $message = 'This font cannot be embedded in the PDF document. If you would like to use ' . 'it anyway, you must pass Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION ' . 'in the $options parameter of the font constructor.'; throw new Zend_Pdf_Exception($message, Zend_Pdf_Exception::FONT_CANT_BE_EMBEDDED); } } else { /* Otherwise, the default behavior is to embed all custom fonts. */ /* This section will change soon to a stream object data * provider model so that we don't have to keep a copy of the * entire font in memory. * * We also cannot build font subsetting until the data provider * model is in place. */ $fontFile = $fontParser->getDataSource()->readAllBytes(); $fontFileObject = $font->getFactory()->newStreamObject($fontFile); $fontFileObject->dictionary->Length1 = new Zend_Pdf_Element_Numeric(strlen($fontFile)); if (!($embeddingOptions & Zend_Pdf_Font::EMBED_DONT_COMPRESS)) { /* Compress the font file using Flate. This generally cuts file * sizes by about half! */ $fontFileObject->dictionary->Filter = new Zend_Pdf_Element_Name('FlateDecode'); } if ($fontParser instanceof Zend_Pdf_FileParser_Font_OpenType_Type1) { $fontDescriptor->FontFile = $fontFileObject; } else { if ($fontParser instanceof Zend_Pdf_FileParser_Font_OpenType_TrueType) { $fontDescriptor->FontFile2 = $fontFileObject; } else { $fontDescriptor->FontFile3 = $fontFileObject; } } } } return $fontDescriptor; }
/** * Object constructor * * @param Zend_Pdf_FileParser_Font_OpenType $fontParser Font parser object * containing OpenType file. * @param integer $embeddingOptions Options for font embedding. * @throws Zend_Pdf_Exception */ public function __construct(Zend_Pdf_FileParser_Font_OpenType $fontParser) { parent::__construct(); $fontParser->parse(); /* Object properties */ $this->_fontNames = $fontParser->names; $this->_isBold = $fontParser->isBold; $this->_isItalic = $fontParser->isItalic; $this->_isMonospaced = $fontParser->isMonospaced; $this->_underlinePosition = $fontParser->underlinePosition; $this->_underlineThickness = $fontParser->underlineThickness; $this->_strikePosition = $fontParser->strikePosition; $this->_strikeThickness = $fontParser->strikeThickness; $this->_unitsPerEm = $fontParser->unitsPerEm; $this->_ascent = $fontParser->ascent; $this->_descent = $fontParser->descent; $this->_lineGap = $fontParser->lineGap; $this->_cmap = $fontParser->cmap; /* Resource dictionary */ $baseFont = $this->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT, 'en', 'UTF-8'); $this->_resource->BaseFont = new Zend_Pdf_Element_Name($baseFont); /** * Prepare widths array. */ /* Constract characters widths array using font CMap and glyphs widths array */ $glyphWidths = $fontParser->glyphWidths; $charGlyphs = $this->_cmap->getCoveredCharactersGlyphs(); $charWidths = array(); foreach ($charGlyphs as $charCode => $glyph) { if (isset($glyphWidths[$glyph]) && !is_null($glyphWidths[$glyph])) { $charWidths[$charCode] = $glyphWidths[$glyph]; } } $this->_charWidths = $charWidths; $this->_missingCharWidth = $glyphWidths[0]; /* Width array optimization. Step1: extract default value */ $widthFrequencies = array_count_values($charWidths); $defaultWidth = null; $defaultWidthFrequency = -1; foreach ($widthFrequencies as $width => $frequency) { if ($frequency > $defaultWidthFrequency) { $defaultWidth = $width; $defaultWidthFrequency = $frequency; } } // Store default value in the font dictionary $this->_resource->DW = new Zend_Pdf_Element_Numeric($this->toEmSpace($defaultWidth)); // Remove characters which corresponds to default width from the widths array $defWidthChars = array_keys($charWidths, $defaultWidth); foreach ($defWidthChars as $charCode) { unset($charWidths[$charCode]); } // Order cheracter widths aray by character codes ksort($charWidths, SORT_NUMERIC); /* Width array optimization. Step2: Compact character codes sequences */ $lastCharCode = -1; $widthsSequences = array(); foreach ($charWidths as $charCode => $width) { if ($lastCharCode == -1) { $charCodesSequense = array(); $sequenceStartCode = $charCode; } else { if ($charCode != $lastCharCode + 1) { // New chracters sequence detected $widthsSequences[$sequenceStartCode] = $charCodesSequense; $charCodesSequense = array(); $sequenceStartCode = $charCode; } } $charCodesSequense[] = $width; $lastCharCode = $charCode; } // Save last sequence, if widths array is not empty (it may happens for monospaced fonts) if (count($charWidths) != 0) { $widthsSequences[$sequenceStartCode] = $charCodesSequense; } $pdfCharsWidths = array(); foreach ($widthsSequences as $startCode => $widthsSequence) { /* Width array optimization. Step3: Compact widths sequences */ $pdfWidths = array(); $lastWidth = -1; $widthsInSequence = 0; foreach ($widthsSequence as $width) { if ($lastWidth != $width) { // New width is detected if ($widthsInSequence != 0) { // Previous width value was a part of the widths sequence. Save it as 'c_1st c_last w'. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width // Reset widths sequence $startCode = $startCode + $widthsInSequence; $widthsInSequence = 0; } // Collect new width $pdfWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($width)); $lastWidth = $width; } else { // Width is equal to previous if (count($pdfWidths) != 0) { // We already have some widths collected // So, we've just detected new widths sequence // Remove last element from widths list, since it's a part of widths sequence array_pop($pdfWidths); // and write the rest if it's not empty if (count($pdfWidths) != 0) { // Save it as 'c_1st [w1 w2 ... wn]'. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array // Reset widths collection $startCode += count($pdfWidths); $pdfWidths = array(); } $widthsInSequence = 2; } else { // Continue widths sequence $widthsInSequence++; } } } // Check if we have widths collection or widths sequence to wite it down if (count($pdfWidths) != 0) { // We have some widths collected // Save it as 'c_1st [w1 w2 ... wn]'. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code $pdfCharsWidths[] = new Zend_Pdf_Element_Array($pdfWidths); // Widths array } else { if ($widthsInSequence != 0) { // We have widths sequence // Save it as 'c_1st c_last w'. $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode); // First character code $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($startCode + $widthsInSequence - 1); // Last character code $pdfCharsWidths[] = new Zend_Pdf_Element_Numeric($this->toEmSpace($lastWidth)); // Width } } } /* Create the Zend_Pdf_Element_Array object and add it to the font's * object factory and resource dictionary. */ $widthsArrayElement = new Zend_Pdf_Element_Array($pdfCharsWidths); $widthsObject = $this->_objectFactory->newObject($widthsArrayElement); $this->_resource->W = $widthsObject; /* CIDSystemInfo dictionary */ $cidSystemInfo = new Zend_Pdf_Element_Dictionary(); $cidSystemInfo->Registry = new Zend_Pdf_Element_String('Adobe'); $cidSystemInfo->Ordering = new Zend_Pdf_Element_String('UCS'); $cidSystemInfo->Supplement = new Zend_Pdf_Element_Numeric(0); $cidSystemInfoObject = $this->_objectFactory->newObject($cidSystemInfo); $this->_resource->CIDSystemInfo = $cidSystemInfoObject; }
/** * Returns the total width in points of the string using the specified font and * size. * * This is not the most efficient way to perform this calculation. I'm * concentrating optimization efforts on the upcoming layout manager class. * Similar calculations exist inside the layout manager class, but widths are * generally calculated only after determining line fragments. * * @link http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535 * @param string $string * @param Zend_Pdf_Resource_Font $font * @param float $fontSize Font size in points * @return float */ protected function width_to_points($string, $font, $fontSize) { try { $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string); $characters = array(); for ($i = 0; $i < strlen($drawingString); $i++) { $characters[] = ord($drawingString[$i++]) << 8 | ord($drawingString[$i]); } $glyphs = $font->glyphNumbersForCharacters($characters); $widths = $font->widthsForGlyphs($glyphs); $stringWidth = array_sum($widths) / $font->getUnitsPerEm() * $fontSize; } catch (Exception $e) { die($e->getMessage); } return $stringWidth; }
/** * Calculate text width * * @param string $text * @param Zend_Pdf_Page|Zend_Pdf_Resource_Font $resource * @param int $fontSize * @param string $encoding * @return double */ public function getTextWidth($text, $resource, $fontSize = null, $encoding = 'UTF-8') { if ($resource instanceof Zend_Pdf_Page) { $font = $resource->getFont(); $fontSize = $resource->getFontSize(); } elseif ($resource instanceof Zend_Pdf_Resource_Font) { $font = $resource; if ($fontSize === null) { throw new Exception('The fontsize is unknown'); } } if (!$font instanceof Zend_Pdf_Resource_Font) { throw new Exception('Invalid resource passed'); } //$drawingText = iconv ( '', $encoding, $text ); $drawingText = $text; $characters = array(); for ($i = 0; $i < strlen($drawingText); $i++) { $characters[] = ord($drawingText[$i]); } $glyphs = $font->glyphNumbersForCharacters($characters); $widths = $font->widthsForGlyphs($glyphs); $textWidth = array_sum($widths) / $font->getUnitsPerEm() * $fontSize; return $textWidth; }