Skip to content

Commit cb3556d

Browse files
committed
Composite of composite glyphs were not included in subsetted fonts
1 parent 7c01f0f commit cb3556d

File tree

5 files changed

+100
-96
lines changed

5 files changed

+100
-96
lines changed

classes/Font_Glyph_Outline.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@
1212

1313
/**
1414
* `glyf` font table.
15-
*
15+
*
1616
* @package php-font-lib
1717
*/
1818
class Font_Glyph_Outline extends Font_Binary_Stream {
1919
/**
2020
* @var Font_Table_glyf
2121
*/
2222
protected $table;
23-
23+
2424
protected $offset;
2525
protected $size;
26-
26+
2727
// Data
2828
public $numberOfContours;
2929
public $xMin;
3030
public $yMin;
3131
public $xMax;
3232
public $yMax;
33-
33+
3434
public $raw;
3535

3636
/**
@@ -43,7 +43,7 @@ class Font_Glyph_Outline extends Font_Binary_Stream {
4343
static function init(Font_Table_glyf $table, $offset, $size) {
4444
$font = $table->getFont();
4545
$font->seek($offset);
46-
46+
4747
if ($font->readInt16() > -1) {
4848
/** @var Font_Glyph_Outline_Simple $glyph */
4949
$glyph = new Font_Glyph_Outline_Simple($table, $offset, $size);
@@ -52,35 +52,35 @@ static function init(Font_Table_glyf $table, $offset, $size) {
5252
/** @var Font_Glyph_Outline_Composite $glyph */
5353
$glyph = new Font_Glyph_Outline_Composite($table, $offset, $size);
5454
}
55-
55+
5656
$glyph->parse();
5757
return $glyph;
5858
}
59-
59+
6060
/**
6161
* @return Font_TrueType
6262
*/
6363
function getFont() {
6464
return $this->table->getFont();
6565
}
66-
66+
6767
function __construct(Font_Table_glyf $table, $offset = null, $size = null) {
6868
$this->table = $table;
6969
$this->offset = $offset;
7070
$this->size = $size;
7171
}
72-
72+
7373
function parse() {
7474
$font = $this->getFont();
7575
$font->seek($this->offset);
76-
76+
7777
if (!$this->size) {
7878
return;
7979
}
80-
80+
8181
$this->raw = $font->read($this->size);
8282
}
83-
83+
8484
function parseData(){
8585
$font = $this->getFont();
8686
$font->seek($this->offset);
@@ -102,6 +102,6 @@ function getSVGContours() {
102102
}
103103

104104
function getGlyphIDs(){
105-
// Inherit
105+
return array();
106106
}
107107
}

