packages feed

kb-text-shape-0.3.0.0: src/KB/Text/Shape/FFI/API/Direct.hsc

module KB.Text.Shape.FFI.API.Direct where

import Prelude hiding (id, error)

import Foreign
import Foreign.C

import KB.Text.Shape.FFI.Allocator (Allocator)
import KB.Text.Shape.FFI.Handles
import KB.Text.Shape.FFI.Enums
import KB.Text.Shape.FFI.Structs

#include "kb_text_shape.h"

-- | @kbts_shape_error kbts_ShapeDirect(kbts_shape_scratchpad *Scratchpad, kbts_glyph_storage *Storage, kbts_direction RunDirection, kbts_glyph_iterator *Output);@
foreign import ccall safe "kbts_ShapeDirect" kbts_ShapeDirect
  :: ShapeScratchpad
  -> Ptr GlyphStorage
  -> Direction -- ^ RunDirection
  -> Ptr GlyphIterator
  -> IO ShapeError

-- | @kbts_shape_error kbts_ShapeDirect2(kbts_shape_scratchpad *Scratchpad, kbts_glyph_storage *Storage, kbts_direction RunDirection, kbts_s16 *VariationVectorNormalized, kbts_glyph_iterator *Output);@
--
-- The normalized vector comes from 'kbts_NormalizeVariationVector' and may be null.
foreign import ccall safe "kbts_ShapeDirect2" kbts_ShapeDirect2
  :: ShapeScratchpad
  -> Ptr GlyphStorage
  -> Direction -- ^ RunDirection
  -> Ptr Int16 -- ^ VariationVectorNormalized
  -> Ptr GlyphIterator
  -> IO ShapeError

-- A font holds all data that corresponds to a given font file.

#ifndef KB_TEXT_SHAPE_NO_CRT
-- | @kbts_font kbts_FontFromFile(const char *FileName, int FontIndex, kbts_allocator_function *Allocator, void *AllocatorData, void **FileData, int *FileSize);@
foreign import ccall safe "hs_FontFromFile" hs_FontFromFile
  :: Ptr CChar -- ^ FileName
  -> CInt -- ^ FontIndex
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> Ptr (Ptr ()) -- ^ FileData
  -> Ptr CInt -- ^ FileSize
  -> Ptr Font
  -> IO LoadFontError
#endif

-- | @int kbts_FontCount(void *FileData, int FileSize);@
foreign import ccall unsafe "kbts_FontCount" kbts_FontCount
  :: Ptr () -- ^ FileData
  -> CInt -- ^ FileSize
  -> CInt

-- | @kbts_font kbts_FontFromMemory(void *FileData, int FileSize, int FontIndex, kbts_allocator_function *Allocator, void *AllocatorData);@
foreign import ccall safe "hs_FontFromMemory" hs_FontFromMemory
  :: Ptr () -- ^ FileData
  -> CInt -- ^ FileSize
  -> CInt -- ^ FontIndex
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> Font
  -> IO LoadFontError

-- | @void kbts_FreeFont(kbts_font *Font);@
foreign import ccall safe "kbts_FreeFont" kbts_FreeFont
  :: Font
  -> IO ()

-- | @int kbts_FontIsValid(kbts_font *Font);@
foreign import ccall unsafe "kbts_FontIsValid" kbts_FontIsValid
  :: Font
  -> CInt

-- | Opaque token for @kbts_load_font_state@
data LoadFontState = LoadFontState -- XXX: neither a struct, nor a handle
{-
  void *FontData;
  kbts_u32 FontDataSize;

  kbts_blob_table Tables[KBTS_BLOB_TABLE_ID_COUNT];
  kbts_u32 LookupCount;
  kbts_u32 LookupSubtableCount;
  kbts_u32 GlyphCount;
  kbts_u32 ScratchSize;

  kbts_u32 GlyphLookupMatrixSizeInBytes;
  kbts_u32 GlyphLookupSubtableMatrixSizeInBytes;
  kbts_u32 TotalSize;
-}
  deriving (Show)

