Skip to content

Commit

Permalink
Long text entry crash FIXED, plus additional minor improvements
Browse files Browse the repository at this point in the history
- Fixed long text entry crash, see Issue #9. Deprecated fix_length arg
- Silvan Extended Template: Added more support for colorless cards
- New helper function `check_fonts()`, pass a list of fonts to check if they exist in Photoshop
- Added optional args for layer and reference to align, align_vertical, align_horizontal
- Deprecated ability words list, implemented new regex to detect these scenarios automatically
- Moved re.compile for card_info function to top of core for minor execution time improvement on large operations
  • Loading branch information
Investigamer committed Jul 28, 2022
1 parent dbf1447 commit 1eb685d
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 182 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 0 additions & 130 deletions proxyshop/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,135 +262,6 @@
"{CHAOS}": "?"
}

# Ability words which should be italicised in formatted text
ability_words = [
"Adamant",
"Addendum",
"Battalion",
"Bloodrush",
"Channel",
"Chroma",
"Cohort",
"Converge",
"Council's dilemma",
"Delirium",
"Domain",
"Eminence",
"Enrage",
"Fateful hour",
"Ferocious",
"Formidable",
"Grandeur",
"Hellbent",
"Heroic",
"Imprint",
"Inspired",
"Join forces",
"Kinship",
"Landfall",
"Lieutenant",
"Metalcraft",
"Morbid",
"Parley",
"Radiance",
"Raid",
"Rally",
"Revolt",
"Spell mastery",
"Strive",
"Sweep",
"Tempting offer",
"Threshold",
"Undergrowth",
"Will of the council",
"Magecraft",

# AFR ability words
"Antimagic Cone",
"Fear Ray",
"Pack tactics",
"Acid Breath",
"Teleport",
"Lightning Breath",
"Wild Magic Surge",
"Two-Weapon Fighting",
"Archery",
"Bear Form",
"Mage Hand",
"Cure Wounds",
"Dispel Magic",
"Gentle Reprise",
"Beacon of Hope",
"Displacement",
"Drag Below",
"Siege Monster",
"Dark One's Own Luck",
"Climb Over",
"Tie Up",
"Rappel Down",
"Rejuvenation",
"Engulf",
"Dissolve",
"Poison Breath",
"Tragic Backstory",
"Cunning Action",
"Stunning Strike",
"Circle of Death",
"Bardic Inspiration",
"Song of Rest",
"Sneak Attack",
"Tail Spikes",
"Dominate Monster",
"Flurry of Blows",
"Divine Intervention",
"Split",
"Magical Tinkering",
"Keen Senses",
"Grant an Advantage",
"Smash the Chest",
"Pry It Open",
"Fire Breath",
"Cone of Cold",
"Brave the Stench",
"Search the Body",
"Search the Room",
"Bewitching Whispers",
"Whispers of the Grave",
"Animate Walking Statue",
"Trapped!",
"Invoke Duplicity",
"Combat Inspiration",
"Cold Breath",
"Life Drain",
"Fight the Current",
"Find a Crossing",
"Intimidate Them",
"Fend Them Off",
"Smash It",
"Lift the Curse",
"Steal Its Eyes",
"Break Their Chains",
"Interrogate Them",
"Foil Their Scheme",
"Learn Their Secrets",
"Journey On",
"Make Camp",
"Rouse the Party",
"Set Off Traps",
"Form a Party",
"Start a Brawl",
"Make a Retreat",
"Stand and Fight",
"Distract the Guards",
"Hide",
"Charge Them",
"Befriend Them",
"Negative Energy Cone",

# Midnight Hunt words
"Coven",
]

# Card rarities
rarity_common = "common"
rarity_uncommon = "uncommon"
Expand Down Expand Up @@ -494,7 +365,6 @@ def load_values(self):

# Cards
self.symbols = symbols
self.ability_words = ability_words
self.basic_land_names = basic_land_names
self.set_symbols = set_symbols
self.Faces = Faces
Expand Down
15 changes: 9 additions & 6 deletions proxyshop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"Planar": ["planar"]
}

# REGEX Patterns
re_art = re.compile(r'\(+(.*?)\)')
re_set = re.compile(r'\[(.*)\]')
re_cre = re.compile(r'{(.*)}')

"""
TEMPLATE FUNCTIONS
Expand Down Expand Up @@ -151,11 +155,6 @@ def retrieve_card_info(filename):
fn_split = re.split('|'.join(map(re.escape, sep)), fn)
name = fn_split[0]

# Precompile pattern
re_art = re.compile(r'\(+(.*?)\)')
re_set = re.compile(r'\[(.*)\]')
re_cre = re.compile(r'{(.*)}')

# Match pattern
artist = re_art.findall(filename)
set_code = re_set.findall(filename)
Expand All @@ -166,7 +165,11 @@ def retrieve_card_info(filename):
else: creator = None
if artist: artist = artist[0]
else: artist = None
if set_code: set_code = set_code[0]
if set_code:
set_code = set_code[0]
if set_code.upper() not in con.set_symbols:
if set_code.upper()[1:] in con.set_symbols:
set_code = set_code[1:]
else: set_code = None

return {
Expand Down
90 changes: 73 additions & 17 deletions proxyshop/format_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ def generate_italics(card_text):
* Generates italics text array from card text to italicise all text within (parentheses) and all ability words.
"""
italic_text = []

