public class TextCanvas extends TexturedRectangle
A text rendering surface in 3D space that displays a grid of characters.

TextCanvas extends TexturedRectangle and renders a 2D grid of characters (rows and columns) onto a texture-mapped rectangle. Each character cell supports independent foreground and background colors.

Characters are rendered using a monospace font at a fixed cell size (16 x 32 texture pixels per character). The texture is a signed distance field (SDF): the mask stores per-texel distance to the nearest glyph edge (generated once per character by SdfGlyphCache and stamped per cell), while the primary bitmap and a separate foreground layer carry the cell colors. The rasterizer re-derives glyph edges per screen pixel from the distance field, so text stays sharp at any zoom and any angle from a single render path.

Usage example


 Transform location = new Transform(new Point3D(0, 0, 500));
 TextCanvas canvas = new TextCanvas(location, "Hello, World!",
         Color.WHITE, Color.BLACK);
 shapeCollection.addShape(canvas);

 // Or create a blank canvas and write to it
 TextCanvas blank = new TextCanvas(location, new TextPointer(10, 40),
         Color.GREEN, Color.BLACK);
 blank.locate(0, 0);
 blank.print("Line 1");
 blank.locate(1, 0);
 blank.print("Line 2");
 
See Also:
  • Field Details

    • FONT_CHAR_WIDTH

      public static final int FONT_CHAR_WIDTH
      Font character width in world coordinates.
      See Also:
    • FONT_CHAR_HEIGHT

      public static final int FONT_CHAR_HEIGHT
      Font character height in world coordinates.
      See Also:
    • FONT_CHAR_WIDTH_TEXTURE_PIXELS

      public static final int FONT_CHAR_WIDTH_TEXTURE_PIXELS
      Font character width in texture pixels.
      See Also:
    • FONT_CHAR_HEIGHT_TEXTURE_PIXELS

      public static final int FONT_CHAR_HEIGHT_TEXTURE_PIXELS
      Font character height in texture pixels.
      See Also:
  • Constructor Details

    • TextCanvas

      public TextCanvas(Transform location, String text, Color foregroundColor, Color backgroundColor)
      Creates a text canvas initialized with the given text string.

      The canvas dimensions are automatically computed from the text content (number of lines determines rows, the longest line determines columns).

      Parameters:
      location - the 3D transform positioning this canvas in the scene
      text - the initial text content (may contain newlines for multiple rows)
      foregroundColor - the default text color
      backgroundColor - the default background color
    • TextCanvas

      public TextCanvas(Transform location, TextPointer dimensions, Color foregroundColor, Color backgroundColor)
      Creates a blank text canvas with the specified dimensions.

      The canvas is initialized with spaces in every cell, filled with the specified background color. Characters can be written using putChar(char), print(String), or setText(String).

      Parameters:
      location - the 3D transform positioning this canvas in the scene
      dimensions - the grid size as a TextPointer where row is the number of rows and column is the number of columns
      foregroundColor - the default text color
      backgroundColor - the default background color
  • Method Details

    • getTextDimensions

      public static TextPointer getTextDimensions(String text)
      Computes the row and column dimensions needed to fit the given text.
      Parameters:
      text - the text content (may contain newlines)
      Returns:
      a TextPointer where row is the number of lines and column is the length of the longest line
    • clear

      public void clear()
      Clears the entire canvas, resetting all characters to spaces with the default colors.

      The SDF mask and both color layers are reset.

    • getSize

      public TextPointer getSize()
      Returns the dimensions of this text canvas.
      Returns:
      a TextPointer where row is the number of rows and column is the number of columns
    • locate

      public void locate(int row, int column)
      Moves the internal cursor to the specified row and column.

      Subsequent calls to putChar(char) and print(String) will begin writing at this position.

      Parameters:
      row - the target row (0-based)
      column - the target column (0-based)
    • print

      public void print(String text)
      Prints a string starting at the current cursor location, advancing the cursor after each character.

      When the cursor reaches the end of a row, it wraps to the beginning of the next row.

      Parameters:
      text - the text to print
      See Also:
    • putChar

      public void putChar(char character)
      Writes a character at the current cursor location and advances the cursor.

      The cursor moves one column to the right. If it exceeds the row width, it wraps to column 0 of the next row.

      Parameters:
      character - the character to write
    • putChar

      public void putChar(int row, int column, char character)
      Writes a character at the specified row and column using the current foreground and background colors.

      If the row or column is out of bounds, the call is silently ignored.

      Parameters:
      row - the row index (0-based)
      column - the column index (0-based)
      character - the character to write
    • putChar

      public void putChar(TextPointer location, char character)
      Writes a character at the position specified by a TextPointer.
      Parameters:
      location - the row and column position
      character - the character to write
    • setBackgroundColor

      public void setBackgroundColor(Color backgroundColor)
      Sets the default background color for subsequent character writes.
      Parameters:
      backgroundColor - the new background color
    • setForegroundColor

      public void setForegroundColor(Color foregroundColor)
      Sets the default foreground (text) color for subsequent character writes.
      Parameters:
      foregroundColor - the new foreground color
    • setText

      public void setText(String text)
      Replaces the entire canvas content with the given multi-line text string.

      Each line of text (separated by newlines) is written to consecutive rows, starting from row 0. Characters beyond the canvas width are ignored.

      Parameters:
      text - the text to display (may contain newline characters)
    • setTextColor

      public void setTextColor(Color color)
      Sets the foreground color of the whole canvas.

      Fills the SDF ink color layer; cell shapes are untouched, so text content and background colors are preserved.

      Parameters:
      color - the new foreground color