| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * Copyright (c) 2019 The RmlUi Team, and contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "ComputeProperty.h"
- #include "../../Include/RmlUi/Core/ComputedValues.h"
- #include "../../Include/RmlUi/Core/Property.h"
- namespace Rml {
- 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)
- {
- RMLUI_ASSERT(property);
-
- float value = property->value.Get<float>();
- switch (property->unit)
- {
- 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;
- }
- // Values based on pixels-per-inch.
- if (property->unit & Property::PPI_UNIT)
- {
- float inch = value * PixelsPerInch;
- switch (property->unit)
- {
- case Property::INCH: // inch
- return inch;
- case Property::CM: // centimeter
- return inch * (1.0f / 2.54f);
- case Property::MM: // millimeter
- return inch * (1.0f / 25.4f);
- case Property::PT: // point
- return inch * (1.0f / 72.0f);
- case Property::PC: // pica
- return inch * (1.0f / 6.0f);
- default:
- break;
- }
- }
- // We're not a numeric property; return 0.
- return 0.0f;
- }
- float ComputeAbsoluteLength(const Property& property, float dp_ratio, Vector2f vp_dimensions)
- {
- RMLUI_ASSERT(property.unit & Property::ABSOLUTE_LENGTH);
- switch (property.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: // inch
- return inch;
- case Property::CM: // centimeter
- return inch * (1.0f / 2.54f);
- case Property::MM: // millimeter
- return inch * (1.0f / 25.4f);
- case Property::PT: // point
- return inch * (1.0f / 72.0f);
- case Property::PC: // pica
- return inch * (1.0f / 6.0f);
- default:
- break;
- }
- }
- }
- RMLUI_ERROR;
- return 0.0f;
- }
- float ComputeAngle(const Property& property)
- {
- float value = property.value.Get<float>();
- switch (property.unit)
- {
- case Property::NUMBER:
- case Property::RAD:
- return value;
- case Property::DEG:
- return Math::DegreesToRadians(value);
- default:
- break;
- }
- 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, 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)
- {
- float multiplier = 1.0f;
- switch (property.unit)
- {
- case Property::PERCENT:
- multiplier = 0.01f;
- //-fallthrough
- case Property::EM:
- if (!parent_values)
- return 0;
- return property.value.Get<float>() * 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.");
- }
- }
- return ComputeAbsoluteLength(property, dp_ratio, vp_dimensions);
- }
- Style::Clip ComputeClip(const Property* property)
- {
- const int value = property->Get<int>();
- if (property->unit == Property::KEYWORD)
- return Style::Clip(static_cast<Style::Clip::Type>(value));
- else if (property->unit == Property::NUMBER)
- return Style::Clip(Style::Clip::Type::Number, static_cast<int8_t>(value));
- RMLUI_ERRORMSG("Invalid clip type");
- return Style::Clip();
- }
- Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
- {
- if (property->unit & Property::LENGTH)
- {
- float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
- return Style::LineHeight(value, Style::LineHeight::Length, value);
- }
- float scale_factor = 1.0f;
- 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;
- default:
- RMLUI_ERRORMSG("Invalid unit for line-height");
- }
- float value = font_size * scale_factor;
- return Style::LineHeight(value, Style::LineHeight::Number, scale_factor);
- }
- 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)
- {
- float value = ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions);
- return Style::VerticalAlign(value);
- }
- else if (property->unit & Property::PERCENT)
- {
- return Style::VerticalAlign(property->Get<float>() * line_height);
- }
- RMLUI_ASSERT(property->unit & Property::KEYWORD);
- return Style::VerticalAlign((Style::VerticalAlign::Type)property->Get<int>());
- }
- Style::LengthPercentage ComputeLengthPercentage(const Property* property, float font_size, float document_font_size, float dp_ratio, Vector2f vp_dimensions)
- {
- using namespace Style;
- if (property->unit & Property::PERCENT)
- return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
- return LengthPercentage(LengthPercentage::Length, ComputeLength(property, 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)
- return LengthPercentageAuto(LengthPercentageAuto::Percentage, property->Get<float>());
- else if (property->unit & Property::KEYWORD)
- return LengthPercentageAuto(LengthPercentageAuto::Auto);
- return LengthPercentageAuto(LengthPercentageAuto::Length, ComputeLength(property, 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)
- {
- using namespace Style;
- 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)
- {
- float percent = 0.0f;
- OriginX origin = (OriginX)property->Get<int>();
- switch (origin)
- {
- case OriginX::Left: percent = 0.0f; break;
- case OriginX::Center: percent = 50.0f; break;
- case OriginX::Right: percent = 100.f; break;
- }
- return LengthPercentage(LengthPercentage::Percentage, percent);
- }
- else if (property->unit & Property::PERCENT)
- return LengthPercentage(LengthPercentage::Percentage, property->Get<float>());
- return LengthPercentage(LengthPercentage::Length, ComputeLength(property, font_size, document_font_size, dp_ratio, vp_dimensions));
- }
- uint16_t ComputeBorderWidth(float computed_length)
- {
- if (computed_length <= 0.f)
- return 0;
- if (computed_length <= 1.f)
- return 1;
-
- return uint16_t(computed_length + 0.5f);
- }
- } // namespace Rml
|