Issue #103: support credits.

This commit is contained in:
Sergey Vartanov 2022-01-26 01:44:22 +03:00
parent 2f1769ae0a
commit 9a6f8d2f87
3 changed files with 69 additions and 30 deletions

View file

@ -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)

View file

@ -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:

View file

@ -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