Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rename drawing areas to cells #47

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ As a command line tool
picture layout options:
-l, --layout LAYOUT specify grid layout (columns x rows) of pictures on
page, e.g. 2x3 or 2,3; default is 1x1
-m, --margin MARGIN set width of empty space around drawing areas; default
is 72 (72 points = 1 inch)
-s, --stretch-small scale small pictures up to fit drawing areas
-a, --fill-area fill drawing areas with pictures, ignoring the
pictures' aspect ratio
-m, --margin MARGIN set width of empty space around the cells containing
pictures; default is 72 (72 points = 1 inch)
-s, --stretch-small scale small pictures up to fit cells
-c, --fill-cell fill cells with pictures, ignoring the pictures'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Consider highlighting the change from -a to -c option

This is a breaking change that might affect existing scripts. It might be helpful to add a note about this change in a prominent place in the README, in addition to the changelog entry.

Suggested change
-c, --fill-cell fill cells with pictures, ignoring the pictures'
-c, --fill-cell fill cells with pictures, ignoring the pictures'
aspect ratio (formerly -a option)

aspect ratio


Examples
Expand Down Expand Up @@ -132,7 +132,7 @@ and their default values correspond to the above shown command line options:
layout=(1, 1),
margin=72,
stretch_small=False,
fill_area=False,
fill_cell=False,
)


Expand Down Expand Up @@ -166,7 +166,7 @@ their default values correspond to the above shown command line options:
layout=(1, 1),
margin=72,
stretch_small=False,
fill_area=False,
fill_cell=False,
)


Expand Down
41 changes: 41 additions & 0 deletions changelog.d/20241015_095444_michalportes1_cell_box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
A new scriv changelog fragment.

Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed

- A bullet item for the Removed category.

-->
<!--
### Added

- A bullet item for the Added category.

-->
### Breaking changes

- Library API: `fill_area` keyword parameters renamed to `fill_cell`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (documentation): Typo: 'parameters' should be 'parameter'

- CLI: the `-a`/`--fill-area` option renamed to `-c`/`--fill-cell`

<!--
### Deprecated

- A bullet item for the Deprecated category.

-->
<!--
### Fixed

- A bullet item for the Fixed category.

-->
<!--
### Security

- A bullet item for the Security category.

-->
12 changes: 6 additions & 6 deletions src/pictureshow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ def _setup_parser():
'--margin',
type=float,
default=72,
help='set width of empty space around drawing areas; '
help='set width of empty space around the cells containing pictures; '
'default is %(default)s (72 points = 1 inch)',
)
picture_group.add_argument(
'-s',
'--stretch-small',
action='store_true',
help='scale small pictures up to fit drawing areas',
help='scale small pictures up to fit cells',
)
picture_group.add_argument(
'-a',
'--fill-area',
'-c',
'--fill-cell',
action='store_true',
help="fill drawing areas with pictures, ignoring the pictures' aspect ratio",
help="fill cells with pictures, ignoring the pictures' aspect ratio",
)

return parser
Expand Down Expand Up @@ -193,7 +193,7 @@ def main(argv=None):
layout=args.layout,
margin=args.margin,
stretch_small=args.stretch_small,
fill_area=args.fill_area,
fill_cell=args.fill_cell,
):
print('.' if ok_flag else '!', end='', flush=True)
print()
Expand Down
68 changes: 34 additions & 34 deletions src/pictureshow/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

DELIMITER = re.compile('[x,]')

_Area = namedtuple('_Area', 'x y width height')
_Box = namedtuple('_Box', 'x y width height')

_Result = namedtuple('_Result', 'num_ok errors num_pages')

