Example #1
0
	Font_Cache FontFamily_Impl::get_font(const FontDescription &desc, float pixel_ratio)
	{
		// Find cached version
		for (auto &cache : font_cache)
		{
			if (cache.pixel_ratio != pixel_ratio)
				continue;
			if (desc.get_style() != cache.engine->get_desc().get_style())
				continue;
			if (desc.get_weight() != cache.engine->get_desc().get_weight())
				continue;
			if (desc.get_subpixel() != cache.engine->get_desc().get_subpixel())
				continue;
			if (desc.get_anti_alias() != cache.engine->get_desc().get_anti_alias())
				continue;

			if (cache.engine->is_automatic_recreation_allowed())
			{
				if (desc.get_height() != cache.engine->get_desc().get_height())
					continue;
			}

			return cache;
		}
		return Font_Cache();
	}
void FontProvider_Vector::load_font(const FontDescription &desc, const std::string &filename)
{
#ifdef WIN32
	font_engine = new FontEngine_Win32(desc, filename);
#elif defined(__APPLE__)
	font_engine = new FontEngine_Cocoa(desc, filename);
#else

	std::string font_file_path = filename;
	if (font_file_path.empty())
	{
	    // Obtain the best matching font file from fontconfig.
		FontConfig &fc = FontConfig::instance();
		font_file_path = fc.match_font(desc);
	}

	std::string path = PathHelp::get_fullpath(font_file_path, PathHelp::path_type_file);
	std::string new_filename = PathHelp::get_filename(font_file_path, PathHelp::path_type_file);
	FileSystem vfs(path);
	IODevice io_dev = vfs.open_file(new_filename);

	int average_width = desc.get_average_width();
	int height = desc.get_height();

	// Ensure width and height are positive
	if (average_width < 0) average_width =-average_width;
	if (height < 0) height =-height;

	font_engine = new FontEngine_Freetype(io_dev, average_width, height);
#endif

	metrics = font_engine->get_metrics();
}
Example #3
0
	std::string FontConfig::match_font(const std::string &typeface_name, const FontDescription &desc) const
	{
		FcPattern * fc_pattern = nullptr;
		FcPattern * fc_match = nullptr;
		try
		{
			int weight = static_cast<int>(desc.get_weight());

			// Build font matching pattern.
			fc_pattern = FcPatternBuild(nullptr,
				FC_FAMILY, FcTypeString, typeface_name.c_str(),
				FC_PIXEL_SIZE, FcTypeDouble, (double)std::abs(desc.get_height()),
				FC_WEIGHT, FcTypeInteger, (weight > 0) ? (int)(weight * (FC_WEIGHT_HEAVY / 900.0)) : FC_WEIGHT_NORMAL,
				FC_SLANT, FcTypeInteger, (desc.get_style() == clan::FontStyle::italic) ? FC_SLANT_ITALIC : ((desc.get_style() == clan::FontStyle::oblique) ? FC_SLANT_OBLIQUE : FC_SLANT_ROMAN),
				FC_SPACING, FcTypeInteger, FC_PROPORTIONAL,
				(char*) nullptr
				);
			if (!fc_pattern)
			{
				throw Exception("CL_FontConfig: Building FontConfig pattern failed.");
			}

			// Execute any needed param substitutions required by the system config.
			if (FcTrue != FcConfigSubstitute(fc_config, fc_pattern, FcMatchPattern))
			{
				throw Exception("CL_FontConfig: Font config substitutions failed.");
			}

			// Supply default values for underspecified font patterns. Never fails.
			FcDefaultSubstitute(fc_pattern);

			// Find best match for pattern and extract filename.
			FcResult match_result; // Doesn't appear to be actually updated.
			fc_match = FcFontMatch(fc_config, fc_pattern, &match_result);
			FcChar8 * fc_font_file_path = nullptr;
			if (FcResultMatch != FcPatternGetString(fc_match, FC_FILE, 0, &fc_font_file_path))
			{
				throw Exception("CL_FontConfig: Could not resolve font pattern to a font file.");
			}

			// Release resources and return results.
			std::string cl_font_file_path((char*)fc_font_file_path);
			FcPatternDestroy(fc_match);
			FcPatternDestroy(fc_pattern);
			return cl_font_file_path;
		}
		catch (...)
		{
			// If any exceptions thrown, ensure fontconfig resources are released.
			if (fc_match) FcPatternDestroy(fc_match);
			if (fc_pattern) FcPatternDestroy(fc_pattern);
			throw;
		}
	}
Example #4
0
void Font_Impl::load_font( GraphicContext &context, const FontDescription &desc, const std::string &filename )
{
	free_font();

	if (desc.get_subpixel())
	{
		glyph_cache.enable_subpixel = true;
		glyph_cache.anti_alias = true;	// Implies anti_alias is set
	}
	else
	{
		glyph_cache.enable_subpixel = false;
		glyph_cache.anti_alias = desc.get_anti_alias();
	}

#ifdef WIN32
	std::string path = PathHelp::get_fullpath(filename, PathHelp::path_type_file);
	std::string new_filename = PathHelp::get_filename(filename, PathHelp::path_type_file);
	FileSystem vfs(path);
	font_engine = new FontEngine_Win32(desc, new_filename, vfs);
	glyph_cache.font_metrics = font_engine->get_metrics();
#elif defined(__APPLE__)
    
    font_engine = new FontEngine_Cocoa(desc, filename);
    glyph_cache.font_metrics = font_engine->get_metrics();

#else
	std::string font_file_path = filename;
	if (font_file_path.empty())
	{
	    // Obtain the best matching font file from fontconfig.
		FontConfig &fc = FontConfig::instance();
		font_file_path = fc.match_font(desc);
	}

	std::string path = PathHelp::get_fullpath(font_file_path, PathHelp::path_type_file);
	std::string new_filename = PathHelp::get_filename(font_file_path, PathHelp::path_type_file);
	FileSystem vfs(path);
	IODevice io_dev = vfs.open_file(new_filename);

	int average_width = desc.get_average_width();
	int height = desc.get_height();

	// Ensure width and height are positive
	if (average_width < 0) average_width =-average_width;
	if (height < 0) height =-height;

	font_engine = new FontEngine_Freetype(io_dev, average_width, height);

	glyph_cache.font_metrics = font_engine->get_metrics();

#endif
}
Example #5
0
void Font_Impl::load_font( GraphicContext &context, const FontDescription &desc, const std::string &filename, FileSystem &fs)
{
	free_font();

	if (desc.get_subpixel())
	{
		glyph_cache.enable_subpixel = true;
		glyph_cache.anti_alias = true;	// Implies anti_alias is set
	}
	else
	{
		glyph_cache.enable_subpixel = false;
		glyph_cache.anti_alias = desc.get_anti_alias();
	}

#ifdef WIN32
	font_engine = new FontEngine_Win32(desc, filename, fs);
	glyph_cache.font_metrics = font_engine->get_metrics();
    
#elif defined(__APPLE__)
    
    font_engine = new FontEngine_Cocoa(desc, filename);
    glyph_cache.font_metrics = font_engine->get_metrics();

#else
	IODevice io_dev = fs.open_file(filename);

	int average_width = desc.get_average_width();
	int height = desc.get_height();

	// Ensure width and height are positive
	if (average_width < 0) average_width =-average_width;
	if (height < 0) height =-height;

	font_engine = new FontEngine_Freetype(io_dev, average_width, height);

	glyph_cache.font_metrics = font_engine->get_metrics();

#endif
}