Skip to content

Commit 141779e

Browse files
committed
Removed archaic units from pagesizes.
1 parent 91c3094 commit 141779e

File tree

3 files changed

+55
-192
lines changed

3 files changed

+55
-192
lines changed

domdf_python_tools/pagesizes/classes.py

Lines changed: 11 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# this package
3838
from ._types import AnyNumber
39-
from .units import Unit, _rounders, cicero, cm, didot, inch, mm, new_cicero, new_didot, pica, pt, scaled_point, um
39+
from .units import Unit, _rounders, cm, inch, mm, pica, pt, um
4040
from .utils import convert_from
4141

4242
__all__ = [
@@ -46,11 +46,6 @@
4646
"Size_cm",
4747
"Size_um",
4848
"Size_pica",
49-
"Size_didot",
50-
"Size_cicero",
51-
"Size_new_didot",
52-
"Size_new_cicero",
53-
"Size_scaled_point",
5449
"PageSize",
5550
]
5651

@@ -225,76 +220,6 @@ class Size_pica(BaseSize):
225220
_unit = pica
226221

227222

228-
class Size_didot(BaseSize):
229-
"""
230-
Subclass of :class:`~domdf_python_tools.pagesizes.classes.BaseSize`
231-
representing a pagesize in didots / French Points.
232-
233-
:param width: The page width
234-
:type width: int, float, Decimal or Unit
235-
:param height: The page height
236-
:type height: int, float, Decimal or Unit
237-
"""
238-
239-
_unit = didot
240-
241-
242-
class Size_cicero(BaseSize):
243-
"""
244-
Subclass of :class:`~domdf_python_tools.pagesizes.classes.BaseSize`
245-
representing a pagesize in ciceros.
246-
247-
:param width: The page width
248-
:type width: int, float, Decimal or Unit
249-
:param height: The page height
250-
:type height: int, float, Decimal or Unit
251-
"""
252-
253-
_unit = cicero
254-
255-
256-
class Size_new_didot(BaseSize):
257-
"""
258-
Subclass of :class:`~domdf_python_tools.pagesizes.classes.BaseSize`
259-
representing a pagesize in new didots.
260-
261-
:param width: The page width
262-
:type width: int, float, Decimal or Unit
263-
:param height: The page height
264-
:type height: int, float, Decimal or Unit
265-
"""
266-
267-
_unit = new_didot
268-
269-
270-
class Size_new_cicero(BaseSize):
271-
"""
272-
Subclass of :class:`~domdf_python_tools.pagesizes.classes.BaseSize`
273-
representing a pagesize in ciceros.
274-
275-
:param width: The page width
276-
:type width: int, float, Decimal or Unit
277-
:param height: The page height
278-
:type height: int, float, Decimal or Unit
279-
"""
280-
281-
_unit = new_cicero
282-
283-
284-
class Size_scaled_point(BaseSize):
285-
"""
286-
Subclass of :class:`~domdf_python_tools.pagesizes.classes.BaseSize`
287-
representing a pagesize in scaled points.
288-
289-
:param width: The page width
290-
:type width: int, float, Decimal or Unit
291-
:param height: The page height
292-
:type height: int, float, Decimal or Unit
293-
"""
294-
295-
_unit = scaled_point
296-
297-
298223
class PageSize(BaseSize):
299224
"""
300225
Subclass of :class:`~domdf_python_tools.pagesizes.classes.BaseSize`
@@ -322,6 +247,14 @@ def __new__(cls, width: AnyNumber, height: AnyNumber, unit: AnyNumber = pt):
322247
width, height = convert_from((width, height), unit) # type: ignore
323248
return super().__new__(cls, width, height)
324249

250+
@property
251+
def pt(self) -> "PageSize":
252+
"""
253+
Returns the pagesize in pt.
254+
"""
255+
256+
return self
257+
325258
@property
326259
def inch(self) -> Size_inch:
327260
"""
@@ -354,6 +287,8 @@ def um(self) -> Size_um:
354287

