Skip to content

Commit

Permalink
Basic implementation of SVG plugin using the lunasvg library
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Apr 25, 2021
1 parent 4686c0c commit 66c9606
Show file tree
Hide file tree
Showing 18 changed files with 2,051 additions and 5 deletions.
13 changes: 13 additions & 0 deletions CMake/FileList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,16 @@ set(Lottie_SRC_FILES
${PROJECT_SOURCE_DIR}/Source/Lottie/LottiePlugin.cpp
)

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

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

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

27 changes: 27 additions & 0 deletions CMake/Modules/Findlunasvg.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Try to find LunaSVG

find_path(LUNASVG_INCLUDE_DIR document.h
HINTS $ENV{LUNASVG_DIR}
PATH_SUFFIXES lunasvg lunasvg/include include )

find_library(LUNASVG_LIBRARY_DEBUG NAMES lunasvg liblunasvg
HINTS $ENV{LUNASVG_DIR} $ENV{LUNASVG_DIR}/build
PATH_SUFFIXES debug Debug)

find_library(LUNASVG_LIBRARY_RELEASE NAMES lunasvg liblunasvg
HINTS $ENV{LUNASVG_DIR} $ENV{LUNASVG_DIR}/build
PATH_SUFFIXES release Release)

set(LUNASVG_LIBRARY
debug ${LUNASVG_LIBRARY_DEBUG}
optimized ${LUNASVG_LIBRARY_RELEASE}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(lunasvg DEFAULT_MSG
LUNASVG_LIBRARY LUNASVG_INCLUDE_DIR)

mark_as_advanced(LUNASVG_INCLUDE_DIR LUNASVG_LIBRARY_DEBUG LUNASVG_LIBRARY_RELEASE )

set(LUNASVG_LIBRARIES ${LUNASVG_LIBRARY} )
set(LUNASVG_INCLUDE_DIRS ${LUNASVG_INCLUDE_DIR} )
7 changes: 7 additions & 0 deletions CMake/SampleFileList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ set(lottie_SRC_FILES
${PROJECT_SOURCE_DIR}/Samples/basic/lottie/src/main.cpp
)

set(svg_HDR_FILES
)

set(svg_SRC_FILES
${PROJECT_SOURCE_DIR}/Samples/basic/svg/src/main.cpp
)

set(sdl2_HDR_FILES
${PROJECT_SOURCE_DIR}/Samples/basic/sdl2/src/RenderInterfaceSDL2.h
${PROJECT_SOURCE_DIR}/Samples/basic/sdl2/src/SystemInterfaceSDL2.h
Expand Down
2 changes: 2 additions & 0 deletions CMake/gen_filelists.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ printpluginfiles "Lua"

printpluginfiles "Lottie"

printpluginfiles "SVG"

popd
2 changes: 1 addition & 1 deletion CMake/gen_samplelists.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ srcdir='${PROJECT_SOURCE_DIR}'
srcpath=Samples
samples=( 'shell'
'basic/animation' 'basic/benchmark' 'basic/bitmapfont' 'basic/customlog' 'basic/databinding' 'basic/demo' 'basic/drag' 'basic/loaddocument' 'basic/treeview' 'basic/transform'
'basic/lottie' 'basic/sdl2' 'basic/sfml2'
'basic/lottie' 'basic/svg' 'basic/sdl2' 'basic/sfml2'
'tutorial/template' 'tutorial/datagrid' 'tutorial/datagrid_tree' 'tutorial/drag'
'invaders' 'luainvaders'
)
Expand Down
38 changes: 38 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ if(NOT DEFINED ENV{RLOTTIE_DIR})
set(ENV{RLOTTIE_DIR} "${PROJECT_SOURCE_DIR}/Dependencies/rlottie")
endif()

if(NOT DEFINED ENV{LUNASVG_DIR})
set(ENV{LUNASVG_DIR} "${PROJECT_SOURCE_DIR}/Dependencies/lunasvg")
endif()

#===================================
# Plaform specific global hacks ====
#===================================
Expand Down Expand Up @@ -253,6 +257,7 @@ elseif( CMAKE_CONFIGURATION_TYPES )
endif()

option(ENABLE_LOTTIE_PLUGIN "Enable plugin for Lottie animations. Requires the rlottie library." OFF)
option(ENABLE_SVG_PLUGIN "Enable plugin for SVG images. Requires the lunasvg library." OFF)

option(DISABLE_RTTI_AND_EXCEPTIONS "Build with rtti and exceptions disabled." OFF)
if(DISABLE_RTTI_AND_EXCEPTIONS)
Expand Down Expand Up @@ -366,6 +371,28 @@ if( ENABLE_LOTTIE_PLUGIN )
endif()
endif()

# lunasvg
if( ENABLE_SVG_PLUGIN )
if(NOT DEFINED LUNASVG_DIR)
set(LUNASVG_DIR $ENV{LUNASVG_DIR})
endif()

message("-- Can SVG plugin be enabled - looking for lunasvg library")

find_package(lunasvg REQUIRED)

list(APPEND CORE_LINK_LIBS ${LUNASVG_LIBRARIES})
list(APPEND CORE_INCLUDE_DIRS ${LUNASVG_INCLUDE_DIR})
list(APPEND CORE_PRIVATE_DEFS RMLUI_ENABLE_SVG_PLUGIN)

list(APPEND Core_HDR_FILES ${SVG_HDR_FILES})
list(APPEND Core_PUB_HDR_FILES ${SVG_PUB_HDR_FILES})
list(APPEND Core_SRC_FILES ${SVG_SRC_FILES})

message("-- Can SVG plugin be enabled - yes - lunasvg library found")
endif()


if(NOT BUILD_FRAMEWORK)
#===================================
# Build libraries ==================
Expand Down Expand Up @@ -736,6 +763,17 @@ if(BUILD_SAMPLES)
BUNDLE DESTINATION ${SAMPLES_DIR}
)
endif()

if( ENABLE_SVG_PLUGIN )
bl_sample(svg ${sample_LIBRARIES})

# The samples always set this as their current working directory
install(DIRECTORY DESTINATION ${SAMPLES_DIR}/basic/svg)
install(TARGETS svg
RUNTIME DESTINATION ${SAMPLES_DIR}/svg
BUNDLE DESTINATION ${SAMPLES_DIR}
)
endif()

# Build and install the tutorials
foreach(tutorial ${tutorials})
Expand Down
1 change: 0 additions & 1 deletion Include/RmlUi/Lottie/ElementLottie.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "../Core/Element.h"
#include "../Core/Geometry.h"
#include "../Core/Texture.h"
#include <memory>

namespace rlottie { class Animation; }

Expand Down
95 changes: 95 additions & 0 deletions Include/RmlUi/SVG/ElementSVG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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"

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_size_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
Loading

0 comments on commit 66c9606

Please sign in to comment.