Skip to content

Commit e2e68fc

Browse files
committed
Add readme.html
Improve composite glyph rendering (with the transformation) Rename all class files WOFF files could not be read anymore Better parsing of EOT headers
1 parent 472d903 commit e2e68fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+268
-167
lines changed

classes/adobe_font_metrics.cls.php renamed to classes/Adobe_Font_Metrics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
77
*/
88

9-
require_once dirname(__FILE__)."/encoding_map.cls.php";
9+
require_once dirname(__FILE__) . "/Encoding_Map.php";
1010

1111
/**
1212
* Adobe Font Metrics file creation utility class.
File renamed without changes.

classes/Font.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* @package php-font-lib
4+
* @link https://github.com/PhenX/php-font-lib
5+
* @author Fabien Ménager <[email protected]>
6+
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7+
*/
8+
9+
/**
10+
* Generic font file.
11+
*
12+
* @package php-font-lib
13+
*/
14+
class Font {
15+
static $debug = false;
16+
17+
/**
18+
* @param string $file The font file
19+
* @return Font_TrueType|null $file
20+
*/
21+
public static function load($file) {
22+
$header = file_get_contents($file, false, null, null, 4);
23+
$class = null;
24+
25+
switch($header) {
26+
case "\x00\x01\x00\x00":
27+
case "true":
28+
case "typ1":
29+
$class = "Font_TrueType"; break;
30+
31+
case "OTTO":
32+
$class = "Font_OpenType"; break;
33+
34+
case "wOFF":
35+
$class = "Font_WOFF"; break;
36+
37+
case "ttcf":
38+
$class = "Font_TrueType_Collection"; break;
39+
40+
// Unknown type or EOT
41+
default:
42+
$magicNumber = file_get_contents($file, false, null, 34, 2);
43+
44+
if ($magicNumber === "LP") {
45+
$class = "Font_EOT";
46+
}
47+
}
48+
49+
if ($class) {
50+
/** @noinspection PhpIncludeInspection */
51+
require_once dirname(__FILE__)."/$class.php";
52+
53+
/** @var Font_TrueType $obj */
54+
$obj = new $class;
55+
$obj->load($file);
56+
57+
return $obj;
58+
}
59+
60+
return null;
61+
}
62+
63+
static function d($str) {
64+
if (!self::$debug) return;
65+
echo "$str\n";
66+
}
67+
68+
static function UTF16ToUTF8($str) {
69+
return mb_convert_encoding($str, "utf-8", "utf-16");
70+
}
71+
72+
static function UTF8ToUTF16($str) {
73+
return mb_convert_encoding($str, "utf-16", "utf-8");
74+
}
75+
}

