├── images ├── hindi.png ├── khmer.png ├── arabic.png ├── myanmar.png ├── smallcaps.png └── gunjala_gondi.png ├── LICENSE └── README.md /images/hindi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JimmyLefevre/kb/HEAD/images/hindi.png -------------------------------------------------------------------------------- /images/khmer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JimmyLefevre/kb/HEAD/images/khmer.png -------------------------------------------------------------------------------- /images/arabic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JimmyLefevre/kb/HEAD/images/arabic.png -------------------------------------------------------------------------------- /images/myanmar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JimmyLefevre/kb/HEAD/images/myanmar.png -------------------------------------------------------------------------------- /images/smallcaps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JimmyLefevre/kb/HEAD/images/smallcaps.png -------------------------------------------------------------------------------- /images/gunjala_gondi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JimmyLefevre/kb/HEAD/images/gunjala_gondi.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | zlib License 2 | 3 | (C) Copyright 2024-2025 Jimmy Lefevre 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kb 2 | 3 | [Single-header](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt) permissively-licensed libraries for C/C++. 4 | 5 | ## Libraries 6 | 7 | - [kb\_text\_shape.h](./kb_text_shape.h): Unicode text segmentation and OpenType shaping 8 | 9 | ## kb_text_shape.h 10 | 11 | ![Example of Arabic shaping with stb_truetype](./images/arabic.png) 12 | ![Example of Hindi shaping with stb_truetype](./images/hindi.png) 13 | ![Example of Khmer shaping with stb_truetype](./images/khmer.png) 14 | ![Example of Myanmar shaping with stb_truetype](./images/myanmar.png) 15 | ![Example of Gunjala Gondi shaping with stb_truetype](./images/gunjala_gondi.png) 16 | ![Example of toggling the smallcaps font feature](./images/smallcaps.png) 17 | 18 | [kb\_text\_shape.h](./kb_text_shape.h) provides: 19 | - ICU-like text segmentation (i.e. breaking Unicode text by direction, line, script, word and grapheme). 20 | - Harfbuzz-like text shaping for OpenType fonts, which means it is capable of handling complex script layout and ligatures, among other things. 21 | - Font coverage checking: know if a font can display a given string. 22 | 23 | It does **not** handle rasterization. It does **not** handle paragraph layout. It does **not** handle selection and loading of system fonts. It will only help you know which glyphs to display where on a single, infinitely-long line, using the fonts you have provided! 24 | 25 | (See https://www.newroadoldway.com/text1.html for an explanation of the different steps of text processing.) 26 | 27 | For an in-depth usage example, check out [refpad](https://github.com/JimmyLefevre/refpad). 28 | 29 | ```c 30 | // Yours to provide: 31 | void DrawGlyph(kbts_u16 GlyphId, kbts_s32 GlyphOffsetX, kbts_s32 GlyphOffsetY, kbts_s32 GlyphAdvanceX, kbts_s32 GlyphAdvanceY, 32 | kbts_direction ParagraphDirection, kbts_direction RunDirection, kbts_script Script, kbts_font *Font); 33 | void NextLine(void); 34 | void *CreateRenderFont(const char *FontPath); 35 | 36 | void HandleText(kbts_shape_context *Context, const char *Text, kbts_language Language) 37 | { 38 | kbts_ShapeBegin(Context, KBTS_DIRECTION_DONT_KNOW, Language); 39 | kbts_ShapeUtf8(Context, Text, (int)strlen(Text), KBTS_USER_ID_GENERATION_MODE_CODEPOINT_INDEX); 40 | kbts_ShapeEnd(Context); 41 | 42 | kbts_run Run; 43 | while(kbts_ShapeRun(Context, &Run)) 44 | { 45 | if(Run.Flags & KBTS_BREAK_FLAG_LINE_HARD) 46 | { 47 | NextLine(); 48 | } 49 | 50 | kbts_glyph *Glyph; 51 | while(kbts_GlyphIteratorNext(&Run.Glyphs, &Glyph)) 52 | { 53 | DrawGlyph(Glyph->Id, Glyph->OffsetX, Glyph->OffsetY, Glyph->AdvanceX, Glyph->AdvanceY, 54 | Run.ParagraphDirection, Run.Direction, Run.Script, Run.Font); 55 | } 56 | } 57 | } 58 | 59 | void Example(void) 60 | { 61 | kbts_shape_context *Context = kbts_CreateShapeContext(0, 0); 62 | kbts_font *FontA = kbts_ShapePushFontFromFile(Context, "NotoSansMyanmar-Regular.ttf", 0); 63 | kbts_font *FontB = kbts_ShapePushFontFromFile(Context, "NotoSansArabic-Regular.ttf", 0); 64 | 65 | FontA->UserData = CreateRenderFont("NotoSansMyanmar-Regular.ttf"); 66 | FontB->UserData = CreateRenderFont("NotoSansArabic-Regular.ttf"); 67 | 68 | HandleText(Context, (const char *)u8"یکအမည်မရှိیک", KBTS_LANGUAGE_ARABIC); 69 | } 70 | ``` 71 | --------------------------------------------------------------------------------