Expand All @@ -39,7 +39,7 @@ def save_pdf(
layout=(1, 1),
margin=72,
stretch_small=False,
fill_area=False,
fill_cell=False,
):
"""Save pictures stored in `self._pic_files` to a PDF document.

Expand All @@ -57,7 +57,7 @@ def save_pdf(
layout=layout,
margin=margin,
stretch_small=stretch_small,
fill_area=fill_area,
fill_cell=fill_cell,
):
pass
return self.result
Expand All @@ -73,7 +73,7 @@ def _save_pdf(
layout,
margin,
stretch_small,
fill_area,
fill_cell,
):
output_file = self._validate_target_path(output_file, force_overwrite)
page_size = self._validate_page_size(page_size, landscape)
Expand All @@ -83,9 +83,9 @@ def _save_pdf(
self._backend.init(output_file, page_size, bg_color)
valid_pics = self._valid_pictures()
self.num_ok = 0
areas = tuple(self._areas(layout, page_size, margin))
cells = tuple(self._cells(layout, page_size, margin))
while True:
for area in areas:
for cell in cells:
try:
while True:
picture = next(valid_pics)
Expand All @@ -99,12 +99,12 @@ def _save_pdf(
return
x, y, *pic_size = self._position_and_size(
self._backend.get_picture_size(picture),
area[2:], # short for (area.width, area.height)
(cell.width, cell.height),
stretch_small,
fill_area,
fill_cell,
)
self._backend.add_picture(
picture, (area.x + x, area.y + y), pic_size
picture, (cell.x + x, cell.y + y), pic_size
)
self.num_ok += 1
yield True
Expand Down Expand Up @@ -184,49 +184,49 @@ def _valid_pictures(self):
yield picture

@staticmethod
def _position_and_size(pic_size, area_size, stretch_small, fill_area):
"""Calculate position and size of the picture in the area."""
area_width, area_height = area_size
if fill_area:
return 0, 0, area_width, area_height
def _position_and_size(pic_size, cell_size, stretch_small, fill_cell):
"""Calculate position and size of the picture within the cell."""
cell_width, cell_height = cell_size
if fill_cell:
return 0, 0, cell_width, cell_height

pic_width, pic_height = pic_size
pic_is_big = pic_width > area_width or pic_height > area_height
pic_is_big = pic_width > cell_width or pic_height > cell_height

# calculate scale factor to fit picture to area
# calculate scale factor to fit picture to cell
if pic_is_big or stretch_small:
pic_is_wide = pic_width / pic_height > area_width / area_height
scale = area_width / pic_width if pic_is_wide else area_height / pic_height
pic_is_wide = pic_width / pic_height > cell_width / cell_height
scale = cell_width / pic_width if pic_is_wide else cell_height / pic_height
pic_width *= scale
pic_height *= scale

# center picture to area
x = (area_width - pic_width) / 2
y = (area_height - pic_height) / 2
# center picture in cell
x = (cell_width - pic_width) / 2
y = (cell_height - pic_height) / 2

return x, y, pic_width, pic_height

@staticmethod
def _areas(layout, page_size, margin):
def _cells(layout, page_size, margin):
num_columns, num_rows = layout
page_width, page_height = page_size

area_width = (page_width - (num_columns + 1) * margin) / num_columns
area_height = (page_height - (num_rows + 1) * margin) / num_rows
if area_width < 1 or area_height < 1:
cell_width = (page_width - (num_columns + 1) * margin) / num_columns
cell_height = (page_height - (num_rows + 1) * margin) / num_rows
Comment on lines +214 to +215
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Use math.floor() to ensure integer values for cell dimensions

The calculation of cell_width and cell_height could result in floating-point values. Consider using math.floor() to ensure integer values, which could prevent potential issues with pixel-perfect layouts.

import math

cell_width = math.floor((page_width - (num_columns + 1) * margin) / num_columns)
cell_height = math.floor((page_height - (num_rows + 1) * margin) / num_rows)

if cell_width < 1 or cell_height < 1:
raise MarginError(f'margin value too high: {margin}')

areas_y_coords = (
page_height - row * (area_height + margin)
cells_y_coords = (
page_height - row * (cell_height + margin)
for row in range(1, num_rows + 1)
)
areas_x_coords = (
margin + col * (area_width + margin)
cells_x_coords = (
margin + col * (cell_width + margin)
for col in range(num_columns)
)
# yield areas row-wise
for y, x in itertools.product(areas_y_coords, areas_x_coords):
yield _Area(x, y, area_width, area_height)
# yield cells row-wise
for y, x in itertools.product(cells_y_coords, cells_x_coords):
yield _Box(x, y, cell_width, cell_height)


def pictures_to_pdf(
Expand All @@ -239,7 +239,7 @@ def pictures_to_pdf(
layout=(1, 1),
margin=72,
stretch_small=False,
fill_area=False,
fill_cell=False,
):
"""Save one or more pictures to a PDF document.

Expand All @@ -259,5 +259,5 @@ def pictures_to_pdf(
layout=layout,
margin=margin,
stretch_small=stretch_small,
fill_area=fill_area,
fill_cell=fill_cell,
)
4 changes: 2 additions & 2 deletions tests/test_pictures_to_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def test_stretch_small(new_pdf):
assert_pdf(new_pdf, 1)


def test_fill_area(new_pdf):
result = pictures_to_pdf(PIC_FILE, output_file=new_pdf, fill_area=True)
def test_fill_cell(new_pdf):
result = pictures_to_pdf(PIC_FILE, output_file=new_pdf, fill_cell=True)

assert result == (1, [], 1)
assert_pdf(new_pdf, 1)
Loading