classes/Font_Glyph_Outline_Composite.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
/**
1313
* Composite glyph outline
14-
*
14+
*
1515
* @package php-font-lib
1616
*/
1717
class Font_Glyph_Outline_Composite extends Font_Glyph_Outline {
@@ -39,6 +39,11 @@ function getGlyphIDs(){
3939
$glyphIDs = array();
4040
foreach ($this->components as $_component) {
4141
$glyphIDs[] = $_component->glyphIndex;
42+
43+
$_glyph = $this->table->data[$_component->glyphIndex];
44+
if ($_glyph instanceof Font_Glyph_Outline_Composite) {
45+
$glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
46+
}
4247
}
4348

4449
return $glyphIDs;
@@ -47,7 +52,7 @@ function getGlyphIDs(){
4752
/*function parse() {
4853
//$this->parseData();
4954
}*/
50-
55+
5156
function parseData(){
5257
parent::parseData();
5358

classes/Font_Table_glyf.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,68 @@
1010

1111
/**
1212
* `glyf` font table.
13-
*
13+
*
1414
* @package php-font-lib
1515
* @property Font_Glyph_Outline[] $data
1616
*/
1717
class Font_Table_glyf extends Font_Table {
1818
protected function _parse(){
1919
$font = $this->getFont();
2020
$offset = $font->pos();
21-
21+
2222
$loca = $font->getData("loca");
2323
$real_loca = array_slice($loca, 0, -1); // Not the last dummy loca entry
24-
24+
2525
$data = array();
26-
26+
2727
foreach($real_loca as $gid => $location) {
2828
$_offset = $offset + $loca[$gid];
2929
$_size = $loca[$gid+1] - $loca[$gid];
3030
$data[$gid] = Font_Glyph_Outline::init($this, $_offset, $_size);
3131
}
32-
32+
3333
$this->data = $data;
3434
}
3535

3636
public function getGlyphIDs($gids = array()){
3737
$glyphIDs = array();
3838

39-
foreach ($this->data as $_gid => $_glyph) {
40-
if (in_array($_gid, $gids) && $_glyph instanceof Font_Glyph_Outline_Composite) {
41-
$glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
42-
}
39+
foreach ($gids as $_gid) {
40+
$_glyph = $this->data[$_gid];
41+
$glyphIDs = array_merge($glyphIDs, $_glyph->getGlyphIDs());
4342
}
4443

4544
return array_unique(array_merge($gids, $glyphIDs));
4645
}
47-
46+
4847
public function toHTML(){
4948
$max = 160;
5049
$font = $this->getFont();
51-
50+
5251
$head = $font->getData("head");
5352
$head_json = json_encode($head);
54-
53+
5554
$os2 = $font->getData("OS/2");
5655
$os2_json = json_encode($os2);
57-
56+
5857
$hmtx = $font->getData("hmtx");
5958
$hmtx_json = json_encode($hmtx);
60-
59+
6160
$names = $font->getData("post", "names");
6261
$glyphIndexArray = array_flip($font->getUnicodeCharMap());
63-
62+
6463
$width = (abs($head["xMin"]) + $head["xMax"]);
6564
$height = (abs($head["yMin"]) + $head["yMax"]);
66-
65+
6766
$ratio = 1;
6867
if ($width > $max || $height > $max) {
6968
$ratio = max($width, $height) / $max;
7069
$width = round($width/$ratio);
7170
$height = round($height/$ratio);
7271
}
73-
72+
7473
$n = 500;
75-
74+
7675
$s = "<h3>"."Only the first $n simple glyphs are shown (".count($this->data)." total)
7776
<div class='glyph-view simple'>Simple glyph</div>
7877
<div class='glyph-view composite'>Composite glyph</div>
@@ -86,14 +85,14 @@ public function toHTML(){
8685
Glyph.width = $width;
8786
Glyph.height = $height;
8887
</script>";
89-
88+
9089
foreach($this->data as $g => $glyph) {
9190
if ($n-- <= 0) {
9291
break;
9392
}
9493

9594
$glyph->parseData();
96-
95+
9796
$shape = array(
9897
"SVGContours" => $glyph->getSVGContours(),
9998
"xMin" => $glyph->xMin,
@@ -107,7 +106,7 @@ public function toHTML(){
107106
$char = isset($glyphIndexArray[$g]) ? $glyphIndexArray[$g] : 0;
108107
$name = isset($names[$g]) ? $names[$g] : sprintf("uni%04x", $char);
109108
$char = $char ? "&#{$glyphIndexArray[$g]};" : "";
110-
109+
111110
$s .= "<div class='glyph-view $type' id='glyph-$g'>
112111
<span class='glyph-id'>$g</span>
113112
<span class='char'>$char</span>
@@ -125,27 +124,27 @@ public function toHTML(){
125124
</div>
126125
<script>Glyph.glyphs.push([$g,$shape_json]);</script>";
127126
}
128-
127+
129128
return $s;
130129
}
131-
132-
130+
131+
133132
protected function _encode() {
134133
$font = $this->getFont();
135134
$subset = $font->getSubset();
136135
$data = $this->data;
137-
136+
138137
$loca = array();
139-
138+
140139
$length = 0;
141140
foreach($subset as $gid) {
142141
$loca[] = $length;
143142
$length += $data[$gid]->encode();
144143
}
145-
144+
146145
$loca[] = $length; // dummy loca
147146
$font->getTableObject("loca")->data = $loca;
148-
147+
149148
return $length;
150149
}
151150
}

0 commit comments

Comments
 (0)