12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997 |
- <?php
- require_once __DIR__ . '/../MpdfException.php';
- if (!defined('_OTL_OLD_SPEC_COMPAT_2'))
- define("_OTL_OLD_SPEC_COMPAT_2", true);
- if (!defined('_TTF_MAC_HEADER'))
- define("_TTF_MAC_HEADER", false);
- if (!defined('_RECALC_PROFILE'))
- define("_RECALC_PROFILE", false);
- define("GF_WORDS", (1 << 0));
- define("GF_SCALE", (1 << 3));
- define("GF_MORE", (1 << 5));
- define("GF_XYSCALE", (1 << 6));
- define("GF_TWOBYTWO", (1 << 7));
- if (!function_exists('unicode_hex')) {
- function unicode_hex($unicode_dec)
- {
- return (sprintf("%05s", strtoupper(dechex($unicode_dec))));
- }
- }
- class TTFontFile
- {
- var $GPOSFeatures;
- var $GPOSLookups;
- var $GPOSScriptLang;
- var $MarkAttachmentType;
- var $MarkGlyphSets;
- var $GlyphClassMarks;
- var $GlyphClassLigatures;
- var $GlyphClassBases;
- var $GlyphClassComponents;
- var $GSUBScriptLang;
- var $rtlPUAstr;
-
- var $fontkey;
- var $useOTL;
- var $maxUni;
- var $sFamilyClass;
- var $sFamilySubClass;
- var $sipset;
- var $smpset;
- var $_pos;
- var $numTables;
- var $searchRange;
- var $entrySelector;
- var $rangeShift;
- var $tables;
- var $otables;
- var $filename;
- var $fh;
- var $glyphPos;
- var $charToGlyph;
- var $ascent;
- var $descent;
- var $lineGap;
- var $hheaascent;
- var $hheadescent;
- var $hhealineGap;
- var $advanceWidthMax;
- var $typoAscender;
- var $typoDescender;
- var $typoLineGap;
- var $usWinAscent;
- var $usWinDescent;
- var $strikeoutSize;
- var $strikeoutPosition;
- var $name;
- var $familyName;
- var $styleName;
- var $fullName;
- var $uniqueFontID;
- var $unitsPerEm;
- var $bbox;
- var $capHeight;
- var $xHeight;
- var $stemV;
- var $italicAngle;
- var $flags;
- var $underlinePosition;
- var $underlineThickness;
- var $charWidths;
- var $defaultWidth;
- var $maxStrLenRead;
- var $numTTCFonts;
- var $TTCFonts;
- var $maxUniChar;
- var $kerninfo;
- var $haskernGPOS;
- var $hassmallcapsGSUB;
- public function __construct()
- {
- $this->maxStrLenRead = 200000;
- }
- function getMetrics($file, $fontkey, $TTCfontID = 0, $debug = false, $BMPonly = false, $useOTL = 0)
- {
- $this->useOTL = $useOTL;
- $this->fontkey = $fontkey;
- $this->filename = $file;
- $this->fh = fopen($file, 'rb');
- if (!$this->fh) {
- throw new MpdfException('Can\'t open file ' . $file);
- }
- $this->_pos = 0;
- $this->charWidths = '';
- $this->glyphPos = array();
- $this->charToGlyph = array();
- $this->tables = array();
- $this->otables = array();
- $this->kerninfo = array();
- $this->haskernGPOS = array();
- $this->hassmallcapsGSUB = array();
- $this->ascent = 0;
- $this->descent = 0;
- $this->lineGap = 0;
- $this->hheaascent = 0;
- $this->hheadescent = 0;
- $this->hhealineGap = 0;
- $this->xHeight = 0;
- $this->capHeight = 0;
- $this->panose = array();
- $this->sFamilyClass = 0;
- $this->sFamilySubClass = 0;
- $this->typoAscender = 0;
- $this->typoDescender = 0;
- $this->typoLineGap = 0;
- $this->usWinAscent = 0;
- $this->usWinDescent = 0;
- $this->advanceWidthMax = 0;
- $this->strikeoutSize = 0;
- $this->strikeoutPosition = 0;
- $this->numTTCFonts = 0;
- $this->TTCFonts = array();
- $this->version = $version = $this->read_ulong();
- $this->panose = array();
- if ($version == 0x4F54544F) {
- throw new MpdfException("Postscript outlines are not supported");
- }
- if ($version == 0x74746366 && !$TTCfontID) {
- throw new MpdfException("ERROR - You must define the TTCfontID for a TrueType Collection in config_fonts.php (" . $file . ")");
- }
- if (!in_array($version, array(0x00010000, 0x74727565)) && !$TTCfontID) {
- throw new MpdfException("Not a TrueType font: version=" . $version);
- }
- if ($TTCfontID > 0) {
- $this->version = $version = $this->read_ulong();
- if (!in_array($version, array(0x00010000, 0x00020000))) {
- throw new MpdfException("ERROR - Error parsing TrueType Collection: version=" . $version . " - " . $file);
- }
- $this->numTTCFonts = $this->read_ulong();
- for ($i = 1; $i <= $this->numTTCFonts; $i++) {
- $this->TTCFonts[$i]['offset'] = $this->read_ulong();
- }
- $this->seek($this->TTCFonts[$TTCfontID]['offset']);
- $this->version = $version = $this->read_ulong();
- }
- $this->readTableDirectory($debug);
- $this->extractInfo($debug, $BMPonly, $useOTL);
- fclose($this->fh);
- }
- function readTableDirectory($debug = false)
- {
- $this->numTables = $this->read_ushort();
- $this->searchRange = $this->read_ushort();
- $this->entrySelector = $this->read_ushort();
- $this->rangeShift = $this->read_ushort();
- $this->tables = array();
- for ($i = 0; $i < $this->numTables; $i++) {
- $record = array();
- $record['tag'] = $this->read_tag();
- $record['checksum'] = array($this->read_ushort(), $this->read_ushort());
- $record['offset'] = $this->read_ulong();
- $record['length'] = $this->read_ulong();
- $this->tables[$record['tag']] = $record;
- }
- if ($debug)
- $this->checksumTables();
- }
- function checksumTables()
- {
-
- foreach ($this->tables AS $t) {
- if ($t['length'] > 0 && $t['length'] < $this->maxStrLenRead) {
- $table = $this->get_chunk($t['offset'], $t['length']);
- $checksum = $this->calcChecksum($table);
- if ($t['tag'] == 'head') {
- $up = unpack('n*', substr($table, 8, 4));
- $adjustment[0] = $up[1];
- $adjustment[1] = $up[2];
- $checksum = $this->sub32($checksum, $adjustment);
- }
- $xchecksum = $t['checksum'];
- if ($xchecksum != $checksum) {
- throw new MpdfException(sprintf('TTF file "%s": invalid checksum %s table: %s (expected %s)', $this->filename, dechex($checksum[0]) . dechex($checksum[1]), $t['tag'], dechex($xchecksum[0]) . dechex($xchecksum[1])));
- }
- }
- }
- }
- function sub32($x, $y)
- {
- $xlo = $x[1];
- $xhi = $x[0];
- $ylo = $y[1];
- $yhi = $y[0];
- if ($ylo > $xlo) {
- $xlo += 1 << 16;
- $yhi += 1;
- }
- $reslo = $xlo - $ylo;
- if ($yhi > $xhi) {
- $xhi += 1 << 16;
- }
- $reshi = $xhi - $yhi;
- $reshi = $reshi & 0xFFFF;
- return array($reshi, $reslo);
- }
- function calcChecksum($data)
- {
- if (strlen($data) % 4) {
- $data .= str_repeat("\0", (4 - (strlen($data) % 4)));
- }
- $len = strlen($data);
- $hi = 0x0000;
- $lo = 0x0000;
- for ($i = 0; $i < $len; $i+=4) {
- $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]);
- $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]);
- $hi += ($lo >> 16) & 0xFFFF;
- $lo = $lo & 0xFFFF;
- }
- $hi = $hi & 0xFFFF;
- return array($hi, $lo);
- }
- function get_table_pos($tag)
- {
- if (!isset($this->tables[$tag])) {
- return array(0, 0);
- }
- $offset = $this->tables[$tag]['offset'];
- $length = $this->tables[$tag]['length'];
- return array($offset, $length);
- }
- function seek($pos)
- {
- $this->_pos = $pos;
- fseek($this->fh, $this->_pos);
- }
- function skip($delta)
- {
- $this->_pos = $this->_pos + $delta;
- fseek($this->fh, $delta, SEEK_CUR);
- }
- function seek_table($tag, $offset_in_table = 0)
- {
- $tpos = $this->get_table_pos($tag);
- $this->_pos = $tpos[0] + $offset_in_table;
- fseek($this->fh, $this->_pos);
- return $this->_pos;
- }
- function read_tag()
- {
- $this->_pos += 4;
- return fread($this->fh, 4);
- }
- function read_short()
- {
- $this->_pos += 2;
- $s = fread($this->fh, 2);
- $a = (ord($s[0]) << 8) + ord($s[1]);
- if ($a & (1 << 15)) {
- $a = ($a - (1 << 16));
- }
- return $a;
- }
- function unpack_short($s)
- {
- $a = (ord($s[0]) << 8) + ord($s[1]);
- if ($a & (1 << 15)) {
- $a = ($a - (1 << 16));
- }
- return $a;
- }
- function read_ushort()
- {
- $this->_pos += 2;
- $s = fread($this->fh, 2);
- return (ord($s[0]) << 8) + ord($s[1]);
- }
- function read_ulong()
- {
- $this->_pos += 4;
- $s = fread($this->fh, 4);
-
- return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]);
- }
- function get_ushort($pos)
- {
- fseek($this->fh, $pos);
- $s = fread($this->fh, 2);
- return (ord($s[0]) << 8) + ord($s[1]);
- }
- function get_ulong($pos)
- {
- fseek($this->fh, $pos);
- $s = fread($this->fh, 4);
-
- return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]);
- }
- function pack_short($val)
- {
- if ($val < 0) {
- $val = abs($val);
- $val = ~$val;
- $val += 1;
- }
- return pack("n", $val);
- }
- function splice($stream, $offset, $value)
- {
- return substr($stream, 0, $offset) . $value . substr($stream, $offset + strlen($value));
- }
- function _set_ushort($stream, $offset, $value)
- {
- $up = pack("n", $value);
- return $this->splice($stream, $offset, $up);
- }
- function _set_short($stream, $offset, $val)
- {
- if ($val < 0) {
- $val = abs($val);
- $val = ~$val;
- $val += 1;
- }
- $up = pack("n", $val);
- return $this->splice($stream, $offset, $up);
- }
- function get_chunk($pos, $length)
- {
- fseek($this->fh, $pos);
- if ($length < 1) {
- return '';
- }
- return (fread($this->fh, $length));
- }
- function get_table($tag)
- {
- list($pos, $length) = $this->get_table_pos($tag);
- if ($length == 0) {
- return '';
- }
- fseek($this->fh, $pos);
- return (fread($this->fh, $length));
- }
- function add($tag, $data)
- {
- if ($tag == 'head') {
- $data = $this->splice($data, 8, "\0\0\0\0");
- }
- $this->otables[$tag] = $data;
- }
-
- function getCTG($file, $TTCfontID = 0, $debug = false, $useOTL = false)
- {
-
- $this->useOTL = $useOTL;
- $this->filename = $file;
- $this->fh = fopen($file, 'rb');
- if (!$this->fh) {
- throw new MpdfException('Can\'t open file ' . $file);
- }
- $this->_pos = 0;
- $this->charWidths = '';
- $this->glyphPos = array();
- $this->charToGlyph = array();
- $this->tables = array();
- $this->numTTCFonts = 0;
- $this->TTCFonts = array();
- $this->skip(4);
- if ($TTCfontID > 0) {
- $this->version = $version = $this->read_ulong();
- if (!in_array($version, array(0x00010000, 0x00020000))) {
- throw new MpdfException("ERROR - Error parsing TrueType Collection: version=" . $version . " - " . $file);
- }
- $this->numTTCFonts = $this->read_ulong();
- for ($i = 1; $i <= $this->numTTCFonts; $i++) {
- $this->TTCFonts[$i]['offset'] = $this->read_ulong();
- }
- $this->seek($this->TTCFonts[$TTCfontID]['offset']);
- $this->version = $version = $this->read_ulong();
- }
- $this->readTableDirectory($debug);
-
- $cmap_offset = $this->seek_table("cmap");
- $this->skip(2);
- $cmapTableCount = $this->read_ushort();
- $unicode_cmap_offset = 0;
- for ($i = 0; $i < $cmapTableCount; $i++) {
- $platformID = $this->read_ushort();
- $encodingID = $this->read_ushort();
- $offset = $this->read_ulong();
- $save_pos = $this->_pos;
- if ($platformID == 3 && $encodingID == 1) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 4) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- break;
- }
- } else if ($platformID == 0) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 4) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- break;
- }
- }
- $this->seek($save_pos);
- }
- $glyphToChar = array();
- $charToGlyph = array();
- $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph);
-
-
-
- if ($useOTL) {
- $this->seek_table("maxp");
- $this->skip(4);
- $numGlyphs = $this->read_ushort();
- $bctr = 0xE000;
- for ($gid = 1; $gid < $numGlyphs; $gid++) {
- if (!isset($glyphToChar[$gid])) {
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
- if ($bctr > 0xF8FF) {
- throw new MpdfException($file . " : WARNING - Font cannot map all included glyphs into Private Use Area U+E000 - U+F8FF; cannot use useOTL on this font");
- }
- $glyphToChar[$gid][] = $bctr;
- $charToGlyph[$bctr] = $gid;
- $bctr++;
- }
- }
- }
-
- fclose($this->fh);
- return ($charToGlyph);
- }
-
- function getTTCFonts($file)
- {
- $this->filename = $file;
- $this->fh = fopen($file, 'rb');
- if (!$this->fh) {
- return ('ERROR - Can\'t open file ' . $file);
- }
- $this->numTTCFonts = 0;
- $this->TTCFonts = array();
- $this->version = $version = $this->read_ulong();
- if ($version == 0x74746366) {
- $this->version = $version = $this->read_ulong();
- if (!in_array($version, array(0x00010000, 0x00020000)))
- return("ERROR - Error parsing TrueType Collection: version=" . $version . " - " . $file);
- }
- else {
- return("ERROR - Not a TrueType Collection: version=" . $version . " - " . $file);
- }
- $this->numTTCFonts = $this->read_ulong();
- for ($i = 1; $i <= $this->numTTCFonts; $i++) {
- $this->TTCFonts[$i]['offset'] = $this->read_ulong();
- }
- }
-
-
- function extractInfo($debug = false, $BMPonly = false, $useOTL = 0)
- {
-
-
-
-
- $name_offset = $this->seek_table("name");
- $format = $this->read_ushort();
- if ($format != 0 && $format != 1)
- throw new MpdfException("Unknown name table format " . $format);
- $numRecords = $this->read_ushort();
- $string_data_offset = $name_offset + $this->read_ushort();
- $names = array(1 => '', 2 => '', 3 => '', 4 => '', 6 => '');
- $K = array_keys($names);
- $nameCount = count($names);
- for ($i = 0; $i < $numRecords; $i++) {
- $platformId = $this->read_ushort();
- $encodingId = $this->read_ushort();
- $languageId = $this->read_ushort();
- $nameId = $this->read_ushort();
- $length = $this->read_ushort();
- $offset = $this->read_ushort();
- if (!in_array($nameId, $K))
- continue;
- $N = '';
- if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) {
- $opos = $this->_pos;
- $this->seek($string_data_offset + $offset);
- if ($length % 2 != 0)
- throw new MpdfException("PostScript name is UTF-16BE string of odd length");
- $length /= 2;
- $N = '';
- while ($length > 0) {
- $char = $this->read_ushort();
- $N .= (chr($char));
- $length -= 1;
- }
- $this->_pos = $opos;
- $this->seek($opos);
- } else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) {
- $opos = $this->_pos;
- $N = $this->get_chunk($string_data_offset + $offset, $length);
- $this->_pos = $opos;
- $this->seek($opos);
- }
- if ($N && $names[$nameId] == '') {
- $names[$nameId] = $N;
- $nameCount -= 1;
- if ($nameCount == 0)
- break;
- }
- }
- if ($names[6])
- $psName = $names[6];
- else if ($names[4])
- $psName = preg_replace('/ /', '-', $names[4]);
- else if ($names[1])
- $psName = preg_replace('/ /', '-', $names[1]);
- else
- $psName = '';
- if (!$psName)
- throw new MpdfException("Could not find PostScript font name: " . $this->filename);
-
- $psNameInvalid = false;
- for ($i = 0; $i < count($psName); $i++) {
- $c = $psName[$i];
- $oc = ord($c);
- if ($oc > 126 || strpos(' [](){}<>/%', $c) !== false) {
-
- $psNameInvalid = true;
- break;
- }
- }
- if ($psNameInvalid && $names[4])
- $psName = preg_replace('/ /', '-', $names[4]);
- $this->name = $psName;
- if ($names[1]) {
- $this->familyName = $names[1];
- } else {
- $this->familyName = $psName;
- }
- if ($names[2]) {
- $this->styleName = $names[2];
- } else {
- $this->styleName = 'Regular';
- }
- if ($names[4]) {
- $this->fullName = $names[4];
- } else {
- $this->fullName = $psName;
- }
- if ($names[3]) {
- $this->uniqueFontID = $names[3];
- } else {
- $this->uniqueFontID = $psName;
- }
- if (!$psNameInvalid && $names[6]) {
- $this->fullName = $names[6];
- }
-
-
-
- $this->seek_table("head");
- if ($debug) {
- $ver_maj = $this->read_ushort();
- $ver_min = $this->read_ushort();
- if ($ver_maj != 1)
- throw new MpdfException('Unknown head table version ' . $ver_maj . '.' . $ver_min);
- $this->fontRevision = $this->read_ushort() . $this->read_ushort();
- $this->skip(4);
- $magic = $this->read_ulong();
- if ($magic != 0x5F0F3CF5)
- throw new MpdfException('Invalid head table magic ' . $magic);
- $this->skip(2);
- }
- else {
- $this->skip(18);
- }
- $this->unitsPerEm = $unitsPerEm = $this->read_ushort();
- $scale = 1000 / $unitsPerEm;
- $this->skip(16);
- $xMin = $this->read_short();
- $yMin = $this->read_short();
- $xMax = $this->read_short();
- $yMax = $this->read_short();
- $this->bbox = array(($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale));
- $this->skip(3 * 2);
- $indexToLocFormat = $this->read_ushort();
- $glyphDataFormat = $this->read_ushort();
- if ($glyphDataFormat != 0) {
- throw new MpdfException('Unknown glyph data format ' . $glyphDataFormat);
- }
-
-
-
- if (isset($this->tables["hhea"])) {
- $this->seek_table("hhea");
- $this->skip(4);
- $hheaAscender = $this->read_short();
- $hheaDescender = $this->read_short();
- $hheaLineGap = $this->read_short();
- $hheaAdvanceWidthMax = $this->read_ushort();
- $this->hheaascent = ($hheaAscender * $scale);
- $this->hheadescent = ($hheaDescender * $scale);
- $this->hhealineGap = ($hheaLineGap * $scale);
- $this->advanceWidthMax = ($hheaAdvanceWidthMax * $scale);
- }
-
-
-
- $use_typo_metrics = false;
- if (isset($this->tables["OS/2"])) {
- $this->seek_table("OS/2");
- $version = $this->read_ushort();
- $this->skip(2);
- $usWeightClass = $this->read_ushort();
- $this->skip(2);
- $fsType = $this->read_ushort();
- if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) {
- $this->restrictedUse = true;
- }
-
- $this->skip(16);
- $yStrikeoutSize = $this->read_short();
- $yStrikeoutPosition = $this->read_short();
- $this->strikeoutSize = ($yStrikeoutSize * $scale);
- $this->strikeoutPosition = ($yStrikeoutPosition * $scale);
- $sF = $this->read_short();
- $this->sFamilyClass = ($sF >> 8);
- $this->sFamilySubClass = ($sF & 0xFF);
- $this->_pos += 10;
- $panose = fread($this->fh, 10);
- $this->panose = array();
- for ($p = 0; $p < strlen($panose); $p++) {
- $this->panose[] = ord($panose[$p]);
- }
-
-
- $this->skip(20);
- $fsSelection = $this->read_ushort();
- $use_typo_metrics = (($fsSelection & 0x80) == 0x80);
- $this->skip(4);
- $sTypoAscender = $this->read_short();
- $sTypoDescender = $this->read_short();
- $sTypoLineGap = $this->read_short();
- if ($sTypoAscender)
- $this->typoAscender = ($sTypoAscender * $scale);
- if ($sTypoDescender)
- $this->typoDescender = ($sTypoDescender * $scale);
- if ($sTypoLineGap)
- $this->typoLineGap = ($sTypoLineGap * $scale);
- $usWinAscent = $this->read_ushort();
- $usWinDescent = $this->read_ushort();
- if ($usWinAscent)
- $this->usWinAscent = ($usWinAscent * $scale);
- if ($usWinDescent)
- $this->usWinDescent = ($usWinDescent * $scale);
- if ($version > 1) {
- $this->skip(8);
- $sxHeight = $this->read_short();
- $this->xHeight = ($sxHeight * $scale);
- $sCapHeight = $this->read_short();
- $this->capHeight = ($sCapHeight * $scale);
- }
- } else {
- $usWeightClass = 400;
- }
- $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2));
-
- if (_FONT_DESCRIPTOR == 'winTypo') {
- $this->ascent = $this->typoAscender;
- $this->descent = $this->typoDescender;
- $this->lineGap = $this->typoLineGap;
- } else if (_FONT_DESCRIPTOR == 'mac') {
- $this->ascent = $this->hheaascent;
- $this->descent = $this->hheadescent;
- $this->lineGap = $this->hhealineGap;
- } else {
- $this->ascent = $this->usWinAscent;
- $this->descent = -$this->usWinDescent;
- $this->lineGap = 0;
-
- if ($this->ascent > $this->bbox[3]) {
- $this->ascent = $this->bbox[3];
- }
- if ($this->descent < $this->bbox[1]) {
- $this->descent = $this->bbox[1];
- }
-
- if ($use_typo_metrics && $this->typoAscender) {
- $this->ascent = $this->typoAscender;
- $this->descent = $this->typoDescender;
- $this->lineGap = $this->typoLineGap;
- }
- }
-
-
-
- $this->seek_table("post");
- if ($debug) {
- $ver_maj = $this->read_ushort();
- $ver_min = $this->read_ushort();
- if ($ver_maj < 1 || $ver_maj > 4)
- throw new MpdfException('Unknown post table version ' . $ver_maj);
- }
- else {
- $this->skip(4);
- }
- $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0;
- $this->underlinePosition = $this->read_short() * $scale;
- $this->underlineThickness = $this->read_short() * $scale;
- $isFixedPitch = $this->read_ulong();
- $this->flags = 4;
- if ($this->italicAngle != 0)
- $this->flags = $this->flags | 64;
- if ($usWeightClass >= 600)
- $this->flags = $this->flags | 262144;
- if ($isFixedPitch)
- $this->flags = $this->flags | 1;
-
-
-
- $this->seek_table("hhea");
- if ($debug) {
- $ver_maj = $this->read_ushort();
- $ver_min = $this->read_ushort();
- if ($ver_maj != 1)
- throw new MpdfException('Unknown hhea table version ' . $ver_maj);
- $this->skip(28);
- }
- else {
- $this->skip(32);
- }
- $metricDataFormat = $this->read_ushort();
- if ($metricDataFormat != 0) {
- throw new MpdfException('Unknown horizontal metric data format ' . $metricDataFormat);
- }
- $numberOfHMetrics = $this->read_ushort();
- if ($numberOfHMetrics == 0) {
- throw new MpdfException('Number of horizontal metrics is 0');
- }
-
-
-
- $this->seek_table("maxp");
- if ($debug) {
- $ver_maj = $this->read_ushort();
- $ver_min = $this->read_ushort();
- if ($ver_maj != 1) {
- throw new MpdfException('Unknown maxp table version ' . $ver_maj);
- }
- }
- else {
- $this->skip(4);
- }
- $numGlyphs = $this->read_ushort();
-
-
-
- $cmap_offset = $this->seek_table("cmap");
- $this->skip(2);
- $cmapTableCount = $this->read_ushort();
- $unicode_cmap_offset = 0;
- for ($i = 0; $i < $cmapTableCount; $i++) {
- $platformID = $this->read_ushort();
- $encodingID = $this->read_ushort();
- $offset = $this->read_ulong();
- $save_pos = $this->_pos;
- if (($platformID == 3 && $encodingID == 1) || $platformID == 0) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 4) {
- if (!$unicode_cmap_offset)
- $unicode_cmap_offset = $cmap_offset + $offset;
- if ($BMPonly)
- break;
- }
- }
-
- else if ((($platformID == 3 && $encodingID == 10) || $platformID == 0) && !$BMPonly) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 12) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- break;
- }
- }
- $this->seek($save_pos);
- }
- if (!$unicode_cmap_offset) {
- throw new MpdfException('Font (' . $this->filename . ') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)');
- }
- $sipset = false;
- $smpset = false;
-
- $this->rtlPUAstr = '';
-
- $this->GSUBScriptLang = array();
- $this->GSUBFeatures = array();
- $this->GSUBLookups = array();
- $this->GPOSScriptLang = array();
- $this->GPOSFeatures = array();
- $this->GPOSLookups = array();
- $this->glyphIDtoUni = '';
-
- if ($format == 12 && !$BMPonly) {
- $this->maxUniChar = 0;
- $this->seek($unicode_cmap_offset + 4);
- $length = $this->read_ulong();
- $limit = $unicode_cmap_offset + $length;
- $this->skip(4);
- $nGroups = $this->read_ulong();
- $glyphToChar = array();
- $charToGlyph = array();
- for ($i = 0; $i < $nGroups; $i++) {
- $startCharCode = $this->read_ulong();
- $endCharCode = $this->read_ulong();
- $startGlyphCode = $this->read_ulong();
-
- if ($endCharCode > 0x20000 && $endCharCode < 0x2FFFF) {
- $sipset = true;
- } else if ($endCharCode > 0x10000 && $endCharCode < 0x1FFFF) {
- $smpset = true;
- }
- $offset = 0;
- for ($unichar = $startCharCode; $unichar <= $endCharCode; $unichar++) {
- $glyph = $startGlyphCode + $offset;
- $offset++;
-
- if ($unichar < 0x30000) {
- $charToGlyph[$unichar] = $glyph;
- $this->maxUniChar = max($unichar, $this->maxUniChar);
- $glyphToChar[$glyph][] = $unichar;
- }
- }
- }
- } else {
- $glyphToChar = array();
- $charToGlyph = array();
- $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph);
- }
- $this->sipset = $sipset;
- $this->smpset = $smpset;
-
-
-
- if ($this->useOTL) {
- $bctr = 0xE000;
- for ($gid = 1; $gid < $numGlyphs; $gid++) {
- if (!isset($glyphToChar[$gid])) {
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
-
- if (($bctr > 0xF8FF) && ($bctr < 0x2CEB0)) {
- if (!$BMPonly) {
- $bctr = 0x2CEB0;
- $this->sipset = $sipset = true;
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
- } else {
- throw new MpdfException($names[1] . " : WARNING - The font does not have enough space to map all (unmapped) included glyphs into Private Use Area U+E000 - U+F8FF");
- }
- }
- $glyphToChar[$gid][] = $bctr;
- $charToGlyph[$bctr] = $gid;
- $this->maxUniChar = max($bctr, $this->maxUniChar);
- $bctr++;
- }
- }
- }
- $this->glyphToChar = $glyphToChar;
-
-
- $this->GSUBScriptLang = array();
- $this->rtlPUAstr = '';
-
- if ($useOTL) {
- $this->_getGDEFtables();
- list($this->GSUBScriptLang, $this->GSUBFeatures, $this->GSUBLookups, $this->rtlPUAstr) = $this->_getGSUBtables();
-
- list($this->GPOSScriptLang, $this->GPOSFeatures, $this->GPOSLookups) = $this->_getGPOStables();
- $this->glyphIDtoUni = str_pad('', 256 * 256 * 3, "\x00");
- foreach ($glyphToChar AS $gid => $arr) {
- if (isset($glyphToChar[$gid][0])) {
- $char = $glyphToChar[$gid][0];
- if ($char != 0 && $char != 65535) {
- $this->glyphIDtoUni[$gid * 3] = chr($char >> 16);
- $this->glyphIDtoUni[$gid * 3 + 1] = chr(($char >> 8) & 0xFF);
- $this->glyphIDtoUni[$gid * 3 + 2] = chr($char & 0xFF);
- }
- }
- }
- }
-
-
-
- if ($this->xHeight == 0) {
- if (isset($charToGlyph[0x78])) {
- $gidx = $charToGlyph[0x78];
- $start = $this->seek_table('loca');
- if ($indexToLocFormat == 0) {
- $this->skip($gidx * 2);
- $locax = $this->read_ushort() * 2;
- } else if ($indexToLocFormat == 1) {
- $this->skip($gidx * 4);
- $locax = $this->read_ulong();
- }
- $start = $this->seek_table('glyf');
- $this->skip($locax);
- $this->skip(8);
- $yMaxx = $this->read_short();
- $this->xHeight = $yMaxx * $scale;
- }
- }
- if ($this->capHeight == 0) {
- if (isset($charToGlyph[0x48])) {
- $gidH = $charToGlyph[0x48];
- $start = $this->seek_table('loca');
- if ($indexToLocFormat == 0) {
- $this->skip($gidH * 2);
- $locaH = $this->read_ushort() * 2;
- } else if ($indexToLocFormat == 1) {
- $this->skip($gidH * 4);
- $locaH = $this->read_ulong();
- }
- $start = $this->seek_table('glyf');
- $this->skip($locaH);
- $this->skip(8);
- $yMaxH = $this->read_short();
- $this->capHeight = $yMaxH * $scale;
- } else {
- $this->capHeight = $this->ascent;
- }
- }
-
-
-
- $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale);
-
-
-
-
- $kern_offset = $this->seek_table("kern");
- $version = $this->read_ushort();
- $nTables = $this->read_ushort();
-
- $sversion = $this->read_ushort();
- $slength = $this->read_ushort();
- $scoverage = $this->read_ushort();
- $format = $scoverage >> 8;
- if ($kern_offset && $version == 0 && $format == 0) {
-
- $nPairs = $this->read_ushort();
- $this->skip(6);
- for ($i = 0; $i < $nPairs; $i++) {
- $left = $this->read_ushort();
- $right = $this->read_ushort();
- $val = $this->read_short();
- if (count($glyphToChar[$left]) == 1 && count($glyphToChar[$right]) == 1) {
- if ($left != 32 && $right != 32) {
- $this->kerninfo[$glyphToChar[$left][0]][$glyphToChar[$right][0]] = intval($val * $scale);
- }
- }
- }
- }
- }
-
- function _getGDEFtables()
- {
-
-
-
-
- if (isset($this->tables["GDEF"])) {
- $gdef_offset = $this->seek_table("GDEF");
-
- $ver_maj = $this->read_ushort();
- $ver_min = $this->read_ushort();
- $GlyphClassDef_offset = $this->read_ushort();
- $AttachList_offset = $this->read_ushort();
- $LigCaretList_offset = $this->read_ushort();
- $MarkAttachClassDef_offset = $this->read_ushort();
-
- if ($ver_min == 2) {
- $MarkGlyphSetsDef_offset = $this->read_ushort();
- }
-
- if ($GlyphClassDef_offset) {
- $this->seek($gdef_offset + $GlyphClassDef_offset);
-
- $GlyphByClass = $this->_getClassDefinitionTable();
- } else {
- $GlyphByClass = array();
- }
- if (isset($GlyphByClass[1]) && count($GlyphByClass[1]) > 0) {
- $this->GlyphClassBases = ' ' . implode('| ', $GlyphByClass[1]);
- } else {
- $this->GlyphClassBases = '';
- }
- if (isset($GlyphByClass[2]) && count($GlyphByClass[2]) > 0) {
- $this->GlyphClassLigatures = ' ' . implode('| ', $GlyphByClass[2]);
- } else {
- $this->GlyphClassLigatures = '';
- }
- if (isset($GlyphByClass[3]) && count($GlyphByClass[3]) > 0) {
- $this->GlyphClassMarks = ' ' . implode('| ', $GlyphByClass[3]);
- } else {
- $this->GlyphClassMarks = '';
- }
- if (isset($GlyphByClass[4]) && count($GlyphByClass[4]) > 0) {
- $this->GlyphClassComponents = ' ' . implode('| ', $GlyphByClass[4]);
- } else {
- $this->GlyphClassComponents = '';
- }
- if (isset($GlyphByClass[3]) && count($GlyphByClass[3]) > 0) {
- $Marks = $GlyphByClass[3];
- }
- else {
- $Marks = array();
- }
-
-
-
-
-
- if ($MarkAttachClassDef_offset) {
- $this->seek($gdef_offset + $MarkAttachClassDef_offset);
- $MarkAttachmentTypes = $this->_getClassDefinitionTable();
- foreach ($MarkAttachmentTypes AS $class => $glyphs) {
- if (is_array($Marks) && count($Marks)) {
- $mat = array_diff($Marks, $MarkAttachmentTypes[$class]);
- sort($mat, SORT_STRING);
- } else {
- $mat = array();
- }
- $this->MarkAttachmentType[$class] = ' ' . implode('| ', $mat);
- }
- } else {
- $this->MarkAttachmentType = array();
- }
-
- if ($ver_min == 2 && $MarkGlyphSetsDef_offset) {
- $this->seek($gdef_offset + $MarkGlyphSetsDef_offset);
- $MarkSetTableFormat = $this->read_ushort();
- $MarkSetCount = $this->read_ushort();
- $MarkSetOffset = array();
- for ($i = 0; $i < $MarkSetCount; $i++) {
- $MarkSetOffset[] = $this->read_ulong();
- }
- for ($i = 0; $i < $MarkSetCount; $i++) {
- $this->seek($MarkSetOffset[$i]);
- $glyphs = $this->_getCoverage();
- $this->MarkGlyphSets[$i] = ' ' . implode('| ', $glyphs);
- }
- } else {
- $this->MarkGlyphSets = array();
- }
- } else {
- throw new MpdfException('Warning - You cannot set this font (' . $this->filename . ') to use OTL, as it does not include OTL tables (or at least, not a GDEF table).');
- }
-
-
-
- $GSUB_offset = 0;
- $GPOS_offset = 0;
- $GSUB_length = 0;
- $s = '';
- if (isset($this->tables["GSUB"])) {
- $GSUB_offset = $this->seek_table("GSUB");
- $GSUB_length = $this->tables["GSUB"]['length'];
- $s .= fread($this->fh, $this->tables["GSUB"]['length']);
- }
- if (isset($this->tables["GPOS"])) {
- $GPOS_offset = $this->seek_table("GPOS");
- $s .= fread($this->fh, $this->tables["GPOS"]['length']);
- }
- if ($s)
- file_put_contents(_MPDF_TTFONTDATAPATH . $this->fontkey . '.GSUBGPOStables.dat', $s);
-
-
- $s = '<?php
- $GSUB_offset = ' . $GSUB_offset . ';
- $GPOS_offset = ' . $GPOS_offset . ';
- $GSUB_length = ' . $GSUB_length . ';
- $GlyphClassBases = \'' . $this->GlyphClassBases . '\';
- $GlyphClassMarks = \'' . $this->GlyphClassMarks . '\';
- $GlyphClassLigatures = \'' . $this->GlyphClassLigatures . '\';
- $GlyphClassComponents = \'' . $this->GlyphClassComponents . '\';
- $MarkGlyphSets = ' . var_export($this->MarkGlyphSets, true) . ';
- $MarkAttachmentType = ' . var_export($this->MarkAttachmentType, true) . ';
- ?>';
- file_put_contents(_MPDF_TTFONTDATAPATH . $this->fontkey . '.GDEFdata.php', $s);
-
- }
- function _getClassDefinitionTable()
- {
-
- $ClassFormat = $this->read_ushort();
- $GlyphByClass = array();
- if ($ClassFormat == 1) {
- $StartGlyph = $this->read_ushort();
- $GlyphCount = $this->read_ushort();
- for ($i = 0; $i < $GlyphCount; $i++) {
- $gid = $StartGlyph + $i;
- $class = $this->read_ushort();
-
-
-
- if (isset($this->glyphToChar[$gid][0])) {
- $GlyphByClass[$class][] = unicode_hex($this->glyphToChar[$gid][0]);
- }
- }
- } else if ($ClassFormat == 2) {
- $tableCount = $this->read_ushort();
- for ($i = 0; $i < $tableCount; $i++) {
- $startGlyphID = $this->read_ushort();
- $endGlyphID = $this->read_ushort();
- $class = $this->read_ushort();
- for ($gid = $startGlyphID; $gid <= $endGlyphID; $gid++) {
- if (isset($this->glyphToChar[$gid][0])) {
- $GlyphByClass[$class][] = unicode_hex($this->glyphToChar[$gid][0]);
- }
- }
- }
- }
- foreach ($GlyphByClass AS $class => $glyphs) {
- sort($GlyphByClass[$class], SORT_STRING);
- }
- ksort($GlyphByClass);
- return $GlyphByClass;
- }
- function _getGSUBtables()
- {
-
-
-
- if (isset($this->tables["GSUB"])) {
- $ffeats = array();
- $gsub_offset = $this->seek_table("GSUB");
- $this->skip(4);
- $ScriptList_offset = $gsub_offset + $this->read_ushort();
- $FeatureList_offset = $gsub_offset + $this->read_ushort();
- $LookupList_offset = $gsub_offset + $this->read_ushort();
-
- $this->seek($ScriptList_offset);
- $ScriptCount = $this->read_ushort();
- for ($i = 0; $i < $ScriptCount; $i++) {
- $ScriptTag = $this->read_tag();
- $ScriptTableOffset = $this->read_ushort();
- $ffeats[$ScriptTag] = $ScriptList_offset + $ScriptTableOffset;
- }
-
- foreach ($ffeats AS $t => $o) {
- $ls = array();
- $this->seek($o);
- $DefLangSys_offset = $this->read_ushort();
- if ($DefLangSys_offset > 0) {
- $ls['DFLT'] = $DefLangSys_offset + $o;
- }
- $LangSysCount = $this->read_ushort();
- for ($i = 0; $i < $LangSysCount; $i++) {
- $LangTag = $this->read_tag();
- $LangTableOffset = $this->read_ushort();
- $ls[$LangTag] = $o + $LangTableOffset;
- }
- $ffeats[$t] = $ls;
- }
-
-
- foreach ($ffeats AS $st => $scripts) {
- foreach ($scripts AS $t => $o) {
- $FeatureIndex = array();
- $langsystable_offset = $o;
- $this->seek($langsystable_offset);
- $LookUpOrder = $this->read_ushort();
- $ReqFeatureIndex = $this->read_ushort();
- if ($ReqFeatureIndex != 0xFFFF) {
- $FeatureIndex[] = $ReqFeatureIndex;
- }
- $FeatureCount = $this->read_ushort();
- for ($i = 0; $i < $FeatureCount; $i++) {
- $FeatureIndex[] = $this->read_ushort();
- }
- $ffeats[$st][$t] = $FeatureIndex;
- }
- }
-
- $this->seek($FeatureList_offset);
- $FeatureCount = $this->read_ushort();
- $Feature = array();
- for ($i = 0; $i < $FeatureCount; $i++) {
- $tag = $this->read_tag();
- if ($tag == 'smcp') {
- $this->hassmallcapsGSUB = true;
- }
- $Feature[$i] = array('tag' => $tag);
- $Feature[$i]['offset'] = $FeatureList_offset + $this->read_ushort();
- }
- for ($i = 0; $i < $FeatureCount; $i++) {
- $this->seek($Feature[$i]['offset']);
- $this->read_ushort();
- $Feature[$i]['LookupCount'] = $Lookupcount = $this->read_ushort();
- $Feature[$i]['LookupListIndex'] = array();
- for ($c = 0; $c < $Lookupcount; $c++) {
- $Feature[$i]['LookupListIndex'][] = $this->read_ushort();
- }
- }
- foreach ($ffeats AS $st => $scripts) {
- foreach ($scripts AS $t => $o) {
- $FeatureIndex = $ffeats[$st][$t];
- foreach ($FeatureIndex AS $k => $fi) {
- $ffeats[$st][$t][$k] = $Feature[$fi];
- }
- }
- }
-
- $gsub = array();
- $GSUBScriptLang = array();
- foreach ($ffeats AS $st => $scripts) {
- foreach ($scripts AS $t => $langsys) {
- $lg = array();
- foreach ($langsys AS $ft) {
- $lg[$ft['LookupListIndex'][0]] = $ft;
- }
-
- ksort($lg);
- foreach ($lg AS $ft) {
- $gsub[$st][$t][$ft['tag']] = $ft['LookupListIndex'];
- }
- if (!isset($GSUBScriptLang[$st])) {
- $GSUBScriptLang[$st] = '';
- }
- $GSUBScriptLang[$st] .= $t . ' ';
- }
- }
-
-
- $this->seek($LookupList_offset);
- $LookupCount = $this->read_ushort();
- $GSLookup = array();
- $Offsets = array();
- $SubtableCount = array();
- for ($i = 0; $i < $LookupCount; $i++) {
- $Offsets[$i] = $LookupList_offset + $this->read_ushort();
- }
- for ($i = 0; $i < $LookupCount; $i++) {
- $this->seek($Offsets[$i]);
- $GSLookup[$i]['Type'] = $this->read_ushort();
- $GSLookup[$i]['Flag'] = $flag = $this->read_ushort();
- $GSLookup[$i]['SubtableCount'] = $SubtableCount[$i] = $this->read_ushort();
- for ($c = 0; $c < $SubtableCount[$i]; $c++) {
- $GSLookup[$i]['Subtables'][$c] = $Offsets[$i] + $this->read_ushort();
- }
-
- if (($flag & 0x0010) == 0x0010) {
- $GSLookup[$i]['MarkFilteringSet'] = $this->read_ushort();
- } else {
- $GSLookup[$i]['MarkFilteringSet'] = '';
- }
-
- if ($GSLookup[$i]['Type'] == 7) {
-
- for ($c = 0; $c < $SubtableCount[$i]; $c++) {
- $this->seek($GSLookup[$i]['Subtables'][$c]);
- $ExtensionPosFormat = $this->read_ushort();
- $type = $this->read_ushort();
- $ext_offset = $this->read_ulong();
- $GSLookup[$i]['Subtables'][$c] = $GSLookup[$i]['Subtables'][$c] + $ext_offset;
- }
- $GSLookup[$i]['Type'] = $type;
- }
- }
-
-
- $this->GSLuCoverage = array();
- for ($i = 0; $i < $LookupCount; $i++) {
- for ($c = 0; $c < $GSLookup[$i]['SubtableCount']; $c++) {
- $this->seek($GSLookup[$i]['Subtables'][$c]);
- $PosFormat = $this->read_ushort();
- if ($GSLookup[$i]['Type'] == 5 && $PosFormat == 3) {
- $this->skip(4);
- } else if ($GSLookup[$i]['Type'] == 6 && $PosFormat == 3) {
- $BacktrackGlyphCount = $this->read_ushort();
- $this->skip(2 * $BacktrackGlyphCount + 2);
- }
-
- $Coverage = $GSLookup[$i]['Subtables'][$c] + $this->read_ushort();
- $this->seek($Coverage);
- $glyphs = $this->_getCoverage(false, 2);
- $this->GSLuCoverage[$i][$c] = $glyphs;
- }
- }
-
-
- $s = '<?php
- $GSLuCoverage = ' . var_export($this->GSLuCoverage, true) . ';
- ?>';
- file_put_contents(_MPDF_TTFONTDATAPATH . $this->fontkey . '.GSUBdata.php', $s);
-
-
-
-
-
-
-
-
- $this->seek($LookupList_offset);
- $LookupCount = $this->read_ushort();
- $Lookup = array();
- for ($i = 0; $i < $LookupCount; $i++) {
- $Lookup[$i]['offset'] = $LookupList_offset + $this->read_ushort();
- }
- for ($i = 0; $i < $LookupCount; $i++) {
- $this->seek($Lookup[$i]['offset']);
- $Lookup[$i]['Type'] = $this->read_ushort();
- $Lookup[$i]['Flag'] = $flag = $this->read_ushort();
- $Lookup[$i]['SubtableCount'] = $this->read_ushort();
- for ($c = 0; $c < $Lookup[$i]['SubtableCount']; $c++) {
- $Lookup[$i]['Subtable'][$c]['Offset'] = $Lookup[$i]['offset'] + $this->read_ushort();
- }
-
- if (($flag & 0x0010) == 0x0010) {
- $Lookup[$i]['MarkFilteringSet'] = $this->read_ushort();
- } else {
- $Lookup[$i]['MarkFilteringSet'] = '';
- }
-
- if ($Lookup[$i]['Type'] == 7) {
-
- for ($c = 0; $c < $Lookup[$i]['SubtableCount']; $c++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['Offset']);
- $ExtensionPosFormat = $this->read_ushort();
- $type = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['Offset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ulong();
- }
- $Lookup[$i]['Type'] = $type;
- }
- }
-
-
- for ($i = 0; $i < $LookupCount; $i++) {
- for ($c = 0; $c < $Lookup[$i]['SubtableCount']; $c++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['Offset']);
- $SubstFormat = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['Format'] = $SubstFormat;
-
-
- if ($Lookup[$i]['Type'] == 1) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- if ($SubstFormat == 1) {
- $Lookup[$i]['Subtable'][$c]['DeltaGlyphID'] = $this->read_short();
- } else if ($SubstFormat == 2) {
- $GlyphCount = $this->read_ushort();
- for ($g = 0; $g < $GlyphCount; $g++) {
- $Lookup[$i]['Subtable'][$c]['Glyphs'][] = $this->read_ushort();
- }
- }
- }
-
- else if ($Lookup[$i]['Type'] == 2) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SequenceCount'] = $SequenceCount = $this->read_short();
- for ($s = 0; $s < $SequenceCount; $s++) {
- $Lookup[$i]['Subtable'][$c]['Sequences'][$s]['Offset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_short();
- }
- for ($s = 0; $s < $SequenceCount; $s++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['Sequences'][$s]['Offset']);
- $Lookup[$i]['Subtable'][$c]['Sequences'][$s]['GlyphCount'] = $this->read_short();
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['Sequences'][$s]['GlyphCount']; $g++) {
- $Lookup[$i]['Subtable'][$c]['Sequences'][$s]['SubstituteGlyphID'][] = $this->read_ushort();
- }
- }
- }
-
- else if ($Lookup[$i]['Type'] == 3) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['AlternateSetCount'] = $AlternateSetCount = $this->read_short();
- for ($s = 0; $s < $AlternateSetCount; $s++) {
- $Lookup[$i]['Subtable'][$c]['AlternateSets'][$s]['Offset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_short();
- }
- for ($s = 0; $s < $AlternateSetCount; $s++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['AlternateSets'][$s]['Offset']);
- $Lookup[$i]['Subtable'][$c]['AlternateSets'][$s]['GlyphCount'] = $this->read_short();
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['AlternateSets'][$s]['GlyphCount']; $g++) {
- $Lookup[$i]['Subtable'][$c]['AlternateSets'][$s]['SubstituteGlyphID'][] = $this->read_ushort();
- }
- }
- }
-
- else if ($Lookup[$i]['Type'] == 4) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['LigSetCount'] = $LigSetCount = $this->read_short();
- for ($s = 0; $s < $LigSetCount; $s++) {
- $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Offset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_short();
- }
- for ($s = 0; $s < $LigSetCount; $s++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Offset']);
- $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['LigCount'] = $this->read_short();
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['LigCount']; $g++) {
- $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['LigatureOffset'][$g] = $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Offset'] + $this->read_ushort();
- }
- }
- for ($s = 0; $s < $LigSetCount; $s++) {
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['LigCount']; $g++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['LigSet'][$s]['LigatureOffset'][$g]);
- $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['LigGlyph'] = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['CompCount'] = $this->read_ushort();
- for ($l = 1; $l < $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['CompCount']; $l++) {
- $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['GlyphID'][$l] = $this->read_ushort();
- }
- }
- }
- }
-
- else if ($Lookup[$i]['Type'] == 5) {
-
- if ($SubstFormat == 1) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SubRuleSetCount'] = $SubRuleSetCount = $this->read_short();
- for ($s = 0; $s < $SubRuleSetCount; $s++) {
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['Offset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_short();
- }
- for ($s = 0; $s < $SubRuleSetCount; $s++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['Offset']);
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRuleCount'] = $this->read_short();
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRuleCount']; $g++) {
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRuleOffset'][$g] = $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['Offset'] + $this->read_ushort();
- }
- }
- for ($s = 0; $s < $SubRuleSetCount; $s++) {
-
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRuleCount']; $g++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRuleOffset'][$g]);
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['GlyphCount'] = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['SubstCount'] = $this->read_ushort();
-
- for ($l = 1; $l < $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['GlyphCount']; $l++) {
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['Input'][$l] = $this->read_ushort();
- }
-
- for ($l = 0; $l < $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['SubstCount']; $l++) {
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['SubstLookupRecord'][$l]['SequenceIndex'] = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$g]['SubstLookupRecord'][$l]['LookupListIndex'] = $this->read_ushort();
- }
- }
- }
- }
-
- else if ($SubstFormat == 2) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ClassDefOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SubClassSetCnt'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['SubClassSetCnt']; $b++) {
- $offset = $this->read_ushort();
- if ($offset == 0x0000) {
- $Lookup[$i]['Subtable'][$c]['SubClassSetOffset'][] = 0;
- } else {
- $Lookup[$i]['Subtable'][$c]['SubClassSetOffset'][] = $Lookup[$i]['Subtable'][$c]['Offset'] + $offset;
- }
- }
- } else {
- throw new MpdfException("GPOS Lookup Type " . $Lookup[$i]['Type'] . ", Format " . $SubstFormat . " not supported (ttfontsuni.php).");
- }
- }
-
- else if ($Lookup[$i]['Type'] == 6) {
-
- if ($SubstFormat == 1) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSetCount'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['ChainSubRuleSetCount']; $b++) {
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSetOffset'][] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- }
- }
-
- else if ($SubstFormat == 2) {
- $Lookup[$i]['Subtable'][$c]['CoverageTableOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['BacktrackClassDefOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['InputClassDefOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['LookaheadClassDefOffset'] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubClassSetCnt'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['ChainSubClassSetCnt']; $b++) {
- $offset = $this->read_ushort();
- if ($offset == 0x0000) {
- $Lookup[$i]['Subtable'][$c]['ChainSubClassSetOffset'][] = $offset;
- } else {
- $Lookup[$i]['Subtable'][$c]['ChainSubClassSetOffset'][] = $Lookup[$i]['Subtable'][$c]['Offset'] + $offset;
- }
- }
- }
-
- else if ($SubstFormat == 3) {
- $Lookup[$i]['Subtable'][$c]['BacktrackGlyphCount'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['BacktrackGlyphCount']; $b++) {
- $Lookup[$i]['Subtable'][$c]['CoverageBacktrack'][] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- }
- $Lookup[$i]['Subtable'][$c]['InputGlyphCount'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['InputGlyphCount']; $b++) {
- $Lookup[$i]['Subtable'][$c]['CoverageInput'][] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- }
- $Lookup[$i]['Subtable'][$c]['LookaheadGlyphCount'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['LookaheadGlyphCount']; $b++) {
- $Lookup[$i]['Subtable'][$c]['CoverageLookahead'][] = $Lookup[$i]['Subtable'][$c]['Offset'] + $this->read_ushort();
- }
- $Lookup[$i]['Subtable'][$c]['SubstCount'] = $this->read_ushort();
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['SubstCount']; $b++) {
- $Lookup[$i]['Subtable'][$c]['SubstLookupRecord'][$b]['SequenceIndex'] = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SubstLookupRecord'][$b]['LookupListIndex'] = $this->read_ushort();
-
- }
- }
- } else {
- throw new MpdfException("Lookup Type " . $Lookup[$i]['Type'] . " not supported.");
- }
- }
- }
-
-
-
- for ($i = 0; $i < $LookupCount; $i++) {
- for ($c = 0; $c < $Lookup[$i]['SubtableCount']; $c++) {
- $SubstFormat = $Lookup[$i]['Subtable'][$c]['Format'];
-
- if ($Lookup[$i]['Type'] == 1) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $glyphs = $this->_getCoverage(false);
- for ($g = 0; $g < count($glyphs); $g++) {
- $replace = array();
- $substitute = array();
- $replace[] = unicode_hex($this->glyphToChar[$glyphs[$g]][0]);
-
- if ($this->_checkGSUBignore($Lookup[$i]['Flag'], $replace[0], $Lookup[$i]['MarkFilteringSet'])) {
- continue;
- }
- if (isset($Lookup[$i]['Subtable'][$c]['DeltaGlyphID'])) {
- $substitute[] = unicode_hex($this->glyphToChar[($glyphs[$g] + $Lookup[$i]['Subtable'][$c]['DeltaGlyphID'])][0]);
- } else {
- $substitute[] = unicode_hex($this->glyphToChar[($Lookup[$i]['Subtable'][$c]['Glyphs'][$g])][0]);
- }
- $Lookup[$i]['Subtable'][$c]['subs'][] = array('Replace' => $replace, 'substitute' => $substitute);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 2) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $glyphs = $this->_getCoverage();
- for ($g = 0; $g < count($glyphs); $g++) {
- $replace = array();
- $substitute = array();
- $replace[] = $glyphs[$g];
-
- if ($this->_checkGSUBignore($Lookup[$i]['Flag'], $replace[0], $Lookup[$i]['MarkFilteringSet'])) {
- continue;
- }
- if (!isset($Lookup[$i]['Subtable'][$c]['Sequences'][$g]['SubstituteGlyphID']) || count($Lookup[$i]['Subtable'][$c]['Sequences'][$g]['SubstituteGlyphID']) == 0) {
- continue;
- }
- foreach ($Lookup[$i]['Subtable'][$c]['Sequences'][$g]['SubstituteGlyphID'] AS $sub) {
- $substitute[] = unicode_hex($this->glyphToChar[$sub][0]);
- }
- $Lookup[$i]['Subtable'][$c]['subs'][] = array('Replace' => $replace, 'substitute' => $substitute);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 3) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $glyphs = $this->_getCoverage();
- for ($g = 0; $g < count($glyphs); $g++) {
- $replace = array();
- $substitute = array();
- $replace[] = $glyphs[$g];
-
- if ($this->_checkGSUBignore($Lookup[$i]['Flag'], $replace[0], $Lookup[$i]['MarkFilteringSet'])) {
- continue;
- }
- $gid = $Lookup[$i]['Subtable'][$c]['AlternateSets'][$g]['SubstituteGlyphID'][0];
- if (!isset($this->glyphToChar[$gid][0])) {
- continue;
- }
- $substitute[] = unicode_hex($this->glyphToChar[$gid][0]);
- $Lookup[$i]['Subtable'][$c]['subs'][] = array('Replace' => $replace, 'substitute' => $substitute);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 4) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $glyphs = $this->_getCoverage();
- $LigSetCount = $Lookup[$i]['Subtable'][$c]['LigSetCount'];
- for ($s = 0; $s < $LigSetCount; $s++) {
- for ($g = 0; $g < $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['LigCount']; $g++) {
- $replace = array();
- $substitute = array();
- $replace[] = $glyphs[$s];
-
- if ($this->_checkGSUBignore($Lookup[$i]['Flag'], $replace[0], $Lookup[$i]['MarkFilteringSet'])) {
- continue;
- }
- for ($l = 1; $l < $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['CompCount']; $l++) {
- $gid = $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['GlyphID'][$l];
- $rpl = unicode_hex($this->glyphToChar[$gid][0]);
-
- if ($this->_checkGSUBignore($Lookup[$i]['Flag'], $rpl, $Lookup[$i]['MarkFilteringSet'])) {
- continue 2;
- }
- $replace[] = $rpl;
- }
- $gid = $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['LigGlyph'];
- if (!isset($this->glyphToChar[$gid][0])) {
- continue;
- }
- $substitute[] = unicode_hex($this->glyphToChar[$gid][0]);
- $Lookup[$i]['Subtable'][$c]['subs'][] = array('Replace' => $replace, 'substitute' => $substitute, 'CompCount' => $Lookup[$i]['Subtable'][$c]['LigSet'][$s]['Ligature'][$g]['CompCount']);
- }
- }
- }
-
- else if ($Lookup[$i]['Type'] == 5) {
-
- if ($SubstFormat == 1) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $Lookup[$i]['Subtable'][$c]['CoverageGlyphs'] = $CoverageGlyphs = $this->_getCoverage();
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['SubRuleSetCount']; $s++) {
- $SubRuleSet = $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s];
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['FirstGlyph'] = $CoverageGlyphs[$s];
- for ($r = 0; $r < $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRuleCount']; $r++) {
- $GlyphCount = $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$r]['GlyphCount'];
- for ($g = 1; $g < $GlyphCount; $g++) {
- $glyphID = $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$r]['Input'][$g];
- $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'][$r]['InputGlyphs'][$g] = unicode_hex($this->glyphToChar[$glyphID][0]);
- }
- }
- }
- }
-
- else if ($SubstFormat == 2) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $Lookup[$i]['Subtable'][$c]['CoverageGlyphs'] = $CoverageGlyphs = $this->_getCoverage();
- $InputClasses = $this->_getClasses($Lookup[$i]['Subtable'][$c]['ClassDefOffset']);
- $Lookup[$i]['Subtable'][$c]['InputClasses'] = $InputClasses;
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['SubClassSetCnt']; $s++) {
- if ($Lookup[$i]['Subtable'][$c]['SubClassSetOffset'][$s] > 0) {
- $this->seek($Lookup[$i]['Subtable'][$c]['SubClassSetOffset'][$s]);
- $Lookup[$i]['Subtable'][$c]['SubClassSet'][$s]['SubClassRuleCnt'] = $SubClassRuleCnt = $this->read_ushort();
- $SubClassRule = array();
- for ($b = 0; $b < $SubClassRuleCnt; $b++) {
- $SubClassRule[$b] = $Lookup[$i]['Subtable'][$c]['SubClassSetOffset'][$s] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['SubClassSet'][$s]['SubClassRule'][$b] = $SubClassRule[$b];
- }
- }
- }
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['SubClassSetCnt']; $s++) {
- if ($Lookup[$i]['Subtable'][$c]['SubClassSetOffset'][$s] > 0) {
- $SubClassRuleCnt = $Lookup[$i]['Subtable'][$c]['SubClassSet'][$s]['SubClassRuleCnt'];
- for ($b = 0; $b < $SubClassRuleCnt; $b++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['SubClassSet'][$s]['SubClassRule'][$b]);
- $Rule = array();
- $Rule['InputGlyphCount'] = $this->read_ushort();
- $Rule['SubstCount'] = $this->read_ushort();
- for ($r = 1; $r < $Rule['InputGlyphCount']; $r++) {
- $Rule['Input'][$r] = $this->read_ushort();
- }
- for ($r = 0; $r < $Rule['SubstCount']; $r++) {
- $Rule['SequenceIndex'][$r] = $this->read_ushort();
- $Rule['LookupListIndex'][$r] = $this->read_ushort();
- }
- $Lookup[$i]['Subtable'][$c]['SubClassSet'][$s]['SubClassRule'][$b] = $Rule;
- }
- }
- }
- }
-
- else if ($SubstFormat == 3) {
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['InputGlyphCount']; $b++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageInput'][$b]);
- $glyphs = $this->_getCoverage();
- $Lookup[$i]['Subtable'][$c]['CoverageInputGlyphs'][] = implode("|", $glyphs);
- }
- throw new MpdfException("Lookup Type 5, SubstFormat 3 not tested. Please report this with the name of font used - " . $this->fontkey);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 6) {
-
- if ($SubstFormat == 1) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $Lookup[$i]['Subtable'][$c]['CoverageGlyphs'] = $CoverageGlyphs = $this->_getCoverage();
- $ChainSubRuleSetCnt = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSetCount'];
- for ($s = 0; $s < $ChainSubRuleSetCnt; $s++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['ChainSubRuleSetOffset'][$s]);
- $ChainSubRuleCnt = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRuleCount'] = $this->read_ushort();
- for ($r = 0; $r < $ChainSubRuleCnt; $r++) {
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRuleOffset'][$r] = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSetOffset'][$s] + $this->read_ushort();
- }
- }
- for ($s = 0; $s < $ChainSubRuleSetCnt; $s++) {
- $ChainSubRuleCnt = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRuleCount'];
- for ($r = 0; $r < $ChainSubRuleCnt; $r++) {
-
- $this->seek($Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRuleOffset'][$r]);
- $BacktrackGlyphCount = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['BacktrackGlyphCount'] = $this->read_ushort();
- for ($g = 0; $g < $BacktrackGlyphCount; $g++) {
- $glyphID = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['BacktrackGlyphs'][$g] = unicode_hex($this->glyphToChar[$glyphID][0]);
- }
- $InputGlyphCount = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['InputGlyphCount'] = $this->read_ushort();
- for ($g = 1; $g < $InputGlyphCount; $g++) {
- $glyphID = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['InputGlyphs'][$g] = unicode_hex($this->glyphToChar[$glyphID][0]);
- }
- $LookaheadGlyphCount = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['LookaheadGlyphCount'] = $this->read_ushort();
- for ($g = 0; $g < $LookaheadGlyphCount; $g++) {
- $glyphID = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['LookaheadGlyphs'][$g] = unicode_hex($this->glyphToChar[$glyphID][0]);
- }
- $SubstCount = $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['SubstCount'] = $this->read_ushort();
- for ($lu = 0; $lu < $SubstCount; $lu++) {
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['SequenceIndex'][$lu] = $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'][$r]['LookupListIndex'][$lu] = $this->read_ushort();
- }
- }
- }
- }
-
- else if ($SubstFormat == 2) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageTableOffset']);
- $Lookup[$i]['Subtable'][$c]['CoverageGlyphs'] = $CoverageGlyphs = $this->_getCoverage();
- $BacktrackClasses = $this->_getClasses($Lookup[$i]['Subtable'][$c]['BacktrackClassDefOffset']);
- $Lookup[$i]['Subtable'][$c]['BacktrackClasses'] = $BacktrackClasses;
- $InputClasses = $this->_getClasses($Lookup[$i]['Subtable'][$c]['InputClassDefOffset']);
- $Lookup[$i]['Subtable'][$c]['InputClasses'] = $InputClasses;
- $LookaheadClasses = $this->_getClasses($Lookup[$i]['Subtable'][$c]['LookaheadClassDefOffset']);
- $Lookup[$i]['Subtable'][$c]['LookaheadClasses'] = $LookaheadClasses;
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['ChainSubClassSetCnt']; $s++) {
- if ($Lookup[$i]['Subtable'][$c]['ChainSubClassSetOffset'][$s] > 0) {
- $this->seek($Lookup[$i]['Subtable'][$c]['ChainSubClassSetOffset'][$s]);
- $Lookup[$i]['Subtable'][$c]['ChainSubClassSet'][$s]['ChainSubClassRuleCnt'] = $ChainSubClassRuleCnt = $this->read_ushort();
- $ChainSubClassRule = array();
- for ($b = 0; $b < $ChainSubClassRuleCnt; $b++) {
- $ChainSubClassRule[$b] = $Lookup[$i]['Subtable'][$c]['ChainSubClassSetOffset'][$s] + $this->read_ushort();
- $Lookup[$i]['Subtable'][$c]['ChainSubClassSet'][$s]['ChainSubClassRule'][$b] = $ChainSubClassRule[$b];
- }
- }
- }
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['ChainSubClassSetCnt']; $s++) {
- if (isset($Lookup[$i]['Subtable'][$c]['ChainSubClassSet'][$s]['ChainSubClassRuleCnt'])) {
- $ChainSubClassRuleCnt = $Lookup[$i]['Subtable'][$c]['ChainSubClassSet'][$s]['ChainSubClassRuleCnt'];
- } else {
- $ChainSubClassRuleCnt = 0;
- }
- for ($b = 0; $b < $ChainSubClassRuleCnt; $b++) {
- if ($Lookup[$i]['Subtable'][$c]['ChainSubClassSetOffset'][$s] > 0) {
- $this->seek($Lookup[$i]['Subtable'][$c]['ChainSubClassSet'][$s]['ChainSubClassRule'][$b]);
- $Rule = array();
- $Rule['BacktrackGlyphCount'] = $this->read_ushort();
- for ($r = 0; $r < $Rule['BacktrackGlyphCount']; $r++) {
- $Rule['Backtrack'][$r] = $this->read_ushort();
- }
- $Rule['InputGlyphCount'] = $this->read_ushort();
- for ($r = 1; $r < $Rule['InputGlyphCount']; $r++) {
- $Rule['Input'][$r] = $this->read_ushort();
- }
- $Rule['LookaheadGlyphCount'] = $this->read_ushort();
- for ($r = 0; $r < $Rule['LookaheadGlyphCount']; $r++) {
- $Rule['Lookahead'][$r] = $this->read_ushort();
- }
- $Rule['SubstCount'] = $this->read_ushort();
- for ($r = 0; $r < $Rule['SubstCount']; $r++) {
- $Rule['SequenceIndex'][$r] = $this->read_ushort();
- $Rule['LookupListIndex'][$r] = $this->read_ushort();
- }
- $Lookup[$i]['Subtable'][$c]['ChainSubClassSet'][$s]['ChainSubClassRule'][$b] = $Rule;
- }
- }
- }
- }
-
- else if ($SubstFormat == 3) {
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['BacktrackGlyphCount']; $b++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageBacktrack'][$b]);
- $glyphs = $this->_getCoverage();
- $Lookup[$i]['Subtable'][$c]['CoverageBacktrackGlyphs'][] = implode("|", $glyphs);
- }
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['InputGlyphCount']; $b++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageInput'][$b]);
- $glyphs = $this->_getCoverage();
- $Lookup[$i]['Subtable'][$c]['CoverageInputGlyphs'][] = implode("|", $glyphs);
-
- }
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['LookaheadGlyphCount']; $b++) {
- $this->seek($Lookup[$i]['Subtable'][$c]['CoverageLookahead'][$b]);
- $glyphs = $this->_getCoverage();
- $Lookup[$i]['Subtable'][$c]['CoverageLookaheadGlyphs'][] = implode("|", $glyphs);
- }
- }
- }
- }
- }
-
-
-
-
- $GSUBScriptLang = array();
- $rtlpua = array();
- foreach ($gsub AS $st => $scripts) {
- foreach ($scripts AS $t => $langsys) {
- $lul = array();
- $tags = array();
- foreach ($langsys AS $tag => $ft) {
- foreach ($ft AS $ll) {
- $lul[$ll] = $tag;
- }
- }
- ksort($lul);
- $volt = $this->_getGSUBarray($Lookup, $lul, $st);
-
-
-
-
-
-
-
-
- $rtl = array();
- $rtlSUB = "array()";
- $finals = '';
- if (strpos('arab syrc hebr thaa nko samr', $st) !== false) {
- foreach ($volt AS $v) {
-
-
- if (strpos('isol fina init medi fin2 fin3 med2', $v['tag']) !== false) {
- $key = $v['match'];
- $key = preg_replace('/[\(\)]*/', '', $key);
- $sub = $v['replace'];
- if ($v['tag'] == 'isol')
- $kk = 0;
- else if ($v['tag'] == 'fina')
- $kk = 1;
- else if ($v['tag'] == 'init')
- $kk = 2;
- else if ($v['tag'] == 'medi')
- $kk = 3;
- else if ($v['tag'] == 'med2')
- $kk = 4;
- else if ($v['tag'] == 'fin2')
- $kk = 5;
- else if ($v['tag'] == 'fin3')
- $kk = 6;
- $rtl[$key][$kk] = $sub;
- if (isset($v['prel']) && count($v['prel']))
- $rtl[$key]['prel'][$kk] = $v['prel'];
- if (isset($v['postl']) && count($v['postl']))
- $rtl[$key]['postl'][$kk] = $v['postl'];
- if (isset($v['ignore']) && $v['ignore']) {
- $rtl[$key]['ignore'][$kk] = $v['ignore'];
- }
- $rtlpua[] = $sub;
- }
-
- else {
- if (isset($v['context']) && $v['context']) {
- foreach ($v['rules'] AS $vs) {
- for ($i = 0; $i < count($vs['match']); $i++) {
- if (isset($vs['replace'][$i]) && preg_match('/^0[A-F0-9]{4}$/', $vs['match'][$i])) {
- if (preg_match('/^0[EF][A-F0-9]{3}$/', $vs['replace'][$i])) {
- $rtlpua[] = $vs['replace'][$i];
- }
- }
- }
- }
- } else {
- preg_match_all('/\((0[A-F0-9]{4})\)/', $v['match'], $m);
- for ($i = 0; $i < count($m[0]); $i++) {
- $sb = explode(' ', $v['replace']);
- foreach ($sb AS $sbg) {
- if (preg_match('/(0[EF][A-F0-9]{3})/', $sbg, $mr)) {
- $rtlpua[] = $mr[1];
- }
- }
- }
- }
- }
- }
-
-
- foreach ($rtl AS $base => $variants) {
- if (isset($variants[1])) {
- if (strpos('0FE8E 0FE94 0FEA2 0FEAA 0FEAE 0FEC2 0FEDA 0FEDE 0FB93 0FECA 0FED2 0FED6 0FEEE 0FEF0 0FEF2', $variants[1]) === false) {
-
-
-
- $finals .= $variants[1] . ' ';
- }
- }
- }
- ksort($rtl);
- $a = var_export($rtl, true);
- $a = preg_replace('/\\\\\\\\/', "\\", $a);
- $a = preg_replace('/\'/', '"', $a);
- $a = preg_replace('/\r/', '', $a);
- $a = preg_replace('/> \n/', '>', $a);
- $a = preg_replace('/\n \)/', ')', $a);
- $a = preg_replace('/\n /', ' ', $a);
- $a = preg_replace('/\[IGNORE(\d+)\]/', '".$ignore[\\1]."', $a);
- $rtlSUB = preg_replace('/[ ]+/', ' ', $a);
- }
-
-
-
- $rphf = array();
- $half = array();
- $pref = array();
- $blwf = array();
- $pstf = array();
- if (strpos('dev2 bng2 gur2 gjr2 ory2 tml2 tel2 knd2 mlm2 deva beng guru gujr orya taml telu knda mlym', $st) !== false) {
- if (strpos('deva beng guru gujr orya taml telu knda mlym', $st) !== false) {
- $is_old_spec = true;
- } else {
- $is_old_spec = false;
- }
-
- $loclsubs = array();
- foreach ($volt AS $v) {
- if (strpos('locl', $v['tag']) !== false) {
- $key = $v['match'];
- $key = preg_replace('/[\(\)]*/', '', $key);
- $sub = $v['replace'];
- if ($key && strlen(trim($key)) == 5 && $sub) {
- $loclsubs[$sub] = $key;
- }
- }
- }
- foreach ($volt AS $v) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (strpos('rphf half pref blwf pstf', $v['tag']) !== false) {
- if (isset($v['context']) && $v['context'] && $v['nBacktrack'] == 0 && $v['nLookahead'] == 0) {
- foreach ($v['rules'] AS $vs) {
- if (count($vs['match']) == 2 && count($vs['replace']) == 1) {
- $sub = $vs['replace'][0];
-
- if (strpos('0094D 009CD 00A4D 00ACD 00B4D 00BCD 00C4D 00CCD 00D4D', $vs['match'][0]) !== false && strpos('pref blwf pstf', $v['tag']) !== false && !$is_old_spec) {
- $key = $vs['match'][1];
- $tag = $v['tag'];
- if (isset($loclsubs[$key])) {
- $$tag[$loclsubs[$key]] = $sub;
- }
- $tmp = &$$tag;
- $tmp[hexdec($key)] = hexdec($sub);
- }
-
-
- else if (strpos('0094D 009CD 00A4D 00ACD 00B4D 00BCD 00C4D 00CCD 00D4D', $vs['match'][1]) !== false && (strpos('rphf half', $v['tag']) !== false || (strpos('pref blwf pstf', $v['tag']) !== false && ($is_old_spec || _OTL_OLD_SPEC_COMPAT_2)))) {
- $key = $vs['match'][0];
- $tag = $v['tag'];
- if (isset($loclsubs[$key])) {
- $$tag[$loclsubs[$key]] = $sub;
- }
- $tmp = &$$tag;
- $tmp[hexdec($key)] = hexdec($sub);
- }
- }
- }
- } else if (!isset($v['context'])) {
- $key = $v['match'];
- $key = preg_replace('/[\(\)]*/', '', $key);
- $sub = $v['replace'];
- if ($key && strlen(trim($key)) == 11 && $sub) {
-
-
-
- if (strpos('0094D 009CD 00A4D 00ACD 00B4D 00BCD 00C4D 00CCD 00D4D', substr($key, 0, 5)) !== false && strpos('pref blwf pstf', $v['tag']) !== false && !$is_old_spec) {
- $key = substr($key, 6, 5);
- $tag = $v['tag'];
- if (isset($loclsubs[$key])) {
- $$tag[$loclsubs[$key]] = $sub;
- }
- $tmp = &$$tag;
- $tmp[hexdec($key)] = hexdec($sub);
- } else if (strpos('0094D 009CD 00A4D 00ACD 00B4D 00BCD 00C4D 00CCD 00D4D', substr($key, 6, 5)) !== false && (strpos('rphf half', $v['tag']) !== false || (strpos('pref blwf pstf', $v['tag']) !== false && ($is_old_spec || _OTL_OLD_SPEC_COMPAT_2)))) {
- $key = substr($key, 0, 5);
- $tag = $v['tag'];
- if (isset($loclsubs[$key])) {
- $$tag[$loclsubs[$key]] = $sub;
- }
- $tmp = &$$tag;
- $tmp[hexdec($key)] = hexdec($sub);
- }
- }
- }
- }
- }
-
- }
-
-
-
-
- if (count($rtl) || count($rphf) || count($half) || count($pref) || count($blwf) || count($pstf) || $finals) {
-
- $s = '<?php
- $rtlSUB = ' . $rtlSUB . ';
- $finals = \'' . $finals . '\';
- $rphf = ' . var_export($rphf, true) . ';
- $half = ' . var_export($half, true) . ';
- $pref = ' . var_export($pref, true) . ';
- $blwf = ' . var_export($blwf, true) . ';
- $pstf = ' . var_export($pstf, true) . ';
- ' . "\n" . '?>';
- file_put_contents(_MPDF_TTFONTDATAPATH . $this->fontkey . '.GSUB.' . $st . '.' . $t . '.php', $s);
- }
-
- if (!isset($GSUBScriptLang[$st])) {
- $GSUBScriptLang[$st] = '';
- }
- $GSUBScriptLang[$st] .= $t . ' ';
-
- }
- }
-
-
- $rtlPUAstr = "";
- if (count($rtlpua)) {
- $rtlpua = array_unique($rtlpua);
- sort($rtlpua);
- $n = count($rtlpua);
- for ($i = 0; $i < $n; $i++) {
- if (hexdec($rtlpua[$i]) < hexdec('E000') || hexdec($rtlpua[$i]) > hexdec('F8FF')) {
- unset($rtlpua[$i]);
- }
- }
- sort($rtlpua, SORT_STRING);
- $rangeid = -1;
- $range = array();
- $prevgid = -2;
-
- foreach ($rtlpua as $gidhex) {
- $gid = hexdec($gidhex);
- if ($gid == ($prevgid + 1)) {
- $range[$rangeid]['end'] = $gidhex;
- $range[$rangeid]['count'] ++;
- } else {
-
- $rangeid++;
- $range[$rangeid] = array();
- $range[$rangeid]['start'] = $gidhex;
- $range[$rangeid]['end'] = $gidhex;
- $range[$rangeid]['count'] = 1;
- }
- $prevgid = $gid;
- }
- foreach ($range AS $rg) {
- if ($rg['count'] == 1) {
- $rtlPUAstr .= "\x{" . $rg['start'] . "}";
- } else if ($rg['count'] == 2) {
- $rtlPUAstr .= "\x{" . $rg['start'] . "}\x{" . $rg['end'] . "}";
- } else {
- $rtlPUAstr .= "\x{" . $rg['start'] . "}-\x{" . $rg['end'] . "}";
- }
- }
- }
-
-
-
-
-
-
-
- }
- return array($GSUBScriptLang, $gsub, $GSLookup, $rtlPUAstr);
- }
-
-
- function _getGSUBarray(&$Lookup, &$lul, $scripttag)
- {
-
-
- $volt = array();
- $reph = '';
- $matraE = '';
- $vatu = '';
- foreach ($lul AS $i => $tag) {
- for ($c = 0; $c < $Lookup[$i]['SubtableCount']; $c++) {
- $SubstFormat = $Lookup[$i]['Subtable'][$c]['Format'];
-
- if ($Lookup[$i]['Type'] == 1) {
- for ($s = 0; $s < count($Lookup[$i]['Subtable'][$c]['subs']); $s++) {
- $inputGlyphs = $Lookup[$i]['Subtable'][$c]['subs'][$s]['Replace'];
- $substitute = $Lookup[$i]['Subtable'][$c]['subs'][$s]['substitute'][0];
-
- $repl = $this->_makeGSUBinputMatch($inputGlyphs, "()");
- $subs = $this->_makeGSUBinputReplacement(1, $substitute, "()", 0, 1, 0);
- $volt[] = array('match' => $repl, 'replace' => $subs, 'tag' => $tag, 'key' => $inputGlyphs[0], 'type' => 1);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 2) {
- for ($s = 0; $s < count($Lookup[$i]['Subtable'][$c]['subs']); $s++) {
- $inputGlyphs = $Lookup[$i]['Subtable'][$c]['subs'][$s]['Replace'];
- $substitute = implode(" ", $Lookup[$i]['Subtable'][$c]['subs'][$s]['substitute']);
-
- $repl = $this->_makeGSUBinputMatch($inputGlyphs, "()");
- $subs = $this->_makeGSUBinputReplacement(1, $substitute, "()", 0, 1, 0);
- $volt[] = array('match' => $repl, 'replace' => $subs, 'tag' => $tag, 'key' => $inputGlyphs[0], 'type' => 2);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 3) {
- for ($s = 0; $s < count($Lookup[$i]['Subtable'][$c]['subs']); $s++) {
- $inputGlyphs = $Lookup[$i]['Subtable'][$c]['subs'][$s]['Replace'];
- $substitute = $Lookup[$i]['Subtable'][$c]['subs'][$s]['substitute'][0];
-
- $repl = $this->_makeGSUBinputMatch($inputGlyphs, "()");
- $subs = $this->_makeGSUBinputReplacement(1, $substitute, "()", 0, 1, 0);
- $volt[] = array('match' => $repl, 'replace' => $subs, 'tag' => $tag, 'key' => $inputGlyphs[0], 'type' => 3);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 4) {
- for ($s = 0; $s < count($Lookup[$i]['Subtable'][$c]['subs']); $s++) {
- $inputGlyphs = $Lookup[$i]['Subtable'][$c]['subs'][$s]['Replace'];
- $substitute = $Lookup[$i]['Subtable'][$c]['subs'][$s]['substitute'][0];
-
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- $repl = $this->_makeGSUBinputMatch($inputGlyphs, $ignore);
- $subs = $this->_makeGSUBinputReplacement(count($inputGlyphs), $substitute, $ignore, 0, count($inputGlyphs), 0);
- $volt[] = array('match' => $repl, 'replace' => $subs, 'tag' => $tag, 'key' => $inputGlyphs[0], 'type' => 4, 'CompCount' => $Lookup[$i]['Subtable'][$c]['subs'][$s]['CompCount'], 'Lig' => $substitute);
- }
- }
-
- else if ($Lookup[$i]['Type'] == 5) {
-
- if ($SubstFormat == 1) {
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['SubRuleSetCount']; $s++) {
-
- $subRule = array();
- foreach ($Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['SubRule'] AS $rule) {
-
- $inputGlyphs = array();
- if ($rule['GlyphCount'] > 1) {
- $inputGlyphs = $rule['InputGlyphs'];
- }
- $inputGlyphs[0] = $Lookup[$i]['Subtable'][$c]['SubRuleSet'][$s]['FirstGlyph'];
- ksort($inputGlyphs);
- $nInput = count($inputGlyphs);
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, array(), 0);
- $subRule = array('context' => 1, 'tag' => $tag, 'matchback' => '', 'match' => $contextInputMatch, 'nBacktrack' => 0, 'nInput' => $nInput, 'nLookahead' => 0, 'rules' => array(),);
- for ($b = 0; $b < $rule['SubstCount']; $b++) {
- $lup = $rule['SubstLookupRecord'][$b]['LookupListIndex'];
- $seqIndex = $rule['SubstLookupRecord'][$b]['SequenceIndex'];
-
- for ($lus = 0; $lus < $Lookup[$lup]['SubtableCount']; $lus++) {
- if (count($Lookup[$lup]['Subtable'][$lus]['subs'])) {
- foreach ($Lookup[$lup]['Subtable'][$lus]['subs'] AS $luss) {
- $lookupGlyphs = $luss['Replace'];
- $mLen = count($lookupGlyphs);
-
-
-
- if (strpos($inputGlyphs[$seqIndex], $lookupGlyphs[0]) === false) {
- continue;
- }
- $REPL = implode(" ", $luss['substitute']);
- if (strpos("isol fina fin2 fin3 medi med2 init ", $tag) !== false && $scripttag == 'arab') {
- $volt[] = array('match' => $lookupGlyphs[0], 'replace' => $REPL, 'tag' => $tag, 'prel' => $backtrackGlyphs, 'postl' => $lookaheadGlyphs, 'ignore' => $ignore);
- } else {
- $subRule['rules'][] = array('type' => $Lookup[$lup]['Type'], 'match' => $lookupGlyphs, 'replace' => $luss['substitute'], 'seqIndex' => $seqIndex, 'key' => $lookupGlyphs[0],);
- }
- }
- }
- }
- }
- if (count($subRule['rules']))
- $volt[] = $subRule;
- }
- }
- }
-
- else if ($SubstFormat == 2) {
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- foreach ($Lookup[$i]['Subtable'][$c]['SubClassSet'] AS $inputClass => $cscs) {
- for ($cscrule = 0; $cscrule < $cscs['SubClassRuleCnt']; $cscrule++) {
- $rule = $cscs['SubClassRule'][$cscrule];
- $inputGlyphs = array();
- $inputGlyphs[0] = $Lookup[$i]['Subtable'][$c]['InputClasses'][$inputClass];
- if ($rule['InputGlyphCount'] > 1) {
-
- for ($gcl = 1; $gcl < $rule['InputGlyphCount']; $gcl++) {
- $classindex = $rule['Input'][$gcl];
- if (isset($Lookup[$i]['Subtable'][$c]['InputClasses'][$classindex])) {
- $inputGlyphs[$gcl] = $Lookup[$i]['Subtable'][$c]['InputClasses'][$classindex];
- }
-
-
- else {
- $inputGlyphs[$gcl] = '';
- }
- }
- }
- $nInput = $rule['InputGlyphCount'];
- $nIsubs = (2 * $nInput) - 1;
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, array(), 0);
- $subRule = array('context' => 1, 'tag' => $tag, 'matchback' => '', 'match' => $contextInputMatch, 'nBacktrack' => 0, 'nInput' => $nInput, 'nLookahead' => 0, 'rules' => array(),);
- for ($b = 0; $b < $rule['SubstCount']; $b++) {
- $lup = $rule['LookupListIndex'][$b];
- $seqIndex = $rule['SequenceIndex'][$b];
-
- for ($lus = 0; $lus < $Lookup[$lup]['SubtableCount']; $lus++) {
- if (isset($Lookup[$lup]['Subtable'][$lus]['subs']) && count($Lookup[$lup]['Subtable'][$lus]['subs'])) {
- foreach ($Lookup[$lup]['Subtable'][$lus]['subs'] AS $luss) {
- $lookupGlyphs = $luss['Replace'];
- $mLen = count($lookupGlyphs);
-
-
-
- if (strpos($inputGlyphs[$seqIndex], $lookupGlyphs[0]) === false) {
- continue;
- }
-
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, $lookupGlyphs, $seqIndex);
- $REPL = implode(" ", $luss['substitute']);
-
- if (strpos("isol fina fin2 fin3 medi med2 init ", $tag) !== false && $scripttag == 'arab') {
- $volt[] = array('match' => $lookupGlyphs[0], 'replace' => $REPL, 'tag' => $tag, 'prel' => $backtrackGlyphs, 'postl' => $lookaheadGlyphs, 'ignore' => $ignore);
- } else {
- $subRule['rules'][] = array('type' => $Lookup[$lup]['Type'], 'match' => $lookupGlyphs, 'replace' => $luss['substitute'], 'seqIndex' => $seqIndex, 'key' => $lookupGlyphs[0],);
- }
- }
- }
- }
- }
- if (count($subRule['rules']))
- $volt[] = $subRule;
- }
- }
- }
-
- else if ($SubstFormat == 3) {
-
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- $inputGlyphs = $Lookup[$i]['Subtable'][$c]['CoverageInputGlyphs'];
- $CoverageInputGlyphs = implode('|', $inputGlyphs);
- $nInput = $Lookup[$i]['Subtable'][$c]['InputGlyphCount'];
- if ($Lookup[$i]['Subtable'][$c]['BacktrackGlyphCount']) {
- $backtrackGlyphs = $Lookup[$i]['Subtable'][$c]['CoverageBacktrackGlyphs'];
- } else {
- $backtrackGlyphs = array();
- }
-
- $backtrackMatch = $this->_makeGSUBbacktrackMatch($backtrackGlyphs, $ignore);
- if ($Lookup[$i]['Subtable'][$c]['LookaheadGlyphCount']) {
- $lookaheadGlyphs = $Lookup[$i]['Subtable'][$c]['CoverageLookaheadGlyphs'];
- } else {
- $lookaheadGlyphs = array();
- }
-
- $lookaheadMatch = $this->_makeGSUBlookaheadMatch($lookaheadGlyphs, $ignore);
- $nBsubs = 2 * count($backtrackGlyphs);
- $nIsubs = (2 * $nInput) - 1;
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, array(), 0);
- $subRule = array('context' => 1, 'tag' => $tag, 'matchback' => $backtrackMatch, 'match' => ($contextInputMatch . $lookaheadMatch), 'nBacktrack' => count($backtrackGlyphs), 'nInput' => $nInput, 'nLookahead' => count($lookaheadGlyphs), 'rules' => array(),);
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['SubstCount']; $b++) {
- $lup = $Lookup[$i]['Subtable'][$c]['SubstLookupRecord'][$b]['LookupListIndex'];
- $seqIndex = $Lookup[$i]['Subtable'][$c]['SubstLookupRecord'][$b]['SequenceIndex'];
- for ($lus = 0; $lus < $Lookup[$lup]['SubtableCount']; $lus++) {
- if (count($Lookup[$lup]['Subtable'][$lus]['subs'])) {
- foreach ($Lookup[$lup]['Subtable'][$lus]['subs'] AS $luss) {
- $lookupGlyphs = $luss['Replace'];
- $mLen = count($lookupGlyphs);
-
-
-
- if (strpos($inputGlyphs[$seqIndex], $lookupGlyphs[0]) === false) {
- continue;
- }
-
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, $lookupGlyphs, $seqIndex);
- $REPL = implode(" ", $luss['substitute']);
- if (strpos("isol fina fin2 fin3 medi med2 init ", $tag) !== false && $scripttag == 'arab') {
- $volt[] = array('match' => $lookupGlyphs[0], 'replace' => $REPL, 'tag' => $tag, 'prel' => $backtrackGlyphs, 'postl' => $lookaheadGlyphs, 'ignore' => $ignore);
- } else {
- $subRule['rules'][] = array('type' => $Lookup[$lup]['Type'], 'match' => $lookupGlyphs, 'replace' => $luss['substitute'], 'seqIndex' => $seqIndex, 'key' => $lookupGlyphs[0],);
- }
- }
- }
- }
- }
- if (count($subRule['rules']))
- $volt[] = $subRule;
- }
- }
-
- else if ($Lookup[$i]['Type'] == 6) {
-
- if ($SubstFormat == 1) {
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- for ($s = 0; $s < $Lookup[$i]['Subtable'][$c]['ChainSubRuleSetCount']; $s++) {
-
- $subRule = array();
- $firstInputGlyph = $Lookup[$i]['Subtable'][$c]['CoverageGlyphs'][$s];
- foreach ($Lookup[$i]['Subtable'][$c]['ChainSubRuleSet'][$s]['ChainSubRule'] AS $rule) {
-
- $inputGlyphs = array();
- if ($rule['InputGlyphCount'] > 1) {
- $inputGlyphs = $rule['InputGlyphs'];
- }
- $inputGlyphs[0] = $firstInputGlyph;
- ksort($inputGlyphs);
- $nInput = count($inputGlyphs);
- if ($rule['BacktrackGlyphCount']) {
- $backtrackGlyphs = $rule['BacktrackGlyphs'];
- } else {
- $backtrackGlyphs = array();
- }
- $backtrackMatch = $this->_makeGSUBbacktrackMatch($backtrackGlyphs, $ignore);
- if ($rule['LookaheadGlyphCount']) {
- $lookaheadGlyphs = $rule['LookaheadGlyphs'];
- } else {
- $lookaheadGlyphs = array();
- }
- $lookaheadMatch = $this->_makeGSUBlookaheadMatch($lookaheadGlyphs, $ignore);
- $nBsubs = 2 * count($backtrackGlyphs);
- $nIsubs = (2 * $nInput) - 1;
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, array(), 0);
- $subRule = array('context' => 1, 'tag' => $tag, 'matchback' => $backtrackMatch, 'match' => ($contextInputMatch . $lookaheadMatch), 'nBacktrack' => count($backtrackGlyphs), 'nInput' => $nInput, 'nLookahead' => count($lookaheadGlyphs), 'rules' => array(),);
- for ($b = 0; $b < $rule['SubstCount']; $b++) {
- $lup = $rule['LookupListIndex'][$b];
- $seqIndex = $rule['SequenceIndex'][$b];
-
- for ($lus = 0; $lus < $Lookup[$lup]['SubtableCount']; $lus++) {
- if (count($Lookup[$lup]['Subtable'][$lus]['subs'])) {
- foreach ($Lookup[$lup]['Subtable'][$lus]['subs'] AS $luss) {
- $lookupGlyphs = $luss['Replace'];
- $mLen = count($lookupGlyphs);
-
-
-
- if (strpos($inputGlyphs[$seqIndex], $lookupGlyphs[0]) === false) {
- continue;
- }
-
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, $lookupGlyphs, $seqIndex);
- $REPL = implode(" ", $luss['substitute']);
- if (strpos("isol fina fin2 fin3 medi med2 init ", $tag) !== false && $scripttag == 'arab') {
- $volt[] = array('match' => $lookupGlyphs[0], 'replace' => $REPL, 'tag' => $tag, 'prel' => $backtrackGlyphs, 'postl' => $lookaheadGlyphs, 'ignore' => $ignore);
- } else {
- $subRule['rules'][] = array('type' => $Lookup[$lup]['Type'], 'match' => $lookupGlyphs, 'replace' => $luss['substitute'], 'seqIndex' => $seqIndex, 'key' => $lookupGlyphs[0],);
- }
- }
- }
- }
- }
- if (count($subRule['rules']))
- $volt[] = $subRule;
- }
- }
- }
-
- else if ($SubstFormat == 2) {
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- foreach ($Lookup[$i]['Subtable'][$c]['ChainSubClassSet'] AS $inputClass => $cscs) {
- for ($cscrule = 0; $cscrule < $cscs['ChainSubClassRuleCnt']; $cscrule++) {
- $rule = $cscs['ChainSubClassRule'][$cscrule];
-
-
-
-
-
-
- $inputGlyphs = array();
- if (isset($Lookup[$i]['Subtable'][$c]['InputClasses'][$inputClass])) {
- $inputGlyphs[0] = $Lookup[$i]['Subtable'][$c]['InputClasses'][$inputClass];
- } else {
- $inputGlyphs[0] = '';
- }
- if ($rule['InputGlyphCount'] > 1) {
-
- for ($gcl = 1; $gcl < $rule['InputGlyphCount']; $gcl++) {
- $classindex = $rule['Input'][$gcl];
- if (isset($Lookup[$i]['Subtable'][$c]['InputClasses'][$classindex])) {
- $inputGlyphs[$gcl] = $Lookup[$i]['Subtable'][$c]['InputClasses'][$classindex];
- }
-
-
- else {
- $inputGlyphs[$gcl] = '';
- }
- }
- }
- $nInput = $rule['InputGlyphCount'];
- if ($rule['BacktrackGlyphCount']) {
- for ($gcl = 0; $gcl < $rule['BacktrackGlyphCount']; $gcl++) {
- $classindex = $rule['Backtrack'][$gcl];
- if (isset($Lookup[$i]['Subtable'][$c]['BacktrackClasses'][$classindex])) {
- $backtrackGlyphs[$gcl] = $Lookup[$i]['Subtable'][$c]['BacktrackClasses'][$classindex];
- }
-
-
- else {
- $backtrackGlyphs[$gcl] = '';
- }
- }
- } else {
- $backtrackGlyphs = array();
- }
-
- $backtrackMatch = $this->_makeGSUBbacktrackMatch($backtrackGlyphs, $ignore);
- if ($rule['LookaheadGlyphCount']) {
- for ($gcl = 0; $gcl < $rule['LookaheadGlyphCount']; $gcl++) {
- $classindex = $rule['Lookahead'][$gcl];
- if (isset($Lookup[$i]['Subtable'][$c]['LookaheadClasses'][$classindex])) {
- $lookaheadGlyphs[$gcl] = $Lookup[$i]['Subtable'][$c]['LookaheadClasses'][$classindex];
- }
-
-
- else {
- $lookaheadGlyphs[$gcl] = '';
- }
- }
- } else {
- $lookaheadGlyphs = array();
- }
-
- $lookaheadMatch = $this->_makeGSUBlookaheadMatch($lookaheadGlyphs, $ignore);
- $nBsubs = 2 * count($backtrackGlyphs);
- $nIsubs = (2 * $nInput) - 1;
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, array(), 0);
- $subRule = array('context' => 1, 'tag' => $tag, 'matchback' => $backtrackMatch, 'match' => ($contextInputMatch . $lookaheadMatch), 'nBacktrack' => count($backtrackGlyphs), 'nInput' => $nInput, 'nLookahead' => count($lookaheadGlyphs), 'rules' => array(),);
- for ($b = 0; $b < $rule['SubstCount']; $b++) {
- $lup = $rule['LookupListIndex'][$b];
- $seqIndex = $rule['SequenceIndex'][$b];
-
- for ($lus = 0; $lus < $Lookup[$lup]['SubtableCount']; $lus++) {
- if (count($Lookup[$lup]['Subtable'][$lus]['subs'])) {
- foreach ($Lookup[$lup]['Subtable'][$lus]['subs'] AS $luss) {
- $lookupGlyphs = $luss['Replace'];
- $mLen = count($lookupGlyphs);
-
-
-
- if (strpos($inputGlyphs[$seqIndex], $lookupGlyphs[0]) === false) {
- continue;
- }
-
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, $lookupGlyphs, $seqIndex);
- $REPL = implode(" ", $luss['substitute']);
-
- if (strpos("isol fina fin2 fin3 medi med2 init ", $tag) !== false && $scripttag == 'arab') {
- $volt[] = array('match' => $lookupGlyphs[0], 'replace' => $REPL, 'tag' => $tag, 'prel' => $backtrackGlyphs, 'postl' => $lookaheadGlyphs, 'ignore' => $ignore);
- } else {
- $subRule['rules'][] = array('type' => $Lookup[$lup]['Type'], 'match' => $lookupGlyphs, 'replace' => $luss['substitute'], 'seqIndex' => $seqIndex, 'key' => $lookupGlyphs[0],);
- }
- }
- }
- }
- }
- if (count($subRule['rules']))
- $volt[] = $subRule;
- }
- }
- }
-
- else if ($SubstFormat == 3) {
-
- $ignore = $this->_getGSUBignoreString($Lookup[$i]['Flag'], $Lookup[$i]['MarkFilteringSet']);
- $inputGlyphs = $Lookup[$i]['Subtable'][$c]['CoverageInputGlyphs'];
- $CoverageInputGlyphs = implode('|', $inputGlyphs);
- $nInput = $Lookup[$i]['Subtable'][$c]['InputGlyphCount'];
- if ($Lookup[$i]['Subtable'][$c]['BacktrackGlyphCount']) {
- $backtrackGlyphs = $Lookup[$i]['Subtable'][$c]['CoverageBacktrackGlyphs'];
- } else {
- $backtrackGlyphs = array();
- }
-
- $backtrackMatch = $this->_makeGSUBbacktrackMatch($backtrackGlyphs, $ignore);
- if ($Lookup[$i]['Subtable'][$c]['LookaheadGlyphCount']) {
- $lookaheadGlyphs = $Lookup[$i]['Subtable'][$c]['CoverageLookaheadGlyphs'];
- } else {
- $lookaheadGlyphs = array();
- }
-
- $lookaheadMatch = $this->_makeGSUBlookaheadMatch($lookaheadGlyphs, $ignore);
- $nBsubs = 2 * count($backtrackGlyphs);
- $nIsubs = (2 * $nInput) - 1;
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, array(), 0);
- $subRule = array('context' => 1, 'tag' => $tag, 'matchback' => $backtrackMatch, 'match' => ($contextInputMatch . $lookaheadMatch), 'nBacktrack' => count($backtrackGlyphs), 'nInput' => $nInput, 'nLookahead' => count($lookaheadGlyphs), 'rules' => array(),);
- for ($b = 0; $b < $Lookup[$i]['Subtable'][$c]['SubstCount']; $b++) {
- $lup = $Lookup[$i]['Subtable'][$c]['SubstLookupRecord'][$b]['LookupListIndex'];
- $seqIndex = $Lookup[$i]['Subtable'][$c]['SubstLookupRecord'][$b]['SequenceIndex'];
- for ($lus = 0; $lus < $Lookup[$lup]['SubtableCount']; $lus++) {
- if (count($Lookup[$lup]['Subtable'][$lus]['subs'])) {
- foreach ($Lookup[$lup]['Subtable'][$lus]['subs'] AS $luss) {
- $lookupGlyphs = $luss['Replace'];
- $mLen = count($lookupGlyphs);
-
-
-
- if (strpos($inputGlyphs[$seqIndex], $lookupGlyphs[0]) === false) {
- continue;
- }
-
- $contextInputMatch = $this->_makeGSUBcontextInputMatch($inputGlyphs, $ignore, $lookupGlyphs, $seqIndex);
- $REPL = implode(" ", $luss['substitute']);
- if (strpos("isol fina fin2 fin3 medi med2 init ", $tag) !== false && $scripttag == 'arab') {
- $volt[] = array('match' => $lookupGlyphs[0], 'replace' => $REPL, 'tag' => $tag, 'prel' => $backtrackGlyphs, 'postl' => $lookaheadGlyphs, 'ignore' => $ignore);
- } else {
- $subRule['rules'][] = array('type' => $Lookup[$lup]['Type'], 'match' => $lookupGlyphs, 'replace' => $luss['substitute'], 'seqIndex' => $seqIndex, 'key' => $lookupGlyphs[0],);
- }
- }
- }
- }
- }
- if (count($subRule['rules']))
- $volt[] = $subRule;
- }
- }
- }
- }
- return $volt;
- }
-
-
-
- function _checkGSUBignore($flag, $glyph, $MarkFilteringSet)
- {
- $ignore = false;
-
- if ((($flag & 0x0008) == 0x0008 && ($flag & 0xFF00) == 0) && strpos($this->GlyphClassMarks, $glyph)) {
- $ignore = true;
- }
- if ((($flag & 0x0004) == 0x0004) && strpos($this->GlyphClassLigatures, $glyph)) {
- $ignore = true;
- }
- if ((($flag & 0x0002) == 0x0002) && strpos($this->GlyphClassBases, $glyph)) {
- $ignore = true;
- }
-
- if ($flag & 0xFF00) {
-
-
- if (strpos($this->MarkAttachmentType[($flag >> 8)], $glyph)) {
- $ignore = true;
- }
- }
-
- if (($flag & 0x0010) && strpos($this->MarkGlyphSets[$MarkFilteringSet], $glyph)) {
- $ignore = true;
- }
- return $ignore;
- }
- function _getGSUBignoreString($flag, $MarkFilteringSet)
- {
-
-
-
- $str = "";
- $ignoreflag = 0;
-
- if ($flag & 0xFF00) {
-
-
- $MarkAttachmentType = $flag >> 8;
- $ignoreflag = $flag;
- $str = $this->MarkAttachmentType[$MarkAttachmentType];
- }
-
- if ($flag & 0x0010) {
- throw new MpdfException("This font " . $this->fontkey . " contains MarkGlyphSets - Not tested yet");
- $str = $this->MarkGlyphSets[$MarkFilteringSet];
- }
-
-
- if (($flag & 0x0008) == 0x0008 && ($flag & 0xFF00) == 0) {
- $ignoreflag = 8;
- $str = $this->GlyphClassMarks;
- }
-
- if (($flag & 0x0004) == 0x0004) {
- $ignoreflag += 4;
- if ($str) {
- $str .= "|";
- }
- $str .= $this->GlyphClassLigatures;
- }
-
- if (($flag & 0x0002) == 0x0002) {
- $ignoreflag += 2;
- if ($str) {
- $str .= "|";
- }
- $str .= $this->GlyphClassBases;
- }
- if ($str) {
-
-
-
-
-
-
-
-
-
-
-
-
-
- return "((?:(?:" . $str . "))*)";
- } else
- return "()";
- }
-
-
- function _makeGSUBcontextInputMatch($inputGlyphs, $ignore, $lookupGlyphs, $seqIndex)
- {
-
-
-
-
- $mLen = count($lookupGlyphs);
- $nInput = count($inputGlyphs);
- $str = "";
- for ($i = 0; $i < $nInput; $i++) {
- if ($i > 0) {
- $str .= $ignore . " ";
- }
- if ($i >= $seqIndex && $i < ($seqIndex + $mLen)) {
- $str .= "(" . $lookupGlyphs[($i - $seqIndex)] . ")";
- } else {
- $str .= "(" . $inputGlyphs[($i)] . ")";
- }
- }
- return $str;
- }
- function _makeGSUBinputMatch($inputGlyphs, $ignore)
- {
-
-
-
-
- $str = "";
- for ($i = 1; $i <= count($inputGlyphs); $i++) {
- if ($i > 1) {
- $str .= $ignore . " ";
- }
- $str .= "(" . $inputGlyphs[($i - 1)] . ")";
- }
- return $str;
- }
- function _makeGSUBbacktrackMatch($backtrackGlyphs, $ignore)
- {
-
-
-
-
-
- $str = "";
- for ($i = (count($backtrackGlyphs) - 1); $i >= 0; $i--) {
- $str .= "(" . $backtrackGlyphs[$i] . ")" . $ignore . " ";
- }
- return $str;
- }
- function _makeGSUBlookaheadMatch($lookaheadGlyphs, $ignore)
- {
-
-
-
-
-
- $str = "";
- for ($i = 0; $i < count($lookaheadGlyphs); $i++) {
- $str .= $ignore . " (" . $lookaheadGlyphs[$i] . ")";
- }
- return $str;
- }
- function _makeGSUBinputReplacement($nInput, $REPL, $ignore, $nBsubs, $mLen, $seqIndex)
- {
-
-
-
-
-
-
-
- if ($ignore == "()") {
- $ign = false;
- } else {
- $ign = true;
- }
- $str = "";
- if ($nInput == 1) {
- $str = $REPL;
- } else if ($nInput > 1) {
- if ($mLen == $nInput) {
- $str = $REPL;
- if ($ign) {
-
- for ($x = 2; $x <= $nInput; $x++) {
- $str .= '\\' . ($nBsubs + (2 * ($x - 1)));
- }
- }
- } else {
- for ($x = 1; $x < ($seqIndex + 1); $x++) {
- if ($x == 1) {
- $str .= '\\' . ($nBsubs + 1);
- } else {
- if ($ign) {
- $str .= '\\' . ($nBsubs + (2 * ($x - 1)));
- }
- $str .= ' \\' . ($nBsubs + 1 + (2 * ($x - 1)));
- }
- }
- if ($seqIndex > 0) {
- $str .= " ";
- }
- $str .= $REPL;
- if ($ign) {
- for ($x = (max(($seqIndex + 1), 2)); $x < ($seqIndex + 1 + $mLen); $x++) {
- $str .= '\\' . ($nBsubs + (2 * ($x - 1)));
- }
- }
- for ($x = ($seqIndex + 1 + $mLen); $x <= $nInput; $x++) {
- if ($ign) {
- $str .= '\\' . ($nBsubs + (2 * ($x - 1)));
- }
- $str .= ' \\' . ($nBsubs + 1 + (2 * ($x - 1)));
- }
- }
- }
- return $str;
- }
-
- function _getCoverage($convert2hex = true, $mode = 1)
- {
- $g = array();
- $ctr = 0;
- $CoverageFormat = $this->read_ushort();
- if ($CoverageFormat == 1) {
- $CoverageGlyphCount = $this->read_ushort();
- for ($gid = 0; $gid < $CoverageGlyphCount; $gid++) {
- $glyphID = $this->read_ushort();
- $uni = $this->glyphToChar[$glyphID][0];
- if ($convert2hex) {
- $g[] = unicode_hex($uni);
- } else if ($mode == 2) {
- $g[$uni] = $ctr;
- $ctr++;
- } else {
- $g[] = $glyphID;
- }
- }
- }
- if ($CoverageFormat == 2) {
- $RangeCount = $this->read_ushort();
- for ($r = 0; $r < $RangeCount; $r++) {
- $start = $this->read_ushort();
- $end = $this->read_ushort();
- $StartCoverageIndex = $this->read_ushort();
- for ($glyphID = $start; $glyphID <= $end; $glyphID++) {
- $uni = $this->glyphToChar[$glyphID][0];
- if ($convert2hex) {
- $g[] = unicode_hex($uni);
- } else if ($mode == 2) {
- $uni = $g[$uni] = $ctr;
- $ctr++;
- } else {
- $g[] = $glyphID;
- }
- }
- }
- }
- return $g;
- }
-
- function _getClasses($offset)
- {
- $this->seek($offset);
- $ClassFormat = $this->read_ushort();
- $GlyphByClass = array();
- if ($ClassFormat == 1) {
- $StartGlyph = $this->read_ushort();
- $GlyphCount = $this->read_ushort();
- for ($i = 0; $i < $GlyphCount; $i++) {
- $startGlyphID = $StartGlyph + $i;
- $endGlyphID = $StartGlyph + $i;
- $class = $this->read_ushort();
- for ($g = $startGlyphID; $g <= $endGlyphID; $g++) {
- if (isset($this->glyphToChar[$g][0])) {
- $GlyphByClass[$class][] = unicode_hex($this->glyphToChar[$g][0]);
- }
- }
- }
- } else if ($ClassFormat == 2) {
- $tableCount = $this->read_ushort();
- for ($i = 0; $i < $tableCount; $i++) {
- $startGlyphID = $this->read_ushort();
- $endGlyphID = $this->read_ushort();
- $class = $this->read_ushort();
- for ($g = $startGlyphID; $g <= $endGlyphID; $g++) {
- if ($this->glyphToChar[$g][0]) {
- $GlyphByClass[$class][] = unicode_hex($this->glyphToChar[$g][0]);
- }
- }
- }
- }
- $gbc = array();
- foreach ($GlyphByClass AS $class => $garr) {
- $gbc[$class] = implode('|', $garr);
- }
- return $gbc;
- }
-
-
-
-
-
- function _getGPOStables()
- {
-
-
-
- if (isset($this->tables["GPOS"])) {
- $ffeats = array();
- $gpos_offset = $this->seek_table("GPOS");
- $this->skip(4);
- $ScriptList_offset = $gpos_offset + $this->read_ushort();
- $FeatureList_offset = $gpos_offset + $this->read_ushort();
- $LookupList_offset = $gpos_offset + $this->read_ushort();
-
- $this->seek($ScriptList_offset);
- $ScriptCount = $this->read_ushort();
- for ($i = 0; $i < $ScriptCount; $i++) {
- $ScriptTag = $this->read_tag();
- $ScriptTableOffset = $this->read_ushort();
- $ffeats[$ScriptTag] = $ScriptList_offset + $ScriptTableOffset;
- }
-
- foreach ($ffeats AS $t => $o) {
- $ls = array();
- $this->seek($o);
- $DefLangSys_offset = $this->read_ushort();
- if ($DefLangSys_offset > 0) {
- $ls['DFLT'] = $DefLangSys_offset + $o;
- }
- $LangSysCount = $this->read_ushort();
- for ($i = 0; $i < $LangSysCount; $i++) {
- $LangTag = $this->read_tag();
- $LangTableOffset = $this->read_ushort();
- $ls[$LangTag] = $o + $LangTableOffset;
- }
- $ffeats[$t] = $ls;
- }
-
-
- foreach ($ffeats AS $st => $scripts) {
- foreach ($scripts AS $t => $o) {
- $FeatureIndex = array();
- $langsystable_offset = $o;
- $this->seek($langsystable_offset);
- $LookUpOrder = $this->read_ushort();
- $ReqFeatureIndex = $this->read_ushort();
- if ($ReqFeatureIndex != 0xFFFF) {
- $FeatureIndex[] = $ReqFeatureIndex;
- }
- $FeatureCount = $this->read_ushort();
- for ($i = 0; $i < $FeatureCount; $i++) {
- $FeatureIndex[] = $this->read_ushort();
- }
- $ffeats[$st][$t] = $FeatureIndex;
- }
- }
-
- $this->seek($FeatureList_offset);
- $FeatureCount = $this->read_ushort();
- $Feature = array();
- for ($i = 0; $i < $FeatureCount; $i++) {
- $tag = $this->read_tag();
- if ($tag == 'kern') {
- $this->haskernGPOS = true;
- }
- $Feature[$i] = array('tag' => $tag);
- $Feature[$i]['offset'] = $FeatureList_offset + $this->read_ushort();
- }
- for ($i = 0; $i < $FeatureCount; $i++) {
- $this->seek($Feature[$i]['offset']);
- $this->read_ushort();
- $Feature[$i]['LookupCount'] = $Lookupcount = $this->read_ushort();
- $Feature[$i]['LookupListIndex'] = array();
- for ($c = 0; $c < $Lookupcount; $c++) {
- $Feature[$i]['LookupListIndex'][] = $this->read_ushort();
- }
- }
- foreach ($ffeats AS $st => $scripts) {
- foreach ($scripts AS $t => $o) {
- $FeatureIndex = $ffeats[$st][$t];
- foreach ($FeatureIndex AS $k => $fi) {
- $ffeats[$st][$t][$k] = $Feature[$fi];
- }
- }
- }
-
- $gpos = array();
- $GPOSScriptLang = array();
- foreach ($ffeats AS $st => $scripts) {
- foreach ($scripts AS $t => $langsys) {
- $lg = array();
- foreach ($langsys AS $ft) {
- $lg[$ft['LookupListIndex'][0]] = $ft;
- }
-
- ksort($lg);
- foreach ($lg AS $ft) {
- $gpos[$st][$t][$ft['tag']] = $ft['LookupListIndex'];
- }
- if (!isset($GPOSScriptLang[$st])) {
- $GPOSScriptLang[$st] = '';
- }
- $GPOSScriptLang[$st] .= $t . ' ';
- }
- }
-
-
- $this->seek($LookupList_offset);
- $LookupCount = $this->read_ushort();
- $Lookup = array();
- $Offsets = array();
- $SubtableCount = array();
- for ($i = 0; $i < $LookupCount; $i++) {
- $Offsets[$i] = $LookupList_offset + $this->read_ushort();
- }
- for ($i = 0; $i < $LookupCount; $i++) {
- $this->seek($Offsets[$i]);
- $Lookup[$i]['Type'] = $this->read_ushort();
- $Lookup[$i]['Flag'] = $flag = $this->read_ushort();
- $Lookup[$i]['SubtableCount'] = $SubtableCount[$i] = $this->read_ushort();
- for ($c = 0; $c < $SubtableCount[$i]; $c++) {
- $Lookup[$i]['Subtables'][$c] = $Offsets[$i] + $this->read_ushort();
- }
-
- if (($flag & 0x0010) == 0x0010) {
- $Lookup[$i]['MarkFilteringSet'] = $this->read_ushort();
- } else {
- $Lookup[$i]['MarkFilteringSet'] = '';
- }
-
- if ($Lookup[$i]['Type'] == 9) {
-
- for ($c = 0; $c < $SubtableCount[$i]; $c++) {
- $this->seek($Lookup[$i]['Subtables'][$c]);
- $ExtensionPosFormat = $this->read_ushort();
- $type = $this->read_ushort();
- $Lookup[$i]['Subtables'][$c] = $Lookup[$i]['Subtables'][$c] + $this->read_ulong();
- }
- $Lookup[$i]['Type'] = $type;
- }
- }
-
-
- $this->LuCoverage = array();
- for ($i = 0; $i < $LookupCount; $i++) {
- for ($c = 0; $c < $Lookup[$i]['SubtableCount']; $c++) {
- $this->seek($Lookup[$i]['Subtables'][$c]);
- $PosFormat = $this->read_ushort();
- if ($Lookup[$i]['Type'] == 7 && $PosFormat == 3) {
- $this->skip(4);
- } else if ($Lookup[$i]['Type'] == 8 && $PosFormat == 3) {
- $BacktrackGlyphCount = $this->read_ushort();
- $this->skip(2 * $BacktrackGlyphCount + 2);
- }
-
-
- $Coverage = $Lookup[$i]['Subtables'][$c] + $this->read_ushort();
- $this->seek($Coverage);
- $glyphs = $this->_getCoverage(false, 2);
- $this->LuCoverage[$i][$c] = $glyphs;
- }
- }
-
- $s = '<?php
- $LuCoverage = ' . var_export($this->LuCoverage, true) . ';
- ?>';
- file_put_contents(_MPDF_TTFONTDATAPATH . $this->fontkey . '.GPOSdata.php', $s);
- return array($GPOSScriptLang, $gpos, $Lookup);
- }
- }
-
-
-
-
-
-
-
- function makeSubset($file, &$subset, $TTCfontID = 0, $debug = false, $useOTL = false)
- {
- $this->useOTL = $useOTL;
- $this->filename = $file;
- $this->fh = fopen($file, 'rb');
- if (!$this->fh) {
- throw new MpdfException('Can\'t open file ' . $file);
- }
- $this->_pos = 0;
- $this->charWidths = '';
- $this->glyphPos = array();
- $this->charToGlyph = array();
- $this->tables = array();
- $this->otables = array();
- $this->ascent = 0;
- $this->descent = 0;
- $this->strikeoutSize = 0;
- $this->strikeoutPosition = 0;
- $this->numTTCFonts = 0;
- $this->TTCFonts = array();
- $this->skip(4);
- $this->maxUni = 0;
- if ($TTCfontID > 0) {
- $this->version = $version = $this->read_ulong();
- if (!in_array($version, array(0x00010000, 0x00020000))) {
- throw new MpdfException("ERROR - Error parsing TrueType Collection: version=" . $version . " - " . $file);
- }
- $this->numTTCFonts = $this->read_ulong();
- for ($i = 1; $i <= $this->numTTCFonts; $i++) {
- $this->TTCFonts[$i]['offset'] = $this->read_ulong();
- }
- $this->seek($this->TTCFonts[$TTCfontID]['offset']);
- $this->version = $version = $this->read_ulong();
- }
- $this->readTableDirectory($debug);
-
-
-
- $this->seek_table("head");
- $this->skip(50);
- $indexToLocFormat = $this->read_ushort();
- $glyphDataFormat = $this->read_ushort();
-
-
-
- $this->seek_table("hhea");
- $this->skip(32);
- $metricDataFormat = $this->read_ushort();
- $orignHmetrics = $numberOfHMetrics = $this->read_ushort();
-
-
-
- $this->seek_table("maxp");
- $this->skip(4);
- $numGlyphs = $this->read_ushort();
-
-
-
- $cmap_offset = $this->seek_table("cmap");
- $this->skip(2);
- $cmapTableCount = $this->read_ushort();
- $unicode_cmap_offset = 0;
- for ($i = 0; $i < $cmapTableCount; $i++) {
- $platformID = $this->read_ushort();
- $encodingID = $this->read_ushort();
- $offset = $this->read_ulong();
- $save_pos = $this->_pos;
- if (($platformID == 3 && $encodingID == 1) || $platformID == 0) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 4) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- break;
- }
- }
- $this->seek($save_pos);
- }
- if (!$unicode_cmap_offset) {
- throw new MpdfException('Font (' . $this->filename . ') does not have Unicode cmap (platform 3, encoding 1, format 4, or platform 0 [any encoding] format 4)');
- }
- $glyphToChar = array();
- $charToGlyph = array();
- $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph);
-
-
-
- if ($useOTL) {
- $bctr = 0xE000;
- for ($gid = 1; $gid < $numGlyphs; $gid++) {
- if (!isset($glyphToChar[$gid])) {
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
- if ($bctr > 0xF8FF) {
- throw new MpdfException($file . " : WARNING - Font cannot map all included glyphs into Private Use Area U+E000 - U+F8FF; cannot use useOTL on this font");
- }
- $glyphToChar[$gid][] = $bctr;
- $charToGlyph[$bctr] = $gid;
- $bctr++;
- }
- }
- }
-
- $this->charToGlyph = $charToGlyph;
- $this->glyphToChar = $glyphToChar;
-
-
-
- $scale = 1;
- $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale);
-
-
-
- $this->getLOCA($indexToLocFormat, $numGlyphs);
- $subsetglyphs = array(0 => 0, 1 => 1, 2 => 2);
- $subsetCharToGlyph = array();
- foreach ($subset AS $code) {
- if (isset($this->charToGlyph[$code])) {
- $subsetglyphs[$this->charToGlyph[$code]] = $code;
- $subsetCharToGlyph[$code] = $this->charToGlyph[$code];
- }
- $this->maxUni = max($this->maxUni, $code);
- }
- list($start, $dummy) = $this->get_table_pos('glyf');
- $glyphSet = array();
- ksort($subsetglyphs);
- $n = 0;
- $fsLastCharIndex = 0;
- foreach ($subsetglyphs AS $originalGlyphIdx => $uni) {
- $fsLastCharIndex = max($fsLastCharIndex, $uni);
- $glyphSet[$originalGlyphIdx] = $n;
- $n++;
- }
- ksort($subsetCharToGlyph);
- foreach ($subsetCharToGlyph AS $uni => $originalGlyphIdx) {
- $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx];
- }
- $this->codeToGlyph = $codeToGlyph;
- ksort($subsetglyphs);
- foreach ($subsetglyphs AS $originalGlyphIdx => $uni) {
- $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs);
- }
- $numGlyphs = $numberOfHMetrics = count($subsetglyphs);
-
-
-
-
-
-
-
- $this->add('name', $this->get_table('name'));
-
-
-
- $tags = array('cvt ', 'fpgm', 'prep', 'gasp');
- foreach ($tags AS $tag) {
- if (isset($this->tables[$tag])) {
- $this->add($tag, $this->get_table($tag));
- }
- }
-
-
-
- if (isset($this->tables['post'])) {
- $opost = $this->get_table('post');
- $post = "\x00\x03\x00\x00" . substr($opost, 4, 12) . "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
- $this->add('post', $post);
- }
-
-
-
- ksort($codeToGlyph);
- unset($codeToGlyph[0]);
-
- $rangeid = 0;
- $range = array();
- $prevcid = -2;
- $prevglidx = -1;
-
- foreach ($codeToGlyph as $cid => $glidx) {
- if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) {
- $range[$rangeid][] = $glidx;
- } else {
-
- $rangeid = $cid;
- $range[$rangeid] = array();
- $range[$rangeid][] = $glidx;
- }
- $prevcid = $cid;
- $prevglidx = $glidx;
- }
-
-
-
-
- $segCount = count($range) + 1;
- $searchRange = 1;
- $entrySelector = 0;
- while ($searchRange * 2 <= $segCount) {
- $searchRange = $searchRange * 2;
- $entrySelector = $entrySelector + 1;
- }
- $searchRange = $searchRange * 2;
- $rangeShift = $segCount * 2 - $searchRange;
- $length = 16 + (8 * $segCount ) + ($numGlyphs + 1);
- $cmap = array(0, 3,
- 0, 0,
- 0, 28,
- 0, 3,
- 0, 28,
- 3, 1,
- 0, 28,
- 4, $length, 0,
- $segCount * 2,
- $searchRange,
- $entrySelector,
- $rangeShift);
-
- foreach ($range AS $start => $subrange) {
- $endCode = $start + (count($subrange) - 1);
- $cmap[] = $endCode;
- }
- $cmap[] = 0xFFFF;
- $cmap[] = 0;
-
- foreach ($range AS $start => $subrange) {
- $cmap[] = $start;
- }
- $cmap[] = 0xFFFF;
-
- foreach ($range AS $start => $subrange) {
- $idDelta = -($start - $subrange[0]);
- $n += count($subrange);
- $cmap[] = $idDelta;
- }
- $cmap[] = 1;
-
- foreach ($range AS $subrange) {
- $cmap[] = 0;
- }
- $cmap[] = 0;
- foreach ($range AS $subrange) {
- foreach ($subrange AS $glidx) {
- $cmap[] = $glidx;
- }
- }
- $cmap[] = 0;
- $cmapstr = '';
- foreach ($cmap AS $cm) {
- $cmapstr .= pack("n", $cm);
- }
- $this->add('cmap', $cmapstr);
-
-
-
- list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf');
- if ($glyfLength < $this->maxStrLenRead) {
- $glyphData = $this->get_table('glyf');
- }
- $offsets = array();
- $glyf = '';
- $pos = 0;
- $hmtxstr = '';
- $xMinT = 0;
- $yMinT = 0;
- $xMaxT = 0;
- $yMaxT = 0;
- $advanceWidthMax = 0;
- $minLeftSideBearing = 0;
- $minRightSideBearing = 0;
- $xMaxExtent = 0;
- $maxPoints = 0;
- $maxContours = 0;
- $maxComponentPoints = 0;
- $maxComponentContours = 0;
- $maxComponentElements = 0;
- $maxComponentDepth = 0;
- $this->glyphdata = array();
- foreach ($subsetglyphs AS $originalGlyphIdx => $uni) {
-
- $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx);
- $hmtxstr .= $hm;
- $offsets[] = $pos;
- $glyphPos = $this->glyphPos[$originalGlyphIdx];
- $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos;
- if ($glyfLength < $this->maxStrLenRead) {
- $data = substr($glyphData, $glyphPos, $glyphLen);
- } else {
- if ($glyphLen > 0)
- $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen);
- else
- $data = '';
- }
- if ($glyphLen > 0) {
- if (_RECALC_PROFILE) {
- $xMin = $this->unpack_short(substr($data, 2, 2));
- $yMin = $this->unpack_short(substr($data, 4, 2));
- $xMax = $this->unpack_short(substr($data, 6, 2));
- $yMax = $this->unpack_short(substr($data, 8, 2));
- $xMinT = min($xMinT, $xMin);
- $yMinT = min($yMinT, $yMin);
- $xMaxT = max($xMaxT, $xMax);
- $yMaxT = max($yMaxT, $yMax);
- $aw = $this->unpack_short(substr($hm, 0, 2));
- $lsb = $this->unpack_short(substr($hm, 2, 2));
- $advanceWidthMax = max($advanceWidthMax, $aw);
- $minLeftSideBearing = min($minLeftSideBearing, $lsb);
- $minRightSideBearing = min($minRightSideBearing, ($aw - $lsb - ($xMax - $xMin)));
- $xMaxExtent = max($xMaxExtent, ($lsb + ($xMax - $xMin)));
- }
- $up = unpack("n", substr($data, 0, 2));
- }
- if ($glyphLen > 2 && ($up[1] & (1 << 15))) {
- $pos_in_glyph = 10;
- $flags = GF_MORE;
- $nComponentElements = 0;
- while ($flags & GF_MORE) {
- $nComponentElements += 1;
- $up = unpack("n", substr($data, $pos_in_glyph, 2));
- $flags = $up[1];
- $up = unpack("n", substr($data, $pos_in_glyph + 2, 2));
- $glyphIdx = $up[1];
- $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx;
- $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]);
- $pos_in_glyph += 4;
- if ($flags & GF_WORDS) {
- $pos_in_glyph += 4;
- } else {
- $pos_in_glyph += 2;
- }
- if ($flags & GF_SCALE) {
- $pos_in_glyph += 2;
- } else if ($flags & GF_XYSCALE) {
- $pos_in_glyph += 4;
- } else if ($flags & GF_TWOBYTWO) {
- $pos_in_glyph += 8;
- }
- }
- $maxComponentElements = max($maxComponentElements, $nComponentElements);
- }
-
- else if (_RECALC_PROFILE && $glyphLen > 2 && $up[1] < (1 << 15) && $up[1] > 0) {
- $nContours = $up[1];
- $this->glyphdata[$originalGlyphIdx]['nContours'] = $nContours;
- $maxContours = max($maxContours, $nContours);
-
- $pos_in_glyph = 10 + ($nContours * 2) - 2;
- $up = unpack("n", substr($data, $pos_in_glyph, 2));
- $points = $up[1] + 1;
- $this->glyphdata[$originalGlyphIdx]['nPoints'] = $points;
- $maxPoints = max($maxPoints, $points);
- }
- $glyf .= $data;
- $pos += $glyphLen;
- if ($pos % 4 != 0) {
- $padding = 4 - ($pos % 4);
- $glyf .= str_repeat("\0", $padding);
- $pos += $padding;
- }
- }
- if (_RECALC_PROFILE) {
- foreach ($this->glyphdata AS $originalGlyphIdx => $val) {
- $maxdepth = $depth = -1;
- $points = 0;
- $contours = 0;
- $this->getGlyphData($originalGlyphIdx, $maxdepth, $depth, $points, $contours);
- $maxComponentDepth = max($maxComponentDepth, $maxdepth);
- $maxComponentPoints = max($maxComponentPoints, $points);
- $maxComponentContours = max($maxComponentContours, $contours);
- }
- }
- $offsets[] = $pos;
- $this->add('glyf', $glyf);
-
-
-
- $this->add('hmtx', $hmtxstr);
-
-
-
- $locastr = '';
- if ((($pos + 1) >> 1) > 0xFFFF) {
- $indexToLocFormat = 1;
- foreach ($offsets AS $offset) {
- $locastr .= pack("N", $offset);
- }
- } else {
- $indexToLocFormat = 0;
- foreach ($offsets AS $offset) {
- $locastr .= pack("n", ($offset / 2));
- }
- }
- $this->add('loca', $locastr);
-
-
-
- $head = $this->get_table('head');
- $head = $this->_set_ushort($head, 50, $indexToLocFormat);
- if (_RECALC_PROFILE) {
- $head = $this->_set_short($head, 36, $xMinT);
- $head = $this->_set_short($head, 38, $yMinT);
- $head = $this->_set_short($head, 40, $xMaxT);
- $head = $this->_set_short($head, 42, $yMaxT);
- $head[17] = chr($head[17] & ~(1 << 4));
- }
- $this->add('head', $head);
-
-
-
- $hhea = $this->get_table('hhea');
- $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics);
- if (_RECALC_PROFILE) {
- $hhea = $this->_set_ushort($hhea, 10, $advanceWidthMax);
- $hhea = $this->_set_short($hhea, 12, $minLeftSideBearing);
- $hhea = $this->_set_short($hhea, 14, $minRightSideBearing);
- $hhea = $this->_set_short($hhea, 16, $xMaxExtent);
- }
- $this->add('hhea', $hhea);
-
-
-
- $maxp = $this->get_table('maxp');
- $maxp = $this->_set_ushort($maxp, 4, $numGlyphs);
- if (_RECALC_PROFILE) {
- $maxp = $this->_set_ushort($maxp, 6, $maxPoints);
- $maxp = $this->_set_ushort($maxp, 8, $maxContours);
- $maxp = $this->_set_ushort($maxp, 10, $maxComponentPoints);
- $maxp = $this->_set_ushort($maxp, 12, $maxComponentContours);
- $maxp = $this->_set_ushort($maxp, 28, $maxComponentElements);
- $maxp = $this->_set_ushort($maxp, 30, $maxComponentDepth);
- }
- $this->add('maxp', $maxp);
-
-
-
- if (isset($this->tables['OS/2'])) {
- $os2_offset = $this->seek_table("OS/2");
- if (_RECALC_PROFILE) {
- $fsSelection = $this->get_ushort($os2_offset + 62);
- $fsSelection = ($fsSelection & ~(1 << 6));
-
-
-
- }
-
-
- $nonBMP = $this->get_ushort($os2_offset + 46);
- $nonBMP = ($nonBMP & ~(1 << 9));
- $os2 = $this->get_table('OS/2');
- if (_RECALC_PROFILE) {
- $os2 = $this->_set_ushort($os2, 62, $fsSelection);
- $os2 = $this->_set_ushort($os2, 66, $fsLastCharIndex);
- $os2 = $this->_set_ushort($os2, 42, 0x0000);
- $os2 = $this->_set_ushort($os2, 44, 0x0000);
- $os2 = $this->_set_ushort($os2, 46, $nonBMP);
- $os2 = $this->_set_ushort($os2, 48, 0x0000);
- $os2 = $this->_set_ushort($os2, 50, 0x0000);
- $os2 = $this->_set_ushort($os2, 52, 0x0000);
- $os2 = $this->_set_ushort($os2, 54, 0x0000);
- $os2 = $this->_set_ushort($os2, 56, 0x0000);
- }
- $os2 = $this->_set_ushort($os2, 46, $nonBMP);
- $this->add('OS/2', $os2);
- }
- fclose($this->fh);
-
- $stm = '';
- $this->endTTFile($stm);
-
- return $stm;
- }
-
-
- function makeSubsetSIP($file, &$subset, $TTCfontID = 0, $debug = false, $useOTL = 0)
- {
- $this->fh = fopen($file, 'rb');
- if (!$this->fh) {
- throw new MpdfException('Can\'t open file ' . $file);
- }
- $this->filename = $file;
- $this->_pos = 0;
- $this->useOTL = $useOTL;
- $this->charWidths = '';
- $this->glyphPos = array();
- $this->charToGlyph = array();
- $this->tables = array();
- $this->otables = array();
- $this->ascent = 0;
- $this->descent = 0;
- $this->strikeoutSize = 0;
- $this->strikeoutPosition = 0;
- $this->numTTCFonts = 0;
- $this->TTCFonts = array();
- $this->skip(4);
- if ($TTCfontID > 0) {
- $this->version = $version = $this->read_ulong();
- if (!in_array($version, array(0x00010000, 0x00020000))) {
- throw new MpdfException("ERROR - Error parsing TrueType Collection: version=" . $version . " - " . $file);
- }
- $this->numTTCFonts = $this->read_ulong();
- for ($i = 1; $i <= $this->numTTCFonts; $i++) {
- $this->TTCFonts[$i]['offset'] = $this->read_ulong();
- }
- $this->seek($this->TTCFonts[$TTCfontID]['offset']);
- $this->version = $version = $this->read_ulong();
- }
- $this->readTableDirectory($debug);
-
-
-
- $this->seek_table("head");
- $this->skip(50);
- $indexToLocFormat = $this->read_ushort();
- $glyphDataFormat = $this->read_ushort();
-
-
-
- $this->seek_table("hhea");
- $this->skip(32);
- $metricDataFormat = $this->read_ushort();
- $orignHmetrics = $numberOfHMetrics = $this->read_ushort();
-
-
-
- $this->seek_table("maxp");
- $this->skip(4);
- $numGlyphs = $this->read_ushort();
-
-
-
- $cmap_offset = $this->seek_table("cmap");
- $this->skip(2);
- $cmapTableCount = $this->read_ushort();
- $unicode_cmap_offset = 0;
- for ($i = 0; $i < $cmapTableCount; $i++) {
- $platformID = $this->read_ushort();
- $encodingID = $this->read_ushort();
- $offset = $this->read_ulong();
- $save_pos = $this->_pos;
- if (($platformID == 3 && $encodingID == 10) || $platformID == 0) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 12) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- break;
- }
- }
-
- if (($platformID == 3 && $encodingID == 1) || $platformID == 0) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 4) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- }
- }
- $this->seek($save_pos);
- }
- if (!$unicode_cmap_offset) {
- throw new MpdfException('Font does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)');
- }
-
- if ($format == 12) {
- $this->maxUniChar = 0;
- $this->seek($unicode_cmap_offset + 4);
- $length = $this->read_ulong();
- $limit = $unicode_cmap_offset + $length;
- $this->skip(4);
- $nGroups = $this->read_ulong();
- $glyphToChar = array();
- $charToGlyph = array();
- for ($i = 0; $i < $nGroups; $i++) {
- $startCharCode = $this->read_ulong();
- $endCharCode = $this->read_ulong();
- $startGlyphCode = $this->read_ulong();
- $offset = 0;
- for ($unichar = $startCharCode; $unichar <= $endCharCode; $unichar++) {
- $glyph = $startGlyphCode + $offset;
- $offset++;
-
- if ($unichar < 0x30000) {
- $charToGlyph[$unichar] = $glyph;
- $this->maxUniChar = max($unichar, $this->maxUniChar);
- $glyphToChar[$glyph][] = $unichar;
- }
- }
- }
- }
-
- else {
- $glyphToChar = array();
- $charToGlyph = array();
- $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph);
- }
-
-
-
- if ($useOTL) {
- $bctr = 0xE000;
- for ($gid = 1; $gid < $numGlyphs; $gid++) {
- if (!isset($glyphToChar[$gid])) {
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
-
- if ($bctr > 0xF8FF && $bctr < 0x2CEB0) {
- $bctr = 0x2CEB0;
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
- }
- $glyphToChar[$gid][] = $bctr;
- $charToGlyph[$bctr] = $gid;
- $this->maxUniChar = max($bctr, $this->maxUniChar);
- $bctr++;
- }
- }
- }
-
-
-
-
- $scale = 1;
- $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale);
-
-
-
- $this->getLOCA($indexToLocFormat, $numGlyphs);
-
- $glyphMap = array(0 => 0);
- $glyphSet = array(0 => 0);
- $codeToGlyph = array();
-
- if (isset($charToGlyph[0x3F])) {
- $subs = $charToGlyph[0x3F];
- }
- else {
- $subs = $charToGlyph[32];
- }
- foreach ($subset AS $code) {
- if (isset($charToGlyph[$code]))
- $originalGlyphIdx = $charToGlyph[$code];
- else if ($code < 128) {
- $originalGlyphIdx = $subs;
- } else {
- $originalGlyphIdx = 0;
- }
- if (!isset($glyphSet[$originalGlyphIdx])) {
- $glyphSet[$originalGlyphIdx] = count($glyphMap);
- $glyphMap[] = $originalGlyphIdx;
- }
- $codeToGlyph[$code] = $glyphSet[$originalGlyphIdx];
- }
- list($start, $dummy) = $this->get_table_pos('glyf');
- $n = 0;
- while ($n < count($glyphMap)) {
- $originalGlyphIdx = $glyphMap[$n];
- $glyphPos = $this->glyphPos[$originalGlyphIdx];
- $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos;
- $n += 1;
- if (!$glyphLen)
- continue;
- $this->seek($start + $glyphPos);
- $numberOfContours = $this->read_short();
- if ($numberOfContours < 0) {
- $this->skip(8);
- $flags = GF_MORE;
- while ($flags & GF_MORE) {
- $flags = $this->read_ushort();
- $glyphIdx = $this->read_ushort();
- if (!isset($glyphSet[$glyphIdx])) {
- $glyphSet[$glyphIdx] = count($glyphMap);
- $glyphMap[] = $glyphIdx;
- }
- if ($flags & GF_WORDS)
- $this->skip(4);
- else
- $this->skip(2);
- if ($flags & GF_SCALE)
- $this->skip(2);
- else if ($flags & GF_XYSCALE)
- $this->skip(4);
- else if ($flags & GF_TWOBYTWO)
- $this->skip(8);
- }
- }
- }
- $numGlyphs = $n = count($glyphMap);
- $numberOfHMetrics = $n;
-
-
-
-
-
-
-
-
- $name = $this->get_table('name');
- $name_offset = $this->seek_table("name");
- $format = $this->read_ushort();
- $numRecords = $this->read_ushort();
- $string_data_offset = $name_offset + $this->read_ushort();
- for ($i = 0; $i < $numRecords; $i++) {
- $platformId = $this->read_ushort();
- $encodingId = $this->read_ushort();
- if ($platformId == 3 && $encodingId == 1) {
- $pos = 6 + ($i * 12) + 2;
- $name = $this->_set_ushort($name, $pos, 0x00);
- }
- $this->skip(8);
- }
- $this->add('name', $name);
-
-
-
- if (isset($this->tables['OS/2'])) {
- $os2 = $this->get_table('OS/2');
- $os2 = $this->_set_ushort($os2, 42, 0x00);
- $os2 = $this->_set_ushort($os2, 44, 0x00);
- $os2 = $this->_set_ushort($os2, 46, 0x00);
- $os2 = $this->_set_ushort($os2, 48, 0x00);
- $os2 = $this->_set_ushort($os2, 50, 0x00);
- $os2 = $this->_set_ushort($os2, 52, 0x00);
- $os2 = $this->_set_ushort($os2, 54, 0x00);
- $os2 = $this->_set_ushort($os2, 56, 0x00);
-
- $os2 = $this->_set_ushort($os2, 78, 0x8000);
- $os2 = $this->_set_ushort($os2, 80, 0x0000);
- $os2 = $this->_set_ushort($os2, 82, 0x0000);
- $os2 = $this->_set_ushort($os2, 84, 0x0000);
- $os2 = $this->_set_ushort($os2, 64, 0x01);
- $os2 = $this->_set_ushort($os2, 66, count($subset));
-
- $os2 = $this->splice($os2, 32, chr(5) . chr(0) . chr(1) . chr(0) . chr(1) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0));
- $this->add('OS/2', $os2);
- }
-
-
-
- $tags = array('cvt ', 'fpgm', 'prep', 'gasp');
- foreach ($tags AS $tag) {
- if (isset($this->tables[$tag])) {
- $this->add($tag, $this->get_table($tag));
- }
- }
-
-
-
- if (isset($this->tables['post'])) {
- $opost = $this->get_table('post');
- $post = "\x00\x03\x00\x00" . substr($opost, 4, 12) . "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
- }
- $this->add('post', $post);
-
-
-
- $hhea = $this->get_table('hhea');
- $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics);
- $this->add('hhea', $hhea);
-
-
-
- $maxp = $this->get_table('maxp');
- $maxp = $this->_set_ushort($maxp, 4, $numGlyphs);
- $this->add('maxp', $maxp);
-
-
-
-
-
-
- $rangeid = 0;
- $range = array();
- $prevcid = -2;
- $prevglidx = -1;
-
- foreach ($subset as $cid => $code) {
- $glidx = $codeToGlyph[$code];
- if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) {
- $range[$rangeid][] = $glidx;
- } else {
-
- $rangeid = $cid;
- $range[$rangeid] = array();
- $range[$rangeid][] = $glidx;
- }
- $prevcid = $cid;
- $prevglidx = $glidx;
- }
-
- $segCount = count($range) + 1;
- $searchRange = 1;
- $entrySelector = 0;
- while ($searchRange * 2 <= $segCount) {
- $searchRange = $searchRange * 2;
- $entrySelector = $entrySelector + 1;
- }
- $searchRange = $searchRange * 2;
- $rangeShift = $segCount * 2 - $searchRange;
- $length = 16 + (8 * $segCount ) + ($numGlyphs + 1);
- $cmap = array(
- 4, $length, 0,
- $segCount * 2,
- $searchRange,
- $entrySelector,
- $rangeShift);
-
- foreach ($range AS $start => $subrange) {
- $endCode = $start + (count($subrange) - 1);
- $cmap[] = $endCode;
- }
- $cmap[] = 0xFFFF;
- $cmap[] = 0;
-
- foreach ($range AS $start => $subrange) {
- $cmap[] = $start;
- }
- $cmap[] = 0xFFFF;
-
- foreach ($range AS $start => $subrange) {
- $idDelta = -($start - $subrange[0]);
- $n += count($subrange);
- $cmap[] = $idDelta;
- }
- $cmap[] = 1;
-
- foreach ($range AS $subrange) {
- $cmap[] = 0;
- }
- $cmap[] = 0;
- foreach ($range AS $subrange) {
- foreach ($subrange AS $glidx) {
- $cmap[] = $glidx;
- }
- }
- $cmap[] = 0;
- $cmapstr4 = '';
- foreach ($cmap AS $cm) {
- $cmapstr4 .= pack("n", $cm);
- }
-
-
-
- $entryCount = count($subset);
- $length = 10 + $entryCount * 2;
- $off = 20 + $length;
- $hoff = $off >> 16;
- $loff = $off & 0xFFFF;
- $cmap = array(0, 2,
- 1, 0,
- 0, 20,
- 3, 0,
- $hoff, $loff,
- 6, $length,
- 0, 1,
- $entryCount
- );
- $cmapstr = '';
- foreach ($subset AS $code) {
- $cmap[] = $codeToGlyph[$code];
- }
- foreach ($cmap AS $cm) {
- $cmapstr .= pack("n", $cm);
- }
- $cmapstr .= $cmapstr4;
- $this->add('cmap', $cmapstr);
-
-
-
- $hmtxstr = '';
- for ($n = 0; $n < $numGlyphs; $n++) {
- $originalGlyphIdx = $glyphMap[$n];
- $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx);
- $hmtxstr .= $hm;
- }
- $this->add('hmtx', $hmtxstr);
-
-
-
- list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf');
- if ($glyfLength < $this->maxStrLenRead) {
- $glyphData = $this->get_table('glyf');
- }
- $offsets = array();
- $glyf = '';
- $pos = 0;
- for ($n = 0; $n < $numGlyphs; $n++) {
- $offsets[] = $pos;
- $originalGlyphIdx = $glyphMap[$n];
- $glyphPos = $this->glyphPos[$originalGlyphIdx];
- $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos;
- if ($glyfLength < $this->maxStrLenRead) {
- $data = substr($glyphData, $glyphPos, $glyphLen);
- } else {
- if ($glyphLen > 0)
- $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen);
- else
- $data = '';
- }
- if ($glyphLen > 0)
- $up = unpack("n", substr($data, 0, 2));
- if ($glyphLen > 2 && ($up[1] & (1 << 15))) {
- $pos_in_glyph = 10;
- $flags = GF_MORE;
- while ($flags & GF_MORE) {
- $up = unpack("n", substr($data, $pos_in_glyph, 2));
- $flags = $up[1];
- $up = unpack("n", substr($data, $pos_in_glyph + 2, 2));
- $glyphIdx = $up[1];
- $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]);
- $pos_in_glyph += 4;
- if ($flags & GF_WORDS) {
- $pos_in_glyph += 4;
- } else {
- $pos_in_glyph += 2;
- }
- if ($flags & GF_SCALE) {
- $pos_in_glyph += 2;
- } else if ($flags & GF_XYSCALE) {
- $pos_in_glyph += 4;
- } else if ($flags & GF_TWOBYTWO) {
- $pos_in_glyph += 8;
- }
- }
- }
- $glyf .= $data;
- $pos += $glyphLen;
- if ($pos % 4 != 0) {
- $padding = 4 - ($pos % 4);
- $glyf .= str_repeat("\0", $padding);
- $pos += $padding;
- }
- }
- $offsets[] = $pos;
- $this->add('glyf', $glyf);
-
-
-
- $locastr = '';
- if ((($pos + 1) >> 1) > 0xFFFF) {
- $indexToLocFormat = 1;
- foreach ($offsets AS $offset) {
- $locastr .= pack("N", $offset);
- }
- } else {
- $indexToLocFormat = 0;
- foreach ($offsets AS $offset) {
- $locastr .= pack("n", ($offset / 2));
- }
- }
- $this->add('loca', $locastr);
-
-
-
- $head = $this->get_table('head');
- $head = $this->_set_ushort($head, 50, $indexToLocFormat);
- $this->add('head', $head);
- fclose($this->fh);
-
- $stm = '';
- $this->endTTFile($stm);
-
- return $stm;
- }
-
-
- function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours)
- {
- $depth++;
- $maxdepth = max($maxdepth, $depth);
- if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) {
- foreach ($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) {
- $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours);
- }
- } else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) {
- $contours += $this->glyphdata[$originalGlyphIdx]['nContours'];
- $points += $this->glyphdata[$originalGlyphIdx]['nPoints'];
- }
- $depth--;
- }
-
-
- function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs)
- {
- $glyphPos = $this->glyphPos[$originalGlyphIdx];
- $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos;
- if (!$glyphLen) {
- return;
- }
- $this->seek($start + $glyphPos);
- $numberOfContours = $this->read_short();
- if ($numberOfContours < 0) {
- $this->skip(8);
- $flags = GF_MORE;
- while ($flags & GF_MORE) {
- $flags = $this->read_ushort();
- $glyphIdx = $this->read_ushort();
- if (!isset($glyphSet[$glyphIdx])) {
- $glyphSet[$glyphIdx] = count($subsetglyphs);
- $subsetglyphs[$glyphIdx] = true;
- }
- $savepos = ftell($this->fh);
- $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs);
- $this->seek($savepos);
- if ($flags & GF_WORDS)
- $this->skip(4);
- else
- $this->skip(2);
- if ($flags & GF_SCALE)
- $this->skip(2);
- else if ($flags & GF_XYSCALE)
- $this->skip(4);
- else if ($flags & GF_TWOBYTWO)
- $this->skip(8);
- }
- }
- }
-
- function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale)
- {
- $start = $this->seek_table("hmtx");
- $aw = 0;
- $this->charWidths = str_pad('', 256 * 256 * 2, "\x00");
- if ($this->maxUniChar > 65536) {
- $this->charWidths .= str_pad('', 256 * 256 * 2, "\x00");
- }
- if ($this->maxUniChar > 131072) {
- $this->charWidths .= str_pad('', 256 * 256 * 2, "\x00");
- }
- $nCharWidths = 0;
- if (($numberOfHMetrics * 4) < $this->maxStrLenRead) {
- $data = $this->get_chunk($start, ($numberOfHMetrics * 4));
- $arr = unpack("n*", $data);
- } else {
- $this->seek($start);
- }
- for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) {
- if (($numberOfHMetrics * 4) < $this->maxStrLenRead) {
- $aw = $arr[($glyph * 2) + 1];
- } else {
- $aw = $this->read_ushort();
- $lsb = $this->read_ushort();
- }
- if (isset($glyphToChar[$glyph]) || $glyph == 0) {
- if ($aw >= (1 << 15)) {
- $aw = 0;
- }
-
- if ($glyph == 0) {
- $this->defaultWidth = $scale * $aw;
- continue;
- }
- foreach ($glyphToChar[$glyph] AS $char) {
- if ($char != 0 && $char != 65535) {
- $w = intval(round($scale * $aw));
- if ($w == 0) {
- $w = 65535;
- }
- if ($char < 196608) {
- $this->charWidths[$char * 2] = chr($w >> 8);
- $this->charWidths[$char * 2 + 1] = chr($w & 0xFF);
- $nCharWidths++;
- }
- }
- }
- }
- }
- $data = $this->get_chunk(($start + $numberOfHMetrics * 4), ($numGlyphs * 2));
- $arr = unpack("n*", $data);
- $diff = $numGlyphs - $numberOfHMetrics;
- $w = intval(round($scale * $aw));
- if ($w == 0) {
- $w = 65535;
- }
- for ($pos = 0; $pos < $diff; $pos++) {
- $glyph = $pos + $numberOfHMetrics;
- if (isset($glyphToChar[$glyph])) {
- foreach ($glyphToChar[$glyph] AS $char) {
- if ($char != 0 && $char != 65535) {
- if ($char < 196608) {
- $this->charWidths[$char * 2] = chr($w >> 8);
- $this->charWidths[$char * 2 + 1] = chr($w & 0xFF);
- $nCharWidths++;
- }
- }
- }
- }
- }
-
-
- $this->charWidths[0] = chr($nCharWidths >> 8);
- $this->charWidths[1] = chr($nCharWidths & 0xFF);
- }
- function getHMetric($numberOfHMetrics, $gid)
- {
- $start = $this->seek_table("hmtx");
- if ($gid < $numberOfHMetrics) {
- $this->seek($start + ($gid * 4));
- $hm = fread($this->fh, 4);
- } else {
- $this->seek($start + (($numberOfHMetrics - 1) * 4));
- $hm = fread($this->fh, 2);
- $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2));
- $hm .= fread($this->fh, 2);
- }
- return $hm;
- }
- function getLOCA($indexToLocFormat, $numGlyphs)
- {
- $start = $this->seek_table('loca');
- $this->glyphPos = array();
- if ($indexToLocFormat == 0) {
- $data = $this->get_chunk($start, ($numGlyphs * 2) + 2);
- $arr = unpack("n*", $data);
- for ($n = 0; $n <= $numGlyphs; $n++) {
- $this->glyphPos[] = ($arr[$n + 1] * 2);
- }
- } else if ($indexToLocFormat == 1) {
- $data = $this->get_chunk($start, ($numGlyphs * 4) + 4);
- $arr = unpack("N*", $data);
- for ($n = 0; $n <= $numGlyphs; $n++) {
- $this->glyphPos[] = ($arr[$n + 1]);
- }
- } else {
- throw new MpdfException('Unknown location table format ' . $indexToLocFormat);
- }
- }
-
- function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph)
- {
- $this->maxUniChar = 0;
- $this->seek($unicode_cmap_offset + 2);
- $length = $this->read_ushort();
- $limit = $unicode_cmap_offset + $length;
- $this->skip(2);
- $segCount = $this->read_ushort() / 2;
- $this->skip(6);
- $endCount = array();
- for ($i = 0; $i < $segCount; $i++) {
- $endCount[] = $this->read_ushort();
- }
- $this->skip(2);
- $startCount = array();
- for ($i = 0; $i < $segCount; $i++) {
- $startCount[] = $this->read_ushort();
- }
- $idDelta = array();
- for ($i = 0; $i < $segCount; $i++) {
- $idDelta[] = $this->read_short();
- }
- $idRangeOffset_start = $this->_pos;
- $idRangeOffset = array();
- for ($i = 0; $i < $segCount; $i++) {
- $idRangeOffset[] = $this->read_ushort();
- }
- for ($n = 0; $n < $segCount; $n++) {
- $endpoint = ($endCount[$n] + 1);
- for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) {
- if ($idRangeOffset[$n] == 0)
- $glyph = ($unichar + $idDelta[$n]) & 0xFFFF;
- else {
- $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n];
- $offset = $idRangeOffset_start + 2 * $n + $offset;
- if ($offset >= $limit)
- $glyph = 0;
- else {
- $glyph = $this->get_ushort($offset);
- if ($glyph != 0)
- $glyph = ($glyph + $idDelta[$n]) & 0xFFFF;
- }
- }
- $charToGlyph[$unichar] = $glyph;
- if ($unichar < 196608) {
- $this->maxUniChar = max($unichar, $this->maxUniChar);
- }
- $glyphToChar[$glyph][] = $unichar;
- }
- }
- }
-
- function endTTFile(&$stm)
- {
- $stm = '';
- $numTables = count($this->otables);
- $searchRange = 1;
- $entrySelector = 0;
- while ($searchRange * 2 <= $numTables) {
- $searchRange = $searchRange * 2;
- $entrySelector = $entrySelector + 1;
- }
- $searchRange = $searchRange * 16;
- $rangeShift = $numTables * 16 - $searchRange;
-
- if (_TTF_MAC_HEADER) {
- $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift));
- } else {
- $stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift));
- }
-
- $tables = $this->otables;
- ksort($tables);
- $offset = 12 + $numTables * 16;
- foreach ($tables AS $tag => $data) {
- if ($tag == 'head') {
- $head_start = $offset;
- }
- $stm .= $tag;
- $checksum = $this->calcChecksum($data);
- $stm .= pack("nn", $checksum[0], $checksum[1]);
- $stm .= pack("NN", $offset, strlen($data));
- $paddedLength = (strlen($data) + 3) & ~3;
- $offset = $offset + $paddedLength;
- }
-
- foreach ($tables AS $tag => $data) {
- $data .= "\0\0\0";
- $stm .= substr($data, 0, (strlen($data) & ~3));
- }
- $checksum = $this->calcChecksum($stm);
- $checksum = $this->sub32(array(0xB1B0, 0xAFBA), $checksum);
- $chk = pack("nn", $checksum[0], $checksum[1]);
- $stm = $this->splice($stm, ($head_start + 8), $chk);
- return $stm;
- }
- function repackageTTF($file, $TTCfontID = 0, $debug = false, $useOTL = false)
- {
-
- $this->useOTL = $useOTL;
- $this->filename = $file;
- $this->fh = fopen($file, 'rb');
- if (!$this->fh) {
- throw new MpdfException('Can\'t open file ' . $file);
- }
- $this->_pos = 0;
- $this->charWidths = '';
- $this->glyphPos = array();
- $this->charToGlyph = array();
- $this->tables = array();
- $this->otables = array();
- $this->ascent = 0;
- $this->descent = 0;
- $this->strikeoutSize = 0;
- $this->strikeoutPosition = 0;
- $this->numTTCFonts = 0;
- $this->TTCFonts = array();
- $this->skip(4);
- $this->maxUni = 0;
- if ($TTCfontID > 0) {
- $this->version = $version = $this->read_ulong();
- if (!in_array($version, array(0x00010000, 0x00020000))) {
- throw new MpdfException("ERROR - Error parsing TrueType Collection: version=" . $version . " - " . $file);
- }
- $this->numTTCFonts = $this->read_ulong();
- for ($i = 1; $i <= $this->numTTCFonts; $i++) {
- $this->TTCFonts[$i]['offset'] = $this->read_ulong();
- }
- $this->seek($this->TTCFonts[$TTCfontID]['offset']);
- $this->version = $version = $this->read_ulong();
- }
- $this->readTableDirectory($debug);
- $tags = array('OS/2', 'glyf', 'head', 'hhea', 'hmtx', 'loca', 'maxp', 'name', 'post', 'cvt ', 'fpgm', 'gasp', 'prep');
- foreach ($tags AS $tag) {
- if (isset($this->tables[$tag])) {
- $this->add($tag, $this->get_table($tag));
- }
- }
-
- if ($useOTL) {
-
-
-
- $this->seek_table("maxp");
- $this->skip(4);
- $numGlyphs = $this->read_ushort();
-
-
-
- $cmap_offset = $this->seek_table("cmap");
- $this->skip(2);
- $cmapTableCount = $this->read_ushort();
- $unicode_cmap_offset = 0;
- for ($i = 0; $i < $cmapTableCount; $i++) {
- $platformID = $this->read_ushort();
- $encodingID = $this->read_ushort();
- $offset = $this->read_ulong();
- $save_pos = $this->_pos;
- if (($platformID == 3 && $encodingID == 1) || $platformID == 0) {
- $format = $this->get_ushort($cmap_offset + $offset);
- if ($format == 4) {
- $unicode_cmap_offset = $cmap_offset + $offset;
- break;
- }
- }
- $this->seek($save_pos);
- }
- if (!$unicode_cmap_offset) {
- throw new MpdfException('Font (' . $this->filename . ') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)');
- }
- $glyphToChar = array();
- $charToGlyph = array();
- $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph);
-
-
- $bctr = 0xE000;
- for ($gid = 1; $gid < $numGlyphs; $gid++) {
- if (!isset($glyphToChar[$gid])) {
- while (isset($charToGlyph[$bctr])) {
- $bctr++;
- }
- if ($bctr > 0xF8FF) {
- throw new MpdfException("Problem. Trying to repackage TF file; not enough space for unmapped glyphs");
- }
- $glyphToChar[$gid][] = $bctr;
- $charToGlyph[$bctr] = $gid;
- $bctr++;
- }
- }
-
-
-
-
- unset($charToGlyph[65535]);
- unset($charToGlyph[0]);
- ksort($charToGlyph);
- $rangeid = 0;
- $range = array();
- $prevcid = -2;
- $prevglidx = -1;
-
- foreach ($charToGlyph as $cid => $glidx) {
- if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) {
- $range[$rangeid][] = $glidx;
- } else {
-
- $rangeid = $cid;
- $range[$rangeid] = array();
- $range[$rangeid][] = $glidx;
- }
- $prevcid = $cid;
- $prevglidx = $glidx;
- }
-
-
-
-
- $segCount = count($range) + 1;
- $searchRange = 1;
- $entrySelector = 0;
- while ($searchRange * 2 <= $segCount) {
- $searchRange = $searchRange * 2;
- $entrySelector = $entrySelector + 1;
- }
- $searchRange = $searchRange * 2;
- $rangeShift = $segCount * 2 - $searchRange;
- $length = 16 + (8 * $segCount ) + ($numGlyphs + 1);
- $cmap = array(0, 3,
- 0, 0,
- 0, 28,
- 0, 3,
- 0, 28,
- 3, 1,
- 0, 28,
- 4, $length, 0,
- $segCount * 2,
- $searchRange,
- $entrySelector,
- $rangeShift);
-
- foreach ($range AS $start => $subrange) {
- $endCode = $start + (count($subrange) - 1);
- $cmap[] = $endCode;
- }
- $cmap[] = 0xFFFF;
- $cmap[] = 0;
-
- foreach ($range AS $start => $subrange) {
- $cmap[] = $start;
- }
- $cmap[] = 0xFFFF;
-
- foreach ($range AS $start => $subrange) {
- $idDelta = -($start - $subrange[0]);
-
- $cmap[] = $idDelta;
- }
- $cmap[] = 1;
-
- foreach ($range AS $subrange) {
- $cmap[] = 0;
- }
- $cmap[] = 0;
- foreach ($range AS $subrange) {
- foreach ($subrange AS $glidx) {
- $cmap[] = $glidx;
- }
- }
- $cmap[] = 0;
- $cmapstr = '';
- foreach ($cmap AS $cm) {
- $cmapstr .= pack("n", $cm);
- }
- $this->add('cmap', $cmapstr);
- } else {
- $this->add('cmap', $this->get_table('cmap'));
- }
- fclose($this->fh);
- $stm = '';
- $this->endTTFile($stm);
- return $stm;
- }
- }
|