- working intrepreter v2
This commit is contained in:
1 parent
1d11cd1c7f
commit
487f9b6c08
33 files changed
+1216
-1366
No files matched your search
@@ -0,0 +1,15 @@
|
||||
from ..extractor import extract_items_from_pdf
|
||||
|
||||
def convert_to_html(
|
||||
input_file_path: str,
|
||||
output_file: io.TextIOWrapper = None,
|
||||
image_folder_path: str = None,
|
||||
codec: str = 'utf-8',
|
||||
maxpages: int = 0,
|
||||
password: str = None,
|
||||
caching: bool = True,
|
||||
check_extractable: bool = True,
|
||||
pagenos: List[int] = None,
|
||||
debug_level: int = logging.WARNING,
|
||||
):
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
from ..utils import get_logger
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
from ..layouts import PDFLayoutAnalyzer, PDFPage, LTPage
|
||||
|
||||
class PDFExctractor(PDFLayoutAnalyzer):
|
||||
def end_page(self, page: PDFPage):
|
||||
assert not self._stack, str(len(self._stack))
|
||||
assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
|
||||
self.pageno += 1
|
||||
return self.cur_item
|
||||
@@ -6,17 +6,13 @@ from io import BytesIO, StringIO, TextIOWrapper
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
from ..interpreter.PDFResourceManager import PDFResourceManager
|
||||
from ..interpreter.PDFPage import PDFPage
|
||||
from ..interpreter.PDFPageInterpreter import PDFPageInterpreter
|
||||
from ..interpreter_v2 import PDFPageInterpreter as PDFPageInterpreterV2
|
||||
from ..interpreter_v2 import PDFPageInterpreter
|
||||
from ..imagewriter import ImageWriter
|
||||
from ..utils import set_log_level, get_logger, logging
|
||||
from .PDFExtractor import PDFExctractor
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
|
||||
def extract_items_from_pdf(
|
||||
input_file_path: str,
|
||||
maxpages: int = 0,
|
||||
@@ -29,42 +25,10 @@ def extract_items_from_pdf(
|
||||
set_log_level(debug_level)
|
||||
|
||||
log.debug("Creating resources....")
|
||||
rsrcmgr = PDFResourceManager(caching=caching)
|
||||
device = PDFExctractor(
|
||||
rsrcmgr=rsrcmgr,
|
||||
pageno=0
|
||||
)
|
||||
interpreter = PDFPageInterpreter(rsrcmgr, device)
|
||||
interpreter = PDFPageInterpreter()
|
||||
log.debug("Reading pages....")
|
||||
with open(input_file_path, 'rb') as input_file:
|
||||
pages = PDFPage.get_pages(input_file, pagenos=pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=check_extractable)
|
||||
|
||||
page_waiter = tqdm(enumerate(pages), disable=debug_level > logging.INFO)
|
||||
for _, page in page_waiter:
|
||||
page_waiter.set_description("Processing pdf...")
|
||||
yield interpreter.process_page(page)
|
||||
page_waiter.close()
|
||||
device.close()
|
||||
|
||||
|
||||
def v2_extract_items_from_pdf(
|
||||
input_file_path: str,
|
||||
maxpages: int = 0,
|
||||
password: str = None,
|
||||
caching: bool = True,
|
||||
check_extractable: bool = True,
|
||||
pagenos: List[int] = None,
|
||||
debug_level: int = logging.WARNING,
|
||||
):
|
||||
set_log_level(debug_level)
|
||||
|
||||
log.debug("Creating resources....")
|
||||
rsrcmgr = PDFResourceManager(caching=caching)
|
||||
interpreter = PDFPageInterpreterV2(rsrcmgr)
|
||||
log.debug("Reading pages....")
|
||||
with open(input_file_path, 'rb') as input_file:
|
||||
pages = PDFPage.get_pages(input_file, pagenos=pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=check_extractable)
|
||||
|
||||
page_waiter = tqdm(enumerate(pages), disable=debug_level > logging.INFO)
|
||||
for _, page in page_waiter:
|
||||
page_waiter.set_description("Processing pdf...")
|
||||
|
||||
@@ -91,12 +91,12 @@ class PDFPage(object):
|
||||
tree_type = tree.get('type')
|
||||
|
||||
if tree_type is LITERAL_PAGES and 'Kids' in tree:
|
||||
# log.info('Pages: Kids=%r', tree['Kids'])
|
||||
log.debug('Pages: Kids=%r', tree['Kids'])
|
||||
for c in list_value(tree['Kids']):
|
||||
for x in search(c, tree):
|
||||
yield x
|
||||
elif tree_type is LITERAL_PAGE:
|
||||
log.info('Page: %r', tree)
|
||||
log.debug('Page: %r', tree)
|
||||
yield (objid, tree)
|
||||
|
||||
pages = False
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
from pdfmajor.interpreter.PDFPage import PDFPage
|
||||
from pdfmajor.interpreter.PDFStream import list_value
|
||||
from pdfmajor.interpreter.PDFContentParser import PDFContentParser
|
||||
from pdfmajor.interpreter.PSStackParser import PSKeyword
|
||||
from pdfmajor.interpreter.PSStackParser import keyword_name
|
||||
|
||||
from .commands import PDFStateStack, PDFCommands
|
||||
from .commands import LTXObject
|
||||
from .utils import init_resources
|
||||
from .XObjectInterpreter import XObjectInterpreter
|
||||
|
||||
class PageInterpreter:
|
||||
class PDFInterpreterError(Exception): pass
|
||||
|
||||
def __init__(self, page: PDFPage, page_num: int, font_cache: dict = {}):
|
||||
(x0, y0, x1, y1) = page.mediabox
|
||||
if page.rotate == 90:
|
||||
ctm = [0, -1, 1, 0, -y0, x1]
|
||||
elif page.rotate == 180:
|
||||
ctm = [-1, 0, 0, -1, x1, y1]
|
||||
elif page.rotate == 270:
|
||||
ctm = [0, 1, -1, 0, y1, -x0]
|
||||
else:
|
||||
ctm = [1, 0, 0, 1, -x0, -y0]
|
||||
|
||||
# Locals
|
||||
self.font_cache = font_cache
|
||||
self.page = page
|
||||
self.page_num = page_num
|
||||
|
||||
# Init State
|
||||
self.state: PDFStateStack = PDFStateStack()
|
||||
self.state.t_matrix = ctm
|
||||
self.state.resources = page.resources
|
||||
# set some global states.
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = None
|
||||
if self.state.colorspace_map:
|
||||
col_space = next(iter(self.state.colorspace_map.values()))
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = col_space
|
||||
# Init Resources
|
||||
init_resources(self.state, self.font_cache)
|
||||
|
||||
def __iter__(self):
|
||||
parser = PDFContentParser(list_value(self.page.contents))
|
||||
for obj in parser:
|
||||
if isinstance(obj, PSKeyword):
|
||||
name = keyword_name(obj)
|
||||
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
|
||||
if method in PDFCommands.commands.keys():
|
||||
func = PDFCommands.commands.get(method)
|
||||
nargs = func.__code__.co_argcount-1
|
||||
if nargs:
|
||||
args = self.state.pop(nargs)
|
||||
if len(args) == nargs:
|
||||
func(self.state, *args)
|
||||
else:
|
||||
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
|
||||
else:
|
||||
# log.debug('exec: %s', name)
|
||||
func(self.state)
|
||||
else:
|
||||
raise self.PDFInterpreterError('Unknown operator: %r' % name)
|
||||
else:
|
||||
self.state.argstack.append(obj)
|
||||
|
||||
for complete_item in self.state.complete_layout_items:
|
||||
if isinstance(complete_item, LTXObject):
|
||||
interpeter = XObjectInterpreter(
|
||||
streams=[complete_item.stream],
|
||||
resources=complete_item.resources,
|
||||
ctm=complete_item.t_matrix
|
||||
)
|
||||
for item in interpeter:
|
||||
complete_item.add(item)
|
||||
yield complete_item
|
||||
self.state.complete_layout_items = []
|
||||
def __repr__(self) -> str:
|
||||
return f"<Page:{self.page_num}/>"
|
||||
@@ -0,0 +1,71 @@
|
||||
from pdfmajor.utils import MATRIX_IDENTITY
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFParser import PDFStream
|
||||
from pdfmajor.interpreter.PSStackParser import PSEOF
|
||||
from pdfmajor.interpreter.PSStackParser import PSKeyword
|
||||
from pdfmajor.interpreter.PSStackParser import keyword_name
|
||||
from pdfmajor.interpreter.PSStackParser import PSStackParser
|
||||
from pdfmajor.interpreter.PDFResourceManager import PDFResourceManager
|
||||
from pdfmajor.interpreter.constants import LITERAL_PDF, LITERAL_TEXT, LITERAL_FONT
|
||||
|
||||
from pdfmajor.interpreter.PDFStream import resolve1, list_value, dict_value
|
||||
from pdfmajor.interpreter.PDFStream.PDFObjRef import PDFObjRef
|
||||
from pdfmajor.interpreter.PDFContentParser import PDFContentParser
|
||||
|
||||
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .commands import PDFCommands, LTXObject
|
||||
from .utils import init_resources
|
||||
|
||||
class XObjectInterpreter:
|
||||
|
||||
class PDFInterpreterError(Exception): pass
|
||||
|
||||
def __init__(self, streams, resources, ctm):
|
||||
self.streams = streams
|
||||
self.font_cache = {}
|
||||
|
||||
self.state = PDFStateStack()
|
||||
self.state.t_matrix = ctm
|
||||
self.state.resources = resources
|
||||
|
||||
# set some global states.
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = None
|
||||
if self.state.colorspace_map:
|
||||
col_space = next(iter(self.state.colorspace_map.values()))
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = col_space
|
||||
init_resources(self.state, self.font_cache)
|
||||
|
||||
def __iter__(self):
|
||||
parser = PDFContentParser(self.streams)
|
||||
for obj in parser:
|
||||
if isinstance(obj, PSKeyword):
|
||||
name = keyword_name(obj)
|
||||
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
|
||||
if method in PDFCommands.commands.keys():
|
||||
func = PDFCommands.commands.get(method)
|
||||
nargs = func.__code__.co_argcount-1
|
||||
if nargs:
|
||||
args = self.state.pop(nargs)
|
||||
if len(args) == nargs:
|
||||
func(self.state, *args)
|
||||
else:
|
||||
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
|
||||
else:
|
||||
# log.debug('exec: %s', name)
|
||||
func(self.state)
|
||||
else:
|
||||
raise self.PDFInterpreterError('Unknown operator: %r' % name)
|
||||
else:
|
||||
self.state.argstack.append(obj)
|
||||
|
||||
for complete_item in self.state.complete_layout_items:
|
||||
if isinstance(complete_item, LTXObject):
|
||||
interpeter = self.__class__(
|
||||
streams=[complete_item.stream],
|
||||
resources=complete_item.resources,
|
||||
ctm=complete_item.t_matrix
|
||||
)
|
||||
for item in interpeter:
|
||||
complete_item.add(item)
|
||||
yield complete_item
|
||||
self.state.complete_layout_items = []
|
||||
@@ -1,124 +1,72 @@
|
||||
from pdfmajor.utils import MATRIX_IDENTITY
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PSStackParser import PSEOF
|
||||
from pdfmajor.interpreter.PSStackParser import PSKeyword
|
||||
from pdfmajor.interpreter.PSStackParser import keyword_name
|
||||
from pdfmajor.interpreter.PSStackParser import PSStackParser
|
||||
from pdfmajor.interpreter.PDFParser import PDFStream
|
||||
from typing import List
|
||||
|
||||
from pdfmajor.interpreter.PDFStream import resolve1, list_value, dict_value
|
||||
from pdfmajor.interpreter.PDFStream.PDFObjRef import PDFObjRef
|
||||
from pdfmajor.interpreter.PDFContentParser import PDFContentParser
|
||||
from tqdm import tqdm
|
||||
|
||||
from pdfmajor.interpreter.PDFPage import PDFPage
|
||||
from pdfmajor.utils import set_log_level, get_logger, logging
|
||||
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .commands import PDFCommands
|
||||
from .commands.state.PDFItem import PDFXObject, PDFImage, PDFItem, PDFShape, PDFText
|
||||
from .commands import LTCharBlock, LTChar
|
||||
from .commands import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
|
||||
from .commands import LTImage
|
||||
from .commands import LTXObject
|
||||
from .PageInterpreter import PageInterpreter
|
||||
|
||||
def get_colorspace(spec):
|
||||
if isinstance(spec, list):
|
||||
name = literal_name(spec[0])
|
||||
else:
|
||||
name = literal_name(spec)
|
||||
if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, PDFStream.validated_stream(spec[1])['N'])
|
||||
elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, len(list_value(spec[1])))
|
||||
else:
|
||||
return PREDEFINED_COLORSPACE.get(name)
|
||||
log = get_logger(__name__)
|
||||
|
||||
class PDFPageInterpreter:
|
||||
class PDFInterpreter:
|
||||
class AccessError(Exception): pass
|
||||
|
||||
class PDFInterpreterError(Exception): pass
|
||||
|
||||
def __init__(self, rsrcmgr):
|
||||
self.rsrcmgr = rsrcmgr
|
||||
self.state: PDFStateStack = PDFStateStack()
|
||||
def __init__(self,
|
||||
input_file_path: str,
|
||||
maxpages: int = 0,
|
||||
caching: bool = True,
|
||||
check_extractable: bool = True,
|
||||
password: str = None,
|
||||
pagenos: List[int] = None,
|
||||
preload: bool = False,
|
||||
debug_level: int = logging.WARNING,
|
||||
):
|
||||
self.input_file_path = input_file_path
|
||||
self.maxpages = maxpages
|
||||
self.caching = caching
|
||||
self.check_extractable = check_extractable
|
||||
self.password = password
|
||||
self.pagenos = pagenos
|
||||
self.debug_level = debug_level
|
||||
self.__pages = []
|
||||
if preload:
|
||||
for _ in self.__load_pages():
|
||||
pass
|
||||
|
||||
def __load_pages(self):
|
||||
set_log_level(self.debug_level)
|
||||
log.info("Opening file...")
|
||||
with open(self.input_file_path, 'rb') as input_file:
|
||||
font_cache = {}
|
||||
log.info("Parsing file...")
|
||||
pages = PDFPage.get_pages(
|
||||
input_file,
|
||||
pagenos=self.pagenos,
|
||||
maxpages=self.maxpages,
|
||||
password=self.password,
|
||||
caching=self.caching,
|
||||
check_extractable=self.check_extractable
|
||||
)
|
||||
for page_num, page in tqdm(enumerate(pages), desc="Reading pages...", disable=self.debug_level > logging.INFO):
|
||||
self.__pages.append(PageInterpreter(page, page_num, font_cache))
|
||||
yield self.__pages[-1]
|
||||
log.info(f"Done Reading {len(self.__pages)} pages.")
|
||||
set_log_level(logging.WARNING)
|
||||
if len(self.__pages) == 0:
|
||||
raise self.AccessError("No pages found in pdf-file")
|
||||
|
||||
# init_resources(resources):
|
||||
# Prepare the fonts and XObjects listed in the Resource attribute.
|
||||
def init_resources(self, resources):
|
||||
self.state.resources = resources
|
||||
for (k, v) in iter(dict_value(self.state.resources).items()):
|
||||
# log.debug('Resource: %r: %r', k, v)
|
||||
if k == 'Font':
|
||||
for (fontid, spec) in iter(dict_value(v).items()):
|
||||
objid = None
|
||||
if isinstance(spec, PDFObjRef):
|
||||
objid = spec.objid
|
||||
spec = dict_value(spec)
|
||||
self.state.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
|
||||
elif k == 'ColorSpace':
|
||||
for (csid, spec) in iter(dict_value(v).items()):
|
||||
self.state.colorspace_map[csid] = get_colorspace(resolve1(spec))
|
||||
elif k == 'ProcSet':
|
||||
self.rsrcmgr.get_procset(list_value(v))
|
||||
elif k == 'XObject':
|
||||
for (xobjid, xobjstrm) in iter(dict_value(v).items()):
|
||||
self.state.xobjmap[xobjid] = xobjstrm
|
||||
|
||||
|
||||
# init_state(ctm)
|
||||
# Initialize the text and graphic states for rendering a page.
|
||||
def init_state(self, ctm):
|
||||
self.state = PDFStateStack()
|
||||
self.state.t_matrix = ctm
|
||||
# set some global states.
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = None
|
||||
if self.state.colorspace_map:
|
||||
col_space = next(iter(self.state.colorspace_map.values()))
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = col_space
|
||||
|
||||
# render_contents(resources, streams, ctm)
|
||||
# Render the content streams.
|
||||
# This method may be called recursively.
|
||||
def render_contents(self, resources, streams, ctm=MATRIX_IDENTITY):
|
||||
self.init_state(ctm)
|
||||
self.init_resources(resources)
|
||||
return self.execute(list_value(streams))
|
||||
|
||||
def process_page(self, page):
|
||||
(x0, y0, x1, y1) = page.mediabox
|
||||
if page.rotate == 90:
|
||||
ctm = [0, -1, 1, 0, -y0, x1]
|
||||
elif page.rotate == 180:
|
||||
ctm = [-1, 0, 0, -1, x1, y1]
|
||||
elif page.rotate == 270:
|
||||
ctm = [0, 1, -1, 0, y1, -x0]
|
||||
def __iter__(self):
|
||||
if len(self.__pages) > 0:
|
||||
set_log_level(self.debug_level)
|
||||
for page in tqdm(self.__pages, desc="Reading pages from store...", disable=self.debug_level > logging.INFO):
|
||||
yield page
|
||||
set_log_level(logging.WARNING)
|
||||
else:
|
||||
ctm = [1, 0, 0, 1, -x0, -y0]
|
||||
return self.render_contents(page.resources, page.contents, ctm=ctm)
|
||||
|
||||
def execute(self, streams):
|
||||
parser = PDFContentParser(streams)
|
||||
for obj in parser:
|
||||
if isinstance(obj, PSKeyword):
|
||||
name = keyword_name(obj)
|
||||
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
|
||||
if method in PDFCommands.commands.keys():
|
||||
func = PDFCommands.commands.get(method)
|
||||
nargs = func.__code__.co_argcount-1
|
||||
if nargs:
|
||||
args = self.state.pop(nargs)
|
||||
if len(args) == nargs:
|
||||
func(self.state, *args)
|
||||
else:
|
||||
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
|
||||
else:
|
||||
# log.debug('exec: %s', name)
|
||||
func(self.state)
|
||||
else:
|
||||
raise self.PDFInterpreterError('Unknown operator: %r' % name)
|
||||
else:
|
||||
self.state.argstack.append(obj)
|
||||
|
||||
for complete_item in self.state.complete_items:
|
||||
if isinstance(complete_item, PDFXObject):
|
||||
interpeter = self.__class__(self.rsrcmgr)
|
||||
for item in interpeter.render_contents(
|
||||
streams=[complete_item.stream],
|
||||
resources=complete_item.resources,
|
||||
ctm=complete_item.ctm
|
||||
):
|
||||
complete_item.children.append(item)
|
||||
yield complete_item
|
||||
self.state.complete_items = []
|
||||
for page in self.__load_pages():
|
||||
yield page
|
||||
@@ -4,8 +4,8 @@ from pdfmajor.utils import MATRIX_IDENTITY, mult_matrix
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream, list_value, dict_value
|
||||
from pdfmajor.interpreter.constants import LITERAL_FORM, LITERAL_IMAGE
|
||||
from ..state import PDFStateStack, PDFColorSpace
|
||||
from ..state.Curves import CurveMethod, CurvePath, CurvePoint
|
||||
from .state import PDFStateStack, PDFColorSpace
|
||||
from .state.Curves import CurveMethod, CurvePath, CurvePoint
|
||||
|
||||
class PDFCommands:
|
||||
commands = {}
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
from .commands import PDFCommands
|
||||
from .state import PDFStateStack, PDFGraphicState, PDFTextState
|
||||
from .state.PDFGraphicState import PDFColor, PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .state.PDFTextState import PDFFont
|
||||
from .state import PDFFont, get_font
|
||||
from .state import LTCharBlock, LTChar
|
||||
from .state import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
|
||||
from .state import LTImage
|
||||
from .state import LTXObject
|
||||
from .state import LTItem, LTComponent, LTContainer
|
||||
@@ -1,4 +1,4 @@
|
||||
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY
|
||||
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream, list_value, dict_value
|
||||
from pdfmajor.interpreter.constants import LITERAL_FORM, LITERAL_IMAGE
|
||||
@@ -6,7 +6,8 @@ from pdfmajor.interpreter.constants import LITERAL_FORM, LITERAL_IMAGE
|
||||
from .PDFCommands import PDFCommands
|
||||
from .state import PDFStateStack, PDFGraphicState
|
||||
from .state.Curves import CurveMethod, CurvePath, CurvePoint
|
||||
from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject
|
||||
# from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject
|
||||
from .state import make_char_block, make_curve, make_image, make_xobject
|
||||
|
||||
# No Support
|
||||
@PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs')
|
||||
@@ -15,17 +16,17 @@ def do_no_support(stack: PDFStateStack) -> PDFStateStack:
|
||||
|
||||
# marked content operators
|
||||
@PDFCommands.add('MP', 'DP')
|
||||
def do_MP(stack: PDFStateStack, tag) -> PDFStateStack:
|
||||
def do_MP(stack: PDFStateStack) -> PDFStateStack:
|
||||
# self.device.do_tag(tag)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('BMC', 'BDC')
|
||||
def do_BMC(stack: PDFStateStack, tag) -> PDFStateStack:
|
||||
def do_BMC(stack: PDFStateStack) -> PDFStateStack:
|
||||
# self.device.begin_tag(tag)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('EMC')
|
||||
def do_EMC(stack: PDFStateStack, tag) -> PDFStateStack:
|
||||
def do_EMC(stack: PDFStateStack) -> PDFStateStack:
|
||||
# self.device.end_tag(tag)
|
||||
return stack
|
||||
|
||||
@@ -166,7 +167,8 @@ def do_re(stack: PDFStateStack, x, y, w, h) -> PDFStateStack:
|
||||
# stroke, fill
|
||||
@PDFCommands.add('S', 'f', 'F', 'B')
|
||||
def do_complete_path(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_items.append(PDFShape(
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
False,
|
||||
stack.curvestacks
|
||||
@@ -177,7 +179,8 @@ def do_complete_path(stack: PDFStateStack) -> PDFStateStack:
|
||||
# sroke, fill-even-odd
|
||||
@PDFCommands.add('f_a', 'B_a')
|
||||
def do_complete_path_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_items.append(PDFShape(
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
True,
|
||||
stack.curvestacks
|
||||
@@ -191,7 +194,8 @@ def do_close_complete(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CLOSE_PATH
|
||||
))
|
||||
stack.complete_items.append(PDFShape(
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
False,
|
||||
stack.curvestacks
|
||||
@@ -205,7 +209,8 @@ def do_close_complete_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CLOSE_PATH
|
||||
))
|
||||
stack.complete_items.append(PDFShape(
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
True,
|
||||
stack.curvestacks
|
||||
@@ -411,11 +416,11 @@ def do_T_a(stack: PDFStateStack) -> PDFStateStack:
|
||||
def do_TJ(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
|
||||
if stack.text.font is None:
|
||||
raise PDFCommands.InvalidOperation("No Font Specified")
|
||||
stack.complete_items.append(PDFText.make(
|
||||
stack.complete_layout_items.append(make_char_block(
|
||||
seq,
|
||||
stack.t_matrix,
|
||||
stack.text.copy(),
|
||||
stack.graphics.copy()
|
||||
stack.graphics.ncolor.copy()
|
||||
))
|
||||
return stack
|
||||
|
||||
@@ -439,7 +444,9 @@ def do__w(stack: PDFStateStack, wordspace, charspace, b) -> PDFStateStack:
|
||||
@PDFCommands.add('EI')
|
||||
def do_EI(stack: PDFStateStack, obj) -> PDFStateStack:
|
||||
if 'W' in obj and 'H' in obj:
|
||||
stack.complete_items.append(PDFImage(obj))
|
||||
stack.complete_layout_items.append(make_image(
|
||||
obj, stack.ctm
|
||||
))
|
||||
return stack
|
||||
|
||||
# invoke an XObject
|
||||
@@ -460,11 +467,17 @@ def do_Do(stack: PDFStateStack, xobjid) -> PDFStateStack:
|
||||
# instead of having their own Resources entry.
|
||||
xobjres = xobj.get('Resources')
|
||||
resources = dict_value(xobjres) if xobjres else stack.resources.copy()
|
||||
stack.complete_items.append(PDFXObject(
|
||||
xobj, bbox, resources, mult_matrix(stack.t_matrix, matrix)
|
||||
stack.complete_layout_items.append(make_xobject(
|
||||
obj=xobj,
|
||||
bbox=bbox,
|
||||
ctm=stack.t_matrix,
|
||||
matrix=matrix,
|
||||
resources=resources
|
||||
))
|
||||
elif subtype is LITERAL_IMAGE and 'Width' in xobj and 'Height' in xobj:
|
||||
stack.complete_items.append(PDFImage(obj))
|
||||
stack.complete_layout_items.append(make_image(
|
||||
obj, stack.ctm
|
||||
))
|
||||
else:
|
||||
# unsupported xobject type.
|
||||
pass
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from .PDFColor import PDFColor, PDFColorSpace
|
||||
from .PDFColorSpace import PREDEFINED_COLORSPACE
|
||||
|
||||
class PDFGraphicState(object):
|
||||
class InvalidOperation(Exception): pass
|
||||
|
||||
@@ -1,755 +0,0 @@
|
||||
|
||||
import sys
|
||||
import struct
|
||||
from io import BytesIO
|
||||
from abc import abstractmethod
|
||||
|
||||
from pdfmajor.interpreter.cmapdb import CMapDB
|
||||
from pdfmajor.interpreter.cmapdb import CMapParser
|
||||
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
|
||||
from pdfmajor.interpreter.cmapdb import CMap
|
||||
from pdfmajor.interpreter.EncodingDB import EncodingDB, name2unicode
|
||||
from pdfmajor.interpreter.PSStackParser import PSStackParser
|
||||
from pdfmajor.interpreter.PSStackParser import PSEOF
|
||||
from pdfmajor.interpreter.PSStackParser import LIT
|
||||
from pdfmajor.interpreter.PSStackParser import KWD
|
||||
from pdfmajor.interpreter.PSStackParser import PSLiteral
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFStream import PDFException
|
||||
from pdfmajor.interpreter.PDFStream import resolve1
|
||||
from pdfmajor.interpreter.PDFStream import int_value
|
||||
from pdfmajor.interpreter.PDFStream import num_value
|
||||
from pdfmajor.interpreter.PDFStream import list_value
|
||||
from pdfmajor.interpreter.PDFStream import dict_value
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream
|
||||
from pdfmajor.interpreter.constants import COMMON_FONT_ATTRIBUTES
|
||||
from pdfmajor.utils import settings, FONT_METRICS, apply_matrix_norm, nunpack, choplist, isnumber
|
||||
|
||||
def get_widths(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, w) in enumerate(v):
|
||||
widths[char1+i] = w
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 3:
|
||||
(char1, char2, w) = r
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = w
|
||||
r = []
|
||||
return widths
|
||||
#assert get_widths([1]) == {}
|
||||
#assert get_widths([1,2,3]) == {1:3, 2:3}
|
||||
#assert get_widths([1,[2,3],6,[7,8]]) == {1:2,2:3, 6:7,7:8}
|
||||
|
||||
def get_widths2(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, (w, vx, vy)) in enumerate(choplist(3, v)):
|
||||
widths[char1+i] = (w, (vx, vy))
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 5:
|
||||
(char1, char2, w, vx, vy) = r # pylint: disable=E0632
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = (w, (vx, vy))
|
||||
r = []
|
||||
return widths
|
||||
#assert get_widths2([1]) == {}
|
||||
#assert get_widths2([1,2,3,4,5]) == {1:(3, (4,5)), 2:(3, (4,5))}
|
||||
#assert get_widths2([1,[2,3,4,5],6,[7,8,9]]) == {1:(2, (3,4)), 6:(7, (8,9))}
|
||||
|
||||
|
||||
## FontMetricsDB
|
||||
##
|
||||
class FontMetricsDB(object):
|
||||
|
||||
@classmethod
|
||||
def get_metrics(klass, fontname):
|
||||
return FONT_METRICS[fontname]
|
||||
|
||||
|
||||
## Type1FontHeaderParser
|
||||
##
|
||||
class Type1FontHeaderParser(PSStackParser):
|
||||
|
||||
KEYWORD_BEGIN = KWD(b'begin')
|
||||
KEYWORD_END = KWD(b'end')
|
||||
KEYWORD_DEF = KWD(b'def')
|
||||
KEYWORD_PUT = KWD(b'put')
|
||||
KEYWORD_DICT = KWD(b'dict')
|
||||
KEYWORD_ARRAY = KWD(b'array')
|
||||
KEYWORD_READONLY = KWD(b'readonly')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
|
||||
def __init__(self, data):
|
||||
PSStackParser.__init__(self, data)
|
||||
self._cid2unicode = {}
|
||||
return
|
||||
|
||||
def get_encoding(self):
|
||||
while 1:
|
||||
try:
|
||||
(cid, name) = self.nextobject()
|
||||
except PSEOF:
|
||||
break
|
||||
try:
|
||||
self._cid2unicode[cid] = name2unicode(name)
|
||||
except KeyError:
|
||||
pass
|
||||
return self._cid2unicode
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_PUT:
|
||||
((_, key), (_, value)) = self.pop(2) # pylint: disable=E0632
|
||||
if (isinstance(key, int) and
|
||||
isinstance(value, PSLiteral)):
|
||||
self.add_results((key, literal_name(value)))
|
||||
return
|
||||
|
||||
|
||||
NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-')
|
||||
|
||||
|
||||
## CFFFont
|
||||
## (Format specified in Adobe Technical Note: #5176
|
||||
## "The Compact Font Format Specification")
|
||||
##
|
||||
def getdict(data):
|
||||
d = {}
|
||||
fp = BytesIO(data)
|
||||
stack = []
|
||||
while 1:
|
||||
c = fp.read(1)
|
||||
if not c:
|
||||
break
|
||||
b0 = ord(c)
|
||||
if b0 <= 21:
|
||||
d[b0] = stack
|
||||
stack = []
|
||||
continue
|
||||
if b0 == 30:
|
||||
s = ''
|
||||
loop = True
|
||||
while loop:
|
||||
b = ord(fp.read(1))
|
||||
for n in (b >> 4, b & 15):
|
||||
if n == 15:
|
||||
loop = False
|
||||
else:
|
||||
s += NIBBLES[n]
|
||||
value = float(s)
|
||||
elif 32 <= b0 and b0 <= 246:
|
||||
value = b0-139
|
||||
else:
|
||||
b1 = ord(fp.read(1))
|
||||
if 247 <= b0 and b0 <= 250:
|
||||
value = ((b0-247) << 8)+b1+108
|
||||
elif 251 <= b0 and b0 <= 254:
|
||||
value = -((b0-251) << 8)-b1-108
|
||||
else:
|
||||
b2 = ord(fp.read(1))
|
||||
if 128 <= b1:
|
||||
b1 -= 256
|
||||
if b0 == 28:
|
||||
value = b1 << 8 | b2
|
||||
else:
|
||||
value = b1 << 24 | b2 << 16 | struct.unpack('>H', fp.read(2))[0]
|
||||
stack.append(value)
|
||||
return d
|
||||
|
||||
|
||||
class CFFFont(object):
|
||||
|
||||
STANDARD_STRINGS = (
|
||||
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign',
|
||||
'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft',
|
||||
'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period',
|
||||
'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six',
|
||||
'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal',
|
||||
'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash',
|
||||
'bracketright', 'asciicircum', 'underscore', 'quoteleft', 'a',
|
||||
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown',
|
||||
'cent', 'sterling', 'fraction', 'yen', 'florin', 'section',
|
||||
'currency', 'quotesingle', 'quotedblleft', 'guillemotleft',
|
||||
'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash',
|
||||
'dagger', 'daggerdbl', 'periodcentered', 'paragraph', 'bullet',
|
||||
'quotesinglbase', 'quotedblbase', 'quotedblright',
|
||||
'guillemotright', 'ellipsis', 'perthousand', 'questiondown',
|
||||
'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve',
|
||||
'dotaccent', 'dieresis', 'ring', 'cedilla', 'hungarumlaut',
|
||||
'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash',
|
||||
'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
|
||||
'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
|
||||
'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn',
|
||||
'onequarter', 'divide', 'brokenbar', 'degree', 'thorn',
|
||||
'threequarters', 'twosuperior', 'registered', 'minus', 'eth',
|
||||
'multiply', 'threesuperior', 'copyright', 'Aacute',
|
||||
'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
|
||||
'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave',
|
||||
'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde',
|
||||
'Oacute', 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde',
|
||||
'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave',
|
||||
'Yacute', 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex',
|
||||
'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute',
|
||||
'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
|
||||
'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex',
|
||||
'odieresis', 'ograve', 'otilde', 'scaron', 'uacute',
|
||||
'ucircumflex', 'udieresis', 'ugrave', 'yacute', 'ydieresis',
|
||||
'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
|
||||
'dollarsuperior', 'ampersandsmall', 'Acutesmall',
|
||||
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
|
||||
'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle',
|
||||
'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
|
||||
'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
|
||||
'commasuperior', 'threequartersemdash', 'periodsuperior',
|
||||
'questionsmall', 'asuperior', 'bsuperior', 'centsuperior',
|
||||
'dsuperior', 'esuperior', 'isuperior', 'lsuperior', 'msuperior',
|
||||
'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior',
|
||||
'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior',
|
||||
'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
|
||||
'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
|
||||
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall',
|
||||
'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall',
|
||||
'Tsmall', 'Usmall', 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall',
|
||||
'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall',
|
||||
'exclamdownsmall', 'centoldstyle', 'Lslashsmall', 'Scaronsmall',
|
||||
'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
|
||||
'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior',
|
||||
'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall',
|
||||
'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths',
|
||||
'onethird', 'twothirds', 'zerosuperior', 'foursuperior',
|
||||
'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
|
||||
'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
|
||||
'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
|
||||
'seveninferior', 'eightinferior', 'nineinferior',
|
||||
'centinferior', 'dollarinferior', 'periodinferior',
|
||||
'commainferior', 'Agravesmall', 'Aacutesmall',
|
||||
'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
|
||||
'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall',
|
||||
'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
|
||||
'Igravesmall', 'Iacutesmall', 'Icircumflexsmall',
|
||||
'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall',
|
||||
'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
|
||||
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
|
||||
'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall',
|
||||
'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
|
||||
'001.001', '001.002', '001.003', 'Black', 'Bold', 'Book',
|
||||
'Light', 'Medium', 'Regular', 'Roman', 'Semibold',
|
||||
)
|
||||
|
||||
class INDEX(object):
|
||||
|
||||
def __init__(self, fp):
|
||||
self.fp = fp
|
||||
self.offsets = []
|
||||
(count, offsize) = struct.unpack('>HB', self.fp.read(3))
|
||||
for _ in range(count+1):
|
||||
self.offsets.append(nunpack(self.fp.read(offsize)))
|
||||
self.base = self.fp.tell()-1
|
||||
self.fp.seek(self.base+self.offsets[-1])
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<INDEX: size=%d>' % len(self)
|
||||
|
||||
def __len__(self): # pylint: disable=E0303
|
||||
return len(self.offsets)-1
|
||||
|
||||
def __getitem__(self, i):
|
||||
self.fp.seek(self.base+self.offsets[i])
|
||||
return self.fp.read(self.offsets[i+1]-self.offsets[i])
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self[i] for i in range(len(self)))
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
# Header
|
||||
(_major, _minor, hdrsize, _) = struct.unpack('BBBB', self.fp.read(4))
|
||||
self.fp.read(hdrsize-4)
|
||||
# Name INDEX
|
||||
self.name_index = self.INDEX(self.fp)
|
||||
# Top DICT INDEX
|
||||
self.dict_index = self.INDEX(self.fp)
|
||||
# String INDEX
|
||||
self.string_index = self.INDEX(self.fp)
|
||||
# Global Subr INDEX
|
||||
self.subr_index = self.INDEX(self.fp)
|
||||
# Top DICT DATA
|
||||
self.top_dict = getdict(self.dict_index[0])
|
||||
(charset_pos,) = self.top_dict.get(15, [0])
|
||||
(encoding_pos,) = self.top_dict.get(16, [0])
|
||||
(charstring_pos,) = self.top_dict.get(17, [0])
|
||||
# CharStrings
|
||||
self.fp.seek(charstring_pos)
|
||||
self.charstring = self.INDEX(self.fp)
|
||||
self.nglyphs = len(self.charstring)
|
||||
# Encodings
|
||||
self.code2gid = {}
|
||||
self.gid2code = {}
|
||||
self.fp.seek(encoding_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
for (code, gid) in enumerate(struct.unpack('B'*n, self.fp.read(n))):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
code = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
code += 1
|
||||
else:
|
||||
raise ValueError('unsupported encoding format: %r' % format)
|
||||
# Charsets
|
||||
self.name2gid = {}
|
||||
self.gid2name = {}
|
||||
self.fp.seek(charset_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
n = self.nglyphs-1
|
||||
for (gid, sid) in enumerate(struct.unpack('>'+'H'*n, self.fp.read(2*n))):
|
||||
gid += 1
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
sid = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
sid += 1
|
||||
elif format == b'\x02':
|
||||
# Format 2
|
||||
assert False, str(('Unhandled', format))
|
||||
else:
|
||||
raise ValueError('unsupported charset format: %r' % format)
|
||||
#print self.code2gid
|
||||
#print self.name2gid
|
||||
#assert 0
|
||||
return
|
||||
|
||||
def getstr(self, sid):
|
||||
if sid < len(self.STANDARD_STRINGS):
|
||||
return self.STANDARD_STRINGS[sid]
|
||||
return self.string_index[sid-len(self.STANDARD_STRINGS)]
|
||||
|
||||
|
||||
## TrueTypeFont
|
||||
##
|
||||
class TrueTypeFont(object):
|
||||
|
||||
class CMapNotFound(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
self.tables = {}
|
||||
self.fonttype = fp.read(4)
|
||||
try:
|
||||
(ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
for _ in range(ntables):
|
||||
(name, _, offset, length) = struct.unpack('>4sLLL', fp.read(16))
|
||||
self.tables[name] = (offset, length)
|
||||
except struct.error:
|
||||
# Do not fail if there are not enough bytes to read. Even for
|
||||
# corrupted PDFs we would like to get as much information as
|
||||
# possible, so continue.
|
||||
pass
|
||||
return
|
||||
|
||||
def create_unicode_map(self):
|
||||
if 'cmap' not in self.tables:
|
||||
raise TrueTypeFont.CMapNotFound
|
||||
(base_offset, _) = self.tables['cmap']
|
||||
fp = self.fp
|
||||
fp.seek(base_offset)
|
||||
(_, nsubtables) = struct.unpack('>HH', fp.read(4))
|
||||
subtables = []
|
||||
for i in range(nsubtables):
|
||||
subtables.append(struct.unpack('>HHL', fp.read(8)))
|
||||
char2gid = {}
|
||||
# Only supports subtable type 0, 2 and 4.
|
||||
for (_1, _2, st_offset) in subtables:
|
||||
fp.seek(base_offset+st_offset)
|
||||
(fmttype, _, _) = struct.unpack('>HHH', fp.read(6))
|
||||
if fmttype == 0:
|
||||
char2gid.update(enumerate(struct.unpack('>256B', fp.read(256))))
|
||||
elif fmttype == 2:
|
||||
subheaderkeys = struct.unpack('>256H', fp.read(512))
|
||||
firstbytes = [0]*8192
|
||||
for (i, k) in enumerate(subheaderkeys):
|
||||
firstbytes[k//8] = i
|
||||
nhdrs = max(subheaderkeys)//8 + 1
|
||||
hdrs = []
|
||||
for i in range(nhdrs):
|
||||
(firstcode, entcount, delta, offset) = struct.unpack('>HHhH', fp.read(8))
|
||||
hdrs.append((i, firstcode, entcount, delta, fp.tell()-2+offset))
|
||||
for (i, firstcode, entcount, delta, pos) in hdrs:
|
||||
if not entcount:
|
||||
continue
|
||||
first = firstcode + (firstbytes[i] << 8)
|
||||
fp.seek(pos)
|
||||
for c in range(entcount):
|
||||
gid = struct.unpack('>H', fp.read(2))
|
||||
if gid:
|
||||
gid += delta
|
||||
char2gid[first+c] = gid
|
||||
elif fmttype == 4:
|
||||
(segcount, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
segcount //= 2
|
||||
ecs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
fp.read(2)
|
||||
scs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
idds = struct.unpack('>%dh' % segcount, fp.read(2*segcount))
|
||||
pos = fp.tell()
|
||||
idrs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
for (ec, sc, idd, idr) in zip(ecs, scs, idds, idrs):
|
||||
if idr:
|
||||
fp.seek(pos+idr)
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (struct.unpack('>H', fp.read(2))[0] + idd) & 0xffff
|
||||
else:
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (c + idd) & 0xffff
|
||||
else:
|
||||
assert False, str(('Unhandled', fmttype))
|
||||
# create unicode map
|
||||
unicode_map = FileUnicodeMap()
|
||||
for (char, gid) in char2gid.items():
|
||||
unicode_map.add_cid2unichr(gid, char)
|
||||
return unicode_map
|
||||
|
||||
|
||||
## Fonts
|
||||
##
|
||||
class PDFFontError(PDFException):
|
||||
pass
|
||||
|
||||
|
||||
class PDFUnicodeNotDefined(PDFFontError):
|
||||
pass
|
||||
|
||||
LITERAL_STANDARD_ENCODING = LIT('StandardEncoding')
|
||||
LITERAL_TYPE1C = LIT('Type1C')
|
||||
|
||||
|
||||
# PDFFont
|
||||
class PDFFont(object):
|
||||
|
||||
def __init__(self, descriptor, widths, default_width=None):
|
||||
self.descriptor = descriptor
|
||||
self.widths = widths
|
||||
self.fontname = resolve1(descriptor.get('FontName', 'unknown'))
|
||||
if isinstance(self.fontname, PSLiteral):
|
||||
self.fontname = literal_name(self.fontname)
|
||||
self.flags = int_value(descriptor.get('Flags', 0))
|
||||
self.ascent = num_value(descriptor.get('Ascent', 0))
|
||||
self.descent = num_value(descriptor.get('Descent', 0))
|
||||
self.italic_angle = num_value(descriptor.get('ItalicAngle', 0))
|
||||
self.default_width = default_width or num_value(descriptor.get('MissingWidth', 0))
|
||||
self.leading = num_value(descriptor.get('Leading', 0))
|
||||
self.bbox = list_value(descriptor.get('FontBBox', (0, 0, 0, 0)))
|
||||
self.hscale = self.vscale = .001
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFFont>'
|
||||
|
||||
def is_vertical(self):
|
||||
return False
|
||||
|
||||
def is_multibyte(self):
|
||||
return False
|
||||
|
||||
def decode(self, bytes):
|
||||
return bytearray(bytes) # map(ord, bytes)
|
||||
|
||||
def get_ascent(self):
|
||||
return self.ascent * self.vscale
|
||||
|
||||
def get_descent(self):
|
||||
return self.descent * self.vscale
|
||||
|
||||
def get_width(self):
|
||||
w = self.bbox[2]-self.bbox[0]
|
||||
if w == 0:
|
||||
w = -self.default_width
|
||||
return w * self.hscale
|
||||
|
||||
def get_height(self):
|
||||
h = self.bbox[3]-self.bbox[1]
|
||||
if h == 0:
|
||||
h = self.ascent - self.descent
|
||||
return h * self.vscale
|
||||
|
||||
def char_width(self, cid):
|
||||
try:
|
||||
return self.widths[cid] * self.hscale
|
||||
except KeyError:
|
||||
try:
|
||||
return self.widths[self.to_unichr(cid)] * self.hscale
|
||||
except (KeyError, PDFUnicodeNotDefined):
|
||||
return self.default_width * self.hscale
|
||||
|
||||
def char_disp(self, cid):
|
||||
return 0
|
||||
|
||||
def string_width(self, s):
|
||||
return sum(self.char_width(cid) for cid in self.decode(s))
|
||||
|
||||
@abstractmethod
|
||||
def to_unichr(self, cid):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def font_weight(self):
|
||||
if 'FontWeight' in self.descriptor.keys():
|
||||
return self.descriptor['FontWeight']
|
||||
elif 'bold' in self.fontname.lower():
|
||||
return 'bold'
|
||||
else:
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_bold(self):
|
||||
return self.font_weight == 'bold' or self.font_weight > 549
|
||||
|
||||
|
||||
# PDFSimpleFont
|
||||
class PDFSimpleFont(PDFFont):
|
||||
|
||||
def __init__(self, descriptor, widths, spec):
|
||||
# Font encoding is specified either by a name of
|
||||
# built-in encoding or a dictionary that describes
|
||||
# the differences.
|
||||
if 'Encoding' in spec:
|
||||
encoding = resolve1(spec['Encoding'])
|
||||
else:
|
||||
encoding = LITERAL_STANDARD_ENCODING
|
||||
if isinstance(encoding, dict):
|
||||
name = literal_name(encoding.get('BaseEncoding', LITERAL_STANDARD_ENCODING))
|
||||
diff = list_value(encoding.get('Differences', []))
|
||||
self.cid2unicode = EncodingDB.get_encoding(name, diff)
|
||||
else:
|
||||
self.cid2unicode = EncodingDB.get_encoding(literal_name(encoding))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
PDFFont.__init__(self, descriptor, widths)
|
||||
return
|
||||
|
||||
def to_unichr(self, cid):
|
||||
if self.unicode_map:
|
||||
try:
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
return self.cid2unicode[cid]
|
||||
except KeyError:
|
||||
raise PDFUnicodeNotDefined(None, cid)
|
||||
|
||||
|
||||
# PDFType1Font
|
||||
class PDFType1Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, rsrcmgr, spec):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
try:
|
||||
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
|
||||
except KeyError:
|
||||
descriptor = dict_value(spec.get('FontDescriptor', {}))
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 255))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
if 'Encoding' not in spec and 'FontFile' in descriptor:
|
||||
# try to recover the missing encoding info from the font file.
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile'))
|
||||
length1 = int_value(self.fontfile['Length1'])
|
||||
data = self.fontfile.get_data()[:length1]
|
||||
parser = Type1FontHeaderParser(BytesIO(data))
|
||||
self.cid2unicode = parser.get_encoding()
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType1Font: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFTrueTypeFont
|
||||
class PDFTrueTypeFont(PDFType1Font):
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFTrueTypeFont: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFType3Font
|
||||
class PDFType3Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, rsrcmgr, spec):
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 0))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
if 'FontDescriptor' in spec:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
else:
|
||||
descriptor = {'Ascent': 0, 'Descent': 0,
|
||||
'FontBBox': spec['FontBBox']}
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
self.matrix = tuple(list_value(spec.get('FontMatrix')))
|
||||
(_, self.descent, _, self.ascent) = self.bbox
|
||||
(self.hscale, self.vscale) = apply_matrix_norm(self.matrix, (1, 1))
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType3Font>'
|
||||
|
||||
|
||||
# PDFCIDFont
|
||||
class PDFCIDFont(PDFFont):
|
||||
|
||||
def __init__(self, rsrcmgr, spec, strict=settings.STRICT):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {}))
|
||||
self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"),
|
||||
resolve1(self.cidsysteminfo.get('Ordering', b'unknown')).decode("latin1"))
|
||||
try:
|
||||
name = literal_name(spec['Encoding'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise PDFFontError('Encoding is unspecified')
|
||||
name = 'unknown'
|
||||
try:
|
||||
self.cmap = CMapDB.get_cmap(name)
|
||||
except CMapDB.CMapNotFound as e:
|
||||
if strict:
|
||||
raise PDFFontError(e)
|
||||
self.cmap = CMap()
|
||||
try:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise PDFFontError('FontDescriptor is missing')
|
||||
descriptor = {}
|
||||
ttf = None
|
||||
if 'FontFile2' in descriptor:
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile2'))
|
||||
ttf = TrueTypeFont(self.basefont,
|
||||
BytesIO(self.fontfile.get_data()))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
elif self.cidcoding in ('Adobe-Identity', 'Adobe-UCS'):
|
||||
if ttf:
|
||||
try:
|
||||
self.unicode_map = ttf.create_unicode_map()
|
||||
except TrueTypeFont.CMapNotFound:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
|
||||
except CMapDB.CMapNotFound as e:
|
||||
pass
|
||||
|
||||
self.vertical = self.cmap.is_vertical()
|
||||
if self.vertical:
|
||||
# writing mode: vertical
|
||||
widths = get_widths2(list_value(spec.get('W2', [])))
|
||||
self.disps = dict((cid, (vx, vy)) for (cid, (_, (vx, vy))) in iter(widths.items()))
|
||||
(vy, w) = spec.get('DW2', [880, -1000])
|
||||
self.default_disp = (None, vy)
|
||||
widths = dict((cid, w) for (cid, (w, _)) in iter(widths.items()))
|
||||
default_width = w
|
||||
else:
|
||||
# writing mode: horizontal
|
||||
self.disps = {}
|
||||
self.default_disp = 0
|
||||
widths = get_widths(list_value(spec.get('W', [])))
|
||||
default_width = spec.get('DW', 1000)
|
||||
PDFFont.__init__(self, descriptor, widths, default_width=default_width)
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFCIDFont: basefont=%r, cidcoding=%r>' % (self.basefont, self.cidcoding)
|
||||
|
||||
def is_vertical(self):
|
||||
return self.vertical
|
||||
|
||||
def is_multibyte(self):
|
||||
return True
|
||||
|
||||
def decode(self, bytes):
|
||||
return self.cmap.decode(bytes)
|
||||
|
||||
def char_disp(self, cid):
|
||||
"Returns an integer for horizontal fonts, a tuple for vertical fonts."
|
||||
return self.disps.get(cid, self.default_disp)
|
||||
|
||||
def to_unichr(self, cid):
|
||||
try:
|
||||
if not self.unicode_map:
|
||||
raise KeyError(cid)
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
raise PDFUnicodeNotDefined(self.cidcoding, cid)
|
||||
|
||||
|
||||
# main
|
||||
def main(argv):
|
||||
for fname in argv[1:]:
|
||||
fp = open(fname, 'rb')
|
||||
#font = TrueTypeFont(fname, fp)
|
||||
font = CFFFont(fname, fp)
|
||||
print (font)
|
||||
fp.close()
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
@@ -0,0 +1,246 @@
|
||||
## CFFFont
|
||||
## (Format specified in Adobe Technical Note: #5176
|
||||
## "The Compact Font Format Specification")
|
||||
##
|
||||
import struct
|
||||
from io import BytesIO
|
||||
from pdfmajor.utils import nunpack
|
||||
|
||||
NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-')
|
||||
|
||||
def getdict(data):
|
||||
d = {}
|
||||
fp = BytesIO(data)
|
||||
stack = []
|
||||
while 1:
|
||||
c = fp.read(1)
|
||||
if not c:
|
||||
break
|
||||
b0 = ord(c)
|
||||
if b0 <= 21:
|
||||
d[b0] = stack
|
||||
stack = []
|
||||
continue
|
||||
if b0 == 30:
|
||||
s = ''
|
||||
loop = True
|
||||
while loop:
|
||||
b = ord(fp.read(1))
|
||||
for n in (b >> 4, b & 15):
|
||||
if n == 15:
|
||||
loop = False
|
||||
else:
|
||||
s += NIBBLES[n]
|
||||
value = float(s)
|
||||
elif 32 <= b0 and b0 <= 246:
|
||||
value = b0-139
|
||||
else:
|
||||
b1 = ord(fp.read(1))
|
||||
if 247 <= b0 and b0 <= 250:
|
||||
value = ((b0-247) << 8)+b1+108
|
||||
elif 251 <= b0 and b0 <= 254:
|
||||
value = -((b0-251) << 8)-b1-108
|
||||
else:
|
||||
b2 = ord(fp.read(1))
|
||||
if 128 <= b1:
|
||||
b1 -= 256
|
||||
if b0 == 28:
|
||||
value = b1 << 8 | b2
|
||||
else:
|
||||
value = b1 << 24 | b2 << 16 | struct.unpack('>H', fp.read(2))[0]
|
||||
stack.append(value)
|
||||
return d
|
||||
|
||||
|
||||
class CFFFont(object):
|
||||
|
||||
STANDARD_STRINGS = (
|
||||
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign',
|
||||
'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft',
|
||||
'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period',
|
||||
'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six',
|
||||
'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal',
|
||||
'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash',
|
||||
'bracketright', 'asciicircum', 'underscore', 'quoteleft', 'a',
|
||||
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown',
|
||||
'cent', 'sterling', 'fraction', 'yen', 'florin', 'section',
|
||||
'currency', 'quotesingle', 'quotedblleft', 'guillemotleft',
|
||||
'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash',
|
||||
'dagger', 'daggerdbl', 'periodcentered', 'paragraph', 'bullet',
|
||||
'quotesinglbase', 'quotedblbase', 'quotedblright',
|
||||
'guillemotright', 'ellipsis', 'perthousand', 'questiondown',
|
||||
'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve',
|
||||
'dotaccent', 'dieresis', 'ring', 'cedilla', 'hungarumlaut',
|
||||
'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash',
|
||||
'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
|
||||
'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
|
||||
'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn',
|
||||
'onequarter', 'divide', 'brokenbar', 'degree', 'thorn',
|
||||
'threequarters', 'twosuperior', 'registered', 'minus', 'eth',
|
||||
'multiply', 'threesuperior', 'copyright', 'Aacute',
|
||||
'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
|
||||
'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave',
|
||||
'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde',
|
||||
'Oacute', 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde',
|
||||
'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave',
|
||||
'Yacute', 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex',
|
||||
'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute',
|
||||
'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
|
||||
'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex',
|
||||
'odieresis', 'ograve', 'otilde', 'scaron', 'uacute',
|
||||
'ucircumflex', 'udieresis', 'ugrave', 'yacute', 'ydieresis',
|
||||
'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
|
||||
'dollarsuperior', 'ampersandsmall', 'Acutesmall',
|
||||
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
|
||||
'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle',
|
||||
'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
|
||||
'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
|
||||
'commasuperior', 'threequartersemdash', 'periodsuperior',
|
||||
'questionsmall', 'asuperior', 'bsuperior', 'centsuperior',
|
||||
'dsuperior', 'esuperior', 'isuperior', 'lsuperior', 'msuperior',
|
||||
'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior',
|
||||
'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior',
|
||||
'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
|
||||
'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
|
||||
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall',
|
||||
'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall',
|
||||
'Tsmall', 'Usmall', 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall',
|
||||
'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall',
|
||||
'exclamdownsmall', 'centoldstyle', 'Lslashsmall', 'Scaronsmall',
|
||||
'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
|
||||
'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior',
|
||||
'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall',
|
||||
'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths',
|
||||
'onethird', 'twothirds', 'zerosuperior', 'foursuperior',
|
||||
'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
|
||||
'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
|
||||
'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
|
||||
'seveninferior', 'eightinferior', 'nineinferior',
|
||||
'centinferior', 'dollarinferior', 'periodinferior',
|
||||
'commainferior', 'Agravesmall', 'Aacutesmall',
|
||||
'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
|
||||
'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall',
|
||||
'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
|
||||
'Igravesmall', 'Iacutesmall', 'Icircumflexsmall',
|
||||
'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall',
|
||||
'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
|
||||
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
|
||||
'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall',
|
||||
'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
|
||||
'001.001', '001.002', '001.003', 'Black', 'Bold', 'Book',
|
||||
'Light', 'Medium', 'Regular', 'Roman', 'Semibold',
|
||||
)
|
||||
|
||||
class INDEX(object):
|
||||
|
||||
def __init__(self, fp):
|
||||
self.fp = fp
|
||||
self.offsets = []
|
||||
(count, offsize) = struct.unpack('>HB', self.fp.read(3))
|
||||
for _ in range(count+1):
|
||||
self.offsets.append(nunpack(self.fp.read(offsize)))
|
||||
self.base = self.fp.tell()-1
|
||||
self.fp.seek(self.base+self.offsets[-1])
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<INDEX: size=%d>' % len(self)
|
||||
|
||||
def __len__(self): # pylint: disable=E0303
|
||||
return len(self.offsets)-1
|
||||
|
||||
def __getitem__(self, i):
|
||||
self.fp.seek(self.base+self.offsets[i])
|
||||
return self.fp.read(self.offsets[i+1]-self.offsets[i])
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self[i] for i in range(len(self)))
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
# Header
|
||||
(_major, _minor, hdrsize, _) = struct.unpack('BBBB', self.fp.read(4))
|
||||
self.fp.read(hdrsize-4)
|
||||
# Name INDEX
|
||||
self.name_index = self.INDEX(self.fp)
|
||||
# Top DICT INDEX
|
||||
self.dict_index = self.INDEX(self.fp)
|
||||
# String INDEX
|
||||
self.string_index = self.INDEX(self.fp)
|
||||
# Global Subr INDEX
|
||||
self.subr_index = self.INDEX(self.fp)
|
||||
# Top DICT DATA
|
||||
self.top_dict = getdict(self.dict_index[0])
|
||||
(charset_pos,) = self.top_dict.get(15, [0])
|
||||
(encoding_pos,) = self.top_dict.get(16, [0])
|
||||
(charstring_pos,) = self.top_dict.get(17, [0])
|
||||
# CharStrings
|
||||
self.fp.seek(charstring_pos)
|
||||
self.charstring = self.INDEX(self.fp)
|
||||
self.nglyphs = len(self.charstring)
|
||||
# Encodings
|
||||
self.code2gid = {}
|
||||
self.gid2code = {}
|
||||
self.fp.seek(encoding_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
for (code, gid) in enumerate(struct.unpack('B'*n, self.fp.read(n))):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
code = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
code += 1
|
||||
else:
|
||||
raise ValueError('unsupported encoding format: %r' % format)
|
||||
# Charsets
|
||||
self.name2gid = {}
|
||||
self.gid2name = {}
|
||||
self.fp.seek(charset_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
n = self.nglyphs-1
|
||||
for (gid, sid) in enumerate(struct.unpack('>'+'H'*n, self.fp.read(2*n))):
|
||||
gid += 1
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
sid = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
sid += 1
|
||||
elif format == b'\x02':
|
||||
# Format 2
|
||||
assert False, str(('Unhandled', format))
|
||||
else:
|
||||
raise ValueError('unsupported charset format: %r' % format)
|
||||
#print self.code2gid
|
||||
#print self.name2gid
|
||||
#assert 0
|
||||
return
|
||||
|
||||
def getstr(self, sid):
|
||||
if sid < len(self.STANDARD_STRINGS):
|
||||
return self.STANDARD_STRINGS[sid]
|
||||
return self.string_index[sid-len(self.STANDARD_STRINGS)]
|
||||
@@ -0,0 +1,140 @@
|
||||
# PDFFont
|
||||
from abc import abstractmethod
|
||||
from io import BytesIO
|
||||
|
||||
from pdfmajor.interpreter.EncodingDB import EncodingDB
|
||||
from pdfmajor.interpreter.PSStackParser import LIT
|
||||
from pdfmajor.interpreter.PSStackParser import PSLiteral
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFStream import resolve1
|
||||
from pdfmajor.interpreter.PDFStream import int_value
|
||||
from pdfmajor.interpreter.PDFStream import num_value
|
||||
from pdfmajor.interpreter.PDFStream import list_value
|
||||
from pdfmajor.interpreter.PDFStream import PDFException
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream
|
||||
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
|
||||
from pdfmajor.interpreter.cmapdb import CMapParser
|
||||
|
||||
## Literals
|
||||
##
|
||||
LITERAL_STANDARD_ENCODING = LIT('StandardEncoding')
|
||||
LITERAL_TYPE1C = LIT('Type1C')
|
||||
|
||||
|
||||
class PDFFont(object):
|
||||
class PDFFontError(PDFException): pass
|
||||
class PDFUnicodeNotDefined(PDFFontError): pass
|
||||
|
||||
def __init__(self, descriptor, widths, default_width=None):
|
||||
self.descriptor = descriptor
|
||||
self.widths = widths
|
||||
self.fontname = resolve1(descriptor.get('FontName', 'unknown'))
|
||||
if isinstance(self.fontname, PSLiteral):
|
||||
self.fontname = literal_name(self.fontname)
|
||||
self.flags = int_value(descriptor.get('Flags', 0))
|
||||
self.ascent = num_value(descriptor.get('Ascent', 0))
|
||||
self.descent = num_value(descriptor.get('Descent', 0))
|
||||
self.italic_angle = num_value(descriptor.get('ItalicAngle', 0))
|
||||
self.default_width = default_width or num_value(descriptor.get('MissingWidth', 0))
|
||||
self.leading = num_value(descriptor.get('Leading', 0))
|
||||
self.bbox = list_value(descriptor.get('FontBBox', (0, 0, 0, 0)))
|
||||
self.hscale = self.vscale = .001
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFFont>'
|
||||
|
||||
def is_vertical(self):
|
||||
return False
|
||||
|
||||
def is_multibyte(self):
|
||||
return False
|
||||
|
||||
def decode(self, bytes):
|
||||
return bytearray(bytes) # map(ord, bytes)
|
||||
|
||||
def get_ascent(self):
|
||||
return self.ascent * self.vscale
|
||||
|
||||
def get_descent(self):
|
||||
return self.descent * self.vscale
|
||||
|
||||
def get_width(self):
|
||||
w = self.bbox[2]-self.bbox[0]
|
||||
if w == 0:
|
||||
w = -self.default_width
|
||||
return w * self.hscale
|
||||
|
||||
def get_height(self):
|
||||
h = self.bbox[3]-self.bbox[1]
|
||||
if h == 0:
|
||||
h = self.ascent - self.descent
|
||||
return h * self.vscale
|
||||
|
||||
def char_width(self, cid):
|
||||
try:
|
||||
return self.widths[cid] * self.hscale
|
||||
except KeyError:
|
||||
try:
|
||||
return self.widths[self.to_unichr(cid)] * self.hscale
|
||||
except (KeyError, self.PDFUnicodeNotDefined):
|
||||
return self.default_width * self.hscale
|
||||
|
||||
def char_disp(self, cid):
|
||||
return 0
|
||||
|
||||
def string_width(self, s):
|
||||
return sum(self.char_width(cid) for cid in self.decode(s))
|
||||
|
||||
@abstractmethod
|
||||
def to_unichr(self, cid):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def font_weight(self):
|
||||
if 'FontWeight' in self.descriptor.keys():
|
||||
return self.descriptor['FontWeight']
|
||||
elif 'bold' in self.fontname.lower():
|
||||
return 'bold'
|
||||
else:
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_bold(self):
|
||||
return self.font_weight == 'bold' or self.font_weight > 549
|
||||
|
||||
# PDFSimpleFont
|
||||
class PDFSimpleFont(PDFFont):
|
||||
|
||||
def __init__(self, descriptor, widths, spec):
|
||||
# Font encoding is specified either by a name of
|
||||
# built-in encoding or a dictionary that describes
|
||||
# the differences.
|
||||
if 'Encoding' in spec:
|
||||
encoding = resolve1(spec['Encoding'])
|
||||
else:
|
||||
encoding = LITERAL_STANDARD_ENCODING
|
||||
if isinstance(encoding, dict):
|
||||
name = literal_name(encoding.get('BaseEncoding', LITERAL_STANDARD_ENCODING))
|
||||
diff = list_value(encoding.get('Differences', []))
|
||||
self.cid2unicode = EncodingDB.get_encoding(name, diff)
|
||||
else:
|
||||
self.cid2unicode = EncodingDB.get_encoding(literal_name(encoding))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
PDFFont.__init__(self, descriptor, widths)
|
||||
return
|
||||
|
||||
def to_unichr(self, cid):
|
||||
if self.unicode_map:
|
||||
try:
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
return self.cid2unicode[cid]
|
||||
except KeyError:
|
||||
raise self.PDFUnicodeNotDefined(None, cid)
|
||||
@@ -0,0 +1,89 @@
|
||||
## TrueTypeFont
|
||||
##
|
||||
import struct
|
||||
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
|
||||
|
||||
class TrueTypeFont(object):
|
||||
|
||||
class CMapNotFound(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
self.tables = {}
|
||||
self.fonttype = fp.read(4)
|
||||
try:
|
||||
(ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
for _ in range(ntables):
|
||||
(name, _, offset, length) = struct.unpack('>4sLLL', fp.read(16))
|
||||
self.tables[name] = (offset, length)
|
||||
except struct.error:
|
||||
# Do not fail if there are not enough bytes to read. Even for
|
||||
# corrupted PDFs we would like to get as much information as
|
||||
# possible, so continue.
|
||||
pass
|
||||
return
|
||||
|
||||
def create_unicode_map(self):
|
||||
if 'cmap' not in self.tables:
|
||||
raise TrueTypeFont.CMapNotFound
|
||||
(base_offset, _) = self.tables['cmap']
|
||||
fp = self.fp
|
||||
fp.seek(base_offset)
|
||||
(_, nsubtables) = struct.unpack('>HH', fp.read(4))
|
||||
subtables = []
|
||||
for i in range(nsubtables):
|
||||
subtables.append(struct.unpack('>HHL', fp.read(8)))
|
||||
char2gid = {}
|
||||
# Only supports subtable type 0, 2 and 4.
|
||||
for (_1, _2, st_offset) in subtables:
|
||||
fp.seek(base_offset+st_offset)
|
||||
(fmttype, _, _) = struct.unpack('>HHH', fp.read(6))
|
||||
if fmttype == 0:
|
||||
char2gid.update(enumerate(struct.unpack('>256B', fp.read(256))))
|
||||
elif fmttype == 2:
|
||||
subheaderkeys = struct.unpack('>256H', fp.read(512))
|
||||
firstbytes = [0]*8192
|
||||
for (i, k) in enumerate(subheaderkeys):
|
||||
firstbytes[k//8] = i
|
||||
nhdrs = max(subheaderkeys)//8 + 1
|
||||
hdrs = []
|
||||
for i in range(nhdrs):
|
||||
(firstcode, entcount, delta, offset) = struct.unpack('>HHhH', fp.read(8))
|
||||
hdrs.append((i, firstcode, entcount, delta, fp.tell()-2+offset))
|
||||
for (i, firstcode, entcount, delta, pos) in hdrs:
|
||||
if not entcount:
|
||||
continue
|
||||
first = firstcode + (firstbytes[i] << 8)
|
||||
fp.seek(pos)
|
||||
for c in range(entcount):
|
||||
gid = struct.unpack('>H', fp.read(2))
|
||||
if gid:
|
||||
gid += delta
|
||||
char2gid[first+c] = gid
|
||||
elif fmttype == 4:
|
||||
(segcount, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
segcount //= 2
|
||||
ecs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
fp.read(2)
|
||||
scs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
idds = struct.unpack('>%dh' % segcount, fp.read(2*segcount))
|
||||
pos = fp.tell()
|
||||
idrs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
for (ec, sc, idd, idr) in zip(ecs, scs, idds, idrs):
|
||||
if idr:
|
||||
fp.seek(pos+idr)
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (struct.unpack('>H', fp.read(2))[0] + idd) & 0xffff
|
||||
else:
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (c + idd) & 0xffff
|
||||
else:
|
||||
assert False, str(('Unhandled', fmttype))
|
||||
# create unicode map
|
||||
unicode_map = FileUnicodeMap()
|
||||
for (char, gid) in char2gid.items():
|
||||
unicode_map.add_cid2unichr(gid, char)
|
||||
return unicode_map
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from pdfmajor.interpreter.PSStackParser import PSStackParser
|
||||
from pdfmajor.interpreter.PSStackParser import PSEOF, KWD
|
||||
from pdfmajor.interpreter.PSStackParser import PSLiteral
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.EncodingDB import name2unicode
|
||||
|
||||
## Type1FontHeaderParser
|
||||
##
|
||||
class Type1FontHeaderParser(PSStackParser):
|
||||
|
||||
KEYWORD_BEGIN = KWD(b'begin')
|
||||
KEYWORD_END = KWD(b'end')
|
||||
KEYWORD_DEF = KWD(b'def')
|
||||
KEYWORD_PUT = KWD(b'put')
|
||||
KEYWORD_DICT = KWD(b'dict')
|
||||
KEYWORD_ARRAY = KWD(b'array')
|
||||
KEYWORD_READONLY = KWD(b'readonly')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
|
||||
def __init__(self, data):
|
||||
PSStackParser.__init__(self, data)
|
||||
self._cid2unicode = {}
|
||||
return
|
||||
|
||||
def get_encoding(self):
|
||||
while 1:
|
||||
try:
|
||||
(cid, name) = self.nextobject()
|
||||
except PSEOF:
|
||||
break
|
||||
try:
|
||||
self._cid2unicode[cid] = name2unicode(name)
|
||||
except KeyError:
|
||||
pass
|
||||
return self._cid2unicode
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_PUT:
|
||||
((_, key), (_, value)) = self.pop(2) # pylint: disable=E0632
|
||||
if (isinstance(key, int) and
|
||||
isinstance(value, PSLiteral)):
|
||||
self.add_results((key, literal_name(value)))
|
||||
return
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
from pdfmajor.utils import get_logger, settings
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFStream import list_value
|
||||
from pdfmajor.interpreter.PDFStream import dict_value
|
||||
from pdfmajor.interpreter.PDFStream import resolve1
|
||||
from pdfmajor.interpreter.constants import LITERAL_FONT
|
||||
|
||||
from .fonts import PDFType1Font, PDFTrueTypeFont, PDFType3Font, PDFCIDFont, PDFFont
|
||||
|
||||
log = get_logger(__file__)
|
||||
|
||||
def get_font(objid: int, spec: dict, cached_fonts: dict = {}):
|
||||
if objid and objid in cached_fonts:
|
||||
font = cached_fonts[objid]
|
||||
else:
|
||||
log.debug('get_font: create: objid=%r, spec=%r', objid, spec)
|
||||
if settings.STRICT:
|
||||
if spec['Type'] is not LITERAL_FONT:
|
||||
raise PDFFont.PDFFontError('Type is not /Font')
|
||||
# Create a Font object.
|
||||
if 'Subtype' in spec:
|
||||
subtype = literal_name(spec['Subtype'])
|
||||
else:
|
||||
if settings.STRICT:
|
||||
raise PDFFont.PDFFontError('Font Subtype is not specified.')
|
||||
subtype = 'Type1'
|
||||
if subtype in ('Type1', 'MMType1'):
|
||||
# Type1 Font
|
||||
font = PDFType1Font(spec)
|
||||
elif subtype == 'TrueType':
|
||||
# TrueType Font
|
||||
font = PDFTrueTypeFont(spec)
|
||||
elif subtype == 'Type3':
|
||||
# Type3 Font
|
||||
font = PDFType3Font(spec)
|
||||
elif subtype in ('CIDFontType0', 'CIDFontType2'):
|
||||
# CID Font
|
||||
font = PDFCIDFont(spec)
|
||||
elif subtype == 'Type0':
|
||||
# Type0 Font
|
||||
dfonts = list_value(spec['DescendantFonts'])
|
||||
assert dfonts
|
||||
subspec = dict_value(dfonts[0]).copy()
|
||||
for k in ('Encoding', 'ToUnicode'):
|
||||
if k in spec:
|
||||
subspec[k] = resolve1(spec[k])
|
||||
font = get_font(None, subspec, cached_fonts)
|
||||
else:
|
||||
if settings.STRICT:
|
||||
raise PDFFont.PDFFontError('Invalid Font spec: %r' % spec)
|
||||
font = PDFType1Font(spec) # this is so wrong!
|
||||
if objid:
|
||||
cached_fonts[objid] = font
|
||||
return font
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
from io import BytesIO
|
||||
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.PDFStream import int_value
|
||||
from pdfmajor.interpreter.PDFStream import num_value
|
||||
from pdfmajor.interpreter.PDFStream import list_value
|
||||
from pdfmajor.interpreter.PDFStream import dict_value
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream
|
||||
from pdfmajor.interpreter.PDFStream import resolve1
|
||||
from pdfmajor.interpreter.cmapdb import CMap, CMapDB, CMapParser
|
||||
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
|
||||
from pdfmajor.utils import settings, apply_matrix_norm
|
||||
|
||||
from .PDFFont import PDFFont, PDFSimpleFont
|
||||
from .util import FontMetricsDB, get_widths, get_widths2
|
||||
from .Type1FontHeaderParser import Type1FontHeaderParser
|
||||
from .TrueTypeFont import TrueTypeFont
|
||||
|
||||
# PDFType1Font
|
||||
class PDFType1Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, spec):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise self.PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
try:
|
||||
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
|
||||
except KeyError:
|
||||
descriptor = dict_value(spec.get('FontDescriptor', {}))
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 255))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
if 'Encoding' not in spec and 'FontFile' in descriptor:
|
||||
# try to recover the missing encoding info from the font file.
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile'))
|
||||
length1 = int_value(self.fontfile['Length1'])
|
||||
data = self.fontfile.get_data()[:length1]
|
||||
parser = Type1FontHeaderParser(BytesIO(data))
|
||||
self.cid2unicode = parser.get_encoding()
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType1Font: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFTrueTypeFont
|
||||
class PDFTrueTypeFont(PDFType1Font):
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFTrueTypeFont: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFType3Font
|
||||
class PDFType3Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, spec):
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 0))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
if 'FontDescriptor' in spec:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
else:
|
||||
descriptor = {'Ascent': 0, 'Descent': 0,
|
||||
'FontBBox': spec['FontBBox']}
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
self.matrix = tuple(list_value(spec.get('FontMatrix')))
|
||||
(_, self.descent, _, self.ascent) = self.bbox
|
||||
(self.hscale, self.vscale) = apply_matrix_norm(self.matrix, (1, 1))
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType3Font>'
|
||||
|
||||
|
||||
# PDFCIDFont
|
||||
class PDFCIDFont(PDFFont):
|
||||
|
||||
def __init__(self, spec, strict=settings.STRICT):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise self.PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {}))
|
||||
self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"),
|
||||
resolve1(self.cidsysteminfo.get('Ordering', b'unknown')).decode("latin1"))
|
||||
try:
|
||||
name = literal_name(spec['Encoding'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise self.PDFFontError('Encoding is unspecified')
|
||||
name = 'unknown'
|
||||
try:
|
||||
self.cmap = CMapDB.get_cmap(name)
|
||||
except CMapDB.CMapNotFound as e:
|
||||
if strict:
|
||||
raise self.PDFFontError(e)
|
||||
self.cmap = CMap()
|
||||
try:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise self.PDFFontError('FontDescriptor is missing')
|
||||
descriptor = {}
|
||||
ttf = None
|
||||
if 'FontFile2' in descriptor:
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile2'))
|
||||
ttf = TrueTypeFont(self.basefont,
|
||||
BytesIO(self.fontfile.get_data()))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
elif self.cidcoding in ('Adobe-Identity', 'Adobe-UCS'):
|
||||
if ttf:
|
||||
try:
|
||||
self.unicode_map = ttf.create_unicode_map()
|
||||
except TrueTypeFont.CMapNotFound:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
|
||||
except CMapDB.CMapNotFound as e:
|
||||
pass
|
||||
|
||||
self.vertical = self.cmap.is_vertical()
|
||||
if self.vertical:
|
||||
# writing mode: vertical
|
||||
widths = get_widths2(list_value(spec.get('W2', [])))
|
||||
self.disps = dict((cid, (vx, vy)) for (cid, (_, (vx, vy))) in iter(widths.items()))
|
||||
(vy, w) = spec.get('DW2', [880, -1000])
|
||||
self.default_disp = (None, vy)
|
||||
widths = dict((cid, w) for (cid, (w, _)) in iter(widths.items()))
|
||||
default_width = w
|
||||
else:
|
||||
# writing mode: horizontal
|
||||
self.disps = {}
|
||||
self.default_disp = 0
|
||||
widths = get_widths(list_value(spec.get('W', [])))
|
||||
default_width = spec.get('DW', 1000)
|
||||
PDFFont.__init__(self, descriptor, widths, default_width=default_width)
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFCIDFont: basefont=%r, cidcoding=%r>' % (self.basefont, self.cidcoding)
|
||||
|
||||
def is_vertical(self):
|
||||
return self.vertical
|
||||
|
||||
def is_multibyte(self):
|
||||
return True
|
||||
|
||||
def decode(self, bytes):
|
||||
return self.cmap.decode(bytes)
|
||||
|
||||
def char_disp(self, cid):
|
||||
"Returns an integer for horizontal fonts, a tuple for vertical fonts."
|
||||
return self.disps.get(cid, self.default_disp)
|
||||
|
||||
def to_unichr(self, cid):
|
||||
try:
|
||||
if not self.unicode_map:
|
||||
raise KeyError(cid)
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
raise self.PDFUnicodeNotDefined(self.cidcoding, cid)
|
||||
@@ -0,0 +1,57 @@
|
||||
from pdfmajor.utils import FONT_METRICS, choplist, isnumber
|
||||
|
||||
|
||||
def get_widths(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, w) in enumerate(v):
|
||||
widths[char1+i] = w
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 3:
|
||||
(char1, char2, w) = r
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = w
|
||||
r = []
|
||||
return widths
|
||||
assert get_widths([1]) == {}
|
||||
assert get_widths([1,2,3]) == {1:3, 2:3}
|
||||
assert get_widths([1,[2,3],6,[7,8]]) == {1:2,2:3, 6:7,7:8}
|
||||
|
||||
def get_widths2(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, (w, vx, vy)) in enumerate(choplist(3, v)):
|
||||
widths[char1+i] = (w, (vx, vy))
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 5:
|
||||
(char1, char2, w, vx, vy) = r # pylint: disable=E0632
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = (w, (vx, vy))
|
||||
r = []
|
||||
return widths
|
||||
assert get_widths2([1]) == {}
|
||||
assert get_widths2([1,2,3,4,5]) == {1:(3, (4,5)), 2:(3, (4,5))}
|
||||
assert get_widths2([1,[2,3,4,5],6,[7,8,9]]) == {1:(2, (3,4)), 6:(7, (8,9))}
|
||||
|
||||
|
||||
## FontMetricsDB
|
||||
##
|
||||
class FontMetricsDB(object):
|
||||
|
||||
@classmethod
|
||||
def get_metrics(klass, fontname):
|
||||
return FONT_METRICS[fontname]
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from .PDFFont import PDFFont
|
||||
from .PDFFont import PDFFont, get_font
|
||||
from pdfmajor.utils import MATRIX_IDENTITY
|
||||
|
||||
## PDFTextState
|
||||
|
||||
@@ -5,10 +5,17 @@ from .PDFGraphicState import PDFGraphicState
|
||||
from .PDFGraphicState.PDFColor import PDFColor
|
||||
from .PDFGraphicState.PDFColorSpace import PREDEFINED_COLORSPACE, OrderedDict, PDFColorSpace
|
||||
|
||||
from .PDFTextState import PDFTextState
|
||||
from .PDFTextState import PDFTextState, PDFFont, get_font
|
||||
|
||||
from .Curves import CurvePath
|
||||
from .PDFItem import PDFItem, PDFImage, PDFShape, PDFXObject, PDFText
|
||||
|
||||
from .layout import LTItem, LTComponent, LTContainer
|
||||
from .layout import LTCharBlock, LTChar
|
||||
from .layout import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
|
||||
from .layout import LTXObject
|
||||
from .layout import LTImage
|
||||
|
||||
from .layout import make_char_block, make_curve, make_image, make_xobject
|
||||
|
||||
class PDFStateStack:
|
||||
def __init__(self):
|
||||
@@ -18,7 +25,7 @@ class PDFStateStack:
|
||||
self.graphicstack : List[PDFGraphicState] = []
|
||||
self.curvestacks: List[CurvePath] = []
|
||||
self.argstack: List[bytes] = []
|
||||
self.complete_items : List[PDFItem] = []
|
||||
self.complete_layout_items : List[LTItem] = []
|
||||
self.colorspace_map: OrderedDict = PREDEFINED_COLORSPACE.copy()
|
||||
self.fontmap = {}
|
||||
self.xobjmap = {}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
from pdfmajor.utils import Bbox
|
||||
from ..state.PDFGraphicState import PDFGraphicStateColor
|
||||
from ..state.Curves import CurvePath
|
||||
from ..PDFGraphicState import PDFColor
|
||||
from ..Curves import CurvePath
|
||||
|
||||
from ._base import LTComponent, Bbox
|
||||
|
||||
@@ -13,8 +13,8 @@ class LTCurve(LTComponent):
|
||||
linewidth: float,
|
||||
paths: List[CurvePath],
|
||||
bbox: Bbox,
|
||||
stroke: PDFGraphicStateColor,
|
||||
fill: PDFGraphicStateColor,
|
||||
stroke: PDFColor,
|
||||
fill: PDFColor,
|
||||
evenodd: bool,
|
||||
):
|
||||
LTComponent.__init__(self, bbox)
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
from typing import List
|
||||
|
||||
from ._base import LTContainer
|
||||
from pdfmajor.utils import Bbox, Point, matrix2str
|
||||
|
||||
## LTFigure
|
||||
##
|
||||
class LTFigure(LTContainer):
|
||||
|
||||
def __init__(self, name: str, pts: List[Point], matrix):
|
||||
LTContainer.__init__(self, Bbox.from_points(pts))
|
||||
self.name = name
|
||||
self.matrix = matrix
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s(%s) %s matrix=%s>' %
|
||||
(self.__class__.__name__, self.name,
|
||||
str(self.bbox), matrix2str(self.matrix)))
|
||||
@@ -1,5 +1,5 @@
|
||||
from pdfmajor.utils import Bbox
|
||||
from ..interpreter.PDFStream import PDFStream
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream
|
||||
from ._base import LTComponent
|
||||
|
||||
## LTImage
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
from ._base import LTContainer
|
||||
from ..utils import Bbox
|
||||
|
||||
class LTPage(LTContainer):
|
||||
"""A class reprensting an extracted page from the pdf
|
||||
Arguments:
|
||||
pageid {int} -- page number
|
||||
bbox {Bbox} -- Boundary Box
|
||||
rotate {float} -- rotation of the page (default: {0})
|
||||
"""
|
||||
|
||||
def __init__(self, pageid: int, bbox: Bbox, rotate: float=0):
|
||||
LTContainer.__init__(self, bbox)
|
||||
self.pageid = pageid
|
||||
self.rotate = rotate
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s(%r) %s rotate=%r>' %
|
||||
(self.__class__.__name__, self.pageid,
|
||||
str(self.bbox), self.rotate))
|
||||
@@ -0,0 +1,22 @@
|
||||
from typing import List
|
||||
|
||||
from pdfmajor.utils import Bbox, Point, matrix2str
|
||||
from pdfmajor.interpreter.PDFStream import PDFStream
|
||||
|
||||
from ._base import LTContainer
|
||||
|
||||
## LTXObject
|
||||
##
|
||||
class LTXObject(LTContainer):
|
||||
|
||||
def __init__(self, name: str, bbox: Bbox, xobj_stream: PDFStream, resources: dict, t_matrix: List[Point] ):
|
||||
LTContainer.__init__(self, bbox)
|
||||
self.name = name
|
||||
self.stream = xobj_stream
|
||||
self.t_matrix = t_matrix
|
||||
self.resources = resources
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s(%s) %s matrix=%s>' %
|
||||
(self.__class__.__name__, self.name,
|
||||
str(self.bbox), matrix2str(self.t_matrix)))
|
||||
@@ -1,178 +0,0 @@
|
||||
import logging
|
||||
import re
|
||||
import io
|
||||
|
||||
from typing import Tuple, List
|
||||
|
||||
from ._base import LTItem
|
||||
from .LTPage import LTPage
|
||||
from .LTCurves import LTCurve, LTLine, LTRect, LTHorizontalLine, LTVerticalLine
|
||||
from .LTFigure import LTFigure
|
||||
from .LTImage import LTImage
|
||||
from .LTChar import LTChar
|
||||
from .LTCharBlock import LTCharBlock
|
||||
|
||||
from pdfmajor.utils import Point, Bbox, INF
|
||||
from pdfmajor.utils import apply_matrix_pt, mult_matrix
|
||||
|
||||
from ..interpreter.PDFFont import PDFUnicodeNotDefined
|
||||
from ..interpreter.PDFResourceManager import PDFResourceManager
|
||||
from ..interpreter.PDFGraphicState import PDFGraphicState, PDFGraphicStateColor
|
||||
from ..interpreter.PDFdevice import PDFTextDevice
|
||||
from ..interpreter.PDFTextState import PDFTextState
|
||||
from ..interpreter.PDFColorSpace import PDFColorSpace
|
||||
from ..interpreter.PDFPage import PDFPage
|
||||
from ..interpreter.PDFStream import PDFStream
|
||||
from ..interpreter.types import CurvePath
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
## PDFLayoutAnalyzer
|
||||
##
|
||||
class PDFPageLayoutAnalyzer:
|
||||
|
||||
|
||||
def __init__(self, page: PDFPage, ctm: Tuple[float], pageno: int=1):
|
||||
self.page = page
|
||||
self.pageno = pageno
|
||||
self._stack = []
|
||||
self.ctm = ctm
|
||||
(x0, y0, x1, y1) = page.mediabox
|
||||
(x0, y0) = apply_matrix_pt(ctm, (x0, y0))
|
||||
(x1, y1) = apply_matrix_pt(ctm, (x1, y1))
|
||||
self.cur_item = LTPage(
|
||||
self.pageno,
|
||||
Bbox( 0, 0, abs(x0-x1), abs(y0-y1) )
|
||||
)
|
||||
|
||||
def done(self):
|
||||
assert not self._stack, str(len(self._stack))
|
||||
assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
|
||||
return self.cur_item
|
||||
|
||||
def begin_figure(self, name: str, bbox: List[Point], matrix: List[Point]):
|
||||
self._stack.append(self.cur_item)
|
||||
|
||||
matrix = mult_matrix(matrix, self.ctm)
|
||||
(x, y, w, h) = bbox
|
||||
|
||||
self.cur_item = LTFigure(name, [
|
||||
apply_matrix_pt(matrix, (p, q))
|
||||
for (p, q) in ((x, y), (x+w, y), (x, y+h), (x+w, y+h))
|
||||
], matrix)
|
||||
|
||||
def end_figure(self, name: str):
|
||||
fig = self.cur_item
|
||||
assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
|
||||
self.cur_item = self._stack.pop()
|
||||
self.cur_item.add(fig)
|
||||
return
|
||||
|
||||
def render_image(self, name: str, stream: PDFStream):
|
||||
assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
|
||||
item = LTImage(
|
||||
name,
|
||||
stream,
|
||||
Bbox(
|
||||
self.cur_item.x0, self.cur_item.y0,
|
||||
self.cur_item.x1, self.cur_item.y1
|
||||
)
|
||||
)
|
||||
self.cur_item.add(item)
|
||||
return
|
||||
|
||||
def paint_path(self, gstate: PDFGraphicState, evenodd: bool, paths: List[CurvePath]):
|
||||
shape = ''.join(x.method.value for x in paths)
|
||||
if shape == 'ml':
|
||||
# horizontal/vertical line
|
||||
pt0 = paths[0].points[0]
|
||||
pt1 = paths[1].points[0]
|
||||
(x0, y0) = apply_matrix_pt(self.ctm, (pt0.x, pt0.y))
|
||||
(x1, y1) = apply_matrix_pt(self.ctm, (pt1.x, pt1.y))
|
||||
if x0 == x1:
|
||||
self.cur_item.add(LTVerticalLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
))
|
||||
elif y0 == y1:
|
||||
self.cur_item.add(LTHorizontalLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
))
|
||||
else:
|
||||
self.cur_item.add(LTLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
))
|
||||
elif shape == 'mlllh':
|
||||
# rectangle
|
||||
pt0 = paths[0].points[0]
|
||||
pt1 = paths[1].points[0]
|
||||
pt2 = paths[2].points[0]
|
||||
pt3 = paths[3].points[0]
|
||||
(x0, y0) = apply_matrix_pt(self.ctm, (pt0.x, pt0.y))
|
||||
(x1, y1) = apply_matrix_pt(self.ctm, (pt1.x, pt1.y))
|
||||
(x2, y2) = apply_matrix_pt(self.ctm, (pt2.x, pt2.y))
|
||||
(x3, y3) = apply_matrix_pt(self.ctm, (pt3.x, pt3.y))
|
||||
if ((x0 == x1 and y1 == y2 and x2 == x3 and y3 == y0) or
|
||||
(y0 == y1 and x1 == x2 and y2 == y3 and x3 == x0)):
|
||||
self.cur_item.add(LTRect(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox(
|
||||
x0=min(x0,x1,x2,x3),
|
||||
x1=max(x0,x1,x2,x3),
|
||||
y0=min(y0,y1,y2,y3),
|
||||
y1=max(y0,y1,y2,y3),
|
||||
),
|
||||
evenodd=evenodd,
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
))
|
||||
else:
|
||||
(x0, y0, x1, y1) = (INF, INF, -INF, -INF)
|
||||
for path in paths:
|
||||
if path.method in [CurvePath.METHOD.MOVE_TO, CurvePath.METHOD.LINE_TO]:
|
||||
x0 = min(x0, path.points[0].x)
|
||||
y0 = min(y0, path.points[0].y)
|
||||
x1 = max(x1, path.points[0].x)
|
||||
y1 = max(y1, path.points[0].y)
|
||||
elif path.method in [CurvePath.METHOD.CURVE_BOTH_TO]:
|
||||
x0 = min(x0, path.points[2].x)
|
||||
y0 = min(y0, path.points[2].y)
|
||||
x1 = max(x1, path.points[2].x)
|
||||
y1 = max(y1, path.points[2].y)
|
||||
elif path.method in [CurvePath.METHOD.CURVE_NEXT_TO, CurvePath.METHOD.CURVE_FIRST_TO]:
|
||||
x0 = min(x0, path.points[1].x)
|
||||
y0 = min(y0, path.points[1].y)
|
||||
x1 = max(x1, path.points[1].x)
|
||||
y1 = max(y1, path.points[1].y)
|
||||
self.cur_item.add(LTCurve(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox(x0,y0,x1,y1),
|
||||
evenodd=evenodd,
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor
|
||||
))
|
||||
|
||||
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
||||
self.cur_item.add(LTCharBlock(
|
||||
chars=chars,
|
||||
textstate=textstate,
|
||||
ncs=ncs,
|
||||
graphicstate=graphicstate
|
||||
))
|
||||
@@ -1,5 +1,5 @@
|
||||
from typing import List
|
||||
from pdfmajor.utils import apply_matrix_pt, INF, Bbox
|
||||
from pdfmajor.utils import apply_matrix_pt, INF, Bbox, Point, MATRIX_IDENTITY, mult_matrix
|
||||
|
||||
from ..Curves import CurvePath
|
||||
from ..PDFGraphicState import PDFGraphicState, PDFColor
|
||||
@@ -7,7 +7,9 @@ from ..PDFTextState import PDFTextState
|
||||
|
||||
from ._base import LTItem, LTComponent, LTContainer
|
||||
from .LTCharBlock import LTCharBlock
|
||||
from .LTCurves import LTCurve, LTHorizontalLine, LTRect, LTVerticalLine
|
||||
from .LTCurves import LTCurve, LTLine, LTHorizontalLine, LTRect, LTVerticalLine
|
||||
from .LTImage import LTImage
|
||||
from .LTXObject import LTXObject
|
||||
from .utils.textdecoder import decode_text_seq
|
||||
|
||||
|
||||
@@ -19,7 +21,7 @@ def make_char_block(seq: bytearray, ctm: tuple, textstate: PDFTextState, color:
|
||||
textstate=textstate
|
||||
)
|
||||
|
||||
def make_figure(ctm: tuple, gstate: PDFGraphicState, evenodd: bool, paths: List[CurvePath]):
|
||||
def make_curve(ctm: tuple, gstate: PDFGraphicState, evenodd: bool, paths: List[CurvePath]):
|
||||
shape = ''.join(x.method.value for x in paths)
|
||||
if shape == 'ml':
|
||||
# horizontal/vertical line
|
||||
@@ -105,3 +107,27 @@ def make_figure(ctm: tuple, gstate: PDFGraphicState, evenodd: bool, paths: List[
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor
|
||||
)
|
||||
|
||||
def make_image(obj, ctm):
|
||||
matrix = mult_matrix(MATRIX_IDENTITY, ctm)
|
||||
return LTImage(
|
||||
name=str(id(obj)),
|
||||
stream=obj,
|
||||
bbox=Bbox.from_points([
|
||||
apply_matrix_pt(matrix, (p, q))
|
||||
for (p, q) in ((0, 0), (1, 0), (0, 1), (1, 1))
|
||||
])
|
||||
)
|
||||
|
||||
def make_xobject(obj, bbox, ctm, matrix, resources):
|
||||
(x, y, w, h) = bbox
|
||||
return LTXObject(
|
||||
name=str(id(obj)),
|
||||
bbox=Bbox.from_points([
|
||||
apply_matrix_pt(matrix, (p, q))
|
||||
for (p, q) in ((x, y), (x+w, y), (x, y+h), (x+w, y+h))
|
||||
]),
|
||||
xobj_stream=obj,
|
||||
resources=resources,
|
||||
t_matrix = ctm
|
||||
)
|
||||
@@ -41,7 +41,7 @@ def __render_string_along(idx: int, seq: bytearray,
|
||||
textstate.linematrix[idx] += adv
|
||||
|
||||
char_meta_datas.append(
|
||||
( text, Bbox.from_points(bbox), )
|
||||
( text, bbox )
|
||||
)
|
||||
if cid == 32 and textstate.wordspace:
|
||||
textstate.linematrix[idx] += textstate.wordspace
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
from pdfmajor.interpreter.PDFParser import PDFStream
|
||||
from pdfmajor.interpreter.PDFStream import list_value, dict_value, resolve1
|
||||
from pdfmajor.interpreter.PDFStream.PDFObjRef import PDFObjRef
|
||||
from pdfmajor.interpreter.PSStackParser import literal_name
|
||||
from pdfmajor.interpreter.constants import LITERAL_PDF, LITERAL_TEXT
|
||||
|
||||
from .commands import PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .commands import PDFStateStack, get_font
|
||||
|
||||
def get_colorspace(spec):
|
||||
if isinstance(spec, list):
|
||||
name = literal_name(spec[0])
|
||||
else:
|
||||
name = literal_name(spec)
|
||||
if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, PDFStream.validated_stream(spec[1])['N'])
|
||||
elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, len(list_value(spec[1])))
|
||||
else:
|
||||
return PREDEFINED_COLORSPACE.get(name)
|
||||
|
||||
def get_procset(procs):
|
||||
for proc in procs:
|
||||
if proc is LITERAL_PDF:
|
||||
pass
|
||||
elif proc is LITERAL_TEXT:
|
||||
pass
|
||||
else:
|
||||
#raise PDFResourceError('ProcSet %r is not supported.' % proc)
|
||||
pass
|
||||
return
|
||||
|
||||
def init_resources(state: PDFStateStack, font_cache: dict = {}):
|
||||
for (k, v) in iter(dict_value(state.resources).items()):
|
||||
# log.debug('Resource: %r: %r', k, v)
|
||||
if k == 'Font':
|
||||
for (fontid, spec) in iter(dict_value(v).items()):
|
||||
objid = None
|
||||
if isinstance(spec, PDFObjRef):
|
||||
objid = spec.objid
|
||||
spec = dict_value(spec)
|
||||
state.fontmap[fontid] = get_font(objid, spec, font_cache)
|
||||
elif k == 'ColorSpace':
|
||||
for (csid, spec) in iter(dict_value(v).items()):
|
||||
state.colorspace_map[csid] = get_colorspace(resolve1(spec))
|
||||
elif k == 'ProcSet':
|
||||
get_procset(list_value(v))
|
||||
elif k == 'XObject':
|
||||
for (xobjid, xobjstrm) in iter(dict_value(v).items()):
|
||||
state.xobjmap[xobjid] = xobjstrm
|
||||
@@ -1,178 +0,0 @@
|
||||
import logging
|
||||
import re
|
||||
import io
|
||||
|
||||
from typing import Tuple, List
|
||||
|
||||
from ._base import LTItem
|
||||
from .LTPage import LTPage
|
||||
from .LTCurves import LTCurve, LTLine, LTRect, LTHorizontalLine, LTVerticalLine
|
||||
from .LTFigure import LTFigure
|
||||
from .LTImage import LTImage
|
||||
from .LTChar import LTChar
|
||||
from .LTCharBlock import LTCharBlock
|
||||
|
||||
from ..utils import Point, Bbox, INF
|
||||
from ..utils import apply_matrix_pt, mult_matrix
|
||||
|
||||
from ..interpreter.PDFFont import PDFUnicodeNotDefined
|
||||
from ..interpreter.PDFResourceManager import PDFResourceManager
|
||||
from ..interpreter.PDFGraphicState import PDFGraphicState, PDFGraphicStateColor
|
||||
from ..interpreter.PDFdevice import PDFTextDevice
|
||||
from ..interpreter.PDFTextState import PDFTextState
|
||||
from ..interpreter.PDFColorSpace import PDFColorSpace
|
||||
from ..interpreter.PDFPage import PDFPage
|
||||
from ..interpreter.PDFStream import PDFStream
|
||||
from ..interpreter.types import CurvePath
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
## PDFLayoutAnalyzer
|
||||
##
|
||||
class PDFPageLayoutAnalyzer:
|
||||
|
||||
|
||||
def __init__(self, page: PDFPage, ctm: Tuple[float], pageno: int=1):
|
||||
self.page = page
|
||||
self.pageno = pageno
|
||||
self._stack = []
|
||||
self.ctm = ctm
|
||||
(x0, y0, x1, y1) = page.mediabox
|
||||
(x0, y0) = apply_matrix_pt(ctm, (x0, y0))
|
||||
(x1, y1) = apply_matrix_pt(ctm, (x1, y1))
|
||||
self.cur_item = LTPage(
|
||||
self.pageno,
|
||||
Bbox( 0, 0, abs(x0-x1), abs(y0-y1) )
|
||||
)
|
||||
|
||||
def done(self):
|
||||
assert not self._stack, str(len(self._stack))
|
||||
assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
|
||||
return self.cur_item
|
||||
|
||||
def begin_figure(self, name: str, bbox: List[Point], matrix: List[Point]):
|
||||
self._stack.append(self.cur_item)
|
||||
|
||||
matrix = mult_matrix(matrix, self.ctm)
|
||||
(x, y, w, h) = bbox
|
||||
|
||||
self.cur_item = LTFigure(name, [
|
||||
apply_matrix_pt(matrix, (p, q))
|
||||
for (p, q) in ((x, y), (x+w, y), (x, y+h), (x+w, y+h))
|
||||
], matrix)
|
||||
|
||||
def end_figure(self, name: str):
|
||||
fig = self.cur_item
|
||||
assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
|
||||
self.cur_item = self._stack.pop()
|
||||
self.cur_item.add(fig)
|
||||
return
|
||||
|
||||
def render_image(self, name: str, stream: PDFStream):
|
||||
assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
|
||||
item = LTImage(
|
||||
name,
|
||||
stream,
|
||||
Bbox(
|
||||
self.cur_item.x0, self.cur_item.y0,
|
||||
self.cur_item.x1, self.cur_item.y1
|
||||
)
|
||||
)
|
||||
self.cur_item.add(item)
|
||||
return
|
||||
|
||||
def paint_path(self, gstate: PDFGraphicState, evenodd: bool, paths: List[CurvePath]):
|
||||
shape = ''.join(x.method.value for x in paths)
|
||||
if shape == 'ml':
|
||||
# horizontal/vertical line
|
||||
pt0 = paths[0].points[0]
|
||||
pt1 = paths[1].points[0]
|
||||
(x0, y0) = apply_matrix_pt(self.ctm, (pt0.x, pt0.y))
|
||||
(x1, y1) = apply_matrix_pt(self.ctm, (pt1.x, pt1.y))
|
||||
if x0 == x1:
|
||||
self.cur_item.add(LTVerticalLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
))
|
||||
elif y0 == y1:
|
||||
self.cur_item.add(LTHorizontalLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
))
|
||||
else:
|
||||
self.cur_item.add(LTLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
))
|
||||
elif shape == 'mlllh':
|
||||
# rectangle
|
||||
pt0 = paths[0].points[0]
|
||||
pt1 = paths[1].points[0]
|
||||
pt2 = paths[2].points[0]
|
||||
pt3 = paths[3].points[0]
|
||||
(x0, y0) = apply_matrix_pt(self.ctm, (pt0.x, pt0.y))
|
||||
(x1, y1) = apply_matrix_pt(self.ctm, (pt1.x, pt1.y))
|
||||
(x2, y2) = apply_matrix_pt(self.ctm, (pt2.x, pt2.y))
|
||||
(x3, y3) = apply_matrix_pt(self.ctm, (pt3.x, pt3.y))
|
||||
if ((x0 == x1 and y1 == y2 and x2 == x3 and y3 == y0) or
|
||||
(y0 == y1 and x1 == x2 and y2 == y3 and x3 == x0)):
|
||||
self.cur_item.add(LTRect(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox(
|
||||
x0=min(x0,x1,x2,x3),
|
||||
x1=max(x0,x1,x2,x3),
|
||||
y0=min(y0,y1,y2,y3),
|
||||
y1=max(y0,y1,y2,y3),
|
||||
),
|
||||
evenodd=evenodd,
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
))
|
||||
else:
|
||||
(x0, y0, x1, y1) = (INF, INF, -INF, -INF)
|
||||
for path in paths:
|
||||
if path.method in [CurvePath.METHOD.MOVE_TO, CurvePath.METHOD.LINE_TO]:
|
||||
x0 = min(x0, path.points[0].x)
|
||||
y0 = min(y0, path.points[0].y)
|
||||
x1 = max(x1, path.points[0].x)
|
||||
y1 = max(y1, path.points[0].y)
|
||||
elif path.method in [CurvePath.METHOD.CURVE_BOTH_TO]:
|
||||
x0 = min(x0, path.points[2].x)
|
||||
y0 = min(y0, path.points[2].y)
|
||||
x1 = max(x1, path.points[2].x)
|
||||
y1 = max(y1, path.points[2].y)
|
||||
elif path.method in [CurvePath.METHOD.CURVE_NEXT_TO, CurvePath.METHOD.CURVE_FIRST_TO]:
|
||||
x0 = min(x0, path.points[1].x)
|
||||
y0 = min(y0, path.points[1].y)
|
||||
x1 = max(x1, path.points[1].x)
|
||||
y1 = max(y1, path.points[1].y)
|
||||
self.cur_item.add(LTCurve(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox(x0,y0,x1,y1),
|
||||
evenodd=evenodd,
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor
|
||||
))
|
||||
|
||||
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
||||
self.cur_item.add(LTCharBlock(
|
||||
chars=chars,
|
||||
textstate=textstate,
|
||||
ncs=ncs,
|
||||
graphicstate=graphicstate
|
||||
))
|
||||
@@ -1,19 +0,0 @@
|
||||
import os
|
||||
|
||||
from pdfmajor.extractor import v2_extract_items_from_pdf
|
||||
from pdfmajor.interpreter_v2 import PDFText
|
||||
|
||||
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
|
||||
CUR_PATH = os.path.dirname(os.getcwd())
|
||||
|
||||
FILE_NAME = os.path.join(
|
||||
CUR_PATH,
|
||||
"samples/pdf/fonts.pdf"
|
||||
)
|
||||
|
||||
for page in v2_extract_items_from_pdf(FILE_NAME):
|
||||
for item in page:
|
||||
if isinstance(item, PDFText):
|
||||
print(item.chars)
|
||||
else:
|
||||
print(item)
|
||||
@@ -0,0 +1,22 @@
|
||||
import os
|
||||
|
||||
# from pdfmajor.extractor import extract_items_from_pdf
|
||||
from pdfmajor.interpreter_v2 import LTCharBlock, PDFInterpreter, logging
|
||||
from pdfmajor.interpreter_v2 import PageInterpreter
|
||||
from pdfmajor.interpreter_v2.commands import LTItem
|
||||
from pdfmajor.interpreter_v2.commands import LTCharBlock, LTChar
|
||||
|
||||
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
|
||||
|
||||
FILE_NAME = os.path.join(
|
||||
CUR_PATH,
|
||||
"samples/pdf/tables.pdf"
|
||||
)
|
||||
|
||||
for page in PDFInterpreter(FILE_NAME, debug_level=logging.INFO):
|
||||
assert isinstance(page, PageInterpreter)
|
||||
for item in page:
|
||||
assert isinstance(item, LTItem)
|
||||
if isinstance(item, LTCharBlock):
|
||||
for char in item:
|
||||
assert(char, LTChar)
|
||||
Reference in new issue
Block a user