Skip to content

Commit da83e15

Browse files
author
Steve Canny
committed
img: add _App0Marker.horz_dpi and .vert_dpi
1 parent eb125c8 commit da83e15

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

docx/image/jpeg.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,34 @@ def __init__(
280280
self._x_density = x_density
281281
self._y_density = y_density
282282

283+
@property
284+
def horz_dpi(self):
285+
"""
286+
Horizontal dots per inch specified in this marker, defaults to 72 if
287+
not specified.
288+
"""
289+
return self._dpi(self._x_density)
290+
291+
@property
292+
def vert_dpi(self):
293+
"""
294+
Vertical dots per inch specified in this marker, defaults to 72 if
295+
not specified.
296+
"""
297+
return self._dpi(self._y_density)
298+
299+
def _dpi(self, density):
300+
"""
301+
Return dots per inch corresponding to *density* value.
302+
"""
303+
if self._density_units == 1:
304+
dpi = density
305+
elif self._density_units == 2:
306+
dpi = int(round(density * 2.54))
307+
else:
308+
dpi = 72
309+
return dpi
310+
283311
@classmethod
284312
def from_stream(cls, stream, marker_code, offset):
285313
"""

tests/image/test_jpeg.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,31 @@ def it_can_construct_from_a_stream_and_offset(self, from_stream_fixture):
231231
)
232232
assert isinstance(app0_marker, _App0Marker)
233233

234+
def it_knows_the_image_dpi(self, dpi_fixture):
235+
density_units, x_density, y_density, horz_dpi, vert_dpi = dpi_fixture
236+
app0 = _App0Marker(
237+
None, None, None, density_units, x_density, y_density
238+
)
239+
assert app0.horz_dpi == horz_dpi
240+
assert app0.vert_dpi == vert_dpi
241+
234242
# fixtures -------------------------------------------------------
235243

244+
@pytest.fixture
245+
def _App0Marker__init_(self, request):
246+
return initializer_mock(request, _App0Marker)
247+
248+
@pytest.fixture(params=[
249+
(0, 100, 200, 72, 72),
250+
(1, 100, 200, 100, 200),
251+
(2, 100, 200, 254, 508),
252+
])
253+
def dpi_fixture(self, request):
254+
density_units, x_density, y_density, horz_dpi, vert_dpi = (
255+
request.param
256+
)
257+
return density_units, x_density, y_density, horz_dpi, vert_dpi
258+
236259
@pytest.fixture
237260
def from_stream_fixture(self, request, _App0Marker__init_):
238261
bytes_ = b'\x00\x10JFIF\x00\x01\x01\x01\x00\x2A\x00\x18'
@@ -244,10 +267,6 @@ def from_stream_fixture(self, request, _App0Marker__init_):
244267
density_units, x_density, y_density
245268
)
246269

247-
@pytest.fixture
248-
def _App0Marker__init_(self, request):
249-
return initializer_mock(request, _App0Marker)
250-
251270

252271
class Describe_SofMarker(object):
253272

0 commit comments

Comments
 (0)