6
6
from fontTools .misc .transform import Transform , Identity
7
7
from fontTools .misc .arrayTools import unionRect
8
8
from fontTools .ttLib import TTFont
9
- from fontTools .ttLib .tables .otTables import CompositeMode , PaintFormat , VariableValue
10
- from fontTools .ttLib .tables .otConverters import VarF2Dot14 , VarFixed
9
+ from fontTools .ttLib .tables .otTables import CompositeMode , PaintFormat
11
10
from fontTools .varLib .varStore import VarStoreInstancer
12
11
import uharfbuzz as hb
13
12
@@ -46,9 +45,13 @@ def __init__(self, path, *, fontNumber=0, lazy=True):
46
45
colrTable = colrTable .table
47
46
self .colrV1Glyphs = {
48
47
glyph .BaseGlyph : glyph
49
- for glyph in colrTable .BaseGlyphV1List . BaseGlyphV1Record
48
+ for glyph in colrTable .BaseGlyphList . BaseGlyphPaintRecord
50
49
}
51
- self .colrLayersV1 = colrTable .LayerV1List
50
+ if colrTable .ClipList is None :
51
+ self .clipBoxes = None
52
+ else :
53
+ self .clipBoxes = colrTable .ClipList .clips
54
+ self .colrLayersV1 = colrTable .LayerList
52
55
if colrTable .VarStore is not None :
53
56
self .instancer = VarStoreInstancer (
54
57
colrTable .VarStore , self .ttFont ["fvar" ].axes
@@ -98,9 +101,13 @@ def colrV1GlyphNames(self):
98
101
return self .colrV1Glyphs .keys ()
99
102
100
103
def getGlyphBounds (self , glyphName ):
101
- if glyphName in self .colrV1Glyphs or glyphName not in self . colrV0Glyphs :
104
+ if glyphName in self .colrV1Glyphs :
102
105
bounds = self ._getGlyphBounds (glyphName )
103
- else :
106
+ if self .clipBoxes is not None :
107
+ box = self .clipBoxes .get (glyphName )
108
+ if box is not None :
109
+ bounds = box .xMin , box .yMin , box .xMax , box .yMax
110
+ elif glyphName in self .colrV0Glyphs :
104
111
# For COLRv0, we take the union of all layer bounds
105
112
bounds = None
106
113
for layer in self .colrV0Glyphs [glyphName ]:
@@ -109,6 +116,8 @@ def getGlyphBounds(self, glyphName):
109
116
bounds = layerBounds
110
117
else :
111
118
bounds = unionRect (layerBounds , bounds )
119
+ else :
120
+ bounds = self ._getGlyphBounds (glyphName )
112
121
return bounds
113
122
114
123
def drawGlyph (self , glyphName , canvas , * , palette = None , textColor = (0 , 0 , 0 , 1 )):
@@ -171,7 +180,7 @@ def _drawPaintColrLayers(self, paint, canvas):
171
180
self ._drawPaint (self .colrLayersV1 .Paint [i ], canvas )
172
181
173
182
def _drawPaintSolid (self , paint , canvas ):
174
- color = self ._getColor (paint .Color . PaletteIndex , paint . Color .Alpha )
183
+ color = self ._getColor (paint .PaletteIndex , paint .Alpha )
175
184
canvas .drawPathSolid (self .currentPath , color )
176
185
177
186
def _drawPaintLinearGradient (self , paint , canvas ):
@@ -247,13 +256,25 @@ def _drawPaintTranslate(self, paint, canvas):
247
256
self ._applyTransform (transform , paint .Paint , canvas )
248
257
249
258
def _drawPaintRotate (self , paint , canvas ):
259
+ transform = Transform ()
260
+ transform = transform .rotate (math .radians (paint .angle ))
261
+ self ._applyTransform (transform , paint .Paint , canvas )
262
+
263
+ def _drawPaintRotateAroundCenter (self , paint , canvas ):
250
264
transform = Transform ()
251
265
transform = transform .translate (paint .centerX , paint .centerY )
252
266
transform = transform .rotate (math .radians (paint .angle ))
253
267
transform = transform .translate (- paint .centerX , - paint .centerY )
254
268
self ._applyTransform (transform , paint .Paint , canvas )
255
269
256
270
def _drawPaintSkew (self , paint , canvas ):
271
+ transform = Transform ()
272
+ transform = transform .skew (
273
+ math .radians (paint .xSkewAngle ), math .radians (paint .ySkewAngle )
274
+ )
275
+ self ._applyTransform (transform , paint .Paint , canvas )
276
+
277
+ def _drawPaintSkewAroundCenter (self , paint , canvas ):
257
278
transform = Transform ()
258
279
transform = transform .translate (paint .centerX , paint .centerY )
259
280
transform = transform .skew (
@@ -263,10 +284,26 @@ def _drawPaintSkew(self, paint, canvas):
263
284
self ._applyTransform (transform , paint .Paint , canvas )
264
285
265
286
def _drawPaintScale (self , paint , canvas ):
266
- # https://github.com/googlefonts/colr-gradients-spec/issues/279
287
+ transform = Transform ()
288
+ transform = transform .scale (paint .scaleX , paint .scaleY )
289
+ self ._applyTransform (transform , paint .Paint , canvas )
290
+
291
+ def _drawPaintScaleAroundCenter (self , paint , canvas ):
267
292
transform = Transform ()
268
293
transform = transform .translate (paint .centerX , paint .centerY )
269
- transform = transform .scale (paint .xScale , paint .yScale )
294
+ transform = transform .scale (paint .scaleX , paint .scaleY )
295
+ transform = transform .translate (- paint .centerX , - paint .centerY )
296
+ self ._applyTransform (transform , paint .Paint , canvas )
297
+
298
+ def _drawPaintScaleUniform (self , paint , canvas ):
299
+ transform = Transform ()
300
+ transform = transform .scale (paint .scale , paint .scale )
301
+ self ._applyTransform (transform , paint .Paint , canvas )
302
+
303
+ def _drawPaintScaleUniformAroundCenter (self , paint , canvas ):
304
+ transform = Transform ()
305
+ transform = transform .translate (paint .centerX , paint .centerY )
306
+ transform = transform .scale (paint .scale , paint .scale )
270
307
transform = transform .translate (- paint .centerX , - paint .centerY )
271
308
self ._applyTransform (transform , paint .Paint , canvas )
272
309
@@ -373,7 +410,7 @@ def _getColor(self, colorIndex, alpha):
373
410
def _readColorLine (self , colorLineTable ):
374
411
return _normalizeColorLine (
375
412
[
376
- (cs .StopOffset , self ._getColor (cs .Color . PaletteIndex , cs . Color .Alpha ))
413
+ (cs .StopOffset , self ._getColor (cs .PaletteIndex , cs .Alpha ))
377
414
for cs in colorLineTable .ColorStop
378
415
]
379
416
)
@@ -442,10 +479,10 @@ def axisValuesToLocation(normalizedAxisValues, axisTags):
442
479
}
443
480
444
481
445
- _conversionFactors = {
446
- VarF2Dot14 : 1 / (1 << 14 ),
447
- VarFixed : 1 / (1 << 16 ),
448
- }
482
+ # _conversionFactors = {
483
+ # VarF2Dot14: 1 / (1 << 14),
484
+ # VarFixed: 1 / (1 << 16),
485
+ # }
449
486
450
487
451
488
class PaintVarWrapper :
@@ -459,16 +496,17 @@ def __repr__(self):
459
496
460
497
def __getattr__ (self , attrName ):
461
498
value = getattr (self ._wrappedPaint , attrName )
462
- if isinstance (value , VariableValue ):
463
- if value .varIdx != 0xFFFFFFFF :
464
- factor = _conversionFactors .get (
465
- type (self ._wrappedPaint .getConverterByName (attrName )), 1
466
- )
467
- value = value .value + self ._instancer [value .varIdx ] * factor
468
- else :
469
- value = value .value
470
- elif type (value ).__name__ .startswith ("Var" ):
471
- value = PaintVarWrapper (value , self ._instancer )
472
- elif isinstance (value , (list , UserList )):
473
- value = [PaintVarWrapper (item , self ._instancer ) for item in value ]
499
+ raise NotImplementedError ("This code is currently not working" )
500
+ # if isinstance(value, VariableValue):
501
+ # if value.varIdx != 0xFFFFFFFF:
502
+ # factor = _conversionFactors.get(
503
+ # type(self._wrappedPaint.getConverterByName(attrName)), 1
504
+ # )
505
+ # value = value.value + self._instancer[value.varIdx] * factor
506
+ # else:
507
+ # value = value.value
508
+ # elif type(value).__name__.startswith("Var"):
509
+ # value = PaintVarWrapper(value, self._instancer)
510
+ # elif isinstance(value, (list, UserList)):
511
+ # value = [PaintVarWrapper(item, self._instancer) for item in value]
474
512
return value
0 commit comments