Skip to content

Commit

Permalink
#2162 - Add support for rendering embedded SVGs while exporting to PNG (
Browse files Browse the repository at this point in the history
#2299)

Co-authored-by: Ilya Zaytsev <[email protected]>
  • Loading branch information
nupagadi and Ilya Zaytsev authored Sep 9, 2024
1 parent 453a087 commit abe5422
Show file tree
Hide file tree
Showing 73 changed files with 18,991 additions and 5 deletions.
Binary file modified api/tests/integration/tests/rendering/ref/linux/images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified api/tests/integration/tests/rendering/ref/mac/images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified api/tests/integration/tests/rendering/ref/win/images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions api/wasm/indigo-ketcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set(EMCC_FLAGS

set(TARGET_FILES
$<TARGET_OBJECTS:${PROJECT_NAME}-object>
$<TARGET_FILE:indigo-static> $<TARGET_FILE:indigo-inchi-static> $<TARGET_FILE:indigo-renderer-static> $<TARGET_FILE:render2d> $<TARGET_FILE:cairo> $<TARGET_FILE:freetype> $<TARGET_FILE:png> $<TARGET_FILE:pixman> $<TARGET_FILE:indigo-core> $<TARGET_FILE:inchi> $<TARGET_FILE:tinyxml2> $<TARGET_FILE:z>
$<TARGET_FILE:indigo-static> $<TARGET_FILE:indigo-inchi-static> $<TARGET_FILE:indigo-renderer-static> $<TARGET_FILE:render2d> $<TARGET_FILE:lunasvg> $<TARGET_FILE:cairo> $<TARGET_FILE:freetype> $<TARGET_FILE:png> $<TARGET_FILE:pixman> $<TARGET_FILE:indigo-core> $<TARGET_FILE:inchi> $<TARGET_FILE:tinyxml2> $<TARGET_FILE:z>
)

set(TARGET_FILES_NORENDER
Expand Down Expand Up @@ -122,4 +122,4 @@ else ()
COMMAND ${COPY_COMMAND} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}${SEP}${PROJECT_NAME}*.tgz ${NATIVE_DIST_DIRECTORY}${SEP}
DEPENDS ${PROJECT_NAME} ${PROJECT_NAME}-separate ${PROJECT_NAME}-separate-norender ${PROJECT_NAME}-norender
)
endif ()
endif ()
3 changes: 3 additions & 0 deletions cmake/setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ if (UNIX OR MINGW)
string(APPEND CMAKE_C_FLAGS " -fvisibility=hidden $ENV{CFLAGS}")
string(APPEND CMAKE_CXX_FLAGS " -fvisibility=hidden -fvisibility-inlines-hidden $ENV{CXXFLAGS}")

set(CMAKE_C_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS_DEBUG "-g3")

if(BUILD_STANDALONE AND NOT EMSCRIPTEN)
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
string(APPEND CMAKE_CXX_FLAGS " -static-libstdc++")
Expand Down
2 changes: 2 additions & 0 deletions core/render2d/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ if (RENDER_ENABLE_CJK)
target_compile_definitions(${PROJECT_NAME} PRIVATE RENDER_ENABLE_CJK)
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC lunasvg)

if (BUILD_STANDALONE)
target_link_libraries(${PROJECT_NAME}
PUBLIC rapidjson
Expand Down
31 changes: 29 additions & 2 deletions core/render2d/src/render_item_aux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#pragma warning(disable : 4251)
#endif

#include <lunasvg.h>
#include <stb_image_write.h>

using namespace indigo;

IMPL_ERROR(RenderItemAuxiliary, "RenderItemAuxiliary");
Expand Down Expand Up @@ -386,6 +390,16 @@ void RenderItemAuxiliary::_drawMeta(bool idle)
}
}

struct StbiContext
{
std::string data;
};

void ketImageStbiWriteFunc(void* context, void* data, int size)
{
static_cast<StbiContext*>(context)->data.assign(static_cast<const char*>(data), size);
}

void RenderItemAuxiliary::_drawImage(const KETImage& img)
{
auto& bb = img.getBoundingBox();
Expand All @@ -395,9 +409,22 @@ void RenderItemAuxiliary::_drawImage(const KETImage& img)
scale(v2);
if (img.getFormat() == KETImage::EKETPNG)
_rc.drawPng(img.getData(), Rect2f(v1, v2));
else if (img.getFormat() == KETImage::EKETPNG)
else if (img.getFormat() == KETImage::EKETSVG)
{
// TODO: implement SVG-rendering
auto document = lunasvg::Document::loadFromData(img.getData());
if (!document)
throw Error("RenderItemAuxiliary::_drawImage: loadFromData error");

auto bitmap = document->renderToBitmap();
if (!bitmap.valid())
throw Error("RenderItemAuxiliary::_drawImage: renderToBitmap error");

StbiContext stbiContext;
int rgbaChannels = 4, stride = 0;
if (!stbi_write_png_to_func(ketImageStbiWriteFunc, &stbiContext, bitmap.width(), bitmap.height(), rgbaChannels, bitmap.data(), stride))
throw Error("RenderItemAuxiliary::_drawImage: stbi_write_png_to_func error");

_rc.drawPng(stbiContext.data, Rect2f(v1, v2));
}
}

Expand Down
8 changes: 7 additions & 1 deletion third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
add_subdirectory(libpng)

option(BUILD_SHARED_LIBS OFF)
add_subdirectory(lunasvg)
target_include_directories(lunasvg INTERFACE lunasvg/3rdparty/stb)
target_compile_definitions(lunasvg INTERFACE STB_IMAGE_WRITE_IMPLEMENTATION)

