Initial release.

This commit is contained in:
Keeba
2025-06-16 08:52:25 +02:00
commit e71b6ae7ff
3 changed files with 22428 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
zlib License
(C) Copyright 2024-2025 Jimmy Lefevre
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
+84
View File
@@ -0,0 +1,84 @@
# kb
[Single-header](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt) permissively-licensed libraries for C/C++.
## Libraries
- [kb\_text\_shape.h](./kb_text_shape.h): Unicode text segmentation and OpenType shaping
## kb_text_shape.h
[kb\_text\_shape.h](./kb_text_shape.h) provides ICU-like text segmentation (i.e. breaking Unicode text by direction, line, word and grapheme). It also provides Harfbuzz-like text shaping for OpenType fonts, which means it is capable of handling complex script layout and ligatures, among other things.
It does **not** handle rasterization. It will only help you know which glyphs to display where!
```c
static kbts_font GlobalFont;
void RenderGlyph(kbts_glyph *Glyph, int X, int Y);
void ShapeText(kbts_cursor *Cursor, uint32_t *Codepoints, size_t CodepointCount, kbts_direction MainDirection, kbts_direction Direction, kbts_script Script)
{
kbts_glyph *Glyphs = (kbts_glyph *)malloc(sizeof(kbts_glyph) * CodepointCount);
for(size_t CodepointIndex = 0; CodepointIndex < CodepointCount; ++CodepointIndex)
{
Glyphs[CodepointIndex] = kbts_CodepointToGlyph(&GlobalFont, Codepoints[CodepointIndex]);
}
kbts_shape_state *State = kbts_CreateShapeState(&GlobalFont);
kbts_shape_config Config = kbts_ShapeConfig(&GlobalFont, Script, KBTS_LANGUAGE_DONT_KNOW);
uint32_t GlyphCount = CodepointCount;
uint32_t GlyphCapacity = GlyphCount;
while(kbts_Shape(State, &Config, MainDirection, Direction, Glyphs, &GlyphCount, GlyphCapacity))
{
Glyphs = (kbts_glyph *)realloc(sizeof(kbts_glyph) * State->RequiredGlyphCapacity);
GlyphCapacity = State->RequiredGlyphCapacity;
}
for(size_t GlyphIndex = 0; GlyphIndex < GlyphCount; ++GlyphIndex)
{
kbts_glyph *Glyph = &Glyphs[GlyphIndex];
int X, Y;
kbts_PositionGlyph(Cursor, Glyph, &X, &Y);
RenderGlyph(Glyph, X, Y);
}
free(Glyphs);
}
void SegmentText(uint32_t *Codepoints, size_t CodepointCount)
{
kbts_cursor Cursor = {0};
kbts_direction Direction = KBTS_DIRECTION_NONE;
kbts_script Script = KBTS_SCRIPT_DONT_KNOW;
size_t RunStart = 0;
kbts_break_state BreakState;
kbts_BeginBreak(&BreakState, KBTS_DIRECTION_NONE, KBTS_JAPANESE_LINE_BREAK_STYLE_NORMAL);
for(size_t CodepointIndex = 0; CodepointIndex < CodepointCount; ++CodepointIndex)
{
kbts_BreakAddCodepoint(&BreakState, Codepoints[CodepointIndex], 1, (CodepointIndex + 1) == CodepointCount);
kbts_break Break;
while(kbts_Break(&BreakState, &Break))
{
if((Break.Position > RunStart) && (Break.Flags & KBTS_BREAK_FLAG_DIRECTION | KBTS_BREAK_FLAG_SCRIPT | KBTS_BREAK_FLAG_LINE_HARD))
{
size_t RunLength = Break.Position - RunStart;
ShapeText(&Cursor, Codepoints + RunStart, RunLength, BreakState.MainDirection, Direction, Script);
}
if(Break.Flags & KBTS_BREAK_FLAG_DIRECTION)
{
Direction = Break.Direction;
}
if(Break.Flags & KBTS_BREAK_FLAG_SCRIPT)
{
Script = Break.Script;
}
}
}
}
```
+22325
View File
File diff suppressed because it is too large Load Diff