I am currently working on a script that deletes all paths outside of the metrics box. Unfortunately, it is not functioning as expected at the moment. I would greatly appreciate any assistance in identifying the issue.
If anyone has experience with similar tasks or can offer any advice, it would be incredibly helpful. Here’s the code I wrote.
import GlyphsApp
font = Glyphs.font
selected_layers = font.selectedLayers # Get currently selected layers
selected_glyphs = [layer.parent for layer in selected_layers] # Get parent glyphs of selected layers
for glyph in selected_glyphs: # Process each selected glyph
for layer in glyph.layers:
bounds = layer.bounds # Get the metrics (bounds) of the layer
x_min, y_min = bounds.origin.x, bounds.origin.y
x_max, y_max = x_min + bounds.size.width, y_min + bounds.size.height
# Delete shapes located outside the metrics
for shape in layer.shapes[:]: # Iterate over a copied list to safely remove items
remove_shape = False # Variable to track whether the shape should be removed
# Iterate over the shape's paths
for path in shape.paths:
for node in path.nodes: # Iterate through the nodes of each path
x, y = node.position.x, node.position.y # Get x, y coordinates of the node
# If a node is outside the metrics, mark the shape for removal
if x < x_min or x > x_max or y < y_min or y > y_max:
remove_shape = True
break # Stop checking if any node is out of bounds
if remove_shape:
break # Stop checking further if shape is marked for removal
if remove_shape:
layer.shapes.remove(shape) # Remove shape located outside the metrics