File size: 4,074 Bytes
58e6885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
ad-hoc 对比验证:同一个 SVG,跑 take_screenshot 两次(开/关 overlap JS)。
目的:证实关掉 overlap JS 后字号回归一致。

用法:
    PYTHONPATH=. python scripts/verify_overlap_off.py
输出:
    output/overlap_verify/<sample>/{before.png, after.png, before_svg.svg, after_svg.svg}
"""

import os
import re
import shutil
import sys

ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.insert(0, ROOT)
os.chdir(ROOT)

from modules.infographics_generator.screenshot_utils import get_driver, take_screenshot
from modules.infographics_generator.parse_utils import convert_svg_to_html

SAMPLES = [
    "output/quality_check/bump_chart_02/1779376263_bump_chart_02_Art_scenario_1_031515_57323.svg",
    "output/quality_check/bump_chart_03/1779376284_bump_chart_03_Art_scenario_1_031515_57323.svg",
    "output/quality_check/bump_chart_06/1779376317_bump_chart_06_Art_scenario_1_031515_57323.svg",
    "output/quality_check/radial_layered_spline_area_chart_grid_01/1779380156_radial_layered_spline_area_chart_grid_01_Economic_Trends_scenario_5_030631_19067.svg",
]

OUT_ROOT = "output/overlap_verify"
os.makedirs(OUT_ROOT, exist_ok=True)


_INLINE_FS_RE = re.compile(r'(\sstyle="[^"]*?)font-size:\s*[^;"]+;?\s*([^"]*")')


def strip_inline_font_size(svg_text: str) -> str:
    """把每个 text 上 inline style 里的 font-size: xxx 段去掉,留下 style 里其它属性。
    如果剥完整 style 变成 style="",整个 style 属性删掉。"""

    def repl(m):
        head, tail = m.group(1), m.group(2)
        rebuilt = head + tail
        rebuilt = rebuilt.replace('style=""', "")
        rebuilt = re.sub(r'style="\s*"', "", rebuilt)
        return rebuilt

    prev = None
    cur = svg_text
    while prev != cur:
        prev = cur
        cur = _INLINE_FS_RE.sub(repl, cur)
    return cur


def font_size_histogram(svg_text: str) -> dict:
    sizes = re.findall(r"font-size:\s*(\d+(?:\.\d+)?)px", svg_text)
    out = {}
    for s in sizes:
        out[s] = out.get(s, 0) + 1
    return out


def run_one(driver, sample_svg_path: str):
    sample_name = (
        os.path.splitext(os.path.basename(sample_svg_path))[0]
        .replace("_", "-")[:80]
    )
    work_dir = os.path.join(OUT_ROOT, sample_name)
    os.makedirs(work_dir, exist_ok=True)
    print(f"\n=== {sample_name} ===", flush=True)

    with open(sample_svg_path, encoding="utf-8") as f:
        original_svg = f.read()
    print(
        f"  patched-svg font-size hist (input): {font_size_histogram(original_svg)}",
        flush=True,
    )

    stripped = strip_inline_font_size(original_svg)
    print(
        f"  stripped-svg font-size hist:        {font_size_histogram(stripped)}",
        flush=True,
    )

    stripped_svg_path = os.path.join(work_dir, "stripped.svg")
    with open(stripped_svg_path, "w", encoding="utf-8") as f:
        f.write(stripped)

    html_path = os.path.join(work_dir, "render.html")
    convert_svg_to_html(stripped_svg_path, html_path)

    take_screenshot(driver, html_path, resolve_text_overlap=True)
    on_png = os.path.join(work_dir, "overlap_ON.png")
    shutil.move(html_path.replace(".html", ".png"), on_png)

    convert_svg_to_html(stripped_svg_path, html_path)
    take_screenshot(driver, html_path, resolve_text_overlap=False)
    off_png = os.path.join(work_dir, "overlap_OFF.png")
    shutil.move(html_path.replace(".html", ".png"), off_png)

    print(f"  -> ON  : {on_png}", flush=True)
    print(f"  -> OFF : {off_png}", flush=True)


def main():
    print(f"OUT_ROOT={OUT_ROOT}", flush=True)
    driver = get_driver()
    for svg in SAMPLES:
        if not os.path.exists(svg):
            print(f"skip missing: {svg}", flush=True)
            continue
        run_one(driver, svg)
    driver.quit()
    print("\nDONE. Open these PNGs side-by-side to compare:", flush=True)
    for root, dirs, files in os.walk(OUT_ROOT):
        for fn in files:
            if fn.endswith(".png"):
                print(" ", os.path.join(root, fn))


if __name__ == "__main__":
    main()