pdfinterp.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. import logging
  2. import re
  3. from io import BytesIO
  4. from typing import Dict, List, Mapping, Optional, Sequence, Tuple, Union, cast
  5. from pdfminer import settings
  6. from pdfminer.casting import safe_cmyk, safe_float, safe_int, safe_matrix, safe_rgb
  7. from pdfminer.cmapdb import CMap, CMapBase, CMapDB
  8. from pdfminer.pdfcolor import PREDEFINED_COLORSPACE, PDFColorSpace
  9. from pdfminer.pdfdevice import PDFDevice, PDFTextSeq
  10. from pdfminer.pdfexceptions import PDFException, PDFValueError
  11. from pdfminer.pdffont import (
  12. PDFCIDFont,
  13. PDFFont,
  14. PDFFontError,
  15. PDFTrueTypeFont,
  16. PDFType1Font,
  17. PDFType3Font,
  18. )
  19. from pdfminer.pdfpage import PDFPage
  20. from pdfminer.pdftypes import (
  21. LITERALS_ASCII85_DECODE,
  22. PDFObjRef,
  23. PDFStream,
  24. dict_value,
  25. list_value,
  26. resolve1,
  27. stream_value,
  28. )
  29. from pdfminer.psexceptions import PSEOF, PSTypeError
  30. from pdfminer.psparser import (
  31. KWD,
  32. LIT,
  33. PSKeyword,
  34. PSLiteral,
  35. PSStackParser,
  36. PSStackType,
  37. keyword_name,
  38. literal_name,
  39. )
  40. from pdfminer.utils import (
  41. MATRIX_IDENTITY,
  42. Matrix,
  43. PathSegment,
  44. Point,
  45. Rect,
  46. choplist,
  47. mult_matrix,
  48. )
  49. log = logging.getLogger(__name__)
  50. class PDFResourceError(PDFException):
  51. pass
  52. class PDFInterpreterError(PDFException):
  53. pass
  54. LITERAL_PDF = LIT("PDF")
  55. LITERAL_TEXT = LIT("Text")
  56. LITERAL_FONT = LIT("Font")
  57. LITERAL_FORM = LIT("Form")
  58. LITERAL_IMAGE = LIT("Image")
  59. class PDFTextState:
  60. matrix: Matrix
  61. linematrix: Point
  62. def __init__(self) -> None:
  63. self.font: Optional[PDFFont] = None
  64. self.fontsize: float = 0
  65. self.charspace: float = 0
  66. self.wordspace: float = 0
  67. self.scaling: float = 100
  68. self.leading: float = 0
  69. self.render: int = 0
  70. self.rise: float = 0
  71. self.reset()
  72. # self.matrix is set
  73. # self.linematrix is set
  74. def __repr__(self) -> str:
  75. return (
  76. "<PDFTextState: font=%r, fontsize=%r, charspace=%r, "
  77. "wordspace=%r, scaling=%r, leading=%r, render=%r, rise=%r, "
  78. "matrix=%r, linematrix=%r>"
  79. % (
  80. self.font,
  81. self.fontsize,
  82. self.charspace,
  83. self.wordspace,
  84. self.scaling,
  85. self.leading,
  86. self.render,
  87. self.rise,
  88. self.matrix,
  89. self.linematrix,
  90. )
  91. )
  92. def copy(self) -> "PDFTextState":
  93. obj = PDFTextState()
  94. obj.font = self.font
  95. obj.fontsize = self.fontsize
  96. obj.charspace = self.charspace
  97. obj.wordspace = self.wordspace
  98. obj.scaling = self.scaling
  99. obj.leading = self.leading
  100. obj.render = self.render
  101. obj.rise = self.rise
  102. obj.matrix = self.matrix
  103. obj.linematrix = self.linematrix
  104. return obj
  105. def reset(self) -> None:
  106. self.matrix = MATRIX_IDENTITY
  107. self.linematrix = (0, 0)
  108. Color = Union[
  109. float, # Greyscale
  110. Tuple[float, float, float], # R, G, B
  111. Tuple[float, float, float, float], # C, M, Y, K
  112. ]
  113. class PDFGraphicState:
  114. def __init__(self) -> None:
  115. self.linewidth: float = 0
  116. self.linecap: Optional[object] = None
  117. self.linejoin: Optional[object] = None
  118. self.miterlimit: Optional[object] = None
  119. self.dash: Optional[Tuple[object, object]] = None
  120. self.intent: Optional[object] = None
  121. self.flatness: Optional[object] = None
  122. # stroking color
  123. self.scolor: Color = 0
  124. self.scs: PDFColorSpace = PREDEFINED_COLORSPACE["DeviceGray"]
  125. # non stroking color
  126. self.ncolor: Color = 0
  127. self.ncs: PDFColorSpace = PREDEFINED_COLORSPACE["DeviceGray"]
  128. def copy(self) -> "PDFGraphicState":
  129. obj = PDFGraphicState()
  130. obj.linewidth = self.linewidth
  131. obj.linecap = self.linecap
  132. obj.linejoin = self.linejoin
  133. obj.miterlimit = self.miterlimit
  134. obj.dash = self.dash
  135. obj.intent = self.intent
  136. obj.flatness = self.flatness
  137. obj.scolor = self.scolor
  138. obj.ncolor = self.ncolor
  139. return obj
  140. def __repr__(self) -> str:
  141. return (
  142. "<PDFGraphicState: linewidth=%r, linecap=%r, linejoin=%r, "
  143. " miterlimit=%r, dash=%r, intent=%r, flatness=%r, "
  144. " stroking color=%r, non stroking color=%r>"
  145. % (
  146. self.linewidth,
  147. self.linecap,
  148. self.linejoin,
  149. self.miterlimit,
  150. self.dash,
  151. self.intent,
  152. self.flatness,
  153. self.scolor,
  154. self.ncolor,
  155. )
  156. )
  157. class PDFResourceManager:
  158. """Repository of shared resources.
  159. ResourceManager facilitates reuse of shared resources
  160. such as fonts and images so that large objects are not
  161. allocated multiple times.
  162. """
  163. def __init__(self, caching: bool = True) -> None:
  164. self.caching = caching
  165. self._cached_fonts: Dict[object, PDFFont] = {}
  166. def get_procset(self, procs: Sequence[object]) -> None:
  167. for proc in procs:
  168. if proc is LITERAL_PDF or proc is LITERAL_TEXT:
  169. pass
  170. else:
  171. pass
  172. def get_cmap(self, cmapname: str, strict: bool = False) -> CMapBase:
  173. try:
  174. return CMapDB.get_cmap(cmapname)
  175. except CMapDB.CMapNotFound:
  176. if strict:
  177. raise
  178. return CMap()
  179. def get_font(self, objid: object, spec: Mapping[str, object]) -> PDFFont:
  180. if objid and objid in self._cached_fonts:
  181. font = self._cached_fonts[objid]
  182. else:
  183. log.debug("get_font: create: objid=%r, spec=%r", objid, spec)
  184. if settings.STRICT:
  185. if spec["Type"] is not LITERAL_FONT:
  186. raise PDFFontError("Type is not /Font")
  187. # Create a Font object.
  188. if "Subtype" in spec:
  189. subtype = literal_name(spec["Subtype"])
  190. else:
  191. if settings.STRICT:
  192. raise PDFFontError("Font Subtype is not specified.")
  193. subtype = "Type1"
  194. if subtype in ("Type1", "MMType1"):
  195. # Type1 Font
  196. font = PDFType1Font(self, spec)
  197. elif subtype == "TrueType":
  198. # TrueType Font
  199. font = PDFTrueTypeFont(self, spec)
  200. elif subtype == "Type3":
  201. # Type3 Font
  202. font = PDFType3Font(self, spec)
  203. elif subtype in ("CIDFontType0", "CIDFontType2"):
  204. # CID Font
  205. font = PDFCIDFont(self, spec)
  206. elif subtype == "Type0":
  207. # Type0 Font
  208. dfonts = list_value(spec["DescendantFonts"])
  209. assert dfonts
  210. subspec = dict_value(dfonts[0]).copy()
  211. for k in ("Encoding", "ToUnicode"):
  212. if k in spec:
  213. subspec[k] = resolve1(spec[k])
  214. font = self.get_font(None, subspec)
  215. else:
  216. if settings.STRICT:
  217. raise PDFFontError("Invalid Font spec: %r" % spec)
  218. font = PDFType1Font(self, spec) # this is so wrong!
  219. if objid and self.caching:
  220. self._cached_fonts[objid] = font
  221. return font
  222. class PDFContentParser(PSStackParser[Union[PSKeyword, PDFStream]]):
  223. def __init__(self, streams: Sequence[object]) -> None:
  224. self.streams = streams
  225. self.istream = 0
  226. # PSStackParser.__init__(fp=None) is safe only because we've overloaded
  227. # all the methods that would attempt to access self.fp without first
  228. # calling self.fillfp().
  229. PSStackParser.__init__(self, None) # type: ignore[arg-type]
  230. def fillfp(self) -> None:
  231. if not self.fp:
  232. if self.istream < len(self.streams):
  233. strm = stream_value(self.streams[self.istream])
  234. self.istream += 1
  235. else:
  236. raise PSEOF("Unexpected EOF, file truncated?")
  237. self.fp = BytesIO(strm.get_data())
  238. def seek(self, pos: int) -> None:
  239. self.fillfp()
  240. PSStackParser.seek(self, pos)
  241. def fillbuf(self) -> None:
  242. if self.charpos < len(self.buf):
  243. return
  244. while 1:
  245. self.fillfp()
  246. self.bufpos = self.fp.tell()
  247. self.buf = self.fp.read(self.BUFSIZ)
  248. if self.buf:
  249. break
  250. self.fp = None # type: ignore[assignment]
  251. self.charpos = 0
  252. def get_inline_data(self, pos: int, target: bytes = b"EI") -> Tuple[int, bytes]:
  253. self.seek(pos)
  254. i = 0
  255. data = b""
  256. while i <= len(target):
  257. self.fillbuf()
  258. if i:
  259. ci = self.buf[self.charpos]
  260. c = bytes((ci,))
  261. data += c
  262. self.charpos += 1
  263. if (
  264. len(target) <= i
  265. and c.isspace()
  266. or i < len(target)
  267. and c == (bytes((target[i],)))
  268. ):
  269. i += 1
  270. else:
  271. i = 0
  272. else:
  273. try:
  274. j = self.buf.index(target[0], self.charpos)
  275. data += self.buf[self.charpos : j + 1]
  276. self.charpos = j + 1
  277. i = 1
  278. except ValueError:
  279. data += self.buf[self.charpos :]
  280. self.charpos = len(self.buf)
  281. data = data[: -(len(target) + 1)] # strip the last part
  282. data = re.sub(rb"(\x0d\x0a|[\x0d\x0a])$", b"", data)
  283. return (pos, data)
  284. def flush(self) -> None:
  285. self.add_results(*self.popall())
  286. KEYWORD_BI = KWD(b"BI")
  287. KEYWORD_ID = KWD(b"ID")
  288. KEYWORD_EI = KWD(b"EI")
  289. def do_keyword(self, pos: int, token: PSKeyword) -> None:
  290. if token is self.KEYWORD_BI:
  291. # inline image within a content stream
  292. self.start_type(pos, "inline")
  293. elif token is self.KEYWORD_ID:
  294. try:
  295. (_, objs) = self.end_type("inline")
  296. if len(objs) % 2 != 0:
  297. error_msg = f"Invalid dictionary construct: {objs!r}"
  298. raise PSTypeError(error_msg)
  299. d = {literal_name(k): resolve1(v) for (k, v) in choplist(2, objs)}
  300. eos = b"EI"
  301. filter = d.get("F", None)
  302. if filter is not None:
  303. if isinstance(filter, PSLiteral):
  304. filter = [filter]
  305. if filter[0] in LITERALS_ASCII85_DECODE:
  306. eos = b"~>"
  307. (pos, data) = self.get_inline_data(pos + len(b"ID "), target=eos)
  308. if eos != b"EI": # it may be necessary for decoding
  309. data += eos
  310. obj = PDFStream(d, data)
  311. self.push((pos, obj))
  312. if eos == b"EI": # otherwise it is still in the stream
  313. self.push((pos, self.KEYWORD_EI))
  314. except PSTypeError:
  315. if settings.STRICT:
  316. raise
  317. else:
  318. self.push((pos, token))
  319. PDFStackT = PSStackType[PDFStream]
  320. """Types that may appear on the PDF argument stack."""
  321. class PDFPageInterpreter:
  322. """Processor for the content of a PDF page
  323. Reference: PDF Reference, Appendix A, Operator Summary
  324. """
  325. def __init__(self, rsrcmgr: PDFResourceManager, device: PDFDevice) -> None:
  326. self.rsrcmgr = rsrcmgr
  327. self.device = device
  328. def dup(self) -> "PDFPageInterpreter":
  329. return self.__class__(self.rsrcmgr, self.device)
  330. def init_resources(self, resources: Dict[object, object]) -> None:
  331. """Prepare the fonts and XObjects listed in the Resource attribute."""
  332. self.resources = resources
  333. self.fontmap: Dict[object, PDFFont] = {}
  334. self.xobjmap = {}
  335. self.csmap: Dict[str, PDFColorSpace] = PREDEFINED_COLORSPACE.copy()
  336. if not resources:
  337. return
  338. def get_colorspace(spec: object) -> Optional[PDFColorSpace]:
  339. if isinstance(spec, list):
  340. name = literal_name(spec[0])
  341. else:
  342. name = literal_name(spec)
  343. if name == "ICCBased" and isinstance(spec, list) and len(spec) >= 2:
  344. return PDFColorSpace(name, stream_value(spec[1])["N"])
  345. elif name == "DeviceN" and isinstance(spec, list) and len(spec) >= 2:
  346. return PDFColorSpace(name, len(list_value(spec[1])))
  347. else:
  348. return PREDEFINED_COLORSPACE.get(name)
  349. for k, v in dict_value(resources).items():
  350. log.debug("Resource: %r: %r", k, v)
  351. if k == "Font":
  352. for fontid, spec in dict_value(v).items():
  353. objid = None
  354. if isinstance(spec, PDFObjRef):
  355. objid = spec.objid
  356. spec = dict_value(spec)
  357. self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
  358. elif k == "ColorSpace":
  359. for csid, spec in dict_value(v).items():
  360. colorspace = get_colorspace(resolve1(spec))
  361. if colorspace is not None:
  362. self.csmap[csid] = colorspace
  363. elif k == "ProcSet":
  364. self.rsrcmgr.get_procset(list_value(v))
  365. elif k == "XObject":
  366. for xobjid, xobjstrm in dict_value(v).items():
  367. self.xobjmap[xobjid] = xobjstrm
  368. def init_state(self, ctm: Matrix) -> None:
  369. """Initialize the text and graphic states for rendering a page."""
  370. # gstack: stack for graphical states.
  371. self.gstack: List[Tuple[Matrix, PDFTextState, PDFGraphicState]] = []
  372. self.ctm = ctm
  373. self.device.set_ctm(self.ctm)
  374. self.textstate = PDFTextState()
  375. self.graphicstate = PDFGraphicState()
  376. self.curpath: List[PathSegment] = []
  377. # argstack: stack for command arguments.
  378. self.argstack: List[PDFStackT] = []
  379. def push(self, obj: PDFStackT) -> None:
  380. self.argstack.append(obj)
  381. def pop(self, n: int) -> List[PDFStackT]:
  382. if n == 0:
  383. return []
  384. x = self.argstack[-n:]
  385. self.argstack = self.argstack[:-n]
  386. return x
  387. def get_current_state(self) -> Tuple[Matrix, PDFTextState, PDFGraphicState]:
  388. return (self.ctm, self.textstate.copy(), self.graphicstate.copy())
  389. def set_current_state(
  390. self,
  391. state: Tuple[Matrix, PDFTextState, PDFGraphicState],
  392. ) -> None:
  393. (self.ctm, self.textstate, self.graphicstate) = state
  394. self.device.set_ctm(self.ctm)
  395. def do_q(self) -> None:
  396. """Save graphics state"""
  397. self.gstack.append(self.get_current_state())
  398. def do_Q(self) -> None:
  399. """Restore graphics state"""
  400. if self.gstack:
  401. self.set_current_state(self.gstack.pop())
  402. def do_cm(
  403. self,
  404. a1: PDFStackT,
  405. b1: PDFStackT,
  406. c1: PDFStackT,
  407. d1: PDFStackT,
  408. e1: PDFStackT,
  409. f1: PDFStackT,
  410. ) -> None:
  411. """Concatenate matrix to current transformation matrix"""
  412. matrix = safe_matrix(a1, b1, c1, d1, e1, f1)
  413. if matrix is None:
  414. log.warning(
  415. f"Cannot concatenate matrix to current transformation matrix because not all values in {(a1, b1, c1, d1, e1, f1)!r} can be parsed as floats"
  416. )
  417. else:
  418. self.ctm = mult_matrix(matrix, self.ctm)
  419. self.device.set_ctm(self.ctm)
  420. def do_w(self, linewidth: PDFStackT) -> None:
  421. """Set line width"""
  422. linewidth_f = safe_float(linewidth)
  423. if linewidth_f is None:
  424. log.warning(
  425. f"Cannot set line width because {linewidth!r} is an invalid float value"
  426. )
  427. else:
  428. self.graphicstate.linewidth = linewidth_f
  429. def do_J(self, linecap: PDFStackT) -> None:
  430. """Set line cap style"""
  431. self.graphicstate.linecap = linecap
  432. def do_j(self, linejoin: PDFStackT) -> None:
  433. """Set line join style"""
  434. self.graphicstate.linejoin = linejoin
  435. def do_M(self, miterlimit: PDFStackT) -> None:
  436. """Set miter limit"""
  437. self.graphicstate.miterlimit = miterlimit
  438. def do_d(self, dash: PDFStackT, phase: PDFStackT) -> None:
  439. """Set line dash pattern"""
  440. self.graphicstate.dash = (dash, phase)
  441. def do_ri(self, intent: PDFStackT) -> None:
  442. """Set color rendering intent"""
  443. self.graphicstate.intent = intent
  444. def do_i(self, flatness: PDFStackT) -> None:
  445. """Set flatness tolerance"""
  446. self.graphicstate.flatness = flatness
  447. def do_gs(self, name: PDFStackT) -> None:
  448. """Set parameters from graphics state parameter dictionary"""
  449. # TODO
  450. def do_m(self, x: PDFStackT, y: PDFStackT) -> None:
  451. """Begin new subpath"""
  452. x_f = safe_float(x)
  453. y_f = safe_float(y)
  454. if x_f is None or y_f is None:
  455. point = ("m", x, y)
  456. log.warning(
  457. f"Cannot start new subpath because not all values in {point!r} can be parsed as floats"
  458. )
  459. else:
  460. point = ("m", x_f, y_f)
  461. self.curpath.append(point)
  462. def do_l(self, x: PDFStackT, y: PDFStackT) -> None:
  463. """Append straight line segment to path"""
  464. x_f = safe_float(x)
  465. y_f = safe_float(y)
  466. if x_f is None or y_f is None:
  467. point = ("l", x, y)
  468. log.warning(
  469. f"Cannot append straight line segment to path because not all values in {point!r} can be parsed as floats"
  470. )
  471. else:
  472. point = ("l", x_f, y_f)
  473. self.curpath.append(point)
  474. def do_c(
  475. self,
  476. x1: PDFStackT,
  477. y1: PDFStackT,
  478. x2: PDFStackT,
  479. y2: PDFStackT,
  480. x3: PDFStackT,
  481. y3: PDFStackT,
  482. ) -> None:
  483. """Append curved segment to path (three control points)"""
  484. x1_f = safe_float(x1)
  485. y1_f = safe_float(y1)
  486. x2_f = safe_float(x2)
  487. y2_f = safe_float(y2)
  488. x3_f = safe_float(x3)
  489. y3_f = safe_float(y3)
  490. if (
  491. x1_f is None
  492. or y1_f is None
  493. or x2_f is None
  494. or y2_f is None
  495. or x3_f is None
  496. or y3_f is None
  497. ):
  498. point = ("c", x1, y1, x2, y2, x3, y3)
  499. log.warning(
  500. f"Cannot append curved segment to path because not all values in {point!r} can be parsed as floats"
  501. )
  502. else:
  503. point = ("c", x1_f, y1_f, x2_f, y2_f, x3_f, y3_f)
  504. self.curpath.append(point)
  505. def do_v(self, x2: PDFStackT, y2: PDFStackT, x3: PDFStackT, y3: PDFStackT) -> None:
  506. """Append curved segment to path (initial point replicated)"""
  507. x2_f = safe_float(x2)
  508. y2_f = safe_float(y2)
  509. x3_f = safe_float(x3)
  510. y3_f = safe_float(y3)
  511. if x2_f is None or y2_f is None or x3_f is None or y3_f is None:
  512. point = ("v", x2, y2, x3, y3)
  513. log.warning(
  514. f"Cannot append curved segment to path because not all values in {point!r} can be parsed as floats"
  515. )
  516. else:
  517. point = ("v", x2_f, y2_f, x3_f, y3_f)
  518. self.curpath.append(point)
  519. def do_y(self, x1: PDFStackT, y1: PDFStackT, x3: PDFStackT, y3: PDFStackT) -> None:
  520. """Append curved segment to path (final point replicated)"""
  521. x1_f = safe_float(x1)
  522. y1_f = safe_float(y1)
  523. x3_f = safe_float(x3)
  524. y3_f = safe_float(y3)
  525. if x1_f is None or y1_f is None or x3_f is None or y3_f is None:
  526. point = ("y", x1, y1, x3, y3)
  527. log.warning(
  528. f"Cannot append curved segment to path because not all values in {point!r} can be parsed as floats"
  529. )
  530. else:
  531. point = ("y", x1_f, y1_f, x3_f, y3_f)
  532. self.curpath.append(point)
  533. def do_h(self) -> None:
  534. """Close subpath"""
  535. self.curpath.append(("h",))
  536. def do_re(self, x: PDFStackT, y: PDFStackT, w: PDFStackT, h: PDFStackT) -> None:
  537. """Append rectangle to path"""
  538. x_f = safe_float(x)
  539. y_f = safe_float(y)
  540. w_f = safe_float(w)
  541. h_f = safe_float(h)
  542. if x_f is None or y_f is None or w_f is None or h_f is None:
  543. values = (x, y, w, h)
  544. log.warning(
  545. f"Cannot append rectangle to path because not all values in {values!r} can be parsed as floats"
  546. )
  547. else:
  548. self.curpath.append(("m", x_f, y_f))
  549. self.curpath.append(("l", x_f + w_f, y_f))
  550. self.curpath.append(("l", x_f + w_f, y_f + h_f))
  551. self.curpath.append(("l", x_f, y_f + h_f))
  552. self.curpath.append(("h",))
  553. def do_S(self) -> None:
  554. """Stroke path"""
  555. self.device.paint_path(self.graphicstate, True, False, False, self.curpath)
  556. self.curpath = []
  557. def do_s(self) -> None:
  558. """Close and stroke path"""
  559. self.do_h()
  560. self.do_S()
  561. def do_f(self) -> None:
  562. """Fill path using nonzero winding number rule"""
  563. self.device.paint_path(self.graphicstate, False, True, False, self.curpath)
  564. self.curpath = []
  565. def do_F(self) -> None:
  566. """Fill path using nonzero winding number rule (obsolete)"""
  567. def do_f_a(self) -> None:
  568. """Fill path using even-odd rule"""
  569. self.device.paint_path(self.graphicstate, False, True, True, self.curpath)
  570. self.curpath = []
  571. def do_B(self) -> None:
  572. """Fill and stroke path using nonzero winding number rule"""
  573. self.device.paint_path(self.graphicstate, True, True, False, self.curpath)
  574. self.curpath = []
  575. def do_B_a(self) -> None:
  576. """Fill and stroke path using even-odd rule"""
  577. self.device.paint_path(self.graphicstate, True, True, True, self.curpath)
  578. self.curpath = []
  579. def do_b(self) -> None:
  580. """Close, fill, and stroke path using nonzero winding number rule"""
  581. self.do_h()
  582. self.do_B()
  583. def do_b_a(self) -> None:
  584. """Close, fill, and stroke path using even-odd rule"""
  585. self.do_h()
  586. self.do_B_a()
  587. def do_n(self) -> None:
  588. """End path without filling or stroking"""
  589. self.curpath = []
  590. def do_W(self) -> None:
  591. """Set clipping path using nonzero winding number rule"""
  592. def do_W_a(self) -> None:
  593. """Set clipping path using even-odd rule"""
  594. def do_CS(self, name: PDFStackT) -> None:
  595. """Set color space for stroking operations
  596. Introduced in PDF 1.1
  597. """
  598. try:
  599. self.graphicstate.scs = self.csmap[literal_name(name)]
  600. except KeyError:
  601. if settings.STRICT:
  602. raise PDFInterpreterError("Undefined ColorSpace: %r" % name)
  603. def do_cs(self, name: PDFStackT) -> None:
  604. """Set color space for nonstroking operations"""
  605. try:
  606. self.graphicstate.ncs = self.csmap[literal_name(name)]
  607. except KeyError:
  608. if settings.STRICT:
  609. raise PDFInterpreterError("Undefined ColorSpace: %r" % name)
  610. def do_G(self, gray: PDFStackT) -> None:
  611. """Set gray level for stroking operations"""
  612. gray_f = safe_float(gray)
  613. if gray_f is None:
  614. log.warning(
  615. f"Cannot set gray level because {gray!r} is an invalid float value"
  616. )
  617. else:
  618. self.graphicstate.scolor = gray_f
  619. self.graphicstate.scs = self.csmap["DeviceGray"]
  620. def do_g(self, gray: PDFStackT) -> None:
  621. """Set gray level for nonstroking operations"""
  622. gray_f = safe_float(gray)
  623. if gray_f is None:
  624. log.warning(
  625. f"Cannot set gray level because {gray!r} is an invalid float value"
  626. )
  627. else:
  628. self.graphicstate.ncolor = gray_f
  629. self.graphicstate.ncs = self.csmap["DeviceGray"]
  630. def do_RG(self, r: PDFStackT, g: PDFStackT, b: PDFStackT) -> None:
  631. """Set RGB color for stroking operations"""
  632. rgb = safe_rgb(r, g, b)
  633. if rgb is None:
  634. log.warning(
  635. f"Cannot set RGB stroke color because not all values in {(r, g, b)!r} can be parsed as floats"
  636. )
  637. else:
  638. self.graphicstate.scolor = rgb
  639. self.graphicstate.scs = self.csmap["DeviceRGB"]
  640. def do_rg(self, r: PDFStackT, g: PDFStackT, b: PDFStackT) -> None:
  641. """Set RGB color for nonstroking operations"""
  642. rgb = safe_rgb(r, g, b)
  643. if rgb is None:
  644. log.warning(
  645. f"Cannot set RGB non-stroke color because not all values in {(r, g, b)!r} can be parsed as floats"
  646. )
  647. else:
  648. self.graphicstate.ncolor = rgb
  649. self.graphicstate.ncs = self.csmap["DeviceRGB"]
  650. def do_K(self, c: PDFStackT, m: PDFStackT, y: PDFStackT, k: PDFStackT) -> None:
  651. """Set CMYK color for stroking operations"""
  652. cmyk = safe_cmyk(c, m, y, k)
  653. if cmyk is None:
  654. log.warning(
  655. f"Cannot set CMYK stroke color because not all values in {(c, m, y, k)!r} can be parsed as floats"
  656. )
  657. else:
  658. self.graphicstate.scolor = cmyk
  659. self.graphicstate.scs = self.csmap["DeviceCMYK"]
  660. def do_k(self, c: PDFStackT, m: PDFStackT, y: PDFStackT, k: PDFStackT) -> None:
  661. """Set CMYK color for nonstroking operations"""
  662. cmyk = safe_cmyk(c, m, y, k)
  663. if cmyk is None:
  664. log.warning(
  665. f"Cannot set CMYK non-stroke color because not all values in {(c, m, y, k)!r} can be parsed as floats"
  666. )
  667. else:
  668. self.graphicstate.ncolor = cmyk
  669. self.graphicstate.ncs = self.csmap["DeviceCMYK"]
  670. def do_SCN(self) -> None:
  671. """Set color for stroking operations."""
  672. n = self.graphicstate.scs.ncomponents
  673. components = self.pop(n)
  674. if len(components) != n:
  675. log.warning(
  676. f"Cannot set stroke color because expected {n} components but got {components:!r}"
  677. )
  678. elif len(components) == 1:
  679. gray = components[0]
  680. gray_f = safe_float(gray)
  681. if gray_f is None:
  682. log.warning(
  683. f"Cannot set gray stroke color because {gray!r} is an invalid float value"
  684. )
  685. else:
  686. self.graphicstate.scolor = gray_f
  687. elif len(components) == 3:
  688. rgb = safe_rgb(*components)
  689. if rgb is None:
  690. log.warning(
  691. f"Cannot set RGB stroke color because components {components!r} cannot be parsed as RGB"
  692. )
  693. else:
  694. self.graphicstate.scolor = rgb
  695. elif len(components) == 4:
  696. cmyk = safe_cmyk(*components)
  697. if cmyk is None:
  698. log.warning(
  699. f"Cannot set CMYK stroke color because components {components!r} cannot be parsed as CMYK"
  700. )
  701. else:
  702. self.graphicstate.scolor = cmyk
  703. else:
  704. log.warning(
  705. f"Cannot set stroke color because {len(components)} components are specified but only 1 (grayscale), 3 (rgb) and 4 (cmyk) are supported"
  706. )
  707. def do_scn(self) -> None:
  708. """Set color for nonstroking operations"""
  709. n = self.graphicstate.ncs.ncomponents
  710. components = self.pop(n)
  711. if len(components) != n:
  712. log.warning(
  713. f"Cannot set non-stroke color because expected {n} components but got {components:!r}"
  714. )
  715. elif len(components) == 1:
  716. gray = components[0]
  717. gray_f = safe_float(gray)
  718. if gray_f is None:
  719. log.warning(
  720. f"Cannot set gray non-stroke color because {gray!r} is an invalid float value"
  721. )
  722. else:
  723. self.graphicstate.ncolor = gray_f
  724. elif len(components) == 3:
  725. rgb = safe_rgb(*components)
  726. if rgb is None:
  727. log.warning(
  728. f"Cannot set RGB non-stroke color because components {components!r} cannot be parsed as RGB"
  729. )
  730. else:
  731. self.graphicstate.ncolor = rgb
  732. elif len(components) == 4:
  733. cmyk = safe_cmyk(*components)
  734. if cmyk is None:
  735. log.warning(
  736. f"Cannot set CMYK non-stroke color because components {components!r} cannot be parsed as CMYK"
  737. )
  738. else:
  739. self.graphicstate.ncolor = cmyk
  740. else:
  741. log.warning(
  742. f"Cannot set non-stroke color because {len(components)} components are specified but only 1 (grayscale), 3 (rgb) and 4 (cmyk) are supported"
  743. )
  744. def do_SC(self) -> None:
  745. """Set color for stroking operations"""
  746. self.do_SCN()
  747. def do_sc(self) -> None:
  748. """Set color for nonstroking operations"""
  749. self.do_scn()
  750. def do_sh(self, name: object) -> None:
  751. """Paint area defined by shading pattern"""
  752. def do_BT(self) -> None:
  753. """Begin text object
  754. Initializing the text matrix, Tm, and the text line matrix, Tlm, to
  755. the identity matrix. Text objects cannot be nested; a second BT cannot
  756. appear before an ET.
  757. """
  758. self.textstate.reset()
  759. def do_ET(self) -> None:
  760. """End a text object"""
  761. def do_BX(self) -> None:
  762. """Begin compatibility section"""
  763. def do_EX(self) -> None:
  764. """End compatibility section"""
  765. def do_MP(self, tag: PDFStackT) -> None:
  766. """Define marked-content point"""
  767. if isinstance(tag, PSLiteral):
  768. self.device.do_tag(tag)
  769. else:
  770. log.warning(
  771. f"Cannot define marked-content point because {tag!r} is not a PSLiteral"
  772. )
  773. def do_DP(self, tag: PDFStackT, props: PDFStackT) -> None:
  774. """Define marked-content point with property list"""
  775. if isinstance(tag, PSLiteral):
  776. self.device.do_tag(tag, props)
  777. else:
  778. log.warning(
  779. f"Cannot define marked-content point with property list because {tag!r} is not a PSLiteral"
  780. )
  781. def do_BMC(self, tag: PDFStackT) -> None:
  782. """Begin marked-content sequence"""
  783. if isinstance(tag, PSLiteral):
  784. self.device.begin_tag(tag)
  785. else:
  786. log.warning(
  787. f"Cannot begin marked-content sequence because {tag!r} is not a PSLiteral"
  788. )
  789. def do_BDC(self, tag: PDFStackT, props: PDFStackT) -> None:
  790. """Begin marked-content sequence with property list"""
  791. if isinstance(tag, PSLiteral):
  792. self.device.begin_tag(tag, props)
  793. else:
  794. log.warning(
  795. f"Cannot begin marked-content sequence with property list because {tag!r} is not a PSLiteral"
  796. )
  797. def do_EMC(self) -> None:
  798. """End marked-content sequence"""
  799. self.device.end_tag()
  800. def do_Tc(self, space: PDFStackT) -> None:
  801. """Set character spacing.
  802. Character spacing is used by the Tj, TJ, and ' operators.
  803. :param space: a number expressed in unscaled text space units.
  804. """
  805. charspace = safe_float(space)
  806. if charspace is None:
  807. log.warning(
  808. f"Could not set character spacing because {space!r} is an invalid float value"
  809. )
  810. else:
  811. self.textstate.charspace = charspace
  812. def do_Tw(self, space: PDFStackT) -> None:
  813. """Set the word spacing.
  814. Word spacing is used by the Tj, TJ, and ' operators.
  815. :param space: a number expressed in unscaled text space units
  816. """
  817. wordspace = safe_float(space)
  818. if wordspace is None:
  819. log.warning(
  820. f"Could not set word spacing becuase {space!r} is an invalid float value"
  821. )
  822. else:
  823. self.textstate.wordspace = wordspace
  824. def do_Tz(self, scale: PDFStackT) -> None:
  825. """Set the horizontal scaling.
  826. :param scale: is a number specifying the percentage of the normal width
  827. """
  828. scale_f = safe_float(scale)
  829. if scale_f is None:
  830. log.warning(
  831. f"Could not set horizontal scaling because {scale!r} is an invalid float value"
  832. )
  833. else:
  834. self.textstate.scaling = scale_f
  835. def do_TL(self, leading: PDFStackT) -> None:
  836. """Set the text leading.
  837. Text leading is used only by the T*, ', and " operators.
  838. :param leading: a number expressed in unscaled text space units
  839. """
  840. leading_f = safe_float(leading)
  841. if leading_f is None:
  842. log.warning(
  843. f"Could not set text leading because {leading!r} is an invalid float value"
  844. )
  845. else:
  846. self.textstate.leading = -leading_f
  847. def do_Tf(self, fontid: PDFStackT, fontsize: PDFStackT) -> None:
  848. """Set the text font
  849. :param fontid: the name of a font resource in the Font subdictionary
  850. of the current resource dictionary
  851. :param fontsize: size is a number representing a scale factor.
  852. """
  853. try:
  854. self.textstate.font = self.fontmap[literal_name(fontid)]
  855. except KeyError:
  856. if settings.STRICT:
  857. raise PDFInterpreterError("Undefined Font id: %r" % fontid)
  858. self.textstate.font = self.rsrcmgr.get_font(None, {})
  859. fontsize_f = safe_float(fontsize)
  860. if fontsize_f is None:
  861. log.warning(
  862. f"Could not set text font because {fontsize!r} is an invalid float value"
  863. )
  864. else:
  865. self.textstate.fontsize = fontsize_f
  866. def do_Tr(self, render: PDFStackT) -> None:
  867. """Set the text rendering mode"""
  868. render_i = safe_int(render)
  869. if render_i is None:
  870. log.warning(
  871. f"Could not set text rendering mode because {render!r} is an invalid int value"
  872. )
  873. else:
  874. self.textstate.render = render_i
  875. def do_Ts(self, rise: PDFStackT) -> None:
  876. """Set the text rise
  877. :param rise: a number expressed in unscaled text space units
  878. """
  879. rise_f = safe_float(rise)
  880. if rise_f is None:
  881. log.warning(
  882. f"Could not set text rise because {rise!r} is an invalid float value"
  883. )
  884. else:
  885. self.textstate.rise = rise_f
  886. def do_Td(self, tx: PDFStackT, ty: PDFStackT) -> None:
  887. """Move to the start of the next line
  888. Offset from the start of the current line by (tx , ty).
  889. """
  890. tx_ = safe_float(tx)
  891. ty_ = safe_float(ty)
  892. if tx_ is not None and ty_ is not None:
  893. (a, b, c, d, e, f) = self.textstate.matrix
  894. e_new = tx_ * a + ty_ * c + e
  895. f_new = tx_ * b + ty_ * d + f
  896. self.textstate.matrix = (a, b, c, d, e_new, f_new)
  897. elif settings.STRICT:
  898. raise PDFValueError(f"Invalid offset ({tx!r}, {ty!r}) for Td")
  899. self.textstate.linematrix = (0, 0)
  900. def do_TD(self, tx: PDFStackT, ty: PDFStackT) -> None:
  901. """Move to the start of the next line.
  902. offset from the start of the current line by (tx , ty). As a side effect, this
  903. operator sets the leading parameter in the text state.
  904. """
  905. tx_ = safe_float(tx)
  906. ty_ = safe_float(ty)
  907. if tx_ is not None and ty_ is not None:
  908. (a, b, c, d, e, f) = self.textstate.matrix
  909. e_new = tx_ * a + ty_ * c + e
  910. f_new = tx_ * b + ty_ * d + f
  911. self.textstate.matrix = (a, b, c, d, e_new, f_new)
  912. elif settings.STRICT:
  913. raise PDFValueError("Invalid offset ({tx}, {ty}) for TD")
  914. if ty_ is not None:
  915. self.textstate.leading = ty_
  916. self.textstate.linematrix = (0, 0)
  917. def do_Tm(
  918. self,
  919. a: PDFStackT,
  920. b: PDFStackT,
  921. c: PDFStackT,
  922. d: PDFStackT,
  923. e: PDFStackT,
  924. f: PDFStackT,
  925. ) -> None:
  926. """Set text matrix and text line matrix"""
  927. values = (a, b, c, d, e, f)
  928. matrix = safe_matrix(*values)
  929. if matrix is None:
  930. log.warning(
  931. f"Could not set text matrix because not all values in {values!r} can be parsed as floats"
  932. )
  933. else:
  934. self.textstate.matrix = matrix
  935. self.textstate.linematrix = (0, 0)
  936. def do_T_a(self) -> None:
  937. """Move to start of next text line"""
  938. (a, b, c, d, e, f) = self.textstate.matrix
  939. self.textstate.matrix = (
  940. a,
  941. b,
  942. c,
  943. d,
  944. self.textstate.leading * c + e,
  945. self.textstate.leading * d + f,
  946. )
  947. self.textstate.linematrix = (0, 0)
  948. def do_TJ(self, seq: PDFStackT) -> None:
  949. """Show text, allowing individual glyph positioning"""
  950. if self.textstate.font is None:
  951. if settings.STRICT:
  952. raise PDFInterpreterError("No font specified!")
  953. return
  954. self.device.render_string(
  955. self.textstate,
  956. cast(PDFTextSeq, seq),
  957. self.graphicstate.ncs,
  958. self.graphicstate.copy(),
  959. )
  960. def do_Tj(self, s: PDFStackT) -> None:
  961. """Show text"""
  962. self.do_TJ([s])
  963. def do__q(self, s: PDFStackT) -> None:
  964. """Move to next line and show text
  965. The ' (single quote) operator.
  966. """
  967. self.do_T_a()
  968. self.do_TJ([s])
  969. def do__w(self, aw: PDFStackT, ac: PDFStackT, s: PDFStackT) -> None:
  970. """Set word and character spacing, move to next line, and show text
  971. The " (double quote) operator.
  972. """
  973. self.do_Tw(aw)
  974. self.do_Tc(ac)
  975. self.do_TJ([s])
  976. def do_BI(self) -> None:
  977. """Begin inline image object"""
  978. def do_ID(self) -> None:
  979. """Begin inline image data"""
  980. def do_EI(self, obj: PDFStackT) -> None:
  981. """End inline image object"""
  982. if isinstance(obj, PDFStream) and "W" in obj and "H" in obj:
  983. iobjid = str(id(obj))
  984. self.device.begin_figure(iobjid, (0, 0, 1, 1), MATRIX_IDENTITY)
  985. self.device.render_image(iobjid, obj)
  986. self.device.end_figure(iobjid)
  987. def do_Do(self, xobjid_arg: PDFStackT) -> None:
  988. """Invoke named XObject"""
  989. xobjid = literal_name(xobjid_arg)
  990. try:
  991. xobj = stream_value(self.xobjmap[xobjid])
  992. except KeyError:
  993. if settings.STRICT:
  994. raise PDFInterpreterError("Undefined xobject id: %r" % xobjid)
  995. return
  996. log.debug("Processing xobj: %r", xobj)
  997. subtype = xobj.get("Subtype")
  998. if subtype is LITERAL_FORM and "BBox" in xobj:
  999. interpreter = self.dup()
  1000. bbox = cast(Rect, list_value(xobj["BBox"]))
  1001. matrix = cast(Matrix, list_value(xobj.get("Matrix", MATRIX_IDENTITY)))
  1002. # According to PDF reference 1.7 section 4.9.1, XObjects in
  1003. # earlier PDFs (prior to v1.2) use the page's Resources entry
  1004. # instead of having their own Resources entry.
  1005. xobjres = xobj.get("Resources")
  1006. if xobjres:
  1007. resources = dict_value(xobjres)
  1008. else:
  1009. resources = self.resources.copy()
  1010. self.device.begin_figure(xobjid, bbox, matrix)
  1011. interpreter.render_contents(
  1012. resources,
  1013. [xobj],
  1014. ctm=mult_matrix(matrix, self.ctm),
  1015. )
  1016. self.device.end_figure(xobjid)
  1017. elif subtype is LITERAL_IMAGE and "Width" in xobj and "Height" in xobj:
  1018. self.device.begin_figure(xobjid, (0, 0, 1, 1), MATRIX_IDENTITY)
  1019. self.device.render_image(xobjid, xobj)
  1020. self.device.end_figure(xobjid)
  1021. else:
  1022. # unsupported xobject type.
  1023. pass
  1024. def process_page(self, page: PDFPage) -> None:
  1025. log.debug("Processing page: %r", page)
  1026. (x0, y0, x1, y1) = page.mediabox
  1027. if page.rotate == 90:
  1028. ctm = (0, -1, 1, 0, -y0, x1)
  1029. elif page.rotate == 180:
  1030. ctm = (-1, 0, 0, -1, x1, y1)
  1031. elif page.rotate == 270:
  1032. ctm = (0, 1, -1, 0, y1, -x0)
  1033. else:
  1034. ctm = (1, 0, 0, 1, -x0, -y0)
  1035. self.device.begin_page(page, ctm)
  1036. self.render_contents(page.resources, page.contents, ctm=ctm)
  1037. self.device.end_page(page)
  1038. def render_contents(
  1039. self,
  1040. resources: Dict[object, object],
  1041. streams: Sequence[object],
  1042. ctm: Matrix = MATRIX_IDENTITY,
  1043. ) -> None:
  1044. """Render the content streams.
  1045. This method may be called recursively.
  1046. """
  1047. log.debug(
  1048. "render_contents: resources=%r, streams=%r, ctm=%r",
  1049. resources,
  1050. streams,
  1051. ctm,
  1052. )
  1053. self.init_resources(resources)
  1054. self.init_state(ctm)
  1055. self.execute(list_value(streams))
  1056. def execute(self, streams: Sequence[object]) -> None:
  1057. try:
  1058. parser = PDFContentParser(streams)
  1059. except PSEOF:
  1060. # empty page
  1061. return
  1062. while True:
  1063. try:
  1064. (_, obj) = parser.nextobject()
  1065. except PSEOF:
  1066. break
  1067. if isinstance(obj, PSKeyword):
  1068. name = keyword_name(obj)
  1069. method = "do_%s" % name.replace("*", "_a").replace('"', "_w").replace(
  1070. "'",
  1071. "_q",
  1072. )
  1073. if hasattr(self, method):
  1074. func = getattr(self, method)
  1075. nargs = func.__code__.co_argcount - 1
  1076. if nargs:
  1077. args = self.pop(nargs)
  1078. log.debug("exec: %s %r", name, args)
  1079. if len(args) == nargs:
  1080. func(*args)
  1081. else:
  1082. log.debug("exec: %s", name)
  1083. func()
  1084. elif settings.STRICT:
  1085. error_msg = "Unknown operator: %r" % name
  1086. raise PDFInterpreterError(error_msg)
  1087. else:
  1088. self.push(obj)