<commit_before>// -*- c-basic-offset: 8; indent-tabs-mode: t -*-
// vim:ts=8:sw=8:noet:ai:
/*
  Copyright (C) 2006 <NAME> <<EMAIL>>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "config.h"

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

#include <assert.h>

#include "mp_msg.h"
#include "ass_fontconfig.h"
#include "ass_bitmap.h"
#include "ass_cache.h"


typedef struct face_cache_item_s {
	face_desc_t desc;
	char* path;
	int index;
	FT_Face face;
} face_cache_item_t;

#define MAX_FACE_CACHE_SIZE 100

static face_cache_item_t* face_cache;
static int face_cache_size;

extern int no_more_font_messages;

static int font_compare(face_desc_t* a, face_desc_t* b) {
	if (strcmp(a->family, b->family) != 0)
		return 0;
	if (a->bold != b->bold)
		return 0;
	if (a->italic != b->italic)
		return 0;
	return 1;
}

/**
 * \brief Get a face object, either from cache or created through FreeType+FontConfig.
 * \param library FreeType library object
 * \param fontconfig_priv fontconfig private data
 * \param desc required face description
 * \param face out: the face object
*/ 
int ass_new_face(FT_Library library, void* fontconfig_priv, face_desc_t* desc, /*out*/ FT_Face* face)
{
	FT_Error error;
	int i;
	char* path;
	int index;
	face_cache_item_t* item;
	
	for (i=0; i<face_cache_size; ++i)
		if (font_compare(desc, &(face_cache[i].desc))) {
			*face = face_cache[i].face;
			return 0;
		}

	if (face_cache_size == MAX_FACE_CACHE_SIZE) {
		mp_msg(MSGT_ASS, MSGL_FATAL, "Too many fonts\n");
		return 1;
	}

	path = fontconfig_select(fontconfig_priv, desc->family, desc->bold, desc->italic, &index);
	
	error = FT_New_Face(library, path, index, face);
	if (error) {
		if (!no_more_font_messages)
			mp_msg(MSGT_ASS, MSGL_WARN, "Error opening font: %s, %d\n", path, index);
		no_more_font_messages = 1;
		return 1;
	}
	
	item = face_cache + face_cache_size;
	item->path = strdup(path);
	item->index = index;
	item->face = *face;
	memcpy(&(item->desc), desc, sizeof(face_desc_t));
	face_cache_size++;
	return 0;
}

void ass_face_cache_init(void)
{
	face_cache = calloc(MAX_FACE_CACHE_SIZE, sizeof(face_cache_item_t));
	face_cache_size = 0;
}

void ass_face_cache_done(void)
{
	int i;
	for (i = 0; i < face_cache_size; ++i) {
		face_cache_item_t* item = face_cache + i;
		if (item->face) FT_Done_Face(item->face);
		if (item->path) free(item->path);
		// FIXME: free desc ?
	}
	free(face_cache);<commit_msg>Prefer microsoft-specific charmaps to all other. There are some fonts with both 'Unicode' and 'Microsoft/Unicode' charmaps, and the second always seems to be the right choice.<commit_after>// -*- c-basic-offset: 8; indent-tabs-mode: t -*-
// vim:ts=8:sw=8:noet:ai:
/*
  Copyright (C) 2006 <NAME> <<EMAIL>>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "config.h"

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

#include <assert.h>

#include "mp_msg.h"
#include "ass_fontconfig.h"
#include "ass_bitmap.h"
#include "ass_cache.h"


typedef struct face_cache_item_s {
	face_desc_t desc;
	char* path;
	int index;
	FT_Face face;
} face_cache_item_t;

#define MAX_FACE_CACHE_SIZE 100

static face_cache_item_t* face_cache;
static int face_cache_size;

extern int no_more_font_messages;

static int font_compare(face_desc_t* a, face_desc_t* b) {
	if (strcmp(a->family, b->family) != 0)
		return 0;
	if (a->bold != b->bold)
		return 0;
	if (a->italic != b->italic)
		return 0;
	return 1;
}

/**
 * Select Microfost Unicode CharMap, if the font has one.
 * Otherwise, let FreeType decide.
 */
static void charmap_magic(FT_Face face)
{
	int i;
	for (i = 0; i < face->num_charmaps; ++i) {
		FT_CharMap cmap = face->charmaps[i];
		unsigned pid = cmap->platform_id;
		unsigned eid = cmap->encoding_id;
		if (pid == 3 /*microsoft*/ && (eid == 1 /*unicode bmp*/ || eid == 10 /*full unicode*/)) {
			FT_Set_Charmap(face, cmap);
			break;
		}
	}
}

/**
 * \brief Get a face object, either from cache or created through FreeType+FontConfig.
 * \param library FreeType library object
 * \param fontconfig_priv fontconfig private data
 * \param desc required face description
 * \param face out: the face object
*/ 
int ass_new_face(FT_Library library, void* fontconfig_priv, face_desc_t* desc, /*out*/ FT_Face* face)
{
	FT_Error error;
	int i;
	char* path;
	int index;
	face_cache_item_t* item;
	
	for (i=0; i<face_cache_size; ++i)
		if (font_compare(desc, &(face_cache[i].desc))) {
			*face = face_cache[i].face;
			return 0;
		}

	if (face_cache_size == MAX_FACE_CACHE_SIZE) {
		mp_msg(MSGT_ASS, MSGL_FATAL, "Too many fonts\n");
		return 1;
	}

	path = fontconfig_select(fontconfig_priv, desc->family, desc->bold, desc->italic, &index);
	
	error = FT_New_Face(library, path, index, face);
	if (error) {
		if (!no_more_font_messages)
			mp_msg(MSGT_ASS, MSGL_WARN, "Error opening font: %s, %d\n", path, index);
		no_more_font_messages = 1;
		return 1;
	}

	charmap_magic(*face);
	
	item = face_cache + face_cache_size;
	item->path = strdup(path);
	item->index = index;
	item->face = *face;
	memcpy(&(item->desc), desc, sizeof(face_desc_t));
	face_cache_size++;
	return 0;
}

void ass_face_cache_init(void)
{
	face_cache = calloc(MAX_FACE_CACHE_SIZE, sizeof(face_cache_item_t));
	face_cache_size = 0;
}

void ass_face_cache_done(void)
{
	int i;
	for (i = 0; i < face_cache_size; ++i) {
		face_cache_item_t* item = face_cache + i;
		if (item->face) FT_Done_Face(item->face);
		if (item->path) free(item->path);
		// FIXME: free desc ?
	}
	free(face_cache);
{'fraction_non_alphanumeric': 0.11847714243716008, 'fraction_numerical': 0.011318535940026459, 'mean_word_length': 4.30319563522993, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 51, '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': '612170', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2454, 'n_words': 750}