File: Font_Table_Directory_Entry.php

package info (click to toggle)
php-font-lib 0.2.2%2Bdfsg-2~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 740 kB
  • sloc: php: 2,542; makefile: 4
file content (124 lines) | stat: -rw-r--r-- 2,481 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
 * @package php-font-lib
 * @link    https://github.com/PhenX/php-font-lib
 * @author  Fabien Ménager <fabien.menager@gmail.com>
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 */

/**
 * Generic Font table directory entry.
 * 
 * @package php-font-lib
 */
class Font_Table_Directory_Entry extends Font_Binary_Stream {
  /**
   * @var Font_TrueType
   */
  protected $font;
  
  /**
   * @var Font_Table
   */
  protected $font_table;
  
  public $entryLength = 4;
  
  public $tag;
  public $checksum;
  public $offset;
  public $length;
  
  protected $origF;
  
  static function computeChecksum($data){
    $len = strlen($data);
    $mod = $len % 4;
    
    if ($mod) { 
      $data = str_pad($data, $len + (4 - $mod), "\0");
    }
    
    $len = strlen($data);
    
    $hi = 0x0000;
    $lo = 0x0000;
    
    for ($i = 0; $i < $len; $i += 4) {
      $hi += (ord($data[$i]  ) << 8) + ord($data[$i+1]);
      $lo += (ord($data[$i+2]) << 8) + ord($data[$i+3]);
      $hi += $lo >> 16;
      $lo = $lo & 0xFFFF;
      $hi = $hi & 0xFFFF;
    }
    
    return ($hi << 8) + $lo;
  }
  
  function __construct(Font_TrueType $font) {
    $this->font = $font;
    $this->f = $font->f;
  }
  
  function parse() {
    $this->tag = $this->font->read(4);
  }
  
  function open($filename, $mode = self::modeRead) {
    // void
  }
  
  function setTable(Font_Table $font_table) {
    $this->font_table = $font_table;
  }
  
  function encode($entry_offset){
    Font::d("\n==== $this->tag ====");
    //Font::d("Entry offset  = $entry_offset");
    
    $data = $this->font_table;
    $font = $this->font;
    
    $table_offset = $font->pos();
    $this->offset = $table_offset;
    $table_length = $data->encode();
    
    $font->seek($table_offset);
    $table_data = $font->read($table_length);
    
    $font->seek($entry_offset);
    
    $font->write($this->tag, 4);
    $font->writeUInt32(self::computeChecksum($table_data));
    $font->writeUInt32($table_offset);
    $font->writeUInt32($table_length);
    
    Font::d("Bytes written = $table_length");
    
    $font->seek($table_offset + $table_length);
  }
  
  /**
   * @return Font_TrueType
   */
  function getFont() {
    return $this->font;
  }
  
  function startRead() {
    $this->font->seek($this->offset);
  }
  
  function endRead() {
    //
  }
  
  function startWrite() {
    $this->font->seek($this->offset);
  }
  
  function endWrite() {
    //
  }
}