public abstract class GeneratedSVGImage

  1. Object
  2. Image
  3. 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:

  1. 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.

  2. DPI-aware default sizing – the SVG’s declared width/height are interpreted as design pixels at CN1Constants.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 with scaled(int, int) or by passing explicit dimensions on construction.

  3. Deterministic animation time – animation progress is read from AnimationTime.now(), so tests that pin the clock with AnimationTime.setTime(long) capture predictable frames. The “first paint” timestamp is captured the first time paintSVG is invoked, making animations start at t = 0 from 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 = -1Sentinel 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

Field details

REPEAT_INDEFINITE

public static final int REPEAT_INDEFINITE = -1
Sentinel value used by progress 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)
Construct with intrinsic SVG dimensions and viewBox metadata. The rendered size defaults to the intrinsic dimensions scaled by the device’s density (so SVGs designed at standard mdpi look correct on every screen). Use scaled(int, int) for explicit pixel sizing.

Parameters

intrinsicWidth int
SVG-declared width in design pixels
intrinsicHeight int
SVG-declared height in design pixels
viewBoxX float
x origin of the viewBox in SVG user units
viewBoxY float
y origin of the viewBox in SVG user units
viewBoxWidth float
width of the viewBox; falls back to intrinsicWidth if <= 0
viewBoxHeight float
height of the viewBox; falls back to intrinsicHeight if <= 0
animated boolean
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)
Construct with an explicit source density for the SVG’s declared dimensions. This is the constructor the auto-generated 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

intrinsicWidth int
Not documented.
intrinsicHeight int
Not documented.
viewBoxX float
Not documented.
viewBoxY float
Not documented.
viewBoxWidth float
Not documented.
viewBoxHeight float
Not documented.
animated boolean
Not documented.
sourceDensity int
one of the CN1Constants.DENSITY_* constants, describing the device class the SVG was designed for. A value of 0 falls 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)
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. This is the constructor the SVGRegistry uses when the CSS rule specified cn1-svg-width / cn1-svg-height, overriding any density-based sizing.

Parameters

intrinsicWidth int
Not documented.
intrinsicHeight int
Not documented.
viewBoxX float
Not documented.
viewBoxY float
Not documented.
viewBoxWidth float
Not documented.
viewBoxHeight float
Not documented.
animated boolean
Not documented.
explicitWidth int
rendered width in device pixels (>= 1)
explicitHeight int
rendered height in device pixels (>= 1)

Method details

mmToPixels

public static int mmToPixels(float mm)
Convert a length in millimeters to device pixels using the current 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 the width of the image

Returns

the width of the image

getHeight

public final int getHeight()
Returns the height of the image

Returns

the height of the image

isAnimation

public final boolean isAnimation()
Returns true if this is an animated image

Returns

true if this image represents an animation

animate

public final boolean animate()
Returning 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)
Generated implementations render the SVG content using the supplied graphics context. 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

g Graphics
the graphics context, already transformed so SVG user-space coordinates map onto the destination rectangle
elapsedMs long
animation time in milliseconds, 0 for non-animated SVGs

drawImage

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. Image subclasses can override this method to perform drawing of custom image types.

Parameters

g Graphics
the graphics object
nativeGraphics Object
the underlying native graphics which might be essential for some image types
x int
the x coordinate
y int
the y coordinate

drawImage

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. Image subclasses can override this method to perform drawing of custom image types.

Parameters

g Graphics
the graphics object
nativeGraphics Object
the underlying native graphics which might be essential for some image types
x int
the x coordinate
y int
the y coordinate
w int
the width to occupy
h int
the height to occupy

scaled

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. The returned image’s animation clock is shared with the source so progress is consistent across both views.

Parameters

width int
width for the scaling
height int
height of the scaled image

Returns

new image instance scaled to the given height and width

resetAnimation

public final void resetAnimation()
Reset the per-instance animation start so the next paint begins at t = 0. Tests typically prefer AnimationTime.setTime(long) instead, which controls every animation in the VM at once.

getIntrinsicWidth

public final int getIntrinsicWidth()
The intrinsic SVG width before DPI scaling – useful for tests or callers that want to apply a different sizing heuristic.

getIntrinsicHeight

public final int getIntrinsicHeight()
The intrinsic SVG height before DPI scaling.

getSourceDensity

public final int getSourceDensity()
Returns the source density the SVG was designed for, in 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)
Compute the active progress through an animation cycle, in the range [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)
Lerp between two ARGB colors. Each channel is linearly interpolated.

lerpValues

public static float lerpValues(float[] values, float t)
Multi-stop floating point lerp. Stops are evenly spaced in [0, 1].

svgTextFont

public static Font svgTextFont(float sizePixels, int styleBits)
Build a font for a generated SVG <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)
Append an SVG elliptical arc segment to the given path using the endpoint parameterization defined by the SVG 1.1 spec (appendix F.6). The current point of 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.