diff --git a/kb_text_shape.h b/kb_text_shape.h index ab98ff8..3775445 100644 --- a/kb_text_shape.h +++ b/kb_text_shape.h @@ -1,4 +1,4 @@ -/* kb_text_shape - v2.32 - text segmentation and shaping +/* kb_text_shape - v2.33 - text segmentation and shaping by Jimmy Lefevre SECURITY @@ -104,6 +104,8 @@ defaults to memset otherwise. KBTS_MEMCPY defaults to memcpy otherwise. + KBTS_MEMMOVE + defaults to memmove otherwise. You can redefine the default allocator by redefining these: KBTS_MALLOC(AllocatorData, Size) defaults to 0 if KB_TEXT_SHAPE_NO_CRT is defined, @@ -1560,6 +1562,21 @@ . . . . VERSION HISTORY + 2.33 - The context's shape_config cache now uses UIDs as a key, making it work with variations. + The context's caches now have proper eviction. + The parser now invalidates font tables one by one, rather than rejecting entire fonts when it finds one issue. + Added some bounds checking when resolving variation data. + Fixed a bug where format 1 pair positioning records read the wrong variation data. + Fixed a sign extension bug when reading 24-bit integers. + Fixed a desync between SizeOfGlyphConfig and PlaceGlyphConfig. + Fixed some superfluous zeroed breaks getting generated in BreakEntireString. + Fixed memory leaks when parsing invalid fonts with FontFromFile and FontFromMemory. + Fixed a string ID mapping issue when variation data reused built-in font strings. + CodepointToGlyphId now returns the codepoint itself if the font is invalid. + New symbol: KBTS_MEMMOVE + kb_text_shape now needs a functional memmove implementation. + You can #define the symbol to provide your own, otherwise we will #include . + New enum member: KBTS_FONT_INFO_STRING_ID_RESERVED. This increments KBTS_FONT_INFO_STRING_ID_COUNT1 by 1. 2.32 - Various Unicode normalization performance improvements. Shaping can be up to 12% faster overall. Add support for 24-bit integers in delta set index maps (these are a font variation thing). Struct layout changes: @@ -3357,6 +3374,7 @@ enum kbts_font_info_string_id_enum KBTS_FONT_INFO_STRING_ID_LIGHT_BACKGROUND_PALETTE, KBTS_FONT_INFO_STRING_ID_DARK_BACKGROUND_PALETTE, KBTS_FONT_INFO_STRING_ID_VARIATIONS_POSTSCRIPT_NAME_PREFIX, + KBTS_FONT_INFO_STRING_ID_RESERVED, KBTS_FONT_INFO_STRING_ID_COUNT1, }; @@ -4844,8 +4862,8 @@ KBTS_EXPORT kbts_script kbts_ScriptTagToScript(kbts_script_tag Tag); #define KBTS__ARRAY_LENGTH(A) (sizeof(A)/sizeof(*(A))) #define KBTS__POINTER_AFTER(Type, X) ((Type *)((X) + 1)) #define KBTS__POINTER_OFFSET(Type, Base, Offset) ((Type *)((char *)(Base) + (Offset))) -#define KBTS__POINTER_DIFF32(Pointer, Base) ((kbts_u32)((char *)(Pointer) - (char *)(Base))) -#define KBTS__POINTER_DIFF(Pointer, Base) ((kbts_un)((char *)(Pointer) - (char *)(Base))) +#define KBTS__POINTER_DIFF32(Pointer, Base) ((kbts_u32)((kbts_uptr)(Pointer) - (kbts_uptr)(Base))) +#define KBTS__POINTER_DIFF(Pointer, Base) ((kbts_un)((kbts_uptr)(Pointer) - (kbts_uptr)(Base))) #define KBTS__ALIGN_POINTER(Type, Pointer, Align) (Type *)(((kbts_un)(Pointer) + ((Align) - 1)) & ~((Align) - 1)) #define KBTS__PASTE_1(A, B) A##B #define KBTS__PASTE_0(A, B) KBTS__PASTE_1(A, B) @@ -4894,6 +4912,11 @@ KBTS_EXPORT kbts_script kbts_ScriptTagToScript(kbts_script_tag Tag); #define KBTS_MEMCPY memcpy #endif +#ifndef KBTS_MEMMOVE +#include +#define KBTS_MEMMOVE memmove +#endif + #ifndef KB_TEXT_SHAPE_NO_CRT #ifndef KBTS_MALLOC #include @@ -5089,10 +5112,10 @@ KBTS_INLINE kbts_u32 kbts__LsbPositionOrBitWidth32(kbts_u32 X) // However, we do want to flag rphf/pref results for reordering, so we want to // keep all of the flags as usual, and only use these feature flags for filtering. #define KBTS__USE_GLYPH_FEATURE_MASK (KBTS_GLYPH_FLAG_ISOL | KBTS_GLYPH_FLAG_FINA | KBTS_GLYPH_FLAG_FIN2 | KBTS_GLYPH_FLAG_FIN3 | \ - KBTS_GLYPH_FLAG_MEDI | KBTS_GLYPH_FLAG_MED2 | KBTS_GLYPH_FLAG_INIT | KBTS_GLYPH_FLAG_NUMR | \ - KBTS_GLYPH_FLAG_DNOM | KBTS_GLYPH_FLAG_FRAC) + KBTS_GLYPH_FLAG_MEDI | KBTS_GLYPH_FLAG_MED2 | KBTS_GLYPH_FLAG_INIT | KBTS_GLYPH_FLAG_NUMR | \ + KBTS_GLYPH_FLAG_DNOM | KBTS_GLYPH_FLAG_FRAC) #define KBTS__JOINING_FEATURE_MASK (KBTS_GLYPH_FLAG_ISOL | KBTS_GLYPH_FLAG_FINA | KBTS_GLYPH_FLAG_FIN2 | KBTS_GLYPH_FLAG_FIN3 | \ - KBTS_GLYPH_FLAG_MEDI | KBTS_GLYPH_FLAG_MED2 | KBTS_GLYPH_FLAG_INIT) + KBTS_GLYPH_FLAG_MEDI | KBTS_GLYPH_FLAG_MED2 | KBTS_GLYPH_FLAG_INIT) #define KBTS__JOINING_FEATURE_TO_GLYPH_FLAG(Feature) (1 << ((Feature) - 1)) typedef kbts_u8 kbts__reph_position; @@ -13850,10 +13873,11 @@ typedef struct kbts__context_font_info typedef struct kbts__existing_shape_config { kbts_shape_config *Config; + kbts_u32 ConfigWriteCursor; + kbts_u32 CacheBinIndex; kbts_u32 FontHash; - kbts_script Script; - kbts_language Language; + kbts_u32 LastUsedTimestamp; } kbts__existing_shape_config; typedef kbts_u32 kbts__context_flags; @@ -13877,11 +13901,14 @@ typedef struct kbts__context_font typedef struct kbts__existing_glyph_config { - kbts_shape_config *ShapeConfig; + kbts_glyph_config *GlyphConfig; + kbts_feature_override *FeatureOverrides; int FeatureOverrideCount; + + kbts_u32 ConfigSize; + kbts_u32 ConfigWriteCursor; kbts_u32 Hash; - kbts_glyph_config *GlyphConfig; } kbts__existing_glyph_config; typedef struct kbts__existing_shape_config_block_header @@ -13895,10 +13922,16 @@ typedef struct kbts__existing_shape_config_block { kbts__existing_shape_config_block_header Header; - kbts_u32 Count; + kbts_u32 OccupiedBits; kbts__existing_shape_config Items[KBTS__EXISTING_SHAPE_CONFIGS_PER_BLOCK]; } kbts__existing_shape_config_block; +typedef struct kbts__huge_shape_config +{ + kbts__existing_shape_config_block *Block; + kbts_u32 IndexWithinBlock; +} kbts__huge_shape_config; + typedef struct kbts__existing_glyph_config_block_header { struct kbts__existing_glyph_config_block_header *Prev; @@ -13910,7 +13943,7 @@ typedef struct kbts__existing_glyph_config_block { kbts__existing_glyph_config_block_header Header; - kbts_u32 Count; + kbts_u32 OccupiedBits; kbts__existing_glyph_config Items[KBTS__EXISTING_GLYPH_CONFIGS_PER_BLOCK]; } kbts__existing_glyph_config_block; @@ -13953,6 +13986,26 @@ typedef struct kbts__context_font_block kbts__context_font Fonts[KBTS__CONTEXT_FONTS_PER_BLOCK]; } kbts__context_font_block; +enum {KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT = 3}; + +static const kbts_u32 kbts__ShapeConfigCacheBinSizes[KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT + 1] = { + 8 * 1024, + 32 * 1024, + 128 * 1024, + 512 * 1024, +}; + +typedef struct kbts__fifo_bin +{ + char *Base; + kbts_u32 WriteCursor; +} kbts__fifo_bin; + +enum { + KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE = 4096, + KBTS__HUGE_SHAPE_CONFIG_CAPACITY = 6, +}; + struct kbts_shape_context { kbts_arena PermanentArena; @@ -14000,9 +14053,17 @@ struct kbts_shape_context kbts_shape_codepoint_iterator RunCodepointIterator; kbts_b32 DoneShapingRuns; + kbts_u32 FindOrCreateShapeConfigTimestamp; + kbts_u32 FindOrCreateGlyphConfigTimestamp; kbts__existing_shape_config_block_header ExistingShapeConfigBlockSentinel; kbts__existing_glyph_config_block_header ExistingGlyphConfigBlockSentinel; + kbts_u32 HugeShapeConfigCount; + kbts__huge_shape_config HugeShapeConfigs[KBTS__HUGE_SHAPE_CONFIG_CAPACITY]; + + kbts__fifo_bin ShapeConfigCacheBins[KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT]; + kbts__fifo_bin GlyphConfigCacheBin; + kbts_u32 ScratchFeatureOverrideCount; kbts_feature_override ScratchFeatureOverrides[KBTS_MAX_SIMULTANEOUS_FEATURES]; @@ -14060,6 +14121,7 @@ struct kbts_shape_config kbts_shaper_properties *ShaperProperties; kbts_uid Uid; + kbts_u32 SizeNotCountingAlignment; kbts__indic_script_properties IndicScriptProperties; kbts__feature *Blwf; @@ -14072,9 +14134,9 @@ struct kbts_shape_config kbts_u32 GlyphCount; kbts_u32 LookupCount; - kbts_u16 *FeatureStageFirstLookupIndices; // [OpList.FeatureStageCount + 1] - kbts__sequential_lookup *SequentialLookups; // [LookupCount] - kbts_u32 *IdSequentialLookupMatrix; + kbts_u32 OffsetToFeatureStageFirstLookupIndices; // u16 [OpList.FeatureStageCount + 1] + kbts_u32 OffsetToSequentialLookups; // sequential_lookups[LookupCount] + kbts_u32 OffsetToIdSequentialLookupMatrix; // u32[] // Indic kbts_glyph Virama; @@ -14086,6 +14148,21 @@ struct kbts_shape_config kbts_glyph Nikhahit; kbts_glyph SaraAa; }; +KBTS_INLINE kbts_u16 *kbts__shape_config_FeatureStageFirstLookupIndices(kbts_shape_config *Config) +{ + kbts_u16 *Result = KBTS__POINTER_OFFSET(kbts_u16, Config, Config->OffsetToFeatureStageFirstLookupIndices); + return Result; +} +KBTS_INLINE kbts__sequential_lookup *kbts__shape_config_SequentialLookups(kbts_shape_config *Config) +{ + kbts__sequential_lookup *Result = KBTS__POINTER_OFFSET(kbts__sequential_lookup, Config, Config->OffsetToSequentialLookups); + return Result; +} +KBTS_INLINE kbts_u32 *kbts__shape_config_IdSequentialLookupMatrix(kbts_shape_config *Config) +{ + kbts_u32 *Result = KBTS__POINTER_OFFSET(kbts_u32, Config, Config->OffsetToIdSequentialLookupMatrix); + return Result; +} static const kbts_u8 kbts__CmapFormatPrecedence[14] = {1, 0, 1, 0, 2, 0, 1, 0, 3, 0, 3, 0, 4, 5}; @@ -15587,88 +15664,99 @@ static float kbts__ItemVariationStoreDelta(kbts__item_variation_store *Store, kb Store) { kbts_u32 *Store_VariationRegionListOffset = kbts__item_variation_store_VariationRegionListOffset(Store); + kbts_u16 *Store_ItemVariationDataCount = kbts__item_variation_store_ItemVariationDataCount(Store); kbts_u32 *Store_ItemVariationDataOffsets = kbts__item_variation_store_ItemVariationDataOffsets(Store); - kbts_un VariationRegionListOffset = kbts__ReadU32Unaligned(Store_VariationRegionListOffset); + kbts_un VariationDataCount = kbts__ReadU16Unaligned(Store_ItemVariationDataCount); - kbts__variation_region_list *VariationRegionList = KBTS__POINTER_OFFSET(kbts__variation_region_list, Store, VariationRegionListOffset); - kbts__region_axis_coordinates *RegionAxisCoordinates = KBTS__POINTER_AFTER(kbts__region_axis_coordinates, VariationRegionList); - kbts_un AxisCount = kbts__ReadU16Unaligned(&VariationRegionList->AxisCount); - - kbts__item_variation_data *ItemVariationData = KBTS__POINTER_OFFSET(kbts__item_variation_data, Store, kbts__ReadU32Unaligned(&Store_ItemVariationDataOffsets[DeltaSetOuterIndex])); - kbts_un RegionIndexCount = kbts__ReadU16Unaligned(&ItemVariationData->RegionIndexCount); - kbts_un WordDeltaCount = kbts__ReadU16Unaligned(&ItemVariationData->WordDeltaCount); - - kbts_u16 *RegionIndices = KBTS__POINTER_AFTER(kbts_u16, ItemVariationData); - char *Deltas = (char *)(RegionIndices + RegionIndexCount); - kbts_s16 *WordDeltas = (kbts_s16 *)(Deltas + DeltaSetInnerIndex * (WordDeltaCount + RegionIndexCount)); - kbts_s8 *HalfWordDeltas = (kbts_s8 *)(WordDeltas + WordDeltaCount); - - KBTS__FOR(RegionIndexIndex, 0, RegionIndexCount) + if(DeltaSetOuterIndex < VariationDataCount) { - kbts_un RegionIndex = kbts__ReadU16Unaligned(&RegionIndices[RegionIndexIndex]); - kbts__region_axis_coordinates *RegionCoords = &RegionAxisCoordinates[RegionIndex * AxisCount]; + kbts_un ItemVariationDataOffset = kbts__ReadU32Unaligned(&Store_ItemVariationDataOffsets[DeltaSetOuterIndex]); - float RegionDelta = 0; - if(RegionIndexIndex < WordDeltaCount) + if(ItemVariationDataOffset) { - RegionDelta = (float)kbts__ReadS16Unaligned(&WordDeltas[RegionIndexIndex]); - } - else - { - RegionDelta = (float)HalfWordDeltas[RegionIndexIndex - WordDeltaCount]; - } + kbts_un VariationRegionListOffset = kbts__ReadU32Unaligned(Store_VariationRegionListOffset); - float RegionFactor = 1; + kbts__variation_region_list *VariationRegionList = KBTS__POINTER_OFFSET(kbts__variation_region_list, Store, VariationRegionListOffset); + kbts__region_axis_coordinates *RegionAxisCoordinates = KBTS__POINTER_AFTER(kbts__region_axis_coordinates, VariationRegionList); + kbts__item_variation_data *ItemVariationData = KBTS__POINTER_OFFSET(kbts__item_variation_data, Store, ItemVariationDataOffset); + kbts_u16 *RegionIndices = KBTS__POINTER_AFTER(kbts_u16, ItemVariationData); - KBTS__FOR(AxisIndex, 0, AxisCount) - { - kbts_s16 InputFixed = VariationVectorNormalized[AxisIndex]; + kbts_un AxisCount = kbts__ReadU16Unaligned(&VariationRegionList->AxisCount); + kbts_un RegionIndexCount = kbts__ReadU16Unaligned(&ItemVariationData->RegionIndexCount); + kbts_un WordDeltaCount = kbts__ReadU16Unaligned(&ItemVariationData->WordDeltaCount); - kbts__region_axis_coordinates *RegionAxis = &RegionCoords[AxisIndex]; - kbts_s16 LowFixed = kbts__ReadS16Unaligned(&RegionAxis->StartCoord); - kbts_s16 PeakFixed = kbts__ReadS16Unaligned(&RegionAxis->PeakCoord); - kbts_s16 HighFixed = kbts__ReadS16Unaligned(&RegionAxis->EndCoord); + char *Deltas = (char *)(RegionIndices + RegionIndexCount); + kbts_s16 *WordDeltas = (kbts_s16 *)(Deltas + DeltaSetInnerIndex * (WordDeltaCount + RegionIndexCount)); + kbts_s8 *HalfWordDeltas = (kbts_s8 *)(WordDeltas + WordDeltaCount); - if(PeakFixed && - (LowFixed <= PeakFixed) && - (PeakFixed <= HighFixed) && - !((LowFixed < 0) && (HighFixed > 0) && (PeakFixed != 0)) && - (InputFixed != PeakFixed)) + KBTS__FOR(RegionIndexIndex, 0, RegionIndexCount) { - float AxisFactor = 0; + kbts_un RegionIndex = kbts__ReadU16Unaligned(&RegionIndices[RegionIndexIndex]); + kbts__region_axis_coordinates *RegionCoords = &RegionAxisCoordinates[RegionIndex * AxisCount]; - if((InputFixed >= LowFixed) && - (InputFixed <= HighFixed)) + float RegionDelta = 0; + if(RegionIndexIndex < WordDeltaCount) { - // @Cleanup: Why don't we just compute the factor, and then check for bounds and NaN afterwards? - float InputFloat = kbts__Fixed2_14ToFloat(InputFixed); - float PeakFloat = kbts__Fixed2_14ToFloat(PeakFixed); + RegionDelta = (float)kbts__ReadS16Unaligned(&WordDeltas[RegionIndexIndex]); + } + else + { + RegionDelta = (float)HalfWordDeltas[RegionIndexIndex - WordDeltaCount]; + } - if(InputFixed < PeakFixed) + float RegionFactor = 1; + + KBTS__FOR(AxisIndex, 0, AxisCount) + { + kbts_s16 InputFixed = VariationVectorNormalized[AxisIndex]; + + kbts__region_axis_coordinates *RegionAxis = &RegionCoords[AxisIndex]; + kbts_s16 LowFixed = kbts__ReadS16Unaligned(&RegionAxis->StartCoord); + kbts_s16 PeakFixed = kbts__ReadS16Unaligned(&RegionAxis->PeakCoord); + kbts_s16 HighFixed = kbts__ReadS16Unaligned(&RegionAxis->EndCoord); + + if(PeakFixed && + (LowFixed <= PeakFixed) && + (PeakFixed <= HighFixed) && + !((LowFixed < 0) && (HighFixed > 0) && (PeakFixed != 0)) && + (InputFixed != PeakFixed)) { - float LowFloat = kbts__Fixed2_14ToFloat(LowFixed); + float AxisFactor = 0; - AxisFactor = (InputFloat - LowFloat) / (PeakFloat - LowFloat); + if((InputFixed >= LowFixed) && + (InputFixed <= HighFixed)) + { + // @Cleanup: Why don't we just compute the factor, and then check for bounds and NaN afterwards? + float InputFloat = kbts__Fixed2_14ToFloat(InputFixed); + float PeakFloat = kbts__Fixed2_14ToFloat(PeakFixed); + + if(InputFixed < PeakFixed) + { + float LowFloat = kbts__Fixed2_14ToFloat(LowFixed); + + AxisFactor = (InputFloat - LowFloat) / (PeakFloat - LowFloat); + } + else + { + float HighFloat = kbts__Fixed2_14ToFloat(HighFixed); + + AxisFactor = (HighFloat - InputFloat) / (HighFloat - PeakFloat); + } + } + + RegionFactor *= AxisFactor; } - else - { - float HighFloat = kbts__Fixed2_14ToFloat(HighFixed); - AxisFactor = (HighFloat - InputFloat) / (HighFloat - PeakFloat); + if(RegionFactor == 0) + { + break; } } - RegionFactor *= AxisFactor; - } - - if(RegionFactor == 0) - { - break; + Result += RegionDelta * RegionFactor; } } - - Result += RegionDelta * RegionFactor; } } @@ -16176,7 +16264,8 @@ typedef struct kbts__byteswap_context char *BoundsStart; char *BoundsEnd; - int Error; + kbts_b32 ErrorInCurrentBounds; + kbts_b32 ErrorInAnyBounds; } kbts__byteswap_context; typedef struct kbts__bounds @@ -16194,13 +16283,18 @@ static kbts__bounds kbts__BeginBounds(kbts__byteswap_context *Context, void *Sta Context->BoundsStart = (char *)Start; Context->BoundsEnd = (char *)End; + Context->ErrorInAnyBounds |= Context->ErrorInCurrentBounds; + Context->ErrorInCurrentBounds = 0; + return Result; } -static void kbts__EndBounds(kbts__byteswap_context *Context, kbts__bounds *Bounds) +static kbts_b32 kbts__EndBounds(kbts__byteswap_context *Context, kbts__bounds *Bounds) { Context->BoundsStart = Bounds->Start; Context->BoundsEnd = Bounds->End; + + return Context->ErrorInCurrentBounds; } typedef kbts_u32 kbts__check_and_mark_region_result_flags; @@ -16215,7 +16309,7 @@ static kbts_b32 kbts__AddressIsMarked(kbts__byteswap_context *Context, void *Poi { kbts_b32 Result = 1; - if(!Context->Error && Pointer) + if(!Context->ErrorInCurrentBounds && Pointer) { if(((char *)Pointer >= Context->BoundsStart) && ((char *)Pointer < Context->BoundsEnd)) { @@ -16230,7 +16324,7 @@ static kbts_b32 kbts__AddressIsMarked(kbts__byteswap_context *Context, void *Poi } else { - Context->Error = 1; + Context->ErrorInCurrentBounds = 1; } } @@ -16290,7 +16384,7 @@ static kbts__check_and_mark_region_result_flags kbts__CheckAndMarkRegion(kbts__b { kbts__check_and_mark_region_result_flags Result = 0; - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { if(((char *)Start <= (char *)End) && ((char *)Start >= Context->BoundsStart) && ((char *)End <= (char *)Context->BoundsEnd)) @@ -16333,13 +16427,13 @@ static kbts__check_and_mark_region_result_flags kbts__CheckAndMarkRegion(kbts__b if(UnexpectedBits) { - Context->Error = 1; + Context->ErrorInCurrentBounds = 1; break; } } - if(!Context->Error & !ExpectedBitsMask) + if(!Context->ErrorInCurrentBounds & !ExpectedBitsMask) { // All good. We can mark. Result |= KBTS__CHECK_AND_MARK_REGION_RESULT_FLAG_NEEDS_BYTESWAP; @@ -16364,7 +16458,7 @@ static kbts__check_and_mark_region_result_flags kbts__CheckAndMarkRegion(kbts__b } else { - Context->Error = 1; + Context->ErrorInCurrentBounds = 1; } } @@ -16379,7 +16473,7 @@ static kbts_b32 kbts__MarkOkToReadContext(kbts__byteswap_context *Context, void kbts__MarkOkToRead(Pointer, Size); } - return !Context->Error; + return !Context->ErrorInCurrentBounds; } static kbts_b32 kbts__ByteSwapArray16Context(kbts__byteswap_context *Context, kbts_u16 *Array, kbts_un Count) @@ -16390,7 +16484,7 @@ static kbts_b32 kbts__ByteSwapArray16Context(kbts__byteswap_context *Context, kb kbts__ByteSwapArray16Unchecked(Array, Count); } - return !Context->Error; + return !Context->ErrorInCurrentBounds; } static kbts_b32 kbts__ByteSwapArray32Context(kbts__byteswap_context *Context, kbts_u32 *Array, kbts_un Count) @@ -16401,7 +16495,7 @@ static kbts_b32 kbts__ByteSwapArray32Context(kbts__byteswap_context *Context, kb kbts__ByteSwapArray32Unchecked(Array, Count); } - return !Context->Error; + return !Context->ErrorInCurrentBounds; } static kbts_b32 kbts__ByteSwap16InPlaceContext(kbts__byteswap_context *Context, kbts_u16 *Pointer, kbts_un *ByteSwappedValue) @@ -16426,7 +16520,7 @@ static kbts_b32 kbts__ByteSwap16InPlaceContext(kbts__byteswap_context *Context, *ByteSwappedValue = Value; } - return !Context->Error; + return !Context->ErrorInCurrentBounds; } static kbts_b32 kbts__ByteSwap32InPlaceContext(kbts__byteswap_context *Context, kbts_u32 *Pointer, kbts_un *ByteSwappedValue) @@ -16451,7 +16545,7 @@ static kbts_b32 kbts__ByteSwap32InPlaceContext(kbts__byteswap_context *Context, *ByteSwappedValue = Value; } - return !Context->Error; + return !Context->ErrorInCurrentBounds; } typedef struct kbts__cover_glyph_result @@ -16565,7 +16659,7 @@ static void kbts__ByteSwapGsubGposCommon(kbts__byteswap_context *Context, kbts__ if((char *)(ScriptRecords + ScriptListScriptCount) > Context->BoundsEnd) { - Context->Error = 1; + Context->ErrorInCurrentBounds = 1; return; } @@ -16663,23 +16757,29 @@ static void kbts__ByteSwapGsubGposCommon(kbts__byteswap_context *Context, kbts__ if(kbts__ByteSwapArray32Context(Context, &Record->ConditionSetOffset, 2)) { kbts_un ConditionSetOffset = kbts__ReadU32Unaligned(&Record->ConditionSetOffset); - kbts__condition_set *Set = KBTS__POINTER_OFFSET(kbts__condition_set, FeatureVariations, ConditionSetOffset); - kbts_un Set_Count; - if(kbts__ByteSwap16InPlaceContext(Context, &Set->Count, &Set_Count)) + if(ConditionSetOffset) { - kbts_u32 *ConditionOffsets = KBTS__POINTER_AFTER(kbts_u32, Set); - if(kbts__ByteSwapArray32Context(Context, ConditionOffsets, Set_Count)) + kbts__condition_set *Set = KBTS__POINTER_OFFSET(kbts__condition_set, FeatureVariations, ConditionSetOffset); + kbts_un Set_Count; + if(kbts__ByteSwap16InPlaceContext(Context, &Set->Count, &Set_Count)) { - KBTS__FOR(ConditionIndex, 0, Set_Count) + kbts_u32 *ConditionOffsets = KBTS__POINTER_AFTER(kbts_u32, Set); + if(kbts__ByteSwapArray32Context(Context, ConditionOffsets, Set_Count)) { - kbts_un ConditionOffset = kbts__ReadU32Unaligned(&ConditionOffsets[ConditionIndex]); - kbts__condition_1 *Condition = KBTS__POINTER_OFFSET(kbts__condition_1, Set, ConditionOffset); + KBTS__FOR(ConditionIndex, 0, Set_Count) + { + kbts_un ConditionOffset = kbts__ReadU32Unaligned(&ConditionOffsets[ConditionIndex]); + kbts__condition_1 *Condition = KBTS__POINTER_OFFSET(kbts__condition_1, Set, ConditionOffset); - kbts__ByteSwapArray16Context(Context, &Condition->Format, 4); + kbts__ByteSwapArray16Context(Context, &Condition->Format, 4); + } } } + } - kbts_un FeatureTableSubstitutionOffset = kbts__ReadU32Unaligned(&Record->FeatureTableSubstitutionOffset); + kbts_un FeatureTableSubstitutionOffset = kbts__ReadU32Unaligned(&Record->FeatureTableSubstitutionOffset); + if(FeatureTableSubstitutionOffset) + { kbts__feature_table_substitution *FeatureSubst = KBTS__POINTER_OFFSET(kbts__feature_table_substitution, FeatureVariations, FeatureTableSubstitutionOffset); if(kbts__ByteSwapArray16Context(Context, &FeatureSubst->Major, 3)) { @@ -16922,7 +17022,7 @@ static void kbts__ByteSwapDeltaSetIndexMap(kbts__byteswap_context *Context, kbts if((Entries + EntryCount * BytesPerEntry) > Context->FileEnd) { - Context->Error |= 1; + Context->ErrorInCurrentBounds |= 1; } // If BytesPerEntry == 1, we have nothing to byteswap. @@ -16947,12 +17047,12 @@ static void kbts__ByteSwapDeltaSetIndexMap(kbts__byteswap_context *Context, kbts kbts__ByteSwapArray32Context(Context, (kbts_u32 *)Entries, EntryCount); } break; - default: Context->Error |= 1; break; + default: Context->ErrorInCurrentBounds |= 1; break; } } else { - Context->Error |= 1; + Context->ErrorInCurrentBounds |= 1; } } } @@ -16983,7 +17083,7 @@ static void kbts__ByteSwapItemVariationStore(kbts__byteswap_context *Context, kb kbts__ByteSwapArray16Context(Context, (kbts_u16 *)RegionAxes, VariationRegionList_RegionCount * VariationRegionList_AxisCount * 3); } - if(!Context->Error && + if(!Context->ErrorInCurrentBounds && kbts__ByteSwapArray32Context(Context, Store_ItemVariationDataOffsets, ItemVariationDataCount)) { KBTS__FOR(ItemVariationDataIndex, 0, ItemVariationDataCount) @@ -17317,7 +17417,7 @@ static void kbts__ByteSwapSequenceContextSubtable(kbts__byteswap_context *Contex kbts_u16 *ClassDefBase = KBTS__POINTER_OFFSET(kbts_u16, Subst, Subst_ClassDefOffset); kbts__ByteSwapClassDefinition(Context, ClassDefBase); - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { KBTS__FOR(SetIndex, 0, Subst_ClassSequenceRuleSetCount) { @@ -17346,7 +17446,7 @@ static void kbts__ByteSwapSequenceContextSubtable(kbts__byteswap_context *Contex } #ifdef KBTS_DUMP - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { kbts__DumpClassDefinition(ClassDefBase); KBTS__FOR(SetIndex, 0, Subst_ClassSequenceRuleSetCount) @@ -17732,7 +17832,7 @@ static void kbts__ByteSwapChainedSequenceContextSubtable(kbts__byteswap_context kbts_u16 *LookaheadClassDefinition = KBTS__POINTER_OFFSET(kbts_u16, Subst, kbts__ReadU16Unaligned(&Subst->LookaheadClassDefOffset)); kbts__ByteSwapClassDefinition(Context, LookaheadClassDefinition); - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { #ifdef KBTS_DUMP KBTS_DUMPF("Backtrack classes:\n"); @@ -17749,7 +17849,7 @@ static void kbts__ByteSwapChainedSequenceContextSubtable(kbts__byteswap_context kbts__ByteSwapChainedSequenceRuleSet(Context, Set); #ifdef KBTS_DUMP - if(!Context->Error && Set) + if(!Context->ErrorInCurrentBounds && Set) { kbts_un Set_Count = kbts__ReadU16Unaligned(&Set->Count); @@ -17878,7 +17978,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb } kbts_un LookupFormat; - if(!Context->Error && + if(!Context->ErrorInCurrentBounds && kbts__ByteSwap16InPlaceContext(Context, Base, &LookupFormat)) { KBTS_DUMPF(" Type %llu.%llu\n" @@ -17894,7 +17994,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb kbts__ByteSwapCoverage(Context, Coverage); # ifdef KBTS_DUMP - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { kbts_u16 Coverage_Format = kbts__ReadU16Unaligned(&Coverage->Format); kbts_un Coverage_Count = kbts__ReadU16Unaligned(&Coverage->Count); @@ -17945,7 +18045,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb kbts__ByteSwapArray16Context(Context, GlyphIds, Subst_GlyphCount); #ifdef KBTS_DUMP - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { KBTS_DUMPF(" ["); KBTS__FOR(IdIndex, 0, Subst_GlyphCount) @@ -17979,7 +18079,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb } #ifdef KBTS_DUMP - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { KBTS_DUMPF(" ["); kbts_u16 *SequenceGlyphIds = KBTS__POINTER_AFTER(kbts_u16, Sequence); @@ -18014,7 +18114,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb } #ifdef KBTS_DUMP - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { KBTS_DUMPF(" ["); kbts_u16 *SetGlyphIds = KBTS__POINTER_AFTER(kbts_u16, Set); @@ -18059,7 +18159,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb kbts__ByteSwapArray16Context(Context, KBTS__POINTER_AFTER(kbts_u16, Ligature), Ligature_ComponentCount - 1); #ifdef KBTS_DUMP - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { KBTS_DUMPF("ligature: ["); kbts_u16 *ComponentIds = KBTS__POINTER_AFTER(kbts_u16, Ligature); @@ -18073,7 +18173,7 @@ static void kbts__ByteSwapGsubLookupSubtable(kbts__byteswap_context *Context, kb } else { - Context->Error = 1; + Context->ErrorInCurrentBounds = 1; } } } @@ -18141,7 +18241,7 @@ static void kbts__ByteSwapGposLookupSubtable(kbts__byteswap_context *Context, kb } } - if(!Context->Error) + if(!Context->ErrorInCurrentBounds) { switch(LookupType) { @@ -18439,145 +18539,150 @@ static int kbts__ShaperRtl(kbts_shaper Shaper) KBTS_EXPORT int kbts_CodepointToGlyphId(kbts_font *Font, int ICodepoint) { - int Result = 0; - kbts_u32 Codepoint = (kbts_u32)ICodepoint; + int Result = ICodepoint; - kbts_u16 *CmapBase = Font->Cmap; - if(CmapBase) + if(kbts_FontIsValid(Font)) { - kbts_u16 CmapFormat = kbts__ReadU16Unaligned(CmapBase); - switch(CmapFormat) + Result = 0; + kbts_u32 Codepoint = (kbts_u32)ICodepoint; + kbts_u16 *CmapBase = Font->Cmap; + + if(CmapBase) { - case 0: - { - kbts__cmap_0 *Cmap0 = (kbts__cmap_0 *)CmapBase; - - if((kbts_un)Codepoint < KBTS__ARRAY_LENGTH(Cmap0->GlyphIdArray)) + kbts_u16 CmapFormat = kbts__ReadU16Unaligned(CmapBase); + switch(CmapFormat) { - Result = Cmap0->GlyphIdArray[Codepoint]; - } - } break; - - case 2: - { - kbts__cmap_2 *Cmap2 = (kbts__cmap_2 *)CmapBase; - kbts__sub_header *SubHeaders = KBTS__POINTER_AFTER(kbts__sub_header, Cmap2); - - kbts_u32 High = (Codepoint >> 8) & 0xFF; - - if(!(Codepoint & 0xFF00)) + case 0: { - High = Codepoint & 0xFF; - } + kbts__cmap_0 *Cmap0 = (kbts__cmap_0 *)CmapBase; - // The Microsoft documentation doesn't mention that the SubHeaderKeys are indices multiplied by 8, for some - // reason..! The Apple documentation does. - kbts_u16 SubHeaderIndex = kbts__ReadU16Unaligned(&Cmap2->SubHeaderKeys[High]) / 8; - kbts__sub_header *SubHeader = &SubHeaders[SubHeaderIndex]; - - if(!SubHeaderIndex) - { - // With SubHeader 0, we only use the first byte. - Codepoint = High; - } - else - { - Codepoint = Codepoint & 0xFF; - } - - kbts_u32 Offset = Codepoint - kbts__ReadU16Unaligned(&SubHeader->FirstCode); - if(Offset < kbts__ReadU16Unaligned(&SubHeader->EntryCount)) - { - kbts_u16 *GlyphIds = KBTS__POINTER_OFFSET(kbts_u16, &SubHeader->IdRangeOffset, kbts__ReadU16Unaligned(&SubHeader->IdRangeOffset)); - kbts_un GlyphId = kbts__ReadU16Unaligned(&GlyphIds[Offset]); - - if(GlyphId) + if((kbts_un)Codepoint < KBTS__ARRAY_LENGTH(Cmap0->GlyphIdArray)) { - GlyphId += kbts__ReadS16Unaligned(&SubHeader->IdDelta); + Result = Cmap0->GlyphIdArray[Codepoint]; + } + } break; + + case 2: + { + kbts__cmap_2 *Cmap2 = (kbts__cmap_2 *)CmapBase; + kbts__sub_header *SubHeaders = KBTS__POINTER_AFTER(kbts__sub_header, Cmap2); + + kbts_u32 High = (Codepoint >> 8) & 0xFF; + + if(!(Codepoint & 0xFF00)) + { + High = Codepoint & 0xFF; } - Result = (kbts_u16)GlyphId; - } - } break; + // The Microsoft documentation doesn't mention that the SubHeaderKeys are indices multiplied by 8, for some + // reason..! The Apple documentation does. + kbts_u16 SubHeaderIndex = kbts__ReadU16Unaligned(&Cmap2->SubHeaderKeys[High]) / 8; + kbts__sub_header *SubHeader = &SubHeaders[SubHeaderIndex]; - case 4: - { - kbts__cmap_4 *Cmap4 = (kbts__cmap_4 *)CmapBase; - kbts_un SegmentCount = kbts__ReadU16Unaligned(&Cmap4->SegmentCountTimesTwo) / 2; - - if(SegmentCount) - { - kbts_u16 *EndCodes = KBTS__POINTER_AFTER(kbts_u16, Cmap4); - kbts_u16 *StartCodes = EndCodes + SegmentCount + 1; - kbts_s16 *IdDeltas = (kbts_s16 *)(StartCodes + SegmentCount); - kbts_u16 *IdRangeOffsets = (kbts_u16 *)(IdDeltas + SegmentCount); - kbts_un SegmentIndexOffset = 0; - - while(SegmentCount > 1) + if(!SubHeaderIndex) { - kbts_un HalfCount = SegmentCount / 2; - SegmentIndexOffset = (kbts__ReadU16Unaligned(&EndCodes[SegmentIndexOffset + HalfCount - 1]) < Codepoint) ? (SegmentIndexOffset + HalfCount) : SegmentIndexOffset; - SegmentCount -= HalfCount; + // With SubHeader 0, we only use the first byte. + Codepoint = High; + } + else + { + Codepoint = Codepoint & 0xFF; } - kbts_u16 Start = kbts__ReadU16Unaligned(&StartCodes[SegmentIndexOffset]); - if((Codepoint >= Start) && (Codepoint <= kbts__ReadU16Unaligned(&EndCodes[SegmentIndexOffset]))) + kbts_u32 Offset = Codepoint - kbts__ReadU16Unaligned(&SubHeader->FirstCode); + if(Offset < kbts__ReadU16Unaligned(&SubHeader->EntryCount)) { - kbts_s16 Delta = kbts__ReadS16Unaligned(&IdDeltas[SegmentIndexOffset]); - kbts_u16 RangeOffset = kbts__ReadU16Unaligned(&IdRangeOffsets[SegmentIndexOffset]); + kbts_u16 *GlyphIds = KBTS__POINTER_OFFSET(kbts_u16, &SubHeader->IdRangeOffset, kbts__ReadU16Unaligned(&SubHeader->IdRangeOffset)); + kbts_un GlyphId = kbts__ReadU16Unaligned(&GlyphIds[Offset]); - kbts_u16 GlyphId = (kbts_u16)Delta; - if(RangeOffset) + if(GlyphId) { - GlyphId += *(&IdRangeOffsets[SegmentIndexOffset] + (Codepoint - Start) + RangeOffset / 2); + GlyphId += kbts__ReadS16Unaligned(&SubHeader->IdDelta); } - else - { - GlyphId += (kbts_u16)(Codepoint); - } - Result = GlyphId; + + Result = (kbts_u16)GlyphId; } - } - } break; + } break; - case 6: - { - kbts__cmap_6 *Cmap6 = (kbts__cmap_6 *)CmapBase; - kbts_u16 *GlyphIds = KBTS__POINTER_AFTER(kbts_u16, Cmap6); - - kbts_un Offset = Codepoint - kbts__ReadU16Unaligned(&Cmap6->FirstCode); - if(Offset < kbts__ReadU16Unaligned(&Cmap6->EntryCount)) + case 4: { - Result = kbts__ReadU16Unaligned(&GlyphIds[Offset]); - } - } break; + kbts__cmap_4 *Cmap4 = (kbts__cmap_4 *)CmapBase; + kbts_un SegmentCount = kbts__ReadU16Unaligned(&Cmap4->SegmentCountTimesTwo) / 2; - case 12: - { - kbts__cmap_12_13 *Cmap12 = (kbts__cmap_12_13 *)CmapBase; - kbts__sequential_map_group *Groups = KBTS__POINTER_AFTER(kbts__sequential_map_group, Cmap12); - - kbts_un GlyphId = 0; - kbts_un GroupCount = kbts__ReadU32Unaligned(&Cmap12->GroupCount); - if(GroupCount) - { - while(GroupCount > 1) + if(SegmentCount) { - kbts_un HalfCount = GroupCount / 2; - Groups = (kbts__ReadU32Unaligned(&Groups[HalfCount - 1].EndCharacterCode) < Codepoint) ? (Groups + HalfCount) : Groups; - GroupCount -= HalfCount; + kbts_u16 *EndCodes = KBTS__POINTER_AFTER(kbts_u16, Cmap4); + kbts_u16 *StartCodes = EndCodes + SegmentCount + 1; + kbts_s16 *IdDeltas = (kbts_s16 *)(StartCodes + SegmentCount); + kbts_u16 *IdRangeOffsets = (kbts_u16 *)(IdDeltas + SegmentCount); + kbts_un SegmentIndexOffset = 0; + + while(SegmentCount > 1) + { + kbts_un HalfCount = SegmentCount / 2; + SegmentIndexOffset = (kbts__ReadU16Unaligned(&EndCodes[SegmentIndexOffset + HalfCount - 1]) < Codepoint) ? (SegmentIndexOffset + HalfCount) : SegmentIndexOffset; + SegmentCount -= HalfCount; + } + + kbts_u16 Start = kbts__ReadU16Unaligned(&StartCodes[SegmentIndexOffset]); + if((Codepoint >= Start) && (Codepoint <= kbts__ReadU16Unaligned(&EndCodes[SegmentIndexOffset]))) + { + kbts_s16 Delta = kbts__ReadS16Unaligned(&IdDeltas[SegmentIndexOffset]); + kbts_u16 RangeOffset = kbts__ReadU16Unaligned(&IdRangeOffsets[SegmentIndexOffset]); + + kbts_u16 GlyphId = (kbts_u16)Delta; + if(RangeOffset) + { + GlyphId += *(&IdRangeOffsets[SegmentIndexOffset] + (Codepoint - Start) + RangeOffset / 2); + } + else + { + GlyphId += (kbts_u16)(Codepoint); + } + Result = GlyphId; + } } - } + } break; - if((Codepoint >= kbts__ReadU32Unaligned(&Groups->StartCharacterCode)) && - (Codepoint <= kbts__ReadU32Unaligned(&Groups->EndCharacterCode))) + case 6: { - kbts_un Offset = Codepoint - kbts__ReadU32Unaligned(&Groups->StartCharacterCode); - GlyphId = kbts__ReadU32Unaligned(&Groups->StartGlyphId) + Offset; - } + kbts__cmap_6 *Cmap6 = (kbts__cmap_6 *)CmapBase; + kbts_u16 *GlyphIds = KBTS__POINTER_AFTER(kbts_u16, Cmap6); - Result = (int)GlyphId; - } break; + kbts_un Offset = Codepoint - kbts__ReadU16Unaligned(&Cmap6->FirstCode); + if(Offset < kbts__ReadU16Unaligned(&Cmap6->EntryCount)) + { + Result = kbts__ReadU16Unaligned(&GlyphIds[Offset]); + } + } break; + + case 12: + { + kbts__cmap_12_13 *Cmap12 = (kbts__cmap_12_13 *)CmapBase; + kbts__sequential_map_group *Groups = KBTS__POINTER_AFTER(kbts__sequential_map_group, Cmap12); + + kbts_un GlyphId = 0; + kbts_un GroupCount = kbts__ReadU32Unaligned(&Cmap12->GroupCount); + if(GroupCount) + { + while(GroupCount > 1) + { + kbts_un HalfCount = GroupCount / 2; + Groups = (kbts__ReadU32Unaligned(&Groups[HalfCount - 1].EndCharacterCode) < Codepoint) ? (Groups + HalfCount) : Groups; + GroupCount -= HalfCount; + } + } + + if((Codepoint >= kbts__ReadU32Unaligned(&Groups->StartCharacterCode)) && + (Codepoint <= kbts__ReadU32Unaligned(&Groups->EndCharacterCode))) + { + kbts_un Offset = Codepoint - kbts__ReadU32Unaligned(&Groups->StartCharacterCode); + GlyphId = kbts__ReadU32Unaligned(&Groups->StartGlyphId) + Offset; + } + + Result = (int)GlyphId; + } break; + } } } @@ -18881,13 +18986,13 @@ static void *kbts__PointerPush(kbts__pointer_bump_allocator *Alloc, kbts_un Size KBTS_INLINE kbts_un kbts__GsubSequentialLookupCount(kbts_shape_config *Config) { - kbts_un Result = Config ? Config->FeatureStageFirstLookupIndices[Config->OpList.FeatureStageCount - 1] : 0; + kbts_un Result = Config ? kbts__shape_config_FeatureStageFirstLookupIndices(Config)[Config->OpList.FeatureStageCount - 1] : 0; return Result; } KBTS_INLINE kbts_un kbts__SequentialLookupCount(kbts_shape_config *Config) { - kbts_un Result = Config ? Config->FeatureStageFirstLookupIndices[Config->OpList.FeatureStageCount] : 0; + kbts_un Result = Config ? kbts__shape_config_FeatureStageFirstLookupIndices(Config)[Config->OpList.FeatureStageCount] : 0; return Result; } @@ -19440,7 +19545,7 @@ static kbts_b32 kbts__BucketGlyph(kbts_shape_scratchpad *Scratchpad, kbts_glyph if(!GlyphConfig) { - kbts_u32 *IdSequentialLookupMatrix = Config->IdSequentialLookupMatrix; + kbts_u32 *IdSequentialLookupMatrix = kbts__shape_config_IdSequentialLookupMatrix(Config); kbts__matrix_index MatrixIndex = kbts__IdSequentialLookupMatrixIndex(MinimumSequentialLookupIndex, Glyph->Id, SequentialLookupCount); kbts_u32 *At = &IdSequentialLookupMatrix[MatrixIndex.WordIndex]; // Mask out lookups < MinimumSequentialLookupIndex. @@ -19468,7 +19573,7 @@ static kbts_b32 kbts__BucketGlyph(kbts_shape_scratchpad *Scratchpad, kbts_glyph } else { - kbts_u32 *IdSequentialLookupMatrix = Config->IdSequentialLookupMatrix; + kbts_u32 *IdSequentialLookupMatrix = kbts__shape_config_IdSequentialLookupMatrix(Config); kbts__matrix_index MatrixIndex = kbts__IdSequentialLookupMatrixIndex(MinimumSequentialLookupIndex, Glyph->Id, SequentialLookupCount); kbts__matrix_index RowIndex = kbts__IdSequentialLookupMatrixIndex(MinimumSequentialLookupIndex, 0, SequentialLookupCount); @@ -20408,6 +20513,7 @@ static kbts_b32 kbts__DoSingleAdjustment(kbts_shape_scratchpad *Scratchpad, kbts { kbts_u16 NextGlyphId = NextGlyph->Id; + void *ValueRecordParent = Base; kbts_u16 *Unpacked1Base; kbts_u16 *Unpacked2Base; kbts_u16 ValueFormat1; @@ -20452,6 +20558,7 @@ static kbts_b32 kbts__DoSingleAdjustment(kbts_shape_scratchpad *Scratchpad, kbts kbts_u16 *Records = KBTS__POINTER_AFTER(kbts_u16, PairRecord); \ Unpacked1Base = Records; \ Unpacked2Base = Records + Size1; \ + ValueRecordParent = Set; \ KBTS_INSTRUMENT_BLOCK_END(PairSearch); \ goto ApplyPairPositioning; \ } @@ -20489,6 +20596,7 @@ static kbts_b32 kbts__DoSingleAdjustment(kbts_shape_scratchpad *Scratchpad, kbts Unpacked1Base = Records; Unpacked2Base = Records + Size1; + ValueRecordParent = Set; KBTS_INSTRUMENT_BLOCK_END(PairSearch); goto ApplyPairPositioning; @@ -20544,7 +20652,7 @@ static kbts_b32 kbts__DoSingleAdjustment(kbts_shape_scratchpad *Scratchpad, kbts KBTS_INSTRUMENT_BLOCK_BEGIN(ApplyPairPositioning); - kbts__resolved_value_record ValueRecord1 = kbts__ResolveValueRecord(Scratchpad, Base, ValueFormat1, Unpacked1Base); + kbts__resolved_value_record ValueRecord1 = kbts__ResolveValueRecord(Scratchpad, ValueRecordParent, ValueFormat1, Unpacked1Base); kbts__ApplyValueRecord(CurrentGlyph, &ValueRecord1); CurrentGlyph->Flags |= KBTS_GLYPH_FLAG_USED_IN_GPOS; @@ -20554,7 +20662,7 @@ static kbts_b32 kbts__DoSingleAdjustment(kbts_shape_scratchpad *Scratchpad, kbts if(ValueFormat2) { - kbts__resolved_value_record ValueRecord2 = kbts__ResolveValueRecord(Scratchpad, Base, ValueFormat2, Unpacked2Base); + kbts__resolved_value_record ValueRecord2 = kbts__ResolveValueRecord(Scratchpad, ValueRecordParent, ValueFormat2, Unpacked2Base); kbts__ApplyValueRecord(NextGlyph, &ValueRecord2); OnePastLastGlyph = NextGlyph->Next; } @@ -22676,11 +22784,11 @@ static void kbts__ExecuteOp(kbts_shape_scratchpad *Scratchpad, kbts_glyph_storag kbts_u32 FilterMask = Config->Shaper == KBTS_SHAPER_USE ? KBTS__USE_GLYPH_FEATURE_MASK : KBTS__GLYPH_FEATURE_MASK; kbts_un FeatureStageIndex = kbts__CurrentBakedFeatureStageIndex(Scratchpad); - kbts_un FirstSequentialLookupIndex = Config->FeatureStageFirstLookupIndices[FeatureStageIndex]; - kbts_un OnePastLastSequentialLookupIndex = Config->FeatureStageFirstLookupIndices[FeatureStageIndex + 1]; + kbts_un FirstSequentialLookupIndex = kbts__shape_config_FeatureStageFirstLookupIndices(Config)[FeatureStageIndex]; + kbts_un OnePastLastSequentialLookupIndex = kbts__shape_config_FeatureStageFirstLookupIndices(Config)[FeatureStageIndex + 1]; KBTS__FOR(SequentialLookupIndex, FirstSequentialLookupIndex, OnePastLastSequentialLookupIndex) { - kbts__sequential_lookup *SequentialLookup = &Config->SequentialLookups[SequentialLookupIndex]; + kbts__sequential_lookup *SequentialLookup = &kbts__shape_config_SequentialLookups(Config)[SequentialLookupIndex]; kbts_un LookupIndex = SequentialLookup->LookupIndex; kbts_u32 SkipFlags = SequentialLookup->SkipFlags; kbts_u32 GlyphFilter = SequentialLookup->GlyphFilter; @@ -22970,7 +23078,7 @@ static void kbts__ExecuteOp(kbts_shape_scratchpad *Scratchpad, kbts_glyph_storag { case 1: LookupEntry = *(kbts_u8 *)AtLookupEntry; break; case 2: LookupEntry = kbts__ReadU16Unaligned((kbts_u16 *)AtLookupEntry); break; - case 3: LookupEntry = (kbts_u32)AtLookupEntry[2] | ((kbts_u32)AtLookupEntry[1] << 8) | ((kbts_u32)AtLookupEntry[0] << 16); break; + case 3: LookupEntry = (kbts_u32)(kbts_u8)AtLookupEntry[2] | ((kbts_u32)(kbts_u8)AtLookupEntry[1] << 8) | ((kbts_u32)(kbts_u8)AtLookupEntry[0] << 16); break; case 4: LookupEntry = kbts__ReadU32Unaligned((kbts_u32 *)AtLookupEntry); break; } @@ -23016,11 +23124,11 @@ static void kbts__ExecuteOp(kbts_shape_scratchpad *Scratchpad, kbts_glyph_storag kbts__lookup_list *LookupList = kbts__GetLookupList(Gpos); kbts_un FeatureStageIndex = kbts__CurrentBakedFeatureStageIndex(Scratchpad); - kbts_un FirstSequentialLookupIndex = Config->FeatureStageFirstLookupIndices[FeatureStageIndex]; - kbts_un OnePastLastSequentialLookupIndex = Config->FeatureStageFirstLookupIndices[FeatureStageIndex + 1]; + kbts_un FirstSequentialLookupIndex = kbts__shape_config_FeatureStageFirstLookupIndices(Config)[FeatureStageIndex]; + kbts_un OnePastLastSequentialLookupIndex = kbts__shape_config_FeatureStageFirstLookupIndices(Config)[FeatureStageIndex + 1]; KBTS__FOR(SequentialLookupIndex, FirstSequentialLookupIndex, OnePastLastSequentialLookupIndex) { - kbts__sequential_lookup *SequentialLookup = &Config->SequentialLookups[SequentialLookupIndex]; + kbts__sequential_lookup *SequentialLookup = &kbts__shape_config_SequentialLookups(Config)[SequentialLookupIndex]; kbts_un LookupIndex = SequentialLookup->LookupIndex; kbts_u32 SkipFlags = SequentialLookup->SkipFlags; @@ -25517,17 +25625,18 @@ static kbts_shape_config *kbts__PlaceShapeConfig(kbts_font *Font, kbts_script Sc } } - Config.SequentialLookups = SequentialLookups; - Config.IdSequentialLookupMatrix = IdSequentialLookupMatrix; + Config.OffsetToSequentialLookups = KBTS__POINTER_DIFF32(SequentialLookups, Result); + Config.OffsetToIdSequentialLookupMatrix = KBTS__POINTER_DIFF32(IdSequentialLookupMatrix, Result); Config.GlyphCount = Font->Blob->GlyphCount; Config.LookupCount = Font->Blob->LookupCount; } - Config.FeatureStageFirstLookupIndices = FeatureStageFirstLookupIndices; + Config.OffsetToFeatureStageFirstLookupIndices = KBTS__POINTER_DIFF32(FeatureStageFirstLookupIndices, Result); Config.Uid.Data[0] = kbts__MurmurFinalize(&Murmur); if(Memory) { + Config.SizeNotCountingAlignment = (kbts_u32)(Bump.At - (kbts_uptr)Result); *Result = Config; } @@ -25540,7 +25649,8 @@ static kbts_shape_config *kbts__PlaceShapeConfig(kbts_font *Font, kbts_script Sc if(!Memory) { // We add the align, just to make sure we can accept any incoming pointer. - *Size = (Bump.At - (kbts_uptr)(void *)0) + KBTS_ALIGNOF(kbts_shape_config); + *Size = ((Bump.At - (kbts_uptr)(void *)Memory) + KBTS_ALIGNOF(kbts_shape_config)); + Result = 0; } @@ -25700,6 +25810,15 @@ KBTS_EXPORT void kbts_DestroyShapeContext(kbts_shape_context *Context) { if(Context) { + KBTS__FOR(HugeShapeConfigIndex, 0, Context->HugeShapeConfigCount) + { + kbts__huge_shape_config *HugeShapeConfig = &Context->HugeShapeConfigs[HugeShapeConfigIndex]; + kbts__existing_shape_config_block *Block = HugeShapeConfig->Block; + kbts__existing_shape_config *Existing = &Block->Items[HugeShapeConfig->IndexWithinBlock]; + + kbts_DestroyShapeConfig(Existing->Config); + } + kbts__FreeArena(&Context->PermanentArena); kbts__FreeArena(&Context->ConfigArena); kbts__FreeArena(&Context->ScratchArena); @@ -25949,6 +26068,47 @@ KBTS_EXPORT void kbts_NormalizeVariationVector(kbts_font *Font, float *Variation } } +static kbts_u16 kbts__StringIdFromOpenTypeStringId(kbts_u16 OpenTypeStringId) +{ + kbts_u16 Result = OpenTypeStringId; + + if(OpenTypeStringId < KBTS_FONT_INFO_STRING_ID_COUNT1) + { + static const kbts_u8 OpenTypeStringIdToKbts[KBTS_FONT_INFO_STRING_ID_COUNT1] = { + KBTS_FONT_INFO_STRING_ID_COPYRIGHT, + KBTS_FONT_INFO_STRING_ID_FAMILY, + KBTS_FONT_INFO_STRING_ID_SUBFAMILY, + KBTS_FONT_INFO_STRING_ID_UID, + KBTS_FONT_INFO_STRING_ID_FULL_NAME, + KBTS_FONT_INFO_STRING_ID_VERSION, + KBTS_FONT_INFO_STRING_ID_POSTSCRIPT_NAME, + KBTS_FONT_INFO_STRING_ID_TRADEMARK, + KBTS_FONT_INFO_STRING_ID_MANUFACTURER, + KBTS_FONT_INFO_STRING_ID_DESIGNER, + KBTS_FONT_INFO_STRING_ID_DESCRIPTION, + KBTS_FONT_INFO_STRING_ID_VENDOR_URL, + KBTS_FONT_INFO_STRING_ID_DESIGNER_URL, + KBTS_FONT_INFO_STRING_ID_LICENSE, + KBTS_FONT_INFO_STRING_ID_LICENSE_URL, + KBTS_FONT_INFO_STRING_ID_RESERVED, + KBTS_FONT_INFO_STRING_ID_TYPOGRAPHIC_FAMILY, + KBTS_FONT_INFO_STRING_ID_TYPOGRAPHIC_SUBFAMILY, + KBTS_FONT_INFO_STRING_ID_COMPATIBLE_FULL_NAME, + KBTS_FONT_INFO_STRING_ID_SAMPLE_TEXT, + KBTS_FONT_INFO_STRING_ID_POSTSCRIPT_CID_FINDFONT_NAME, + KBTS_FONT_INFO_STRING_ID_WWS_FAMILY_NAME, + KBTS_FONT_INFO_STRING_ID_WWS_SUBFAMILY_NAME, + KBTS_FONT_INFO_STRING_ID_LIGHT_BACKGROUND_PALETTE, + KBTS_FONT_INFO_STRING_ID_DARK_BACKGROUND_PALETTE, + KBTS_FONT_INFO_STRING_ID_VARIATIONS_POSTSCRIPT_NAME_PREFIX, + }; + + Result = OpenTypeStringIdToKbts[OpenTypeStringId]; + } + + return Result; +} + KBTS_EXPORT int kbts_GetVariationAxisInfo(kbts_font *Font, int AxisIndex, kbts_variation_axis_info *Info) { int Result = 0; @@ -25968,7 +26128,7 @@ KBTS_EXPORT int kbts_GetVariationAxisInfo(kbts_font *Font, int AxisIndex, kbts_v Info->DefaultValue = kbts__Fixed16_16ToFloatFromMemory(&Axis->DefaultValue); Info->MaximumValue = kbts__Fixed16_16ToFloatFromMemory(&Axis->MaxValue); Info->Flags = kbts__ReadU16Unaligned(&Axis->Flags); - Info->NameStringId = kbts__ReadU16Unaligned(&Axis->AxisNameId); + Info->NameStringId = kbts__StringIdFromOpenTypeStringId(kbts__ReadU16Unaligned(&Axis->AxisNameId)); Result = 1; } @@ -26003,8 +26163,8 @@ KBTS_EXPORT int kbts_GetVariationInstance(kbts_font *Font, int InstanceIndex, kb PostscriptNameStringId = kbts__ReadU16Unaligned(PostscriptNameId); } - Info->SubfamilyNameStringId = kbts__ReadU16Unaligned(&Instance->SubfamilyNameId); - Info->PostscriptNameStringId = PostscriptNameStringId; + Info->SubfamilyNameStringId = kbts__StringIdFromOpenTypeStringId(kbts__ReadU16Unaligned(&Instance->SubfamilyNameId)); + Info->PostscriptNameStringId = kbts__StringIdFromOpenTypeStringId(PostscriptNameStringId); Info->Flags = Flags; if(VariationVector) @@ -26470,31 +26630,6 @@ KBTS_EXPORT kbts_glyph *kbts_PushGlyph(kbts_glyph_storage *Storage, kbts_font *F return Result; } - -KBTS_EXPORT int kbts_SizeOfGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount) -{ - kbts_un NonBinaryOverrideCount = 0; - KBTS__FOR(OverrideIndex, 0, (kbts_un)OverrideCount) - { - kbts_feature_override *Override = &Overrides[OverrideIndex]; - - if(Override->Value > 1) - { - NonBinaryOverrideCount += 1; - } - } - - kbts_un SequentialLookupCount = kbts__SequentialLookupCount(ShapeConfig); - kbts_un LastSequentialLookupIndex = (SequentialLookupCount) ? (SequentialLookupCount - 1) : 0; - kbts__matrix_index LastRowEntryMatrixIndex = kbts__IdSequentialLookupMatrixIndex(LastSequentialLookupIndex, 0, SequentialLookupCount); - kbts_un MatrixRowSizeInBytes = sizeof(kbts_u32) * (LastRowEntryMatrixIndex.WordIndex + 1); - - kbts_un Result = sizeof(kbts_glyph_config) + - NonBinaryOverrideCount * sizeof(kbts__enabled_lookup) + - MatrixRowSizeInBytes * 2; - return (int)Result; -} - static kbts_u32 kbts__GlyphConfigHash(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, kbts_un OverrideCount) { kbts__murmur_state Murmur; @@ -26513,23 +26648,29 @@ static kbts_u32 kbts__GlyphConfigHash(kbts_shape_config *ShapeConfig, kbts_featu return Result; } -KBTS_EXPORT kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount, void *Memory) +static kbts_glyph_config *kbts__PlaceGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount, void *Memory, kbts_un *Size_) { kbts__pointer_bump_allocator Bump = kbts__PointerBumpAllocator(Memory); kbts_glyph_config *Result = kbts__PointerPushType(&Bump, kbts_glyph_config); - if(ShapeConfig && Memory) + if(ShapeConfig) { - KBTS_MEMSET(Result, 0, sizeof(*Result)); + if(Memory) + { + KBTS_MEMSET(Result, 0, sizeof(*Result)); + } kbts_un SequentialLookupCount = kbts__SequentialLookupCount(ShapeConfig); kbts_un LastSequentialLookupIndex = (SequentialLookupCount) ? (SequentialLookupCount - 1) : 0; kbts__matrix_index LastRowEntryMatrixIndex = kbts__IdSequentialLookupMatrixIndex(LastSequentialLookupIndex, 0, SequentialLookupCount); kbts_un MatrixRowSizeInBytes = sizeof(kbts_u32) * (LastRowEntryMatrixIndex.WordIndex + 1); kbts_u32 *EnabledLookupBits = (kbts_u32 *)kbts__PointerPush(&Bump, MatrixRowSizeInBytes, KBTS_ALIGNOF(kbts_u32)); - KBTS_MEMSET(EnabledLookupBits, 0, MatrixRowSizeInBytes); kbts_u32 *DisabledLookupBits = (kbts_u32 *)kbts__PointerPush(&Bump, MatrixRowSizeInBytes, KBTS_ALIGNOF(kbts_u32)); - KBTS_MEMSET(DisabledLookupBits, 0, MatrixRowSizeInBytes); + if(Memory) + { + KBTS_MEMSET(EnabledLookupBits, 0, MatrixRowSizeInBytes); + KBTS_MEMSET(DisabledLookupBits, 0, MatrixRowSizeInBytes); + } kbts_glyph_flags EnabledFilterFlags = 0; kbts_glyph_flags DisabledFilterFlags = 0; @@ -26622,7 +26763,7 @@ KBTS_EXPORT kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeCon kbts_b32 Found = 0; KBTS__FOR(SequentialLookupIndex, FirstSequentialLookupIndex, OnePastLastSequentialLookupIndex) { - kbts__sequential_lookup *SequentialLookup = &ShapeConfig->SequentialLookups[SequentialLookupIndex]; + kbts__sequential_lookup *SequentialLookup = &kbts__shape_config_SequentialLookups(ShapeConfig)[SequentialLookupIndex]; if(SequentialLookup->LookupIndex == LookupIndex) { @@ -26638,7 +26779,10 @@ KBTS_EXPORT kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeCon kbts__matrix_index SequentialMatrixIndex = kbts__IdSequentialLookupMatrixIndex(FoundSequentialLookupIndex, 0, SequentialLookupCount); if(FoundOverride->Value) { - EnabledLookupBits[SequentialMatrixIndex.WordIndex] |= 1u << SequentialMatrixIndex.BitIndex; + if(Memory) + { + EnabledLookupBits[SequentialMatrixIndex.WordIndex] |= 1u << SequentialMatrixIndex.BitIndex; + } if((FoundOverride->Value > 1) && (NonBinaryEnabledLookupCount < KBTS_MAX_SIMULTANEOUS_FEATURES)) @@ -26650,7 +26794,10 @@ KBTS_EXPORT kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeCon } else { - DisabledLookupBits[SequentialMatrixIndex.WordIndex] |= 1u << SequentialMatrixIndex.BitIndex; + if(Memory) + { + DisabledLookupBits[SequentialMatrixIndex.WordIndex] |= 1u << SequentialMatrixIndex.BitIndex; + } } } } @@ -26663,17 +26810,43 @@ KBTS_EXPORT kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeCon if(NonBinaryEnabledLookupCount) { OutNonBinaryEnabledLookups = kbts__PointerPushArray(&Bump, kbts__enabled_lookup, NonBinaryEnabledLookupCount); - KBTS_MEMCPY(OutNonBinaryEnabledLookups, NonBinaryEnabledLookups, sizeof(*NonBinaryEnabledLookups) * NonBinaryEnabledLookupCount); + if(Memory) + { + KBTS_MEMCPY(OutNonBinaryEnabledLookups, NonBinaryEnabledLookups, sizeof(*NonBinaryEnabledLookups) * NonBinaryEnabledLookupCount); + } } - Result->NonBinaryEnabledLookups = OutNonBinaryEnabledLookups; - Result->NonBinaryEnabledLookupCount = (kbts_u32)NonBinaryEnabledLookupCount; - Result->EnabledLookupBits = EnabledLookupBits; - Result->DisabledLookupBits = DisabledLookupBits; - Result->EnabledFilterFlags = EnabledFilterFlags; - Result->DisabledFilterFlags = DisabledFilterFlags; + if(Memory) + { + Result->NonBinaryEnabledLookups = OutNonBinaryEnabledLookups; + Result->NonBinaryEnabledLookupCount = (kbts_u32)NonBinaryEnabledLookupCount; + Result->EnabledLookupBits = EnabledLookupBits; + Result->DisabledLookupBits = DisabledLookupBits; + Result->EnabledFilterFlags = EnabledFilterFlags; + Result->DisabledFilterFlags = DisabledFilterFlags; + } } + if(!Memory) + { + // Pad to alignment to accept any memory address. + *Size_ = Bump.At - (kbts_uptr)Memory + KBTS_ALIGNOF(kbts_glyph_config) - 1; + } + + return Result; +} + +KBTS_EXPORT int kbts_SizeOfGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount) +{ + kbts_un Result; + kbts__PlaceGlyphConfig(ShapeConfig, Overrides, OverrideCount, 0, &Result); + return (int)Result; +} + +KBTS_EXPORT kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount, void *Memory) +{ + kbts_un Size = 0; + kbts_glyph_config *Result = kbts__PlaceGlyphConfig(ShapeConfig, Overrides, OverrideCount, Memory, &Size); return Result; } @@ -27906,65 +28079,6 @@ KBTS_EXPORT int kbts_ShapePopGlobalVariation(kbts_shape_context *Context, kbts_u return Result; } -static kbts_shape_config *kbts__FindOrCreateShapeConfig(kbts_shape_context *Context, kbts__interned_context_font_info *FontInfo, kbts_script Script, kbts_language Language) -{ - kbts_shape_config *Result = 0; - - if(FontInfo) - { - for(kbts__existing_shape_config_block *ExistingBlock = (kbts__existing_shape_config_block *)Context->ExistingShapeConfigBlockSentinel.Next; - kbts__ExistingShapeConfigBlockIsValid(Context, ExistingBlock); - ExistingBlock = (kbts__existing_shape_config_block *)ExistingBlock->Header.Next) - { - KBTS__FOR(ExistingIndex, 0, ExistingBlock->Count) - { - kbts__existing_shape_config *Existing = &ExistingBlock->Items[ExistingIndex]; - - if((Existing->FontHash == FontInfo->Hash) && - (Existing->Script == Script) && - (Existing->Language == Language)) - { - Result = Existing->Config; - - break; - } - } - } - - if(!Result) - { - kbts__existing_shape_config_block *Last = (kbts__existing_shape_config_block *)Context->ExistingShapeConfigBlockSentinel.Prev; - if(!kbts__ExistingShapeConfigBlockIsValid(Context, Last) || - (Last->Count == KBTS__EXISTING_SHAPE_CONFIGS_PER_BLOCK)) - { - kbts__existing_shape_config_block *NewBlock = kbts__PushType(&Context->ConfigArena, kbts__existing_shape_config_block); - if(!NewBlock) - { - Context->Error = KBTS_SHAPE_ERROR_OUT_OF_MEMORY; - - return 0; - } - - KBTS__DLLIST_INSERT_BEFORE(&NewBlock->Header, &Context->ExistingShapeConfigBlockSentinel); - NewBlock->Count = 0; - - Last = NewBlock; - } - - Result = kbts_CreateShapeConfig2(FontInfo->Font, Script, Language, FontInfo->VariationVector, kbts__ArenaAllocator, &Context->ConfigArena, 0); - - KBTS_ASSERT(Last->Count < KBTS__EXISTING_SHAPE_CONFIGS_PER_BLOCK); - kbts__existing_shape_config *NewExisting = &Last->Items[Last->Count++]; - NewExisting->Config = Result; - NewExisting->FontHash = FontInfo->Hash; - NewExisting->Script = Script; - NewExisting->Language = Language; - } -} - - return Result; -} - static kbts_b32 kbts__UidEquals(kbts_uid A, kbts_uid B) { kbts_b32 Result = (A.Data[0] == B.Data[0]) & @@ -27974,63 +28088,415 @@ static kbts_b32 kbts__UidEquals(kbts_uid A, kbts_uid B) return Result; } -static kbts_glyph_config *kbts__FindOrCreateGlyphConfig(kbts_shape_context *Context, kbts_shape_config *ShapeConfig, kbts_feature_override *FeatureOverrides, int FeatureOverrideCount) +typedef struct kbts__fifo_bin_allocation { - kbts_glyph_config *Result = 0; + void *Pointer; + kbts_u32 WriteCursor; +} kbts__fifo_bin_allocation; - if(ShapeConfig) +static kbts__fifo_bin_allocation kbts__FifoBinPush(kbts__fifo_bin *Bin, kbts_un BinSize, kbts_un Size, kbts_un Alignment) +{ + // This assumes the bin is aligned at least to Alignment. + kbts_un WriteCursorWrapped = Bin->WriteCursor & (BinSize - 1); + kbts_un AlignmentWriteCursorIncrement = Alignment - (WriteCursorWrapped & (Alignment - 1)); + kbts_un WriteCursorAligned = KBTS__ROUND_UP_POW2(WriteCursorWrapped, Alignment); + + char *Pointer = Bin->Base + WriteCursorAligned; + char *OnePastLastBinByte = Bin->Base + BinSize; + if((Pointer + Size) > OnePastLastBinByte) { - if(FeatureOverrideCount > 0) + Bin->WriteCursor += BinSize - WriteCursorWrapped; + AlignmentWriteCursorIncrement = 0; + + Pointer = Bin->Base; + } + + kbts__fifo_bin_allocation Result = KBTS__ZERO; + Result.Pointer = (void *)Pointer; + Result.WriteCursor = Bin->WriteCursor; + + Bin->WriteCursor += AlignmentWriteCursorIncrement + Size; + return Result; +} + +KBTS_INLINE kbts_b32 kbts__U32WrappingNewer(kbts_u32 A, kbts_u32 B) +{ + kbts_u32 D = A - B; + kbts_b32 Result = (kbts_s32)D > 0; + return Result; +} + +static kbts_shape_config *kbts__FindOrCreateShapeConfig(kbts_shape_context *Context, kbts__interned_context_font_info *FontInfo, kbts_script Script, kbts_language Language) +{ + kbts_shape_config *Result = 0; + + if(FontInfo && FontInfo->Font) + { + if((Context->FindOrCreateShapeConfigTimestamp % 32) == 31) { - kbts_u32 Hash = kbts__GlyphConfigHash(ShapeConfig, FeatureOverrides, (kbts_un)FeatureOverrideCount); - - for(kbts__existing_glyph_config_block *ExistingBlock = (kbts__existing_glyph_config_block *)Context->ExistingGlyphConfigBlockSentinel.Next; - kbts__ExistingGlyphConfigBlockIsValid(Context, ExistingBlock); - ExistingBlock = (kbts__existing_glyph_config_block *)ExistingBlock->Header.Next) + for(kbts__existing_shape_config_block *ExistingBlock = (kbts__existing_shape_config_block *)Context->ExistingShapeConfigBlockSentinel.Next; + kbts__ExistingShapeConfigBlockIsValid(Context, ExistingBlock); + ExistingBlock = (kbts__existing_shape_config_block *)ExistingBlock->Header.Next) { - KBTS__FOR(ExistingIndex, 0, ExistingBlock->Count) + kbts_u32 OccupiedBits = ExistingBlock->OccupiedBits; + kbts_u32 NewOccupiedBits = OccupiedBits; + + while(OccupiedBits) { - kbts__existing_glyph_config *Existing = &ExistingBlock->Items[ExistingIndex]; + kbts_un ExistingIndex = kbts__LsbPositionOrBitWidth32(OccupiedBits); + kbts__existing_shape_config *Existing = &ExistingBlock->Items[ExistingIndex]; - if(Hash == Existing->Hash) + if(Existing->CacheBinIndex < KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT) { - Result = Existing->GlyphConfig; + kbts__fifo_bin *Bin = &Context->ShapeConfigCacheBins[Existing->CacheBinIndex]; + kbts_u32 BinSize = kbts__ShapeConfigCacheBinSizes[Existing->CacheBinIndex + 1]; + kbts_u32 WrappedDistance = (Existing->ConfigWriteCursor + BinSize) - Bin->WriteCursor; - break; + if(WrappedDistance > 0x7FFFFFFFu) + { + NewOccupiedBits &= ~(1u << ExistingIndex); + } } + + OccupiedBits &= ~(1u << ExistingIndex); + } + + ExistingBlock->OccupiedBits = NewOccupiedBits; + } + } + Context->FindOrCreateShapeConfigTimestamp += 1; + kbts_u32 CurrentTimestamp = Context->FindOrCreateShapeConfigTimestamp; + + kbts_uid Uid; + kbts_PlaceShapeConfig2(FontInfo->Font, Script, Language, FontInfo->VariationVector, 0, &Uid); + + kbts_u32 EmptyIndex = 0; + kbts__existing_shape_config_block *EmptyBlock = 0; + for(kbts__existing_shape_config_block *ExistingBlock = (kbts__existing_shape_config_block *)Context->ExistingShapeConfigBlockSentinel.Next; + kbts__ExistingShapeConfigBlockIsValid(Context, ExistingBlock); + ExistingBlock = (kbts__existing_shape_config_block *)ExistingBlock->Header.Next) + { + kbts_u32 OccupiedBits = ExistingBlock->OccupiedBits; + + if(!EmptyBlock && + (OccupiedBits != ~0u)) + { + EmptyBlock = ExistingBlock; + EmptyIndex = kbts__LsbPositionOrBitWidth32(~OccupiedBits); + } + + while(OccupiedBits) + { + kbts_un ExistingIndex = kbts__LsbPositionOrBitWidth32(OccupiedBits); + kbts__existing_shape_config *Existing = &ExistingBlock->Items[ExistingIndex]; + + if((Existing->FontHash == FontInfo->Hash) && + kbts__UidEquals(Existing->Config->Uid, Uid)) + { + kbts_un ExistingBinIndex = Existing->CacheBinIndex; + if(ExistingBinIndex < KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT) + { + kbts__fifo_bin *ExistingBin = &Context->ShapeConfigCacheBins[ExistingBinIndex]; + kbts_u32 ExistingBinSize = kbts__ShapeConfigCacheBinSizes[ExistingBinIndex + 1]; + kbts_u32 BinWriteCursor = ExistingBin->WriteCursor; + kbts_u32 ConfigWriteCursor = Existing->ConfigWriteCursor; + + kbts_u32 MaxValidWriteCursor = ConfigWriteCursor + ExistingBinSize; + kbts_u32 WrappedDistance = MaxValidWriteCursor - BinWriteCursor; + if(WrappedDistance > 0x7FFFFFFFu) + { + // We got overwritten. Invalidate. + ExistingBlock->OccupiedBits &= ~(1u << ExistingIndex); + + EmptyBlock = ExistingBlock; + EmptyIndex = (kbts_u32)ExistingIndex; + } + else + { + if(WrappedDistance <= (ExistingBinSize / 4 + ExistingBinSize / 16)) + { + // We are close to being invalidated. Copy ourselves to the front. + kbts_un ExistingConfigSize = Existing->Config->SizeNotCountingAlignment; + kbts__fifo_bin_allocation To = kbts__FifoBinPush(ExistingBin, ExistingBinSize, ExistingConfigSize, KBTS_ALIGNOF(kbts_shape_config)); + + KBTS_MEMMOVE(To.Pointer, Existing->Config, ExistingConfigSize); + + Existing->Config = (kbts_shape_config *)To.Pointer; + Existing->ConfigWriteCursor = To.WriteCursor; + Existing->LastUsedTimestamp = CurrentTimestamp; + } + + Result = Existing->Config; + } + } + else + { + Result = Existing->Config; + } + + goto DoneFindingShapeConfig; + } + + OccupiedBits &= ~(1u << ExistingIndex); + } + } + DoneFindingShapeConfig:; + + if(!Result) + { + if(!EmptyBlock) + { + EmptyBlock = kbts__PushType(&Context->ConfigArena, kbts__existing_shape_config_block); + if(!EmptyBlock) + { + Context->Error = KBTS_SHAPE_ERROR_OUT_OF_MEMORY; + + return 0; + } + + KBTS__DLLIST_INSERT_BEFORE(&EmptyBlock->Header, &Context->ExistingShapeConfigBlockSentinel); + EmptyBlock->OccupiedBits = 0; + EmptyIndex = 0; + } + + kbts_un ResultSize = kbts_SizeOfShapeConfig2(FontInfo->Font, Script, Language, FontInfo->VariationVector); + + kbts_un ResultBinMaxElementSize = 0; + kbts_un ResultBinSize = 0; + kbts_un ResultBinIndex = 0; + kbts_u32 ResultWriteCursor = 0; + for(; + ResultBinIndex < KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT; + ++ResultBinIndex) + { + ResultBinMaxElementSize = kbts__ShapeConfigCacheBinSizes[ResultBinIndex]; + ResultBinSize = kbts__ShapeConfigCacheBinSizes[ResultBinIndex + 1]; + + if(ResultSize <= ResultBinMaxElementSize) + { + break; } } - if(!Result) + kbts__existing_shape_config *NewExisting = &EmptyBlock->Items[EmptyIndex]; + NewExisting->CacheBinIndex = (kbts_u32)ResultBinIndex; + NewExisting->LastUsedTimestamp = CurrentTimestamp; + NewExisting->FontHash = FontInfo->Hash; + + if(ResultBinIndex < KBTS__SHAPE_CONFIG_CACHE_BIN_COUNT) { - kbts__existing_glyph_config_block *Last = (kbts__existing_glyph_config_block *)Context->ExistingGlyphConfigBlockSentinel.Prev; - if(!kbts__ExistingGlyphConfigBlockIsValid(Context, Last) || - (Last->Count == KBTS__EXISTING_GLYPH_CONFIGS_PER_BLOCK)) + kbts__fifo_bin *Bin = &Context->ShapeConfigCacheBins[ResultBinIndex]; + + if(!Bin->Base) { - kbts__existing_glyph_config_block *NewBlock = kbts__PushType(&Context->ConfigArena, kbts__existing_glyph_config_block); - if(!NewBlock) + Bin->Base = (char *)kbts__PushSize(&Context->PermanentArena, ResultBinSize, KBTS_ALIGNOF(kbts_shape_config)); + + if(!Bin->Base) { Context->Error = KBTS_SHAPE_ERROR_OUT_OF_MEMORY; return 0; } - - KBTS__DLLIST_INSERT_BEFORE(&NewBlock->Header, &Context->ExistingGlyphConfigBlockSentinel); - NewBlock->Count = 0; - - Last = NewBlock; } - Result = kbts_CreateGlyphConfig(ShapeConfig, FeatureOverrides, FeatureOverrideCount, kbts__ArenaAllocator, &Context->ConfigArena); + kbts__fifo_bin_allocation Allocation = kbts__FifoBinPush(Bin, ResultBinSize, ResultSize, KBTS_ALIGNOF(kbts_shape_config)); + ResultWriteCursor = Allocation.WriteCursor; - KBTS_ASSERT(Last->Count < KBTS__EXISTING_GLYPH_CONFIGS_PER_BLOCK); - kbts__existing_glyph_config *Existing = &Last->Items[Last->Count++]; - Existing->ShapeConfig = ShapeConfig; - Existing->FeatureOverrides = FeatureOverrides; - Existing->FeatureOverrideCount = FeatureOverrideCount; - Existing->GlyphConfig = Result; - Existing->Hash = Hash; + Result = kbts_PlaceShapeConfig2(FontInfo->Font, Script, Language, FontInfo->VariationVector, Allocation.Pointer, 0); } + else + { + if(Context->HugeShapeConfigCount >= KBTS__HUGE_SHAPE_CONFIG_CAPACITY) + { + kbts_u32 OldestHugeConfigTimestamp = CurrentTimestamp + 1; + kbts__existing_shape_config_block *OldestHugeConfigBlock = 0; + kbts_u32 OldestHugeConfigIndexWithinBlock = 0; + kbts_un OldestHugeConfigIndex = 0; + + KBTS__FOR(HugeShapeConfigIndex, 0, Context->HugeShapeConfigCount) + { + kbts__huge_shape_config *Huge = &Context->HugeShapeConfigs[HugeShapeConfigIndex]; + kbts__existing_shape_config_block *HugeBlock = Huge->Block; + kbts_u32 HugeTimestamp = HugeBlock->Items[Huge->IndexWithinBlock].LastUsedTimestamp; + + if(!kbts__U32WrappingNewer(HugeTimestamp, OldestHugeConfigTimestamp)) + { + OldestHugeConfigTimestamp = HugeTimestamp; + OldestHugeConfigBlock = HugeBlock; + OldestHugeConfigIndexWithinBlock = Huge->IndexWithinBlock; + OldestHugeConfigIndex = HugeShapeConfigIndex; + } + } + + if(OldestHugeConfigBlock) + { + kbts__existing_shape_config *ToRemove = &OldestHugeConfigBlock->Items[OldestHugeConfigIndexWithinBlock]; + kbts_DestroyShapeConfig(ToRemove->Config); + ToRemove->Config = 0; + OldestHugeConfigBlock->OccupiedBits &= ~(1u << OldestHugeConfigIndexWithinBlock); + + Context->HugeShapeConfigs[OldestHugeConfigIndex] = Context->HugeShapeConfigs[--Context->HugeShapeConfigCount]; + } + } + + Result = kbts_CreateShapeConfig2(FontInfo->Font, Script, Language, FontInfo->VariationVector, Context->PermanentArena.Allocator, Context->PermanentArena.AllocatorData, 0); + + kbts__huge_shape_config *NewHuge = &Context->HugeShapeConfigs[Context->HugeShapeConfigCount++]; + NewHuge->Block = EmptyBlock; + NewHuge->IndexWithinBlock = (kbts_u32)EmptyIndex; + } + + NewExisting->ConfigWriteCursor = ResultWriteCursor; + NewExisting->Config = Result; + NewExisting->LastUsedTimestamp = CurrentTimestamp; + + EmptyBlock->OccupiedBits |= 1u << EmptyIndex; + } + } + + return Result; +} + +static kbts_glyph_config *kbts__FindOrCreateGlyphConfig(kbts_shape_context *Context, kbts_shape_config *ShapeConfig, kbts_feature_override *FeatureOverrides, int FeatureOverrideCount) +{ + kbts_glyph_config *Result = 0; + + if(ShapeConfig && + (FeatureOverrideCount > 0)) + { + if((Context->FindOrCreateGlyphConfigTimestamp % 32) == 31) + { + for(kbts__existing_glyph_config_block *ExistingBlock = (kbts__existing_glyph_config_block *)Context->ExistingGlyphConfigBlockSentinel.Next; + kbts__ExistingGlyphConfigBlockIsValid(Context, ExistingBlock); + ExistingBlock = (kbts__existing_glyph_config_block *)ExistingBlock->Header.Next) + { + kbts_u32 OccupiedBits = ExistingBlock->OccupiedBits; + kbts_u32 NewOccupiedBits = OccupiedBits; + + while(OccupiedBits) + { + kbts_un ExistingIndex = kbts__LsbPositionOrBitWidth32(OccupiedBits); + kbts__existing_glyph_config *Existing = &ExistingBlock->Items[ExistingIndex]; + + kbts_u32 WrappedDistance = (Existing->ConfigWriteCursor + KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE) - Context->GlyphConfigCacheBin.WriteCursor; + if(WrappedDistance > 0x7FFFFFFFu) + { + NewOccupiedBits &= ~(1u << ExistingIndex); + } + + OccupiedBits &= ~(1u << ExistingIndex); + } + + ExistingBlock->OccupiedBits = NewOccupiedBits; + } + } + Context->FindOrCreateGlyphConfigTimestamp += 1; + + kbts_u32 Hash = kbts__GlyphConfigHash(ShapeConfig, FeatureOverrides, (kbts_un)FeatureOverrideCount); + + kbts__existing_glyph_config_block *EmptyBlock = 0; + kbts_u32 EmptyIndex = 0; + + for(kbts__existing_glyph_config_block *ExistingBlock = (kbts__existing_glyph_config_block *)Context->ExistingGlyphConfigBlockSentinel.Next; + kbts__ExistingGlyphConfigBlockIsValid(Context, ExistingBlock); + ExistingBlock = (kbts__existing_glyph_config_block *)ExistingBlock->Header.Next) + { + kbts_u32 OccupiedBits = ExistingBlock->OccupiedBits; + + if(!EmptyBlock && + (OccupiedBits != ~0u)) + { + EmptyBlock = ExistingBlock; + EmptyIndex = kbts__LsbPositionOrBitWidth32(~OccupiedBits); + } + + while(OccupiedBits) + { + kbts_un ExistingIndex = kbts__LsbPositionOrBitWidth32(OccupiedBits); + kbts__existing_glyph_config *Existing = &ExistingBlock->Items[ExistingIndex]; + + if(Hash == Existing->Hash) + { + kbts__fifo_bin *ExistingBin = &Context->GlyphConfigCacheBin; + + kbts_u32 BinWriteCursor = ExistingBin->WriteCursor; + kbts_u32 ConfigWriteCursor = Existing->ConfigWriteCursor; + kbts_u32 MaxValidWriteCursor = ConfigWriteCursor + KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE; + kbts_u32 WrappedDistance = MaxValidWriteCursor - BinWriteCursor; + if(WrappedDistance > 0x7FFFFFFFu) + { + // We got overwritten. Invalidate. + ExistingBlock->OccupiedBits &= ~(1u << ExistingIndex); + + EmptyBlock = ExistingBlock; + EmptyIndex = (kbts_u32)ExistingIndex; + } + else + { + if(WrappedDistance <= (KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE / 4 + KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE / 16)) + { + // We are close to being invalidated. Copy ourselves to the front. + kbts_un ExistingConfigSize = Existing->ConfigSize; + kbts__fifo_bin_allocation To = kbts__FifoBinPush(ExistingBin, KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE, ExistingConfigSize, KBTS_ALIGNOF(kbts_shape_config)); + + KBTS_MEMMOVE(To.Pointer, Existing->GlyphConfig, ExistingConfigSize); + + Existing->GlyphConfig = (kbts_glyph_config *)To.Pointer; + Existing->ConfigWriteCursor = To.WriteCursor; + } + + Result = Existing->GlyphConfig; + } + + goto DoneFindingGlyphConfig; + } + + OccupiedBits &= ~(1u << ExistingIndex); + } + } + DoneFindingGlyphConfig:; + + if(!Result) + { + if(!Context->GlyphConfigCacheBin.Base) + { + Context->GlyphConfigCacheBin.Base = (char *)kbts__PushSize(&Context->PermanentArena, KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE, KBTS_ALIGNOF(kbts_glyph_config)); + if(!Context->GlyphConfigCacheBin.Base) + { + Context->Error = KBTS_SHAPE_ERROR_OUT_OF_MEMORY; + + return 0; + } + } + + if(!EmptyBlock) + { + EmptyBlock = kbts__PushType(&Context->ConfigArena, kbts__existing_glyph_config_block); + if(!EmptyBlock) + { + Context->Error = KBTS_SHAPE_ERROR_OUT_OF_MEMORY; + + return 0; + } + + KBTS__DLLIST_INSERT_BEFORE(&EmptyBlock->Header, &Context->ExistingGlyphConfigBlockSentinel); + EmptyBlock->OccupiedBits = 0; + } + + kbts_un Size = kbts_SizeOfGlyphConfig(ShapeConfig, FeatureOverrides, FeatureOverrideCount); + kbts__fifo_bin_allocation Allocation = kbts__FifoBinPush(&Context->GlyphConfigCacheBin, KBTS__GLYPH_CONFIG_CACHE_BIN_SIZE, Size, KBTS_ALIGNOF(kbts_glyph_config)); + + Result = kbts_PlaceGlyphConfig(ShapeConfig, FeatureOverrides, FeatureOverrideCount, Allocation.Pointer); + + kbts__existing_glyph_config *Existing = &EmptyBlock->Items[EmptyIndex]; + Existing->FeatureOverrides = FeatureOverrides; + Existing->FeatureOverrideCount = FeatureOverrideCount; + Existing->GlyphConfig = Result; + Existing->Hash = Hash; + Existing->ConfigWriteCursor = Allocation.WriteCursor; + Existing->ConfigSize = (kbts_u32)Size; + + EmptyBlock->OccupiedBits |= 1u << EmptyIndex; } } @@ -28869,8 +29335,6 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ ByteSwapContext.MarkedBytesBitfield = (kbts_u32 *)ScratchMemory; KBTS_MEMSET(ByteSwapContext.MarkedBytesBitfield, 0, MarkedBytesBitfieldSize); - kbts__BeginBounds(&ByteSwapContext, ByteSwapContext.FileBase, ByteSwapContext.FileEnd); - { char *TableEnd; kbts__head *Head = kbts__BlobTableHeaderChecked(Header, KBTS_BLOB_TABLE_ID_HEAD, kbts__head, &TableEnd, &Result); @@ -29058,12 +29522,12 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } } - if(!TableValid) + kbts_b32 ContextError = kbts__EndBounds(&ByteSwapContext, &Bounds); + if(ContextError | !TableValid) { - Result = KBTS_LOAD_FONT_ERROR_INVALID_FONT; + Font->Cmap = 0; + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_CMAP].Length = 0; } - - kbts__EndBounds(&ByteSwapContext, &Bounds); } } @@ -29133,7 +29597,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__coverage *Coverage = KBTS__POINTER_OFFSET(kbts__coverage, MarkGlyphSets, CoverageOffset); kbts__ByteSwapCoverage(&ByteSwapContext, Coverage); - if(ByteSwapContext.Error) + if(ByteSwapContext.ErrorInCurrentBounds) { goto InvalidFont; } @@ -29154,7 +29618,10 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_GDEF].Length = 0; + } } } @@ -29169,12 +29636,12 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ { kbts__bounds Bounds = kbts__BeginBounds(&ByteSwapContext, Hea, TableEnd); - if(!kbts__ByteSwapArray16Context(&ByteSwapContext, (kbts_u16 *)Hea, sizeof(kbts__hea) / sizeof(kbts_u16))) - { - Result = KBTS_LOAD_FONT_ERROR_INVALID_FONT; - } + kbts__ByteSwapArray16Context(&ByteSwapContext, (kbts_u16 *)Hea, sizeof(kbts__hea) / sizeof(kbts_u16)); - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[HeaTableIds[HeaTableIndex]].Length = 0; + } } } } @@ -29220,22 +29687,25 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ { U16Count = 14; } - else - { - goto InvalidFont; - } - if(kbts__ByteSwapArray16Context(&ByteSwapContext, &Maxp->GlyphCount, U16Count)) + if(U16Count) { - Header->GlyphCount = kbts__ReadU16Unaligned(&Maxp->GlyphCount); - } - else - { - Result = KBTS_LOAD_FONT_ERROR_INVALID_FONT; + if(kbts__ByteSwapArray16Context(&ByteSwapContext, &Maxp->GlyphCount, U16Count)) + { + Header->GlyphCount = kbts__ReadU16Unaligned(&Maxp->GlyphCount); + } + else + { + // We need a valid MAXP table. + Result = KBTS_LOAD_FONT_ERROR_INVALID_FONT; + } } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_MAXP].Length = 0; + } } else { @@ -29328,7 +29798,10 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_NAME].Length = 0; + } } // We should normally set Result to INVALID_FONT if there is no name table. @@ -29351,39 +29824,40 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts_un InstanceCount = kbts__ReadU16Unaligned(&Fvar->InstanceCount); kbts_un InstanceSize = kbts__ReadU16Unaligned(&Fvar->InstanceSize); - if((char *)(Axes + AxisCount) > TableEnd) + if((char *)(Axes + AxisCount) <= TableEnd) { - goto InvalidFont; - } - - KBTS__FOR(AxisIndex, 0, AxisCount) - { - kbts__variation_axis_record *Axis = &Axes[AxisIndex]; - - kbts__MarkOkToReadContext(&ByteSwapContext, &Axis->Tag, sizeof(Axis->Tag)); - kbts__ByteSwapArray32Context(&ByteSwapContext, (kbts_u32 *)&Axis->MinValue, 3); - kbts__ByteSwapArray16Context(&ByteSwapContext, &Axis->Flags, 2); - } - - kbts_b32 InstancesContainPostscriptNameId = (InstanceSize >= (sizeof(kbts__instance_record) + sizeof(kbts__fixed16_16) * AxisCount + sizeof(kbts_u16))); - - KBTS__FOR(InstanceIndex, 0, InstanceCount) - { - kbts__instance_record *Instance = KBTS__POINTER_OFFSET(kbts__instance_record, Instances, InstanceIndex * InstanceSize); - kbts__fixed16_16 *InstanceCoordinates = KBTS__POINTER_AFTER(kbts__fixed16_16, Instance); - - kbts__ByteSwap16InPlaceContext(&ByteSwapContext, &Instance->SubfamilyNameId, 0); - kbts__ByteSwapArray32Context(&ByteSwapContext, (kbts_u32 *)InstanceCoordinates, AxisCount); - - if(InstancesContainPostscriptNameId) + KBTS__FOR(AxisIndex, 0, AxisCount) { - kbts_u16 *InstancePostscriptNameId = (kbts_u16 *)(InstanceCoordinates + AxisCount); - kbts__ByteSwap16InPlaceContext(&ByteSwapContext, InstancePostscriptNameId, 0); + kbts__variation_axis_record *Axis = &Axes[AxisIndex]; + + kbts__MarkOkToReadContext(&ByteSwapContext, &Axis->Tag, sizeof(Axis->Tag)); + kbts__ByteSwapArray32Context(&ByteSwapContext, (kbts_u32 *)&Axis->MinValue, 3); + kbts__ByteSwapArray16Context(&ByteSwapContext, &Axis->Flags, 2); + } + + kbts_b32 InstancesContainPostscriptNameId = (InstanceSize >= (sizeof(kbts__instance_record) + sizeof(kbts__fixed16_16) * AxisCount + sizeof(kbts_u16))); + + KBTS__FOR(InstanceIndex, 0, InstanceCount) + { + kbts__instance_record *Instance = KBTS__POINTER_OFFSET(kbts__instance_record, Instances, InstanceIndex * InstanceSize); + kbts__fixed16_16 *InstanceCoordinates = KBTS__POINTER_AFTER(kbts__fixed16_16, Instance); + + kbts__ByteSwap16InPlaceContext(&ByteSwapContext, &Instance->SubfamilyNameId, 0); + kbts__ByteSwapArray32Context(&ByteSwapContext, (kbts_u32 *)InstanceCoordinates, AxisCount); + + if(InstancesContainPostscriptNameId) + { + kbts_u16 *InstancePostscriptNameId = (kbts_u16 *)(InstanceCoordinates + AxisCount); + kbts__ByteSwap16InPlaceContext(&ByteSwapContext, InstancePostscriptNameId, 0); + } } } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_FVAR].Length = 0; + } } } @@ -29415,18 +29889,21 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } else { - goto InvalidFont; + break; } } else { - goto InvalidFont; + break; } } } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_AVAR].Length = 0; + } } } @@ -29480,7 +29957,10 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[TableId].Length = 0; + } } } } @@ -29513,7 +29993,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } else { - goto InvalidFont; + break; } } @@ -29521,15 +30001,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } else { - goto InvalidFont; + ByteSwapContext.ErrorInCurrentBounds |= 1; } } - else - { - goto InvalidFont; - } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_MVAR].Length = 0; + } } } @@ -29540,53 +30019,53 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__bounds Bounds = kbts__BeginBounds(&ByteSwapContext, Gsub, GsubTableEnd); kbts__ByteSwapGsubGposCommon(&ByteSwapContext, Gsub); - if(ByteSwapContext.Error) + + if(!ByteSwapContext.ErrorInCurrentBounds) { - goto InvalidFont; - } + kbts_u16 LookupListOffset = kbts__ReadU16Unaligned(&Gsub->LookupListOffset); + kbts_u16 ScriptListOffset = kbts__ReadU16Unaligned(&Gsub->ScriptListOffset); + kbts_u16 FeatureListOffset = kbts__ReadU16Unaligned(&Gsub->FeatureListOffset); - kbts_u16 LookupListOffset = kbts__ReadU16Unaligned(&Gsub->LookupListOffset); - kbts_u16 ScriptListOffset = kbts__ReadU16Unaligned(&Gsub->ScriptListOffset); - kbts_u16 FeatureListOffset = kbts__ReadU16Unaligned(&Gsub->FeatureListOffset); - - // @Robustness: Is this enough? - if((LookupListOffset >= 10) && - (LookupListOffset != ScriptListOffset) && - (LookupListOffset != FeatureListOffset)) - { - kbts__lookup_list *LookupList = kbts__GetLookupList(Gsub); - kbts_un LookupList_Count = kbts__ReadU16Unaligned(&LookupList->Count); - - kbts_u16 *LookupList_Offsets = KBTS__POINTER_AFTER(kbts_u16, LookupList); - if(!kbts__ByteSwapArray16Context(&ByteSwapContext, LookupList_Offsets, LookupList_Count)) + // @Robustness: Is this enough? + if((LookupListOffset >= 10) && + (LookupListOffset != ScriptListOffset) && + (LookupListOffset != FeatureListOffset)) { - goto InvalidFont; - } + kbts__lookup_list *LookupList = kbts__GetLookupList(Gsub); + kbts_un LookupList_Count = kbts__ReadU16Unaligned(&LookupList->Count); - KBTS__FOR(LookupIndex, 0, LookupList_Count) - { - kbts__lookup *PackedLookup = kbts__GetLookup(LookupList, LookupIndex); - - KBTS_DUMPF("GSUB Lookup %llu:\n", LookupIndex); - - if(kbts__ByteSwapLookup(&ByteSwapContext, PackedLookup) && - !ByteSwapContext.Error) + kbts_u16 *LookupList_Offsets = KBTS__POINTER_AFTER(kbts_u16, LookupList); + if(kbts__ByteSwapArray16Context(&ByteSwapContext, LookupList_Offsets, LookupList_Count)) { - kbts__unpacked_lookup Lookup = kbts__UnpackLookup(Gdef, PackedLookup); - KBTS_DUMPF(" Flags %u\n", Lookup.Flags); - - KBTS__FOR(SubstitutionIndex, 0, Lookup.SubtableCount) + KBTS__FOR(LookupIndex, 0, LookupList_Count) { - kbts_u16 *Base = KBTS__POINTER_OFFSET(kbts_u16, PackedLookup, kbts__ReadU16Unaligned(&Lookup.SubtableOffsets[SubstitutionIndex])); + kbts__lookup *PackedLookup = kbts__GetLookup(LookupList, LookupIndex); - KBTS_DUMPF(" Subtable %llu:\n", SubstitutionIndex); + KBTS_DUMPF("GSUB Lookup %llu:\n", LookupIndex); - kbts__ByteSwapGsubLookupSubtable(&ByteSwapContext, Lookup.Type, Base); + if(kbts__ByteSwapLookup(&ByteSwapContext, PackedLookup) && + !ByteSwapContext.ErrorInCurrentBounds) + { + kbts__unpacked_lookup Lookup = kbts__UnpackLookup(Gdef, PackedLookup); + KBTS_DUMPF(" Flags %u\n", Lookup.Flags); + + KBTS__FOR(SubstitutionIndex, 0, Lookup.SubtableCount) + { + kbts_u16 *Base = KBTS__POINTER_OFFSET(kbts_u16, PackedLookup, kbts__ReadU16Unaligned(&Lookup.SubtableOffsets[SubstitutionIndex])); + + KBTS_DUMPF(" Subtable %llu:\n", SubstitutionIndex); + + kbts__ByteSwapGsubLookupSubtable(&ByteSwapContext, Lookup.Type, Base); + } + } } } } + } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_GSUB].Length = 0; } } @@ -29599,7 +30078,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__bounds Bounds = kbts__BeginBounds(&ByteSwapContext, Gpos, TableEnd); kbts__ByteSwapGsubGposCommon(&ByteSwapContext, Gpos); - if(!ByteSwapContext.Error) + if(!ByteSwapContext.ErrorInCurrentBounds) { kbts__lookup_list *LookupList = kbts__GetLookupList(Gpos); kbts_un LookupList_Count = kbts__ReadU16Unaligned(&LookupList->Count); @@ -29613,7 +30092,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ KBTS_DUMPF("GPOS Lookup %llu:\n", LookupIndex); if(kbts__ByteSwapLookup(&ByteSwapContext, PackedLookup) && - !ByteSwapContext.Error) + !ByteSwapContext.ErrorInCurrentBounds) { kbts__unpacked_lookup Lookup = kbts__UnpackLookup(Gdef, PackedLookup); @@ -29632,15 +30111,13 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } } - kbts__EndBounds(&ByteSwapContext, &Bounds); + if(kbts__EndBounds(&ByteSwapContext, &Bounds)) + { + Font->Blob->Tables[KBTS_BLOB_TABLE_ID_GPOS].Length = 0; + } } } - if(ByteSwapContext.Error) - { - goto InvalidFont; - } - // At this point, we are done byteswapping the file, so we can start reusing scratch memory. // Bake our own data. @@ -29682,7 +30159,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__lookup_list *LookupList = kbts__GetLookupList(ShapingTable); if(!kbts__SpanIsInBounds(&ByteSwapContext, LookupList, LookupList + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un LookupList_Count = kbts__ReadU16Unaligned(&LookupList->Count); @@ -29692,7 +30169,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__lookup *PackedLookup = kbts__GetLookup(LookupList, LookupIndex); if(!kbts__SpanIsInBounds(&ByteSwapContext, PackedLookup, PackedLookup + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_u16 Lookup_Type = kbts__ReadU16Unaligned(&PackedLookup->Type); @@ -29706,7 +30183,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts_u16 *Lookup_SubtableOffsets = KBTS__POINTER_AFTER(kbts_u16, PackedLookup); if(!kbts__SpanIsInBounds(&ByteSwapContext, Lookup_SubtableOffsets, Lookup_SubtableOffsets + OffsetCount)) { - goto InvalidFont; + goto InvalidTable; } LookupSubtableIndexOffsets[RunningLookupIndex] = (kbts_u32)RunningSubtableIndex; @@ -29722,7 +30199,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__extension *Extension = (kbts__extension *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Extension, Extension + 1)) { - goto InvalidFont; + goto InvalidTable; } // CAREFUL: Here, we remap LookupType only. @@ -29735,7 +30212,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ if(!kbts__SpanIsInBounds(&ByteSwapContext, Base, Base + 1)) { - goto InvalidFont; + goto InvalidTable; } if(kbts__LookupBeginsWithCoverage(ShapingTableId, LookupType, kbts__ReadU16Unaligned(&Base[0]))) @@ -29748,7 +30225,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__ligature_substitution *Subst = (kbts__ligature_substitution *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Subst_LigatureSetCount = kbts__ReadU16Unaligned(&Subst->LigatureSetCount); @@ -29758,14 +30235,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__ligature_set *Set = kbts__GetLigatureSet(Subst, SetIndex); if(!kbts__SpanIsInBounds(&ByteSwapContext, Set, Set + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Set_Count = kbts__ReadU16Unaligned(&Set->Count); kbts_u16 *LigatureOffsets = KBTS__POINTER_AFTER(kbts_u16, Set); if(!kbts__SpanIsInBounds(&ByteSwapContext, LigatureOffsets, LigatureOffsets + Set_Count)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(LigatureIndex, 0, Set_Count) @@ -29773,14 +30250,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__ligature *Ligature = kbts__GetLigature(Set, LigatureIndex); if(!kbts__SpanIsInBounds(&ByteSwapContext, Ligature, Ligature + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Ligature_ComponentCount = kbts__ReadU16Unaligned(&Ligature->ComponentCount); kbts_u16 *Ids = KBTS__POINTER_AFTER(kbts_u16, Ligature); if(!kbts__SpanIsInBounds(&ByteSwapContext, Ids, Ids + Ligature_ComponentCount - 1)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(IdIndex, 1, Ligature_ComponentCount) @@ -29794,7 +30271,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } else { - goto InvalidFont; + goto InvalidTable; } } } @@ -29810,14 +30287,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__sequence_context_1 *Subst = (kbts__sequence_context_1 *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Subst_SeqRuleSetCount = kbts__ReadU16Unaligned(&Subst->SeqRuleSetCount); kbts_u16 *SequenceRuleSetOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, SequenceRuleSetOffsets, SequenceRuleSetOffsets + Subst_SeqRuleSetCount)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(SetIndex, 0, Subst_SeqRuleSetCount) @@ -29828,7 +30305,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ { if(!kbts__SpanIsInBounds(&ByteSwapContext, Set, Set + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Set_Count = kbts__ReadU16Unaligned(&Set->Count); @@ -29837,14 +30314,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__sequence_rule *Rule = kbts__GetSequenceRule(Set, RuleIndex); if(!kbts__SpanIsInBounds(&ByteSwapContext, Rule, Rule + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Rule_GlyphCount = kbts__ReadU16Unaligned(&Rule->GlyphCount); kbts_u16 *SequenceGlyphIds = KBTS__POINTER_AFTER(kbts_u16, Rule); if(!kbts__SpanIsInBounds(&ByteSwapContext, SequenceGlyphIds, SequenceGlyphIds + Rule_GlyphCount)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(InputIndex, 1, Rule_GlyphCount) @@ -29858,7 +30335,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ } else { - goto InvalidFont; + goto InvalidTable; } } } @@ -29871,14 +30348,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__sequence_context_2 *Subst = (kbts__sequence_context_2 *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Subst_ClassSequenceRuleSetCount = kbts__ReadU16Unaligned(&Subst->ClassSequenceRuleSetCount); kbts_u16 *ClassSequenceRuleSetOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, ClassSequenceRuleSetOffsets, ClassSequenceRuleSetOffsets + Subst_ClassSequenceRuleSetCount)) { - goto InvalidFont; + goto InvalidTable; } kbts_u64 ClassesIncluded[16] = KBTS__ZERO; @@ -29890,7 +30367,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ { if(!kbts__SpanIsInBounds(&ByteSwapContext, Set, Set + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Set_Count = kbts__ReadU16Unaligned(&Set->Count); @@ -29899,14 +30376,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__class_sequence_rule *Rule = kbts__GetClassSequenceRule(Set, RuleIndex); if(!kbts__SpanIsInBounds(&ByteSwapContext, Rule, Rule + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Rule_GlyphCount = kbts__ReadU16Unaligned(&Rule->GlyphCount); kbts_u16 *SequenceClasses = KBTS__POINTER_AFTER(kbts_u16, Rule); if(!kbts__SpanIsInBounds(&ByteSwapContext, SequenceClasses, SequenceClasses + Rule_GlyphCount - 1)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(SequenceIndex, 1, Rule_GlyphCount) @@ -29931,14 +30408,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__sequence_context_3 *Subst = (kbts__sequence_context_3 *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Subst_GlyphCount = kbts__ReadU16Unaligned(&Subst->GlyphCount); kbts_u16 *CoverageOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, CoverageOffsets, CoverageOffsets + Subst_GlyphCount)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(CoverageIndex, 1, Subst_GlyphCount) @@ -29962,14 +30439,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__chained_sequence_context_1 *Subst = (kbts__chained_sequence_context_1 *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Subst_ChainedSequenceRuleSetCount = kbts__ReadU16Unaligned(&Subst->ChainedSequenceRuleSetCount); kbts_u16 *ChainedSequenceRuleSetOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, ChainedSequenceRuleSetOffsets, ChainedSequenceRuleSetOffsets + Subst_ChainedSequenceRuleSetCount)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(SetIndex, 0, Subst_ChainedSequenceRuleSetCount) @@ -29977,14 +30454,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__chained_sequence_rule_set *Set = KBTS__POINTER_OFFSET(kbts__chained_sequence_rule_set, Subst, kbts__ReadU16Unaligned(&ChainedSequenceRuleSetOffsets[SetIndex])); if(!kbts__SpanIsInBounds(&ByteSwapContext, Set, Set + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Set_Count = kbts__ReadU16Unaligned(&Set->Count); kbts_u16 *ChainedClassSequenceRuleOffsets = KBTS__POINTER_AFTER(kbts_u16, Set); if(!kbts__SpanIsInBounds(&ByteSwapContext, ChainedClassSequenceRuleOffsets, ChainedClassSequenceRuleOffsets + Set_Count)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(RuleIndex, 0, Set_Count) @@ -29994,7 +30471,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__unpacked_chained_sequence_rule Unpacked; if(!kbts__UnpackChainedSequenceRuleChecked(&ByteSwapContext, Rule, &Unpacked)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(BacktrackIndex, 0, Unpacked.BacktrackCount) @@ -30035,7 +30512,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__chained_sequence_context_2 *Subst = (kbts__chained_sequence_context_2 *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un BacktrackClassDefOffset = kbts__ReadU16Unaligned(&Subst->BacktrackClassDefOffset); @@ -30068,7 +30545,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts_u16 *ChainedClassSequenceRuleSetOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, ChainedClassSequenceRuleSetOffsets, ChainedClassSequenceRuleSetOffsets + Subst_ChainedClassSequenceRuleSetCount)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(SetIndex, 0, Subst_ChainedClassSequenceRuleSetCount) @@ -30078,7 +30555,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ { if(!kbts__SpanIsInBounds(&ByteSwapContext, Set, Set + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un Set_Count = kbts__ReadU16Unaligned(&Set->Count); @@ -30086,7 +30563,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts_u16 *ChainedSequenceRuleOffsets = KBTS__POINTER_AFTER(kbts_u16, Set); if(!kbts__SpanIsInBounds(&ByteSwapContext, ChainedSequenceRuleOffsets, ChainedSequenceRuleOffsets + Set_Count)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(RuleIndex, 0, Set_Count) @@ -30096,7 +30573,7 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__unpacked_chained_sequence_rule Unpacked; if(!kbts__UnpackChainedSequenceRuleChecked(&ByteSwapContext, Rule, &Unpacked)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(BacktrackIndex, 0, Unpacked.BacktrackCount) @@ -30139,35 +30616,35 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__chained_sequence_context_3 *Subst = (kbts__chained_sequence_context_3 *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un BacktrackCount = kbts__ReadU16Unaligned(&Subst->BacktrackGlyphCount); kbts_u16 *BacktrackCoverageOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, BacktrackCoverageOffsets, BacktrackCoverageOffsets + BacktrackCount + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un InputCount = kbts__ReadU16Unaligned(&BacktrackCoverageOffsets[BacktrackCount]); kbts_u16 *InputCoverageOffsets = BacktrackCoverageOffsets + BacktrackCount + 1; if(!kbts__SpanIsInBounds(&ByteSwapContext, InputCoverageOffsets, InputCoverageOffsets + InputCount + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un LookaheadCount = kbts__ReadU16Unaligned(&InputCoverageOffsets[InputCount]); kbts_u16 *LookaheadCoverageOffsets = InputCoverageOffsets + InputCount + 1; if(!kbts__SpanIsInBounds(&ByteSwapContext, LookaheadCoverageOffsets, LookaheadCoverageOffsets + LookaheadCount + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un RecordCount = kbts__ReadU16Unaligned(&LookaheadCoverageOffsets[LookaheadCount]); kbts__sequence_lookup_record *Records = (kbts__sequence_lookup_record *)(LookaheadCoverageOffsets + LookaheadCount + 1); if(!kbts__SpanIsInBounds(&ByteSwapContext, Records, Records + RecordCount)) { - goto InvalidFont; + goto InvalidTable; } Coverage = KBTS__POINTER_OFFSET(kbts__coverage, Subst, kbts__ReadU16Unaligned(&InputCoverageOffsets[0])); @@ -30197,28 +30674,28 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__reverse_chain_substitution *Subst = (kbts__reverse_chain_substitution *)Base; if(!kbts__SpanIsInBounds(&ByteSwapContext, Subst, Subst + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un BacktrackCount = kbts__ReadU16Unaligned(&Subst->BacktrackGlyphCount); kbts_u16 *BacktrackCoverageOffsets = KBTS__POINTER_AFTER(kbts_u16, Subst); if(!kbts__SpanIsInBounds(&ByteSwapContext, BacktrackCoverageOffsets, BacktrackCoverageOffsets + BacktrackCount + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un LookaheadCount = kbts__ReadU16Unaligned(&BacktrackCoverageOffsets[BacktrackCount]); kbts_u16 *LookaheadCoverageOffsets = BacktrackCoverageOffsets + BacktrackCount + 1; if(!kbts__SpanIsInBounds(&ByteSwapContext, LookaheadCoverageOffsets, LookaheadCoverageOffsets + LookaheadCount + 1)) { - goto InvalidFont; + goto InvalidTable; } kbts_un SubstituteGlyphCount = kbts__ReadU16Unaligned(&LookaheadCoverageOffsets[LookaheadCount]); kbts_u16 *SubstituteGlyphIds = LookaheadCoverageOffsets + LookaheadCount + 1; if(!kbts__SpanIsInBounds(&ByteSwapContext, SubstituteGlyphIds, SubstituteGlyphIds + SubstituteGlyphCount)) { - goto InvalidFont; + goto InvalidTable; } KBTS__FOR(BacktrackIndex, 0, BacktrackCount) @@ -30246,6 +30723,14 @@ KBTS_EXPORT kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_ kbts__EndBounds(&ByteSwapContext, &Bounds); } + if(0) + { + InvalidTable:; + Font->Blob->Tables[TableId].Length = 0; + + RunningLookupIndex = (InGpos) ? GposLookupIndexOffset : 0; + } + if(!InGpos) { GposLookupIndexOffset = RunningLookupIndex; @@ -30476,6 +30961,10 @@ KBTS_EXPORT kbts_font kbts_FontFromMemory(void *FileData, int FileSize, int Font kbts_PlaceBlob(&Result, &LoadFontState, ScratchMemory, OutputMemory); + if(Result.Error) + { + kbts__AllocatorFree(Allocator, AllocatorData, OutputMemory); + } kbts__AllocatorFree(Allocator, AllocatorData, ScratchMemory); Result.Allocator = Allocator; @@ -30524,7 +31013,6 @@ KBTS_EXPORT kbts_font kbts_FontFromFile(const char *FileName, int FontIndex, kbt if(!Result.Error) { - if(FileData) { *FileData = Data; @@ -30544,6 +31032,11 @@ KBTS_EXPORT kbts_font kbts_FontFromFile(const char *FileName, int FontIndex, kbt { Result.Error = KBTS_LOAD_FONT_ERROR_READ_ERROR; } + + if(Result.Error) + { + kbts__AllocatorFree(Allocator, AllocatorData, Data); + } } else { @@ -32089,6 +32582,7 @@ KBTS_EXPORT void kbts_BreakEntireString(kbts_direction Direction, kbts_japanese_ char *InputElement = (char *)Input; char *End = (char *)Input + InputSizeInBytes; kbts_un BreaksWritten = 0; + kbts_un BreakCapacityNeeded = 0; kbts_un MaxBreakPosition = 0; kbts_un BreakCapacity = (kbts_un)IBreakCapacity; kbts_un BreakFlagCapacity = (kbts_un)IBreakFlagCapacity; @@ -32133,42 +32627,44 @@ KBTS_EXPORT void kbts_BreakEntireString(kbts_direction Direction, kbts_japanese_ kbts_break Break; while(kbts_Break(&BreakState, &Break)) { - if(Breaks && (BreaksWritten < BreakCapacity)) + int Found = 0; + + // @Speed: Binary search! + for(kbts_un ExistingBreakIndex = BreaksWritten - 1; + ExistingBreakIndex < BreaksWritten; + --ExistingBreakIndex) { - int Found = 0; - kbts_un ExistingBreakCount = KBTS__MIN(BreaksWritten, BreakCapacity); + kbts_break *Existing = &Breaks[ExistingBreakIndex]; - // @Speed: Binary search! - for(kbts_un ExistingBreakIndex = ExistingBreakCount; - ExistingBreakIndex; - --ExistingBreakIndex) + if(Existing->Position == Break.Position) { - kbts_break *Existing = &Breaks[ExistingBreakIndex - 1]; + Existing->Flags |= Break.Flags; - if(Existing->Position == Break.Position) + if(Break.Flags & KBTS_BREAK_FLAG_SCRIPT) { - Existing->Flags |= Break.Flags; - - if(Break.Flags & KBTS_BREAK_FLAG_SCRIPT) - { - Existing->Script = Break.Script; - } - - if(Break.Flags & KBTS_BREAK_FLAG_DIRECTION) - { - Existing->Direction = Break.Direction; - } - - Found = 1; - - break; + Existing->Script = Break.Script; } + + if(Break.Flags & KBTS_BREAK_FLAG_DIRECTION) + { + Existing->Direction = Break.Direction; + } + + Found = 1; + + break; + } + } + + if(!Found) + { + if(Breaks && + (BreaksWritten < BreakCapacity)) + { + Breaks[BreaksWritten++] = Break; } - if(!Found) - { - Breaks[BreaksWritten] = Break; - } + BreakCapacityNeeded += 1; } if(BreakFlags && ((kbts_u32)Break.Position < BreakFlagCapacity)) @@ -32177,14 +32673,12 @@ KBTS_EXPORT void kbts_BreakEntireString(kbts_direction Direction, kbts_japanese_ } MaxBreakPosition = KBTS__MAX(MaxBreakPosition, (kbts_u32)Break.Position); - - BreaksWritten += 1; } } if(BreakCount) { - *BreakCount = (int)BreaksWritten; + *BreakCount = (int)BreakCapacityNeeded; } if(BreakFlagCount) { @@ -32749,39 +33243,39 @@ KBTS_EXPORT int kbts_LookupFontString(kbts_font *Font, kbts_font_info_string_id { kbts_u16 OpenTypeStringId = (kbts_u16)StringId; - if(OpenTypeStringId < KBTS_FONT_INFO_STRING_ID_COUNT1) + if(StringId < KBTS_FONT_INFO_STRING_ID_COUNT1) { - // @Cleanup: Since we reserve this transform to the first few IDs, we can probably - // make it much simpler. - switch(StringId) - { - case KBTS_FONT_INFO_STRING_ID_COPYRIGHT: OpenTypeStringId = 0; break; - case KBTS_FONT_INFO_STRING_ID_FAMILY: OpenTypeStringId = 1; break; - case KBTS_FONT_INFO_STRING_ID_SUBFAMILY: OpenTypeStringId = 2; break; - case KBTS_FONT_INFO_STRING_ID_UID: OpenTypeStringId = 3; break; - case KBTS_FONT_INFO_STRING_ID_FULL_NAME: OpenTypeStringId = 4; break; - case KBTS_FONT_INFO_STRING_ID_VERSION: OpenTypeStringId = 5; break; - case KBTS_FONT_INFO_STRING_ID_POSTSCRIPT_NAME: OpenTypeStringId = 6; break; - case KBTS_FONT_INFO_STRING_ID_TRADEMARK: OpenTypeStringId = 7; break; - case KBTS_FONT_INFO_STRING_ID_MANUFACTURER: OpenTypeStringId = 8; break; - case KBTS_FONT_INFO_STRING_ID_DESIGNER: OpenTypeStringId = 9; break; - case KBTS_FONT_INFO_STRING_ID_TYPOGRAPHIC_FAMILY: OpenTypeStringId = 16; break; - case KBTS_FONT_INFO_STRING_ID_TYPOGRAPHIC_SUBFAMILY: OpenTypeStringId = 17; break; - case KBTS_FONT_INFO_STRING_ID_DESCRIPTION: OpenTypeStringId = 10; break; - case KBTS_FONT_INFO_STRING_ID_VENDOR_URL: OpenTypeStringId = 11; break; - case KBTS_FONT_INFO_STRING_ID_DESIGNER_URL: OpenTypeStringId = 12; break; - case KBTS_FONT_INFO_STRING_ID_LICENSE: OpenTypeStringId = 13; break; - case KBTS_FONT_INFO_STRING_ID_LICENSE_URL: OpenTypeStringId = 14; break; - case KBTS_FONT_INFO_STRING_ID_COMPATIBLE_FULL_NAME: OpenTypeStringId = 18; break; - case KBTS_FONT_INFO_STRING_ID_SAMPLE_TEXT: OpenTypeStringId = 19; break; - case KBTS_FONT_INFO_STRING_ID_POSTSCRIPT_CID_FINDFONT_NAME: OpenTypeStringId = 20; break; - case KBTS_FONT_INFO_STRING_ID_WWS_FAMILY_NAME: OpenTypeStringId = 21; break; - case KBTS_FONT_INFO_STRING_ID_WWS_SUBFAMILY_NAME: OpenTypeStringId = 22; break; - case KBTS_FONT_INFO_STRING_ID_LIGHT_BACKGROUND_PALETTE: OpenTypeStringId = 23; break; - case KBTS_FONT_INFO_STRING_ID_DARK_BACKGROUND_PALETTE: OpenTypeStringId = 24; break; - case KBTS_FONT_INFO_STRING_ID_VARIATIONS_POSTSCRIPT_NAME_PREFIX: OpenTypeStringId = 25; break; - default: OpenTypeStringId = 0xFFFF; break; - } + static const kbts_u16 KbtsStringIdToOpenType[KBTS_FONT_INFO_STRING_ID_COUNT1] = { + /* NONE */ 0xFFFFu, + /* COPYRIGHT */ 0, + /* FAMILY */ 1, + /* SUBFAMILY */ 2, + /* UID */ 3, + /* FULL_NAME */ 4, + /* VERSION */ 5, + /* POSTSCRIPT_NAME */ 6, + /* TRADEMARK */ 7, + /* MANUFACTURER */ 8, + /* DESIGNER */ 9, + /* TYPOGRAPHIC_FAMILY */ 16, + /* TYPOGRAPHIC_SUBFAMILY */ 17, + /* DESCRIPTION */ 10, + /* VENDOR_URL */ 11, + /* DESIGNER_URL */ 12, + /* LICENSE */ 13, + /* LICENSE_URL */ 14, + /* COMPATIBLE_FULL_NAME */ 18, + /* SAMPLE_TEXT */ 19, + /* POSTSCRIPT_CID_FINDFONT_NAME */ 20, + /* WWS_FAMILY_NAME */ 21, + /* WWS_SUBFAMILY_NAME */ 22, + /* LIGHT_BACKGROUND_PALETTE */ 23, + /* DARK_BACKGROUND_PALETTE */ 24, + /* VARIATIONS_POSTSCRIPT_NAME_PREFIX */ 25, + /* RESERVED */ 15, + }; + + OpenTypeStringId = KbtsStringIdToOpenType[StringId]; } if(OpenTypeStringId != 0xFFFF)