Merge pull request #5371 from odin-lang/bill/vendor/kb-text-shape

`vendor/kb_text_shape`
This commit is contained in:
gingerBill
2025-06-20 10:17:02 +01:00
committed by GitHub
7 changed files with 24388 additions and 1 deletions

View File

@@ -45,4 +45,7 @@ package all
@(require) import stbi "vendor:stb/image"
@(require) import "vendor:stb/rect_pack"
@(require) import "vendor:stb/truetype"
@(require) import "vendor:stb/vorbis"
@(require) import "vendor:stb/vorbis"
@(require) import "vendor:kb_text_shape"

View File

@@ -0,0 +1,151 @@
package vendor_kb_text_shape
when ODIN_OS == .Windows {
foreign import lib {
"lib/kb_text_shape.lib",
}
} else {
foreign import lib {
"kb_text_shape.a",
}
}
import "core:c"
import "core:mem"
#assert(size_of(c.int) == size_of(b32))
#assert(size_of(u32) == size_of(b32))
@(default_calling_convention="c", link_prefix="kbts_", require_results)
foreign lib {
FontIsValid :: proc(Font: ^font) -> b32 ---
SizeOfShapeState :: proc(Font: ^font) -> un ---
ResetShapeState :: proc(State: ^shape_state) ---
ShapeConfig :: proc(Font: ^font, Script: script, Language: language) -> shape_config ---
ShaperIsComplex :: proc(Shaper: shaper) -> b32 ---
ScriptIsComplex :: proc(Script: script) -> b32 ---
Shape :: proc(State: ^shape_state, Config: ^shape_config,
MainDirection, RunDirection: direction,
Glyphs: [^]glyph, GlyphCount: ^u32, GlyphCapacity: u32) -> c.int ---
Cursor :: proc(Direction: direction) -> cursor ---
BeginBreak :: proc(State: ^break_state, MainDirection: direction, JapaneseLineBreakStyle: japanese_line_break_style) ---
BreakStateIsValid :: proc(State: ^break_state) -> b32 ---
BreakAddCodepoint :: proc(State: ^break_state, Codepoint: rune, PositionIncrement: u32, EndOfText: c.int) ---
BreakFlush :: proc(State: ^break_state) ---
Break :: proc(State: ^break_state, Break: ^break_type) -> b32 ---
CodepointToGlyph :: proc(Font: ^font, Codepoint: rune) -> glyph ---
InferScript :: proc(Direction: ^direction, Script: ^script, GlyphScript: script) ---
}
@(require_results)
PlaceShapeState :: proc "c" (Memory: []byte) -> ^shape_state {
@(default_calling_convention="c", require_results)
foreign lib {
kbts_PlaceShapeState :: proc(Address: rawptr, Size: un) -> ^shape_state ---
}
return kbts_PlaceShapeState(raw_data(Memory), un(len(Memory)))
}
@(require_results)
DecodeUtf8 :: proc "contextless" (String: string) -> (Codepoint: rune, SourceCharactersConsumed: u32, Valid: bool) {
decode :: struct {
Codepoint: rune,
SourceCharactersConsumed: u32,
Valid: b32,
}
@(default_calling_convention="c", require_results)
foreign lib {
kbts_DecodeUtf8 :: proc(Utf8: [^]byte, Length: uint) -> decode ---
}
Decode := kbts_DecodeUtf8(raw_data(String), len(String))
return Decode.Codepoint, Decode.SourceCharactersConsumed, bool(Decode.Valid)
}
@(require_results)
ReadFontHeader :: proc "c" (Font: ^font, Data: []byte) -> un {
@(default_calling_convention="c", require_results)
foreign lib {
kbts_ReadFontHeader :: proc(Font: ^font, Data: rawptr, Size: un) -> un ---
}
return kbts_ReadFontHeader(Font, raw_data(Data), un(len(Data)))
}
@(require_results)
ReadFontData :: proc "c" (Font: ^font, Scratch: []byte) -> un {
@(default_calling_convention="c", require_results)
foreign lib {
kbts_ReadFontData :: proc(Font: ^font, Scratch: rawptr, ScratchSize: un) -> un ---
}
return kbts_ReadFontData(Font, raw_data(Scratch), un(len(Scratch)))
}
@(require_results)
PostReadFontInitialize :: proc "c" (Font: ^font, Memory: []byte) -> b32 {
@(default_calling_convention="c", require_results)
foreign lib {
kbts_PostReadFontInitialize :: proc(Font: ^font, Memory: rawptr, MemorySize: un) -> b32 ---
}
return kbts_PostReadFontInitialize(Font, raw_data(Memory), un(len(Memory)))
}
@(require_results)
FontFromMemory :: proc(Data: []byte, allocator: mem.Allocator) -> (Result: font, Err: mem.Allocator_Error) {
ClonedData := mem.make_aligned([]byte, len(Data), 16, allocator) or_return
defer if Err != nil {
delete(ClonedData, allocator)
}
copy(ClonedData, Data)
ScratchSize := ReadFontHeader(&Result, ClonedData)
Scratch := mem.make_aligned([]byte, ScratchSize, 16, allocator) or_return
MemorySize := ReadFontData(&Result, Scratch)
Memory := Scratch
if MemorySize > ScratchSize {
delete(Scratch, allocator)
Memory = mem.make_aligned([]byte, MemorySize, 16, allocator) or_return
}
defer if Err != nil {
delete(Memory, allocator)
}
_ = PostReadFontInitialize(&Result, Memory)
return
}
FreeFont :: proc(Font: ^font, allocator: mem.Allocator) {
free(Font.FileBase, allocator)
free(Font.GlyphLookupMatrix, allocator)
Font^ = {}
}
@(require_results)
CreateShapeState :: proc(Font: ^font, allocator: mem.Allocator) -> (Result: ^shape_state, Err: mem.Allocator_Error) {
Size := SizeOfShapeState(Font)
Memory := mem.make_aligned([]byte, Size, 16, allocator) or_return
Result = PlaceShapeState(Memory)
return
}
FreeShapeState :: proc(State: ^shape_state, allocator: mem.Allocator) {
free(State, allocator)
}
@(require_results)
PositionGlyph :: proc(Cursor: ^cursor, Glyph: ^glyph) -> (X, Y: i32) {
@(default_calling_convention="c", require_results)
foreign lib {
kbts_PositionGlyph :: proc(Cursor: ^cursor, Glyph: ^glyph, X, Y: ^i32) ---
}
kbts_PositionGlyph(Cursor, Glyph, &X, &Y)
return
}

File diff suppressed because it is too large Load Diff

Binary file not shown.

8
vendor/kb_text_shape/src/build.bat vendored Normal file
View File

@@ -0,0 +1,8 @@
@echo off
if not exist "..\lib" mkdir ..\lib
cl -nologo -MT -TC -O2 -c kb_text_shape.c
lib -nologo kb_text_shape.obj -out:..\lib\kb_text_shape.lib
del *.obj

View File

@@ -0,0 +1,5 @@
#include <stdint.h>
#define KB_TEXT_SHAPE_NO_CRT
#define KB_TEXT_SHAPE_IMPLEMENTATION
#include "kb_text_shape.h"

22330
vendor/kb_text_shape/src/kb_text_shape.h vendored Normal file

File diff suppressed because it is too large Load Diff