public abstract class GeneratedSVGImage
ImplementsActionSource
Base class for SVG images emitted by the build-time transcoder
(maven/svg-transcoder).
A subclass is generated per source SVG file. The subclass overrides
paintSVG(Graphics, long) to issue the actual drawing commands and
passes its intrinsic dimensions and viewBox to the constructor. This class
handles three concerns the generated code should not have to think about:
Viewport mapping – the viewBox is scaled into the destination rectangle on every paint, so the same generated class can render at any requested width/height without re-emitting code.
DPI-aware default sizing – the SVG’s declared
width/heightare interpreted as design pixels atCN1Constants.DENSITY_MEDIUM(the same convention CN1 uses for its multi-image density buckets) and scaled to the device density so an icon that looks “right” on a desktop simulator also looks right on a high-DPI handset. Override withscaled(int, int)or by passing explicit dimensions on construction.Deterministic animation time – animation progress is read from
AnimationTime.now(), so tests that pin the clock withAnimationTime.setTime(long)capture predictable frames. The “first paint” timestamp is captured the first timepaintSVGis invoked, making animations start att = 0from the user’s perspective regardless of when the image instance was constructed.
Generated SVGs are normally registered with a Resources
object via the auto-generated com.codename1.generated.svg.SVGRegistry so
they appear under their original filename when calling
Resources.getImage(String).
Fields
public static final int REPEAT_INDEFINITE = -1 | Sentinel value used by progress when an animation declared repeatCount="indefinite". |
Constructors
protected GeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated) | Construct with intrinsic SVG dimensions and viewBox metadata. |
protected GeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated, int sourceDensity) | Construct with an explicit source density for the SVG’s declared dimensions. |
protected GeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated, int explicitWidth, int explicitHeight) | Construct with explicit absolute width/height – expressed in device pixels here, but the auto-generated subclass takes them in millimeters and converts via Display.convertToPixels(float) so the dimensions carry across DPIs the same way font-size: 3mm does. |
Methods
public static int mmToPixels(float mm) | Convert a length in millimeters to device pixels using the current Display DPI. |
public final int getWidth() | Returns the width of the image |
public final int getHeight() | Returns the height of the image |
public final boolean isAnimation() | Returns true if this is an animated image |
public final boolean animate() | Returning true requests a repaint on the next animation tick so the embedding component re-reads the SMIL clock. |
protected abstract void paintSVG(Graphics g, long elapsedMs) | Generated implementations render the SVG content using the supplied graphics context. |
protected final void drawImage(Graphics g, Object nativeGraphics, int x, int y) | Callback invoked internally by Codename One to draw the image/frame onto the display. |
protected final void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h) | Callback invoked internally by Codename One to draw the image/frame onto the display. |
public final Image scaled(int width, int height) | Returns a lightweight view onto this image that reports width / height from getWidth / getHeight but reuses the underlying rendering. |
public final void resetAnimation() | Reset the per-instance animation start so the next paint begins at t = 0. |
public final int getIntrinsicWidth() | The intrinsic SVG width before DPI scaling – useful for tests or callers that want to apply a different sizing heuristic. |
public final int getIntrinsicHeight() | The intrinsic SVG height before DPI scaling. |
public final int getSourceDensity() | Returns the source density the SVG was designed for, in CN1Constants.DENSITY_* units. |
public static float progress(long elapsedMs, long beginMs, long durMs, int repeatCount, boolean freeze) | Compute the active progress through an animation cycle, in the range [0, 1]. |
public static float lerp(float from, float to, float t) | |
public static int lerpColor(int fromArgb, int toArgb, float t) | Lerp between two ARGB colors. |
public static float lerpValues(float[] values, float t) | Multi-stop floating point lerp. |
public static Font svgTextFont(float sizePixels, int styleBits) | Build a font for a generated SVG <text> element. |
public static void svgArc(GeneralPath p, float x1, float y1, float rx, float ry, float xAxisRotationDeg, boolean largeArc, boolean sweep, float x2, float y2) | Append an SVG elliptical arc segment to the given path using the endpoint parameterization defined by the SVG 1.1 spec (appendix F.6). |
Inherited methods
From Image
isSimdOptimizationsEnabled, setSimdOptimizationsEnabled, resetSimdOptimizationsEnabled, isSVGSupported, createSVG, createIndexed, createImage, createImage, createImage, createImage, createImage, isAlphaMutableImageSupported, createImage, createImage, exifRotation, exifRotation, exifRotation, getExifOrientationTag, getExifOrientationTag, isJPEG, isPNG, asyncLock, lock, isLocked, unlock, getSVGDocument, isSVG, createMask, applyMask, applyMask, applyMaskAutoScale, subImage, mirror, rotate, modifyAlpha, modifyAlphaWithTranslucency, modifyAlpha, getGraphics, toRGB, getRGB, getRGB, getRGBCached, scaledWidth, scaledHeight, scaledSmallerRatio, scaledLargerRatio, fill, getImage, scale, isOpaque, getImageName, setImageName, dispose, rotate90Degrees, rotate180Degrees, rotate270Degrees, flipHorizontally, flipVertically, requiresDrawImage, addActionListener, removeActionListener, fireChangedEvent
Field details
REPEAT_INDEFINITE
public static final int REPEAT_INDEFINITE = -1progress when an animation declared
repeatCount="indefinite".Constructor details
GeneratedSVGImage
protected GeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated)scaled(int, int) for explicit pixel sizing.Parameters
intrinsicWidthint- SVG-declared width in design pixels
intrinsicHeightint- SVG-declared height in design pixels
viewBoxXfloat- x origin of the viewBox in SVG user units
viewBoxYfloat- y origin of the viewBox in SVG user units
viewBoxWidthfloat- width of the viewBox; falls back to
intrinsicWidthif<= 0 viewBoxHeightfloat- height of the viewBox; falls back to
intrinsicHeightif<= 0 animatedboolean- true if any SMIL animation was found inside the SVG
GeneratedSVGImage
protected GeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated, int sourceDensity)SVGRegistry
targets when the CSS for an SVG declared a cn1-source-dpi – the
runtime width/height then track that hint instead of assuming
CN1Constants.DENSITY_MEDIUM.Parameters
intrinsicWidthint- Not documented.
intrinsicHeightint- Not documented.
viewBoxXfloat- Not documented.
viewBoxYfloat- Not documented.
viewBoxWidthfloat- Not documented.
viewBoxHeightfloat- Not documented.
animatedboolean- Not documented.
sourceDensityint- one of the
CN1Constants.DENSITY_*constants, describing the device class the SVG was designed for. A value of0falls back to the SVG’s intrinsic pixels (no scaling).
GeneratedSVGImage
protected GeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated, int explicitWidth, int explicitHeight)Display.convertToPixels(float) so the dimensions
carry across DPIs the same way font-size: 3mm does. This is the
constructor the SVGRegistry uses when the CSS rule specified
cn1-svg-width / cn1-svg-height, overriding any density-based
sizing.Parameters
intrinsicWidthint- Not documented.
intrinsicHeightint- Not documented.
viewBoxXfloat- Not documented.
viewBoxYfloat- Not documented.
viewBoxWidthfloat- Not documented.
viewBoxHeightfloat- Not documented.
animatedboolean- Not documented.
explicitWidthint- rendered width in device pixels (
>= 1) explicitHeightint- rendered height in device pixels (
>= 1)
Method details
mmToPixels
public static int mmToPixels(float mm)Display DPI. Provided as a static helper for the generated subclass
constructors that accept mm-typed dimensions. Throws
IllegalArgumentException for mm <= 0 – callers should never pin
an SVG to a zero-or-negative physical size. Falls back to treating
the input as a literal pixel count when Display is not initialized
(e.g. during unit tests that construct an image before
Display.init() has run).getWidth
public final int getWidth()Returns
getHeight
public final int getHeight()Returns
isAnimation
public final boolean isAnimation()Returns
animate
public final boolean animate()true requests a repaint on the next animation tick so the
embedding component re-reads the SMIL clock. The animation state itself
is re-derived from AnimationTime.now() on each paint, so there is
nothing to advance imperatively.Returns
true if the animation state changed.paintSVG
protected abstract void paintSVG(Graphics g, long elapsedMs)elapsedMs is the number of milliseconds since the
first paint of this image instance, measured against
AnimationTime.now() so test code can pin the clock.Parameters
gGraphics- the graphics context, already transformed so SVG user-space coordinates map onto the destination rectangle
elapsedMslong- animation time in milliseconds,
0for non-animated SVGs
drawImage
protected final void drawImage(Graphics g, Object nativeGraphics, int x, int y)Parameters
gGraphics- the graphics object
nativeGraphicsObject- the underlying native graphics which might be essential for some image types
xint- the x coordinate
yint- the y coordinate
drawImage
protected final void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h)Parameters
gGraphics- the graphics object
nativeGraphicsObject- the underlying native graphics which might be essential for some image types
xint- the x coordinate
yint- the y coordinate
wint- the width to occupy
hint- the height to occupy
scaled
public final Image scaled(int width, int height)width /
height from getWidth / getHeight but reuses the underlying
rendering. The returned image’s animation clock is shared with the
source so progress is consistent across both views.Parameters
widthint- width for the scaling
heightint- height of the scaled image
Returns
resetAnimation
public final void resetAnimation()t = 0. Tests typically prefer AnimationTime.setTime(long) instead,
which controls every animation in the VM at once.getIntrinsicWidth
public final int getIntrinsicWidth()getIntrinsicHeight
public final int getIntrinsicHeight()getSourceDensity
public final int getSourceDensity()CN1Constants.DENSITY_* units. 0 means “no scaling, use intrinsic
pixels”. CSS can override the default via cn1-source-dpi:.progress
public static float progress(long elapsedMs, long beginMs, long durMs, int repeatCount, boolean freeze)[0, 1]. Honors begin offsets, repeat counts and the SMIL fill=“freeze”
behavior. Generated code calls this per animated attribute per paint.lerp
public static float lerp(float from, float to, float t)lerpColor
public static int lerpColor(int fromArgb, int toArgb, float t)lerpValues
public static float lerpValues(float[] values, float t)[0, 1].svgTextFont
public static Font svgTextFont(float sizePixels, int styleBits)<text> element. Always loads a
native: TrueType face (HelveticaNeue on iOS, Roboto on Android,
the platform default elsewhere) so the size argument is honored at
arbitrary pixel resolutions. Weight / style maps to the matching
face name because some platforms ignore the derive weight argument.svgArc
public static void svgArc(GeneralPath p, float x1, float y1, float rx, float ry, float xAxisRotationDeg, boolean largeArc, boolean sweep, float x2, float y2)p is treated as the arc’s start; on return the
current point is the end of the arc. Decomposes into up to four
cubic Beziers – a single quadrant per Bezier – for accuracy.