-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraqm.py
More file actions
294 lines (240 loc) · 10.3 KB
/
raqm.py
File metadata and controls
294 lines (240 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# ******************************************************************************
# Copyright (c) 2024. All rights reserved.
# # This work is licensed under the Creative Commons Attribution 4.0
# International License. To view a copy of this license,
# visit # http://creativecommons.org/licenses/by/4.0/.
#
# Author: roximn <roximn148@gmail.com>
# ******************************************************************************
from functools import reduce
import math
from pathlib import Path
import tempfile
from subprocess import run
import cairo
from shiny import reactive
from shiny.express import module, ui, render
from fontTools import ttLib
from fontTools.pens.svgPathPen import SVGPathPen
from .utils import BBox, GlyphInfo
# ******************************************************************************
RAQM = '/home/roximn/projects/libraqm/build/src/raqm'
# ******************************************************************************
@module
def modRaqm(input, output, session):
# Font file ----------------------------------------------------------------
@reactive.calc
def ttFontFile():
files = input.raqmFile()
if files is None:
return None
if len(files) < 1:
return None
fileData: dict = files[0]
if 'datapath' not in fileData:
return None
return fileData['datapath']
@reactive.calc
def ttFont():
fontFile = ttFontFile()
if fontFile is None:
return None
try:
tt = ttLib.TTFont(fontFile)
except Exception as e:
print(f"Error reading file: {e}")
return None
return tt
# Maximum bounding box -----------------------------------------------------
@reactive.calc
def maxBBox() -> BBox | None:
tt = ttFont()
if tt is None:
return None
bbMax = BBox()
glyfTable = tt['glyf']
for glyphName in tt.getGlyphOrder():
metrics = glyfTable[glyphName]
xMin = getattr(metrics, 'xMin', 0)
yMin = getattr(metrics, 'yMin', 0)
xMax = getattr(metrics, 'xMax', 0)
yMax = getattr(metrics, 'yMax', 0)
bbMax.update(x1=xMin, y1=yMin, x2=xMax, y2=yMax)
bbMax.addMargin(100)
return bbMax
# Font File Selection ------------------------------------------------------
with ui.card(class_='bg-light border-dark'):
with ui.layout_columns(col_widths=(8, 2, 2)):
ui.input_file("raqmFile", "Choose a font file:",
accept=['.ttf', '.otf', '*.*'],
multiple=False)
ui.input_radio_buttons("textDirection", "Direction:",
{"rtl": "RTL", "ltr": "LTR"})
ui.input_select("textLanguage", "Language:",
{"urd": "Urdu", "ara": "Arabic", "fas": "Persian",
"pus": "Pashto", "snd": "Sindhi", "eng": "English"})
# Text Input ---------------------------------------------------------------
with ui.card(class_='bg-light border-dark'):
ui.input_text("textInput", "", placeholder="Enter text to render", width='100%')
@module
def textImage(input, output, session, gis: list[GlyphInfo]):
@render.express
def renderSvgText():
tt = ttFont()
if tt is None:
return
go = tt.getGlyphOrder()
glyphNames = [go[gi.gid] for gi in gis]
glyphSet = tt.getGlyphSet()
paths = []
x, y, dx = 0, 0, 50
for gn, gi in zip(glyphNames, gis):
glyph = glyphSet[gn]
pen = SVGPathPen(glyphSet)
glyph.draw(pen)
commands = pen.getCommands()
x += gi.x
y += gi.y
print(f"Rendering glyph {gi.gid} at ({x}, {y})")
paths.append(
ui.tags.Tag('path',
d=commands,
transform=f'translate({x}, {y})',
fill='blue',
stroke='none')
)
paths.append(
ui.tags.Tag('line',
x1=f'{x-dx}', y1=f'{y}',
x2=f'{x+dx}', y2=f'{y}',
stroke="red", stroke_width="10")
)
paths.append(
ui.tags.Tag('line',
x1=f'{x}', y1=f'{y-dx}',
x2=f'{x}', y2=f'{y+dx}',
stroke="red", stroke_width="10")
)
x += gi.xAdvance
y += gi.yAdvance
bbMax = maxBBox()
W = x
ui.tags.svg(
ui.tags.Tag('line',
x1=f'{0}', y1='0',
x2=f'{W}', y2='0',
stroke="red", stroke_width="5"),
ui.tags.Tag('line',
x1=f'{0}', y1=f'{bbMax.y1:.2f}',
x2=f'{0}', y2=f'{bbMax.y2:.2f}',
stroke="red", stroke_width="5"),
*paths,
width=1200,
height=400,
xmlns='http://www.w3.org/2000/svg',
viewBox= f'{bbMax.x1:.2f} {bbMax.y1:.2f} {W} {bbMax.height:.2f}',
transform='matrix(1, 0, 0, -1, 0, 0)'
)
# Cairo Text Rendering -----------------------------------------------------
@render.express
def renderText():
def showMessage(msg: str):
ui.notification_show(msg, type='message', duration=2, id='renderNotification')
def showError(msg: str):
ui.notification_show(msg, type='error', duration=10, id='errorNotification')
def drawGlyph(ctx: cairo.Context, gid, x, y, color, dx, dy, ax, ay, W, H, outline=False):
ctx.save()
ctx.translate(x, y)
glRun = [cairo.Glyph(gid, dx, dy)]
if outline:
ctx.set_source_rgba(0, 1, 0, 0.5)
ctx.arc(0, 0, 3, 0, 2 * math.pi)
ctx.stroke_preserve()
ctx.fill()
extents = ctx.glyph_extents(glRun)
# print(f'{extents.x_bearing},{extents.y_bearing} '
# f'{extents.width}:{extents.height} - '
# f'{extents.x_advance}:{extents.y_advance}')
ctx.set_source_rgba(0, 1, 1, 0.25)
ctx.set_line_width(1)
ctx.rectangle(extents.x_bearing, extents.y_bearing, extents.width, extents.height)
ctx.stroke()
ctx.set_line_width(2)
ctx.rectangle(extents.x_bearing + dx, extents.y_bearing + dy, extents.width, extents.height)
ctx.stroke()
ctx.set_line_width(1)
ctx.move_to(extents.x_bearing, extents.y_bearing)
ctx.rel_line_to(dx, dy)
ctx.stroke()
if ax:
ctx.set_line_width(1)
ctx.move_to(ax, -H)
ctx.line_to(ax, +H)
ctx.stroke()
if ay:
ctx.set_line_width(1)
ctx.move_to(-W, ay)
ctx.line_to(+W, ay)
ctx.stroke()
ctx.set_source_rgba(*color, 0.8)
ctx.show_glyphs(glRun)
ctx.restore()
ttf = ttFontFile()
tt = ttFont()
txt = input.textInput()
if ttf is None or not txt:
return
fontName = tt['name'].getName(1, 3, 1).toUnicode()
direction = input.textDirection()
lang = input.textLanguage()
defaultColor = (0, 0, 0)
WIDTH, HEIGHT = 1200, 200
SZ = 48
MARGIN = 100
EMF = SZ / tt['head'].unitsPerEm
def getGlyphInfo(tt, txt: str, direction: str, lang: str):
result = run([RAQM, tt, txt, direction, lang],
text=True, capture_output=True, check=True)
output = result.stdout.splitlines()[1:]
gi = [GlyphInfo(*[int(x) for x in line.split(' ')])for line in output]
return gi
if ttf is not None and txt:
try:
gInfos = getGlyphInfo(ttf, txt, direction, lang)
showMessage(f"{len(gInfos)} glyphs rendered.")
svgData = ''
with tempfile.NamedTemporaryFile() as fp:
with cairo.SVGSurface(fp.name, WIDTH, HEIGHT) as surface:
surface.set_document_unit(cairo.SVG_UNIT_PX)
ctx = cairo.Context(surface)
ctx.select_font_face(fontName, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
ctx.set_font_size(SZ)
# Origin
ctx.translate(WIDTH - MARGIN, HEIGHT - MARGIN)
ctx.set_source_rgb(1, 0, 0)
ctx.set_line_width(1)
ctx.move_to(-WIDTH, 0)
ctx.line_to(+WIDTH, 0)
ctx.stroke()
ctx.move_to(0, -HEIGHT)
ctx.line_to(0, +HEIGHT)
ctx.stroke()
# Cairo drawing
breath = reduce(lambda v, e: v + e.xAdvance * EMF, gInfos, 0)
curX, curY = -breath, 0
for gi in gInfos:
drawGlyph(ctx,
gi.gid, curX, curY,
defaultColor,
gi.x * EMF, -gi.y * EMF,
gi.xAdvance * EMF, -gi.yAdvance * EMF,
WIDTH, HEIGHT,
outline=True)
curX += gi.xAdvance * EMF
curY -= gi.yAdvance * EMF
svgData = Path(fp.name).read_text()
ui.HTML(svgData)
except Exception as e:
showError(f"Error: {e!s}.")
if hasattr(e, 'stdout'):
showError(f"Error: {e.stdout!s}")