|
SVG for C++
|
See the C++ code for this logo
This a header-only library for generating SVG files from a simple C++ interface. It can also perform non-trivial tasks such as calculating a bounding box for an SVG's elements, or merging several graphics together.
Want to see more? Read the documentation.
autoscale() measures element geometry, including simple SVG rotate(...) transforms, visible element stroke-width attributes, local <use> references, and approximate Text bounds from x, y, dx, dy, font-size, text-anchor, and baseline attributes. It sets width, height, and viewBox; use responsive_autoscale() when the SVG should calculate only viewBox and let its container control display size. It does not inspect CSS transforms, rendered effects such as filters or shadows, stroke joins, font metrics, CSS class rules, media queries, or external stylesheets; put autoscale-critical stroke widths on elements or pass SVG::Margins when the drawing needs extra page space for those effects.
Nested SVG::SVG elements are supported as normal SVG viewports. By default, autoscale() first autoscales nested SVG children, then measures their viewport boxes in the parent coordinate system. Use AutoscaleOptions to preserve existing nested viewport sizes.
Use set_vars() and SVG::Classes when a drawing has reusable theme tokens or repeated class names. Variable names are normalized with a leading --, class selectors can be built without hand-writing ".", and vars.format() is useful for CSS expressions that combine several variables.
Attribute names are stored and looked up case-sensitively, matching SVG/XML behavior. Use the exact SVG spelling for mixed-case names such as viewBox; viewbox is a separate attribute and will not be used by SVG viewers as a substitute.
class_list() treats the class attribute as a normalized token list, so repeated whitespace and duplicate class tokens are cleaned up. Use get_elements_by_class() when you want token-aware matches instead of substring checks.
Range-for over any SVG element visits that element and its descendants in depth-first document order. Use descendants() when you want to skip the current element. The iterator also exposes the accumulated supported SVG transform for each visited element through it.transform().
For type-specific searches, get_children<T>() returns matching descendants and get_immediate_children<T>() limits the lookup to direct children.
Use defs() helpers to define paint servers without manually constructing XML-style gradient elements. The helpers reuse an existing definition when the id is already present.
Use autoscale() when the SVG should set its own display width, height, and viewBox. Use responsive_autoscale() when you want only the viewBox updated, leaving sizing to CSS or the embedding page.
When an element uses SVG features the library cannot measure well enough, provide an explicit layout bounding box for autoscale without changing the element's own get_bbox() result.
Text layout bounds are estimates, not exact font shaping or browser measurements. The built-in estimator is UTF-8 aware, treats combining marks as zero-width, and uses conservative advances for ASCII, CJK/full-width text, emoji, and bold text. If an element only needs a little extra layout space, use bbox_padding(); an explicit layout_bbox() still takes precedence.
Use snap_to() to position measured elements against each other with SVG transforms. Passing only RelativeAlignment uses Anchor::Center, so offsets do not require spelling out the center anchor. Combine the two enums with | when you need start or end alignment along the shared edge. Use align_to() when elements should share an axis without becoming neighbors.
Use rotate_about_bbox() when a label or annotation should rotate around a measured corner or center while still using a normal SVG transform attribute that autoscale can measure.
This package supports creating basic animations via CSS keyframes via the frame_animate() function.