|
from xml.etree.ElementTree import ElementTree |
|
|
|
def apply_transformations(output_svg, brightness, text_scale, line_scale): |
|
""" |
|
Applies transformations to the SVG file for brightness, text size, and line thickness. |
|
|
|
Args: |
|
output_svg (str): Path to the SVG file. |
|
brightness (float): Brightness level (0.5 to 5.0). |
|
text_scale (float): Scale for text size (e.g., 1.5 for 150%). |
|
line_scale (float): Scale for line thickness (0.5 to 5.0). |
|
""" |
|
tree = ElementTree() |
|
tree.parse(output_svg) |
|
root = tree.getroot() |
|
|
|
|
|
for element in root.findall(".//path"): |
|
current_style = element.attrib.get("style", "") |
|
new_style = f"stroke-width:{line_scale}px; fill-opacity:{brightness/5}; {current_style}" |
|
element.attrib["style"] = new_style.strip() |
|
|
|
|
|
for element in root.findall(".//text"): |
|
|
|
font_size_attr = element.attrib.get("style", "") |
|
if "font-size" in font_size_attr: |
|
|
|
current_font_size = extract_font_size(font_size_attr) |
|
new_font_size = current_font_size * text_scale |
|
new_style = font_size_attr.replace( |
|
f"font-size:{current_font_size}px;", f"font-size:{new_font_size}px;" |
|
) |
|
element.attrib["style"] = new_style |
|
else: |
|
|
|
new_style = f"font-size:{text_scale}em; {element.attrib.get('style', '')}" |
|
element.attrib["style"] = new_style.strip() |
|
|
|
|
|
tree.write(output_svg) |
|
|
|
def extract_font_size(style): |
|
"""Extracts the font-size in px from a style string. Defaults to 16px if not found.""" |
|
import re |
|
match = re.search(r"font-size:(\d+(\.\d+)?)px;", style) |
|
if match: |
|
return float(match.group(1)) |
|
return 16.0 |
|
|