Ruby 巨集 API


用 Ruby 腳本批次處理字符:選取、設定路徑寬度、縮放、圓角與刪除。

開啟控制台

  1. 開啟「字符總覽」。
  2. 點導覽列的「Lab」,選「巨集」。

Lab 選單

操作 功能
Ctrl + Enter(macOS:Cmd + Enter) 執行
「執行」 執行
「停止」 執行中才出現。中止腳本並重設執行環境
「關閉」 關閉控制台

執行環境

項目 說明
Ruby 版本 3.4.1
全域物件 font
輸出 puts、print、p 的內容顯示在輸出區
回傳值 最後一個運算式的值以 => 開頭顯示
狀態 全域變數在多次執行之間保留,按「停止」或重新整理頁面後清除
標準函式庫 可用,例如 require "set"、sleep、format
puts "a", "b"
print "c"
print "d\n"
p 1, [2, 3]
nil

輸出:

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

全域變數在兩次執行之間保留。連續執行兩次 $counter = ($counter || 0) + 1,分別得到 => 1 與 => 2。

範例使用的字體

以下範例使用的字體內容如下。

字符 碼位 內容
.notdef、space —、U+0020 必備字
一 uni4E00 U+4E00,額外碼位 U+2F00 一個矩形輪廓
二 uni4E8C U+4E8C 兩個矩形輪廓
川 uni5DDD U+5DDD 三條骨架路徑,寬度 40
花 uni82B1 U+82B1 七個輪廓
uniE000、uniE001 U+E000、U+E001 私用區,各一個矩形

font

font.all

選取全部字符,回傳 GlyphSet。未編碼的字符(例如 .notdef)也包含在內。

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

輸出:

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)

依 Unicode 碼位選取,回傳 GlyphSet。

寫法 範例 輸出
Range font.select(0x4E00..0x9FFF) #<GlyphSet size=4 [uni4E00 uni4E8C uni5DDD uni82B1]>
起訖碼位 font.select(0x4E00, 0x4E8C) #<GlyphSet size=2 [uni4E00 uni4E8C]>
單一碼位 font.select(0x5DDD).names ["uni5DDD"]
單一字元 font.select("花") #<GlyphSet size=1 [uni82B1]>
起訖字元 font.select("一", "二").names ["uni4E00", "uni4E8C"]

font.all 與 font.select 會同步「字符總覽」的選取狀態。執行 font.select(0x4E00..0x9FFF) 後關閉控制台,總覽中對應的字符已被選取。

選取同步到字符總覽

font.deselect

清除「字符總覽」的選取。回傳 nil。

font.deselect

font.filter_history

列出最近的濾鏡操作,由新到舊,最多 5 筆。

font.filter_history.first

輸出:

