| 1 |
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file. |
| 2 |
|
| 3 |
"""Fix SVG icons by removing conflicting fill attributes. |
| 4 |
|
| 5 |
This script automatically finds and fixes SVG icons that have conflicting |
| 6 |
fill attributes (both fill="..." and style="fill:...") which cause |
| 7 |
inconsistent rendering between Qt and Windows native renderers. |
| 8 |
""" |
| 9 |
|
| 10 |
import re |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
|
| 14 |
def has_conflicting_fill(content: str) -> bool: |
| 15 |
"""Check if SVG content has conflicting fill attributes. |
| 16 |
|
| 17 |
Args: |
| 18 |
content: SVG file content |
| 19 |
|
| 20 |
Returns: |
| 21 |
True if conflicting fill attributes are found |
| 22 |
""" |
| 23 |
# Check for elements with both fill attribute and style with fill |
| 24 |
pattern1 = ( |
| 25 |
r"<(circle|rect|path|polygon|ellipse)[^>]*" |
| 26 |
r'fill="[^"]*"[^>]*style="[^"]*fill:[^"]+"' |
| 27 |
) |
| 28 |
pattern2 = ( |
| 29 |
r"<(circle|rect|path|polygon|ellipse)[^>]*" |
| 30 |
r'style="[^"]*fill:[^"]+"[^>]*fill="[^"]*"' |
| 31 |
) |
| 32 |
return bool(re.search(pattern1, content) or re.search(pattern2, content)) |
| 33 |
|
| 34 |
|
| 35 |
def find_problematic_icons(icons_dir: Path) -> list[Path]: |
| 36 |
"""Find all SVG icons with conflicting fill attributes. |
| 37 |
|
| 38 |
Args: |
| 39 |
icons_dir: Path to the icons directory |
| 40 |
|
| 41 |
Returns: |
| 42 |
List of paths to problematic SVG files |
| 43 |
""" |
| 44 |
problematic = [] |
| 45 |
for svg_file in icons_dir.rglob("*.svg"): |
| 46 |
content = svg_file.read_text(encoding="utf-8") |
| 47 |
if has_conflicting_fill(content): |
| 48 |
problematic.append(svg_file) |
| 49 |
return problematic |
| 50 |
|
| 51 |
|
| 52 |
def fix_icon(filepath: str) -> None: |
| 53 |
"""Fix an icon by removing conflicting fill attributes from elements |
| 54 |
that have both fill attribute and style with fill. |
| 55 |
|
| 56 |
Args: |
| 57 |
filepath: Path to the SVG file |
| 58 |
""" |
| 59 |
path = Path(filepath) |
| 60 |
content = path.read_text(encoding="utf-8") |
| 61 |
|
| 62 |
# Pattern to find elements with both fill="..." and style="...fill:..." |
| 63 |
def fix_element(match): |
| 64 |
element = match.group(0) |
| 65 |
# Check if has both fill attribute and style with fill |
| 66 |
if 'fill="' in element and 'style="' in element: |
| 67 |
# Extract fill color from style |
| 68 |
style_match = re.search(r'style="[^"]*fill:\s*([^;"\s]+)', element) |
| 69 |
if style_match: |
| 70 |
fill_color = style_match.group(1) |
| 71 |
# Remove the conflicting fill attribute |
| 72 |
element = re.sub(r'\s+fill="[^"]*"', "", element) |
| 73 |
# Remove the style attribute |
| 74 |
element = re.sub(r'\s+style="[^"]*"', "", element) |
| 75 |
# Add the fill from style as a direct attribute before the closing |
| 76 |
if element.endswith("/>"): |
| 77 |
element = element[:-2] + f' fill="{fill_color}" fill-opacity="1" />' |
| 78 |
elif element.endswith(">"): |
| 79 |
element = element[:-1] + f' fill="{fill_color}" fill-opacity="1">' |
| 80 |
return element |
| 81 |
|
| 82 |
# Apply to all elements |
| 83 |
for tag in ["circle", "rect", "path", "polygon", "ellipse"]: |
| 84 |
pattern = f"<{tag}[^>]*/?>" |
| 85 |
content = re.sub(pattern, fix_element, content) |
| 86 |
|
| 87 |
path.write_text(content, encoding="utf-8") |
| 88 |
print(f"Fixed: {filepath}") |
| 89 |
|
| 90 |
|
| 91 |
def main(): |
| 92 |
"""Find and fix all icon files with conflicting fill attributes.""" |
| 93 |
icons_dir = Path(__file__).parent.parent / "datalab" / "data" / "icons" |
| 94 |
|
| 95 |
print(f"Scanning {icons_dir} for SVG icons with conflicting fill attributes...") |
| 96 |
problematic_files = find_problematic_icons(icons_dir) |
| 97 |
|
| 98 |
if not problematic_files: |
| 99 |
print("No icons with conflicting fill attributes found!") |
| 100 |
return |
| 101 |
|
| 102 |
print(f"Found {len(problematic_files)} icons with conflicting fill attributes:") |
| 103 |
for file in problematic_files: |
| 104 |
rel_path = file.relative_to(icons_dir) |
| 105 |
print(f" - {rel_path}") |
| 106 |
|
| 107 |
print("\nFixing icons...") |
| 108 |
for file in problematic_files: |
| 109 |
fix_icon(str(file)) |
| 110 |
|
| 111 |
print(f"\nSuccessfully fixed {len(problematic_files)} icons!") |
| 112 |
|
| 113 |
|
| 114 |
if __name__ == "__main__": |
| 115 |
main() |