summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--luaotfload-colors.lua60
1 files changed, 27 insertions, 33 deletions
diff --git a/luaotfload-colors.lua b/luaotfload-colors.lua
index 58089dc..5be0a1f 100644
--- a/luaotfload-colors.lua
+++ b/luaotfload-colors.lua
@@ -23,41 +23,37 @@ local registerotffeature = otffeatures.register
local add_color_callback --[[ this used to be a global‽ ]]
-local lpeg = require"lpeg"
-local lpegmatch = lpeg.match
+--[[doc--
+Color string validator / parser.
+--doc]]--
+
+local lpeg = require"lpeg"
+local lpegmatch = lpeg.match
local C, Cg, Ct, P, R, S = lpeg.C, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S
-local digit16 = R("09", "af", "AF")
-local octet = C(digit16 * digit16)
-local p_rgb = Cg(octet, "red")
- * Cg(octet, "green")
- * Cg(octet, "blue")
-local p_rgba = p_rgb * Cg(octet, "alpha")
-local p_color = Ct(p_rgba + p_rgb)
-
-local old_sanitize_color_expression = function (value)
- local sanitized
- if value then
- value = tostring(value)
- local n_value = #value
- if n_value == 6 or n_value == 8 then
- sanitized = value
- elseif n_value == 7 then --> take first six bytes
- _, _, sanitized = stringsub(value, 1, 6)
- elseif n_value > 8 then --> take first eight bytes
- _, _, sanitized = stringsub(value, 1, 8)
- else
- -- broken color code ignored, issue a warning?
- luaotfload.warning(
- "“%s” is not a valid rgb[a] color expression", value)
- end
- end
- return sanitized
-end
+local digit16 = R("09", "af", "AF")
+local octet = C(digit16 * digit16)
+
+local p_rgb = octet * octet * octet
+local p_rgba = p_rgb * octet
+local valid_digits = C(p_rgba + p_rgb) -- matches eight or six hex digits
+
+local p_Crgb = Cg(octet, "red") --- for captures
+ * Cg(octet, "green")
+ * Cg(octet, "blue")
+local p_Crgba = p_Crgb * Cg(octet, "alpha")
+local extract_color = Ct(p_Crgba + p_Crgb)
+--- string -> (string | nil)
local sanitize_color_expression = function (digits)
- local old = old_sanitize_color_expression(digits)
- return old
+ digits = tostring(digits)
+ local sanitized = lpegmatch(valid_digits, digits)
+ if not sanitized then
+ luaotfload.warning(
+ "“%s” is not a valid rgb[a] color expression", digits)
+ return nil
+ end
+ return sanitized
end
--[[doc--
@@ -71,8 +67,6 @@ end
--- value)
---
local function setcolor (tfmdata, value)
- print("~~~~~~~~~~~~~")
- print(value)
local sanitized = sanitize_color_expression(value)
local properties = tfmdata.properties