Changeset - r27601:272d990bb25f
[Not reviewed]
master
0 1 0
Rubidium - 14 months ago 2023-06-18 15:23:11
rubidium@openttd.org
Codechange: use std::string over stredup+free for splitting font name
1 file changed with 22 insertions and 22 deletions:
0 comments (0 inline, 0 general)
src/os/unix/font_unix.cpp
Show inline comments
 
@@ -19,12 +19,26 @@
 

	
 
#include <ft2build.h>
 
#include FT_FREETYPE_H
 

	
 
extern FT_Library _library;
 

	
 
/**
 
 * Split the font name into the font family and style. These fields are separated by a comma,
 
 * but the style does not necessarily need to exist.
 
 * @param font_name The font name.
 
 * @return The font family and style.
 
 */
 
static std::tuple<std::string, std::string> SplitFontFamilyAndStyle(std::string_view font_name)
 
{
 
	auto separator = font_name.find(',');
 
	if (separator == std::string_view::npos) return { std::string(font_name), std::string() };
 

	
 
	auto begin = font_name.find_first_not_of("\t ", separator + 1);
 
	return { std::string(font_name.substr(0, separator)), std::string(font_name.substr(begin)) };
 
}
 

	
 
FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
 
{
 
	FT_Error err = FT_Err_Cannot_Open_Resource;
 

	
 
	if (!FcInit()) {
 
@@ -32,65 +46,51 @@ FT_Error GetFontByFaceName(const char *f
 
		return err;
 
	}
 

	
 
	auto fc_instance = FcConfigReference(nullptr);
 
	assert(fc_instance != nullptr);
 

	
 
	FcPattern *match;
 
	FcPattern *pat;
 
	FcFontSet *fs;
 
	FcResult  result;
 
	char *font_style;
 
	char *font_family;
 

	
 
	/* Split & strip the font's style */
 
	font_family = stredup(font_name);
 
	font_style = strchr(font_family, ',');
 
	if (font_style != nullptr) {
 
		font_style[0] = '\0';
 
		font_style++;
 
		while (*font_style == ' ' || *font_style == '\t') font_style++;
 
	}
 
	auto [font_family, font_style] = SplitFontFamilyAndStyle(font_name);
 

	
 
	/* Resolve the name and populate the information structure */
 
	pat = FcNameParse((FcChar8 *)font_family);
 
	if (font_style != nullptr) FcPatternAddString(pat, FC_STYLE, (FcChar8 *)font_style);
 
	FcPattern *pat = FcNameParse((FcChar8 *)font_family.data());
 
	if (!font_style.empty()) FcPatternAddString(pat, FC_STYLE, (FcChar8 *)font_style.data());
 
	FcConfigSubstitute(nullptr, pat, FcMatchPattern);
 
	FcDefaultSubstitute(pat);
 
	fs = FcFontSetCreate();
 
	match = FcFontMatch(nullptr, pat, &result);
 
	FcFontSet *fs = FcFontSetCreate();
 
	FcResult  result;
 
	FcPattern *match = FcFontMatch(nullptr, pat, &result);
 

	
 
	if (fs != nullptr && match != nullptr) {
 
		int i;
 
		FcChar8 *family;
 
		FcChar8 *style;
 
		FcChar8 *file;
 
		int32_t index;
 
		FcFontSetAdd(fs, match);
 

	
 
		for (i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
 
		for (int i = 0; err != FT_Err_Ok && i < fs->nfont; i++) {
 
			/* Try the new filename */
 
			if (FcPatternGetString(fs->fonts[i], FC_FILE, 0, &file) == FcResultMatch &&
 
				FcPatternGetString(fs->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch &&
 
				FcPatternGetString(fs->fonts[i], FC_STYLE, 0, &style) == FcResultMatch &&
 
				FcPatternGetInteger(fs->fonts[i], FC_INDEX, 0, &index) == FcResultMatch) {
 

	
 
				/* The correct style? */
 
				if (font_style != nullptr && !StrEqualsIgnoreCase(font_style, (char *)style)) continue;
 
				if (!font_style.empty() && !StrEqualsIgnoreCase(font_style, (char *)style)) continue;
 

	
 
				/* Font config takes the best shot, which, if the family name is spelled
 
					* wrongly a 'random' font, so check whether the family name is the
 
					* same as the supplied name */
 
				if (StrEqualsIgnoreCase(font_family, (char *)family)) {
 
					err = FT_New_Face(_library, (char *)file, index, face);
 
				}
 
			}
 
		}
 
	}
 

	
 
	free(font_family);
 
	FcPatternDestroy(pat);
 
	FcFontSetDestroy(fs);
 
	FcConfigDestroy(fc_instance);
 

	
 
	return err;
 
}
0 comments (0 inline, 0 general)