Add scale to icon grid.

This commit is contained in:
Sergey Vartanov 2021-11-07 15:36:35 +03:00
parent d812058ecb
commit c543ddf894
2 changed files with 23 additions and 20 deletions

View file

@ -279,6 +279,7 @@ class ShapeSpecification:
tags: dict[str, Any] = None, tags: dict[str, Any] = None,
outline: bool = False, outline: bool = False,
outline_opacity: float = 1.0, outline_opacity: float = 1.0,
scale: float = 1.0,
) -> None: ) -> None:
""" """
Draw icon shape into SVG file. Draw icon shape into SVG file.
@ -289,15 +290,18 @@ class ShapeSpecification:
displayed, this argument should be None displayed, this argument should be None
:param outline: draw outline for the shape :param outline: draw outline for the shape
:param outline_opacity: opacity of the outline :param outline_opacity: opacity of the outline
:param scale: scale icon by the magnitude
""" """
scale: np.ndarray = np.array((1, 1)) scale_vector: np.ndarray = np.array((scale, scale))
if self.flip_vertically: if self.flip_vertically:
scale = np.array((1, -1)) scale_vector = np.array((scale, -scale))
if self.flip_horizontally: if self.flip_horizontally:
scale = np.array((-1, 1)) scale_vector = np.array((-scale, scale))
point: np.ndarray = np.array(list(map(int, point))) point: np.ndarray = np.array(list(map(int, point)))
path: SVGPath = self.shape.get_path(point, self.offset, scale) path: SVGPath = self.shape.get_path(
point, self.offset * scale, scale_vector
)
path.update({"fill": self.color.hex}) path.update({"fill": self.color.hex})
if outline and self.use_outline: if outline and self.use_outline:
@ -355,6 +359,7 @@ class Icon:
point: np.ndarray, point: np.ndarray,
tags: dict[str, Any] = None, tags: dict[str, Any] = None,
outline: bool = False, outline: bool = False,
scale: float = 1,
) -> None: ) -> None:
""" """
Draw icon to SVG. Draw icon to SVG.
@ -369,12 +374,14 @@ class Icon:
opacity: float = 0.7 if bright else 0.5 opacity: float = 0.7 if bright else 0.5
outline_group: Group = Group(opacity=opacity) outline_group: Group = Group(opacity=opacity)
for shape_specification in self.shape_specifications: for shape_specification in self.shape_specifications:
shape_specification.draw(outline_group, point, tags, True) shape_specification.draw(
outline_group, point, tags, True, scale=scale
)
svg.add(outline_group) svg.add(outline_group)
else: else:
group: Group = Group(opacity=self.opacity) group: Group = Group(opacity=self.opacity)
for shape_specification in self.shape_specifications: for shape_specification in self.shape_specifications:
shape_specification.draw(group, point, tags) shape_specification.draw(group, point, tags, scale=scale)
svg.add(group) svg.add(group)
def draw_to_file( def draw_to_file(

View file

@ -9,7 +9,6 @@ from typing import Optional
import numpy as np import numpy as np
from colour import Color from colour import Color
from svgwrite import Drawing from svgwrite import Drawing
from svgwrite.shapes import Rect
from map_machine.pictogram.icon import ( from map_machine.pictogram.icon import (
Icon, Icon,
@ -166,6 +165,7 @@ class IconCollection:
columns: int = 16, columns: int = 16,
step: float = 24, step: float = 24,
background_color: Color = Color("white"), background_color: Color = Color("white"),
scale: float = 1.0,
) -> None: ) -> None:
""" """
Draw icons in the form of table. Draw icons in the form of table.
@ -174,26 +174,22 @@ class IconCollection:
:param columns: number of columns in grid :param columns: number of columns in grid
:param step: horizontal and vertical distance between icons in grid :param step: horizontal and vertical distance between icons in grid
:param background_color: background color :param background_color: background color
:param scale: scale icon by the magnitude
""" """
point: np.ndarray = np.array((step / 2, step / 2)) point: np.ndarray = np.array((step / 2 * scale, step / 2 * scale))
width: float = step * columns width: float = step * columns * scale
height: int = int(int(len(self.icons) / (width / step) + 1) * step) height: int = int(int(len(self.icons) / columns + 1) * step * scale)
svg: Drawing = Drawing(str(file_name), (width, height)) svg: Drawing = Drawing(str(file_name), (width, height))
svg.add(svg.rect((0, 0), (width, height), fill=background_color.hex)) svg.add(svg.rect((0, 0), (width, height), fill=background_color.hex))
for icon in self.icons: for icon in self.icons:
icon: Icon icon.draw(svg, point, scale=scale)
rectangle: Rect = svg.rect( point += np.array((step * scale, 0))
point - np.array((10, 10)), (20, 20), fill=background_color.hex
)
svg.add(rectangle)
icon.draw(svg, point)
point += np.array((step, 0))
if point[0] > width - 8: if point[0] > width - 8:
point[0] = step / 2 point[0] = step / 2 * scale
point += np.array((0, step)) point += np.array((0, step * scale))
height += step height += step * scale
with file_name.open("w", encoding="utf-8") as output_file: with file_name.open("w", encoding="utf-8") as output_file:
svg.write(output_file) svg.write(output_file)