Package com.openhtmltopdf.layout
Class BoxBuilder
- java.lang.Object
-
- com.openhtmltopdf.layout.BoxBuilder
-
public class BoxBuilder extends java.lang.Object
This class is responsible for creating the box tree from the DOM. This is mostly just a one-to-one translation from theElement
to anInlineBox
or aBlockBox
(or some subclass ofBlockBox
), but the tree is reorganized according to the CSS rules. This includes inserting anonymous block and inline boxes, anonymous table content, and:before
and:after
content. White space is also normalized at this point. Table columns and table column groups are added to the table which owns them, but are not created as regular boxes. Floated and absolutely positioned content is always treated as inline content for purposes of inserting anonymous block boxes and calculating the kind of content contained in a given block box.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
BoxBuilder.ChildBoxInfo
private static class
BoxBuilder.CreateChildrenContext
-
Field Summary
Fields Modifier and Type Field Description private static int
CONTENT_LIST_DOCUMENT
private static int
CONTENT_LIST_MARGIN_BOX
static int
MARGIN_BOX_HORIZONTAL
static int
MARGIN_BOX_VERTICAL
-
Constructor Summary
Constructors Constructor Description BoxBuilder()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
addColumnOrColumnGroup(LayoutContext c, TableBox table, org.w3c.dom.Element e, CalculatedStyle style)
private static void
addColumns(LayoutContext c, TableBox table, TableColumn parent)
private static boolean
containsOrphanedTableContent(java.util.List<Styleable> children)
private static void
createAnonymousBlock(SharedContext c, Box parent, java.util.List<Styleable> inline, java.util.List<InlineBox> savedParents)
private static void
createAnonymousBlock(SharedContext c, Box parent, java.util.List<Styleable> inline, java.util.List<InlineBox> savedParents, IdentValue display)
private static void
createAnonymousTableContent(LayoutContext c, BlockBox source, IdentValue next, java.util.List<Styleable> childrenForAnonymous, java.util.List<Styleable> childrenWithAnonymous)
private static BlockBox
createBlockBox(CalculatedStyle style, BoxBuilder.ChildBoxInfo info, boolean generated)
private static Styleable
createChildBlockBox(LayoutContext c, BoxBuilder.ChildBoxInfo info, org.w3c.dom.Element element, CalculatedStyle style)
static void
createChildren(LayoutContext c, BlockBox parent)
private static void
createChildren(LayoutContext c, BlockBox blockParent, org.w3c.dom.Element parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, boolean inline)
private static void
createColumnContainer(LayoutContext c, Styleable child, org.w3c.dom.Element element, CalculatedStyle style)
private static void
createElementChild(LayoutContext c, org.w3c.dom.Element parent, BlockBox blockParent, org.w3c.dom.Node working, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, BoxBuilder.CreateChildrenContext context)
private static BlockBox
createFootnoteBody(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle style)
Creates the footnote body to put at the bottom of the page inside a page's footnote area.private static org.w3c.dom.Element
createFootnoteCallAnchor(LayoutContext c, org.w3c.dom.Element parent)
Used to create the anchor element to link to the footnote body for the footnote-call pseudo element.private static org.w3c.dom.Element
createFootnoteTarget(LayoutContext c, org.w3c.dom.Element parent)
Creates an element with id for the footnote-marker pseudo element so we can link to it from the footnote-call pseduo element.private static java.util.List<Styleable>
createGeneratedContent(LayoutContext c, org.w3c.dom.Element element, java.lang.String peName, CalculatedStyle style, PropertyValue property, BoxBuilder.ChildBoxInfo info)
Creates generated content boxes for pseudo elements such as::before
.private static java.util.List<Styleable>
createGeneratedContentList(LayoutContext c, org.w3c.dom.Element element, java.util.List<PropertyValue> values, java.lang.String peName, CalculatedStyle style, int mode, BoxBuilder.ChildBoxInfo info, java.util.List<Styleable> result)
private static java.util.List<Styleable>
createGeneratedMarginBoxContent(LayoutContext c, org.w3c.dom.Element element, PropertyValue property, CalculatedStyle style, BoxBuilder.ChildBoxInfo info)
private static InlineBox
createInlineBox(java.lang.String text, org.w3c.dom.Element parent, CalculatedStyle parentStyle, org.w3c.dom.Text node)
private static void
createInlineChildren(LayoutContext c, org.w3c.dom.Element parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, BoxBuilder.CreateChildrenContext context, org.w3c.dom.Element element)
private static TableCellBox
createMarginBox(LayoutContext c, CascadedStyle cascadedStyle, boolean alwaysCreate)
static TableBox
createMarginTable(LayoutContext c, PageInfo pageInfo, MarginBoxName[] names, int height, int direction)
static BlockBox
createRootBox(LayoutContext c, org.w3c.dom.Document document)
private static InlineBox
doBidi(LayoutContext c, org.w3c.dom.Text textNode, org.w3c.dom.Element parent, CalculatedStyle parentStyle, InlineBox previousIB, java.util.List<Styleable> children)
Attempts to divide a Text node further into directional text runs, either LTR or RTL.private static InlineBox
doFakeBidi(LayoutContext c, org.w3c.dom.Text textNode, org.w3c.dom.Element parent, CalculatedStyle parentStyle, InlineBox previousIB, java.util.List<Styleable> children)
private static java.lang.String
getAttributeValue(FSFunction attrFunc, org.w3c.dom.Element e)
private static IdentValue
getNextTableNestingLevel(IdentValue display)
private static IdentValue
getPreviousTableNestingLevel(IdentValue display)
static BlockBox
getRunningBlock(LayoutContext c, PropertyValue value)
private static void
insertAnonymousBlocks(SharedContext c, Box parent, java.util.List<Styleable> children, boolean layoutRunningBlocks)
private static void
insertGeneratedContent(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle parentStyle, java.lang.String peName, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info)
private static boolean
isAllProperTableNesting(IdentValue parentDisplay, java.util.List<Styleable> children)
private static boolean
isAttrFunction(FSFunction function)
static boolean
isElementFunction(FSFunction function)
private static boolean
isGeneratedElement(org.w3c.dom.Element element)
Don't output elements that have been artificially created to support footnotes and content property images.private static boolean
isInsertedBoxIgnored(org.w3c.dom.Element element)
private static boolean
isNestingTableContent(IdentValue display)
private static boolean
isParentInline(BlockBox box)
private static boolean
isProperTableNesting(IdentValue parent, IdentValue child)
private static boolean
isTableRowOrRowGroup(Styleable child)
private static boolean
isValidFootnote(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle style)
private static boolean
isValidFootnotePseudo(CalculatedStyle style)
private static void
logInvalidFootnotePseudo(java.lang.String peName, CalculatedStyle style)
private static void
logInvalidFootnoteStyle(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle style)
private static BoxBuilder.ChildBoxInfo
lookForBlockContent(java.util.List<Styleable> styleables)
private static CounterFunction
makeCounterFunction(FSFunction function, LayoutContext c, CalculatedStyle style)
private static boolean
matchesTableLevel(IdentValue target, IdentValue value)
private static void
rebalanceInlineContent(java.util.List<Styleable> content)
Makes sure that anyInlineBox
incontent
both starts and ends withincontent
.private static BlockBox
reorderTableContent(LayoutContext c, TableBox table)
Reorganizes a table so that the header is the first row group and the footer the last.private static void
resolveChildren(LayoutContext c, BlockBox owner, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info)
private static void
resolveChildTableContent(LayoutContext c, BlockBox parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, IdentValue target)
Handles the situation when we find table content, but our parent is not table related.private static void
resolveElementCounters(LayoutContext c, org.w3c.dom.Node working, org.w3c.dom.Element element, CalculatedStyle style)
private static void
resolveTableContent(LayoutContext c, BlockBox parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info)
Handles the situation when our current parent is table related.private static InlineBox
setupInlineChild(InlineBox child, InlineBox previousIB)
private static void
splitParagraphs(LayoutContext c, org.w3c.dom.Document document)
Split the document into paragraphs for use in analyzing bi-directional text runs.private static void
stripAllWhitespace(java.util.List<Styleable> content)
private static java.util.List<Styleable>
wrapGeneratedContent(LayoutContext c, org.w3c.dom.Element element, java.lang.String peName, CalculatedStyle style, BoxBuilder.ChildBoxInfo info, BoxBuilder.ChildBoxInfo childInfo, java.util.List<Styleable> inlineBoxes)
-
-
-
Field Detail
-
MARGIN_BOX_VERTICAL
public static final int MARGIN_BOX_VERTICAL
- See Also:
- Constant Field Values
-
MARGIN_BOX_HORIZONTAL
public static final int MARGIN_BOX_HORIZONTAL
- See Also:
- Constant Field Values
-
CONTENT_LIST_DOCUMENT
private static final int CONTENT_LIST_DOCUMENT
- See Also:
- Constant Field Values
-
CONTENT_LIST_MARGIN_BOX
private static final int CONTENT_LIST_MARGIN_BOX
- See Also:
- Constant Field Values
-
-
Method Detail
-
splitParagraphs
private static void splitParagraphs(LayoutContext c, org.w3c.dom.Document document)
Split the document into paragraphs for use in analyzing bi-directional text runs.- Parameters:
c
-document
-
-
createRootBox
public static BlockBox createRootBox(LayoutContext c, org.w3c.dom.Document document)
-
createChildren
public static void createChildren(LayoutContext c, BlockBox parent)
-
isInsertedBoxIgnored
private static boolean isInsertedBoxIgnored(org.w3c.dom.Element element)
-
createMarginTable
public static TableBox createMarginTable(LayoutContext c, PageInfo pageInfo, MarginBoxName[] names, int height, int direction)
-
createMarginBox
private static TableCellBox createMarginBox(LayoutContext c, CascadedStyle cascadedStyle, boolean alwaysCreate)
-
resolveChildren
private static void resolveChildren(LayoutContext c, BlockBox owner, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info)
-
isAllProperTableNesting
private static boolean isAllProperTableNesting(IdentValue parentDisplay, java.util.List<Styleable> children)
-
resolveChildTableContent
private static void resolveChildTableContent(LayoutContext c, BlockBox parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, IdentValue target)
Handles the situation when we find table content, but our parent is not table related. For example,div
->td
. Anonymous tables are then constructed by repeatedly pulling together consecutive same-table-level siblings and wrapping them in the next highest table level (e.g. consecutivetd
elements will be wrapped in an anonymoustr
, then atbody
, and finally atable
).
-
matchesTableLevel
private static boolean matchesTableLevel(IdentValue target, IdentValue value)
-
rebalanceInlineContent
private static void rebalanceInlineContent(java.util.List<Styleable> content)
Makes sure that anyInlineBox
incontent
both starts and ends withincontent
. Used to ensure that it is always possible to construct anonymous blocks once an element's children has been distributed among anonymous table objects.
-
stripAllWhitespace
private static void stripAllWhitespace(java.util.List<Styleable> content)
-
resolveTableContent
private static void resolveTableContent(LayoutContext c, BlockBox parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info)
Handles the situation when our current parent is table related. If everything is properly nested (e.g. atr
contains onlytd
elements), nothing is done. Otherwise anonymous boxes are inserted to ensure the integrity of the table model.
-
isTableRowOrRowGroup
private static boolean isTableRowOrRowGroup(Styleable child)
-
containsOrphanedTableContent
private static boolean containsOrphanedTableContent(java.util.List<Styleable> children)
-
isParentInline
private static boolean isParentInline(BlockBox box)
-
createAnonymousTableContent
private static void createAnonymousTableContent(LayoutContext c, BlockBox source, IdentValue next, java.util.List<Styleable> childrenForAnonymous, java.util.List<Styleable> childrenWithAnonymous)
-
reorderTableContent
private static BlockBox reorderTableContent(LayoutContext c, TableBox table)
Reorganizes a table so that the header is the first row group and the footer the last. If the table has caption boxes, they will be pulled out and added to an anonymous block box along with the table itself. If not, the table is returned.
-
lookForBlockContent
private static BoxBuilder.ChildBoxInfo lookForBlockContent(java.util.List<Styleable> styleables)
-
getNextTableNestingLevel
private static IdentValue getNextTableNestingLevel(IdentValue display)
-
getPreviousTableNestingLevel
private static IdentValue getPreviousTableNestingLevel(IdentValue display)
-
isProperTableNesting
private static boolean isProperTableNesting(IdentValue parent, IdentValue child)
-
isNestingTableContent
private static boolean isNestingTableContent(IdentValue display)
-
isAttrFunction
private static boolean isAttrFunction(FSFunction function)
-
isElementFunction
public static boolean isElementFunction(FSFunction function)
-
makeCounterFunction
private static CounterFunction makeCounterFunction(FSFunction function, LayoutContext c, CalculatedStyle style)
-
getAttributeValue
private static java.lang.String getAttributeValue(FSFunction attrFunc, org.w3c.dom.Element e)
-
createGeneratedContentList
private static java.util.List<Styleable> createGeneratedContentList(LayoutContext c, org.w3c.dom.Element element, java.util.List<PropertyValue> values, java.lang.String peName, CalculatedStyle style, int mode, BoxBuilder.ChildBoxInfo info, java.util.List<Styleable> result)
-
createFootnoteTarget
private static org.w3c.dom.Element createFootnoteTarget(LayoutContext c, org.w3c.dom.Element parent)
Creates an element with id for the footnote-marker pseudo element so we can link to it from the footnote-call pseduo element.
SeecreateFootnoteCallAnchor(LayoutContext, Element)
-
createFootnoteCallAnchor
private static org.w3c.dom.Element createFootnoteCallAnchor(LayoutContext c, org.w3c.dom.Element parent)
Used to create the anchor element to link to the footnote body for the footnote-call pseudo element.
SeecreateFootnoteTarget(LayoutContext, Element)
-
getRunningBlock
public static BlockBox getRunningBlock(LayoutContext c, PropertyValue value)
-
insertGeneratedContent
private static void insertGeneratedContent(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle parentStyle, java.lang.String peName, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info)
-
createGeneratedContent
private static java.util.List<Styleable> createGeneratedContent(LayoutContext c, org.w3c.dom.Element element, java.lang.String peName, CalculatedStyle style, PropertyValue property, BoxBuilder.ChildBoxInfo info)
Creates generated content boxes for pseudo elements such as::before
.- Parameters:
element
- The containing element where the pseudo element appears. Forspan::before
the element would be aspan
.peName
- Examples includebefore
,after
,footnote-call
andfootnote-marker
.style
- The child style for this pseudo element. Forspan::before
this would include all the styles set explicitly on::before
as well as those that inherit fromspan
following the cascade rules.property
- The values of thecontent
CSS property.info
- In/out param. Whether the resultant box(es) contain block level content.- Returns:
- The generated box(es). Typically one
BlockBox
or multiple inline boxes.
-
wrapGeneratedContent
private static java.util.List<Styleable> wrapGeneratedContent(LayoutContext c, org.w3c.dom.Element element, java.lang.String peName, CalculatedStyle style, BoxBuilder.ChildBoxInfo info, BoxBuilder.ChildBoxInfo childInfo, java.util.List<Styleable> inlineBoxes)
-
createGeneratedMarginBoxContent
private static java.util.List<Styleable> createGeneratedMarginBoxContent(LayoutContext c, org.w3c.dom.Element element, PropertyValue property, CalculatedStyle style, BoxBuilder.ChildBoxInfo info)
-
createBlockBox
private static BlockBox createBlockBox(CalculatedStyle style, BoxBuilder.ChildBoxInfo info, boolean generated)
-
addColumns
private static void addColumns(LayoutContext c, TableBox table, TableColumn parent)
-
addColumnOrColumnGroup
private static void addColumnOrColumnGroup(LayoutContext c, TableBox table, org.w3c.dom.Element e, CalculatedStyle style)
-
createInlineBox
private static InlineBox createInlineBox(java.lang.String text, org.w3c.dom.Element parent, CalculatedStyle parentStyle, org.w3c.dom.Text node)
-
isValidFootnote
private static boolean isValidFootnote(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle style)
-
logInvalidFootnoteStyle
private static void logInvalidFootnoteStyle(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle style)
-
isValidFootnotePseudo
private static boolean isValidFootnotePseudo(CalculatedStyle style)
-
logInvalidFootnotePseudo
private static void logInvalidFootnotePseudo(java.lang.String peName, CalculatedStyle style)
-
isGeneratedElement
private static boolean isGeneratedElement(org.w3c.dom.Element element)
Don't output elements that have been artificially created to support footnotes and content property images.
-
createElementChild
private static void createElementChild(LayoutContext c, org.w3c.dom.Element parent, BlockBox blockParent, org.w3c.dom.Node working, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, BoxBuilder.CreateChildrenContext context)
-
createInlineChildren
private static void createInlineChildren(LayoutContext c, org.w3c.dom.Element parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, BoxBuilder.CreateChildrenContext context, org.w3c.dom.Element element)
-
createChildBlockBox
private static Styleable createChildBlockBox(LayoutContext c, BoxBuilder.ChildBoxInfo info, org.w3c.dom.Element element, CalculatedStyle style)
-
createFootnoteBody
private static BlockBox createFootnoteBody(LayoutContext c, org.w3c.dom.Element element, CalculatedStyle style)
Creates the footnote body to put at the bottom of the page inside a page's footnote area.
-
createColumnContainer
private static void createColumnContainer(LayoutContext c, Styleable child, org.w3c.dom.Element element, CalculatedStyle style)
-
resolveElementCounters
private static void resolveElementCounters(LayoutContext c, org.w3c.dom.Node working, org.w3c.dom.Element element, CalculatedStyle style)
-
createChildren
private static void createChildren(LayoutContext c, BlockBox blockParent, org.w3c.dom.Element parent, java.util.List<Styleable> children, BoxBuilder.ChildBoxInfo info, boolean inline)
-
doFakeBidi
private static InlineBox doFakeBidi(LayoutContext c, org.w3c.dom.Text textNode, org.w3c.dom.Element parent, CalculatedStyle parentStyle, InlineBox previousIB, java.util.List<Styleable> children)
-
doBidi
private static InlineBox doBidi(LayoutContext c, org.w3c.dom.Text textNode, org.w3c.dom.Element parent, CalculatedStyle parentStyle, InlineBox previousIB, java.util.List<Styleable> children)
Attempts to divide a Text node further into directional text runs, either LTR or RTL.- Parameters:
c
-textNode
-parent
-parentStyle
-- Returns:
- the previousIB.
-
insertAnonymousBlocks
private static void insertAnonymousBlocks(SharedContext c, Box parent, java.util.List<Styleable> children, boolean layoutRunningBlocks)
-
createAnonymousBlock
private static void createAnonymousBlock(SharedContext c, Box parent, java.util.List<Styleable> inline, java.util.List<InlineBox> savedParents)
-
createAnonymousBlock
private static void createAnonymousBlock(SharedContext c, Box parent, java.util.List<Styleable> inline, java.util.List<InlineBox> savedParents, IdentValue display)
-
-