Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Scripta/scripta/renderer.py at main · rajatnagda45/Scripta · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
rajatnagda45
Scripta
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
Scripta
/
scripta
/
renderer.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
153 lines (124 loc) · 4.95 KB
main
Breadcrumbs
Scripta
/
scripta
/
renderer.py
Copy path
Top
File metadata and controls
Code
Blame
153 lines (124 loc) · 4.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""
Renderer — composites glyph images onto a canvas with WriterState perturbations.
Font-rendered glyphs come in as RGBA images. We apply:
- Recolor (white → transparent, black → ink color)
- Scale, rotate, slant (from GlyphParams)
- Alpha (opacity from fatigue)
- Paste onto canvas at position + baseline drift
"""
import
math
from
typing
import
Optional
,
Tuple
import
numpy
as
np
from
PIL
import
Image
,
ImageFilter
import
config
from
scripta
.
debug_utils
import
debug_log
from
scripta
.
glyph_store
import
GlyphStore
from
scripta
.
variation_engine
import
WriterState
,
GlyphParams
def
_recolor
(
img
:
Image
.
Image
,
ink_rgb
:
Tuple
[
int
,
int
,
int
])
->
Image
.
Image
:
"""Replace glyph color with ink_rgb, preserving the source alpha channel."""
img
=
img
.
convert
(
"RGBA"
)
_
,
_
,
_
,
a
=
img
.
split
()
a_arr
=
np
.
array
(
a
,
dtype
=
np
.
uint8
)
out
=
np
.
zeros
((
img
.
height
,
img
.
width
,
4
),
dtype
=
np
.
uint8
)
out
[:, :,
0
]
=
ink_rgb
[
0
]
out
[:, :,
1
]
=
ink_rgb
[
1
]
out
[:, :,
2
]
=
ink_rgb
[
2
]
out
[:, :,
3
]
=
a_arr
return
Image
.
fromarray
(
out
,
"RGBA"
)
def
_apply_params
(
img
:
Image
.
Image
,
params
:
GlyphParams
)
->
Image
.
Image
:
"""Apply scale, rotation, and slant shear to the glyph."""
w
,
h
=
img
.
size
# Scale
new_w
=
max
(
1
,
int
(
w
*
params
.
scale
))
new_h
=
max
(
1
,
int
(
h
*
params
.
scale
))
img
=
img
.
resize
((
new_w
,
new_h
),
Image
.
LANCZOS
)
# Rotation
if
abs
(
params
.
rotate_deg
)
>
0.2
:
img
=
img
.
rotate
(
params
.
rotate_deg
,
resample
=
Image
.
BICUBIC
,
expand
=
True
,
fillcolor
=
(
0
,
0
,
0
,
0
),
)
# Slant (horizontal shear)
if
abs
(
params
.
slant
)
>
0.8
:
shear
=
math
.
tan
(
math
.
radians
(
params
.
slant
*
0.4
))
# dampen slant for fonts
w2
,
h2
=
img
.
size
extra_w
=
int
(
abs
(
shear
)
*
h2
)
offset
=
-
shear
*
h2
/
2
if
shear
>
0
else
-
shear
*
h2
/
2
data
=
(
1
,
shear
,
offset
,
0
,
1
,
0
)
img
=
img
.
transform
(
(
w2
+
extra_w
,
h2
),
Image
.
AFFINE
,
data
,
resample
=
Image
.
BICUBIC
,
fillcolor
=
(
0
,
0
,
0
,
0
),
)
return
img
def
_apply_alpha
(
img
:
Image
.
Image
,
alpha
:
float
)
->
Image
.
Image
:
if
abs
(
alpha
-
1.0
)
<
0.01
:
return
img
r
,
g
,
b
,
a
=
img
.
split
()
a
=
a
.
point
(
lambda
v
:
int
(
v
*
alpha
))
return
Image
.
merge
(
"RGBA"
, [
r
,
g
,
b
,
a
])
def
_add_stroke_variation
(
img
:
Image
.
Image
,
tremor
:
float
)
->
Image
.
Image
:
"""Subtle edge roughness — simulates pen nib/ball inconsistency."""
if
tremor
<
0.05
:
return
img
# Very slight blur on high tremor (pen wobble softens edges)
radius
=
tremor
*
0.6
return
img
.
filter
(
ImageFilter
.
GaussianBlur
(
radius
=
radius
))
class
Renderer
:
def
__init__
(
self
,
glyph_store
:
GlyphStore
,
writer_state
:
WriterState
,
target_height
:
int
=
config
.
GLYPH_TARGET_HEIGHT
,
word_spacing
:
int
=
config
.
WORD_SPACING
,
char_spacing
:
int
=
config
.
CHAR_SPACING
,
):
self
.
store
=
glyph_store
self
.
state
=
writer_state
self
.
target_height
=
target_height
self
.
word_spacing
=
word_spacing
self
.
char_spacing
=
char_spacing
self
.
ink_rgb
=
config
.
INK_COLORS
[
writer_state
.
ink_color
]
self
.
_rng
=
np
.
random
.
default_rng
()
def
render_word
(
self
,
canvas
:
Image
.
Image
,
word
:
str
,
x
:
int
,
y
:
int
,
)
->
int
:
"""
Render `word` onto `canvas` at baseline position (x, y).
Returns the x coordinate after the word (ready for next word).
"""
if
self
.
state
.
chars_written
<
80
:
debug_log
(
"renderer"
,
f"render_word word='
{
word
}
' at x=
{
x
}
, y=
{
y
}
"
)
self
.
state
.
on_word
(
word
)
params
=
self
.
state
.
next_word_params
(
word
)
# Get word image from store (font-rendered)
glyph
=
self
.
store
.
get_word_image
(
word
,
self
.
state
.
writer_id
,
self
.
_rng
)
if
glyph
is
None
:
return
x
+
self
.
word_width_estimate
(
word
)
# Recolor to ink
glyph
=
_recolor
(
glyph
,
self
.
ink_rgb
)
# Apply variation transforms
glyph
=
_apply_params
(
glyph
,
params
)
glyph
=
_add_stroke_variation
(
glyph
,
params
.
tremor
)
glyph
=
_apply_alpha
(
glyph
,
params
.
alpha
)
# Position: x is left edge, y is baseline — glyph hangs above
paste_x
=
int
(
x
+
params
.
offset_x
)
paste_y
=
int
(
y
-
glyph
.
height
+
params
.
offset_y
+
params
.
baseline_y
)
# Keep within canvas bounds
paste_x
=
max
(
0
,
min
(
paste_x
,
canvas
.
width
-
1
))
paste_y
=
max
(
0
,
min
(
paste_y
,
canvas
.
height
-
glyph
.
height
))
canvas
.
paste
(
glyph
, (
paste_x
,
paste_y
),
glyph
)
advance
=
paste_x
+
glyph
.
width
+
int
(
self
.
word_spacing
*
params
.
spacing_factor
)
return
advance
def
word_width_estimate
(
self
,
word
:
str
)
->
int
:
"""Fast width estimate for line-wrap pre-computation."""
# Font-based: ~0.55× height per character is a good estimate for Caveat
return
int
(
len
(
word
)
*
self
.
target_height
*
0.52
+
self
.
word_spacing
)
You can’t perform that action at this time.