evaluator.js 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PartialEvaluator = exports.EvaluatorPreprocessor = void 0;
  27. var _util = require("../shared/util.js");
  28. var _cmap = require("./cmap.js");
  29. var _primitives = require("./primitives.js");
  30. var _fonts = require("./fonts.js");
  31. var _fonts_utils = require("./fonts_utils.js");
  32. var _encodings = require("./encodings.js");
  33. var _standard_fonts = require("./standard_fonts.js");
  34. var _unicode = require("./unicode.js");
  35. var _pattern = require("./pattern.js");
  36. var _to_unicode_map = require("./to_unicode_map.js");
  37. var _function = require("./function.js");
  38. var _parser = require("./parser.js");
  39. var _image_utils = require("./image_utils.js");
  40. var _stream = require("./stream.js");
  41. var _bidi = require("./bidi.js");
  42. var _colorspace = require("./colorspace.js");
  43. var _decode_stream = require("./decode_stream.js");
  44. var _glyphlist = require("./glyphlist.js");
  45. var _core_utils = require("./core_utils.js");
  46. var _metrics = require("./metrics.js");
  47. var _xfa_fonts = require("./xfa_fonts.js");
  48. var _murmurhash = require("./murmurhash3.js");
  49. var _operator_list = require("./operator_list.js");
  50. var _image = require("./image.js");
  51. const DefaultPartialEvaluatorOptions = Object.freeze({
  52. maxImageSize: -1,
  53. disableFontFace: false,
  54. ignoreErrors: false,
  55. isEvalSupported: true,
  56. fontExtraProperties: false,
  57. useSystemFonts: true,
  58. cMapUrl: null,
  59. standardFontDataUrl: null
  60. });
  61. const PatternType = {
  62. TILING: 1,
  63. SHADING: 2
  64. };
  65. const deferred = Promise.resolve();
  66. function normalizeBlendMode(value, parsingArray = false) {
  67. if (Array.isArray(value)) {
  68. for (let i = 0, ii = value.length; i < ii; i++) {
  69. const maybeBM = normalizeBlendMode(value[i], true);
  70. if (maybeBM) {
  71. return maybeBM;
  72. }
  73. }
  74. (0, _util.warn)(`Unsupported blend mode Array: ${value}`);
  75. return "source-over";
  76. }
  77. if (!(0, _primitives.isName)(value)) {
  78. if (parsingArray) {
  79. return null;
  80. }
  81. return "source-over";
  82. }
  83. switch (value.name) {
  84. case "Normal":
  85. case "Compatible":
  86. return "source-over";
  87. case "Multiply":
  88. return "multiply";
  89. case "Screen":
  90. return "screen";
  91. case "Overlay":
  92. return "overlay";
  93. case "Darken":
  94. return "darken";
  95. case "Lighten":
  96. return "lighten";
  97. case "ColorDodge":
  98. return "color-dodge";
  99. case "ColorBurn":
  100. return "color-burn";
  101. case "HardLight":
  102. return "hard-light";
  103. case "SoftLight":
  104. return "soft-light";
  105. case "Difference":
  106. return "difference";
  107. case "Exclusion":
  108. return "exclusion";
  109. case "Hue":
  110. return "hue";
  111. case "Saturation":
  112. return "saturation";
  113. case "Color":
  114. return "color";
  115. case "Luminosity":
  116. return "luminosity";
  117. }
  118. if (parsingArray) {
  119. return null;
  120. }
  121. (0, _util.warn)(`Unsupported blend mode: ${value.name}`);
  122. return "source-over";
  123. }
  124. class TimeSlotManager {
  125. static get TIME_SLOT_DURATION_MS() {
  126. return (0, _util.shadow)(this, "TIME_SLOT_DURATION_MS", 20);
  127. }
  128. static get CHECK_TIME_EVERY() {
  129. return (0, _util.shadow)(this, "CHECK_TIME_EVERY", 100);
  130. }
  131. constructor() {
  132. this.reset();
  133. }
  134. check() {
  135. if (++this.checked < TimeSlotManager.CHECK_TIME_EVERY) {
  136. return false;
  137. }
  138. this.checked = 0;
  139. return this.endTime <= Date.now();
  140. }
  141. reset() {
  142. this.endTime = Date.now() + TimeSlotManager.TIME_SLOT_DURATION_MS;
  143. this.checked = 0;
  144. }
  145. }
  146. class PartialEvaluator {
  147. constructor({
  148. xref,
  149. handler,
  150. pageIndex,
  151. idFactory,
  152. fontCache,
  153. builtInCMapCache,
  154. standardFontDataCache,
  155. globalImageCache,
  156. options = null
  157. }) {
  158. this.xref = xref;
  159. this.handler = handler;
  160. this.pageIndex = pageIndex;
  161. this.idFactory = idFactory;
  162. this.fontCache = fontCache;
  163. this.builtInCMapCache = builtInCMapCache;
  164. this.standardFontDataCache = standardFontDataCache;
  165. this.globalImageCache = globalImageCache;
  166. this.options = options || DefaultPartialEvaluatorOptions;
  167. this.parsingType3Font = false;
  168. this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
  169. }
  170. get _pdfFunctionFactory() {
  171. const pdfFunctionFactory = new _function.PDFFunctionFactory({
  172. xref: this.xref,
  173. isEvalSupported: this.options.isEvalSupported
  174. });
  175. return (0, _util.shadow)(this, "_pdfFunctionFactory", pdfFunctionFactory);
  176. }
  177. clone(newOptions = null) {
  178. const newEvaluator = Object.create(this);
  179. newEvaluator.options = Object.assign(Object.create(null), this.options, newOptions);
  180. return newEvaluator;
  181. }
  182. hasBlendModes(resources, nonBlendModesSet) {
  183. if (!(resources instanceof _primitives.Dict)) {
  184. return false;
  185. }
  186. if (resources.objId && nonBlendModesSet.has(resources.objId)) {
  187. return false;
  188. }
  189. const processed = new _primitives.RefSet(nonBlendModesSet);
  190. if (resources.objId) {
  191. processed.put(resources.objId);
  192. }
  193. const nodes = [resources],
  194. xref = this.xref;
  195. while (nodes.length) {
  196. const node = nodes.shift();
  197. const graphicStates = node.get("ExtGState");
  198. if (graphicStates instanceof _primitives.Dict) {
  199. for (let graphicState of graphicStates.getRawValues()) {
  200. if (graphicState instanceof _primitives.Ref) {
  201. if (processed.has(graphicState)) {
  202. continue;
  203. }
  204. try {
  205. graphicState = xref.fetch(graphicState);
  206. } catch (ex) {
  207. processed.put(graphicState);
  208. (0, _util.info)(`hasBlendModes - ignoring ExtGState: "${ex}".`);
  209. continue;
  210. }
  211. }
  212. if (!(graphicState instanceof _primitives.Dict)) {
  213. continue;
  214. }
  215. if (graphicState.objId) {
  216. processed.put(graphicState.objId);
  217. }
  218. const bm = graphicState.get("BM");
  219. if (bm instanceof _primitives.Name) {
  220. if (bm.name !== "Normal") {
  221. return true;
  222. }
  223. continue;
  224. }
  225. if (bm !== undefined && Array.isArray(bm)) {
  226. for (const element of bm) {
  227. if (element instanceof _primitives.Name && element.name !== "Normal") {
  228. return true;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. const xObjects = node.get("XObject");
  235. if (!(xObjects instanceof _primitives.Dict)) {
  236. continue;
  237. }
  238. for (let xObject of xObjects.getRawValues()) {
  239. if (xObject instanceof _primitives.Ref) {
  240. if (processed.has(xObject)) {
  241. continue;
  242. }
  243. try {
  244. xObject = xref.fetch(xObject);
  245. } catch (ex) {
  246. processed.put(xObject);
  247. (0, _util.info)(`hasBlendModes - ignoring XObject: "${ex}".`);
  248. continue;
  249. }
  250. }
  251. if (!(0, _primitives.isStream)(xObject)) {
  252. continue;
  253. }
  254. if (xObject.dict.objId) {
  255. processed.put(xObject.dict.objId);
  256. }
  257. const xResources = xObject.dict.get("Resources");
  258. if (!(xResources instanceof _primitives.Dict)) {
  259. continue;
  260. }
  261. if (xResources.objId && processed.has(xResources.objId)) {
  262. continue;
  263. }
  264. nodes.push(xResources);
  265. if (xResources.objId) {
  266. processed.put(xResources.objId);
  267. }
  268. }
  269. }
  270. processed.forEach(ref => {
  271. nonBlendModesSet.put(ref);
  272. });
  273. return false;
  274. }
  275. async fetchBuiltInCMap(name) {
  276. const cachedData = this.builtInCMapCache.get(name);
  277. if (cachedData) {
  278. return cachedData;
  279. }
  280. let data;
  281. if (this.options.cMapUrl !== null) {
  282. const url = `${this.options.cMapUrl}${name}.bcmap`;
  283. const response = await fetch(url);
  284. if (!response.ok) {
  285. throw new Error(`fetchBuiltInCMap: failed to fetch file "${url}" with "${response.statusText}".`);
  286. }
  287. data = {
  288. cMapData: new Uint8Array(await response.arrayBuffer()),
  289. compressionType: _util.CMapCompressionType.BINARY
  290. };
  291. } else {
  292. data = await this.handler.sendWithPromise("FetchBuiltInCMap", {
  293. name
  294. });
  295. }
  296. if (data.compressionType !== _util.CMapCompressionType.NONE) {
  297. this.builtInCMapCache.set(name, data);
  298. }
  299. return data;
  300. }
  301. async fetchStandardFontData(name) {
  302. const cachedData = this.standardFontDataCache.get(name);
  303. if (cachedData) {
  304. return new _stream.Stream(cachedData);
  305. }
  306. if (this.options.useSystemFonts && name !== "Symbol" && name !== "ZapfDingbats") {
  307. return null;
  308. }
  309. const standardFontNameToFileName = (0, _standard_fonts.getFontNameToFileMap)(),
  310. filename = standardFontNameToFileName[name];
  311. let data;
  312. if (this.options.standardFontDataUrl !== null) {
  313. const url = `${this.options.standardFontDataUrl}${filename}`;
  314. const response = await fetch(url);
  315. if (!response.ok) {
  316. (0, _util.warn)(`fetchStandardFontData: failed to fetch file "${url}" with "${response.statusText}".`);
  317. } else {
  318. data = await response.arrayBuffer();
  319. }
  320. } else {
  321. try {
  322. data = await this.handler.sendWithPromise("FetchStandardFontData", {
  323. filename
  324. });
  325. } catch (e) {
  326. (0, _util.warn)(`fetchStandardFontData: failed to fetch file "${filename}" with "${e}".`);
  327. }
  328. }
  329. if (!data) {
  330. return null;
  331. }
  332. this.standardFontDataCache.set(name, data);
  333. return new _stream.Stream(data);
  334. }
  335. async buildFormXObject(resources, xobj, smask, operatorList, task, initialState, localColorSpaceCache) {
  336. const dict = xobj.dict;
  337. const matrix = dict.getArray("Matrix");
  338. let bbox = dict.getArray("BBox");
  339. if (Array.isArray(bbox) && bbox.length === 4) {
  340. bbox = _util.Util.normalizeRect(bbox);
  341. } else {
  342. bbox = null;
  343. }
  344. let optionalContent = null,
  345. groupOptions;
  346. if (dict.has("OC")) {
  347. optionalContent = await this.parseMarkedContentProps(dict.get("OC"), resources);
  348. operatorList.addOp(_util.OPS.beginMarkedContentProps, ["OC", optionalContent]);
  349. }
  350. const group = dict.get("Group");
  351. if (group) {
  352. groupOptions = {
  353. matrix,
  354. bbox,
  355. smask,
  356. isolated: false,
  357. knockout: false
  358. };
  359. const groupSubtype = group.get("S");
  360. let colorSpace = null;
  361. if ((0, _primitives.isName)(groupSubtype, "Transparency")) {
  362. groupOptions.isolated = group.get("I") || false;
  363. groupOptions.knockout = group.get("K") || false;
  364. if (group.has("CS")) {
  365. const cs = group.getRaw("CS");
  366. const cachedColorSpace = _colorspace.ColorSpace.getCached(cs, this.xref, localColorSpaceCache);
  367. if (cachedColorSpace) {
  368. colorSpace = cachedColorSpace;
  369. } else {
  370. colorSpace = await this.parseColorSpace({
  371. cs,
  372. resources,
  373. localColorSpaceCache
  374. });
  375. }
  376. }
  377. }
  378. if (smask && smask.backdrop) {
  379. colorSpace = colorSpace || _colorspace.ColorSpace.singletons.rgb;
  380. smask.backdrop = colorSpace.getRgb(smask.backdrop, 0);
  381. }
  382. operatorList.addOp(_util.OPS.beginGroup, [groupOptions]);
  383. }
  384. operatorList.addOp(_util.OPS.paintFormXObjectBegin, [matrix, bbox]);
  385. return this.getOperatorList({
  386. stream: xobj,
  387. task,
  388. resources: dict.get("Resources") || resources,
  389. operatorList,
  390. initialState
  391. }).then(function () {
  392. operatorList.addOp(_util.OPS.paintFormXObjectEnd, []);
  393. if (group) {
  394. operatorList.addOp(_util.OPS.endGroup, [groupOptions]);
  395. }
  396. if (optionalContent) {
  397. operatorList.addOp(_util.OPS.endMarkedContent, []);
  398. }
  399. });
  400. }
  401. _sendImgData(objId, imgData, cacheGlobally = false) {
  402. const transfers = imgData ? [imgData.data.buffer] : null;
  403. if (this.parsingType3Font || cacheGlobally) {
  404. return this.handler.send("commonobj", [objId, "Image", imgData], transfers);
  405. }
  406. return this.handler.send("obj", [objId, this.pageIndex, "Image", imgData], transfers);
  407. }
  408. async buildPaintImageXObject({
  409. resources,
  410. image,
  411. isInline = false,
  412. operatorList,
  413. cacheKey,
  414. localImageCache,
  415. localColorSpaceCache
  416. }) {
  417. const dict = image.dict;
  418. const imageRef = dict.objId;
  419. const w = dict.get("Width", "W");
  420. const h = dict.get("Height", "H");
  421. if (!(w && (0, _util.isNum)(w)) || !(h && (0, _util.isNum)(h))) {
  422. (0, _util.warn)("Image dimensions are missing, or not numbers.");
  423. return undefined;
  424. }
  425. const maxImageSize = this.options.maxImageSize;
  426. if (maxImageSize !== -1 && w * h > maxImageSize) {
  427. (0, _util.warn)("Image exceeded maximum allowed size and was removed.");
  428. return undefined;
  429. }
  430. const imageMask = dict.get("ImageMask", "IM") || false;
  431. let imgData, args;
  432. if (imageMask) {
  433. const width = dict.get("Width", "W");
  434. const height = dict.get("Height", "H");
  435. const bitStrideLength = width + 7 >> 3;
  436. const imgArray = image.getBytes(bitStrideLength * height, true);
  437. const decode = dict.getArray("Decode", "D");
  438. imgData = _image.PDFImage.createMask({
  439. imgArray,
  440. width,
  441. height,
  442. imageIsFromDecodeStream: image instanceof _decode_stream.DecodeStream,
  443. inverseDecode: !!decode && decode[0] > 0
  444. });
  445. imgData.cached = !!cacheKey;
  446. args = [imgData];
  447. operatorList.addOp(_util.OPS.paintImageMaskXObject, args);
  448. if (cacheKey) {
  449. localImageCache.set(cacheKey, imageRef, {
  450. fn: _util.OPS.paintImageMaskXObject,
  451. args
  452. });
  453. }
  454. return undefined;
  455. }
  456. const softMask = dict.get("SMask", "SM") || false;
  457. const mask = dict.get("Mask") || false;
  458. const SMALL_IMAGE_DIMENSIONS = 200;
  459. if (isInline && !softMask && !mask && w + h < SMALL_IMAGE_DIMENSIONS) {
  460. const imageObj = new _image.PDFImage({
  461. xref: this.xref,
  462. res: resources,
  463. image,
  464. isInline,
  465. pdfFunctionFactory: this._pdfFunctionFactory,
  466. localColorSpaceCache
  467. });
  468. imgData = imageObj.createImageData(true);
  469. operatorList.addOp(_util.OPS.paintInlineImageXObject, [imgData]);
  470. return undefined;
  471. }
  472. let objId = `img_${this.idFactory.createObjId()}`,
  473. cacheGlobally = false;
  474. if (this.parsingType3Font) {
  475. objId = `${this.idFactory.getDocId()}_type3_${objId}`;
  476. } else if (imageRef) {
  477. cacheGlobally = this.globalImageCache.shouldCache(imageRef, this.pageIndex);
  478. if (cacheGlobally) {
  479. objId = `${this.idFactory.getDocId()}_${objId}`;
  480. }
  481. }
  482. operatorList.addDependency(objId);
  483. args = [objId, w, h];
  484. _image.PDFImage.buildImage({
  485. xref: this.xref,
  486. res: resources,
  487. image,
  488. isInline,
  489. pdfFunctionFactory: this._pdfFunctionFactory,
  490. localColorSpaceCache
  491. }).then(imageObj => {
  492. imgData = imageObj.createImageData(false);
  493. if (cacheKey && imageRef && cacheGlobally) {
  494. this.globalImageCache.addByteSize(imageRef, imgData.data.length);
  495. }
  496. return this._sendImgData(objId, imgData, cacheGlobally);
  497. }).catch(reason => {
  498. (0, _util.warn)(`Unable to decode image "${objId}": "${reason}".`);
  499. return this._sendImgData(objId, null, cacheGlobally);
  500. });
  501. operatorList.addOp(_util.OPS.paintImageXObject, args);
  502. if (cacheKey) {
  503. localImageCache.set(cacheKey, imageRef, {
  504. fn: _util.OPS.paintImageXObject,
  505. args
  506. });
  507. if (imageRef) {
  508. (0, _util.assert)(!isInline, "Cannot cache an inline image globally.");
  509. this.globalImageCache.addPageIndex(imageRef, this.pageIndex);
  510. if (cacheGlobally) {
  511. this.globalImageCache.setData(imageRef, {
  512. objId,
  513. fn: _util.OPS.paintImageXObject,
  514. args,
  515. byteSize: 0
  516. });
  517. }
  518. }
  519. }
  520. return undefined;
  521. }
  522. handleSMask(smask, resources, operatorList, task, stateManager, localColorSpaceCache) {
  523. const smaskContent = smask.get("G");
  524. const smaskOptions = {
  525. subtype: smask.get("S").name,
  526. backdrop: smask.get("BC")
  527. };
  528. const transferObj = smask.get("TR");
  529. if ((0, _function.isPDFFunction)(transferObj)) {
  530. const transferFn = this._pdfFunctionFactory.create(transferObj);
  531. const transferMap = new Uint8Array(256);
  532. const tmp = new Float32Array(1);
  533. for (let i = 0; i < 256; i++) {
  534. tmp[0] = i / 255;
  535. transferFn(tmp, 0, tmp, 0);
  536. transferMap[i] = tmp[0] * 255 | 0;
  537. }
  538. smaskOptions.transferMap = transferMap;
  539. }
  540. return this.buildFormXObject(resources, smaskContent, smaskOptions, operatorList, task, stateManager.state.clone(), localColorSpaceCache);
  541. }
  542. handleTransferFunction(tr) {
  543. let transferArray;
  544. if (Array.isArray(tr)) {
  545. transferArray = tr;
  546. } else if ((0, _function.isPDFFunction)(tr)) {
  547. transferArray = [tr];
  548. } else {
  549. return null;
  550. }
  551. const transferMaps = [];
  552. let numFns = 0,
  553. numEffectfulFns = 0;
  554. for (const entry of transferArray) {
  555. const transferObj = this.xref.fetchIfRef(entry);
  556. numFns++;
  557. if ((0, _primitives.isName)(transferObj, "Identity")) {
  558. transferMaps.push(null);
  559. continue;
  560. } else if (!(0, _function.isPDFFunction)(transferObj)) {
  561. return null;
  562. }
  563. const transferFn = this._pdfFunctionFactory.create(transferObj);
  564. const transferMap = new Uint8Array(256),
  565. tmp = new Float32Array(1);
  566. for (let j = 0; j < 256; j++) {
  567. tmp[0] = j / 255;
  568. transferFn(tmp, 0, tmp, 0);
  569. transferMap[j] = tmp[0] * 255 | 0;
  570. }
  571. transferMaps.push(transferMap);
  572. numEffectfulFns++;
  573. }
  574. if (!(numFns === 1 || numFns === 4)) {
  575. return null;
  576. }
  577. if (numEffectfulFns === 0) {
  578. return null;
  579. }
  580. return transferMaps;
  581. }
  582. handleTilingType(fn, color, resources, pattern, patternDict, operatorList, task, cacheKey, localTilingPatternCache) {
  583. const tilingOpList = new _operator_list.OperatorList();
  584. const patternResources = _primitives.Dict.merge({
  585. xref: this.xref,
  586. dictArray: [patternDict.get("Resources"), resources]
  587. });
  588. return this.getOperatorList({
  589. stream: pattern,
  590. task,
  591. resources: patternResources,
  592. operatorList: tilingOpList
  593. }).then(function () {
  594. const operatorListIR = tilingOpList.getIR();
  595. const tilingPatternIR = (0, _pattern.getTilingPatternIR)(operatorListIR, patternDict, color);
  596. operatorList.addDependencies(tilingOpList.dependencies);
  597. operatorList.addOp(fn, tilingPatternIR);
  598. if (cacheKey) {
  599. localTilingPatternCache.set(cacheKey, patternDict.objId, {
  600. operatorListIR,
  601. dict: patternDict
  602. });
  603. }
  604. }).catch(reason => {
  605. if (reason instanceof _util.AbortException) {
  606. return;
  607. }
  608. if (this.options.ignoreErrors) {
  609. this.handler.send("UnsupportedFeature", {
  610. featureId: _util.UNSUPPORTED_FEATURES.errorTilingPattern
  611. });
  612. (0, _util.warn)(`handleTilingType - ignoring pattern: "${reason}".`);
  613. return;
  614. }
  615. throw reason;
  616. });
  617. }
  618. handleSetFont(resources, fontArgs, fontRef, operatorList, task, state, fallbackFontDict = null, cssFontInfo = null) {
  619. const fontName = fontArgs && fontArgs[0] instanceof _primitives.Name ? fontArgs[0].name : null;
  620. return this.loadFont(fontName, fontRef, resources, fallbackFontDict, cssFontInfo).then(translated => {
  621. if (!translated.font.isType3Font) {
  622. return translated;
  623. }
  624. return translated.loadType3Data(this, resources, task).then(function () {
  625. operatorList.addDependencies(translated.type3Dependencies);
  626. return translated;
  627. }).catch(reason => {
  628. this.handler.send("UnsupportedFeature", {
  629. featureId: _util.UNSUPPORTED_FEATURES.errorFontLoadType3
  630. });
  631. return new TranslatedFont({
  632. loadedName: "g_font_error",
  633. font: new _fonts.ErrorFont(`Type3 font load error: ${reason}`),
  634. dict: translated.font,
  635. evaluatorOptions: this.options
  636. });
  637. });
  638. }).then(translated => {
  639. state.font = translated.font;
  640. translated.send(this.handler);
  641. return translated.loadedName;
  642. });
  643. }
  644. handleText(chars, state) {
  645. const font = state.font;
  646. const glyphs = font.charsToGlyphs(chars);
  647. if (font.data) {
  648. const isAddToPathSet = !!(state.textRenderingMode & _util.TextRenderingMode.ADD_TO_PATH_FLAG);
  649. if (isAddToPathSet || state.fillColorSpace.name === "Pattern" || font.disableFontFace || this.options.disableFontFace) {
  650. PartialEvaluator.buildFontPaths(font, glyphs, this.handler, this.options);
  651. }
  652. }
  653. return glyphs;
  654. }
  655. ensureStateFont(state) {
  656. if (state.font) {
  657. return;
  658. }
  659. const reason = new _util.FormatError("Missing setFont (Tf) operator before text rendering operator.");
  660. if (this.options.ignoreErrors) {
  661. this.handler.send("UnsupportedFeature", {
  662. featureId: _util.UNSUPPORTED_FEATURES.errorFontState
  663. });
  664. (0, _util.warn)(`ensureStateFont: "${reason}".`);
  665. return;
  666. }
  667. throw reason;
  668. }
  669. async setGState({
  670. resources,
  671. gState,
  672. operatorList,
  673. cacheKey,
  674. task,
  675. stateManager,
  676. localGStateCache,
  677. localColorSpaceCache
  678. }) {
  679. const gStateRef = gState.objId;
  680. let isSimpleGState = true;
  681. const gStateObj = [];
  682. const gStateKeys = gState.getKeys();
  683. let promise = Promise.resolve();
  684. for (let i = 0, ii = gStateKeys.length; i < ii; i++) {
  685. const key = gStateKeys[i];
  686. const value = gState.get(key);
  687. switch (key) {
  688. case "Type":
  689. break;
  690. case "LW":
  691. case "LC":
  692. case "LJ":
  693. case "ML":
  694. case "D":
  695. case "RI":
  696. case "FL":
  697. case "CA":
  698. case "ca":
  699. gStateObj.push([key, value]);
  700. break;
  701. case "Font":
  702. isSimpleGState = false;
  703. promise = promise.then(() => {
  704. return this.handleSetFont(resources, null, value[0], operatorList, task, stateManager.state).then(function (loadedName) {
  705. operatorList.addDependency(loadedName);
  706. gStateObj.push([key, [loadedName, value[1]]]);
  707. });
  708. });
  709. break;
  710. case "BM":
  711. gStateObj.push([key, normalizeBlendMode(value)]);
  712. break;
  713. case "SMask":
  714. if ((0, _primitives.isName)(value, "None")) {
  715. gStateObj.push([key, false]);
  716. break;
  717. }
  718. if ((0, _primitives.isDict)(value)) {
  719. isSimpleGState = false;
  720. promise = promise.then(() => {
  721. return this.handleSMask(value, resources, operatorList, task, stateManager, localColorSpaceCache);
  722. });
  723. gStateObj.push([key, true]);
  724. } else {
  725. (0, _util.warn)("Unsupported SMask type");
  726. }
  727. break;
  728. case "TR":
  729. const transferMaps = this.handleTransferFunction(value);
  730. gStateObj.push([key, transferMaps]);
  731. break;
  732. case "OP":
  733. case "op":
  734. case "OPM":
  735. case "BG":
  736. case "BG2":
  737. case "UCR":
  738. case "UCR2":
  739. case "TR2":
  740. case "HT":
  741. case "SM":
  742. case "SA":
  743. case "AIS":
  744. case "TK":
  745. (0, _util.info)("graphic state operator " + key);
  746. break;
  747. default:
  748. (0, _util.info)("Unknown graphic state operator " + key);
  749. break;
  750. }
  751. }
  752. return promise.then(function () {
  753. if (gStateObj.length > 0) {
  754. operatorList.addOp(_util.OPS.setGState, [gStateObj]);
  755. }
  756. if (isSimpleGState) {
  757. localGStateCache.set(cacheKey, gStateRef, gStateObj);
  758. }
  759. });
  760. }
  761. loadFont(fontName, font, resources, fallbackFontDict = null, cssFontInfo = null) {
  762. const errorFont = async () => {
  763. return new TranslatedFont({
  764. loadedName: "g_font_error",
  765. font: new _fonts.ErrorFont(`Font "${fontName}" is not available.`),
  766. dict: font,
  767. evaluatorOptions: this.options
  768. });
  769. };
  770. const xref = this.xref;
  771. let fontRef;
  772. if (font) {
  773. if (!(0, _primitives.isRef)(font)) {
  774. throw new _util.FormatError('The "font" object should be a reference.');
  775. }
  776. fontRef = font;
  777. } else {
  778. const fontRes = resources.get("Font");
  779. if (fontRes) {
  780. fontRef = fontRes.getRaw(fontName);
  781. }
  782. }
  783. if (!fontRef) {
  784. const partialMsg = `Font "${fontName || font && font.toString()}" is not available`;
  785. if (!this.options.ignoreErrors && !this.parsingType3Font) {
  786. (0, _util.warn)(`${partialMsg}.`);
  787. return errorFont();
  788. }
  789. this.handler.send("UnsupportedFeature", {
  790. featureId: _util.UNSUPPORTED_FEATURES.errorFontMissing
  791. });
  792. (0, _util.warn)(`${partialMsg} -- attempting to fallback to a default font.`);
  793. if (fallbackFontDict) {
  794. fontRef = fallbackFontDict;
  795. } else {
  796. fontRef = PartialEvaluator.fallbackFontDict;
  797. }
  798. }
  799. if (this.fontCache.has(fontRef)) {
  800. return this.fontCache.get(fontRef);
  801. }
  802. font = xref.fetchIfRef(fontRef);
  803. if (!(0, _primitives.isDict)(font)) {
  804. return errorFont();
  805. }
  806. if (font.cacheKey && this.fontCache.has(font.cacheKey)) {
  807. return this.fontCache.get(font.cacheKey);
  808. }
  809. const fontCapability = (0, _util.createPromiseCapability)();
  810. let preEvaluatedFont;
  811. try {
  812. preEvaluatedFont = this.preEvaluateFont(font);
  813. preEvaluatedFont.cssFontInfo = cssFontInfo;
  814. } catch (reason) {
  815. (0, _util.warn)(`loadFont - preEvaluateFont failed: "${reason}".`);
  816. return errorFont();
  817. }
  818. const {
  819. descriptor,
  820. hash
  821. } = preEvaluatedFont;
  822. const fontRefIsRef = (0, _primitives.isRef)(fontRef);
  823. let fontID;
  824. if (fontRefIsRef) {
  825. fontID = `f${fontRef.toString()}`;
  826. }
  827. if (hash && (0, _primitives.isDict)(descriptor)) {
  828. if (!descriptor.fontAliases) {
  829. descriptor.fontAliases = Object.create(null);
  830. }
  831. const fontAliases = descriptor.fontAliases;
  832. if (fontAliases[hash]) {
  833. const aliasFontRef = fontAliases[hash].aliasRef;
  834. if (fontRefIsRef && aliasFontRef && this.fontCache.has(aliasFontRef)) {
  835. this.fontCache.putAlias(fontRef, aliasFontRef);
  836. return this.fontCache.get(fontRef);
  837. }
  838. } else {
  839. fontAliases[hash] = {
  840. fontID: this.idFactory.createFontId()
  841. };
  842. }
  843. if (fontRefIsRef) {
  844. fontAliases[hash].aliasRef = fontRef;
  845. }
  846. fontID = fontAliases[hash].fontID;
  847. }
  848. if (fontRefIsRef) {
  849. this.fontCache.put(fontRef, fontCapability.promise);
  850. } else {
  851. if (!fontID) {
  852. fontID = this.idFactory.createFontId();
  853. }
  854. font.cacheKey = `cacheKey_${fontID}`;
  855. this.fontCache.put(font.cacheKey, fontCapability.promise);
  856. }
  857. (0, _util.assert)(fontID && fontID.startsWith("f"), 'The "fontID" must be (correctly) defined.');
  858. font.loadedName = `${this.idFactory.getDocId()}_${fontID}`;
  859. this.translateFont(preEvaluatedFont).then(translatedFont => {
  860. if (translatedFont.fontType !== undefined) {
  861. const xrefFontStats = xref.stats.fontTypes;
  862. xrefFontStats[translatedFont.fontType] = true;
  863. }
  864. fontCapability.resolve(new TranslatedFont({
  865. loadedName: font.loadedName,
  866. font: translatedFont,
  867. dict: font,
  868. evaluatorOptions: this.options
  869. }));
  870. }).catch(reason => {
  871. this.handler.send("UnsupportedFeature", {
  872. featureId: _util.UNSUPPORTED_FEATURES.errorFontTranslate
  873. });
  874. (0, _util.warn)(`loadFont - translateFont failed: "${reason}".`);
  875. try {
  876. const fontFile3 = descriptor && descriptor.get("FontFile3");
  877. const subtype = fontFile3 && fontFile3.get("Subtype");
  878. const fontType = (0, _fonts_utils.getFontType)(preEvaluatedFont.type, subtype && subtype.name);
  879. const xrefFontStats = xref.stats.fontTypes;
  880. xrefFontStats[fontType] = true;
  881. } catch (ex) {}
  882. fontCapability.resolve(new TranslatedFont({
  883. loadedName: font.loadedName,
  884. font: new _fonts.ErrorFont(reason instanceof Error ? reason.message : reason),
  885. dict: font,
  886. evaluatorOptions: this.options
  887. }));
  888. });
  889. return fontCapability.promise;
  890. }
  891. buildPath(operatorList, fn, args, parsingText = false) {
  892. const lastIndex = operatorList.length - 1;
  893. if (!args) {
  894. args = [];
  895. }
  896. if (lastIndex < 0 || operatorList.fnArray[lastIndex] !== _util.OPS.constructPath) {
  897. if (parsingText) {
  898. (0, _util.warn)(`Encountered path operator "${fn}" inside of a text object.`);
  899. operatorList.addOp(_util.OPS.save, null);
  900. }
  901. operatorList.addOp(_util.OPS.constructPath, [[fn], args]);
  902. if (parsingText) {
  903. operatorList.addOp(_util.OPS.restore, null);
  904. }
  905. } else {
  906. const opArgs = operatorList.argsArray[lastIndex];
  907. opArgs[0].push(fn);
  908. Array.prototype.push.apply(opArgs[1], args);
  909. }
  910. }
  911. parseColorSpace({
  912. cs,
  913. resources,
  914. localColorSpaceCache
  915. }) {
  916. return _colorspace.ColorSpace.parseAsync({
  917. cs,
  918. xref: this.xref,
  919. resources,
  920. pdfFunctionFactory: this._pdfFunctionFactory,
  921. localColorSpaceCache
  922. }).catch(reason => {
  923. if (reason instanceof _util.AbortException) {
  924. return null;
  925. }
  926. if (this.options.ignoreErrors) {
  927. this.handler.send("UnsupportedFeature", {
  928. featureId: _util.UNSUPPORTED_FEATURES.errorColorSpace
  929. });
  930. (0, _util.warn)(`parseColorSpace - ignoring ColorSpace: "${reason}".`);
  931. return null;
  932. }
  933. throw reason;
  934. });
  935. }
  936. parseShading({
  937. keyObj,
  938. shading,
  939. resources,
  940. localColorSpaceCache,
  941. localShadingPatternCache,
  942. matrix = null
  943. }) {
  944. let id = localShadingPatternCache.get(keyObj);
  945. if (!id) {
  946. var shadingFill = _pattern.Pattern.parseShading(shading, matrix, this.xref, resources, this.handler, this._pdfFunctionFactory, localColorSpaceCache);
  947. const patternIR = shadingFill.getIR();
  948. id = `pattern_${this.idFactory.createObjId()}`;
  949. localShadingPatternCache.set(keyObj, id);
  950. this.handler.send("obj", [id, this.pageIndex, "Pattern", patternIR]);
  951. }
  952. return id;
  953. }
  954. handleColorN(operatorList, fn, args, cs, patterns, resources, task, localColorSpaceCache, localTilingPatternCache, localShadingPatternCache) {
  955. const patternName = args.pop();
  956. if (patternName instanceof _primitives.Name) {
  957. const name = patternName.name;
  958. const localTilingPattern = localTilingPatternCache.getByName(name);
  959. if (localTilingPattern) {
  960. try {
  961. const color = cs.base ? cs.base.getRgb(args, 0) : null;
  962. const tilingPatternIR = (0, _pattern.getTilingPatternIR)(localTilingPattern.operatorListIR, localTilingPattern.dict, color);
  963. operatorList.addOp(fn, tilingPatternIR);
  964. return undefined;
  965. } catch (ex) {}
  966. }
  967. const pattern = patterns.get(name);
  968. if (pattern) {
  969. const dict = (0, _primitives.isStream)(pattern) ? pattern.dict : pattern;
  970. const typeNum = dict.get("PatternType");
  971. if (typeNum === PatternType.TILING) {
  972. const color = cs.base ? cs.base.getRgb(args, 0) : null;
  973. return this.handleTilingType(fn, color, resources, pattern, dict, operatorList, task, name, localTilingPatternCache);
  974. } else if (typeNum === PatternType.SHADING) {
  975. const shading = dict.get("Shading");
  976. const matrix = dict.getArray("Matrix");
  977. const objId = this.parseShading({
  978. keyObj: pattern,
  979. shading,
  980. matrix,
  981. resources,
  982. localColorSpaceCache,
  983. localShadingPatternCache
  984. });
  985. operatorList.addOp(fn, ["Shading", objId]);
  986. return undefined;
  987. }
  988. throw new _util.FormatError(`Unknown PatternType: ${typeNum}`);
  989. }
  990. }
  991. throw new _util.FormatError(`Unknown PatternName: ${patternName}`);
  992. }
  993. _parseVisibilityExpression(array, nestingCounter, currentResult) {
  994. const MAX_NESTING = 10;
  995. if (++nestingCounter > MAX_NESTING) {
  996. (0, _util.warn)("Visibility expression is too deeply nested");
  997. return;
  998. }
  999. const length = array.length;
  1000. const operator = this.xref.fetchIfRef(array[0]);
  1001. if (length < 2 || !(0, _primitives.isName)(operator)) {
  1002. (0, _util.warn)("Invalid visibility expression");
  1003. return;
  1004. }
  1005. switch (operator.name) {
  1006. case "And":
  1007. case "Or":
  1008. case "Not":
  1009. currentResult.push(operator.name);
  1010. break;
  1011. default:
  1012. (0, _util.warn)(`Invalid operator ${operator.name} in visibility expression`);
  1013. return;
  1014. }
  1015. for (let i = 1; i < length; i++) {
  1016. const raw = array[i];
  1017. const object = this.xref.fetchIfRef(raw);
  1018. if (Array.isArray(object)) {
  1019. const nestedResult = [];
  1020. currentResult.push(nestedResult);
  1021. this._parseVisibilityExpression(object, nestingCounter, nestedResult);
  1022. } else if ((0, _primitives.isRef)(raw)) {
  1023. currentResult.push(raw.toString());
  1024. }
  1025. }
  1026. }
  1027. async parseMarkedContentProps(contentProperties, resources) {
  1028. let optionalContent;
  1029. if ((0, _primitives.isName)(contentProperties)) {
  1030. const properties = resources.get("Properties");
  1031. optionalContent = properties.get(contentProperties.name);
  1032. } else if ((0, _primitives.isDict)(contentProperties)) {
  1033. optionalContent = contentProperties;
  1034. } else {
  1035. throw new _util.FormatError("Optional content properties malformed.");
  1036. }
  1037. const optionalContentType = optionalContent.get("Type").name;
  1038. if (optionalContentType === "OCG") {
  1039. return {
  1040. type: optionalContentType,
  1041. id: optionalContent.objId
  1042. };
  1043. } else if (optionalContentType === "OCMD") {
  1044. const expression = optionalContent.get("VE");
  1045. if (Array.isArray(expression)) {
  1046. const result = [];
  1047. this._parseVisibilityExpression(expression, 0, result);
  1048. if (result.length > 0) {
  1049. return {
  1050. type: "OCMD",
  1051. expression: result
  1052. };
  1053. }
  1054. }
  1055. const optionalContentGroups = optionalContent.get("OCGs");
  1056. if (Array.isArray(optionalContentGroups) || (0, _primitives.isDict)(optionalContentGroups)) {
  1057. const groupIds = [];
  1058. if (Array.isArray(optionalContentGroups)) {
  1059. for (const ocg of optionalContentGroups) {
  1060. groupIds.push(ocg.toString());
  1061. }
  1062. } else {
  1063. groupIds.push(optionalContentGroups.objId);
  1064. }
  1065. return {
  1066. type: optionalContentType,
  1067. ids: groupIds,
  1068. policy: (0, _primitives.isName)(optionalContent.get("P")) ? optionalContent.get("P").name : null,
  1069. expression: null
  1070. };
  1071. } else if ((0, _primitives.isRef)(optionalContentGroups)) {
  1072. return {
  1073. type: optionalContentType,
  1074. id: optionalContentGroups.toString()
  1075. };
  1076. }
  1077. }
  1078. return null;
  1079. }
  1080. getOperatorList({
  1081. stream,
  1082. task,
  1083. resources,
  1084. operatorList,
  1085. initialState = null,
  1086. fallbackFontDict = null
  1087. }) {
  1088. resources = resources || _primitives.Dict.empty;
  1089. initialState = initialState || new EvalState();
  1090. if (!operatorList) {
  1091. throw new Error('getOperatorList: missing "operatorList" parameter');
  1092. }
  1093. const self = this;
  1094. const xref = this.xref;
  1095. let parsingText = false;
  1096. const localImageCache = new _image_utils.LocalImageCache();
  1097. const localColorSpaceCache = new _image_utils.LocalColorSpaceCache();
  1098. const localGStateCache = new _image_utils.LocalGStateCache();
  1099. const localTilingPatternCache = new _image_utils.LocalTilingPatternCache();
  1100. const localShadingPatternCache = new Map();
  1101. const xobjs = resources.get("XObject") || _primitives.Dict.empty;
  1102. const patterns = resources.get("Pattern") || _primitives.Dict.empty;
  1103. const stateManager = new StateManager(initialState);
  1104. const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
  1105. const timeSlotManager = new TimeSlotManager();
  1106. function closePendingRestoreOPS(argument) {
  1107. for (let i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
  1108. operatorList.addOp(_util.OPS.restore, []);
  1109. }
  1110. }
  1111. return new Promise(function promiseBody(resolve, reject) {
  1112. const next = function (promise) {
  1113. Promise.all([promise, operatorList.ready]).then(function () {
  1114. try {
  1115. promiseBody(resolve, reject);
  1116. } catch (ex) {
  1117. reject(ex);
  1118. }
  1119. }, reject);
  1120. };
  1121. task.ensureNotTerminated();
  1122. timeSlotManager.reset();
  1123. const operation = {};
  1124. let stop, i, ii, cs, name, isValidName;
  1125. while (!(stop = timeSlotManager.check())) {
  1126. operation.args = null;
  1127. if (!preprocessor.read(operation)) {
  1128. break;
  1129. }
  1130. let args = operation.args;
  1131. let fn = operation.fn;
  1132. switch (fn | 0) {
  1133. case _util.OPS.paintXObject:
  1134. isValidName = args[0] instanceof _primitives.Name;
  1135. name = args[0].name;
  1136. if (isValidName) {
  1137. const localImage = localImageCache.getByName(name);
  1138. if (localImage) {
  1139. operatorList.addOp(localImage.fn, localImage.args);
  1140. args = null;
  1141. continue;
  1142. }
  1143. }
  1144. next(new Promise(function (resolveXObject, rejectXObject) {
  1145. if (!isValidName) {
  1146. throw new _util.FormatError("XObject must be referred to by name.");
  1147. }
  1148. let xobj = xobjs.getRaw(name);
  1149. if (xobj instanceof _primitives.Ref) {
  1150. const localImage = localImageCache.getByRef(xobj);
  1151. if (localImage) {
  1152. operatorList.addOp(localImage.fn, localImage.args);
  1153. resolveXObject();
  1154. return;
  1155. }
  1156. const globalImage = self.globalImageCache.getData(xobj, self.pageIndex);
  1157. if (globalImage) {
  1158. operatorList.addDependency(globalImage.objId);
  1159. operatorList.addOp(globalImage.fn, globalImage.args);
  1160. resolveXObject();
  1161. return;
  1162. }
  1163. xobj = xref.fetch(xobj);
  1164. }
  1165. if (!(0, _primitives.isStream)(xobj)) {
  1166. throw new _util.FormatError("XObject should be a stream");
  1167. }
  1168. const type = xobj.dict.get("Subtype");
  1169. if (!(0, _primitives.isName)(type)) {
  1170. throw new _util.FormatError("XObject should have a Name subtype");
  1171. }
  1172. if (type.name === "Form") {
  1173. stateManager.save();
  1174. self.buildFormXObject(resources, xobj, null, operatorList, task, stateManager.state.clone(), localColorSpaceCache).then(function () {
  1175. stateManager.restore();
  1176. resolveXObject();
  1177. }, rejectXObject);
  1178. return;
  1179. } else if (type.name === "Image") {
  1180. self.buildPaintImageXObject({
  1181. resources,
  1182. image: xobj,
  1183. operatorList,
  1184. cacheKey: name,
  1185. localImageCache,
  1186. localColorSpaceCache
  1187. }).then(resolveXObject, rejectXObject);
  1188. return;
  1189. } else if (type.name === "PS") {
  1190. (0, _util.info)("Ignored XObject subtype PS");
  1191. } else {
  1192. throw new _util.FormatError(`Unhandled XObject subtype ${type.name}`);
  1193. }
  1194. resolveXObject();
  1195. }).catch(function (reason) {
  1196. if (reason instanceof _util.AbortException) {
  1197. return;
  1198. }
  1199. if (self.options.ignoreErrors) {
  1200. self.handler.send("UnsupportedFeature", {
  1201. featureId: _util.UNSUPPORTED_FEATURES.errorXObject
  1202. });
  1203. (0, _util.warn)(`getOperatorList - ignoring XObject: "${reason}".`);
  1204. return;
  1205. }
  1206. throw reason;
  1207. }));
  1208. return;
  1209. case _util.OPS.setFont:
  1210. var fontSize = args[1];
  1211. next(self.handleSetFont(resources, args, null, operatorList, task, stateManager.state, fallbackFontDict).then(function (loadedName) {
  1212. operatorList.addDependency(loadedName);
  1213. operatorList.addOp(_util.OPS.setFont, [loadedName, fontSize]);
  1214. }));
  1215. return;
  1216. case _util.OPS.beginText:
  1217. parsingText = true;
  1218. break;
  1219. case _util.OPS.endText:
  1220. parsingText = false;
  1221. break;
  1222. case _util.OPS.endInlineImage:
  1223. var cacheKey = args[0].cacheKey;
  1224. if (cacheKey) {
  1225. const localImage = localImageCache.getByName(cacheKey);
  1226. if (localImage) {
  1227. operatorList.addOp(localImage.fn, localImage.args);
  1228. args = null;
  1229. continue;
  1230. }
  1231. }
  1232. next(self.buildPaintImageXObject({
  1233. resources,
  1234. image: args[0],
  1235. isInline: true,
  1236. operatorList,
  1237. cacheKey,
  1238. localImageCache,
  1239. localColorSpaceCache
  1240. }));
  1241. return;
  1242. case _util.OPS.showText:
  1243. if (!stateManager.state.font) {
  1244. self.ensureStateFont(stateManager.state);
  1245. continue;
  1246. }
  1247. args[0] = self.handleText(args[0], stateManager.state);
  1248. break;
  1249. case _util.OPS.showSpacedText:
  1250. if (!stateManager.state.font) {
  1251. self.ensureStateFont(stateManager.state);
  1252. continue;
  1253. }
  1254. var arr = args[0];
  1255. var combinedGlyphs = [];
  1256. var arrLength = arr.length;
  1257. var state = stateManager.state;
  1258. for (i = 0; i < arrLength; ++i) {
  1259. const arrItem = arr[i];
  1260. if ((0, _util.isString)(arrItem)) {
  1261. Array.prototype.push.apply(combinedGlyphs, self.handleText(arrItem, state));
  1262. } else if ((0, _util.isNum)(arrItem)) {
  1263. combinedGlyphs.push(arrItem);
  1264. }
  1265. }
  1266. args[0] = combinedGlyphs;
  1267. fn = _util.OPS.showText;
  1268. break;
  1269. case _util.OPS.nextLineShowText:
  1270. if (!stateManager.state.font) {
  1271. self.ensureStateFont(stateManager.state);
  1272. continue;
  1273. }
  1274. operatorList.addOp(_util.OPS.nextLine);
  1275. args[0] = self.handleText(args[0], stateManager.state);
  1276. fn = _util.OPS.showText;
  1277. break;
  1278. case _util.OPS.nextLineSetSpacingShowText:
  1279. if (!stateManager.state.font) {
  1280. self.ensureStateFont(stateManager.state);
  1281. continue;
  1282. }
  1283. operatorList.addOp(_util.OPS.nextLine);
  1284. operatorList.addOp(_util.OPS.setWordSpacing, [args.shift()]);
  1285. operatorList.addOp(_util.OPS.setCharSpacing, [args.shift()]);
  1286. args[0] = self.handleText(args[0], stateManager.state);
  1287. fn = _util.OPS.showText;
  1288. break;
  1289. case _util.OPS.setTextRenderingMode:
  1290. stateManager.state.textRenderingMode = args[0];
  1291. break;
  1292. case _util.OPS.setFillColorSpace:
  1293. {
  1294. const cachedColorSpace = _colorspace.ColorSpace.getCached(args[0], xref, localColorSpaceCache);
  1295. if (cachedColorSpace) {
  1296. stateManager.state.fillColorSpace = cachedColorSpace;
  1297. continue;
  1298. }
  1299. next(self.parseColorSpace({
  1300. cs: args[0],
  1301. resources,
  1302. localColorSpaceCache
  1303. }).then(function (colorSpace) {
  1304. if (colorSpace) {
  1305. stateManager.state.fillColorSpace = colorSpace;
  1306. }
  1307. }));
  1308. return;
  1309. }
  1310. case _util.OPS.setStrokeColorSpace:
  1311. {
  1312. const cachedColorSpace = _colorspace.ColorSpace.getCached(args[0], xref, localColorSpaceCache);
  1313. if (cachedColorSpace) {
  1314. stateManager.state.strokeColorSpace = cachedColorSpace;
  1315. continue;
  1316. }
  1317. next(self.parseColorSpace({
  1318. cs: args[0],
  1319. resources,
  1320. localColorSpaceCache
  1321. }).then(function (colorSpace) {
  1322. if (colorSpace) {
  1323. stateManager.state.strokeColorSpace = colorSpace;
  1324. }
  1325. }));
  1326. return;
  1327. }
  1328. case _util.OPS.setFillColor:
  1329. cs = stateManager.state.fillColorSpace;
  1330. args = cs.getRgb(args, 0);
  1331. fn = _util.OPS.setFillRGBColor;
  1332. break;
  1333. case _util.OPS.setStrokeColor:
  1334. cs = stateManager.state.strokeColorSpace;
  1335. args = cs.getRgb(args, 0);
  1336. fn = _util.OPS.setStrokeRGBColor;
  1337. break;
  1338. case _util.OPS.setFillGray:
  1339. stateManager.state.fillColorSpace = _colorspace.ColorSpace.singletons.gray;
  1340. args = _colorspace.ColorSpace.singletons.gray.getRgb(args, 0);
  1341. fn = _util.OPS.setFillRGBColor;
  1342. break;
  1343. case _util.OPS.setStrokeGray:
  1344. stateManager.state.strokeColorSpace = _colorspace.ColorSpace.singletons.gray;
  1345. args = _colorspace.ColorSpace.singletons.gray.getRgb(args, 0);
  1346. fn = _util.OPS.setStrokeRGBColor;
  1347. break;
  1348. case _util.OPS.setFillCMYKColor:
  1349. stateManager.state.fillColorSpace = _colorspace.ColorSpace.singletons.cmyk;
  1350. args = _colorspace.ColorSpace.singletons.cmyk.getRgb(args, 0);
  1351. fn = _util.OPS.setFillRGBColor;
  1352. break;
  1353. case _util.OPS.setStrokeCMYKColor:
  1354. stateManager.state.strokeColorSpace = _colorspace.ColorSpace.singletons.cmyk;
  1355. args = _colorspace.ColorSpace.singletons.cmyk.getRgb(args, 0);
  1356. fn = _util.OPS.setStrokeRGBColor;
  1357. break;
  1358. case _util.OPS.setFillRGBColor:
  1359. stateManager.state.fillColorSpace = _colorspace.ColorSpace.singletons.rgb;
  1360. args = _colorspace.ColorSpace.singletons.rgb.getRgb(args, 0);
  1361. break;
  1362. case _util.OPS.setStrokeRGBColor:
  1363. stateManager.state.strokeColorSpace = _colorspace.ColorSpace.singletons.rgb;
  1364. args = _colorspace.ColorSpace.singletons.rgb.getRgb(args, 0);
  1365. break;
  1366. case _util.OPS.setFillColorN:
  1367. cs = stateManager.state.fillColorSpace;
  1368. if (cs.name === "Pattern") {
  1369. next(self.handleColorN(operatorList, _util.OPS.setFillColorN, args, cs, patterns, resources, task, localColorSpaceCache, localTilingPatternCache, localShadingPatternCache));
  1370. return;
  1371. }
  1372. args = cs.getRgb(args, 0);
  1373. fn = _util.OPS.setFillRGBColor;
  1374. break;
  1375. case _util.OPS.setStrokeColorN:
  1376. cs = stateManager.state.strokeColorSpace;
  1377. if (cs.name === "Pattern") {
  1378. next(self.handleColorN(operatorList, _util.OPS.setStrokeColorN, args, cs, patterns, resources, task, localColorSpaceCache, localTilingPatternCache, localShadingPatternCache));
  1379. return;
  1380. }
  1381. args = cs.getRgb(args, 0);
  1382. fn = _util.OPS.setStrokeRGBColor;
  1383. break;
  1384. case _util.OPS.shadingFill:
  1385. var shadingRes = resources.get("Shading");
  1386. if (!shadingRes) {
  1387. throw new _util.FormatError("No shading resource found");
  1388. }
  1389. var shading = shadingRes.get(args[0].name);
  1390. if (!shading) {
  1391. throw new _util.FormatError("No shading object found");
  1392. }
  1393. const patternId = self.parseShading({
  1394. keyObj: shading,
  1395. shading,
  1396. resources,
  1397. localColorSpaceCache,
  1398. localShadingPatternCache
  1399. });
  1400. args = [patternId];
  1401. fn = _util.OPS.shadingFill;
  1402. break;
  1403. case _util.OPS.setGState:
  1404. isValidName = args[0] instanceof _primitives.Name;
  1405. name = args[0].name;
  1406. if (isValidName) {
  1407. const localGStateObj = localGStateCache.getByName(name);
  1408. if (localGStateObj) {
  1409. if (localGStateObj.length > 0) {
  1410. operatorList.addOp(_util.OPS.setGState, [localGStateObj]);
  1411. }
  1412. args = null;
  1413. continue;
  1414. }
  1415. }
  1416. next(new Promise(function (resolveGState, rejectGState) {
  1417. if (!isValidName) {
  1418. throw new _util.FormatError("GState must be referred to by name.");
  1419. }
  1420. const extGState = resources.get("ExtGState");
  1421. if (!(extGState instanceof _primitives.Dict)) {
  1422. throw new _util.FormatError("ExtGState should be a dictionary.");
  1423. }
  1424. const gState = extGState.get(name);
  1425. if (!(gState instanceof _primitives.Dict)) {
  1426. throw new _util.FormatError("GState should be a dictionary.");
  1427. }
  1428. self.setGState({
  1429. resources,
  1430. gState,
  1431. operatorList,
  1432. cacheKey: name,
  1433. task,
  1434. stateManager,
  1435. localGStateCache,
  1436. localColorSpaceCache
  1437. }).then(resolveGState, rejectGState);
  1438. }).catch(function (reason) {
  1439. if (reason instanceof _util.AbortException) {
  1440. return;
  1441. }
  1442. if (self.options.ignoreErrors) {
  1443. self.handler.send("UnsupportedFeature", {
  1444. featureId: _util.UNSUPPORTED_FEATURES.errorExtGState
  1445. });
  1446. (0, _util.warn)(`getOperatorList - ignoring ExtGState: "${reason}".`);
  1447. return;
  1448. }
  1449. throw reason;
  1450. }));
  1451. return;
  1452. case _util.OPS.moveTo:
  1453. case _util.OPS.lineTo:
  1454. case _util.OPS.curveTo:
  1455. case _util.OPS.curveTo2:
  1456. case _util.OPS.curveTo3:
  1457. case _util.OPS.closePath:
  1458. case _util.OPS.rectangle:
  1459. self.buildPath(operatorList, fn, args, parsingText);
  1460. continue;
  1461. case _util.OPS.markPoint:
  1462. case _util.OPS.markPointProps:
  1463. case _util.OPS.beginCompat:
  1464. case _util.OPS.endCompat:
  1465. continue;
  1466. case _util.OPS.beginMarkedContentProps:
  1467. if (!(0, _primitives.isName)(args[0])) {
  1468. (0, _util.warn)(`Expected name for beginMarkedContentProps arg0=${args[0]}`);
  1469. continue;
  1470. }
  1471. if (args[0].name === "OC") {
  1472. next(self.parseMarkedContentProps(args[1], resources).then(data => {
  1473. operatorList.addOp(_util.OPS.beginMarkedContentProps, ["OC", data]);
  1474. }).catch(reason => {
  1475. if (reason instanceof _util.AbortException) {
  1476. return;
  1477. }
  1478. if (self.options.ignoreErrors) {
  1479. self.handler.send("UnsupportedFeature", {
  1480. featureId: _util.UNSUPPORTED_FEATURES.errorMarkedContent
  1481. });
  1482. (0, _util.warn)(`getOperatorList - ignoring beginMarkedContentProps: "${reason}".`);
  1483. return;
  1484. }
  1485. throw reason;
  1486. }));
  1487. return;
  1488. }
  1489. args = [args[0].name, args[1] instanceof _primitives.Dict ? args[1].get("MCID") : null];
  1490. break;
  1491. case _util.OPS.beginMarkedContent:
  1492. case _util.OPS.endMarkedContent:
  1493. default:
  1494. if (args !== null) {
  1495. for (i = 0, ii = args.length; i < ii; i++) {
  1496. if (args[i] instanceof _primitives.Dict) {
  1497. break;
  1498. }
  1499. }
  1500. if (i < ii) {
  1501. (0, _util.warn)("getOperatorList - ignoring operator: " + fn);
  1502. continue;
  1503. }
  1504. }
  1505. }
  1506. operatorList.addOp(fn, args);
  1507. }
  1508. if (stop) {
  1509. next(deferred);
  1510. return;
  1511. }
  1512. closePendingRestoreOPS();
  1513. resolve();
  1514. }).catch(reason => {
  1515. if (reason instanceof _util.AbortException) {
  1516. return;
  1517. }
  1518. if (this.options.ignoreErrors) {
  1519. this.handler.send("UnsupportedFeature", {
  1520. featureId: _util.UNSUPPORTED_FEATURES.errorOperatorList
  1521. });
  1522. (0, _util.warn)(`getOperatorList - ignoring errors during "${task.name}" ` + `task: "${reason}".`);
  1523. closePendingRestoreOPS();
  1524. return;
  1525. }
  1526. throw reason;
  1527. });
  1528. }
  1529. getTextContent({
  1530. stream,
  1531. task,
  1532. resources,
  1533. stateManager = null,
  1534. normalizeWhitespace = false,
  1535. combineTextItems = false,
  1536. includeMarkedContent = false,
  1537. sink,
  1538. seenStyles = new Set()
  1539. }) {
  1540. resources = resources || _primitives.Dict.empty;
  1541. stateManager = stateManager || new StateManager(new TextState());
  1542. const WhitespaceRegexp = /\s/g;
  1543. const textContent = {
  1544. items: [],
  1545. styles: Object.create(null)
  1546. };
  1547. const textContentItem = {
  1548. initialized: false,
  1549. str: [],
  1550. totalWidth: 0,
  1551. totalHeight: 0,
  1552. width: 0,
  1553. height: 0,
  1554. vertical: false,
  1555. lastCharSize: 0,
  1556. prevTransform: null,
  1557. textAdvanceScale: 0,
  1558. spaceWidth: 0,
  1559. spaceInFlowMin: 0,
  1560. spaceInFlowMax: 0,
  1561. trackingSpaceMin: Infinity,
  1562. transform: null,
  1563. fontName: null,
  1564. hasEOL: false,
  1565. isLastCharWhiteSpace: false
  1566. };
  1567. const TRACKING_SPACE_FACTOR = 0.3;
  1568. const SPACE_IN_FLOW_MIN_FACTOR = 0.3;
  1569. const SPACE_IN_FLOW_MAX_FACTOR = 1.3;
  1570. const self = this;
  1571. const xref = this.xref;
  1572. const showSpacedTextBuffer = [];
  1573. let xobjs = null;
  1574. const emptyXObjectCache = new _image_utils.LocalImageCache();
  1575. const emptyGStateCache = new _image_utils.LocalGStateCache();
  1576. const preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
  1577. let textState;
  1578. function getCurrentTextTransform() {
  1579. const font = textState.font;
  1580. const tsm = [textState.fontSize * textState.textHScale, 0, 0, textState.fontSize, 0, textState.textRise];
  1581. if (font.isType3Font && (textState.fontSize <= 1 || font.isCharBBox) && !(0, _util.isArrayEqual)(textState.fontMatrix, _util.FONT_IDENTITY_MATRIX)) {
  1582. const glyphHeight = font.bbox[3] - font.bbox[1];
  1583. if (glyphHeight > 0) {
  1584. tsm[3] *= glyphHeight * textState.fontMatrix[3];
  1585. }
  1586. }
  1587. return _util.Util.transform(textState.ctm, _util.Util.transform(textState.textMatrix, tsm));
  1588. }
  1589. function ensureTextContentItem() {
  1590. if (textContentItem.initialized) {
  1591. return textContentItem;
  1592. }
  1593. const font = textState.font,
  1594. loadedName = font.loadedName;
  1595. if (!seenStyles.has(loadedName)) {
  1596. seenStyles.add(loadedName);
  1597. textContent.styles[loadedName] = {
  1598. fontFamily: font.fallbackName,
  1599. ascent: font.ascent,
  1600. descent: font.descent,
  1601. vertical: font.vertical
  1602. };
  1603. }
  1604. textContentItem.fontName = loadedName;
  1605. const trm = textContentItem.transform = getCurrentTextTransform();
  1606. if (!font.vertical) {
  1607. textContentItem.width = textContentItem.totalWidth = 0;
  1608. textContentItem.height = textContentItem.totalHeight = Math.hypot(trm[2], trm[3]);
  1609. textContentItem.vertical = false;
  1610. } else {
  1611. textContentItem.width = textContentItem.totalWidth = Math.hypot(trm[0], trm[1]);
  1612. textContentItem.height = textContentItem.totalHeight = 0;
  1613. textContentItem.vertical = true;
  1614. }
  1615. const scaleLineX = Math.hypot(textState.textLineMatrix[0], textState.textLineMatrix[1]);
  1616. const scaleCtmX = Math.hypot(textState.ctm[0], textState.ctm[1]);
  1617. textContentItem.textAdvanceScale = scaleCtmX * scaleLineX;
  1618. textContentItem.lastCharSize = textContentItem.lastCharSize || 0;
  1619. const spaceWidth = font.spaceWidth / 1000 * textState.fontSize;
  1620. if (spaceWidth) {
  1621. textContentItem.spaceWidth = spaceWidth;
  1622. textContentItem.trackingSpaceMin = spaceWidth * TRACKING_SPACE_FACTOR;
  1623. textContentItem.spaceInFlowMin = spaceWidth * SPACE_IN_FLOW_MIN_FACTOR;
  1624. textContentItem.spaceInFlowMax = spaceWidth * SPACE_IN_FLOW_MAX_FACTOR;
  1625. } else {
  1626. textContentItem.spaceWidth = 0;
  1627. textContentItem.trackingSpaceMin = Infinity;
  1628. }
  1629. textContentItem.hasEOL = false;
  1630. textContentItem.initialized = true;
  1631. return textContentItem;
  1632. }
  1633. function updateAdvanceScale() {
  1634. if (!textContentItem.initialized) {
  1635. return;
  1636. }
  1637. const scaleLineX = Math.hypot(textState.textLineMatrix[0], textState.textLineMatrix[1]);
  1638. const scaleCtmX = Math.hypot(textState.ctm[0], textState.ctm[1]);
  1639. const scaleFactor = scaleCtmX * scaleLineX;
  1640. if (scaleFactor === textContentItem.textAdvanceScale) {
  1641. return;
  1642. }
  1643. if (!textContentItem.vertical) {
  1644. textContentItem.totalWidth += textContentItem.width * textContentItem.textAdvanceScale;
  1645. textContentItem.width = 0;
  1646. } else {
  1647. textContentItem.totalHeight += textContentItem.height * textContentItem.textAdvanceScale;
  1648. textContentItem.height = 0;
  1649. }
  1650. textContentItem.textAdvanceScale = scaleFactor;
  1651. }
  1652. function replaceWhitespace(str) {
  1653. const ii = str.length;
  1654. let i = 0,
  1655. code;
  1656. while (i < ii && (code = str.charCodeAt(i)) >= 0x20 && code <= 0x7f) {
  1657. i++;
  1658. }
  1659. return i < ii ? str.replace(WhitespaceRegexp, " ") : str;
  1660. }
  1661. function runBidiTransform(textChunk) {
  1662. const text = textChunk.str.join("");
  1663. const bidiResult = (0, _bidi.bidi)(text, -1, textChunk.vertical);
  1664. const str = normalizeWhitespace ? replaceWhitespace(bidiResult.str) : bidiResult.str;
  1665. return {
  1666. str,
  1667. dir: bidiResult.dir,
  1668. width: textChunk.totalWidth,
  1669. height: textChunk.totalHeight,
  1670. transform: textChunk.transform,
  1671. fontName: textChunk.fontName,
  1672. hasEOL: textChunk.hasEOL
  1673. };
  1674. }
  1675. function handleSetFont(fontName, fontRef) {
  1676. return self.loadFont(fontName, fontRef, resources).then(function (translated) {
  1677. if (!translated.font.isType3Font) {
  1678. return translated;
  1679. }
  1680. return translated.loadType3Data(self, resources, task).catch(function () {}).then(function () {
  1681. return translated;
  1682. });
  1683. }).then(function (translated) {
  1684. textState.font = translated.font;
  1685. textState.fontMatrix = translated.font.fontMatrix || _util.FONT_IDENTITY_MATRIX;
  1686. });
  1687. }
  1688. function compareWithLastPosition(fontSize) {
  1689. if (!combineTextItems || !textState.font || !textContentItem.prevTransform) {
  1690. return;
  1691. }
  1692. const currentTransform = getCurrentTextTransform();
  1693. const posX = currentTransform[4];
  1694. const posY = currentTransform[5];
  1695. const lastPosX = textContentItem.prevTransform[4];
  1696. const lastPosY = textContentItem.prevTransform[5];
  1697. if (lastPosX === posX && lastPosY === posY) {
  1698. return;
  1699. }
  1700. const advanceX = (posX - lastPosX) / textContentItem.textAdvanceScale;
  1701. const advanceY = (posY - lastPosY) / textContentItem.textAdvanceScale;
  1702. const HALF_LAST_CHAR = -0.5 * textContentItem.lastCharSize;
  1703. if (textState.font.vertical) {
  1704. if (Math.abs(advanceX) > textContentItem.width / textContentItem.textAdvanceScale) {
  1705. appendEOL();
  1706. return;
  1707. }
  1708. if (HALF_LAST_CHAR > advanceY) {
  1709. return;
  1710. }
  1711. if (advanceY > textContentItem.trackingSpaceMin) {
  1712. textContentItem.height += advanceY;
  1713. } else if (!addFakeSpaces(advanceY, 0, textContentItem.prevTransform)) {
  1714. if (textContentItem.str.length === 0) {
  1715. textContent.items.push({
  1716. str: " ",
  1717. dir: "ltr",
  1718. width: 0,
  1719. height: advanceY,
  1720. transform: textContentItem.prevTransform,
  1721. fontName: textContentItem.fontName,
  1722. hasEOL: false
  1723. });
  1724. textContentItem.isLastCharWhiteSpace = true;
  1725. } else {
  1726. textContentItem.height += advanceY;
  1727. }
  1728. }
  1729. return;
  1730. }
  1731. if (Math.abs(advanceY) > textContentItem.height / textContentItem.textAdvanceScale) {
  1732. appendEOL();
  1733. return;
  1734. }
  1735. if (HALF_LAST_CHAR > advanceX) {
  1736. return;
  1737. }
  1738. if (advanceX <= textContentItem.trackingSpaceMin) {
  1739. textContentItem.width += advanceX;
  1740. } else if (!addFakeSpaces(advanceX, 0, textContentItem.prevTransform)) {
  1741. if (textContentItem.str.length === 0) {
  1742. textContent.items.push({
  1743. str: " ",
  1744. dir: "ltr",
  1745. width: advanceX,
  1746. height: 0,
  1747. transform: textContentItem.prevTransform,
  1748. fontName: textContentItem.fontName,
  1749. hasEOL: false
  1750. });
  1751. textContentItem.isLastCharWhiteSpace = true;
  1752. } else {
  1753. textContentItem.width += advanceX;
  1754. }
  1755. }
  1756. }
  1757. function buildTextContentItem({
  1758. chars,
  1759. extraSpacing,
  1760. isFirstChunk
  1761. }) {
  1762. const font = textState.font;
  1763. if (!chars) {
  1764. const charSpacing = textState.charSpacing + extraSpacing;
  1765. if (charSpacing) {
  1766. if (!font.vertical) {
  1767. textState.translateTextMatrix(charSpacing * textState.textHScale, 0);
  1768. } else {
  1769. textState.translateTextMatrix(0, charSpacing);
  1770. }
  1771. }
  1772. return;
  1773. }
  1774. const NormalizedUnicodes = (0, _unicode.getNormalizedUnicodes)();
  1775. const glyphs = font.charsToGlyphs(chars);
  1776. const scale = textState.fontMatrix[0] * textState.fontSize;
  1777. if (isFirstChunk) {
  1778. compareWithLastPosition(scale);
  1779. }
  1780. let textChunk = ensureTextContentItem();
  1781. let size = 0;
  1782. let lastCharSize = 0;
  1783. for (let i = 0, ii = glyphs.length; i < ii; i++) {
  1784. const glyph = glyphs[i];
  1785. let charSpacing = textState.charSpacing + (i === ii - 1 ? extraSpacing : 0);
  1786. let glyphUnicode = glyph.unicode;
  1787. if (glyph.isSpace) {
  1788. charSpacing += textState.wordSpacing;
  1789. textChunk.isLastCharWhiteSpace = true;
  1790. } else {
  1791. glyphUnicode = NormalizedUnicodes[glyphUnicode] || glyphUnicode;
  1792. glyphUnicode = (0, _unicode.reverseIfRtl)(glyphUnicode);
  1793. textChunk.isLastCharWhiteSpace = false;
  1794. }
  1795. textChunk.str.push(glyphUnicode);
  1796. const glyphWidth = font.vertical && glyph.vmetric ? glyph.vmetric[0] : glyph.width;
  1797. let scaledDim = glyphWidth * scale;
  1798. if (!font.vertical) {
  1799. scaledDim *= textState.textHScale;
  1800. textState.translateTextMatrix(scaledDim, 0);
  1801. } else {
  1802. textState.translateTextMatrix(0, scaledDim);
  1803. scaledDim = Math.abs(scaledDim);
  1804. }
  1805. size += scaledDim;
  1806. if (charSpacing) {
  1807. if (!font.vertical) {
  1808. charSpacing *= textState.textHScale;
  1809. }
  1810. scaledDim += charSpacing;
  1811. const wasSplit = charSpacing > textContentItem.trackingSpaceMin && addFakeSpaces(charSpacing, size);
  1812. if (!font.vertical) {
  1813. textState.translateTextMatrix(charSpacing, 0);
  1814. } else {
  1815. textState.translateTextMatrix(0, charSpacing);
  1816. }
  1817. if (wasSplit) {
  1818. textChunk = ensureTextContentItem();
  1819. size = 0;
  1820. } else {
  1821. size += charSpacing;
  1822. }
  1823. }
  1824. lastCharSize = scaledDim;
  1825. }
  1826. textChunk.lastCharSize = lastCharSize;
  1827. if (!font.vertical) {
  1828. textChunk.width += size;
  1829. } else {
  1830. textChunk.height += size;
  1831. }
  1832. textChunk.prevTransform = getCurrentTextTransform();
  1833. }
  1834. function appendEOL() {
  1835. if (textContentItem.initialized) {
  1836. textContentItem.hasEOL = true;
  1837. flushTextContentItem();
  1838. } else if (textContent.items.length > 0) {
  1839. textContent.items[textContent.items.length - 1].hasEOL = true;
  1840. } else {
  1841. textContent.items.push({
  1842. str: "",
  1843. dir: "ltr",
  1844. width: 0,
  1845. height: 0,
  1846. transform: getCurrentTextTransform(),
  1847. fontName: textState.font.loadedName,
  1848. hasEOL: true
  1849. });
  1850. }
  1851. textContentItem.isLastCharWhiteSpace = false;
  1852. textContentItem.lastCharSize = 0;
  1853. }
  1854. function addFakeSpaces(width, size, transf = null) {
  1855. if (textContentItem.spaceInFlowMin <= width && width <= textContentItem.spaceInFlowMax) {
  1856. if (textContentItem.initialized) {
  1857. textContentItem.str.push(" ");
  1858. textContentItem.isLastCharWhiteSpace = true;
  1859. }
  1860. return false;
  1861. }
  1862. const fontName = textContentItem.fontName;
  1863. let height = 0;
  1864. width *= textContentItem.textAdvanceScale;
  1865. if (!textContentItem.vertical) {
  1866. textContentItem.width += size;
  1867. } else {
  1868. textContentItem.height += size;
  1869. height = width;
  1870. width = 0;
  1871. }
  1872. flushTextContentItem();
  1873. if (textContentItem.isLastCharWhiteSpace) {
  1874. return true;
  1875. }
  1876. textContentItem.isLastCharWhiteSpace = true;
  1877. textContent.items.push({
  1878. str: " ",
  1879. dir: "ltr",
  1880. width,
  1881. height,
  1882. transform: transf ? transf : getCurrentTextTransform(),
  1883. fontName,
  1884. hasEOL: false
  1885. });
  1886. return true;
  1887. }
  1888. function flushTextContentItem() {
  1889. if (!textContentItem.initialized || !textContentItem.str) {
  1890. return;
  1891. }
  1892. if (!textContentItem.vertical) {
  1893. textContentItem.totalWidth += textContentItem.width * textContentItem.textAdvanceScale;
  1894. } else {
  1895. textContentItem.totalHeight += textContentItem.height * textContentItem.textAdvanceScale;
  1896. }
  1897. textContent.items.push(runBidiTransform(textContentItem));
  1898. textContentItem.initialized = false;
  1899. textContentItem.str.length = 0;
  1900. }
  1901. function enqueueChunk() {
  1902. const length = textContent.items.length;
  1903. if (length > 0) {
  1904. sink.enqueue(textContent, length);
  1905. textContent.items = [];
  1906. textContent.styles = Object.create(null);
  1907. }
  1908. }
  1909. const timeSlotManager = new TimeSlotManager();
  1910. return new Promise(function promiseBody(resolve, reject) {
  1911. const next = function (promise) {
  1912. enqueueChunk();
  1913. Promise.all([promise, sink.ready]).then(function () {
  1914. try {
  1915. promiseBody(resolve, reject);
  1916. } catch (ex) {
  1917. reject(ex);
  1918. }
  1919. }, reject);
  1920. };
  1921. task.ensureNotTerminated();
  1922. timeSlotManager.reset();
  1923. const operation = {};
  1924. let stop,
  1925. args = [];
  1926. while (!(stop = timeSlotManager.check())) {
  1927. args.length = 0;
  1928. operation.args = args;
  1929. if (!preprocessor.read(operation)) {
  1930. break;
  1931. }
  1932. textState = stateManager.state;
  1933. const fn = operation.fn;
  1934. args = operation.args;
  1935. switch (fn | 0) {
  1936. case _util.OPS.setFont:
  1937. var fontNameArg = args[0].name,
  1938. fontSizeArg = args[1];
  1939. if (textState.font && fontNameArg === textState.fontName && fontSizeArg === textState.fontSize) {
  1940. break;
  1941. }
  1942. flushTextContentItem();
  1943. textState.fontName = fontNameArg;
  1944. textState.fontSize = fontSizeArg;
  1945. next(handleSetFont(fontNameArg, null));
  1946. return;
  1947. case _util.OPS.setTextRise:
  1948. flushTextContentItem();
  1949. textState.textRise = args[0];
  1950. break;
  1951. case _util.OPS.setHScale:
  1952. flushTextContentItem();
  1953. textState.textHScale = args[0] / 100;
  1954. break;
  1955. case _util.OPS.setLeading:
  1956. flushTextContentItem();
  1957. textState.leading = args[0];
  1958. break;
  1959. case _util.OPS.moveText:
  1960. textState.translateTextLineMatrix(args[0], args[1]);
  1961. textState.textMatrix = textState.textLineMatrix.slice();
  1962. break;
  1963. case _util.OPS.setLeadingMoveText:
  1964. flushTextContentItem();
  1965. textState.leading = -args[1];
  1966. textState.translateTextLineMatrix(args[0], args[1]);
  1967. textState.textMatrix = textState.textLineMatrix.slice();
  1968. break;
  1969. case _util.OPS.nextLine:
  1970. appendEOL();
  1971. textState.carriageReturn();
  1972. break;
  1973. case _util.OPS.setTextMatrix:
  1974. textState.setTextMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
  1975. textState.setTextLineMatrix(args[0], args[1], args[2], args[3], args[4], args[5]);
  1976. updateAdvanceScale();
  1977. break;
  1978. case _util.OPS.setCharSpacing:
  1979. textState.charSpacing = args[0];
  1980. break;
  1981. case _util.OPS.setWordSpacing:
  1982. textState.wordSpacing = args[0];
  1983. break;
  1984. case _util.OPS.beginText:
  1985. flushTextContentItem();
  1986. textState.textMatrix = _util.IDENTITY_MATRIX.slice();
  1987. textState.textLineMatrix = _util.IDENTITY_MATRIX.slice();
  1988. break;
  1989. case _util.OPS.showSpacedText:
  1990. if (!stateManager.state.font) {
  1991. self.ensureStateFont(stateManager.state);
  1992. continue;
  1993. }
  1994. const spaceFactor = (textState.font.vertical ? 1 : -1) * textState.fontSize / 1000;
  1995. const elements = args[0];
  1996. let isFirstChunk = true;
  1997. for (let i = 0, ii = elements.length; i < ii - 1; i++) {
  1998. const item = elements[i];
  1999. if (typeof item === "string") {
  2000. showSpacedTextBuffer.push(item);
  2001. } else if (typeof item === "number" && item !== 0) {
  2002. const str = showSpacedTextBuffer.join("");
  2003. showSpacedTextBuffer.length = 0;
  2004. buildTextContentItem({
  2005. chars: str,
  2006. extraSpacing: item * spaceFactor,
  2007. isFirstChunk
  2008. });
  2009. if (str && isFirstChunk) {
  2010. isFirstChunk = false;
  2011. }
  2012. }
  2013. }
  2014. const item = elements[elements.length - 1];
  2015. if (typeof item === "string") {
  2016. showSpacedTextBuffer.push(item);
  2017. }
  2018. if (showSpacedTextBuffer.length > 0) {
  2019. const str = showSpacedTextBuffer.join("");
  2020. showSpacedTextBuffer.length = 0;
  2021. buildTextContentItem({
  2022. chars: str,
  2023. extraSpacing: 0,
  2024. isFirstChunk
  2025. });
  2026. }
  2027. break;
  2028. case _util.OPS.showText:
  2029. if (!stateManager.state.font) {
  2030. self.ensureStateFont(stateManager.state);
  2031. continue;
  2032. }
  2033. buildTextContentItem({
  2034. chars: args[0],
  2035. extraSpacing: 0,
  2036. isFirstChunk: true
  2037. });
  2038. break;
  2039. case _util.OPS.nextLineShowText:
  2040. if (!stateManager.state.font) {
  2041. self.ensureStateFont(stateManager.state);
  2042. continue;
  2043. }
  2044. textContentItem.hasEOL = true;
  2045. flushTextContentItem();
  2046. textState.carriageReturn();
  2047. buildTextContentItem({
  2048. chars: args[0],
  2049. extraSpacing: 0,
  2050. isFirstChunk: true
  2051. });
  2052. break;
  2053. case _util.OPS.nextLineSetSpacingShowText:
  2054. if (!stateManager.state.font) {
  2055. self.ensureStateFont(stateManager.state);
  2056. continue;
  2057. }
  2058. textContentItem.hasEOL = true;
  2059. flushTextContentItem();
  2060. textState.wordSpacing = args[0];
  2061. textState.charSpacing = args[1];
  2062. textState.carriageReturn();
  2063. buildTextContentItem({
  2064. chars: args[2],
  2065. extraSpacing: 0,
  2066. isFirstChunk: true
  2067. });
  2068. break;
  2069. case _util.OPS.paintXObject:
  2070. flushTextContentItem();
  2071. if (!xobjs) {
  2072. xobjs = resources.get("XObject") || _primitives.Dict.empty;
  2073. }
  2074. var isValidName = args[0] instanceof _primitives.Name;
  2075. var name = args[0].name;
  2076. if (isValidName && emptyXObjectCache.getByName(name)) {
  2077. break;
  2078. }
  2079. next(new Promise(function (resolveXObject, rejectXObject) {
  2080. if (!isValidName) {
  2081. throw new _util.FormatError("XObject must be referred to by name.");
  2082. }
  2083. let xobj = xobjs.getRaw(name);
  2084. if (xobj instanceof _primitives.Ref) {
  2085. if (emptyXObjectCache.getByRef(xobj)) {
  2086. resolveXObject();
  2087. return;
  2088. }
  2089. const globalImage = self.globalImageCache.getData(xobj, self.pageIndex);
  2090. if (globalImage) {
  2091. resolveXObject();
  2092. return;
  2093. }
  2094. xobj = xref.fetch(xobj);
  2095. }
  2096. if (!(0, _primitives.isStream)(xobj)) {
  2097. throw new _util.FormatError("XObject should be a stream");
  2098. }
  2099. const type = xobj.dict.get("Subtype");
  2100. if (!(0, _primitives.isName)(type)) {
  2101. throw new _util.FormatError("XObject should have a Name subtype");
  2102. }
  2103. if (type.name !== "Form") {
  2104. emptyXObjectCache.set(name, xobj.dict.objId, true);
  2105. resolveXObject();
  2106. return;
  2107. }
  2108. const currentState = stateManager.state.clone();
  2109. const xObjStateManager = new StateManager(currentState);
  2110. const matrix = xobj.dict.getArray("Matrix");
  2111. if (Array.isArray(matrix) && matrix.length === 6) {
  2112. xObjStateManager.transform(matrix);
  2113. }
  2114. enqueueChunk();
  2115. const sinkWrapper = {
  2116. enqueueInvoked: false,
  2117. enqueue(chunk, size) {
  2118. this.enqueueInvoked = true;
  2119. sink.enqueue(chunk, size);
  2120. },
  2121. get desiredSize() {
  2122. return sink.desiredSize;
  2123. },
  2124. get ready() {
  2125. return sink.ready;
  2126. }
  2127. };
  2128. self.getTextContent({
  2129. stream: xobj,
  2130. task,
  2131. resources: xobj.dict.get("Resources") || resources,
  2132. stateManager: xObjStateManager,
  2133. normalizeWhitespace,
  2134. combineTextItems,
  2135. includeMarkedContent,
  2136. sink: sinkWrapper,
  2137. seenStyles
  2138. }).then(function () {
  2139. if (!sinkWrapper.enqueueInvoked) {
  2140. emptyXObjectCache.set(name, xobj.dict.objId, true);
  2141. }
  2142. resolveXObject();
  2143. }, rejectXObject);
  2144. }).catch(function (reason) {
  2145. if (reason instanceof _util.AbortException) {
  2146. return;
  2147. }
  2148. if (self.options.ignoreErrors) {
  2149. (0, _util.warn)(`getTextContent - ignoring XObject: "${reason}".`);
  2150. return;
  2151. }
  2152. throw reason;
  2153. }));
  2154. return;
  2155. case _util.OPS.setGState:
  2156. isValidName = args[0] instanceof _primitives.Name;
  2157. name = args[0].name;
  2158. if (isValidName && emptyGStateCache.getByName(name)) {
  2159. break;
  2160. }
  2161. next(new Promise(function (resolveGState, rejectGState) {
  2162. if (!isValidName) {
  2163. throw new _util.FormatError("GState must be referred to by name.");
  2164. }
  2165. const extGState = resources.get("ExtGState");
  2166. if (!(extGState instanceof _primitives.Dict)) {
  2167. throw new _util.FormatError("ExtGState should be a dictionary.");
  2168. }
  2169. const gState = extGState.get(name);
  2170. if (!(gState instanceof _primitives.Dict)) {
  2171. throw new _util.FormatError("GState should be a dictionary.");
  2172. }
  2173. const gStateFont = gState.get("Font");
  2174. if (!gStateFont) {
  2175. emptyGStateCache.set(name, gState.objId, true);
  2176. resolveGState();
  2177. return;
  2178. }
  2179. flushTextContentItem();
  2180. textState.fontName = null;
  2181. textState.fontSize = gStateFont[1];
  2182. handleSetFont(null, gStateFont[0]).then(resolveGState, rejectGState);
  2183. }).catch(function (reason) {
  2184. if (reason instanceof _util.AbortException) {
  2185. return;
  2186. }
  2187. if (self.options.ignoreErrors) {
  2188. (0, _util.warn)(`getTextContent - ignoring ExtGState: "${reason}".`);
  2189. return;
  2190. }
  2191. throw reason;
  2192. }));
  2193. return;
  2194. case _util.OPS.beginMarkedContent:
  2195. if (includeMarkedContent) {
  2196. textContent.items.push({
  2197. type: "beginMarkedContent",
  2198. tag: (0, _primitives.isName)(args[0]) ? args[0].name : null
  2199. });
  2200. }
  2201. break;
  2202. case _util.OPS.beginMarkedContentProps:
  2203. if (includeMarkedContent) {
  2204. flushTextContentItem();
  2205. let mcid = null;
  2206. if ((0, _primitives.isDict)(args[1])) {
  2207. mcid = args[1].get("MCID");
  2208. }
  2209. textContent.items.push({
  2210. type: "beginMarkedContentProps",
  2211. id: Number.isInteger(mcid) ? `${self.idFactory.getPageObjId()}_mcid${mcid}` : null,
  2212. tag: (0, _primitives.isName)(args[0]) ? args[0].name : null
  2213. });
  2214. }
  2215. break;
  2216. case _util.OPS.endMarkedContent:
  2217. if (includeMarkedContent) {
  2218. flushTextContentItem();
  2219. textContent.items.push({
  2220. type: "endMarkedContent"
  2221. });
  2222. }
  2223. break;
  2224. }
  2225. if (textContent.items.length >= sink.desiredSize) {
  2226. stop = true;
  2227. break;
  2228. }
  2229. }
  2230. if (stop) {
  2231. next(deferred);
  2232. return;
  2233. }
  2234. flushTextContentItem();
  2235. enqueueChunk();
  2236. resolve();
  2237. }).catch(reason => {
  2238. if (reason instanceof _util.AbortException) {
  2239. return;
  2240. }
  2241. if (this.options.ignoreErrors) {
  2242. (0, _util.warn)(`getTextContent - ignoring errors during "${task.name}" ` + `task: "${reason}".`);
  2243. flushTextContentItem();
  2244. enqueueChunk();
  2245. return;
  2246. }
  2247. throw reason;
  2248. });
  2249. }
  2250. extractDataStructures(dict, baseDict, properties) {
  2251. const xref = this.xref;
  2252. let cidToGidBytes;
  2253. const toUnicodePromise = this.readToUnicode(properties.toUnicode || dict.get("ToUnicode") || baseDict.get("ToUnicode"));
  2254. if (properties.composite) {
  2255. const cidSystemInfo = dict.get("CIDSystemInfo");
  2256. if ((0, _primitives.isDict)(cidSystemInfo)) {
  2257. properties.cidSystemInfo = {
  2258. registry: (0, _util.stringToPDFString)(cidSystemInfo.get("Registry")),
  2259. ordering: (0, _util.stringToPDFString)(cidSystemInfo.get("Ordering")),
  2260. supplement: cidSystemInfo.get("Supplement")
  2261. };
  2262. }
  2263. const cidToGidMap = dict.get("CIDToGIDMap");
  2264. if ((0, _primitives.isStream)(cidToGidMap)) {
  2265. cidToGidBytes = cidToGidMap.getBytes();
  2266. }
  2267. }
  2268. const differences = [];
  2269. let baseEncodingName = null;
  2270. let encoding;
  2271. if (dict.has("Encoding")) {
  2272. encoding = dict.get("Encoding");
  2273. if ((0, _primitives.isDict)(encoding)) {
  2274. baseEncodingName = encoding.get("BaseEncoding");
  2275. baseEncodingName = (0, _primitives.isName)(baseEncodingName) ? baseEncodingName.name : null;
  2276. if (encoding.has("Differences")) {
  2277. const diffEncoding = encoding.get("Differences");
  2278. let index = 0;
  2279. for (let j = 0, jj = diffEncoding.length; j < jj; j++) {
  2280. const data = xref.fetchIfRef(diffEncoding[j]);
  2281. if ((0, _util.isNum)(data)) {
  2282. index = data;
  2283. } else if ((0, _primitives.isName)(data)) {
  2284. differences[index++] = data.name;
  2285. } else {
  2286. throw new _util.FormatError(`Invalid entry in 'Differences' array: ${data}`);
  2287. }
  2288. }
  2289. }
  2290. } else if ((0, _primitives.isName)(encoding)) {
  2291. baseEncodingName = encoding.name;
  2292. } else {
  2293. throw new _util.FormatError("Encoding is not a Name nor a Dict");
  2294. }
  2295. if (baseEncodingName !== "MacRomanEncoding" && baseEncodingName !== "MacExpertEncoding" && baseEncodingName !== "WinAnsiEncoding") {
  2296. baseEncodingName = null;
  2297. }
  2298. }
  2299. if (baseEncodingName) {
  2300. properties.defaultEncoding = (0, _encodings.getEncoding)(baseEncodingName);
  2301. } else {
  2302. const isSymbolicFont = !!(properties.flags & _fonts_utils.FontFlags.Symbolic);
  2303. const isNonsymbolicFont = !!(properties.flags & _fonts_utils.FontFlags.Nonsymbolic);
  2304. encoding = _encodings.StandardEncoding;
  2305. if (properties.type === "TrueType" && !isNonsymbolicFont) {
  2306. encoding = _encodings.WinAnsiEncoding;
  2307. }
  2308. if (isSymbolicFont) {
  2309. encoding = _encodings.MacRomanEncoding;
  2310. if (!properties.file || properties.isInternalFont) {
  2311. if (/Symbol/i.test(properties.name)) {
  2312. encoding = _encodings.SymbolSetEncoding;
  2313. } else if (/Dingbats|Wingdings/i.test(properties.name)) {
  2314. encoding = _encodings.ZapfDingbatsEncoding;
  2315. }
  2316. }
  2317. }
  2318. properties.defaultEncoding = encoding;
  2319. }
  2320. properties.differences = differences;
  2321. properties.baseEncodingName = baseEncodingName;
  2322. properties.hasEncoding = !!baseEncodingName || differences.length > 0;
  2323. properties.dict = dict;
  2324. return toUnicodePromise.then(readToUnicode => {
  2325. properties.toUnicode = readToUnicode;
  2326. return this.buildToUnicode(properties);
  2327. }).then(builtToUnicode => {
  2328. properties.toUnicode = builtToUnicode;
  2329. if (cidToGidBytes) {
  2330. properties.cidToGidMap = this.readCidToGidMap(cidToGidBytes, builtToUnicode);
  2331. }
  2332. return properties;
  2333. });
  2334. }
  2335. _simpleFontToUnicode(properties, forceGlyphs = false) {
  2336. (0, _util.assert)(!properties.composite, "Must be a simple font.");
  2337. const toUnicode = [];
  2338. const encoding = properties.defaultEncoding.slice();
  2339. const baseEncodingName = properties.baseEncodingName;
  2340. const differences = properties.differences;
  2341. for (const charcode in differences) {
  2342. const glyphName = differences[charcode];
  2343. if (glyphName === ".notdef") {
  2344. continue;
  2345. }
  2346. encoding[charcode] = glyphName;
  2347. }
  2348. const glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  2349. for (const charcode in encoding) {
  2350. let glyphName = encoding[charcode];
  2351. if (glyphName === "") {
  2352. continue;
  2353. } else if (glyphsUnicodeMap[glyphName] === undefined) {
  2354. let code = 0;
  2355. switch (glyphName[0]) {
  2356. case "G":
  2357. if (glyphName.length === 3) {
  2358. code = parseInt(glyphName.substring(1), 16);
  2359. }
  2360. break;
  2361. case "g":
  2362. if (glyphName.length === 5) {
  2363. code = parseInt(glyphName.substring(1), 16);
  2364. }
  2365. break;
  2366. case "C":
  2367. case "c":
  2368. if (glyphName.length >= 3 && glyphName.length <= 4) {
  2369. const codeStr = glyphName.substring(1);
  2370. if (forceGlyphs) {
  2371. code = parseInt(codeStr, 16);
  2372. break;
  2373. }
  2374. code = +codeStr;
  2375. if (Number.isNaN(code) && Number.isInteger(parseInt(codeStr, 16))) {
  2376. return this._simpleFontToUnicode(properties, true);
  2377. }
  2378. }
  2379. break;
  2380. default:
  2381. const unicode = (0, _unicode.getUnicodeForGlyph)(glyphName, glyphsUnicodeMap);
  2382. if (unicode !== -1) {
  2383. code = unicode;
  2384. }
  2385. }
  2386. if (code > 0 && code <= 0x10ffff && Number.isInteger(code)) {
  2387. if (baseEncodingName && code === +charcode) {
  2388. const baseEncoding = (0, _encodings.getEncoding)(baseEncodingName);
  2389. if (baseEncoding && (glyphName = baseEncoding[charcode])) {
  2390. toUnicode[charcode] = String.fromCharCode(glyphsUnicodeMap[glyphName]);
  2391. continue;
  2392. }
  2393. }
  2394. toUnicode[charcode] = String.fromCodePoint(code);
  2395. }
  2396. continue;
  2397. }
  2398. toUnicode[charcode] = String.fromCharCode(glyphsUnicodeMap[glyphName]);
  2399. }
  2400. return toUnicode;
  2401. }
  2402. async buildToUnicode(properties) {
  2403. properties.hasIncludedToUnicodeMap = !!properties.toUnicode && properties.toUnicode.length > 0;
  2404. if (properties.hasIncludedToUnicodeMap) {
  2405. if (!properties.composite && properties.hasEncoding) {
  2406. properties.fallbackToUnicode = this._simpleFontToUnicode(properties);
  2407. }
  2408. return properties.toUnicode;
  2409. }
  2410. if (!properties.composite) {
  2411. return new _to_unicode_map.ToUnicodeMap(this._simpleFontToUnicode(properties));
  2412. }
  2413. 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"))) {
  2414. const {
  2415. registry,
  2416. ordering
  2417. } = properties.cidSystemInfo;
  2418. const ucs2CMapName = _primitives.Name.get(`${registry}-${ordering}-UCS2`);
  2419. const ucs2CMap = await _cmap.CMapFactory.create({
  2420. encoding: ucs2CMapName,
  2421. fetchBuiltInCMap: this._fetchBuiltInCMapBound,
  2422. useCMap: null
  2423. });
  2424. const toUnicode = [];
  2425. properties.cMap.forEach(function (charcode, cid) {
  2426. if (cid > 0xffff) {
  2427. throw new _util.FormatError("Max size of CID is 65,535");
  2428. }
  2429. const ucs2 = ucs2CMap.lookup(cid);
  2430. if (ucs2) {
  2431. toUnicode[charcode] = String.fromCharCode((ucs2.charCodeAt(0) << 8) + ucs2.charCodeAt(1));
  2432. }
  2433. });
  2434. return new _to_unicode_map.ToUnicodeMap(toUnicode);
  2435. }
  2436. return new _to_unicode_map.IdentityToUnicodeMap(properties.firstChar, properties.lastChar);
  2437. }
  2438. readToUnicode(cmapObj) {
  2439. if (!cmapObj) {
  2440. return Promise.resolve(null);
  2441. }
  2442. if ((0, _primitives.isName)(cmapObj)) {
  2443. return _cmap.CMapFactory.create({
  2444. encoding: cmapObj,
  2445. fetchBuiltInCMap: this._fetchBuiltInCMapBound,
  2446. useCMap: null
  2447. }).then(function (cmap) {
  2448. if (cmap instanceof _cmap.IdentityCMap) {
  2449. return new _to_unicode_map.IdentityToUnicodeMap(0, 0xffff);
  2450. }
  2451. return new _to_unicode_map.ToUnicodeMap(cmap.getMap());
  2452. });
  2453. } else if ((0, _primitives.isStream)(cmapObj)) {
  2454. return _cmap.CMapFactory.create({
  2455. encoding: cmapObj,
  2456. fetchBuiltInCMap: this._fetchBuiltInCMapBound,
  2457. useCMap: null
  2458. }).then(function (cmap) {
  2459. if (cmap instanceof _cmap.IdentityCMap) {
  2460. return new _to_unicode_map.IdentityToUnicodeMap(0, 0xffff);
  2461. }
  2462. const map = new Array(cmap.length);
  2463. cmap.forEach(function (charCode, token) {
  2464. const str = [];
  2465. for (let k = 0; k < token.length; k += 2) {
  2466. const w1 = token.charCodeAt(k) << 8 | token.charCodeAt(k + 1);
  2467. if ((w1 & 0xf800) !== 0xd800) {
  2468. str.push(w1);
  2469. continue;
  2470. }
  2471. k += 2;
  2472. const w2 = token.charCodeAt(k) << 8 | token.charCodeAt(k + 1);
  2473. str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
  2474. }
  2475. map[charCode] = String.fromCodePoint.apply(String, str);
  2476. });
  2477. return new _to_unicode_map.ToUnicodeMap(map);
  2478. }, reason => {
  2479. if (reason instanceof _util.AbortException) {
  2480. return null;
  2481. }
  2482. if (this.options.ignoreErrors) {
  2483. this.handler.send("UnsupportedFeature", {
  2484. featureId: _util.UNSUPPORTED_FEATURES.errorFontToUnicode
  2485. });
  2486. (0, _util.warn)(`readToUnicode - ignoring ToUnicode data: "${reason}".`);
  2487. return null;
  2488. }
  2489. throw reason;
  2490. });
  2491. }
  2492. return Promise.resolve(null);
  2493. }
  2494. readCidToGidMap(glyphsData, toUnicode) {
  2495. const result = [];
  2496. for (let j = 0, jj = glyphsData.length; j < jj; j++) {
  2497. const glyphID = glyphsData[j++] << 8 | glyphsData[j];
  2498. const code = j >> 1;
  2499. if (glyphID === 0 && !toUnicode.has(code)) {
  2500. continue;
  2501. }
  2502. result[code] = glyphID;
  2503. }
  2504. return result;
  2505. }
  2506. extractWidths(dict, descriptor, properties) {
  2507. const xref = this.xref;
  2508. let glyphsWidths = [];
  2509. let defaultWidth = 0;
  2510. const glyphsVMetrics = [];
  2511. let defaultVMetrics;
  2512. let i, ii, j, jj, start, code, widths;
  2513. if (properties.composite) {
  2514. defaultWidth = dict.has("DW") ? dict.get("DW") : 1000;
  2515. widths = dict.get("W");
  2516. if (widths) {
  2517. for (i = 0, ii = widths.length; i < ii; i++) {
  2518. start = xref.fetchIfRef(widths[i++]);
  2519. code = xref.fetchIfRef(widths[i]);
  2520. if (Array.isArray(code)) {
  2521. for (j = 0, jj = code.length; j < jj; j++) {
  2522. glyphsWidths[start++] = xref.fetchIfRef(code[j]);
  2523. }
  2524. } else {
  2525. const width = xref.fetchIfRef(widths[++i]);
  2526. for (j = start; j <= code; j++) {
  2527. glyphsWidths[j] = width;
  2528. }
  2529. }
  2530. }
  2531. }
  2532. if (properties.vertical) {
  2533. let vmetrics = dict.getArray("DW2") || [880, -1000];
  2534. defaultVMetrics = [vmetrics[1], defaultWidth * 0.5, vmetrics[0]];
  2535. vmetrics = dict.get("W2");
  2536. if (vmetrics) {
  2537. for (i = 0, ii = vmetrics.length; i < ii; i++) {
  2538. start = xref.fetchIfRef(vmetrics[i++]);
  2539. code = xref.fetchIfRef(vmetrics[i]);
  2540. if (Array.isArray(code)) {
  2541. for (j = 0, jj = code.length; j < jj; j++) {
  2542. glyphsVMetrics[start++] = [xref.fetchIfRef(code[j++]), xref.fetchIfRef(code[j++]), xref.fetchIfRef(code[j])];
  2543. }
  2544. } else {
  2545. const vmetric = [xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i]), xref.fetchIfRef(vmetrics[++i])];
  2546. for (j = start; j <= code; j++) {
  2547. glyphsVMetrics[j] = vmetric;
  2548. }
  2549. }
  2550. }
  2551. }
  2552. }
  2553. } else {
  2554. const firstChar = properties.firstChar;
  2555. widths = dict.get("Widths");
  2556. if (widths) {
  2557. j = firstChar;
  2558. for (i = 0, ii = widths.length; i < ii; i++) {
  2559. glyphsWidths[j++] = xref.fetchIfRef(widths[i]);
  2560. }
  2561. defaultWidth = parseFloat(descriptor.get("MissingWidth")) || 0;
  2562. } else {
  2563. const baseFontName = dict.get("BaseFont");
  2564. if ((0, _primitives.isName)(baseFontName)) {
  2565. const metrics = this.getBaseFontMetrics(baseFontName.name);
  2566. glyphsWidths = this.buildCharCodeToWidth(metrics.widths, properties);
  2567. defaultWidth = metrics.defaultWidth;
  2568. }
  2569. }
  2570. }
  2571. let isMonospace = true;
  2572. let firstWidth = defaultWidth;
  2573. for (const glyph in glyphsWidths) {
  2574. const glyphWidth = glyphsWidths[glyph];
  2575. if (!glyphWidth) {
  2576. continue;
  2577. }
  2578. if (!firstWidth) {
  2579. firstWidth = glyphWidth;
  2580. continue;
  2581. }
  2582. if (firstWidth !== glyphWidth) {
  2583. isMonospace = false;
  2584. break;
  2585. }
  2586. }
  2587. if (isMonospace) {
  2588. properties.flags |= _fonts_utils.FontFlags.FixedPitch;
  2589. }
  2590. properties.defaultWidth = defaultWidth;
  2591. properties.widths = glyphsWidths;
  2592. properties.defaultVMetrics = defaultVMetrics;
  2593. properties.vmetrics = glyphsVMetrics;
  2594. }
  2595. isSerifFont(baseFontName) {
  2596. const fontNameWoStyle = baseFontName.split("-")[0];
  2597. return fontNameWoStyle in (0, _standard_fonts.getSerifFonts)() || fontNameWoStyle.search(/serif/gi) !== -1;
  2598. }
  2599. getBaseFontMetrics(name) {
  2600. let defaultWidth = 0;
  2601. let widths = Object.create(null);
  2602. let monospace = false;
  2603. const stdFontMap = (0, _standard_fonts.getStdFontMap)();
  2604. let lookupName = stdFontMap[name] || name;
  2605. const Metrics = (0, _metrics.getMetrics)();
  2606. if (!(lookupName in Metrics)) {
  2607. if (this.isSerifFont(name)) {
  2608. lookupName = "Times-Roman";
  2609. } else {
  2610. lookupName = "Helvetica";
  2611. }
  2612. }
  2613. const glyphWidths = Metrics[lookupName];
  2614. if ((0, _util.isNum)(glyphWidths)) {
  2615. defaultWidth = glyphWidths;
  2616. monospace = true;
  2617. } else {
  2618. widths = glyphWidths();
  2619. }
  2620. return {
  2621. defaultWidth,
  2622. monospace,
  2623. widths
  2624. };
  2625. }
  2626. buildCharCodeToWidth(widthsByGlyphName, properties) {
  2627. const widths = Object.create(null);
  2628. const differences = properties.differences;
  2629. const encoding = properties.defaultEncoding;
  2630. for (let charCode = 0; charCode < 256; charCode++) {
  2631. if (charCode in differences && widthsByGlyphName[differences[charCode]]) {
  2632. widths[charCode] = widthsByGlyphName[differences[charCode]];
  2633. continue;
  2634. }
  2635. if (charCode in encoding && widthsByGlyphName[encoding[charCode]]) {
  2636. widths[charCode] = widthsByGlyphName[encoding[charCode]];
  2637. continue;
  2638. }
  2639. }
  2640. return widths;
  2641. }
  2642. preEvaluateFont(dict) {
  2643. const baseDict = dict;
  2644. let type = dict.get("Subtype");
  2645. if (!(0, _primitives.isName)(type)) {
  2646. throw new _util.FormatError("invalid font Subtype");
  2647. }
  2648. let composite = false;
  2649. let hash, toUnicode;
  2650. if (type.name === "Type0") {
  2651. const df = dict.get("DescendantFonts");
  2652. if (!df) {
  2653. throw new _util.FormatError("Descendant fonts are not specified");
  2654. }
  2655. dict = Array.isArray(df) ? this.xref.fetchIfRef(df[0]) : df;
  2656. if (!(dict instanceof _primitives.Dict)) {
  2657. throw new _util.FormatError("Descendant font is not a dictionary.");
  2658. }
  2659. type = dict.get("Subtype");
  2660. if (!(0, _primitives.isName)(type)) {
  2661. throw new _util.FormatError("invalid font Subtype");
  2662. }
  2663. composite = true;
  2664. }
  2665. const firstChar = dict.get("FirstChar") || 0,
  2666. lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff);
  2667. const descriptor = dict.get("FontDescriptor");
  2668. if (descriptor) {
  2669. hash = new _murmurhash.MurmurHash3_64();
  2670. const encoding = baseDict.getRaw("Encoding");
  2671. if ((0, _primitives.isName)(encoding)) {
  2672. hash.update(encoding.name);
  2673. } else if ((0, _primitives.isRef)(encoding)) {
  2674. hash.update(encoding.toString());
  2675. } else if ((0, _primitives.isDict)(encoding)) {
  2676. for (const entry of encoding.getRawValues()) {
  2677. if ((0, _primitives.isName)(entry)) {
  2678. hash.update(entry.name);
  2679. } else if ((0, _primitives.isRef)(entry)) {
  2680. hash.update(entry.toString());
  2681. } else if (Array.isArray(entry)) {
  2682. const diffLength = entry.length,
  2683. diffBuf = new Array(diffLength);
  2684. for (let j = 0; j < diffLength; j++) {
  2685. const diffEntry = entry[j];
  2686. if ((0, _primitives.isName)(diffEntry)) {
  2687. diffBuf[j] = diffEntry.name;
  2688. } else if ((0, _util.isNum)(diffEntry) || (0, _primitives.isRef)(diffEntry)) {
  2689. diffBuf[j] = diffEntry.toString();
  2690. }
  2691. }
  2692. hash.update(diffBuf.join());
  2693. }
  2694. }
  2695. }
  2696. hash.update(`${firstChar}-${lastChar}`);
  2697. toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");
  2698. if ((0, _primitives.isStream)(toUnicode)) {
  2699. const stream = toUnicode.str || toUnicode;
  2700. const uint8array = stream.buffer ? new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength) : new Uint8Array(stream.bytes.buffer, stream.start, stream.end - stream.start);
  2701. hash.update(uint8array);
  2702. } else if ((0, _primitives.isName)(toUnicode)) {
  2703. hash.update(toUnicode.name);
  2704. }
  2705. const widths = dict.get("Widths") || baseDict.get("Widths");
  2706. if (Array.isArray(widths)) {
  2707. const widthsBuf = [];
  2708. for (const entry of widths) {
  2709. if ((0, _util.isNum)(entry) || (0, _primitives.isRef)(entry)) {
  2710. widthsBuf.push(entry.toString());
  2711. }
  2712. }
  2713. hash.update(widthsBuf.join());
  2714. }
  2715. if (composite) {
  2716. hash.update("compositeFont");
  2717. const compositeWidths = dict.get("W") || baseDict.get("W");
  2718. if (Array.isArray(compositeWidths)) {
  2719. const widthsBuf = [];
  2720. for (const entry of compositeWidths) {
  2721. if ((0, _util.isNum)(entry) || (0, _primitives.isRef)(entry)) {
  2722. widthsBuf.push(entry.toString());
  2723. } else if (Array.isArray(entry)) {
  2724. const subWidthsBuf = [];
  2725. for (const element of entry) {
  2726. if ((0, _util.isNum)(element) || (0, _primitives.isRef)(element)) {
  2727. subWidthsBuf.push(element.toString());
  2728. }
  2729. }
  2730. widthsBuf.push(`[${subWidthsBuf.join()}]`);
  2731. }
  2732. }
  2733. hash.update(widthsBuf.join());
  2734. }
  2735. }
  2736. }
  2737. return {
  2738. descriptor,
  2739. dict,
  2740. baseDict,
  2741. composite,
  2742. type: type.name,
  2743. firstChar,
  2744. lastChar,
  2745. toUnicode,
  2746. hash: hash ? hash.hexdigest() : ""
  2747. };
  2748. }
  2749. async translateFont({
  2750. descriptor,
  2751. dict,
  2752. baseDict,
  2753. composite,
  2754. type,
  2755. firstChar,
  2756. lastChar,
  2757. toUnicode,
  2758. cssFontInfo
  2759. }) {
  2760. const isType3Font = type === "Type3";
  2761. let properties;
  2762. if (!descriptor) {
  2763. if (isType3Font) {
  2764. descriptor = new _primitives.Dict(null);
  2765. descriptor.set("FontName", _primitives.Name.get(type));
  2766. descriptor.set("FontBBox", dict.getArray("FontBBox") || [0, 0, 0, 0]);
  2767. } else {
  2768. let baseFontName = dict.get("BaseFont");
  2769. if (!(0, _primitives.isName)(baseFontName)) {
  2770. throw new _util.FormatError("Base font is not specified");
  2771. }
  2772. baseFontName = baseFontName.name.replace(/[,_]/g, "-");
  2773. const metrics = this.getBaseFontMetrics(baseFontName);
  2774. const fontNameWoStyle = baseFontName.split("-")[0];
  2775. 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);
  2776. properties = {
  2777. type,
  2778. name: baseFontName,
  2779. loadedName: baseDict.loadedName,
  2780. widths: metrics.widths,
  2781. defaultWidth: metrics.defaultWidth,
  2782. flags,
  2783. firstChar,
  2784. lastChar,
  2785. toUnicode,
  2786. xHeight: 0,
  2787. capHeight: 0,
  2788. italicAngle: 0,
  2789. isType3Font
  2790. };
  2791. const widths = dict.get("Widths");
  2792. const standardFontName = (0, _standard_fonts.getStandardFontName)(baseFontName);
  2793. let file = null;
  2794. if (standardFontName) {
  2795. properties.isStandardFont = true;
  2796. file = await this.fetchStandardFontData(standardFontName);
  2797. properties.isInternalFont = !!file;
  2798. }
  2799. return this.extractDataStructures(dict, dict, properties).then(newProperties => {
  2800. if (widths) {
  2801. const glyphWidths = [];
  2802. let j = firstChar;
  2803. for (let i = 0, ii = widths.length; i < ii; i++) {
  2804. glyphWidths[j++] = this.xref.fetchIfRef(widths[i]);
  2805. }
  2806. newProperties.widths = glyphWidths;
  2807. } else {
  2808. newProperties.widths = this.buildCharCodeToWidth(metrics.widths, newProperties);
  2809. }
  2810. return new _fonts.Font(baseFontName, file, newProperties);
  2811. });
  2812. }
  2813. }
  2814. let fontName = descriptor.get("FontName");
  2815. let baseFont = dict.get("BaseFont");
  2816. if ((0, _util.isString)(fontName)) {
  2817. fontName = _primitives.Name.get(fontName);
  2818. }
  2819. if ((0, _util.isString)(baseFont)) {
  2820. baseFont = _primitives.Name.get(baseFont);
  2821. }
  2822. if (!isType3Font) {
  2823. const fontNameStr = fontName && fontName.name;
  2824. const baseFontStr = baseFont && baseFont.name;
  2825. if (fontNameStr !== baseFontStr) {
  2826. (0, _util.info)(`The FontDescriptor's FontName is "${fontNameStr}" but ` + `should be the same as the Font's BaseFont "${baseFontStr}".`);
  2827. if (fontNameStr && baseFontStr && baseFontStr.startsWith(fontNameStr)) {
  2828. fontName = baseFont;
  2829. }
  2830. }
  2831. }
  2832. fontName = fontName || baseFont;
  2833. if (!(0, _primitives.isName)(fontName)) {
  2834. throw new _util.FormatError("invalid font name");
  2835. }
  2836. let fontFile, subtype, length1, length2, length3;
  2837. try {
  2838. fontFile = descriptor.get("FontFile", "FontFile2", "FontFile3");
  2839. } catch (ex) {
  2840. if (!this.options.ignoreErrors) {
  2841. throw ex;
  2842. }
  2843. (0, _util.warn)(`translateFont - fetching "${fontName.name}" font file: "${ex}".`);
  2844. fontFile = new _stream.NullStream();
  2845. }
  2846. let isStandardFont = false;
  2847. let isInternalFont = false;
  2848. let glyphScaleFactors = null;
  2849. if (fontFile) {
  2850. if (fontFile.dict) {
  2851. const subtypeEntry = fontFile.dict.get("Subtype");
  2852. if (subtypeEntry instanceof _primitives.Name) {
  2853. subtype = subtypeEntry.name;
  2854. }
  2855. length1 = fontFile.dict.get("Length1");
  2856. length2 = fontFile.dict.get("Length2");
  2857. length3 = fontFile.dict.get("Length3");
  2858. }
  2859. } else if (cssFontInfo) {
  2860. const standardFontName = (0, _xfa_fonts.getXfaFontName)(fontName.name);
  2861. if (standardFontName) {
  2862. cssFontInfo.fontFamily = `${cssFontInfo.fontFamily}-PdfJS-XFA`;
  2863. cssFontInfo.lineHeight = standardFontName.lineHeight || null;
  2864. glyphScaleFactors = standardFontName.factors || null;
  2865. fontFile = await this.fetchStandardFontData(standardFontName.name);
  2866. isInternalFont = !!fontFile;
  2867. type = "TrueType";
  2868. }
  2869. } else if (!isType3Font) {
  2870. const standardFontName = (0, _standard_fonts.getStandardFontName)(fontName.name);
  2871. if (standardFontName) {
  2872. isStandardFont = true;
  2873. fontFile = await this.fetchStandardFontData(standardFontName);
  2874. isInternalFont = !!fontFile;
  2875. }
  2876. }
  2877. properties = {
  2878. type,
  2879. name: fontName.name,
  2880. subtype,
  2881. file: fontFile,
  2882. length1,
  2883. length2,
  2884. length3,
  2885. isStandardFont,
  2886. isInternalFont,
  2887. loadedName: baseDict.loadedName,
  2888. composite,
  2889. fixedPitch: false,
  2890. fontMatrix: dict.getArray("FontMatrix") || _util.FONT_IDENTITY_MATRIX,
  2891. firstChar,
  2892. lastChar,
  2893. toUnicode,
  2894. bbox: descriptor.getArray("FontBBox") || dict.getArray("FontBBox"),
  2895. ascent: descriptor.get("Ascent"),
  2896. descent: descriptor.get("Descent"),
  2897. xHeight: descriptor.get("XHeight") || 0,
  2898. capHeight: descriptor.get("CapHeight") || 0,
  2899. flags: descriptor.get("Flags"),
  2900. italicAngle: descriptor.get("ItalicAngle") || 0,
  2901. isType3Font,
  2902. cssFontInfo,
  2903. scaleFactors: glyphScaleFactors
  2904. };
  2905. if (composite) {
  2906. const cidEncoding = baseDict.get("Encoding");
  2907. if ((0, _primitives.isName)(cidEncoding)) {
  2908. properties.cidEncoding = cidEncoding.name;
  2909. }
  2910. const cMap = await _cmap.CMapFactory.create({
  2911. encoding: cidEncoding,
  2912. fetchBuiltInCMap: this._fetchBuiltInCMapBound,
  2913. useCMap: null
  2914. });
  2915. properties.cMap = cMap;
  2916. properties.vertical = properties.cMap.vertical;
  2917. }
  2918. return this.extractDataStructures(dict, baseDict, properties).then(newProperties => {
  2919. this.extractWidths(dict, descriptor, newProperties);
  2920. return new _fonts.Font(fontName.name, fontFile, newProperties);
  2921. });
  2922. }
  2923. static buildFontPaths(font, glyphs, handler, evaluatorOptions) {
  2924. function buildPath(fontChar) {
  2925. const glyphName = `${font.loadedName}_path_${fontChar}`;
  2926. try {
  2927. if (font.renderer.hasBuiltPath(fontChar)) {
  2928. return;
  2929. }
  2930. handler.send("commonobj", [glyphName, "FontPath", font.renderer.getPathJs(fontChar)]);
  2931. } catch (reason) {
  2932. if (evaluatorOptions.ignoreErrors) {
  2933. handler.send("UnsupportedFeature", {
  2934. featureId: _util.UNSUPPORTED_FEATURES.errorFontBuildPath
  2935. });
  2936. (0, _util.warn)(`buildFontPaths - ignoring ${glyphName} glyph: "${reason}".`);
  2937. return;
  2938. }
  2939. throw reason;
  2940. }
  2941. }
  2942. for (const glyph of glyphs) {
  2943. buildPath(glyph.fontChar);
  2944. const accent = glyph.accent;
  2945. if (accent && accent.fontChar) {
  2946. buildPath(accent.fontChar);
  2947. }
  2948. }
  2949. }
  2950. static get fallbackFontDict() {
  2951. const dict = new _primitives.Dict();
  2952. dict.set("BaseFont", _primitives.Name.get("PDFJS-FallbackFont"));
  2953. dict.set("Type", _primitives.Name.get("FallbackType"));
  2954. dict.set("Subtype", _primitives.Name.get("FallbackType"));
  2955. dict.set("Encoding", _primitives.Name.get("WinAnsiEncoding"));
  2956. return (0, _util.shadow)(this, "fallbackFontDict", dict);
  2957. }
  2958. }
  2959. exports.PartialEvaluator = PartialEvaluator;
  2960. class TranslatedFont {
  2961. constructor({
  2962. loadedName,
  2963. font,
  2964. dict,
  2965. evaluatorOptions
  2966. }) {
  2967. this.loadedName = loadedName;
  2968. this.font = font;
  2969. this.dict = dict;
  2970. this._evaluatorOptions = evaluatorOptions || DefaultPartialEvaluatorOptions;
  2971. this.type3Loaded = null;
  2972. this.type3Dependencies = font.isType3Font ? new Set() : null;
  2973. this.sent = false;
  2974. }
  2975. send(handler) {
  2976. if (this.sent) {
  2977. return;
  2978. }
  2979. this.sent = true;
  2980. handler.send("commonobj", [this.loadedName, "Font", this.font.exportData(this._evaluatorOptions.fontExtraProperties)]);
  2981. }
  2982. fallback(handler) {
  2983. if (!this.font.data) {
  2984. return;
  2985. }
  2986. this.font.disableFontFace = true;
  2987. PartialEvaluator.buildFontPaths(this.font, this.font.glyphCacheValues, handler, this._evaluatorOptions);
  2988. }
  2989. loadType3Data(evaluator, resources, task) {
  2990. if (this.type3Loaded) {
  2991. return this.type3Loaded;
  2992. }
  2993. if (!this.font.isType3Font) {
  2994. throw new Error("Must be a Type3 font.");
  2995. }
  2996. const type3Evaluator = evaluator.clone({
  2997. ignoreErrors: false
  2998. });
  2999. type3Evaluator.parsingType3Font = true;
  3000. const translatedFont = this.font,
  3001. type3Dependencies = this.type3Dependencies;
  3002. let loadCharProcsPromise = Promise.resolve();
  3003. const charProcs = this.dict.get("CharProcs");
  3004. const fontResources = this.dict.get("Resources") || resources;
  3005. const charProcOperatorList = Object.create(null);
  3006. const isEmptyBBox = !translatedFont.bbox || (0, _util.isArrayEqual)(translatedFont.bbox, [0, 0, 0, 0]);
  3007. for (const key of charProcs.getKeys()) {
  3008. loadCharProcsPromise = loadCharProcsPromise.then(() => {
  3009. const glyphStream = charProcs.get(key);
  3010. const operatorList = new _operator_list.OperatorList();
  3011. return type3Evaluator.getOperatorList({
  3012. stream: glyphStream,
  3013. task,
  3014. resources: fontResources,
  3015. operatorList
  3016. }).then(() => {
  3017. if (operatorList.fnArray[0] === _util.OPS.setCharWidthAndBounds) {
  3018. this._removeType3ColorOperators(operatorList, isEmptyBBox);
  3019. }
  3020. charProcOperatorList[key] = operatorList.getIR();
  3021. for (const dependency of operatorList.dependencies) {
  3022. type3Dependencies.add(dependency);
  3023. }
  3024. }).catch(function (reason) {
  3025. (0, _util.warn)(`Type3 font resource "${key}" is not available.`);
  3026. const dummyOperatorList = new _operator_list.OperatorList();
  3027. charProcOperatorList[key] = dummyOperatorList.getIR();
  3028. });
  3029. });
  3030. }
  3031. this.type3Loaded = loadCharProcsPromise.then(() => {
  3032. translatedFont.charProcOperatorList = charProcOperatorList;
  3033. if (this._bbox) {
  3034. translatedFont.isCharBBox = true;
  3035. translatedFont.bbox = this._bbox;
  3036. }
  3037. });
  3038. return this.type3Loaded;
  3039. }
  3040. _removeType3ColorOperators(operatorList, isEmptyBBox = false) {
  3041. if (isEmptyBBox) {
  3042. if (!this._bbox) {
  3043. this._bbox = [Infinity, Infinity, -Infinity, -Infinity];
  3044. }
  3045. const charBBox = _util.Util.normalizeRect(operatorList.argsArray[0].slice(2));
  3046. this._bbox[0] = Math.min(this._bbox[0], charBBox[0]);
  3047. this._bbox[1] = Math.min(this._bbox[1], charBBox[1]);
  3048. this._bbox[2] = Math.max(this._bbox[2], charBBox[2]);
  3049. this._bbox[3] = Math.max(this._bbox[3], charBBox[3]);
  3050. }
  3051. let i = 1,
  3052. ii = operatorList.length;
  3053. while (i < ii) {
  3054. switch (operatorList.fnArray[i]) {
  3055. case _util.OPS.setStrokeColorSpace:
  3056. case _util.OPS.setFillColorSpace:
  3057. case _util.OPS.setStrokeColor:
  3058. case _util.OPS.setStrokeColorN:
  3059. case _util.OPS.setFillColor:
  3060. case _util.OPS.setFillColorN:
  3061. case _util.OPS.setStrokeGray:
  3062. case _util.OPS.setFillGray:
  3063. case _util.OPS.setStrokeRGBColor:
  3064. case _util.OPS.setFillRGBColor:
  3065. case _util.OPS.setStrokeCMYKColor:
  3066. case _util.OPS.setFillCMYKColor:
  3067. case _util.OPS.shadingFill:
  3068. case _util.OPS.setRenderingIntent:
  3069. operatorList.fnArray.splice(i, 1);
  3070. operatorList.argsArray.splice(i, 1);
  3071. ii--;
  3072. continue;
  3073. case _util.OPS.setGState:
  3074. const [gStateObj] = operatorList.argsArray[i];
  3075. let j = 0,
  3076. jj = gStateObj.length;
  3077. while (j < jj) {
  3078. const [gStateKey] = gStateObj[j];
  3079. switch (gStateKey) {
  3080. case "TR":
  3081. case "TR2":
  3082. case "HT":
  3083. case "BG":
  3084. case "BG2":
  3085. case "UCR":
  3086. case "UCR2":
  3087. gStateObj.splice(j, 1);
  3088. jj--;
  3089. continue;
  3090. }
  3091. j++;
  3092. }
  3093. break;
  3094. }
  3095. i++;
  3096. }
  3097. }
  3098. }
  3099. class StateManager {
  3100. constructor(initialState = new EvalState()) {
  3101. this.state = initialState;
  3102. this.stateStack = [];
  3103. }
  3104. save() {
  3105. const old = this.state;
  3106. this.stateStack.push(this.state);
  3107. this.state = old.clone();
  3108. }
  3109. restore() {
  3110. const prev = this.stateStack.pop();
  3111. if (prev) {
  3112. this.state = prev;
  3113. }
  3114. }
  3115. transform(args) {
  3116. this.state.ctm = _util.Util.transform(this.state.ctm, args);
  3117. }
  3118. }
  3119. class TextState {
  3120. constructor() {
  3121. this.ctm = new Float32Array(_util.IDENTITY_MATRIX);
  3122. this.fontName = null;
  3123. this.fontSize = 0;
  3124. this.font = null;
  3125. this.fontMatrix = _util.FONT_IDENTITY_MATRIX;
  3126. this.textMatrix = _util.IDENTITY_MATRIX.slice();
  3127. this.textLineMatrix = _util.IDENTITY_MATRIX.slice();
  3128. this.charSpacing = 0;
  3129. this.wordSpacing = 0;
  3130. this.leading = 0;
  3131. this.textHScale = 1;
  3132. this.textRise = 0;
  3133. }
  3134. setTextMatrix(a, b, c, d, e, f) {
  3135. const m = this.textMatrix;
  3136. m[0] = a;
  3137. m[1] = b;
  3138. m[2] = c;
  3139. m[3] = d;
  3140. m[4] = e;
  3141. m[5] = f;
  3142. }
  3143. setTextLineMatrix(a, b, c, d, e, f) {
  3144. const m = this.textLineMatrix;
  3145. m[0] = a;
  3146. m[1] = b;
  3147. m[2] = c;
  3148. m[3] = d;
  3149. m[4] = e;
  3150. m[5] = f;
  3151. }
  3152. translateTextMatrix(x, y) {
  3153. const m = this.textMatrix;
  3154. m[4] = m[0] * x + m[2] * y + m[4];
  3155. m[5] = m[1] * x + m[3] * y + m[5];
  3156. }
  3157. translateTextLineMatrix(x, y) {
  3158. const m = this.textLineMatrix;
  3159. m[4] = m[0] * x + m[2] * y + m[4];
  3160. m[5] = m[1] * x + m[3] * y + m[5];
  3161. }
  3162. carriageReturn() {
  3163. this.translateTextLineMatrix(0, -this.leading);
  3164. this.textMatrix = this.textLineMatrix.slice();
  3165. }
  3166. clone() {
  3167. const clone = Object.create(this);
  3168. clone.textMatrix = this.textMatrix.slice();
  3169. clone.textLineMatrix = this.textLineMatrix.slice();
  3170. clone.fontMatrix = this.fontMatrix.slice();
  3171. return clone;
  3172. }
  3173. }
  3174. class EvalState {
  3175. constructor() {
  3176. this.ctm = new Float32Array(_util.IDENTITY_MATRIX);
  3177. this.font = null;
  3178. this.textRenderingMode = _util.TextRenderingMode.FILL;
  3179. this.fillColorSpace = _colorspace.ColorSpace.singletons.gray;
  3180. this.strokeColorSpace = _colorspace.ColorSpace.singletons.gray;
  3181. }
  3182. clone() {
  3183. return Object.create(this);
  3184. }
  3185. }
  3186. class EvaluatorPreprocessor {
  3187. static get opMap() {
  3188. const getOPMap = (0, _core_utils.getLookupTableFactory)(function (t) {
  3189. t.w = {
  3190. id: _util.OPS.setLineWidth,
  3191. numArgs: 1,
  3192. variableArgs: false
  3193. };
  3194. t.J = {
  3195. id: _util.OPS.setLineCap,
  3196. numArgs: 1,
  3197. variableArgs: false
  3198. };
  3199. t.j = {
  3200. id: _util.OPS.setLineJoin,
  3201. numArgs: 1,
  3202. variableArgs: false
  3203. };
  3204. t.M = {
  3205. id: _util.OPS.setMiterLimit,
  3206. numArgs: 1,
  3207. variableArgs: false
  3208. };
  3209. t.d = {
  3210. id: _util.OPS.setDash,
  3211. numArgs: 2,
  3212. variableArgs: false
  3213. };
  3214. t.ri = {
  3215. id: _util.OPS.setRenderingIntent,
  3216. numArgs: 1,
  3217. variableArgs: false
  3218. };
  3219. t.i = {
  3220. id: _util.OPS.setFlatness,
  3221. numArgs: 1,
  3222. variableArgs: false
  3223. };
  3224. t.gs = {
  3225. id: _util.OPS.setGState,
  3226. numArgs: 1,
  3227. variableArgs: false
  3228. };
  3229. t.q = {
  3230. id: _util.OPS.save,
  3231. numArgs: 0,
  3232. variableArgs: false
  3233. };
  3234. t.Q = {
  3235. id: _util.OPS.restore,
  3236. numArgs: 0,
  3237. variableArgs: false
  3238. };
  3239. t.cm = {
  3240. id: _util.OPS.transform,
  3241. numArgs: 6,
  3242. variableArgs: false
  3243. };
  3244. t.m = {
  3245. id: _util.OPS.moveTo,
  3246. numArgs: 2,
  3247. variableArgs: false
  3248. };
  3249. t.l = {
  3250. id: _util.OPS.lineTo,
  3251. numArgs: 2,
  3252. variableArgs: false
  3253. };
  3254. t.c = {
  3255. id: _util.OPS.curveTo,
  3256. numArgs: 6,
  3257. variableArgs: false
  3258. };
  3259. t.v = {
  3260. id: _util.OPS.curveTo2,
  3261. numArgs: 4,
  3262. variableArgs: false
  3263. };
  3264. t.y = {
  3265. id: _util.OPS.curveTo3,
  3266. numArgs: 4,
  3267. variableArgs: false
  3268. };
  3269. t.h = {
  3270. id: _util.OPS.closePath,
  3271. numArgs: 0,
  3272. variableArgs: false
  3273. };
  3274. t.re = {
  3275. id: _util.OPS.rectangle,
  3276. numArgs: 4,
  3277. variableArgs: false
  3278. };
  3279. t.S = {
  3280. id: _util.OPS.stroke,
  3281. numArgs: 0,
  3282. variableArgs: false
  3283. };
  3284. t.s = {
  3285. id: _util.OPS.closeStroke,
  3286. numArgs: 0,
  3287. variableArgs: false
  3288. };
  3289. t.f = {
  3290. id: _util.OPS.fill,
  3291. numArgs: 0,
  3292. variableArgs: false
  3293. };
  3294. t.F = {
  3295. id: _util.OPS.fill,
  3296. numArgs: 0,
  3297. variableArgs: false
  3298. };
  3299. t["f*"] = {
  3300. id: _util.OPS.eoFill,
  3301. numArgs: 0,
  3302. variableArgs: false
  3303. };
  3304. t.B = {
  3305. id: _util.OPS.fillStroke,
  3306. numArgs: 0,
  3307. variableArgs: false
  3308. };
  3309. t["B*"] = {
  3310. id: _util.OPS.eoFillStroke,
  3311. numArgs: 0,
  3312. variableArgs: false
  3313. };
  3314. t.b = {
  3315. id: _util.OPS.closeFillStroke,
  3316. numArgs: 0,
  3317. variableArgs: false
  3318. };
  3319. t["b*"] = {
  3320. id: _util.OPS.closeEOFillStroke,
  3321. numArgs: 0,
  3322. variableArgs: false
  3323. };
  3324. t.n = {
  3325. id: _util.OPS.endPath,
  3326. numArgs: 0,
  3327. variableArgs: false
  3328. };
  3329. t.W = {
  3330. id: _util.OPS.clip,
  3331. numArgs: 0,
  3332. variableArgs: false
  3333. };
  3334. t["W*"] = {
  3335. id: _util.OPS.eoClip,
  3336. numArgs: 0,
  3337. variableArgs: false
  3338. };
  3339. t.BT = {
  3340. id: _util.OPS.beginText,
  3341. numArgs: 0,
  3342. variableArgs: false
  3343. };
  3344. t.ET = {
  3345. id: _util.OPS.endText,
  3346. numArgs: 0,
  3347. variableArgs: false
  3348. };
  3349. t.Tc = {
  3350. id: _util.OPS.setCharSpacing,
  3351. numArgs: 1,
  3352. variableArgs: false
  3353. };
  3354. t.Tw = {
  3355. id: _util.OPS.setWordSpacing,
  3356. numArgs: 1,
  3357. variableArgs: false
  3358. };
  3359. t.Tz = {
  3360. id: _util.OPS.setHScale,
  3361. numArgs: 1,
  3362. variableArgs: false
  3363. };
  3364. t.TL = {
  3365. id: _util.OPS.setLeading,
  3366. numArgs: 1,
  3367. variableArgs: false
  3368. };
  3369. t.Tf = {
  3370. id: _util.OPS.setFont,
  3371. numArgs: 2,
  3372. variableArgs: false
  3373. };
  3374. t.Tr = {
  3375. id: _util.OPS.setTextRenderingMode,
  3376. numArgs: 1,
  3377. variableArgs: false
  3378. };
  3379. t.Ts = {
  3380. id: _util.OPS.setTextRise,
  3381. numArgs: 1,
  3382. variableArgs: false
  3383. };
  3384. t.Td = {
  3385. id: _util.OPS.moveText,
  3386. numArgs: 2,
  3387. variableArgs: false
  3388. };
  3389. t.TD = {
  3390. id: _util.OPS.setLeadingMoveText,
  3391. numArgs: 2,
  3392. variableArgs: false
  3393. };
  3394. t.Tm = {
  3395. id: _util.OPS.setTextMatrix,
  3396. numArgs: 6,
  3397. variableArgs: false
  3398. };
  3399. t["T*"] = {
  3400. id: _util.OPS.nextLine,
  3401. numArgs: 0,
  3402. variableArgs: false
  3403. };
  3404. t.Tj = {
  3405. id: _util.OPS.showText,
  3406. numArgs: 1,
  3407. variableArgs: false
  3408. };
  3409. t.TJ = {
  3410. id: _util.OPS.showSpacedText,
  3411. numArgs: 1,
  3412. variableArgs: false
  3413. };
  3414. t["'"] = {
  3415. id: _util.OPS.nextLineShowText,
  3416. numArgs: 1,
  3417. variableArgs: false
  3418. };
  3419. t['"'] = {
  3420. id: _util.OPS.nextLineSetSpacingShowText,
  3421. numArgs: 3,
  3422. variableArgs: false
  3423. };
  3424. t.d0 = {
  3425. id: _util.OPS.setCharWidth,
  3426. numArgs: 2,
  3427. variableArgs: false
  3428. };
  3429. t.d1 = {
  3430. id: _util.OPS.setCharWidthAndBounds,
  3431. numArgs: 6,
  3432. variableArgs: false
  3433. };
  3434. t.CS = {
  3435. id: _util.OPS.setStrokeColorSpace,
  3436. numArgs: 1,
  3437. variableArgs: false
  3438. };
  3439. t.cs = {
  3440. id: _util.OPS.setFillColorSpace,
  3441. numArgs: 1,
  3442. variableArgs: false
  3443. };
  3444. t.SC = {
  3445. id: _util.OPS.setStrokeColor,
  3446. numArgs: 4,
  3447. variableArgs: true
  3448. };
  3449. t.SCN = {
  3450. id: _util.OPS.setStrokeColorN,
  3451. numArgs: 33,
  3452. variableArgs: true
  3453. };
  3454. t.sc = {
  3455. id: _util.OPS.setFillColor,
  3456. numArgs: 4,
  3457. variableArgs: true
  3458. };
  3459. t.scn = {
  3460. id: _util.OPS.setFillColorN,
  3461. numArgs: 33,
  3462. variableArgs: true
  3463. };
  3464. t.G = {
  3465. id: _util.OPS.setStrokeGray,
  3466. numArgs: 1,
  3467. variableArgs: false
  3468. };
  3469. t.g = {
  3470. id: _util.OPS.setFillGray,
  3471. numArgs: 1,
  3472. variableArgs: false
  3473. };
  3474. t.RG = {
  3475. id: _util.OPS.setStrokeRGBColor,
  3476. numArgs: 3,
  3477. variableArgs: false
  3478. };
  3479. t.rg = {
  3480. id: _util.OPS.setFillRGBColor,
  3481. numArgs: 3,
  3482. variableArgs: false
  3483. };
  3484. t.K = {
  3485. id: _util.OPS.setStrokeCMYKColor,
  3486. numArgs: 4,
  3487. variableArgs: false
  3488. };
  3489. t.k = {
  3490. id: _util.OPS.setFillCMYKColor,
  3491. numArgs: 4,
  3492. variableArgs: false
  3493. };
  3494. t.sh = {
  3495. id: _util.OPS.shadingFill,
  3496. numArgs: 1,
  3497. variableArgs: false
  3498. };
  3499. t.BI = {
  3500. id: _util.OPS.beginInlineImage,
  3501. numArgs: 0,
  3502. variableArgs: false
  3503. };
  3504. t.ID = {
  3505. id: _util.OPS.beginImageData,
  3506. numArgs: 0,
  3507. variableArgs: false
  3508. };
  3509. t.EI = {
  3510. id: _util.OPS.endInlineImage,
  3511. numArgs: 1,
  3512. variableArgs: false
  3513. };
  3514. t.Do = {
  3515. id: _util.OPS.paintXObject,
  3516. numArgs: 1,
  3517. variableArgs: false
  3518. };
  3519. t.MP = {
  3520. id: _util.OPS.markPoint,
  3521. numArgs: 1,
  3522. variableArgs: false
  3523. };
  3524. t.DP = {
  3525. id: _util.OPS.markPointProps,
  3526. numArgs: 2,
  3527. variableArgs: false
  3528. };
  3529. t.BMC = {
  3530. id: _util.OPS.beginMarkedContent,
  3531. numArgs: 1,
  3532. variableArgs: false
  3533. };
  3534. t.BDC = {
  3535. id: _util.OPS.beginMarkedContentProps,
  3536. numArgs: 2,
  3537. variableArgs: false
  3538. };
  3539. t.EMC = {
  3540. id: _util.OPS.endMarkedContent,
  3541. numArgs: 0,
  3542. variableArgs: false
  3543. };
  3544. t.BX = {
  3545. id: _util.OPS.beginCompat,
  3546. numArgs: 0,
  3547. variableArgs: false
  3548. };
  3549. t.EX = {
  3550. id: _util.OPS.endCompat,
  3551. numArgs: 0,
  3552. variableArgs: false
  3553. };
  3554. t.BM = null;
  3555. t.BD = null;
  3556. t.true = null;
  3557. t.fa = null;
  3558. t.fal = null;
  3559. t.fals = null;
  3560. t.false = null;
  3561. t.nu = null;
  3562. t.nul = null;
  3563. t.null = null;
  3564. });
  3565. return (0, _util.shadow)(this, "opMap", getOPMap());
  3566. }
  3567. static get MAX_INVALID_PATH_OPS() {
  3568. return (0, _util.shadow)(this, "MAX_INVALID_PATH_OPS", 20);
  3569. }
  3570. constructor(stream, xref, stateManager = new StateManager()) {
  3571. this.parser = new _parser.Parser({
  3572. lexer: new _parser.Lexer(stream, EvaluatorPreprocessor.opMap),
  3573. xref
  3574. });
  3575. this.stateManager = stateManager;
  3576. this.nonProcessedArgs = [];
  3577. this._numInvalidPathOPS = 0;
  3578. }
  3579. get savedStatesDepth() {
  3580. return this.stateManager.stateStack.length;
  3581. }
  3582. read(operation) {
  3583. let args = operation.args;
  3584. while (true) {
  3585. const obj = this.parser.getObj();
  3586. if (obj instanceof _primitives.Cmd) {
  3587. const cmd = obj.cmd;
  3588. const opSpec = EvaluatorPreprocessor.opMap[cmd];
  3589. if (!opSpec) {
  3590. (0, _util.warn)(`Unknown command "${cmd}".`);
  3591. continue;
  3592. }
  3593. const fn = opSpec.id;
  3594. const numArgs = opSpec.numArgs;
  3595. let argsLength = args !== null ? args.length : 0;
  3596. if (!opSpec.variableArgs) {
  3597. if (argsLength !== numArgs) {
  3598. const nonProcessedArgs = this.nonProcessedArgs;
  3599. while (argsLength > numArgs) {
  3600. nonProcessedArgs.push(args.shift());
  3601. argsLength--;
  3602. }
  3603. while (argsLength < numArgs && nonProcessedArgs.length !== 0) {
  3604. if (args === null) {
  3605. args = [];
  3606. }
  3607. args.unshift(nonProcessedArgs.pop());
  3608. argsLength++;
  3609. }
  3610. }
  3611. if (argsLength < numArgs) {
  3612. const partialMsg = `command ${cmd}: expected ${numArgs} args, ` + `but received ${argsLength} args.`;
  3613. if (fn >= _util.OPS.moveTo && fn <= _util.OPS.endPath && ++this._numInvalidPathOPS > EvaluatorPreprocessor.MAX_INVALID_PATH_OPS) {
  3614. throw new _util.FormatError(`Invalid ${partialMsg}`);
  3615. }
  3616. (0, _util.warn)(`Skipping ${partialMsg}`);
  3617. if (args !== null) {
  3618. args.length = 0;
  3619. }
  3620. continue;
  3621. }
  3622. } else if (argsLength > numArgs) {
  3623. (0, _util.info)(`Command ${cmd}: expected [0, ${numArgs}] args, ` + `but received ${argsLength} args.`);
  3624. }
  3625. this.preprocessCommand(fn, args);
  3626. operation.fn = fn;
  3627. operation.args = args;
  3628. return true;
  3629. }
  3630. if (obj === _primitives.EOF) {
  3631. return false;
  3632. }
  3633. if (obj !== null) {
  3634. if (args === null) {
  3635. args = [];
  3636. }
  3637. args.push(obj);
  3638. if (args.length > 33) {
  3639. throw new _util.FormatError("Too many arguments");
  3640. }
  3641. }
  3642. }
  3643. }
  3644. preprocessCommand(fn, args) {
  3645. switch (fn | 0) {
  3646. case _util.OPS.save:
  3647. this.stateManager.save();
  3648. break;
  3649. case _util.OPS.restore:
  3650. this.stateManager.restore();
  3651. break;
  3652. case _util.OPS.transform:
  3653. this.stateManager.transform(args);
  3654. break;
  3655. }
  3656. }
  3657. }
  3658. exports.EvaluatorPreprocessor = EvaluatorPreprocessor;