Debian Codegraph @

texmf-dist/doc/luatex/base/luatex-harfbuzz.tex

raw
1 % language=uk
2
3 \environment luatex-style
4
5 \startcomponent luatex-harfbuzz
6
7 \definelayer
8 [hbpage]
9 [width=\paperwidth,
10 height=\paperheight]
11
12
13 \setupbackgrounds
14 [page]
15 [background=hbpage,
16 state=start]
17
18
19 \startchapter[reference=harfbuzz,title={The HarfBuzz libraries}]
20
21
22 \startsection[title={The \type {luaharfbuzz} library}][library=luaharfbuzz]
23
24 The documentation of the library is a raw "html-to-pdf"
25 rendering of the API at \type{https://harfbuzz.github.io/luaharfbuzz};
26 the example is almost the verbatim copy \type{core_type.lua}.
27 \page
28
29
30 \dorecurse{15}{%
31 \setlayer[hbpage][x=0mm,y=0mm,]{\externalfigure[graphics/luaharfbuzz-Documentation.pdf][page=\recurselevel,scale=950]}
32 \blank
33 \page[empty]
34 }
35
36 \dorecurse{3}{%
37 \setlayer[hbpage][x=0mm,y=0mm,]{\externalfigure[graphics/luaharfbuzz-Documentation-subset.pdf][page=\recurselevel,scale=950]}
38 \blank
39 \page[empty]
40 }
41
42
43 \startsubsection[title={Example}]
44
45 The example is (a small modification of) the file \type{core_types.lua}
46 and it requires the file \type{harfbuzz.lua}; both are
47 under \type{luaharfbuzz} folder of the source code.
48 The fonts \type{notonastaliq.ttf} and \type{amiri-regular.ttf'} are under
49 \type{luaharfbuzz/fonts}.
50
51
52 \blank[3*big]
53
54 \startbuffer[hbmod]
55 local hb = require("luaharfbuzz")
56
57 -- special tags
58 hb.Tag.NONE = hb.Tag.new()
59
60 -- special script codes (ISO 15924)
61 hb.Script.COMMON = hb.Script.new("Zyyy")
62 hb.Script.INHERITED = hb.Script.new("Zinh")
63 hb.Script.UNKNOWN = hb.Script.new("Zzzz")
64 hb.Script.INVALID = hb.Script.from_iso15924_tag(hb.Tag.NONE)
65
66 -- directions
67 hb.Direction.INVALID = hb.Direction.new("invalid")
68 hb.Direction.LTR = hb.Direction.new("ltr")
69 hb.Direction.RTL = hb.Direction.new("rtl")
70 hb.Direction.TTB = hb.Direction.new("ttb")
71 hb.Direction.BTT = hb.Direction.new("btt")
72
73 -- special languages
74 hb.Language.INVALID = hb.Language.new()
75
76 hb.shape = function(font, buf, options)
77 options = options or { }
78
79 -- Apply options to buffer if they are set.
80 if options.language then buf:set_language(options.language) end
81 if options.script then buf:set_script(options.script) end
82 if options.direction then buf:set_direction(options.direction) end
83
84 -- Guess segment properties, in case all steps above have failed
85 -- to set the right properties.
86 buf:guess_segment_properties()
87
88 local features = {}
89
90 -- Parse features
91 if type(options.features) == "string" then
92 for fs in string.gmatch(options.features, '([^,]+)') do
93 local feature = hb.Feature.new(fs)
94 if feature then
95 table.insert(features, hb.Feature.new(fs))
96 else
97 error(string.format("Invalid feature string: '%s'", fs))
98 end
99 end
100 elseif type(options.features) == "table" then
101 features = options.features
102 elseif options.features then -- non-nil but not a string or table
103 error("Invalid features option")
104 end
105
106 return hb.shape_full(font,buf,features,options.shapers or {})
107 end
108
109 -- For backward compatibility
110 hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES = hb.Buffer.CLUSTER_LEVEL_MONOTONE_GRAPHEMES
111 hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS = hb.Buffer.CLUSTER_LEVEL_MONOTONE_CHARACTERS
112 hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_CHARACTERS = hb.Buffer.CLUSTER_LEVEL_CHARACTERS
113 hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_DEFAULT = hb.Buffer.CLUSTER_LEVEL_DEFAULT
114
115 hb.Tag.HB_TAG_NONE = hb.Tag.NONE
116
117 hb.Script.HB_SCRIPT_COMMON = hb.Script.COMMON
118 hb.Script.HB_SCRIPT_INHERITED = hb.Script.INHERITED
119 hb.Script.HB_SCRIPT_UNKNOWN = hb.Script.UNKNOWN
120 hb.Script.HB_SCRIPT_INVALID = hb.Script.INVALID
121
122 hb.Language.HB_LANGUAGE_INVALID = hb.Language.INVALID
123
124 hb.Direction.HB_DIRECTION_INVALID = hb.Direction.INVALID
125 hb.Direction.HB_DIRECTION_LTR = hb.Direction.LTR
126 hb.Direction.HB_DIRECTION_RTL = hb.Direction.RTL
127 hb.Direction.HB_DIRECTION_TTB = hb.Direction.TTB
128 hb.Direction.HB_DIRECTION_BTT = hb.Direction.BTT
129
130 hb.Direction.HB_DIRECTION_IS_VALID = hb.Direction.is_valid
131 hb.Direction.HB_DIRECTION_IS_HORIZONTAL = hb.Direction.is_horizontal
132 hb.Direction.HB_DIRECTION_IS_VERTICAL = hb.Direction.is_vertical
133 hb.Direction.HB_DIRECTION_IS_FORWARD = hb.Direction.is_forward
134 hb.Direction.HB_DIRECTION_IS_BACKWARD = hb.Direction.is_backward
135
136 hb.Buffer.get_glyph_infos_and_positions = hb.Buffer.get_glyphs
137
138 return hb
139
140 \stopbuffer
141
142 \blank[3*big]
143
144 The file \type{harfbuzz.lua}:
145 \typebuffer[hbmod]
146
147
148 \startbuffer[hbex]
149 local harfbuzz = require('harfbuzz')
150
151 -- Harfbuzz API Version
152 print("Harfbuzz API version", harfbuzz.version())
153
154 -- Shapers available
155 print("Shapers:", harfbuzz.shapers())
156
157
158 -- harfbuzz.Face
159 local face = harfbuzz.Face.new('notonastaliq.ttf')
160 print('\nFace upem = '..face:get_upem())
161
162 -- harfbuzz.Font
163 local font = harfbuzz.Font.new(face)
164 local xs, xy = font:get_scale()
165 print("\nDefault font scale = X: "..xs..", Y: "..xy)
166
167 -- harfbuzz.Buffer
168 local text = "یہ" -- U+06CC U+06C1
169 local buf = harfbuzz.Buffer.new()
170 buf:add_utf8(text)
171
172 -- harfbuzz.shape (Shapes text)
173 print("\nShaping '"..text.."' set with Noto Nastaliq Urdu")
174 harfbuzz.shape(font, buf, { language = harfbuzz.Language.new("urd"),
175 script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.RTL})
176
177 local glyphs = buf:get_glyphs()
178 print("No. of glyphs", #glyphs)
179 for k,v in pairs(glyphs) do
180 print(k)
181 for k1,v1 in pairs(v) do
182 print("",k1,v1)
183 end
184 end
185
186 local opts = { language = harfbuzz.Language.new("eng"),
187 script = harfbuzz.Script.new("Latn"), direction = harfbuzz.Direction.LTR }
188
189 local amiri_face = harfbuzz.Face.new('amiri-regular.ttf')
190 local amiri_font = harfbuzz.Font.new(amiri_face)
191
192 -- shaping '123' w/o features
193 print("\nShaping '123' set with Amiri Regular and no features")
194 buf= harfbuzz.Buffer.new()
195 buf:add_utf8("123")
196 harfbuzz.shape(amiri_font, buf, opts)
197 glyphs = buf:get_glyphs()
198 for k,v in pairs(glyphs) do
199 print(k)
200 for k1,v1 in pairs(v) do
201 print("",k1,v1)
202 end
203 end
204
205
206 -- shaping '123' with '+numr' (numerators)
207 print("\nShaping '123' set with Amiri Regular with 'numr' feature turned on")
208 buf= harfbuzz.Buffer.new()
209 buf:add_utf8("123")
210 opts.features = "+numr"
211 harfbuzz.shape(amiri_font, buf, opts)
212 glyphs = buf:get_glyphs()
213 for k,v in pairs(glyphs) do
214 print(k)
215 for k1,v1 in pairs(v) do
216 print("",k1,v1)
217 end
218 end
219 \stopbuffer
220
221 The example:
222 \typebuffer[hbex]
223
224
225 \startbuffer[hbout]
226
227 Harfbuzz API version 12.2.0
228 Shapers: graphite2 ot fallback
229
230 Face upem = 2048
231
232 Default font scale = X: 2048, Y: 2048
233
234 Shaping 'یہ' set with Noto Nastaliq Urdu
235 No. of glyphs 4
236 1
237 codepoint 277
238 y_advance 0.0
239 cluster 2
240 flags 1.0
241 y_offset 0.0
242 x_advance 472.0
243 x_offset 0.0
244 2
245 codepoint 19
246 y_advance 0.0
247 cluster 0
248 y_offset -383.0
249 x_advance 0.0
250 x_offset 310.0
251 3
252 codepoint 985
253 y_advance 0.0
254 cluster 0
255 y_offset 0.0
256 x_advance 0.0
257 x_offset 0.0
258 4
259 codepoint 316
260 y_advance 0.0
261 cluster 0
262 y_offset -68.0
263 x_advance 731.0
264 x_offset 0.0
265
266 Shaping '123' set with Amiri Regular and no features
267 1
268 codepoint 20
269 y_advance 0.0
270 cluster 0
271 y_offset 0.0
272 x_advance 1090.0
273 x_offset 0.0
274 2
275 codepoint 21
276 y_advance 0.0
277 cluster 1
278 y_offset 0.0
279 x_advance 1090.0
280 x_offset 0.0
281 3
282 codepoint 22
283 y_advance 0.0
284 cluster 2
285 y_offset 0.0
286 x_advance 1090.0
287 x_offset 0.0
288
289 Shaping '123' set with Amiri Regular with 'numr' feature turned on
290 1
291 codepoint 6673
292 y_advance 0.0
293 cluster 0
294 y_offset 0.0
295 x_advance 600.0
296 x_offset 0.0
297 2
298 codepoint 6674
299 y_advance 0.0
300 cluster 1
301 y_offset 0.0
302 x_advance 600.0
303 x_offset 0.0
304 3
305 codepoint 6675
306 y_advance 0.0
307 cluster 2
308 y_offset 0.0
309 x_advance 600.0
310 x_offset 0.0
311 \stopbuffer
312
313 \blank[3*big]
314 The result:
315 \typebuffer[hbout]
316
317
318 \stopsubsection
319 \stopsection
320
321 \stopchapter
322 \stopcomponent