86 lines
3.5 KiB
Python
86 lines
3.5 KiB
Python
import json
|
|
import sys
|
|
import os
|
|
|
|
def convert_json_to_svgs(json_path):
|
|
"""
|
|
Читает map.json файл и создает из него набор SVG файлов, по одному на страницу.
|
|
"""
|
|
try:
|
|
with open(json_path, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
except Exception as e:
|
|
print(f"Error reading or parsing JSON file: {e}")
|
|
return
|
|
|
|
map_data = data.get("map", [])
|
|
font_map = data.get("font_map", {})
|
|
|
|
# Группируем элементы по страницам
|
|
pages = {}
|
|
for item in map_data:
|
|
page_num = item["ipage"]
|
|
if page_num not in pages:
|
|
pages[page_num] = []
|
|
pages[page_num].append(item)
|
|
|
|
# --- Создаем CSS-классы для шрифтов ---
|
|
style_block = "<style type=\"text/css\">\n"
|
|
# Создаем классы для шрифтов .font0, .font1 и т.д.
|
|
# Сортируем по значению (индексу), чтобы обеспечить правильный порядок
|
|
sorted_font_map = sorted(font_map.items(), key=lambda x: x[1])
|
|
for font_name, font_index in sorted_font_map:
|
|
style_block += f"\t.font{font_index} {{ font-family: '{font_name}'; }}\n"
|
|
|
|
# Создаем классы для размеров .size10, .size9 и т.д.
|
|
all_sizes = sorted(list(set(item["size"] for item in map_data)), reverse=True)
|
|
size_classes = {}
|
|
for i, size in enumerate(all_sizes):
|
|
class_name = f"size{i}"
|
|
size_classes[size] = class_name
|
|
style_block += f"\t.{class_name} {{ font-size: {size}px; }}\n"
|
|
style_block += "</style>"
|
|
|
|
# --- Создаем SVG для каждой страницы ---
|
|
output_dir = "verizon_svgs"
|
|
if not os.path.exists(output_dir):
|
|
os.makedirs(output_dir)
|
|
|
|
for page_num, items in pages.items():
|
|
svg_filename = os.path.join(output_dir, f"{page_num + 1}.svg")
|
|
|
|
svg_header = f"""<?xml version="1.0" encoding="utf-8"?>
|
|
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
|
|
viewBox="0 0 612 792" style="enable-background:new 0 0 612 792;" xml:space="preserve">
|
|
{style_block}
|
|
"""
|
|
svg_footer = "\n</svg>"
|
|
|
|
svg_body = ""
|
|
for item in items:
|
|
text = item["field"].replace('&', '&').replace('<', '<').replace('>', '>')
|
|
x = item["x_ill"]
|
|
# --- КЛЮЧЕВОЕ ИСПРАВЛЕНИЕ ---
|
|
# y в SVG transform - это базовая линия. Она примерно равна y_ill + size.
|
|
y = item["y_ill"] + item["size"]
|
|
|
|
font_name = item["ifont"]
|
|
ifont_class = f"font{font_map.get(font_name, 0)}"
|
|
|
|
size = item["size"]
|
|
size_class = size_classes.get(size, "")
|
|
|
|
svg_body += f' <text transform="matrix(1 0 0 1 {x:.2f} {y:.2f})" class="{ifont_class} {size_class}">{text}</text>\n'
|
|
|
|
# Сохраняем SVG файл
|
|
with open(svg_filename, 'w', encoding='utf-8') as f:
|
|
f.write(svg_header + svg_body + svg_footer)
|
|
print(f"Successfully created {svg_filename}")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python json_to_svg.py <path_to_map.json>")
|
|
sys.exit(1)
|
|
|
|
json_path = sys.argv[1]
|
|
convert_json_to_svgs(json_path) |