if (BUILD_STANDALONE)
# InChI don't have a Conan package yet
add_subdirectory(cppcodec)
Expand All @@ -20,4 +26,4 @@ if (ENABLE_TESTS)
add_subdirectory(googletest)
endif()

add_subdirectory(object_threadsafe)
add_subdirectory(object_threadsafe)
17 changes: 17 additions & 0 deletions third_party/lunasvg/3rdparty/plutovg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
target_sources(lunasvg
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/plutovg.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-paint.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-geometry.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-blend.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-rle.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-dash.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-ft-raster.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-ft-stroker.c"
"${CMAKE_CURRENT_LIST_DIR}/plutovg-ft-math.c"
)

target_include_directories(lunasvg
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}"
)
166 changes: 166 additions & 0 deletions third_party/lunasvg/3rdparty/plutovg/FTL.TXT
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
The FreeType Project LICENSE
----------------------------

2006-Jan-27

Copyright 1996-2002, 2006 by
David Turner, Robert Wilhelm, and Werner Lemberg



Introduction
============

The FreeType Project is distributed in several archive packages;
some of them may contain, in addition to the FreeType font engine,
various tools and contributions which rely on, or relate to, the
FreeType Project.

This license applies to all files found in such packages, and
which do not fall under their own explicit license. The license
affects thus the FreeType font engine, the test programs,
documentation and makefiles, at the very least.

This license was inspired by the BSD, Artistic, and IJG
(Independent JPEG Group) licenses, which all encourage inclusion
and use of free software in commercial and freeware products
alike. As a consequence, its main points are that:

o We don't promise that this software works. However, we will be
interested in any kind of bug reports. (`as is' distribution)

o You can use this software for whatever you want, in parts or
full form, without having to pay us. (`royalty-free' usage)

o You may not pretend that you wrote this software. If you use
it, or only parts of it, in a program, you must acknowledge
somewhere in your documentation that you have used the
FreeType code. (`credits')

We specifically permit and encourage the inclusion of this
software, with or without modifications, in commercial products.
We disclaim all warranties covering The FreeType Project and
assume no liability related to The FreeType Project.


Finally, many people asked us for a preferred form for a
credit/disclaimer to use in compliance with this license. We thus
encourage you to use the following text:

"""
Portions of this software are copyright � <year> The FreeType
Project (www.freetype.org). All rights reserved.
"""

Please replace <year> with the value from the FreeType version you
actually use.


Legal Terms
===========

0. Definitions
--------------

Throughout this license, the terms `package', `FreeType Project',
and `FreeType archive' refer to the set of files originally
distributed by the authors (David Turner, Robert Wilhelm, and
Werner Lemberg) as the `FreeType Project', be they named as alpha,
beta or final release.

`You' refers to the licensee, or person using the project, where
`using' is a generic term including compiling the project's source
code as well as linking it to form a `program' or `executable'.
This program is referred to as `a program using the FreeType
engine'.

This license applies to all files distributed in the original
FreeType Project, including all source code, binaries and
documentation, unless otherwise stated in the file in its
original, unmodified form as distributed in the original archive.
If you are unsure whether or not a particular file is covered by
this license, you must contact us to verify this.

The FreeType Project is copyright (C) 1996-2000 by David Turner,
Robert Wilhelm, and Werner Lemberg. All rights reserved except as
specified below.

1. No Warranty
--------------

THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO
USE, OF THE FREETYPE PROJECT.

2. Redistribution
-----------------

This license grants a worldwide, royalty-free, perpetual and
irrevocable right and license to use, execute, perform, compile,
display, copy, create derivative works of, distribute and
sublicense the FreeType Project (in both source and object code
forms) and derivative works thereof for any purpose; and to
authorize others to exercise some or all of the rights granted
herein, subject to the following conditions:

o Redistribution of source code must retain this license file
(`FTL.TXT') unaltered; any additions, deletions or changes to
the original files must be clearly indicated in accompanying
documentation. The copyright notices of the unaltered,
original files must be preserved in all copies of source
files.

o Redistribution in binary form must provide a disclaimer that
states that the software is based in part of the work of the
FreeType Team, in the distribution documentation. We also
encourage you to put an URL to the FreeType web page in your
documentation, though this isn't mandatory.

These conditions apply to any software derived from or based on
the FreeType Project, not just the unmodified files. If you use
our work, you must acknowledge us. However, no fee need be paid
to us.

3. Advertising
--------------

Neither the FreeType authors and contributors nor you shall use
the name of the other for commercial, advertising, or promotional
purposes without specific prior written permission.

We suggest, but do not require, that you use one or more of the
following phrases to refer to this software in your documentation
or advertising materials: `FreeType Project', `FreeType Engine',
`FreeType library', or `FreeType Distribution'.

As you have not signed this license, you are not required to
accept it. However, as the FreeType Project is copyrighted
material, only this license, or another one contracted with the
authors, grants you the right to use, distribute, and modify it.
Therefore, by using, distributing, or modifying the FreeType
Project, you indicate that you understand and accept all the terms
of this license.

4. Contacts
-----------

There are two mailing lists related to FreeType:

o [email protected]

Discusses general use and applications of FreeType, as well as
future and wanted additions to the library and distribution.
If you are looking for support, start in this list if you
haven't found anything to help you in the documentation.

o [email protected]

Discusses bugs, as well as engine internals, design issues,
specific licenses, porting, etc.

Our home page can be found at

http://www.freetype.org
Loading

0 comments on commit abe5422

Please sign in to comment.