ocr-foundry 0.2.0__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.
@@ -0,0 +1,200 @@
1
+ """Fixed 5x5-vertex piecewise-affine elastic mesh for page contract v4.
2
+
3
+ The mesh covers the pre-mesh canvas ``[0,W] x [0,H]`` (pixel-edge float64
4
+ coordinates) with 4x4 cells, every cell split along its top-left to
5
+ bottom-right diagonal into two triangles (32 total). Boundary vertices are
6
+ fixed; the nine interior vertices carry the displacement. Forward and inverse
7
+ maps are exact per triangle, so no approximate geometry is introduced.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from dataclasses import dataclass
13
+
14
+ import numpy as np
15
+
16
+ from .errors import InputValidationError
17
+
18
+ GRID = 5 # vertices per axis
19
+ CELLS = GRID - 1
20
+ MAX_AMPLITUDE_FRACTION = 0.1 # of the smaller cell dimension
21
+ MIN_AREA_RATIO = 0.25 # displaced / source triangle area
22
+ INTERIOR = [(j * GRID + i) for j in range(1, CELLS) for i in range(1, CELLS)]
23
+ _BOUNDS_TOLERANCE = 1e-6
24
+
25
+
26
+ def triangle_indices() -> np.ndarray:
27
+ """(32, 3) vertex indices, ``vertex = row * GRID + col``, positively oriented."""
28
+ triangles = []
29
+ for j in range(CELLS):
30
+ for i in range(CELLS):
31
+ v00 = j * GRID + i
32
+ v10, v01 = v00 + 1, v00 + GRID
33
+ triangles.append((v00, v10, v01 + 1)) # lx >= ly
34
+ triangles.append((v00, v01 + 1, v01)) # lx <= ly
35
+ return np.asarray(triangles, dtype=np.int64)
36
+
37
+
38
+ def source_vertices(width: float, height: float) -> np.ndarray:
39
+ xs = np.arange(GRID, dtype=np.float64) * (width / CELLS)
40
+ ys = np.arange(GRID, dtype=np.float64) * (height / CELLS)
41
+ return np.stack(np.meshgrid(xs, ys), axis=-1).reshape(-1, 2)
42
+
43
+
44
+ def max_amplitude(width: float, height: float) -> float:
45
+ return MAX_AMPLITUDE_FRACTION * min(width, height) / CELLS
46
+
47
+
48
+ def _signed_areas(vertices: np.ndarray, triangles: np.ndarray) -> np.ndarray:
49
+ a, b, c = (vertices[triangles[:, k]] for k in range(3))
50
+ ab, ac = b - a, c - a
51
+ return 0.5 * (ab[:, 0] * ac[:, 1] - ab[:, 1] * ac[:, 0])
52
+
53
+
54
+ def _affines(frm: np.ndarray, to: np.ndarray, triangles: np.ndarray) -> np.ndarray:
55
+ """(32, 3, 2) matrices A with ``[x, y, 1] @ A`` mapping frm triangles onto to."""
56
+ return np.stack([np.linalg.solve(np.c_[frm[t], np.ones(3)], to[t]) for t in triangles])
57
+
58
+
59
+ @dataclass(frozen=True)
60
+ class Mesh:
61
+ """Validated source/destination vertex sets plus exact per-triangle maps."""
62
+
63
+ source: np.ndarray
64
+ destination: np.ndarray
65
+ triangles: np.ndarray
66
+ width: float
67
+ height: float
68
+ forward_affines: np.ndarray
69
+ inverse_affines: np.ndarray
70
+ inverse_barycentric: np.ndarray
71
+
72
+ def locate(self, points: np.ndarray) -> np.ndarray:
73
+ """Source-triangle index of each point; shared edges resolve consistently."""
74
+ fx = points[:, 0] / (self.width / CELLS)
75
+ fy = points[:, 1] / (self.height / CELLS)
76
+ i = np.clip(np.floor(fx), 0, CELLS - 1).astype(np.int64)
77
+ j = np.clip(np.floor(fy), 0, CELLS - 1).astype(np.int64)
78
+ lower = (fx - i) >= (fy - j)
79
+ return 2 * (j * CELLS + i) + np.where(lower, 0, 1)
80
+
81
+ def forward(self, points: np.ndarray) -> np.ndarray:
82
+ """Map pre-mesh points to destination; points must lie on the canvas."""
83
+ points = np.asarray(points, dtype=np.float64)
84
+ low, high = (
85
+ -_BOUNDS_TOLERANCE,
86
+ (self.width + _BOUNDS_TOLERANCE, self.height + _BOUNDS_TOLERANCE),
87
+ )
88
+ if (points < low).any() or (points > high).any():
89
+ raise InputValidationError("annotation geometry lies outside the elastic mesh canvas")
90
+ tri = self.locate(points)
91
+ homogeneous = np.c_[points, np.ones(len(points))]
92
+ return np.einsum("nk,nkc->nc", homogeneous, self.forward_affines[tri])
93
+
94
+ def inverse(self, points: np.ndarray) -> np.ndarray:
95
+ """Map destination points to pre-mesh points; NaN where no triangle contains them.
96
+
97
+ Points on shared edges belong to several triangles that agree exactly;
98
+ the lowest triangle index wins deterministically.
99
+ """
100
+ points = np.asarray(points, dtype=np.float64)
101
+ result = np.full((len(points), 2), np.nan, dtype=np.float64)
102
+ pending = np.ones(len(points), dtype=bool)
103
+ for t, tri in enumerate(self.triangles):
104
+ corners = self.destination[tri]
105
+ low, high = corners.min(axis=0) - 1e-9, corners.max(axis=0) + 1e-9
106
+ candidate = (
107
+ pending
108
+ & (points[:, 0] >= low[0])
109
+ & (points[:, 0] <= high[0])
110
+ & (points[:, 1] >= low[1])
111
+ & (points[:, 1] <= high[1])
112
+ )
113
+ index = np.flatnonzero(candidate)
114
+ if index.size == 0:
115
+ continue
116
+ homogeneous = np.c_[points[index], np.ones(index.size)]
117
+ inside = (homogeneous @ self.inverse_barycentric[t]).min(axis=1) >= -1e-9
118
+ hit = index[inside]
119
+ result[hit] = homogeneous[inside] @ self.inverse_affines[t]
120
+ pending[hit] = False
121
+ return result
122
+
123
+ def breaks(self, a: np.ndarray, b: np.ndarray) -> list[float]:
124
+ """Parameters in (0,1) where segment a->b crosses a source mesh edge."""
125
+ cw, ch = self.width / CELLS, self.height / CELLS
126
+ ax, ay = float(a[0]) / cw, float(a[1]) / ch
127
+ bx, by = float(b[0]) / cw, float(b[1]) / ch
128
+ ts: list[float] = []
129
+ for g0, g1, levels in (
130
+ (ax, bx, range(GRID)),
131
+ (ay, by, range(GRID)),
132
+ (ax - ay, bx - by, range(-CELLS, GRID)),
133
+ ):
134
+ if abs(g1 - g0) <= 1e-12:
135
+ continue
136
+ for k in levels:
137
+ t = (k - g0) / (g1 - g0)
138
+ if 1e-12 < t < 1.0 - 1e-12:
139
+ ts.append(t)
140
+ ts.sort()
141
+ unique: list[float] = []
142
+ for t in ts:
143
+ if not unique or t - unique[-1] > 1e-12:
144
+ unique.append(t)
145
+ return unique
146
+
147
+ def polyline(self, points: np.ndarray, closed: bool) -> np.ndarray:
148
+ """Exact image of a pre-mesh polygon/polyline: split at every mesh edge, map all."""
149
+ points = np.asarray(points, dtype=np.float64)
150
+ minimum = 3 if closed else 2
151
+ if points.ndim != 2 or points.shape[1] != 2 or len(points) < minimum:
152
+ kind = "closed polygon" if closed else "open polyline"
153
+ raise InputValidationError(f"elastic mesh {kind} requires at least {minimum} 2D points")
154
+ count = len(points)
155
+ pieces: list[np.ndarray] = []
156
+ for index in range(count if closed else count - 1):
157
+ a, b = points[index], points[(index + 1) % count]
158
+ pieces.append(a)
159
+ pieces.extend(a + t * (b - a) for t in self.breaks(a, b))
160
+ if not closed:
161
+ pieces.append(points[-1])
162
+ return self.forward(np.asarray(pieces, dtype=np.float64))
163
+
164
+
165
+ def make_mesh(width: float, height: float, destination: np.ndarray | None = None) -> Mesh:
166
+ """Build and validate a mesh; ``destination`` defaults to the undisplaced grid."""
167
+ source = source_vertices(width, height)
168
+ destination = source.copy() if destination is None else np.asarray(destination, np.float64)
169
+ triangles = triangle_indices()
170
+ if destination.shape != source.shape or not np.isfinite(destination).all():
171
+ raise InputValidationError("elastic mesh vertices must be finite")
172
+ boundary = np.ones(len(source), dtype=bool)
173
+ boundary[INTERIOR] = False
174
+ if not np.array_equal(destination[boundary], source[boundary]):
175
+ raise InputValidationError("elastic mesh boundary vertices must remain fixed")
176
+ ratio = _signed_areas(destination, triangles) / _signed_areas(source, triangles)
177
+ if not (ratio >= MIN_AREA_RATIO).all():
178
+ raise InputValidationError(
179
+ "elastic mesh draw folds or collapses a triangle "
180
+ f"(minimum area ratio {ratio.min():.3f} < {MIN_AREA_RATIO})"
181
+ )
182
+ return Mesh(
183
+ source,
184
+ destination,
185
+ triangles,
186
+ float(width),
187
+ float(height),
188
+ _affines(source, destination, triangles),
189
+ _affines(destination, source, triangles),
190
+ np.stack([np.linalg.inv(np.c_[destination[t], np.ones(3)]) for t in triangles]),
191
+ )
192
+
193
+
194
+ def displaced_mesh(width: float, height: float, amplitude: float, unit_draws: np.ndarray) -> Mesh:
195
+ """Mesh whose interior vertices move by ``amplitude * unit_draws`` (18 values in [-1,1])."""
196
+ if len(unit_draws) != 2 * len(INTERIOR):
197
+ raise InputValidationError("elastic mesh needs one x and y draw per interior vertex")
198
+ destination = source_vertices(width, height)
199
+ destination[INTERIOR] += amplitude * np.asarray(unit_draws, np.float64).reshape(-1, 2)
200
+ return make_mesh(width, height, destination)
@@ -0,0 +1,264 @@
1
+ """Opt-in layout rasterization from RAQM glyph positions and logical clusters.
2
+
3
+ The shared RAQM library shapes one paragraph, including font ranges, before any
4
+ rasterization. FreeType draws those exact glyph indices; words never get shaped
5
+ in isolation. UTF-32 input makes cluster indices Python Unicode offsets.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import ctypes as ct
10
+ import ctypes.util
11
+ import math
12
+ import random
13
+ import re
14
+ from functools import lru_cache
15
+ from itertools import pairwise
16
+ from typing import TYPE_CHECKING, Any
17
+
18
+ import cv2
19
+ import numpy as np
20
+ from PIL import features
21
+
22
+ from .errors import InputValidationError
23
+ from .layout_transform import _size
24
+ from .text_support import find_fallback_fonts_for_clusters, split_grapheme_clusters
25
+
26
+ if TYPE_CHECKING:
27
+ from .rendering import ImageRenderer
28
+
29
+
30
+ # Public ABI prefixes from freetype.h/ftimage.h. FT_Long/FT_Pos/FT_Fixed are
31
+ # C long (including Windows LLP64); enum and FT_Int are C int. Only trailing,
32
+ # unread fields are omitted. Faces and glyph slots are owned by the linked FT.
33
+ class _FTBitmap(ct.Structure):
34
+ _fields_ = [("rows", ct.c_uint), ("width", ct.c_uint), ("pitch", ct.c_int),
35
+ ("buffer", ct.POINTER(ct.c_ubyte)), ("num_grays", ct.c_ushort),
36
+ ("pixel_mode", ct.c_ubyte), ("palette_mode", ct.c_ubyte), ("palette", ct.c_void_p)]
37
+
38
+
39
+ class _FTSlot(ct.Structure):
40
+ _fields_ = [("library", ct.c_void_p), ("face", ct.c_void_p), ("next", ct.c_void_p),
41
+ ("glyph_index", ct.c_uint), ("generic", ct.c_void_p * 2),
42
+ ("metrics", ct.c_long * 8), ("linearHoriAdvance", ct.c_long),
43
+ ("linearVertAdvance", ct.c_long), ("advance", ct.c_long * 2),
44
+ ("format", ct.c_uint), ("bitmap", _FTBitmap),
45
+ ("bitmap_left", ct.c_int), ("bitmap_top", ct.c_int)]
46
+
47
+
48
+ class _FTFace(ct.Structure):
49
+ _fields_ = [("num_faces", ct.c_long), ("face_index", ct.c_long), ("face_flags", ct.c_long),
50
+ ("style_flags", ct.c_long), ("num_glyphs", ct.c_long),
51
+ ("family_name", ct.c_char_p), ("style_name", ct.c_char_p),
52
+ ("num_fixed_sizes", ct.c_int), ("available_sizes", ct.c_void_p),
53
+ ("num_charmaps", ct.c_int), ("charmaps", ct.c_void_p),
54
+ ("generic", ct.c_void_p * 2), ("bbox", ct.c_long * 4),
55
+ ("units_per_EM", ct.c_ushort), ("ascender", ct.c_short), ("descender", ct.c_short),
56
+ ("height", ct.c_short), ("max_advance_width", ct.c_short),
57
+ ("max_advance_height", ct.c_short), ("underline_position", ct.c_short),
58
+ ("underline_thickness", ct.c_short), ("glyph", ct.POINTER(_FTSlot))]
59
+
60
+
61
+ class _Glyph(ct.Structure):
62
+ _fields_ = [("index", ct.c_uint), ("x_advance", ct.c_int), ("y_advance", ct.c_int),
63
+ ("x_offset", ct.c_int), ("y_offset", ct.c_int), ("cluster", ct.c_uint32),
64
+ ("ftface", ct.c_void_p)]
65
+
66
+
67
+ @lru_cache(maxsize=1)
68
+ def _raqm() -> Any:
69
+ path = ctypes.util.find_library("raqm")
70
+ if not path or not features.check_feature("raqm"):
71
+ raise InputValidationError(
72
+ "word annotations and vertical writing require a shared libraqm and RAQM-enabled Pillow; "
73
+ "install your OS libraqm package (for example lib will be named libraqm.so) and RAQM-enabled Pillow"
74
+ )
75
+ try:
76
+ lib = ct.CDLL(path)
77
+ signatures = {
78
+ "raqm_create": ([], ct.c_void_p),
79
+ "raqm_destroy": ([ct.c_void_p], None),
80
+ "raqm_set_text": ([ct.c_void_p, ct.POINTER(ct.c_uint32), ct.c_size_t], ct.c_bool),
81
+ "raqm_set_par_direction": ([ct.c_void_p, ct.c_int], ct.c_bool),
82
+ "raqm_set_language": ([ct.c_void_p, ct.c_char_p, ct.c_size_t, ct.c_size_t], ct.c_bool),
83
+ "raqm_set_freetype_face_range": ([ct.c_void_p, ct.c_void_p, ct.c_size_t, ct.c_size_t], ct.c_bool),
84
+ "raqm_layout": ([ct.c_void_p], ct.c_bool),
85
+ # Faces must belong to RAQM's linked FreeType, not a wheel's bundled
86
+ # FreeType: mixing their HarfBuzz callbacks corrupts glyph metrics.
87
+ "FT_Init_FreeType": ([ct.POINTER(ct.c_void_p)], ct.c_int),
88
+ "FT_New_Face": ([ct.c_void_p, ct.c_char_p, ct.c_long, ct.POINTER(ct.POINTER(_FTFace))], ct.c_int),
89
+ "FT_Set_Pixel_Sizes": ([ct.c_void_p, ct.c_uint, ct.c_uint], ct.c_int),
90
+ "FT_Load_Glyph": ([ct.c_void_p, ct.c_uint, ct.c_int32], ct.c_int),
91
+ "FT_Done_Face": ([ct.c_void_p], ct.c_int),
92
+ "FT_Done_FreeType": ([ct.c_void_p], ct.c_int),
93
+ "raqm_get_glyphs": ([ct.c_void_p, ct.POINTER(ct.c_size_t)], ct.POINTER(_Glyph)),
94
+ }
95
+ for name, (args, result) in signatures.items():
96
+ function = getattr(lib, name)
97
+ function.argtypes, function.restype = args, result
98
+ return lib
99
+ except (OSError, AttributeError) as exc:
100
+ raise InputValidationError(f"cannot load shared RAQM glyph API: {exc}") from exc
101
+
102
+
103
+ def require_layout_shaper() -> None:
104
+ _raqm()
105
+
106
+
107
+ def _polygon(bounds: tuple[float, float, float, float]) -> list[list[float]]:
108
+ x0, y0, x1, y1 = bounds
109
+ return [[x0, y0], [x1, y0], [x1, y1], [x0, y1]]
110
+
111
+
112
+ def render_positioned(renderer: ImageRenderer, text: str, writing_mode: str,
113
+ include_words: bool, retry_limit: int) -> tuple[np.ndarray, int, dict[str, Any]] | None:
114
+ """Shape and draw once, returning word geometry in the returned canvas space."""
115
+ lib = _raqm()
116
+ vertical = writing_mode != "horizontal-tb"
117
+ target = renderer._sample_target_height()
118
+ cfg = renderer._cfg
119
+ if cfg.cluster_font_fallback:
120
+ paths = sorted({entry[0] for entry in renderer.font_manager._pool_for(text)})
121
+ random.shuffle(paths)
122
+ fonts = [renderer.font_manager.get_font_by_path_and_size(p, target) for p in paths]
123
+ fonts = [font for font in fonts if font is not None]
124
+ if not fonts:
125
+ return None
126
+ runs = find_fallback_fonts_for_clusters(split_grapheme_clusters(text), fonts[0], fonts[1:])
127
+ if not runs:
128
+ return None
129
+ else:
130
+ font = renderer._select_font(text, target, retry_limit)
131
+ if font is None:
132
+ return None
133
+ runs = [(text, font)]
134
+ ft_library = ct.c_void_p()
135
+ if lib.FT_Init_FreeType(ct.byref(ft_library)):
136
+ raise InputValidationError("RAQM's linked FreeType could not initialize")
137
+ faces = {}
138
+ font_sizes = {}
139
+ rq = lib.raqm_create()
140
+ if not rq:
141
+ lib.FT_Done_FreeType(ft_library)
142
+ raise InputValidationError("RAQM could not allocate a paragraph")
143
+ try:
144
+ source = (ct.c_uint32 * len(text))(*(ord(ch) for ch in text))
145
+ if not lib.raqm_set_text(rq, source, len(text)):
146
+ raise InputValidationError("RAQM rejected Unicode paragraph")
147
+ direction = 3 if vertical else {"auto": 0, "rtl": 1, "ltr": 2}[cfg.direction]
148
+ if not lib.raqm_set_par_direction(rq, direction):
149
+ raise InputValidationError("RAQM rejected writing direction")
150
+ if cfg.shaping_language and not lib.raqm_set_language(rq, cfg.shaping_language.encode(), 0, len(text)):
151
+ raise InputValidationError("RAQM rejected shaping language")
152
+ offset = 0
153
+ for run, font in runs:
154
+ key = (str(font.path), font.size, getattr(font, "index", 0))
155
+ face = ct.POINTER(_FTFace)()
156
+ if lib.FT_New_Face(ft_library, key[0].encode(), key[2], ct.byref(face)):
157
+ raise InputValidationError(f"FreeType cannot open font {key[0]}")
158
+ address = ct.cast(face, ct.c_void_p).value
159
+ faces[address] = face
160
+ font_sizes[address] = key[1]
161
+ if lib.FT_Set_Pixel_Sizes(face, 0, key[1]):
162
+ raise InputValidationError("FreeType rejected font size")
163
+ if not lib.raqm_set_freetype_face_range(rq, face, offset, len(run)):
164
+ raise InputValidationError("RAQM rejected a font range")
165
+ offset += len(run)
166
+ if not lib.raqm_layout(rq):
167
+ raise InputValidationError("RAQM could not shape the paragraph")
168
+ length = ct.c_size_t()
169
+ glyphs = lib.raqm_get_glyphs(rq, ct.byref(length))
170
+ tokens = list(re.finditer(r"\S+", text)) if include_words else []
171
+ word_bounds: list[list[tuple[int, int, int, int]]] = [[] for _ in tokens]
172
+ clusters = sorted({glyphs[i].cluster for i in range(length.value)} | {len(text)})
173
+ cluster_ends = dict(pairwise(clusters))
174
+ pieces = []
175
+ pen_x = pen_y = 0
176
+ style = renderer._sample_decorations(text)
177
+ if style.effect is not None:
178
+ raise InputValidationError("positioned layout does not support text effects; disable text_effect")
179
+ for i in range(length.value):
180
+ glyph = glyphs[i]
181
+ face = faces[glyph.ftface]
182
+ if lib.FT_Load_Glyph(face, glyph.index, 4): # FT_LOAD_RENDER, normal grayscale target
183
+ raise InputValidationError("FreeType could not rasterize a positioned glyph")
184
+ slot = face.contents.glyph.contents
185
+ bitmap = slot.bitmap
186
+ x = round((pen_x + glyph.x_offset) / 64) + slot.bitmap_left
187
+ y = -round((pen_y + glyph.y_offset) / 64) - slot.bitmap_top
188
+ pen_x += glyph.x_advance
189
+ pen_y += glyph.y_advance
190
+ if not bitmap.width or not bitmap.rows:
191
+ continue
192
+ raw = np.ctypeslib.as_array(bitmap.buffer, shape=(bitmap.rows * abs(bitmap.pitch),))
193
+ alpha = raw.reshape(bitmap.rows, abs(bitmap.pitch))
194
+ if bitmap.pixel_mode == 1: # FT_PIXEL_MODE_MONO
195
+ alpha = np.unpackbits(alpha, axis=1)[:, :bitmap.width] * 255
196
+ elif bitmap.pixel_mode == 2: # FT_PIXEL_MODE_GRAY
197
+ alpha = alpha[:, :bitmap.width].copy()
198
+ else:
199
+ raise InputValidationError(f"unsupported FreeType bitmap pixel mode {bitmap.pixel_mode}")
200
+ if bitmap.pitch < 0:
201
+ alpha = alpha[::-1].copy()
202
+ if glyph.cluster in style.sub_indices or glyph.cluster in style.super_indices:
203
+ alpha = cv2.resize(alpha, None, fx=0.7, fy=0.7, interpolation=cv2.INTER_AREA)
204
+ y += round(font_sizes[glyph.ftface] * (0.2 if glyph.cluster in style.sub_indices else -0.3))
205
+ if style.bold:
206
+ alpha = cv2.dilate(np.pad(alpha, ((1, 1), (1, 1))), np.ones((3, 3), np.uint8))
207
+ x -= 1
208
+ y -= 1
209
+ if style.italic:
210
+ shift = math.ceil(alpha.shape[0] * 0.2)
211
+ alpha = cv2.warpAffine(alpha, np.array([[1, -0.2, shift], [0, 1, 0]], dtype=np.float32),
212
+ (alpha.shape[1] + shift, alpha.shape[0]))
213
+ bounds = (x, y, x + alpha.shape[1], y + alpha.shape[0])
214
+ pieces.append((x, y, alpha))
215
+ for j, token in enumerate(tokens):
216
+ if glyph.cluster < token.end() and cluster_ends.get(glyph.cluster, len(text)) > token.start():
217
+ word_bounds[j].append(bounds)
218
+ if not pieces:
219
+ return None
220
+ x0 = min(x for x, _, _ in pieces) - 10
221
+ y0 = min(y for _, y, _ in pieces) - 10
222
+ x1 = max(x + a.shape[1] for x, _, a in pieces) + 10
223
+ y1 = max(y + a.shape[0] for _, y, a in pieces) + 10
224
+ _size(x1-x0, y1-y0)
225
+ mask = np.zeros((y1-y0, x1-x0), dtype=np.uint8)
226
+ for x, y, alpha in pieces:
227
+ region = mask[y-y0:y-y0+alpha.shape[0], x-x0:x-x0+alpha.shape[1]]
228
+ np.maximum(region, alpha, out=region)
229
+ if style.underline:
230
+ if vertical:
231
+ mask[10:-10, -7:-5] = 255
232
+ else:
233
+ mask[-7:-5, 10:-10] = 255
234
+ scale = target / (mask.shape[1] if vertical else mask.shape[0])
235
+ width, height = max(1, round(mask.shape[1]*scale)), max(1, round(mask.shape[0]*scale))
236
+ if not vertical and cfg.image_width is not None:
237
+ width = min(width, cfg.image_width)
238
+ # The same rounded resize factors map every glyph/word and the raster.
239
+ sx, sy = width / mask.shape[1], height / mask.shape[0]
240
+ mask = cv2.resize(mask, (width, height), interpolation=cv2.INTER_AREA)
241
+ pad_left = 0
242
+ if not vertical and cfg.image_width is not None and cfg.image_width > width:
243
+ total_padding = cfg.image_width - width
244
+ pad_left = random.randint(0, total_padding) if renderer.random_align_when_padded else total_padding // 2
245
+ mask = np.pad(mask, ((0, 0), (pad_left, total_padding-pad_left)))
246
+ color = renderer._sample_deco_color(255) if style.color else (0, 0, 0)
247
+ if renderer.color_mode == 3:
248
+ image = np.rint(255 - mask[..., None].astype(float) * (1-np.asarray(color)/255)).astype(np.uint8)
249
+ else:
250
+ image = 255 - mask
251
+ words = []
252
+ for token, bounds in zip(tokens, word_bounds, strict=True):
253
+ if not bounds:
254
+ raise InputValidationError(f"word at Unicode offset {token.start()} has no rendered glyph geometry")
255
+ box = ((min(b[0] for b in bounds)-x0)*sx + pad_left, (min(b[1] for b in bounds)-y0)*sy,
256
+ (max(b[2] for b in bounds)-x0)*sx + pad_left, (max(b[3] for b in bounds)-y0)*sy)
257
+ words.append({"text": token.group(), "start": token.start(), "end": token.end(), "polygon": _polygon(box)})
258
+ return image, target, {"font": str(runs[0][1].path), "font_size": runs[0][1].size,
259
+ "fonts": list(dict.fromkeys(str(f.path) for _, f in runs)), "words": words}
260
+ finally:
261
+ lib.raqm_destroy(rq)
262
+ for face in faces.values():
263
+ lib.FT_Done_Face(face)
264
+ lib.FT_Done_FreeType(ft_library)