Add color and outline options to icon drawing.

This commit is contained in:
Sergey Vartanov 2021-07-10 19:23:48 +03:00
parent c1abab06a4
commit 557cc0cc98
3 changed files with 43 additions and 10 deletions

View file

@ -11,6 +11,7 @@ from typing import List
import numpy as np
import svgwrite
from colour import Color
from roentgen import server, tile
from roentgen.constructor import Constructor
@ -178,6 +179,7 @@ def draw_icons() -> None:
"""
os.makedirs("icon_set", exist_ok=True)
os.makedirs("icon_set/ids", exist_ok=True)
os.makedirs("icon_set/josm", exist_ok=True)
os.makedirs("icon_set/names", exist_ok=True)
scheme: Scheme = Scheme(Path("scheme/default.yml"))
extractor: ShapeExtractor = ShapeExtractor(
@ -186,6 +188,7 @@ def draw_icons() -> None:
collection: IconCollection = IconCollection.from_scheme(scheme, extractor)
collection.draw_grid(Path("icon_grid.svg"))
collection.draw_icons(Path("icon_set/ids"))
collection.draw_icons(Path("icon_set/josm"), color=Color("black"), outline=True)
collection.draw_icons(Path("icon_set/names"), by_name=True)

View file

@ -3,14 +3,14 @@ Icon grid drawing.
"""
from dataclasses import dataclass
from pathlib import Path
from typing import List, Set
from typing import Dict, List, Optional, Set
import numpy as np
from colour import Color
from svgwrite import Drawing
from roentgen.icon import Icon, ShapeExtractor, ShapeSpecification, Shape
from roentgen.scheme import Scheme
from roentgen.icon import Icon, Shape, ShapeExtractor, ShapeSpecification
from roentgen.scheme import NodeMatcher, Scheme
__author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
@ -103,20 +103,35 @@ class IconCollection:
return cls(icons)
def draw_icons(self, output_directory: Path, by_name: bool = False):
def draw_icons(
self,
output_directory: Path,
by_name: bool = False,
color: Optional[Color] = None,
outline: bool = False,
):
"""
:param output_directory: path to the directory to store individual SVG
files for icons
:param by_name: use names instead of identifiers
:param color: fill color
:param outline: if true, draw outline beneath the icon
"""
if by_name:
def get_file_name(x):
def get_file_name(x) -> str:
"""Generate human-readable file name."""
return f"Röntgen {' + '.join(x.get_names())}.svg"
else:
def get_file_name(x):
def get_file_name(x) -> str:
"""Generate file name with unique identifier."""
return f"{'___'.join(x.get_shape_ids())}.svg"
for icon in self.icons:
icon.draw_to_file(output_directory / get_file_name(icon))
icon.draw_to_file(
output_directory / get_file_name(icon),
color=color,
outline=outline,
)
def draw_grid(
self,

View file

@ -96,7 +96,6 @@ class Shape:
"""
Draw icon into SVG file.
:param svg: SVG file to draw to
:param point: icon position
:param offset: additional offset
:param scale: scale resulting image
@ -111,7 +110,9 @@ class Shape:
transformations.append(f"translate({self.offset[0]},{self.offset[1]})")
return svgwrite.path.Path(d=self.path, transform=" ".join(transformations))
return svgwrite.path.Path(
d=self.path, transform=" ".join(transformations)
)
class ShapeExtractor:
@ -365,15 +366,29 @@ class Icon:
for shape_specification in self.shape_specifications:
shape_specification.draw(svg, point, tags)
def draw_to_file(self, file_name: Path):
def draw_to_file(
self,
file_name: Path,
color: Optional[Color] = None,
outline: bool = False,
):
"""
Draw icon to the SVG file.
:param file_name: output SVG file name
:param color: fill color
:param outline: if true, draw outline beneath the icon
"""
svg: Drawing = Drawing(str(file_name), (16, 16))
for shape_specification in self.shape_specifications:
if color:
shape_specification.color = color
shape_specification.draw(svg, (8, 8), outline=outline)
for shape_specification in self.shape_specifications:
if color:
shape_specification.color = color
shape_specification.draw(svg, (8, 8))
with file_name.open("w") as output_file: