Class LayoutPainter

java.lang.Object
com.codename1.wordprocessing.LayoutPainter

public class LayoutPainter extends Object

Paints laid out items on a Graphics, the way DocumentView paints its pages.

Anything that shows text laid out by DocumentLayout -- a slide's text box, a thumbnail, a print preview -- paints it with this class, so it looks exactly as it does in the word processor: each word is drawn where the layout measured it, underlines and strike lines follow the measured text, and pictures are decoded once and kept.

void paintFirstPage(Graphics g, Document doc, int x, int y) {
    DocumentLayout layout = new DocumentLayout(doc, new ScreenMeasurer());
    LayoutPainter painter = new LayoutPainter(layout.getMeasurer());
    painter.paint(g, layout.getPage(0).getItems(), x, y, 2f);
}
  • Constructor Summary

    Constructors
    Constructor
    Description
    A painter for items laid out with a measurer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    drawWords(Graphics g, LayoutItem it, int x, int y, float scale)
    Draws an item a word at a time, each where the layout put it, in the graphics' current font and color.
    font(TextStyle style, int px)
    The platform font text of a style is drawn in, at a pixel size.
    The measurer items are positioned with.
    The decoded image of a picture, decoded once; null when it is linked or not an image the platform reads.
    int
    offset(LayoutItem it, int chars, float scale)
    Where the first chars characters of an item end, in pixels from its left edge, by the measurer the item was laid out with.
    void
    paint(Graphics g, List<LayoutItem> items, int left, int top, float scale)
    Paints items.
    void
    paintItem(Graphics g, LayoutItem it, int left, int top, float scale, boolean lastOnLine)
    Paints one item.
    void
    paintPicture(Graphics g, Picture p, int x, int y, int w, int h)
    Paints a picture scaled into a box, or a frame when it cannot be decoded.
    void
    paintText(Graphics g, LayoutItem it, int x, int baseline, float scale, boolean lastOnLine)
    Paints a text item: its words, underline, strike line and revision marks.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • LayoutPainter

      public LayoutPainter(Measurer measurer)

      A painter for items laid out with a measurer.

      Parameters
      • measurer: the measurer the items were laid out with; word positions are computed with it
  • Method Details

    • getMeasurer

      public Measurer getMeasurer()

      The measurer items are positioned with.

      Returns

      the measurer

    • font

      public Font font(TextStyle style, int px)

      The platform font text of a style is drawn in, at a pixel size.

      Parameters
      • style: the style

      • px: the size in pixels

      Returns

      the font

    • paint

      public void paint(Graphics g, List<LayoutItem> items, int left, int top, float scale)

      Paints items.

      Parameters
      • g: the graphics

      • items: the items, in points relative to their page

      • left: where the page's left edge is, in pixels

      • top: where the page's top edge is, in pixels

      • scale: pixels per point

    • paintItem

      public void paintItem(Graphics g, LayoutItem it, int left, int top, float scale, boolean lastOnLine)

      Paints one item.

      Parameters
      • g: the graphics

      • it: the item

      • left: the page's left edge in pixels

      • top: the page's top edge in pixels

      • scale: pixels per point

      • lastOnLine: true when it is the last item of its line, whose underline stops at its last glyph instead of running through its trailing spaces

    • paintText

      public void paintText(Graphics g, LayoutItem it, int x, int baseline, float scale, boolean lastOnLine)

      Paints a text item: its words, underline, strike line and revision marks.

      Parameters
      • g: the graphics

      • it: a LayoutItem.TEXT item

      • x: its left edge in pixels

      • baseline: its baseline in pixels

      • scale: pixels per point

      • lastOnLine: true when it ends its line

    • paintPicture

      public void paintPicture(Graphics g, Picture p, int x, int y, int w, int h)

      Paints a picture scaled into a box, or a frame when it cannot be decoded.

      Parameters
      • g: the graphics

      • p: the picture

      • x: the box's left edge

      • y: its top edge

      • w: its width

      • h: its height

    • image

      public Image image(Picture p)

      The decoded image of a picture, decoded once; null when it is linked or not an image the platform reads.

      Parameters
      • p: the picture
      Returns

      the image or null

    • offset

      public int offset(LayoutItem it, int chars, float scale)

      Where the first chars characters of an item end, in pixels from its left edge, by the measurer the item was laid out with.

      Parameters
      • it: a text item

      • chars: the number of characters

      • scale: pixels per point

      Returns

      the offset in pixels

    • drawWords

      public void drawWords(Graphics g, LayoutItem it, int x, int y, float scale)

      Draws an item a word at a time, each where the layout put it, in the graphics' current font and color. The font drawn at a whole pixel size is never exactly the one measured, and drawn in one piece the difference would add up along the run; this way it stays inside the space after each word.

      Parameters
      • g: the graphics

      • it: a text item

      • x: its left edge in pixels

      • y: the top of its glyphs in pixels

      • scale: pixels per point