-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
92 lines (81 loc) · 2.87 KB
/
utils.py
File metadata and controls
92 lines (81 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from bs4 import BeautifulSoup
from ezdxf.tools.text import MTextEditor
def polygon_orientation(coords):
# coords = [(x1,y1), (x2,y2), ...]
area = 0
for i in range(len(coords)):
x1, y1 = coords[i]
x2, y2 = coords[(i + 1) % len(coords)]
area += (x2 - x1) * (y2 + y1)
return "CW" if area > 0 else "CCW"
def line_normals(p1, p2, orientation="CCW"):
dx, dy = p2[0]-p1[0], p2[1]-p1[1]
if orientation == "CCW": # inside = left normal
inside = (-dy, dx)
outside = (dy, -dx)
else: # CW polygon
inside = (dy, -dx)
outside = (-dy, dx)
return inside, outside
def line_direction(angle) -> str:
# Normalize angle between -180 and 180
if -90 <= angle <= 90:
return "left → right"
else:
return "right → left"
def format_number(num, mode="tenth"):
if mode == "tenth":
# Add one zero if number < 10
if num < 10:
return f"0{num}"
return str(num)
elif mode == "hundredth":
# Add one zero if number < 100
# Add two zeros if number < 10
if num < 10:
return f"00{num}"
elif num < 100:
return f"0{num}"
return str(num)
else:
raise ValueError("mode must be either 'tenth' or 'hundredth'")
def html_to_mtext(html_text: str):
if not html_text:
return ""
html_text = html_text.replace('\n', '')
soup = BeautifulSoup(html_text, "html.parser")
editor = MTextEditor()
def parse_tag(tag):
for child in tag.children:
if isinstance(child, str): # Plain text
editor.append(child.strip())
else:
# Apply formatting recursively
if child.name == "b" or child.name == "strong":
editor.append("\\B")
parse_tag(child)
editor.append("\\b")
elif child.name == "i":
editor.append("\\I")
parse_tag(child)
editor.append("\\i")
elif child.name == "u":
editor.append(MTextEditor.UNDERLINE_START)
parse_tag(child)
editor.append(MTextEditor.UNDERLINE_STOP)
elif child.name == "br":
editor.append(MTextEditor.NEW_LINE)
elif child.name == "p":
prev = child.previous_sibling
while prev and str(prev).strip() == "":
prev = prev.previous_sibling
if prev is not None:
editor.append(MTextEditor.NEW_LINE)
parse_tag(child)
# editor.append(MTextEditor.NEW_LINE)
else:
parse_tag(child)
parse_tag(soup)
result = str(editor)
result = result.replace('\n', '')
return result