Class GeneratedSVGImage

java.lang.Object
com.codename1.ui.Image
com.codename1.ui.GeneratedSVGImage
All Implemented Interfaces:
ActionSource

public abstract class GeneratedSVGImage extends Image

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(Graphics, long) 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).

  • Field Details

  • 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 to intrinsicWidth if <= 0

      • viewBoxHeight: height of the viewBox; falls back to intrinsicHeight if <= 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 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
      • sourceDensity: 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

      invalid reference
      SVGRegistry
      uses when the CSS rule specified

      cn1-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 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()
      Description copied from class: Image

      Returns the width of the image

      Returns

      the width of the image

      Overrides:
      getWidth in class Image
    • getHeight

      public final int getHeight()
      Description copied from class: Image

      Returns the height of the image

      Returns

      the height of the image

      Overrides:
      getHeight in class Image
    • isAnimation

      public final boolean isAnimation()
      Description copied from class: Image

      Returns true if this is an animated image

      Returns

      true if this image represents an animation

      Overrides:
      isAnimation in class Image
    • 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.
      Overrides:
      animate in class Image
    • 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: the graphics context, already transformed so SVG user-space coordinates map onto the destination rectangle

      • elapsedMs: animation time in milliseconds, 0 for non-animated SVGs

    • drawImage

      protected final void drawImage(Graphics g, Object nativeGraphics, int x, int y)
      Description copied from class: Image

      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: the graphics object

      • nativeGraphics: the underlying native graphics which might be essential for some image types

      • x: the x coordinate

      • y: the y coordinate

      Overrides:
      drawImage in class Image
    • drawImage

      protected final void drawImage(Graphics g, Object nativeGraphics, int x, int y, int w, int h)
      Description copied from class: Image

      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: 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

      Overrides:
      drawImage in class Image
    • 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.
      Overrides:
      scaled in class Image
    • 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.