Skip to content

Commit 8fadad9

Browse files
committed
Fix a few validation issues and add type hinting comments
1 parent 1b4a4af commit 8fadad9

8 files changed

+164
-126
lines changed

classes/adobe_font_metrics.cls.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ function __construct(Font_TrueType $font) {
2626
}
2727

2828
function write($file, $encoding = null){
29+
$map_data = array();
30+
2931
if ($encoding) {
3032
$encoding = preg_replace("/[^a-z0-9-_]/", "", $encoding);
3133
$map_file = dirname(__FILE__)."/../maps/$encoding.map";

classes/font.cls.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Font {
1616

1717
/**
1818
* @param string $file The font file
19-
* @return Font_TrueType $file
19+
* @return Font_TrueType|null $file
2020
*/
2121
public static function load($file) {
2222
$header = file_get_contents($file, false, null, null, 4);
@@ -48,12 +48,15 @@ public static function load($file) {
4848

4949
if ($class) {
5050
require_once dirname(__FILE__)."/".strtolower($class).".cls.php";
51-
51+
52+
/** @var Font_TrueType $obj */
5253
$obj = new $class;
5354
$obj->load($file);
5455

5556
return $obj;
5657
}
58+
59+
return null;
5760
}
5861

5962
static function d($str) {

classes/font_binary_stream.cls.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,21 @@ static function backtrace(){
5959
* Open a font file in read mode
6060
*
6161
* @param string $filename The file name of the font to open
62+
*
63+
* @return bool
6264
*/
6365
public function load($filename) {
6466
return $this->open($filename, self::modeRead);
6567
}
66-
68+
6769
/**
6870
* Open a font file in a chosen mode
69-
*
71+
*
7072
* @param string $filename The file name of the font to open
71-
* @param string $mode The opening mode
73+
* @param string $mode The opening mode
74+
*
75+
* @throws Exception
76+
* @return bool
7277
*/
7378
public function open($filename, $mode = self::modeRead) {
7479
if (!in_array($mode, array(self::modeRead, self::modeWrite, self::modeReadWrite))) {
@@ -85,11 +90,13 @@ public function open($filename, $mode = self::modeRead) {
8590
public function close() {
8691
return fclose($this->f) != false;
8792
}
88-
93+
8994
/**
9095
* Change the internal file pointer
91-
*
96+
*
9297
* @param resource $fp
98+
*
99+
* @throws Exception
93100
*/
94101
public function setFile($fp) {
95102
if (!is_resource($fp)) {
@@ -98,10 +105,12 @@ public function setFile($fp) {
98105

99106
$this->f = $fp;
100107
}
101-
108+
102109
/**
103110
* Create a temporary file in write mode
104-
*
111+
*
112+
* @param bool $allow_memory Allow in-memory files
113+
*
105114
* @return resource the temporary file pointer resource
106115
*/
107116
public static function getTempFile($allow_memory = true) {
@@ -123,6 +132,7 @@ public static function getTempFile($allow_memory = true) {
123132
* Move the internal file pinter to $offset bytes
124133
*
125134
* @param int $offset
135+
*
126136
* @return bool True if the $offset position exists in the file
127137
*/
128138
public function seek($offset) {
@@ -143,12 +153,18 @@ public function skip($n) {
143153
}
144154

145155
public function read($n) {
146-
if ($n < 1) return "";
156+
if ($n < 1) {
157+
return "";
158+
}
159+
147160
return fread($this->f, $n);
148161
}
149-
162+
150163
public function write($data, $length = null) {
151-
if ($data === null || $data === "") return;
164+
if ($data === null || $data === "") {
165+
return 0;
166+
}
167+
152168
return fwrite($this->f, $data, $length);
153169
}
154170

@@ -276,6 +292,7 @@ public function pack($def, $data) {
276292
* Read a data of type $type in the file from the current position
277293
*
278294
* @param mixed $type The data type to read
295+
*
279296
* @return mixed The data that was read
280297
*/
281298
public function r($type) {
@@ -305,6 +322,8 @@ public function r($type) {
305322
}
306323
return $ret;
307324
}
325+
326+
return null;
308327
}
309328
}
310329

@@ -313,6 +332,7 @@ public function r($type) {
313332
*
314333
* @param mixed $type The data type to write
315334
* @param mixed $data The data to write
335+
*
316336
* @return int The number of bytes read
317337
*/
318338
public function w($type, $data) {
@@ -342,13 +362,16 @@ public function w($type, $data) {
342362
}
343363
return $ret;
344364
}
365+
366+
return null;
345367
}
346368
}
347369

348370
/**
349371
* Converts a Uint32 value to string
350372
*
351373
* @param int $uint32
374+
*
352375
* @return string The string
353376
*/
354377
public function convertUInt32ToStr($uint32) {

classes/font_glyph_outline.cls.php

Lines changed: 109 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,110 @@
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-
* @version $Id: font_table_glyf.cls.php 46 2012-04-02 20:22:38Z fabien.menager $
8-
*/
9-
10-
require_once dirname(__FILE__)."/font_glyph_outline_simple.cls.php";
11-
require_once dirname(__FILE__)."/font_glyph_outline_composite.cls.php";
12-
13-
/**
14-
* `glyf` font table.
15-
*
16-
* @package php-font-lib
17-
*/
18-
class Font_Glyph_Outline extends Font_Binary_Stream {
19-
const ARG_1_AND_2_ARE_WORDS = 0x0001;
20-
const ARGS_ARE_XY_VALUES = 0x0002;
21-
const ROUND_XY_TO_GRID = 0x0004;
22-
const WE_HAVE_A_SCALE = 0x0008;
23-
const MORE_COMPONENTS = 0x0020;
24-
const WE_HAVE_AN_X_AND_Y_SCALE = 0x0040;
25-
const WE_HAVE_A_TWO_BY_TWO = 0x0080;
26-
const WE_HAVE_INSTRUCTIONS = 0x0100;
27-
const USE_MY_METRICS = 0x0200;
28-
const OVERLAP_COMPOUND = 0x0400;
29-
30-
/**
31-
* @var Font_Table_glyf
32-
*/
33-
protected $table;
34-
35-
protected $offset;
36-
protected $size;
37-
38-
// Data
39-
public $numberOfContours;
40-
public $xMin;
41-
public $yMin;
42-
public $xMax;
43-
public $yMax;
44-
45-
public $raw;
46-
47-
/**
48-
* @return Font_Glyph_Outline
49-
*/
50-
static function init(Font_Table_glyf $table, $offset, $size) {
51-
$font = $table->getFont();
52-
$font->seek($offset);
53-
54-
/**
55-
* @var Font_Glyph_Outline
56-
*/
57-
$glyph;
58-
59-
if ($font->readInt16() > -1) {
60-
$glyph = new Font_Glyph_Outline_Simple($table, $offset, $size);
61-
}
62-
else {
63-
$glyph = new Font_Glyph_Outline_Composite($table, $offset, $size);
64-
}
65-
66-
$glyph->parse();
67-
return $glyph;
68-
}
69-
70-
/**
71-
* @return Font_TrueType
72-
*/
73-
function getFont() {
74-
return $this->table->getFont();
75-
}
76-
77-
function __construct(Font_Table_glyf $table, $offset = null, $size = null) {
78-
$this->table = $table;
79-
$this->offset = $offset;
80-
$this->size = $size;
81-
}
82-
83-
function parse() {
84-
$font = $this->getFont();
85-
$font->seek($this->offset);
86-
87-
if (!$this->size) {
88-
return;
89-
}
90-
91-
$this->raw = $font->read($this->size);
92-
}
93-
94-
function parseData(){
95-
$font = $this->getFont();
96-
$font->seek($this->offset);
97-
98-
$this->numberOfContours = $font->readInt16();
99-
$this->xMin = $font->readFWord();
100-
$this->yMin = $font->readFWord();
101-
$this->xMax = $font->readFWord();
102-
$this->yMax = $font->readFWord();
103-
}
104-
105-
function encode(){
106-
$font = $this->getFont();
107-
return $font->write($this->raw, strlen($this->raw));
108-
}
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+
* @version $Id: font_table_glyf.cls.php 46 2012-04-02 20:22:38Z fabien.menager $
8+
*/
9+
10+
require_once dirname(__FILE__)."/font_glyph_outline_simple.cls.php";
11+
require_once dirname(__FILE__)."/font_glyph_outline_composite.cls.php";
12+
13+
/**
14+
* `glyf` font table.
15+
*
16+
* @package php-font-lib
17+
*/
18+
class Font_Glyph_Outline extends Font_Binary_Stream {
19+
const ARG_1_AND_2_ARE_WORDS = 0x0001;
20+
const ARGS_ARE_XY_VALUES = 0x0002;
21+
const ROUND_XY_TO_GRID = 0x0004;
22+
const WE_HAVE_A_SCALE = 0x0008;
23+
const MORE_COMPONENTS = 0x0020;
24+
const WE_HAVE_AN_X_AND_Y_SCALE = 0x0040;
25+
const WE_HAVE_A_TWO_BY_TWO = 0x0080;
26+
const WE_HAVE_INSTRUCTIONS = 0x0100;
27+
const USE_MY_METRICS = 0x0200;
28+
const OVERLAP_COMPOUND = 0x0400;
29+
30+
/**
31+
* @var Font_Table_glyf
32+
*/
33+
protected $table;
34+
35+
protected $offset;
36+
protected $size;
37+
38+
// Data
39+
public $numberOfContours;
40+
public $xMin;
41+
public $yMin;
42+
public $xMax;
43+
public $yMax;
44+
45+
public $raw;
46+
47+
/**
48+
* @param Font_Table_glyf $table
49+
* @param $offset
50+
* @param $size
51+
*
52+
* @return Font_Glyph_Outline
53+
*/
54+
static function init(Font_Table_glyf $table, $offset, $size) {
55+
$font = $table->getFont();
56+
$font->seek($offset);
57+
58+
if ($font->readInt16() > -1) {
59+
/** @var Font_Glyph_Outline_Simple $glyph */
60+
$glyph = new Font_Glyph_Outline_Simple($table, $offset, $size);
61+
}
62+
else {
63+
/** @var Font_Glyph_Outline_Composite $glyph */
64+
$glyph = new Font_Glyph_Outline_Composite($table, $offset, $size);
65+
}
66+
67+
$glyph->parse();
68+
return $glyph;
69+
}
70+
71+
/**
72+
* @return Font_TrueType
73+
*/
74+
function getFont() {
75+
return $this->table->getFont();
76+
}
77+
78+
function __construct(Font_Table_glyf $table, $offset = null, $size = null) {
79+
$this->table = $table;
80+
$this->offset = $offset;
81+
$this->size = $size;
82+
}
83+
84+
function parse() {
85+
$font = $this->getFont();
86+
$font->seek($this->offset);
87+
88+
if (!$this->size) {
89+
return;
90+
}
91+
92+
$this->raw = $font->read($this->size);
93+
}
94+
95+
function parseData(){
96+
$font = $this->getFont();
97+
$font->seek($this->offset);
98+
99+
$this->numberOfContours = $font->readInt16();
100+
$this->xMin = $font->readFWord();
101+
$this->yMin = $font->readFWord();
102+
$this->xMax = $font->readFWord();
103+
$this->yMax = $font->readFWord();
104+
}
105+
106+
function encode(){
107+
$font = $this->getFont();
108+
return $font->write($this->raw, strlen($this->raw));
109+
}
109110
}

classes/font_table_name.cls.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ protected function _parse(){
5151

5252
protected function _encode(){
5353
$font = $this->getFont();
54-
54+
55+
/** @var Font_Table_name_Record[] $records */
5556
$records = $this->data["records"];
5657
$count_records = count($records);
5758

0 commit comments

Comments
 (0)