Example #1
0
	void FontFamily_Impl::add(const FontDescription &desc, const std::string &typeface_name)
	{
		FontFamily_Definition definition;
		definition.desc = desc.clone();
		definition.typeface_name = typeface_name;
		font_definitions.push_back(definition);
	}
Example #2
0
	void FontFamily_Impl::add(const FontDescription &desc, DataBuffer &font_databuffer)
	{
		FontFamily_Definition definition;
		definition.desc = desc.clone();
		definition.font_databuffer = font_databuffer;
		font_definitions.push_back(definition);
	}
Example #3
0
	Font_Impl::Font_Impl(FontFamily &new_font_family, const FontDescription &description)
	{
		new_font_family.throw_if_null();

		font_family = new_font_family;

		selected_description = description.clone();
		selected_line_height = description.get_line_height();
	}
Example #4
0
	Font Font::load(Canvas &canvas, const std::string &family_name, const FontDescription &reference_desc, FontFamily &font_family, const XMLResourceDocument &doc, std::function<Resource<Sprite>(Canvas &, const std::string &)> cb_get_sprite)
	{
		DomElement font_element;
		XMLResourceNode resource;

		resource = doc.get_resource(family_name);
		font_element = resource.get_element();

		DomElement sprite_element = font_element.named_item("sprite").to_element();

		if (!sprite_element.is_null())
		{
			if (!sprite_element.has_attribute("glyphs"))
				throw Exception(string_format("Font resource %1 has no 'glyphs' attribute.", resource.get_name()));

			if (!sprite_element.has_attribute("letters"))
				throw Exception(string_format("Font resource %1 has no 'letters' attribute.", resource.get_name()));

			if (!cb_get_sprite)
				throw Exception(string_format("Font resource %1 requires a sprite loader callback specified.", resource.get_name()));
				
			Resource<Sprite> spr_glyphs = cb_get_sprite(canvas, sprite_element.get_attribute("glyphs"));

			const std::string &letters = sprite_element.get_attribute("letters");

			int spacelen = StringHelp::text_to_int(sprite_element.get_attribute("spacelen", "-1"));
			bool monospace = StringHelp::text_to_bool(sprite_element.get_attribute("monospace", "false"));

			// Modify the default font metrics, if specified

			float height = 0.0f;
			float line_height = 0.0f;
			float ascent = 0.0f;
			float descent = 0.0f;
			float internal_leading = 0.0f;
			float external_leading = 0.0f;

			if (sprite_element.has_attribute("height"))
				height = StringHelp::text_to_float(sprite_element.get_attribute("height", "0"));

			if (sprite_element.has_attribute("line_height"))
				line_height = StringHelp::text_to_float(sprite_element.get_attribute("line_height", "0"));

			if (sprite_element.has_attribute("ascent"))
				ascent = StringHelp::text_to_float(sprite_element.get_attribute("ascent", "0"));

			if (sprite_element.has_attribute("descent"))
				descent = StringHelp::text_to_float(sprite_element.get_attribute("descent", "0"));

			if (sprite_element.has_attribute("internal_leading"))
				internal_leading = StringHelp::text_to_float(sprite_element.get_attribute("internal_leading", "0"));

			if (sprite_element.has_attribute("external_leading"))
				external_leading = StringHelp::text_to_float(sprite_element.get_attribute("external_leading", "0"));

			FontMetrics font_metrics(height, ascent, descent, internal_leading, external_leading, line_height, canvas.get_pixel_ratio());

			font_family.add(canvas, spr_glyphs.get(), letters, spacelen, monospace, font_metrics);

			FontDescription desc = reference_desc.clone();
			return Font(font_family, desc);
		}

		DomElement ttf_element = font_element.named_item("ttf").to_element();
		if (ttf_element.is_null())
			ttf_element = font_element.named_item("freetype").to_element();

		if (!ttf_element.is_null())
		{
			FontDescription desc = reference_desc.clone();

			std::string filename;
			if (ttf_element.has_attribute("file"))
			{
				filename = PathHelp::combine(resource.get_base_path(), ttf_element.get_attribute("file"));
			}

			if (!ttf_element.has_attribute("typeface"))
				throw Exception(string_format("Font resource %1 has no 'typeface' attribute.", resource.get_name()));

			std::string font_typeface_name = ttf_element.get_attribute("typeface");

			if (ttf_element.has_attribute("height"))
				desc.set_height(ttf_element.get_attribute_int("height", 0));

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

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

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

			if (filename.empty())
			{
				font_family.add(font_typeface_name, desc);
				return Font(font_family, desc);
			}
			else
			{
				font_family.add(desc, filename, resource.get_file_system());
				return Font(font_family, desc);
			}
		}

		throw Exception(string_format("Font resource %1 did not have a <sprite> or <ttf> child element", resource.get_name()));
	}
Example #5
0
		FontEngine_Sprite(const FontDescription &desc, FontMetrics &metrics) : font_metrics(metrics) { font_description = desc.clone(); }
