Coverage for lib/lottie/objects/text.py: 46%

138 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-20 16:17 +0100

1from .base import LottieObject, LottieProp, LottieEnum 

2from .properties import Value, MultiDimensional, ColorValue, Color 

3from ..nvector import NVector 

4from .helpers import Transform 

5from .shapes import ShapeElement 

6 

7 

8#ingroup Lottie 

9class MaskedPath(LottieObject): 

10 """! 

11 Uses the path described by a layer mask to put the text on said path. 

12 """ 

13 _props = [ 

14 LottieProp("mask", "m", int, False), 

15 LottieProp("first_margin", "f", Value, False), 

16 LottieProp("last_margin", "l", Value, False), 

17 LottieProp("reverse_path", "r", Value, False), 

18 LottieProp("force_alignment", "a", Value, False), 

19 LottieProp("perpendicular_to_path", "p", Value, False), 

20 ] 

21 

22 def __init__(self): 

23 super().__init__() 

24 

25 ## Index of the mask to use 

26 self.mask = None 

27 self.first_margin = None 

28 self.last_margin = None 

29 self.reverse_path = None 

30 self.force_alignment = None 

31 self.perpendicular_to_path = None 

32 

33 

34## @ingroup Lottie 

35## @ingroup LottieCheck 

36class TextAnimatorDataProperty(Transform): 

37 _props = [ 

38 LottieProp("rotate_x", "rx", Value), 

39 LottieProp("rotate_y", "ry", Value), 

40 

41 LottieProp("stroke_width", "sw", Value), 

42 LottieProp("stroke_color", "sc", ColorValue), 

43 LottieProp("stroke_hue", "sh", Value, False), 

44 LottieProp("stroke_saturation", "ss", Value, False), 

45 LottieProp("stroke_brightness", "sb", Value, False), 

46 LottieProp("stroke_opacity", "so", Value, False), 

47 

48 LottieProp("fill_color", "fc", ColorValue), 

49 LottieProp("fill_hue", "fh", Value), 

50 LottieProp("fill_saturation", "fs", Value), 

51 LottieProp("fill_brightness", "fb", Value), 

52 LottieProp("fill_opacity", "fo", Value, False), 

53 

54 LottieProp("tracking", "t", Value), 

55 LottieProp("scale", "s", MultiDimensional), 

56 LottieProp("blur", "bl", Value, False), 

57 LottieProp("line_spacing", "ls", Value, False), 

58 

59 ] 

60 

61 def __init__(self): 

62 super().__init__() 

63 ## Angle? 

64 self.rx = Value() 

65 ## Angle? 

66 self.ry = Value() 

67 

68 ## Stroke width 

69 self.stroke_width = Value() 

70 ## Stroke color 

71 self.stroke_color = ColorValue() 

72 self.stroke_hue = None 

73 self.stroke_saturation = None 

74 self.stroke_brightness = None 

75 self.stroke_opacity = None 

76 

77 ## Fill color 

78 self.fill_color = ColorValue() 

79 ## Hue 

80 self.fill_hue = Value() 

81 ## Saturation 0-100 

82 self.fill_saturation = Value() 

83 ## Brightness 0-100 

84 self.fill_brightness = Value() 

85 self.fill_opacity = None 

86 

87 ## Tracking 

88 self.tracking = Value() 

89 self.blur = None 

90 self.line_spacing = None 

91 

92 

93## @ingroup Lottie 

94class TextGrouping(LottieEnum): 

95 Characters = 1 

96 Word = 2 

97 Line = 3 

98 All = 4 

99 

100 @classmethod 

101 def default(cls): 

102 return cls.Characters 

103 

104 

105## @ingroup Lottie 

106## @ingroup LottieCheck 

107class TextMoreOptions(LottieObject): 

108 _props = [ 

109 LottieProp("alignment", "a", MultiDimensional), 

110 LottieProp("grouping", "g", TextGrouping), 

111 ] 

112 

113 def __init__(self): 

114 self.alignment = MultiDimensional(NVector(0, 0)) 

115 self.grouping = TextGrouping.default() 

116 

117 

118## @ingroup Lottie 

119class TextJustify(LottieEnum): 

120 Left = 0 

121 Right = 1 

122 Center = 2 

123 JustifyWithLastLineLeft = 3 

124 JustifyWithLastLineRight = 4 

125 JustifyWithLastLineCenter = 5 

126 JustifyWithLastLineFull = 6 

127 

128 

129#ingroup Lottie 

130class TextCaps(LottieEnum): 

131 Regular = 0 

132 AllCaps = 1 

133 SmallCaps = 2 

134 

135 

136## @ingroup Lottie 

137class TextDocument(LottieObject): 

138 """! 

139 @see http://docs.aenhancers.com/other/textdocument/ 

140 

141 Note that for multi-line text, lines are separated by \\r 

142 """ 

143 _props = [ 

144 LottieProp("font_family", "f", str), 

145 LottieProp("fill_color", "fc", Color), 

146 

147 LottieProp("stroke_color", "sc", Color, False), 

148 LottieProp("stroke_width", "sw", float, False), 

149 LottieProp("stroke_over_fill", "of", bool, False), 

150 

151 LottieProp("font_size", "s", float), 

152 LottieProp("line_height", "lh", float), 

153 LottieProp("wrap_size", "sz", NVector), 

154 LottieProp("wrap_position", "ps", NVector, False), 

155 

156 LottieProp("text", "t", str), 

157 LottieProp("justify", "j", TextJustify), 

158 LottieProp("text_caps", "ca", TextCaps, False), 

159 LottieProp("tracking", "tr", float, False), 

160 LottieProp("baseline_shift", "ls", float, False), 

161 ] 

