summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luaotfload/luaotfload-t3.lua
blob: 215ba4b918ba3cfce944ce461465d493fdfb5df7 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
-----------------------------------------------------------------------
--         FILE:  luaotfload-t3.lua
--  DESCRIPTION:  part of luaotfload / Type 3
-----------------------------------------------------------------------
do -- block to avoid to many local variables error
 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") { 
     name          = "luaotfload-t3",
     version       = "3.22",       --TAGVERSION
     date          = "2022-06-15", --TAGDATE
     description   = "luaotfload submodule / Type 3 font handling",
     license       = "GPL v2.0",
     author        = "Marcel Krüger",
     copyright     = "Luaotfload Development Team",     
 }
end

--[[
local function u8_to_utf16be(s)
  local d = {}
  local i = 0
  for _, cp, cp2 in utf8.codes(s) do
    i = i + 1
    if cp > 0x10000 then
      cp = cp - 0x10000
      local high = (cp>>10) | 0xD800
      local low = (cp&0x3FF) | 0xDC00
      d[i] = string.format('%04X%04X', high, low)
    else
      d[i] = string.format('%04X', cp)
    end
  end
  return table.concat(d, '', 1, i)
end
]]

local function t3factory(basename, designsize, callback)
  local count = 0
  local gid_to_t3 = {}
  local t3_to_gid = {}
  return function(size)
    local fids = {}
    return function(gid, width, unicode)
      local t3id = gid_to_t3[gid]
      if not t3id then
        t3id = count
        count = count + 1
        gid_to_t3[gid] = t3id
        t3_to_gid[t3id] = gid
      end
      local t3fid, t3cid = t3id >> 8, ~(t3id & 0xFF)
      local fid = fids[t3fid + 1]
      local fontdir = fid and font.getfont(fid)
      local characters = fontdir and fontdir.characters
      if not (characters and characters[t3cid]) then
        characters = characters or {}
        -- First create the character
        local char = {
          width = width,
          height = 0, -- We never look at these two anyway
          depth = 0,
          tounicode = unicode,
        }
        characters[t3cid] = char
        if fid then -- Font already exists
          font.addcharacters(fid, {
            characters = {
              [t3cid] = char,
            },
          })
        else
          fontdir = {
            name = basename .. '_' .. basesize .. '_' .. t3fid,
            format = 'type3',
            psname = 'none',
            filename = 'not_a_real_filename', -- Can't be null to ensure reuse
            fullname = basename .. basesize .. '_' .. t3fid,
            characters = characters,
            designsize = basesize,
            size = size,
            cidinfo = {}, -- Can't be null to ensure reuse
            attributes = '/FontDescriptor<<\z
              /Type/FontDescriptor\z
              /FontName/VirtualLuaFont\z
              /Flags 4\z
              /ItalicAngle 0\z
            >>', -- TODO: Needs work
            t3_handler = function(stage, _, cid)
              cid = cid and assert(t3_to_gid[(t3fid << 8) | ~cid])
              return callback(stage, cid)
            end
          }
          fid = font.define(fontdir)
          fids[t3fid + 1] = fid
        end
      end
      vf.font(fid)
      vf.char(t3cid)
    end
  end
end

luatexbase.add_to_callback('provide_charproc_data', function(stage, fid, ...)
  local fontdir = font.getfont(fid)
  if not fontdir then error'Unknown font' end
  local handler = fontdir.t3_handler
  if not handler then error'Missing t3handler in Type 3 font' end
  return handler(stage, fid, ...)
end, 'luaotfload.t3')

return t3factory
--[===[
          function callback(stage, cid)
            if stage == 1 then
                local char = assert(characters[cid])
                local head = assert(char.head)
                local resource = tex.saveboxresource(head, nil--[[attributes]], nil--[[resources]], true--[[immediate]], 0--[[mode]], nil--[[margin]])
                local name = string.format('/F%i', pdf.getxformname(resource))
                local resources = characters.resources
                if resources then
                  characters.resources = string.format('%s %s %i 0 R', resources, name, resource)
                else
                  characters.resources = string.format('%s %i 0 R', name, resource)
                end
                char.resource = name
              elseif stage == 2 then
                local char = assert(characters[cid])
                width = char.width/65781
                return pdf.obj{
                  type = 'stream',
                  immediate = true,
                  -- objnum =
                  -- attr =
                  -- compresslevel =
                  string = string.format('%g 0 d0 %s Do', width, char.resource)
                }, width
              elseif stage == 3 then
                return 65781/655360, string.format('/XObject<<%s>>', characters.resources)
              end
              print(stage, fid, cid)
            end,
          }
]===]