instance Storable LoadFontState where
  alignment ~_ = #alignment kbts_load_font_state
  sizeOf ~_ = #size kbts_load_font_state
  peek _ptr = pure LoadFontState
  poke ptr LoadFontState{} = fillBytes ptr 0x00 (#size kbts_load_font_state)

-- | @kbts_load_font_error kbts_LoadFont(kbts_font *Font, kbts_load_font_state *State, void *FontData, int FontDataSize, int FontIndex, int *ScratchSize_, int *OutputSize_);@
foreign import ccall unsafe "kbts_LoadFont" kbts_LoadFont
  :: Font
  -> Ptr LoadFontState
  -> Ptr () -- ^ FontData
  -> CInt -- ^ FontDataSize
  -> CInt -- ^ FontIndex
  -> Ptr CInt -- ^ ScratchSize_
  -> Ptr CInt -- ^ OutputSize_
  -> IO LoadFontError

-- | @kbts_load_font_error kbts_PlaceBlob(kbts_font *Font, kbts_load_font_state *State, void *ScratchMemory, void *OutputMemory);@
foreign import ccall unsafe "kbts_PlaceBlob" kbts_PlaceBlob
  :: Font
  -> Ptr LoadFontState
  -> Ptr () -- ^ ScratchMemory
  -> Ptr () -- ^ OutputMemory
  -> IO LoadFontError

-- | @void kbts_GetFontInfo(kbts_font *Font, kbts_font_info *Info);@
foreign import ccall unsafe "kbts_GetFontInfo" kbts_GetFontInfo
  :: Font
  -> Ptr FontInfo
  -> IO ()

-- | @void kbts_GetFontInfo2(kbts_font *Font, kbts_font_info2 *Info);@
--
-- @Info->Size@ must be filled out before the call, see 'prepareFontInfo2'.
foreign import ccall unsafe "kbts_GetFontInfo2" kbts_GetFontInfo2
  :: Font
  -> Ptr FontInfo2
  -> IO ()

-- | @int kbts_LookupFontString(kbts_font *Font, kbts_font_info_string_id StringId, kbts_windows_language_id LanguageId, char *String, int StringCapacity);@
--
-- Writes up to @StringCapacity@ bytes of UTF-8 and returns the full byte length of the string.
foreign import ccall unsafe "kbts_LookupFontString" kbts_LookupFontString
  :: Font
  -> FontInfoStringId
  -> WindowsLanguageId
  -> Ptr CChar -- ^ String
  -> CInt -- ^ StringCapacity
  -> IO CInt

-- | @int kbts_WindowsIdsForLanguage(kbts_language Language, kbts_windows_language_id *Ids, int IdCapacity);@
--
-- Returns the number of matching IDs. Nothing is written unless @IdCapacity@ fits them all.
foreign import ccall unsafe "kbts_WindowsIdsForLanguage" kbts_WindowsIdsForLanguage
  :: Language
  -> Ptr WindowsLanguageId
  -> CInt -- ^ IdCapacity
  -> IO CInt

-- ** Variations

-- | @void kbts_GetVariationInfo(kbts_font *Font, int *AxisCount, int *InstanceCount);@
foreign import ccall unsafe "kbts_GetVariationInfo" kbts_GetVariationInfo
  :: Font
  -> Ptr CInt -- ^ AxisCount
  -> Ptr CInt -- ^ InstanceCount
  -> IO ()

-- | @int kbts_GetVariationAxisInfo(kbts_font *Font, int AxisIndex, kbts_variation_axis_info *Info);@
--
-- Exported, but not declared in the upstream header.
foreign import ccall unsafe "kbts_GetVariationAxisInfo" kbts_GetVariationAxisInfo
  :: Font
  -> CInt -- ^ AxisIndex
  -> Ptr VariationAxisInfo
  -> IO CInt

-- | @int kbts_GetVariationInstance(kbts_font *Font, int InstanceIndex, kbts_variation_instance_info *Info, float *VariationVector);@
--
-- Exported, but not declared in the upstream header.
foreign import ccall unsafe "kbts_GetVariationInstance" kbts_GetVariationInstance
  :: Font
  -> CInt -- ^ InstanceIndex
  -> Ptr VariationInstanceInfo
  -> Ptr Float -- ^ VariationVector, may be null
  -> IO CInt

-- | @void kbts_NormalizeVariationVector(kbts_font *Font, float *VariationVector, kbts_s16 *VariationVectorNormalized);@
foreign import ccall unsafe "kbts_NormalizeVariationVector" kbts_NormalizeVariationVector
  :: Font
  -> Ptr Float -- ^ VariationVector
  -> Ptr Int16 -- ^ VariationVectorNormalized
  -> IO ()

-- A shape_config is a bag of pre-computed data for a specific shaping setup.

-- | @int kbts_SizeOfShapeConfig2(kbts_font *Font, kbts_script Script, kbts_language Language, float *VariationVector);@
foreign import ccall unsafe "kbts_SizeOfShapeConfig2" kbts_SizeOfShapeConfig2
  :: Font
  -> Script
  -> Language
  -> Ptr Float -- ^ VariationVector, may be null
  -> IO CInt

-- | @kbts_shape_config *kbts_PlaceShapeConfig2(kbts_font *Font, kbts_script Script, kbts_language Language, float *VariationVector, void *Memory, kbts_uid *Uid);@
foreign import ccall unsafe "kbts_PlaceShapeConfig2" kbts_PlaceShapeConfig2
  :: Font
  -> Script
  -> Language
  -> Ptr Float -- ^ VariationVector, may be null
  -> Ptr () -- ^ Memory
  -> Ptr Uid -- ^ may be null
  -> IO ShapeConfig

-- | @kbts_shape_config *kbts_CreateShapeConfig2(kbts_font *Font, kbts_script Script, kbts_language Language, float *VariationVector, kbts_allocator_function *Allocator, void *AllocatorData, kbts_uid *Uid);@
foreign import ccall safe "kbts_CreateShapeConfig2" kbts_CreateShapeConfig2
  :: Font
  -> Script
  -> Language
  -> Ptr Float -- ^ VariationVector, may be null
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> Ptr Uid -- ^ may be null
  -> IO ShapeConfig

-- | @void kbts_ShapeConfigUid(kbts_shape_config *Config, kbts_uid *Uid);@
foreign import ccall unsafe "kbts_ShapeConfigUid" kbts_ShapeConfigUid
  :: ShapeConfig
  -> Ptr Uid
  -> IO ()

-- | @int kbts_SizeOfShapeConfig(kbts_font *Font, kbts_script Script, kbts_language Language);@
foreign import ccall unsafe "kbts_SizeOfShapeConfig" kbts_SizeOfShapeConfig
  :: Font
  -> Script
  -> Language
  -> CInt

-- | @kbts_shape_config *kbts_PlaceShapeConfig(kbts_font *Font, kbts_script Script, kbts_language Language, void *Memory);@
foreign import ccall unsafe "kbts_PlaceShapeConfig" kbts_PlaceShapeConfig
  :: Font
  -> Script
  -> Language
  -> Ptr () -- ^ Memory
  -> IO ShapeConfig

-- | @kbts_shape_config *kbts_CreateShapeConfig(kbts_font *Font, kbts_script Script, kbts_language Language, kbts_allocator_function *Allocator, void *AllocatorData);@
foreign import ccall safe "kbts_CreateShapeConfig" kbts_CreateShapeConfig
  :: Font
  -> Script
  -> Language
  -> FunPtr Allocator
  -> Ptr ()
  -> IO ShapeConfig

-- | @void kbts_DestroyShapeConfig(kbts_shape_config *Config);@
foreign import ccall safe "kbts_DestroyShapeConfig" kbts_DestroyShapeConfig
  :: ShapeConfig
  -> IO ()

-- A glyph_storage holds and recycles glyph data.

-- | @int kbts_InitializeGlyphStorage(kbts_glyph_storage *Storage, kbts_allocator_function *Allocator, void *AllocatorData)@
foreign import ccall unsafe "kbts_InitializeGlyphStorage" kbts_InitializeGlyphStorage
  :: Ptr GlyphStorage
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> IO CInt

-- | @int kbts_InitializeGlyphStorageFixedMemory(kbts_glyph_storage *Storage, void *Memory, int MemorySize);@
foreign import ccall unsafe "kbts_InitializeGlyphStorageFixedMemory" kbts_InitializeGlyphStorageFixedMemory
  :: Ptr GlyphStorage
  -> Ptr () -- ^ Memory
  -> CInt -- ^ MemorySize
  -> IO CInt

-- | @kbts_glyph *kbts_PushGlyph(kbts_glyph_storage *Storage, kbts_font *Font, int Codepoint, kbts_glyph_config *Config, int UserId);@
foreign import ccall safe "kbts_PushGlyph" kbts_PushGlyph
  :: Ptr GlyphStorage
  -> Font
  -> CInt -- ^ Codepoint
  -> GlyphConfig
  -> CInt
  -> IO (Ptr Glyph)

-- | @void kbts_ClearActiveGlyphs(kbts_glyph_storage *Storage);@
foreign import ccall unsafe "kbts_ClearActiveGlyphs" kbts_ClearActiveGlyphs
  :: Ptr GlyphStorage
  -> IO ()

-- | @void kbts_FreeAllGlyphs(kbts_glyph_storage *Storage);@
foreign import ccall safe "kbts_FreeAllGlyphs" kbts_FreeAllGlyphs
  :: Ptr GlyphStorage
  -> IO ()

-- | Wrapped @kbts_glyph kbts_CodepointToGlyph(kbts_font *Font, int Codepoint, kbts_glyph_config *Config, int UserId);@
--
-- The original returns struct by value.
foreign import ccall unsafe "hs_CodepointToGlyph" hs_CodepointToGlyph
  :: Font
  -> CInt -- ^ Codepoint
  -> GlyphConfig
  -> CInt -- ^ UserId
  -> Ptr Glyph
  -> IO ()

-- | @int kbts_CodepointToGlyphId(kbts_font *Font, int Codepoint);@
foreign import ccall unsafe "kbts_CodepointToGlyphId" kbts_CodepointToGlyphId
  :: Font
  -> CInt -- ^ Codepoint
  -> IO CInt

-- | Wrapped @kbts_glyph_iterator kbts_ActiveGlyphIterator(kbts_glyph_storage *Storage);@
--
-- The original returns struct by value.
foreign import ccall unsafe "hs_ActiveGlyphIterator" hs_ActiveGlyphIterator
  :: Ptr GlyphStorage
  -> Ptr GlyphIterator
  -> IO ()

-- A glyph_config specifies glyph-specific shaping parameters.
--
-- A single glyph_config can be shared by multiple glyphs.

-- | @int kbts_SizeOfGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount);@
foreign import ccall unsafe "kbts_SizeOfGlyphConfig" kbts_SizeOfGlyphConfig
  :: ShapeConfig
  -> Ptr FeatureOverride
  -> CInt -- ^ OverrideCount
  -> CInt

