Skip to content

Commit

Permalink
Implement no_antialias rendering param differently
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Aug 27, 2022
1 parent 2583618 commit 3de1e8e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
2 changes: 0 additions & 2 deletions docs/devel/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ These are various tasks for the maintainer to keep in mind, in no specific order
### Main Code
* Make the bindings file `_pypdfium.py` public
* Make members of `_utils.py` public (move into `misc.py`)
* Address points listed in [`planned_changes.md`](../source/planned_changes.md)
* Investigate other PDFium rendering functions. Ideally, we would want to add margins, flip and colour schemes. Also consider making form rendering optional.
* Implement `no_antialias` differently (probably it'd be best to provide separate options for each)
* Read up what `FPDFPage_Flatten()` does and if we could use it somehow
* Consolidate and extend the support model

Expand Down
4 changes: 2 additions & 2 deletions docs/source/planned_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

# Planned Changes

The following API breaking changes are being considered for the next major release:
* Rendering parameters might change, especially the anti-aliasing options.
<!-- The following API breaking changes are being considered for the next major release: -->
Currently, no API breaking changes are planned.
10 changes: 6 additions & 4 deletions src/pypdfium2/_cli/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def main(args):
else:
page_indices = [i for i in range(len(pdf))]

n_digits = len(str( max(page_indices)+1 ))

renderer = pdf.render_topil(
kwargs = dict(
page_indices = page_indices,
scale = args.scale,
rotation = args.rotation,
Expand All @@ -152,11 +150,15 @@ def main(args):
annotations = not args.no_annotations,
greyscale = args.greyscale,
optimise_mode = args.optimise_mode,
no_antialias = args.no_antialias,
n_processes = args.processes,
)
for type in args.no_antialias:
kwargs["no_smooth%s" % type] = True

renderer = pdf.render_topil(**kwargs)
prefix = splitext(basename(input_path))[0] + "_"
n_digits = len(str( max(page_indices)+1 ))

for image, index in zip(renderer, page_indices):
suffix = str(index+1).zfill(n_digits)
output_path = "%s.%s" % (join(args.output, prefix+suffix), args.format)
Expand Down
21 changes: 14 additions & 7 deletions src/pypdfium2/_helpers/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ def render_base(
annotations = True,
greyscale = False,
optimise_mode = OptimiseMode.NONE,
no_antialias = (),
no_smoothtext = False,
no_smoothimage = False,
no_smoothpath = False,
):
"""
Rasterise the page to a ctypes ubyte array.
Expand Down Expand Up @@ -293,8 +295,14 @@ def render_base(
optimise_mode (OptimiseMode):
How to optimise page rendering.
no_antialias (typing.Sequence[str]):
A list of item types that shall not be smoothed (text, image, path).
no_smoothtext (bool):
Disable anti-aliasing of text. Implicitly wipes out :attr:`.OptimiseMode.LCD_DISPLAY`.
no_smoothimage (bool):
Disable anti-aliasing of images.
no_smoothpath (bool):
Disable anti-aliasing of paths.
Returns:
(``ctypes.c_ubyte_Array_%d``, str, (int, int)):
Expand Down Expand Up @@ -336,12 +344,11 @@ def render_base(
render_flags |= pdfium.FPDF_ANNOT
if greyscale:
render_flags |= pdfium.FPDF_GRAYSCALE

if "text" in no_antialias:
if no_smoothtext:
render_flags |= pdfium.FPDF_RENDER_NO_SMOOTHTEXT
if "image" in no_antialias:
if no_smoothimage:
render_flags |= pdfium.FPDF_RENDER_NO_SMOOTHIMAGE
if "path" in no_antialias:
if no_smoothpath:
render_flags |= pdfium.FPDF_RENDER_NO_SMOOTHPATH

if optimise_mode is OptimiseMode.NONE:
Expand Down
4 changes: 3 additions & 1 deletion tests/helpers/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ def test_render_page_optimisation(sample_page):

def test_render_page_noantialias(sample_page):
pil_image = sample_page.render_topil(
no_antialias = ("text", "image", "path"),
no_smoothtext = True,
no_smoothimage = True,
no_smoothpath = True,
scale = 0.5,
)
assert isinstance(pil_image, PIL.Image.Image)
Expand Down

0 comments on commit 3de1e8e

Please sign in to comment.