Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG decorators, caching and content cropping #598

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMake/FileList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -561,15 +561,22 @@ set(Lottie_SRC_FILES
)

set(SVG_HDR_FILES
${PROJECT_SOURCE_DIR}/Source/SVG/DecoratorSVG.h
${PROJECT_SOURCE_DIR}/Source/SVG/DecoratorSVGInstancer.h
${PROJECT_SOURCE_DIR}/Source/SVG/SVGPlugin.h
${PROJECT_SOURCE_DIR}/Source/SVG/SVGCache.h
)

set(SVG_PUB_HDR_FILES
${PROJECT_SOURCE_DIR}/Include/RmlUi/SVG/ElementSVG.h
${PROJECT_SOURCE_DIR}/Include/RmlUi/SVG/SVGTypes.h
)

set(SVG_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/SVG/DecoratorSVG.cpp
${PROJECT_SOURCE_DIR}/Source/SVG/DecoratorSVGInstancer.cpp
${PROJECT_SOURCE_DIR}/Source/SVG/ElementSVG.cpp
${PROJECT_SOURCE_DIR}/Source/SVG/SVGCache.cpp
${PROJECT_SOURCE_DIR}/Source/SVG/SVGPlugin.cpp
)

182 changes: 86 additions & 96 deletions Include/RmlUi/SVG/ElementSVG.h
Original file line number Diff line number Diff line change
@@ -1,96 +1,86 @@
/*
* 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-2023 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.
*
*/

#ifndef RMLUI_SVG_ELEMENT_SVG_H
#define RMLUI_SVG_ELEMENT_SVG_H

#include "../Core/Element.h"
#include "../Core/Geometry.h"
#include "../Core/Header.h"
#include "../Core/Texture.h"

namespace lunasvg {
class Document;
}

namespace Rml {

class RMLUICORE_API ElementSVG : public Element {
public:
RMLUI_RTTI_DefineWithParent(ElementSVG, Element)

ElementSVG(const String& tag);
virtual ~ElementSVG();

/// Returns the element's inherent size.
bool GetIntrinsicDimensions(Vector2f& dimensions, float& ratio) override;

protected:
/// Renders the image.
void OnRender() override;

/// Regenerates the element's geometry.
void OnResize() override;

/// Checks for changes to the image's source or dimensions.
/// @param[in] changed_attributes A list of attributes changed on the element.
void OnAttributeChange(const ElementAttributes& changed_attributes) override;

/// Called when properties on the element are changed.
/// @param[in] changed_properties The properties changed on the element.
void OnPropertyChange(const PropertyIdSet& changed_properties) override;

private:
// Generates the element's geometry.
void GenerateGeometry();
// Loads the SVG document specified by the 'src' attribute.
bool LoadSource();
// Update the texture when necessary.
void UpdateTexture();

bool source_dirty = false;
bool geometry_dirty = false;
bool texture_dirty = false;

// The texture this element is rendering from.
Texture texture;

// The image's intrinsic dimensions.
Vector2f intrinsic_dimensions;
// The element's size for rendering.
Vector2i render_dimensions;

// The geometry used to render this element.
Geometry geometry;

UniquePtr<lunasvg::Document> svg_document;
};

} // namespace Rml

#endif
/*
* 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.
*
*/

#ifndef RMLUI_SVG_ELEMENT_SVG_H
#define RMLUI_SVG_ELEMENT_SVG_H

#include "../Core/Header.h"
#include "../Core/Element.h"
#include "../Core/Geometry.h"
#include "../Core/Texture.h"

#include "SVGTypes.h"

namespace Rml {

class RMLUICORE_API ElementSVG : public Element
{
public:
RMLUI_RTTI_DefineWithParent(ElementSVG, Element)

ElementSVG(const String& tag);
virtual ~ElementSVG();

/// Returns the element's inherent size.
bool GetIntrinsicDimensions(Vector2f& dimensions, float& ratio) override;

protected:
/// Renders the image.
void OnRender() override;

/// Regenerates the element's geometry.
void OnResize() override;

/// Checks for changes to the image's source or dimensions.
/// @param[in] changed_attributes A list of attributes changed on the element.
void OnAttributeChange(const ElementAttributes& changed_attributes) override;

/// Called when properties on the element are changed.
/// @param[in] changed_properties The properties changed on the element.
void OnPropertyChange(const PropertyIdSet& changed_properties) override;

private:
void UpdateCachedData();

bool is_dirty = false;
bool source_dirty = false;

SVG::SVGHandle handle = 0;

Geometry* geometry = nullptr;
// The image's intrinsic dimensions.
Vector2f intrinsic_dimensions;

// The element's size for rendering.
String source_path;
bool content_fit = false;
};

} // namespace Rml

#endif
43 changes: 43 additions & 0 deletions Include/RmlUi/SVG/SVGTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.
*
*/

#ifndef RMLUI_SVG_SVG_TYPES_H
#define RMLUI_SVG_SVG_TYPES_H

#include "../Core/Types.h"

namespace Rml {
namespace SVG {

using SVGHandle = size_t;

} // namespace SVG
} // namespace Rml

#endif

82 changes: 82 additions & 0 deletions Source/SVG/DecoratorSVG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 "DecoratorSVG.h"
#include "SVGCache.h"
#include "../../Include/RmlUi/Core/Element.h"
#include "../../Include/RmlUi/Core/Geometry.h"

namespace Rml {
namespace SVG {

DecoratorSVG::DecoratorSVG(const String& source, const bool crop_to_content)
: source_path(source)
, crop_to_content(crop_to_content)
{
RMLUI_ASSERT(!source_path.empty());
}

DecoratorSVG::~DecoratorSVG()
{
}

// Called on a decorator to generate any required per-element data for a newly decorated element.
DecoratorDataHandle DecoratorSVG::GenerateElementData(Element* element) const
{
SVG::SVGHandle const handle = SVG::SVGCache::GetHandle(source_path, element, crop_to_content, Box::PADDING);
if (handle == 0u)
{
return 0u;
}

Data* const data = new Data();
data->handle = handle;
Vector2f dimensions;
data->geometry = SVG::SVGCache::GetGeometry(handle, dimensions);

return reinterpret_cast<DecoratorDataHandle>(data);
}

// Called to release element data generated by this decorator.
void DecoratorSVG::ReleaseElementData(DecoratorDataHandle element_data) const
{
Data* data = reinterpret_cast<Data*>(element_data);
SVG::SVGCache::ReleaseHandle(data->handle);
delete data;
}

// Called to render the decorator on an element.
void DecoratorSVG::RenderElement(Element* element, DecoratorDataHandle element_data) const
{
Data* data = reinterpret_cast<Data*>(element_data);
if (data)
data->geometry->Render(element->GetAbsoluteOffset(Box::PADDING).Round());
}

} // namespace SVG
} // namespace Rml
Loading
Loading