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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 | class FormattedTextField (TextField):
"""
A utility class for packing in the required infrastructure to format text with action descriptors.
"""
"""
PROPERTIES
"""
@cached_property
def text_details(self) -> dict:
# Generate italic text arrays from things in (parentheses), ability words, and the given flavor text
italic_text = ft.generate_italics(self.contents) if self.contents else []
# Add flavor text to italics array
flavor_text = self.flavor_text
if self.flavor_text.count("*") >= 2:
# Don't italicize text between asterisk
flavor_text_split = self.flavor_text.split("*")
italic_text.extend([v for i, v in enumerate(flavor_text_split) if not i % 2 and not v == ''])
flavor_text = ''.join(flavor_text_split)
elif self.flavor_text:
# Regular flavor text
italic_text.append(self.flavor_text)
# Locate symbols and update the input string
ret = ft.locate_symbols(self.contents)
input_string = f"{ret['input_string']}\r{flavor_text}" if self.contents else flavor_text
# Locate italics text indices
italics_indices = ft.locate_italics(input_string, italic_text)
return {
'input_string': input_string,
'symbol_indices': ret['symbol_indices'],
'italics_indices': italics_indices,
'rules_text': ret['input_string'],
'flavor_text': flavor_text,
}
@property
def italics_indices(self) -> list[dict]:
return self.text_details['italics_indices']
@property
def symbol_indices(self) -> list[dict]:
return self.text_details['symbol_indices']
@property
def input(self) -> str:
return self.text_details['input_string']
@property
def flavor_text_updated(self) -> str:
return self.text_details['flavor_text']
@property
def rules_text_updated(self) -> str:
return self.text_details['rules_text']
@cached_property
def flavor_text(self) -> str:
return self.kwargs.get('flavor', '').replace('\n', '\r')
@property
def divider(self) -> Optional[ArtLayer]:
# Default to None unless overridden
return
@cached_property
def contents_centered(self) -> bool:
return self.kwargs.get('centered', False)
@cached_property
def flavor_centered(self) -> bool:
return self.kwargs.get('flavor_centered', self.contents_centered)
@cached_property
def line_break_lead(self) -> Union[int, float]:
return self.kwargs.get(
'line_break_lead',
0 if self.contents_centered else con.line_break_lead
)
@cached_property
def flavor_text_lead(self) -> Union[int, float]:
# Lead with divider
if self.divider:
return self.kwargs.get('flavor_text_lead_divider', con.flavor_text_lead_divider)
# Lead without divider
return self.kwargs.get('flavor_text_lead', con.flavor_text_lead)
@cached_property
def flavor_index(self) -> int:
return len(self.contents) if len(self.flavor_text) > 0 else -1
@cached_property
def quote_index(self) -> int:
return self.input.find("\r", self.flavor_index + 3) if self.flavor_index >= 0 else -1
@cached_property
def bold_rules_text(self) -> bool:
return self.kwargs.get('bold_rules_text', False)
@cached_property
def right_align_quote(self) -> bool:
return self.kwargs.get('right_align_quote', False)
@cached_property
def flavor_color(self) -> Optional[SolidColor]:
return self.kwargs.get('flavor_color', None)
@cached_property
def font_size(self) -> float:
if font_size := self.kwargs.get('font_size'):
return font_size * get_text_scale_factor(self.layer)
return self.layer.textItem.size * get_text_scale_factor(self.layer)
"""
METHODS
"""
def format_text(self):
"""
Inserts the given string into the active layer and formats it according to defined parameters with symbols
from the NDPMTG font.
"""
# Set up main descriptors and lists
main_descriptor = ActionDescriptor()
style_list = ActionList()
main_list = ActionList()
# Descriptor ID's
idTo = sID("to")
size = sID("size")
idFrom = sID("from")
leading = sID("leading")
fontName = sID("fontName")
textStyle = sID("textStyle")
pointsUnit = sID("pointsUnit")
spaceAfter = sID("spaceAfter")
autoLeading = sID("autoLeading")
startIndent = sID("startIndent")
spaceBefore = sID("spaceBefore")
leadingType = sID("leadingType")
styleRange = sID("textStyleRange")
paragraphStyle = sID("paragraphStyle")
firstLineIndent = sID("firstLineIndent")
fontPostScriptName = sID("fontPostScriptName")
paragraphStyleRange = sID("paragraphStyleRange")
# Spin up the text insertion action
main_style = ActionDescriptor()
main_range = ActionDescriptor()
main_descriptor.putString(sID("textKey"), self.input)
main_range.putInteger(idFrom, 0)
main_range.putInteger(idTo, len(self.input))
main_style.putString(fontPostScriptName, con.font_rules_text) # MPlantin default
main_style.putString(fontName, con.font_rules_text) # MPlantin default
main_style.putUnitDouble(size, pointsUnit, self.font_size)
apply_color(main_style, self.color)
main_style.putBoolean(autoLeading, False)
main_style.putUnitDouble(leading, pointsUnit, self.font_size)
main_range.putObject(textStyle, textStyle, main_style)
main_list.putObject(styleRange, main_range)
# Bold the contents if necessary
if self.bold_rules_text and self.flavor_index != 0:
bold_range = ActionDescriptor()
bold_style = ActionDescriptor()
contents_index = len(self.input) - 1 if self.flavor_index < 0 else self.flavor_index - 1
bold_range.putInteger(idFrom, 0) # bold start index
bold_range.putInteger(idTo, contents_index) # bold end index
bold_style.putString(fontPostScriptName, con.font_rules_text_bold)
bold_style.putString(fontName, con.font_rules_text_bold)
bold_style.putUnitDouble(size, pointsUnit, self.font_size)
apply_color(bold_style, self.color)
bold_style.putBoolean(autoLeading, False)
bold_style.putUnitDouble(leading, pointsUnit, self.font_size)
bold_range.putObject(textStyle, textStyle, bold_style)
main_list.putObject(styleRange, bold_range)
# Italicize text from our italics indices
for i in self.italics_indices:
italic_range = ActionDescriptor()
italic_style = ActionDescriptor()
italic_range.putInteger(idFrom, i['start_index']) # italics start index
italic_range.putInteger(idTo, i['end_index']) # italics end index
italic_style.putString(fontPostScriptName, con.font_rules_text_italic)
italic_style.putString(fontName, con.font_rules_text_italic)
italic_style.putUnitDouble(size, pointsUnit, self.font_size)
apply_color(italic_style, self.color)
italic_style.putBoolean(autoLeading, False)
italic_style.putUnitDouble(leading, pointsUnit, self.font_size)
italic_range.putObject(textStyle, textStyle, italic_style)
main_list.putObject(styleRange, italic_range)
# Format each symbol correctly
for symbol_index in self.symbol_indices:
ft.format_symbol(
action_list=main_list,
symbol_index=symbol_index['index'],
symbol_colors=symbol_index['colors'],
font_size=self.font_size,
)
# Insert actions for bold, italics, and symbol formatting
main_descriptor.putList(styleRange, main_list)
# Paragraph formatting
desc141 = ActionDescriptor()
desc142 = ActionDescriptor()
desc141.putInteger(idFrom, 0)
desc141.putInteger(idTo, len(self.input)) # input string length
desc142.putUnitDouble(firstLineIndent, pointsUnit, 0)
desc142.putUnitDouble(startIndent, pointsUnit, 0)
desc142.putUnitDouble(sID("endIndent"), pointsUnit, 0)
desc142.putUnitDouble(spaceBefore, pointsUnit, self.line_break_lead)
desc142.putUnitDouble(spaceAfter, pointsUnit, 0)
desc142.putInteger(sID("dropCapMultiplier"), 1)
desc142.putEnumerated(leadingType, leadingType, sID("leadingBelow"))
# Adjust formatting for modal card with bullet points
if "\u2022" in self.input:
desc143 = ActionDescriptor()
startIndexBullet = self.input.find("\u2022")
endIndexBullet = self.input.rindex("\u2022")
desc141.putInteger(idFrom, startIndexBullet)
desc141.putInteger(idTo, endIndexBullet + 1)
desc142.putUnitDouble(firstLineIndent, pointsUnit, -con.modal_indent) # negative modal indent
desc142.putUnitDouble(startIndent, pointsUnit, con.modal_indent) # modal indent
desc142.putUnitDouble(spaceBefore, pointsUnit, 1)
desc142.putUnitDouble(spaceAfter, pointsUnit, 0)
desc143.putString(fontPostScriptName, con.font_mana) # NDPMTG default
desc143.putString(fontName, con.font_rules_text) # MPlantin default
desc143.putUnitDouble(size, pointsUnit, 12)
desc143.putBoolean(autoLeading, False)
desc142.putObject(sID("defaultStyle"), textStyle, desc143)
desc141.putObject(paragraphStyle, paragraphStyle, desc142)
style_list.putObject(paragraphStyleRange, desc141)
main_descriptor.putList(paragraphStyleRange, style_list)
# Flavor text actions
if self.flavor_index >= 0:
# Add linebreak spacing between rules and flavor text
desc141.putInteger(idFrom, self.flavor_index + 3)
desc141.putInteger(idTo, self.flavor_index + 4)
desc142.putUnitDouble(firstLineIndent, pointsUnit, 0)
desc142.putUnitDouble(sID("impliedFirstLineIndent"), pointsUnit, 0)
desc142.putUnitDouble(startIndent, pointsUnit, 0)
desc142.putUnitDouble(sID("impliedStartIndent"), pointsUnit, 0)
desc142.putUnitDouble(spaceBefore, pointsUnit, self.flavor_text_lead) # Space between rules and flavor text
desc141.putObject(paragraphStyle, paragraphStyle, desc142)
style_list.putObject(paragraphStyleRange, desc141)
main_descriptor.putList(paragraphStyleRange, style_list)
# Adjust flavor text color
if self.flavor_color:
colored_range = ActionList()
colored_style = ActionDescriptor()
desc145 = ActionDescriptor()
colored_style.PutInteger(sID("from"), self.flavor_index)
colored_style.PutInteger(sID("to"), len(self.input))
desc145.putString(fontPostScriptName, con.font_rules_text_italic) # MPlantin italic default
desc145.putString(fontName, con.font_rules_text_italic) # MPlantin italic default
desc145.putUnitDouble(size, pointsUnit, self.font_size)
desc145.putBoolean(autoLeading, False)
desc145.putUnitDouble(leading, pointsUnit, self.font_size)
apply_color(desc145, self.flavor_color)
colored_style.PutObject(sID("textStyle"), sID("textStyle"), desc145)
colored_range.PutObject(sID("textStyleRange"), colored_style)
main_descriptor.putList(sID("textStyleRange"), colored_range)
# Quote actions flavor text
if self.quote_index >= 0:
# Adjust line break spacing if there's a line break in the flavor text
desc141.putInteger(idFrom, self.quote_index + 3)
desc141.putInteger(idTo, len(self.input))
desc142.putUnitDouble(spaceBefore, pointsUnit, 0)
desc141.putObject(paragraphStyle, paragraphStyle, desc142)
style_list.putObject(paragraphStyleRange, desc141)
# Optional, align quote credit to right
if self.right_align_quote and self.input.find('"\r—') >= 0:
# Get start and ending index of quotation credit
index_start = self.input.find('"\r—') + 2
index_end = len(self.input) - 1
# Align this part, disable justification reset
ft.align_formatted_text_right(style_list, index_start, index_end)
# Add quote actions to primary action
main_descriptor.putList(paragraphStyleRange, style_list)
# Push changes to text layer
textLayer = sID("textLayer")
ref101 = ActionReference()
desc119 = ActionDescriptor()
ref101.putEnumerated(textLayer, sID("ordinal"), sID("targetEnum"))
desc119.putReference(sID("target"), ref101)
desc119.putObject(idTo, textLayer, main_descriptor)
app.executeAction(sID("set"), desc119, NO_DIALOG)
# Disable hyphenation and set text composer
self.docref.activeLayer.textItem.hyphenation = False
# TODO: Test if this line is necessary
# set_composer_single_line(self.layer)
def execute(self):
super().execute()
# Format text
self.docref.activeLayer = self.layer
self.format_text()
if self.contents_centered:
self.layer.textItem.justification = Justification.Center
|