ImGui: Enable lunasvg

This commit is contained in:
Stenzek 2024-08-25 14:39:59 +10:00
parent 5540d07119
commit 11787a5969
No known key found for this signature in database
6 changed files with 57 additions and 30 deletions

View file

@ -16,6 +16,7 @@ find_package(ZLIB REQUIRED) # 1.3, but Mac currently doesn't use it.
find_package(PNG 1.6.40 REQUIRED) find_package(PNG 1.6.40 REQUIRED)
find_package(JPEG REQUIRED) find_package(JPEG REQUIRED)
find_package(Freetype 2.13.2 REQUIRED) # 2.13.3, but flatpak is still on 2.13.2. find_package(Freetype 2.13.2 REQUIRED) # 2.13.3, but flatpak is still on 2.13.2.
find_package(lunasvg 2.4.1 REQUIRED)
find_package(cpuinfo REQUIRED) find_package(cpuinfo REQUIRED)
find_package(DiscordRPC 3.4.0 REQUIRED) find_package(DiscordRPC 3.4.0 REQUIRED)
find_package(SoundTouch 2.3.3 REQUIRED) find_package(SoundTouch 2.3.3 REQUIRED)

View file

@ -19,4 +19,4 @@ set(SRCS
add_library(imgui ${SRCS}) add_library(imgui ${SRCS})
target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/src") target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_include_directories(imgui INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") target_include_directories(imgui INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(imgui PUBLIC Freetype::Freetype) target_link_libraries(imgui PUBLIC Freetype::Freetype lunasvg::lunasvg)

View file

@ -29,7 +29,7 @@
<ItemDefinitionGroup> <ItemDefinitionGroup>
<ClCompile> <ClCompile>
<WarningLevel>TurnOffAllWarnings</WarningLevel> <WarningLevel>TurnOffAllWarnings</WarningLevel>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)src;$(DepsIncludeDir)freetype2;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)src;$(DepsIncludeDir)freetype2;$(DepsIncludeDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="..\msvc\vsprops\Targets.props" /> <Import Project="..\msvc\vsprops\Targets.props" />

View file

@ -87,7 +87,7 @@
// Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided). // Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided).
// Only works in combination with IMGUI_ENABLE_FREETYPE. // Only works in combination with IMGUI_ENABLE_FREETYPE.
// (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement) // (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement)
//#define IMGUI_ENABLE_FREETYPE_LUNASVG #define IMGUI_ENABLE_FREETYPE_LUNASVG
//---- Use stb_truetype to build and rasterize the font atlas (default) //---- Use stb_truetype to build and rasterize the font atlas (default)
// The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend. // The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend.

View file

@ -48,7 +48,8 @@
#ifdef IMGUI_ENABLE_FREETYPE_LUNASVG #ifdef IMGUI_ENABLE_FREETYPE_LUNASVG
#include FT_OTSVG_H // <freetype/otsvg.h> #include FT_OTSVG_H // <freetype/otsvg.h>
#include FT_BBOX_H // <freetype/ftbbox.h> #include FT_BBOX_H // <freetype/ftbbox.h>
#include <lunasvg.h> #include <algorithm>
#include <lunasvg_c.h>
#if !((FREETYPE_MAJOR >= 2) && (FREETYPE_MINOR >= 12)) #if !((FREETYPE_MAJOR >= 2) && (FREETYPE_MINOR >= 12))
#error IMGUI_ENABLE_FREETYPE_LUNASVG requires FreeType version >= 2.12 #error IMGUI_ENABLE_FREETYPE_LUNASVG requires FreeType version >= 2.12
#endif #endif
@ -835,11 +836,26 @@ void ImGuiFreeType::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* u
// The original code from the demo is licensed under CeCILL-C Free Software License Agreement (https://gitlab.freedesktop.org/freetype/freetype/-/blob/master/LICENSE.TXT) // The original code from the demo is licensed under CeCILL-C Free Software License Agreement (https://gitlab.freedesktop.org/freetype/freetype/-/blob/master/LICENSE.TXT)
struct LunasvgPortState struct LunasvgPortState
{ {
LunasvgPortState();
~LunasvgPortState();
FT_Error err = FT_Err_Ok; FT_Error err = FT_Err_Ok;
lunasvg::Matrix matrix; lunasvg_matrix* matrix = nullptr;
std::unique_ptr<lunasvg::Document> svg = nullptr; lunasvg_document* svg = nullptr;
}; };
LunasvgPortState::LunasvgPortState()
{
matrix = lunasvg_matrix_create();
}
LunasvgPortState::~LunasvgPortState()
{
lunasvg_matrix_destroy(matrix);
if (svg)
lunasvg_document_destroy(svg);
}
static FT_Error ImGuiLunasvgPortInit(FT_Pointer* _state) static FT_Error ImGuiLunasvgPortInit(FT_Pointer* _state)
{ {
*_state = IM_NEW(LunasvgPortState)(); *_state = IM_NEW(LunasvgPortState)();
@ -860,9 +876,10 @@ static FT_Error ImGuiLunasvgPortRender(FT_GlyphSlot slot, FT_Pointer* _state)
return state->err; return state->err;
// rows is height, pitch (or stride) equals to width * sizeof(int32) // rows is height, pitch (or stride) equals to width * sizeof(int32)
lunasvg::Bitmap bitmap((uint8_t*)slot->bitmap.buffer, slot->bitmap.width, slot->bitmap.rows, slot->bitmap.pitch); lunasvg_bitmap* bitmap = lunasvg_bitmap_create_with_data((uint8_t*)slot->bitmap.buffer, slot->bitmap.width, slot->bitmap.rows, slot->bitmap.pitch);
state->svg->setMatrix(state->svg->matrix().identity()); // Reset the svg matrix to the default value lunasvg_document_set_identity_matrix(state->svg); // Reset the svg matrix to the default value
state->svg->render(bitmap, state->matrix); // state->matrix is already scaled and translated lunasvg_document_render(state->svg, bitmap, state->matrix); // state->matrix is already scaled and translated
lunasvg_bitmap_destroy(bitmap);
state->err = FT_Err_Ok; state->err = FT_Err_Ok;
return state->err; return state->err;
} }
@ -878,47 +895,55 @@ static FT_Error ImGuiLunasvgPortPresetSlot(FT_GlyphSlot slot, FT_Bool cache, FT_
if (cache) if (cache)
return state->err; return state->err;
state->svg = lunasvg::Document::loadFromData((const char*)document->svg_document, document->svg_document_length); if (state->svg)
lunasvg_document_destroy(state->svg);
state->svg = lunasvg_document_load_from_data(document->svg_document, document->svg_document_length);
if (state->svg == nullptr) if (state->svg == nullptr)
{ {
state->err = FT_Err_Invalid_SVG_Document; state->err = FT_Err_Invalid_SVG_Document;
return state->err; return state->err;
} }
lunasvg::Box box = state->svg->box(); lunasvg_box* box = lunasvg_box_create();
double scale = std::min(metrics.x_ppem / box.w, metrics.y_ppem / box.h); double box_x, box_y, box_w, box_h;
lunasvg_document_get_box(state->svg, box);
lunasvg_box_get_values(box, &box_x, &box_y, &box_w, &box_h);
double scale = std::min(metrics.x_ppem / box_w, metrics.y_ppem / box_h);
double xx = (double)document->transform.xx / (1 << 16); double xx = (double)document->transform.xx / (1 << 16);
double xy = -(double)document->transform.xy / (1 << 16); double xy = -(double)document->transform.xy / (1 << 16);
double yx = -(double)document->transform.yx / (1 << 16); double yx = -(double)document->transform.yx / (1 << 16);
double yy = (double)document->transform.yy / (1 << 16); double yy = (double)document->transform.yy / (1 << 16);
double x0 = (double)document->delta.x / 64 * box.w / metrics.x_ppem; double x0 = (double)document->delta.x / 64 * box_w / metrics.x_ppem;
double y0 = -(double)document->delta.y / 64 * box.h / metrics.y_ppem; double y0 = -(double)document->delta.y / 64 * box_h / metrics.y_ppem;
// Scale and transform, we don't translate the svg yet // Scale and transform, we don't translate the svg yet
state->matrix.identity(); lunasvg_matrix_identity(state->matrix);
state->matrix.scale(scale, scale); lunasvg_matrix_scale(state->matrix, scale, scale);
state->matrix.transform(xx, xy, yx, yy, x0, y0); lunasvg_matrix_transform(state->matrix, xx, xy, yx, yy, x0, y0);
state->svg->setMatrix(state->matrix); lunasvg_document_set_matrix(state->svg, state->matrix);
// Pre-translate the matrix for the rendering step // Pre-translate the matrix for the rendering step
state->matrix.translate(-box.x, -box.y); lunasvg_matrix_translate(state->matrix, -box_x, -box_y);
// Get the box again after the transformation // Get the box again after the transformation
box = state->svg->box(); lunasvg_document_get_box(state->svg, box);
lunasvg_box_get_values(box, &box_x, &box_y, &box_w, &box_h);
lunasvg_box_destroy(box);
// Calculate the bitmap size // Calculate the bitmap size
slot->bitmap_left = FT_Int(box.x); slot->bitmap_left = FT_Int(box_x);
slot->bitmap_top = FT_Int(-box.y); slot->bitmap_top = FT_Int(-box_y);
slot->bitmap.rows = (unsigned int)(ImCeil((float)box.h)); slot->bitmap.rows = (unsigned int)(ImCeil((float)box_h));
slot->bitmap.width = (unsigned int)(ImCeil((float)box.w)); slot->bitmap.width = (unsigned int)(ImCeil((float)box_w));
slot->bitmap.pitch = slot->bitmap.width * 4; slot->bitmap.pitch = slot->bitmap.width * 4;
slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA; slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA;
// Compute all the bearings and set them correctly. The outline is scaled already, we just need to use the bounding box. // Compute all the bearings and set them correctly. The outline is scaled already, we just need to use the bounding box.
double metrics_width = box.w; double metrics_width = box_w;
double metrics_height = box.h; double metrics_height = box_h;
double horiBearingX = box.x; double horiBearingX = box_x;
double horiBearingY = -box.y; double horiBearingY = -box_y;
double vertBearingX = slot->metrics.horiBearingX / 64.0 - slot->metrics.horiAdvance / 64.0 / 2.0; double vertBearingX = slot->metrics.horiBearingX / 64.0 - slot->metrics.horiAdvance / 64.0 / 2.0;
double vertBearingY = (slot->metrics.vertAdvance / 64.0 - slot->metrics.height / 64.0) / 2.0; double vertBearingY = (slot->metrics.vertAdvance / 64.0 - slot->metrics.height / 64.0) / 2.0;
slot->metrics.width = FT_Pos(IM_ROUND(metrics_width * 64.0)); // Using IM_ROUND() assume width and height are positive slot->metrics.width = FT_Pos(IM_ROUND(metrics_width * 64.0)); // Using IM_ROUND() assume width and height are positive

View file

@ -26,7 +26,7 @@
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)SDL2</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)SDL2</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <Link>
<AdditionalDependencies>%(AdditionalDependencies);cpuinfo.lib;freetype.lib;jpeg.lib;libpng16.lib;libwebp.lib;SDL2.lib;soundtouch.lib;zlib.lib;zstd.lib</AdditionalDependencies> <AdditionalDependencies>%(AdditionalDependencies);cpuinfo.lib;freetype.lib;jpeg.lib;libpng16.lib;libwebp.lib;lunasvg.lib;SDL2.lib;soundtouch.lib;zlib.lib;zstd.lib</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
@ -38,6 +38,7 @@
<DepsDLLs Include="$(DepsBinDir)libpng16.dll" /> <DepsDLLs Include="$(DepsBinDir)libpng16.dll" />
<DepsDLLs Include="$(DepsBinDir)libsharpyuv.dll" /> <DepsDLLs Include="$(DepsBinDir)libsharpyuv.dll" />
<DepsDLLs Include="$(DepsBinDir)libwebp.dll" /> <DepsDLLs Include="$(DepsBinDir)libwebp.dll" />
<DepsDLLs Include="$(DepsBinDir)lunasvg.dll" />
<DepsDLLs Include="$(DepsBinDir)SDL2.dll" /> <DepsDLLs Include="$(DepsBinDir)SDL2.dll" />
<DepsDLLs Include="$(DepsBinDir)shaderc_shared.dll" /> <DepsDLLs Include="$(DepsBinDir)shaderc_shared.dll" />
<DepsDLLs Include="$(DepsBinDir)soundtouch.dll" /> <DepsDLLs Include="$(DepsBinDir)soundtouch.dll" />