pastry
truetype font renderer
pastry.h
2591 bytes raw
1#ifndef PASTRY_H
2#define PASTRY_H
3
4#include <stddef.h>
5#include <stdint.h>
6#include <stdlib.h>
7#include <string.h>
8#include <limits.h>
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14#ifndef PASTRY_MALLOC
15# define PASTRY_MALLOC(size) malloc(size)
16#endif
17
18#ifndef PASTRY_FREE
19# define PASTRY_FREE(ptr) free(ptr)
20#endif
21
22#ifndef PASTRY_REALLOC
23# define PASTRY_REALLOC(ptr, size) realloc(ptr, size)
24#endif
25
26#ifndef PASTRY_CALLOC
27static inline void* pastry_calloc_impl(size_t n, size_t size) {
28 if (n == 0 || size == 0) return NULL;
29 if (n > SIZE_MAX / size) return NULL;
30
31 size_t total = n * size;
32 void* ptr = PASTRY_MALLOC(total);
33 // most environments do have memset tbh, even really limited ones
34 // this should be fine
35 if (ptr) memset(ptr, 0, total);
36 return ptr;
37}
38# define PASTRY_CALLOC(n, size) pastry_calloc_impl(n, size)
39#endif
40
41// if mmap is not present in your environment
42// #define PASTRY_NO_MMAP
43
44#ifndef PASTRY_DOWNWARD_Y
45# define PASTRY_DOWNWARD_Y 0x01
46#endif
47
48typedef struct PASTRY PASTRY;
49typedef struct PASTRY_Font PASTRY_Font;
50typedef uint_least32_t PASTRY_UChar;
51typedef uint_fast32_t PASTRY_Glyph;
52typedef struct PASTRY_LMetrics PASTRY_LMetrics;
53typedef struct PASTRY_GMetrics PASTRY_GMetrics;
54typedef struct PASTRY_Kerning PASTRY_Kerning;
55typedef struct PASTRY_Image PASTRY_Image;
56
57struct PASTRY
58{
59 PASTRY_Font *font;
60 double xScale;
61 double yScale;
62 double xOffset;
63 double yOffset;
64 int flags;
65};
66
67struct PASTRY_LMetrics
68{
69 double ascender;
70 double descender;
71 double lineGap;
72};
73
74struct PASTRY_GMetrics
75{
76 double advanceWidth;
77 double leftSideBearing;
78 int yOffset;
79 int minWidth;
80 int minHeight;
81};
82
83struct PASTRY_Kerning
84{
85 double xShift;
86 double yShift;
87};
88
89struct PASTRY_Image
90{
91 void *pixels;
92 int width;
93 int height;
94};
95
96const char *pastry_version(void);
97
98PASTRY_Font *pastry_loadmem (const void *mem, size_t size);
99PASTRY_Font *pastry_loadfile(const char *filename);
100void pastry_freefont(PASTRY_Font *font);
101
102int pastry_lmetrics(const PASTRY *pastry, PASTRY_LMetrics *metrics);
103int pastry_lookup (const PASTRY *pastry, PASTRY_UChar codepoint, PASTRY_Glyph *glyph);
104int pastry_gmetrics(const PASTRY *pastry, PASTRY_Glyph glyph, PASTRY_GMetrics *metrics);
105int pastry_kerning (const PASTRY *pastry, PASTRY_Glyph leftGlyph, PASTRY_Glyph rightGlyph,
106 PASTRY_Kerning *kerning);
107int pastry_render (const PASTRY *pastry, PASTRY_Glyph glyph, PASTRY_Image image);
108
109#ifdef __cplusplus
110}
111#endif
112
113#endif