Ruby macro API


Use Ruby scripts to process glyphs in bulk: select them, set path widths, scale, round corners, and delete.

Opening the console

  1. Open “Glyphs”.
  2. Click “Lab” in the navigation bar and choose “Script”.

Lab menu

Action What it does
Ctrl + Enter (macOS: Cmd + Enter) Run
“Run” Run
“Stop” Shown only while running. Aborts the script and resets the runtime
“Close” Close the console

Runtime

Item Details
Ruby version 3.4.1
Global object font
Output Anything from puts, print, and p appears in the output pane
Return value The value of the last expression is shown, prefixed with =>
State Global variables persist between runs and are cleared when you click “Stop” or reload the page
Standard library Available, e.g. require "set", sleep, format
puts "a", "b"
print "c"
print "d\n"
p 1, [2, 3]
nil

Output:

a
b
cd
1
[2, 3]
=> nil

Global variables persist between runs. Running $counter = ($counter || 0) + 1 twice in a row gives => 1, then => 2.

Font used in the examples

The examples below use a font with the following contents.

Glyph Code point Contents
.notdef, space —, U+0020 Required glyphs
一 uni4E00 U+4E00, extra code point U+2F00 One rectangular contour
二 uni4E8C U+4E8C Two rectangular contours
川 uni5DDD U+5DDD Three skeleton paths, width 40
花 uni82B1 U+82B1 Seven contours
uniE000, uniE001 U+E000, U+E001 Private Use Area, one rectangle each

font

font.all

Selects every glyph and returns a GlyphSet. Unencoded glyphs (such as .notdef) are included.

set = font.all
puts set.size
p set.names
set

Output:

8
[".notdef", "space", "uni4E00", "uni4E8C", "uni5DDD", "uni82B1", "uniE000", "uniE001"]
=> #<GlyphSet size=8 [.notdef space uni4E00 uni4E8C uni5DDD uni82B1 uniE000 uniE001]>

font.select(from, to = nil)

Selects by Unicode code point and returns a GlyphSet.

Form Example Output
Range font.select(0x4E00..0x9FFF) #<GlyphSet size=4 [uni4E00 uni4E8C uni5DDD uni82B1]>
Start and end code points font.select(0x4E00, 0x4E8C) #<GlyphSet size=2 [uni4E00 uni4E8C]>
Single code point font.select(0x5DDD).names ["uni5DDD"]
Single character font.select("花") #<GlyphSet size=1 [uni82B1]>
Start and end characters font.select("一", "二").names ["uni4E00", "uni4E8C"]

font.all and font.select sync with the selection in “Glyphs”. Run font.select(0x4E00..0x9FFF), close the console, and those glyphs are already selected in the overview.

Selection synced to Glyphs

font.deselect

Clears the selection in “Glyphs”. Returns nil.

font.deselect

font.filter_history

Lists recent filter operations, newest first, up to 5.

font.filter_history.first

Output:

=> {"id" => "89077f14-e568-4263-9de8-db5814d47ec0", "kind" => "filter.roundCorner", "params" => {"inner" => 0, "outer" => 10, "smooth" => 0, "angleEqualize" => 0}, "userName" => "Alex", "glyphCount" => 1, "createdAt" => "2026-11-17T09:21:04.477621+00:00", "finishedAt" => "2026-11-17T09:21:08.355064+00:00", "revertedAt" => nil}
Field Description
id Operation ID; pass it to font.revert_filter
kind Filter type
params Parameters used
userName Who ran it
glyphCount Number of glyphs changed
createdAt, finishedAt Start and finish times
revertedAt When it was reverted; nil if not reverted

After applying rounded corners 6 times in a row, font.filter_history.size is 5.

font.revert_filter(op_id, force: false)

Restores the glyphs changed by a filter operation to their state before it was applied.

r = font.select("二").round_corners(20)
font.revert_filter(r["op"])

Output:

=> {"restored" => 1, "skippedEdited" => [], "skippedRange" => 0}
Returned field Description
restored Number of glyphs restored
skippedEdited Names of glyphs skipped because they were edited after the filter was applied
skippedRange Number skipped because they’re outside your edit permission

Glyphs edited after the filter are skipped by default and restored only with force: true.

Step Code Output
Apply rounded corners font.select("一").round_corners(20) "corners" => 4
Edit and save font.select("一").set_scale(110) "glyphs" => 1
Revert (default) font.revert_filter(op) {"restored" => 0, "skippedEdited" => ["uni4E00"], "skippedRange" => 0}
Revert (forced) font.revert_filter(op, force: true) {"restored" => 1, "skippedEdited" => [], "skippedRange" => 0}

Use either “default” or “forced”, not both. Keep three things in mind:

GlyphSet

The return value of font.all and font.select. It holds only glyph IDs, names, and code points, not contour data.

Reading

Method Description
size Number of glyphs
names Array of names
ids Array of IDs
entries Array of raw entries, each {"id", "name", "unicode"}
each, map, select, first, count… All Enumerable methods
inspect, to_s Summary string listing only the first 8 names
set = font.select(0x4E00..0x9FFF)
set.each { |g| puts "#{g["name"]} U+#{g["unicode"].to_s(16).upcase} #{g["id"]}" }
p set.map { |g| g["unicode"] }
p set.ids
p set.first
set.count

