<commit_before>#include "fitz.h"
#include "mupdf.h"

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_XFREE86_H

static char *basefontnames[14][7] =
{
	{ "Courier", "CourierNew", "CourierNewPSMT", nil },
	{ "Courier-Bold", "CourierNew,Bold", "Courier,Bold",
		"CourierNewPS-BoldMT", "CourierNew-Bold", nil },
	{ "Courier-Oblique", "CourierNew,Italic", "Courier,Italic",
		"CourierNewPS-ItalicMT", "CourierNew-Italic", nil },
	{ "Courier-BoldOblique", "CourierNew,BoldItalic", "Courier,BoldItalic",
		"CourierNewPS-BoldItalicMT", "CourierNew-BoldItalic", nil },
	{ "Helvetica", "ArialMT", "Arial", nil },
	{ "Helvetica-Bold", "Arial-BoldMT", "Arial,Bold", "Arial-Bold",
		"Helvetica,Bold", nil },
	{ "Helvetica-Oblique", "Arial-ItalicMT", "Arial,Italic", "Arial-Italic",
		"Helvetica,Italic", "Helvetica-Italic", nil },
	{ "Helvetica-BoldOblique", "Arial-BoldItalicMT",
		"Arial,BoldItalic", "Arial-BoldItalic",
		"Helvetica,BoldItalic", "Helvetica-BoldItalic", nil },
	{ "Times-Roman", "TimesNewRomanPSMT", "TimesNewRoman",
		"TimesNewRomanPS", nil },
	{ "Times-Bold", "TimesNewRomanPS-BoldMT", "TimesNewRoman,Bold",
		"TimesNewRomanPS-Bold", "TimesNewRoman-Bold", nil },
	{ "Times-Italic", "TimesNewRomanPS-ItalicMT", "TimesNewRoman,Italic",
		"TimesNewRomanPS-Italic", "TimesNewRoman-Italic", nil },
	{ "Times-BoldItalic", "TimesNewRomanPS-BoldItalicMT",
		"TimesNewRoman,BoldItalic", "TimesNewRomanPS-BoldItalic",
		"TimesNewRoman-BoldItalic", nil },
	{ "Symbol", nil },
	{ "ZapfDingbats", nil }
};

/*
 * FreeType and Rendering glue
 */

enum { UNKNOWN, TYPE1, TRUETYPE, CID };

static int ftkind(FT_Face face)
{
	const char *kind = FT_Get_X11_Font_Format(face);
	pdf_logfont("ft font format %s\n", kind);
	if (!strcmp(kind, "TrueType"))
		return TRUETYPE;
	if (!strcmp(kind, "Type 1"))
		return TYPE1;
	if (!strcmp(kind, "CFF"))
		return TYPE1;
	if (!strcmp(kind, "CID Type 1"))
		return TYPE1;
	return UNKNOWN;
}

static int ftcharindex(FT_Face face, int cid)
{
	int gid = FT_Get_Char_Index(face, cid);
	if (gid == 0)
		gid = FT_Get_Char_Index(face, 0xf000 + cid);
	return gid;
}

static inline int ftcidtogid(pdf_fontdesc *fontdesc, int cid)
{
	if (fontdesc->tottfcmap)
	{
		cid = pdf_lookupcmap(fontdesc->tottfcmap, cid);
		return ftcharindex(fontdesc->font->ftface, cid);
	}

	if (fontdesc->cidtogid)
		return fontdesc->cidtogid[cid];

	return cid;
}

int pdf_fontcidtogid(pdf_fontdesc *fontdesc, int cid)
{
    if (fontdesc->font->ftface)
	return ftcidtogid(fontdesc, cid);
    return cid;
}

