1*2d1272b8SAndroid Build Coastguard Worker# Configuring HarfBuzz 2*2d1272b8SAndroid Build Coastguard Worker 3*2d1272b8SAndroid Build Coastguard WorkerMost of the time you will not need any custom configuration. The configuration 4*2d1272b8SAndroid Build Coastguard Workeroptions provided by `meson` should be enough. In particular, if you just want 5*2d1272b8SAndroid Build Coastguard WorkerHarfBuzz library plus hb-shape / hb-view utilities, make sure FreeType and Cairo 6*2d1272b8SAndroid Build Coastguard Workerare available and found during configuration. 7*2d1272b8SAndroid Build Coastguard Worker 8*2d1272b8SAndroid Build Coastguard WorkerIf you are building for distribution, you should more carefully consider whether 9*2d1272b8SAndroid Build Coastguard Workeryou need Glib, ICU, Graphite2, as well as CoreText / Uniscribe / DWrite. Make 10*2d1272b8SAndroid Build Coastguard Workersure the relevant ones are enabled. 11*2d1272b8SAndroid Build Coastguard Worker 12*2d1272b8SAndroid Build Coastguard WorkerIf you are building for custom environment (embedded, downloadable app, etc) 13*2d1272b8SAndroid Build Coastguard Workerwhere you mostly just want to call `hb_shape()` and the binary size of the 14*2d1272b8SAndroid Build Coastguard Workerresulting library is very important to you, the rest of this file guides you 15*2d1272b8SAndroid Build Coastguard Workerthrough your options to disable features you may not need, in exchange for 16*2d1272b8SAndroid Build Coastguard Workerbinary size savings. 17*2d1272b8SAndroid Build Coastguard Worker 18*2d1272b8SAndroid Build Coastguard Worker## Compiler Options 19*2d1272b8SAndroid Build Coastguard Worker 20*2d1272b8SAndroid Build Coastguard WorkerMake sure you build with your compiler's "optimize for size" option. On `gcc` 21*2d1272b8SAndroid Build Coastguard Workerthis is `-Os`, and can be enabled by passing `CXXFLAGS=-Os`. On clang there 22*2d1272b8SAndroid Build Coastguard Workeris an even more extreme flag, `-Oz`. Meson also provides `--buildtype=minsize` 23*2d1272b8SAndroid Build Coastguard Workerfor more convenience. 24*2d1272b8SAndroid Build Coastguard Worker 25*2d1272b8SAndroid Build Coastguard WorkerHarfBuzz heavily uses inline functions and the optimize-size flag can make the 26*2d1272b8SAndroid Build Coastguard Workerlibrary smaller by 20% or more. Moreover, sometimes, based on the target CPU, 27*2d1272b8SAndroid Build Coastguard Workerthe optimize-size builds perform *faster* as well, thanks to lower code 28*2d1272b8SAndroid Build Coastguard Workerfootprint and caching effects. So, definitely try that even if size is not 29*2d1272b8SAndroid Build Coastguard Workerextremely tight but you have a huge application. For example, Chrome does 30*2d1272b8SAndroid Build Coastguard Workerthat. Note that this configuration also automatically enables certain internal 31*2d1272b8SAndroid Build Coastguard Workeroptimizations. Search for `HB_OPTIMIZE_SIZE` for details, if you are using 32*2d1272b8SAndroid Build Coastguard Workerother compilers, or continue reading. 33*2d1272b8SAndroid Build Coastguard Worker 34*2d1272b8SAndroid Build Coastguard WorkerAnother compiler option to consider is "link-time optimization", also known as 35*2d1272b8SAndroid Build Coastguard Worker'lto'. To enable that, feel free to use `-Db_lto=true` of meson. 36*2d1272b8SAndroid Build Coastguard WorkerThis, also, can have a huge impact on the final size, 20% or more. 37*2d1272b8SAndroid Build Coastguard Worker 38*2d1272b8SAndroid Build Coastguard WorkerFinally, if you are making a static library build or otherwise linking the 39*2d1272b8SAndroid Build Coastguard Workerlibrary into your app, make sure your linker removes unused functions. This 40*2d1272b8SAndroid Build Coastguard Workercan be tricky and differ from environment to environment, but you definitely 41*2d1272b8SAndroid Build Coastguard Workerwant to make sure this happens. Otherwise, every unused public function will 42*2d1272b8SAndroid Build Coastguard Workerbe adding unneeded bytes to your binary. The following pointers might come 43*2d1272b8SAndroid Build Coastguard Workerhandy: 44*2d1272b8SAndroid Build Coastguard Worker 45*2d1272b8SAndroid Build Coastguard Worker * https://lwn.net/Articles/741494/ (all of the four-part series) 46*2d1272b8SAndroid Build Coastguard Worker * https://elinux.org/images/2/2d/ELC2010-gc-sections_Denys_Vlasenko.pdf 47*2d1272b8SAndroid Build Coastguard Worker 48*2d1272b8SAndroid Build Coastguard WorkerCombining the above three build options should already shrink your library a lot. 49*2d1272b8SAndroid Build Coastguard WorkerThe rest of this file shows you ways to shrink the library even further at the 50*2d1272b8SAndroid Build Coastguard Workerexpense of removing functionality (that may not be needed). The remaining 51*2d1272b8SAndroid Build Coastguard Workeroptions are all enabled by defining pre-processor macros, which can be done 52*2d1272b8SAndroid Build Coastguard Workervia `CXXFLAGS` or `CPPFLAGS` similarly. 53*2d1272b8SAndroid Build Coastguard Worker 54*2d1272b8SAndroid Build Coastguard Worker 55*2d1272b8SAndroid Build Coastguard Worker## Unicode-functions 56*2d1272b8SAndroid Build Coastguard Worker 57*2d1272b8SAndroid Build Coastguard WorkerAccess to Unicode data can be configured at compile time as well as run-time. 58*2d1272b8SAndroid Build Coastguard WorkerBy default, HarfBuzz ships with its own compact subset of properties from 59*2d1272b8SAndroid Build Coastguard WorkerUnicode Character Database that it needs. This is a highly-optimized 60*2d1272b8SAndroid Build Coastguard Workerimplementation that depending on compile settings (optimize-size or not) 61*2d1272b8SAndroid Build Coastguard Workertakes around ~40kb or ~60kb. Using this implementation (default) is highly 62*2d1272b8SAndroid Build Coastguard Workerrecommended, as HarfBuzz always ships with data from latest version of Unicode. 63*2d1272b8SAndroid Build Coastguard WorkerThis implementation can be disabled by defining `HB_NO_UCD`. 64*2d1272b8SAndroid Build Coastguard Worker 65*2d1272b8SAndroid Build Coastguard WorkerFor example, if you are enabling ICU as a built-in option, or GLib, those 66*2d1272b8SAndroid Build Coastguard Workercan provide Unicode data as well, so defining `HB_NO_UCD` might save you 67*2d1272b8SAndroid Build Coastguard Workerspace without reducing functionality (to the extent that the Unicode version 68*2d1272b8SAndroid Build Coastguard Workerof those implementations is recent.) 69*2d1272b8SAndroid Build Coastguard Worker 70*2d1272b8SAndroid Build Coastguard WorkerIf, however, you provide your own Unicode data to HarfBuzz at run-time by 71*2d1272b8SAndroid Build Coastguard Workercalling `hb_buffer_set_unicode_funcs` on every buffer you create, and you do 72*2d1272b8SAndroid Build Coastguard Workernot rely on `hb_unicode_funcs_get_default()` results, you can disable the 73*2d1272b8SAndroid Build Coastguard Workerinternal implementation by defining both `HB_NO_UCD` and `HB_NO_UNICODE_FUNCS`. 74*2d1272b8SAndroid Build Coastguard WorkerThe latter is needed to guard against accidentally building a library without 75*2d1272b8SAndroid Build Coastguard Workerany default Unicode implementations. 76*2d1272b8SAndroid Build Coastguard Worker 77*2d1272b8SAndroid Build Coastguard Worker 78*2d1272b8SAndroid Build Coastguard Worker## Font-functions 79*2d1272b8SAndroid Build Coastguard Worker 80*2d1272b8SAndroid Build Coastguard WorkerAccess to certain font functionalities can also be configured at run-time. By 81*2d1272b8SAndroid Build Coastguard Workerdefault, HarfBuzz uses an efficient internal implementation of OpenType 82*2d1272b8SAndroid Build Coastguard Workerfunctionality for this. This internal implementation is called `hb-ot-font`. 83*2d1272b8SAndroid Build Coastguard WorkerAll newly-created `hb_font_t` objects by default use `hb-ot-font`. Using this 84*2d1272b8SAndroid Build Coastguard Workeris highly recommended, and is what fonts use by default when they are created. 85*2d1272b8SAndroid Build Coastguard Worker 86*2d1272b8SAndroid Build Coastguard WorkerMost embedded uses will probably use HarfBuzz with FreeType using `hb-ft.h`. 87*2d1272b8SAndroid Build Coastguard WorkerIn that case, or if you otherwise provide those functions by calling 88*2d1272b8SAndroid Build Coastguard Worker`hb_font_set_funcs()` on every font you create, you can disable `hb-ot-font` 89*2d1272b8SAndroid Build Coastguard Workerwithout loss of functionality by defining `HB_NO_OT_FONT`. 90*2d1272b8SAndroid Build Coastguard Worker 91*2d1272b8SAndroid Build Coastguard Worker 92*2d1272b8SAndroid Build Coastguard Worker## Shapers 93*2d1272b8SAndroid Build Coastguard Worker 94*2d1272b8SAndroid Build Coastguard WorkerMost HarfBuzz clients use it for the main shaper, called "ot". However, it 95*2d1272b8SAndroid Build Coastguard Workeris legitimate to want to compile HarfBuzz with only another backend, eg. 96*2d1272b8SAndroid Build Coastguard WorkerCoreText, for example for an iOS app. For that, you want `HB_NO_OT_SHAPE`. 97*2d1272b8SAndroid Build Coastguard WorkerIf you are going down that route, check if you want `HB_NO_OT`. 98*2d1272b8SAndroid Build Coastguard Worker 99*2d1272b8SAndroid Build Coastguard WorkerThis is very rarely what you need. Make sure you understand exactly what you 100*2d1272b8SAndroid Build Coastguard Workerare doing. 101*2d1272b8SAndroid Build Coastguard Worker 102*2d1272b8SAndroid Build Coastguard WorkerDefining `HB_NO_FALLBACK_SHAPE` however is pretty harmless. That removes the 103*2d1272b8SAndroid Build Coastguard Worker(unused) "fallback" shaper. This is defined by the `HB_TINY` profile already 104*2d1272b8SAndroid Build Coastguard Worker(more below). 105*2d1272b8SAndroid Build Coastguard Worker 106*2d1272b8SAndroid Build Coastguard Worker 107*2d1272b8SAndroid Build Coastguard Worker## Thread-safety 108*2d1272b8SAndroid Build Coastguard Worker 109*2d1272b8SAndroid Build Coastguard WorkerBy default HarfBuzz builds as a thread-safe library. The exception is that 110*2d1272b8SAndroid Build Coastguard Workerthe `HB_TINY` predefined configuration (more below) disables thread-safety. 111*2d1272b8SAndroid Build Coastguard Worker 112*2d1272b8SAndroid Build Coastguard WorkerIf you do *not* need thread-safety in the library (eg. you always call into 113*2d1272b8SAndroid Build Coastguard WorkerHarfBuzz from the same thread), you can disable thread-safety by defining 114*2d1272b8SAndroid Build Coastguard Worker`HB_NO_MT`. As noted already, this is enabled by `HB_TINY`. 115*2d1272b8SAndroid Build Coastguard Worker 116*2d1272b8SAndroid Build Coastguard Worker 117*2d1272b8SAndroid Build Coastguard Worker## Pre-defined configurations 118*2d1272b8SAndroid Build Coastguard Worker 119*2d1272b8SAndroid Build Coastguard WorkerThe [`hb-config.hh`](src/hb-config.hh) internal header supports three 120*2d1272b8SAndroid Build Coastguard Workerpre-defined configurations as well grouping of various configuration options. 121*2d1272b8SAndroid Build Coastguard WorkerThe pre-defined configurations are: 122*2d1272b8SAndroid Build Coastguard Worker 123*2d1272b8SAndroid Build Coastguard Worker * `HB_MINI`: Disables shaping of AAT as well as legacy fonts. Ie. it produces 124*2d1272b8SAndroid Build Coastguard Worker a capable OpenType shaper only. 125*2d1272b8SAndroid Build Coastguard Worker 126*2d1272b8SAndroid Build Coastguard Worker * `HB_LEAN`: Disables various non-shaping functionality in the library, as well 127*2d1272b8SAndroid Build Coastguard Worker as esoteric or rarely-used shaping features. See the definition for details. 128*2d1272b8SAndroid Build Coastguard Worker 129*2d1272b8SAndroid Build Coastguard Worker * `HB_TINY`: Enables both `HB_MINI` and `HB_LEAN` configurations, as well as 130*2d1272b8SAndroid Build Coastguard Worker disabling thread-safety and debugging, and use even more size-optimized data 131*2d1272b8SAndroid Build Coastguard Worker tables. 132*2d1272b8SAndroid Build Coastguard Worker 133*2d1272b8SAndroid Build Coastguard WorkerTo setup the build with these options use something like: 134*2d1272b8SAndroid Build Coastguard Worker``` 135*2d1272b8SAndroid Build Coastguard Worker$ meson setup build -Dcpp_args=-DHB_MINI -Dc_args=-DHB_MINI 136*2d1272b8SAndroid Build Coastguard Worker``` 137*2d1272b8SAndroid Build Coastguard Worker 138*2d1272b8SAndroid Build Coastguard Worker## Tailoring configuration 139*2d1272b8SAndroid Build Coastguard Worker 140*2d1272b8SAndroid Build Coastguard WorkerMost of the time, one of the pre-defined configuration is exactly what one needs. 141*2d1272b8SAndroid Build Coastguard WorkerSometimes, however, the pre-defined configuration cuts out features that might 142*2d1272b8SAndroid Build Coastguard Workerbe desired in the library. Unfortunately there is no quick way to undo those 143*2d1272b8SAndroid Build Coastguard Workerconfigurations from the command-line. 144*2d1272b8SAndroid Build Coastguard Worker 145*2d1272b8SAndroid Build Coastguard WorkerHowever, configuration can still be overridden from a file. To do that, add your 146*2d1272b8SAndroid Build Coastguard Workeroverride instructions (mostly `undef` instructions) to a header file and define 147*2d1272b8SAndroid Build Coastguard Workerthe macro `HB_CONFIG_OVERRIDE_H` to the string containing to that header file's 148*2d1272b8SAndroid Build Coastguard Workername. HarfBuzz will then include that file at the appropriate place during 149*2d1272b8SAndroid Build Coastguard Workerconfiguration. 150*2d1272b8SAndroid Build Coastguard Worker 151*2d1272b8SAndroid Build Coastguard WorkerUp until HarfBuzz 3.1.2, the configuration override header file's name was 152*2d1272b8SAndroid Build Coastguard Workerfixed and called `config-override.h`, and was activated by defining the macro 153*2d1272b8SAndroid Build Coastguard Worker`HAVE_CONFIG_OVERRIDE_H`. That still works. 154*2d1272b8SAndroid Build Coastguard Worker 155*2d1272b8SAndroid Build Coastguard Worker 156*2d1272b8SAndroid Build Coastguard Worker## Notes 157*2d1272b8SAndroid Build Coastguard Worker 158*2d1272b8SAndroid Build Coastguard WorkerNote that the config option `HB_NO_CFF`, which is enabled by `HB_LEAN` and 159*2d1272b8SAndroid Build Coastguard Worker`HB_TINY` does *not* mean that the resulting library won't work with CFF fonts. 160*2d1272b8SAndroid Build Coastguard WorkerThe library can shape valid CFF fonts just fine, with or without this option. 161*2d1272b8SAndroid Build Coastguard WorkerThis option disables (among other things) the code to calculate glyph extents 162*2d1272b8SAndroid Build Coastguard Workerfor CFF fonts or draw them, which many clients might not need. 163