-- | @kbts_glyph_config *kbts_PlaceGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount, void *Memory);@
foreign import ccall unsafe "kbts_PlaceGlyphConfig" kbts_PlaceGlyphConfig
  :: ShapeConfig
  -> Ptr FeatureOverride
  -> CInt -- ^ OverriderCount
  -> Ptr () -- ^ Memory
  -> IO GlyphConfig

-- | @kbts_glyph_config *kbts_CreateGlyphConfig(kbts_shape_config *ShapeConfig, kbts_feature_override *Overrides, int OverrideCount, kbts_allocator_function *Allocator, void *AllocatorData);@
foreign import ccall safe "kbts_CreateGlyphConfig" kbts_CreateGlyphConfig
  :: ShapeConfig
  -> Ptr FeatureOverride
  -> CInt -- ^ OverrideCount
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> IO GlyphConfig

-- | @void kbts_DestroyGlyphConfig(kbts_glyph_config *Config);@
foreign import ccall safe "kbts_DestroyGlyphConfig" kbts_DestroyGlyphConfig
  :: GlyphConfig
  -> IO ()

-- A shape_scratchpad holds all transient runtime shaping data.
--
-- While the shape_config is immutable and can be trivially shared among threads,
-- a shape_scratchpad is mutable and needs to be per-thread.

