Add configuration to shape extractor.

This commit is contained in:
Sergey Vartanov 2021-05-21 01:28:22 +03:00
parent a8da4fce0a
commit d73e2b6a53
5 changed files with 24 additions and 10 deletions

View file

@ -6,6 +6,7 @@ Author: Sergey Vartanov (me@enzet.ru).
import argparse import argparse
import os import os
import sys import sys
from pathlib import Path
import numpy as np import numpy as np
import svgwrite import svgwrite
@ -14,7 +15,7 @@ from roentgen import ui
from roentgen.constructor import Constructor from roentgen.constructor import Constructor
from roentgen.flinger import Flinger from roentgen.flinger import Flinger
from roentgen.grid import draw_all_icons from roentgen.grid import draw_all_icons
from roentgen.icon import ShapeExtractor from roentgen.icon import ShapeExtractor, ShapeConfiguration
from roentgen.mapper import ( from roentgen.mapper import (
AUTHOR_MODE, CREATION_TIME_MODE, ICONS_FILE_NAME, Painter, TAGS_FILE_NAME, AUTHOR_MODE, CREATION_TIME_MODE, ICONS_FILE_NAME, Painter, TAGS_FILE_NAME,
check_level_number, check_level_overground check_level_number, check_level_overground
@ -82,7 +83,9 @@ def main(argv) -> None:
svg: svgwrite.Drawing = ( svg: svgwrite.Drawing = (
svgwrite.Drawing(options.output_file_name, size=size)) svgwrite.Drawing(options.output_file_name, size=size))
icon_extractor: ShapeExtractor = ShapeExtractor(ICONS_FILE_NAME) icon_extractor: ShapeExtractor = ShapeExtractor(
ICONS_FILE_NAME, Path("icons/config.json")
)
def check_level(x) -> bool: def check_level(x) -> bool:
""" Draw objects on all levels. """ """ Draw objects on all levels. """
@ -128,8 +131,8 @@ def draw_element(target: str, tags_description: str):
""" """
tags = dict([x.split("=") for x in tags_description.split(",")]) tags = dict([x.split("=") for x in tags_description.split(",")])
scheme = Scheme("scheme/default.yml") scheme = Scheme("scheme/default.yml")
icon_extractor = ShapeExtractor("icons/icons.svg") extractor = ShapeExtractor("icons/icons.svg", Path("icons/config.json"))
icon, priority = scheme.get_icon(icon_extractor, tags) icon, priority = scheme.get_icon(extractor, tags)
is_for_node: bool = target == "node" is_for_node: bool = target == "node"
labels = scheme.construct_text(tags, True) labels = scheme.construct_text(tags, True)
point = Point( point = Point(

View file

@ -4,6 +4,7 @@ Icon grid drawing.
Author: Sergey Vartanov (me@enzet.ru). Author: Sergey Vartanov (me@enzet.ru).
""" """
from os.path import join from os.path import join
from pathlib import Path
from typing import Any, Dict, List, Set from typing import Any, Dict, List, Set
import numpy as np import numpy as np
@ -36,7 +37,9 @@ def draw_all_icons(
icons: List[Icon] = [] icons: List[Icon] = []
icons_file_name: str = "icons/icons.svg" icons_file_name: str = "icons/icons.svg"
extractor: ShapeExtractor = ShapeExtractor(icons_file_name) extractor: ShapeExtractor = ShapeExtractor(
icons_file_name, Path("icons/config.json")
)
def add() -> None: def add() -> None:
""" """

View file

@ -81,7 +81,8 @@ class ShapeConfiguration:
""" """
def __init__(self, file_name: Path): def __init__(self, file_name: Path):
content = json.load(file_name) with file_name.open() as input_file:
content = json.load(input_file)
self.right_directed: Set[str] = set(content["right_directed"]) self.right_directed: Set[str] = set(content["right_directed"])
@ -92,7 +93,7 @@ class ShapeExtractor:
Shape is a single path with "id" attribute that aligned to 16×16 grid. Shape is a single path with "id" attribute that aligned to 16×16 grid.
""" """
def __init__(self, svg_file_name: str): def __init__(self, svg_file_name: str, configuration_file_name: Path):
""" """
:param svg_file_name: input SVG file name with icons. File may contain :param svg_file_name: input SVG file name with icons. File may contain
any other irrelevant graphics. any other irrelevant graphics.
@ -108,6 +109,8 @@ class ShapeExtractor:
if isinstance(node, Element): if isinstance(node, Element):
self.parse(node) self.parse(node)
self.configuration = ShapeConfiguration(configuration_file_name)
def parse(self, node: Element) -> None: def parse(self, node: Element) -> None:
""" """
Extract icon paths into a map. Extract icon paths into a map.

View file

@ -173,8 +173,10 @@ class Scheme:
return False return False
def get_icon( def get_icon(
self, icon_extractor: ShapeExtractor, tags: Dict[str, Any], self,
for_: str = "node" icon_extractor: ShapeExtractor,
tags: Dict[str, Any],
for_: str = "node",
) -> Tuple[IconSet, int]: ) -> Tuple[IconSet, int]:
""" """
Construct icon set. Construct icon set.

View file

@ -4,6 +4,7 @@ Test icon generation for nodes.
Author: Sergey Vartanov (me@enzet.ru). Author: Sergey Vartanov (me@enzet.ru).
""" """
from os import makedirs from os import makedirs
from pathlib import Path
from typing import Dict from typing import Dict
from roentgen.grid import draw_all_icons from roentgen.grid import draw_all_icons
@ -19,7 +20,9 @@ def test_icons() -> None:
def get_icon(tags: Dict[str, str]): def get_icon(tags: Dict[str, str]):
scheme = Scheme("scheme/default.yml") scheme = Scheme("scheme/default.yml")
icon_extractor = ShapeExtractor("icons/icons.svg") icon_extractor = ShapeExtractor(
"icons/icons.svg", Path("icons/config.json")
)
icon, _ = scheme.get_icon(icon_extractor, tags) icon, _ = scheme.get_icon(icon_extractor, tags)
return icon return icon