Skip to content

Commit

Permalink
A few style improvements (CC #212)
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Apr 23, 2023
1 parent 82d2a12 commit e418360
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
7 changes: 1 addition & 6 deletions src/pypdfium2/_helpers/bitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ def new_foreign(cls, width, height, format, rev_byteorder=False, force_packed=Fa
Using this method is discouraged. Prefer :meth:`.new_native` instead.
"""

if force_packed:
stride = width * consts.BitmapTypeToNChannels[format]
else:
stride = 0

stride = width * consts.BitmapTypeToNChannels[format] if force_packed else 0
raw = pdfium_c.FPDFBitmap_CreateEx(width, height, format, None, stride)
return cls.from_raw(raw, rev_byteorder)

Expand Down
21 changes: 8 additions & 13 deletions src/pypdfium2/_helpers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def init_forms(self, config=None):
self.formenv = PdfFormEnv(raw, config, self)


# ?TODO(v5) consider cached property
# TODO?(v5) consider cached property
def get_formtype(self):
"""
Returns:
Expand All @@ -173,7 +173,7 @@ def get_formtype(self):
return pdfium_c.FPDF_GetFormType(self)


# ?TODO(v5) consider cached property
# TODO?(v5) consider cached property
def get_pagemode(self):
"""
Returns:
Expand All @@ -182,7 +182,7 @@ def get_pagemode(self):
return pdfium_c.FPDFDoc_GetPageMode(self)


# ?TODO(v5) consider cached property
# TODO?(v5) consider cached property
def is_tagged(self):
"""
Returns:
Expand Down Expand Up @@ -469,7 +469,8 @@ def page_as_xobject(self, index, dest_pdf):
)


# TODO(v5) consider switching to a wrapper around the raw bookmark with on-demand cached properties
# TODO(v5) consider switching to a wrapper class around the raw bookmark
# (either with getter methods, or possibly cached properties)
def _get_bookmark(self, bookmark, level):

n_bytes = pdfium_c.FPDFBookmark_GetTitle(bookmark, None, 0)
Expand Down Expand Up @@ -555,23 +556,17 @@ def _process_page(cls, index, input_data, password, renderer, converter, pass_in
password = password,
)
if need_formenv:
if mk_formconfig:
pdf.init_forms(config=mk_formconfig())
else:
pdf.init_forms()
page = pdf[index]
pdf.init_forms(config=mk_formconfig()) if mk_formconfig else pdf.init_forms()

page = pdf[index]
bitmap = renderer(page, **kwargs)
info = bitmap.get_info()
result = converter(bitmap)

for g in (bitmap, page, pdf):
g.close()

if pass_info:
return result, info
else:
return result
return (result, info) if pass_info else result


def render(
Expand Down
12 changes: 4 additions & 8 deletions src/pypdfium2/_helpers/pageobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def get_pos(self):
if self.page is None:
raise RuntimeError("Must not call get_pos() on a loose pageobject.")

left, bottom, right, top = c_float(), c_float(), c_float(), c_float()
success = pdfium_c.FPDFPageObj_GetBounds(self, left, bottom, right, top)
l, b, r, t = c_float(), c_float(), c_float(), c_float()
success = pdfium_c.FPDFPageObj_GetBounds(self, l, b, r, t)
if not success:
raise PdfiumError("Failed to locate pageobject.")

return (left.value, bottom.value, right.value, top.value)
return (l.value, b.value, r.value, t.value)


def get_matrix(self):
Expand Down Expand Up @@ -207,11 +207,7 @@ def load_jpeg(self, source, pages=None, inline=False, autoclose=True):
raise ValueError(f"Cannot load JPEG from {source} - not a file path or byte buffer.")

bufaccess, ld_data = utils.get_bufreader(buffer)

if inline:
loader = pdfium_c.FPDFImageObj_LoadJpegFileInline
else:
loader = pdfium_c.FPDFImageObj_LoadJpegFile
loader = pdfium_c.FPDFImageObj_LoadJpegFileInline if inline else pdfium_c.FPDFImageObj_LoadJpegFile

This comment was marked as outdated.

Copy link
@mara004

mara004 Apr 23, 2023

Author Member

Changing inline(=False) to buffered(=True) and swapping loaders here (i. e. handling the default first) might be slightly nicer. However, we probably shouldn't do this, not even on a major release, on behalf of API stability.

This comment was marked as outdated.

Copy link
@mara004

mara004 Apr 29, 2023

Author Member

Oh, actually this is quite elegant as-is, since it's an opt-in parameter of the CLI: inline=args.inline


c_pages, page_count = utils.pages_c_array(pages)
success = loader(c_pages, page_count, self, bufaccess)
Expand Down
2 changes: 1 addition & 1 deletion src/pypdfium2/_helpers/textpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_text_range(self, index=0, count=-1, errors="ignore"):
if count == -1:
count = self.count_chars() - index

# FIXME does not take possible unicode surrogation into account
# FIXME not sure if we may calculate buffer size like that - what about unicode surrogation, for one thing?
n_bytes = count * 2
buffer = ctypes.create_string_buffer(n_bytes+2)
buffer_ptr = ctypes.cast(buffer, ctypes.POINTER(ctypes.c_ushort))
Expand Down

0 comments on commit e418360

Please sign in to comment.