tcpdf_fonts.php 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  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 string $fontfile Font file (full path).
  55. * @param string $fonttype 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 string $enc 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 int $flags 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 string $outpath Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  59. * @param int $platid 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 int $encid 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 boolean $addcbbox If true includes the character bounding box information on the php font file.
  62. * @param boolean $link If true link to system font instead of copying the font data (not transportable) - Note: do not work with Type1 fonts.
  63. * @return string|false 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. $c = 0;
  557. foreach ($encodingTables as $enctable) {
  558. // get only specified Platform ID and Encoding ID
  559. if (($enctable['platformID'] == $platid) AND ($enctable['encodingID'] == $encid)) {
  560. $offset = $table['cmap']['offset'] + $enctable['offset'];
  561. $format = TCPDF_STATIC::_getUSHORT($font, $offset);
  562. $offset += 2;
  563. switch ($format) {
  564. case 0: { // Format 0: Byte encoding table
  565. $offset += 4; // skip length and version/language
  566. for ($c = 0; $c < 256; ++$c) {
  567. $g = TCPDF_STATIC::_getBYTE($font, $offset);
  568. $ctg[$c] = $g;
  569. ++$offset;
  570. }
  571. break;
  572. }
  573. case 2: { // Format 2: High-byte mapping through table
  574. $offset += 4; // skip length and version/language
  575. $numSubHeaders = 0;
  576. for ($i = 0; $i < 256; ++$i) {
  577. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  578. $subHeaderKeys[$i] = (TCPDF_STATIC::_getUSHORT($font, $offset) / 8);
  579. $offset += 2;
  580. if ($numSubHeaders < $subHeaderKeys[$i]) {
  581. $numSubHeaders = $subHeaderKeys[$i];
  582. }
  583. }
  584. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  585. ++$numSubHeaders;
  586. // read subHeader structures
  587. $subHeaders = array();
  588. $numGlyphIndexArray = 0;
  589. for ($k = 0; $k < $numSubHeaders; ++$k) {
  590. $subHeaders[$k]['firstCode'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  591. $offset += 2;
  592. $subHeaders[$k]['entryCount'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  593. $offset += 2;
  594. $subHeaders[$k]['idDelta'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  595. $offset += 2;
  596. $subHeaders[$k]['idRangeOffset'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  597. $offset += 2;
  598. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  599. $subHeaders[$k]['idRangeOffset'] /= 2;
  600. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  601. }
  602. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  603. $glyphIndexArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  604. $offset += 2;
  605. }
  606. for ($i = 0; $i < 256; ++$i) {
  607. $k = $subHeaderKeys[$i];
  608. if ($k == 0) {
  609. // one byte code
  610. $c = $i;
  611. $g = $glyphIndexArray[0];
  612. $ctg[$c] = $g;
  613. } else {
  614. // two bytes code
  615. $start_byte = $subHeaders[$k]['firstCode'];
  616. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  617. for ($j = $start_byte; $j < $end_byte; ++$j) {
  618. // combine high and low bytes
  619. $c = (($i << 8) + $j);
  620. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  621. $g = ($glyphIndexArray[$idRangeOffset] + $subHeaders[$k]['idDelta']) % 65536;
  622. if ($g < 0) {
  623. $g = 0;
  624. }
  625. $ctg[$c] = $g;
  626. }
  627. }
  628. }
  629. break;
  630. }
  631. case 4: { // Format 4: Segment mapping to delta values
  632. $length = TCPDF_STATIC::_getUSHORT($font, $offset);
  633. $offset += 2;
  634. $offset += 2; // skip version/language
  635. $segCount = floor(TCPDF_STATIC::_getUSHORT($font, $offset) / 2);
  636. $offset += 2;
  637. $offset += 6; // skip searchRange, entrySelector, rangeShift
  638. $endCount = array(); // array of end character codes for each segment
  639. for ($k = 0; $k < $segCount; ++$k) {
  640. $endCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  641. $offset += 2;
  642. }
  643. $offset += 2; // skip reservedPad
  644. $startCount = array(); // array of start character codes for each segment
  645. for ($k = 0; $k < $segCount; ++$k) {
  646. $startCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  647. $offset += 2;
  648. }
  649. $idDelta = array(); // delta for all character codes in segment
  650. for ($k = 0; $k < $segCount; ++$k) {
  651. $idDelta[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  652. $offset += 2;
  653. }
  654. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  655. for ($k = 0; $k < $segCount; ++$k) {
  656. $idRangeOffset[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  657. $offset += 2;
  658. }
  659. $gidlen = (floor($length / 2) - 8 - (4 * $segCount));
  660. $glyphIdArray = array(); // glyph index array
  661. for ($k = 0; $k < $gidlen; ++$k) {
  662. $glyphIdArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  663. $offset += 2;
  664. }
  665. for ($k = 0; $k < $segCount - 1; ++$k) {
  666. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  667. if ($idRangeOffset[$k] == 0) {
  668. $g = ($idDelta[$k] + $c) % 65536;
  669. } else {
  670. $gid = (floor($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  671. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  672. }
  673. if ($g < 0) {
  674. $g = 0;
  675. }
  676. $ctg[$c] = $g;
  677. }
  678. }
  679. break;
  680. }
  681. case 6: { // Format 6: Trimmed table mapping
  682. $offset += 4; // skip length and version/language
  683. $firstCode = TCPDF_STATIC::_getUSHORT($font, $offset);
  684. $offset += 2;
  685. $entryCount = TCPDF_STATIC::_getUSHORT($font, $offset);
  686. $offset += 2;
  687. for ($k = 0; $k < $entryCount; ++$k) {
  688. $c = ($k + $firstCode);
  689. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  690. $offset += 2;
  691. $ctg[$c] = $g;
  692. }
  693. break;
  694. }
  695. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  696. $offset += 10; // skip reserved, length and version/language
  697. for ($k = 0; $k < 8192; ++$k) {
  698. $is32[$k] = TCPDF_STATIC::_getBYTE($font, $offset);
  699. ++$offset;
  700. }
  701. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  702. $offset += 4;
  703. for ($i = 0; $i < $nGroups; ++$i) {
  704. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  705. $offset += 4;
  706. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  707. $offset += 4;
  708. $startGlyphID = TCPDF_STATIC::_getULONG($font, $offset);
  709. $offset += 4;
  710. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  711. $is32idx = floor($c / 8);
  712. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  713. $c = $k;
  714. } else {
  715. // 32 bit format
  716. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  717. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  718. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  719. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  720. }
  721. $ctg[$c] = 0;
  722. ++$startGlyphID;
  723. }
  724. }
  725. break;
  726. }
  727. case 10: { // Format 10: Trimmed array
  728. $offset += 10; // skip reserved, length and version/language
  729. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  730. $offset += 4;
  731. $numChars = TCPDF_STATIC::_getULONG($font, $offset);
  732. $offset += 4;
  733. for ($k = 0; $k < $numChars; ++$k) {
  734. $c = ($k + $startCharCode);
  735. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  736. $ctg[$c] = $g;
  737. $offset += 2;
  738. }
  739. break;
  740. }
  741. case 12: { // Format 12: Segmented coverage
  742. $offset += 10; // skip length and version/language
  743. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  744. $offset += 4;
  745. for ($k = 0; $k < $nGroups; ++$k) {
  746. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  747. $offset += 4;
  748. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  749. $offset += 4;
  750. $startGlyphCode = TCPDF_STATIC::_getULONG($font, $offset);
  751. $offset += 4;
  752. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  753. $ctg[$c] = $startGlyphCode;
  754. ++$startGlyphCode;
  755. }
  756. }
  757. break;
  758. }
  759. case 13: { // Format 13: Many-to-one range mappings
  760. // to be implemented ...
  761. break;
  762. }
  763. case 14: { // Format 14: Unicode Variation Sequences
  764. // to be implemented ...
  765. break;
  766. }
  767. }
  768. }
  769. }
  770. if (!isset($ctg[0])) {
  771. $ctg[0] = 0;
  772. }
  773. // get xHeight (height of x)
  774. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  775. $yMin = TCPDF_STATIC::_getFWORD($font, $offset);
  776. $offset += 4;
  777. $yMax = TCPDF_STATIC::_getFWORD($font, $offset);
  778. $offset += 2;
  779. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  780. // get CapHeight (height of H)
  781. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  782. $yMin = TCPDF_STATIC::_getFWORD($font, $offset);
  783. $offset += 4;
  784. $yMax = TCPDF_STATIC::_getFWORD($font, $offset);
  785. $offset += 2;
  786. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  787. // ceate widths array
  788. $cw = array();
  789. $offset = $table['hmtx']['offset'];
  790. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  791. $cw[$i] = round(TCPDF_STATIC::_getUFWORD($font, $offset) * $urk);
  792. $offset += 4; // skip lsb
  793. }
  794. if ($numberOfHMetrics < $numGlyphs) {
  795. // fill missing widths with the last value
  796. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  797. }
  798. $fmetric['MissingWidth'] = $cw[0];
  799. $fmetric['cw'] = '';
  800. $fmetric['cbbox'] = '';
  801. for ($cid = 0; $cid <= 65535; ++$cid) {
  802. if (isset($ctg[$cid])) {
  803. if (isset($cw[$ctg[$cid]])) {
  804. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  805. }
  806. if ($addcbbox AND isset($indexToLoc[$ctg[$cid]])) {
  807. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[$cid]]);
  808. $xMin = round(TCPDF_STATIC::_getFWORD($font, $offset + 2) * $urk);
  809. $yMin = round(TCPDF_STATIC::_getFWORD($font, $offset + 4) * $urk);
  810. $xMax = round(TCPDF_STATIC::_getFWORD($font, $offset + 6) * $urk);
  811. $yMax = round(TCPDF_STATIC::_getFWORD($font, $offset + 8) * $urk);
  812. $fmetric['cbbox'] .= ','.$cid.'=>array('.$xMin.','.$yMin.','.$xMax.','.$yMax.')';
  813. }
  814. }
  815. }
  816. } // end of true type
  817. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  818. $fmetric['type'] = 'TrueType';
  819. }
  820. // ---------- create php font file ----------
  821. $pfile = '<'.'?'.'php'."\n";
  822. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  823. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  824. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  825. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  826. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  827. if ($fmetric['MissingWidth'] > 0) {
  828. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  829. } else {
  830. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  831. }
  832. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  833. if ($fmetric['type'] == 'Type1') {
  834. // Type 1
  835. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  836. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  837. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  838. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  839. } else {
  840. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  841. if ($fmetric['type'] == 'cidfont0') {
  842. // CID-0
  843. switch ($fonttype) {
  844. case 'CID0JP': {
  845. $pfile .= '// Japanese'."\n";
  846. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  847. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  848. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  849. break;
  850. }
  851. case 'CID0KR': {
  852. $pfile .= '// Korean'."\n";
  853. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  854. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  855. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  856. break;
  857. }
  858. case 'CID0CS': {
  859. $pfile .= '// Chinese Simplified'."\n";
  860. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  861. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  862. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  863. break;
  864. }
  865. case 'CID0CT':
  866. default: {
  867. $pfile .= '// Chinese Traditional'."\n";
  868. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  869. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  870. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  871. break;
  872. }
  873. }
  874. } else {
  875. // TrueType
  876. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  877. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  878. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  879. // create CIDToGIDMap
  880. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  881. foreach ($ctg as $cid => $gid) {
  882. $cidtogidmap = self::updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  883. }
  884. // store compressed CIDToGIDMap
  885. $fp = TCPDF_STATIC::fopenLocal($outpath.$fmetric['ctg'], 'wb');
  886. fwrite($fp, gzcompress($cidtogidmap));
  887. fclose($fp);
  888. }
  889. }
  890. $pfile .= '$desc=array(';
  891. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  892. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  893. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  894. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  895. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  896. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  897. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  898. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  899. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  900. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  901. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  902. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  903. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  904. $pfile .= ');'."\n";
  905. if (!empty($fmetric['cbbox'])) {
  906. $pfile .= '$cbbox=array('.substr($fmetric['cbbox'], 1).');'."\n";
  907. }
  908. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  909. $pfile .= '// --- EOF ---'."\n";
  910. // store file
  911. $fp = TCPDF_STATIC::fopenLocal($outpath.$font_name.'.php', 'w');
  912. fwrite($fp, $pfile);
  913. fclose($fp);
  914. // return TCPDF font name
  915. return $font_name;
  916. }
  917. /**
  918. * Returs the checksum of a TTF table.
  919. * @param string $table table to check
  920. * @param int $length length of table in bytes
  921. * @return int checksum
  922. * @author Nicola Asuni
  923. * @since 5.2.000 (2010-06-02)
  924. * @public static
  925. */
  926. public static function _getTTFtableChecksum($table, $length) {
  927. $sum = 0;
  928. $tlen = ($length / 4);
  929. $offset = 0;
  930. for ($i = 0; $i < $tlen; ++$i) {
  931. $v = unpack('Ni', substr($table, $offset, 4));
  932. $sum += $v['i'];
  933. $offset += 4;
  934. }
  935. $sum = unpack('Ni', pack('N', $sum));
  936. return $sum['i'];
  937. }
  938. /**
  939. * Returns a subset of the TrueType font data without the unused glyphs.
  940. * @param string $font TrueType font data.
  941. * @param array $subsetchars Array of used characters (the glyphs to keep).
  942. * @return string A subset of TrueType font data without the unused glyphs.
  943. * @author Nicola Asuni
  944. * @since 5.2.000 (2010-06-02)
  945. * @public static
  946. */
  947. public static function _getTrueTypeFontSubset($font, $subsetchars) {
  948. ksort($subsetchars);
  949. $offset = 0; // offset position of the font data
  950. if (TCPDF_STATIC::_getULONG($font, $offset) != 0x10000) {
  951. // sfnt version must be 0x00010000 for TrueType version 1.0.
  952. return $font;
  953. }
  954. $c = 0;
  955. $offset += 4;
  956. // get number of tables
  957. $numTables = TCPDF_STATIC::_getUSHORT($font, $offset);
  958. $offset += 2;
  959. // skip searchRange, entrySelector and rangeShift
  960. $offset += 6;
  961. // tables array
  962. $table = array();
  963. // for each table
  964. for ($i = 0; $i < $numTables; ++$i) {
  965. // get table info
  966. $tag = substr($font, $offset, 4);
  967. $offset += 4;
  968. $table[$tag] = array();
  969. $table[$tag]['checkSum'] = TCPDF_STATIC::_getULONG($font, $offset);
  970. $offset += 4;
  971. $table[$tag]['offset'] = TCPDF_STATIC::_getULONG($font, $offset);
  972. $offset += 4;
  973. $table[$tag]['length'] = TCPDF_STATIC::_getULONG($font, $offset);
  974. $offset += 4;
  975. }
  976. // check magicNumber
  977. $offset = $table['head']['offset'] + 12;
  978. if (TCPDF_STATIC::_getULONG($font, $offset) != 0x5F0F3CF5) {
  979. // magicNumber must be 0x5F0F3CF5
  980. return $font;
  981. }
  982. $offset += 4;
  983. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  984. $offset = $table['head']['offset'] + 50;
  985. $short_offset = (TCPDF_STATIC::_getSHORT($font, $offset) == 0);
  986. $offset += 2;
  987. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  988. $indexToLoc = array();
  989. $offset = $table['loca']['offset'];
  990. if ($short_offset) {
  991. // short version
  992. $tot_num_glyphs = floor($table['loca']['length'] / 2); // numGlyphs + 1
  993. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  994. $indexToLoc[$i] = TCPDF_STATIC::_getUSHORT($font, $offset) * 2;
  995. $offset += 2;
  996. }
  997. } else {
  998. // long version
  999. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  1000. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  1001. $indexToLoc[$i] = TCPDF_STATIC::_getULONG($font, $offset);
  1002. $offset += 4;
  1003. }
  1004. }
  1005. // get glyphs indexes of chars from cmap table
  1006. $subsetglyphs = array(); // glyph IDs on key
  1007. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  1008. $offset = $table['cmap']['offset'] + 2;
  1009. $numEncodingTables = TCPDF_STATIC::_getUSHORT($font, $offset);
  1010. $offset += 2;
  1011. $encodingTables = array();
  1012. for ($i = 0; $i < $numEncodingTables; ++$i) {
  1013. $encodingTables[$i]['platformID'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1014. $offset += 2;
  1015. $encodingTables[$i]['encodingID'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1016. $offset += 2;
  1017. $encodingTables[$i]['offset'] = TCPDF_STATIC::_getULONG($font, $offset);
  1018. $offset += 4;
  1019. }
  1020. foreach ($encodingTables as $enctable) {
  1021. // get all platforms and encodings
  1022. $offset = $table['cmap']['offset'] + $enctable['offset'];
  1023. $format = TCPDF_STATIC::_getUSHORT($font, $offset);
  1024. $offset += 2;
  1025. switch ($format) {
  1026. case 0: { // Format 0: Byte encoding table
  1027. $offset += 4; // skip length and version/language
  1028. for ($c = 0; $c < 256; ++$c) {
  1029. if (isset($subsetchars[$c])) {
  1030. $g = TCPDF_STATIC::_getBYTE($font, $offset);
  1031. $subsetglyphs[$g] = true;
  1032. }
  1033. ++$offset;
  1034. }
  1035. break;
  1036. }
  1037. case 2: { // Format 2: High-byte mapping through table
  1038. $offset += 4; // skip length and version/language
  1039. $numSubHeaders = 0;
  1040. for ($i = 0; $i < 256; ++$i) {
  1041. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  1042. $subHeaderKeys[$i] = (TCPDF_STATIC::_getUSHORT($font, $offset) / 8);
  1043. $offset += 2;
  1044. if ($numSubHeaders < $subHeaderKeys[$i]) {
  1045. $numSubHeaders = $subHeaderKeys[$i];
  1046. }
  1047. }
  1048. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  1049. ++$numSubHeaders;
  1050. // read subHeader structures
  1051. $subHeaders = array();
  1052. $numGlyphIndexArray = 0;
  1053. for ($k = 0; $k < $numSubHeaders; ++$k) {
  1054. $subHeaders[$k]['firstCode'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1055. $offset += 2;
  1056. $subHeaders[$k]['entryCount'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1057. $offset += 2;
  1058. $subHeaders[$k]['idDelta'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1059. $offset += 2;
  1060. $subHeaders[$k]['idRangeOffset'] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1061. $offset += 2;
  1062. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  1063. $subHeaders[$k]['idRangeOffset'] /= 2;
  1064. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  1065. }
  1066. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  1067. $glyphIndexArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1068. $offset += 2;
  1069. }
  1070. for ($i = 0; $i < 256; ++$i) {
  1071. $k = $subHeaderKeys[$i];
  1072. if ($k == 0) {
  1073. // one byte code
  1074. $c = $i;
  1075. if (isset($subsetchars[$c])) {
  1076. $g = $glyphIndexArray[0];
  1077. $subsetglyphs[$g] = true;
  1078. }
  1079. } else {
  1080. // two bytes code
  1081. $start_byte = $subHeaders[$k]['firstCode'];
  1082. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  1083. for ($j = $start_byte; $j < $end_byte; ++$j) {
  1084. // combine high and low bytes
  1085. $c = (($i << 8) + $j);
  1086. if (isset($subsetchars[$c])) {
  1087. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  1088. $g = ($glyphIndexArray[$idRangeOffset] + $subHeaders[$k]['idDelta']) % 65536;
  1089. if ($g < 0) {
  1090. $g = 0;
  1091. }
  1092. $subsetglyphs[$g] = true;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. break;
  1098. }
  1099. case 4: { // Format 4: Segment mapping to delta values
  1100. $length = TCPDF_STATIC::_getUSHORT($font, $offset);
  1101. $offset += 2;
  1102. $offset += 2; // skip version/language
  1103. $segCount = floor(TCPDF_STATIC::_getUSHORT($font, $offset) / 2);
  1104. $offset += 2;
  1105. $offset += 6; // skip searchRange, entrySelector, rangeShift
  1106. $endCount = array(); // array of end character codes for each segment
  1107. for ($k = 0; $k < $segCount; ++$k) {
  1108. $endCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1109. $offset += 2;
  1110. }
  1111. $offset += 2; // skip reservedPad
  1112. $startCount = array(); // array of start character codes for each segment
  1113. for ($k = 0; $k < $segCount; ++$k) {
  1114. $startCount[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1115. $offset += 2;
  1116. }
  1117. $idDelta = array(); // delta for all character codes in segment
  1118. for ($k = 0; $k < $segCount; ++$k) {
  1119. $idDelta[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1120. $offset += 2;
  1121. }
  1122. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  1123. for ($k = 0; $k < $segCount; ++$k) {
  1124. $idRangeOffset[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1125. $offset += 2;
  1126. }
  1127. $gidlen = (floor($length / 2) - 8 - (4 * $segCount));
  1128. $glyphIdArray = array(); // glyph index array
  1129. for ($k = 0; $k < $gidlen; ++$k) {
  1130. $glyphIdArray[$k] = TCPDF_STATIC::_getUSHORT($font, $offset);
  1131. $offset += 2;
  1132. }
  1133. for ($k = 0; $k < $segCount; ++$k) {
  1134. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  1135. if (isset($subsetchars[$c])) {
  1136. if ($idRangeOffset[$k] == 0) {
  1137. $g = ($idDelta[$k] + $c) % 65536;
  1138. } else {
  1139. $gid = (floor($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  1140. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  1141. }
  1142. if ($g < 0) {
  1143. $g = 0;
  1144. }
  1145. $subsetglyphs[$g] = true;
  1146. }
  1147. }
  1148. }
  1149. break;
  1150. }
  1151. case 6: { // Format 6: Trimmed table mapping
  1152. $offset += 4; // skip length and version/language
  1153. $firstCode = TCPDF_STATIC::_getUSHORT($font, $offset);
  1154. $offset += 2;
  1155. $entryCount = TCPDF_STATIC::_getUSHORT($font, $offset);
  1156. $offset += 2;
  1157. for ($k = 0; $k < $entryCount; ++$k) {
  1158. $c = ($k + $firstCode);
  1159. if (isset($subsetchars[$c])) {
  1160. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  1161. $subsetglyphs[$g] = true;
  1162. }
  1163. $offset += 2;
  1164. }
  1165. break;
  1166. }
  1167. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  1168. $offset += 10; // skip reserved, length and version/language
  1169. for ($k = 0; $k < 8192; ++$k) {
  1170. $is32[$k] = TCPDF_STATIC::_getBYTE($font, $offset);
  1171. ++$offset;
  1172. }
  1173. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  1174. $offset += 4;
  1175. for ($i = 0; $i < $nGroups; ++$i) {
  1176. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1177. $offset += 4;
  1178. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1179. $offset += 4;
  1180. $startGlyphID = TCPDF_STATIC::_getULONG($font, $offset);
  1181. $offset += 4;
  1182. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  1183. $is32idx = floor($c / 8);
  1184. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  1185. $c = $k;
  1186. } else {
  1187. // 32 bit format
  1188. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  1189. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  1190. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  1191. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  1192. }
  1193. if (isset($subsetchars[$c])) {
  1194. $subsetglyphs[$startGlyphID] = true;
  1195. }
  1196. ++$startGlyphID;
  1197. }
  1198. }
  1199. break;
  1200. }
  1201. case 10: { // Format 10: Trimmed array
  1202. $offset += 10; // skip reserved, length and version/language
  1203. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1204. $offset += 4;
  1205. $numChars = TCPDF_STATIC::_getULONG($font, $offset);
  1206. $offset += 4;
  1207. for ($k = 0; $k < $numChars; ++$k) {
  1208. $c = ($k + $startCharCode);
  1209. if (isset($subsetchars[$c])) {
  1210. $g = TCPDF_STATIC::_getUSHORT($font, $offset);
  1211. $subsetglyphs[$g] = true;
  1212. }
  1213. $offset += 2;
  1214. }
  1215. break;
  1216. }
  1217. case 12: { // Format 12: Segmented coverage
  1218. $offset += 10; // skip length and version/language
  1219. $nGroups = TCPDF_STATIC::_getULONG($font, $offset);
  1220. $offset += 4;
  1221. for ($k = 0; $k < $nGroups; ++$k) {
  1222. $startCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1223. $offset += 4;
  1224. $endCharCode = TCPDF_STATIC::_getULONG($font, $offset);
  1225. $offset += 4;
  1226. $startGlyphCode = TCPDF_STATIC::_getULONG($font, $offset);
  1227. $offset += 4;
  1228. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  1229. if (isset($subsetchars[$c])) {
  1230. $subsetglyphs[$startGlyphCode] = true;
  1231. }
  1232. ++$startGlyphCode;
  1233. }
  1234. }
  1235. break;
  1236. }
  1237. case 13: { // Format 13: Many-to-one range mappings
  1238. // to be implemented ...
  1239. break;
  1240. }
  1241. case 14: { // Format 14: Unicode Variation Sequences
  1242. // to be implemented ...
  1243. break;
  1244. }
  1245. }
  1246. }
  1247. // include all parts of composite glyphs
  1248. $new_sga = $subsetglyphs;
  1249. while (!empty($new_sga)) {
  1250. $sga = $new_sga;
  1251. $new_sga = array();
  1252. foreach ($sga as $key => $val) {
  1253. if (isset($indexToLoc[$key])) {
  1254. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  1255. $numberOfContours = TCPDF_STATIC::_getSHORT($font, $offset);
  1256. $offset += 2;
  1257. if ($numberOfContours < 0) { // composite glyph
  1258. $offset += 8; // skip xMin, yMin, xMax, yMax
  1259. do {
  1260. $flags = TCPDF_STATIC::_getUSHORT($font, $offset);
  1261. $offset += 2;
  1262. $glyphIndex = TCPDF_STATIC::_getUSHORT($font, $offset);
  1263. $offset += 2;
  1264. if (!isset($subsetglyphs[$glyphIndex])) {
  1265. // add missing glyphs
  1266. $new_sga[$glyphIndex] = true;
  1267. }
  1268. // skip some bytes by case
  1269. if ($flags & 1) {
  1270. $offset += 4;
  1271. } else {
  1272. $offset += 2;
  1273. }
  1274. if ($flags & 8) {
  1275. $offset += 2;
  1276. } elseif ($flags & 64) {
  1277. $offset += 4;
  1278. } elseif ($flags & 128) {
  1279. $offset += 8;
  1280. }
  1281. } while ($flags & 32);
  1282. }
  1283. }
  1284. }
  1285. $subsetglyphs += $new_sga;
  1286. }
  1287. // sort glyphs by key (and remove duplicates)
  1288. ksort($subsetglyphs);
  1289. // build new glyf and loca tables
  1290. $glyf = '';
  1291. $loca = '';
  1292. $offset = 0;
  1293. $glyf_offset = $table['glyf']['offset'];
  1294. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  1295. if (isset($subsetglyphs[$i])) {
  1296. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  1297. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  1298. } else {
  1299. $length = 0;
  1300. }
  1301. if ($short_offset) {
  1302. $loca .= pack('n', floor($offset / 2));
  1303. } else {
  1304. $loca .= pack('N', $offset);
  1305. }
  1306. $offset += $length;
  1307. }
  1308. // array of table names to preserve (loca and glyf tables will be added later)
  1309. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  1310. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  1311. // get the tables to preserve
  1312. $offset = 12;
  1313. foreach ($table as $tag => $val) {
  1314. if (in_array($tag, $table_names)) {
  1315. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  1316. if ($tag == 'head') {
  1317. // set the checkSumAdjustment to 0
  1318. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  1319. }
  1320. $pad = 4 - ($table[$tag]['length'] % 4);
  1321. if ($pad != 4) {
  1322. // the length of a table must be a multiple of four bytes
  1323. $table[$tag]['length'] += $pad;
  1324. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  1325. }
  1326. $table[$tag]['offset'] = $offset;
  1327. $offset += $table[$tag]['length'];
  1328. // check sum is not changed (so keep the following line commented)
  1329. //$table[$tag]['checkSum'] = self::_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  1330. } else {
  1331. unset($table[$tag]);
  1332. }
  1333. }
  1334. // add loca
  1335. $table['loca']['data'] = $loca;
  1336. $table['loca']['length'] = strlen($loca);
  1337. $pad = 4 - ($table['loca']['length'] % 4);
  1338. if ($pad != 4) {
  1339. // the length of a table must be a multiple of four bytes
  1340. $table['loca']['length'] += $pad;
  1341. $table['loca']['data'] .= str_repeat("\x0", $pad);
  1342. }
  1343. $table['loca']['offset'] = $offset;
  1344. $table['loca']['checkSum'] = self::_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  1345. $offset += $table['loca']['length'];
  1346. // add glyf
  1347. $table['glyf']['data'] = $glyf;
  1348. $table['glyf']['length'] = strlen($glyf);
  1349. $pad = 4 - ($table['glyf']['length'] % 4);
  1350. if ($pad != 4) {
  1351. // the length of a table must be a multiple of four bytes
  1352. $table['glyf']['length'] += $pad;
  1353. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  1354. }
  1355. $table['glyf']['offset'] = $offset;
  1356. $table['glyf']['checkSum'] = self::_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  1357. // rebuild font
  1358. $font = '';
  1359. $font .= pack('N', 0x10000); // sfnt version
  1360. $numTables = count($table);
  1361. $font .= pack('n', $numTables); // numTables
  1362. $entrySelector = floor(log($numTables, 2));
  1363. $searchRange = pow(2, $entrySelector) * 16;
  1364. $rangeShift = ($numTables * 16) - $searchRange;
  1365. $font .= pack('n', $searchRange); // searchRange
  1366. $font .= pack('n', $entrySelector); // entrySelector
  1367. $font .= pack('n', $rangeShift); // rangeShift
  1368. $offset = ($numTables * 16);
  1369. foreach ($table as $tag => $data) {
  1370. $font .= $tag; // tag
  1371. $font .= pack('N', $data['checkSum']); // checkSum
  1372. $font .= pack('N', ($data['offset'] + $offset)); // offset
  1373. $font .= pack('N', $data['length']); // length
  1374. }
  1375. foreach ($table as $data) {
  1376. $font .= $data['data'];
  1377. }
  1378. // set checkSumAdjustment on head table
  1379. $checkSumAdjustment = 0xB1B0AFBA - self::_getTTFtableChecksum($font, strlen($font));
  1380. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  1381. return $font;
  1382. }
  1383. /**
  1384. * Outputs font widths
  1385. * @param array $font font data
  1386. * @param int $cidoffset offset for CID values
  1387. * @return string PDF command string for font widths
  1388. * @author Nicola Asuni
  1389. * @since 4.4.000 (2008-12-07)
  1390. * @public static
  1391. */
  1392. public static function _putfontwidths($font, $cidoffset=0) {
  1393. ksort($font['cw']);
  1394. $rangeid = 0;
  1395. $range = array();
  1396. $prevcid = -2;
  1397. $prevwidth = -1;
  1398. $interval = false;
  1399. // for each character
  1400. foreach ($font['cw'] as $cid => $width) {
  1401. $cid -= $cidoffset;
  1402. if ($font['subset'] AND (!isset($font['subsetchars'][$cid]))) {
  1403. // ignore the unused characters (font subsetting)
  1404. continue;
  1405. }
  1406. if ($width != $font['dw']) {
  1407. if ($cid == ($prevcid + 1)) {
  1408. // consecutive CID
  1409. if ($width == $prevwidth) {
  1410. if ($width == $range[$rangeid][0]) {
  1411. $range[$rangeid][] = $width;
  1412. } else {
  1413. array_pop($range[$rangeid]);
  1414. // new range
  1415. $rangeid = $prevcid;
  1416. $range[$rangeid] = array();
  1417. $range[$rangeid][] = $prevwidth;
  1418. $range[$rangeid][] = $width;
  1419. }
  1420. $interval = true;
  1421. $range[$rangeid]['interval'] = true;
  1422. } else {
  1423. if ($interval) {
  1424. // new range
  1425. $rangeid = $cid;
  1426. $range[$rangeid] = array();
  1427. $range[$rangeid][] = $width;
  1428. } else {
  1429. $range[$rangeid][] = $width;
  1430. }
  1431. $interval = false;
  1432. }
  1433. } else {
  1434. // new range
  1435. $rangeid = $cid;
  1436. $range[$rangeid] = array();
  1437. $range[$rangeid][] = $width;
  1438. $interval = false;
  1439. }
  1440. $prevcid = $cid;
  1441. $prevwidth = $width;
  1442. }
  1443. }
  1444. // optimize ranges
  1445. $prevk = -1;
  1446. $nextk = -1;
  1447. $prevint = false;
  1448. foreach ($range as $k => $ws) {
  1449. $cws = count($ws);
  1450. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  1451. if (isset($range[$k]['interval'])) {
  1452. unset($range[$k]['interval']);
  1453. }
  1454. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  1455. unset($range[$k]);
  1456. } else {
  1457. $prevk = $k;
  1458. }
  1459. $nextk = $k + $cws;
  1460. if (isset($ws['interval'])) {
  1461. if ($cws > 3) {
  1462. $prevint = true;
  1463. } else {
  1464. $prevint = false;
  1465. }
  1466. if (isset($range[$k]['interval'])) {
  1467. unset($range[$k]['interval']);
  1468. }
  1469. --$nextk;
  1470. } else {
  1471. $prevint = false;
  1472. }
  1473. }
  1474. // output data
  1475. $w = '';
  1476. foreach ($range as $k => $ws) {
  1477. if (count(array_count_values($ws)) == 1) {
  1478. // interval mode is more compact
  1479. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  1480. } else {
  1481. // range mode
  1482. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  1483. }
  1484. }
  1485. return '/W ['.$w.' ]';
  1486. }
  1487. /**
  1488. * Update the CIDToGIDMap string with a new value.
  1489. * @param string $map CIDToGIDMap.
  1490. * @param int $cid CID value.
  1491. * @param int $gid GID value.
  1492. * @return string CIDToGIDMap.
  1493. * @author Nicola Asuni
  1494. * @since 5.9.123 (2011-09-29)
  1495. * @public static
  1496. */
  1497. public static function updateCIDtoGIDmap($map, $cid, $gid) {
  1498. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  1499. if ($gid > 0xFFFF) {
  1500. $gid -= 0x10000;
  1501. }
  1502. $map[($cid * 2)] = chr($gid >> 8);
  1503. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  1504. }
  1505. return $map;
  1506. }
  1507. /**
  1508. * Return fonts path
  1509. * @return string
  1510. * @public static
  1511. */
  1512. public static function _getfontpath() {
  1513. if (!defined('K_PATH_FONTS') AND is_dir($fdir = realpath(dirname(__FILE__).'/../fonts'))) {
  1514. if (substr($fdir, -1) != '/') {
  1515. $fdir .= '/';
  1516. }
  1517. define('K_PATH_FONTS', $fdir);
  1518. }
  1519. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  1520. }
  1521. /**
  1522. * Return font full path
  1523. * @param string $file Font file name.
  1524. * @param string $fontdir Font directory (set to false fto search on default directories)
  1525. * @return string Font full path or empty string
  1526. * @author Nicola Asuni
  1527. * @since 6.0.025
  1528. * @public static
  1529. */
  1530. public static function getFontFullPath($file, $fontdir=false) {
  1531. $fontfile = '';
  1532. // search files on various directories
  1533. if (($fontdir !== false) AND @TCPDF_STATIC::file_exists($fontdir.$file)) {
  1534. $fontfile = $fontdir.$file;
  1535. } elseif (@TCPDF_STATIC::file_exists(self::_getfontpath().$file)) {
  1536. $fontfile = self::_getfontpath().$file;
  1537. } elseif (@TCPDF_STATIC::file_exists($file)) {
  1538. $fontfile = $file;
  1539. }
  1540. return $fontfile;
  1541. }
  1542. /**
  1543. * Get a reference font size.
  1544. * @param string $size String containing font size value.
  1545. * @param float $refsize Reference font size in points.
  1546. * @return float value in points
  1547. * @public static
  1548. */
  1549. public static function getFontRefSize($size, $refsize=12) {
  1550. switch ($size) {
  1551. case 'xx-small': {
  1552. $size = ($refsize - 4);
  1553. break;
  1554. }
  1555. case 'x-small': {
  1556. $size = ($refsize - 3);
  1557. break;
  1558. }
  1559. case 'small': {
  1560. $size = ($refsize - 2);
  1561. break;
  1562. }
  1563. case 'medium': {
  1564. $size = $refsize;
  1565. break;
  1566. }
  1567. case 'large': {
  1568. $size = ($refsize + 2);
  1569. break;
  1570. }
  1571. case 'x-large': {
  1572. $size = ($refsize + 4);
  1573. break;
  1574. }
  1575. case 'xx-large': {
  1576. $size = ($refsize + 6);
  1577. break;
  1578. }
  1579. case 'smaller': {
  1580. $size = ($refsize - 3);
  1581. break;
  1582. }
  1583. case 'larger': {
  1584. $size = ($refsize + 3);
  1585. break;
  1586. }
  1587. }
  1588. return $size;
  1589. }
  1590. // ====================================================================================================================
  1591. // REIMPLEMENTED
  1592. // ====================================================================================================================
  1593. /**
  1594. * Returns the unicode caracter specified by the value
  1595. * @param int $c UTF-8 value
  1596. * @param boolean $unicode True if we are in unicode mode, false otherwise.
  1597. * @return string Returns the specified character.
  1598. * @since 2.3.000 (2008-03-05)
  1599. * @public static
  1600. */
  1601. public static function unichr($c, $unicode=true) {
  1602. $c = intval($c);
  1603. if (!$unicode) {
  1604. return chr($c);
  1605. } elseif ($c <= 0x7F) {
  1606. // one byte
  1607. return chr($c);
  1608. } elseif ($c <= 0x7FF) {
  1609. // two bytes
  1610. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  1611. } elseif ($c <= 0xFFFF) {
  1612. // three bytes
  1613. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  1614. } elseif ($c <= 0x10FFFF) {
  1615. // four bytes
  1616. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  1617. } else {
  1618. return '';
  1619. }
  1620. }
  1621. /**
  1622. * Returns the unicode caracter specified by UTF-8 value
  1623. * @param int $c UTF-8 value
  1624. * @return string Returns the specified character.
  1625. * @public static
  1626. */
  1627. public static function unichrUnicode($c) {
  1628. return self::unichr($c, true);
  1629. }
  1630. /**
  1631. * Returns the unicode caracter specified by ASCII value
  1632. * @param int $c UTF-8 value
  1633. * @return string Returns the specified character.
  1634. * @public static
  1635. */
  1636. public static function unichrASCII($c) {
  1637. return self::unichr($c, false);
  1638. }
  1639. /**
  1640. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  1641. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  1642. * <pre>
  1643. * Encoding UTF-16:
  1644. *
  1645. * Encoding of a single character from an ISO 10646 character value to
  1646. * UTF-16 proceeds as follows. Let U be the character number, no greater
  1647. * than 0x10FFFF.
  1648. *
  1649. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  1650. * terminate.
  1651. *
  1652. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  1653. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  1654. * represented in 20 bits.
  1655. *
  1656. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  1657. * 0xDC00, respectively. These integers each have 10 bits free to
  1658. * encode the character value, for a total of 20 bits.
  1659. *
  1660. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  1661. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  1662. * bits of W2. Terminate.
  1663. *
  1664. * Graphically, steps 2 through 4 look like:
  1665. * U' = yyyyyyyyyyxxxxxxxxxx
  1666. * W1 = 110110yyyyyyyyyy
  1667. * W2 = 110111xxxxxxxxxx
  1668. * </pre>
  1669. * @param array $unicode array containing UTF-8 unicode values
  1670. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  1671. * @return string
  1672. * @protected
  1673. * @author Nicola Asuni
  1674. * @since 2.1.000 (2008-01-08)
  1675. * @public static
  1676. */
  1677. public static function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  1678. $outstr = ''; // string to be returned
  1679. if ($setbom) {
  1680. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  1681. }
  1682. foreach ($unicode as $char) {
  1683. if ($char == 0x200b) {
  1684. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  1685. } elseif ($char == 0xFFFD) {
  1686. $outstr .= "\xFF\xFD"; // replacement character
  1687. } elseif ($char < 0x10000) {
  1688. $outstr .= chr($char >> 0x08);
  1689. $outstr .= chr($char & 0xFF);
  1690. } else {
  1691. $char -= 0x10000;
  1692. $w1 = 0xD800 | ($char >> 0x0a);
  1693. $w2 = 0xDC00 | ($char & 0x3FF);
  1694. $outstr .= chr($w1 >> 0x08);
  1695. $outstr .= chr($w1 & 0xFF);
  1696. $outstr .= chr($w2 >> 0x08);
  1697. $outstr .= chr($w2 & 0xFF);
  1698. }
  1699. }
  1700. return $outstr;
  1701. }
  1702. /**
  1703. * Convert an array of UTF8 values to array of unicode characters
  1704. * @param array $ta The input array of UTF8 values.
  1705. * @param boolean $isunicode True for Unicode mode, false otherwise.
  1706. * @return array Return array of unicode characters
  1707. * @since 4.5.037 (2009-04-07)
  1708. * @public static
  1709. */
  1710. public static function UTF8ArrayToUniArray($ta, $isunicode=true) {
  1711. if ($isunicode) {
  1712. return array_map(array('TCPDF_FONTS', 'unichrUnicode'), $ta);
  1713. }
  1714. return array_map(array('TCPDF_FONTS', 'unichrASCII'), $ta);
  1715. }
  1716. /**
  1717. * Extract a slice of the $strarr array and return it as string.
  1718. * @param string[] $strarr The input array of characters.
  1719. * @param int $start the starting element of $strarr.
  1720. * @param int $end first element that will not be returned.
  1721. * @param boolean $unicode True if we are in unicode mode, false otherwise.
  1722. * @return string Return part of a string
  1723. * @public static
  1724. */
  1725. public static function UTF8ArrSubString($strarr, $start='', $end='', $unicode=true) {
  1726. if (strlen($start) == 0) {
  1727. $start = 0;
  1728. }
  1729. if (strlen($end) == 0) {
  1730. $end = count($strarr);
  1731. }
  1732. $string = '';
  1733. for ($i = $start; $i < $end; ++$i) {
  1734. $string .= self::unichr($strarr[$i], $unicode);
  1735. }
  1736. return $string;
  1737. }
  1738. /**
  1739. * Extract a slice of the $uniarr array and return it as string.
  1740. * @param string[] $uniarr The input array of characters.
  1741. * @param int $start the starting element of $strarr.
  1742. * @param int $end first element that will not be returned.
  1743. * @return string Return part of a string
  1744. * @since 4.5.037 (2009-04-07)
  1745. * @public static
  1746. */
  1747. public static function UniArrSubString($uniarr, $start='', $end='') {
  1748. if (strlen($start) == 0) {
  1749. $start = 0;
  1750. }
  1751. if (strlen($end) == 0) {
  1752. $end = count($uniarr);
  1753. }
  1754. $string = '';
  1755. for ($i=$start; $i < $end; ++$i) {
  1756. $string .= $uniarr[$i];
  1757. }
  1758. return $string;
  1759. }
  1760. /**
  1761. * Converts UTF-8 characters array to array of Latin1 characters array<br>
  1762. * @param array $unicode array containing UTF-8 unicode values
  1763. * @return array
  1764. * @author Nicola Asuni
  1765. * @since 4.8.023 (2010-01-15)
  1766. * @public static
  1767. */
  1768. public static function UTF8ArrToLatin1Arr($unicode) {
  1769. $outarr = array(); // array to be returned
  1770. foreach ($unicode as $char) {
  1771. if ($char < 256) {
  1772. $outarr[] = $char;
  1773. } elseif (array_key_exists($char, TCPDF_FONT_DATA::$uni_utf8tolatin)) {
  1774. // map from UTF-8
  1775. $outarr[] = TCPDF_FONT_DATA::$uni_utf8tolatin[$char];
  1776. } elseif ($char == 0xFFFD) {
  1777. // skip
  1778. } else {
  1779. $outarr[] = 63; // '?' character
  1780. }
  1781. }
  1782. return $outarr;
  1783. }
  1784. /**
  1785. * Converts UTF-8 characters array to Latin1 string<br>
  1786. * @param array $unicode array containing UTF-8 unicode values
  1787. * @return string
  1788. * @author Nicola Asuni
  1789. * @since 4.8.023 (2010-01-15)
  1790. * @public static
  1791. */
  1792. public static function UTF8ArrToLatin1($unicode) {
  1793. $outstr = ''; // string to be returned
  1794. foreach ($unicode as $char) {
  1795. if ($char < 256) {
  1796. $outstr .= chr($char);
  1797. } elseif (array_key_exists($char, TCPDF_FONT_DATA::$uni_utf8tolatin)) {
  1798. // map from UTF-8
  1799. $outstr .= chr(TCPDF_FONT_DATA::$uni_utf8tolatin[$char]);
  1800. } elseif ($char == 0xFFFD) {
  1801. // skip
  1802. } else {
  1803. $outstr .= '?';
  1804. }
  1805. }
  1806. return $outstr;
  1807. }
  1808. /**
  1809. * Converts UTF-8 character to integer value.<br>
  1810. * Uses the getUniord() method if the value is not cached.
  1811. * @param string $uch character string to process.
  1812. * @return int Unicode value
  1813. * @public static
  1814. */
  1815. public static function uniord($uch) {
  1816. if (!isset(self::$cache_uniord[$uch])) {
  1817. self::$cache_uniord[$uch] = self::getUniord($uch);
  1818. }
  1819. return self::$cache_uniord[$uch];
  1820. }
  1821. /**
  1822. * Converts UTF-8 character to integer value.<br>
  1823. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  1824. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  1825. * <pre>
  1826. * Char. number range | UTF-8 octet sequence
  1827. * (hexadecimal) | (binary)
  1828. * --------------------+-----------------------------------------------
  1829. * 0000 0000-0000 007F | 0xxxxxxx
  1830. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  1831. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  1832. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  1833. * ---------------------------------------------------------------------
  1834. *
  1835. * ABFN notation:
  1836. * ---------------------------------------------------------------------
  1837. * UTF8-octets = *( UTF8-char )
  1838. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  1839. * UTF8-1 = %x00-7F
  1840. * UTF8-2 = %xC2-DF UTF8-tail
  1841. *
  1842. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  1843. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  1844. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  1845. * %xF4 %x80-8F 2( UTF8-tail )
  1846. * UTF8-tail = %x80-BF
  1847. * ---------------------------------------------------------------------
  1848. * </pre>
  1849. * @param string $uch character string to process.
  1850. * @return int Unicode value
  1851. * @author Nicola Asuni
  1852. * @public static
  1853. */
  1854. public static function getUniord($uch) {
  1855. if (function_exists('mb_convert_encoding')) {
  1856. list(, $char) = @unpack('N', mb_convert_encoding($uch, 'UCS-4BE', 'UTF-8'));
  1857. if ($char >= 0) {
  1858. return $char;
  1859. }
  1860. }
  1861. $bytes = array(); // array containing single character byte sequences
  1862. $countbytes = 0;
  1863. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  1864. $length = strlen($uch);
  1865. for ($i = 0; $i < $length; ++$i) {
  1866. $char = ord($uch[$i]); // get one string character at time
  1867. if ($countbytes == 0) { // get starting octect
  1868. if ($char <= 0x7F) {
  1869. return $char; // use the character "as is" because is ASCII
  1870. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  1871. $bytes[] = ($char - 0xC0) << 0x06;
  1872. ++$countbytes;
  1873. $numbytes = 2;
  1874. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  1875. $bytes[] = ($char - 0xE0) << 0x0C;
  1876. ++$countbytes;
  1877. $numbytes = 3;
  1878. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  1879. $bytes[] = ($char - 0xF0) << 0x12;
  1880. ++$countbytes;
  1881. $numbytes = 4;
  1882. } else {
  1883. // use replacement character for other invalid sequences
  1884. return 0xFFFD;
  1885. }
  1886. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  1887. $bytes[] = $char - 0x80;
  1888. ++$countbytes;
  1889. if ($countbytes == $numbytes) {
  1890. // compose UTF-8 bytes to a single unicode value
  1891. $char = $bytes[0];
  1892. for ($j = 1; $j < $numbytes; ++$j) {
  1893. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  1894. }
  1895. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  1896. // The definition of UTF-8 prohibits encoding character numbers between
  1897. // U+D800 and U+DFFF, which are reserved for use with the UTF-16
  1898. // encoding form (as surrogate pairs) and do not directly represent
  1899. // characters.
  1900. return 0xFFFD; // use replacement character
  1901. } else {
  1902. return $char;
  1903. }
  1904. }
  1905. } else {
  1906. // use replacement character for other invalid sequences
  1907. return 0xFFFD;
  1908. }
  1909. }
  1910. return 0xFFFD;
  1911. }
  1912. /**
  1913. * Converts UTF-8 strings to codepoints array.<br>
  1914. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  1915. * @param string $str string to process.
  1916. * @param boolean $isunicode True when the documetn is in Unicode mode, false otherwise.
  1917. * @param array $currentfont Reference to current font array.
  1918. * @return array containing codepoints (UTF-8 characters values)
  1919. * @author Nicola Asuni
  1920. * @public static
  1921. */
  1922. public static function UTF8StringToArray($str, $isunicode, &$currentfont) {
  1923. if ($isunicode) {
  1924. // requires PCRE unicode support turned on
  1925. $chars = TCPDF_STATIC::pregSplit('//','u', $str, -1, PREG_SPLIT_NO_EMPTY);
  1926. $carr = array_map(array('TCPDF_FONTS', 'uniord'), $chars);
  1927. } else {
  1928. $chars = str_split($str);
  1929. $carr = array_map('ord', $chars);
  1930. }
  1931. if (is_array($currentfont['subsetchars']) && is_array($carr)) {
  1932. $currentfont['subsetchars'] += array_fill_keys($carr, true);
  1933. } else {
  1934. $currentfont['subsetchars'] = array_merge($currentfont['subsetchars'], $carr);
  1935. }
  1936. return $carr;
  1937. }
  1938. /**
  1939. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  1940. * @param string $str string to process.
  1941. * @param boolean $isunicode True when the documetn is in Unicode mode, false otherwise.
  1942. * @param array $currentfont Reference to current font array.
  1943. * @return string
  1944. * @since 3.2.000 (2008-06-23)
  1945. * @public static
  1946. */
  1947. public static function UTF8ToLatin1($str, $isunicode, &$currentfont) {
  1948. $unicode = self::UTF8StringToArray($str, $isunicode, $currentfont); // array containing UTF-8 unicode values
  1949. return self::UTF8ArrToLatin1($unicode);
  1950. }
  1951. /**
  1952. * Converts UTF-8 strings to UTF16-BE.<br>
  1953. * @param string $str string to process.
  1954. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  1955. * @param boolean $isunicode True when the documetn is in Unicode mode, false otherwise.
  1956. * @param array $currentfont Reference to current font array.
  1957. * @return string
  1958. * @author Nicola Asuni
  1959. * @since 1.53.0.TC005 (2005-01-05)
  1960. * @public static
  1961. */
  1962. public static function UTF8ToUTF16BE($str, $setbom, $isunicode, &$currentfont) {
  1963. if (!$isunicode) {
  1964. return $str; // string is not in unicode
  1965. }
  1966. $unicode = self::UTF8StringToArray($str, $isunicode, $currentfont); // array containing UTF-8 unicode values
  1967. return self::arrUTF8ToUTF16BE($unicode, $setbom);
  1968. }
  1969. /**
  1970. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  1971. * @param string $str string to manipulate.
  1972. * @param bool $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  1973. * @param bool $forcertl if true forces RTL text direction
  1974. * @param boolean $isunicode True if the document is in Unicode mode, false otherwise.
  1975. * @param array $currentfont Reference to current font array.
  1976. * @return string
  1977. * @author Nicola Asuni
  1978. * @since 2.1.000 (2008-01-08)
  1979. * @public static
  1980. */
  1981. public static function utf8StrRev($str, $setbom, $forcertl, $isunicode, &$currentfont) {
  1982. return self::utf8StrArrRev(self::UTF8StringToArray($str, $isunicode, $currentfont), $str, $setbom, $forcertl, $isunicode, $currentfont);
  1983. }
  1984. /**
  1985. * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  1986. * @param array $arr array of unicode values.
  1987. * @param string $str string to manipulate (or empty value).
  1988. * @param bool $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  1989. * @param bool $forcertl if true forces RTL text direction
  1990. * @param boolean $isunicode True if the document is in Unicode mode, false otherwise.
  1991. * @param array $currentfont Reference to current font array.
  1992. * @return string
  1993. * @author Nicola Asuni
  1994. * @since 4.9.000 (2010-03-27)
  1995. * @public static
  1996. */
  1997. public static function utf8StrArrRev($arr, $str, $setbom, $forcertl, $isunicode, &$currentfont) {
  1998. return self::arrUTF8ToUTF16BE(self::utf8Bidi($arr, $str, $forcertl, $isunicode, $currentfont), $setbom);
  1999. }
  2000. /**
  2001. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  2002. * @param array $ta array of characters composing the string.
  2003. * @param string $str string to process
  2004. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  2005. * @param boolean $isunicode True if the document is in Unicode mode, false otherwise.
  2006. * @param array $currentfont Reference to current font array.
  2007. * @return array of unicode chars
  2008. * @author Nicola Asuni
  2009. * @since 2.4.000 (2008-03-06)
  2010. * @public static
  2011. */
  2012. public static function utf8Bidi($ta, $str, $forcertl, $isunicode, &$currentfont) {
  2013. // paragraph embedding level
  2014. $pel = 0;
  2015. // max level
  2016. $maxlevel = 0;
  2017. if (TCPDF_STATIC::empty_string($str)) {
  2018. // create string from array
  2019. $str = self::UTF8ArrSubString($ta, '', '', $isunicode);
  2020. }
  2021. // check if string contains arabic text
  2022. if (preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_ARABIC, $str)) {
  2023. $arabic = true;
  2024. } else {
  2025. $arabic = false;
  2026. }
  2027. // check if string contains RTL text
  2028. if (!($forcertl OR $arabic OR preg_match(TCPDF_FONT_DATA::$uni_RE_PATTERN_RTL, $str))) {
  2029. return $ta;
  2030. }
  2031. // get number of chars
  2032. $numchars = count($ta);
  2033. if ($forcertl == 'R') {
  2034. $pel = 1;
  2035. } elseif ($forcertl == 'L') {
  2036. $pel = 0;
  2037. } else {
  2038. // P2. In each paragraph, find the first character of type L, AL, or R.
  2039. // 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.
  2040. for ($i=0; $i < $numchars; ++$i) {
  2041. $type = TCPDF_FONT_DATA::$uni_type[$ta[$i]];
  2042. if ($type == 'L') {
  2043. $pel = 0;
  2044. break;
  2045. } elseif (($type == 'AL') OR ($type == 'R')) {
  2046. $pel = 1;
  2047. break;
  2048. }
  2049. }
  2050. }
  2051. // Current Embedding Level
  2052. $cel = $pel;
  2053. // directional override status
  2054. $dos = 'N';
  2055. $remember = array();
  2056. // start-of-level-run
  2057. $sor = $pel % 2 ? 'R' : 'L';
  2058. $eor = $sor;
  2059. // Array of characters data
  2060. $chardata = Array();
  2061. // 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.
  2062. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  2063. for ($i=0; $i < $numchars; ++$i) {
  2064. if ($ta[$i] == TCPDF_FONT_DATA::$uni_RLE) {
  2065. // X2. With each RLE, compute the least greater odd embedding level.
  2066. // 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.
  2067. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2068. $next_level = $cel + ($cel % 2) + 1;
  2069. if ($next_level < 62) {
  2070. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_RLE, 'cel' => $cel, 'dos' => $dos);
  2071. $cel = $next_level;
  2072. $dos = 'N';
  2073. $sor = $eor;
  2074. $eor = $cel % 2 ? 'R' : 'L';
  2075. }
  2076. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_LRE) {
  2077. // X3. With each LRE, compute the least greater even embedding level.
  2078. // 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.
  2079. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2080. $next_level = $cel + 2 - ($cel % 2);
  2081. if ( $next_level < 62 ) {
  2082. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_LRE, 'cel' => $cel, 'dos' => $dos);
  2083. $cel = $next_level;
  2084. $dos = 'N';
  2085. $sor = $eor;
  2086. $eor = $cel % 2 ? 'R' : 'L';
  2087. }
  2088. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_RLO) {
  2089. // X4. With each RLO, compute the least greater odd embedding level.
  2090. // 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.
  2091. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2092. $next_level = $cel + ($cel % 2) + 1;
  2093. if ($next_level < 62) {
  2094. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_RLO, 'cel' => $cel, 'dos' => $dos);
  2095. $cel = $next_level;
  2096. $dos = 'R';
  2097. $sor = $eor;
  2098. $eor = $cel % 2 ? 'R' : 'L';
  2099. }
  2100. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_LRO) {
  2101. // X5. With each LRO, compute the least greater even embedding level.
  2102. // 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.
  2103. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  2104. $next_level = $cel + 2 - ($cel % 2);
  2105. if ( $next_level < 62 ) {
  2106. $remember[] = array('num' => TCPDF_FONT_DATA::$uni_LRO, 'cel' => $cel, 'dos' => $dos);
  2107. $cel = $next_level;
  2108. $dos = 'L';
  2109. $sor = $eor;
  2110. $eor = $cel % 2 ? 'R' : 'L';
  2111. }
  2112. } elseif ($ta[$i] == TCPDF_FONT_DATA::$uni_PDF) {
  2113. // 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.
  2114. if (count($remember)) {
  2115. $last = count($remember ) - 1;
  2116. if (($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_RLE) OR
  2117. ($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_LRE) OR
  2118. ($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_RLO) OR
  2119. ($remember[$last]['num'] == TCPDF_FONT_DATA::$uni_LRO)) {
  2120. $match = array_pop($remember);
  2121. $cel = $match['cel'];
  2122. $dos = $match['dos'];
  2123. $sor = $eor;
  2124. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  2125. }
  2126. }
  2127. } elseif (($ta[$i] != TCPDF_FONT_DATA::$uni_RLE) AND
  2128. ($ta[$i] != TCPDF_FONT_DATA::$uni_LRE) AND
  2129. ($ta[$i] != TCPDF_FONT_DATA::$uni_RLO) AND
  2130. ($ta[$i] != TCPDF_FONT_DATA::$uni_LRO) AND
  2131. ($ta[$i] != TCPDF_FONT_DATA::$uni_PDF)) {
  2132. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  2133. // a. Set the level of the current character to the current embedding level.
  2134. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  2135. if ($dos != 'N') {
  2136. $chardir = $dos;
  2137. } else {
  2138. if (isset(TCPDF_FONT_DATA::$uni_type[$ta[$i]])) {
  2139. $chardir = TCPDF_FONT_DATA::$uni_type[$ta[$i]];
  2140. } else {
  2141. $chardir = 'L';
  2142. }
  2143. }
  2144. // stores string characters and other information
  2145. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  2146. }
  2147. } // end for each char
  2148. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  2149. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  2150. // 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.
  2151. // 3.3.3 Resolving Weak Types
  2152. // 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.
  2153. // Nonspacing marks are now resolved based on the previous characters.
  2154. $numchars = count($chardata);
  2155. // 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.
  2156. $prevlevel = -1; // track level changes
  2157. $levcount = 0; // counts consecutive chars at the same level
  2158. for ($i=0; $i < $numchars; ++$i) {
  2159. if ($chardata[$i]['type'] == 'NSM') {
  2160. if ($levcount) {
  2161. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2162. } elseif ($i > 0) {
  2163. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  2164. }
  2165. }
  2166. if ($chardata[$i]['level'] != $prevlevel) {
  2167. $levcount = 0;
  2168. } else {
  2169. ++$levcount;
  2170. }
  2171. $prevlevel = $chardata[$i]['level'];
  2172. }
  2173. // 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.
  2174. $prevlevel = -1;
  2175. $levcount = 0;
  2176. for ($i=0; $i < $numchars; ++$i) {
  2177. if ($chardata[$i]['char'] == 'EN') {
  2178. for ($j=$levcount; $j >= 0; $j--) {
  2179. if ($chardata[$j]['type'] == 'AL') {
  2180. $chardata[$i]['type'] = 'AN';
  2181. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  2182. break;
  2183. }
  2184. }
  2185. }
  2186. if ($chardata[$i]['level'] != $prevlevel) {
  2187. $levcount = 0;
  2188. } else {
  2189. ++$levcount;
  2190. }
  2191. $prevlevel = $chardata[$i]['level'];
  2192. }
  2193. // W3. Change all ALs to R.
  2194. for ($i=0; $i < $numchars; ++$i) {
  2195. if ($chardata[$i]['type'] == 'AL') {
  2196. $chardata[$i]['type'] = 'R';
  2197. }
  2198. }
  2199. // 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.
  2200. $prevlevel = -1;
  2201. $levcount = 0;
  2202. for ($i=0; $i < $numchars; ++$i) {
  2203. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  2204. if (($chardata[$i]['type'] == 'ES') 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'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  2207. $chardata[$i]['type'] = 'EN';
  2208. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  2209. $chardata[$i]['type'] = 'AN';
  2210. }
  2211. }
  2212. if ($chardata[$i]['level'] != $prevlevel) {
  2213. $levcount = 0;
  2214. } else {
  2215. ++$levcount;
  2216. }
  2217. $prevlevel = $chardata[$i]['level'];
  2218. }
  2219. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  2220. $prevlevel = -1;
  2221. $levcount = 0;
  2222. for ($i=0; $i < $numchars; ++$i) {
  2223. if ($chardata[$i]['type'] == 'ET') {
  2224. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  2225. $chardata[$i]['type'] = 'EN';
  2226. } else {
  2227. $j = $i+1;
  2228. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  2229. if ($chardata[$j]['type'] == 'EN') {
  2230. $chardata[$i]['type'] = 'EN';
  2231. break;
  2232. } elseif ($chardata[$j]['type'] != 'ET') {
  2233. break;
  2234. }
  2235. ++$j;
  2236. }
  2237. }
  2238. }
  2239. if ($chardata[$i]['level'] != $prevlevel) {
  2240. $levcount = 0;
  2241. } else {
  2242. ++$levcount;
  2243. }
  2244. $prevlevel = $chardata[$i]['level'];
  2245. }
  2246. // W6. Otherwise, separators and terminators change to Other Neutral.
  2247. $prevlevel = -1;
  2248. $levcount = 0;
  2249. for ($i=0; $i < $numchars; ++$i) {
  2250. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  2251. $chardata[$i]['type'] = 'ON';
  2252. }
  2253. if ($chardata[$i]['level'] != $prevlevel) {
  2254. $levcount = 0;
  2255. } else {
  2256. ++$levcount;
  2257. }
  2258. $prevlevel = $chardata[$i]['level'];
  2259. }
  2260. //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.
  2261. $prevlevel = -1;
  2262. $levcount = 0;
  2263. for ($i=0; $i < $numchars; ++$i) {
  2264. if ($chardata[$i]['char'] == 'EN') {
  2265. for ($j=$levcount; $j >= 0; $j--) {
  2266. if ($chardata[$j]['type'] == 'L') {
  2267. $chardata[$i]['type'] = 'L';
  2268. } elseif ($chardata[$j]['type'] == 'R') {
  2269. break;
  2270. }
  2271. }
  2272. }
  2273. if ($chardata[$i]['level'] != $prevlevel) {
  2274. $levcount = 0;
  2275. } else {
  2276. ++$levcount;
  2277. }
  2278. $prevlevel = $chardata[$i]['level'];
  2279. }
  2280. // 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.
  2281. $prevlevel = -1;
  2282. $levcount = 0;
  2283. for ($i=0; $i < $numchars; ++$i) {
  2284. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  2285. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  2286. $chardata[$i]['type'] = 'L';
  2287. } elseif (($chardata[$i]['type'] == 'N') AND
  2288. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  2289. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  2290. $chardata[$i]['type'] = 'R';
  2291. } elseif ($chardata[$i]['type'] == 'N') {
  2292. // N2. Any remaining neutrals take the embedding direction
  2293. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2294. }
  2295. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  2296. // first char
  2297. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  2298. $chardata[$i]['type'] = 'L';
  2299. } elseif (($chardata[$i]['type'] == 'N') AND
  2300. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  2301. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  2302. $chardata[$i]['type'] = 'R';
  2303. } elseif ($chardata[$i]['type'] == 'N') {
  2304. // N2. Any remaining neutrals take the embedding direction
  2305. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2306. }
  2307. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  2308. //last char
  2309. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  2310. $chardata[$i]['type'] = 'L';
  2311. } elseif (($chardata[$i]['type'] == 'N') AND
  2312. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  2313. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  2314. $chardata[$i]['type'] = 'R';
  2315. } elseif ($chardata[$i]['type'] == 'N') {
  2316. // N2. Any remaining neutrals take the embedding direction
  2317. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2318. }
  2319. } elseif ($chardata[$i]['type'] == 'N') {
  2320. // N2. Any remaining neutrals take the embedding direction
  2321. $chardata[$i]['type'] = $chardata[$i]['sor'];
  2322. }
  2323. if ($chardata[$i]['level'] != $prevlevel) {
  2324. $levcount = 0;
  2325. } else {
  2326. ++$levcount;
  2327. }
  2328. $prevlevel = $chardata[$i]['level'];
  2329. }
  2330. // 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.
  2331. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  2332. for ($i=0; $i < $numchars; ++$i) {
  2333. $odd = $chardata[$i]['level'] % 2;
  2334. if ($odd) {
  2335. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  2336. $chardata[$i]['level'] += 1;
  2337. }
  2338. } else {
  2339. if ($chardata[$i]['type'] == 'R') {
  2340. $chardata[$i]['level'] += 1;
  2341. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  2342. $chardata[$i]['level'] += 2;
  2343. }
  2344. }
  2345. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  2346. }
  2347. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  2348. // 1. Segment separators,
  2349. // 2. Paragraph separators,
  2350. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  2351. // 4. Any sequence of white space characters at the end of the line.
  2352. for ($i=0; $i < $numchars; ++$i) {
  2353. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  2354. $chardata[$i]['level'] = $pel;
  2355. } elseif ($chardata[$i]['type'] == 'WS') {
  2356. $j = $i+1;
  2357. while ($j < $numchars) {
  2358. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  2359. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  2360. $chardata[$i]['level'] = $pel;
  2361. break;
  2362. } elseif ($chardata[$j]['type'] != 'WS') {
  2363. break;
  2364. }
  2365. ++$j;
  2366. }
  2367. }
  2368. }
  2369. // Arabic Shaping
  2370. // 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.
  2371. if ($arabic) {
  2372. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  2373. $alfletter = array(1570,1571,1573,1575);
  2374. $chardata2 = $chardata;
  2375. $laaletter = false;
  2376. $charAL = array();
  2377. $x = 0;
  2378. for ($i=0; $i < $numchars; ++$i) {
  2379. if ((TCPDF_FONT_DATA::$uni_type[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  2380. $charAL[$x] = $chardata[$i];
  2381. $charAL[$x]['i'] = $i;
  2382. $chardata[$i]['x'] = $x;
  2383. ++$x;
  2384. }
  2385. }
  2386. $numAL = $x;
  2387. for ($i=0; $i < $numchars; ++$i) {
  2388. $thischar = $chardata[$i];
  2389. if ($i > 0) {
  2390. $prevchar = $chardata[($i-1)];
  2391. } else {
  2392. $prevchar = false;
  2393. }
  2394. if (($i+1) < $numchars) {
  2395. $nextchar = $chardata[($i+1)];
  2396. } else {
  2397. $nextchar = false;
  2398. }
  2399. if (TCPDF_FONT_DATA::$uni_type[$thischar['char']] == 'AL') {
  2400. $x = $thischar['x'];
  2401. if ($x > 0) {
  2402. $prevchar = $charAL[($x-1)];
  2403. } else {
  2404. $prevchar = false;
  2405. }
  2406. if (($x+1) < $numAL) {
  2407. $nextchar = $charAL[($x+1)];
  2408. } else {
  2409. $nextchar = false;
  2410. }
  2411. // if laa letter
  2412. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  2413. $arabicarr = TCPDF_FONT_DATA::$uni_laa_array;
  2414. $laaletter = true;
  2415. if ($x > 1) {
  2416. $prevchar = $charAL[($x-2)];
  2417. } else {
  2418. $prevchar = false;
  2419. }
  2420. } else {
  2421. $arabicarr = TCPDF_FONT_DATA::$uni_arabicsubst;
  2422. $laaletter = false;
  2423. }
  2424. if (($prevchar !== false) AND ($nextchar !== false) AND
  2425. ((TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'NSM')) AND
  2426. ((TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'NSM')) AND
  2427. ($prevchar['type'] == $thischar['type']) AND
  2428. ($nextchar['type'] == $thischar['type']) AND
  2429. ($nextchar['char'] != 1567)) {
  2430. if (in_array($prevchar['char'], $endedletter)) {
  2431. if (isset($arabicarr[$thischar['char']][2])) {
  2432. // initial
  2433. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  2434. }
  2435. } else {
  2436. if (isset($arabicarr[$thischar['char']][3])) {
  2437. // medial
  2438. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  2439. }
  2440. }
  2441. } elseif (($nextchar !== false) AND
  2442. ((TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$nextchar['char']] == 'NSM')) AND
  2443. ($nextchar['type'] == $thischar['type']) AND
  2444. ($nextchar['char'] != 1567)) {
  2445. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  2446. // initial
  2447. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  2448. }
  2449. } elseif ((($prevchar !== false) AND
  2450. ((TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'AL') OR (TCPDF_FONT_DATA::$uni_type[$prevchar['char']] == 'NSM')) AND
  2451. ($prevchar['type'] == $thischar['type'])) OR
  2452. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  2453. // final
  2454. if (($i > 1) AND ($thischar['char'] == 1607) AND
  2455. ($chardata[$i-1]['char'] == 1604) AND
  2456. ($chardata[$i-2]['char'] == 1604)) {
  2457. //Allah Word
  2458. // mark characters to delete with false
  2459. $chardata2[$i-2]['char'] = false;
  2460. $chardata2[$i-1]['char'] = false;
  2461. $chardata2[$i]['char'] = 65010;
  2462. } else {
  2463. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  2464. if (isset($arabicarr[$thischar['char']][0])) {
  2465. // isolated
  2466. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  2467. }
  2468. } else {
  2469. if (isset($arabicarr[$thischar['char']][1])) {
  2470. // final
  2471. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  2472. }
  2473. }
  2474. }
  2475. } elseif (isset($arabicarr[$thischar['char']][0])) {
  2476. // isolated
  2477. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  2478. }
  2479. // if laa letter
  2480. if ($laaletter) {
  2481. // mark characters to delete with false
  2482. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  2483. }
  2484. } // end if AL (Arabic Letter)
  2485. } // end for each char
  2486. /*
  2487. * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
  2488. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  2489. */
  2490. for ($i = 0; $i < ($numchars-1); ++$i) {
  2491. if (($chardata2[$i]['char'] == 1617) AND (isset(TCPDF_FONT_DATA::$uni_diacritics[($chardata2[$i+1]['char'])]))) {
  2492. // check if the subtitution font is defined on current font
  2493. if (isset($currentfont['cw'][(TCPDF_FONT_DATA::$uni_diacritics[($chardata2[$i+1]['char'])])])) {
  2494. $chardata2[$i]['char'] = false;
  2495. $chardata2[$i+1]['char'] = TCPDF_FONT_DATA::$uni_diacritics[($chardata2[$i+1]['char'])];
  2496. }
  2497. }
  2498. }
  2499. // remove marked characters
  2500. foreach ($chardata2 as $key => $value) {
  2501. if ($value['char'] === false) {
  2502. unset($chardata2[$key]);
  2503. }
  2504. }
  2505. $chardata = array_values($chardata2);
  2506. $numchars = count($chardata);
  2507. unset($chardata2);
  2508. unset($arabicarr);
  2509. unset($laaletter);
  2510. unset($charAL);
  2511. }
  2512. // 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.
  2513. for ($j=$maxlevel; $j > 0; $j--) {
  2514. $ordarray = Array();
  2515. $revarr = Array();
  2516. $onlevel = false;
  2517. for ($i=0; $i < $numchars; ++$i) {
  2518. if ($chardata[$i]['level'] >= $j) {
  2519. $onlevel = true;
  2520. if (isset(TCPDF_FONT_DATA::$uni_mirror[$chardata[$i]['char']])) {
  2521. // 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.
  2522. $chardata[$i]['char'] = TCPDF_FONT_DATA::$uni_mirror[$chardata[$i]['char']];
  2523. }
  2524. $revarr[] = $chardata[$i];
  2525. } else {
  2526. if ($onlevel) {
  2527. $revarr = array_reverse($revarr);
  2528. $ordarray = array_merge($ordarray, $revarr);
  2529. $revarr = Array();
  2530. $onlevel = false;
  2531. }
  2532. $ordarray[] = $chardata[$i];
  2533. }
  2534. }
  2535. if ($onlevel) {
  2536. $revarr = array_reverse($revarr);
  2537. $ordarray = array_merge($ordarray, $revarr);
  2538. }
  2539. $chardata = $ordarray;
  2540. }
  2541. $ordarray = array();
  2542. foreach ($chardata as $cd) {
  2543. $ordarray[] = $cd['char'];
  2544. // store char values for subsetting
  2545. $currentfont['subsetchars'][$cd['char']] = true;
  2546. }
  2547. return $ordarray;
  2548. }
  2549. } // END OF TCPDF_FONTS CLASS
  2550. //============================================================+
  2551. // END OF FILE
  2552. //============================================================+