summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luaharfbuzz/spec/buffer_spec.lua
blob: 503b902c9dc650aa28c838c916a936a13db68945 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
local harfbuzz = require("harfbuzz")

describe("harfbuzz.Buffer", function()
  it("can be created", function()
    harfbuzz.Buffer.new()
  end)

  it("can add a single codepoints with explicit cluster value", function()
    local b = harfbuzz.Buffer.new()
    b:add(0x06CC, 42)
    local glyphs = b:get_glyphs()
    assert.are_equal(#glyphs, 1)
    assert.are_equal(glyphs[1].cluster, 42)
    assert.are_equal(glyphs[1].codepoint, 0x06CC)
  end)

  it("can add a UTF8 string", function()
    local b = harfbuzz.Buffer.new()
    local s = "Some String"
    b:add_utf8(s)
    assert.are_equal(string.len(s), b:get_length())
  end)

  it("can add a UTF 8 string with item_offset", function()
    local b = harfbuzz.Buffer.new()
    local s = "Some String"
    local o = 5
    b:add_utf8(s,o)
    assert.are_equal(string.len(s) - o, b:get_length())
  end)

  it("can add a UTF 8 string with item_length", function()
    local b = harfbuzz.Buffer.new()
    local s = "Some String"
    local o = 5
    local l = 2
    b:add_utf8(s,o,l)
    assert.are_equal(l, b:get_length())
  end)

  it("can add codepoints", function()
    local b = harfbuzz.Buffer.new()
    local s = { 0x06CC, 0x06C1 }
    b:add_codepoints(s)
    assert.are_equal(#s, b:get_length())
  end)

  it("can add codepoints with item_offset", function()
    local b = harfbuzz.Buffer.new()
    local s = { 0x06CC, 0x06C1 }
    local o = 1
    b:add_codepoints(s,o)
    assert.are_equal(#s - o, b:get_length())
  end)

  it("can add codepoints with item_length", function()
    local b = harfbuzz.Buffer.new()
    local s = { 0x06CC, 0x06C1 }
    local o = 1
    local l = 1
    b:add_codepoints(s,o,l)
    assert.are_equal(l, b:get_length())
  end)

  it("can call guess_segment_properties", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("Some String")
    b:guess_segment_properties()
  end)

  it("can get and set the direction of a buffer", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("abc")
    local dir = harfbuzz.Direction.RTL
    b:set_direction(dir)
    assert.are_equal(dir, b:get_direction())
  end)

  it("sets direction to INVALID if direction is invalid", function()
    local b = harfbuzz.Buffer.new()
    b:set_direction(harfbuzz.Direction.new("invalid"))
    assert.are_equal(harfbuzz.Direction.INVALID, b:get_direction())
  end)

  it("can get the direction correctly", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("یہ")
    b:guess_segment_properties()
    assert.are_equal(harfbuzz.Direction.RTL, b:get_direction())
  end)

  it("can get and set the language of a buffer", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("یہ")
    local urd = harfbuzz.Language.new("urd")
    b:set_language(urd)
    assert.are_equal(urd, b:get_language())
  end)

  it("Sets language to INVALID if language is invalid", function()
    local b = harfbuzz.Buffer.new()
    b:set_language(harfbuzz.Language.INVALID)
    assert.are_equal(harfbuzz.Language.INVALID, b:get_language())
  end)

  it("can get the language correctly", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("یہ")
    b:guess_segment_properties()
    assert.are_not_equal(harfbuzz.Language.INVALID, b:get_language())
  end)

  it("can get and set the script of a buffer", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("abc")
    b:set_script(harfbuzz.Script.new("latn"))
    assert.are_equal("Latn", tostring(b:get_script()))
  end)

  it("returns script as UNKNOWN if script is invalid", function()
    local b = harfbuzz.Buffer.new()
    b:set_script(harfbuzz.Script.new("xxx"))
    assert.are_equal(harfbuzz.Script.UNKNOWN, b:get_script())
  end)

  it("can get the script correctly", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("یہ")
    assert.are_equal(harfbuzz.Script.new(""), b:get_script())
    b:guess_segment_properties()
    assert.are_equal(harfbuzz.Script.new("Arab"), b:get_script())
  end)

  it("can reverse the buffer", function()
    local face = harfbuzz.Face.new('fonts/notonastaliq.ttf')
    local font = harfbuzz.Font.new(face)
    local urdu_text = "یہ" -- U+06CC U+06C1
    local options = { language = harfbuzz.Language.new("urd"), script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.RTL }

    local buf= harfbuzz.Buffer.new()
    buf:add_utf8(urdu_text)
    harfbuzz.shape(font, buf, options)
    local orig_glyphs = buf:get_glyphs()
    buf:reverse()
    local reversed_glyphs = buf:get_glyphs()

    assert.are_equal(#orig_glyphs, #reversed_glyphs)

    for c = 1, #orig_glyphs do
      local g = orig_glyphs[#orig_glyphs - (c - 1)]
      local r = reversed_glyphs[c]
      assert.are_equal(g.codepoint, r.codepoint)
      assert.are_equal(g.cluster, r.cluster)
      assert.are_equal(g.x_advance, r.x_advance)
      assert.are_equal(g.y_advance, r.y_advance)
      assert.are_equal(g.x_offset, r.x_offset)
      assert.are_equal(g.y_offset, r.y_offset)
    end

  end)

  it("can get the length of the buffer", function()
    local b = harfbuzz.Buffer.new()
    local s = "some string"
    b:add_utf8(s)
    assert.are_equal(string.len(s), b:get_length())
  end)

  it("can get the cluster level of the buffer", function()
    local b = harfbuzz.Buffer.new()
    assert.are_equal(harfbuzz.Buffer.CLUSTER_LEVEL_DEFAULT, b:get_cluster_level())
  end)

  it("can set the cluster level of the buffer", function()
    local b = harfbuzz.Buffer.new()
    b:set_cluster_level(harfbuzz.Buffer.CLUSTER_LEVEL_CHARACTERS)
    assert.are_equal(harfbuzz.Buffer.CLUSTER_LEVEL_CHARACTERS, b:get_cluster_level())
  end)

  it("can get the replacement glyph for invisible characters of the buffer", function()
    local b = harfbuzz.Buffer.new()
    assert.are_equal(0, b:get_invisible_glyph())
  end)

  it("can set the replacement glyph for invisible characters of the buffer", function()
    local b = harfbuzz.Buffer.new()
    b:set_invisible_glyph(3)
    assert.are_equal(3, b:get_invisible_glyph())
  end)

  it("can get the replacement codepoint of the buffer", function()
    local b = harfbuzz.Buffer.new()
    assert.are_equal(0xFFFD, b:get_replacement_codepoint())
  end)

  it("can set the replacement codepoint of the buffer", function()
    local b = harfbuzz.Buffer.new()
    b:set_replacement_codepoint(0xFFFF)
    assert.are_equal(0xFFFF, b:get_replacement_codepoint())
    b:add_utf8("\xFF")
    local glyphs = b:get_glyphs()
    assert.are_equal(1, #glyphs)
    assert.are_equal(0xFFFF, glyphs[1].codepoint)
  end)

  it("can get the flags of the buffer", function()
    local b = harfbuzz.Buffer.new()
    assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT, b:get_flags())
  end)

  it("can set the flags of the buffer", function()
    local b = harfbuzz.Buffer.new()
    b:set_flags(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT)
    assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT, b:get_flags())
  end)

  it("can clear the buffer", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("abc")
    b:set_flags(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT)
    b:clear_contents()
    assert.are_equal(0, b:get_length())
    assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT, b:get_flags())
  end)

  it("can reset the buffer", function()
    local b = harfbuzz.Buffer.new()
    b:add_utf8("abc")
    b:set_flags(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT)
    b:reset()
    assert.are_equal(0, b:get_length())
    assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT, b:get_flags())
  end)
end)