Browse Source

Added "in", "cm", "mm", "pt" and "pc" units.

New units are based on a new RenderInterface::GetPixelsPerInch() method.
Arkadiusz Wahlig 13 years ago
parent
commit
fc0168ce1d

+ 9 - 1
Include/Rocket/Core/Property.h

@@ -60,7 +60,15 @@ public:
 		// Relative values.
 		// Relative values.
 		EM = 1 << 6,				// number suffixed by 'em'; fetch as < float >
 		EM = 1 << 6,				// number suffixed by 'em'; fetch as < float >
 		PERCENT = 1 << 7,			// number suffixed by '%'; fetch as < float >
 		PERCENT = 1 << 7,			// number suffixed by '%'; fetch as < float >
-		RELATIVE_UNIT = EM | PERCENT
+		RELATIVE_UNIT = EM | PERCENT,
+
+		// Values based on pixels-per-inch.
+		IN = 1 << 8,				// number suffixed by 'in'; fetch as < float >
+		CM = 1 << 9,				// number suffixed by 'cm'; fetch as < float >
+		MM = 1 << 10,				// number suffixed by 'mm'; fetch as < float >
+		PT = 1 << 11,				// number suffixed by 'pt'; fetch as < float >
+		PC = 1 << 12,				// number suffixed by 'pc'; fetch as < float >
+		PPI_UNIT = IN | CM | MM | PT | PC
 	};
 	};
 
 
 	Property();
 	Property();

+ 4 - 0
Include/Rocket/Core/RenderInterface.h

@@ -112,6 +112,10 @@ public:
 	/// @return The renderer's vertical texel offset. The default implementation returns 0.
 	/// @return The renderer's vertical texel offset. The default implementation returns 0.
 	virtual float GetVerticalTexelOffset();
 	virtual float GetVerticalTexelOffset();
 
 
+	/// Returns the number of pixels per inch.
+	/// @returns The number of pixels per inch. The default implementation returns 100.
+	virtual float GetPixelsPerInch();
+
 	/// Called when this render interface is released.
 	/// Called when this render interface is released.
 	virtual void Release();
 	virtual void Release();
 
 

+ 17 - 0
Source/Core/ElementStyle.cpp

@@ -368,6 +368,23 @@ float ElementStyle::ResolveProperty(const String& name, float base_value)
 		return property->value.Get< float >();
 		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::IN) // 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.
 	// We're not a numeric property; return 0.
 	return 0.0f;
 	return 0.0f;
 }
 }

+ 5 - 0
Source/Core/PropertyParserNumber.cpp

@@ -35,6 +35,11 @@ PropertyParserNumber::PropertyParserNumber()
 {
 {
 	unit_suffixes.push_back(UnitSuffix(Property::PX, "px"));
 	unit_suffixes.push_back(UnitSuffix(Property::PX, "px"));
 	unit_suffixes.push_back(UnitSuffix(Property::EM, "em"));
 	unit_suffixes.push_back(UnitSuffix(Property::EM, "em"));
+	unit_suffixes.push_back(UnitSuffix(Property::IN, "in"));
+	unit_suffixes.push_back(UnitSuffix(Property::CM, "cm"));
+	unit_suffixes.push_back(UnitSuffix(Property::MM, "mm"));
+	unit_suffixes.push_back(UnitSuffix(Property::PT, "pt"));
+	unit_suffixes.push_back(UnitSuffix(Property::PC, "pc"));
 	unit_suffixes.push_back(UnitSuffix(Property::PERCENT, "%"));
 	unit_suffixes.push_back(UnitSuffix(Property::PERCENT, "%"));
 }
 }
 
 

+ 6 - 0
Source/Core/RenderInterface.cpp

@@ -86,6 +86,12 @@ float RenderInterface::GetVerticalTexelOffset()
 	return 0;
 	return 0;
 }
 }
 
 
+// Returns the number of pixels per inch.
+float RenderInterface::GetPixelsPerInch()
+{
+	return 100;
+}
+
 // Called when this render interface is released.
 // Called when this render interface is released.
 void RenderInterface::Release()
 void RenderInterface::Release()
 {
 {