=> {"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}
欄位 說明
id 操作 ID,交給 font.revert_filter
kind 濾鏡種類
params 套用時的參數
userName 執行者
glyphCount 改到的字符數
createdAt、finishedAt 開始與完成時間
revertedAt 還原時間,未還原為 nil

連續套用 6 次圓角後,font.filter_history.size 為 5。

font.revert_filter(op_id, force: false)

把一次濾鏡操作改過的字符還原成套用前的版本。

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

輸出:

=> {"restored" => 1, "skippedEdited" => [], "skippedRange" => 0}
回傳欄位 說明
restored 還原的字符數
skippedEdited 套用後又被修改而跳過的字符名稱
skippedRange 超出編輯權限範圍而跳過的數量

套用後又被修改的字符:預設跳過,force: true 才會還原。

步驟 程式 輸出
套用圓角 font.select("一").round_corners(20) "corners" => 4
修改並儲存 font.select("一").set_scale(110) "glyphs" => 1
還原(預設) font.revert_filter(op) {"restored" => 0, "skippedEdited" => ["uni4E00"], "skippedRange" => 0}
還原(強制) font.revert_filter(op, force: true) {"restored" => 1, "skippedEdited" => [], "skippedRange" => 0}

「預設」與「強制」二選一。使用時要注意三件事:

GlyphSet

font.all 與 font.select 的回傳值。只持有字符的 ID、名稱與碼位,不含輪廓資料。

讀取

方法 說明
size 字符數
names 名稱陣列
ids ID 陣列
entries 原始資料陣列,每筆是 {"id", "name", "unicode"}
each、map、select、first、count… Enumerable 的所有方法
inspect、to_s 摘要字串,名稱只列前 8 個
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

輸出:

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 的 select、map 回傳的是一般陣列,不是 GlyphSet。要繼續呼叫下面的修改方法,用 Fontion::GlyphSet.new(陣列) 包回來。

set_path_width(width)

設定骨架路徑的寬度,單位是字型單位。只作用在骨架路徑,一般輪廓不受影響。

font.select("川").set_path_width 60

輸出:

=> {"glyphs" => 1, "contours" => 3, "skipped" => 0}
回傳欄位 說明
glyphs 改到的字符數
contours 改到的路徑數
skipped 超出編輯權限範圍而跳過的字符數
情況 程式 結果
骨架路徑 font.select("川").set_path_width 60 三條路徑的寬度由 40 變成 60
寬度已相同 再執行一次同樣的程式 "glyphs" => 0, "contours" => 0
一般輪廓 font.select("一").set_path_width 60 "glyphs" => 0, "contours" => 0,輪廓不變
小數 font.select("川").set_path_width 42.5 寬度變成 42.5
0 或負數 font.all.set_path_width 0 錯誤:寬度須為大於 0 的數字
字串 font.all.set_path_width "60" 錯誤:寬度須為大於 0 的數字

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

依百分比縮放輪廓,100 是原尺寸。回傳欄位與 set_path_width 相同。

參數 說明
sx 水平縮放百分比,必須大於 0
sy 垂直縮放百分比。省略時與 sx 相同
at: 縮放原點 [x, y]。省略時是每個字符自己的外框中心
程式 縮放前的外框 縮放後的外框
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

外框以「左, 上 – 右, 下」表示。

錯誤的參數 訊息
set_scale(-50) 縮放百分比須為大於 0 的數字
set_scale(100, at: [0]) 原點須為 [x, y] 兩個數字

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

圓角濾鏡,與導覽列「濾鏡」的圓角相同。

參數 說明
radius 外角半徑
inner: 內角半徑
smooth: 0–100,平滑程度
stem: 字幹寬度,半徑不超過它的一半;0 為不限制
equalize: 0–100,角度等化
r = font.select("二").round_corners(20)
p r

輸出:

{"glyphs" => 1, "corners" => 8, "skipped" => 0, "incompatible" => [], "op" => "be7e2a88-c8fc-4eee-af44-33eb6b3b4d02"}
回傳欄位 說明
glyphs 改到的字符數
corners 圓掉的角數
skipped 超出編輯權限範圍而跳過的字符數
incompatible 各主板結構不一致而略過的字符名稱
op 操作 ID,可交給 font.revert_filter。沒有改到任何字符時是 nil
情況 程式 結果
兩個矩形 font.select("二").round_corners(20) 8 個角;每個輪廓由 4 段變成 8 段
全部參數 font.select("一").round_corners(20, inner: 8, smooth: 40, stem: 80, equalize: 50) 4 個角
只有骨架路徑 font.select("川").round_corners(20) "glyphs" => 0, "corners" => 0, "op" => nil
半徑為 0 font.select("二").round_corners(0) "glyphs" => 0, "corners" => 0, "op" => nil
參數超出範圍 font.all.round_corners(10, smooth: 150) 錯誤:圓角參數須為 0 以上的數字(smooth、equalize 為 0–100)

delete

刪除集合內的字符。不會出現確認視窗,刪除後無法復原。

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

輸出:

#<GlyphSet size=2 [uniE000 uniE001]>
=> {"deleted" => 2, "skipped" => 0}
回傳欄位 說明
deleted 刪除的字符數
skipped 跳過的字符數
情況 結果
必備字 .notdef、space 跳過:{"deleted" => 0, "skipped" => 2}
空集合 {"deleted" => 0, "skipped" => 0}

刪除後「字符總覽」的選取會被清除。

錯誤處理

API 的錯誤是 Fontion::Error,可以用 rescue 攔截。

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

輸出:

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

沒有攔截時,輸出區以紅字顯示錯誤與呼叫位置,腳本中止:

eval_async:17:in 'Fontion.call': 縮放百分比須為大於 0 的數字 (Fontion::Error)
eval_async:79:in 'Fontion::GlyphSet#set_scale'
eval_async:1:in '<main>'
訊息 原因
碼位範圍不正確 select 的起點大於終點,或不是有效碼位
寬度須為大於 0 的數字 set_path_width 的參數
縮放百分比須為大於 0 的數字 set_scale 的 sx、sy
原點須為 [x, y] 兩個數字 set_scale 的 at:
圓角參數須為 0 以上的數字(smooth、equalize 為 0–100) round_corners 的參數
操作 ID 不正確 revert_filter 的 ID 是空字串
此操作已還原 revert_filter 的操作已還原過

Ruby 本身的錯誤也會顯示在輸出區,例如語法錯誤(SyntaxError)與呼叫不存在的方法(NoMethodError)。

組合範例

多個不連續範圍,一次設定寬度

font.select 一次只能選一段連續的碼位。把多次選取的 entries 合併,再包成一個 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

輸出:

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

三個字符中只有「川」有骨架路徑,所以改到 1 個字符、3 條路徑。

指定一串字,先縮放再設定寬度

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

輸出:

共 3 個字符
=> {"scale" => {"glyphs" => 3, "contours" => 6, "skipped" => 0}, "width" => {"glyphs" => 1, "contours" => 3, "skipped" => 0}}

依名稱篩選

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

輸出:

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

分段處理並彙總結果

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 字、%d 輪廓", range.first, range.last, r["glyphs"], r["contours"])
  r.each { |k, v| total[k] += v }
end
total

輸出:

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

套用圓角,結果超出預期就還原

set = font.select(0x4E00..0x4E8C)
r = set.round_corners(15, inner: 5)
puts "圓了 #{r["corners"]} 個角"
if r["corners"] > 6
  puts "超過預期,還原"
  p font.revert_filter(r["op"])
end
font.deselect

輸出:

圓了 8 個角
超過預期,還原
{"restored" => 1, "skippedEdited" => [], "skippedRange" => 0}
=> nil

刪除前先確認範圍

set = font.select(0xE000..0xF8FF)
p set          # 先確認要刪的字符
set.delete

輸出:

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

方法總表

方法 回傳
font.all GlyphSet
font.select(from, to = nil) GlyphSet
font.deselect nil
font.filter_history 陣列
font.revert_filter(op_id, force: false) {"restored", "skippedEdited", "skippedRange"}
GlyphSet#size、names、ids、entries 數字或陣列
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