Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
#include "mupdf/pdf.h"
2
3
void
4
pdf_set_font_wmode(fz_context *ctx, pdf_font_desc *font, int wmode)
5
{
6
font->wmode = wmode;
7
}
8
9
void
10
pdf_set_default_hmtx(fz_context *ctx, pdf_font_desc *font, int w)
11
{
12
font->dhmtx.w = w;
13
}
14
15
void
16
pdf_set_default_vmtx(fz_context *ctx, pdf_font_desc *font, int y, int w)
17
{
18
font->dvmtx.y = y;
19
font->dvmtx.w = w;
20
}
21
22
void
23
pdf_add_hmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int w)
24
{
25
if (font->hmtx_len + 1 >= font->hmtx_cap)
26
{
27
int new_cap = font->hmtx_cap + 16;
28
font->hmtx = fz_resize_array(ctx, font->hmtx, new_cap, sizeof(pdf_hmtx));
29
font->hmtx_cap = new_cap;
30
}
31
32
font->hmtx[font->hmtx_len].lo = lo;
33
font->hmtx[font->hmtx_len].hi = hi;
34
font->hmtx[font->hmtx_len].w = w;
35
font->hmtx_len++;
36
}
37
38
void
39
pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, int w)
40
{
41
if (font->vmtx_len + 1 >= font->vmtx_cap)
42
{
43
int new_cap = font->vmtx_cap + 16;
44
font->vmtx = fz_resize_array(ctx, font->vmtx, new_cap, sizeof(pdf_vmtx));
45
font->vmtx_cap = new_cap;
46
}
47
48
font->vmtx[font->vmtx_len].lo = lo;
49
font->vmtx[font->vmtx_len].hi = hi;
50
font->vmtx[font->vmtx_len].x = x;
51
font->vmtx[font->vmtx_len].y = y;
52
font->vmtx[font->vmtx_len].w = w;
53
font->vmtx_len++;
54
}
55
56
static int cmph(const void *a0, const void *b0)
57
{
58
pdf_hmtx *a = (pdf_hmtx*)a0;
59
pdf_hmtx *b = (pdf_hmtx*)b0;
60
return a->lo - b->lo;
61
}
62
63
static int cmpv(const void *a0, const void *b0)
64
{
65
pdf_vmtx *a = (pdf_vmtx*)a0;
66
pdf_vmtx *b = (pdf_vmtx*)b0;
67
return a->lo - b->lo;
68
}
69
70
void
71
pdf_end_hmtx(fz_context *ctx, pdf_font_desc *font)
72
{
73
if (!font->hmtx)
74
return;
75
qsort(font->hmtx, font->hmtx_len, sizeof(pdf_hmtx), cmph);
76
font->size += font->hmtx_cap * sizeof(pdf_hmtx);
77
}
78
79
void
80
pdf_end_vmtx(fz_context *ctx, pdf_font_desc *font)
81
{
82
if (!font->vmtx)
83
return;
84
qsort(font->vmtx, font->vmtx_len, sizeof(pdf_vmtx), cmpv);
85
font->size += font->vmtx_cap * sizeof(pdf_vmtx);
86
}
87
88
pdf_hmtx
89
pdf_lookup_hmtx(fz_context *ctx, pdf_font_desc *font, int cid)
90
{
91
int l = 0;
92
int r = font->hmtx_len - 1;
93
int m;
94
95
if (!font->hmtx)
96
goto notfound;
97
98
while (l <= r)
99
{
100
m = (l + r) >> 1;
101
if (cid < font->hmtx[m].lo)
102
r = m - 1;
103
else if (cid > font->hmtx[m].hi)
104
l = m + 1;
105
else
106
return font->hmtx[m];
107
}
108
109
notfound:
110
return font->dhmtx;
111
}
112
113
pdf_vmtx
114
pdf_lookup_vmtx(fz_context *ctx, pdf_font_desc *font, int cid)
115
{
116
pdf_hmtx h;
117
pdf_vmtx v;
118
int l = 0;
119
int r = font->vmtx_len - 1;
120
int m;
121
122
if (!font->vmtx)
123
goto notfound;
124
125
while (l <= r)
126
{
127
m = (l + r) >> 1;
128
if (cid < font->vmtx[m].lo)
129
r = m - 1;
130
else if (cid > font->vmtx[m].hi)
131
l = m + 1;
132
else
133
return font->vmtx[m];
134
}
135
136
notfound:
137
h = pdf_lookup_hmtx(ctx, font, cid);
138
v = font->dvmtx;
139
v.x = h.w / 2;
140
return v;
141
}
142
143