Class PrintRecordSheet

java.lang.Object
megameklab.printing.PrintRecordSheet
All Implemented Interfaces:
Printable, IdConstants
Direct Known Subclasses:
PrintCompositeTankSheet, PrintEntity, PrintSmallUnitSheet

public abstract class PrintRecordSheet extends Object implements Printable, IdConstants
Base class for rendering record sheets. This is mostly a collection of utility methods.
  • Field Details

  • Constructor Details

    • PrintRecordSheet

      protected PrintRecordSheet(int firstPage, RecordSheetOptions options)
      Creates an SVG object for the record sheet
      Parameters:
      firstPage - The print job page number for this sheet
      options - Overrides the global options for which elements are printed
  • Method Details

    • getFirstPage

      protected final int getFirstPage()
      Returns:
      The page number of the first page of this record sheet within the book.
    • getSVGDocument

      @Nullable public final Document getSVGDocument()
    • setSVGDocument

      public final void setSVGDocument(@Nullable Document svgDocument)
    • setCallback

      public void setCallback(Consumer<Integer> callback)
      Provides a callback function that can be used to provide updates on printing progress. As each page is rendered, the callback is invoked with the page number.
      Parameters:
      callback - The function to call with the current page number.
    • getTypeface

      protected final String getTypeface()
      Returns:
      The name of the typeface to use when printing record sheets.
    • getNormalFont

      protected final Font getNormalFont(float size)
      Used for measuring font metrics of a normal weight font
      Parameters:
      size - The font size
      Returns:
      A font derived from the default
    • getBoldFont

      protected final Font getBoldFont(float size)
      Used for measuring font metrics of a bold weight font
      Parameters:
      size - The font size
      Returns:
      A font derived from the default bold
    • getItalicFont

      protected final Font getItalicFont(float size)
      Used for measuring font metrics of an italic font
      Parameters:
      size - The font size
      Returns:
      A font derived from the default italic
    • getBoldItalicFont

      protected final Font getBoldItalicFont(float size)
      Used for measuring font metrics of a bold italic font
      Parameters:
      size - The font size
      Returns:
      A font derived from the default italic
    • loadTemplate

      @Nullable protected Document loadTemplate(int pageIndex, PageFormat pageFormat, boolean useUnitTestTemplateDirectory)
      Creates the base template document. This is usually loaded from a file, but some composite record sheets override this to create a document in memory which is then filled in using the individual record sheet templates.
      Parameters:
      pageIndex - The index of this page in the print job
      pageFormat - The page format selected by the user
      useUnitTestTemplateDirectory - true if the unit test template directory should be used
      Returns:
      An SVG document for one page of the print job
    • loadTemplate

      @Deprecated(since="0.51.0", forRemoval=true) @Nullable protected Document loadTemplate(int pageIndex, PageFormat pageFormat)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Creates the base template document. This is usually loaded from a file, but some composite record sheets override this to create a document in memory which is then filled in using the individual record sheet templates.
      Parameters:
      pageIndex - The index of this page in the print job
      pageFormat - The page format selected by the user
      Returns:
      An SVG document for one page of the print job
    • createDocument

      public boolean createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin)
    • createDocument

      public boolean createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin, boolean useUnitTestTemplateDirectory)
      Returns:
      true if the document was created successfully, otherwise false
    • applyCoreComponentsCriticalDamage

      protected void applyCoreComponentsCriticalDamage()
      Applies the critical damage to the core components of the unit and crew. This should be overridden by subclasses that have core components.
    • fillCoreComponentCriticalDamage

      protected void fillCoreComponentCriticalDamage(String idPrefix, int damage)
    • fillCoreComponentCriticalDamage

      protected void fillCoreComponentCriticalDamage(String id, boolean damaged)
    • print

      public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
      Specified by:
      print in interface Printable
    • exportPDF

      @Nullable public InputStream exportPDF(int pageNumber, PageFormat pageFormat) throws Exception
      Throws:
      Exception
    • build

      public org.apache.batik.gvt.GraphicsNode build()
    • getPageCount

      public int getPageCount()
      Returns:
      The number of pages required to print this record sheet
    • processImage

      protected void processImage(int pageNum, PageFormat pageFormat)
      Renders the sheet to the Graphics object.
      Parameters:
      pageNum - Indicates which page of multipage sheets to print. The first page is 0.
    • getSVGFileName

      protected abstract String getSVGFileName(int pageNumber)
      Parameters:
      pageNumber - The page number in the current record sheet, where the first page is numbered zero.
      Returns:
      The file name for the current page in the record sheet image directory
    • getRecordSheetTitle

      protected abstract String getRecordSheetTitle()
      Returns:
      The title to use for the record sheet
    • getBookmarkNames

      public abstract List<String> getBookmarkNames()
      Used to build an outline of a PDF document
      Returns:
      Names of outline entries
    • setTextField

      protected void setTextField(String id, int i)
    • setTextField

      protected void setTextField(String id, String text)
    • setTextField

      protected void setTextField(String id, String text, boolean unhide)
      Sets the text content of the text element in the SVG diagram corresponding with the given id. If the element does not exist, does nothing. If the text is null, hides the element instead. Any text previously in the element is cleared.
      Parameters:
      id - The String id of a text element
      text - The text to set as content
      unhide - Sets the element visible if the text is non-null
    • addLineThrough

      protected void addLineThrough(Element parent, double x, double y, double width)
      Add a line through the text at the given coordinates. The line is drawn at the center of the text.
      Parameters:
      parent - The SVG element to add the line to.
      x - The X position of the line.
      y - The Y position of the line.
      width - The width of the line.
    • addLineThrough

      protected void addLineThrough(Element parent, double x, double y, double width, String color)
      Add a line through the text at the given coordinates. The line is drawn at the center of the text.
      Parameters:
      parent - The SVG element to add the line to.
      x - The X position of the line.
      y - The Y position of the line.
      width - The width of the line.
      color - The color of the line.
    • addTextElement

      protected double addTextElement(Element parent, double x, double y, String text, float fontSize, String anchor, String weight)
      Convenience method for creating a new SVG Text element and adding it to the parent. The width of the text is returned, to aid in layout.
      Parameters:
      parent - The SVG element to add the text element to.
      x - The X position of the new element.
      y - The Y position of the new element.
      text - The text to display.
      fontSize - Font size of the text.
      anchor - Set the Text elements text-anchor. Should be either start, middle, or end.
      weight - The font weight, either normal or bold.
      Returns:
      The width of the text in the current font size
    • addTextElement

      protected double addTextElement(Element parent, double x, double y, String text, float fontSize, String anchor, String weight, String fontStyle, String fill)
      Convenience method for creating a new SVG Text element and adding it to the parent. The height of the text is returned, to aid in layout.
      Parameters:
      parent - The SVG element to add the text element to.
      x - The X position of the new element.
      y - The Y position of the new element.
      text - The text to display.
      fontSize - Font size of the text.
      anchor - Set the Text elements text-anchor. Should be either start, middle, or end.
      weight - The font weight, either normal or bold.
      fontStyle - The font style, either normal or italic.
      fill - The fill color for the text (e.g. foreground color)
      Returns:
      The width of the added text element
    • addTextElement

      protected double addTextElement(Element parent, double x, double y, String text, float fontSize, String anchor, String weight, String fontStyle, String fill, String id, String className)
      Convenience method for creating a new SVG Text element and adding it to the parent. The height of the text is returned, to aid in layout.
      Parameters:
      parent - The SVG element to add the text element to.
      x - The X position of the new element.
      y - The Y position of the new element.
      text - The text to display.
      fontSize - Font size of the text.
      anchor - Set the Text elements text-anchor. Should be either start, middle, or end.
      weight - The font weight, either normal or bold.
      fontStyle - The font style, either normal or italic.
      fill - The fill color for the text (e.g. foreground color)
      id - The id of the text element, or null if not needed.
      className - The class name of the text element, or null if not needed.
      Returns:
      The width of the added text element
    • addTextElementToFit

      protected void addTextElementToFit(Element parent, double x, double y, double width, String text, float fontSize, String anchor, String weight)
      Creates a new text element with black fill and adds it to the parent. If the text is wider than the available space, the text is compressed to fit.
      Parameters:
      parent - The SVG element to add the text element to.
      x - The X position of the new element.
      y - The Y position of the new element.
      width - The width of the space the text has to fit.
      text - The text to display.
      fontSize - Font size of the text.
      anchor - Set the Text elements text-anchor. Should be either start, middle, or end.
      weight - The font weight, either normal or bold.
    • addTextElementToFit

      protected void addTextElementToFit(Element parent, double x, double y, double width, String text, float fontSize, String anchor, String weight, String fill)
      Creates a new text element and adds it to the parent. If the text is wider than the available space, the text is compressed to fit.
      Parameters:
      parent - The SVG element to add the text element to.
      x - The X position of the new element.
      y - The Y position of the new element.
      width - The width of the space the text has to fit.
      text - The text to display.
      fontSize - Font size of the text.
      anchor - Set the Text elements text-anchor. Should be either start, middle, or end.
      weight - The font weight, either normal or bold.
      fill - The fill color for the text (e.g. foreground color)
    • addTextElementToFit

      protected void addTextElementToFit(Element parent, double x, double y, double width, String text, float fontSize, String anchor, String weight, String fill, String id, String className)
      Creates a new text element and adds it to the parent. If the text is wider than the available space, the text is compressed to fit.
      Parameters:
      parent - The SVG element to add the text element to.
      x - The X position of the new element.
      y - The Y position of the new element.
      width - The width of the space the text has to fit.
      text - The text to display.
      fontSize - Font size of the text.
      anchor - Set the Text elements text-anchor. Should be either start, middle, or end.
      weight - The font weight, either normal or bold.
      fill - The fill color for the text (e.g. foreground color)
      id - The id of the text element, or null if not needed.
      className - The class name of the text element, or null if not needed.
    • addMultilineTextElement

      protected int addMultilineTextElement(Element canvas, double x, double y, double width, double lineHeight, String text, float fontSize, String anchor, String weight)
      Adds a text element to a region with limited width. If there are multiple lines, the text will be split over multiple lines, broken on a space character. The space will still be included on the next line as a small indent.
      Parameters:
      canvas - The parent SVGElement to the new Text.
      x - The x coordinate of the upper left corner of the text region
      y - The y coordinate of the upper left corner of the text region
      width - The allowable width of the text element.
      lineHeight - The amount to increase the y coordinate for a new line
      text - The text to add
      fontSize - The font-size attribute
      anchor - The text-anchor attribute
      weight - The font-weight attribute
      Returns:
      The number of lines of text added
    • addMultilineTextElement

      protected int addMultilineTextElement(Element canvas, double x, double y, double width, double lineHeight, String text, float fontSize, String anchor, String weight, String fontStyle, String className)
      Adds a text element to a region with limited width. If there are multiple lines, the text will be split over multiple lines, broken on a space character. The space will still be included on the next line as a small indent.
      Parameters:
      canvas - The parent SVGElement to the new Text.
      x - The x coordinate of the upper left corner of the text region
      y - The y coordinate of the upper left corner of the text region
      width - The allowable width of the text element.
      lineHeight - The amount to increase the y coordinate for a new line
      text - The text to add
      fontSize - The font-size attribute
      anchor - The text-anchor attribute
      weight - The font-weight attribute
      fontStyle - The font style, e.g., normal or italic.
      Returns:
      The number of lines of text added
    • addMultilineTextElement

      protected int addMultilineTextElement(Element canvas, double x, double y, double width, double lineHeight, String text, float fontSize, String anchor, String weight, String fontStyle, String fill, char delimiter, String className)
      Adds a text element to a region with limited width. If there are multiple lines, the text will be split over multiple lines, broken on the provided character. The line break character will be included on the next line.
      Parameters:
      canvas - The parent SVGElement to the new Text.
      x - The x coordinate of the upper left corner of the text region
      y - The y coordinate of the upper left corner of the text region
      width - The allowable width of the text element.
      lineHeight - The amount to increase the y coordinate for a new line
      text - The text to add
      fontSize - The font-size attribute
      anchor - The text-anchor attribute
      weight - The font-weight attribute
      fontStyle - The font style, e.g., normal or italic.
      fill - The fill color for the text (e.g. foreground color)
      delimiter - The character to use as an acceptable line ending
      Returns:
      The number of lines of text added
    • createPip

      @Deprecated(since="0.51.0", forRemoval=true) protected Element createPip(double x, double y, double radius, double strokeWidth)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • createPip

      protected Element createPip(double x, double y, double radius, double strokeWidth, PipType type, String fill)
      Approximates a circle using four Bézier curves.
      Parameters:
      x - Position of left of bounding rectangle.
      y - Position of top of bounding rectangle.
      radius - Radius of the circle
      strokeWidth - Width of the stroke
      type - PipType
      fill - Fill color for the pip
      Returns:
      A Path describing the circle
    • createPip

      protected Element createPip(double x, double y, double radius, double strokeWidth, PipType type, String fill, String className, String loc, boolean rear)
      Approximates a circle using four Bézier curves.
      Parameters:
      x - Position of left of bounding rectangle.
      y - Position of top of bounding rectangle.
      radius - Radius of the circle
      strokeWidth - Width of the stroke
      type - PipType
      fill - Fill color for the pip
      className - Class name for the pip, or null if not needed
      loc - Location of the pip, or null if not needed
      rear - If true, the pip is a rear pip
      Returns:
      A Path describing the circle
    • createRoundedRectangle

      protected Element createRoundedRectangle(double x, double y, double width, double height, double radius, double control, double strokeWidth, String stroke)
      Creates a rectangle with Bézier curves on the corners
      Parameters:
      x - The x coordinate of the top left corner
      y - The y coordinate of the top left corner
      width - The width of the rectangle
      height - The height of the rectangle
      radius - The radius of the curve in the corners
      control - The length from the endpoint of each curve to its control point
      strokeWidth - The width of the stroke
      stroke - The fill color of the stroke
      Returns:
      A path element
    • hideElement

      protected void hideElement(String id)
    • hideElement

      protected void hideElement(String id, boolean hide)
    • removeElement

      protected void removeElement(String id)
    • hideElement

      protected void hideElement(Element element)
      Sets the visibility attribute to "hidden"
      Parameters:
      element - The element to hide
    • hideElement

      protected void hideElement(Element element, boolean hide)
      Sets an element's visibility attribute
      Parameters:
      element - The element to hide or show
      hide - If true, visibility will be set to hidden. If false, the
    • getFontHeight

      public float getFontHeight(float fontSize)
      Determines the vertical space taken up by a line of text.
      Parameters:
      fontSize - Value of CSS font-size attribute
      Returns:
      The height of the bounding box of a text element
    • getFontHeight

      protected float getFontHeight(float fontSize, Font font)
      Determines the vertical space taken up by a line of text.
      Parameters:
      fontSize - Value of CSS font-size attribute
      font - The font to use for the text
      Returns:
      The height of the bounding box of a text element
    • getNormalTextLength

      public double getNormalTextLength(String text, float fontSize)
    • getBoldTextLength

      public double getBoldTextLength(String text, float fontSize)
    • getItalicTextLength

      public double getItalicTextLength(String text, float fontSize)
    • getBoldItalicTextLength

      public double getBoldItalicTextLength(String text, float fontSize)
    • getTextLength

      public double getTextLength(String text, float fontSize)
    • getTextLength

      public double getTextLength(String text, float fontSize, String weight)
    • getTextLength

      public double getTextLength(String text, float fontSize, String weight, String fontStyle)
    • getRectBBox

      public static Rectangle2D getRectBBox(org.w3c.dom.svg.SVGRectElement rect)
    • embedImage

      public void embedImage(@Nullable File imageFile, Element canvas, Rectangle2D bbox, boolean center)
      Inserts an image into the SVG diagram scaled to fit into the provided bounds.
      Parameters:
      imageFile - The file containing the image to embed.
      canvas - The parent element for the image element.
      bbox - The bounding box for the image. The image will be scaled to fit.
      center - Whether to center the image vertically and horizontally.
    • embedImage

      public void embedImage(@Nullable Image image, Element canvas, Rectangle2D bbox, boolean center, @Nullable String className)
      Inserts an image into the SVG diagram scaled to fit into the provided bounds.
      Parameters:
      image - The file containing the image to embed.
      canvas - The parent element for the image element.
      bbox - The bounding box for the image. The image will be scaled to fit.
      center - Whether to center the image vertically and horizontally.
    • includeReferenceCharts

      protected boolean includeReferenceCharts()
      Used to determine whether to scale the record sheet to make room for charts. This depends both on whether the option is selected and on whether the sheet supports reference charts.
      Returns:
      Whether to include reference tables
    • getRightSideReferenceTables

      protected List<ReferenceTable> getRightSideReferenceTables()
    • addReferenceCharts

      protected void addReferenceCharts(PageFormat pageFormat)
      Adds reference charts to the right side of the record sheet.
      Parameters:
      pageFormat - The document's page format.
    • placeReferenceCharts

      protected void placeReferenceCharts(List<ReferenceTable> tables, Node parent, double x, double y, double width, double height)
      Adds a list of reference tables to the document, sizing to fit withing the available space. Layout is vertical.
      Parameters:
      tables - The list of tables to add.
      parent - The parent node of the table Element.
      x - The x coordinate of the top left corner of the tables, relative to the parent node.
      y - The y coordinate of the top left corder of the tables, relative to the parent node.
      width - The width of the table column.
      height - The height of the table column.
    • getDamageFillColor

      protected String getDamageFillColor()
      Returns:
      The color to use for the damage fill