/** * Method to add a font to the PDF. * * @param string $font * @param boolean $embedOverride * @throws Exception * @return \Pop\Pdf\Pdf */ public function addFont($font, $embedOverride = false) { // Embed the font file. if (file_exists($font)) { $fontIndex = count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) == 0 ? 1 : count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) + 1; $objectIndex = $this->lastIndex($this->objects) + 1; $fontParser = new Parser\Font($font, $fontIndex, $objectIndex, $this->compress); if (!$fontParser->isEmbeddable() && !$embedOverride) { throw new Exception('Error: The font license does not allow for it to be embedded.'); } $this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts[$fontParser->getFontName()] = $fontParser->getFontRef(); $fontObjects = $fontParser->getObjects(); foreach ($fontObjects as $key => $value) { $this->objects[$key] = $value; } $this->lastFontName = $fontParser->getFontName(); // Else, use a standard font. } else { // Check to make sure the font is a standard PDF font. if (!array_key_exists($font, $this->standardFonts)) { throw new Exception('Error: That font is not contained within the standard PDF fonts.'); } // Set the font index. $ft_index = count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) == 0 ? 1 : count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) + 1; // Set the font name and the next object index. $f = 'MF' . $ft_index; $i = $this->lastIndex($this->objects) + 1; // Add the font to the current page's fonts and add the font to _objects array. $this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts[$font] = "/{$f} {$i} 0 R"; $this->objects[$i] = new Object\Object("{$i} 0 obj\n<<\n /Type /Font\n /Subtype /Type1\n /Name /{$f}\n /BaseFont /{$font}\n /Encoding /WinAnsiEncoding\n>>\nendobj\n\n"); $this->lastFontName = $font; } if (!in_array($this->lastFontName, $this->fonts)) { $this->fonts[] = $this->lastFontName; } return $this; }