1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace Com\Tecnick\Pdf\Font;
17:
18: use \Com\Tecnick\File\Byte;
19: use \Com\Tecnick\File\Dir;
20: use \Com\Tecnick\File\File;
21: use \Com\Tecnick\Unicode\Data\Encoding;
22: use \Com\Tecnick\Pdf\Font\Import\TypeOne;
23: use \Com\Tecnick\Pdf\Font\Import\TrueType;
24: use \Com\Tecnick\Pdf\Font\UniToCid;
25: use \Com\Tecnick\Pdf\Font\Exception as FontException;
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
38: abstract class ImportUtil
39: {
40: 41: 42: 43: 44:
45: protected $font = '';
46:
47: 48: 49: 50: 51:
52: protected $fbyte;
53:
54: 55: 56: 57: 58:
59: protected $fdt = array();
60:
61: 62: 63: 64: 65: 66: 67:
68: protected function makeFontName($font_file)
69: {
70: $font_path_parts = pathinfo($font_file);
71: return str_replace(
72: array('bold', 'oblique', 'italic', 'regular'),
73: array('b', 'i', 'i', ''),
74: preg_replace('/[^a-z0-9_]/', '', strtolower($font_path_parts['filename']))
75: );
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85:
86: protected function findOutputPath($output_path = null)
87: {
88: if (!empty($output_path) && is_writable($output_path)) {
89: return $output_path;
90: }
91: if (defined('K_PATH_FONTS') && is_writable(K_PATH_FONTS)) {
92: return K_PATH_FONTS;
93: }
94: $dirobj = new Dir();
95: $dir = $dirobj->findParentDir('fonts', __DIR__);
96: if ($dir == '/') {
97: $dir = sys_get_temp_dir();
98: }
99: if (substr($dir, -1) !== '/') {
100: $dir .= '/';
101: }
102: return $dir;
103: }
104:
105: 106: 107: 108: 109: 110: 111:
112: protected function getFontType($font_type)
113: {
114:
115: if (empty($font_type)) {
116: if (substr($this->font, 0, 16) == 'StartFontMetrics') {
117:
118: return 'Core';
119: }
120: if (substr($this->font, 0, 4) == 'OTTO') {
121: throw new FontException('Unsupported font format: OpenType with CFF data');
122: }
123: if ($this->fbyte->getULong(0) == 0x10000) {
124: return 'TrueTypeUnicode';
125: }
126: return 'Type1';
127: }
128: if (strpos($font_type, 'CID0') === 0) {
129: return 'cidfont0';
130: }
131: if (in_array($font_type, array('Core', 'Type1', 'TrueType', 'TrueTypeUnicode'))) {
132: return $font_type;
133: }
134: throw new FontException('unknown or unsupported font type: '.$font_type);
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: protected function getEncodingTable($encoding)
145: {
146: if (empty($encoding) && ($this->fdt['type'] == 'Type1') && (($this->fdt['Flags'] & 4) == 0)) {
147: return 'cp1252';
148: }
149: return preg_replace('/[^A-Za-z0-9_\-]/', '', $encoding);
150: }
151:
152: 153: 154: 155: 156:
157: protected function getEncodingDiff()
158: {
159: $diff = '';
160: if ((($this->fdt['type'] == 'TrueType') || ($this->fdt['type'] == 'Type1'))
161: && (!empty($this->fdt['enc'])
162: && ($this->fdt['enc'] != 'cp1252')
163: && isset(Encoding::$map[$this->fdt['enc']]))
164: ) {
165:
166: $enc_ref = Encoding::$map['cp1252'];
167: $enc_target = Encoding::$map[$this->fdt['enc']];
168: $last = 0;
169: for ($idx = 32; $idx <= 255; ++$idx) {
170: if ($enc_target[$idx] != $enc_ref[$idx]) {
171: if ($idx != ($last + 1)) {
172: $diff .= $idx.' ';
173: }
174: $last = $idx;
175: $diff .= '/'.$enc_target[$idx].' ';
176: }
177: }
178: }
179: return $diff;
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189: 190:
191: protected function updateCIDtoGIDmap($map, $cid, $gid)
192: {
193: if (($cid >= 0) && ($cid <= 0xFFFF) && ($gid >= 0)) {
194: if ($gid > 0xFFFF) {
195: $gid -= 0x10000;
196: }
197: $map[($cid * 2)] = chr($gid >> 8);
198: $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
199: }
200: return $map;
201: }
202: }
203: