コード例 #1
0
// Resolves one of this element's properties.
float ElementStyle::ResolveProperty(const String& name, float base_value)
{
	const Property* property = GetProperty(name);
	if (!property)
	{
		ROCKET_ERROR;
		return 0.0f;
	}

	if (property->unit & Property::RELATIVE_UNIT)
	{
		// The calculated value of the font-size property is inherited, so we need to check if this
		// is an inherited property. If so, then we return our parent's font size instead.
		if (name == FONT_SIZE)
		{
			Rocket::Core::Element* parent = element->GetParentNode();
			if (parent == NULL)
				return 0;

			if (GetLocalProperty(FONT_SIZE) == NULL)
				return parent->ResolveProperty(FONT_SIZE, 0);

			// The base value for font size is always the height of *this* element's parent's font.
			base_value = parent->ResolveProperty(FONT_SIZE, 0);
		}

		if (property->unit & Property::PERCENT)
			return base_value * property->value.Get< float >() * 0.01f;
		else if (property->unit & Property::EM)
		{
			// If an em-relative font size is specified, it is expressed relative to the parent's
			// font height.
			if (name == FONT_SIZE)
				return property->value.Get< float >() * base_value;
			else
				return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
		}
	}

	if (property->unit & Property::NUMBER || property->unit & Property::PX)
	{
		return property->value.Get< float >();
	}

	// We're not a numeric property; return 0.
	return 0.0f;
}
コード例 #2
0
ファイル: ElementStyle.cpp プロジェクト: Gambini/libRocket
// Resolves one of this element's properties.
float ElementStyle::ResolveProperty(const String& name, float base_value)
{
	const Property* property = GetProperty(name);
	if (!property)
	{
		ROCKET_ERROR;
		return 0.0f;
	}

	if (property->unit & Property::RELATIVE_UNIT)
	{
		// The calculated value of the font-size property is inherited, so we need to check if this
		// is an inherited property. If so, then we return our parent's font size instead.
		if (name == FONT_SIZE)
		{
			// If the rem unit is used, the font-size is inherited directly from the document,
			// otherwise we use the parent's font size.
			if (property->unit & Property::REM)
			{
				Rocket::Core::ElementDocument* owner_document = element->GetOwnerDocument();
				if (owner_document == NULL)
					return 0;

				base_value = element->GetOwnerDocument()->ResolveProperty(FONT_SIZE, 0);
			}
			else
			{
				Rocket::Core::Element* parent = element->GetParentNode();
				if (parent == NULL)
					return 0;

				if (GetLocalProperty(FONT_SIZE) == NULL)
					return parent->ResolveProperty(FONT_SIZE, 0);

				// The base value for font size is always the height of *this* element's parent's font.
				base_value = parent->ResolveProperty(FONT_SIZE, 0);
			}
		}

		if (property->unit & Property::PERCENT)
			return base_value * property->value.Get< float >() * 0.01f;
		else if (property->unit & Property::EM)
		{
			// If an em-relative font size is specified, it is expressed relative to the parent's
			// font height.
			if (name == FONT_SIZE)
				return property->value.Get< float >() * base_value;
			else
				return property->value.Get< float >() * ElementUtilities::GetFontSize(element);
		}
		else if (property->unit & Property::REM)
		{
			// If an rem-relative font size is specified, it is expressed relative to the document's
			// font height.
			if (name == FONT_SIZE)
				return property->value.Get< float >() * base_value;
			else
				return property->value.Get< float >() * ElementUtilities::GetFontSize(element->GetOwnerDocument());
		}
	}

	if (property->unit & Property::NUMBER || property->unit & Property::PX)
	{
		return property->value.Get< float >();
	}
    
    // Values based on pixels-per-inch.
	if (property->unit & Property::PPI_UNIT)
	{
		float inch = property->value.Get< float >() * element->GetRenderInterface()->GetPixelsPerInch();

		if (property->unit & Property::INCH) // inch
			return inch;
		if (property->unit & Property::CM) // centimeter
			return inch / 2.54f;
		if (property->unit & Property::MM) // millimeter
			return inch / 25.4f;
		if (property->unit & Property::PT) // point
			return inch / 72.0f;
		if (property->unit & Property::PC) // pica
			return inch / 6.0f;
	}

	// We're not a numeric property; return 0.
	return 0.0f;
}