Skip to content

Commit

Permalink
Font metrics refactor (#13)
Browse files Browse the repository at this point in the history
* Initial work on refactoring the font metric code

- Moved away from the archaic method of writing out PHP to a file and including it in the next run, by writing out the metrics data as JSON and reading from the file as required.
- General tidying up of code and trying to make sense of the > character 127 processing, and why we're doing what we're doing.
- For some reason, character widths appear to be broken for characters > 127 when reading the 127 file - when the file doesn't exist and we process on-the-fly, the widths appear to be OK. Hmmz.

* A bunch of reactors and cleanup around writing and accessing the font metrics and character widths files

* Fix to generate_unicode_font_specs, to ensure that we are actually outputting the correct font - reset() doesn't like operating on the results of explode() directly
Updated PDF.php to ensure that font metrics files get written with .php extension - aborted the JSON path!
Changed some of the variable naming conventions back to their original values in PDF.php, because of the side-effect of these variables not existing in the resulting file (boo, more refactor needed)

* Merge branch 'master' of github.com:DocnetUK/tfpdf into font_metrics_refactor

# Conflicts:
#	src/font/unifont/babelstonehan-bold-font-metrics.json
#	src/font/unifont/babelstonehan-bold-font-metrics.php
#	src/font/unifont/babelstonehan-bold.mtx.php
#	src/font/unifont/babelstonehan-bolditalic-font-metrics.json
#	src/font/unifont/babelstonehan-bolditalic-font-metrics.php
#	src/font/unifont/babelstonehan-bolditalic.mtx.php
#	src/font/unifont/babelstonehan-font-metrics.json
#	src/font/unifont/babelstonehan-font-metrics.php
#	src/font/unifont/babelstonehan-italic-font-metrics.json
#	src/font/unifont/babelstonehan-italic-font-metrics.php
#	src/font/unifont/babelstonehan-italic.mtx.php
#	src/font/unifont/babelstonehan.mtx.php
#	src/font/unifont/dejavusans-bold-font-metrics.json
#	src/font/unifont/dejavusans-bold-font-metrics.php
#	src/font/unifont/dejavusans-bold.mtx.php
#	src/font/unifont/dejavusans-boldoblique-font-metrics.json
#	src/font/unifont/dejavusans-boldoblique-font-metrics.php
#	src/font/unifont/dejavusans-boldoblique.mtx.php
#	src/font/unifont/dejavusans-extralight-font-metrics.json
#	src/font/unifont/dejavusans-extralight-font-metrics.php
#	src/font/unifont/dejavusans-extralight.mtx.php
#	src/font/unifont/dejavusans-oblique-font-metrics.json
#	src/font/unifont/dejavusans-oblique-font-metrics.php
#	src/font/unifont/dejavusans-oblique.mtx.php
#	src/font/unifont/dejavusanscondensed-bold-font-metrics.json
#	src/font/unifont/dejavusanscondensed-bold-font-metrics.php
#	src/font/unifont/dejavusanscondensed-bold.mtx.php
#	src/font/unifont/dejavusanscondensed-boldoblique-font-metrics.json
#	src/font/unifont/dejavusanscondensed-boldoblique-font-metrics.php
#	src/font/unifont/dejavusanscondensed-boldoblique.mtx.php
#	src/font/unifont/dejavusanscondensed-font-metrics.json
#	src/font/unifont/dejavusanscondensed-font-metrics.php
#	src/font/unifont/dejavusanscondensed-oblique-font-metrics.json
#	src/font/unifont/dejavusanscondensed-oblique-font-metrics.php
#	src/font/unifont/dejavusanscondensed-oblique.mtx.php
#	src/font/unifont/dejavusanscondensed.mtx.php
#	src/font/unifont/dejavusansmono-bold-font-metrics.json
#	src/font/unifont/dejavusansmono-bold-font-metrics.php
#	src/font/unifont/dejavusansmono-bold.mtx.php
#	src/font/unifont/dejavusansmono-boldoblique-font-metrics.json
#	src/font/unifont/dejavusansmono-boldoblique-font-metrics.php
#	src/font/unifont/dejavusansmono-boldoblique.mtx.php
#	src/font/unifont/dejavusansmono-font-metrics.json
#	src/font/unifont/dejavusansmono-font-metrics.php
#	src/font/unifont/dejavusansmono-oblique-font-metrics.json
#	src/font/unifont/dejavusansmono-oblique-font-metrics.php
#	src/font/unifont/dejavusansmono-oblique.mtx.php
#	src/font/unifont/dejavusansmono.mtx.php
#	src/font/unifont/dejavuserif-bold-font-metrics.json
#	src/font/unifont/dejavuserif-bold-font-metrics.php
#	src/font/unifont/dejavuserif-bold.mtx.php
#	src/font/unifont/dejavuserif-bolditalic-font-metrics.json
#	src/font/unifont/dejavuserif-bolditalic-font-metrics.php
#	src/font/unifont/dejavuserif-bolditalic.mtx.php
#	src/font/unifont/dejavuserif-font-metrics.json
#	src/font/unifont/dejavuserif-font-metrics.php
#	src/font/unifont/dejavuserif-italic-font-metrics.json
#	src/font/unifont/dejavuserif-italic-font-metrics.php
#	src/font/unifont/dejavuserif-italic.mtx.php
#	src/font/unifont/dejavuserif.mtx.php
#	src/font/unifont/dejavuserifcondensed-bold-font-metrics.json
#	src/font/unifont/dejavuserifcondensed-bold-font-metrics.php
#	src/font/unifont/dejavuserifcondensed-bold.mtx.php
#	src/font/unifont/dejavuserifcondensed-bolditalic-font-metrics.json
#	src/font/unifont/dejavuserifcondensed-bolditalic-font-metrics.php
#	src/font/unifont/dejavuserifcondensed-bolditalic.mtx.php
#	src/font/unifont/dejavuserifcondensed-font-metrics.json
#	src/font/unifont/dejavuserifcondensed-font-metrics.php
#	src/font/unifont/dejavuserifcondensed-italic-font-metrics.json
#	src/font/unifont/dejavuserifcondensed-italic-font-metrics.php
#	src/font/unifont/dejavuserifcondensed-italic.mtx.php
#	src/font/unifont/dejavuserifcondensed.mtx.php
#	src/tFPDF/PDF.php
  • Loading branch information
David Wilcock authored Nov 25, 2016
1 parent b21d98a commit 0587857
Show file tree
Hide file tree
Showing 28 changed files with 56 additions and 53 deletions.
3 changes: 2 additions & 1 deletion bin/generate_unicode_font_specs.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
if (count($arr_matches) > 1) {

//$str_font_family = str_replace("-", "", $arr_matches[1]);
$str_font_family = reset(explode("-", $arr_matches[1]));
$arr_bits = explode("-", $arr_matches[1]);
$str_font_family = reset($arr_bits);
$str_style = "";
if (strpos($arr_matches[1], "Bold") !== FALSE) {
$str_style .= "B";
Expand Down
62 changes: 31 additions & 31 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"name": "docnet/tfpdf",
"description": "tfpdf",
"keywords": ["pdf"],
"homepage": "http://github.com/DocnetUK/tfpdf",
"type": "library",
"license": "LGPL",
"authors": [
{
"name": "David Wilcock",
"email": "[email protected]"
},
{
"name": "Dave Blencowe",
"email": "[email protected]"
},
{
"name": "Ian Back",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": { "tFPDF\\": "src/tFPDF/" }
"name": "docnet/tfpdf",
"description": "tfpdf",
"keywords": ["pdf"],
"homepage": "http://github.com/DocnetUK/tfpdf",
"type": "library",
"license": "LGPL",
"authors": [
{
"name": "David Wilcock",
"email": "[email protected]"
},
"scripts": {
"test": "phpunit"
{
"name": "Dave Blencowe",
"email": "[email protected]"
},
"require": {
"php": ">= 5.5",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "5.5.*"
{
"name": "Ian Back",
"email": "[email protected]"
}
}
],
"autoload": {
"psr-4": { "tFPDF\\": "src/tFPDF/" }
},
"scripts": {
"test": "phpunit"
},
"require": {
"php": ">= 5.5",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "5.5.*"
}
}
44 changes: 23 additions & 21 deletions src/tFPDF/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class PDF
/**
* @var string
*/
protected $str_font_metrics_file_suffix = '-font-metrics.json';
protected $str_font_metrics_file_suffix = '-font-metrics.php';

/**
* @var string
Expand Down Expand Up @@ -1012,8 +1012,8 @@ public function AddFont($str_family, $str_style = '', $str_file = '', $bol_unico
$str_file = str_replace(' ', '', $str_family) . strtolower($str_style) . '.php';
}
}
$str_font_key = $str_family . $str_style;
if (isset($this->arr_fonts[$str_font_key])) {
$fontkey = $str_family . $str_style;
if (isset($this->arr_fonts[$fontkey])) {
return;
}
if ($bol_unicode) {
Expand All @@ -1024,20 +1024,22 @@ public function AddFont($str_family, $str_style = '', $str_file = '', $bol_unico
}
$str_unicode_file = $this->str_unifont_path . strtolower(substr($str_file, 0, (strpos($str_file, '.'))));
$str_unicode_filename = $this->getFontPath() . $str_unicode_file;
$str_name = '';
$int_original_size = 0;
$name = '';
$originalsize = 0;
$arr_ttf_stat = stat($str_ttf_filename);
if (file_exists($str_unicode_filename . $this->str_font_metrics_file_suffix)) {
include($str_unicode_filename . $this->str_font_metrics_file_suffix);
}
$arr_descriptors = [];
$flt_underline_pos = 0.00;
$flt_underline_thickness = 0.00;
if (!isset($str_type) || !isset($str_name) || $int_original_size != $arr_ttf_stat['size']) {

if (!isset($type) || !isset($name) || $originalsize != $arr_ttf_stat['size']) {

$obj_ttf = new TTFontFile();
$obj_ttf->getMetrics($str_ttf_filename);
$arr_character_widths = $obj_ttf->getCharWidths();
$str_name = preg_replace('/[ ()]/', '', $obj_ttf->getFullName());
$name = preg_replace('/[ ()]/', '', $obj_ttf->getFullName());
$arr_descriptors = array(
'Ascent' => round($obj_ttf->getAscent()),
'Descent' => round($obj_ttf->getDescent()),
Expand All @@ -1050,18 +1052,18 @@ public function AddFont($str_family, $str_style = '', $str_file = '', $bol_unico
);
$flt_underline_pos = round($obj_ttf->getUnderlinePosition());
$flt_underline_thickness = round($obj_ttf->getUnderlineThickness());
$int_original_size = $arr_ttf_stat['size'] + 0;
$str_type = self::FONT_TRUETYPE;
$originalsize = $arr_ttf_stat['size'] + 0;
$type = self::FONT_TRUETYPE;
// Generate metrics .php file
$str_metrics_data = '<?php' . "\n";
$str_metrics_data .= '$name=\'' . $str_name . "';\n";
$str_metrics_data .= '$type=\'' . $str_type . "';\n";
$str_metrics_data .= '$name=\'' . $name . "';\n";
$str_metrics_data .= '$type=\'' . $type . "';\n";
$str_metrics_data .= '$desc=' . var_export($arr_descriptors, true) . ";\n";
$str_metrics_data .= '$up=' . $flt_underline_pos . ";\n";
$str_metrics_data .= '$ut=' . $flt_underline_thickness . ";\n";
$str_metrics_data .= '$ttffile=\'' . str_replace(__DIR__ . "/", "", $str_ttf_filename) . "';\n";
$str_metrics_data .= '$originalsize=' . $int_original_size . ";\n";
$str_metrics_data .= '$fontkey=\'' . $str_font_key . "';\n";
$str_metrics_data .= '$originalsize=' . $originalsize . ";\n";
$str_metrics_data .= '$fontkey=\'' . $fontkey . "';\n";
$str_metrics_data .= "?>";
if (is_writable(dirname($this->getFontPath() . $this->str_unifont_path))) {
$this->writeFontFile($str_unicode_filename . $this->str_font_metrics_file_suffix, $str_metrics_data);
Expand All @@ -1078,20 +1080,20 @@ public function AddFont($str_family, $str_style = '', $str_file = '', $bol_unico
} else {
$arr_numbers = range(0, 32);
}
$this->arr_fonts[$str_font_key] = array(
$this->arr_fonts[$fontkey] = array(
'i' => $int_font_count,
'type' => $str_type,
'name' => $str_name,
'type' => $type,
'name' => $name,
'desc' => $arr_descriptors,
'up' => $flt_underline_pos,
'ut' => $flt_underline_thickness,
'cw' => $arr_character_widths,
'ttffile' => $str_ttf_filename,
'fontkey' => $str_font_key,
'fontkey' => $fontkey,
'subset' => $arr_numbers,
'unifilename' => $str_unicode_filename
);
$this->arr_font_files[$str_font_key] = array('length1' => $int_original_size, 'type' => self::FONT_TRUETYPE, 'ttffile' => $str_ttf_filename);
$this->arr_font_files[$fontkey] = array('length1' => $originalsize, 'type' => self::FONT_TRUETYPE, 'ttffile' => $str_ttf_filename);
$this->arr_font_files[$str_file] = array('type' => self::FONT_TRUETYPE);
unset($arr_character_widths);
} else {
Expand All @@ -1117,7 +1119,7 @@ public function AddFont($str_family, $str_style = '', $str_file = '', $bol_unico
);
}
}
$this->arr_fonts[$str_font_key] = $arr_info;
$this->arr_fonts[$fontkey] = $arr_info;
}
}

Expand Down Expand Up @@ -2554,8 +2556,8 @@ public function PutFonts()
$this->Out('/BaseFont /' . $str_font_name . '');
$this->Out('/CIDSystemInfo ' . ($this->int_current_object + 2) . ' 0 R');
$this->Out('/FontDescriptor ' . ($this->int_current_object + 3) . ' 0 R');
if (isset($arr_font_data['desc']->MissingWidth)) {
$this->Out('/DW ' . $arr_font_data['desc']->MissingWidth . '');
if (isset($arr_font_data['desc']['MissingWidth'])) {
$this->Out('/DW ' . $arr_font_data['desc']['MissingWidth'] . '');
}

$this->PutTTFontWidths($arr_font_data, $obj_ttf->getMaxUni());
Expand Down

0 comments on commit 0587857

Please sign in to comment.