Selaa lähdekoodia

Scale pixels-per-inch (PPI) units based on the context's dp-ratio (#468)

Placeholder solution to fix PPI for high-DPI screens. 

Co-authored-by: Michael Ragazzon <[email protected]>
Maximilian Stark 2 vuotta sitten
vanhempi
sitoutus
f09cc28445

+ 1 - 1
Include/RmlUi/Core/Unit.h

@@ -82,7 +82,7 @@ enum class Unit {
 	LENGTH = PX | DP | VW | VH | EM | REM | PPI_UNIT,
 	LENGTH_PERCENT = LENGTH | PERCENT,
 	NUMBER_LENGTH_PERCENT = NUMBER | LENGTH | PERCENT,
-	ABSOLUTE_LENGTH = PX | PPI_UNIT,
+	DP_SCALABLE_LENGTH = DP | PPI_UNIT,
 	ANGLE = DEG | RAD,
 	NUMERIC = NUMBER_LENGTH_PERCENT | ANGLE | X
 };

+ 16 - 20
Source/Core/ComputeProperty.cpp

@@ -37,26 +37,21 @@ const Style::ComputedValues DefaultComputedValues{nullptr};
 
 static constexpr float PixelsPerInch = 96.0f;
 
-float ComputeAbsoluteLength(NumericValue value)
+static float ComputePPILength(NumericValue value, float dp_ratio)
 {
-	if (value.unit == Unit::PX)
-	{
-		return value.number;
-	}
-	else if (Any(value.unit & Unit::PPI_UNIT))
-	{
-		// Values based on pixels-per-inch.
-		const float inch = value.number * PixelsPerInch;
+	RMLUI_ASSERT(Any(value.unit & Unit::PPI_UNIT));
 
-		switch (value.unit)
-		{
-		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;
-		}
+	// Values based on pixels-per-inch. Scaled by the dp-ratio as a placeholder solution until we make the pixel unit itself scalable.
+	const float inch = value.number * PixelsPerInch * dp_ratio;
+
+	switch (value.unit)
+	{
+	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;
 	}
 
 	RMLUI_ERROR;
@@ -65,11 +60,12 @@ float ComputeAbsoluteLength(NumericValue value)
 
 float ComputeLength(NumericValue value, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
 {
-	if (Any(value.unit & Unit::ABSOLUTE_LENGTH))
-		return ComputeAbsoluteLength(value);
+	if (Any(value.unit & Unit::PPI_UNIT))
+		return ComputePPILength(value, dp_ratio);
 
 	switch (value.unit)
 	{
+	case Unit::PX: return value.number;
 	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;

+ 0 - 2
Source/Core/ComputeProperty.h

@@ -39,8 +39,6 @@ class Property;
 // Note that numbers and percentages are not lengths, they have to be resolved elsewhere.
 float ComputeLength(NumericValue value, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions);
 
-float ComputeAbsoluteLength(NumericValue value);
-
 float ComputeAngle(NumericValue value);
 
 float ComputeFontsize(NumericValue value, const Style::ComputedValues& values, const Style::ComputedValues* parent_values,

+ 13 - 13
Source/Core/Element.cpp

@@ -576,18 +576,18 @@ bool Element::SetProperty(PropertyId id, const Property& property)
 void Element::RemoveProperty(const String& name)
 {
 	auto property_id = StyleSheetSpecification::GetPropertyId(name);
-    if (property_id != PropertyId::Invalid)
-        meta->style.RemoveProperty(property_id);
-    else
-    {
-        auto shorthand_id = StyleSheetSpecification::GetShorthandId(name);
-        if (shorthand_id != ShorthandId::Invalid)
-        {
-            auto property_id_set = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand_id);
-            for (auto it = property_id_set.begin(); it != property_id_set.end(); ++it)
-                meta->style.RemoveProperty(*it);
-        }
-    }
+	if (property_id != PropertyId::Invalid)
+		meta->style.RemoveProperty(property_id);
+	else
+	{
+		auto shorthand_id = StyleSheetSpecification::GetShorthandId(name);
+		if (shorthand_id != ShorthandId::Invalid)
+		{
+			auto property_id_set = StyleSheetSpecification::GetShorthandUnderlyingProperties(shorthand_id);
+			for (auto it = property_id_set.begin(); it != property_id_set.end(); ++it)
+				meta->style.RemoveProperty(*it);
+		}
+	}
 }
 
 void Element::RemoveProperty(PropertyId id)
@@ -2822,7 +2822,7 @@ void Element::OnStyleSheetChangeRecursive()
 void Element::OnDpRatioChangeRecursive()
 {
 	GetElementDecoration()->DirtyDecorators();
-	GetStyle()->DirtyPropertiesWithUnits(Unit::DP);
+	GetStyle()->DirtyPropertiesWithUnits(Unit::DP_SCALABLE_LENGTH);
 
 	OnDpRatioChange();
 

+ 8 - 6
Source/Core/ElementStyle.cpp

@@ -352,6 +352,12 @@ static float ComputeLength(NumericValue value, Element* element)
 	float dp_ratio = 1.0f;
 	Vector2f vp_dimensions(1.0f);
 
+	if (Any(value.unit & Unit::DP_SCALABLE_LENGTH))
+	{
+		if (Context* context = element->GetContext())
+			dp_ratio = context->GetDensityIndependentPixelRatio();
+	}
+
 	switch (value.unit)
 	{
 	case Unit::EM: font_size = element->GetComputedValues().font_size(); break;
@@ -361,10 +367,6 @@ static float ComputeLength(NumericValue value, Element* element)
 		else
 			doc_font_size = DefaultComputedValues.font_size();
 		break;
-	case Unit::DP:
-		if (Context* context = element->GetContext())
-			dp_ratio = context->GetDensityIndependentPixelRatio();
-		break;
 	case Unit::VW:
 	case Unit::VH:
 		if (Context* context = element->GetContext())
@@ -379,8 +381,8 @@ static float ComputeLength(NumericValue value, Element* element)
 
 float ElementStyle::ResolveNumericValue(NumericValue value, float base_value) const
 {
-	if (Any(value.unit & Unit::ABSOLUTE_LENGTH))
-		return ComputeAbsoluteLength(value);
+	if (value.unit == Unit::PX)
+		return value.number;
 	else if (Any(value.unit & Unit::LENGTH))
 		return ComputeLength(value, element);