12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610 |
- /**
- * @licstart The following is the entire license notice for the
- * JavaScript code in this page
- *
- * Copyright 2022 Mozilla Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @licend The above is the entire license notice for the
- * JavaScript code in this page
- */
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.PartialEvaluator = exports.EvaluatorPreprocessor = void 0;
- var _util = require("../shared/util.js");
- var _cmap = require("./cmap.js");
- var _primitives = require("./primitives.js");
- var _fonts = require("./fonts.js");
- var _fonts_utils = require("./fonts_utils.js");
- var _encodings = require("./encodings.js");
- var _standard_fonts = require("./standard_fonts.js");
- var _unicode = require("./unicode.js");
- var _pattern = require("./pattern.js");
- var _xfa_fonts = require("./xfa_fonts.js");
- var _to_unicode_map = require("./to_unicode_map.js");
- var _function = require("./function.js");
- var _parser = require("./parser.js");
- var _image_utils = require("./image_utils.js");
- var _stream = require("./stream.js");
- var _base_stream = require("./base_stream.js");
- var _bidi = require("./bidi.js");
- var _colorspace = require("./colorspace.js");
- var _decode_stream = require("./decode_stream.js");
- var _glyphlist = require("./glyphlist.js");
- var _core_utils = require("./core_utils.js");
- var _metrics = require("./metrics.js");
- var _murmurhash = require("../shared/murmurhash3.js");
- var _operator_list = require("./operator_list.js");
- var _image = require("./image.js");
- const DefaultPartialEvaluatorOptions = Object.freeze({
- maxImageSize: -1,
- disableFontFace: false,
- ignoreErrors: false,
- isEvalSupported: true,
- fontExtraProperties: false,
- useSystemFonts: true,
- cMapUrl: null,
- standardFontDataUrl: null
- });
- const PatternType = {
- TILING: 1,
- SHADING: 2
- };
- const TEXT_CHUNK_BATCH_SIZE = 10;
- const deferred = Promise.resolve();
- function normalizeBlendMode(value, parsingArray = false) {
- if (Array.isArray(value)) {
- for (let i = 0, ii = value.length; i < ii; i++) {
- const maybeBM = normalizeBlendMode(value[i], true);
- if (maybeBM) {
- return maybeBM;
- }
- }
- (0, _util.warn)(`Unsupported blend mode Array: ${value}`);
- return "source-over";
- }
- if (!(value instanceof _primitives.Name)) {
- if (parsingArray) {
- return null;
- }
- return "source-over";
- }
- switch (value.name) {
- case "Normal":
- case "Compatible":
- return "source-over";
- case "Multiply":
- return "multiply";
- case "Screen":
- return "screen";
- case "Overlay":
- return "overlay";
- case "Darken":
- return "darken";
- case "Lighten":
- return "lighten";
- case "ColorDodge":
- return "color-dodge";
- case "ColorBurn":
- return "color-burn";
- case "HardLight":
- return "hard-light";
- case "SoftLight":
- return "soft-light";
- case "Difference":
- return "difference";
- case "Exclusion":
- return "exclusion";
- case "Hue":
- return "hue";
- case "Saturation":
- return "saturation";
- case "Color":
- return "color";
- case "Luminosity":
- return "luminosity";
- }
- if (parsingArray) {
- return null;
- }
- (0, _util.warn)(`Unsupported blend mode: ${value.name}`);
- return "source-over";
- }
- function incrementCachedImageMaskCount(data) {
- if (data.fn === _util.OPS.paintImageMaskXObject && data.args[0] && data.args[0].count > 0) {
- data.args[0].count++;
- }
- }
- class TimeSlotManager {
- static get TIME_SLOT_DURATION_MS() {
- return (0, _util.shadow)(this, "TIME_SLOT_DURATION_MS", 20);
- }
- static get CHECK_TIME_EVERY() {
- return (0, _util.shadow)(this, "CHECK_TIME_EVERY", 100);
- }
- constructor() {
- this.reset();
- }
- check() {
- if (++this.checked < TimeSlotManager.CHECK_TIME_EVERY) {
- return false;
- }
- this.checked = 0;
- return this.endTime <= Date.now();
- }
- reset() {
- this.endTime = Date.now() + TimeSlotManager.TIME_SLOT_DURATION_MS;
- this.checked = 0;
- }
- }
- class PartialEvaluator {
- constructor({
- xref,
- handler,
- pageIndex,
- idFactory,
- fontCache,
- builtInCMapCache,
- standardFontDataCache,
- globalImageCache,
- options = null
- }) {
- this.xref = xref;
- this.handler = handler;
- this.pageIndex = pageIndex;
- this.idFactory = idFactory;
- this.fontCache = fontCache;
- this.builtInCMapCache = builtInCMapCache;
- this.standardFontDataCache = standardFontDataCache;
- this.globalImageCache = globalImageCache;
- this.options = options || DefaultPartialEvaluatorOptions;
- this.parsingType3Font = false;
- this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
- }
- get _pdfFunctionFactory() {
- const pdfFunctionFactory = new _function.PDFFunctionFactory({
- xref: this.xref,
- isEvalSupported: this.options.isEvalSupported
- });
- return (0, _util.shadow)(this, "_pdfFunctionFactory", pdfFunctionFactory);
- }
- clone(newOptions = null) {
- const newEvaluator = Object.create(this);
- newEvaluator.options = Object.assign(Object.create(null), this.options, newOptions);
- return newEvaluator;
- }
- hasBlendModes(resources, nonBlendModesSet) {
- if (!(resources instanceof _primitives.Dict)) {
- return false;
- }
- if (resources.objId && nonBlendModesSet.has(resources.objId)) {
- return false;
- }
- const processed = new _primitives.RefSet(nonBlendModesSet);
- if (resources.objId) {
- processed.put(resources.objId);
- }
- const nodes = [resources],
- xref = this.xref;
- while (nodes.length) {
- const node = nodes.shift();
- const graphicStates = node.get("ExtGState");
- if (graphicStates instanceof _primitives.Dict) {
- for (let graphicState of graphicStates.getRawValues()) {
- if (graphicState instanceof _primitives.Ref) {
- if (processed.has(graphicState)) {
- continue;
- }
- try {
- graphicState = xref.fetch(graphicState);
- } catch (ex) {
- processed.put(graphicState);
- (0, _util.info)(`hasBlendModes - ignoring ExtGState: "${ex}".`);
- continue;
- }
- }
- if (!(graphicState instanceof _primitives.Dict)) {
- continue;
- }
- if (graphicState.objId) {
- processed.put(graphicState.objId);
- }
- const bm = graphicState.get("BM");
- if (bm instanceof _primitives.Name) {
- if (bm.name !== "Normal") {
- return true;
- }
- continue;
- }
- if (bm !== undefined && Array.isArray(bm)) {
- for (const element of bm) {
- if (element instanceof _primitives.Name && element.name !== "Normal") {
- return true;
- }
- }
- }
- }
- }
- const xObjects = node.get("XObject");
- if (!(xObjects instanceof _primitives.Dict)) {
- continue;
- }
- for (let xObject of xObjects.getRawValues()) {
- if (xObject instanceof _primitives.Ref) {
- if (processed.has(xObject)) {
- continue;
- }
- try {
- xObject = xref.fetch(xObject);
- } catch (ex) {
- processed.put(xObject);
- (0, _util.info)(`hasBlendModes - ignoring XObject: "${ex}".`);
- continue;
- }
- }
- if (!(xObject instanceof _base_stream.BaseStream)) {
- continue;
- }
- if (xObject.dict.objId) {
- processed.put(xObject.dict.objId);
- }
- const xResources = xObject.dict.get("Resources");
- if (!(xResources instanceof _primitives.Dict)) {
- continue;
- }
- if (xResources.objId && processed.has(xResources.objId)) {
- continue;
- }
- nodes.push(xResources);
- if (xResources.objId) {
- processed.put(xResources.objId);
- }
- }
- }
- for (const ref of processed) {
- nonBlendModesSet.put(ref);
- }
- return false;
- }
- async fetchBuiltInCMap(name) {
- const cachedData = this.builtInCMapCache.get(name);
- if (cachedData) {
- return cachedData;
- }
- let data;
- if (this.options.cMapUrl !== null) {
- const url = `${this.options.cMapUrl}${name}.bcmap`;
- const response = await fetch(url);
- if (!response.ok) {
- throw new Error(`fetchBuiltInCMap: failed to fetch file "${url}" with "${response.statusText}".`);
- }
- data = {
- cMapData: new Uint8Array(await response.arrayBuffer()),
- compressionType: _util.CMapCompressionType.BINARY
- };
- } else {
- data = await this.handler.sendWithPromise("FetchBuiltInCMap", {
- name
- });
- }
- if (data.compressionType !== _util.CMapCompressionType.NONE) {
- this.builtInCMapCache.set(name, data);
- }
- return data;
- }
- async fetchStandardFontData(name) {
- const cachedData = this.standardFontDataCache.get(name);
- if (cachedData) {
- return new _stream.Stream(cachedData);
- }
- if (this.options.useSystemFonts && name !== "Symbol" && name !== "ZapfDingbats") {
- return null;
- }
- const standardFontNameToFileName = (0, _standard_fonts.getFontNameToFileMap)(),
- filename = standardFontNameToFileName[name];
- let data;
- if (this.options.standardFontDataUrl !== null) {
- const url = `${this.options.standardFontDataUrl}${filename}`;
- const response = await fetch(url);
- if (!response.ok) {
- (0, _util.warn)(`fetchStandardFontData: failed to fetch file "${url}" with "${response.statusText}".`);
- } else {
- data = await response.arrayBuffer();
- }
- } else {
- try {
- data = await this.handler.sendWithPromise("FetchStandardFontData", {
- filename
- });
- } catch (e) {
- (0, _util.warn)(`fetchStandardFontData: failed to fetch file "${filename}" with "${e}".`);
- }
- }
- if (!data) {
- return null;
- }
- this.standardFontDataCache.set(name, data);
- return new _stream.Stream(data);
- }
- async buildFormXObject(resources, xobj, smask, operatorList, task, initialState, localColorSpaceCache) {
- const dict = xobj.dict;
- const matrix = dict.getArray("Matrix");
- let bbox = dict.getArray("BBox");
- if (Array.isArray(bbox) && bbox.length === 4) {
- bbox = _util.Util.normalizeRect(bbox);
- } else {
- bbox = null;
- }
- let optionalContent, groupOptions;
- if (dict.has("OC")) {
- optionalContent = await this.parseMarkedContentProps(dict.get("OC"), resources);
- }
- if (optionalContent !== undefined) {
- operatorList.addOp(_util.OPS.beginMarkedContentProps, ["OC", optionalContent]);
- }
- const group = dict.get("Group");
- if (group) {
- groupOptions = {
- matrix,
- bbox,
- smask,
- isolated: false,
- knockout: false
- };
- const groupSubtype = group.get("S");
- let colorSpace = null;
- if ((0, _primitives.isName)(groupSubtype, "Transparency")) {
- groupOptions.isolated = group.get("I") || false;
- groupOptions.knockout = group.get("K") || false;
- if (group.has("CS")) {
- const cs = group.getRaw("CS");
- const cachedColorSpace = _colorspace.ColorSpace.getCached(cs, this.xref, localColorSpaceCache);
- if (cachedColorSpace) {
- colorSpace = cachedColorSpace;
- } else {
- colorSpace = await this.parseColorSpace({
- cs,
- resources,
- localColorSpaceCache
- });
- }
- }
- }
- if (smask && smask.backdrop) {
- colorSpace = colorSpace || _colorspace.ColorSpace.singletons.rgb;
- smask.backdrop = colorSpace.getRgb(smask.backdrop, 0);
- }
- operatorList.addOp(_util.OPS.beginGroup, [groupOptions]);
- }
- const args = group ? [matrix, null] : [matrix, bbox];
- operatorList.addOp(_util.OPS.paintFormXObjectBegin, args);
- return this.getOperatorList({
- stream: xobj,
- task,
- resources: dict.get("Resources") || resources,
- operatorList,
- initialState
- }).then(function () {
- operatorList.addOp(_util.OPS.paintFormXObjectEnd, []);
- if (group) {
- operatorList.addOp(_util.OPS.endGroup, [groupOptions]);
- }
- if (optionalContent !== undefined) {
- operatorList.addOp(_util.OPS.endMarkedContent, []);
- }
- });
- }
- _sendImgData(objId, imgData, cacheGlobally = false) {
- const transfers = imgData ? [imgData.bitmap || imgData.data.buffer] : null;
- if (this.parsingType3Font || cacheGlobally) {
- return this.handler.send("commonobj", [objId, "Image", imgData], transfers);
- }
- return this.handler.send("obj", [objId, this.pageIndex, "Image", imgData], transfers);
- }
- async buildPaintImageXObject({
- resources,
- image,
- isInline = false,
- operatorList,
- cacheKey,
- localImageCache,
- localColorSpaceCache
- }) {
- const dict = image.dict;
- const imageRef = dict.objId;
- const w = dict.get("W", "Width");
- const h = dict.get("H", "Height");
- if (!(w && typeof w === "number") || !(h && typeof h === "number")) {
- (0, _util.warn)("Image dimensions are missing, or not numbers.");
- return;
- }
- const maxImageSize = this.options.maxImageSize;
- if (maxImageSize !== -1 && w * h > maxImageSize) {
- const msg = "Image exceeded maximum allowed size and was removed.";
- if (this.options.ignoreErrors) {
- (0, _util.warn)(msg);
- return;
- }
- throw new Error(msg);
- }
- let optionalContent;
- if (dict.has("OC")) {
- optionalContent = await this.parseMarkedContentProps(dict.get("OC"), resources);
- }
- const imageMask = dict.get("IM", "ImageMask") || false;
- let imgData, args;
- if (imageMask) {
- const interpolate = dict.get("I", "Interpolate");
- const bitStrideLength = w + 7 >> 3;
- const imgArray = image.getBytes(bitStrideLength * h);
- const decode = dict.getArray("D", "Decode");
- if (this.parsingType3Font) {
- imgData = _image.PDFImage.createRawMask({
- imgArray,
- width: w,
- height: h,
- imageIsFromDecodeStream: image instanceof _decode_stream.DecodeStream,
- inverseDecode: !!decode && decode[0] > 0,
- interpolate
- });
- imgData.cached = !!cacheKey;
- args = [imgData];
- operatorList.addImageOps(_util.OPS.paintImageMaskXObject, args, optionalContent);
- if (cacheKey) {
- localImageCache.set(cacheKey, imageRef, {
- fn: _util.OPS.paintImageMaskXObject,
- args,
- optionalContent
- });
- }
- return;
- }
- imgData = _image.PDFImage.createMask({
- imgArray,
- width: w,
- height: h,
- imageIsFromDecodeStream: image instanceof _decode_stream.DecodeStream,
- inverseDecode: !!decode && decode[0] > 0,
- interpolate
- });
- if (imgData.isSingleOpaquePixel) {
- operatorList.addImageOps(_util.OPS.paintSolidColorImageMask, [], optionalContent);
- if (cacheKey) {
- localImageCache.set(cacheKey, imageRef, {
- fn: _util.OPS.paintSolidColorImageMask,
- args: [],
- optionalContent
- });
- }
- return;
- }
- const objId = `mask_${this.idFactory.createObjId()}`;
- operatorList.addDependency(objId);
- this._sendImgData(objId, imgData);
- args = [{
- data: objId,
- width: imgData.width,
- height: imgData.height,
- interpolate: imgData.interpolate,
- count: 1
- }];
- operatorList.addImageOps(_util.OPS.paintImageMaskXObject, args, optionalContent);
- if (cacheKey) {
- localImageCache.set(cacheKey, imageRef, {
- fn: _util.OPS.paintImageMaskXObject,
- args,
- optionalContent
- });
- }
- return;
- }
- const softMask = dict.get("SM", "SMask") || false;
- const mask = dict.get("Mask") || false;
- const SMALL_IMAGE_DIMENSIONS = 200;
- if (isInline && !softMask && !mask && w + h < SMALL_IMAGE_DIMENSIONS) {
- const imageObj = new _image.PDFImage({
- xref: this.xref,
- res: resources,
- image,
- isInline,
- pdfFunctionFactory: this._pdfFunctionFactory,
- localColorSpaceCache
- });
- imgData = imageObj.createImageData(true);
- operatorList.addImageOps(_util.OPS.paintInlineImageXObject, [imgData], optionalContent);
- return;
- }
- let objId = `img_${this.idFactory.createObjId()}`,
- cacheGlobally = false;
- if (this.parsingType3Font) {
- objId = `${this.idFactory.getDocId()}_type3_${objId}`;
- } else if (imageRef) {
- cacheGlobally = this.globalImageCache.shouldCache(imageRef, this.pageIndex);
- if (cacheGlobally) {
- objId = `${this.idFactory.getDocId()}_${objId}`;
- }
- }
- operatorList.addDependency(objId);
- args = [objId, w, h];
- _image.PDFImage.buildImage({
- xref: this.xref,
- res: resources,
- image,
- isInline,
- pdfFunctionFactory: this._pdfFunctionFactory,
- localColorSpaceCache
- }).then(imageObj => {
- imgData = imageObj.createImageData(false);
- if (cacheKey && imageRef && cacheGlobally) {
- this.globalImageCache.addByteSize(imageRef, imgData.data.length);
- }
- return this._sendImgData(objId, imgData, cacheGlobally);
- }).catch(reason => {
- (0, _util.warn)(`Unable to decode image "${objId}": "${reason}".`);
- return this._sendImgData(objId, null, cacheGlobally);
- });
- operatorList.addImageOps(_util.OPS.paintImageXObject, args, optionalContent);
- if (cacheKey) {
- localImageCache.set(cacheKey, imageRef, {
- fn: _util.OPS.paintImageXObject,
- args,
- optionalContent
- });
- if (imageRef) {
- (0, _util.assert)(!isInline, "Cannot cache an inline image globally.");
- this.globalImageCache.addPageIndex(imageRef, this.pageIndex);
- if (cacheGlobally) {
- this.globalImageCache.setData(imageRef, {
- objId,
- fn: _util.OPS.paintImageXObject,
- args,
- optionalContent,
- byteSize: 0
- });
- }
- }
- }
- }
- handleSMask(smask, resources, operatorList, task, stateManager, localColorSpaceCache) {
- const smaskContent = smask.get("G");
- const smaskOptions = {
- subtype: smask.get("S").name,
- backdrop: smask.get("BC")
- };
- const transferObj = smask.get("TR");
- if ((0, _function.isPDFFunction)(transferObj)) {
- const transferFn = this._pdfFunctionFactory.create(transferObj);
- const transferMap = new Uint8Array(256);
- const tmp = new Float32Array(1);
- for (let i = 0; i < 256; i++) {
- tmp[0] = i / 255;
- transferFn(tmp, 0, tmp, 0);
- transferMap[i] = tmp[0] * 255 | 0;
- }
- smaskOptions.transferMap = transferMap;
- }
- return this.buildFormXObject(resources, smaskContent, smaskOptions, operatorList, task, stateManager.state.clone(), localColorSpaceCache);
- }
- handleTransferFunction(tr) {
- let transferArray;
- if (Array.isArray(tr)) {
- transferArray = tr;
- } else if ((0, _function.isPDFFunction)(tr)) {
- transferArray = [tr];
- } else {
- return null;
- }
- const transferMaps = [];
- let numFns = 0,
- numEffectfulFns = 0;
- for (const entry of transferArray) {
- const transferObj = this.xref.fetchIfRef(entry);
- numFns++;
- if ((0, _primitives.isName)(transferObj, "Identity")) {
- transferMaps.push(null);
- continue;
- } else if (!(0, _function.isPDFFunction)(transferObj)) {
- return null;
- }
- const transferFn = this._pdfFunctionFactory.create(transferObj);
- const transferMap = new Uint8Array(256),
- tmp = new Float32Array(1);
- for (let j = 0; j < 256; j++) {
- tmp[0] = j / 255;
- transferFn(tmp, 0, tmp, 0);
- transferMap[j] = tmp[0] * 255 | 0;
- }
- transferMaps.push(transferMap);
- numEffectfulFns++;
- }
- if (!(numFns === 1 || numFns === 4)) {
- return null;
- }
- if (numEffectfulFns === 0) {
- return null;
- }
- return transferMaps;
- }
- handleTilingType(fn, color, resources, pattern, patternDict, operatorList, task, localTilingPatternCache) {
- const tilingOpList = new _operator_list.OperatorList();
- const patternResources = _primitives.Dict.merge({
- xref: this.xref,
- dictArray: [patternDict.get("Resources"), resources]
- });
- return this.getOperatorList({
- stream: pattern,
- task,
- resources: patternResources,
- operatorList: tilingOpList
- }).then(function () {
- const operatorListIR = tilingOpList.getIR();
- const tilingPatternIR = (0, _pattern.getTilingPatternIR)(operatorListIR, patternDict, color);
- operatorList.addDependencies(tilingOpList.dependencies);
- operatorList.addOp(fn, tilingPatternIR);
- if (patternDict.objId) {
- localTilingPatternCache.set(null, patternDict.objId, {
- operatorListIR,
- dict: patternDict
- });
- }
- }).catch(reason => {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (this.options.ignoreErrors) {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorTilingPattern
- });
- (0, _util.warn)(`handleTilingType - ignoring pattern: "${reason}".`);
- return;
- }
- throw reason;
- });
- }
- handleSetFont(resources, fontArgs, fontRef, operatorList, task, state, fallbackFontDict = null, cssFontInfo = null) {
- const fontName = fontArgs && fontArgs[0] instanceof _primitives.Name ? fontArgs[0].name : null;
- return this.loadFont(fontName, fontRef, resources, fallbackFontDict, cssFontInfo).then(translated => {
- if (!translated.font.isType3Font) {
- return translated;
- }
- return translated.loadType3Data(this, resources, task).then(function () {
- operatorList.addDependencies(translated.type3Dependencies);
- return translated;
- }).catch(reason => {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorFontLoadType3
- });
- return new TranslatedFont({
- loadedName: "g_font_error",
- font: new _fonts.ErrorFont(`Type3 font load error: ${reason}`),
- dict: translated.font,
- evaluatorOptions: this.options
- });
- });
- }).then(translated => {
- state.font = translated.font;
- translated.send(this.handler);
- return translated.loadedName;
- });
- }
- handleText(chars, state) {
- const font = state.font;
- const glyphs = font.charsToGlyphs(chars);
- if (font.data) {
- const isAddToPathSet = !!(state.textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG);
- if (isAddToPathSet || state.fillColorSpace.name === "Pattern" || font.disableFontFace || this.options.disableFontFace) {
- PartialEvaluator.buildFontPaths(font, glyphs, this.handler, this.options);
- }
- }
- return glyphs;
- }
- ensureStateFont(state) {
- if (state.font) {
- return;
- }
- const reason = new _util.FormatError("Missing setFont (Tf) operator before text rendering operator.");
- if (this.options.ignoreErrors) {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorFontState
- });
- (0, _util.warn)(`ensureStateFont: "${reason}".`);
- return;
- }
- throw reason;
- }
- async setGState({
- resources,
- gState,
- operatorList,
- cacheKey,
- task,
- stateManager,
- localGStateCache,
- localColorSpaceCache
- }) {
- const gStateRef = gState.objId;
- let isSimpleGState = true;
- const gStateObj = [];
- const gStateKeys = gState.getKeys();
- let promise = Promise.resolve();
- for (let i = 0, ii = gStateKeys.length; i < ii; i++) {
- const key = gStateKeys[i];
- const value = gState.get(key);
- switch (key) {
- case "Type":
- break;
- case "LW":
- case "LC":
- case "LJ":
- case "ML":
- case "D":
- case "RI":
- case "FL":
- case "CA":
- case "ca":
- gStateObj.push([key, value]);
- break;
- case "Font":
- isSimpleGState = false;
- promise = promise.then(() => {
- return this.handleSetFont(resources, null, value[0], operatorList, task, stateManager.state).then(function (loadedName) {
- operatorList.addDependency(loadedName);
- gStateObj.push([key, [loadedName, value[1]]]);
- });
- });
- break;
- case "BM":
- gStateObj.push([key, normalizeBlendMode(value)]);
- break;
- case "SMask":
- if ((0, _primitives.isName)(value, "None")) {
- gStateObj.push([key, false]);
- break;
- }
- if (value instanceof _primitives.Dict) {
- isSimpleGState = false;
- promise = promise.then(() => {
- return this.handleSMask(value, resources, operatorList, task, stateManager, localColorSpaceCache);
- });
- gStateObj.push([key, true]);
- } else {
- (0, _util.warn)("Unsupported SMask type");
- }
- break;
- case "TR":
- const transferMaps = this.handleTransferFunction(value);
- gStateObj.push([key, transferMaps]);
- break;
- case "OP":
- case "op":
- case "OPM":
- case "BG":
- case "BG2":
- case "UCR":
- case "UCR2":
- case "TR2":
- case "HT":
- case "SM":
- case "SA":
- case "AIS":
- case "TK":
- (0, _util.info)("graphic state operator " + key);
- break;
- default:
- (0, _util.info)("Unknown graphic state operator " + key);
- break;
- }
- }
- return promise.then(function () {
- if (gStateObj.length > 0) {
- operatorList.addOp(_util.OPS.setGState, [gStateObj]);
- }
- if (isSimpleGState) {
- localGStateCache.set(cacheKey, gStateRef, gStateObj);
- }
- });
- }
- loadFont(fontName, font, resources, fallbackFontDict = null, cssFontInfo = null) {
- const errorFont = async () => {
- return new TranslatedFont({
- loadedName: "g_font_error",
- font: new _fonts.ErrorFont(`Font "${fontName}" is not available.`),
- dict: font,
- evaluatorOptions: this.options
- });
- };
- const xref = this.xref;
- let fontRef;
- if (font) {
- if (font instanceof _primitives.Ref) {
- fontRef = font;
- }
- } else {
- const fontRes = resources.get("Font");
- if (fontRes) {
- fontRef = fontRes.getRaw(fontName);
- }
- }
- if (!fontRef) {
- const partialMsg = `Font "${fontName || font && font.toString()}" is not available`;
- if (!this.options.ignoreErrors && !this.parsingType3Font) {
- (0, _util.warn)(`${partialMsg}.`);
- return errorFont();
- }
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorFontMissing
- });
- (0, _util.warn)(`${partialMsg} -- attempting to fallback to a default font.`);
- if (fallbackFontDict) {
- fontRef = fallbackFontDict;
- } else {
- fontRef = PartialEvaluator.fallbackFontDict;
- }
- }
- if (this.parsingType3Font && this.type3FontRefs.has(fontRef)) {
- return errorFont();
- }
- if (this.fontCache.has(fontRef)) {
- return this.fontCache.get(fontRef);
- }
- font = xref.fetchIfRef(fontRef);
- if (!(font instanceof _primitives.Dict)) {
- return errorFont();
- }
- if (font.cacheKey && this.fontCache.has(font.cacheKey)) {
- return this.fontCache.get(font.cacheKey);
- }
- const fontCapability = (0, _util.createPromiseCapability)();
- let preEvaluatedFont;
- try {
- preEvaluatedFont = this.preEvaluateFont(font);
- preEvaluatedFont.cssFontInfo = cssFontInfo;
- } catch (reason) {
- (0, _util.warn)(`loadFont - preEvaluateFont failed: "${reason}".`);
- return errorFont();
- }
- const {
- descriptor,
- hash
- } = preEvaluatedFont;
- const fontRefIsRef = fontRef instanceof _primitives.Ref;
- let fontID;
- if (fontRefIsRef) {
- fontID = `f${fontRef.toString()}`;
- }
- if (hash && descriptor instanceof _primitives.Dict) {
- if (!descriptor.fontAliases) {
- descriptor.fontAliases = Object.create(null);
- }
- const fontAliases = descriptor.fontAliases;
- if (fontAliases[hash]) {
- const aliasFontRef = fontAliases[hash].aliasRef;
- if (fontRefIsRef && aliasFontRef && this.fontCache.has(aliasFontRef)) {
- this.fontCache.putAlias(fontRef, aliasFontRef);
- return this.fontCache.get(fontRef);
- }
- } else {
- fontAliases[hash] = {
- fontID: this.idFactory.createFontId()
- };
- }
- if (fontRefIsRef) {
- fontAliases[hash].aliasRef = fontRef;
- }
- fontID = fontAliases[hash].fontID;
- }
- if (fontRefIsRef) {
- this.fontCache.put(fontRef, fontCapability.promise);
- } else {
- if (!fontID) {
- fontID = this.idFactory.createFontId();
- }
- font.cacheKey = `cacheKey_${fontID}`;
- this.fontCache.put(font.cacheKey, fontCapability.promise);
- }
- (0, _util.assert)(fontID && fontID.startsWith("f"), 'The "fontID" must be (correctly) defined.');
- font.loadedName = `${this.idFactory.getDocId()}_${fontID}`;
- this.translateFont(preEvaluatedFont).then(translatedFont => {
- if (translatedFont.fontType !== undefined) {
- xref.stats.addFontType(translatedFont.fontType);
- }
- fontCapability.resolve(new TranslatedFont({
- loadedName: font.loadedName,
- font: translatedFont,
- dict: font,
- evaluatorOptions: this.options
- }));
- }).catch(reason => {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorFontTranslate
- });
- (0, _util.warn)(`loadFont - translateFont failed: "${reason}".`);
- try {
- const fontFile3 = descriptor && descriptor.get("FontFile3");
- const subtype = fontFile3 && fontFile3.get("Subtype");
- const fontType = (0, _fonts_utils.getFontType)(preEvaluatedFont.type, subtype && subtype.name);
- if (fontType !== undefined) {
- xref.stats.addFontType(fontType);
- }
- } catch (ex) {}
- fontCapability.resolve(new TranslatedFont({
- loadedName: font.loadedName,
- font: new _fonts.ErrorFont(reason instanceof Error ? reason.message : reason),
- dict: font,
- evaluatorOptions: this.options
- }));
- });
- return fontCapability.promise;
- }
- buildPath(operatorList, fn, args, parsingText = false) {
- const lastIndex = operatorList.length - 1;
- if (!args) {
- args = [];
- }
- let minMax;
- if (lastIndex < 0 || operatorList.fnArray[lastIndex] !== _util.OPS.constructPath) {
- if (parsingText) {
- (0, _util.warn)(`Encountered path operator "${fn}" inside of a text object.`);
- operatorList.addOp(_util.OPS.save, null);
- }
- minMax = [Infinity, -Infinity, Infinity, -Infinity];
- operatorList.addOp(_util.OPS.constructPath, [[fn], args, minMax]);
- if (parsingText) {
- operatorList.addOp(_util.OPS.restore, null);
- }
- } else {
- const opArgs = operatorList.argsArray[lastIndex];
- opArgs[0].push(fn);
- Array.prototype.push.apply(opArgs[1], args);
- minMax = opArgs[2];
- }
- switch (fn) {
- case _util.OPS.rectangle:
- minMax[0] = Math.min(minMax[0], args[0], args[0] + args[2]);
- minMax[1] = Math.max(minMax[1], args[0], args[0] + args[2]);
- minMax[2] = Math.min(minMax[2], args[1], args[1] + args[3]);
- minMax[3] = Math.max(minMax[3], args[1], args[1] + args[3]);
- break;
- case _util.OPS.moveTo:
- case _util.OPS.lineTo:
- minMax[0] = Math.min(minMax[0], args[0]);
- minMax[1] = Math.max(minMax[1], args[0]);
- minMax[2] = Math.min(minMax[2], args[1]);
- minMax[3] = Math.max(minMax[3], args[1]);
- break;
- }
- }
- parseColorSpace({
- cs,
- resources,
- localColorSpaceCache
- }) {
- return _colorspace.ColorSpace.parseAsync({
- cs,
- xref: this.xref,
- resources,
- pdfFunctionFactory: this._pdfFunctionFactory,
- localColorSpaceCache
- }).catch(reason => {
- if (reason instanceof _util.AbortException) {
- return null;
- }
- if (this.options.ignoreErrors) {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorColorSpace
- });
- (0, _util.warn)(`parseColorSpace - ignoring ColorSpace: "${reason}".`);
- return null;
- }
- throw reason;
- });
- }
- parseShading({
- shading,
- resources,
- localColorSpaceCache,
- localShadingPatternCache
- }) {
- let id = localShadingPatternCache.get(shading);
- if (!id) {
- var shadingFill = _pattern.Pattern.parseShading(shading, this.xref, resources, this.handler, this._pdfFunctionFactory, localColorSpaceCache);
- const patternIR = shadingFill.getIR();
- id = `pattern_${this.idFactory.createObjId()}`;
- localShadingPatternCache.set(shading, id);
- this.handler.send("obj", [id, this.pageIndex, "Pattern", patternIR]);
- }
- return id;
- }
- handleColorN(operatorList, fn, args, cs, patterns, resources, task, localColorSpaceCache, localTilingPatternCache, localShadingPatternCache) {
- const patternName = args.pop();
- if (patternName instanceof _primitives.Name) {
- const rawPattern = patterns.getRaw(patternName.name);
- const localTilingPattern = rawPattern instanceof _primitives.Ref && localTilingPatternCache.getByRef(rawPattern);
- if (localTilingPattern) {
- try {
- const color = cs.base ? cs.base.getRgb(args, 0) : null;
- const tilingPatternIR = (0, _pattern.getTilingPatternIR)(localTilingPattern.operatorListIR, localTilingPattern.dict, color);
- operatorList.addOp(fn, tilingPatternIR);
- return undefined;
- } catch (ex) {}
- }
- const pattern = this.xref.fetchIfRef(rawPattern);
- if (pattern) {
- const dict = pattern instanceof _base_stream.BaseStream ? pattern.dict : pattern;
- const typeNum = dict.get("PatternType");
- if (typeNum === PatternType.TILING) {
- const color = cs.base ? cs.base.getRgb(args, 0) : null;
- return this.handleTilingType(fn, color, resources, pattern, dict, operatorList, task, localTilingPatternCache);
- } else if (typeNum === PatternType.SHADING) {
- const shading = dict.get("Shading");
- const matrix = dict.getArray("Matrix");
- const objId = this.parseShading({
- shading,
- resources,
- localColorSpaceCache,
- localShadingPatternCache
- });
- operatorList.addOp(fn, ["Shading", objId, matrix]);
- return undefined;
- }
- throw new _util.FormatError(`Unknown PatternType: ${typeNum}`);
- }
- }
- throw new _util.FormatError(`Unknown PatternName: ${patternName}`);
- }
- _parseVisibilityExpression(array, nestingCounter, currentResult) {
- const MAX_NESTING = 10;
- if (++nestingCounter > MAX_NESTING) {
- (0, _util.warn)("Visibility expression is too deeply nested");
- return;
- }
- const length = array.length;
- const operator = this.xref.fetchIfRef(array[0]);
- if (length < 2 || !(operator instanceof _primitives.Name)) {
- (0, _util.warn)("Invalid visibility expression");
- return;
- }
- switch (operator.name) {
- case "And":
- case "Or":
- case "Not":
- currentResult.push(operator.name);
- break;
- default:
- (0, _util.warn)(`Invalid operator ${operator.name} in visibility expression`);
- return;
- }
- for (let i = 1; i < length; i++) {
- const raw = array[i];
- const object = this.xref.fetchIfRef(raw);
- if (Array.isArray(object)) {
- const nestedResult = [];
- currentResult.push(nestedResult);
- this._parseVisibilityExpression(object, nestingCounter, nestedResult);
- } else if (raw instanceof _primitives.Ref) {
- currentResult.push(raw.toString());
- }
- }
- }
- async parseMarkedContentProps(contentProperties, resources) {
- let optionalContent;
- if (contentProperties instanceof _primitives.Name) {
- const properties = resources.get("Properties");
- optionalContent = properties.get(contentProperties.name);
- } else if (contentProperties instanceof _primitives.Dict) {
- optionalContent = contentProperties;
- } else {
- throw new _util.FormatError("Optional content properties malformed.");
- }
- const optionalContentType = optionalContent.get("Type").name;
- if (optionalContentType === "OCG") {
- return {
- type: optionalContentType,
- id: optionalContent.objId
- };
- } else if (optionalContentType === "OCMD") {
- const expression = optionalContent.get("VE");
- if (Array.isArray(expression)) {
- const result = [];
- this._parseVisibilityExpression(expression, 0, result);
- if (result.length > 0) {
- return {
- type: "OCMD",
- expression: result
- };
- }
- }
- const optionalContentGroups = optionalContent.get("OCGs");
- if (Array.isArray(optionalContentGroups) || optionalContentGroups instanceof _primitives.Dict) {
- const groupIds = [];
- if (Array.isArray(optionalContentGroups)) {
- for (const ocg of optionalContentGroups) {
- groupIds.push(ocg.toString());
- }
- } else {
- groupIds.push(optionalContentGroups.objId);
- }
- return {
- type: optionalContentType,
- ids: groupIds,
- policy: optionalContent.get("P") instanceof _primitives.Name ? optionalContent.get("P").name : null,
- expression: null
- };
- } else if (optionalContentGroups instanceof _primitives.Ref) {
- return {
- type: optionalContentType,
- id: optionalContentGroups.toString()
- };
- }
- }
- return null;
- }
- getOperatorList({
- stream,
- task,
- resources,
- operatorList,
- initialState = null,
- fallbackFontDict = null
- }) {
- resources = resources || _primitives.Dict.empty;
- initialState = initialState || new EvalState();
- if (!operatorList) {
- throw new Error('getOperatorList: missing "operatorList" parameter');
- }
- const self = this;
- const xref = this.xref;
- let parsingText = false;
- const localImageCache = new _image_utils.LocalImageCache();
- const localColorSpaceCache = new _image_utils.LocalColorSpaceCache();
- const localGStateCache = new _image_utils.LocalGStateCache();
- const localTilingPatternCache = new _image_utils.LocalTilingPatternCache();
- const localShadingPatternCache = new Map();
- const xobjs = resources.get("XObject") || _primitives.Dict.empty;
- const patterns = resources.get("Pattern") || _primitives.Dict.empty;
- const stateManager = new StateManager(initialState);
- const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
- const timeSlotManager = new TimeSlotManager();
- function closePendingRestoreOPS(argument) {
- for (let i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
- operatorList.addOp(_util.OPS.restore, []);
- }
- }
- return new Promise(function promiseBody(resolve, reject) {
- const next = function (promise) {
- Promise.all([promise, operatorList.ready]).then(function () {
- try {
- promiseBody(resolve, reject);
- } catch (ex) {
- reject(ex);
- }
- }, reject);
- };
- task.ensureNotTerminated();
- timeSlotManager.reset();
- const operation = {};
- let stop, i, ii, cs, name, isValidName;
- while (!(stop = timeSlotManager.check())) {
- operation.args = null;
- if (!preprocessor.read(operation)) {
- break;
- }
- let args = operation.args;
- let fn = operation.fn;
- switch (fn | 0) {
- case _util.OPS.paintXObject:
- isValidName = args[0] instanceof _primitives.Name;
- name = args[0].name;
- if (isValidName) {
- const localImage = localImageCache.getByName(name);
- if (localImage) {
- operatorList.addImageOps(localImage.fn, localImage.args, localImage.optionalContent);
- incrementCachedImageMaskCount(localImage);
- args = null;
- continue;
- }
- }
- next(new Promise(function (resolveXObject, rejectXObject) {
- if (!isValidName) {
- throw new _util.FormatError("XObject must be referred to by name.");
- }
- let xobj = xobjs.getRaw(name);
- if (xobj instanceof _primitives.Ref) {
- const localImage = localImageCache.getByRef(xobj);
- if (localImage) {
- operatorList.addImageOps(localImage.fn, localImage.args, localImage.optionalContent);
- incrementCachedImageMaskCount(localImage);
- resolveXObject();
- return;
- }
- const globalImage = self.globalImageCache.getData(xobj, self.pageIndex);
- if (globalImage) {
- operatorList.addDependency(globalImage.objId);
- operatorList.addImageOps(globalImage.fn, globalImage.args, globalImage.optionalContent);
- resolveXObject();
- return;
- }
- xobj = xref.fetch(xobj);
- }
- if (!(xobj instanceof _base_stream.BaseStream)) {
- throw new _util.FormatError("XObject should be a stream");
- }
- const type = xobj.dict.get("Subtype");
- if (!(type instanceof _primitives.Name)) {
- throw new _util.FormatError("XObject should have a Name subtype");
- }
- if (type.name === "Form") {
- stateManager.save();
- self.buildFormXObject(resources, xobj, null, operatorList, task, stateManager.state.clone(), localColorSpaceCache).then(function () {
- stateManager.restore();
- resolveXObject();
- }, rejectXObject);
- return;
- } else if (type.name === "Image") {
- self.buildPaintImageXObject({
- resources,
- image: xobj,
- operatorList,
- cacheKey: name,
- localImageCache,
- localColorSpaceCache
- }).then(resolveXObject, rejectXObject);
- return;
- } else if (type.name === "PS") {
- (0, _util.info)("Ignored XObject subtype PS");
- } else {
- throw new _util.FormatError(`Unhandled XObject subtype ${type.name}`);
- }
- resolveXObject();
- }).catch(function (reason) {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (self.options.ignoreErrors) {
- self.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorXObject
- });
- (0, _util.warn)(`getOperatorList - ignoring XObject: "${reason}".`);
- return;
- }
- throw reason;
- }));
- return;
- case _util.OPS.setFont:
- var fontSize = args[1];
- next(self.handleSetFont(resources, args, null, operatorList, task, stateManager.state, fallbackFontDict).then(function (loadedName) {
- operatorList.addDependency(loadedName);
- operatorList.addOp(_util.OPS.setFont, [loadedName, fontSize]);
- }));
- return;
- case _util.OPS.beginText:
- parsingText = true;
- break;
- case _util.OPS.endText:
- parsingText = false;
- break;
- case _util.OPS.endInlineImage:
- var cacheKey = args[0].cacheKey;
- if (cacheKey) {
- const localImage = localImageCache.getByName(cacheKey);
- if (localImage) {
- operatorList.addImageOps(localImage.fn, localImage.args, localImage.optionalContent);
- incrementCachedImageMaskCount(localImage);
- args = null;
- continue;
- }
- }
- next(self.buildPaintImageXObject({
- resources,
- image: args[0],
- isInline: true,
- operatorList,
- cacheKey,
- localImageCache,
- localColorSpaceCache
- }));
- return;
- case _util.OPS.showText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- args[0] = self.handleText(args[0], stateManager.state);
- break;
- case _util.OPS.showSpacedText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- var arr = args[0];
- var combinedGlyphs = [];
- var arrLength = arr.length;
- var state = stateManager.state;
- for (i = 0; i < arrLength; ++i) {
- const arrItem = arr[i];
- if (typeof arrItem === "string") {
- Array.prototype.push.apply(combinedGlyphs, self.handleText(arrItem, state));
- } else if (typeof arrItem === "number") {
- combinedGlyphs.push(arrItem);
- }
- }
- args[0] = combinedGlyphs;
- fn = _util.OPS.showText;
- break;
- case _util.OPS.nextLineShowText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- operatorList.addOp(_util.OPS.nextLine);
- args[0] = self.handleText(args[0], stateManager.state);
- fn = _util.OPS.showText;
- break;
- case _util.OPS.nextLineSetSpacingShowText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- operatorList.addOp(_util.OPS.nextLine);
- operatorList.addOp(_util.OPS.setWordSpacing, [args.shift()]);
- operatorList.addOp(_util.OPS.setCharSpacing, [args.shift()]);
- args[0] = self.handleText(args[0], stateManager.state);
- fn = _util.OPS.showText;
- break;
- case _util.OPS.setTextRenderingMode:
- stateManager.state.textRenderingMode = args[0];
- break;
- case _util.OPS.setFillColorSpace:
- {
- const cachedColorSpace = _colorspace.ColorSpace.getCached(args[0], xref, localColorSpaceCache);
- if (cachedColorSpace) {
- stateManager.state.fillColorSpace = cachedColorSpace;
- continue;
- }
- next(self.parseColorSpace({
- cs: args[0],
- resources,
- localColorSpaceCache
- }).then(function (colorSpace) {
- if (colorSpace) {
- stateManager.state.fillColorSpace = colorSpace;
- }
- }));
- return;
- }
- case _util.OPS.setStrokeColorSpace:
- {
- const cachedColorSpace = _colorspace.ColorSpace.getCached(args[0], xref, localColorSpaceCache);
- if (cachedColorSpace) {
- stateManager.state.strokeColorSpace = cachedColorSpace;
- continue;
- }
- next(self.parseColorSpace({
- cs: args[0],
- resources,
- localColorSpaceCache
- }).then(function (colorSpace) {
- if (colorSpace) {
- stateManager.state.strokeColorSpace = colorSpace;
- }
- }));
- return;
- }
- case _util.OPS.setFillColor:
- cs = stateManager.state.fillColorSpace;
- args = cs.getRgb(args, 0);
- fn = _util.OPS.setFillRGBColor;
- break;
- case _util.OPS.setStrokeColor:
- cs = stateManager.state.strokeColorSpace;
- args = cs.getRgb(args, 0);
- fn = _util.OPS.setStrokeRGBColor;
- break;
- case _util.OPS.setFillGray:
- stateManager.state.fillColorSpace = _colorspace.ColorSpace.singletons.gray;
- args = _colorspace.ColorSpace.singletons.gray.getRgb(args, 0);
- fn = _util.OPS.setFillRGBColor;
- break;
- case _util.OPS.setStrokeGray:
- stateManager.state.strokeColorSpace = _colorspace.ColorSpace.singletons.gray;
- args = _colorspace.ColorSpace.singletons.gray.getRgb(args, 0);
- fn = _util.OPS.setStrokeRGBColor;
- break;
- case _util.OPS.setFillCMYKColor:
- stateManager.state.fillColorSpace = _colorspace.ColorSpace.singletons.cmyk;
- args = _colorspace.ColorSpace.singletons.cmyk.getRgb(args, 0);
- fn = _util.OPS.setFillRGBColor;
- break;
- case _util.OPS.setStrokeCMYKColor:
- stateManager.state.strokeColorSpace = _colorspace.ColorSpace.singletons.cmyk;
- args = _colorspace.ColorSpace.singletons.cmyk.getRgb(args, 0);
- fn = _util.OPS.setStrokeRGBColor;
- break;
- case _util.OPS.setFillRGBColor:
- stateManager.state.fillColorSpace = _colorspace.ColorSpace.singletons.rgb;
- args = _colorspace.ColorSpace.singletons.rgb.getRgb(args, 0);
- break;
- case _util.OPS.setStrokeRGBColor:
- stateManager.state.strokeColorSpace = _colorspace.ColorSpace.singletons.rgb;
- args = _colorspace.ColorSpace.singletons.rgb.getRgb(args, 0);
- break;
- case _util.OPS.setFillColorN:
- cs = stateManager.state.fillColorSpace;
- if (cs.name === "Pattern") {
- next(self.handleColorN(operatorList, _util.OPS.setFillColorN, args, cs, patterns, resources, task, localColorSpaceCache, localTilingPatternCache, localShadingPatternCache));
- return;
- }
- args = cs.getRgb(args, 0);
- fn = _util.OPS.setFillRGBColor;
- break;
- case _util.OPS.setStrokeColorN:
- cs = stateManager.state.strokeColorSpace;
- if (cs.name === "Pattern") {
- next(self.handleColorN(operatorList, _util.OPS.setStrokeColorN, args, cs, patterns, resources, task, localColorSpaceCache, localTilingPatternCache, localShadingPatternCache));
- return;
- }
- args = cs.getRgb(args, 0);
- fn = _util.OPS.setStrokeRGBColor;
- break;
- case _util.OPS.shadingFill:
- var shadingRes = resources.get("Shading");
- if (!shadingRes) {
- throw new _util.FormatError("No shading resource found");
- }
- var shading = shadingRes.get(args[0].name);
- if (!shading) {
- throw new _util.FormatError("No shading object found");
- }
- const patternId = self.parseShading({
- shading,
- resources,
- localColorSpaceCache,
- localShadingPatternCache
- });
- args = [patternId];
- fn = _util.OPS.shadingFill;
- break;
- case _util.OPS.setGState:
- isValidName = args[0] instanceof _primitives.Name;
- name = args[0].name;
- if (isValidName) {
- const localGStateObj = localGStateCache.getByName(name);
- if (localGStateObj) {
- if (localGStateObj.length > 0) {
- operatorList.addOp(_util.OPS.setGState, [localGStateObj]);
- }
- args = null;
- continue;
- }
- }
- next(new Promise(function (resolveGState, rejectGState) {
- if (!isValidName) {
- throw new _util.FormatError("GState must be referred to by name.");
- }
- const extGState = resources.get("ExtGState");
- if (!(extGState instanceof _primitives.Dict)) {
- throw new _util.FormatError("ExtGState should be a dictionary.");
- }
- const gState = extGState.get(name);
- if (!(gState instanceof _primitives.Dict)) {
- throw new _util.FormatError("GState should be a dictionary.");
- }
- self.setGState({
- resources,
- gState,
- operatorList,
- cacheKey: name,
- task,
- stateManager,
- localGStateCache,
- localColorSpaceCache
- }).then(resolveGState, rejectGState);
- }).catch(function (reason) {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (self.options.ignoreErrors) {
- self.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorExtGState
- });
- (0, _util.warn)(`getOperatorList - ignoring ExtGState: "${reason}".`);
- return;
- }
- throw reason;
- }));
- return;
- case _util.OPS.moveTo:
- case _util.OPS.lineTo:
- case _util.OPS.curveTo:
- case _util.OPS.curveTo2:
- case _util.OPS.curveTo3:
- case _util.OPS.closePath:
- case _util.OPS.rectangle:
- self.buildPath(operatorList, fn, args, parsingText);
- continue;
- case _util.OPS.markPoint:
- case _util.OPS.markPointProps:
- case _util.OPS.beginCompat:
- case _util.OPS.endCompat:
- continue;
- case _util.OPS.beginMarkedContentProps:
- if (!(args[0] instanceof _primitives.Name)) {
- (0, _util.warn)(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
- continue;
- }
- if (args[0].name === "OC") {
- next(self.parseMarkedContentProps(args[1], resources).then(data => {
- operatorList.addOp(_util.OPS.beginMarkedContentProps, ["OC", data]);
- }).catch(reason => {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (self.options.ignoreErrors) {
- self.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorMarkedContent
- });
- (0, _util.warn)(`getOperatorList - ignoring beginMarkedContentProps: "${reason}".`);
- return;
- }
- throw reason;
- }));
- return;
- }
- args = [args[0].name, args[1] instanceof _primitives.Dict ? args[1].get("MCID") : null];
- break;
- case _util.OPS.beginMarkedContent:
- case _util.OPS.endMarkedContent:
- default:
- if (args !== null) {
- for (i = 0, ii = args.length; i < ii; i++) {
- if (args[i] instanceof _primitives.Dict) {
- break;
- }
- }
- if (i < ii) {
- (0, _util.warn)("getOperatorList - ignoring operator: " + fn);
- continue;
- }
- }
- }
- operatorList.addOp(fn, args);
- }
- if (stop) {
- next(deferred);
- return;
- }
- closePendingRestoreOPS();
- resolve();
- }).catch(reason => {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (this.options.ignoreErrors) {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorOperatorList
- });
- (0, _util.warn)(`getOperatorList - ignoring errors during "${task.name}" ` + `task: "${reason}".`);
- closePendingRestoreOPS();
- return;
- }
- throw reason;
- });
- }
- getTextContent({
- stream,
- task,
- resources,
- stateManager = null,
- combineTextItems = false,
- includeMarkedContent = false,
- sink,
- seenStyles = new Set(),
- viewBox
- }) {
- resources = resources || _primitives.Dict.empty;
- stateManager = stateManager || new StateManager(new TextState());
- const NormalizedUnicodes = (0, _unicode.getNormalizedUnicodes)();
- const textContent = {
- items: [],
- styles: Object.create(null)
- };
- const textContentItem = {
- initialized: false,
- str: [],
- totalWidth: 0,
- totalHeight: 0,
- width: 0,
- height: 0,
- vertical: false,
- prevTransform: null,
- textAdvanceScale: 0,
- spaceInFlowMin: 0,
- spaceInFlowMax: 0,
- trackingSpaceMin: Infinity,
- negativeSpaceMax: -Infinity,
- notASpace: -Infinity,
- transform: null,
- fontName: null,
- hasEOL: false
- };
- const twoLastChars = [" ", " "];
- let twoLastCharsPos = 0;
- function saveLastChar(char) {
- const nextPos = (twoLastCharsPos + 1) % 2;
- const ret = twoLastChars[twoLastCharsPos] !== " " && twoLastChars[nextPos] === " ";
- twoLastChars[twoLastCharsPos] = char;
- twoLastCharsPos = nextPos;
- return ret;
- }
- function resetLastChars() {
- twoLastChars[0] = twoLastChars[1] = " ";
- twoLastCharsPos = 0;
- }
- const TRACKING_SPACE_FACTOR = 0.1;
- const NOT_A_SPACE_FACTOR = 0.03;
- const NEGATIVE_SPACE_FACTOR = -0.2;
- const SPACE_IN_FLOW_MIN_FACTOR = 0.1;
- const SPACE_IN_FLOW_MAX_FACTOR = 0.6;
- const self = this;
- const xref = this.xref;
- const showSpacedTextBuffer = [];
- let xobjs = null;
- const emptyXObjectCache = new _image_utils.LocalImageCache();
- const emptyGStateCache = new _image_utils.LocalGStateCache();
- const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
- let textState;
- function getCurrentTextTransform() {
- const font = textState.font;
- const tsm = [textState.fontSize * textState.textHScale, 0, 0, textState.fontSize, 0, textState.textRise];
- if (font.isType3Font && (textState.fontSize <= 1 || font.isCharBBox) && !(0, _util.isArrayEqual)(textState.fontMatrix, _util.FONT_IDENTITY_MATRIX)) {
- const glyphHeight = font.bbox[3] - font.bbox[1];
- if (glyphHeight > 0) {
- tsm[3] *= glyphHeight * textState.fontMatrix[3];
- }
- }
- return _util.Util.transform(textState.ctm, _util.Util.transform(textState.textMatrix, tsm));
- }
- function ensureTextContentItem() {
- if (textContentItem.initialized) {
- return textContentItem;
- }
- const font = textState.font,
- loadedName = font.loadedName;
- if (!seenStyles.has(loadedName)) {
- seenStyles.add(loadedName);
- textContent.styles[loadedName] = {
- fontFamily: font.fallbackName,
- ascent: font.ascent,
- descent: font.descent,
- vertical: font.vertical
- };
- }
- textContentItem.fontName = loadedName;
- const trm = textContentItem.transform = getCurrentTextTransform();
- if (!font.vertical) {
- textContentItem.width = textContentItem.totalWidth = 0;
- textContentItem.height = textContentItem.totalHeight = Math.hypot(trm[2], trm[3]);
- textContentItem.vertical = false;
- } else {
- textContentItem.width = textContentItem.totalWidth = Math.hypot(trm[0], trm[1]);
- textContentItem.height = textContentItem.totalHeight = 0;
- textContentItem.vertical = true;
- }
- const scaleLineX = Math.hypot(textState.textLineMatrix[0], textState.textLineMatrix[1]);
- const scaleCtmX = Math.hypot(textState.ctm[0], textState.ctm[1]);
- textContentItem.textAdvanceScale = scaleCtmX * scaleLineX;
- textContentItem.trackingSpaceMin = textState.fontSize * TRACKING_SPACE_FACTOR;
- textContentItem.notASpace = textState.fontSize * NOT_A_SPACE_FACTOR;
- textContentItem.negativeSpaceMax = textState.fontSize * NEGATIVE_SPACE_FACTOR;
- textContentItem.spaceInFlowMin = textState.fontSize * SPACE_IN_FLOW_MIN_FACTOR;
- textContentItem.spaceInFlowMax = textState.fontSize * SPACE_IN_FLOW_MAX_FACTOR;
- textContentItem.hasEOL = false;
- textContentItem.initialized = true;
- return textContentItem;
- }
- function updateAdvanceScale() {
- if (!textContentItem.initialized) {
- return;
- }
- const scaleLineX = Math.hypot(textState.textLineMatrix[0], textState.textLineMatrix[1]);
- const scaleCtmX = Math.hypot(textState.ctm[0], textState.ctm[1]);
- const scaleFactor = scaleCtmX * scaleLineX;
- if (scaleFactor === textContentItem.textAdvanceScale) {
- return;
- }
- if (!textContentItem.vertical) {
- textContentItem.totalWidth += textContentItem.width * textContentItem.textAdvanceScale;
- textContentItem.width = 0;
- } else {
- textContentItem.totalHeight += textContentItem.height * textContentItem.textAdvanceScale;
- textContentItem.height = 0;
- }
- textContentItem.textAdvanceScale = scaleFactor;
- }
- function runBidiTransform(textChunk) {
- const text = textChunk.str.join("");
- const bidiResult = (0, _bidi.bidi)(text, -1, textChunk.vertical);
- return {
- str: bidiResult.str,
- dir: bidiResult.dir,
- width: Math.abs(textChunk.totalWidth),
- height: Math.abs(textChunk.totalHeight),
- transform: textChunk.transform,
- fontName: textChunk.fontName,
- hasEOL: textChunk.hasEOL
- };
- }
- function handleSetFont(fontName, fontRef) {
- return self.loadFont(fontName, fontRef, resources).then(function (translated) {
- if (!translated.font.isType3Font) {
- return translated;
- }
- return translated.loadType3Data(self, resources, task).catch(function () {}).then(function () {
- return translated;
- });
- }).then(function (translated) {
- textState.font = translated.font;
- textState.fontMatrix = translated.font.fontMatrix || _util.FONT_IDENTITY_MATRIX;
- });
- }
- function applyInverseRotation(x, y, matrix) {
- const scale = Math.hypot(matrix[0], matrix[1]);
- return [(matrix[0] * x + matrix[1] * y) / scale, (matrix[2] * x + matrix[3] * y) / scale];
- }
- function compareWithLastPosition() {
- const currentTransform = getCurrentTextTransform();
- let posX = currentTransform[4];
- let posY = currentTransform[5];
- const shiftedX = posX - viewBox[0];
- const shiftedY = posY - viewBox[1];
- if (shiftedX < 0 || shiftedX > viewBox[2] || shiftedY < 0 || shiftedY > viewBox[3]) {
- return false;
- }
- if (!combineTextItems || !textState.font || !textContentItem.prevTransform) {
- return true;
- }
- let lastPosX = textContentItem.prevTransform[4];
- let lastPosY = textContentItem.prevTransform[5];
- if (lastPosX === posX && lastPosY === posY) {
- return true;
- }
- let rotate = -1;
- if (currentTransform[0] && currentTransform[1] === 0 && currentTransform[2] === 0) {
- rotate = currentTransform[0] > 0 ? 0 : 180;
- } else if (currentTransform[1] && currentTransform[0] === 0 && currentTransform[3] === 0) {
- rotate = currentTransform[1] > 0 ? 90 : 270;
- }
- switch (rotate) {
- case 0:
- break;
- case 90:
- [posX, posY] = [posY, posX];
- [lastPosX, lastPosY] = [lastPosY, lastPosX];
- break;
- case 180:
- [posX, posY, lastPosX, lastPosY] = [-posX, -posY, -lastPosX, -lastPosY];
- break;
- case 270:
- [posX, posY] = [-posY, -posX];
- [lastPosX, lastPosY] = [-lastPosY, -lastPosX];
- break;
- default:
- [posX, posY] = applyInverseRotation(posX, posY, currentTransform);
- [lastPosX, lastPosY] = applyInverseRotation(lastPosX, lastPosY, textContentItem.prevTransform);
- }
- if (textState.font.vertical) {
- const advanceY = (lastPosY - posY) / textContentItem.textAdvanceScale;
- const advanceX = posX - lastPosX;
- const textOrientation = Math.sign(textContentItem.height);
- if (advanceY < textOrientation * textContentItem.negativeSpaceMax) {
- if (Math.abs(advanceX) > 0.5 * textContentItem.width) {
- appendEOL();
- return true;
- }
- resetLastChars();
- flushTextContentItem();
- return true;
- }
- if (Math.abs(advanceX) > textContentItem.width) {
- appendEOL();
- return true;
- }
- if (advanceY <= textOrientation * textContentItem.notASpace) {
- resetLastChars();
- }
- if (advanceY <= textOrientation * textContentItem.trackingSpaceMin) {
- textContentItem.height += advanceY;
- } else if (!addFakeSpaces(advanceY, textContentItem.prevTransform, textOrientation)) {
- if (textContentItem.str.length === 0) {
- resetLastChars();
- textContent.items.push({
- str: " ",
- dir: "ltr",
- width: 0,
- height: Math.abs(advanceY),
- transform: textContentItem.prevTransform,
- fontName: textContentItem.fontName,
- hasEOL: false
- });
- } else {
- textContentItem.height += advanceY;
- }
- }
- return true;
- }
- const advanceX = (posX - lastPosX) / textContentItem.textAdvanceScale;
- const advanceY = posY - lastPosY;
- const textOrientation = Math.sign(textContentItem.width);
- if (advanceX < textOrientation * textContentItem.negativeSpaceMax) {
- if (Math.abs(advanceY) > 0.5 * textContentItem.height) {
- appendEOL();
- return true;
- }
- resetLastChars();
- flushTextContentItem();
- return true;
- }
- if (Math.abs(advanceY) > textContentItem.height) {
- appendEOL();
- return true;
- }
- if (advanceX <= textOrientation * textContentItem.notASpace) {
- resetLastChars();
- }
- if (advanceX <= textOrientation * textContentItem.trackingSpaceMin) {
- textContentItem.width += advanceX;
- } else if (!addFakeSpaces(advanceX, textContentItem.prevTransform, textOrientation)) {
- if (textContentItem.str.length === 0) {
- resetLastChars();
- textContent.items.push({
- str: " ",
- dir: "ltr",
- width: Math.abs(advanceX),
- height: 0,
- transform: textContentItem.prevTransform,
- fontName: textContentItem.fontName,
- hasEOL: false
- });
- } else {
- textContentItem.width += advanceX;
- }
- }
- return true;
- }
- function buildTextContentItem({
- chars,
- extraSpacing
- }) {
- const font = textState.font;
- if (!chars) {
- const charSpacing = textState.charSpacing + extraSpacing;
- if (charSpacing) {
- if (!font.vertical) {
- textState.translateTextMatrix(charSpacing * textState.textHScale, 0);
- } else {
- textState.translateTextMatrix(0, -charSpacing);
- }
- }
- return;
- }
- const glyphs = font.charsToGlyphs(chars);
- const scale = textState.fontMatrix[0] * textState.fontSize;
- for (let i = 0, ii = glyphs.length; i < ii; i++) {
- const glyph = glyphs[i];
- if (glyph.isInvisibleFormatMark) {
- continue;
- }
- let charSpacing = textState.charSpacing + (i + 1 === ii ? extraSpacing : 0);
- let glyphWidth = glyph.width;
- if (font.vertical) {
- glyphWidth = glyph.vmetric ? glyph.vmetric[0] : -glyphWidth;
- }
- let scaledDim = glyphWidth * scale;
- if (glyph.isWhitespace) {
- if (!font.vertical) {
- charSpacing += scaledDim + textState.wordSpacing;
- textState.translateTextMatrix(charSpacing * textState.textHScale, 0);
- } else {
- charSpacing += -scaledDim + textState.wordSpacing;
- textState.translateTextMatrix(0, -charSpacing);
- }
- saveLastChar(" ");
- continue;
- }
- if (!compareWithLastPosition()) {
- continue;
- }
- const textChunk = ensureTextContentItem();
- if (glyph.isZeroWidthDiacritic) {
- scaledDim = 0;
- }
- if (!font.vertical) {
- scaledDim *= textState.textHScale;
- textState.translateTextMatrix(scaledDim, 0);
- textChunk.width += scaledDim;
- } else {
- textState.translateTextMatrix(0, scaledDim);
- scaledDim = Math.abs(scaledDim);
- textChunk.height += scaledDim;
- }
- if (scaledDim) {
- textChunk.prevTransform = getCurrentTextTransform();
- }
- let glyphUnicode = glyph.unicode;
- glyphUnicode = NormalizedUnicodes[glyphUnicode] || glyphUnicode;
- glyphUnicode = (0, _unicode.reverseIfRtl)(glyphUnicode);
- if (saveLastChar(glyphUnicode)) {
- textChunk.str.push(" ");
- }
- textChunk.str.push(glyphUnicode);
- if (charSpacing) {
- if (!font.vertical) {
- textState.translateTextMatrix(charSpacing * textState.textHScale, 0);
- } else {
- textState.translateTextMatrix(0, -charSpacing);
- }
- }
- }
- }
- function appendEOL() {
- resetLastChars();
- if (textContentItem.initialized) {
- textContentItem.hasEOL = true;
- flushTextContentItem();
- } else {
- textContent.items.push({
- str: "",
- dir: "ltr",
- width: 0,
- height: 0,
- transform: getCurrentTextTransform(),
- fontName: textState.font.loadedName,
- hasEOL: true
- });
- }
- }
- function addFakeSpaces(width, transf, textOrientation) {
- if (textOrientation * textContentItem.spaceInFlowMin <= width && width <= textOrientation * textContentItem.spaceInFlowMax) {
- if (textContentItem.initialized) {
- resetLastChars();
- textContentItem.str.push(" ");
- }
- return false;
- }
- const fontName = textContentItem.fontName;
- let height = 0;
- if (textContentItem.vertical) {
- height = width;
- width = 0;
- }
- flushTextContentItem();
- resetLastChars();
- textContent.items.push({
- str: " ",
- dir: "ltr",
- width: Math.abs(width),
- height: Math.abs(height),
- transform: transf || getCurrentTextTransform(),
- fontName,
- hasEOL: false
- });
- return true;
- }
- function flushTextContentItem() {
- if (!textContentItem.initialized || !textContentItem.str) {
- return;
- }
- if (!textContentItem.vertical) {
- textContentItem.totalWidth += textContentItem.width * textContentItem.textAdvanceScale;
- } else {
- textContentItem.totalHeight += textContentItem.height * textContentItem.textAdvanceScale;
- }
- textContent.items.push(runBidiTransform(textContentItem));
- textContentItem.initialized = false;
- textContentItem.str.length = 0;
- }
- function enqueueChunk(batch = false) {
- const length = textContent.items.length;
- if (length === 0) {
- return;
- }
- if (batch && length < TEXT_CHUNK_BATCH_SIZE) {
- return;
- }
- sink.enqueue(textContent, length);
- textContent.items = [];
- textContent.styles = Object.create(null);
- }
- const timeSlotManager = new TimeSlotManager();
- return new Promise(function promiseBody(resolve, reject) {
- const next = function (promise) {
- enqueueChunk(true);
- Promise.all([promise, sink.ready]).then(function () {
- try {
- promiseBody(resolve, reject);
- } catch (ex) {
- reject(ex);
- }
- }, reject);
- };
- task.ensureNotTerminated();
- timeSlotManager.reset();
- const operation = {};
- let stop,
- args = [];
- while (!(stop = timeSlotManager.check())) {
- args.length = 0;
- operation.args = args;
- if (!preprocessor.read(operation)) {
- break;
- }
- textState = stateManager.state;
- const fn = operation.fn;
- args = operation.args;
- switch (fn | 0) {
- case _util.OPS.setFont:
- var fontNameArg = args[0].name,
- fontSizeArg = args[1];
- if (textState.font && fontNameArg === textState.fontName && fontSizeArg === textState.fontSize) {
- break;
- }
- flushTextContentItem();
- textState.fontName = fontNameArg;
- textState.fontSize = fontSizeArg;
- next(handleSetFont(fontNameArg, null));
- return;
- case _util.OPS.setTextRise:
- textState.textRise = args[0];
- break;
- case _util.OPS.setHScale:
- textState.textHScale = args[0] / 100;
- break;
- case _util.OPS.setLeading:
- textState.leading = args[0];
- break;
- case _util.OPS.moveText:
- textState.translateTextLineMatrix(args[0], args[1]);
- textState.textMatrix = textState.textLineMatrix.slice();
- break;
- case _util.OPS.setLeadingMoveText:
- textState.leading = -args[1];
- textState.translateTextLineMatrix(args[0], args[1]);
- textState.textMatrix = textState.textLineMatrix.slice();
- break;
- case _util.OPS.nextLine:
- textState.carriageReturn();
- break;
- case _util.OPS.setTextMatrix:
- textState.setTextMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
- textState.setTextLineMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
- updateAdvanceScale();
- break;
- case _util.OPS.setCharSpacing:
- textState.charSpacing = args[0];
- break;
- case _util.OPS.setWordSpacing:
- textState.wordSpacing = args[0];
- break;
- case _util.OPS.beginText:
- textState.textMatrix = _util.IDENTITY_MATRIX.slice();
- textState.textLineMatrix = _util.IDENTITY_MATRIX.slice();
- break;
- case _util.OPS.showSpacedText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- const spaceFactor = (textState.font.vertical ? 1 : -1) * textState.fontSize / 1000;
- const elements = args[0];
- for (let i = 0, ii = elements.length; i < ii - 1; i++) {
- const item = elements[i];
- if (typeof item === "string") {
- showSpacedTextBuffer.push(item);
- } else if (typeof item === "number" && item !== 0) {
- const str = showSpacedTextBuffer.join("");
- showSpacedTextBuffer.length = 0;
- buildTextContentItem({
- chars: str,
- extraSpacing: item * spaceFactor
- });
- }
- }
- const item = elements.at(-1);
- if (typeof item === "string") {
- showSpacedTextBuffer.push(item);
- }
- if (showSpacedTextBuffer.length > 0) {
- const str = showSpacedTextBuffer.join("");
- showSpacedTextBuffer.length = 0;
- buildTextContentItem({
- chars: str,
- extraSpacing: 0
- });
- }
- break;
- case _util.OPS.showText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- buildTextContentItem({
- chars: args[0],
- extraSpacing: 0
- });
- break;
- case _util.OPS.nextLineShowText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- textState.carriageReturn();
- buildTextContentItem({
- chars: args[0],
- extraSpacing: 0
- });
- break;
- case _util.OPS.nextLineSetSpacingShowText:
- if (!stateManager.state.font) {
- self.ensureStateFont(stateManager.state);
- continue;
- }
- textState.wordSpacing = args[0];
- textState.charSpacing = args[1];
- textState.carriageReturn();
- buildTextContentItem({
- chars: args[2],
- extraSpacing: 0
- });
- break;
- case _util.OPS.paintXObject:
- flushTextContentItem();
- if (!xobjs) {
- xobjs = resources.get("XObject") || _primitives.Dict.empty;
- }
- var isValidName = args[0] instanceof _primitives.Name;
- var name = args[0].name;
- if (isValidName && emptyXObjectCache.getByName(name)) {
- break;
- }
- next(new Promise(function (resolveXObject, rejectXObject) {
- if (!isValidName) {
- throw new _util.FormatError("XObject must be referred to by name.");
- }
- let xobj = xobjs.getRaw(name);
- if (xobj instanceof _primitives.Ref) {
- if (emptyXObjectCache.getByRef(xobj)) {
- resolveXObject();
- return;
- }
- const globalImage = self.globalImageCache.getData(xobj, self.pageIndex);
- if (globalImage) {
- resolveXObject();
- return;
- }
- xobj = xref.fetch(xobj);
- }
- if (!(xobj instanceof _base_stream.BaseStream)) {
- throw new _util.FormatError("XObject should be a stream");
- }
- const type = xobj.dict.get("Subtype");
- if (!(type instanceof _primitives.Name)) {
- throw new _util.FormatError("XObject should have a Name subtype");
- }
- if (type.name !== "Form") {
- emptyXObjectCache.set(name, xobj.dict.objId, true);
- resolveXObject();
- return;
- }
- const currentState = stateManager.state.clone();
- const xObjStateManager = new StateManager(currentState);
- const matrix = xobj.dict.getArray("Matrix");
- if (Array.isArray(matrix) && matrix.length === 6) {
- xObjStateManager.transform(matrix);
- }
- enqueueChunk();
- const sinkWrapper = {
- enqueueInvoked: false,
- enqueue(chunk, size) {
- this.enqueueInvoked = true;
- sink.enqueue(chunk, size);
- },
- get desiredSize() {
- return sink.desiredSize;
- },
- get ready() {
- return sink.ready;
- }
- };
- self.getTextContent({
- stream: xobj,
- task,
- resources: xobj.dict.get("Resources") || resources,
- stateManager: xObjStateManager,
- combineTextItems,
- includeMarkedContent,
- sink: sinkWrapper,
- seenStyles,
- viewBox
- }).then(function () {
- if (!sinkWrapper.enqueueInvoked) {
- emptyXObjectCache.set(name, xobj.dict.objId, true);
- }
- resolveXObject();
- }, rejectXObject);
- }).catch(function (reason) {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (self.options.ignoreErrors) {
- (0, _util.warn)(`getTextContent - ignoring XObject: "${reason}".`);
- return;
- }
- throw reason;
- }));
- return;
- case _util.OPS.setGState:
- isValidName = args[0] instanceof _primitives.Name;
- name = args[0].name;
- if (isValidName && emptyGStateCache.getByName(name)) {
- break;
- }
- next(new Promise(function (resolveGState, rejectGState) {
- if (!isValidName) {
- throw new _util.FormatError("GState must be referred to by name.");
- }
- const extGState = resources.get("ExtGState");
- if (!(extGState instanceof _primitives.Dict)) {
- throw new _util.FormatError("ExtGState should be a dictionary.");
- }
- const gState = extGState.get(name);
- if (!(gState instanceof _primitives.Dict)) {
- throw new _util.FormatError("GState should be a dictionary.");
- }
- const gStateFont = gState.get("Font");
- if (!gStateFont) {
- emptyGStateCache.set(name, gState.objId, true);
- resolveGState();
- return;
- }
- flushTextContentItem();
- textState.fontName = null;
- textState.fontSize = gStateFont[1];
- handleSetFont(null, gStateFont[0]).then(resolveGState, rejectGState);
- }).catch(function (reason) {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (self.options.ignoreErrors) {
- (0, _util.warn)(`getTextContent - ignoring ExtGState: "${reason}".`);
- return;
- }
- throw reason;
- }));
- return;
- case _util.OPS.beginMarkedContent:
- flushTextContentItem();
- if (includeMarkedContent) {
- textContent.items.push({
- type: "beginMarkedContent",
- tag: args[0] instanceof _primitives.Name ? args[0].name : null
- });
- }
- break;
- case _util.OPS.beginMarkedContentProps:
- flushTextContentItem();
- if (includeMarkedContent) {
- let mcid = null;
- if (args[1] instanceof _primitives.Dict) {
- mcid = args[1].get("MCID");
- }
- textContent.items.push({
- type: "beginMarkedContentProps",
- id: Number.isInteger(mcid) ? `${self.idFactory.getPageObjId()}_mcid${mcid}` : null,
- tag: args[0] instanceof _primitives.Name ? args[0].name : null
- });
- }
- break;
- case _util.OPS.endMarkedContent:
- flushTextContentItem();
- if (includeMarkedContent) {
- textContent.items.push({
- type: "endMarkedContent"
- });
- }
- break;
- }
- if (textContent.items.length >= sink.desiredSize) {
- stop = true;
- break;
- }
- }
- if (stop) {
- next(deferred);
- return;
- }
- flushTextContentItem();
- enqueueChunk();
- resolve();
- }).catch(reason => {
- if (reason instanceof _util.AbortException) {
- return;
- }
- if (this.options.ignoreErrors) {
- (0, _util.warn)(`getTextContent - ignoring errors during "${task.name}" ` + `task: "${reason}".`);
- flushTextContentItem();
- enqueueChunk();
- return;
- }
- throw reason;
- });
- }
- extractDataStructures(dict, baseDict, properties) {
- const xref = this.xref;
- let cidToGidBytes;
- const toUnicodePromise = this.readToUnicode(properties.toUnicode || dict.get("ToUnicode") || baseDict.get("ToUnicode"));
- if (properties.composite) {
- const cidSystemInfo = dict.get("CIDSystemInfo");
- if (cidSystemInfo instanceof _primitives.Dict) {
- properties.cidSystemInfo = {
- registry: (0, _util.stringToPDFString)(cidSystemInfo.get("Registry")),
- ordering: (0, _util.stringToPDFString)(cidSystemInfo.get("Ordering")),
- supplement: cidSystemInfo.get("Supplement")
- };
- }
- try {
- const cidToGidMap = dict.get("CIDToGIDMap");
- if (cidToGidMap instanceof _base_stream.BaseStream) {
- cidToGidBytes = cidToGidMap.getBytes();
- }
- } catch (ex) {
- if (!this.options.ignoreErrors) {
- throw ex;
- }
- (0, _util.warn)(`extractDataStructures - ignoring CIDToGIDMap data: "${ex}".`);
- }
- }
- const differences = [];
- let baseEncodingName = null;
- let encoding;
- if (dict.has("Encoding")) {
- encoding = dict.get("Encoding");
- if (encoding instanceof _primitives.Dict) {
- baseEncodingName = encoding.get("BaseEncoding");
- baseEncodingName = baseEncodingName instanceof _primitives.Name ? baseEncodingName.name : null;
- if (encoding.has("Differences")) {
- const diffEncoding = encoding.get("Differences");
- let index = 0;
- for (let j = 0, jj = diffEncoding.length; j < jj; j++) {
- const data = xref.fetchIfRef(diffEncoding[j]);
- if (typeof data === "number") {
- index = data;
- } else if (data instanceof _primitives.Name) {
- differences[index++] = data.name;
- } else {
- throw new _util.FormatError(`Invalid entry in 'Differences' array: ${data}`);
- }
- }
- }
- } else if (encoding instanceof _primitives.Name) {
- baseEncodingName = encoding.name;
- } else {
- const msg = "Encoding is not a Name nor a Dict";
- if (!this.options.ignoreErrors) {
- throw new _util.FormatError(msg);
- }
- (0, _util.warn)(msg);
- }
- if (baseEncodingName !== "MacRomanEncoding" && baseEncodingName !== "MacExpertEncoding" && baseEncodingName !== "WinAnsiEncoding") {
- baseEncodingName = null;
- }
- }
- if (baseEncodingName) {
- properties.defaultEncoding = (0, _encodings.getEncoding)(baseEncodingName);
- } else {
- const isSymbolicFont = !!(properties.flags & _fonts_utils.FontFlags.Symbolic);
- const isNonsymbolicFont = !!(properties.flags & _fonts_utils.FontFlags.Nonsymbolic);
- encoding = _encodings.StandardEncoding;
- if (properties.type === "TrueType" && !isNonsymbolicFont) {
- encoding = _encodings.WinAnsiEncoding;
- }
- if (isSymbolicFont) {
- encoding = _encodings.MacRomanEncoding;
- if (!properties.file || properties.isInternalFont) {
- if (/Symbol/i.test(properties.name)) {
- encoding = _encodings.SymbolSetEncoding;
- } else if (/Dingbats|Wingdings/i.test(properties.name)) {
- encoding = _encodings.ZapfDingbatsEncoding;
- }
- }
- }
- properties.defaultEncoding = encoding;
- }
- properties.differences = differences;
- properties.baseEncodingName = baseEncodingName;
- properties.hasEncoding = !!baseEncodingName || differences.length > 0;
- properties.dict = dict;
- return toUnicodePromise.then(readToUnicode => {
- properties.toUnicode = readToUnicode;
- return this.buildToUnicode(properties);
- }).then(builtToUnicode => {
- properties.toUnicode = builtToUnicode;
- if (cidToGidBytes) {
- properties.cidToGidMap = this.readCidToGidMap(cidToGidBytes, builtToUnicode);
- }
- return properties;
- });
- }
- _simpleFontToUnicode(properties, forceGlyphs = false) {
- (0, _util.assert)(!properties.composite, "Must be a simple font.");
- const toUnicode = [];
- const encoding = properties.defaultEncoding.slice();
- const baseEncodingName = properties.baseEncodingName;
- const differences = properties.differences;
- for (const charcode in differences) {
- const glyphName = differences[charcode];
- if (glyphName === ".notdef") {
- continue;
- }
- encoding[charcode] = glyphName;
- }
- const glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
- for (const charcode in encoding) {
- let glyphName = encoding[charcode];
- if (glyphName === "") {
- continue;
- } else if (glyphsUnicodeMap[glyphName] === undefined) {
- let code = 0;
- switch (glyphName[0]) {
- case "G":
- if (glyphName.length === 3) {
- code = parseInt(glyphName.substring(1), 16);
- }
- break;
- case "g":
- if (glyphName.length === 5) {
- code = parseInt(glyphName.substring(1), 16);
- }
- break;
- case "C":
- case "c":
- if (glyphName.length >= 3 && glyphName.length <= 4) {
- const codeStr = glyphName.substring(1);
- if (forceGlyphs) {
- code = parseInt(codeStr, 16);
- break;
- }
- code = +codeStr;
- if (Number.isNaN(code) && Number.isInteger(parseInt(codeStr, 16))) {
- return this._simpleFontToUnicode(properties, true);
- }
- }
- break;
- default:
- const unicode = (0, _unicode.getUnicodeForGlyph)(glyphName, glyphsUnicodeMap);
- if (unicode !== -1) {
- code = unicode;
- }
- }
- if (code > 0 && code <= 0x10ffff && Number.isInteger(code)) {
- if (baseEncodingName && code === +charcode) {
- const baseEncoding = (0, _encodings.getEncoding)(baseEncodingName);
- if (baseEncoding && (glyphName = baseEncoding[charcode])) {
- toUnicode[charcode] = String.fromCharCode(glyphsUnicodeMap[glyphName]);
- continue;
- }
- }
- toUnicode[charcode] = String.fromCodePoint(code);
- }
- continue;
- }
- toUnicode[charcode] = String.fromCharCode(glyphsUnicodeMap[glyphName]);
- }
- return toUnicode;
- }
- async buildToUnicode(properties) {
- properties.hasIncludedToUnicodeMap = !!properties.toUnicode && properties.toUnicode.length > 0;
- if (properties.hasIncludedToUnicodeMap) {
- if (!properties.composite && properties.hasEncoding) {
- properties.fallbackToUnicode = this._simpleFontToUnicode(properties);
- }
- return properties.toUnicode;
- }
- if (!properties.composite) {
- return new _to_unicode_map.ToUnicodeMap(this._simpleFontToUnicode(properties));
- }
- if (properties.composite && (properties.cMap.builtInCMap && !(properties.cMap instanceof _cmap.IdentityCMap) || properties.cidSystemInfo.registry === "Adobe" && (properties.cidSystemInfo.ordering === "GB1" || properties.cidSystemInfo.ordering === "CNS1" || properties.cidSystemInfo.ordering === "Japan1" || properties.cidSystemInfo.ordering === "Korea1"))) {
- const {
- registry,
- ordering
- } = properties.cidSystemInfo;
- const ucs2CMapName = _primitives.Name.get(`${registry}-${ordering}-UCS2`);
- const ucs2CMap = await _cmap.CMapFactory.create({
- encoding: ucs2CMapName,
- fetchBuiltInCMap: this._fetchBuiltInCMapBound,
- useCMap: null
- });
- const toUnicode = [];
- properties.cMap.forEach(function (charcode, cid) {
- if (cid > 0xffff) {
- throw new _util.FormatError("Max size of CID is 65,535");
- }
- const ucs2 = ucs2CMap.lookup(cid);
- if (ucs2) {
- toUnicode[charcode] = String.fromCharCode((ucs2.charCodeAt(0) << 8) + ucs2.charCodeAt(1));
- }
- });
- return new _to_unicode_map.ToUnicodeMap(toUnicode);
- }
- return new _to_unicode_map.IdentityToUnicodeMap(properties.firstChar, properties.lastChar);
- }
- readToUnicode(cmapObj) {
- if (!cmapObj) {
- return Promise.resolve(null);
- }
- if (cmapObj instanceof _primitives.Name) {
- return _cmap.CMapFactory.create({
- encoding: cmapObj,
- fetchBuiltInCMap: this._fetchBuiltInCMapBound,
- useCMap: null
- }).then(function (cmap) {
- if (cmap instanceof _cmap.IdentityCMap) {
- return new _to_unicode_map.IdentityToUnicodeMap(0, 0xffff);
- }
- return new _to_unicode_map.ToUnicodeMap(cmap.getMap());
- });
- } else if (cmapObj instanceof _base_stream.BaseStream) {
- return _cmap.CMapFactory.create({
- encoding: cmapObj,
- fetchBuiltInCMap: this._fetchBuiltInCMapBound,
- useCMap: null
- }).then(function (cmap) {
- if (cmap instanceof _cmap.IdentityCMap) {
- return new _to_unicode_map.IdentityToUnicodeMap(0, 0xffff);
- }
- const map = new Array(cmap.length);
- cmap.forEach(function (charCode, token) {
- if (typeof token === "number") {
- map[charCode] = String.fromCodePoint(token);
- return;
- }
- const str = [];
- for (let k = 0; k < token.length; k += 2) {
- const w1 = token.charCodeAt(k) << 8 | token.charCodeAt(k + 1);
- if ((w1 & 0xf800) !== 0xd800) {
- str.push(w1);
- continue;
- }
- k += 2;
- const w2 = token.charCodeAt(k) << 8 | token.charCodeAt(k + 1);
- str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
- }
- map[charCode] = String.fromCodePoint(...str);
- });
- return new _to_unicode_map.ToUnicodeMap(map);
- }, reason => {
- if (reason instanceof _util.AbortException) {
- return null;
- }
- if (this.options.ignoreErrors) {
- this.handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorFontToUnicode
- });
- (0, _util.warn)(`readToUnicode - ignoring ToUnicode data: "${reason}".`);
- return null;
- }
- throw reason;
- });
- }
- return Promise.resolve(null);
- }
- readCidToGidMap(glyphsData, toUnicode) {
- const result = [];
- for (let j = 0, jj = glyphsData.length; j < jj; j++) {
- const glyphID = glyphsData[j++] << 8 | glyphsData[j];
- const code = j >> 1;
- if (glyphID === 0 && !toUnicode.has(code)) {
- continue;
- }
- result[code] = glyphID;
- }
- return result;
- }
- extractWidths(dict, descriptor, properties) {
- const xref = this.xref;
- let glyphsWidths = [];
- let defaultWidth = 0;
- const glyphsVMetrics = [];
- let defaultVMetrics;
- let i, ii, j, jj, start, code, widths;
- if (properties.composite) {
- defaultWidth = dict.has("DW") ? dict.get("DW") : 1000;
- widths = dict.get("W");
- if (widths) {
- for (i = 0, ii = widths.length; i < ii; i++) {
- start = xref.fetchIfRef(widths[i++]);
- code = xref.fetchIfRef(widths[i]);
- if (Array.isArray(code)) {
- for (j = 0, jj = code.length; j < jj; j++) {
- glyphsWidths[start++] = xref.fetchIfRef(code[j]);
- }
- } else {
- const width = xref.fetchIfRef(widths[++i]);
- for (j = start; j <= code; j++) {
- glyphsWidths[j] = width;
- }
- }
- }
- }
- if (properties.vertical) {
- let vmetrics = dict.getArray("DW2") || [880, -1000];
- defaultVMetrics = [vmetrics[1], defaultWidth * 0.5, vmetrics[0]];
- vmetrics = dict.get("W2");
- if (vmetrics) {
- for (i = 0, ii = vmetrics.length; i < ii; i++) {
- start = xref.fetchIfRef(vmetrics[i++]);
- code = xref.fetchIfRef(vmetrics[i]);
- if (Array.isArray(code)) {
- for (j = 0, jj = code.length; j < jj; j++) {
- glyphsVMetrics[start++] = [xref.fetchIfRef(code[j++]), xref.fetchIfRef(code[j++]), xref.fetchIfRef(code[j])];
- }
- } else {
- const vmetric = [xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i])];
- for (j = start; j <= code; j++) {
- glyphsVMetrics[j] = vmetric;
- }
- }
- }
- }
- }
- } else {
- const firstChar = properties.firstChar;
- widths = dict.get("Widths");
- if (widths) {
- j = firstChar;
- for (i = 0, ii = widths.length; i < ii; i++) {
- glyphsWidths[j++] = xref.fetchIfRef(widths[i]);
- }
- defaultWidth = parseFloat(descriptor.get("MissingWidth")) || 0;
- } else {
- const baseFontName = dict.get("BaseFont");
- if (baseFontName instanceof _primitives.Name) {
- const metrics = this.getBaseFontMetrics(baseFontName.name);
- glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties);
- defaultWidth = metrics.defaultWidth;
- }
- }
- }
- let isMonospace = true;
- let firstWidth = defaultWidth;
- for (const glyph in glyphsWidths) {
- const glyphWidth = glyphsWidths[glyph];
- if (!glyphWidth) {
- continue;
- }
- if (!firstWidth) {
- firstWidth = glyphWidth;
- continue;
- }
- if (firstWidth !== glyphWidth) {
- isMonospace = false;
- break;
- }
- }
- if (isMonospace) {
- properties.flags |= _fonts_utils.FontFlags.FixedPitch;
- }
- properties.defaultWidth = defaultWidth;
- properties.widths = glyphsWidths;
- properties.defaultVMetrics = defaultVMetrics;
- properties.vmetrics = glyphsVMetrics;
- }
- isSerifFont(baseFontName) {
- const fontNameWoStyle = baseFontName.split("-")[0];
- return fontNameWoStyle in (0, _standard_fonts.getSerifFonts)() || fontNameWoStyle.search(/serif/gi) !== -1;
- }
- getBaseFontMetrics(name) {
- let defaultWidth = 0;
- let widths = Object.create(null);
- let monospace = false;
- const stdFontMap = (0, _standard_fonts.getStdFontMap)();
- let lookupName = stdFontMap[name] || name;
- const Metrics = (0, _metrics.getMetrics)();
- if (!(lookupName in Metrics)) {
- if (this.isSerifFont(name)) {
- lookupName = "Times-Roman";
- } else {
- lookupName = "Helvetica";
- }
- }
- const glyphWidths = Metrics[lookupName];
- if (typeof glyphWidths === "number") {
- defaultWidth = glyphWidths;
- monospace = true;
- } else {
- widths = glyphWidths();
- }
- return {
- defaultWidth,
- monospace,
- widths
- };
- }
- buildCharCodeToWidth(widthsByGlyphName, properties) {
- const widths = Object.create(null);
- const differences = properties.differences;
- const encoding = properties.defaultEncoding;
- for (let charCode = 0; charCode < 256; charCode++) {
- if (charCode in differences && widthsByGlyphName[differences[charCode]]) {
- widths[charCode] = widthsByGlyphName[differences[charCode]];
- continue;
- }
- if (charCode in encoding && widthsByGlyphName[encoding[charCode]]) {
- widths[charCode] = widthsByGlyphName[encoding[charCode]];
- continue;
- }
- }
- return widths;
- }
- preEvaluateFont(dict) {
- const baseDict = dict;
- let type = dict.get("Subtype");
- if (!(type instanceof _primitives.Name)) {
- throw new _util.FormatError("invalid font Subtype");
- }
- let composite = false;
- let hash, toUnicode;
- if (type.name === "Type0") {
- const df = dict.get("DescendantFonts");
- if (!df) {
- throw new _util.FormatError("Descendant fonts are not specified");
- }
- dict = Array.isArray(df) ? this.xref.fetchIfRef(df[0]) : df;
- if (!(dict instanceof _primitives.Dict)) {
- throw new _util.FormatError("Descendant font is not a dictionary.");
- }
- type = dict.get("Subtype");
- if (!(type instanceof _primitives.Name)) {
- throw new _util.FormatError("invalid font Subtype");
- }
- composite = true;
- }
- const firstChar = dict.get("FirstChar") || 0,
- lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff);
- const descriptor = dict.get("FontDescriptor");
- if (descriptor) {
- hash = new _murmurhash.MurmurHash3_64();
- const encoding = baseDict.getRaw("Encoding");
- if (encoding instanceof _primitives.Name) {
- hash.update(encoding.name);
- } else if (encoding instanceof _primitives.Ref) {
- hash.update(encoding.toString());
- } else if (encoding instanceof _primitives.Dict) {
- for (const entry of encoding.getRawValues()) {
- if (entry instanceof _primitives.Name) {
- hash.update(entry.name);
- } else if (entry instanceof _primitives.Ref) {
- hash.update(entry.toString());
- } else if (Array.isArray(entry)) {
- const diffLength = entry.length,
- diffBuf = new Array(diffLength);
- for (let j = 0; j < diffLength; j++) {
- const diffEntry = entry[j];
- if (diffEntry instanceof _primitives.Name) {
- diffBuf[j] = diffEntry.name;
- } else if (typeof diffEntry === "number" || diffEntry instanceof _primitives.Ref) {
- diffBuf[j] = diffEntry.toString();
- }
- }
- hash.update(diffBuf.join());
- }
- }
- }
- hash.update(`${firstChar}-${lastChar}`);
- toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
- if (toUnicode instanceof _base_stream.BaseStream) {
- const stream = toUnicode.str || toUnicode;
- const uint8array = stream.buffer ? new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength) : new Uint8Array(stream.bytes.buffer, stream.start, stream.end - stream.start);
- hash.update(uint8array);
- } else if (toUnicode instanceof _primitives.Name) {
- hash.update(toUnicode.name);
- }
- const widths = dict.get("Widths") || baseDict.get("Widths");
- if (Array.isArray(widths)) {
- const widthsBuf = [];
- for (const entry of widths) {
- if (typeof entry === "number" || entry instanceof _primitives.Ref) {
- widthsBuf.push(entry.toString());
- }
- }
- hash.update(widthsBuf.join());
- }
- if (composite) {
- hash.update("compositeFont");
- const compositeWidths = dict.get("W") || baseDict.get("W");
- if (Array.isArray(compositeWidths)) {
- const widthsBuf = [];
- for (const entry of compositeWidths) {
- if (typeof entry === "number" || entry instanceof _primitives.Ref) {
- widthsBuf.push(entry.toString());
- } else if (Array.isArray(entry)) {
- const subWidthsBuf = [];
- for (const element of entry) {
- if (typeof element === "number" || element instanceof _primitives.Ref) {
- subWidthsBuf.push(element.toString());
- }
- }
- widthsBuf.push(`[${subWidthsBuf.join()}]`);
- }
- }
- hash.update(widthsBuf.join());
- }
- const cidToGidMap = dict.getRaw("CIDToGIDMap") || baseDict.getRaw("CIDToGIDMap");
- if (cidToGidMap instanceof _primitives.Name) {
- hash.update(cidToGidMap.name);
- } else if (cidToGidMap instanceof _primitives.Ref) {
- hash.update(cidToGidMap.toString());
- } else if (cidToGidMap instanceof _base_stream.BaseStream) {
- hash.update(cidToGidMap.peekBytes());
- }
- }
- }
- return {
- descriptor,
- dict,
- baseDict,
- composite,
- type: type.name,
- firstChar,
- lastChar,
- toUnicode,
- hash: hash ? hash.hexdigest() : ""
- };
- }
- async translateFont({
- descriptor,
- dict,
- baseDict,
- composite,
- type,
- firstChar,
- lastChar,
- toUnicode,
- cssFontInfo
- }) {
- const isType3Font = type === "Type3";
- let properties;
- if (!descriptor) {
- if (isType3Font) {
- descriptor = new _primitives.Dict(null);
- descriptor.set("FontName", _primitives.Name.get(type));
- descriptor.set("FontBBox", dict.getArray("FontBBox") || [0, 0, 0, 0]);
- } else {
- let baseFontName = dict.get("BaseFont");
- if (!(baseFontName instanceof _primitives.Name)) {
- throw new _util.FormatError("Base font is not specified");
- }
- baseFontName = baseFontName.name.replace(/[,_]/g, "-");
- const metrics = this.getBaseFontMetrics(baseFontName);
- const fontNameWoStyle = baseFontName.split("-")[0];
- const flags = (this.isSerifFont(fontNameWoStyle) ? _fonts_utils.FontFlags.Serif : 0) | (metrics.monospace ? _fonts_utils.FontFlags.FixedPitch : 0) | ((0, _standard_fonts.getSymbolsFonts)()[fontNameWoStyle] ? _fonts_utils.FontFlags.Symbolic : _fonts_utils.FontFlags.Nonsymbolic);
- properties = {
- type,
- name: baseFontName,
- loadedName: baseDict.loadedName,
- widths: metrics.widths,
- defaultWidth: metrics.defaultWidth,
- isSimulatedFlags: true,
- flags,
- firstChar,
- lastChar,
- toUnicode,
- xHeight: 0,
- capHeight: 0,
- italicAngle: 0,
- isType3Font
- };
- const widths = dict.get("Widths");
- const standardFontName = (0, _standard_fonts.getStandardFontName)(baseFontName);
- let file = null;
- if (standardFontName) {
- properties.isStandardFont = true;
- file = await this.fetchStandardFontData(standardFontName);
- properties.isInternalFont = !!file;
- }
- return this.extractDataStructures(dict, dict, properties).then(newProperties => {
- if (widths) {
- const glyphWidths = [];
- let j = firstChar;
- for (let i = 0, ii = widths.length; i < ii; i++) {
- glyphWidths[j++] = this.xref.fetchIfRef(widths[i]);
- }
- newProperties.widths = glyphWidths;
- } else {
- newProperties.widths = this.buildCharCodeToWidth(metrics.widths, newProperties);
- }
- return new _fonts.Font(baseFontName, file, newProperties);
- });
- }
- }
- let fontName = descriptor.get("FontName");
- let baseFont = dict.get("BaseFont");
- if (typeof fontName === "string") {
- fontName = _primitives.Name.get(fontName);
- }
- if (typeof baseFont === "string") {
- baseFont = _primitives.Name.get(baseFont);
- }
- if (!isType3Font) {
- const fontNameStr = fontName && fontName.name;
- const baseFontStr = baseFont && baseFont.name;
- if (fontNameStr !== baseFontStr) {
- (0, _util.info)(`The FontDescriptor's FontName is "${fontNameStr}" but ` + `should be the same as the Font's BaseFont "${baseFontStr}".`);
- if (fontNameStr && baseFontStr && baseFontStr.startsWith(fontNameStr)) {
- fontName = baseFont;
- }
- }
- }
- fontName = fontName || baseFont;
- if (!(fontName instanceof _primitives.Name)) {
- throw new _util.FormatError("invalid font name");
- }
- let fontFile, subtype, length1, length2, length3;
- try {
- fontFile = descriptor.get("FontFile", "FontFile2", "FontFile3");
- } catch (ex) {
- if (!this.options.ignoreErrors) {
- throw ex;
- }
- (0, _util.warn)(`translateFont - fetching "${fontName.name}" font file: "${ex}".`);
- fontFile = new _stream.NullStream();
- }
- let isStandardFont = false;
- let isInternalFont = false;
- let glyphScaleFactors = null;
- if (fontFile) {
- if (fontFile.dict) {
- const subtypeEntry = fontFile.dict.get("Subtype");
- if (subtypeEntry instanceof _primitives.Name) {
- subtype = subtypeEntry.name;
- }
- length1 = fontFile.dict.get("Length1");
- length2 = fontFile.dict.get("Length2");
- length3 = fontFile.dict.get("Length3");
- }
- } else if (cssFontInfo) {
- const standardFontName = (0, _xfa_fonts.getXfaFontName)(fontName.name);
- if (standardFontName) {
- cssFontInfo.fontFamily = `${cssFontInfo.fontFamily}-PdfJS-XFA`;
- cssFontInfo.metrics = standardFontName.metrics || null;
- glyphScaleFactors = standardFontName.factors || null;
- fontFile = await this.fetchStandardFontData(standardFontName.name);
- isInternalFont = !!fontFile;
- baseDict = dict = (0, _xfa_fonts.getXfaFontDict)(fontName.name);
- composite = true;
- }
- } else if (!isType3Font) {
- const standardFontName = (0, _standard_fonts.getStandardFontName)(fontName.name);
- if (standardFontName) {
- isStandardFont = true;
- fontFile = await this.fetchStandardFontData(standardFontName);
- isInternalFont = !!fontFile;
- }
- }
- properties = {
- type,
- name: fontName.name,
- subtype,
- file: fontFile,
- length1,
- length2,
- length3,
- isStandardFont,
- isInternalFont,
- loadedName: baseDict.loadedName,
- composite,
- fixedPitch: false,
- fontMatrix: dict.getArray("FontMatrix") || _util.FONT_IDENTITY_MATRIX,
- firstChar,
- lastChar,
- toUnicode,
- bbox: descriptor.getArray("FontBBox") || dict.getArray("FontBBox"),
- ascent: descriptor.get("Ascent"),
- descent: descriptor.get("Descent"),
- xHeight: descriptor.get("XHeight") || 0,
- capHeight: descriptor.get("CapHeight") || 0,
- flags: descriptor.get("Flags"),
- italicAngle: descriptor.get("ItalicAngle") || 0,
- isType3Font,
- cssFontInfo,
- scaleFactors: glyphScaleFactors
- };
- if (composite) {
- const cidEncoding = baseDict.get("Encoding");
- if (cidEncoding instanceof _primitives.Name) {
- properties.cidEncoding = cidEncoding.name;
- }
- const cMap = await _cmap.CMapFactory.create({
- encoding: cidEncoding,
- fetchBuiltInCMap: this._fetchBuiltInCMapBound,
- useCMap: null
- });
- properties.cMap = cMap;
- properties.vertical = properties.cMap.vertical;
- }
- return this.extractDataStructures(dict, baseDict, properties).then(newProperties => {
- this.extractWidths(dict, descriptor, newProperties);
- return new _fonts.Font(fontName.name, fontFile, newProperties);
- });
- }
- static buildFontPaths(font, glyphs, handler, evaluatorOptions) {
- function buildPath(fontChar) {
- const glyphName = `${font.loadedName}_path_${fontChar}`;
- try {
- if (font.renderer.hasBuiltPath(fontChar)) {
- return;
- }
- handler.send("commonobj", [glyphName, "FontPath", font.renderer.getPathJs(fontChar)]);
- } catch (reason) {
- if (evaluatorOptions.ignoreErrors) {
- handler.send("UnsupportedFeature", {
- featureId: _util.UNSUPPORTED_FEATURES.errorFontBuildPath
- });
- (0, _util.warn)(`buildFontPaths - ignoring ${glyphName} glyph: "${reason}".`);
- return;
- }
- throw reason;
- }
- }
- for (const glyph of glyphs) {
- buildPath(glyph.fontChar);
- const accent = glyph.accent;
- if (accent && accent.fontChar) {
- buildPath(accent.fontChar);
- }
- }
- }
- static get fallbackFontDict() {
- const dict = new _primitives.Dict();
- dict.set("BaseFont", _primitives.Name.get("PDFJS-FallbackFont"));
- dict.set("Type", _primitives.Name.get("FallbackType"));
- dict.set("Subtype", _primitives.Name.get("FallbackType"));
- dict.set("Encoding", _primitives.Name.get("WinAnsiEncoding"));
- return (0, _util.shadow)(this, "fallbackFontDict", dict);
- }
- }
- exports.PartialEvaluator = PartialEvaluator;
- class TranslatedFont {
- constructor({
- loadedName,
- font,
- dict,
- evaluatorOptions
- }) {
- this.loadedName = loadedName;
- this.font = font;
- this.dict = dict;
- this._evaluatorOptions = evaluatorOptions || DefaultPartialEvaluatorOptions;
- this.type3Loaded = null;
- this.type3Dependencies = font.isType3Font ? new Set() : null;
- this.sent = false;
- }
- send(handler) {
- if (this.sent) {
- return;
- }
- this.sent = true;
- handler.send("commonobj", [this.loadedName, "Font", this.font.exportData(this._evaluatorOptions.fontExtraProperties)]);
- }
- fallback(handler) {
- if (!this.font.data) {
- return;
- }
- this.font.disableFontFace = true;
- PartialEvaluator.buildFontPaths(this.font, this.font.glyphCacheValues, handler, this._evaluatorOptions);
- }
- loadType3Data(evaluator, resources, task) {
- if (this.type3Loaded) {
- return this.type3Loaded;
- }
- if (!this.font.isType3Font) {
- throw new Error("Must be a Type3 font.");
- }
- const type3Evaluator = evaluator.clone({
- ignoreErrors: false
- });
- type3Evaluator.parsingType3Font = true;
- const type3FontRefs = new _primitives.RefSet(evaluator.type3FontRefs);
- if (this.dict.objId && !type3FontRefs.has(this.dict.objId)) {
- type3FontRefs.put(this.dict.objId);
- }
- type3Evaluator.type3FontRefs = type3FontRefs;
- const translatedFont = this.font,
- type3Dependencies = this.type3Dependencies;
- let loadCharProcsPromise = Promise.resolve();
- const charProcs = this.dict.get("CharProcs");
- const fontResources = this.dict.get("Resources") || resources;
- const charProcOperatorList = Object.create(null);
- const fontBBox = _util.Util.normalizeRect(translatedFont.bbox || [0, 0, 0, 0]),
- width = fontBBox[2] - fontBBox[0],
- height = fontBBox[3] - fontBBox[1];
- const fontBBoxSize = Math.hypot(width, height);
- for (const key of charProcs.getKeys()) {
- loadCharProcsPromise = loadCharProcsPromise.then(() => {
- const glyphStream = charProcs.get(key);
- const operatorList = new _operator_list.OperatorList();
- return type3Evaluator.getOperatorList({
- stream: glyphStream,
- task,
- resources: fontResources,
- operatorList
- }).then(() => {
- if (operatorList.fnArray[0] === _util.OPS.setCharWidthAndBounds) {
- this._removeType3ColorOperators(operatorList, fontBBoxSize);
- }
- charProcOperatorList[key] = operatorList.getIR();
- for (const dependency of operatorList.dependencies) {
- type3Dependencies.add(dependency);
- }
- }).catch(function (reason) {
- (0, _util.warn)(`Type3 font resource "${key}" is not available.`);
- const dummyOperatorList = new _operator_list.OperatorList();
- charProcOperatorList[key] = dummyOperatorList.getIR();
- });
- });
- }
- this.type3Loaded = loadCharProcsPromise.then(() => {
- translatedFont.charProcOperatorList = charProcOperatorList;
- if (this._bbox) {
- translatedFont.isCharBBox = true;
- translatedFont.bbox = this._bbox;
- }
- });
- return this.type3Loaded;
- }
- _removeType3ColorOperators(operatorList, fontBBoxSize = NaN) {
- const charBBox = _util.Util.normalizeRect(operatorList.argsArray[0].slice(2)),
- width = charBBox[2] - charBBox[0],
- height = charBBox[3] - charBBox[1];
- const charBBoxSize = Math.hypot(width, height);
- if (width === 0 || height === 0) {
- operatorList.fnArray.splice(0, 1);
- operatorList.argsArray.splice(0, 1);
- } else if (fontBBoxSize === 0 || Math.round(charBBoxSize / fontBBoxSize) >= 10) {
- if (!this._bbox) {
- this._bbox = [Infinity, Infinity, -Infinity, -Infinity];
- }
- this._bbox[0] = Math.min(this._bbox[0], charBBox[0]);
- this._bbox[1] = Math.min(this._bbox[1], charBBox[1]);
- this._bbox[2] = Math.max(this._bbox[2], charBBox[2]);
- this._bbox[3] = Math.max(this._bbox[3], charBBox[3]);
- }
- let i = 0,
- ii = operatorList.length;
- while (i < ii) {
- switch (operatorList.fnArray[i]) {
- case _util.OPS.setCharWidthAndBounds:
- break;
- case _util.OPS.setStrokeColorSpace:
- case _util.OPS.setFillColorSpace:
- case _util.OPS.setStrokeColor:
- case _util.OPS.setStrokeColorN:
- case _util.OPS.setFillColor:
- case _util.OPS.setFillColorN:
- case _util.OPS.setStrokeGray:
- case _util.OPS.setFillGray:
- case _util.OPS.setStrokeRGBColor:
- case _util.OPS.setFillRGBColor:
- case _util.OPS.setStrokeCMYKColor:
- case _util.OPS.setFillCMYKColor:
- case _util.OPS.shadingFill:
- case _util.OPS.setRenderingIntent:
- operatorList.fnArray.splice(i, 1);
- operatorList.argsArray.splice(i, 1);
- ii--;
- continue;
- case _util.OPS.setGState:
- const [gStateObj] = operatorList.argsArray[i];
- let j = 0,
- jj = gStateObj.length;
- while (j < jj) {
- const [gStateKey] = gStateObj[j];
- switch (gStateKey) {
- case "TR":
- case "TR2":
- case "HT":
- case "BG":
- case "BG2":
- case "UCR":
- case "UCR2":
- gStateObj.splice(j, 1);
- jj--;
- continue;
- }
- j++;
- }
- break;
- }
- i++;
- }
- }
- }
- class StateManager {
- constructor(initialState = new EvalState()) {
- this.state = initialState;
- this.stateStack = [];
- }
- save() {
- const old = this.state;
- this.stateStack.push(this.state);
- this.state = old.clone();
- }
- restore() {
- const prev = this.stateStack.pop();
- if (prev) {
- this.state = prev;
- }
- }
- transform(args) {
- this.state.ctm = _util.Util.transform(this.state.ctm, args);
- }
- }
- class TextState {
- constructor() {
- this.ctm = new Float32Array(_util.IDENTITY_MATRIX);
- this.fontName = null;
- this.fontSize = 0;
- this.font = null;
- this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
- this.textMatrix = _util.IDENTITY_MATRIX.slice();
- this.textLineMatrix = _util.IDENTITY_MATRIX.slice();
- this.charSpacing = 0;
- this.wordSpacing = 0;
- this.leading = 0;
- this.textHScale = 1;
- this.textRise = 0;
- }
- setTextMatrix(a, b, c, d, e, f) {
- const m = this.textMatrix;
- m[0] = a;
- m[1] = b;
- m[2] = c;
- m[3] = d;
- m[4] = e;
- m[5] = f;
- }
- setTextLineMatrix(a, b, c, d, e, f) {
- const m = this.textLineMatrix;
- m[0] = a;
- m[1] = b;
- m[2] = c;
- m[3] = d;
- m[4] = e;
- m[5] = f;
- }
- translateTextMatrix(x, y) {
- const m = this.textMatrix;
- m[4] = m[0] * x + m[2] * y + m[4];
- m[5] = m[1] * x + m[3] * y + m[5];
- }
- translateTextLineMatrix(x, y) {
- const m = this.textLineMatrix;
- m[4] = m[0] * x + m[2] * y + m[4];
- m[5] = m[1] * x + m[3] * y + m[5];
- }
- carriageReturn() {
- this.translateTextLineMatrix(0, -this.leading);
- this.textMatrix = this.textLineMatrix.slice();
- }
- clone() {
- const clone = Object.create(this);
- clone.textMatrix = this.textMatrix.slice();
- clone.textLineMatrix = this.textLineMatrix.slice();
- clone.fontMatrix = this.fontMatrix.slice();
- return clone;
- }
- }
- class EvalState {
- constructor() {
- this.ctm = new Float32Array(_util.IDENTITY_MATRIX);
- this.font = null;
- this.textRenderingMode = _util.TextRenderingMode.FILL;
- this.fillColorSpace = _colorspace.ColorSpace.singletons.gray;
- this.strokeColorSpace = _colorspace.ColorSpace.singletons.gray;
- }
- clone() {
- return Object.create(this);
- }
- }
- class EvaluatorPreprocessor {
- static get opMap() {
- const getOPMap = (0, _core_utils.getLookupTableFactory)(function (t) {
- t.w = {
- id: _util.OPS.setLineWidth,
- numArgs: 1,
- variableArgs: false
- };
- t.J = {
- id: _util.OPS.setLineCap,
- numArgs: 1,
- variableArgs: false
- };
- t.j = {
- id: _util.OPS.setLineJoin,
- numArgs: 1,
- variableArgs: false
- };
- t.M = {
- id: _util.OPS.setMiterLimit,
- numArgs: 1,
- variableArgs: false
- };
- t.d = {
- id: _util.OPS.setDash,
- numArgs: 2,
- variableArgs: false
- };
- t.ri = {
- id: _util.OPS.setRenderingIntent,
- numArgs: 1,
- variableArgs: false
- };
- t.i = {
- id: _util.OPS.setFlatness,
- numArgs: 1,
- variableArgs: false
- };
- t.gs = {
- id: _util.OPS.setGState,
- numArgs: 1,
- variableArgs: false
- };
- t.q = {
- id: _util.OPS.save,
- numArgs: 0,
- variableArgs: false
- };
- t.Q = {
- id: _util.OPS.restore,
- numArgs: 0,
- variableArgs: false
- };
- t.cm = {
- id: _util.OPS.transform,
- numArgs: 6,
- variableArgs: false
- };
- t.m = {
- id: _util.OPS.moveTo,
- numArgs: 2,
- variableArgs: false
- };
- t.l = {
- id: _util.OPS.lineTo,
- numArgs: 2,
- variableArgs: false
- };
- t.c = {
- id: _util.OPS.curveTo,
- numArgs: 6,
- variableArgs: false
- };
- t.v = {
- id: _util.OPS.curveTo2,
- numArgs: 4,
- variableArgs: false
- };
- t.y = {
- id: _util.OPS.curveTo3,
- numArgs: 4,
- variableArgs: false
- };
- t.h = {
- id: _util.OPS.closePath,
- numArgs: 0,
- variableArgs: false
- };
- t.re = {
- id: _util.OPS.rectangle,
- numArgs: 4,
- variableArgs: false
- };
- t.S = {
- id: _util.OPS.stroke,
- numArgs: 0,
- variableArgs: false
- };
- t.s = {
- id: _util.OPS.closeStroke,
- numArgs: 0,
- variableArgs: false
- };
- t.f = {
- id: _util.OPS.fill,
- numArgs: 0,
- variableArgs: false
- };
- t.F = {
- id: _util.OPS.fill,
- numArgs: 0,
- variableArgs: false
- };
- t["f*"] = {
- id: _util.OPS.eoFill,
- numArgs: 0,
- variableArgs: false
- };
- t.B = {
- id: _util.OPS.fillStroke,
- numArgs: 0,
- variableArgs: false
- };
- t["B*"] = {
- id: _util.OPS.eoFillStroke,
- numArgs: 0,
- variableArgs: false
- };
- t.b = {
- id: _util.OPS.closeFillStroke,
- numArgs: 0,
- variableArgs: false
- };
- t["b*"] = {
- id: _util.OPS.closeEOFillStroke,
- numArgs: 0,
- variableArgs: false
- };
- t.n = {
- id: _util.OPS.endPath,
- numArgs: 0,
- variableArgs: false
- };
- t.W = {
- id: _util.OPS.clip,
- numArgs: 0,
- variableArgs: false
- };
- t["W*"] = {
- id: _util.OPS.eoClip,
- numArgs: 0,
- variableArgs: false
- };
- t.BT = {
- id: _util.OPS.beginText,
- numArgs: 0,
- variableArgs: false
- };
- t.ET = {
- id: _util.OPS.endText,
- numArgs: 0,
- variableArgs: false
- };
- t.Tc = {
- id: _util.OPS.setCharSpacing,
- numArgs: 1,
- variableArgs: false
- };
- t.Tw = {
- id: _util.OPS.setWordSpacing,
- numArgs: 1,
- variableArgs: false
- };
- t.Tz = {
- id: _util.OPS.setHScale,
- numArgs: 1,
- variableArgs: false
- };
- t.TL = {
- id: _util.OPS.setLeading,
- numArgs: 1,
- variableArgs: false
- };
- t.Tf = {
- id: _util.OPS.setFont,
- numArgs: 2,
- variableArgs: false
- };
- t.Tr = {
- id: _util.OPS.setTextRenderingMode,
- numArgs: 1,
- variableArgs: false
- };
- t.Ts = {
- id: _util.OPS.setTextRise,
- numArgs: 1,
- variableArgs: false
- };
- t.Td = {
- id: _util.OPS.moveText,
- numArgs: 2,
- variableArgs: false
- };
- t.TD = {
- id: _util.OPS.setLeadingMoveText,
- numArgs: 2,
- variableArgs: false
- };
- t.Tm = {
- id: _util.OPS.setTextMatrix,
- numArgs: 6,
- variableArgs: false
- };
- t["T*"] = {
- id: _util.OPS.nextLine,
- numArgs: 0,
- variableArgs: false
- };
- t.Tj = {
- id: _util.OPS.showText,
- numArgs: 1,
- variableArgs: false
- };
- t.TJ = {
- id: _util.OPS.showSpacedText,
- numArgs: 1,
- variableArgs: false
- };
- t["'"] = {
- id: _util.OPS.nextLineShowText,
- numArgs: 1,
- variableArgs: false
- };
- t['"'] = {
- id: _util.OPS.nextLineSetSpacingShowText,
- numArgs: 3,
- variableArgs: false
- };
- t.d0 = {
- id: _util.OPS.setCharWidth,
- numArgs: 2,
- variableArgs: false
- };
- t.d1 = {
- id: _util.OPS.setCharWidthAndBounds,
- numArgs: 6,
- variableArgs: false
- };
- t.CS = {
- id: _util.OPS.setStrokeColorSpace,
- numArgs: 1,
- variableArgs: false
- };
- t.cs = {
- id: _util.OPS.setFillColorSpace,
- numArgs: 1,
- variableArgs: false
- };
- t.SC = {
- id: _util.OPS.setStrokeColor,
- numArgs: 4,
- variableArgs: true
- };
- t.SCN = {
- id: _util.OPS.setStrokeColorN,
- numArgs: 33,
- variableArgs: true
- };
- t.sc = {
- id: _util.OPS.setFillColor,
- numArgs: 4,
- variableArgs: true
- };
- t.scn = {
- id: _util.OPS.setFillColorN,
- numArgs: 33,
- variableArgs: true
- };
- t.G = {
- id: _util.OPS.setStrokeGray,
- numArgs: 1,
- variableArgs: false
- };
- t.g = {
- id: _util.OPS.setFillGray,
- numArgs: 1,
- variableArgs: false
- };
- t.RG = {
- id: _util.OPS.setStrokeRGBColor,
- numArgs: 3,
- variableArgs: false
- };
- t.rg = {
- id: _util.OPS.setFillRGBColor,
- numArgs: 3,
- variableArgs: false
- };
- t.K = {
- id: _util.OPS.setStrokeCMYKColor,
- numArgs: 4,
- variableArgs: false
- };
- t.k = {
- id: _util.OPS.setFillCMYKColor,
- numArgs: 4,
- variableArgs: false
- };
- t.sh = {
- id: _util.OPS.shadingFill,
- numArgs: 1,
- variableArgs: false
- };
- t.BI = {
- id: _util.OPS.beginInlineImage,
- numArgs: 0,
- variableArgs: false
- };
- t.ID = {
- id: _util.OPS.beginImageData,
- numArgs: 0,
- variableArgs: false
- };
- t.EI = {
- id: _util.OPS.endInlineImage,
- numArgs: 1,
- variableArgs: false
- };
- t.Do = {
- id: _util.OPS.paintXObject,
- numArgs: 1,
- variableArgs: false
- };
- t.MP = {
- id: _util.OPS.markPoint,
- numArgs: 1,
- variableArgs: false
- };
- t.DP = {
- id: _util.OPS.markPointProps,
- numArgs: 2,
- variableArgs: false
- };
- t.BMC = {
- id: _util.OPS.beginMarkedContent,
- numArgs: 1,
- variableArgs: false
- };
- t.BDC = {
- id: _util.OPS.beginMarkedContentProps,
- numArgs: 2,
- variableArgs: false
- };
- t.EMC = {
- id: _util.OPS.endMarkedContent,
- numArgs: 0,
- variableArgs: false
- };
- t.BX = {
- id: _util.OPS.beginCompat,
- numArgs: 0,
- variableArgs: false
- };
- t.EX = {
- id: _util.OPS.endCompat,
- numArgs: 0,
- variableArgs: false
- };
- t.BM = null;
- t.BD = null;
- t.true = null;
- t.fa = null;
- t.fal = null;
- t.fals = null;
- t.false = null;
- t.nu = null;
- t.nul = null;
- t.null = null;
- });
- return (0, _util.shadow)(this, "opMap", getOPMap());
- }
- static get MAX_INVALID_PATH_OPS() {
- return (0, _util.shadow)(this, "MAX_INVALID_PATH_OPS", 10);
- }
- constructor(stream, xref, stateManager = new StateManager()) {
- this.parser = new _parser.Parser({
- lexer: new _parser.Lexer(stream, EvaluatorPreprocessor.opMap),
- xref
- });
- this.stateManager = stateManager;
- this.nonProcessedArgs = [];
- this._isPathOp = false;
- this._numInvalidPathOPS = 0;
- }
- get savedStatesDepth() {
- return this.stateManager.stateStack.length;
- }
- read(operation) {
- let args = operation.args;
- while (true) {
- const obj = this.parser.getObj();
- if (obj instanceof _primitives.Cmd) {
- const cmd = obj.cmd;
- const opSpec = EvaluatorPreprocessor.opMap[cmd];
- if (!opSpec) {
- (0, _util.warn)(`Unknown command "${cmd}".`);
- continue;
- }
- const fn = opSpec.id;
- const numArgs = opSpec.numArgs;
- let argsLength = args !== null ? args.length : 0;
- if (!this._isPathOp) {
- this._numInvalidPathOPS = 0;
- }
- this._isPathOp = fn >= _util.OPS.moveTo && fn <= _util.OPS.endPath;
- if (!opSpec.variableArgs) {
- if (argsLength !== numArgs) {
- const nonProcessedArgs = this.nonProcessedArgs;
- while (argsLength > numArgs) {
- nonProcessedArgs.push(args.shift());
- argsLength--;
- }
- while (argsLength < numArgs && nonProcessedArgs.length !== 0) {
- if (args === null) {
- args = [];
- }
- args.unshift(nonProcessedArgs.pop());
- argsLength++;
- }
- }
- if (argsLength < numArgs) {
- const partialMsg = `command ${cmd}: expected ${numArgs} args, ` + `but received ${argsLength} args.`;
- if (this._isPathOp && ++this._numInvalidPathOPS > EvaluatorPreprocessor.MAX_INVALID_PATH_OPS) {
- throw new _util.FormatError(`Invalid ${partialMsg}`);
- }
- (0, _util.warn)(`Skipping ${partialMsg}`);
- if (args !== null) {
- args.length = 0;
- }
- continue;
- }
- } else if (argsLength > numArgs) {
- (0, _util.info)(`Command ${cmd}: expected [0, ${numArgs}] args, ` + `but received ${argsLength} args.`);
- }
- this.preprocessCommand(fn, args);
- operation.fn = fn;
- operation.args = args;
- return true;
- }
- if (obj === _primitives.EOF) {
- return false;
- }
- if (obj !== null) {
- if (args === null) {
- args = [];
- }
- args.push(obj);
- if (args.length > 33) {
- throw new _util.FormatError("Too many arguments");
- }
- }
- }
- }
- preprocessCommand(fn, args) {
- switch (fn | 0) {
- case _util.OPS.save:
- this.stateManager.save();
- break;
- case _util.OPS.restore:
- this.stateManager.restore();
- break;
- case _util.OPS.transform:
- this.stateManager.transform(args);
- break;
- }
- }
- }
- exports.EvaluatorPreprocessor = EvaluatorPreprocessor;
|