fontlab-python-plugmix 1.1.18__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fontlab_plugmix/__init__.py +41 -0
- fontlab_plugmix/backend.py +285 -0
- fontlab_plugmix/boot.py +140 -0
- fontlab_plugmix/filter.py +83 -0
- fontlab_plugmix/filters/__init__.py +4 -0
- fontlab_plugmix/filters/apply.py +158 -0
- fontlab_plugmix/filters/delete_small_paths.py +57 -0
- fontlab_plugmix/filters/dialog.py +119 -0
- fontlab_plugmix/filters/host.py +120 -0
- fontlab_plugmix/general.py +144 -0
- fontlab_plugmix/general_plugins/__init__.py +4 -0
- fontlab_plugmix/general_plugins/host.py +137 -0
- fontlab_plugmix/general_plugins/zoom_to_selection.py +27 -0
- fontlab_plugmix/geometry.py +64 -0
- fontlab_plugmix/kinds.py +33 -0
- fontlab_plugmix/menu.py +29 -0
- fontlab_plugmix/palette.py +24 -0
- fontlab_plugmix/palettes/__init__.py +4 -0
- fontlab_plugmix/palettes/apply.py +159 -0
- fontlab_plugmix/palettes/host.py +369 -0
- fontlab_plugmix/palettes/step_and_repeat.py +53 -0
- fontlab_plugmix/palettes/ui.py +86 -0
- fontlab_plugmix/plugin.py +14 -0
- fontlab_plugmix/plugins/__init__.py +4 -0
- fontlab_plugmix/plugins/anchor_cloud.py +30 -0
- fontlab_plugmix/plugins/angled_handles.py +40 -0
- fontlab_plugmix/plugins/center_lines.py +28 -0
- fontlab_plugmix/plugins/crosshair.py +39 -0
- fontlab_plugmix/plugins/duplicate_anchors.py +50 -0
- fontlab_plugmix/plugins/glyph_focus.py +28 -0
- fontlab_plugmix/plugins/imprecise_italic_angle.py +44 -0
- fontlab_plugmix/plugins/node_indexes.py +26 -0
- fontlab_plugmix/plugins/nodes_close_to_zones.py +35 -0
- fontlab_plugmix/plugins/offcurve_outside_bounds.py +31 -0
- fontlab_plugmix/plugins/only_selected_handles.py +38 -0
- fontlab_plugmix/plugins/rsb_line.py +39 -0
- fontlab_plugmix/plugins/segment_length.py +36 -0
- fontlab_plugmix/plugins/selected_coordinates.py +27 -0
- fontlab_plugmix/plugins/smooth_node_angles.py +51 -0
- fontlab_plugmix/plugins/tops_and_bottoms.py +34 -0
- fontlab_plugmix/plugins/two_point_distance.py +38 -0
- fontlab_plugmix/plugins/unitizer.py +35 -0
- fontlab_plugmix/plugins/vertical_metrics.py +39 -0
- fontlab_plugmix/registry.py +434 -0
- fontlab_plugmix/reporter.py +127 -0
- fontlab_plugmix/snapshot.py +285 -0
- fontlab_plugmix/tool.py +104 -0
- fontlab_plugmix/tools/__init__.py +4 -0
- fontlab_plugmix/tools/bridge.py +169 -0
- fontlab_plugmix/tools/host.py +143 -0
- fontlab_plugmix/tools/smart_select.py +77 -0
- fontlab_python_plugmix-1.1.18.dist-info/METADATA +444 -0
- fontlab_python_plugmix-1.1.18.dist-info/RECORD +56 -0
- fontlab_python_plugmix-1.1.18.dist-info/WHEEL +4 -0
- fontlab_python_plugmix-1.1.18.dist-info/licenses/LICENSE +201 -0
- fontlab_python_plugmix-1.1.18.dist-info/licenses/NOTICE +228 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/snapshot.py
|
|
2
|
+
"""Copy native FontLab glyph data into immutable, Python-only records."""
|
|
3
|
+
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from importlib import import_module
|
|
7
|
+
from time import monotonic
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True)
|
|
12
|
+
class Node:
|
|
13
|
+
"""A shape-transformed node in layer font units."""
|
|
14
|
+
|
|
15
|
+
x: float
|
|
16
|
+
y: float
|
|
17
|
+
on_curve: bool
|
|
18
|
+
selected: bool
|
|
19
|
+
smooth: bool = False
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class Contour:
|
|
24
|
+
"""Nodes in native contour order."""
|
|
25
|
+
|
|
26
|
+
nodes: tuple[Node, ...]
|
|
27
|
+
closed: bool = True
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class Anchor:
|
|
32
|
+
"""A named anchor in layer font units."""
|
|
33
|
+
|
|
34
|
+
name: str
|
|
35
|
+
x: float
|
|
36
|
+
y: float
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class Zone:
|
|
41
|
+
"""One selected-master alignment zone with its outer edge."""
|
|
42
|
+
|
|
43
|
+
name: str
|
|
44
|
+
position: float
|
|
45
|
+
edge: float
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True)
|
|
49
|
+
class Metrics:
|
|
50
|
+
"""Master metrics; italic_slant is positive when the top leans right."""
|
|
51
|
+
|
|
52
|
+
ascender: float
|
|
53
|
+
descender: float
|
|
54
|
+
x_height: float
|
|
55
|
+
cap_height: float
|
|
56
|
+
italic_slant: float
|
|
57
|
+
upm: float
|
|
58
|
+
zones: tuple[Zone, ...] = ()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dataclass(frozen=True)
|
|
62
|
+
class GlyphSnapshot:
|
|
63
|
+
"""Read-only data needed by reporter computations."""
|
|
64
|
+
|
|
65
|
+
contours: tuple[Contour, ...]
|
|
66
|
+
anchors: tuple[Anchor, ...]
|
|
67
|
+
advance: float
|
|
68
|
+
metrics: Metrics
|
|
69
|
+
bounds: tuple[float, float, float, float] | None = None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True)
|
|
73
|
+
class NativeSource:
|
|
74
|
+
"""Short-lived handles resolved from one editor glyph item."""
|
|
75
|
+
|
|
76
|
+
package: Any
|
|
77
|
+
layer: Any
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass(frozen=True)
|
|
81
|
+
class ActiveGlyph:
|
|
82
|
+
"""Checked current glyph and its selected-master layer for palette actions."""
|
|
83
|
+
|
|
84
|
+
package: Any
|
|
85
|
+
glyph: Any
|
|
86
|
+
layer: Any
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def active_glyph(
|
|
90
|
+
fontlab: Any,
|
|
91
|
+
*,
|
|
92
|
+
items: Callable[[Callable[[], Any]], list[Any]] | None = None,
|
|
93
|
+
) -> ActiveGlyph | None:
|
|
94
|
+
"""Resolve the active canvas slot against the workspace's current package."""
|
|
95
|
+
workspace = fontlab.flWorkspace.instance()
|
|
96
|
+
package = workspace.currentPackage
|
|
97
|
+
if package is None or package.isNull or workspace.activeFontPanel is not None:
|
|
98
|
+
return None
|
|
99
|
+
canvas = workspace.getActiveCanvas()
|
|
100
|
+
if canvas is None or canvas.isNull:
|
|
101
|
+
return None
|
|
102
|
+
if items is None:
|
|
103
|
+
items = import_module("fontlabapp.qtsafe").qt_list
|
|
104
|
+
for block in items(canvas.textBlocks):
|
|
105
|
+
for index in range(int(block.glyphsCount())):
|
|
106
|
+
info = block.getGlyphAt(index)
|
|
107
|
+
if info is None or not info.isActive:
|
|
108
|
+
continue
|
|
109
|
+
glyph = info.glyph
|
|
110
|
+
if (
|
|
111
|
+
glyph is None
|
|
112
|
+
or glyph.isNull
|
|
113
|
+
or int(glyph.package.fgId) != int(package.fgId)
|
|
114
|
+
):
|
|
115
|
+
return None
|
|
116
|
+
name = str(info.symbol.layerName)
|
|
117
|
+
layer = glyph.findLayer(name or package.master)
|
|
118
|
+
return (
|
|
119
|
+
None
|
|
120
|
+
if layer is None or layer.isNull
|
|
121
|
+
else ActiveGlyph(package, glyph, layer)
|
|
122
|
+
)
|
|
123
|
+
return None
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def palette_key(
|
|
127
|
+
active: ActiveGlyph | None,
|
|
128
|
+
*,
|
|
129
|
+
items: Callable[[Callable[[], Any]], list[Any]] | None = None,
|
|
130
|
+
) -> tuple[Any, ...] | None:
|
|
131
|
+
"""Read a cheap revision and node-selection key without a full snapshot."""
|
|
132
|
+
if active is None:
|
|
133
|
+
return None
|
|
134
|
+
if items is None:
|
|
135
|
+
items = import_module("fontlabapp.qtsafe").qt_list
|
|
136
|
+
stamp = active.glyph.lastModified
|
|
137
|
+
revision = (
|
|
138
|
+
stamp.toMSecsSinceEpoch() if hasattr(stamp, "toMSecsSinceEpoch") else stamp
|
|
139
|
+
)
|
|
140
|
+
# This checks every native node each tick; keep capture itself behind the key.
|
|
141
|
+
selected = []
|
|
142
|
+
for shape in items(lambda: active.layer.shapes):
|
|
143
|
+
if shape.isComponent:
|
|
144
|
+
continue
|
|
145
|
+
for contour in items(lambda shape=shape: shape.contours):
|
|
146
|
+
selected.append(tuple(bool(node.selected) for node in items(contour.nodes)))
|
|
147
|
+
return (
|
|
148
|
+
int(active.package.fgId),
|
|
149
|
+
int(active.glyph.id),
|
|
150
|
+
str(active.package.master),
|
|
151
|
+
revision,
|
|
152
|
+
tuple(selected),
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def selected_layers() -> Any:
|
|
157
|
+
"""Read the active Font/Glyph window selection through App's checked adapter."""
|
|
158
|
+
return import_module("fontlabapp.layer_selection").capture(monotonic() + 2)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def select_contours(
|
|
162
|
+
layer: Any,
|
|
163
|
+
indices: tuple[int, ...],
|
|
164
|
+
*,
|
|
165
|
+
toggle: bool = False,
|
|
166
|
+
items: Callable[[Callable[[], Any]], list[Any]] | None = None,
|
|
167
|
+
) -> None:
|
|
168
|
+
"""Select whole editable contours by index; never change outline geometry."""
|
|
169
|
+
items = items or import_module("fontlabapp.qtsafe").qt_list
|
|
170
|
+
contours = [
|
|
171
|
+
contour
|
|
172
|
+
for shape in items(lambda: layer.shapes)
|
|
173
|
+
if not shape.isComponent
|
|
174
|
+
for contour in items(lambda shape=shape: shape.contours)
|
|
175
|
+
]
|
|
176
|
+
targets = set(indices)
|
|
177
|
+
for index, contour in enumerate(contours):
|
|
178
|
+
nodes = items(contour.nodes)
|
|
179
|
+
if toggle:
|
|
180
|
+
if index in targets:
|
|
181
|
+
selected = not all(bool(node.selected) for node in nodes)
|
|
182
|
+
for node in nodes:
|
|
183
|
+
node.selected = selected
|
|
184
|
+
else:
|
|
185
|
+
for node in nodes:
|
|
186
|
+
node.selected = index in targets
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def resolve_source(info: Any) -> NativeSource | None:
|
|
190
|
+
"""Use the same glyph → current-master layer path as anchor rings."""
|
|
191
|
+
glyph = info.glyph
|
|
192
|
+
if glyph is None or glyph.isNull:
|
|
193
|
+
return None
|
|
194
|
+
package = glyph.package
|
|
195
|
+
layer = glyph.findLayer(package.master)
|
|
196
|
+
if layer is None or layer.isNull:
|
|
197
|
+
return None
|
|
198
|
+
return NativeSource(package, layer)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _contours(
|
|
202
|
+
layer: Any, items: Callable[[Callable[[], Any]], list[Any]]
|
|
203
|
+
) -> tuple[Contour, ...]:
|
|
204
|
+
"""Read editable outlines and apply each shape's native transform."""
|
|
205
|
+
rows = []
|
|
206
|
+
for shape in items(lambda: layer.shapes):
|
|
207
|
+
if shape.isComponent:
|
|
208
|
+
continue
|
|
209
|
+
for contour in items(lambda shape=shape: shape.contours):
|
|
210
|
+
nodes = []
|
|
211
|
+
for node in items(contour.nodes):
|
|
212
|
+
point = shape.transform.map(node.position)
|
|
213
|
+
nodes.append(
|
|
214
|
+
Node(
|
|
215
|
+
float(point.x()),
|
|
216
|
+
float(point.y()),
|
|
217
|
+
bool(node.isOn()),
|
|
218
|
+
bool(node.selected),
|
|
219
|
+
bool(getattr(node, "smooth", False)),
|
|
220
|
+
)
|
|
221
|
+
)
|
|
222
|
+
rows.append(Contour(tuple(nodes), bool(getattr(contour, "closed", True))))
|
|
223
|
+
return tuple(rows)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _zones(
|
|
227
|
+
package: Any, items: Callable[[Callable[[], Any]], list[Any]]
|
|
228
|
+
) -> tuple[Zone, ...]:
|
|
229
|
+
"""Read the selected master's native PostScript top and bottom zones."""
|
|
230
|
+
zones = []
|
|
231
|
+
for field, direction in (("ps_zonesT", 1), ("ps_zonesB", -1)):
|
|
232
|
+
for item in items(lambda field=field: getattr(package, field, ())):
|
|
233
|
+
position = float(item.position)
|
|
234
|
+
zones.append(
|
|
235
|
+
Zone(str(item.name), position, position + direction * float(item.width))
|
|
236
|
+
)
|
|
237
|
+
return tuple(zones)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def _bounds(layer: Any) -> tuple[float, float, float, float] | None:
|
|
241
|
+
"""Copy the native ink box, including curve extrema and components."""
|
|
242
|
+
box = getattr(layer, "boundingBox", None)
|
|
243
|
+
if box is None or (box.width() <= 0 and box.height() <= 0):
|
|
244
|
+
return None
|
|
245
|
+
x, y = float(box.x()), float(box.y())
|
|
246
|
+
return x, y, x + float(box.width()), y + float(box.height())
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def capture(
|
|
250
|
+
source: NativeSource,
|
|
251
|
+
*,
|
|
252
|
+
fontlab: Any = None,
|
|
253
|
+
items: Callable[[Callable[[], Any]], list[Any]] | None = None,
|
|
254
|
+
include_zones: bool = False,
|
|
255
|
+
include_bounds: bool = False,
|
|
256
|
+
) -> GlyphSnapshot:
|
|
257
|
+
"""Read one current native layer; inject native adapters in offline tests."""
|
|
258
|
+
if fontlab is None:
|
|
259
|
+
fontlab = import_module("fontlab")
|
|
260
|
+
if items is None:
|
|
261
|
+
items = import_module("fontlabapp.qtsafe").qt_list
|
|
262
|
+
layer, package = source.layer, source.package
|
|
263
|
+
metrics = fontlab.FontMetrics(package, package.master)
|
|
264
|
+
pins = items(lambda: layer.anchors)
|
|
265
|
+
anchors = tuple(
|
|
266
|
+
Anchor(str(pin.name), float(pin.point.x()), float(pin.point.y()))
|
|
267
|
+
for pin in pins
|
|
268
|
+
)
|
|
269
|
+
values = Metrics(
|
|
270
|
+
float(metrics.ascender),
|
|
271
|
+
float(metrics.descender),
|
|
272
|
+
float(metrics.xHeight),
|
|
273
|
+
float(metrics.capsHeight),
|
|
274
|
+
# OpenType post uses counter-clockwise degrees from vertical.
|
|
275
|
+
-float(package.italicAngle_value),
|
|
276
|
+
float(package.upm),
|
|
277
|
+
_zones(package, items) if include_zones else (),
|
|
278
|
+
)
|
|
279
|
+
return GlyphSnapshot(
|
|
280
|
+
_contours(layer, items),
|
|
281
|
+
anchors,
|
|
282
|
+
float(layer.advanceWidth),
|
|
283
|
+
values,
|
|
284
|
+
_bounds(layer) if include_bounds else None,
|
|
285
|
+
)
|
fontlab_plugmix/tool.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/tool.py
|
|
2
|
+
"""FontLab-native tool contract and a snapshot-backed active context."""
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from typing import Any, ClassVar
|
|
7
|
+
|
|
8
|
+
from . import snapshot
|
|
9
|
+
from .plugin import Plugin
|
|
10
|
+
from .reporter import Mark
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class ToolEvent:
|
|
15
|
+
"""A copied native event with glyph and viewport coordinates."""
|
|
16
|
+
|
|
17
|
+
x: float
|
|
18
|
+
y: float
|
|
19
|
+
view_x: float
|
|
20
|
+
view_y: float
|
|
21
|
+
buttons: int = 0
|
|
22
|
+
shift: bool = False
|
|
23
|
+
alt: bool = False
|
|
24
|
+
ctrl: bool = False
|
|
25
|
+
meta: bool = False
|
|
26
|
+
key: str | None = None
|
|
27
|
+
clicks: int = 1
|
|
28
|
+
scale: float = 1.0
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ToolContext:
|
|
32
|
+
"""Offer checked active glyph reads, selection and undoable edits."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, services: Any) -> None:
|
|
35
|
+
self.services = services
|
|
36
|
+
|
|
37
|
+
def snapshot(self) -> snapshot.GlyphSnapshot | None:
|
|
38
|
+
"""Capture through the workspace path, never CurrentGlyph()."""
|
|
39
|
+
active = snapshot.active_glyph(self.services.fontlab)
|
|
40
|
+
if active is None:
|
|
41
|
+
return None
|
|
42
|
+
return snapshot.capture(
|
|
43
|
+
snapshot.NativeSource(active.package, active.layer),
|
|
44
|
+
fontlab=self.services.fontlab,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def select(self, indices: tuple[int, ...], *, toggle: bool = False) -> None:
|
|
48
|
+
"""Change selection flags only; no outline geometry or history write."""
|
|
49
|
+
active = snapshot.active_glyph(self.services.fontlab)
|
|
50
|
+
if active is not None:
|
|
51
|
+
snapshot.select_contours(active.layer, indices, toggle=toggle)
|
|
52
|
+
self.redraw()
|
|
53
|
+
|
|
54
|
+
def edit(self, plan_fn: Any, title: str) -> Any:
|
|
55
|
+
"""Commit a caller-provided `(work, facts, editor)` through App's writer."""
|
|
56
|
+
work, facts, editor = plan_fn(self.snapshot())
|
|
57
|
+
return import_module("fontlabapp.layer_writer").apply_work(
|
|
58
|
+
work, title, facts, editor
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def redraw(self) -> None:
|
|
62
|
+
"""Ask FontLab to repaint the active Glyph canvas."""
|
|
63
|
+
self.services.fontlab.UpdateActiveCanvas()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Tool(Plugin):
|
|
67
|
+
"""One native tool; event handlers return True only when they handle input."""
|
|
68
|
+
|
|
69
|
+
kind: ClassVar[str] = "tool"
|
|
70
|
+
cursor: ClassVar[str | None] = None
|
|
71
|
+
|
|
72
|
+
def activate(self, ctx: ToolContext) -> None:
|
|
73
|
+
"""Receive an active context after FontLab switches to this tool."""
|
|
74
|
+
|
|
75
|
+
def deactivate(self) -> None:
|
|
76
|
+
"""Release per-activation state."""
|
|
77
|
+
|
|
78
|
+
def press(self, event: ToolEvent) -> bool:
|
|
79
|
+
"""Handle mouse press."""
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
def move(self, event: ToolEvent) -> bool:
|
|
83
|
+
"""Handle mouse move."""
|
|
84
|
+
return False
|
|
85
|
+
|
|
86
|
+
def release(self, event: ToolEvent) -> bool:
|
|
87
|
+
"""Handle mouse release."""
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
def double_click(self, event: ToolEvent) -> bool:
|
|
91
|
+
"""Handle a double click."""
|
|
92
|
+
return False
|
|
93
|
+
|
|
94
|
+
def key_down(self, event: ToolEvent) -> bool:
|
|
95
|
+
"""Observe key press without taking FontLab shortcuts."""
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
def key_up(self, event: ToolEvent) -> bool:
|
|
99
|
+
"""Observe key release without taking FontLab shortcuts."""
|
|
100
|
+
return False
|
|
101
|
+
|
|
102
|
+
def marks(self, glyph: snapshot.GlyphSnapshot | None) -> list[Mark]:
|
|
103
|
+
"""Return overlay marks only while active."""
|
|
104
|
+
return []
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/tools/bridge.py
|
|
2
|
+
"""Guard FontLab Python tool callbacks and map viewport events to glyph units."""
|
|
3
|
+
|
|
4
|
+
from importlib import import_module
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from fontlab_plugmix import snapshot
|
|
8
|
+
from fontlab_plugmix.backend import active_glyph_map
|
|
9
|
+
from fontlab_plugmix.reporter import Reporter
|
|
10
|
+
from fontlab_plugmix.tool import ToolContext, ToolEvent
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class _ToolPainter(Reporter):
|
|
14
|
+
"""Use the Reporter renderer only while this tool is active."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, tool: Any) -> None:
|
|
17
|
+
self.tool = tool
|
|
18
|
+
|
|
19
|
+
def compute(self, glyph: snapshot.GlyphSnapshot) -> list[Any]:
|
|
20
|
+
"""Delegate pure mark computation to the active tool."""
|
|
21
|
+
return self.tool.marks(glyph)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _glyph_point(canvas: Any, view: Any) -> tuple[float, float]:
|
|
25
|
+
"""Invert backend's glyph/block map after FontLab removes pan and zoom."""
|
|
26
|
+
affine = import_module("fontlabapp.affine")
|
|
27
|
+
transform = active_glyph_map(canvas)
|
|
28
|
+
if transform is None:
|
|
29
|
+
raise ValueError("No active glyph on this canvas")
|
|
30
|
+
t = affine.concat(
|
|
31
|
+
affine.from_qtransform(transform.glyph),
|
|
32
|
+
affine.from_qtransform(transform.block),
|
|
33
|
+
)
|
|
34
|
+
point = canvas.mapFromViewport(view)
|
|
35
|
+
return affine.apply(affine.invert(t), float(point.x()), float(point.y()))
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ToolBridge:
|
|
39
|
+
"""Strongly own one plugin, its overlay adapter and native callbacks."""
|
|
40
|
+
|
|
41
|
+
def __init__(self, tool: Any, services: Any) -> None:
|
|
42
|
+
self.tool, self.services = tool, services
|
|
43
|
+
self.context = ToolContext(services)
|
|
44
|
+
self.painter = _ToolPainter(tool)
|
|
45
|
+
self.paint_token: int | None = None
|
|
46
|
+
self.active = False
|
|
47
|
+
self._last: tuple[float, float] | None = None
|
|
48
|
+
self._reported: set[tuple[str, str]] = set()
|
|
49
|
+
|
|
50
|
+
def _error(self, name: str, error: Exception) -> None:
|
|
51
|
+
"""Report each callback failure once, including a failing sink."""
|
|
52
|
+
key = name, f"{type(error).__name__}: {error}"
|
|
53
|
+
if key in self._reported:
|
|
54
|
+
return
|
|
55
|
+
self._reported.add(key)
|
|
56
|
+
try:
|
|
57
|
+
self.services.sink(error, self.tool)
|
|
58
|
+
except Exception: # noqa: BLE001, S110 - native callback boundary
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
def _call(self, name: str, action: Any) -> bool:
|
|
62
|
+
"""Return False for any native or plugin failure."""
|
|
63
|
+
try:
|
|
64
|
+
return bool(action())
|
|
65
|
+
except Exception as error: # noqa: BLE001 - native callback boundary
|
|
66
|
+
self._error(name, error)
|
|
67
|
+
return False
|
|
68
|
+
|
|
69
|
+
def activate(self) -> bool:
|
|
70
|
+
"""Register the overlay, then activate the tool; unwind on failure."""
|
|
71
|
+
|
|
72
|
+
def action() -> bool:
|
|
73
|
+
if self.active:
|
|
74
|
+
return True
|
|
75
|
+
self.paint_token = self.services.painters.register(
|
|
76
|
+
self.painter, self.services.qt, self.services.sink
|
|
77
|
+
)
|
|
78
|
+
try:
|
|
79
|
+
self.tool.activate(self.context)
|
|
80
|
+
except Exception:
|
|
81
|
+
self.services.painters.remove(self.paint_token)
|
|
82
|
+
self.paint_token = None
|
|
83
|
+
raise
|
|
84
|
+
self.active = True
|
|
85
|
+
self._last = None
|
|
86
|
+
return True
|
|
87
|
+
|
|
88
|
+
return self._call("activate", action)
|
|
89
|
+
|
|
90
|
+
def deactivate(self) -> bool:
|
|
91
|
+
"""Remove the painter even when plugin deactivation fails."""
|
|
92
|
+
|
|
93
|
+
def action() -> bool:
|
|
94
|
+
if not self.active and self.paint_token is None:
|
|
95
|
+
return True
|
|
96
|
+
self.active = False
|
|
97
|
+
self._last = None
|
|
98
|
+
try:
|
|
99
|
+
self.tool.deactivate()
|
|
100
|
+
finally:
|
|
101
|
+
token, self.paint_token = self.paint_token, None
|
|
102
|
+
if token is not None:
|
|
103
|
+
self.services.painters.remove(token)
|
|
104
|
+
return True
|
|
105
|
+
|
|
106
|
+
return self._call("deactivate", action)
|
|
107
|
+
|
|
108
|
+
def _event(
|
|
109
|
+
self, event: Any, canvas: Any, *, clicks: int = 1, key: bool = False
|
|
110
|
+
) -> ToolEvent:
|
|
111
|
+
"""Copy Qt event fields before the native callback invalidates them."""
|
|
112
|
+
qt = self.services.qt.QtCore.Qt
|
|
113
|
+
if key and self._last is None:
|
|
114
|
+
point = canvas.viewPort.mapFromGlobal(self.services.qt.QtGui.QCursor.pos())
|
|
115
|
+
elif key:
|
|
116
|
+
point = self.services.qt.QtCore.QPointF(*self._last)
|
|
117
|
+
else:
|
|
118
|
+
point = event.localPos()
|
|
119
|
+
view_x, view_y = float(point.x()), float(point.y())
|
|
120
|
+
x, y = _glyph_point(canvas, point)
|
|
121
|
+
if not key:
|
|
122
|
+
self._last = view_x, view_y
|
|
123
|
+
flags = int(event.modifiers())
|
|
124
|
+
return ToolEvent(
|
|
125
|
+
x,
|
|
126
|
+
y,
|
|
127
|
+
view_x,
|
|
128
|
+
view_y,
|
|
129
|
+
0 if key else int(event.buttons()),
|
|
130
|
+
bool(flags & int(qt.ShiftModifier)),
|
|
131
|
+
bool(flags & int(qt.AltModifier)),
|
|
132
|
+
bool(flags & int(qt.ControlModifier)),
|
|
133
|
+
bool(flags & int(qt.MetaModifier)),
|
|
134
|
+
str(event.text()) if key else None,
|
|
135
|
+
clicks,
|
|
136
|
+
float(canvas.scale()),
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
def _dispatch(
|
|
140
|
+
self, name: str, event: Any, canvas: Any, *, clicks: int = 1, key: bool = False
|
|
141
|
+
) -> bool:
|
|
142
|
+
"""Call one handler only while active; keys never swallow shortcuts."""
|
|
143
|
+
if not self.active:
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
def action() -> bool:
|
|
147
|
+
mapped = self._event(event, canvas, clicks=clicks, key=key)
|
|
148
|
+
handled = bool(getattr(self.tool, name)(mapped))
|
|
149
|
+
return False if key else handled
|
|
150
|
+
|
|
151
|
+
return self._call(name, action)
|
|
152
|
+
|
|
153
|
+
def mousePressed(self, event: Any, canvas: Any) -> bool:
|
|
154
|
+
return self._dispatch("press", event, canvas)
|
|
155
|
+
|
|
156
|
+
def mouseMoved(self, event: Any, canvas: Any) -> bool:
|
|
157
|
+
return self._dispatch("move", event, canvas)
|
|
158
|
+
|
|
159
|
+
def mouseReleased(self, event: Any, canvas: Any) -> bool:
|
|
160
|
+
return self._dispatch("release", event, canvas)
|
|
161
|
+
|
|
162
|
+
def mouseDoubleClicked(self, event: Any, canvas: Any) -> bool:
|
|
163
|
+
return self._dispatch("double_click", event, canvas, clicks=2)
|
|
164
|
+
|
|
165
|
+
def keyPressed(self, event: Any, canvas: Any) -> bool:
|
|
166
|
+
return self._dispatch("key_down", event, canvas, key=True)
|
|
167
|
+
|
|
168
|
+
def keyReleased(self, event: Any, canvas: Any) -> bool:
|
|
169
|
+
return self._dispatch("key_up", event, canvas, key=True)
|