You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2654 lines
94 KiB

4 years ago
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf_fonts.php
  4. // Version : 1.1.0
  5. // Begin : 2008-01-01
  6. // Last Update : 2014-12-10
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2008-2014 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // Description :Font methods for TCPDF library.
  31. //
  32. //============================================================+
  33. /**
  34. * @file
  35. * Unicode data and font methods for TCPDF library.
  36. * @author Nicola Asuni
  37. * @package com.tecnick.tcpdf
  38. */
  39. /**
  40. * @class TCPDF_FONTS
  41. * Font methods for TCPDF library.
  42. * @package com.tecnick.tcpdf
  43. * @version 1.1.0
  44. * @author Nicola Asuni - info@tecnick.com
  45. */
  46. class TCPDF_FONTS {
  47. /**
  48. * Static cache used for speed up uniord performances
  49. * @protected
  50. */
  51. protected static $cache_uniord = array();
  52. /**
  53. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  54. * @param $fontfile (string) Font file (full path).
  55. * @param $fonttype (string) Font type. Leave empty for autodetect mode. Valid values are: TrueTypeUnicode, TrueType, Type1, CID0JP = CID-0 Japanese, CID0KR = CID-0 Korean, CID0CS = CID-0 Chinese Simplified, CID0CT = CID-0 Chinese Traditional.
  56. * @param $enc (string) Name of the encoding table to use. Leave empty for default mode. Omit this parameter for TrueType Unicode and symbolic fonts like Symbol or ZapfDingBats.
  57. * @param $flags (int) Unsigned 32-bit integer containing flags specifying various characteristics of the font (PDF32000:2008 - 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for symbol or +32 for non-symbol; +64 for italic. Fixed and Italic mode are generally autodetected so you have to set it to 32 = non-symbolic font (default) or 4 = symbolic font.
  58. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  59. * @param $platid (int) Platform ID for CMAP table to extract (when building a Unicode font for Windows this value should be 3, for Macintosh should be 1).
  60. * @param $encid (int) Encoding ID for CMAP table to extract (when building a Unicode font for Windows this value should be 1, for Macintosh should be 0). When Platform ID is 3, legal values for Encoding ID are: 0=Symbol, 1=Unicode, 2=ShiftJIS, 3=PRC, 4=Big5, 5=Wansung, 6=Johab, 7=Reserved, 8=Reserved, 9=Reserved, 10=UCS-4.
  61. * @param $addcbbox (boolean) If true includes the character bounding box information on the php font file.
  62. * @param $link (boolean) If true link to system font instead of copying the font data (not transportable) - Note: do not work with Type1 fonts.
  63. * @return (string) TCPDF font name or boolean false in case of error.
  64. * @author Nicola Asuni
  65. * @since 5.9.123 (2010-09-30)
  66. * @public static
  67. */
  68. public static function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='', $platid=3, $encid=1, $addcbbox=false, $link=false) {
  69. if (!TCPDF_STATIC::file_exists($fontfile)) {
  70. // Could not find file
  71. return false;
  72. }
  73. // font metrics
  74. $fmetric = array();
  75. // build new font name for TCPDF compatibility
  76. $font_path_parts = pathinfo($fontfile);
  77. if (!isset($font_path_parts['filename'])) {
  78. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  79. }
  80. $font_name = strtolower($font_path_parts['filename']);
  81. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  82. $search = array('bold', 'oblique', 'italic', 'regular');
  83. $replace = array('b', 'i', 'i', '');
  84. $font_name = str_replace($search, $replace, $font_name);
  85. if (empty($font_name)) {
  86. // set generic name
  87. $font_name = 'tcpdffont';
  88. }
  89. // set output path
  90. if (empty($outpath)) {
  91. $outpath = self::_getfontpath();
  92. }
  93. // check if this font already exist
  94. if (@TCPDF_STATIC::file_exists($outpath.$font_name.'.php')) {
  95. // this font already exist (delete it from fonts folder to rebuild it)
  96. return $font_name;
  97. }
  98. $fmetric['file'] = $font_name;
  99. $fmetric['ctg'] = $font_name.'.ctg.z';
  100. // get font data
  101. $font = file_get_contents($fontfile);
  102. $fmetric['originalsize'] = strlen($font);
  103. // autodetect font type
  104. if (empty($fonttype)) {
  105. if (TCPDF_STATIC::_getULONG($font, 0) == 0x10000) {
  106. // True Type (Unicode or not)
  107. $fonttype = 'TrueTypeUnicode';
  108. } elseif (substr($font, 0, 4) == 'OTTO') {
  109. // Open Type (Unicode or not)
  110. //Unsupported font format: OpenType with CFF data
  111. return false;
  112. } else {
  113. // Type 1
  114. $fonttype = 'Type1';
  115. }
  116. }
  117. // set font type
  118. switch ($fonttype) {
  119. case 'CID0CT':
  120. case 'CID0CS':
  121. case 'CID0KR':
  122. case 'CID0JP': {
  123. $fmetric['type'] = 'cidfont0';
  124. break;
  125. }
  126. case 'Type1': {
  127. $fmetric['type'] = 'Type1';
  128. if (empty($enc) AND (($flags & 4) == 0)) {
  129. $enc = 'cp1252';
  130. }
  131. break;
  132. }
  133. case 'TrueType': {
  134. $fmetric['type'] = 'TrueType';
  135. break;
  136. }
  137. case 'TrueTypeUnicode':
  138. default: {
  139. $fmetric['type'] = 'TrueTypeUnicode';
  140. break;
  141. }
  142. }
  143. // set encoding maps (if any)
  144. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  145. $fmetric['diff'] = '';
  146. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  147. if (!empty($enc) AND ($enc != 'cp1252') AND isset(TCPDF_FONT_DATA::$encmap[$enc])) {
  148. // build differences from reference encoding
  149. $enc_ref = TCPDF_FONT_DATA::$encmap['cp1252'];
  150. $enc_target = TCPDF_FONT_DATA::$encmap[$enc];
  151. $last = 0;
  152. for ($i = 32; $i <= 255; ++$i) {
  153. if ($enc_target[$i] != $enc_ref[$i]) {
  154. if ($i != ($last + 1)) {
  155. $fmetric['diff'] .= $i.' ';
  156. }
  157. $last = $i;
  158. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  159. }
  160. }
  161. }
  162. }
  163. // parse the font by type
  164. if ($fmetric['type'] == 'Type1') {
  165. // ---------- TYPE 1 ----------
  166. // read first segment
  167. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  168. if ($a['marker'] != 128) {
  169. // Font file is not a valid binary Type1
  170. return false;
  171. }
  172. $fmetric['size1'] = $a['size'];
  173. $data = substr($font, 6, $fmetric['size1']);
  174. // read second segment
  175. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  176. if ($a['marker'] != 128) {
  177. // Font file is not a valid binary Type1
  178. return false;
  179. }
  180. $fmetric['size2'] = $a['size'];
  181. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  182. $data .= $encrypted;
  183. // store compressed font
  184. $fmetric['file'] .= '.z';
  185. $fp = TCPDF_STATIC::fopenLocal($outpath.$fmetric['file'], 'wb');
  186. fwrite($fp, gzcompress($data));
  187. fclose($fp);
  188. // get font info
  189. $fmetric['Flags'] = $flags;
  190. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  191. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  192. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  193. $fmetric['bbox'] = trim($matches[1]);
  194. $bv = explode(' ', $fmetric['bbox']);
  195. $fmetric['Ascent'] = intval($bv[3]);
  196. $fmetric['Descent'] = intval($bv[1]);
  197. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  198. $fmetric['italicAngle'] = intval($matches[1]);
  199. if ($fmetric['italicAngle'] != 0) {
  200. $fmetric['Flags'] |= 64;
  201. }
  202. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  203. $fmetric['underlinePosition'] = intval($matches[1]);
  204. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  205. $fmetric['underlineThickness'] = intval($matches[1]);
  206. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  207. if ($matches[1] == 'true') {
  208. $fmetric['Flags'] |= 1;
  209. }
  210. // get internal map
  211. $imap = array();
  212. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  213. foreach ($fmap as $v) {
  214. $imap[$v[2]] = $v[1];
  215. }
  216. }
  217. // decrypt eexec encrypted part
  218. $r = 55665; // eexec encryption constant
  219. $c1 = 52845;
  220. $c2 = 22719;
  221. $elen = strlen($encrypted);
  222. $eplain = '';
  223. for ($i = 0; $i < $elen; ++$i) {
  224. $chr = ord($encrypted[$i]);
  225. $eplain .= chr($chr ^ ($r >> 8));
  226. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  227. }
  228. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  229. if ($matches[1] == 'true') {
  230. $fmetric['Flags'] |= 0x40000;
  231. }
  232. }
  233. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  234. $fmetric['StemV'] = intval($matches[1]);
  235. } else {
  236. $fmetric['StemV'] = 70;
  237. }
  238. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  239. $fmetric['StemH'] = intval($matches[1]);
  240. } else {
  241. $fmetric['StemH'] = 30;
  242. }
  243. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  244. $bv = explode(' ', $matches[1]);
  245. if (count($bv) >= 6) {
  246. $v1 = intval($bv[2]);
  247. $v2 = intval($bv[4]);
  248. if ($v1 <= $v2) {
  249. $fmetric['XHeight'] = $v1;
  250. $fmetric['CapHeight'] = $v2;
  251. } else {
  252. $fmetric['XHeight'] = $v2;
  253. $fmetric['CapHeight'] = $v1;
  254. }
  255. } else {
  256. $fmetric['XHeight'] = 450;
  257. $fmetric['CapHeight'] = 700;
  258. }
  259. } else {
  260. $fmetric['XHeight'] = 450;
  261. $fmetric['CapHeight'] = 700;
  262. }
  263. // get the number of random bytes at the beginning of charstrings
  264. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  265. $lenIV = intval($matches[1]);
  266. } else {
  267. $lenIV = 4;
  268. }
  269. $fmetric['Leading'] = 0;
  270. // get charstring data
  271. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  272. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  273. if (!empty($enc) AND isset(TCPDF_FONT_DATA::$encmap[$enc])) {
  274. $enc_map = TCPDF_FONT_DATA::$encmap[$enc];
  275. } else {
  276. $enc_map = false;
  277. }
  278. $fmetric['cw'] = '';
  279. $fmetric['MaxWidth'] = 0;
  280. $cwidths = array();
  281. foreach ($matches as $k => $v) {
  282. $cid = 0;
  283. if (isset($imap[$v[1]])) {
  284. $cid = $imap[$v[1]];
  285. } elseif ($enc_map !== false) {
  286. $cid = array_search($v[1], $enc_map);
  287. if ($cid === false) {
  288. $cid = 0;
  289. } elseif ($cid > 1000) {
  290. $cid -= 1000;
  291. }
  292. }
  293. // decrypt charstring encrypted part
  294. $r = 4330; // charstring encryption constant
  295. $c1 = 52845;
  296. $c2 = 22719;
  297. $cd = $v[2];
  298. $clen = strlen($cd);
  299. $ccom = array();
  300. for ($i = 0; $i < $clen; ++$i) {
  301. $chr = ord($cd[$i]);
  302. $ccom[] = ($chr ^ ($r >> 8));
  303. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  304. }
  305. // decode numbers
  306. $cdec = array();
  307. $ck = 0;
  308. $i = $lenIV;
  309. while ($i < $clen) {
  310. if ($ccom[$i] < 32) {
  311. $cdec[$ck] = $ccom[$i];
  312. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  313. // hsbw command: update width
  314. $cwidths[$cid] = $cdec[($ck - 1)];
  315. }
  316. ++$i;
  317. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  318. $cdec[$ck] = ($ccom[$i] - 139);
  319. ++$i;
  320. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  321. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  322. $i += 2;
  323. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  324. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  325. $i += 2;
  326. } elseif ($ccom[$i] == 255) {
  327. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  328. $vsval = unpack('li', $sval);
  329. $cdec[$ck] = $vsval['i'];
  330. $i += 5;
  331. }
  332. ++$ck;
  333. }
  334. } // end for each matches
  335. $fmetric['MissingWidth'] = $cwidths[0];
  336. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  337. $fmetric['AvgWidth'] = 0;
  338. // set chars widths
  339. for ($cid = 0; $cid <= 255; ++$cid) {
  340. if (isset($cwidths[$cid])) {
  341. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  342. $fmetric['MaxWidth'] = $cwidths[$cid];
  343. }
  344. $fmetric['AvgWidth'] += $cwidths[$cid];
  345. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  346. } else {
  347. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  348. }
  349. }
  350. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  351. } else {
  352. // ---------- TRUE TYPE ----------
  353. $offset = 0; // offset position of the font data
  354. if (TCPDF_STATIC::_getULONG($font, $offset) != 0x10000) {
  355. // sfnt version must be 0x00010000 for TrueType version 1.0.
  356. return false;
  357. }
  358. if ($fmetric['type'] != 'cidfont0') {
  359. if ($link) {
  360. // creates a symbolic link to the existing font
  361. symlink($fontfile, $outpath.$fmetric['file']);
  362. } else {
  363. // store compressed font
  364. $fmetric['file'] .= '.z';
  365. $fp = TCPDF_STATIC::fopenLocal($outpath.$fmetric['file'], 'wb');
  366. fwrite($fp, gzcompress($font));
  367. fclose($fp);
  368. }
  369. }
  370. $offset += 4;
  371. // get number of tables
  372. $numTables = TCPDF_STATIC::_getUSHORT($font, $offset);
  373. $offset += 2;
  374. // skip searchRange, entrySelector and rangeShift
  375. $offset += 6;
  376. // tables array
  377. $table = array();
  378. // ---------- get tables ----------
  379. for ($i = 0; $i < $numTables; ++$i) {
  380. // get table info
  381. $tag = substr($font, $offset, 4);
  382. $offset += 4;
  383. $table[$tag] = array();
  384. $table[$tag]['checkSum'] = TCPDF_STATIC::_getULONG($font, $offset);
  385. $offset += 4;
  386. $table[$tag]['offset'] = TCPDF_STATIC::_getULONG($font, $offset);
  387. $offset += 4;
  388. $table[$tag]['length'] = TCPDF_STATIC::_getULONG($font, $offset);
  389. $offset += 4;
  390. }
  391. // check magicNumber
  392. $offset = $table['head']['offset'] + 12;
  393. if (TCPDF_STATIC::_getULONG($font, $offset) != 0x5F0F3CF5) {
  394. // magicNumber must be 0x5F0F3CF5
  395. return false;
  396. }
  397. $offset += 4;
  398. $offset += 2; // skip flags
  399. // get FUnits
  400. $fmetric['unitsPerEm'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  401. $offset += 2;
  402. // units ratio constant
  403. $urk = (1000 / $fmetric['unitsPerEm']);
  404. $offset += 16; // skip created, modified
  405. $xMin = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  406. $offset += 2;
  407. $yMin = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  408. $offset += 2;
  409. $xMax = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  410. $offset += 2;
  411. $yMax = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  412. $offset += 2;
  413. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  414. $macStyle = TCPDF_STATIC::_getUSHORT($font, $offset);
  415. $offset += 2;
  416. // PDF font flags
  417. $fmetric['Flags'] = $flags;
  418. if (($macStyle & 2) == 2) {
  419. // italic flag
  420. $fmetric['Flags'] |= 64;
  421. }
  422. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  423. $offset = $table['head']['offset'] + 50;
  424. $short_offset = (TCPDF_STATIC::_getSHORT($font, $offset) == 0);
  425. $offset += 2;
  426. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  427. $indexToLoc = array();
  428. $offset = $table['loca']['offset'];
  429. if ($short_offset) {
  430. // short version
  431. $tot_num_glyphs = floor($table['loca']['length'] / 2); // numGlyphs + 1
  432. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  433. $indexToLoc[$i] = TCPDF_STATIC::_getUSHORT($font, $offset) * 2;
  434. if (isset($indexToLoc[($i - 1)]) && ($indexToLoc[$i] == $indexToLoc[($i - 1)])) {
  435. // the last glyph didn't have an outline
  436. unset($indexToLoc[($i - 1)]);
  437. }
  438. $offset += 2;
  439. }
  440. } else {
  441. // long version
  442. $tot_num_glyphs = floor($table['loca']['length'] / 4); // numGlyphs + 1
  443. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  444. $indexToLoc[$i] = TCPDF_STATIC::_getULONG($font, $offset);
  445. if (isset($indexToLoc[($i - 1)]) && ($indexToLoc[$i] == $indexToLoc[($i - 1)])) {
  446. // the last glyph didn't have an outline
  447. unset($indexToLoc[($i - 1)]);
  448. }
  449. $offset += 4;
  450. }
  451. }
  452. // get glyphs indexes of chars from cmap table
  453. $offset = $table['cmap']['offset'] + 2;
  454. $numEncodingTables = TCPDF_STATIC::_getUSHORT($font, $offset);
  455. $offset += 2;
  456. $encodingTables = array();
  457. for ($i = 0; $i < $numEncodingTables; ++$i) {
  458. $encodingTables[$i]['platformID'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  459. $offset += 2;
  460. $encodingTables[$i]['encodingID'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  461. $offset += 2;
  462. $encodingTables[$i]['offset'] = TCPDF_STATIC::_getULONG($font, $offset);
  463. $offset += 4;
  464. }
  465. // ---------- get os/2 metrics ----------
  466. $offset = $table['OS/2']['offset'];
  467. $offset += 2; // skip version
  468. // xAvgCharWidth
  469. $fmetric['AvgWidth'] = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  470. $offset += 2;
  471. // usWeightClass
  472. $usWeightClass = round(TCPDF_STATIC::_getUFWORD($font, $offset) * $urk);
  473. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  474. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  475. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  476. $offset += 2;
  477. $offset += 2; // usWidthClass
  478. $fsType = TCPDF_STATIC::_getSHORT($font, $offset);
  479. $offset += 2;
  480. if ($fsType == 2) {
  481. // This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.
  482. return false;
  483. }
  484. // ---------- get font name ----------
  485. $fmetric['name'] = '';
  486. $offset = $table['name']['offset'];
  487. $offset += 2; // skip Format selector (=0).
  488. // Number of NameRecords that follow n.
  489. $numNameRecords = TCPDF_STATIC::_getUSHORT($font, $offset);
  490. $offset += 2;
  491. // Offset to start of string storage (from start of table).
  492. $stringStorageOffset = TCPDF_STATIC::_getUSHORT($font, $offset);
  493. $offset += 2;
  494. for ($i = 0; $i < $numNameRecords; ++$i) {
  495. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  496. // Name ID.
  497. $nameID = TCPDF_STATIC::_getUSHORT($font, $offset);
  498. $offset += 2;
  499. if ($nameID == 6) {
  500. // String length (in bytes).
  501. $stringLength = TCPDF_STATIC::_getUSHORT($font, $offset);
  502. $offset += 2;
  503. // String offset from start of storage area (in bytes).
  504. $stringOffset = TCPDF_STATIC::_getUSHORT($font, $offset);
  505. $offset += 2;
  506. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  507. $fmetric['name'] = substr($font, $offset, $stringLength);
  508. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  509. break;
  510. } else {
  511. $offset += 4; // skip String length, String offset
  512. }
  513. }
  514. if (empty($fmetric['name'])) {
  515. $fmetric['name'] = $font_name;
  516. }
  517. // ---------- get post data ----------
  518. $offset = $table['post']['offset'];
  519. $offset += 4; // skip Format Type
  520. $fmetric['italicAngle'] = TCPDF_STATIC::_getFIXED($font, $offset);
  521. $offset += 4;
  522. $fmetric['underlinePosition'] = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  523. $offset += 2;
  524. $fmetric['underlineThickness'] = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  525. $offset += 2;
  526. $isFixedPitch = (TCPDF_STATIC::_getULONG($font, $offset) == 0) ? false : true;
  527. $offset += 2;
  528. if ($isFixedPitch) {
  529. $fmetric['Flags'] |= 1;
  530. }
  531. // ---------- get hhea data ----------
  532. $offset = $table['hhea']['offset'];
  533. $offset += 4; // skip Table version number
  534. // Ascender
  535. $fmetric['Ascent'] = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  536. $offset += 2;
  537. // Descender
  538. $fmetric['Descent'] = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  539. $offset += 2;
  540. // LineGap
  541. $fmetric['Leading'] = round(TCPDF_STATIC::_getFWORD($font, $offset) * $urk);
  542. $offset += 2;
  543. // advanceWidthMax
  544. $fmetric['MaxWidth'] = round(TCPDF_STATIC::_getUFWORD($font, $offset) * $urk);
  545. $offset += 2;
  546. $offset += 22; // skip some values
  547. // get the number of hMetric entries in hmtx table
  548. $numberOfHMetrics = TCPDF_STATIC::_getUSHORT($font, $offset);
  549. // ---------- get maxp data ----------
  550. $offset = $table['maxp']['offset'];
  551. $offset += 4; // skip Table version number
  552. // get the the number of glyphs in the font.
  553. $numGlyphs = TCPDF_STATIC::_getUSHORT($font, $offset);
  554. // ---------- get CIDToGIDMap ----------
  555. $ctg = array();
  556. foreach ($encodingTables as $enctable) {
  557. // get only specified Platform ID and Encoding ID
  558. if (($enctable['platformID'] == $platid) AND ($enctable['encodingID'] == $encid)) {
  559. $offset = $table['cmap']['offset'] + $enctable['offset'];
  560. $format = TCPDF_STATIC::_getUSHORT($font, $offset);
  561. $offset += 2;
  562. switch ($format) {
  563. case 0: { // Format 0: Byte encoding table
  564. $offset += 4; // skip length and version/language
  565. for ($c = 0; $c < 256; ++$c) {
  566. $g = TCPDF_STATIC::_getBYTE($font, $offset);
  567. $ctg[$c] = $g;
  568. ++$offset;
  569. }
  570. break;
  571. }
  572. case 2: { // Format 2: High-byte mapping through table
  573. $offset += 4; // skip length and version/language
  574. $numSubHeaders = 0;
  575. for ($i = 0; $i < 256; ++$i) {
  576. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  577. $subHeaderKeys[$i] = (TCPDF_STATIC::_getUSHORT($font, $offset) / 8);
  578. $offset += 2;
  579. if ($numSubHeaders < $subHeaderKeys[$i]) {
  580. $numSubHeaders = $subHeaderKeys[$i];
  581. }
  582. }
  583. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  584. ++$numSubHeaders;
  585. // read subHeader structures
  586. $subHeaders = array();
  587. $numGlyphIndexArray = 0;
  588. for ($k = 0; $k < $numSubHeaders; ++$k) {
  589. $subHeaders[$k]['firstCode'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  590. $offset += 2;
  591. $subHeaders[$k]['entryCount'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  592. $offset += 2;
  593. $subHeaders[$k]['idDelta'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  594. $offset += 2;
  595. $subHeaders[$k]['idRangeOffset'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  596. $offset += 2;
  597. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  598. $subHeaders[$k]['idRangeOffset'] /= 2;
  599. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  600. }
  601. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  602. $glyphIndexArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  603. $offset += 2;
  604. }
  605. for ($i = 0; $i < 256; ++$i) {
  606. $k = $subHeaderKeys[$i];
  607. if ($k == 0) {
  608. // one byte code
  609. $c = $i;
  610. $g = $glyphIndexArray[0];
  611. $ctg[$c] = $g;
  612. } else {
  613. // two bytes code
  614. $start_byte = $subHeaders[$k]['firstCode'];
  615. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  616. for ($j = $start_byte; $j < $end_byte; ++$j) {
  617. // combine high and low bytes
  618. $c = (($i << 8) + $j);
  619. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  620. $g = ($glyphIndexArray[$idRangeOffset] + $subHeaders[$k]['idDelta']) % 65536;
  621. if ($g < 0) {
  622. $g = 0;
  623. }
  624. $ctg[$c] = $g;
  625. }
  626. }
  627. }
  628. break;
  629. }
  630. case 4: { // Format 4: Segment mapping to delta values
  631. $length = TCPDF_STATIC::_getUSHORT($font, $offset);
  632. $offset += 2;
  633. $offset += 2; // skip version/language
  634. $segCount = floor(TCPDF_STATIC::_getUSHORT($font, $offset) / 2);
  635. $offset += 2;
  636. $offset += 6; // skip searchRange, entrySelector, rangeShift
  637. $endCount = array(); // array of end character codes for each segment
  638. for ($k = 0; $k < $segCount; ++$k) {
  639. $endCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  640. $offset += 2;
  641. }
  642. $offset += 2; // skip reservedPad
  643. $startCount = array(); // array of start character codes for each segment
  644. for ($k = 0; $k < $segCount; ++$k) {
  645. $startCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  646. $offset += 2;
  647. }
  648. $idDelta = array(); // delta for all character codes in segment
  649. for ($k = 0; $k < $segCount; ++$k) {
  650. $idDelta[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  651. $offset += 2;
  652. }
  653. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  654. for ($k = 0; $k < $segCount; ++$k) {
  655. $idRangeOffset[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  656. $offset += 2;
  657. }
  658. $gidlen = (floor($length / 2) - 8 - (4 * $segCount));
  659. $glyphIdArray = array(); // glyph index array
  660. for ($k = 0; $k < $gidlen; ++$k) {
  661. $glyphIdArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  662. $offset += 2;
  663. }
  664. for ($k = 0; $k < $segCount - 1; ++$k) {
  665. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  666. if ($idRangeOffset[$k] == 0) {
  667. $g = ($idDelta[$k] + $c) % 65536;
  668. } else {
  669. $gid = (floor($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  670. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  671. }
  672. if ($g < 0) {
  673. $g = 0;
  674. }
  675. $ctg[$c] = $g;
  676. }
  677. }
  678. break;
  679. }
  680. case 6: { // Format 6: Trimmed table mapping
  681. $offset += 4; // skip length and version/language
  682. $firstCode = TCPDF_STATIC::_getUSHORT($font, $offset);
  683. $offset += 2;
  684. $entryCount = TCPDF_STATIC::_getUSHORT($font, $offset);
  685. $offset += 2;
  686. for ($k = 0; $k < $entryCount; ++$k) {
  687. $c = ($k + $firstCode);
  688. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  689. $offset += 2;
  690. $ctg[$c] = $g;
  691. }
  692. break;
  693. }
  694. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  695. $offset += 10; // skip reserved, length and version/language
  696. for ($k = 0; $k < 8192; ++$k) {
  697. $is32[$k] = TCPDF_STATIC::_getBYTE($font, $offset);
  698. ++$offset;
  699. }
  700. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  701. $offset += 4;
  702. for ($i = 0; $i < $nGroups; ++$i) {
  703. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  704. $offset += 4;
  705. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  706. $offset += 4;
  707. $startGlyphID = TCPDF_STATIC::_getULONG($font, $offset);
  708. $offset += 4;
  709. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  710. $is32idx = floor($c / 8);
  711. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  712. $c = $k;
  713. } else {
  714. // 32 bit format
  715. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  716. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  717. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  718. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  719. }
  720. $ctg[$c] = 0;
  721. ++$startGlyphID;
  722. }
  723. }
  724. break;
  725. }
  726. case 10: { // Format 10: Trimmed array
  727. $offset += 10; // skip reserved, length and version/language
  728. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  729. $offset += 4;
  730. $numChars = TCPDF_STATIC::_getULONG($font, $offset);
  731. $offset += 4;
  732. for ($k = 0; $k < $numChars; ++$k) {
  733. $c = ($k + $startCharCode);
  734. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  735. $ctg[$c] = $g;
  736. $offset += 2;
  737. }
  738. break;
  739. }
  740. case 12: { // Format 12: Segmented coverage
  741. $offset += 10; // skip length and version/language
  742. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  743. $offset += 4;
  744. for ($k = 0; $k < $nGroups; ++$k) {
  745. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  746. $offset += 4;
  747. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  748. $offset += 4;
  749. $startGlyphCode = TCPDF_STATIC::_getULONG($font, $offset);
  750. $offset += 4;
  751. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  752. $ctg[$c] = $startGlyphCode;
  753. ++$startGlyphCode;
  754. }
  755. }
  756. break;
  757. }
  758. case 13: { // Format 13: Many-to-one range mappings
  759. // to be implemented ...
  760. break;
  761. }
  762. case 14: { // Format 14: Unicode Variation Sequences
  763. // to be implemented ...
  764. break;
  765. }
  766. }
  767. }
  768. }
  769. if (!isset($ctg[0])) {
  770. $ctg[0] = 0;
  771. }
  772. // get xHeight (height of x)
  773. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  774. $yMin = TCPDF_STATIC::_getFWORD($font, $offset);
  775. $offset += 4;
  776. $yMax = TCPDF_STATIC::_getFWORD($font, $offset);
  777. $offset += 2;
  778. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  779. // get CapHeight (height of H)
  780. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  781. $yMin = TCPDF_STATIC::_getFWORD($font, $offset);
  782. $offset += 4;
  783. $yMax = TCPDF_STATIC::_getFWORD($font, $offset);
  784. $offset += 2;
  785. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  786. // ceate widths array
  787. $cw = array();
  788. $offset = $table['hmtx']['offset'];
  789. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  790. $cw[$i] = round(TCPDF_STATIC::_getUFWORD($font, $offset) * $urk);
  791. $offset += 4; // skip lsb
  792. }
  793. if ($numberOfHMetrics < $numGlyphs) {
  794. // fill missing widths with the last value
  795. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  796. }
  797. $fmetric['MissingWidth'] = $cw[0];
  798. $fmetric['cw'] = '';
  799. $fmetric['cbbox'] = '';
  800. for ($cid = 0; $cid <= 65535; ++$cid) {
  801. if (isset($ctg[$cid])) {
  802. if (isset($cw[$ctg[$cid]])) {
  803. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  804. }
  805. if ($addcbbox AND isset($indexToLoc[$ctg[$cid]])) {
  806. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[$cid]]);
  807. $xMin = round(TCPDF_STATIC::_getFWORD($font, $offset + 2) * $urk);
  808. $yMin = round(TCPDF_STATIC::_getFWORD($font, $offset + 4) * $urk);
  809. $xMax = round(TCPDF_STATIC::_getFWORD($font, $offset + 6) * $urk);
  810. $yMax = round(TCPDF_STATIC::_getFWORD($font, $offset + 8) * $urk);
  811. $fmetric['cbbox'] .= ','.$cid.'=>array('.$xMin.','.$yMin.','.$xMax.','.$yMax.')';
  812. }
  813. }
  814. }
  815. } // end of true type
  816. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  817. $fmetric['type'] = 'TrueType';
  818. }
  819. // ---------- create php font file ----------
  820. $pfile = '<'.'?'.'php'."\n";
  821. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  822. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  823. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  824. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  825. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  826. if ($fmetric['MissingWidth'] > 0) {
  827. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  828. } else {
  829. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  830. }
  831. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  832. if ($fmetric['type'] == 'Type1') {
  833. // Type 1
  834. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  835. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  836. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  837. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  838. } else {
  839. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  840. if ($fmetric['type'] == 'cidfont0') {
  841. // CID-0
  842. switch ($fonttype) {
  843. case 'CID0JP': {
  844. $pfile .= '// Japanese'."\n";
  845. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  846. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  847. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  848. break;
  849. }
  850. case 'CID0KR': {
  851. $pfile .= '// Korean'."\n";
  852. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  853. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  854. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  855. break;
  856. }
  857. case 'CID0CS': {
  858. $pfile .= '// Chinese Simplified'."\n";
  859. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  860. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  861. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  862. break;
  863. }
  864. case 'CID0CT':
  865. default: {
  866. $pfile .= '// Chinese Traditional'."\n";
  867. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  868. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  869. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  870. break;
  871. }
  872. }
  873. } else {
  874. // TrueType
  875. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  876. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  877. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  878. // create CIDToGIDMap
  879. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  880. foreach ($ctg as $cid => $gid) {
  881. $cidtogidmap = self::updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  882. }
  883. // store compressed CIDToGIDMap
  884. $fp = TCPDF_STATIC::fopenLocal($outpath.$fmetric['ctg'], 'wb');
  885. fwrite($fp, gzcompress($cidtogidmap));
  886. fclose($fp);
  887. }
  888. }
  889. $pfile .= '$desc=array(';
  890. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  891. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  892. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  893. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  894. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  895. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  896. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  897. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  898. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  899. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  900. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  901. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  902. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  903. $pfile .= ');'."\n";
  904. if (!empty($fmetric['cbbox'])) {
  905. $pfile .= '$cbbox=array('.substr($fmetric['cbbox'], 1).');'."\n";
  906. }
  907. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  908. $pfile .= '// --- EOF ---'."\n";
  909. // store file
  910. $fp = TCPDF_STATIC::fopenLocal($outpath.$font_name.'.php', 'w');
  911. fwrite($fp, $pfile);
  912. fclose($fp);
  913. // return TCPDF font name
  914. return $font_name;
  915. }
  916. /**
  917. * Returs the checksum of a TTF table.
  918. * @param $table (string) table to check
  919. * @param $length (int) length of table in bytes
  920. * @return int checksum
  921. * @author Nicola Asuni
  922. * @since 5.2.000 (2010-06-02)
  923. * @public static
  924. */
  925. public static function _getTTFtableChecksum($table, $length) {
  926. $sum = 0;
  927. $tlen = ($length / 4);
  928. $offset = 0;
  929. for ($i = 0; $i < $tlen; ++$i) {
  930. $v = unpack('Ni', substr($table, $offset, 4));
  931. $sum += $v['i'];
  932. $offset += 4;
  933. }
  934. $sum = unpack('Ni', pack('N', $sum));
  935. return $sum['i'];
  936. }
  937. /**
  938. * Returns a subset of the TrueType font data without the unused glyphs.
  939. * @param $font (string) TrueType font data.
  940. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  941. * @return (string) A subset of TrueType font data without the unused glyphs.
  942. * @author Nicola Asuni
  943. * @since 5.2.000 (2010-06-02)
  944. * @public static
  945. */
  946. public static function _getTrueTypeFontSubset($font, $subsetchars) {
  947. ksort($subsetchars);
  948. $offset = 0; // offset position of the font data
  949. if (TCPDF_STATIC::_getULONG($font, $offset) != 0x10000) {
  950. // sfnt version must be 0x00010000 for TrueType version 1.0.
  951. return $font;
  952. }
  953. $offset += 4;
  954. // get number of tables
  955. $numTables = TCPDF_STATIC::_getUSHORT($font, $offset);
  956. $offset += 2;
  957. // skip searchRange, entrySelector and rangeShift
  958. $offset += 6;
  959. // tables array
  960. $table = array();
  961. // for each table
  962. for ($i = 0; $i < $numTables; ++$i) {
  963. // get table info
  964. $tag = substr($font, $offset, 4);
  965. $offset += 4;
  966. $table[$tag] = array();
  967. $table[$tag]['checkSum'] = TCPDF_STATIC::_getULONG($font, $offset);
  968. $offset += 4;
  969. $table[$tag]['offset'] = TCPDF_STATIC::_getULONG($font, $offset);
  970. $offset += 4;
  971. $table[$tag]['length'] = TCPDF_STATIC::_getULONG($font, $offset);
  972. $offset += 4;
  973. }
  974. // check magicNumber
  975. $offset = $table['head']['offset'] + 12;
  976. if (TCPDF_STATIC::_getULONG($font, $offset) != 0x5F0F3CF5) {
  977. // magicNumber must be 0x5F0F3CF5
  978. return $font;
  979. }
  980. $offset += 4;
  981. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  982. $offset = $table['head']['offset'] + 50;
  983. $short_offset = (TCPDF_STATIC::_getSHORT($font, $offset) == 0);
  984. $offset += 2;
  985. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  986. $indexToLoc = array();
  987. $offset = $table['loca']['offset'];
  988. if ($short_offset) {
  989. // short version
  990. $tot_num_glyphs = floor($table['loca']['length'] / 2); // numGlyphs + 1
  991. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  992. $indexToLoc[$i] = TCPDF_STATIC::_getUSHORT($font, $offset) * 2;
  993. $offset += 2;
  994. }
  995. } else {
  996. // long version
  997. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  998. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  999. $indexToLoc[$i] = TCPDF_STATIC::_getULONG($font, $offset);
  1000. $offset += 4;
  1001. }
  1002. }
  1003. // get glyphs indexes of chars from cmap table
  1004. $subsetglyphs = array(); // glyph IDs on key
  1005. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  1006. $offset = $table['cmap']['offset'] + 2;
  1007. $numEncodingTables = TCPDF_STATIC::_getUSHORT($font, $offset);
  1008. $offset += 2;
  1009. $encodingTables = array();
  1010. for ($i = 0; $i < $numEncodingTables; ++$i) {
  1011. $encodingTables[$i]['platformID'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1012. $offset += 2;
  1013. $encodingTables[$i]['encodingID'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1014. $offset += 2;
  1015. $encodingTables[$i]['offset'] = TCPDF_STATIC::_getULONG($font, $offset);
  1016. $offset += 4;
  1017. }
  1018. foreach ($encodingTables as $enctable) {
  1019. // get all platforms and encodings
  1020. $offset = $table['cmap']['offset'] + $enctable['offset'];
  1021. $format = TCPDF_STATIC::_getUSHORT($font, $offset);
  1022. $offset += 2;
  1023. switch ($format) {
  1024. case 0: { // Format 0: Byte encoding table
  1025. $offset += 4; // skip length and version/language
  1026. for ($c = 0; $c < 256; ++$c) {
  1027. if (isset($subsetchars[$c])) {
  1028. $g = TCPDF_STATIC::_getBYTE($font, $offset);
  1029. $subsetglyphs[$g] = true;
  1030. }
  1031. ++$offset;
  1032. }
  1033. break;
  1034. }
  1035. case 2: { // Format 2: High-byte mapping through table
  1036. $offset += 4; // skip length and version/language
  1037. $numSubHeaders = 0;
  1038. for ($i = 0; $i < 256; ++$i) {
  1039. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  1040. $subHeaderKeys[$i] = (TCPDF_STATIC::_getUSHORT($font, $offset) / 8);
  1041. $offset += 2;
  1042. if ($numSubHeaders < $subHeaderKeys[$i]) {
  1043. $numSubHeaders = $subHeaderKeys[$i];
  1044. }
  1045. }
  1046. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  1047. ++$numSubHeaders;
  1048. // read subHeader structures
  1049. $subHeaders = array();
  1050. $numGlyphIndexArray = 0;
  1051. for ($k = 0; $k < $numSubHeaders; ++$k) {
  1052. $subHeaders[$k]['firstCode'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1053. $offset += 2;
  1054. $subHeaders[$k]['entryCount'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1055. $offset += 2;
  1056. $subHeaders[$k]['idDelta'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1057. $offset += 2;
  1058. $subHeaders[$k]['idRangeOffset'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1059. $offset += 2;
  1060. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  1061. $subHeaders[$k]['idRangeOffset'] /= 2;
  1062. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  1063. }
  1064. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  1065. $glyphIndexArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1066. $offset += 2;
  1067. }
  1068. for ($i = 0; $i < 256; ++$i) {
  1069. $k = $subHeaderKeys[$i];
  1070. if ($k == 0) {
  1071. // one byte code
  1072. $c = $i;
  1073. if (isset($subsetchars[$c])) {
  1074. $g = $glyphIndexArray[0];
  1075. $subsetglyphs[$g] = true;
  1076. }
  1077. } else {
  1078. // two bytes code
  1079. $start_byte = $subHeaders[$k]['firstCode'];
  1080. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  1081. for ($j = $start_byte; $j < $end_byte; ++$j) {
  1082. // combine high and low bytes
  1083. $c = (($i << 8) + $j);
  1084. if (isset($subsetchars[$c])) {
  1085. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  1086. $g = ($glyphIndexArray[$idRangeOffset] + $subHeaders[$k]['idDelta']) % 65536;
  1087. if ($g < 0) {
  1088. $g = 0;
  1089. }
  1090. $subsetglyphs[$g] = true;
  1091. }
  1092. }
  1093. }
  1094. }
  1095. break;
  1096. }
  1097. case 4: { // Format 4: Segment mapping to delta values
  1098. $length = TCPDF_STATIC::_getUSHORT($font, $offset);
  1099. $offset += 2;
  1100. $offset += 2; // skip version/language
  1101. $segCount = floor(TCPDF_STATIC::_getUSHORT($font, $offset) / 2);
  1102. $offset += 2;
  1103. $offset += 6; // skip searchRange, entrySelector, rangeShift
  1104. $endCount = array(); // array of end character codes for each segment
  1105. for ($k = 0; $k < $segCount; ++$k) {
  1106. $endCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1107. $offset += 2;
  1108. }
  1109. $offset += 2; // skip reservedPad
  1110. $startCount = array(); // array of start character codes for each segment
  1111. for ($k = 0; $k < $segCount; ++$k) {
  1112. $startCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1113. $offset += 2;
  1114. }
  1115. $idDelta = array(); // delta for all character codes in segment
  1116. for ($k = 0; $k < $segCount; ++$k) {
  1117. $idDelta[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1118. $offset += 2;
  1119. }
  1120. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  1121. for ($k = 0; $k < $segCount; ++$k) {
  1122. $idRangeOffset[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1123. $offset += 2;
  1124. }
  1125. $gidlen = (floor($length / 2) - 8 - (4 * $segCount));
  1126. $glyphIdArray = array(); // glyph index array
  1127. for ($k = 0; $k < $gidlen; ++$k) {
  1128. $glyphIdArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1129. $offset += 2;
  1130. }
  1131. for ($k = 0; $k < $segCount; ++$k) {
  1132. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  1133. if (isset($subsetchars[$c])) {
  1134. if ($idRangeOffset[$k] == 0) {
  1135. $g = ($idDelta[$k] + $c) % 65536;
  1136. } else {
  1137. $gid = (floor($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  1138. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  1139. }
  1140. if ($g < 0) {
  1141. $g = 0;
  1142. }
  1143. $subsetglyphs[$g] = true;
  1144. }
  1145. }
  1146. }
  1147. break;
  1148. }
  1149. case 6: { // Format 6: Trimmed table mapping
  1150. $offset += 4; // skip length and version/language
  1151. $firstCode = TCPDF_STATIC::_getUSHORT($font, $offset);
  1152. $offset += 2;
  1153. $entryCount = TCPDF_STATIC::_getUSHORT($font, $offset);
  1154. $offset += 2;
  1155. for ($k = 0; $k < $entryCount; ++$k) {
  1156. $c = ($k + $firstCode);
  1157. if (isset($subsetchars[$c])) {
  1158. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  1159. $subsetglyphs[$g] = true;
  1160. }
  1161. $offset += 2;
  1162. }
  1163. break;
  1164. }
  1165. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  1166. $offset += 10; // skip reserved, length and version/language
  1167. for ($k = 0; $k < 8192; ++$k) {
  1168. $is32[$k] = TCPDF_STATIC::_getBYTE($font, $offset);
  1169. ++$offset;
  1170. }
  1171. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  1172. $offset += 4;
  1173. for ($i = 0; $i < $nGroups; ++$i) {
  1174. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1175. $offset += 4;
  1176. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1177. $offset += 4;
  1178. $startGlyphID = TCPDF_STATIC::_getULONG($font, $offset);
  1179. $offset += 4;
  1180. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  1181. $is32idx = floor($c / 8);
  1182. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  1183. $c = $k;
  1184. } else {
  1185. // 32 bit format
  1186. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  1187. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  1188. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  1189. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  1190. }
  1191. if (isset($subsetchars[$c])) {
  1192. $subsetglyphs[$startGlyphID] = true;
  1193. }
  1194. ++$startGlyphID;
  1195. }
  1196. }
  1197. break;
  1198. }
  1199. case 10: { // Format 10: Trimmed array
  1200. $offset += 10; // skip reserved, length and version/language
  1201. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1202. $offset += 4;
  1203. $numChars = TCPDF_STATIC::_getULONG($font, $offset);
  1204. $offset += 4;
  1205. for ($k = 0; $k < $numChars; ++$k) {
  1206. $c = ($k + $startCharCode);
  1207. if (isset($subsetchars[$c])) {
  1208. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  1209. $subsetglyphs[$g] = true;
  1210. }
  1211. $offset += 2;
  1212. }
  1213. break;
  1214. }
  1215. case 12: { // Format 12: Segmented coverage
  1216. $offset += 10; // skip length and version/language
  1217. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  1218. $offset += 4;
  1219. for ($k = 0; $k < $nGroups; ++$k) {
  1220. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1221. $offset += 4;
  1222. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1223. $offset += 4;
  1224. $startGlyphCode = TCPDF_STATIC::_getULONG($font, $offset);
  1225. $offset += 4;
  1226. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  1227. if (isset($subsetchars[$c])) {
  1228. $subsetglyphs[$startGlyphCode] = true;
  1229. }
  1230. ++$startGlyphCode;
  1231. }
  1232. }
  1233. break;
  1234. }
  1235. case 13: { // Format 13: Many-to-one range mappings
  1236. // to be implemented ...
  1237. break;
  1238. }
  1239. case 14: { // Format 14: Unicode Variation Sequences
  1240. // to be implemented ...
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. // include all parts of composite glyphs
  1246. $new_sga = $subsetglyphs;
  1247. while (!empty($new_sga)) {
  1248. $sga = $new_sga;
  1249. $new_sga = array();
  1250. foreach ($sga as $key => $val) {
  1251. if (isset($indexToLoc[$key])) {
  1252. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  1253. $numberOfContours = TCPDF_STATIC::_getSHORT($font, $offset);
  1254. $offset += 2;
  1255. if ($numberOfContours < 0) { // composite glyph
  1256. $offset += 8; // skip xMin, yMin, xMax, yMax
  1257. do {
  1258. $flags = TCPDF_STATIC::_getUSHORT($font, $offset);
  1259. $offset += 2;
  1260. $glyphIndex = TCPDF_STATIC::_getUSHORT($font, $offset);
  1261. $offset += 2;
  1262. if (!isset($subsetglyphs[$glyphIndex])) {
  1263. // add missing glyphs
  1264. $new_sga[$glyphIndex] = true;
  1265. }
  1266. // skip some bytes by case
  1267. if ($flags & 1) {
  1268. $offset += 4;
  1269. } else {
  1270. $offset += 2;
  1271. }
  1272. if ($flags & 8) {
  1273. $offset += 2;
  1274. } elseif ($flags & 64) {
  1275. $offset += 4;
  1276. } elseif ($flags & 128) {
  1277. $offset += 8;
  1278. }
  1279. } while ($flags & 32);
  1280. }
  1281. }
  1282. }
  1283. $subsetglyphs += $new_sga;
  1284. }
  1285. // sort glyphs by key (and remove duplicates)
  1286. ksort($subsetglyphs);
  1287. // build new glyf and loca tables
  1288. $glyf = '';
  1289. $loca = '';
  1290. $offset = 0;
  1291. $glyf_offset = $table['glyf']['offset'];
  1292. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  1293. if (isset($subsetglyphs[$i])) {
  1294. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  1295. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  1296. } else {
  1297. $length = 0;
  1298. }
  1299. if ($short_offset) {
  1300. $loca .= pack('n', floor($offset / 2));
  1301. } else {
  1302. $loca .= pack('N', $offset);
  1303. }
  1304. $offset += $length;
  1305. }
  1306. // array of table names to preserve (loca and glyf tables will be added later)
  1307. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  1308. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  1309. // get the tables to preserve
  1310. $offset = 12;
  1311. foreach ($table as $tag => $val) {
  1312. if (in_array($tag, $table_names)) {
  1313. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  1314. if ($tag == 'head') {
  1315. // set the checkSumAdjustment to 0
  1316. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  1317. }
  1318. $pad = 4 - ($table[$tag]['length'] % 4);
  1319. if ($pad != 4) {
  1320. // the length of a table must be a multiple of four bytes
  1321. $table[$tag]['length'] += $pad;
  1322. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  1323. }
  1324. $table[$tag]['offset'] = $offset;
  1325. $offset += $table[$tag]['length'];
  1326. // check sum is not changed (so keep the following line commented)
  1327. //$table[$tag]['checkSum'] = self::_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  1328. } else {
  1329. unset($table[$tag]);
  1330. }
  1331. }
  1332. // add loca
  1333. $table['loca']['data'] = $loca;
  1334. $table['loca']['length'] = strlen($loca);
  1335. $pad = 4 - ($table['loca']['length'] % 4);
  1336. if ($pad != 4) {
  1337. // the length of a table must be a multiple of four bytes
  1338. $table['loca']['length'] += $pad;
  1339. $table['loca']['data'] .= str_repeat("\x0", $pad);
  1340. }
  1341. $table['loca']['offset'] = $offset;
  1342. $table['loca']['checkSum'] = self::_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  1343. $offset += $table['loca']['length'];
  1344. // add glyf
  1345. $table['glyf']['data'] = $glyf;
  1346. $table['glyf']['length'] = strlen($glyf);
  1347. $pad = 4 - ($table['glyf']['length'] % 4);
  1348. if ($pad != 4) {
  1349. // the length of a table must be a multiple of four bytes
  1350. $table['glyf']['length'] += $pad;
  1351. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  1352. }
  1353. $table['glyf']['offset'] = $offset;
  1354. $table['glyf']['checkSum'] = self::_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  1355. // rebuild font
  1356. $font = '';
  1357. $font .= pack('N', 0x10000); // sfnt version
  1358. $numTables = count($table);
  1359. $font .= pack('n', $numTables); // numTables
  1360. $entrySelector = floor(log($numTables, 2));
  1361. $searchRange = pow(2, $entrySelector) * 16;
  1362. $rangeShift = ($numTables * 16) - $searchRange;
  1363. $font .= pack('n', $searchRange); // searchRange
  1364. $font .= pack('n', $entrySelector); // entrySelector
  1365. $font .= pack('n', $rangeShift); // rangeShift
  1366. $offset = ($numTables * 16);
  1367. foreach ($table as $tag => $data) {
  1368. $font .= $tag; // tag
  1369. $font .= pack('N', $data['checkSum']); // checkSum
  1370. $font .= pack('N', ($data['offset'] + $offset)); // offset
  1371. $font .= pack('N', $data['length']); // length
  1372. }
  1373. foreach ($table as $data) {
  1374. $font .= $data['data'];
  1375. }
  1376. // set checkSumAdjustment on head table
  1377. $checkSumAdjustment = 0xB1B0AFBA - self::_getTTFtableChecksum($font, strlen($font));
  1378. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  1379. return $font;
  1380. }
  1381. /**
  1382. * Outputs font widths
  1383. * @param $font (array) font data
  1384. * @param $cidoffset (int) offset for CID values
  1385. * @return PDF command string for font widths
  1386. * @author Nicola Asuni
  1387. * @since 4.4.000 (2008-12-07)
  1388. * @public static
  1389. */
  1390. public static function _putfontwidths($font, $cidoffset=0) {
  1391. ksort($font['cw']);
  1392. $rangeid = 0;
  1393. $range = array();
  1394. $prevcid = -2;
  1395. $prevwidth = -1;
  1396. $interval = false;
  1397. // for each character
  1398. foreach ($font['cw'] as $cid => $width) {
  1399. $cid -= $cidoffset;
  1400. if ($font['subset'] AND (!isset($font['subsetchars'][$cid]))) {
  1401. // ignore the unused characters (font subsetting)
  1402. continue;
  1403. }
  1404. if ($width != $font['dw']) {
  1405. if ($cid == ($prevcid + 1)) {
  1406. // consecutive CID
  1407. if ($width == $prevwidth) {
  1408. if ($width == $range[$rangeid][0]) {
  1409. $range[$rangeid][] = $width;
  1410. } else {
  1411. array_pop($range[$rangeid]);
  1412. // new range
  1413. $rangeid = $prevcid;
  1414. $range[$rangeid] = array();
  1415. $range[$rangeid][] = $prevwidth;
  1416. $range[$rangeid][] = $width;
  1417. }
  1418. $interval = true;
  1419. $range[$rangeid]['interval'] = true;
  1420. } else {
  1421. if ($interval) {
  1422. // new range
  1423. $rangeid = $cid;
  1424. $range[$rangeid] = array();
  1425. $range[$rangeid][] = $width;
  1426. } else {
  1427. $range[$rangeid][] = $width;
  1428. }
  1429. $interval = false;
  1430. }
  1431. } else {
  1432. // new range
  1433. $rangeid = $cid;
  1434. $range[$rangeid] = array();
  1435. $range[$rangeid][] = $width;
  1436. $interval = false;
  1437. }
  1438. $prevcid = $cid;
  1439. $prevwidth = $width;
  1440. }
  1441. }
  1442. // optimize ranges
  1443. $prevk = -1;
  1444. $nextk = -1;
  1445. $prevint = false;
  1446. foreach ($range as $k => $ws) {
  1447. $cws = count($ws);
  1448. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  1449. if (isset($range[$k]['interval'])) {
  1450. unset($range[$k]['interval']);
  1451. }
  1452. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  1453. unset($range[$k]);
  1454. } else {
  1455. $prevk = $k;
  1456. }
  1457. $nextk = $k + $cws;
  1458. if (isset($ws['interval'])) {
  1459. if ($cws > 3) {
  1460. $prevint = true;
  1461. } else {
  1462. $prevint = false;
  1463. }
  1464. if (isset($range[$k]['interval'])) {
  1465. unset($range[$k]['interval']);
  1466. }
  1467. --$nextk;
  1468. } else {
  1469. $prevint = false;
  1470. }
  1471. }
  1472. // output data
  1473. $w = '';
  1474. foreach ($range as $k => $ws) {
  1475. if (count(array_count_values($ws)) == 1) {
  1476. // interval mode is more compact
  1477. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  1478. } else {
  1479. // range mode
  1480. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  1481. }
  1482. }
  1483. return '/W ['.$w.' ]';
  1484. }
  1485. /**
  1486. * Update the CIDToGIDMap string with a new value.
  1487. * @param $map (string) CIDToGIDMap.
  1488. * @param $cid (int) CID value.
  1489. * @param $gid (int) GID value.
  1490. * @return (string) CIDToGIDMap.
  1491. * @author Nicola Asuni
  1492. * @since 5.9.123 (2011-09-29)
  1493. * @public static
  1494. */
  1495. public static function updateCIDtoGIDmap($map, $cid, $gid) {
  1496. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  1497. if ($gid > 0xFFFF) {
  1498. $gid -= 0x10000;
  1499. }
  1500. $map[($cid * 2)] = chr($gid >> 8);
  1501. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  1502. }
  1503. return $map;
  1504. }
  1505. /**
  1506. * Return fonts path
  1507. * @return string
  1508. * @public static
  1509. */
  1510. public static function _getfontpath() {
  1511. if (!defined('K_PATH_FONTS') AND is_dir($fdir = realpath(dirname(__FILE__).'/../fonts'))) {
  1512. if (substr($fdir, -1) != '/') {
  1513. $fdir .= '/';
  1514. }
  1515. define('K_PATH_FONTS', $fdir);
  1516. }
  1517. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  1518. }
  1519. /**
  1520. * Return font full path
  1521. * @param $file (string) Font file name.
  1522. * @param $fontdir (string) Font directory (set to false fto search on default directories)
  1523. * @return string Font full path or empty string
  1524. * @author Nicola Asuni
  1525. * @since 6.0.025
  1526. * @public static
  1527. */
  1528. public static function getFontFullPath($file, $fontdir=false) {
  1529. $fontfile = '';
  1530. // search files on various directories
  1531. if (($fontdir !== false) AND @TCPDF_STATIC::file_exists($fontdir.$file)) {
  1532. $fontfile = $fontdir.$file;
  1533. } elseif (@TCPDF_STATIC::file_exists(self::_getfontpath().$file)) {
  1534. $fontfile = self::_getfontpath().$file;
  1535. } elseif (@TCPDF_STATIC::file_exists($file)) {
  1536. $fontfile = $file;
  1537. }
  1538. return $fontfile;
  1539. }
  1540. /**
  1541. * Get a reference font size.
  1542. * @param $size (string) String containing font size value.
  1543. * @param $refsize (float) Reference font size in points.
  1544. * @return float value in points
  1545. * @public static
  1546. */
  1547. public static function getFontRefSize($size, $refsize=12) {
  1548. switch ($size) {
  1549. case 'xx-small': {
  1550. $size = ($refsize - 4);
  1551. break;
  1552. }
  1553. case 'x-small': {
  1554. $size = ($refsize - 3);
  1555. break;
  1556. }
  1557. case 'small': {
  1558. $size = ($refsize - 2);
  1559. break;
  1560. }
  1561. case 'medium': {
  1562. $size = $refsize;
  1563. break;
  1564. }
  1565. case 'large': {
  1566. $size = ($refsize + 2);
  1567. break;
  1568. }
  1569. case 'x-large': {
  1570. $size = ($refsize + 4);
  1571. break;
  1572. }
  1573. case 'xx-large': {
  1574. $size = ($refsize + 6);
  1575. break;
  1576. }
  1577. case 'smaller': {
  1578. $size = ($refsize - 3);
  1579. break;
  1580. }
  1581. case 'larger': {
  1582. $size = ($refsize + 3);
  1583. break;
  1584. }
  1585. }
  1586. return $size;
  1587. }
  1588. // ====================================================================================================================
  1589. // REIMPLEMENTED
  1590. // ====================================================================================================================
  1591. /**
  1592. * Returns the unicode caracter specified by the value
  1593. * @param $c (int) UTF-8 value
  1594. * @param $unicode (boolean) True if we are in unicode mode, false otherwise.
  1595. * @return Returns the specified character.
  1596. * @since 2.3.000 (2008-03-05)
  1597. * @public static
  1598. */
  1599. public static function unichr($c, $unicode=true) {
  1600. $c = intval($c);
  1601. if (!$unicode) {
  1602. return chr($c);
  1603. } elseif ($c <= 0x7F) {
  1604. // one byte
  1605. return chr($c);
  1606. } elseif ($c <= 0x7FF) {
  1607. // two bytes
  1608. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  1609. } elseif ($c <= 0xFFFF) {
  1610. // three bytes
  1611. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  1612. } elseif ($c <= 0x10FFFF) {
  1613. // four bytes
  1614. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  1615. } else {
  1616. return '';
  1617. }
  1618. }
  1619. /**
  1620. * Returns the unicode caracter specified by UTF-8 value
  1621. * @param $c (int) UTF-8 value
  1622. * @return Returns the specified character.
  1623. * @public static
  1624. */
  1625. public static function unichrUnicode($c) {
  1626. return self::unichr($c, true);
  1627. }
  1628. /**
  1629. * Returns the unicode caracter specified by ASCII value
  1630. * @param $c (int) UTF-8 value
  1631. * @return Returns the specified character.
  1632. * @public static
  1633. */
  1634. public static function unichrASCII($c) {
  1635. return self::unichr($c, false);
  1636. }
  1637. /**
  1638. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  1639. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  1640. * <pre>
  1641. * Encoding UTF-16:
  1642. *
  1643. * Encoding of a single character from an ISO 10646 character value to
  1644. * UTF-16 proceeds as follows. Let U be the character number, no greater
  1645. * than 0x10FFFF.
  1646. *
  1647. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  1648. * terminate.
  1649. *
  1650. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  1651. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  1652. * represented in 20 bits.
  1653. *
  1654. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  1655. * 0xDC00, respectively. These integers each have 10 bits free to
  1656. * encode the character value, for a total of 20 bits.
  1657. *
  1658. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  1659. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  1660. * bits of W2. Terminate.
  1661. *
  1662. * Graphically, steps 2 through 4 look like:
  1663. * U' = yyyyyyyyyyxxxxxxxxxx
  1664. * W1 = 110110yyyyyyyyyy
  1665. * W2 = 110111xxxxxxxxxx
  1666. * </pre>
  1667. * @param $unicode (array) array containing UTF-8 unicode values
  1668. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  1669. * @return string
  1670. * @protected
  1671. * @author Nicola Asuni
  1672. * @since 2.1.000 (2008-01-08)
  1673. * @public static
  1674. */
  1675. public static function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  1676. $outstr = ''; // string to be returned
  1677. if ($setbom) {
  1678. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  1679. }
  1680. foreach ($unicode as $char) {
  1681. if ($char == 0x200b) {
  1682. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  1683. } elseif ($char == 0xFFFD) {
  1684. $outstr .= "\xFF\xFD"; // replacement character
  1685. } elseif ($char < 0x10000) {
  1686. $outstr .= chr($char >> 0x08);
  1687. $outstr .= chr($char & 0xFF);
  1688. } else {
  1689. $char -= 0x10000;
  1690. $w1 = 0xD800 | ($char >> 0x0a);
  1691. $w2 = 0xDC00 | ($char & 0x3FF);
  1692. $outstr .= chr($w1 >> 0x08);
  1693. $outstr .= chr($w1 & 0xFF);
  1694. $outstr .= chr($w2 >> 0x08);
  1695. $outstr .= chr($w2 & 0xFF);
  1696. }
  1697. }
  1698. return $outstr;
  1699. }
  1700. /**
  1701. * Convert an array of UTF8 values to array of unicode characters
  1702. * @param $ta (array) The input array of UTF8 values.
  1703. * @param $isunicode (boolean) True for Unicode mode, false otherwise.
  1704. * @return Return array of unicode characters
  1705. * @since 4.5.037 (2009-04-07)
  1706. * @public static
  1707. */
  1708. public static function UTF8ArrayToUniArray($ta, $isunicode=true) {
  1709. if ($isunicode) {
  1710. return array_map(array('TCPDF_FONTS', 'unichrUnicode'), $ta);
  1711. }
  1712. return array_map(array('TCPDF_FONTS', 'unichrASCII'), $ta);
  1713. }
  1714. /**
  1715. * Extract a slice of the $strarr array and return it as string.
  1716. * @param $strarr (string) The input array of characters.
  1717. * @param $start (int) the starting element of $strarr.
  1718. * @param $end (int) first element that will not be returned.
  1719. * @param $unicode (boolean) True if we are in unicode mode, false otherwise.
  1720. * @return Return part of a string
  1721. * @public static
  1722. */
  1723. public static function UTF8ArrSubString($strarr, $start='', $end='', $unicode=true) {
  1724. if (strlen($start) == 0) {
  1725. $start = 0;
  1726. }
  1727. if (strlen($end) == 0) {
  1728. $end = count($strarr);
  1729. }
  1730. $string = '';
  1731. for ($i = $start; $i < $end; ++$i) {
  1732. $string .= self::unichr($strarr[$i], $unicode);
  1733. }
  1734. return $string;
  1735. }
  1736. /**
  1737. * Extract a slice of the $uniarr array and return it as string.
  1738. * @param $uniarr (string) The input array of characters.
  1739. * @param $start (int) the starting element of $strarr.
  1740. * @param $end (int) first element that will not be returned.
  1741. * @return Return part of a string
  1742. * @since 4.5.037 (2009-04-07)
  1743. * @public static
  1744. */
  1745. public static function UniArrSubString($uniarr, $start='', $end='') {
  1746. if (strlen($start) == 0) {
  1747. $start = 0;
  1748. }
  1749. if (strlen($end) == 0) {
  1750. $end = count($uniarr);
  1751. }
  1752. $string = '';
  1753. for ($i=$start; $i < $end; ++$i) {
  1754. $string .= $uniarr[$i];
  1755. }
  1756. return $string;
  1757. }
  1758. /**
  1759. * Converts UTF-8 characters array to array of Latin1 characters array<br>
  1760. * @param $unicode (array) array containing UTF-8 unicode values
  1761. * @return array
  1762. * @author Nicola Asuni
  1763. * @since 4.8.023 (2010-01-15)
  1764. * @public static
  1765. */
  1766. public static function UTF8ArrToLatin1Arr($unicode) {
  1767. $outarr = array(); // array to be returned
  1768. foreach ($unicode as $char) {
  1769. if ($char < 256) {
  1770. $outarr[] = $char;
  1771. } elseif (array_key_exists($char, TCPDF_FONT_DATA::$uni_utf8tolatin)) {
  1772. // map from UTF-8
  1773. $outarr[] = TCPDF_FONT_DATA::$uni_utf8tolatin[$char];
  1774. } elseif ($char == 0xFFFD) {
  1775. // skip
  1776. } else {
  1777. $outarr[] = 63; // '?' character
  1778. }
  1779. }
  1780. return $outarr;
  1781. }
  1782. /**
  1783. * Converts UTF-8 characters array to array of Latin1 string<br>
  1784. * @param $unicode (array) array containing UTF-8 unicode values
  1785. * @return array
  1786. * @author Nicola Asuni
  1787. * @since 4.8.023 (2010-01-15)
  1788. * @public static
  1789. */
  1790. public static function UTF8ArrToLatin1($unicode) {
  1791. $outstr = ''; // string to be returned
  1792. foreach ($unicode as $char) {
  1793. if ($char < 256) {
  1794. $outstr .= chr($char);
  1795. } elseif (array_key_exists($char, TCPDF_FONT_DATA::$uni_utf8tolatin)) {
  1796. // map from UTF-8
  1797. $outstr .= chr(TCPDF_FONT_DATA::$uni_utf8tolatin[$char]);
  1798. } elseif ($char == 0xFFFD) {
  1799. // skip
  1800. } else {
  1801. $outstr .= '?';
  1802. }
  1803. }
  1804. return $outstr;
  1805. }
  1806. /**
  1807. * Converts UTF-8 character to integer value.<br>
  1808. * Uses the getUniord() method if the value is not cached.
  1809. * @param $uch (string) character string to process.
  1810. * @return integer Unicode value
  1811. * @public static
  1812. */
  1813. public static function uniord($uch) {
  1814. if (!isset(self::$cache_uniord[$uch])) {
  1815. self::$cache_uniord[$uch] = self::getUniord($uch);
  1816. }
  1817. return self::$cache_uniord[$uch];
  1818. }
  1819. /**
  1820. * Converts UTF-8 character to integer value.<br>
  1821. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  1822. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  1823. * <pre>
  1824. * Char. number range | UTF-8 octet sequence
  1825. * (hexadecimal) | (binary)
  1826. * --------------------+-----------------------------------------------
  1827. * 0000 0000-0000 007F | 0xxxxxxx
  1828. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  1829. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  1830. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  1831. * ---------------------------------------------------------------------
  1832. *
  1833. * ABFN notation:
  1834. * ---------------------------------------------------------------------
  1835. * UTF8-octets = *( UTF8-char )
  1836. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  1837. * UTF8-1 = %x00-7F
  1838. * UTF8-2 = %xC2-DF UTF8-tail
  1839. *
  1840. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  1841. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  1842. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  1843. * %xF4 %x80-8F 2( UTF8-tail )
  1844. * UTF8-tail = %x80-BF
  1845. * ---------------------------------------------------------------------
  1846. * </pre>
  1847. * @param $uch (string) character string to process.
  1848. * @return integer Unicode value
  1849. * @author Nicola Asuni
  1850. * @public static
  1851. */
  1852. public static function getUniord($uch) {
  1853. if (function_exists('mb_convert_encoding')) {
  1854. list(, $char) = @unpack('N', mb_convert_encoding($uch, 'UCS-4BE', 'UTF-8'));
  1855. if ($char >= 0) {
  1856. return $char;
  1857. }
  1858. }
  1859. $bytes = array(); // array containing single character byte sequences
  1860. $countbytes = 0;
  1861. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  1862. $length = strlen($uch);
  1863. for ($i = 0; $i < $length; ++$i) {
  1864. $char = ord($uch[$i]); // get one string character at time
  1865. if ($countbytes == 0) { // get starting octect
  1866. if ($char <= 0x7F) {
  1867. return $char; // use the character "as is" because is ASCII
  1868. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  1869. $bytes[] = ($char - 0xC0) << 0x06;
  1870. ++$countbytes;
  1871. $numbytes = 2;
  1872. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  1873. $bytes[] = ($char - 0xE0) << 0x0C;
  1874. ++$countbytes;
  1875. $numbytes = 3;
  1876. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  1877. $bytes[] = ($char - 0xF0) << 0x12;
  1878. ++$countbytes;
  1879. $numbytes = 4;
  1880. } else {
  1881. // use replacement character for other invalid sequences
  1882. return 0xFFFD;
  1883. }
  1884. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  1885. $bytes[] = $char - 0x80;
  1886. ++$countbytes;
  1887. if ($countbytes == $numbytes) {
  1888. // compose UTF-8 bytes to a single unicode value
  1889. $char = $bytes[0];
  1890. for ($j = 1; $j < $numbytes; ++$j) {
  1891. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  1892. }
  1893. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  1894. // The definition of UTF-8 prohibits encoding character numbers between
  1895. // U+D800 and U+DFFF, which are reserved for use with the UTF-16
  1896. // encoding form (as surrogate pairs) and do not directly represent
  1897. // characters.
  1898. return 0xFFFD; // use replacement character
  1899. } else {
  1900. return $char;
  1901. }
  1902. }
  1903. } else {
  1904. // use replacement character for other invalid sequences
  1905. return 0xFFFD;
  1906. }
  1907. }
  1908. return 0xFFFD;
  1909. }
  1910. /**
  1911. * Converts UTF-8 strings to codepoints array.<br>
  1912. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  1913. * @param $str (string) string to process.
  1914. * @param $isunicode (boolean) True when the documetn is in Unicode mode, false otherwise.
  1915. * @param $currentfont (array) Reference to current font array.
  1916. * @return array containing codepoints (UTF-8 characters values)
  1917. * @author Nicola Asuni
  1918. * @public static
  1919. */
  1920. public static function UTF8StringToArray($str, $isunicode=true, &$currentfont) {
  1921. if ($isunicode) {
  1922. // requires PCRE unicode support turned on
  1923. $chars = TCPDF_STATIC::pregSplit('//','u', $str, -1, PREG_SPLIT_NO_EMPTY);
  1924. $carr = array_map(array('TCPDF_FONTS', 'uniord'), $chars);
  1925. } else {
  1926. $chars = str_split($str);
  1927. $carr = array_map('ord', $chars);
  1928. }
  1929. if (is_array($currentfont['subsetchars']) && is_array($carr)) {
  1930. $currentfont['subsetchars'] += array_fill_keys($carr, true);
  1931. } else {
  1932. $currentfont['subsetchars'] = array_merge($currentfont['subsetchars'], $carr);
  1933. }
  1934. return $carr;
  1935. }
  1936. /**
  1937. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  1938. * @param $str (string) string to process.
  1939. * @param $isunicode (boolean) True when the documetn is in Unicode mode, false otherwise.
  1940. * @param $currentfont (array) Reference to current font array.
  1941. * @return string
  1942. * @since 3.2.000 (2008-06-23)
  1943. * @public static
  1944. */
  1945. public static function UTF8ToLatin1($str, $isunicode=true, &$currentfont) {
  1946. $unicode = self::UTF8StringToArray($str, $isunicode, $currentfont); // array containing UTF-8 unicode values
  1947. return self::UTF8ArrToLatin1($unicode);
  1948. }
  1949. /**
  1950. * Converts UTF-8 strings to UTF16-BE.<br>
  1951. * @param $str (string) string to process.
  1952. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  1953. * @param $isunicode (boolean) True when the documetn is in Unicode mode, false otherwise.
  1954. * @param $currentfont (array) Reference to current font array.
  1955. * @return string
  1956. * @author Nicola Asuni
  1957. * @since 1.53.0.TC005 (2005-01-05)
  1958. * @public static
  1959. */
  1960. public static function UTF8ToUTF16BE($str, $setbom=false, $isunicode=true, &$currentfont) {
  1961. if (!$isunicode) {
  1962. return $str; // string is not in unicode
  1963. }
  1964. $unicode = self::UTF8StringToArray($str, $isunicode, $currentfont); // array containing UTF-8 unicode values
  1965. return self::arrUTF8ToUTF16BE($unicode, $setbom);
  1966. }
  1967. /**
  1968. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  1969. * @param $str (string) string to manipulate.
  1970. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  1971. * @param $forcertl (bool) if true forces RTL text direction
  1972. * @param $isunicode (boolean) True if the document is in Unicode mode, false otherwise.
  1973. * @param $currentfont (array) Reference to current font array.
  1974. * @return string
  1975. * @author Nicola Asuni
  1976. * @since 2.1.000 (2008-01-08)
  1977. * @public static
  1978. */
  1979. public static function utf8StrRev($str, $setbom=false, $forcertl=false, $isunicode=true, &$currentfont) {
  1980. return self::utf8StrArrRev(self::UTF8StringToArray($str, $isunicode, $currentfont), $str, $setbom, $forcertl, $isunicode, $currentfont);
  1981. }
  1982. /**
  1983. * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  1984. * @param $arr (array) array of unicode values.
  1985. * @param $str (string) string to manipulate (or empty value).
  1986. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  1987. * @param $forcertl (bool) if true forces RTL text direction
  1988. * @param $isunicode (boolean) True if the document is in Unicode mode, false otherwise.
  1989. * @param $currentfont (array) Reference to current font array.
  1990. * @return string
  1991. * @author Nicola Asuni
  1992. * @since 4.9.000 (2010-03-27)
  1993. * @public static
  1994. */
  1995. public static function utf8StrArrRev($arr, $str='', $setbom=false, $forcertl=false, $isunicode=true, &$currentfont) {
  1996. return self::arrUTF8ToUTF16BE(self::utf8Bidi($arr, $str, $forcertl, $isunicode, $currentfont), $setbom);
  1997. }
  1998. /**
  1999. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  2000. * @param $ta (array) array of characters composing the string.
  2001. * @param $str (string) string to process
  2002. * @param $forcertl (bool) if 'R' forces RTL, if 'L' forces LTR
  2003. * @param $isunicode (boolean) True if the document is in Unicode mode, false otherwise.
  2004. * @param $currentfont (array) Reference to current font array.
  2005. * @return array of unicode chars
  2006. * @author Nicola Asuni
  2007. * @since 2.4.000 (2008-03-06)
  2008. * @public static
  2009. */
  2010. public static function utf8Bidi($ta, $str='', $forcertl=false, $isunicode=true, &$currentfont) {
  2011. // paragraph embedding level
  2012. $pel = 0;
  2013. // max level
  2014. $maxlevel = 0;
  2015. if (TCPDF_STATIC::empty_string($str)) {
  2016. // create string from array
  2017. $str = self::UTF8ArrSubString($ta, '', '', $isunicode);
  2018. }
  2019. // check if string contains arabic text
  2020. if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $str)) {
  2021. $arabic = true;
  2022. } else {
  2023. $arabic = false;
  2024. }
  2025. // check if string contains RTL text
  2026. if (!($forcertl OR $arabic OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $str))) {
  2027. return $ta;
  2028. }
  2029. // get number of chars
  2030. $numchars = count($ta);
  2031. if ($forcertl == 'R') {
  2032. $pel = 1;
  2033. } elseif ($forcertl == 'L') {
  2034. $pel = 0;
  2035. } else {
  2036. // P2. In each paragraph, find the first character of type L, AL, or R.
  2037. // P3. If a character is found in P2 and it is of type AL or R, then set the paragraph embedding level to one; otherwise, set it to zero.
  2038. for ($i=0; $i < $numchars; ++$i) {
  2039. $type = TCPDF_FONT_DATA::$uni_type[$ta[$i]];
  2040. if ($type == 'L') {
  2041. $pel = 0;
  2042. break;
  2043. } elseif (($type == 'AL') OR ($type == 'R')) {
  2044. $pel = 1;
  2045. break;
  2046. }
  2047. }
  2048. }
  2049. // Current Embedding Level
  2050. $cel = $pel;
  2051. // directional override status
  2052. $dos = 'N';
  2053. $remember = array();
  2054. // start-of-level-run
  2055. $sor = $pel % 2 ? 'R' : 'L';
  2056. $eor = $sor;
  2057. // Array of characters data
  2058. $chardata = Array();
  2059. // X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
  2060. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  2061. for ($i=0; $i < $numchars; ++$i) {
  2062. if ($ta[$i] == TCPDF_FONT_DATA::$uni_RLE) {
  2063. // X2. With each RLE, compute the least greater odd embedding level.
  2064. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  2065. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2066. $next_level = $cel + ($cel % 2) + 1;
  2067. if ($next_level < 62) {
  2068. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_RLE, 'cel' => $cel, 'dos' => $dos);
  2069. $cel = $next_level;
  2070. $dos = 'N';
  2071. $sor = $eor;
  2072. $eor = $cel % 2 ? 'R' : 'L';
  2073. }
  2074. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_LRE) {
  2075. // X3. With each LRE, compute the least greater even embedding level.
  2076. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  2077. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2078. $next_level = $cel + 2 - ($cel % 2);
  2079. if ( $next_level < 62 ) {
  2080. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_LRE, 'cel' => $cel, 'dos' => $dos);
  2081. $cel = $next_level;
  2082. $dos = 'N';
  2083. $sor = $eor;
  2084. $eor = $cel % 2 ? 'R' : 'L';
  2085. }
  2086. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_RLO) {
  2087. // X4. With each RLO, compute the least greater odd embedding level.
  2088. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
  2089. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2090. $next_level = $cel + ($cel % 2) + 1;
  2091. if ($next_level < 62) {
  2092. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_RLO, 'cel' => $cel, 'dos' => $dos);
  2093. $cel = $next_level;
  2094. $dos = 'R';
  2095. $sor = $eor;
  2096. $eor = $cel % 2 ? 'R' : 'L';
  2097. }
  2098. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_LRO) {
  2099. // X5. With each LRO, compute the least greater even embedding level.
  2100. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
  2101. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2102. $next_level = $cel + 2 - ($cel % 2);
  2103. if ( $next_level < 62 ) {
  2104. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_LRO, 'cel' => $cel, 'dos' => $dos);
  2105. $cel = $next_level;
  2106. $dos = 'L';
  2107. $sor = $eor;
  2108. $eor = $cel % 2 ? 'R' : 'L';
  2109. }
  2110. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_PDF) {
  2111. // X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
  2112. if (count($remember)) {
  2113. $last = count($remember ) - 1;
  2114. if (($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_RLE) OR
  2115. ($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_LRE) OR
  2116. ($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_RLO) OR
  2117. ($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_LRO)) {
  2118. $match = array_pop($remember);
  2119. $cel = $match['cel'];
  2120. $dos = $match['dos'];
  2121. $sor = $eor;
  2122. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  2123. }
  2124. }
  2125. } elseif (($ta[$i] != TCPDF_FONT_DATA::$uni_RLE) AND
  2126. ($ta[$i] != TCPDF_FONT_DATA::$uni_LRE) AND
  2127. ($ta[$i] != TCPDF_FONT_DATA::$uni_RLO) AND
  2128. ($ta[$i] != TCPDF_FONT_DATA::$uni_LRO) AND
  2129. ($ta[$i] != TCPDF_FONT_DATA::$uni_PDF)) {
  2130. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  2131. // a. Set the level of the current character to the current embedding level.
  2132. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  2133. if ($dos != 'N') {
  2134. $chardir = $dos;
  2135. } else {
  2136. if (isset(TCPDF_FONT_DATA::$uni_type[$ta[$i]])) {
  2137. $chardir = TCPDF_FONT_DATA::$uni_type[$ta[$i]];
  2138. } else {
  2139. $chardir = 'L';
  2140. }
  2141. }
  2142. // stores string characters and other information
  2143. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  2144. }
  2145. } // end for each char
  2146. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  2147. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  2148. // X10. The remaining rules are applied to each run of characters at the same level. For each run, determine the start-of-level-run (sor) and end-of-level-run (eor) type, either L or R. This depends on the higher of the two levels on either side of the boundary (at the start or end of the paragraph, the level of the 'other' run is the base embedding level). If the higher level is odd, the type is R; otherwise, it is L.
  2149. // 3.3.3 Resolving Weak Types
  2150. // Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
  2151. // Nonspacing marks are now resolved based on the previous characters.
  2152. $numchars = count($chardata);
  2153. // W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
  2154. $prevlevel = -1; // track level changes
  2155. $levcount = 0; // counts consecutive chars at the same level
  2156. for ($i=0; $i < $numchars; ++$i) {
  2157. if ($chardata[$i]['type'] == 'NSM') {
  2158. if ($levcount) {
  2159. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2160. } elseif ($i > 0) {
  2161. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  2162. }
  2163. }
  2164. if ($chardata[$i]['level'] != $prevlevel) {
  2165. $levcount = 0;
  2166. } else {
  2167. ++$levcount;
  2168. }
  2169. $prevlevel = $chardata[$i]['level'];
  2170. }
  2171. // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number.
  2172. $prevlevel = -1;
  2173. $levcount = 0;
  2174. for ($i=0; $i < $numchars; ++$i) {
  2175. if ($chardata[$i]['char'] == 'EN') {
  2176. for ($j=$levcount; $j >= 0; $j--) {
  2177. if ($chardata[$j]['type'] == 'AL') {
  2178. $chardata[$i]['type'] = 'AN';
  2179. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  2180. break;
  2181. }
  2182. }
  2183. }
  2184. if ($chardata[$i]['level'] != $prevlevel) {
  2185. $levcount = 0;
  2186. } else {
  2187. ++$levcount;
  2188. }
  2189. $prevlevel = $chardata[$i]['level'];
  2190. }
  2191. // W3. Change all ALs to R.
  2192. for ($i=0; $i < $numchars; ++$i) {
  2193. if ($chardata[$i]['type'] == 'AL') {
  2194. $chardata[$i]['type'] = 'R';
  2195. }
  2196. }
  2197. // W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
  2198. $prevlevel = -1;
  2199. $levcount = 0;
  2200. for ($i=0; $i < $numchars; ++$i) {
  2201. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  2202. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  2203. $chardata[$i]['type'] = 'EN';
  2204. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  2205. $chardata[$i]['type'] = 'EN';
  2206. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  2207. $chardata[$i]['type'] = 'AN';
  2208. }
  2209. }
  2210. if ($chardata[$i]['level'] != $prevlevel) {
  2211. $levcount = 0;
  2212. } else {
  2213. ++$levcount;
  2214. }
  2215. $prevlevel = $chardata[$i]['level'];
  2216. }
  2217. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  2218. $prevlevel = -1;
  2219. $levcount = 0;
  2220. for ($i=0; $i < $numchars; ++$i) {
  2221. if ($chardata[$i]['type'] == 'ET') {
  2222. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  2223. $chardata[$i]['type'] = 'EN';
  2224. } else {
  2225. $j = $i+1;
  2226. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  2227. if ($chardata[$j]['type'] == 'EN') {
  2228. $chardata[$i]['type'] = 'EN';
  2229. break;
  2230. } elseif ($chardata[$j]['type'] != 'ET') {
  2231. break;
  2232. }
  2233. ++$j;
  2234. }
  2235. }
  2236. }
  2237. if ($chardata[$i]['level'] != $prevlevel) {
  2238. $levcount = 0;
  2239. } else {
  2240. ++$levcount;
  2241. }
  2242. $prevlevel = $chardata[$i]['level'];
  2243. }
  2244. // W6. Otherwise, separators and terminators change to Other Neutral.
  2245. $prevlevel = -1;
  2246. $levcount = 0;
  2247. for ($i=0; $i < $numchars; ++$i) {
  2248. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  2249. $chardata[$i]['type'] = 'ON';
  2250. }
  2251. if ($chardata[$i]['level'] != $prevlevel) {
  2252. $levcount = 0;
  2253. } else {
  2254. ++$levcount;
  2255. }
  2256. $prevlevel = $chardata[$i]['level'];
  2257. }
  2258. //W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
  2259. $prevlevel = -1;
  2260. $levcount = 0;
  2261. for ($i=0; $i < $numchars; ++$i) {
  2262. if ($chardata[$i]['char'] == 'EN') {
  2263. for ($j=$levcount; $j >= 0; $j--) {
  2264. if ($chardata[$j]['type'] == 'L') {
  2265. $chardata[$i]['type'] = 'L';
  2266. } elseif ($chardata[$j]['type'] == 'R') {
  2267. break;
  2268. }
  2269. }
  2270. }
  2271. if ($chardata[$i]['level'] != $prevlevel) {
  2272. $levcount = 0;
  2273. } else {
  2274. ++$levcount;
  2275. }
  2276. $prevlevel = $chardata[$i]['level'];
  2277. }
  2278. // N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
  2279. $prevlevel = -1;
  2280. $levcount = 0;
  2281. for ($i=0; $i < $numchars; ++$i) {
  2282. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  2283. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  2284. $chardata[$i]['type'] = 'L';
  2285. } elseif (($chardata[$i]['type'] == 'N') AND
  2286. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  2287. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  2288. $chardata[$i]['type'] = 'R';
  2289. } elseif ($chardata[$i]['type'] == 'N') {
  2290. // N2. Any remaining neutrals take the embedding direction
  2291. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2292. }
  2293. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  2294. // first char
  2295. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  2296. $chardata[$i]['type'] = 'L';
  2297. } elseif (($chardata[$i]['type'] == 'N') AND
  2298. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  2299. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  2300. $chardata[$i]['type'] = 'R';
  2301. } elseif ($chardata[$i]['type'] == 'N') {
  2302. // N2. Any remaining neutrals take the embedding direction
  2303. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2304. }
  2305. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  2306. //last char
  2307. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  2308. $chardata[$i]['type'] = 'L';
  2309. } elseif (($chardata[$i]['type'] == 'N') AND
  2310. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  2311. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  2312. $chardata[$i]['type'] = 'R';
  2313. } elseif ($chardata[$i]['type'] == 'N') {
  2314. // N2. Any remaining neutrals take the embedding direction
  2315. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2316. }
  2317. } elseif ($chardata[$i]['type'] == 'N') {
  2318. // N2. Any remaining neutrals take the embedding direction
  2319. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2320. }
  2321. if ($chardata[$i]['level'] != $prevlevel) {
  2322. $levcount = 0;
  2323. } else {
  2324. ++$levcount;
  2325. }
  2326. $prevlevel = $chardata[$i]['level'];
  2327. }
  2328. // I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
  2329. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  2330. for ($i=0; $i < $numchars; ++$i) {
  2331. $odd = $chardata[$i]['level'] % 2;
  2332. if ($odd) {
  2333. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  2334. $chardata[$i]['level'] += 1;
  2335. }
  2336. } else {
  2337. if ($chardata[$i]['type'] == 'R') {
  2338. $chardata[$i]['level'] += 1;
  2339. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  2340. $chardata[$i]['level'] += 2;
  2341. }
  2342. }
  2343. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  2344. }
  2345. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  2346. // 1. Segment separators,
  2347. // 2. Paragraph separators,
  2348. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  2349. // 4. Any sequence of white space characters at the end of the line.
  2350. for ($i=0; $i < $numchars; ++$i) {
  2351. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  2352. $chardata[$i]['level'] = $pel;
  2353. } elseif ($chardata[$i]['type'] == 'WS') {
  2354. $j = $i+1;
  2355. while ($j < $numchars) {
  2356. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  2357. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  2358. $chardata[$i]['level'] = $pel;
  2359. break;
  2360. } elseif ($chardata[$j]['type'] != 'WS') {
  2361. break;
  2362. }
  2363. ++$j;
  2364. }
  2365. }
  2366. }
  2367. // Arabic Shaping
  2368. // Cursively connected scripts, such as Arabic or Syriac, require the selection of positional character shapes that depend on adjacent characters. Shaping is logically applied after the Bidirectional Algorithm is used and is limited to characters within the same directional run.
  2369. if ($arabic) {
  2370. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  2371. $alfletter = array(1570,1571,1573,1575);
  2372. $chardata2 = $chardata;
  2373. $laaletter = false;
  2374. $charAL = array();
  2375. $x = 0;
  2376. for ($i=0; $i < $numchars; ++$i) {
  2377. if ((TCPDF_FONT_DATA::$uni_type[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  2378. $charAL[$x] = $chardata[$i];
  2379. $charAL[$x]['i'] = $i;
  2380. $chardata[$i]['x'] = $x;
  2381. ++$x;
  2382. }
  2383. }
  2384. $numAL = $x;
  2385. for ($i=0; $i < $numchars; ++$i) {
  2386. $thischar = $chardata[$i];
  2387. if ($i > 0) {
  2388. $prevchar = $chardata[($i-1)];
  2389. } else {
  2390. $prevchar = false;
  2391. }
  2392. if (($i+1) < $numchars) {
  2393. $nextchar = $chardata[($i+1)];
  2394. } else {
  2395. $nextchar = false;
  2396. }
  2397. if (TCPDF_FONT_DATA::$uni_type[$thischar['char']] == 'AL') {
  2398. $x = $thischar['x'];
  2399. if ($x > 0) {
  2400. $prevchar = $charAL[($x-1)];
  2401. } else {
  2402. $prevchar = false;
  2403. }
  2404. if (($x+1) < $numAL) {
  2405. $nextchar = $charAL[($x+1)];
  2406. } else {
  2407. $nextchar = false;
  2408. }
  2409. // if laa letter
  2410. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  2411. $arabicarr = TCPDF_FONT_DATA::$uni_laa_array;
  2412. $laaletter = true;
  2413. if ($x > 1) {
  2414. $prevchar = $charAL[($x-2)];
  2415. } else {
  2416. $prevchar = false;
  2417. }
  2418. } else {
  2419. $arabicarr = TCPDF_FONT_DATA::$uni_arabicsubst;
  2420. $laaletter = false;
  2421. }
  2422. if (($prevchar !== false) AND ($nextchar !== false) AND
  2423. ((TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'NSM')) AND
  2424. ((TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'NSM')) AND
  2425. ($prevchar['type'] == $thischar['type']) AND
  2426. ($nextchar['type'] == $thischar['type']) AND
  2427. ($nextchar['char'] != 1567)) {
  2428. if (in_array($prevchar['char'], $endedletter)) {
  2429. if (isset($arabicarr[$thischar['char']][2])) {
  2430. // initial
  2431. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  2432. }
  2433. } else {
  2434. if (isset($arabicarr[$thischar['char']][3])) {
  2435. // medial
  2436. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  2437. }
  2438. }
  2439. } elseif (($nextchar !== false) AND
  2440. ((TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'NSM')) AND
  2441. ($nextchar['type'] == $thischar['type']) AND
  2442. ($nextchar['char'] != 1567)) {
  2443. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  2444. // initial
  2445. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  2446. }
  2447. } elseif ((($prevchar !== false) AND
  2448. ((TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'NSM')) AND
  2449. ($prevchar['type'] == $thischar['type'])) OR
  2450. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  2451. // final
  2452. if (($i > 1) AND ($thischar['char'] == 1607) AND
  2453. ($chardata[$i-1]['char'] == 1604) AND
  2454. ($chardata[$i-2]['char'] == 1604)) {
  2455. //Allah Word
  2456. // mark characters to delete with false
  2457. $chardata2[$i-2]['char'] = false;
  2458. $chardata2[$i-1]['char'] = false;
  2459. $chardata2[$i]['char'] = 65010;
  2460. } else {
  2461. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  2462. if (isset($arabicarr[$thischar['char']][0])) {
  2463. // isolated
  2464. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  2465. }
  2466. } else {
  2467. if (isset($arabicarr[$thischar['char']][1])) {
  2468. // final
  2469. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  2470. }
  2471. }
  2472. }
  2473. } elseif (isset($arabicarr[$thischar['char']][0])) {
  2474. // isolated
  2475. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  2476. }
  2477. // if laa letter
  2478. if ($laaletter) {
  2479. // mark characters to delete with false
  2480. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  2481. }
  2482. } // end if AL (Arabic Letter)
  2483. } // end for each char
  2484. /*
  2485. * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
  2486. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  2487. */
  2488. for ($i = 0; $i < ($numchars-1); ++$i) {
  2489. if (($chardata2[$i]['char'] == 1617) AND (isset(TCPDF_FONT_DATA::$uni_diacritics[($chardata2[$i+1]['char'])]))) {
  2490. // check if the subtitution font is defined on current font
  2491. if (isset($currentfont['cw'][(TCPDF_FONT_DATA::$uni_diacritics[($chardata2[$i+1]['char'])])])) {
  2492. $chardata2[$i]['char'] = false;
  2493. $chardata2[$i+1]['char'] = TCPDF_FONT_DATA::$uni_diacritics[($chardata2[$i+1]['char'])];
  2494. }
  2495. }
  2496. }
  2497. // remove marked characters
  2498. foreach ($chardata2 as $key => $value) {
  2499. if ($value['char'] === false) {
  2500. unset($chardata2[$key]);
  2501. }
  2502. }
  2503. $chardata = array_values($chardata2);
  2504. $numchars = count($chardata);
  2505. unset($chardata2);
  2506. unset($arabicarr);
  2507. unset($laaletter);
  2508. unset($charAL);
  2509. }
  2510. // L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
  2511. for ($j=$maxlevel; $j > 0; $j--) {
  2512. $ordarray = Array();
  2513. $revarr = Array();
  2514. $onlevel = false;
  2515. for ($i=0; $i < $numchars; ++$i) {
  2516. if ($chardata[$i]['level'] >= $j) {
  2517. $onlevel = true;
  2518. if (isset(TCPDF_FONT_DATA::$uni_mirror[$chardata[$i]['char']])) {
  2519. // L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true.
  2520. $chardata[$i]['char'] = TCPDF_FONT_DATA::$uni_mirror[$chardata[$i]['char']];
  2521. }
  2522. $revarr[] = $chardata[$i];
  2523. } else {
  2524. if ($onlevel) {
  2525. $revarr = array_reverse($revarr);
  2526. $ordarray = array_merge($ordarray, $revarr);
  2527. $revarr = Array();
  2528. $onlevel = false;
  2529. }
  2530. $ordarray[] = $chardata[$i];
  2531. }
  2532. }
  2533. if ($onlevel) {
  2534. $revarr = array_reverse($revarr);
  2535. $ordarray = array_merge($ordarray, $revarr);
  2536. }
  2537. $chardata = $ordarray;
  2538. }
  2539. $ordarray = array();
  2540. foreach ($chardata as $cd) {
  2541. $ordarray[] = $cd['char'];
  2542. // store char values for subsetting
  2543. $currentfont['subsetchars'][$cd['char']] = true;
  2544. }
  2545. return $ordarray;
  2546. }
  2547. } // END OF TCPDF_FONTS CLASS
  2548. //============================================================+
  2549. // END OF FILE
  2550. //============================================================+