Debian Codegraph @

texk/web2c/luatexdir/luaharfbuzz/spec/buffer_spec.lua

raw symbols SCIP
1 local harfbuzz = require("harfbuzz")
2
3 describe("harfbuzz.Buffer", function()
4 it("can be created", function()
5 harfbuzz.Buffer.new()
6 end)
7
8 it("can add a single codepoints with explicit cluster value", function()
9 local b = harfbuzz.Buffer.new()
10 b:add(0x06CC, 42)
11 local glyphs = b:get_glyphs()
12 assert.are_equal(#glyphs, 1)
13 assert.are_equal(glyphs[1].cluster, 42)
14 assert.are_equal(glyphs[1].codepoint, 0x06CC)
15 end)
16
17 it("can add a UTF8 string", function()
18 local b = harfbuzz.Buffer.new()
19 local s = "Some String"
20 b:add_utf8(s)
21 assert.are_equal(string.len(s), b:get_length())
22 end)
23
24 it("can add a UTF 8 string with item_offset", function()
25 local b = harfbuzz.Buffer.new()
26 local s = "Some String"
27 local o = 5
28 b:add_utf8(s,o)
29 assert.are_equal(string.len(s) - o, b:get_length())
30 end)
31
32 it("can add a UTF 8 string with item_length", function()
33 local b = harfbuzz.Buffer.new()
34 local s = "Some String"
35 local o = 5
36 local l = 2
37 b:add_utf8(s,o,l)
38 assert.are_equal(l, b:get_length())
39 end)
40
41 it("can add codepoints", function()
42 local b = harfbuzz.Buffer.new()
43 local s = { 0x06CC, 0x06C1 }
44 b:add_codepoints(s)
45 assert.are_equal(#s, b:get_length())
46 end)
47
48 it("can add codepoints with item_offset", function()
49 local b = harfbuzz.Buffer.new()
50 local s = { 0x06CC, 0x06C1 }
51 local o = 1
52 b:add_codepoints(s,o)
53 assert.are_equal(#s - o, b:get_length())
54 end)
55
56 it("can add codepoints with item_length", function()
57 local b = harfbuzz.Buffer.new()
58 local s = { 0x06CC, 0x06C1 }
59 local o = 1
60 local l = 1
61 b:add_codepoints(s,o,l)
62 assert.are_equal(l, b:get_length())
63 end)
64
65 it("can call guess_segment_properties", function()
66 local b = harfbuzz.Buffer.new()
67 b:add_utf8("Some String")
68 b:guess_segment_properties()
69 end)
70
71 it("can get and set the direction of a buffer", function()
72 local b = harfbuzz.Buffer.new()
73 b:add_utf8("abc")
74 local dir = harfbuzz.Direction.RTL
75 b:set_direction(dir)
76 assert.are_equal(dir, b:get_direction())
77 end)
78
79 it("sets direction to INVALID if direction is invalid", function()
80 local b = harfbuzz.Buffer.new()
81 b:set_direction(harfbuzz.Direction.new("invalid"))
82 assert.are_equal(harfbuzz.Direction.INVALID, b:get_direction())
83 end)
84
85 it("can get the direction correctly", function()
86 local b = harfbuzz.Buffer.new()
87 b:add_utf8("یہ")
88 b:guess_segment_properties()
89 assert.are_equal(harfbuzz.Direction.RTL, b:get_direction())
90 end)
91
92 it("can get and set the language of a buffer", function()
93 local b = harfbuzz.Buffer.new()
94 b:add_utf8("یہ")
95 local urd = harfbuzz.Language.new("urd")
96 b:set_language(urd)
97 assert.are_equal(urd, b:get_language())
98 end)
99
100 it("Sets language to INVALID if language is invalid", function()
101 local b = harfbuzz.Buffer.new()
102 b:set_language(harfbuzz.Language.INVALID)
103 assert.are_equal(harfbuzz.Language.INVALID, b:get_language())
104 end)
105
106 it("can get the language correctly", function()
107 local b = harfbuzz.Buffer.new()
108 b:add_utf8("یہ")
109 b:guess_segment_properties()
110 assert.are_not_equal(harfbuzz.Language.INVALID, b:get_language())
111 end)
112
113 it("can get and set the script of a buffer", function()
114 local b = harfbuzz.Buffer.new()
115 b:add_utf8("abc")
116 b:set_script(harfbuzz.Script.new("latn"))
117 assert.are_equal("Latn", tostring(b:get_script()))
118 end)
119
120 it("returns script as UNKNOWN if script is invalid", function()
121 local b = harfbuzz.Buffer.new()
122 b:set_script(harfbuzz.Script.new("xxx"))
123 assert.are_equal(harfbuzz.Script.UNKNOWN, b:get_script())
124 end)
125
126 it("can get the script correctly", function()
127 local b = harfbuzz.Buffer.new()
128 b:add_utf8("یہ")
129 assert.are_equal(harfbuzz.Script.new(""), b:get_script())
130 b:guess_segment_properties()
131 assert.are_equal(harfbuzz.Script.new("Arab"), b:get_script())
132 end)
133
134 it("can reverse the buffer", function()
135 local face = harfbuzz.Face.new('fonts/notonastaliq.ttf')
136 local font = harfbuzz.Font.new(face)
137 local urdu_text = "یہ" -- U+06CC U+06C1
138 local options = { language = harfbuzz.Language.new("urd"), script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.RTL }
139
140 local buf= harfbuzz.Buffer.new()
141 buf:add_utf8(urdu_text)
142 harfbuzz.shape(font, buf, options)
143 local orig_glyphs = buf:get_glyphs()
144 buf:reverse()
145 local reversed_glyphs = buf:get_glyphs()
146
147 assert.are_equal(#orig_glyphs, #reversed_glyphs)
148
149 for c = 1, #orig_glyphs do
150 local g = orig_glyphs[#orig_glyphs - (c - 1)]
151 local r = reversed_glyphs[c]
152 assert.are_equal(g.codepoint, r.codepoint)
153 assert.are_equal(g.cluster, r.cluster)
154 assert.are_equal(g.x_advance, r.x_advance)
155 assert.are_equal(g.y_advance, r.y_advance)
156 assert.are_equal(g.x_offset, r.x_offset)
157 assert.are_equal(g.y_offset, r.y_offset)
158 end
159
160 end)
161
162 it("can get the length of the buffer", function()
163 local b = harfbuzz.Buffer.new()
164 local s = "some string"
165 b:add_utf8(s)
166 assert.are_equal(string.len(s), b:get_length())
167 end)
168
169 it("can get the cluster level of the buffer", function()
170 local b = harfbuzz.Buffer.new()
171 assert.are_equal(harfbuzz.Buffer.CLUSTER_LEVEL_DEFAULT, b:get_cluster_level())
172 end)
173
174 it("can set the cluster level of the buffer", function()
175 local b = harfbuzz.Buffer.new()
176 b:set_cluster_level(harfbuzz.Buffer.CLUSTER_LEVEL_CHARACTERS)
177 assert.are_equal(harfbuzz.Buffer.CLUSTER_LEVEL_CHARACTERS, b:get_cluster_level())
178 end)
179
180 it("can get the replacement glyph for invisible characters of the buffer", function()
181 local b = harfbuzz.Buffer.new()
182 assert.are_equal(0, b:get_invisible_glyph())
183 end)
184
185 it("can set the replacement glyph for invisible characters of the buffer", function()
186 local b = harfbuzz.Buffer.new()
187 b:set_invisible_glyph(3)
188 assert.are_equal(3, b:get_invisible_glyph())
189 end)
190
191 it("can get the replacement codepoint of the buffer", function()
192 local b = harfbuzz.Buffer.new()
193 assert.are_equal(0xFFFD, b:get_replacement_codepoint())
194 end)
195
196 it("can set the replacement codepoint of the buffer", function()
197 local b = harfbuzz.Buffer.new()
198 b:set_replacement_codepoint(0xFFFF)
199 assert.are_equal(0xFFFF, b:get_replacement_codepoint())
200 b:add_utf8("\xFF")
201 local glyphs = b:get_glyphs()
202 assert.are_equal(1, #glyphs)
203 assert.are_equal(0xFFFF, glyphs[1].codepoint)
204 end)
205
206 it("can get the flags of the buffer", function()
207 local b = harfbuzz.Buffer.new()
208 assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT, b:get_flags())
209 end)
210
211 it("can set the flags of the buffer", function()
212 local b = harfbuzz.Buffer.new()
213 b:set_flags(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT)
214 assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT, b:get_flags())
215 end)
216
217 it("can clear the buffer", function()
218 local b = harfbuzz.Buffer.new()
219 b:add_utf8("abc")
220 b:set_flags(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT)
221 b:clear_contents()
222 assert.are_equal(0, b:get_length())
223 assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT, b:get_flags())
224 end)
225
226 it("can reset the buffer", function()
227 local b = harfbuzz.Buffer.new()
228 b:add_utf8("abc")
229 b:set_flags(harfbuzz.Buffer.FLAG_DEFAULT | harfbuzz.Buffer.FLAG_BOT)
230 b:reset()
231 assert.are_equal(0, b:get_length())
232 assert.are_equal(harfbuzz.Buffer.FLAG_DEFAULT, b:get_flags())
233 end)
234 end)
235
236