@@ -29,6 +29,16 @@ class Style2D:
29
29
stroke_cap = skia .Paint .kRound_Cap
30
30
stroke_join = skia .Paint .kMiter_Join
31
31
32
+ # Flag that holds whether stroke() is called user
33
+ stroke_set = False
34
+ text_font = skia .Font (skia .Typeface ())
35
+ text_leading = 0
36
+ text_size = 15
37
+ text_align = (constants .LEFT , constants .TOP )
38
+ text_wrap = None
39
+ text_baseline = constants .BASELINE
40
+ text_style = constants .NORMAL
41
+
32
42
def set_stroke_cap (self , c ):
33
43
if c == constants .ROUND :
34
44
self .stroke_cap = skia .Paint .kRound_Cap
@@ -53,9 +63,6 @@ def __init__(self):
53
63
self .style = Style2D ()
54
64
self .style_stack = []
55
65
self .path = None
56
- self .font = skia .Font ()
57
- self .typeface = skia .Typeface .MakeDefault ()
58
- self .font .setTypeface (self .typeface )
59
66
self .curve_tightness = 0
60
67
61
68
# Transforms functions
@@ -131,36 +138,63 @@ def render_text(self, text, x, y):
131
138
# full path works relative does not
132
139
# assert (typeface is not None), "should not be NULL"
133
140
# handle alignment manually
141
+ if self .style .stroke_enabled and self .style .stroke_set :
142
+ self .paint .setStyle (skia .Paint .kStroke_Style )
143
+ self .paint .setColor (skia .Color4f (* self .style .stroke_color ))
144
+ self .paint .setStrokeWidth (self .style .stroke_weight )
145
+ self .canvas .drawSimpleText (text , x , y , self .style .text_font , self .paint )
146
+
134
147
if self .style .fill_enabled :
135
148
self .paint .setStyle (skia .Paint .kFill_Style )
136
- self .paint .setColor (self .style .fill_color )
137
- self .canvas .drawPath ( self .path , self .paint )
149
+ self .paint .setColor (skia . Color4f ( * self .style .fill_color ) )
150
+ self .canvas .drawSimpleText ( text , x , y , self .style . text_font , self .paint )
138
151
139
- if self .style .stroke_enabled :
140
- self .paint .setStyle (skia .Paint .kStroke_Style )
141
- self .paint .setColor (self .style .stroke_color )
142
- self .paint .setStrokeWidth (self .style .stroke_weight )
143
- self .canvas .drawPath (self .path , self .paint )
144
- self .canvas .drawSimpleText (text , x , y , self .font , self .paint )
152
+ def text (self , text , position , max_width = None , max_height = None ):
153
+ self .render_text (text , * position )
145
154
146
155
def load_font (self , path ):
147
156
"""
148
157
path: string
149
158
Absolute path of the font file
150
159
returns: skia.Typeface
151
160
"""
152
- typeface = skia .Typeface .MakeFromFile (path )
161
+ typeface = skia .Typeface () .MakeFromFile (path = path )
153
162
return typeface
154
163
155
- def set_font (self , font ):
164
+ def text_font (self , font , size = None ):
156
165
"""
157
166
Sets the current font to be used for rendering text
158
167
"""
159
- self .typeface = font
160
- self .font .setTypeface (self .typeface )
168
+ if size :
169
+ self .style .text_size = size
170
+ if isinstance (font , str ):
171
+ self .style .text_font .setTypeface (skia .Typeface .MakeFromName (font ))
172
+ else :
173
+ self .style .text_font .setTypeface (font )
174
+
175
+ def text_size (self , size ):
176
+ self .style .text_font .setSize (size )
177
+
178
+ def text_style (self , s ):
179
+ skia_font_style = None
180
+ if s == constants .BOLD :
181
+ skia_font_style = skia .FontStyle .Bold ()
182
+ elif s == constants .BOLDITALIC :
183
+ skia_font_style = skia .FontStyle .BoldItalic ()
184
+ elif s == constants .ITALIC :
185
+ skia_font_style = skia .FontStyle .BoldItalic ()
186
+ elif s == constants .NORMAL :
187
+ skia_font_style = skia .FontStyle .Normal ()
188
+ else :
189
+ return self .style .text_style
190
+
191
+ self .style .text_style = s
192
+ # skia.Font.
193
+ family_name = self .style .text_font .getTypeface ().getFamilyName ()
194
+ typeface = skia .Typeface .MakeFromName (family_name , skia_font_style )
195
+ self .style .text_font .setTypeface (typeface )
161
196
162
- def set_font_size (self , size ):
163
- self .font .setSize (size )
197
+ return self .style .text_style
164
198
165
199
def render_image (self , img , * args ):
166
200
self .canvas .drawImage (img , * args )
@@ -213,7 +247,8 @@ def render(self, fill=True, stroke=True, rewind=True):
213
247
214
248
def reset (self ):
215
249
self .reset_matrix ()
216
- self .font .setSize (15 )
250
+ # NOTE: We have to handle PGraphics stroke_set as well separately
251
+ self .style .stroke_set = False
217
252
218
253
def line (self , path ):
219
254
x1 , y1 , x2 , y2 = path [0 ].x , path [0 ].y , path [1 ].x , path [1 ].y
0 commit comments