Output:

uni4E00 U+4E00 g_uni4E00
uni4E8C U+4E8C g_uni4E8C
uni5DDD U+5DDD g_uni5DDD
uni82B1 U+82B1 g_uni82B1
[19968, 20108, 24029, 33457]
["g_uni4E00", "g_uni4E8C", "g_uni5DDD", "g_uni82B1"]
{"id" => "g_uni4E00", "name" => "uni4E00", "unicode" => 19968}
=> 4

Enumerable’s select and map return plain arrays, not a GlyphSet. To keep calling the editing methods below, wrap the result with Fontion::GlyphSet.new(array).

set_path_width(width)

Sets the width of skeleton paths, in font units. Only skeleton paths are affected; regular contours are left alone.

font.select("川").set_path_width 60

Output:

=> {"glyphs" => 1, "contours" => 3, "skipped" => 0}
Returned field Description
glyphs Number of glyphs changed
contours Number of paths changed
skipped Number of glyphs skipped because they’re outside your edit permission
Case Code Result
Skeleton paths font.select("川").set_path_width 60 All three paths go from width 40 to 60
Width already matches Run the same code again "glyphs" => 0, "contours" => 0
Regular contour font.select("一").set_path_width 60 "glyphs" => 0, "contours" => 0; contour unchanged
Decimal font.select("川").set_path_width 42.5 Width becomes 42.5
Zero or negative font.all.set_path_width 0 Error: 寬度須為大於 0 的數字 (width must be a number greater than 0)
String font.all.set_path_width "60" Error: 寬度須為大於 0 的數字

set_scale(sx, sy = nil, at: nil)

Scales contours by percentage; 100 is the original size. Returns the same fields as set_path_width.

Parameter Description
sx Horizontal scale percentage; must be greater than 0
sy Vertical scale percentage. Defaults to sx
at: Scale origin [x, y]. Defaults to the center of each glyph’s own bounding box
Code Bounding box before Bounding box after
font.select("花").set_scale(120) 38, 33 – 924, 935 -51.5, -57.25 – 1011.69, 1025.15
font.select("二").set_scale(120, 90) 100, 250 – 900, 700 20, 272.5 – 980, 677.5
font.select("一").set_scale(80, at: [0, 0]) 100, 400 – 900, 480 80, 320 – 720, 384

Bounding boxes are written as “left, top – right, bottom”.

Invalid argument Message
set_scale(-50) 縮放百分比須為大於 0 的數字 (scale percentage must be a number greater than 0)
set_scale(100, at: [0]) 原點須為 [x, y] 兩個數字 (origin must be two numbers [x, y])

round_corners(radius, inner: 0, smooth: 0, stem: 0, equalize: 0)

The rounded corners filter, the same one found under “Filters” in the navigation bar.

Parameter Description
radius Outer corner radius
inner: Inner corner radius
smooth: 0–100, smoothness
stem: Stem width; the radius won’t exceed half of it. 0 means no limit
equalize: 0–100, angle equalization
r = font.select("二").round_corners(20)
p r

Output:

{"glyphs" => 1, "corners" => 8, "skipped" => 0, "incompatible" => [], "op" => "be7e2a88-c8fc-4eee-af44-33eb6b3b4d02"}
Returned field Description
glyphs Number of glyphs changed
corners Number of corners rounded
skipped Number of glyphs skipped because they’re outside your edit permission
incompatible Names of glyphs skipped because their structure differs between masters
op Operation ID, which you can pass to font.revert_filter. nil if nothing changed
Case Code Result
Two rectangles font.select("二").round_corners(20) 8 corners; each contour goes from 4 segments to 8
All parameters font.select("一").round_corners(20, inner: 8, smooth: 40, stem: 80, equalize: 50) 4 corners
Skeleton paths only font.select("川").round_corners(20) "glyphs" => 0, "corners" => 0, "op" => nil
Radius of 0 font.select("二").round_corners(0) "glyphs" => 0, "corners" => 0, "op" => nil
Parameter out of range font.all.round_corners(10, smooth: 150) Error: 圓角參數須為 0 以上的數字(smooth、equalize 為 0–100) (corner parameters must be numbers ≥ 0; smooth and equalize must be 0–100)

delete

Deletes the glyphs in the set. There’s no confirmation dialog, and deletion can’t be undone.

set = font.select(0xE000..0xF8FF)
p set
set.delete

Output:

#<GlyphSet size=2 [uniE000 uniE001]>
=> {"deleted" => 2, "skipped" => 0}
Returned field Description
deleted Number of glyphs deleted
skipped Number of glyphs skipped
Case Result
Required glyphs .notdef, space Skipped: {"deleted" => 0, "skipped" => 2}
Empty set {"deleted" => 0, "skipped" => 0}

Deleting clears the selection in “Glyphs”.

Error handling

API errors are raised as Fontion::Error, which you can catch with rescue.

