Issues #67, #122: refactor text constructing.

Fixes main label mode.
This commit is contained in:
Sergey Vartanov 2022-04-08 02:35:59 +03:00
parent a082722b6d
commit 6e38ca5aa8
5 changed files with 38 additions and 41 deletions

View file

@ -39,7 +39,7 @@ from map_machine.pictogram.icon import (
)
from map_machine.pictogram.point import Point
from map_machine.scheme import LineStyle, RoadMatcher, Scheme
from map_machine.text import Label
from map_machine.text import Label, TextConstructor
from map_machine.ui.cli import BuildingMode
from map_machine.util import MinMax
@ -171,6 +171,7 @@ class Constructor:
self.scheme: Scheme = scheme
self.extractor: ShapeExtractor = extractor
self.configuration: MapConfiguration = configuration
self.text_constructor: TextConstructor = TextConstructor(self.scheme)
if self.configuration.level == "all":
self.check_level = lambda x: True
@ -315,8 +316,10 @@ class Constructor:
self.extractor, line.tags, processed, self.configuration
)
if icon_set is not None:
labels: list[Label] = self.scheme.construct_text(
line.tags, processed, self.configuration.label_mode
labels: list[Label] = self.text_constructor.construct_text(
line.tags,
processed,
self.configuration.label_mode,
)
point: Point = Point(
icon_set,
@ -355,7 +358,7 @@ class Constructor:
self.extractor, line.tags, processed, self.configuration
)
if icon_set is not None:
labels: list[Label] = self.scheme.construct_text(
labels: list[Label] = self.text_constructor.construct_text(
line.tags, processed, self.configuration.label_mode
)
point: Point = Point(
@ -497,7 +500,7 @@ class Constructor:
if icon_set is None:
return
labels: list[Label] = self.scheme.construct_text(
labels: list[Label] = self.text_constructor.construct_text(
tags, processed, self.configuration.label_mode
)
self.scheme.process_ignored(tags, processed)

View file

@ -13,7 +13,7 @@ from map_machine.map_configuration import LabelMode
from map_machine.pictogram.icon import ShapeExtractor
from map_machine.pictogram.point import Point
from map_machine.scheme import LineStyle, Scheme
from map_machine.text import Label
from map_machine.text import Label, TextConstructor
from map_machine.workspace import workspace
__author__ = "Sergey Vartanov"
@ -45,7 +45,10 @@ def draw_element(options: argparse.Namespace) -> None:
processed: set[str] = set()
icon, _ = scheme.get_icon(extractor, tags, processed)
is_for_node: bool = target == "node"
labels: list[Label] = scheme.construct_text(tags, processed, LabelMode.ALL)
text_constructor: TextConstructor = TextConstructor(scheme)
labels: list[Label] = text_constructor.construct_text(
tags, processed, LabelMode.ALL
)
point: Point = Point(
icon,
labels,

View file

@ -13,7 +13,7 @@ import yaml
from colour import Color
from map_machine.feature.direction import DirectionSet
from map_machine.map_configuration import MapConfiguration, LabelMode
from map_machine.map_configuration import MapConfiguration
from map_machine.osm.osm_reader import Tagged, Tags
from map_machine.pictogram.icon import (
DEFAULT_SHAPE_ID,
@ -24,7 +24,6 @@ from map_machine.pictogram.icon import (
ShapeSpecification,
DEFAULT_SMALL_SHAPE_ID,
)
from map_machine.text import Label, TextConstructor
__author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
@ -365,12 +364,6 @@ class Scheme:
# Storage for created icon sets.
self.cache: dict[str, tuple[IconSet, int]] = {}
self.text_constructor: TextConstructor = TextConstructor(
self.get_color("text_color"),
self.get_color("text_main_color"),
self.get_color("text_outline_color"),
)
@classmethod
def from_file(cls, file_name: Path) -> "Scheme":
"""
@ -624,25 +617,6 @@ class Scheme:
return matcher
return None
def construct_text(
self, tags: Tags, processed: set[str], label_mode: LabelMode
) -> list[Label]:
"""Construct labels for not processed tags."""
texts: list[Label] = self.text_constructor.construct_text(
tags, processed, label_mode
)
for tag in tags:
if self.is_writable(tag, tags[tag]) and tag not in processed:
texts.append(
Label(
tags[tag],
self.get_color("text_color"),
self.get_color("text_outline_color"),
)
)
return texts
def is_area(self, tags: Tags) -> bool:
"""Check whether way described by tags is area."""
for matcher in self.area_matchers:

View file

@ -8,6 +8,7 @@ from colour import Color
from map_machine.map_configuration import LabelMode
from map_machine.osm.osm_reader import Tags
from map_machine.scheme import Scheme
__author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
@ -72,10 +73,13 @@ def format_frequency(value: str) -> str:
@dataclass
class TextConstructor:
default_color: Color
main_color: Color
default_out_color: Color
def __init__(self, scheme: Scheme) -> None:
self.scheme: Scheme = scheme
self.default_color: Color = self.scheme.get_color("text_color")
self.main_color: Color = self.scheme.get_color("text_main_color")
self.default_out_color: Color = self.scheme.get_color(
"text_outline_color"
)
def label(self, text: str, size: float = DEFAULT_FONT_SIZE):
return Label(
@ -114,7 +118,10 @@ class TextConstructor:
return texts
def construct_text(
self, tags: Tags, processed: set[str], label_mode: LabelMode
self,
tags: Tags,
processed: set[str],
label_mode: LabelMode,
) -> list[Label]:
"""Construct list of labels from OSM tags."""
@ -192,4 +199,13 @@ class TextConstructor:
texts.append(self.label(f"{tags['height']} m"))
processed.add("height")
for tag in tags:
if self.scheme.is_writable(tag, tags[tag]) and tag not in processed:
texts.append(
Label(
tags[tag],
self.default_color,
self.default_out_color,
)
)
return texts

View file

@ -2,7 +2,7 @@
Test label generation for nodes.
"""
from map_machine.map_configuration import LabelMode
from map_machine.text import Label
from map_machine.text import Label, TextConstructor
from tests import SCHEME
__author__ = "Sergey Vartanov"
@ -12,7 +12,8 @@ __email__ = "me@enzet.ru"
def construct_labels(tags: dict[str, str]) -> list[Label]:
"""Construct labels from OSM node tags."""
processed: set[str] = set()
return SCHEME.construct_text(tags, processed, LabelMode.ALL)
text_constructor: TextConstructor = TextConstructor(SCHEME)
return text_constructor.construct_text(tags, processed, LabelMode.ALL)
def test_1_label() -> None: