### Build Examples Configuration (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/CMakeLists.txt Configures whether to build the example applications for LunaSVG. If enabled, it includes the `examples` subdirectory to compile and install them. ```cmake option(LUNASVG_BUILD_EXAMPLES "Build examples" ON) if(LUNASVG_BUILD_EXAMPLES) add_subdirectory(examples) endif() ``` -------------------------------- ### Install PlutoVG with Meson Source: https://github.com/sammycage/lunasvg/blob/master/plutovg/README.md This bash script outlines the steps to install PlutoVG using the Meson build system. It includes cloning the repository, setting up the build directory, compiling the project, and installing it. ```bash git clone https://github.com/sammycage/plutovg.git cd plutovg meson setup build Meson compile -C build Meson install -C build ``` -------------------------------- ### Installation Rules (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/CMakeLists.txt Defines the installation targets for LunaSVG, including headers, the library itself (shared, static, or runtime components), and the generated CMake package configuration files. This ensures the library can be properly installed and used by other projects. ```cmake install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/lunasvg.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lunasvg ) install(TARGETS lunasvg EXPORT lunasvgTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) install(EXPORT lunasvgTargets FILE lunasvgTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/lunasvg NAMESPACE lunasvg:: ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lunasvgConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/lunasvgConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/lunasvg ) ``` -------------------------------- ### Plutovg Examples Build Option (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/plutovg/CMakeLists.txt Controls whether the examples subdirectory, which likely contains sample code demonstrating the plutovg library's functionality, will be built. Defaults to ON. ```cmake option(PLUTOVG_BUILD_EXAMPLES "Build examples" ON) if(PLUTOVG_BUILD_EXAMPLES) add_subdirectory(examples) endif() ``` -------------------------------- ### Install PlutoVG with CMake Source: https://github.com/sammycage/lunasvg/blob/master/plutovg/README.md This bash script details the process of installing PlutoVG using CMake. It involves cloning the source code, configuring the build with CMake, compiling the project, and then performing the installation. ```bash git clone https://github.com/sammycage/plutovg.git cd plutovg cmake -B build . cmake --build build cmake --install build ``` -------------------------------- ### Install LunaSVG using CMake Source: https://github.com/sammycage/lunasvg/blob/master/README.md Instructions for installing the LunaSVG library using the CMake build system. This involves cloning the repository, configuring the build, and installing the library. It also shows how to link LunaSVG to your CMake projects and optionally use FetchContent for direct integration. ```bash git clone https://github.com/sammycage/lunasvg.git cd lunasvg cmake -B build . cmake --build build cmake --install build ``` ```cmake find_package(lunasvg REQUIRED) # Link LunaSVG to your target target_link_libraries(your_target_name PRIVATE lunasvg::lunasvg) ``` ```cmake include(FetchContent) FetchContent_Declare( lunasvg GIT_REPOSITORY https://github.com/sammycage/lunasvg.git GIT_TAG master # Specify the desired branch or tag ) FetchContent_MakeAvailable(lunasvg) # Link LunaSVG to your target target_link_libraries(your_target_name PRIVATE lunasvg::lunasvg) ``` ```bash cmake -B build -DUSE_SYSTEM_PLUTOVG=ON . cmake --build build ``` -------------------------------- ### Install and Build LunaSVG with Meson Source: https://github.com/sammycage/lunasvg/blob/master/README.md Steps to clone the LunaSVG repository, set up a Meson build directory, compile, and install the library. This process is essential for using LunaSVG in other Meson-based projects. ```bash git clone https://github.com/sammycage/lunasvg.git cd lunasvg meson setup build meson compile -C build meson install -C build ``` -------------------------------- ### Plutovg Package and Installation Configuration (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/plutovg/CMakeLists.txt Configures package discovery files (plutovgConfig.cmake) and installs the library, headers, and configuration files. It also generates a pkg-config file for easier integration with other projects. ```cmake configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/plutovgConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/plutovgConfig.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/plutovg ) write_basic_package_version_file(plutovgConfigVersion.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion ) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/plutovg.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/plutovg ) install(TARGETS plutovg EXPORT plutovgTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) install(EXPORT plutovgTargets FILE plutovgTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/plutovg NAMESPACE plutovg:: ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/plutovgConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/plutovgConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/plutovg ) export(EXPORT plutovgTargets FILE ${CMAKE_CURRENT_BINARY_DIR}/plutovgTargets.cmake NAMESPACE plutovg:: ) file(RELATIVE_PATH plutovg_pc_prefix_relative "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig" "${CMAKE_INSTALL_PREFIX}" ) set(plutovg_pc_cflags "") set(plutovg_pc_libs_private "") if(MATH_LIBRARY) string(APPEND plutovg_pc_libs_private " -lm") endif() if(Threads_FOUND AND CMAKE_THREAD_LIBS_INIT) string(APPEND plutovg_pc_libs_private " ${CMAKE_THREAD_LIBS_INIT}") endif() if(NOT BUILD_SHARED_LIBS) string(APPEND plutovg_pc_cflags " -DPLUTOVG_BUILD_STATIC") endif() string(CONFIGURE [[ prefix=${pcfiledir}/@plutovg_pc_prefix_relative@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ Name: PlutoVG Description: Tiny 2D vector graphics library in C Version: @PROJECT_VERSION@ Cflags: -I${includedir}/plutovg@plutovg_pc_cflags@ Libs: -L${libdir} -lplutovg Libs.private:@plutovg_pc_libs_private@ ]] plutovg_pc @ONLY) file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/plutovg.pc" "${plutovg_pc}") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/plutovg.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" ) ``` -------------------------------- ### Dynamic Theming with CSS Stylesheets (C++) Source: https://context7.com/sammycage/lunasvg/llms.txt Illustrates applying different CSS stylesheets to a single SVG document using LunaSVG to achieve themed rendering. The example loads an SVG, applies a summer theme, renders it, then applies a winter theme and renders again, saving both outputs. Requires the lunasvg library. ```cpp #include using namespace lunasvg; static const char kLandscapeContent[] = R"SVG( )SVG"; static const char kSummerStyle[] = R"CSS( .sky { fill: #4A90E2; } .sun { fill: #FF7F00; } .mountain { fill: #2E3A59; } .cloud { fill: #FFFFFF; opacity: 0.8; } .ground { fill: #2E8B57; } )CSS"; static const char kWinterStyle[] = R"CSS( .sky { fill: #87CEEB; } .sun { fill: #ADD8E6; } .mountain { fill: #2F4F4F; } .cloud { fill: #FFFFFF; opacity: 0.8; } .ground { fill: #FFFAFA; } )CSS"; int main() { auto document = Document::loadFromData(kLandscapeContent); // Apply summer theme and render document->applyStyleSheet(kSummerStyle); document->renderToBitmap().writeToPng("summer.png"); // Apply winter theme to same document document->applyStyleSheet(kWinterStyle); document->renderToBitmap().writeToPng("winter.png"); return 0; } ``` -------------------------------- ### Integrate LunaSVG using a Wrap File Source: https://github.com/sammycage/lunasvg/blob/master/README.md Instructions for creating a `lunasvg.wrap` file to include LunaSVG directly within your project's subprojects directory. This avoids the need for a system-wide installation. ```ini [wrap-git] url = https://github.com/sammycage/lunasvg.git revision = head depth = 1 [provide] lunasvg = lunasvg_dep ``` -------------------------------- ### Calculate Bounding Boxes for SVG Elements with LunaSVG C++ Source: https://context7.com/sammycage/lunasvg/llms.txt Provides C++ code to calculate various bounding box types for SVG elements using LunaSVG. It demonstrates how to obtain the untransformed bounding box, the local bounding box (considering the element's own transformations), and the global bounding box (including parent transformations). The example also shows how to get the bounding box of the entire SVG document. This is essential for layout calculations and positioning. ```cpp #include #include using namespace lunasvg; int main() { auto document = Document::loadFromData(R"SVG( )SVG"); auto element = document->getElementById("box"); // Get bounding box without transformations Box bbox = element.getBoundingBox(); std::cout << "Untransformed: x=" << bbox.x << " y=" << bbox.y << " w=" << bbox.w << " h=" << bbox.h << "\n"; // Get local bounding box (with element's own transform) Box localBox = element.getLocalBoundingBox(); std::cout << "Local: x=" << localBox.x << " y=" << localBox.y << " w=" << localBox.w << " h=" << localBox.h << "\n"; // Get global bounding box (with all parent transforms) Box globalBox = element.getGlobalBoundingBox(); std::cout << "Global: x=" << globalBox.x << " y=" << globalBox.y << " w=" << globalBox.w << " h=" << globalBox.h << "\n"; // Document bounding box Box docBox = document->boundingBox(); std::cout << "Document: w=" << docBox.w << " h=" << docBox.h << "\n"; return 0; } ``` -------------------------------- ### Dynamically Apply Stylesheets to SVG using LunaSVG C++ Source: https://github.com/sammycage/lunasvg/blob/master/README.md Illustrates how to load SVG content from a string and dynamically apply different stylesheets to change its appearance. The example renders a landscape SVG with both summer and winter themes, outputting 'summer.png' and 'winter.png'. ```cpp #include using namespace lunasvg; static const char kLandspaceContent[] = R"SVG( )SVG"; static const char kSummerStyle[] = R"CSS( .sky { fill: #4A90E2; } .sun { fill: #FF7F00; } .mountain { fill: #2E3A59; } .cloud { fill: #FFFFFF; opacity: 0.8; } .ground { fill: #2E8B57; } )CSS"; static const char kWinterStyle[] = R"CSS( .sky { fill: #87CEEB; } .sun { fill: #ADD8E6; } .mountain { fill: #2F4F4F; } .cloud { fill: #FFFFFF; opacity: 0.8; } .ground { fill: #FFFAFA; } )CSS"; int main() { auto document = Document::loadFromData(kLandspaceContent); document->applyStyleSheet(kSummerStyle); document->renderToBitmap().writeToPng("summer.png"); document->applyStyleSheet(kWinterStyle); document->renderToBitmap().writeToPng("winter.png"); return 0; } ``` -------------------------------- ### Include LunaSVG Dependency in Meson Project Source: https://github.com/sammycage/lunasvg/blob/master/README.md Demonstrates how to declare LunaSVG as a dependency in a Meson build file. This allows your project to link against the installed LunaSVG library. ```meson lunasvg_dep = dependency('lunasvg', required: true) ``` -------------------------------- ### Package Configuration File Generation (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/CMakeLists.txt Generates the necessary CMake package configuration files (`lunasvgConfig.cmake` and `lunasvgConfigVersion.cmake`) for installing LunaSVG. These files enable other CMake projects to find and use the installed library. ```cmake include(GNUInstallDirs) include(CMakePackageConfigHelpers) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/lunasvgConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/lunasvgConfig.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/lunasvg ) write_basic_package_version_file(lunasvgConfigVersion.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion ) ``` -------------------------------- ### Retrieve LunaSVG Dependency from Wrap Fallback Source: https://github.com/sammycage/lunasvg/blob/master/README.md Shows how to retrieve the LunaSVG dependency when using a wrap file fallback in Meson. This ensures the dependency is correctly found whether installed or included via wrap. ```meson lunasvg_dep = dependency('lunasvg', fallback: ['lunasvg', 'lunasvg_dep']) ``` -------------------------------- ### Include Directory Configuration (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/CMakeLists.txt Configures the include directories for the LunaSVG library. It distinguishes between private includes (for internal use) and public includes (for external consumption), ensuring proper header visibility during build and installation. ```cmake target_include_directories(lunasvg PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/source ) target_include_directories(lunasvg PUBLIC $ $ ) ``` -------------------------------- ### Element Selection with CSS Selectors in LunaSVG Source: https://context7.com/sammycage/lunasvg/llms.txt Shows how to query and select SVG elements using CSS selectors like class names and IDs. The example selects all elements with the class 'box', modifies them, retrieves an element by its ID, and saves the result to a PNG. ```cpp #include #include using namespace lunasvg; static const char kSVGContent[] = R"SVG( )SVG"; int main() { auto document = Document::loadFromData(kSVGContent); // Select all elements with class "box" auto boxElements = document->querySelectorAll(".box"); std::cout << "Found " << boxElements.size() << " box elements\n"; // Modify all boxes for(auto& element : boxElements) { element.setAttribute("rx", "10"); // Add rounded corners element.setAttribute("opacity", "0.7"); } // Get element by ID auto specificElement = document->getElementById("red-rect"); if(specificElement) { specificElement.setAttribute("fill", "orange"); } document->renderToBitmap().writeToPng("modified_selection.png"); return 0; } ``` -------------------------------- ### Export Target for CMake (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/CMakeLists.txt Exports the LunaSVG targets to be used by other CMake projects. This is crucial for enabling cross-project dependency management and ensuring that installed libraries can be found and linked against. ```cmake export(EXPORT lunasvgTargets FILE ${CMAKE_CURRENT_BINARY_DIR}/lunasvgTargets.cmake NAMESPACE lunasvg:: ) ``` -------------------------------- ### Manipulate SVG Element Attributes with LunaSVG C++ Source: https://context7.com/sammycage/lunasvg/llms.txt Demonstrates how to dynamically modify SVG element attributes like 'fill', 'stroke', and 'r' using LunaSVG in C++. It shows how to get an element by its ID, check for attribute existence, and set new attribute values before rendering the modified SVG to a PNG bitmap. This is useful for creating dynamic SVG visualizations. ```cpp #include using namespace lunasvg; int main() { auto document = Document::loadFromData(R"SVG( )SVG"); // Get element by ID auto circle = document->getElementById("myCircle"); // Check if attribute exists if(circle.hasAttribute("fill")) { std::string currentFill = circle.getAttribute("fill"); } // Modify attributes circle.setAttribute("fill", "red"); circle.setAttribute("stroke", "black"); circle.setAttribute("stroke-width", "5"); circle.setAttribute("r", "60"); // Increase radius // Render modified document document->renderToBitmap().writeToPng("modified_circle.png"); return 0; } ``` -------------------------------- ### Matrix Transformations for Rendering (C++) Source: https://context7.com/sammycage/lunasvg/llms.txt Illustrates the creation and manipulation of transformation matrices in C++ using LunaSVG for advanced rendering control. It covers creating identity, translated, scaled, rotated, and sheared matrices, as well as matrix multiplication, inversion, and applying transformations to a rendering. ```cpp #include using namespace lunasvg; int main() { auto document = Document::loadFromFile("input.svg"); Bitmap bitmap(800, 600); // Create identity matrix Matrix m1; // Chain transformations Matrix m2 = Matrix::translated(100, 50) .scale(1.5, 1.5) .rotate(30); // Individual transformations Matrix m3; m3.translate(200, 100); m3.scale(2.0, 2.0); m3.rotate(45, 400, 300); // Rotate around point m3.shear(0.5, 0.0); // Horizontal shear // Matrix multiplication Matrix combined = m2 * m3; // Matrix inversion Matrix inverse = combined.inverse(); // Check matrix components float a = combined.a; // Horizontal scale float b = combined.b; // Vertical shear float c = combined.c; // Horizontal shear float d = combined.d; // Vertical scale float e = combined.e; // Horizontal translation float f = combined.f; // Vertical translation // Apply to rendering document->render(bitmap, combined); bitmap.writeToPng("transformed.png"); return 0; } ``` -------------------------------- ### CMake Project Configuration and Versioning Source: https://github.com/sammycage/lunasvg/blob/master/CMakeLists.txt Sets up the minimum CMake version, defines project version components, and initializes the project with C++ language support. It also configures build-related properties like C++ standard and visibility. ```cmake cmake_minimum_required(VERSION 3.15) set(LUNASVG_VERSION_MAJOR 3) set(LUNASVG_VERSION_MINOR 5) set(LUNASVG_VERSION_MICRO 0) project(lunasvg LANGUAGES CXX VERSION ${LUNASVG_VERSION_MAJOR}.${LUNASVG_VERSION_MINOR}.${LUNASVG_VERSION_MICRO}) ``` -------------------------------- ### Document Loading from File Source: https://context7.com/sammycage/lunasvg/llms.txt Demonstrates how to load an SVG document from a file path and render it to a PNG image using the default intrinsic size of the SVG. ```APIDOC ## Document Loading from File ### Description Load an SVG document from a file path and render it to a PNG image. ### Method Document::loadFromFile(const char* path) ### Endpoint N/A (Library function) ### Parameters #### Path Parameters - **path** (string) - Required - The file path to the SVG file. ### Request Example ```cpp #include using namespace lunasvg; int main() { // Load SVG document from file auto document = Document::loadFromFile("tiger.svg"); if(document == nullptr) { return -1; // Failed to load } // Render with default dimensions (intrinsic size) auto bitmap = document->renderToBitmap(); if(bitmap.isNull()) { return -1; // Failed to render } // Save to PNG file bitmap.writeToPng("tiger.png"); return 0; } ``` ### Response #### Success Response - **Document** - A pointer to the loaded SVG document object. - **Bitmap** - A bitmap object representing the rendered SVG. #### Response Example N/A (Code example demonstrates usage and output file writing) ``` -------------------------------- ### Load and Render SVG from File to PNG (C++) Source: https://context7.com/sammycage/lunasvg/llms.txt Demonstrates loading an SVG document from a file path using LunaSVG and rendering it to a PNG bitmap. It handles potential loading and rendering errors and saves the output. Requires the lunasvg library. ```cpp #include using namespace lunasvg; int main() { // Load SVG document from file auto document = Document::loadFromFile("tiger.svg"); if(document == nullptr) { return -1; // Failed to load } // Render with default dimensions (intrinsic size) auto bitmap = document->renderToBitmap(); if(bitmap.isNull()) { return -1; // Failed to render } // Save to PNG file bitmap.writeToPng("tiger.png"); return 0; } ``` -------------------------------- ### Dynamic CSS Styling Source: https://context7.com/sammycage/lunasvg/llms.txt Shows how to apply different CSS stylesheets to the same SVG document to generate various themed outputs without reloading the SVG content. ```APIDOC ## Dynamic CSS Styling ### Description Apply different CSS stylesheets to the same SVG document to generate themed outputs. ### Method Document::applyStyleSheet(const char* css) ### Endpoint N/A (Library function) ### Parameters #### Path Parameters - **css** (string) - Required - A C-string containing the CSS rules to apply. ### Request Example ```cpp #include using namespace lunasvg; static const char kLandscapeContent[] = R"SVG( )SVG"; static const char kSummerStyle[] = R"CSS( .sky { fill: #4A90E2; } .sun { fill: #FF7F00; } .mountain { fill: #2E3A59; } .cloud { fill: #FFFFFF; opacity: 0.8; } .ground { fill: #2E8B57; } )CSS"; static const char kWinterStyle[] = R"CSS( .sky { fill: #87CEEB; } .sun { fill: #ADD8E6; } .mountain { fill: #2F4F4F; } .cloud { fill: #FFFFFF; opacity: 0.8; } .ground { fill: #FFFAFA; } )CSS"; int main() { auto document = Document::loadFromData(kLandscapeContent); // Apply summer theme and render document->applyStyleSheet(kSummerStyle); document->renderToBitmap().writeToPng("summer.png"); // Apply winter theme to same document document->applyStyleSheet(kWinterStyle); document->renderToBitmap().writeToPng("winter.png"); return 0; } ``` ### Response #### Success Response - **Bitmap** - A bitmap object representing the rendered SVG with the applied stylesheet. #### Response Example N/A (Code example demonstrates usage and output file writing) ``` -------------------------------- ### Convert SVG to PNG using svg2png Tool Source: https://github.com/sammycage/lunasvg/blob/master/README.md Demonstrates the usage of the LunaSVG command-line tool `svg2png` for converting SVG files to PNG format. It shows basic usage and options for specifying resolution and background color. ```bash svg2png [filename] [resolution] [bgColor] $ svg2png input.svg $ svg2png input.svg 512x512 $ svg2png input.svg 512x512 0xff00ffff ``` -------------------------------- ### Bitmap Manipulation with LunaSVG Source: https://context7.com/sammycage/lunasvg/llms.txt Illustrates direct bitmap creation and manipulation, including rendering an SVG to a bitmap with specified dimensions and color. It covers accessing bitmap data, converting color formats (ARGB32 Premultiplied to RGBA Plain), clearing the bitmap with a color, and writing the result to a PNG file. ```cpp #include using namespace lunasvg; int main() { auto document = Document::loadFromFile("input.svg"); // Create bitmap with specific dimensions auto bitmap = document->renderToBitmap(512, 512, 0x00000000); // Get bitmap information int width = bitmap.width(); int height = bitmap.height(); int stride = bitmap.stride(); uint8_t* data = bitmap.data(); // Convert from ARGB32 Premultiplied to RGBA Plain format bitmap.convertToRGBA(); // Clear bitmap with a specific color (red with full opacity) bitmap.clear(0xFF0000FF); // Write to PNG file if(!bitmap.writeToPng("output.png")) { return -1; // Failed to write } return 0; } ``` -------------------------------- ### DOM Tree Navigation (C++) Source: https://context7.com/sammycage/lunasvg/llms.txt Demonstrates how to navigate and manipulate the SVG document's tree structure using the LunaSVG library in C++. It shows how to load SVG from data, access the root element, iterate through children, check node types, and retrieve parent elements. ```cpp #include #include using namespace lunasvg; int main() { auto document = Document::loadFromData(R"SVG( )SVG"); // Get document root element Element root = document->documentElement(); // Get all children NodeList children = root.children(); std::cout << "Root has " << children.size() << " children\n"; for(const auto& node : children) { // Check node type if(node.isElement()) { Element elem = node.toElement(); std::cout << "Element with id: " << elem.getAttribute("id") << "\n"; // Get parent element Element parent = node.parentElement(); if(parent) { std::cout << "Parent id: " << parent.getAttribute("id") << "\n"; } } else if(node.isTextNode()) { TextNode text = node.toTextNode(); std::cout << "Text: " << text.data() << "\n"; } } return 0; } ``` -------------------------------- ### SVG Element Hit Testing with LunaSVG Source: https://github.com/sammycage/lunasvg/blob/master/README.md Demonstrates SVG element hit testing using `elementFromPoint(x, y)` in LunaSVG. It loads an SVG, performs point-based hit detection, applies transformations to matched elements, and saves the results for comparison. Dependencies include the LunaSVG library. Input is an SVG string and an array of points. Output includes console logs and modified PNG images. ```cpp #include #include #include using namespace lunasvg; static const char kSVGContent[] = R"SVG( )SVG"; int main() { auto document = Document::loadFromData(kSVGContent); document->renderToBitmap().writeToPng("original.png"); const std::pair points[] = { {30, 30}, // inside red-rect {200, 70}, // center of blue-circle {310, 50}, // inside green-rect {0, 0}, // outside all shapes }; for(const auto& [x, y] : points) { if(auto element = document->elementFromPoint(x, y)) { std::cout << "Element at (" << x << ", " << y << "): " << element.getAttribute("id") << "\n"; element.setAttribute("stroke", "black"); element.setAttribute("stroke-width", "3"); element.setAttribute("transform", "skewX(9)"); } else { std::cout << "No element found at (" << x << ", " << y << ")\n"; } } document->renderToBitmap().writeToPng("modified.png"); return 0; } ``` -------------------------------- ### Element Hit Testing with LunaSVG Source: https://context7.com/sammycage/lunasvg/llms.txt Demonstrates how to perform point-based hit detection to find SVG elements at given coordinates. It loads an SVG, defines test points, iterates through them to find intersecting elements, and optionally modifies them. The output is saved to a PNG file. ```cpp #include #include #include using namespace lunasvg; static const char kSVGContent[] = R"SVG( )SVG"; int main() { auto document = Document::loadFromData(kSVGContent); // Define test points const std::pair points[] = { {30, 30}, // inside red-rect {200, 70}, // center of blue-circle {310, 50}, // inside green-rect {0, 0}, // outside all shapes }; // Test each point for element intersection for(const auto& [x, y] : points) { if(auto element = document->elementFromPoint(x, y)) { std::cout << "Element at (" << x << ", " << y << "): " << element.getAttribute("id") << "\n"; // Modify found element element.setAttribute("stroke", "black"); element.setAttribute("stroke-width", "3"); element.setAttribute("transform", "skewX(9)"); } else { std::cout << "No element found at (" << x << ", " << y << ")\n"; } } document->renderToBitmap().writeToPng("modified.png"); return 0; } ``` -------------------------------- ### Custom Rendering with Transformation Matrices in LunaSVG Source: https://context7.com/sammycage/lunasvg/llms.txt Demonstrates how to render an SVG with custom geometric transformations applied. This involves creating a transformation matrix, defining translation, rotation, and scaling operations, and then using this matrix during the SVG rendering process to a bitmap, which is then saved to a PNG file. ```cpp #include using namespace lunasvg; int main() { auto document = Document::loadFromFile("logo.svg"); // Create a bitmap to render onto Bitmap bitmap(800, 600); bitmap.clear(0xFFFFFFFF); // White background // Create transformation matrix: translate, rotate, scale Matrix transform; transform.translate(400, 300); // Move to center transform.rotate(45); // Rotate 45 degrees transform.scale(2.0, 2.0); // Scale 2x // Render with transformation document->render(bitmap, transform); bitmap.writeToPng("transformed.png"); return 0; } ``` -------------------------------- ### Plutovg Library Build Configuration (CMake) Source: https://github.com/sammycage/lunasvg/blob/master/plutovg/CMakeLists.txt Defines the plutovg library, lists its source and header files, sets compiler standards and visibility, and configures include directories. It also handles linking against system libraries like math and threads. ```cmake cmake_minimum_required(VERSION 3.15) set(PLUTOVG_VERSION_MAJOR 1) set(PLUTOVG_VERSION_MINOR 3) set(PLUTOVG_VERSION_MICRO 2) project(plutovg LANGUAGES C VERSION ${PLUTOVG_VERSION_MAJOR}.${PLUTOVG_VERSION_MINOR}.${PLUTOVG_VERSION_MICRO}) set(plutovg_sources source/plutovg-blend.c source/plutovg-canvas.c source/plutovg-font.c source/plutovg-matrix.c source/plutovg-paint.c source/plutovg-path.c source/plutovg-rasterize.c source/plutovg-surface.c source/plutovg-ft-math.c source/plutovg-ft-raster.c source/plutovg-ft-stroker.c ) set(plutovg_headers include/plutovg.h source/plutovg-private.h source/plutovg-utils.h source/plutovg-ft-math.h source/plutovg-ft-raster.h source/plutovg-ft-stroker.h source/plutovg-ft-types.h source/plutovg-stb-image-write.h source/plutovg-stb-image.h source/plutovg-stb-truetype.h ) add_library(plutovg ${plutovg_sources} ${plutovg_headers}) add_library(plutovg::plutovg ALIAS plutovg) include(GNUInstallDirs) include(CMakePackageConfigHelpers) include(CheckIncludeFile) set_target_properties(plutovg PROPERTIES SOVERSION ${PLUTOVG_VERSION_MAJOR} C_VISIBILITY_PRESET hidden C_STANDARD_REQUIRED ON C_STANDARD 11 ) target_include_directories(plutovg PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/source ) target_include_directories(plutovg PUBLIC $ $ ) find_library(MATH_LIBRARY m) if(MATH_LIBRARY) target_link_libraries(plutovg PRIVATE m) endif() check_include_file("threads.h" HAVE_THREADS_H) if(HAVE_THREADS_H) target_compile_definitions(plutovg PRIVATE HAVE_THREADS_H) endif() find_package(Threads) if(Threads_FOUND) target_link_libraries(plutovg PRIVATE Threads::Threads) endif() find_library(STDTHREADS_LIBRARY stdthreads) if(STDTHREADS_LIBRARY) target_link_libraries(plutovg PRIVATE stdthreads) endif() target_compile_definitions(plutovg PRIVATE PLUTOVG_BUILD) if(NOT BUILD_SHARED_LIBS) target_compile_definitions(plutovg PUBLIC PLUTOVG_BUILD_STATIC) endif() option(PLUTOVG_DISABLE_FONT_FACE_CACHE_LOAD "Disable loading font face cache from files and directories" OFF) if(PLUTOVG_DISABLE_FONT_FACE_CACHE_LOAD) target_compile_definitions(plutovg PRIVATE PLUTOVG_DISABLE_FONT_FACE_CACHE_LOAD) endif() ``` -------------------------------- ### Document Loading from String Data Source: https://context7.com/sammycage/lunasvg/llms.txt Illustrates loading SVG content directly from a string variable, allowing for in-memory SVG manipulation and rendering with custom dimensions and background colors. ```APIDOC ## Document Loading from String Data ### Description Load SVG content from memory using string data and render it to a PNG image with custom dimensions and background color. ### Method Document::loadFromData(const char* data) ### Endpoint N/A (Library function) ### Parameters #### Path Parameters - **data** (string) - Required - A C-string containing the SVG content. #### Query Parameters - **width** (integer) - Optional - The desired width for rendering the bitmap. Defaults to intrinsic SVG width. - **height** (integer) - Optional - The desired height for rendering the bitmap. Defaults to intrinsic SVG height. - **backgroundColor** (uint32_t) - Optional - The background color in ARGB32 Premultiplied format (e.g., 0xFFFFFFFF for white). Defaults to transparent. ### Request Example ```cpp #include using namespace lunasvg; static const char kSVGContent[] = R"SVG( )SVG"; int main() { // Load from string data auto document = Document::loadFromData(kSVGContent); if(document == nullptr) { return -1; } // Render with custom dimensions and background color // Width: 800px, Height: 400px, Background: white (0xFFFFFFFF) auto bitmap = document->renderToBitmap(800, 400, 0xFFFFFFFF); bitmap.writeToPng("output.png"); return 0; } ``` ### Response #### Success Response - **Document** - A pointer to the loaded SVG document object. - **Bitmap** - A bitmap object representing the rendered SVG. #### Response Example N/A (Code example demonstrates usage and output file writing) ``` -------------------------------- ### Command Line SVG to PNG Converter (C++) Source: https://context7.com/sammycage/lunasvg/llms.txt A C++ command-line tool that converts SVG files to PNG format. It supports custom resolution (width and height) and background color specified in hexadecimal. The tool uses the LunaSVG library for SVG parsing and rendering. ```cpp #include #include #include using namespace lunasvg; int main(int argc, char* argv[]) { // Default values std::string filename; std::uint32_t width = 0, height = 0; std::uint32_t bgColor = 0x00000000; // Transparent // Parse command line arguments if(argc > 1) filename.assign(argv[1]); if(argc > 2) { std::stringstream ss; ss << argv[2]; ss >> width; if(ss.fail() || ss.get() != 'x') return -1; ss >> height; } if(argc > 3) { std::stringstream ss; ss << std::hex << argv[3]; ss >> std::hex >> bgColor; } // Load and render auto document = Document::loadFromFile(filename); if(document == nullptr) { std::cerr << "Failed to load: " << filename << "\n"; return -1; } // width=0 or height=0 means use intrinsic size auto bitmap = document->renderToBitmap(width, height, bgColor); if(bitmap.isNull()) { std::cerr << "Failed to render\n"; return -1; } // Generate output filename auto lastSlash = filename.find_last_of("/\\"); auto basename = lastSlash == std::string::npos ? filename : filename.substr(lastSlash + 1); basename.append(".png"); bitmap.writeToPng(basename); std::cout << "Generated: " << basename << std::endl; return 0; } ``` -------------------------------- ### Load and Render SVG from String to PNG (C++) Source: https://context7.com/sammycage/lunasvg/llms.txt Shows how to load SVG content directly from a string using LunaSVG. The code renders the SVG to a PNG image with specified dimensions and a background color. It requires the lunasvg library and handles loading failures. ```cpp #include using namespace lunasvg; static const char kSVGContent[] = R"SVG( )SVG"; int main() { // Load from string data auto document = Document::loadFromData(kSVGContent); if(document == nullptr) { return -1; } // Render with custom dimensions and background color // Width: 800px, Height: 400px, Background: white (0xFFFFFFFF) auto bitmap = document->renderToBitmap(800, 400, 0xFFFFFFFF); bitmap.writeToPng("output.png"); return 0; } ```