begin
  font.all.set_scale(-50)
rescue Fontion::Error => e
  puts "Failed: #{e.message}"
end

Output:

Failed: 縮放百分比須為大於 0 的數字
=> nil

If you don’t catch it, the output pane shows the error and its call location in red, and the script stops:

eval_async:17:in 'Fontion.call': 縮放百分比須為大於 0 的數字 (Fontion::Error)
eval_async:79:in 'Fontion::GlyphSet#set_scale'
eval_async:1:in '<main>'
Message Cause
碼位範圍不正確 (invalid code point range) select’s start is greater than its end, or isn’t a valid code point
寬度須為大於 0 的數字 (width must be a number greater than 0) Argument to set_path_width
縮放百分比須為大於 0 的數字 (scale percentage must be a number greater than 0) sx or sy of set_scale
原點須為 [x, y] 兩個數字 (origin must be two numbers [x, y]) at: of set_scale
圓角參數須為 0 以上的數字(smooth、equalize 為 0–100) (corner parameters out of range) Arguments to round_corners
操作 ID 不正確 (invalid operation ID) The ID passed to revert_filter is an empty string
此操作已還原 (operation already reverted) The operation passed to revert_filter has already been reverted

Ruby’s own errors also appear in the output pane, such as syntax errors (SyntaxError) and calls to methods that don’t exist (NoMethodError).

Combined examples

Set the width across several non-contiguous ranges at once

font.select only takes one contiguous range of code points at a time. Merge the entries from several selections and wrap them in a single GlyphSet.

ranges = [0x5DDD..0x5DDD, 0x4E00..0x4E8C]
entries = ranges.flat_map { |r| font.select(r).entries }
set = Fontion::GlyphSet.new(entries)
p set
set.set_path_width 50

Output:

#<GlyphSet size=3 [uni5DDD uni4E00 uni4E8C]>
=> {"glyphs" => 1, "contours" => 3, "skipped" => 0}

Of the three glyphs, only 川 has skeleton paths, so 1 glyph and 3 paths are changed.

Pick a string of characters, scale them, then set the width

chars = "一二川"
entries = chars.each_char.flat_map { |c| font.select(c).entries }
set = Fontion::GlyphSet.new(entries)
puts "#{set.size} glyphs"
r1 = set.set_scale(100, 110)
r2 = set.set_path_width 70
{ "scale" => r1, "width" => r2 }

Output:

3 glyphs
=> {"scale" => {"glyphs" => 3, "contours" => 6, "skipped" => 0}, "width" => {"glyphs" => 1, "contours" => 3, "skipped" => 0}}

Filter by name

picked = font.all.select { |g| g["name"].start_with?("uni4E") }
set = Fontion::GlyphSet.new(picked)
p set.names
set.set_scale(95)

Output:

["uni4E00", "uni4E8C"]
=> {"glyphs" => 2, "contours" => 3, "skipped" => 0}

Process in batches and total the results

total = Hash.new(0)
[0x4E00..0x4EFF, 0x5D00..0x5DFF, 0x8200..0x82FF].each do |range|
  set = font.select(range)
  next if set.size == 0
  r = set.set_scale(100)
  puts format("U+%04X–U+%04X: %d glyphs, %d contours", range.first, range.last, r["glyphs"], r["contours"])
  r.each { |k, v| total[k] += v }
end
total

Output:

U+4E00–U+4EFF: 2 glyphs, 3 contours
U+5D00–U+5DFF: 1 glyphs, 3 contours
U+8200–U+82FF: 1 glyphs, 7 contours
=> {"glyphs" => 4, "contours" => 13, "skipped" => 0}

Round corners, and revert if the result is more than expected

set = font.select(0x4E00..0x4E8C)
r = set.round_corners(15, inner: 5)
puts "Rounded #{r["corners"]} corners"
if r["corners"] > 6
  puts "More than expected, reverting"
  p font.revert_filter(r["op"])
end
font.deselect

Output:

Rounded 8 corners
More than expected, reverting
{"restored" => 1, "skippedEdited" => [], "skippedRange" => 0}
=> nil

Check the range before deleting

set = font.select(0xE000..0xF8FF)
p set          # check which glyphs will be deleted first
set.delete

Output:

#<GlyphSet size=2 [uniE000 uniE001]>
=> {"deleted" => 2, "skipped" => 0}

Method summary

Method Returns
font.all GlyphSet
font.select(from, to = nil) GlyphSet
font.deselect nil
font.filter_history Array
font.revert_filter(op_id, force: false) {"restored", "skippedEdited", "skippedRange"}
GlyphSet#size, names, ids, entries Number or array
GlyphSet#set_path_width(width) {"glyphs", "contours", "skipped"}
GlyphSet#set_scale(sx, sy = nil, at: nil) {"glyphs", "contours", "skipped"}
GlyphSet#round_corners(radius, inner:, smooth:, stem:, equalize:) {"glyphs", "corners", "skipped", "incompatible", "op"}
GlyphSet#delete {"deleted", "skipped"}
Fontion::GlyphSet.new(entries) GlyphSet