-- | @kbts_un kbts_SizeOfShapeScratchpad(kbts_shape_config *Config);@
foreign import ccall unsafe "kbts_SizeOfShapeScratchpad" kbts_SizeOfShapeScratchpad
  :: ShapeConfig
  -> CSize

-- | @kbts_shape_scratchpad *kbts_PlaceShapeScratchpad(kbts_shape_config *Config, void *Memory, kbts_allocator_function *Allocator, void *AllocatorData);@
foreign import ccall safe "kbts_PlaceShapeScratchpad" kbts_PlaceShapeScratchpad
  :: ShapeConfig
  -> Ptr () -- ^ Memory
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> IO ShapeScratchpad

-- | @kbts_shape_scratchpad *kbts_PlaceShapeScratchpadFixedMemory(kbts_shape_config *Config, void *Memory, int Size);@
foreign import ccall unsafe "kbts_PlaceShapeScratchpadFixedMemory" kbts_PlaceShapeScratchpadFixedMemory
  :: ShapeConfig
  -> Ptr () -- ^ Memory
  -> CInt -- ^ Size
  -> IO ShapeScratchpad

-- | @kbts_shape_scratchpad *kbts_CreateShapeScratchpad(kbts_shape_config *Config, kbts_allocator_function *Allocator, void *AllocatorData);@
foreign import ccall safe "kbts_CreateShapeScratchpad" kbts_CreateShapeScratchpad
  :: ShapeConfig
  -> FunPtr Allocator
  -> Ptr () -- ^ AllocatorData
  -> IO ShapeScratchpad

-- | @void kbts_DestroyShapeScratchpad(kbts_shape_scratchpad *Scratchpad);@
foreign import ccall safe "kbts_DestroyShapeScratchpad" kbts_DestroyShapeScratchpad
  :: ShapeScratchpad
  -> IO ()