162 

163 def __init__(self, text="", font_size=10, color=None, font_family=""): 

164 self.font_family = font_family 

165 

166 ## Text color 

167 self.fill_color = color or Color(0, 0, 0) 

168 

169 self.stroke_color = None 

170 self.stroke_width = 0 

171 ## Render stroke above the fill 

172 self.stroke_over_fill = None 

173 

174 ## Line height when wrapping 

175 self.line_height = None 

176 ## Text alignment 

177 self.justify = TextJustify.Left 

178 ## Size of the box containing the text 

179 self.wrap_size = None 

180 ## Position of the box containing the text 

181 self.wrap_position = None 

182 ## Text 

183 self.text = text 

184 ## Font Size 

185 self.font_size = font_size 

186 ## Text caps 

187 self.text_caps = None 

188 ## Text Tracking 

189 self.tracking = None 

190 self.baseline_shift = None 

191 

192 

193## @ingroup Lottie 

194class TextDataKeyframe(LottieObject): 

195 _props = [ 

196 LottieProp("start", "s", TextDocument), 

197 LottieProp("time", "t", float), 

198 ] 

199 

200 def __init__(self, time=0, start=None): 

201 ## Start value of keyframe segment. 

202 self.start = start 

203 ## Start time of keyframe segment. 

204 self.time = time 

205 

206 

207## @ingroup Lottie 

208class TextData(LottieObject): 

209 _props = [ 

210 LottieProp("keyframes", "k", TextDataKeyframe, True), 

211 ] 

212 

213 def __init__(self): 

214 self.keyframes = [] 

215 

216 def get_value(self, time): 

217 for kf in self.keyframes: 

218 if kf.time >= time: 

219 return kf.start 

220 return None 

221 

222 

223## @ingroup Lottie 

224class TextAnimatorData(LottieObject): 

225 _props = [ 

226 LottieProp("properties", "a", TextAnimatorDataProperty, True), 

227 LottieProp("data", "d", TextData, False), 

228 LottieProp("more_options", "m", TextMoreOptions, False), 

229 LottieProp("masked_path", "p", MaskedPath), 

230 ] 

231 

232 def __init__(self): 

233 self.properties = [] 

234 self.data = TextData() 

235 self.more_options = TextMoreOptions() 

236 self.masked_path = MaskedPath() 

237 

238 def add_keyframe(self, time, item): 

239 self.data.keyframes.append(TextDataKeyframe(time, item)) 

240 

241 def get_value(self, time): 

242 return self.data.get_value(time) 

243 

244 

245## @ingroup Lottie 

246class FontPathOrigin(LottieEnum): 

247 Local = 0 

248 CssUrl = 1 

249 ScriptUrl = 2 

250 FontUrl = 3 

251 

252 

253## @ingroup Lottie 

254class Font(LottieObject): 

255 _props = [ 

256 LottieProp("ascent", "ascent", float), 

257 LottieProp("font_family", "fFamily", str), 

258 LottieProp("name", "fName", str), 

259 LottieProp("font_style", "fStyle", str), 

260 LottieProp("path", "fPath", str), 

261 LottieProp("weight", "fWeight", str), 

262 LottieProp("origin", "origin", FontPathOrigin), 

263 LottieProp("css_class", "fClass", str), 

264 ] 

265 

266 def __init__(self, font_family="sans", font_style="Regular", name=None): 

267 self.ascent = None 

268 self.font_family = font_family 

269 self.font_style = font_style 

270 self.name = name or "%s-%s" % (font_family, font_style) 

271 self.path = None 

272 self.weight = None 

273 self.origin = None 

274 self.css_class = None 

275 

276 

277## @ingroup Lottie 

278class FontList(LottieObject): 

279 _props = [ 

280 LottieProp("list", "list", Font, True), 

281 ] 

282 

283 def __init__(self): 

284 self.list = [] 

285 

286 def append(self, font): 

287 self.list.append(font) 

288 

289 

290## @ingroup Lottie 

291class CharacterData(LottieObject): 

292 """! 

293 Character shapes 

294 """ 

295 _props = [ 

296 LottieProp("shapes", "shapes", ShapeElement, True), 

297 ] 

298 

299 def __init__(self): 

300 self.shapes = [] 

301 

302 

303## @ingroup Lottie 

304class Chars(LottieObject): 

305 """! 

306 Defines character shapes to avoid loading system fonts 

307 """ 

308 _props = [ 

309 LottieProp("character", "ch", str, False), 

310 LottieProp("font_family", "fFamily", str, False), 

311 LottieProp("font_size", "size", float, False), 

312 LottieProp("font_style", "style", str, False), 

313 LottieProp("width", "w", float, False), 

314 LottieProp("data", "data", CharacterData, False), 

315 ] 

316 

317 def __init__(self): 

318 ## Character Value 

319 self.character = "" 

320 ## Character Font Family 

321 self.font_family = "" 

322 ## Character Font Size 

323 self.font_size = 0 

324 ## Character Font Style 

325 self.font_style = "" # Regular 

326 ## Character Width 

327 self.width = 0 

328 ## Character Data 

329 self.data = CharacterData() 

330 

331 @property 

332 def shapes(self): 

333 return self.data.shapes