diff --git a/README.md b/README.md index 140311566..3bbbab73f 100644 --- a/README.md +++ b/README.md @@ -242,8 +242,8 @@ Nonetheless, the following guide may be helpful to get started with the raw API, # Initialise an integer object (defaults to 0) c_version = ctypes.c_int() # Let the function assign a value to the c_int object, and capture its return code (True for success, False for failure) - success = pdfium.FPDF_GetFileVersion(pdf, c_version) - if success: + ok = pdfium.FPDF_GetFileVersion(pdf, c_version) + if ok: # If successful, get the Python int by accessing the `value` attribute of the c_int object version = c_version.value else: diff --git a/src/pypdfium2/_helpers/attachment.py b/src/pypdfium2/_helpers/attachment.py index 64091e5ad..886fd6487 100644 --- a/src/pypdfium2/_helpers/attachment.py +++ b/src/pypdfium2/_helpers/attachment.py @@ -66,9 +66,9 @@ def get_data(self): buffer = ctypes.create_string_buffer(n_bytes) out_buflen = ctypes.c_ulong() - success = pdfium_c.FPDFAttachment_GetFile(self, buffer, n_bytes, out_buflen) + ok = pdfium_c.FPDFAttachment_GetFile(self, buffer, n_bytes, out_buflen) out_buflen = out_buflen.value - if not success: + if not ok: raise PdfiumError("Failed to extract attachment (error status).") if n_bytes < out_buflen: raise PdfiumError(f"Failed to extract attachment (expected {n_bytes} bytes, but got {out_buflen}).") @@ -86,8 +86,8 @@ def set_data(self, data): data (bytes | ctypes.Array): New file data for the attachment. May be any data type that can be implicitly converted to :class:`~ctypes.c_void_p`. """ - success = pdfium_c.FPDFAttachment_SetFile(self, self.pdf, data, len(data)) - if not success: + ok = pdfium_c.FPDFAttachment_SetFile(self, self.pdf, data, len(data)) + if not ok: raise PdfiumError("Failed to set attachment data.") @@ -138,6 +138,6 @@ def set_str_value(self, key, value): """ enc_value = (value + "\x00").encode("utf-16-le") enc_value_ptr = ctypes.cast(enc_value, pdfium_c.FPDF_WIDESTRING) - success = pdfium_c.FPDFAttachment_SetStringValue(self, _encode_key(key), enc_value_ptr) - if not success: + ok = pdfium_c.FPDFAttachment_SetStringValue(self, _encode_key(key), enc_value_ptr) + if not ok: raise PdfiumError(f"Failed to set attachment param '{key}' to '{value}'.") diff --git a/src/pypdfium2/_helpers/document.py b/src/pypdfium2/_helpers/document.py index 2e434828d..de0b46f72 100644 --- a/src/pypdfium2/_helpers/document.py +++ b/src/pypdfium2/_helpers/document.py @@ -197,11 +197,11 @@ def _save_to(self, buffer, version=None, flags=pdfium_c.FPDF_NO_INCREMENTAL): saveargs = (self, c_writer, flags) if version is None: - success = pdfium_c.FPDF_SaveAsCopy(*saveargs) + ok = pdfium_c.FPDF_SaveAsCopy(*saveargs) else: - success = pdfium_c.FPDF_SaveWithVersion(*saveargs, version) + ok = pdfium_c.FPDF_SaveWithVersion(*saveargs, version) - if not success: + if not ok: raise PdfiumError("Failed to save document.") @@ -251,8 +251,8 @@ def get_version(self): or None if the document is new or its version could not be determined. """ version = ctypes.c_int() - success = pdfium_c.FPDF_GetFileVersion(self, version) - if not success: + ok = pdfium_c.FPDF_GetFileVersion(self, version) + if not ok: return None return version.value @@ -335,8 +335,8 @@ def del_attachment(self, index): Handles to the attachment in question received from :meth:`.get_attachment` must not be accessed anymore after this method has been called. """ - success = pdfium_c.FPDFDoc_DeleteAttachment(self, index) - if not success: + ok = pdfium_c.FPDFDoc_DeleteAttachment(self, index) + if not ok: raise PdfiumError(f"Failed to delete attachment at index {index}.") @@ -409,16 +409,16 @@ def import_pages(self, pdf, pages=None, index=None): index = len(self) if isinstance(pages, str): - success = pdfium_c.FPDF_ImportPages(self, pdf, pages.encode("ascii"), index) + ok = pdfium_c.FPDF_ImportPages(self, pdf, pages.encode("ascii"), index) else: page_count = 0 c_pages = None if pages: page_count = len(pages) c_pages = (ctypes.c_int * page_count)(*pages) - success = pdfium_c.FPDF_ImportPagesByIndex(self, pdf, c_pages, page_count, index) + ok = pdfium_c.FPDF_ImportPagesByIndex(self, pdf, c_pages, page_count, index) - if not success: + if not ok: raise PdfiumError("Failed to import pages.") @@ -428,8 +428,8 @@ def get_page_size(self, index): (float, float): Width and height in PDF canvas units of the page at *index* (zero-based). """ size = pdfium_c.FS_SIZEF() - success = pdfium_c.FPDF_GetPageSizeByIndexF(self, index, size) - if not success: + ok = pdfium_c.FPDF_GetPageSizeByIndexF(self, index, size) + if not ok: raise PdfiumError("Failed to get page size by index.") return (size.width, size.height) diff --git a/src/pypdfium2/_helpers/page.py b/src/pypdfium2/_helpers/page.py index 45746975e..3406b98c8 100644 --- a/src/pypdfium2/_helpers/page.py +++ b/src/pypdfium2/_helpers/page.py @@ -85,8 +85,8 @@ def set_rotation(self, rotation): def _get_box(self, box_func, fallback_func, fallback_ok): left, bottom, right, top = c_float(), c_float(), c_float(), c_float() - success = box_func(self, left, bottom, right, top) - if not success: + ok = box_func(self, left, bottom, right, top) + if not ok: return (fallback_func() if fallback_ok else None) return (left.value, bottom.value, right.value, top.value) @@ -170,8 +170,8 @@ def get_bbox(self): The bounding box of the page (the intersection between its media box and crop box). """ rect = pdfium_c.FS_RECTF() - success = pdfium_c.FPDF_GetPageBoundingBox(self, rect) - if not success: + ok = pdfium_c.FPDF_GetPageBoundingBox(self, rect) + if not ok: raise PdfiumError("Failed to get page bounding box.") return (rect.left, rect.bottom, rect.right, rect.top) @@ -238,8 +238,8 @@ def gen_content(self): If page content was changed, this function should be called once before saving the document or re-loading the page. """ - success = pdfium_c.FPDFPage_GenerateContent(self) - if not success: + ok = pdfium_c.FPDFPage_GenerateContent(self) + if not ok: raise PdfiumError("Failed to generate page content.") diff --git a/src/pypdfium2/_helpers/pageobjects.py b/src/pypdfium2/_helpers/pageobjects.py index 64a95d76a..0cb7de17f 100644 --- a/src/pypdfium2/_helpers/pageobjects.py +++ b/src/pypdfium2/_helpers/pageobjects.py @@ -90,8 +90,8 @@ def get_pos(self): raise RuntimeError("Must not call get_pos() on a loose pageobject.") 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: + ok = pdfium_c.FPDFPageObj_GetBounds(self, l, b, r, t) + if not ok: raise PdfiumError("Failed to locate pageobject.") return (l.value, b.value, r.value, t.value) @@ -103,8 +103,8 @@ def get_matrix(self): PdfMatrix: The pageobject's current transform matrix. """ fs_matrix = pdfium_c.FS_MATRIX() - success = pdfium_c.FPDFPageObj_GetMatrix(self, fs_matrix) - if not success: + ok = pdfium_c.FPDFPageObj_GetMatrix(self, fs_matrix) + if not ok: raise PdfiumError("Failed to get matrix of pageobject.") return PdfMatrix.from_raw(fs_matrix) @@ -114,8 +114,8 @@ def set_matrix(self, matrix): Parameters: matrix (PdfMatrix): Set this matrix as the pageobject's transform matrix. """ - success = pdfium_c.FPDFPageObj_SetMatrix(self, matrix) - if not success: + ok = pdfium_c.FPDFPageObj_SetMatrix(self, matrix) + if not ok: raise PdfiumError("Failed to set matrix of pageobject.") @@ -170,8 +170,8 @@ def get_metadata(self): """ # https://crbug.com/pdfium/1928 metadata = pdfium_c.FPDF_IMAGEOBJ_METADATA() - success = pdfium_c.FPDFImageObj_GetImageMetadata(self, self.page, metadata) - if not success: + ok = pdfium_c.FPDFImageObj_GetImageMetadata(self, self.page, metadata) + if not ok: raise PdfiumError("Failed to get image metadata.") return metadata @@ -185,8 +185,8 @@ def get_size(self): """ # https://pdfium-review.googlesource.com/c/pdfium/+/106290 w, h = c_uint(), c_uint() - success = pdfium_c.FPDFImageObj_GetImagePixelSize(self, w, h) - if not success: + ok = pdfium_c.FPDFImageObj_GetImagePixelSize(self, w, h) + if not ok: raise PdfiumError("Failed to get image size.") return w.value, h.value @@ -221,8 +221,8 @@ def load_jpeg(self, source, pages=None, inline=False, autoclose=True): loader = pdfium_c.FPDFImageObj_LoadJpegFileInline if inline else pdfium_c.FPDFImageObj_LoadJpegFile c_pages, page_count = utils.pages_c_array(pages) - success = loader(c_pages, page_count, self, bufaccess) - if not success: + ok = loader(c_pages, page_count, self, bufaccess) + if not ok: raise PdfiumError("Failed to load JPEG into image object.") if inline: @@ -248,8 +248,8 @@ def set_bitmap(self, bitmap, pages=None): A list of loaded pages that might contain the image object. See :meth:`.load_jpeg`. """ c_pages, page_count = utils.pages_c_array(pages) - success = pdfium_c.FPDFImageObj_SetBitmap(c_pages, page_count, self, bitmap) - if not success: + ok = pdfium_c.FPDFImageObj_SetBitmap(c_pages, page_count, self, bitmap) + if not ok: raise PdfiumError("Failed to set image to bitmap.") diff --git a/src/pypdfium2/_helpers/textpage.py b/src/pypdfium2/_helpers/textpage.py index 8c46b2609..cafbe7444 100644 --- a/src/pypdfium2/_helpers/textpage.py +++ b/src/pypdfium2/_helpers/textpage.py @@ -151,14 +151,14 @@ def get_charbox(self, index, loose=False): if loose: rect = pdfium_c.FS_RECTF() - success = pdfium_c.FPDFText_GetLooseCharBox(self, index, rect) + ok = pdfium_c.FPDFText_GetLooseCharBox(self, index, rect) l, b, r, t = rect.left, rect.bottom, rect.right, rect.top else: l, b, r, t = c_double(), c_double(), c_double(), c_double() - success = pdfium_c.FPDFText_GetCharBox(self, index, l, r, b, t) # yes, lrbt! + ok = pdfium_c.FPDFText_GetCharBox(self, index, l, r, b, t) # yes, lrbt! l, b, r, t = l.value, b.value, r.value, t.value - if not success: + if not ok: raise PdfiumError("Failed to get charbox.") return l, b, r, t @@ -174,8 +174,8 @@ def get_rect(self, index): Float values for left, bottom, right and top in PDF canvas units. """ l, b, r, t = c_double(), c_double(), c_double(), c_double() - success = pdfium_c.FPDFText_GetRect(self, index, l, t, r, b) # yes, ltrb! - if not success: + ok = pdfium_c.FPDFText_GetRect(self, index, l, t, r, b) # yes, ltrb! + if not ok: raise PdfiumError("Failed to get rectangle. (Make sure count_rects() was called with default params once before subsequent get_rect() calls.)") return (l.value, b.value, r.value, t.value) @@ -236,8 +236,8 @@ def parent(self): # AutoCloseable hook def _get_occurrence(self, find_func): - success = find_func(self) - if not success: + ok = find_func(self) + if not ok: return None index = pdfium_c.FPDFText_GetSchResultIndex(self) count = pdfium_c.FPDFText_GetSchCount(self)