diff --git a/docs/devel/changelog_staging.md b/docs/devel/changelog_staging.md index ff6960a98..52daaa5a3 100644 --- a/docs/devel/changelog_staging.md +++ b/docs/devel/changelog_staging.md @@ -7,3 +7,4 @@ - Disruption: Two components of `PdfDocument` have been removed to clean up the code (without a major release, due to their insignificance): - Removal of `update_rendering_input()`. Callers are expected to save and re-open the document on their if they wish that changes take effect with the multi-page renderer. - The multipage renderer does not implicitly read byte buffers into memory anymore. Callers are expected to take an explicit decision by providing a different input in the first place. +- Docs: The changelog page now selectively includes an entry for the next release that may be shown on `latest` builds. diff --git a/src/pypdfium2/_helpers/_utils.py b/src/pypdfium2/_helpers/_utils.py deleted file mode 100644 index 6945ff52f..000000000 --- a/src/pypdfium2/_helpers/_utils.py +++ /dev/null @@ -1,27 +0,0 @@ -# SPDX-FileCopyrightText: 2022 geisserml -# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause - -import pypdfium2._pypdfium as pdfium - - -def validate_colours(fill_colour, colour_scheme): - colours = [fill_colour] - if colour_scheme is not None: - colours += list( colour_scheme.colours.values() ) - for col in colours: - if len(col) != 4: - raise ValueError("Colour must consist of exactly 4 values.") - if not all(0 <= c <= 255 for c in col): - raise ValueError("Colour value exceeds boundaries.") - - -def auto_bitmap_format(fill_colour, greyscale, prefer_bgrx): - # no need to take alpha values of colour_scheme into account (drawings are additive) - if (fill_colour[3] < 255): - return pdfium.FPDFBitmap_BGRA - elif greyscale: - return pdfium.FPDFBitmap_Gray - elif prefer_bgrx: - return pdfium.FPDFBitmap_BGRx - else: - return pdfium.FPDFBitmap_BGR diff --git a/src/pypdfium2/_helpers/document.py b/src/pypdfium2/_helpers/document.py index 96422d64f..837b6fd83 100644 --- a/src/pypdfium2/_helpers/document.py +++ b/src/pypdfium2/_helpers/document.py @@ -35,44 +35,6 @@ logger = logging.getLogger(__name__) -def _open_pdf(input_data, password=None): - - if isinstance(password, str): - password = password.encode("utf-8") - - ld_data = None - if isinstance(input_data, str): - pdf = pdfium.FPDF_LoadDocument(input_data.encode("utf-8"), password) - elif isinstance(input_data, bytes): - pdf = pdfium.FPDF_LoadMemDocument64(input_data, len(input_data), password) - ld_data = DataHolder(input_data) - elif is_input_buffer(input_data): - fileaccess, ld_data = get_fileaccess(input_data) - pdf = pdfium.FPDF_LoadCustomDocument(fileaccess, password) - else: - raise TypeError("Invalid input type '%s'" % type(input_data).__name__) - - if pdfium.FPDF_GetPageCount(pdf) < 1: - err_code = pdfium.FPDF_GetLastError() - pdfium_msg = ErrorToStr.get(err_code, "Error code %s" % err_code) - raise PdfiumError("Loading the document failed (PDFium: %s)" % pdfium_msg) - - return pdf, ld_data - - -class _writer_class: - - def __init__(self, buffer): - self.buffer = buffer - if not callable( getattr(self.buffer, "write", None) ): - raise ValueError("Output buffer must implement the write() method.") - - def __call__(self, _, data, size): - block = ctypes.cast(data, ctypes.POINTER(ctypes.c_ubyte * size)) - self.buffer.write(block.contents) - return 1 - - class PdfDocument (BitmapConvAliases): """ Document helper class. @@ -608,6 +570,44 @@ def render_to( assert len(page_indices) == i +def _open_pdf(input_data, password=None): + + if isinstance(password, str): + password = password.encode("utf-8") + + ld_data = None + if isinstance(input_data, str): + pdf = pdfium.FPDF_LoadDocument(input_data.encode("utf-8"), password) + elif isinstance(input_data, bytes): + pdf = pdfium.FPDF_LoadMemDocument64(input_data, len(input_data), password) + ld_data = DataHolder(input_data) + elif is_input_buffer(input_data): + fileaccess, ld_data = get_fileaccess(input_data) + pdf = pdfium.FPDF_LoadCustomDocument(fileaccess, password) + else: + raise TypeError("Invalid input type '%s'" % type(input_data).__name__) + + if pdfium.FPDF_GetPageCount(pdf) < 1: + err_code = pdfium.FPDF_GetLastError() + pdfium_msg = ErrorToStr.get(err_code, "Error code %s" % err_code) + raise PdfiumError("Loading the document failed (PDFium: %s)" % pdfium_msg) + + return pdf, ld_data + + +class _writer_class: + + def __init__(self, buffer): + self.buffer = buffer + if not callable( getattr(self.buffer, "write", None) ): + raise ValueError("Output buffer must implement the write() method.") + + def __call__(self, _, data, size): + block = ctypes.cast(data, ctypes.POINTER(ctypes.c_ubyte * size)) + self.buffer.write(block.contents) + return 1 + + class PdfXObject: """ XObject helper class. diff --git a/src/pypdfium2/_helpers/page.py b/src/pypdfium2/_helpers/page.py index 8c134a2a2..b373fd9f0 100644 --- a/src/pypdfium2/_helpers/page.py +++ b/src/pypdfium2/_helpers/page.py @@ -8,10 +8,6 @@ import logging from ctypes import c_float import pypdfium2._pypdfium as pdfium -from pypdfium2._helpers._utils import ( - validate_colours, - auto_bitmap_format, -) from pypdfium2._helpers.misc import ( OptimiseMode, PdfiumError, @@ -488,10 +484,12 @@ def render_base( Image size is given in pixels as a tuple of width and height. """ - validate_colours(fill_colour, colour_scheme) + _validate_colours(fill_colour) + if colour_scheme is not None: + _validate_colours(*colour_scheme.colours.values()) if force_bitmap_format in (None, pdfium.FPDFBitmap_Unknown): - cl_pdfium = auto_bitmap_format(fill_colour, greyscale, prefer_bgrx) + cl_pdfium = _auto_bitmap_format(fill_colour, greyscale, prefer_bgrx) else: cl_pdfium = force_bitmap_format @@ -589,6 +587,26 @@ def render_base( return buffer, cl_string, (width, height) +def _validate_colours(*colours): + for col in colours: + if len(col) != 4: + raise ValueError("Colour must consist of exactly 4 values.") + if not all(0 <= c <= 255 for c in col): + raise ValueError("Colour value exceeds boundaries.") + + +def _auto_bitmap_format(fill_colour, greyscale, prefer_bgrx): + # no need to take alpha values of colour_scheme into account (drawings are additive) + if (fill_colour[3] < 255): + return pdfium.FPDFBitmap_BGRA + elif greyscale: + return pdfium.FPDFBitmap_Gray + elif prefer_bgrx: + return pdfium.FPDFBitmap_BGRx + else: + return pdfium.FPDFBitmap_BGR + + class ColourScheme: """ Rendering colour scheme.