summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/font-imp-properties.lua
blob: 21b55aeb2b0e9ed7dd05ece2cd6c76e28361c16b (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
if not modules then modules = { } end modules ['font-imp-properties'] = {
    version   = 1.001,
    comment   = "companion to font-ini.mkiv and hand-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

if not context then return end

local next, type, tonumber, select = next, type, tonumber, select
local byte, find, formatters = string.byte, string.find, string.formatters
local utfchar = utf.char
local sortedhash, sortedkeys, sort = table.sortedhash, table.sortedkeys, table.sort
local insert = table.insert

local context            = context
local fonts              = fonts
local utilities          = utilities

local helpers            = fonts.helpers

local handlers           = fonts.handlers
local hashes             = fonts.hashes
local otf                = handlers.otf
local afm                = handlers.afm

local registerotffeature = otf.features.register
local registerafmfeature = afm.features.register

local fontdata           = hashes.identifiers
local fontproperties     = hashes.properties

local constructors       = fonts.constructors
local getprivate         = constructors.getprivate

local allocate           = utilities.storage.allocate

local setmetatableindex  = table.setmetatableindex

local implement          = interfaces.implement

do

    local P, lpegpatterns, lpegmatch  = lpeg.P, lpeg.patterns, lpeg.match

    local amount, stretch, shrink, extra

    local factor  = lpegpatterns.unsigned
    local space   = lpegpatterns.space
    local pattern = (
                                            (factor / function(n) amount  = tonumber(n) or amount  end)
        + (P("+") + P("plus" )) * space^0 * (factor / function(n) stretch = tonumber(n) or stretch end)
        + (P("-") + P("minus")) * space^0 * (factor / function(n) shrink  = tonumber(n) or shrink  end)
        + (         P("extra")) * space^0 * (factor / function(n) extra   = tonumber(n) or extra   end)
        + space^1
    )^1

    local function initialize(tfmdata,key,value)
        local characters = tfmdata.characters
        local parameters = tfmdata.parameters
        if type(value) == "string" then
            local emwidth = parameters.quad
            amount, stretch, shrink, extra = 0, 0, 0, false
            lpegmatch(pattern,value)
            if not extra then
                if shrink ~= 0 then
                    extra = shrink
                elseif stretch ~= 0 then
                    extra = stretch
                else
                    extra = amount
                end
            end
            parameters.space         = amount  * emwidth
            parameters.space_stretch = stretch * emwidth
            parameters.space_shrink  = shrink  * emwidth
            parameters.extra_space   = extra   * emwidth
        end
    end

    -- 1.1 + 1.2 - 1.3 minus 1.4 plus 1.1 extra 1.4 -- last one wins

    registerotffeature {
        name        = "spacing",
        description = "space settings",
        manipulators = {
            base = initialize,
            node = initialize,
        }
    }

end

do

    local function initialize(tfmdata,value)
        local properties = tfmdata.properties
        if properties then
            properties.identity = value == "vertical" and "vertical" or "horizontal"
        end
    end

    registerotffeature {
        name         = "identity",
        description  = "set font identity",
        initializers = {
            base = initialize,
            node = initialize,
        }
    }

    local function initialize(tfmdata,value)
        local properties = tfmdata.properties
        if properties then
            properties.writingmode = value == "vertical" and "vertical" or "horizontal"
        end
    end

    registerotffeature {
        name         = "writingmode",
        description  = "set font direction",
        initializers = {
            base = initialize,
            node = initialize,
        }
    }

end