/*
* 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 "precompiled.h"
#include "../../Include/RmlUi/Core/StyleSheet.h"
#include
#include "ElementDefinition.h"
#include "StyleSheetFactory.h"
#include "StyleSheetNode.h"
#include "StyleSheetParser.h"
#include "Utilities.h"
#include "../../Include/RmlUi/Core/Element.h"
#include "../../Include/RmlUi/Core/PropertyDefinition.h"
#include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
#include "../../Include/RmlUi/Core/FontEffectInstancer.h"
namespace Rml {
namespace Core {
// Sorts style nodes based on specificity.
static bool StyleSheetNodeSort(const StyleSheetNode* lhs, const StyleSheetNode* rhs)
{
return lhs->GetSpecificity() < rhs->GetSpecificity();
}
StyleSheet::StyleSheet()
{
root = std::make_unique("", StyleSheetNode::ROOT);
specificity_offset = 0;
}
StyleSheet::~StyleSheet()
{
}
bool StyleSheet::LoadStyleSheet(Stream* stream)
{
StyleSheetParser parser;
specificity_offset = parser.Parse(root.get(), stream, *this, keyframes, decorator_map, spritesheet_list);
return specificity_offset >= 0;
}
/// Combines this style sheet with another one, producing a new sheet
SharedPtr StyleSheet::CombineStyleSheet(const StyleSheet& other_sheet) const
{
SharedPtr new_sheet = std::make_shared();
if (!new_sheet->root->MergeHierarchy(root.get()) ||
!new_sheet->root->MergeHierarchy(other_sheet.root.get(), specificity_offset))
{
return nullptr;
}
// Any matching @keyframe names are overridden as per CSS rules
new_sheet->keyframes.reserve(keyframes.size() + other_sheet.keyframes.size());
new_sheet->keyframes = keyframes;
for (auto& other_keyframes : other_sheet.keyframes)
{
new_sheet->keyframes[other_keyframes.first] = other_keyframes.second;
}
// Copy over the decorators, and replace any matching decorator names from other_sheet
new_sheet->decorator_map.reserve(decorator_map.size() + other_sheet.decorator_map.size());
new_sheet->decorator_map = decorator_map;
for (auto& other_decorator: other_sheet.decorator_map)
{
new_sheet->decorator_map[other_decorator.first] = other_decorator.second;
}
new_sheet->spritesheet_list.Reserve(
spritesheet_list.NumSpriteSheets() + other_sheet.spritesheet_list.NumSpriteSheets(),
spritesheet_list.NumSprites() + other_sheet.spritesheet_list.NumSprites()
);
new_sheet->spritesheet_list = other_sheet.spritesheet_list;
new_sheet->spritesheet_list.Merge(spritesheet_list);
new_sheet->specificity_offset = specificity_offset + other_sheet.specificity_offset;
return new_sheet;
}
// Builds the node index for a combined style sheet.
void StyleSheet::BuildNodeIndexAndOptimizeProperties()
{
if (complete_node_index.empty())
{
styled_node_index.clear();
complete_node_index.clear();
root->BuildIndexAndOptimizeProperties(styled_node_index, complete_node_index, *this);
root->SetStructurallyVolatileRecursive(false);
}
}
// Returns the Keyframes of the given name, or null if it does not exist.
Keyframes * StyleSheet::GetKeyframes(const String & name)
{
auto it = keyframes.find(name);
if (it != keyframes.end())
return &(it->second);
return nullptr;
}
SharedPtr StyleSheet::GetDecorator(const String& name) const
{
auto it = decorator_map.find(name);
if (it == decorator_map.end())
return nullptr;
return it->second.decorator;
}
const Sprite* StyleSheet::GetSprite(const String& name) const
{
return spritesheet_list.GetSprite(name);
}
DecoratorList StyleSheet::InstanceDecoratorsFromString(const String& decorator_string_value, const String& source_file, int source_line_number) const
{
// Decorators are declared as
// decorator: [, ...];
// Where is either a @decorator name:
// decorator: invader-theme-background, ...;
// or is an anonymous decorator with inline properties
// decorator: tiled-box( ), ...;
DecoratorList decorator_list;
if (decorator_string_value.empty() || decorator_string_value == NONE)
return decorator_list;
// Make sure we don't split inside the parenthesis since they may appear in decorator shorthands.
StringList decorator_string_list;
StringUtilities::ExpandString(decorator_string_list, decorator_string_value, ',', '(', ')');
decorator_list.reserve(decorator_string_list.size());
// Get or instance each decorator in the comma-separated string list
for (const String& decorator_string : decorator_string_list)
{
const size_t shorthand_open = decorator_string.find('(');
const size_t shorthand_close = decorator_string.rfind(')');
const bool invalid_parenthesis = (shorthand_open == String::npos || shorthand_close == String::npos || shorthand_open >= shorthand_close);
if (invalid_parenthesis)
{
// We found no parenthesis, that means the value must be a name of a @decorator rule, look it up
SharedPtr decorator = GetDecorator(decorator_string);
if (decorator)
decorator_list.emplace_back(std::move(decorator));
else
Log::Message(Log::LT_WARNING, "Decorator name '%s' could not be found in any @decorator rule, declared at %s:%d", decorator_string.c_str(), source_file.c_str(), source_line_number);
}
else
{
// Since we have parentheses it must be an anonymous decorator with inline properties
const String type = StringUtilities::StripWhitespace(decorator_string.substr(0, shorthand_open));
// Check for valid decorator type
DecoratorInstancer* instancer = Factory::GetDecoratorInstancer(type);
if (!instancer)
{
Log::Message(Log::LT_WARNING, "Decorator type '%s' not found, declared at %s:%d", type.c_str(), source_file.c_str(), source_line_number);
continue;
}
const String shorthand = decorator_string.substr(shorthand_open + 1, shorthand_close - shorthand_open - 1);
const PropertySpecification& specification = instancer->GetPropertySpecification();
// Parse the shorthand properties given by the 'decorator' shorthand property
PropertyDictionary properties;
if (!specification.ParsePropertyDeclaration(properties, "decorator", shorthand, source_file, source_line_number))
{
Log::Message(Log::LT_WARNING, "Could not parse decorator value '%s' at %s:%d", decorator_string.c_str(), source_file.c_str(), source_line_number);
continue;
}
// Set unspecified values to their defaults
specification.SetPropertyDefaults(properties);
SharedPtr decorator = instancer->InstanceDecorator(type, properties, DecoratorInstancerInterface(*this));
if (decorator)
decorator_list.emplace_back(std::move(decorator));
else
{
Log::Message(Log::LT_WARNING, "Decorator '%s' could not be instanced, declared at %s:%d", decorator_string.c_str(), source_file.c_str(), source_line_number);
continue;
}
}
}
return decorator_list;
}
FontEffectListPtr StyleSheet::InstanceFontEffectsFromString(const String& font_effect_string_value, const String& source_file, int source_line_number) const
{
// Font-effects are declared as
// font-effect: [, ...];
// Where is declared with inline properties, e.g.
// font-effect: outline( 1px black ), ...;
if (font_effect_string_value.empty() || font_effect_string_value == NONE)
return nullptr;
FontEffectList font_effect_list;
// Make sure we don't split inside the parenthesis since they may appear in decorator shorthands.
StringList font_effect_string_list;
StringUtilities::ExpandString(font_effect_string_list, font_effect_string_value, ',', '(', ')');
font_effect_list.reserve(font_effect_string_list.size());
// Get or instance each decorator in the comma-separated string list
for (const String& font_effect_string : font_effect_string_list)
{
const size_t shorthand_open = font_effect_string.find('(');
const size_t shorthand_close = font_effect_string.rfind(')');
const bool invalid_parenthesis = (shorthand_open == String::npos || shorthand_close == String::npos || shorthand_open >= shorthand_close);
if (invalid_parenthesis)
{
// We found no parenthesis, font-effects can only be declared anonymously for now.
Log::Message(Log::LT_WARNING, "Invalid syntax for font-effect '%s', declared at %s:%d", font_effect_string.c_str(), source_file.c_str(), source_line_number);
}
else
{
// Since we have parentheses it must be an anonymous decorator with inline properties
const String type = StringUtilities::StripWhitespace(font_effect_string.substr(0, shorthand_open));
// Check for valid font-effect type
FontEffectInstancer* instancer = Factory::GetFontEffectInstancer(type);
if (!instancer)
{
Log::Message(Log::LT_WARNING, "Font-effect type '%s' not found, declared at %s:%d", type.c_str(), source_file.c_str(), source_line_number);
continue;
}
const String shorthand = font_effect_string.substr(shorthand_open + 1, shorthand_close - shorthand_open - 1);
const PropertySpecification& specification = instancer->GetPropertySpecification();
// Parse the shorthand properties given by the 'font-effect' shorthand property
PropertyDictionary properties;
if (!specification.ParsePropertyDeclaration(properties, "font-effect", shorthand, source_file, source_line_number))
{
Log::Message(Log::LT_WARNING, "Could not parse decorator value '%s' at %s:%d", font_effect_string.c_str(), source_file.c_str(), source_line_number);
continue;
}
// Set unspecified values to their defaults
specification.SetPropertyDefaults(properties);
SharedPtr font_effect = instancer->InstanceFontEffect(type, properties);
if (font_effect)
{
// Create a unique hash value for the given type and values
size_t fingerprint = std::hash{}(type);
for (const auto& id_value : properties.GetProperties())
Utilities::HashCombine(fingerprint, id_value.second.Get());
font_effect->SetFingerprint(fingerprint);
font_effect_list.emplace_back(std::move(font_effect));
}
else
{
Log::Message(Log::LT_WARNING, "Font-effect '%s' could not be instanced, declared at %s:%d", font_effect_string.c_str(), source_file.c_str(), source_line_number);
continue;
}
}
}
// Partition the list such that the back layer effects appear before the front layer effects
std::stable_partition(font_effect_list.begin(), font_effect_list.end(),
[](const SharedPtr& effect) { return effect->GetLayer() == FontEffect::Layer::Back; }
);
return std::make_shared(std::move(font_effect_list));
}
// Returns the compiled element definition for a given element hierarchy.
SharedPtr StyleSheet::GetElementDefinition(const Element* element) const
{
RMLUI_ASSERT_NONRECURSIVE;
// See if there are any styles defined for this element.
// Using static to avoid allocations. Make sure we don't call this function recursively.
static std::vector< const StyleSheetNode* > applicable_nodes;
applicable_nodes.clear();
String tags[] = {element->GetTagName(), ""};
for (int i = 0; i < 2; i++)
{
auto it_nodes = styled_node_index.find(tags[i]);
if (it_nodes != styled_node_index.end())
{
const NodeList& nodes = it_nodes->second;
// There are! Now see if we satisfy all of their parenting requirements. What this involves is traversing the style
// nodes backwards, trying to match nodes in the element's hierarchy to nodes in the style hierarchy.
for (StyleSheetNode* node : nodes)
{
if (node->IsApplicable(element))
{
// Get the node to add any of its non-tag children that we match into our list.
node->GetApplicableDescendants(applicable_nodes, element);
}
}
}
}
std::sort(applicable_nodes.begin(), applicable_nodes.end(), StyleSheetNodeSort);
// If this element definition won't actually store any information, don't bother with it.
if (applicable_nodes.empty())
return NULL;
// Check if this puppy has already been cached in the node index; it may be that it has already been created by an
// element with a different address but an identical output definition.
size_t seed = 0;
for (const StyleSheetNode* node : applicable_nodes)
Utilities::HashCombine(seed, node);
auto cache_iterator = node_cache.find(seed);
if (cache_iterator != node_cache.end())
{
SharedPtr& definition = (*cache_iterator).second;
applicable_nodes.clear();
return definition;
}
// Create the new definition and add it to our cache. One reference count is added, bringing the total to two; one
// for the element that requested it, and one for the cache.
auto new_definition = std::make_shared(applicable_nodes);
// Add to the node cache.
node_cache[seed] = new_definition;
return new_definition;
}
}
}