355288
return Size_um.from_pt(self)
356289

290+
µm = um
291+
357292
@property
358293
def pc(self) -> Size_pica:
359294
"""
@@ -363,53 +298,3 @@ def pc(self) -> Size_pica:
363298
return Size_pica.from_pt(self)
364299

365300
pica = pc
366-
367-
@property
368-
def dd(self) -> Size_didot:
369-
"""
370-
Returns the pagesize in didots.
371-
"""
372-
373-
return Size_didot.from_pt(self)
374-
375-
didot = dd
376-
377-
@property
378-
def cc(self) -> Size_cicero:
379-
"""
380-
Returns the pagesize in ciceros.
381-
"""
382-
383-
return Size_cicero.from_pt(self)
384-
385-
cicero = cc
386-
387-
@property
388-
def nd(self) -> Size_new_didot:
389-
"""
390-
Returns the pagesize in new didots.
391-
"""
392-
393-
return Size_new_didot.from_pt(self)
394-
395-
new_didot = nd
396-
397-
@property
398-
def nc(self) -> Size_new_cicero:
399-
"""
400-
Returns the pagesize in new ciceros.
401-
"""
402-
403-
return Size_new_cicero.from_pt(self)
404-
405-
new_cicero = nc
406-
407-
@property
408-
def sp(self) -> Size_scaled_point:
409-
"""
410-
Returns the pagesize in scaled point.
411-
"""
412-
413-
return Size_scaled_point.from_pt(self)
414-
415-
scaled_point = sp

domdf_python_tools/pagesizes/units.py

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#
3232

3333
# stdlib
34+
import math
3435
from decimal import ROUND_HALF_UP, Decimal
3536
from typing import Union
3637

@@ -42,16 +43,6 @@
4243
"um",
4344
"pc",
4445
"pica",
45-
"dd",
46-
"didot",
47-
"cc",
48-
"cicero",
49-
"nd",
50-
"new_didot",
51-
"nc",
52-
"new_cicero",
53-
"sp",
54-
"scaled_point",
5546
"Unit",
5647
]
5748

@@ -157,50 +148,74 @@ class Unit(float):
157148
>>> (3*mm) % 2.5
158149
<Unit '0.500 mm': 1.417pt>
159150
160-
Diving by a unit, or modulo division of two units, is not officially supported.
151+
Dividing by a unit, or modulo division of two units, is not officially supported.
161152
"""
162153

163154
name: str = "pt"
164155
_in_pt: float = 1
165156

166157
def __repr__(self):
158+
value = _rounders(float(self), "0.000")
159+
as_pt = _rounders(self.as_pt(), "0.000")
160+
return f"<Unit '{value} {self.name}': {as_pt}pt>"
161+
162+
def __str__(self):
167163
value = _rounders(float(self), "0.000")
168164
as_pt = _rounders(self.as_pt(), "0.000")
169165
return f"<Unit '{value}\u205F{self.name}': {as_pt}pt>"
170166

171167
def __mul__(self, other: Union[float, "Unit"]) -> "Unit":
172168
if isinstance(other, Unit):
173-
return NotImplemented
169+
raise NotImplementedError("Multiplying a unit by another unit is not allowed.")
174170

175171
return self.__class__(super().__mul__(other))
176172

177173
__rmul__ = __mul__
178174

179175
def __truediv__(self, other: Union[float, "Unit"]) -> "Unit":
180176
if isinstance(other, Unit):
181-
return NotImplemented
177+
raise NotImplementedError("Dividing a unit by another unit is not allowed.")
182178

183179
return self.__class__(super().__truediv__(other))
184180

