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,41 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/__init__.py
|
|
2
|
+
"""Small lazy public API for native FontLab reporter plugins."""
|
|
3
|
+
|
|
4
|
+
from importlib import import_module
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"Filter",
|
|
9
|
+
"General",
|
|
10
|
+
"LayerEdit",
|
|
11
|
+
"Mark",
|
|
12
|
+
"Palette",
|
|
13
|
+
"Param",
|
|
14
|
+
"Plugin",
|
|
15
|
+
"Reporter",
|
|
16
|
+
"Tool",
|
|
17
|
+
"ToolEvent",
|
|
18
|
+
"install",
|
|
19
|
+
"registry",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def __getattr__(name: str) -> Any:
|
|
24
|
+
"""Load FontLab-free definitions only when requested."""
|
|
25
|
+
if name in {"Reporter", "Mark"}:
|
|
26
|
+
return getattr(import_module(".reporter", __name__), name)
|
|
27
|
+
if name in {"Filter", "LayerEdit", "Param"}:
|
|
28
|
+
return getattr(import_module(".filter", __name__), name)
|
|
29
|
+
if name == "Plugin":
|
|
30
|
+
return import_module(".plugin", __name__).Plugin
|
|
31
|
+
if name == "Palette":
|
|
32
|
+
return import_module(".palette", __name__).Palette
|
|
33
|
+
if name in {"Tool", "ToolEvent"}:
|
|
34
|
+
return getattr(import_module(".tool", __name__), name)
|
|
35
|
+
if name == "General":
|
|
36
|
+
return import_module(".general", __name__).General
|
|
37
|
+
if name == "registry":
|
|
38
|
+
return import_module(".registry", __name__)
|
|
39
|
+
if name == "install":
|
|
40
|
+
return import_module(".boot", __name__).install
|
|
41
|
+
raise AttributeError(name)
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/backend.py
|
|
2
|
+
"""Native per-canvas painters with an EditorPainters fallback for old builds.
|
|
3
|
+
|
|
4
|
+
FontLab's text-block item applies its own transform before drawing a glyph
|
|
5
|
+
item (`fltextblockitem.cpp`). `EditorPainters` applies the glyph-item and block
|
|
6
|
+
transforms in that order. This backend does the same, then uses the native
|
|
7
|
+
`canvas.mapToViewportF` map for final viewport coordinates. The HT Letterspacer
|
|
8
|
+
consumer uses the glyph transform alone; that suffices only when the block's
|
|
9
|
+
transform is identity.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from importlib import import_module
|
|
15
|
+
from itertools import count
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from .reporter import Reporter
|
|
19
|
+
from .snapshot import NativeSource, resolve_source
|
|
20
|
+
|
|
21
|
+
POLL_MS = 250
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class _CanvasEntry:
|
|
26
|
+
"""Keep a live canvas and its native tokens together."""
|
|
27
|
+
|
|
28
|
+
identifier: int
|
|
29
|
+
canvas: Any
|
|
30
|
+
tokens: dict[int, int] = field(default_factory=dict)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class _GlyphMap:
|
|
35
|
+
"""Apply the glyph-item position, then its text-block transform."""
|
|
36
|
+
|
|
37
|
+
glyph: Any
|
|
38
|
+
block: Any
|
|
39
|
+
|
|
40
|
+
def map(self, point: Any) -> Any:
|
|
41
|
+
"""Map one layer point into canvas coordinates."""
|
|
42
|
+
return self.block.map(self.glyph.map(point))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(frozen=True)
|
|
46
|
+
class _CanvasMap:
|
|
47
|
+
"""Expose FontLab's canvas-to-viewport map as ``map``."""
|
|
48
|
+
|
|
49
|
+
canvas: Any
|
|
50
|
+
|
|
51
|
+
def map(self, point: Any) -> Any:
|
|
52
|
+
"""Map a canvas point into viewport pixels."""
|
|
53
|
+
return self.canvas.mapToViewportF(point)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class _PaintContext:
|
|
58
|
+
"""The renderer's small context for one native glyph occurrence."""
|
|
59
|
+
|
|
60
|
+
painter: Any
|
|
61
|
+
layer: NativeSource
|
|
62
|
+
glyph_to_canvas: _GlyphMap
|
|
63
|
+
canvas_to_view: _CanvasMap
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def active_glyph_map(canvas: Any, items: Any = None) -> _GlyphMap | None:
|
|
67
|
+
"""Find the active glyph occurrence, including later text-line positions."""
|
|
68
|
+
if canvas is None or canvas.isNull:
|
|
69
|
+
return None
|
|
70
|
+
items = items or import_module("fontlabapp.qtsafe").qt_list
|
|
71
|
+
for block in items(canvas.textBlocks):
|
|
72
|
+
for info in items(lambda block=block: block.getAllGlyphs() or ()):
|
|
73
|
+
if info.isActive:
|
|
74
|
+
return _GlyphMap(info.transform, block.transform)
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class NativeCanvasBackend:
|
|
79
|
+
"""Attach every enabled reporter to every active canvas encountered."""
|
|
80
|
+
|
|
81
|
+
def __init__(
|
|
82
|
+
self,
|
|
83
|
+
qt: Any,
|
|
84
|
+
fontlab: Any,
|
|
85
|
+
sink: Callable[[Exception, object], object],
|
|
86
|
+
*,
|
|
87
|
+
items: Callable[[Callable[[], Any]], list[Any]] | None = None,
|
|
88
|
+
) -> None:
|
|
89
|
+
self.qt, self.fl, self.sink = qt, fontlab, sink
|
|
90
|
+
self.items = items or import_module("fontlabapp.qtsafe").qt_list
|
|
91
|
+
self.canvases: dict[int, _CanvasEntry] = {}
|
|
92
|
+
self._failed_canvas_ids: dict[int, str] = {}
|
|
93
|
+
self._reporters: dict[int, Reporter] = {}
|
|
94
|
+
self._ids = count(1)
|
|
95
|
+
self._timer: Any = None
|
|
96
|
+
self._kept_timers: list[Any] = []
|
|
97
|
+
|
|
98
|
+
def register(self, reporter: Reporter, qt: Any, sink: Any) -> int:
|
|
99
|
+
"""Retain a reporter and attach it to all known live canvases."""
|
|
100
|
+
token = next(self._ids)
|
|
101
|
+
self._reporters[token] = reporter
|
|
102
|
+
try:
|
|
103
|
+
if self._timer is None:
|
|
104
|
+
self._start_timer()
|
|
105
|
+
except Exception:
|
|
106
|
+
self._reporters.pop(token)
|
|
107
|
+
raise
|
|
108
|
+
self._poll()
|
|
109
|
+
for entry in tuple(self.canvases.values()):
|
|
110
|
+
self._attach(entry, token)
|
|
111
|
+
return token
|
|
112
|
+
|
|
113
|
+
def remove(self, token: int) -> None:
|
|
114
|
+
"""Remove a reporter's native token from each surviving canvas."""
|
|
115
|
+
if self._reporters.pop(token, None) is None:
|
|
116
|
+
return
|
|
117
|
+
for entry in tuple(self.canvases.values()):
|
|
118
|
+
native = entry.tokens.pop(token, None)
|
|
119
|
+
if native is None:
|
|
120
|
+
continue
|
|
121
|
+
try:
|
|
122
|
+
if bool(entry.canvas.isNull):
|
|
123
|
+
continue # FontLab already dropped the painter with its canvas
|
|
124
|
+
entry.canvas.removePythonPainter(native)
|
|
125
|
+
except Exception as error: # noqa: BLE001 - stale native wrapper
|
|
126
|
+
self._report(error, self)
|
|
127
|
+
if not self._reporters:
|
|
128
|
+
self._stop_timer()
|
|
129
|
+
self.canvases.clear()
|
|
130
|
+
|
|
131
|
+
def close(self) -> None:
|
|
132
|
+
"""Drop every painter and stop polling; safe to call twice."""
|
|
133
|
+
try:
|
|
134
|
+
for token in tuple(self._reporters):
|
|
135
|
+
self.remove(token)
|
|
136
|
+
finally:
|
|
137
|
+
self._reporters.clear()
|
|
138
|
+
if self._timer is not None:
|
|
139
|
+
self._stop_timer()
|
|
140
|
+
self.canvases.clear()
|
|
141
|
+
self._failed_canvas_ids.clear()
|
|
142
|
+
|
|
143
|
+
def _start_timer(self) -> None:
|
|
144
|
+
"""Poll new active canvases while any reporter is enabled."""
|
|
145
|
+
timer = self.qt.QtCore.QTimer()
|
|
146
|
+
timer.setInterval(POLL_MS)
|
|
147
|
+
timer.timeout.connect(self._poll)
|
|
148
|
+
timer.start()
|
|
149
|
+
self._timer = timer
|
|
150
|
+
|
|
151
|
+
def _stop_timer(self) -> None:
|
|
152
|
+
"""Disconnect before deletion, retaining failed teardown wrappers."""
|
|
153
|
+
timer, self._timer = self._timer, None
|
|
154
|
+
if timer is None:
|
|
155
|
+
return
|
|
156
|
+
try:
|
|
157
|
+
timer.stop()
|
|
158
|
+
timer.timeout.disconnect(self._poll)
|
|
159
|
+
timer.deleteLater()
|
|
160
|
+
except Exception as error: # noqa: BLE001 - Qt teardown boundary
|
|
161
|
+
self._kept_timers.append(timer)
|
|
162
|
+
self._report(error, self)
|
|
163
|
+
|
|
164
|
+
def _report(self, error: Exception, source: object) -> None:
|
|
165
|
+
"""An error sink must never unwind through native paint or Qt timers."""
|
|
166
|
+
try:
|
|
167
|
+
self.sink(error, source)
|
|
168
|
+
except Exception: # noqa: BLE001, S110 - native callback boundary
|
|
169
|
+
pass
|
|
170
|
+
|
|
171
|
+
def _poll(self) -> None:
|
|
172
|
+
"""Observe active canvases and forget wrappers destroyed by FontLab."""
|
|
173
|
+
if not self._reporters:
|
|
174
|
+
return
|
|
175
|
+
try:
|
|
176
|
+
for identifier, entry in tuple(self.canvases.items()):
|
|
177
|
+
try:
|
|
178
|
+
gone = bool(entry.canvas.isNull)
|
|
179
|
+
except Exception as error: # noqa: BLE001 - stale native wrapper
|
|
180
|
+
self._report(error, self)
|
|
181
|
+
gone = True
|
|
182
|
+
if gone:
|
|
183
|
+
del self.canvases[identifier]
|
|
184
|
+
canvas = self.fl.flWorkspace.instance().getActiveCanvas()
|
|
185
|
+
if canvas is None or canvas.isNull:
|
|
186
|
+
return
|
|
187
|
+
identifier = int(canvas.id)
|
|
188
|
+
entry = self.canvases.setdefault(
|
|
189
|
+
identifier, _CanvasEntry(identifier, canvas)
|
|
190
|
+
)
|
|
191
|
+
for token in tuple(self._reporters):
|
|
192
|
+
self._attach(entry, token)
|
|
193
|
+
except Exception as error: # noqa: BLE001 - Qt timer boundary
|
|
194
|
+
self._report(error, self)
|
|
195
|
+
|
|
196
|
+
def _attach(self, entry: _CanvasEntry, token: int) -> None:
|
|
197
|
+
"""Register one callback at the reporter's requested native order."""
|
|
198
|
+
if token in entry.tokens or entry.identifier in self._failed_canvas_ids:
|
|
199
|
+
return
|
|
200
|
+
reporter = self._reporters[token]
|
|
201
|
+
callback = lambda painter, canvas: self._paint(token, reporter, painter, canvas)
|
|
202
|
+
try:
|
|
203
|
+
order = (
|
|
204
|
+
entry.canvas.UnderOwner
|
|
205
|
+
if reporter.layer == "background"
|
|
206
|
+
else entry.canvas.OverOwner
|
|
207
|
+
)
|
|
208
|
+
entry.tokens[token] = entry.canvas.appendPythonPainter(callback, order)
|
|
209
|
+
except Exception as error: # noqa: BLE001 - native registration boundary
|
|
210
|
+
self._failed_canvas_ids[entry.identifier] = str(error)
|
|
211
|
+
self._report(error, reporter)
|
|
212
|
+
|
|
213
|
+
def _paint(self, token: int, reporter: Reporter, painter: Any, canvas: Any) -> None:
|
|
214
|
+
"""Draw every active glyph occurrence in viewport coordinates; never raise."""
|
|
215
|
+
if token not in self._reporters:
|
|
216
|
+
return
|
|
217
|
+
try:
|
|
218
|
+
for block in self.items(canvas.textBlocks):
|
|
219
|
+
for info in self.items(lambda block=block: block.getAllGlyphs() or ()):
|
|
220
|
+
if not info.isActive:
|
|
221
|
+
continue
|
|
222
|
+
source = resolve_source(info)
|
|
223
|
+
if source is None:
|
|
224
|
+
continue
|
|
225
|
+
context = _PaintContext(
|
|
226
|
+
painter,
|
|
227
|
+
source,
|
|
228
|
+
_GlyphMap(info.transform, block.transform),
|
|
229
|
+
_CanvasMap(canvas),
|
|
230
|
+
)
|
|
231
|
+
reporter.paint(context, self.qt, self.sink)
|
|
232
|
+
except Exception as error: # noqa: BLE001 - native paint boundary
|
|
233
|
+
self._report(error, reporter)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
class EditorBackend:
|
|
237
|
+
"""Use App's transparent viewport overlay on builds without native hooks."""
|
|
238
|
+
|
|
239
|
+
def __init__(
|
|
240
|
+
self, painters: Any, sink: Callable[[Exception, object], object]
|
|
241
|
+
) -> None:
|
|
242
|
+
self.painters, self.sink = painters, sink
|
|
243
|
+
self._warned = False
|
|
244
|
+
|
|
245
|
+
def register(self, reporter: Reporter, qt: Any, sink: Any) -> int:
|
|
246
|
+
"""Draw after native glyphs; announce background substitution once."""
|
|
247
|
+
if reporter.layer == "background" and not self._warned:
|
|
248
|
+
self._warned = True
|
|
249
|
+
self.sink(
|
|
250
|
+
RuntimeError(
|
|
251
|
+
"Background reporters draw over the canvas on this FontLab build"
|
|
252
|
+
),
|
|
253
|
+
self,
|
|
254
|
+
)
|
|
255
|
+
return self.painters.register(
|
|
256
|
+
"after_native_layer",
|
|
257
|
+
lambda context: reporter.paint(context, qt, sink),
|
|
258
|
+
layer_filter="active",
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def remove(self, token: int) -> None:
|
|
262
|
+
"""Remove one App painter token."""
|
|
263
|
+
self.painters.remove(token)
|
|
264
|
+
|
|
265
|
+
def close(self) -> None:
|
|
266
|
+
"""Close App's viewport overlay owner."""
|
|
267
|
+
self.painters.close()
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def select_backend(
|
|
271
|
+
qt: Any,
|
|
272
|
+
fontlab: Any,
|
|
273
|
+
sink: Callable[[Exception, object], object],
|
|
274
|
+
*,
|
|
275
|
+
editor_factory: Any = None,
|
|
276
|
+
items: Callable[[Callable[[], Any]], list[Any]] | None = None,
|
|
277
|
+
) -> NativeCanvasBackend | EditorBackend:
|
|
278
|
+
"""Choose native hooks once; import EditorPainters only for older builds."""
|
|
279
|
+
if hasattr(getattr(fontlab, "flCanvas", None), "appendPythonPainter"):
|
|
280
|
+
return NativeCanvasBackend(qt, fontlab, sink, items=items)
|
|
281
|
+
factory = (
|
|
282
|
+
editor_factory or import_module("fontlabapp.editor_painters").EditorPainters
|
|
283
|
+
)
|
|
284
|
+
painters = factory(qt, fontlab, error_sink=sink, layer_resolver=resolve_source)
|
|
285
|
+
return EditorBackend(painters, sink)
|
fontlab_plugmix/boot.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/boot.py
|
|
2
|
+
"""One-call FontLab startup and explicit teardown."""
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from . import menu, registry
|
|
10
|
+
from .backend import select_backend
|
|
11
|
+
from .filters.host import FilterHost
|
|
12
|
+
from .general_plugins.host import GeneralHost
|
|
13
|
+
from .kinds import Services
|
|
14
|
+
from .palettes.host import PaletteHost
|
|
15
|
+
from .tools.host import ToolHost
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class _State:
|
|
20
|
+
"""Keep every Qt owner alive until uninstall has detached its signals."""
|
|
21
|
+
|
|
22
|
+
config_dir: Path | None
|
|
23
|
+
reporters: registry.Registry
|
|
24
|
+
painters: Any
|
|
25
|
+
menus: Any
|
|
26
|
+
filters: FilterHost
|
|
27
|
+
palettes: PaletteHost
|
|
28
|
+
tools: ToolHost
|
|
29
|
+
generals: GeneralHost
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
_state: _State | None = None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _error(error: Exception, source: object) -> None:
|
|
36
|
+
"""Log host errors without raising through native Qt callbacks."""
|
|
37
|
+
try:
|
|
38
|
+
print(f"Plugmix {type(source).__name__}: {error}")
|
|
39
|
+
except Exception: # noqa: BLE001, S110 - native callback boundary
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _runtime() -> tuple[Any, Any, Any]:
|
|
44
|
+
"""Import PythonQt, FontLab and owned menus only inside FontLab."""
|
|
45
|
+
qt = import_module("PythonQt")
|
|
46
|
+
fontlab = import_module("fontlab")
|
|
47
|
+
menus_cls = import_module("fontlabapp.owned_menus").OwnedMenus
|
|
48
|
+
return qt, fontlab, menus_cls
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def install(config_dir: str | Path | None = None) -> None:
|
|
52
|
+
"""Idempotently restore every plugin kind and its owned native hooks."""
|
|
53
|
+
global _state
|
|
54
|
+
path = None if config_dir is None else Path(config_dir)
|
|
55
|
+
if _state is not None:
|
|
56
|
+
if path != _state.config_dir:
|
|
57
|
+
raise ValueError(
|
|
58
|
+
"Plugmix is already installed with another config directory"
|
|
59
|
+
)
|
|
60
|
+
return
|
|
61
|
+
qt, fontlab, menus_cls = _runtime()
|
|
62
|
+
reporters = registry.configure(path, error_sink=_error)
|
|
63
|
+
painters = select_backend(qt, fontlab, _error)
|
|
64
|
+
menus = None
|
|
65
|
+
filters = None
|
|
66
|
+
palettes = None
|
|
67
|
+
tools = None
|
|
68
|
+
generals = None
|
|
69
|
+
try:
|
|
70
|
+
menus = menus_cls(qt, fontlab, error_sink=_error, owned_title="Plugmix")
|
|
71
|
+
reporters.attach(painters, qt, _error)
|
|
72
|
+
menu.install(menus, reporters)
|
|
73
|
+
filters = FilterHost(Services(qt, fontlab, menus, painters, _error), reporters)
|
|
74
|
+
filters.install()
|
|
75
|
+
palettes = PaletteHost(
|
|
76
|
+
Services(qt, fontlab, menus, painters, _error), reporters
|
|
77
|
+
)
|
|
78
|
+
palettes.install()
|
|
79
|
+
tools = ToolHost(Services(qt, fontlab, menus, painters, _error), reporters)
|
|
80
|
+
tools.install()
|
|
81
|
+
generals = GeneralHost(
|
|
82
|
+
Services(qt, fontlab, menus, painters, _error), reporters, palettes
|
|
83
|
+
)
|
|
84
|
+
generals.install()
|
|
85
|
+
except Exception:
|
|
86
|
+
try:
|
|
87
|
+
reporters.detach()
|
|
88
|
+
finally:
|
|
89
|
+
try:
|
|
90
|
+
try:
|
|
91
|
+
try:
|
|
92
|
+
if generals is not None:
|
|
93
|
+
generals.close()
|
|
94
|
+
finally:
|
|
95
|
+
try:
|
|
96
|
+
if tools is not None:
|
|
97
|
+
tools.close()
|
|
98
|
+
finally:
|
|
99
|
+
try:
|
|
100
|
+
if palettes is not None:
|
|
101
|
+
palettes.close()
|
|
102
|
+
finally:
|
|
103
|
+
if filters is not None:
|
|
104
|
+
filters.close()
|
|
105
|
+
finally:
|
|
106
|
+
if menus is not None:
|
|
107
|
+
menus.close()
|
|
108
|
+
finally:
|
|
109
|
+
painters.close()
|
|
110
|
+
raise
|
|
111
|
+
_state = _State(
|
|
112
|
+
path, reporters, painters, menus, filters, palettes, tools, generals
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def uninstall() -> None:
|
|
117
|
+
"""Remove actions and overlays while preserving saved enable choices."""
|
|
118
|
+
global _state
|
|
119
|
+
if _state is None:
|
|
120
|
+
return
|
|
121
|
+
state = _state
|
|
122
|
+
if state.palettes.close() is False:
|
|
123
|
+
return
|
|
124
|
+
_state = None
|
|
125
|
+
try:
|
|
126
|
+
try:
|
|
127
|
+
try:
|
|
128
|
+
try:
|
|
129
|
+
state.generals.close()
|
|
130
|
+
finally:
|
|
131
|
+
state.tools.close()
|
|
132
|
+
finally:
|
|
133
|
+
state.filters.close()
|
|
134
|
+
finally:
|
|
135
|
+
state.menus.close()
|
|
136
|
+
finally:
|
|
137
|
+
try:
|
|
138
|
+
state.reporters.detach()
|
|
139
|
+
finally:
|
|
140
|
+
state.painters.close()
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# this_file: src/fontlab_plugmix/filter.py
|
|
2
|
+
"""Pure Filter contract and declarative, validated parameter values."""
|
|
3
|
+
|
|
4
|
+
from collections.abc import Mapping
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from math import isfinite
|
|
7
|
+
from typing import Any, ClassVar, Literal
|
|
8
|
+
|
|
9
|
+
from .plugin import Plugin
|
|
10
|
+
from .snapshot import Anchor, Contour, GlyphSnapshot
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True)
|
|
14
|
+
class Param:
|
|
15
|
+
"""One typed dialog field with an optional numeric range."""
|
|
16
|
+
|
|
17
|
+
key: str
|
|
18
|
+
label: str
|
|
19
|
+
kind: Literal["float", "int", "bool", "choice"]
|
|
20
|
+
default: float | int | bool | str
|
|
21
|
+
minimum: float | None = None
|
|
22
|
+
maximum: float | None = None
|
|
23
|
+
choices: tuple[str, ...] = ()
|
|
24
|
+
|
|
25
|
+
def parse(self, value: Any) -> float | int | bool | str:
|
|
26
|
+
"""Reject wrong types and values outside the declared domain."""
|
|
27
|
+
valid = {
|
|
28
|
+
"float": (int, float),
|
|
29
|
+
"int": (int,),
|
|
30
|
+
"bool": (bool,),
|
|
31
|
+
"choice": (str,),
|
|
32
|
+
}
|
|
33
|
+
if self.kind not in valid or type(value) not in valid[self.kind]:
|
|
34
|
+
raise ValueError(f"{self.label}: expected {self.kind}")
|
|
35
|
+
if self.kind == "choice":
|
|
36
|
+
if value not in self.choices:
|
|
37
|
+
raise ValueError(f"{self.label}: unknown choice {value}")
|
|
38
|
+
return value
|
|
39
|
+
if self.kind == "bool":
|
|
40
|
+
return value
|
|
41
|
+
try:
|
|
42
|
+
number = float(value)
|
|
43
|
+
except (ValueError, OverflowError) as error:
|
|
44
|
+
raise ValueError(f"{self.label}: invalid number") from error
|
|
45
|
+
if (
|
|
46
|
+
not isfinite(number)
|
|
47
|
+
or (self.minimum is not None and number < self.minimum)
|
|
48
|
+
or (self.maximum is not None and number > self.maximum)
|
|
49
|
+
):
|
|
50
|
+
raise ValueError(f"{self.label}: outside allowed range")
|
|
51
|
+
return number if self.kind == "float" else value
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True)
|
|
55
|
+
class LayerEdit:
|
|
56
|
+
"""Replacement layer values; None leaves the corresponding value intact."""
|
|
57
|
+
|
|
58
|
+
contours: tuple[Contour, ...] | None = None
|
|
59
|
+
advance: float | None = None
|
|
60
|
+
anchors: tuple[Anchor, ...] | None = None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class Filter(Plugin):
|
|
64
|
+
"""Compute a layer edit from a frozen snapshot and typed values."""
|
|
65
|
+
|
|
66
|
+
kind = "filter"
|
|
67
|
+
params: ClassVar[tuple[Param, ...]] = ()
|
|
68
|
+
|
|
69
|
+
@classmethod
|
|
70
|
+
def validate(cls, values: Mapping[str, Any]) -> dict[str, Any]:
|
|
71
|
+
"""Fill defaults and reject unknown or invalid parameters."""
|
|
72
|
+
keys = {param.key for param in cls.params}
|
|
73
|
+
unknown = set(values) - keys
|
|
74
|
+
if unknown:
|
|
75
|
+
raise ValueError(f"Unknown {cls.name} parameters: {sorted(unknown)}")
|
|
76
|
+
return {
|
|
77
|
+
param.key: param.parse(values.get(param.key, param.default))
|
|
78
|
+
for param in cls.params
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
def compute(self, snapshot: GlyphSnapshot, values: Mapping[str, Any]) -> LayerEdit:
|
|
82
|
+
"""Return a pure proposed edit for one layer."""
|
|
83
|
+
raise NotImplementedError
|