|
|
@@ -37,141 +37,110 @@ const Style::ComputedValues DefaultComputedValues{nullptr};
|
|
|
|
|
|
static constexpr float PixelsPerInch = 96.0f;
|
|
|
|
|
|
-float ComputeLength(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
|
|
|
+float ComputeAbsoluteLength(NumericValue value)
|
|
|
{
|
|
|
- RMLUI_ASSERT(property);
|
|
|
-
|
|
|
- float value = property->value.Get<float>();
|
|
|
-
|
|
|
- switch (property->unit)
|
|
|
+ if (value.unit == Unit::PX)
|
|
|
{
|
|
|
- case Property::NUMBER:
|
|
|
- case Property::PX:
|
|
|
- case Property::RAD: return value;
|
|
|
-
|
|
|
- case Property::EM: return value * font_size;
|
|
|
- case Property::REM: return value * document_font_size;
|
|
|
- case Property::DP: return value * dp_ratio;
|
|
|
- case Property::VW: return value * vp_dimensions.x * 0.01f;
|
|
|
- case Property::VH: return value * vp_dimensions.y * 0.01f;
|
|
|
-
|
|
|
- case Property::DEG: return Math::DegreesToRadians(value);
|
|
|
- default: break;
|
|
|
+ return value.number;
|
|
|
}
|
|
|
-
|
|
|
- // Values based on pixels-per-inch.
|
|
|
- if (property->unit & Property::PPI_UNIT)
|
|
|
+ else if (Any(value.unit & Unit::PPI_UNIT))
|
|
|
{
|
|
|
- float inch = value * PixelsPerInch;
|
|
|
+ // Values based on pixels-per-inch.
|
|
|
+ const float inch = value.number * PixelsPerInch;
|
|
|
|
|
|
- switch (property->unit)
|
|
|
+ switch (value.unit)
|
|
|
{
|
|
|
- case Property::INCH: return inch; // inch
|
|
|
- case Property::CM: return inch * (1.0f / 2.54f); // centimeter
|
|
|
- case Property::MM: return inch * (1.0f / 25.4f); // millimeter
|
|
|
- case Property::PT: return inch * (1.0f / 72.0f); // point
|
|
|
- case Property::PC: return inch * (1.0f / 6.0f); // pica
|
|
|
+ case Unit::INCH: return inch;
|
|
|
+ case Unit::CM: return inch * (1.0f / 2.54f);
|
|
|
+ case Unit::MM: return inch * (1.0f / 25.4f);
|
|
|
+ case Unit::PT: return inch * (1.0f / 72.0f);
|
|
|
+ case Unit::PC: return inch * (1.0f / 6.0f);
|
|
|
default: break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // We're not a numeric property; return 0.
|
|
|
- return 0.0f;
|
|
|
+ RMLUI_ERROR;
|
|
|
+ return 0.f;
|
|
|
}
|
|
|
|
|
|
-float ComputeAbsoluteLength(const Property& property, float dp_ratio, Vector2f vp_dimensions)
|
|
|
+float ComputeLength(NumericValue value, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
|
|
|
{
|
|
|
- RMLUI_ASSERT(property.unit & Property::ABSOLUTE_LENGTH);
|
|
|
+ if (Any(value.unit & Unit::ABSOLUTE_LENGTH))
|
|
|
+ return ComputeAbsoluteLength(value);
|
|
|
|
|
|
- switch (property.unit)
|
|
|
+ switch (value.unit)
|
|
|
{
|
|
|
- case Property::PX: return property.value.Get<float>();
|
|
|
- case Property::DP: return property.value.Get<float>() * dp_ratio;
|
|
|
- case Property::VW: return property.value.Get<float>() * vp_dimensions.x * 0.01f;
|
|
|
- case Property::VH: return property.value.Get<float>() * vp_dimensions.y * 0.01f;
|
|
|
- default:
|
|
|
- // Values based on pixels-per-inch.
|
|
|
- if (property.unit & Property::PPI_UNIT)
|
|
|
- {
|
|
|
- float inch = property.value.Get<float>() * PixelsPerInch;
|
|
|
-
|
|
|
- switch (property.unit)
|
|
|
- {
|
|
|
- case Property::INCH: return inch; // inch
|
|
|
- case Property::CM: return inch * (1.0f / 2.54f); // centimeter
|
|
|
- case Property::MM: return inch * (1.0f / 25.4f); // millimeter
|
|
|
- case Property::PT: return inch * (1.0f / 72.0f); // point
|
|
|
- case Property::PC: return inch * (1.0f / 6.0f); // pica
|
|
|
- default: break;
|
|
|
- }
|
|
|
- }
|
|
|
+ case Unit::EM: return value.number * font_size;
|
|
|
+ case Unit::REM: return value.number * document_font_size;
|
|
|
+ case Unit::DP: return value.number * dp_ratio;
|
|
|
+ case Unit::VW: return value.number * vp_dimensions.x * 0.01f;
|
|
|
+ case Unit::VH: return value.number * vp_dimensions.y * 0.01f;
|
|
|
+ default: break;
|
|
|
}
|
|
|
|
|
|
RMLUI_ERROR;
|
|
|
return 0.0f;
|
|
|
}
|
|
|
|
|
|
-float ComputeAngle(const Property& property)
|
|
|
+float ComputeAngle(NumericValue value)
|
|
|
{
|
|
|
- float value = property.value.Get<float>();
|
|
|
-
|
|
|
- switch (property.unit)
|
|
|
+ switch (value.unit)
|
|
|
{
|
|
|
- case Property::NUMBER:
|
|
|
- case Property::RAD: return value;
|
|
|
+ case Unit::NUMBER:
|
|
|
+ case Unit::RAD: return value.number;
|
|
|
|
|
|
- case Property::DEG: return Math::DegreesToRadians(value);
|
|
|
+ case Unit::DEG: return Math::DegreesToRadians(value.number);
|
|
|
default: break;
|
|
|
}
|
|
|
|
|
|
+ RMLUI_ERROR;
|
|
|
return 0.0f;
|
|
|
}
|
|
|
|
|
|
-String ComputeFontFamily(String font_family)
|
|
|
-{
|
|
|
- return StringUtilities::ToLower(std::move(font_family));
|
|
|
-}
|
|
|
-
|
|
|
-float ComputeFontsize(const Property& property, const Style::ComputedValues& values, const Style::ComputedValues* parent_values,
|
|
|
+float ComputeFontsize(NumericValue value, const Style::ComputedValues& values, const Style::ComputedValues* parent_values,
|
|
|
const Style::ComputedValues* document_values, float dp_ratio, Vector2f vp_dimensions)
|
|
|
{
|
|
|
- // 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 (property.unit & Property::RELATIVE_UNIT)
|
|
|
+ if (Any(value.unit & (Unit::PERCENT | Unit::EM | Unit::REM)))
|
|
|
{
|
|
|
+ // Relative values are based on the parent's or document's font size instead of our own.
|
|
|
float multiplier = 1.0f;
|
|
|
|
|
|
- switch (property.unit)
|
|
|
+ switch (value.unit)
|
|
|
{
|
|
|
- case Property::PERCENT:
|
|
|
+ case Unit::PERCENT:
|
|
|
multiplier = 0.01f;
|
|
|
//-fallthrough
|
|
|
- case Property::EM:
|
|
|
+ case Unit::EM:
|
|
|
if (!parent_values)
|
|
|
return 0;
|
|
|
- return property.value.Get<float>() * multiplier * parent_values->font_size();
|
|
|
+ return value.number * multiplier * parent_values->font_size();
|
|
|
|
|
|
- case Property::REM:
|
|
|
- if (!document_values)
|
|
|
- return 0;
|
|
|
- // If the current element is a document, the rem unit is relative to the default size
|
|
|
- if (&values == document_values)
|
|
|
- return property.value.Get<float>() * DefaultComputedValues.font_size();
|
|
|
- // Otherwise it is relative to the document font size
|
|
|
- return property.value.Get<float>() * document_values->font_size();
|
|
|
- default: RMLUI_ERRORMSG("A relative unit must be percentage, em or rem.");
|
|
|
+ case Unit::REM:
|
|
|
+ // If the current element is a document, the rem unit is relative to the default size.
|
|
|
+ if (!document_values || &values == document_values)
|
|
|
+ return value.number * DefaultComputedValues.font_size();
|
|
|
+
|
|
|
+ // Otherwise it is relative to the document font size.
|
|
|
+ return value.number * document_values->font_size();
|
|
|
+ default: break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return ComputeAbsoluteLength(property, dp_ratio, vp_dimensions);
|
|
|
+ // Font-relative lengths handled above, other lengths should be handled as normal.
|
|
|
+ return ComputeLength(value, 0.f, 0.f, dp_ratio, vp_dimensions);
|
|
|
+}
|
|
|
+
|
|
|
+String ComputeFontFamily(String font_family)
|
|
|
+{
|
|
|
+ return StringUtilities::ToLower(std::move(font_family));
|
|
|
}
|
|
|
|
|
|
Style::Clip ComputeClip(const Property* property)
|
|
|
{
|
|
|
const int value = property->Get<int>();
|
|
|
- if (property->unit == Property::KEYWORD)
|
|
|
+ if (property->unit == Unit::KEYWORD)
|
|
|
return Style::Clip(static_cast<Style::Clip::Type>(value));
|
|
|
- else if (property->unit == Property::NUMBER)
|
|
|
+ else if (property->unit == Unit::NUMBER)
|
|
|
return Style::Clip(Style::Clip::Type::Number, static_cast<int8_t>(value));
|
|
|
RMLUI_ERRORMSG("Invalid clip type");
|
|
|
return Style::Clip();
|
|
|
@@ -179,9 +148,9 @@ Style::Clip ComputeClip(const Property* property)
|
|
|
|
|
|
Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
|
|
|
{
|
|
|
- if (property->unit & Property::LENGTH)
|
|
|
+ if (Any(property->unit & Unit::LENGTH))
|
|
|
{
|
|
|
- float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
|
|
|
+ float value = ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions);
|
|
|
return Style::LineHeight(value, Style::LineHeight::Length, value);
|
|
|
}
|
|
|
|
|
|
@@ -189,8 +158,8 @@ Style::LineHeight ComputeLineHeight(const Property* property, float font_size, f
|
|
|
|
|
|
switch (property->unit)
|
|
|
{
|
|
|
- case Property::NUMBER: scale_factor = property->value.Get<float>(); break;
|
|
|
- case Property::PERCENT: scale_factor = property->value.Get<float>() * 0.01f; break;
|
|
|
+ case Unit::NUMBER: scale_factor = property->value.Get<float>(); break;
|
|
|
+ case Unit::PERCENT: scale_factor = property->value.Get<float>() * 0.01f; break;
|
|
|
default: RMLUI_ERRORMSG("Invalid unit for line-height");
|
|
|
}
|
|
|
|
|
|
@@ -201,17 +170,17 @@ Style::LineHeight ComputeLineHeight(const Property* property, float font_size, f
|
|
|
Style::VerticalAlign ComputeVerticalAlign(const Property* property, float line_height, float font_size, float document_font_size, float dp_ratio,
|
|
|
Vector2f vp_dimensions)
|
|
|
{
|
|
|
- if (property->unit & Property::LENGTH)
|
|
|
+ if (Any(property->unit & Unit::LENGTH))
|
|
|
{
|
|
|
- float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
|
|
|
+ float value = ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions);
|
|
|
return Style::VerticalAlign(value);
|
|
|
}
|
|
|
- else if (property->unit & Property::PERCENT)
|
|
|
+ else if (property->unit == Unit::PERCENT)
|
|
|
{
|
|
|
return Style::VerticalAlign(property->Get<float>() * line_height * 0.01f);
|
|
|
}
|
|
|
|
|
|
- RMLUI_ASSERT(property->unit & Property::KEYWORD);
|
|
|
+ RMLUI_ASSERT(property->unit == Unit::KEYWORD);
|
|
|
return Style::VerticalAlign((Style::VerticalAlign::Type)property->Get<int>());
|
|
|
}
|
|
|
|
|
|
@@ -219,23 +188,24 @@ Style::LengthPercentage ComputeLengthPercentage(const Property* property, float
|
|
|
Vector2f vp_dimensions)
|
|
|
{
|
|
|
using namespace Style;
|
|
|
- if (property->unit & Property::PERCENT)
|
|
|
+ if (property->unit == Unit::PERCENT)
|
|
|
return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
|
|
|
|
|
|
- return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
|
|
|
+ return LengthPercentage(LengthPercentage::Length,
|
|
|
+ ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions));
|
|
|
}
|
|
|
|
|
|
Style::LengthPercentageAuto ComputeLengthPercentageAuto(const Property* property, float font_size, float document_font_size, float dp_ratio,
|
|
|
Vector2f vp_dimensions)
|
|
|
{
|
|
|
using namespace Style;
|
|
|
- // Assuming here that 'auto' is the only possible keyword
|
|
|
- if (property->unit & Property::PERCENT)
|
|
|
+ if (property->unit == Unit::PERCENT)
|
|
|
return LengthPercentageAuto(LengthPercentageAuto::Percentage, property->Get<float>());
|
|
|
- else if (property->unit & Property::KEYWORD)
|
|
|
+ else if (property->unit == Unit::KEYWORD)
|
|
|
return LengthPercentageAuto(LengthPercentageAuto::Auto);
|
|
|
|
|
|
- return LengthPercentageAuto(LengthPercentageAuto::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
|
|
|
+ return LengthPercentageAuto(LengthPercentageAuto::Length,
|
|
|
+ ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions));
|
|
|
}
|
|
|
|
|
|
Style::LengthPercentage ComputeOrigin(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
|
|
|
@@ -244,7 +214,7 @@ Style::LengthPercentage ComputeOrigin(const Property* property, float font_size,
|
|
|
static_assert(
|
|
|
(int)OriginX::Left == (int)OriginY::Top && (int)OriginX::Center == (int)OriginY::Center && (int)OriginX::Right == (int)OriginY::Bottom, "");
|
|
|
|
|
|
- if (property->unit & Property::KEYWORD)
|
|
|
+ if (property->unit == Unit::KEYWORD)
|
|
|
{
|
|
|
float percent = 0.0f;
|
|
|
OriginX origin = (OriginX)property->Get<int>();
|
|
|
@@ -256,21 +226,22 @@ Style::LengthPercentage ComputeOrigin(const Property* property, float font_size,
|
|
|
}
|
|
|
return LengthPercentage(LengthPercentage::Percentage, percent);
|
|
|
}
|
|
|
- else if (property->unit & Property::PERCENT)
|
|
|
+ else if (property->unit == Unit::PERCENT)
|
|
|
return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
|
|
|
|
|
|
- return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
|
|
|
+ return LengthPercentage(LengthPercentage::Length,
|
|
|
+ ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions));
|
|
|
}
|
|
|
|
|
|
Style::LengthPercentage ComputeMaxSize(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
|
|
|
{
|
|
|
using namespace Style;
|
|
|
- if (property->unit & Property::KEYWORD)
|
|
|
+ if (Any(property->unit & Unit::KEYWORD))
|
|
|
return LengthPercentage(LengthPercentage::Length, FLT_MAX);
|
|
|
- else if (property->unit & Property::PERCENT)
|
|
|
+ else if (Any(property->unit & Unit::PERCENT))
|
|
|
return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
|
|
|
|
|
|
- const float length = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
|
|
|
+ const float length = ComputeLength(property->GetNumericValue(), font_size, document_font_size, dp_ratio, vp_dimensions);
|
|
|
return LengthPercentage(LengthPercentage::Length, length < 0.f ? FLT_MAX : length);
|
|
|
}
|
|
|
|