Debian Codegraph @

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

raw symbols SCIP
1 local harfbuzz = require("harfbuzz")
2
3 local compare_glyphs_against_fixture = function(glyphs, fixture)
4 local json = require('dkjson')
5 local f = io.open("fixtures/"..fixture)
6 local s = f:read("*all")
7 f:close()
8 local hb_shape_glyphs = json.decode(s)
9 assert.are_equal(#hb_shape_glyphs, #glyphs)
10 for c = 1, #glyphs do
11 local g = glyphs[c]
12 local h = hb_shape_glyphs[c]
13 assert.are_equal(h.g, g.codepoint)
14 assert.are_equal(h.cl, g.cluster)
15 assert.are_equal(h.ax, g.x_advance)
16 assert.are_equal(h.ay, g.y_advance)
17 assert.are_equal(h.dx, g.x_offset)
18 assert.are_equal(h.dy, g.y_offset)
19 assert.are_equal(h.fl, g.flags)
20 end
21 end
22
23
24 describe("harfbuzz module shaping functions", function()
25 local face = harfbuzz.Face.new('fonts/notonastaliq.ttf')
26 local font = harfbuzz.Font.new(face)
27 local urdu_text = "یہ" -- U+06CC U+06C1
28
29 it("can take a buffer and font and shape it, with output matching hb-shape", function()
30 local buf = harfbuzz.Buffer.new()
31 buf:add_utf8(urdu_text)
32
33 harfbuzz.shape(font, buf)
34 local glyphs = buf:get_glyphs()
35 assert.True(#glyphs > 0)
36
37 -- Compare against output of hb-shape
38 compare_glyphs_against_fixture(glyphs, 'notonastaliq_U06CC_U06C1.json')
39 end)
40
41 it("can take a buffer, font and an options table with script, language and direction settings.", function()
42 local buf = harfbuzz.Buffer.new()
43 buf:add_utf8(urdu_text)
44
45 harfbuzz.shape(font, buf, { language = harfbuzz.Language.new("urd"), script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.HB_DIRECTION_RTL })
46 local glyphs = buf:get_glyphs()
47 assert.True(#glyphs > 0)
48
49 -- Compare against output of hb-shape
50 compare_glyphs_against_fixture(glyphs, 'notonastaliq_U06CC_U06C1.json')
51 end)
52
53 it("can take codepoints, font and an options table with script, language and direction settings. #mac", function()
54 local buf = harfbuzz.Buffer.new()
55 local korean_text = { 0xAC00, 0xB098, 0xB2E4 }
56 buf:add_codepoints(korean_text)
57
58 local face_korean = harfbuzz.Face.new('/Library/Fonts/AppleGothic.ttf')
59 local font_korean = harfbuzz.Font.new(face_korean)
60
61 harfbuzz.shape(font_korean, buf, { language = harfbuzz.Language.new("KOR"), script = harfbuzz.Script.new("hang"), direction = harfbuzz.Direction.HB_DIRECTION_LTR })
62 local glyphs = buf:get_glyphs()
63 assert.True(#glyphs > 0)
64
65 -- Compare against output of hb-shape
66 compare_glyphs_against_fixture(glyphs, 'AppleGothic_korean_issue_22.json')
67 end)
68
69 it("can take a string containing a comma-delimited list of valid features", function()
70 local buf = harfbuzz.Buffer.new()
71 buf:add_utf8(urdu_text)
72
73 harfbuzz.shape(font, buf, { language = harfbuzz.Language.new("urd"), script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.HB_DIRECTION_RTL, features = "+kern,smcp" })
74 local glyphs = buf:get_glyphs()
75 assert.True(#glyphs > 0)
76 end)
77
78 describe("features option", function()
79 local buf
80 local options
81
82 before_each(function()
83 buf= harfbuzz.Buffer.new()
84 buf:add_utf8(urdu_text)
85 options = { language = harfbuzz.Language.new("urd"), script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.HB_DIRECTION_RTL }
86 end)
87
88 it("can take a table containing a valid features", function()
89 options.features = {
90 harfbuzz.Feature.new('+kern'),
91 harfbuzz.Feature.new('smcp')
92 }
93
94 harfbuzz.shape(font, buf, options)
95 local glyphs = buf:get_glyphs()
96 assert.True(#glyphs > 0)
97 end)
98
99 it("throws an error if feature string is invalid", function()
100 options.features = "#kern"
101 assert.has_error(function()
102 harfbuzz.shape(font, buf, options)
103 end, "Invalid feature string: '#kern'")
104 end)
105
106 it("throws an error if feature option is not a table or string", function()
107 options.features = 25
108 assert.has_error(function()
109 harfbuzz.shape(font, buf, options)
110 end, "Invalid features option")
111 end)
112
113 it("throws an error if features table does not contain a feature", function()
114 options.features = {
115 harfbuzz.Feature.new('+kern'),
116 25,
117 harfbuzz.Feature.new('smcp')
118 }
119 assert.has_error(function()
120 harfbuzz.shape(font, buf, options)
121 end, "bad argument #-1 to 'shape_full' (harfbuzz.Feature expected, got number)")
122 end)
123
124 it("shapes a string appropriately with the features turned on",function()
125 buf= harfbuzz.Buffer.new()
126 buf:add_utf8("123")
127 local opts = { language = harfbuzz.Language.new("eng"), script = harfbuzz.Script.new("Latn"), direction = harfbuzz.Direction.HB_DIRECTION_LTR }
128 local amiri_face = harfbuzz.Face.new('fonts/amiri-regular.ttf')
129 local amiri_font = harfbuzz.Font.new(amiri_face)
130
131 -- Check normal shaping w/o features
132 buf= harfbuzz.Buffer.new()
133 buf:add_utf8("123")
134
135 harfbuzz.shape(amiri_font, buf, opts)
136 local glyphs = buf:get_glyphs()
137 compare_glyphs_against_fixture(glyphs, "amiri-regular_123.json")
138
139 -- Check shaping with '+numr'
140 buf= harfbuzz.Buffer.new()
141 buf:add_utf8("123")
142 opts.features = "+numr"
143 harfbuzz.shape(amiri_font, buf, opts)
144 glyphs = buf:get_glyphs()
145 compare_glyphs_against_fixture(glyphs, "amiri-regular_123_numr.json")
146 end)
147
148 it("can set specefic shaper",function()
149 options.shapers = { "fallback"}
150 harfbuzz.shape(font, buf, options)
151 local glyphs = buf:get_glyphs()
152 assert.are_equal(2, #glyphs)
153 assert.are_equal(906, glyphs[1].codepoint)
154 assert.are_equal(909, glyphs[2].codepoint)
155 end)
156 end)
157 end)