summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-scripts.lua
blob: 814223b5ed5e1ef7f4a1a945861726e64c0df827 (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
88
89
90
91
-----------------------------------------------------------------------
--         FILE:  luaotfload-script.lua
--  DESCRIPTION:  part of luaotfload / script
-----------------------------------------------------------------------

local ProvidesLuaModule = { 
    name          = "luaotfload-script",
    version       = "3.12",       --TAGVERSION
    date          = "2020-02-02", --TAGDATE
    description   = "luaotfload submodule / Script helpers",
    license       = "CC0 1.0 Universal",
    author        = "Marcel Krüger"
}

if luatexbase and luatexbase.provides_module then
  luatexbase.provides_module (ProvidesLuaModule)
end  

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,
  },
}