summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/luatex/base/luatex-harfbuzz.tex
blob: c7ed992405a81464777b865f23ef299680db7ef5 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
% language=uk

\environment luatex-style

\startcomponent luatex-harfbuzz

\definelayer
  [hbpage]
  [width=\paperwidth,
   height=\paperheight]


\setupbackgrounds
  [page]
  [background=hbpage,
   state=start]


\startchapter[reference=harfbuzz,title={The HarfBuzz libraries}]


\startsection[title={The \type {luaharfbuzz} library}][library=luaharfbuzz]

At the moment the documentation of the library is a raw "html-to-pdf" 
rendering of the \type{index.html} file under \type{luaharfbuzz/docs} 
folder of the source code. The example is almost the verbatim copy
of the file under \type{luaharfbuzz/examples}.
\page


\dorecurse{17}{%
\setlayer[hbpage][x=0mm,y=0mm,]{\externalfigure[graphics/luaharfbuzz.pdf][page=\recurselevel]}
\blank
\page[empty]
}


\startsubsection[title={Example}]

The example is (a small modification of)  the file \type{core_types.lua}
and it requires the file \type{harfbuzz.lua}; both are 
under \type{luaharfbuzz}  folder of the source code.
The fonts \type{notonastaliq.ttf} and \type{amiri-regular.ttf'} are under 
\type{luaharfbuzz/fonts}.


\blank[3*big]

\startbuffer[hbmod]
local hb = require("luaharfbuzz")

-- special tags
hb.Tag.NONE = hb.Tag.new()

-- special script codes (ISO 15924)
hb.Script.COMMON    = hb.Script.new("Zyyy")
hb.Script.INHERITED = hb.Script.new("Zinh")
hb.Script.UNKNOWN   = hb.Script.new("Zzzz")
hb.Script.INVALID   = hb.Script.from_iso15924_tag(hb.Tag.NONE)

-- directions
hb.Direction.INVALID = hb.Direction.new("invalid")
hb.Direction.LTR = hb.Direction.new("ltr")
hb.Direction.RTL = hb.Direction.new("rtl")
hb.Direction.TTB = hb.Direction.new("ttb")
hb.Direction.BTT = hb.Direction.new("btt")

-- special languages
hb.Language.INVALID = hb.Language.new()

hb.shape = function(font, buf, options)
  options = options or { }

  -- Apply options to buffer if they are set.
  if options.language then buf:set_language(options.language) end
  if options.script then buf:set_script(options.script) end
  if options.direction then buf:set_direction(options.direction) end

  -- Guess segment properties, in case all steps above have failed
  -- to set the right properties.
  buf:guess_segment_properties()

  local features = {}

  -- Parse features
  if type(options.features) == "string" then
    for fs in string.gmatch(options.features, '([^,]+)') do
      local feature = hb.Feature.new(fs)
      if feature then
        table.insert(features, hb.Feature.new(fs))
      else
        error(string.format("Invalid feature string: '%s'", fs))
      end
    end
  elseif type(options.features) == "table" then
    features = options.features
  elseif options.features then -- non-nil but not a string or table
    error("Invalid features option")
  end

  return hb.shape_full(font,buf,features,options.shapers or {})
end

-- For backward compatibility
hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES  = hb.Buffer.CLUSTER_LEVEL_MONOTONE_GRAPHEMES
hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS = hb.Buffer.CLUSTER_LEVEL_MONOTONE_CHARACTERS
hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_CHARACTERS          = hb.Buffer.CLUSTER_LEVEL_CHARACTERS
hb.Buffer.HB_BUFFER_CLUSTER_LEVEL_DEFAULT             = hb.Buffer.CLUSTER_LEVEL_DEFAULT

hb.Tag.HB_TAG_NONE = hb.Tag.NONE

hb.Script.HB_SCRIPT_COMMON    = hb.Script.COMMON
hb.Script.HB_SCRIPT_INHERITED = hb.Script.INHERITED
hb.Script.HB_SCRIPT_UNKNOWN   = hb.Script.UNKNOWN
hb.Script.HB_SCRIPT_INVALID   = hb.Script.INVALID

hb.Language.HB_LANGUAGE_INVALID = hb.Language.INVALID

hb.Direction.HB_DIRECTION_INVALID = hb.Direction.INVALID
hb.Direction.HB_DIRECTION_LTR = hb.Direction.LTR
hb.Direction.HB_DIRECTION_RTL = hb.Direction.RTL
hb.Direction.HB_DIRECTION_TTB = hb.Direction.TTB
hb.Direction.HB_DIRECTION_BTT = hb.Direction.BTT

hb.Direction.HB_DIRECTION_IS_VALID = hb.Direction.is_valid
hb.Direction.HB_DIRECTION_IS_HORIZONTAL = hb.Direction.is_horizontal
hb.Direction.HB_DIRECTION_IS_VERTICAL = hb.Direction.is_vertical
hb.Direction.HB_DIRECTION_IS_FORWARD = hb.Direction.is_forward
hb.Direction.HB_DIRECTION_IS_BACKWARD = hb.Direction.is_backward

hb.Buffer.get_glyph_infos_and_positions = hb.Buffer.get_glyphs

return hb

\stopbuffer

\blank[3*big]

The file \type{harfbuzz.lua}:
\typebuffer[hbmod]


\startbuffer[hbex]
local harfbuzz = require('harfbuzz')

-- Harfbuzz API Version
print("Harfbuzz API version", harfbuzz.version())

-- Shapers available
print("Shapers:", harfbuzz.shapers())


-- harfbuzz.Face
local face = harfbuzz.Face.new('notonastaliq.ttf')
print('\nFace upem = '..face:get_upem())

-- harfbuzz.Font
local font = harfbuzz.Font.new(face)
local xs, xy = font:get_scale()
print("\nDefault font scale = X: "..xs..", Y: "..xy)

-- harfbuzz.Buffer
local text = "یہ" -- U+06CC U+06C1
local buf = harfbuzz.Buffer.new()
buf:add_utf8(text)

-- harfbuzz.shape (Shapes text)
print("\nShaping '"..text.."' set with Noto Nastaliq Urdu")
harfbuzz.shape(font, buf, { language = harfbuzz.Language.new("urd"), 
script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.RTL})

local glyphs = buf:get_glyphs()
print("No. of glyphs", #glyphs)
for k,v in pairs(glyphs) do 
  print(k) 
  for k1,v1 in pairs(v) do 
   print("",k1,v1) 
  end 
end 

local opts = { language = harfbuzz.Language.new("eng"), 
script = harfbuzz.Script.new("Latn"), direction = harfbuzz.Direction.LTR }

local amiri_face = harfbuzz.Face.new('amiri-regular.ttf')
local amiri_font = harfbuzz.Font.new(amiri_face)

-- shaping '123' w/o features
print("\nShaping '123' set with Amiri Regular and no features")
buf= harfbuzz.Buffer.new()
buf:add_utf8("123")
harfbuzz.shape(amiri_font, buf, opts)
glyphs = buf:get_glyphs()
for k,v in pairs(glyphs) do 
  print(k) 
  for k1,v1 in pairs(v) do 
   print("",k1,v1) 
  end 
end 


-- shaping '123' with '+numr' (numerators)
print("\nShaping '123' set with Amiri Regular with 'numr' feature turned on")
buf= harfbuzz.Buffer.new()
buf:add_utf8("123")
opts.features = "+numr"
harfbuzz.shape(amiri_font, buf, opts)
glyphs = buf:get_glyphs()
for k,v in pairs(glyphs) do 
  print(k) 
  for k1,v1 in pairs(v) do 
   print("",k1,v1) 
  end 
end 
\stopbuffer

The example:
\typebuffer[hbex]


\startbuffer[hbout]
Harfbuzz API version	2.6.4
Shapers:	graphite2	ot	fallback

Face upem = 2048

Default font scale = X: 2048, Y: 2048

Shaping 'یہ' set with Noto Nastaliq Urdu
No. of glyphs	4
1
    flags	1.0
    cluster	2
    codepoint	277
    x_advance	472.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0
2
    cluster	0
    codepoint	19
    x_advance	0.0
    y_advance	0.0
    x_offset	310.0
    y_offset	-383.0
3
    cluster	0
    codepoint	985
    x_advance	0.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0
4
    cluster	0
    codepoint	316
    x_advance	731.0
    y_advance	0.0
    x_offset	0.0
    y_offset	-68.0

Shaping '123' set with Amiri Regular and no features
1
    cluster	0
    codepoint	20
    x_advance	1090.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0
2
    cluster	1
    codepoint	21
    x_advance	1090.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0
3
    cluster	2
    codepoint	22
    x_advance	1090.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0

Shaping '123' set with Amiri Regular with 'numr' feature turned on
1
    cluster	0
    codepoint	6673
    x_advance	600.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0
2
    cluster	1
    codepoint	6674
    x_advance	600.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0
3
    cluster	2
    codepoint	6675
    x_advance	600.0
    y_advance	0.0
    x_offset	0.0
    y_offset	0.0

\stopbuffer

\blank[3*big]
The result:
\typebuffer[hbout]


\stopsubsection
\stopsection

\stopchapter
\stopcomponent