示例#1
0
 /**
  * Object constructor
  *
  */
 public function __construct(CidFont\AbstractCidFont $descendantFont)
 {
     parent::__construct();
     $this->_objectFactory->attach($descendantFont->getFactory());
     $this->_fontType = 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 InternalType\NameObject('Type0');
     $this->_resource->BaseFont = new InternalType\NameObject($descendantFont->getResource()->BaseFont->value);
     $this->_resource->DescendantFonts = new InternalType\ArrayObject(array($descendantFont->getResource()));
     $this->_resource->Encoding = new InternalType\NameObject('Identity-H');
     $toUnicode = $this->_objectFactory->newStreamObject(self::getToUnicodeCMapData());
     $this->_resource->ToUnicode = $toUnicode;
 }
示例#2
0
 /**
  * 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 InternalType\NameObject('WinAnsiEncoding');
 }
示例#3
0
 /**
  * Object constructor
  *
  * @param \ZendPdf\BinaryParser\Font\OpenType\AbstractOpenType $fontParser Font parser object
  *   containing OpenType file.
  * @param integer $embeddingOptions Options for font embedding.
  * @throws \ZendPdf\Exception\ExceptionInterface
  */
 public function __construct(Pdf\BinaryParser\Font\OpenType\AbstractOpenType $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(Pdf\Font::NAME_POSTSCRIPT, 'en', 'UTF-8');
     $this->_resource->BaseFont = new InternalType\NameObject($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) {
         $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 InternalType\NumericObject($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;
         } elseif ($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 InternalType\NumericObject($startCode);
                     // First character code
                     $pdfCharsWidths[] = new InternalType\NumericObject($startCode + $widthsInSequence - 1);
                     // Last character code
                     $pdfCharsWidths[] = new InternalType\NumericObject($this->toEmSpace($lastWidth));
                     // Width
                     // Reset widths sequence
                     $startCode = $startCode + $widthsInSequence;
                     $widthsInSequence = 0;
                 }
                 // Collect new width
                 $pdfWidths[] = new InternalType\NumericObject($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 InternalType\NumericObject($startCode);
                         // First character code
                         $pdfCharsWidths[] = new InternalType\ArrayObject($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 InternalType\NumericObject($startCode);
             // First character code
             $pdfCharsWidths[] = new InternalType\ArrayObject($pdfWidths);
             // Widths array
         } elseif ($widthsInSequence != 0) {
             // We have widths sequence
             // Save it as 'c_1st c_last w'.
             $pdfCharsWidths[] = new InternalType\NumericObject($startCode);
             // First character code
             $pdfCharsWidths[] = new InternalType\NumericObject($startCode + $widthsInSequence - 1);
             // Last character code
             $pdfCharsWidths[] = new InternalType\NumericObject($this->toEmSpace($lastWidth));
             // Width
         }
     }
     /* Create the \ZendPdf\InternalType\ArrayObject object and add it to the font's
      * object factory and resource dictionary.
      */
     $widthsArrayElement = new InternalType\ArrayObject($pdfCharsWidths);
     $widthsObject = $this->_objectFactory->newObject($widthsArrayElement);
     $this->_resource->W = $widthsObject;
     /* CIDSystemInfo dictionary */
     $cidSystemInfo = new InternalType\DictionaryObject();
     $cidSystemInfo->Registry = new InternalType\StringObject('Adobe');
     $cidSystemInfo->Ordering = new InternalType\StringObject('UCS');
     $cidSystemInfo->Supplement = new InternalType\NumericObject(0);
     $cidSystemInfoObject = $this->_objectFactory->newObject($cidSystemInfo);
     $this->_resource->CIDSystemInfo = $cidSystemInfoObject;
 }
示例#4
0
 /**
  * @param string $text
  * @param AnchorPath $p
  * @param FontStyle $fontStyle
  *
  * @return mixed[]
  * @throws \Exception
  */
 private function preparePathText($text, AnchorPath $p, ZendAbstractFont $font, FontStyle $fontStyle)
 {
     $fontScale = $fontStyle->getSize() / (double) $font->getUnitsPerEm();
     // split text into encoded characters and widths
     $chars = [];
     $widthSum = 0;
     foreach (TextUtils::getCodes($text) as $code) {
         $char = $font->encodeString(html_entity_decode('&#' . $code . ';', ENT_NOQUOTES, 'UTF-8'), 'UTF-8');
         $width = $font->widthForGlyph($font->glyphNumberForCharacter($code));
         $widthSum += $width;
         $chars[] = [$char, $fontScale * $width];
     }
     // compute start position
     switch ($fontStyle->getHAlign()) {
         case FontStyle::HORIZONTAL_ALIGN_MIDDLE:
             $startPos = ($p->getLen() - $fontScale * $widthSum) / 2.0;
             break;
         case FontStyle::HORIZONTAL_ALIGN_RIGHT:
             $startPos = $p->getLen() - $fontScale * $widthSum;
             break;
         case FontStyle::HORIZONTAL_ALIGN_LEFT:
         default:
             $startPos = 0;
     }
     // compute vertical shift
     switch ($fontStyle->getVAlign()) {
         case FontStyle::VERTICAL_ALIGN_TOP:
             $yShift = $fontScale * ($font->getAscent() - $font->getDescent());
             break;
         case FontStyle::VERTICAL_ALIGN_CENTRAL:
             $yShift = 0.5 * $fontScale * $font->getAscent();
             break;
         case FontStyle::VERTICAL_ALIGN_BOTTOM:
             $yShift = $fontScale * $font->getDescent();
             break;
         case FontStyle::VERTICAL_ALIGN_BASE:
         default:
             $yShift = 0;
     }
     return [$chars, $startPos, $yShift];
 }
示例#5
0
 public function widthForStringUsingFontSize($string, AbstractFont $font, $fontSize)
 {
     $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;
     return $stringWidth;
 }
示例#6
0
 /**
  * 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 \ZendPdf\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 \ZendPdf\Resource\Font\AbstractFont $font Font
  * @param \ZendPdf\BinaryParser\Font\OpenType\AbstractOpenType $fontParser Font parser object containing parsed TrueType file.
  * @param integer $embeddingOptions Options for font embedding.
  * @return \ZendPdf\InternalType\DictionaryObject
  * @throws \ZendPdf\Exception\ExceptionInterface
  */
 public static function factory(AbstractFont $font, OpenTypeFontParser\AbstractOpenType $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 InternalType\DictionaryObject();
     $fontDescriptor->Type = new InternalType\NameObject('FontDescriptor');
     $fontDescriptor->FontName = new InternalType\NameObject($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 InternalType\NumericObject($flags);
     $fontBBox = array(new InternalType\NumericObject($font->toEmSpace($fontParser->xMin)), new InternalType\NumericObject($font->toEmSpace($fontParser->yMin)), new InternalType\NumericObject($font->toEmSpace($fontParser->xMax)), new InternalType\NumericObject($font->toEmSpace($fontParser->yMax)));
     $fontDescriptor->FontBBox = new InternalType\ArrayObject($fontBBox);
     $fontDescriptor->ItalicAngle = new InternalType\NumericObject($fontParser->italicAngle);
     $fontDescriptor->Ascent = new InternalType\NumericObject($font->toEmSpace($fontParser->ascent));
     $fontDescriptor->Descent = new InternalType\NumericObject($font->toEmSpace($fontParser->descent));
     $fontDescriptor->CapHeight = new InternalType\NumericObject($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 InternalType\NumericObject(0);
     $fontDescriptor->MissingWidth = new InternalType\NumericObject($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 & 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 & 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 \\ZendPdf\\Font::EMBED_SUPPRESS_EMBED_EXCEPTION ' . 'in the $options parameter of the font constructor.';
                 throw new Exception\DomainException($message);
             }
         } 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 InternalType\NumericObject(strlen($fontFile));
             if (!($embeddingOptions & Pdf\Font::EMBED_DONT_COMPRESS)) {
                 /* Compress the font file using Flate. This generally cuts file
                  * sizes by about half!
                  */
                 $fontFileObject->dictionary->Filter = new InternalType\NameObject('FlateDecode');
             }
             // Type1 fonts are not implemented now
             // if ($fontParser instanceof OpenTypeFontParser\Type1) {
             //     $fontDescriptor->FontFile  = $fontFileObject;
             // } else
             if ($fontParser instanceof OpenTypeFontParser\TrueType) {
                 $fontDescriptor->FontFile2 = $fontFileObject;
             } else {
                 $fontDescriptor->FontFile3 = $fontFileObject;
             }
         }
     }
     return $fontDescriptor;
 }
示例#7
0
 private function isFontDefiningSpaceInSingleByte(ZendResourceFont $font)
 {
     return $font->getFontType() === ZendFont::TYPE_STANDARD;
 }
示例#8
0
文件: Page.php 项目: marcyniu/vendors
 /**
  * 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 \ZendPdf\Exception\ExceptionInterface
  * @return \ZendPdf\Page
  */
 public function drawText($text, $x, $y, $charEncoding = '')
 {
     if ($this->_font === null) {
         throw new Exception\LogicException('Font has not been set');
     }
     $this->_addProcSet('Text');
     $textObj = new InternalType\StringObject($this->_font->encodeString($text, $charEncoding));
     $xObj = new InternalType\NumericObject($x);
     $yObj = new InternalType\NumericObject($y);
     $this->_contents .= "BT\n" . $xObj->toString() . ' ' . $yObj->toString() . " Td\n" . $textObj->toString() . " Tj\n" . "ET\n";
     return $this;
 }