From 9a6f8d2f872c42f7b74491603a414d8a85a2ba83 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Wed, 26 Jan 2022 01:44:22 +0300 Subject: [PATCH] Issue #103: support credits. --- map_machine/drawing.py | 29 +++++++++++++++++++++++++ map_machine/mapper.py | 31 ++++++++++++++++++++++++--- map_machine/pictogram/point.py | 39 +++++++++++----------------------- 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/map_machine/drawing.py b/map_machine/drawing.py index ccf963c..10b88d6 100644 --- a/map_machine/drawing.py +++ b/map_machine/drawing.py @@ -20,6 +20,8 @@ __email__ = "me@enzet.ru" PathCommands = list[Union[float, str, np.ndarray]] +DEFAULT_FONT: str = "Helvetica" + @dataclass class Style: @@ -295,3 +297,30 @@ def parse_path(path: str) -> PathCommands: index += 1 return result + + +def draw_text( + svg: svgwrite.Drawing, + text: str, + point: np.ndarray, + size: float, + fill: Color, + anchor: str = "middle", + stroke_linejoin: str = "round", + stroke_width: float = 1.0, + stroke: Optional[Color] = None, + opacity: float = 1.0, +): + text_element = svg.text( + text, + point, + font_size=size, + text_anchor=anchor, + font_family=DEFAULT_FONT, + fill=fill.hex, + stroke_linejoin=stroke_linejoin, + stroke_width=stroke_width, + stroke=stroke.hex if stroke else "none", + opacity=opacity, + ) + svg.add(text_element) diff --git a/map_machine/mapper.py b/map_machine/mapper.py index ca696a1..f67cceb 100644 --- a/map_machine/mapper.py +++ b/map_machine/mapper.py @@ -15,6 +15,7 @@ from svgwrite.path import Path as SVGPath from svgwrite.shapes import Rect from map_machine.constructor import Constructor +from map_machine.drawing import draw_text from map_machine.feature.road import Intersection, Road, RoadPart from map_machine.figure import StyledFigure from map_machine.geometry.boundary_box import BoundaryBox @@ -113,6 +114,8 @@ class Map: self.svg, occupied, self.configuration.label_mode ) + self.draw_credits(constructor.flinger.size) + def draw_buildings(self, constructor: Constructor) -> None: """Draw buildings: shade, walls, and roof.""" if self.configuration.building_mode == BuildingMode.NO: @@ -172,10 +175,32 @@ class Map: intersection: Intersection = Intersection(list(parts)) intersection.draw(self.svg, True) + def draw_credits(self, size: np.ndarray): + + for text, point in ( + ("Rendering: © Map Machine", np.array((15, 27))), + ("Data: © OpenStreetMap contributors", np.array((15, 15))), + ): + for stroke_width, stroke, opacity in ( + (3.0, Color("white"), 0.7), + (1.0, None, 1.0), + ): + draw_text( + self.svg, + text, + size - point, + 10, + Color("#888888"), + anchor="end", + stroke_width=stroke_width, + stroke=stroke, + opacity=opacity, + ) + def render_map(arguments: argparse.Namespace) -> None: """ - Map Machine entry point. + Map rendering entry point. :param arguments: command-line arguments """ @@ -257,10 +282,10 @@ def render_map(arguments: argparse.Namespace) -> None: ) constructor.construct() - painter: Map = Map( + map_: Map = Map( flinger=flinger, svg=svg, scheme=scheme, configuration=configuration ) - painter.draw(constructor) + map_.draw(constructor) logging.info(f"Writing output SVG to {arguments.output_file_name}...") with open(arguments.output_file_name, "w", encoding="utf-8") as output_file: diff --git a/map_machine/pictogram/point.py b/map_machine/pictogram/point.py index a88b8d8..36887fd 100644 --- a/map_machine/pictogram/point.py +++ b/map_machine/pictogram/point.py @@ -7,6 +7,7 @@ import numpy as np import svgwrite from colour import Color +from map_machine.drawing import draw_text from map_machine.map_configuration import LabelMode from map_machine.osm.osm_reader import Tagged from map_machine.pictogram.icon import Icon, IconSet @@ -15,8 +16,6 @@ from map_machine.text import Label __author__ = "Sergey Vartanov" __email__ = "me@enzet.ru" -DEFAULT_FONT: str = "Helvetica" - class Occupied: """ @@ -238,42 +237,28 @@ class Point(Tagged): svg.add(svg.rect((point[0] + i, point[1] + j), (1, 1))) if out_fill_2: - text_element = svg.text( + draw_text( + svg, text, point, - font_size=size, - text_anchor="middle", - font_family=DEFAULT_FONT, - fill=out_fill_2.hex, - stroke_linejoin="round", + size, + fill=out_fill_2, stroke_width=5.0, - stroke=out_fill_2.hex, + stroke=out_fill_2, opacity=out_opacity_2, ) - svg.add(text_element) if out_fill: - text_element = svg.text( + draw_text( + svg, text, point, - font_size=size, - text_anchor="middle", - font_family=DEFAULT_FONT, - fill=out_fill.hex, - stroke_linejoin="round", + size, + fill, stroke_width=3.0, - stroke=out_fill.hex, + stroke=out_fill, opacity=out_opacity, ) - svg.add(text_element) - text_element = svg.text( - text, - point, - font_size=size, - text_anchor="middle", - font_family=DEFAULT_FONT, - fill=fill.hex, - ) - svg.add(text_element) + draw_text(svg, text, point, size, fill) self.y += 11