185-
__div__ = __truediv__
181+
def __floordiv__(self, other: Union[float, "Unit"]) -> "Unit":
182+
if isinstance(other, Unit):
183+
raise NotImplementedError("Dividing a unit by another unit is not allowed.")
184+
185+
return self.__class__(super().__floordiv__(other))
186+
187+
def __eq__(self, other: Union[float, "Unit"]) -> bool:
188+
if isinstance(other, Unit):
189+
if self._in_pt != 1:
190+
self_value = self.as_pt()
191+
else:
192+
self_value = self
193+
194+
if other._in_pt != 1:
195+
other_value = other.as_pt()
196+
else:
197+
other_value = other
198+
199+
return math.isclose(float(self_value), float(other_value), abs_tol=1e-8)
200+
else:
201+
return super().__eq__(other)
186202

187203
def __mod__(self, other: Union[float, "Unit"]) -> "Unit":
188204
if isinstance(other, Unit):
189-
return NotImplemented
205+
raise NotImplementedError("Modulo division of a unit by another unit is not allowed.")
190206

191207
return self.__class__(super().__mod__(other))
192208

193209
def __pow__(self, power, modulo=None):
194-
return NotImplemented
210+
raise NotImplementedError("Powers are not supported for units.")
195211

196212
def __rtruediv__(self, other):
197-
return NotImplemented
213+
raise NotImplementedError("Dividing by a unit is not allowed.")
198214

199215
__rdiv__ = __rtruediv__
200216

201217
def __add__(self, other: Union[float, "Unit"]) -> "Unit":
202218
if isinstance(other, Unit):
203-
print(float(self.as_pt()) + float(other.as_pt()))
204219
return self.__class__.from_pt(float(self.as_pt()) + float(other.as_pt()))
205220
else:
206221
return self.__class__(super().__add__(other))
@@ -214,13 +229,13 @@ def __sub__(self, other: Union[float, "Unit"]) -> "Unit":
214229
return self.__class__(super().__sub__(other))
215230

216231
def __rsub__(self, other: Union[float, "Unit"]) -> "Unit":
217-
if isinstance(other, Unit):
232+
if isinstance(other, Unit): # pragma: no cover (sub should be called instead)
218233
return self.__class__.from_pt(float(other.as_pt()) - float(self.as_pt()))
219234
else:
220235
return self.__class__(super().__rsub__(other))
221236

222237
def as_pt(self) -> "Unit":
223-
return Unit(float(self) * self._in_pt)
238+
return Unit(float(_rounders(float(self) * self._in_pt, "0.000000")))
224239

225240
@classmethod
226241
def from_pt(cls, value: float) -> "Unit":
@@ -236,7 +251,7 @@ class Unitpt(Unit):
236251

237252

238253
class UnitInch(Unit):
239-
name = "in"
254+
name = "inch"
240255
_in_pt = 72.0
241256

242257

@@ -252,48 +267,18 @@ class Unitmm(Unit):
252267

253268
class Unitum(Unit):
254269
name = "µm"
255-
_in_pt = 0.283464566929
270+
_in_pt = 0.00283464566929
256271

257272

258273
class Unitpc(Unit):
259274
name = "pc"
260275
_in_pt = 12.0
261276

262277

263-
class Unitdd(Unit):
264-
name = "dd"
265-
_in_pt = 1.07
266-
267-
268-
class Unitcc(Unit):
269-
name = "cc"
270-
_in_pt = 12.84
271-
272-
273-
class Unitnd(Unit):
274-
name = "nd"
275-
_in_pt = 1.067
276-
277-
278-
class Unitnc(Unit):
279-
name = "nc"
280-
_in_pt = 12.804
281-
282-
283-
class Unitsp(Unit):
284-
name = "sp"
285-
_in_pt = 1 / 65536
286-
287-
288278
# Units
289279
pt = Unitpt(1)
290280
inch = UnitInch(1)
291281
cm = Unitcm(1)
292282
mm = Unitmm(1)
293283
um = Unitum(1)
294284
pc = pica = Unitpc(1)
295-
dd = didot = Unitdd(1)
296-
cc = cicero = Unitcc(1)
297-
nd = new_didot = Unitnd(1)
298-
nc = new_cicero = Unitnc(1)
299-
sp = scaled_point = Unitsp(1)

0 commit comments

Comments
 (0)