Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include "mupdf/html.h"
2
3
unsigned char *pdf_lookup_builtin_font(fz_context *ctx, const char *name, unsigned int *len);
4
5
static const char *font_names[16] =
6
{
7
"Times-Roman", "Times-Italic", "Times-Bold", "Times-BoldItalic",
8
"Helvetica", "Helvetica-Oblique", "Helvetica-Bold", "Helvetica-BoldOblique",
9
"Courier", "Courier-Oblique", "Courier-Bold", "Courier-BoldOblique",
10
"Courier", "Courier-Oblique", "Courier-Bold", "Courier-BoldOblique",
11
};
12
13
fz_font *
14
fz_load_html_font(fz_context *ctx, fz_html_font_set *set,
15
const char *family, const char *variant, const char *style, const char *weight)
16
{
17
unsigned char *data;
18
unsigned int size;
19
20
int is_mono = !strcmp(family, "monospace");
21
int is_sans = !strcmp(family, "sans-serif");
22
int is_bold = !strcmp(weight, "bold") || !strcmp(weight, "bolder") || atoi(weight) > 400;
23
int is_italic = !strcmp(style, "italic") || !strcmp(style, "oblique");
24
25
int idx = is_mono * 8 + is_sans * 4 + is_bold * 2 + is_italic;
26
if (!set->fonts[idx])
27
{
28
data = pdf_lookup_builtin_font(ctx, font_names[idx], &size);
29
if (!data)
30
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot load html font: %s", font_names[idx]);
31
set->fonts[idx] = fz_new_font_from_memory(ctx, font_names[idx], data, size, 0, 1);
32
}
33
34
return set->fonts[idx];
35
}
36
37
fz_html_font_set *fz_new_html_font_set(fz_context *ctx)
38
{
39
return fz_malloc_struct(ctx, fz_html_font_set);
40
}
41
42
void fz_drop_html_font_set(fz_context *ctx, fz_html_font_set *set)
43
{
44
int i;
45
for (i = 0; i < nelem(set->fonts); ++i)
46
fz_drop_font(ctx, set->fonts[i]);
47
fz_free(ctx, set);
48
}
49
50