void CL_FontProvider_Freetype::load_font(const CL_StringRef &resource_id, CL_ResourceManager *resources)
{
	CL_Resource resource = resources->get_resource(resource_id);
	if (resource.get_type() != "font")
		throw CL_Exception(cl_format("Resource '%1' is not of type 'font'", resource.get_name()));

	CL_DomElement freetype_element = resource.get_element().named_item("freetype").to_element();
	if (freetype_element.is_null())
		throw CL_Exception(cl_format("Font resource '%1' has no 'freetype' child element", resource.get_name()));

	CL_FontDescription desc;

	if (freetype_element.has_attribute("file"))
		desc.set_typeface_name(freetype_element.get_attribute("file"));
	else
		throw CL_Exception(cl_format("Font resource '%1' has no 'file' attribute", resource.get_name()));

	if (freetype_element.has_attribute("height"))
		desc.set_height(freetype_element.get_attribute_int("height", 0));
	else
		throw CL_Exception(cl_format("Font resource '%1' has no 'height' attribute", resource.get_name()));

	if (freetype_element.has_attribute("average_width"))
		desc.set_average_width(freetype_element.get_attribute_int("average_width", 0));

	if (freetype_element.has_attribute("anti_alias"))
		desc.set_anti_alias(freetype_element.get_attribute_bool("anti_alias", true));

	if (freetype_element.has_attribute("subpixel"))
		desc.set_subpixel(freetype_element.get_attribute_bool("subpixel", true));

	load_font(desc, resources->get_directory(resource));
}
Exemple #2
0
void
CLLoader::addFont(string const &path, int size, Resources *resources, string const &alias) {
  int realSize = (4 * size) / 3;
  CL_FontDescription desc;
  desc.set_typeface_name(path);
  desc.set_height(realSize);
  desc.set_anti_alias(true);
  desc.set_subpixel(false);
  CL_Font_Freetype *font;
  try {
    font = new CL_Font_Freetype(desc);
  } catch (CL_Exception e) {
    cerr << "Error building CL_Font: " << e.what() << endl;
    return;
  }
    
  resources->setFont(path, new CLResFont(font));
  if (alias.length() > 0) {
    resources->setFont(alias, new CLResFont(font));
  }
}