classes/font_binary_stream.cls.php renamed to classes/Font_Binary_Stream.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ class Font_Binary_Stream {
3131
const longDateTime = 12;
3232
const char = 13;
3333

34-
private static $sizes = array(
35-
self::uint8 => 1,
36-
self::int8 => 1,
37-
self::uint16 => 2,
38-
self::int16 => 2,
39-
self::uint32 => 4,
40-
self::int32 => 4,
41-
self::shortFrac => 4,
42-
self::Fixed => 4,
43-
self::FWord => 2,
44-
self::uFWord => 2,
45-
self::F2Dot14 => 2,
46-
self::longDateTime => 8,
47-
self::char => 1,
48-
);
49-
5034
const modeRead = "rb";
5135
const modeWrite = "wb";
5236
const modeReadWrite = "rb+";

classes/font_eot.cls.php renamed to classes/Font_EOT.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,25 @@
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
77
*/
88

9-
require_once dirname(__FILE__)."/font_truetype.cls.php";
10-
require_once dirname(__FILE__)."/font_eot_header.cls.php";
9+
require_once dirname(__FILE__) . "/Font_TrueType.php";
10+
require_once dirname(__FILE__) . "/Font_EOT_Header.php";
1111

1212
/**
1313
* EOT font file.
1414
*
1515
* @package php-font-lib
1616
*/
1717
class Font_EOT extends Font_TrueType {
18-
private $origF;
19-
private $fileOffset = 0;
20-
2118
/**
2219
* @var Font_EOT_Header
2320
*/
2421
public $header;
25-
22+
2623
function parseHeader(){
2724
if (!empty($this->header)) {
2825
return;
2926
}
3027

31-
$this->seek(0);
32-
3328
$this->header = new Font_EOT_Header($this);
3429
$this->header->parse();
3530
}

classes/font_eot_header.cls.php renamed to classes/Font_EOT_Header.php

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
77
*/
88

9-
require_once dirname(__FILE__)."/font_header.cls.php";
9+
require_once dirname(__FILE__) . "/Font_Header.php";
1010

1111
/**
1212
* TrueType font file header.
1313
*
1414
* @package php-font-lib
15+
*
16+
* @property Font_EOT $font
1517
*/
1618
class Font_EOT_Header extends Font_Header {
19+
const TTEMBED_SUBSET = 0x00000001;
20+
const TTEMBED_TTCOMPRESSED = 0x00000004;
21+
const TTEMBED_FAILIFVARIATIONSIMULATED = 0x00000010;
22+
const TTMBED_EMBEDEUDC = 0x00000020;
23+
const TTEMBED_VALIDATIONTESTS = 0x00000040; // Deprecated
24+
const TTEMBED_WEBOBJECT = 0x00000080;
25+
const TTEMBED_XORENCRYPTDATA = 0x10000000;
26+
1727
protected $def = array(
1828
"format" => self::uint32,
1929
"numTables" => self::uint16,
2030
"searchRange" => self::uint16,
2131
"entrySelector" => self::uint16,
2232
"rangeShift" => self::uint16,
2333
);
24-
34+
2535
public function parse(){
2636
$font = $this->font;
2737

@@ -30,11 +40,7 @@ public function parse(){
3040
"FontDataSize" => self::uint32,
3141
"Version" => self::uint32,
3242
"Flags" => self::uint32,
33-
));
34-
35-
$this->data["FontPANOSE"] = $font->read(10);
36-
37-
$this->data += $font->unpack(array(
43+
"FontPANOSE" => array(self::uint8, 10),
3844
"Charset" => self::uint8,
3945
"Italic" => self::uint8,
4046
"Weight" => self::uint32,
@@ -51,16 +57,66 @@ public function parse(){
5157
"Reserved2" => self::uint32,
5258
"Reserved3" => self::uint32,
5359
"Reserved4" => self::uint32,
54-
"Padding1" => self::uint16,
5560
));
5661

62+
$this->data["Padding1"] = $font->readUInt16();
5763
$this->readString("FamilyName");
64+
5865
$this->data["Padding2"] = $font->readUInt16();
5966
$this->readString("StyleName");
67+
6068
$this->data["Padding3"] = $font->readUInt16();
6169
$this->readString("VersionName");
70+
6271
$this->data["Padding4"] = $font->readUInt16();
6372
$this->readString("FullName");
73+
74+
switch ($this->data["Version"]) {
75+
default:
76+
throw new Exception("Unknown EOT version ".$this->data["Version"]);
77+
78+
case 0x00010000:
79+
// Nothing to do more
80+
break;
81+
82+
case 0x00020001:
83+
$this->data["Padding5"] = $font->readUInt16();
84+
$this->readString("RootString");
85+
break;
86+
87+
case 0x00020002:
88+
$this->data["Padding5"] = $font->readUInt16();
89+
$this->readString("RootString");
90+
91+
$this->data["RootStringCheckSum"] = $font->readUInt32();
92+
$this->data["EUDCCodePage"] = $font->readUInt32();
93+
94+
$this->data["Padding6"] = $font->readUInt16();
95+
$this->readString("Signature");
96+
97+
$this->data["EUDCFlags"] = $font->readUInt32();
98+
$this->data["EUDCFontSize"] = $font->readUInt32();
99+
break;
100+
}
101+
102+
if (!empty($this->data["RootString"])) {
103+
$this->data["RootString"] = explode("\0", $this->data["RootString"]);
104+
}
105+
106+
$flags = $this->data["Flags"];
107+
108+
if ($flags & self::TTEMBED_TTCOMPRESSED) {
109+
$mtx_version = $font->readUInt8();
110+
$mtx_copy_limit = $font->readUInt8() << 16 | $font->readUInt8() << 8 | $font->readUInt8();
111+
$mtx_offset_1 = $font->readUInt8() << 16 | $font->readUInt8() << 8 | $font->readUInt8();
112+
$mtx_offset_2 = $font->readUInt8() << 16 | $font->readUInt8() << 8 | $font->readUInt8();
113+
}
114+
115+
if ($flags & self::TTEMBED_XORENCRYPTDATA) {
116+
// Process XOR
117+
}
118+
119+
// TODO Read font data ...
64120
}
65121

66122
private function readString($name) {

classes/font_glyph_outline.cls.php renamed to classes/Font_Glyph_Outline.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* @link https://github.com/PhenX/php-font-lib
55
* @author Fabien Ménager <[email protected]>
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7-
* @version $Id: font_table_glyf.cls.php 46 2012-04-02 20:22:38Z fabien.menager $
7+
* @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
88
*/
99

10-
require_once dirname(__FILE__)."/font_glyph_outline_simple.cls.php";
11-
require_once dirname(__FILE__)."/font_glyph_outline_composite.cls.php";
10+
require_once dirname(__FILE__) . "/Font_Glyph_Outline_Simple.php";
11+
require_once dirname(__FILE__) . "/Font_Glyph_Outline_Composite.php";
1212

1313
/**
1414
* `glyf` font table.
@@ -96,4 +96,8 @@ function encode(){
9696
$font = $this->getFont();
9797
return $font->write($this->raw, strlen($this->raw));
9898
}
99+
100+
function getSVGContours() {
101+
// Inherit
102+
}
99103
}

classes/font_glyph_outline_component.cls.php renamed to classes/Font_Glyph_Outline_Component.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @link https://github.com/PhenX/php-font-lib
55
* @author Fabien Ménager <[email protected]>
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7-
* @version $Id: font_table_glyf.cls.php 46 2012-04-02 20:22:38Z fabien.menager $
7+
* @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
88
*/
99

1010
/**
@@ -19,4 +19,12 @@ class Font_Glyph_Outline_Component {
1919
public $point_compound;
2020
public $point_component;
2121
public $instructions;
22+
23+
function getMatrix(){
24+
return array(
25+
$this->a, $this->b,
26+
$this->c, $this->d,
27+
$this->e, $this->f,
28+
);
29+
}
2230
}

classes/font_glyph_outline_composite.cls.php renamed to classes/Font_Glyph_Outline_Composite.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* @link https://github.com/PhenX/php-font-lib
55
* @author Fabien Ménager <[email protected]>
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7-
* @version $Id: font_table_glyf.cls.php 46 2012-04-02 20:22:38Z fabien.menager $
7+
* @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
88
*/
99

10-
require_once dirname(__FILE__)."/font_glyph_outline_component.cls.php";
10+
require_once dirname(__FILE__) . "/Font_Glyph_Outline_Component.php";
1111

1212
/**
1313
* Composite glyph outline
@@ -210,17 +210,21 @@ function encode(){
210210
}
211211

212212
public function getSVGContours(){
213-
$path = "";
213+
$contours = array();
214214

215215
/** @var Font_Table_glyf $glyph_data */
216216
$glyph_data = $this->getFont()->getTableObject("glyf");
217217

218+
/** @var Font_Glyph_Outline[] $glyphs */
218219
$glyphs = $glyph_data->data;
219220

220221
foreach ($this->components as $component) {
221-
$path .= $glyphs[$component->glyphIndex]->getSVGContours();
222+
$contours[] = array(
223+
"contours" => $glyphs[$component->glyphIndex]->getSVGContours(),
224+
"transform" => $component->getMatrix(),
225+
);
222226
}
223227

224-
return $path;
228+
return $contours;
225229
}
226230
}

classes/font_glyph_outline_simple.cls.php renamed to classes/Font_Glyph_Outline_Simple.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @link https://github.com/PhenX/php-font-lib
55
* @author Fabien Ménager <[email protected]>
66
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7-
* @version $Id: font_table_glyf.cls.php 46 2012-04-02 20:22:38Z fabien.menager $
7+
* @version $Id: Font_Table_glyf.php 46 2012-04-02 20:22:38Z fabien.menager $
88
*/
99

1010
/**

0 commit comments

Comments
 (0)