summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/luaotfload/luaotfload-scripts.lua
blob: 8a4f51ea5898beebdeaf6df9e664caa300b648e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-----------------------------------------------------------------------
--         FILE:  luaotfload-script.lua
--  DESCRIPTION:  part of luaotfload / script
-----------------------------------------------------------------------

assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { 
    name          = "luaotfload-script",
    version       = "3.18",       --TAGVERSION
    date          = "2021-05-21", --TAGDATE
    description   = "luaotfload submodule / Script helpers",
    license       = "CC0 1.0 Universal",
    author        = "Marcel Krüger"
}

local canonical_name = {
  dflt = "DFLT",
  hira = "kana",
  laoo = "lao",
  yiii = "yi",
  nkoo = "nko",
  yaii = "vai",
  ["lao "] = "lao",
  ["yi  "] = "yi",
  ["nko "] = "nko",
  ["vai "] = "vai",
}
local versioned_script = {
  mym = "mymr", mymr = "mym",
  bng = "beng", beng = "bng",
  dev = "deva", deva = "dev",
  gjr = "gujr", gujr = "gjr",
  gur = "guru", guru = "gur",
  knd = "knda", knda = "knd",
  mlm = "mlym", mlym = "mlm",
  ory = "orya", orya = "ory",
  tml = "taml", taml = "tml",
  tel = "telu", telu = "tel",
}
local function get_versioned(original)
  local base = original:gsub("%d$", "") -- Strip any existing version
  local versioned = versioned_script[base]
  if not versioned then
    return original
  end
  if #base == 3 then
    local t = base
    base = versioned
    versioned = t
  end
  if base == "mymr" then
    return "mym2", "mymr"
  end
  return versioned .. '3', versioned .. '2', base
end

-- We never return trailing spaces because I consider them implementation details.
local function script_to_ot(iso)
  iso = iso:lower()
  return get_versioned(canonical_name[iso] or iso)
end

local function script_to_iso(tag)
  tag = tag:lower()
  tag = canonical_name[tag] or tag
  local stripped, did_strip = tag:gsub("%d$", "")
  tag = did_strip == 1 and versioned_script[stripped] or tag
  local tag_length = #tag
  if tag_length == 4 then return tag end -- Optimization for common case
  -- I promise you, I am not making this one up
  return tag .. string.rep(tag:sub(tag_length, tag_length), 4-tag_length)
end

local function to_harfbuzz(script, language)
  local otscript = script_to_iso(script)
  -- if script_to_ot(otscript) == script then
  --   return otscript, language
  -- end
  return otscript, "x-hbot" .. language .. "-hbsc" .. script
end

return {
  to_harfbuzz = to_harfbuzz,
  script = {
    to_ot = script_to_ot,
    to_iso = script_to_iso,
  },
}