-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtext_v2.py
More file actions
219 lines (169 loc) · 6.17 KB
/
text_v2.py
File metadata and controls
219 lines (169 loc) · 6.17 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
# SPDX-FileCopyrightText: 2024 BigBlueButton Inc. and by respective authors
#
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations
from typing import Optional, TypeVar
import cairo
import gi
from bbb_presentation_video.events.helpers import Position, Size
from bbb_presentation_video.renderer.tldraw.shape import (
FrameShape,
LabelledShapeProto,
StickyShapeV2,
TextShapeV2,
)
from bbb_presentation_video.renderer.tldraw.shape.text import (
create_pango_layout,
get_layout_size,
show_layout_by_lines,
)
from bbb_presentation_video.renderer.tldraw.utils import (
CANVAS,
FONT_SIZES,
STICKY_FONT_SIZES,
STICKY_PADDING,
STICKY_TEXT_COLOR,
STROKES,
AlignStyle,
ColorStyle,
FontStyle,
SizeStyle,
)
TEXT_OUTLINE_WIDTH: float = 2.0
# tldraw v2 line height multiplier (from bbb-export-annotations/shapes/TextShape.js)
V2_LINE_HEIGHT: float = 1.35
CairoSomeSurface = TypeVar("CairoSomeSurface", bound=cairo.Surface)
def finalize_v2_text(
ctx: cairo.Context[CairoSomeSurface], id: str, shape: TextShapeV2
) -> None:
print(f"\tTldraw: Finalizing Text (v2): {id}")
style = shape.style
stroke = STROKES[style.color]
font_size = FONT_SIZES[style.size]
ctx.rotate(shape.rotation)
# A group is used so the text border and fill can be drawn opaque (to avoid over-draw issues), then
# be blended with alpha afterwards
ctx.push_group()
# Use the shape's width to constrain text wrapping, matching the tldraw client
text_width = shape.size.width if shape.size.width > 0 else None
layout = create_pango_layout(ctx, style, font_size, width=text_width)
layout.set_text(shape.text, -1)
# Draw text border (outside stroke)
ctx.save()
ctx.set_source_rgb(*CANVAS)
ctx.set_line_width(TEXT_OUTLINE_WIDTH * 2)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
show_layout_by_lines(ctx, layout, padding=4, do_path=True, line_height_multiplier=V2_LINE_HEIGHT)
ctx.stroke()
ctx.restore()
# Draw text
ctx.set_source_rgb(*stroke)
show_layout_by_lines(ctx, layout, padding=4, line_height_multiplier=V2_LINE_HEIGHT)
# Composite result with opacity applied
ctx.pop_group_to_source()
ctx.paint_with_alpha(style.opacity)
def finalize_v2_label(
ctx: cairo.Context[CairoSomeSurface],
shape: LabelledShapeProto,
*,
offset: Optional[Position] = None,
) -> Size:
if shape.label is None or shape.label == "":
return Size(16, 32)
print(f"\t\tFinalizing Label (v2)")
style = shape.style
stroke = STROKES[ColorStyle.BLACK] # v2 labels are always black
font_size = FONT_SIZES[style.size]
# A group is used so the text border and fill can be drawn opaque (to avoid over-draw issues), then
# be blended with alpha afterwards
ctx.push_group()
# Create layout aligning the text horizontally within the shape
style.textAlign = shape.align
layout = create_pango_layout(
ctx, style, font_size, width=shape.size.width, padding=4
)
layout.set_text(shape.label, -1)
label_size = get_layout_size(layout, padding=4, line_height_multiplier=V2_LINE_HEIGHT)
bounds = shape.size
if offset is None:
offset = shape.label_offset()
x = offset.x
# Align text vertically in the shape
if shape.verticalAlign == AlignStyle.START:
y = offset.y
elif shape.verticalAlign == AlignStyle.END:
y = bounds.height - label_size.height + offset.y
else:
y = bounds.height / 2 - label_size.height / 2 + offset.y
ctx.translate(x, y)
# Draw text border (outside stroke)
ctx.save()
ctx.set_source_rgb(*CANVAS)
ctx.set_line_width(TEXT_OUTLINE_WIDTH * 2)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
show_layout_by_lines(ctx, layout, padding=4, do_path=True, line_height_multiplier=V2_LINE_HEIGHT)
ctx.stroke()
ctx.restore()
# Draw the original text on top
ctx.set_source_rgb(*stroke)
show_layout_by_lines(ctx, layout, padding=4, line_height_multiplier=V2_LINE_HEIGHT)
# Composite result with opacity applied
ctx.pop_group_to_source()
ctx.paint_with_alpha(style.opacity)
return label_size
def finalize_frame_name(
ctx: cairo.Context[CairoSomeSurface],
shape: FrameShape,
) -> Size:
if shape.label is None or shape.label == "":
return Size(0, 0)
print(f"\t\tFinalizing Frame name")
style = shape.style
stroke = STROKES[ColorStyle.BLACK]
font_size = 15
ctx.save()
# Create layout aligning the text to the top left
style.textAlign = AlignStyle.START
style.font = FontStyle.ARIAL
layout = create_pango_layout(
ctx,
style,
font_size,
width=shape.size.width,
padding=0,
)
layout.set_text(shape.label, -1)
label_size = get_layout_size(layout, padding=4, line_height_multiplier=V2_LINE_HEIGHT)
x = 0
y = -20
ctx.translate(x, y)
ctx.set_source_rgba(stroke.r, stroke.g, stroke.b, style.opacity)
show_layout_by_lines(ctx, layout, padding=4, line_height_multiplier=V2_LINE_HEIGHT)
ctx.restore()
return label_size
def finalize_sticky_text_v2(
ctx: cairo.Context[CairoSomeSurface], shape: StickyShapeV2
) -> None:
if shape.text is None or shape.text == "":
return
print(f"\t\tFinalizing Sticky Text (v2)")
style = shape.style
# Horizontal alignment
style.textAlign = shape.align
font_size = STICKY_FONT_SIZES[style.size]
layout = create_pango_layout(
ctx, style, font_size, width=shape.size.width, padding=STICKY_PADDING
)
layout.set_text(shape.text, -1)
# Calculate vertical position to center the text
_, text_height = get_layout_size(layout, padding=STICKY_PADDING, line_height_multiplier=V2_LINE_HEIGHT)
x, y = ctx.get_current_point()
if shape.verticalAlign is AlignStyle.MIDDLE:
y = (shape.size.height - text_height) / 2
elif shape.verticalAlign is AlignStyle.END:
y = shape.size.height - text_height
ctx.translate(x, y)
ctx.set_source_rgba(
STICKY_TEXT_COLOR.r, STICKY_TEXT_COLOR.g, STICKY_TEXT_COLOR.b, style.opacity
)
show_layout_by_lines(ctx, layout, padding=STICKY_PADDING, line_height_multiplier=V2_LINE_HEIGHT)