Spaces:
Sleeping
Sleeping
| """ | |
| 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() | |