λ

pastry

truetype font renderer

settings

pastry.h

2591 bytes raw

#ifndef PASTRY_H
#define PASTRY_H

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef PASTRY_MALLOC
# define PASTRY_MALLOC(size) malloc(size)
#endif

#ifndef PASTRY_FREE
# define PASTRY_FREE(ptr) free(ptr)
#endif

#ifndef PASTRY_REALLOC
# define PASTRY_REALLOC(ptr, size) realloc(ptr, size)
#endif

#ifndef PASTRY_CALLOC
static inline void* pastry_calloc_impl(size_t n, size_t size) {
    if (n == 0 || size == 0) return NULL;
    if (n > SIZE_MAX / size) return NULL;

    size_t total = n * size;
    void* ptr = PASTRY_MALLOC(total);
    // most environments do have memset tbh, even really limited ones
    // this should be fine
    if (ptr) memset(ptr, 0, total);
    return ptr;
}
# define PASTRY_CALLOC(n, size) pastry_calloc_impl(n, size)
#endif

// if mmap is not present in your environment
// #define PASTRY_NO_MMAP

#ifndef PASTRY_DOWNWARD_Y
# define PASTRY_DOWNWARD_Y 0x01
#endif

typedef struct PASTRY          PASTRY;
typedef struct PASTRY_Font     PASTRY_Font;
typedef uint_least32_t         PASTRY_UChar;
typedef uint_fast32_t          PASTRY_Glyph;
typedef struct PASTRY_LMetrics PASTRY_LMetrics;
typedef struct PASTRY_GMetrics PASTRY_GMetrics;
typedef struct PASTRY_Kerning  PASTRY_Kerning;
typedef struct PASTRY_Image    PASTRY_Image;

struct PASTRY
{
    PASTRY_Font *font;
    double    xScale;
    double    yScale;
    double    xOffset;
    double    yOffset;
    int       flags;
};

struct PASTRY_LMetrics
{
    double ascender;
    double descender;
    double lineGap;
};

struct PASTRY_GMetrics
{
    double advanceWidth;
    double leftSideBearing;
    int    yOffset;
    int    minWidth;
    int    minHeight;
};

struct PASTRY_Kerning
{
    double xShift;
    double yShift;
};

struct PASTRY_Image
{
    void *pixels;
    int   width;
    int   height;
};

const char *pastry_version(void);

PASTRY_Font *pastry_loadmem (const void *mem, size_t size);
PASTRY_Font *pastry_loadfile(const char *filename);
void         pastry_freefont(PASTRY_Font *font);

int pastry_lmetrics(const PASTRY *pastry, PASTRY_LMetrics *metrics);
int pastry_lookup  (const PASTRY *pastry, PASTRY_UChar codepoint, PASTRY_Glyph *glyph);
int pastry_gmetrics(const PASTRY *pastry, PASTRY_Glyph glyph, PASTRY_GMetrics *metrics);
int pastry_kerning (const PASTRY *pastry, PASTRY_Glyph leftGlyph, PASTRY_Glyph rightGlyph,
                    PASTRY_Kerning *kerning);
int pastry_render  (const PASTRY *pastry, PASTRY_Glyph glyph, PASTRY_Image image);

#ifdef __cplusplus
}
#endif

#endif