1
0
Fork 0
mirror of https://git.lynn.is/Gwen/python-layout.git synced 2024-01-13 01:31:55 +01:00
python-layout/pillow_layout/internal/helpers.py
2023-02-07 23:54:13 +01:00

46 lines
1.1 KiB
Python

def get_line_height(font):
# just a bunch of high and low characters, idk how to do this better
_, bb_top, _, bb_bottom = font.getbbox("Ålgjpqvy")
return bb_bottom - bb_top
def min_with_none(*args):
result = None
for arg in args:
if arg is not None:
if result is None:
result = arg
else:
result = min(result, arg)
return result
def max_with_none(*args):
result = None
for arg in args:
if arg is not None:
if result is None:
result = arg
else:
result = max(result, arg)
return result
def line_intersection(line1, line2):
# copied from https://stackoverflow.com/a/20677983
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
print(line1, line2)
raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y