Class GeneratedSVGImage
- All Implemented Interfaces:
ActionSource
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 timepaintSVG(Graphics, long)is 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).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intSentinel value used byprogress(long, long, long, int, boolean)when an animation declaredrepeatCount="indefinite". -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedGeneratedSVGImage(int intrinsicWidth, int intrinsicHeight, float viewBoxX, float viewBoxY, float viewBoxWidth, float viewBoxHeight, boolean animated) Construct with intrinsic SVG dimensions and viewBox metadata.protectedGeneratedSVGImage(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.protectedGeneratedSVGImage(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 viaDisplay.convertToPixels(float)so the dimensions carry across DPIs the same wayfont-size: 3mmdoes. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleananimate()Returningtruerequests a repaint on the next animation tick so the embedding component re-reads the SMIL clock.protected final voidCallback invoked internally by Codename One to draw the image/frame onto the display.protected final voidCallback invoked internally by Codename One to draw the image/frame onto the display.final intReturns the height of the imagefinal intThe intrinsic SVG height before DPI scaling.final intThe intrinsic SVG width before DPI scaling -- useful for tests or callers that want to apply a different sizing heuristic.final intReturns the source density the SVG was designed for, inCN1Constants.DENSITY_*units.final intgetWidth()Returns the width of the imagefinal booleanReturns true if this is an animated imagestatic floatlerp(float from, float to, float t) static intlerpColor(int fromArgb, int toArgb, float t) Lerp between two ARGB colors.static floatlerpValues(float[] values, float t) Multi-stop floating point lerp.static intmmToPixels(float mm) Convert a length in millimeters to device pixels using the currentDisplayDPI.protected abstract voidGenerated implementations render the SVG content using the supplied graphics context.static floatprogress(long elapsedMs, long beginMs, long durMs, int repeatCount, boolean freeze) Compute the active progress through an animation cycle, in the range[0, 1].final voidReset the per-instance animation start so the next paint begins att = 0.final Imagescaled(int width, int height) Returns a lightweight view onto this image that reportswidth/heightfromgetWidth()/getHeight()but reuses the underlying rendering.static voidsvgArc(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).static FontsvgTextFont(float sizePixels, int styleBits) Build a font for a generated SVG<text>element.Methods inherited from class Image
addActionListener, applyMask, applyMask, applyMaskAutoScale, asyncLock, createImage, createImage, createImage, createImage, createImage, createImage, createImage, createIndexed, createMask, createSVG, dispose, exifRotation, exifRotation, exifRotation, fill, fireChangedEvent, flipHorizontally, flipVertically, getExifOrientationTag, getExifOrientationTag, getGraphics, getImage, getImageName, getRGB, getRGB, getRGBCached, getSVGDocument, isAlphaMutableImageSupported, isJPEG, isLocked, isOpaque, isPNG, isSimdOptimizationsEnabled, isSVG, isSVGSupported, lock, mirror, modifyAlpha, modifyAlpha, modifyAlphaWithTranslucency, removeActionListener, requiresDrawImage, resetSimdOptimizationsEnabled, rotate, rotate180Degrees, rotate270Degrees, rotate90Degrees, scale, scaledHeight, scaledLargerRatio, scaledSmallerRatio, scaledWidth, setImageName, setSimdOptimizationsEnabled, subImage, toRGB, unlock
-
Field Details
-
REPEAT_INDEFINITE
public static final int REPEAT_INDEFINITESentinel value used byprogress(long, long, long, int, boolean)when an animation declaredrepeatCount="indefinite".- See Also:
-
-
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: SVG-declared width in design pixels -
intrinsicHeight: SVG-declared height in design pixels -
viewBoxX: x origin of the viewBox in SVG user units -
viewBoxY: y origin of the viewBox in SVG user units -
viewBoxWidth: width of the viewBox; falls back tointrinsicWidthif<= 0 -
viewBoxHeight: height of the viewBox; falls back tointrinsicHeightif<= 0 -
animated: 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
SVGRegistrytargets when the CSS for an SVG declared acn1-source-dpi-- the runtime width/height then track that hint instead of assumingCN1Constants.DENSITY_MEDIUM.Parameters
sourceDensity: one of theCN1Constants.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) 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 wayfont-size: 3mmdoes. This is the constructor theuses when the CSS rule specifiedinvalid reference
SVGRegistrycn1-svg-width/cn1-svg-height, overriding any density-based sizing.Parameters
-
explicitWidth: rendered width in device pixels (>= 1) -
explicitHeight: 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 currentDisplayDPI. Provided as a static helper for the generated subclass constructors that accept mm-typed dimensions. ThrowsIllegalArgumentExceptionformm <= 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 whenDisplayis not initialized (e.g. during unit tests that construct an image beforeDisplay.init()has run). -
getWidth
-
getHeight
-
isAnimation
public final boolean isAnimation()Description copied from class:ImageReturns true if this is an animated image
Returns
true if this image represents an animation
- Overrides:
isAnimationin classImage
-
animate
public final boolean animate()Returningtruerequests a repaint on the next animation tick so the embedding component re-reads the SMIL clock. The animation state itself is re-derived fromAnimationTime.now()on each paint, so there is nothing to advance imperatively. -
paintSVG
Generated implementations render the SVG content using the supplied graphics context.
elapsedMsis the number of milliseconds since the first paint of this image instance, measured againstAnimationTime.now()so test code can pin the clock.Parameters
-
g: the graphics context, already transformed so SVG user-space coordinates map onto the destination rectangle -
elapsedMs: animation time in milliseconds,0for non-animated SVGs
-
-
drawImage
Description copied from class:ImageCallback 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: the graphics object -
nativeGraphics: the underlying native graphics which might be essential for some image types -
x: the x coordinate -
y: the y coordinate
-
-
drawImage
Description copied from class:ImageCallback 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: the graphics object -
nativeGraphics: the underlying native graphics which might be essential for some image types -
x: the x coordinate -
y: the y coordinate -
w: the width to occupy -
h: the height to occupy
-
-
scaled
Returns a lightweight view onto this image that reportswidth/heightfromgetWidth()/getHeight()but reuses the underlying rendering. The returned image's animation clock is shared with the source so progress is consistent across both views. -
resetAnimation
public final void resetAnimation()Reset the per-instance animation start so the next paint begins att = 0. Tests typically preferAnimationTime.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, inCN1Constants.DENSITY_*units.0means "no scaling, use intrinsic pixels". CSS can override the default viacn1-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
Build a font for a generated SVG<text>element. Always loads anative: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 ofpis 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.
-