| 1 | [](https://github.com/harfbuzz/luaharfbuzz/actions/workflows/ci.yml) |
| 2 | |
| 3 | # _luaharfbuzz_ |
| 4 | |
| 5 | Lua bindings for [Harfbuzz]. |
| 6 | |
| 7 | [Harfbuzz]:http://harfbuzz.org |
| 8 | |
| 9 | ## Contents |
| 10 | |
| 11 | * [Overview](#overview) |
| 12 | * [Installing Harfbuzz](#installing-harfbuzz) |
| 13 | * [Installing _luaharfbuzz_](#installing-luaharfbuzz) |
| 14 | * [Documentation](#documentation) |
| 15 | * [Sample Code](#sample-code) |
| 16 | * [Development](#development) |
| 17 | * [Contact](#contact) |
| 18 | |
| 19 | ## Overview |
| 20 | HarfBuzz is an OpenType text shaping engine. It is used in software like Qt, |
| 21 | Pango, Firefox, Chromium, XeTeX and LibreOffice. |
| 22 | |
| 23 | _luaharfbuzz_ provides bindings for the most common types in Harfbuzz. The |
| 24 | initial motivation for building it was to use Harfbuzz with the [LuaTeX] |
| 25 | typesetting system. However, the module isn’t tied to LuaTeX. It |
| 26 | can be used with any Lua codebase. |
| 27 | |
| 28 | As of 2020, luaharfbuzz is bundled with [LuaTeX]. Additional support to use |
| 29 | the HarfBuzz renderer with LuaLaTeX and other formats is available via [luaotfload]. Read more: |
| 30 | |
| 31 | * [TUG 2020 — Marcel Krüger — HarfBuzz in LuaLaTeX](https://www.youtube.com/watch?v=xPj6vNo8exY&t=2120s) |
| 32 | * [Typesetting Bangla script with LuaLaTeX](http://tug.org/TUGboat/tb41-1/tb127fischer-bangla.pdf) |
| 33 | * [Bringing world scripts to LuaTeX: The HarfBuzz experiment](https://tug.org/TUGboat/tb40-1/tb124hosny-harfbuzz.pdf) |
| 34 | |
| 35 | [LuaTeX]:luatex.org |
| 36 | [luaotfload]: https://github.com/latex3/luaotfload |
| 37 | |
| 38 | ## Installing Harfbuzz |
| 39 | |
| 40 | Make sure [Harfbuzz] libraries and headers are installed. before trying to |
| 41 | install _luaharfbuzz_ |
| 42 | |
| 43 | #### OS X |
| 44 | |
| 45 | Install via [Homebrew](http://brew.sh/) |
| 46 | |
| 47 | ``` |
| 48 | brew install harfbuzz |
| 49 | ``` |
| 50 | #### Ubuntu Linux |
| 51 | |
| 52 | ``` |
| 53 | apt-get install libharfbuzz0b libharfbuzz-dev |
| 54 | ``` |
| 55 | |
| 56 | #### Other Platforms |
| 57 | _Send a pull request if you want to include specific instructions to install |
| 58 | Harfbuzz on your preferred platform._ |
| 59 | |
| 60 | Before building the package, LuaRocks populates the `HARFBUZZ_INCDIR` and `HARFBUZZ_LIBDIR` to point to the correct locations. If you can populate these variables manually before running LuaRocks, you can install _luaharfbuzz_ on any system that supports Lua and Harfbuzz. |
| 61 | |
| 62 | ## Installing _luaharfbuzz_ |
| 63 | |
| 64 | #### Luarocks |
| 65 | If [Luarocks] and Harfbuzz are installed, _luaharfbuzz_ can be installed like this: |
| 66 | |
| 67 | ``` |
| 68 | luarocks install luaharfbuzz |
| 69 | ``` |
| 70 | |
| 71 | [Luarocks]: https://luarocks.org |
| 72 | |
| 73 | ## Documentation |
| 74 | * [API Docs](http://harfbuzz.github.io/luaharfbuzz/) |
| 75 | * [Wiki](http://github.com/harfbuzz/luaharfbuzz/wiki) |
| 76 | |
| 77 | ## Sample Code |
| 78 | |
| 79 | Here is some sample code, showcasing the core types and methods in the API. |
| 80 | |
| 81 | ```lua |
| 82 | local harfbuzz = require('harfbuzz') |
| 83 | local serpent = require('serpent') -- luarocks install serpent |
| 84 | |
| 85 | -- Harfbuzz API Version |
| 86 | print("Harfbuzz API version", harfbuzz.version()) |
| 87 | |
| 88 | -- Shapers available |
| 89 | print("Shapers:", serpent.line({ harfbuzz.shapers() }, {comment = false})) |
| 90 | |
| 91 | -- harfbuzz.Face |
| 92 | local face = harfbuzz.Face.new('../fonts/notonastaliq.ttf') |
| 93 | print('\nFace upem = '..face:get_upem()) |
| 94 | |
| 95 | -- harfbuzz.Font |
| 96 | local font = harfbuzz.Font.new(face) |
| 97 | local xs, xy = font:get_scale() |
| 98 | print("\nDefault font scale = X: "..xs..", Y: "..xy) |
| 99 | |
| 100 | -- harfbuzz.Buffer |
| 101 | local text = "یہ" -- U+06CC U+06C1 |
| 102 | local buf = harfbuzz.Buffer.new() |
| 103 | buf:add_utf8(text) |
| 104 | |
| 105 | -- harfbuzz.shape (Shapes text) |
| 106 | print("\nShaping '"..text.."' set with Noto Nastaliq Urdu") |
| 107 | harfbuzz.shape(font, buf, { language = harfbuzz.Language.new("urd"), script = harfbuzz.Script.new("Arab"), direction = harfbuzz.Direction.RTL}) |
| 108 | |
| 109 | local glyphs = buf:get_glyphs() |
| 110 | print("No. of glyphs", #glyphs) |
| 111 | print(serpent.line(glyphs, {comment = false})) |
| 112 | |
| 113 | local opts = { language = harfbuzz.Language.new("eng"), script = harfbuzz.Script.new("Latn"), direction = harfbuzz.Direction.LTR } |
| 114 | local amiri_face = harfbuzz.Face.new('../fonts/amiri-regular.ttf') |
| 115 | local amiri_font = harfbuzz.Font.new(amiri_face) |
| 116 | |
| 117 | -- shaping '123' w/o features |
| 118 | print("\nShaping '123' set with Amiri Regular and no features") |
| 119 | buf= harfbuzz.Buffer.new() |
| 120 | buf:add_utf8("123") |
| 121 | harfbuzz.shape(amiri_font, buf, opts) |
| 122 | glyphs = buf:get_glyphs() |
| 123 | print(serpent.line(glyphs, {comment = false})) |
| 124 | |
| 125 | -- shaping '123' with '+numr' (numerators) |
| 126 | print("\nShaping '123' set with Amiri Regular with 'numr' feature turned on") |
| 127 | buf= harfbuzz.Buffer.new() |
| 128 | buf:add_utf8("123") |
| 129 | opts.features = "+numr" |
| 130 | harfbuzz.shape(amiri_font, buf, opts) |
| 131 | glyphs = buf:get_glyphs() |
| 132 | print(serpent.line(glyphs, {comment = false})) |
| 133 | ``` |
| 134 | |
| 135 | ## Development |
| 136 | |
| 137 | #### Building |
| 138 | You can build the package for development purposes using LuaRocks as well. It is recommended that you build it to your local tree (using `--local`) to isolate it from your actual installation. For convenience, you can use `make`: |
| 139 | |
| 140 | ``` |
| 141 | make build |
| 142 | ``` |
| 143 | |
| 144 | #### Testing and Linting |
| 145 | In order to make changes to the code and run the tests, the following dependencies need to be installed: |
| 146 | |
| 147 | * [Busted](http://olivinelabs.com/busted/) – `luarocks install busted` |
| 148 | * [luacheck](luacheck.readthedocs.org) – `luarocks install luacheck` |
| 149 | * [ldoc](https://stevedonovan.github.io/ldoc/) – `luarocks install ldoc` |
| 150 | |
| 151 | Run the test suite: |
| 152 | ``` |
| 153 | make test |
| 154 | ``` |
| 155 | |
| 156 | Lint the codebase: |
| 157 | ``` |
| 158 | make lint |
| 159 | ``` |
| 160 | |
| 161 | Generate documentation from sources: |
| 162 | ``` |
| 163 | make doc |
| 164 | ``` |
| 165 | |
| 166 | ## Contact |
| 167 | Open a Github issue, or email me at <[email protected]>. |