static int ftwidth(pdf_fontdesc *fontdesc, int cid)
{
	int gid, fterr;

	gid = ftcidtogid(fontdesc, cid);

	fterr = FT_Load_Glyph(fontdesc->font->ftface, gid,
			FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
	if (fterr)
	{
		fz_warn("freetype load glyph (gid %d): %s", gid, ft_errorstring(fterr));
		return 0;
	}
	return ((FT_Face)fontdesc->font->ftface)->glyph->advance.x;
}

/*
 * Basic encoding tables
 */

static char *cleanfontname(char *fontname)
{
	int i, k;
	for (i = 0; i < 14; i++)
		for (k = 0; basefontnames[i][k]; k++)
			if (!strcmp(basefontnames[i][k], fontname))
				return basefontnames[i][0];
	return fontname;
}

static int mrecode(char *name)
{
	int i;
	for (i = 0; i < 256; i++)
		if (pdf_macroman[i] && !strcmp(name, pdf_macroman[i]))<commit_msg>Ignore spaces when looking for a matching built in font.<commit_after>#include "fitz.h"
#include "mupdf.h"

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_XFREE86_H

static char *basefontnames[14][7] =
{
	{ "Courier", "CourierNew", "CourierNewPSMT", nil },
	{ "Courier-Bold", "CourierNew,Bold", "Courier,Bold",
		"CourierNewPS-BoldMT", "CourierNew-Bold", nil },
	{ "Courier-Oblique", "CourierNew,Italic", "Courier,Italic",
		"CourierNewPS-ItalicMT", "CourierNew-Italic", nil },
	{ "Courier-BoldOblique", "CourierNew,BoldItalic", "Courier,BoldItalic",
		"CourierNewPS-BoldItalicMT", "CourierNew-BoldItalic", nil },
	{ "Helvetica", "ArialMT", "Arial", nil },
	{ "Helvetica-Bold", "Arial-BoldMT", "Arial,Bold", "Arial-Bold",
		"Helvetica,Bold", nil },
	{ "Helvetica-Oblique", "Arial-ItalicMT", "Arial,Italic", "Arial-Italic",
		"Helvetica,Italic", "Helvetica-Italic", nil },
	{ "Helvetica-BoldOblique", "Arial-BoldItalicMT",
		"Arial,BoldItalic", "Arial-BoldItalic",
		"Helvetica,BoldItalic", "Helvetica-BoldItalic", nil },
	{ "Times-Roman", "TimesNewRomanPSMT", "TimesNewRoman",
		"TimesNewRomanPS", nil },
	{ "Times-Bold", "TimesNewRomanPS-BoldMT", "TimesNewRoman,Bold",
		"TimesNewRomanPS-Bold", "TimesNewRoman-Bold", nil },
	{ "Times-Italic", "TimesNewRomanPS-ItalicMT", "TimesNewRoman,Italic",
		"TimesNewRomanPS-Italic", "TimesNewRoman-Italic", nil },
	{ "Times-BoldItalic", "TimesNewRomanPS-BoldItalicMT",
		"TimesNewRoman,BoldItalic", "TimesNewRomanPS-BoldItalic",
		"TimesNewRoman-BoldItalic", nil },
	{ "Symbol", nil },
	{ "ZapfDingbats", nil }
};

static int strcmpignorespace(char *a, char *b)
{
    while (1)
    {
	while (*a == ' ')
	    a++;
	while (*b == ' ')
	    b++;
	if (*a != *b)
	    return 1;
	if (*a == 0)
	    return *a != *b;
	if (*b == 0)
	    return *a != *b;
	a++;
	b++;
    }
}

static char *cleanfontname(char *fontname)
{
	int i, k;
	for (i = 0; i < 14; i++)
		for (k = 0; basefontnames[i][k]; k++)
			if (!strcmpignorespace(basefontnames[i][k], fontname))
				return basefontnames[i][0];
	return fontname;
}

/*
 * FreeType and Rendering glue
 */

enum { UNKNOWN, TYPE1, TRUETYPE, CID };

static int ftkind(FT_Face face)
{
	const char *kind = FT_Get_X11_Font_Format(face);
	pdf_logfont("ft font format %s\n", kind);
	if (!strcmp(kind, "TrueType"))
		return TRUETYPE;
	if (!strcmp(kind, "Type 1"))
		return TYPE1;
	if (!strcmp(kind, "CFF"))
		return TYPE1;
	if (!strcmp(kind, "CID Type 1"))
		return TYPE1;
	return UNKNOWN;
}

static int ftcharindex(FT_Face face, int cid)
{
	int gid = FT_Get_Char_Index(face, cid);
	if (gid == 0)
		gid = FT_Get_Char_Index(face, 0xf000 + cid);
	return gid;
}

static inline int ftcidtogid(pdf_fontdesc *fontdesc, int cid)
{
	if (fontdesc->tottfcmap)
	{
		cid = pdf_lookupcmap(fontdesc->tottfcmap, cid);
		return ftcharindex(fontdesc->font->ftface, cid);
	}

	if (fontdesc->cidtogid)
		return fontdesc->cidtogid[cid];

	return cid;
}

int pdf_fontcidtogid(pdf_fontdesc *fontdesc, int cid)
{
    if (fontdesc->font->ftface)
	return ftcidtogid(fontdesc, cid);
    return cid;
}

static int ftwidth(pdf_fontdesc *fontdesc, int cid)
{
	int gid, fterr;

	gid = ftcidtogid(fontdesc, cid);

	fterr = FT_Load_Glyph(fontdesc->font->ftface, gid,
			FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM);
	if (fterr)
	{
		fz_warn("freetype load glyph (gid %d): %s", gid, ft_errorstring(fterr));
		return 0;
	}
	return ((FT_Face)fontdesc->font->ftface)->glyph->advance.x;
}

/*
 * Basic encoding tables
 */

static int mrecode(char *name)
{
	int i;
	for (i = 0; i < 256; i++)
		if (pdf_macroman[i] && !strcmp(name, pdf_macroman[i]))
{'fraction_non_alphanumeric': 0.17245873153779323, 'fraction_numerical': 0.008977700550246163, 'mean_word_length': 5.080105633802817, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '459168', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3000, 'n_words': 563}