Example #6
0
Font Font_Impl::load(Canvas &canvas, const FontDescription &reference_desc, const std::string &id, const XMLResourceDocument &doc, Callback_2<Resource<Sprite>, Canvas &, const std::string &> cb_get_sprite)
{
	XMLResourceNode resource = doc.get_resource(id);
	std::string type = resource.get_element().get_tag_name();
	
	if (type != "font")
		throw Exception(string_format("Resource '%1' is not of type 'font'", id));

	DomElement sprite_element = resource.get_element().named_item("sprite").to_element();

	FontMetrics font_metrics;

	if (!sprite_element.is_null())
	{
		if (!sprite_element.has_attribute("glyphs")) 
			throw Exception(string_format("Font resource %1 has no 'glyphs' attribute.", resource.get_name()));
		
		if (!sprite_element.has_attribute("letters")) 
			throw Exception(string_format("Font resource %1 has no 'letters' attribute.", resource.get_name()));

		Resource<Sprite> spr_glyphs = cb_get_sprite.invoke(canvas, sprite_element.get_attribute("glyphs"));

		const std::string &letters = sprite_element.get_attribute("letters");

		int spacelen = StringHelp::text_to_int(sprite_element.get_attribute("spacelen", "-1"));
		bool monospace = StringHelp::text_to_bool(sprite_element.get_attribute("monospace", "false"));

		// Modify the default font metrics, if specified

		if (sprite_element.has_attribute("height")) 
			font_metrics.set_height(StringHelp::text_to_float(sprite_element.get_attribute("height", "0")));

		if (sprite_element.has_attribute("ascent")) 
			font_metrics.set_ascent(StringHelp::text_to_float(sprite_element.get_attribute("ascent", "0")));

		if (sprite_element.has_attribute("descent")) 
			font_metrics.set_descent(StringHelp::text_to_float(sprite_element.get_attribute("descent", "0")));

		if (sprite_element.has_attribute("internal_leading")) 
			font_metrics.set_internal_leading(StringHelp::text_to_float(sprite_element.get_attribute("internal_leading", "0")));

		if (sprite_element.has_attribute("external_leading")) 
			font_metrics.set_external_leading(StringHelp::text_to_float(sprite_element.get_attribute("external_leading", "0")));

		if (sprite_element.has_attribute("average_character_width")) 
			font_metrics.set_average_character_width(StringHelp::text_to_float(sprite_element.get_attribute("average_character_width", "0")));

		if (sprite_element.has_attribute("max_character_width")) 
			font_metrics.set_max_character_width(StringHelp::text_to_float(sprite_element.get_attribute("max_character_width", "0")));

		font_metrics.set_weight(400.0f);
		if (sprite_element.has_attribute("weight")) 
			font_metrics.set_weight(StringHelp::text_to_float(sprite_element.get_attribute("weight", "0")));

		if (sprite_element.has_attribute("overhang")) 
			font_metrics.set_overhang(StringHelp::text_to_float(sprite_element.get_attribute("overhang", "0")));

		font_metrics.set_digitized_aspect_x(96.0f);
		if (sprite_element.has_attribute("digitized_aspect_x")) 
			font_metrics.set_digitized_aspect_x(StringHelp::text_to_float(sprite_element.get_attribute("digitized_aspect_x", "0")));

		font_metrics.set_digitized_aspect_y(96.0f);
		if (sprite_element.has_attribute("digitized_aspect_y")) 
			font_metrics.set_digitized_aspect_y(StringHelp::text_to_float(sprite_element.get_attribute("digitized_aspect_y", "0")));

		if (sprite_element.has_attribute("italic")) 
			font_metrics.set_italic(StringHelp::text_to_bool(sprite_element.get_attribute("italic", "0")));

		if (sprite_element.has_attribute("underlined")) 
			font_metrics.set_underlined(StringHelp::text_to_bool(sprite_element.get_attribute("underlined", "0")));

		if (sprite_element.has_attribute("struck_out")) 
			font_metrics.set_struck_out(StringHelp::text_to_bool(sprite_element.get_attribute("struck_out", "0")));

		if (sprite_element.has_attribute("fixed_pitch")) 
			font_metrics.set_fixed_pitch(StringHelp::text_to_bool(sprite_element.get_attribute("fixed_pitch", "0")));

		return Font(canvas, spr_glyphs.get(), letters, spacelen, monospace, font_metrics);
	}

	DomElement ttf_element = resource.get_element().named_item("ttf").to_element();
	if (ttf_element.is_null())
		ttf_element = resource.get_element().named_item("freetype").to_element();

	if (!ttf_element.is_null())
	{
		FontDescription desc = reference_desc.clone();

		std::string filename;

		if (ttf_element.has_attribute("file"))
			filename = ttf_element.get_attribute("file"); desc.set_typeface_name(ttf_element.get_attribute("file"));

		if (ttf_element.has_attribute("typeface"))
			desc.set_typeface_name(ttf_element.get_attribute("typeface"));

		if (ttf_element.has_attribute("height"))
			desc.set_height(ttf_element.get_attribute_int("height", 0));

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

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

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

		return Font(canvas, desc, filename);
	}

	throw Exception(string_format("Font resource %1 did not have a <sprite> or <ttf> child element", resource.get_name()));

}