- Core library with SVG and PNG generation - CLI tool with generate and batch commands - Cross-platform path handling for Windows compatibility - Comprehensive test suite with integration tests
59 lines
2.4 KiB
Go
59 lines
2.4 KiB
Go
/*
|
|
Package renderer is responsible for translating the intermediate representation of an
|
|
identicon (generated by the `engine` package) into a final output format, such
|
|
as SVG or PNG.
|
|
|
|
This package is internal to the jdenticon library and its API is not guaranteed
|
|
to be stable. Do not use it directly.
|
|
|
|
# Core Concept: The Renderer Interface
|
|
|
|
The central component of this package is the `Renderer` interface. It defines a
|
|
contract for any format-specific renderer. The primary method, `Render`, takes a
|
|
list of shapes and colors and writes the output to an `io.Writer`.
|
|
|
|
This interface-based approach makes the system extensible. To add a new output
|
|
format (e.g., WebP), one would simply need to create a new struct that implements
|
|
the `Renderer` interface.
|
|
|
|
# Implementations
|
|
|
|
- svg.go: Provides `SvgRenderer`, which generates a vector-based SVG image. It
|
|
builds an XML tree representing the SVG structure and writes it out. This
|
|
renderer is highly efficient and produces scalable output that maintains
|
|
visual compatibility with the JavaScript Jdenticon library.
|
|
|
|
- png.go: Provides `PngRenderer`, which generates a raster-based PNG image. It
|
|
utilizes Go's standard `image` and `image/draw` packages to draw the shapes onto
|
|
a canvas and then encodes the result as a PNG.
|
|
|
|
- fast_png.go: Provides `FastPngRenderer`, an optimized PNG implementation that
|
|
uses more efficient drawing algorithms for better performance in high-throughput
|
|
scenarios.
|
|
|
|
# Rendering Pipeline
|
|
|
|
The rendering process follows this flow:
|
|
|
|
1. The engine package generates `RenderedElement` structures containing shape
|
|
geometries and color information.
|
|
|
|
2. A renderer implementation receives this intermediate representation along with
|
|
size and configuration parameters.
|
|
|
|
3. The renderer translates the abstract shapes into format-specific commands
|
|
(SVG paths, PNG pixel operations, etc.).
|
|
|
|
4. The final output is written to the provided `io.Writer`, allowing for
|
|
flexible destination handling (files, HTTP responses, etc.).
|
|
|
|
# Performance Considerations
|
|
|
|
The renderers are designed for efficiency:
|
|
- SVG rendering uses a `strings.Builder` for efficient, low-allocation string construction
|
|
- PNG rendering includes both standard and fast implementations
|
|
- All renderers support concurrent use across multiple goroutines
|
|
- Memory allocations are minimized through object reuse where possible
|
|
*/
|
|
package renderer
|