-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.txt
144 lines (113 loc) · 5.47 KB
/
chat.txt
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import re
def convert_svg_matrix_to_actual_coordinates(transform_string, x, y):
# Parse the transform string to get the matrix values
matrix_values = list(map(float, re.findall(r"matrix\(([^)]+)\)", transform_string)[0].split(',')))
# Apply the matrix transformation to the point
new_x = float(matrix_values[0])*x + float(matrix_values[2])*y + float(matrix_values[4])
new_y = float(matrix_values[1])*x + float(matrix_values[3])*y + float(matrix_values[5])
return new_x, new_y
print(convert_svg_matrix_to_actual_coordinates("matrix(3.,-0.707107, 0.707107,0.707107,0,0)", 0, 0))
#####################################################
import xml.etree.ElementTree as ET
import numpy as np
# Parse the SVG file
tree = ET.parse('your_file.svg')
root = tree.getroot()
# Function to apply matrix transformation to a point
def apply_matrix(matrix, point):
x, y = point
new_x = matrix[0] * x + matrix[2] * y + matrix[4]
new_y = matrix[1] * x + matrix[3] * y + matrix[5]
return new_x, new_y
# Function to parse the transform attribute and get the transformation matrix
def parse_transform(transform):
if transform is None:
return np.identity(3)
else:
transform_values = [float(value) for value in transform.split('(')[1].split(')')[0].split(',')]
return np.array(transform_values).reshape((3, 2))
# Function to parse path data and apply transformations
def parse_path(path_element):
path_data = path_element.attrib['d']
transform = path_element.attrib.get('transform')
# Parse the transform attribute to get the transformation matrix
matrix = parse_transform(transform)
# Parse path data and apply the transformation
path_commands = path_data.split()
current_point = (0, 0)
transformed_points = []
i = 0
while i < len(path_commands):
command = path_commands[i]
i += 1
if command.isalpha(): # Check if the command is a letter (e.g., M, L, etc.)
args = [float(value) for value in path_commands[i:i+6]]
i += len(args)
if command == 'M':
current_point = args[0], args[1]
transformed_points.append(apply_matrix(matrix, current_point))
elif command == 'L':
for j in range(0, len(args), 2):
current_point = args[j], args[j + 1]
transformed_points.append(apply_matrix(matrix, current_point))
elif command == 'C':
# Cubic Bezier Curve
for j in range(0, len(args), 6):
control_point1 = args[j], args[j + 1]
control_point2 = args[j + 2], args[j + 3]
end_point = args[j + 4], args[j + 5]
transformed_points.extend([apply_matrix(matrix, control_point1),
apply_matrix(matrix, control_point2),
apply_matrix(matrix, end_point)])
elif command == 'V':
# Vertical Line
current_point = current_point[0], args[0]
transformed_points.append(apply_matrix(matrix, current_point))
elif command == 'H':
# Horizontal Line
current_point = args[0], current_point[1]
transformed_points.append(apply_matrix(matrix, current_point))
# Add other command types as needed
return transformed_points
# Find all path elements in the SVG file
path_elements = root.findall('.//{http://www.w3.org/2000/svg}path')
# Loop through each path and parse the transformed coordinates
for path_element in path_elements:
transformed_points = parse_path(path_element)
print("Transformed Points:", transformed_points)
#####################################################################
import xml.etree.ElementTree as ET
def merge_text_to_words(svg_file_path, output_svg_file_path, proximity_threshold=5):
tree = ET.parse(svg_file_path)
root = tree.getroot()
# Dictionary to store words and their corresponding text elements
words = {}
# Iterate through <text> elements in the SVG
for text_element in root.iter('text'):
x = float(text_element.attrib.get('x', 0))
y = float(text_element.attrib.get('y', 0))
# Check if there is a nearby word based on proximity threshold
found_word = False
for key, (word_x, word_y) in words.items():
if abs(x - word_x) < proximity_threshold and abs(y - word_y) < proximity_threshold:
# Concatenate the text content to the existing word
words[key] = (word_x, word_y, text_element.text if text_element.text else '')
found_word = True
break
if not found_word:
# Create a new word entry
words[len(words) + 1] = (x, y, text_element.text if text_element.text else '')
# Remove the current <text> element from the SVG
root.remove(text_element)
# Create new <text> elements for the merged words
for _, (word_x, word_y, word_content) in words.items():
new_text = ET.Element('text', {'x': str(word_x), 'y': str(word_y)})
new_text.text = word_content
root.append(new_text)
# Save the modified SVG to a new file
tree.write(output_svg_file_path)
# Example usage
input_svg_file = 'input.svg'
output_svg_file = 'output.svg'
merge_text_to_words(input_svg_file, output_svg_file)
#####################################################################