summaryrefslogtreecommitdiff
path: root/source/fitz/harfbuzz.c
blob: 60d75469b4d2e7ba91e4c3ca72e50a5d1a15280f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Some additional glue functions for using Harfbuzz with
 * custom allocators.
 */

#include "mupdf/fitz.h"

#include "hb.h"

/* Potentially we can write different versions
 * of get_context and set_context for different
 * threading systems.
 *
 * This simple version relies on harfbuzz never
 * trying to make 2 allocations at once on
 * different threads. The only way that can happen
 * is when one of those other threads is someone
 * outside MuPDF calling harfbuzz while MuPDF
 * is running.
 *
 * If this is actually a problem, then we can
 * reimplement set_context/get_context using
 * Thread Local Storage.
 */

static fz_context *hb_secret = NULL;

static void set_context(fz_context *ctx)
{
	hb_secret = ctx;
}

static fz_context *get_context()
{
	return hb_secret;
}

void hb_lock(fz_context *ctx)
{
	fz_lock(ctx, FZ_LOCK_FREETYPE);

	set_context(ctx);
}

void hb_unlock(fz_context *ctx)
{
	set_context(NULL);

	fz_unlock(ctx, FZ_LOCK_FREETYPE);
}

void *hb_malloc(size_t size)
{
	fz_context *ctx = get_context();

	/* Should never happen, but possibly someone else
	 * is calling our version of the library. */
	if (ctx == NULL)
		return malloc(size);

	return fz_malloc_no_throw(ctx, (unsigned int)size);
}

void *hb_calloc(size_t n, size_t size)
{
	fz_context *ctx = get_context();

	/* Should never happen, but possibly someone else
	 * is calling our version of the library. */
	if (ctx == NULL)
		return calloc(n, size);

	return fz_calloc_no_throw(ctx, (unsigned int)n, (unsigned int)size);
}

void *hb_realloc(void *ptr, size_t size)
{
	fz_context *ctx = get_context();

	/* Should never happen, but possibly someone else
	 * is calling our version of the library. */
	if (ctx == NULL)
		return realloc(ptr, size);

	return fz_resize_array_no_throw(ctx, ptr, (unsigned int)1, (unsigned int)size);
}

void hb_free(void *ptr)
{
	fz_context *ctx = get_context();

	/* Should never happen, but possibly someone else
	 * is calling our version of the library. */
	if (ctx == NULL)
		free(ptr);
	else
		fz_free(ctx, ptr);
}