# Find and add reminder text
end_index = 0
while True:
start_index = card_text.find("(", end_index)
Expand All @@ -514,9 +516,19 @@ def generate_italics(card_text):
italic_text.extend([card_text[start_index:end_index]])
else: break

# Attach all ability words to the italics array
for ability_word in con.ability_words:
italic_text.extend([ability_word + " \u2014"]) # Include em dash
# Find and add ability words
reg = re.compile(r"[•|\w|\-|\s|]*? — ")
for match in reg.findall(card_text):
# Cover boast cards and choose cards that aren't weird AFR cases
if (
any(s in match for s in ("• ", "Boast"))
and card_text[0:12] != "Choose one —"
): continue

# Fix bullet points and carriage return
if "• " in match: match = match.replace("• ", "")
if "\r" in match: match = match.replace("\r", "")
italic_text.extend([match])

return italic_text

Expand Down Expand Up @@ -571,10 +583,7 @@ def classic_align_right(primedesc, start, end):
return primedesc


def scale_text_right_overlap(
layer: ps.LayerKind.TextLayer,
reference: ps.LayerKind.TextLayer
) -> None:
def scale_text_right_overlap(layer, reference) -> None:
"""
Scales a text layer down (in 0.2 pt increments) until its right bound
has a 24 px clearance from a reference layer's left bound.
Expand All @@ -591,22 +600,27 @@ def scale_text_right_overlap(
elif reference.bounds == [0, 0, 0, 0]: return

# Can't find UnitValue object in python api
step_size = 0.2
reference_left_bound = reference.bounds[0]
layer_left_bound = layer.bounds[0]
layer_right_bound = layer.bounds[2]
old_size = float(layer.textItem.size)
step, half_step = 0.4, 0.2

# Obtain proper spacing for this document size
spacing = int((app.activeDocument.width / 3264) * 24)
spacing = int((app.activeDocument.width / 3264) * 36)

# Guard against the reference's left bound being left of the layer's left bound
if reference_left_bound >= layer_left_bound:
# Step down the font till it clears the reference
while layer_right_bound > (reference_left_bound - spacing): # minimum 24 px gap
layer.textItem.size -= step_size
layer.textItem.size -= step
layer_right_bound = layer.bounds[2]

layer.textItem.size += half_step
layer_right_bound = layer.bounds[2]
if layer_right_bound > (reference_left_bound - spacing):
layer.textItem.size -= half_step

# Shift baseline up to keep text centered vertically
if old_size > layer.textItem.size:
layer.textItem.baselineShift = (old_size * 0.3) - (layer.textItem.size * 0.3)
Expand All @@ -615,21 +629,63 @@ def scale_text_right_overlap(
if contents: reference.textItem.contents = contents


def scale_text_to_fit_reference(layer, reference_layer):
def scale_text_to_fit_reference(layer, ref, spacing: int = None):
"""
Resize a given text layer's contents (in 0.25 pt increments) until it fits inside a specified reference layer.
The resulting text layer will have equal font and lead sizes.
@param layer: Text layer to scale.
@param ref: Reference layer the text should fit inside.
@param spacing: [Optional] Amount of mandatory spacing at the bottom of text layer.
"""
# Establish base variables, ensure a level of spacing at the margins
if reference_layer is None: return
spacing = int((app.activeDocument.width / 3264) * 64)
ref_height = psd.get_layer_dimensions(reference_layer)['height'] - spacing
if not ref: return
if not spacing: # If no spacing provided, use default
spacing = int((app.activeDocument.width / 3264) * 64)
ref_height = psd.get_layer_dimensions(ref)['height'] - spacing
font_size = layer.textItem.size
step_size = 0.25
step, half_step = 0.4, 0.2

# step down font and lead sizes by the step size, and update those sizes in the layer
# Step down font and lead sizes by the step size, and update those sizes in the layer
if ref_height > psd.get_text_layer_dimensions(layer)['height']: return
while ref_height < psd.get_text_layer_dimensions(layer)['height']:
font_size -= step_size
font_size -= step
layer.textItem.size = font_size
layer.textItem.leading = font_size

# Take a half step back up, check if still in bounds and adjust back if needed
font_size += half_step
layer.textItem.size = font_size
layer.textItem.leading = font_size
if ref_height < psd.get_text_layer_dimensions(layer)['height']:
font_size -= half_step
layer.textItem.size = font_size
layer.textItem.leading = font_size


def scale_text_to_fit_height(layer, height: int):
"""
Resize a given text layer's contents (in 0.25 pt increments) until it fits inside a specified reference layer.
The resulting text layer will have equal font and lead sizes.
@param layer: Text layer to scale.
@param height: Reference height to fit.
"""
# Establish base variables, ensure a level of spacing at the margins
font_size = layer.textItem.size
step, half_step = 0.4, 0.2

# Step down font and lead sizes by the step size, and update those sizes in the layer
if height > psd.get_text_layer_dimensions(layer)['height']: return
while height < psd.get_text_layer_dimensions(layer)['height']:
font_size -= step
layer.textItem.size = font_size
layer.textItem.leading = font_size

# Take a half step back up, check if still in bounds and adjust back if needed
font_size += half_step
layer.textItem.size = font_size
layer.textItem.leading = font_size
if height < psd.get_text_layer_dimensions(layer)['height']:
font_size -= half_step
layer.textItem.size = font_size
layer.textItem.leading = font_size

Expand Down
Loading

0 comments on commit 1eb685d

Please sign in to comment.