/** * @param string $file * @param array $options * @return array */ public static function generate($file, array $options) { Cufon::log('Processing %s', $file); $script = new FontForgeScript(); $script->open($file); $script->flattenCID(); $script->reEncode('unicode'); $script->selectNone(); if (!empty($options['glyphs'])) { foreach ($options['glyphs'] as $glyph) { $ranges = explode(',', $glyph); foreach ($ranges as $range) { if (strpos($range, '-')) { // the range regex allows for things like 0xff-0xff-0xff, so we'll // just ignore everything between the first and last one. $points = explode('-', $range); $script->selectUnicodeRange(intval(reset($points), 16), intval(end($points), 16)); } else { $script->selectUnicode(intval($range, 16)); } } } } if (!empty($options['customGlyphs'])) { $glyphs = preg_split('//u', $options['customGlyphs'], -1, PREG_SPLIT_NO_EMPTY); foreach ($glyphs as $glyph) { $script->selectUnicode(UnicodeRange::getCodePoint($glyph)); } } $script->selectInvert(); $script->detachAndRemoveGlyphs(); $script->setFontOrder(FontForgeScript::ORDER_CUBIC); if (!$options['disableScaling']) { $script->scaleToEm($options['emSize']); } $script->selectAll(); $script->verticalFlip(0); if ($options['simplify']) { $script->simplify($options['simplifyDelta']); } $script->roundToInt(1); $svgFile = Cufon::getUnusedFilename('.svg'); Cufon::log('Converting to SVG with filename %s', $svgFile); $script->printNameIDs(); $script->generate($svgFile); $output = trim($script->execute()); $fonts = array(); $copyright = ''; if (!empty($output)) { $copyright = self::createJSDocComment("The following copyright notice may not be removed under " . "any circumstances.\n\n{$output}"); } foreach (SVGFontContainer::fromFile($svgFile, $options) as $font) { $fonts[$font->getId()] = sprintf("%s%s(%s);\n", $copyright, $options['callback'], $font->toJavaScript()); } unlink($svgFile); return $fonts; }
/** * @param string $file * @param array $options * @return array */ public static function generate($file, array $options) { Cufon::log('Processing %s', $file); $script = new FontForgeScript(); $script->open($file); $script->selectNone(); if (!empty($options['glyphs'])) { foreach ($options['glyphs'] as $glyph) { $ranges = explode(',', $glyph); foreach ($ranges as $range) { if (strpos($range, '-')) { // the range regex allows for things like 0xff-0xff-0xff, so we'll // just ignore everything between the first and last one. $points = explode('-', $range); $script->selectUnicodeRange(intval(reset($points), 16), intval(end($points), 16)); } else { $script->selectUnicode(intval($range, 16)); } } } } if (!empty($options['customGlyphs'])) { $glyphs = preg_split('//u', $options['customGlyphs'], -1, PREG_SPLIT_NO_EMPTY); foreach ($glyphs as $glyph) { // http://www.php.net/manual/en/function.ord.php#68914 $cp = unpack('N', mb_convert_encoding($glyph, 'UCS-4BE', 'UTF-8')); $script->selectUnicode($cp[1]); } } $script->selectInvert(); $script->detachAndRemoveGlyphs(); $script->setFontOrder(FontForgeScript::ORDER_CUBIC); if (!$options['disableScaling']) { $script->scaleToEm($options['emSize']); } $script->removeAllKerns(); $script->selectAll(); $script->verticalFlip(0); if ($options['simplify']) { $script->simplify($options['simplifyDelta']); } $script->roundToInt(1); $svgFile = Cufon::getUnusedFilename('.svg'); Cufon::log('Converting to SVG with filename %s', $svgFile); $script->generate($svgFile); $script->execute(); $fonts = array(); foreach (SVGFontContainer::fromFile($svgFile) as $font) { $fonts[$font->getId()] = $options['callback'] . '(' . $font->toJSON() . ');'; } unlink($svgFile); return $fonts; }
/** * @return string */ public function execute() { $filename = Cufon::getUnusedFilename('.pe'); file_put_contents($filename, $this->__toString()); $this->files[] = $filename; chmod($filename, 0777); $command = sprintf('env %s -script %s', CUFON_FONTFORGE, escapeshellarg($filename)); Cufon::log('Executing command: %s', $command); $status = 0; $output = array(); exec($command, $output, $status); Cufon::log('Exited with status %d', $status); if ($status > 0) { throw new ConversionException('Conversion failed'); } return implode("\n", $output); }
exit(0); } } } if (empty($files)) { require 'view/upload-empty-error.php'; exit(0); } $errors = array_diff_key(array_filter($options, 'is_null'), $optional); if (!empty($errors)) { require 'view/input-error.php'; exit(0); } ob_start(); $fonts = array(); foreach ($files as $file) { try { foreach (Cufon::generate($file, $options) as $id => $json) { echo $json; $fonts[] = $id; } } catch (ConversionException $e) { ob_clean(); require 'view/conversion-error.php'; exit(0); } } $filename = preg_replace(array('/\\s+/', '/[^a-z0-9_\\-]/i'), array('_', ''), empty($fonts) ? 'Cufon Font' : implode('-', $fonts)) . '.font.js'; header(sprintf('Content-Disposition: attachment; filename=%s', $filename)); header('Content-Type: text/javascript'); }
break; case UPLOAD_ERR_NO_FILE: continue 2; case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: Cufon::log('Upload failed (too large): %s', $_FILES['font']['name'][$key]); require 'view/upload-size-error.php'; exit(0); default: Cufon::log('Upload failed (code: %d): %s', $error, $_FILES['font']['name'][$key]); header('HTTP/1.1 500 Internal Server Error'); echo '500 Internal Server Error'; exit(0); } try { foreach (Cufon::generate($_FILES['font']['tmp_name'][$key], $options) as $id => $json) { echo $json; $fonts[] = $id; } } catch (ConversionException $e) { ob_clean(); require 'view/conversion-error.php'; exit(0); } } } if (!$upload) { require 'view/upload-empty-error.php'; exit(0); } $filename = preg_replace(array('/\\s+/', '/[^a-z0-9_\\-]/i'), array('_', ''), empty($fonts) ? 'Cufon Font' : implode('-', $fonts)) . '.font.js';