Menu

How to iterate through all TextBoxes?

hhan
2015-01-04
2015-01-06
  • hhan

    hhan - 2015-01-04

    I want to support printing of documents, not only rendering to screen. I'm trying to avoid printing a line of text partially in multiple pages.

    I think I have a way of avoid it but I need to iterate through all TextBox of the document. This does not work:

    • Obtain a reference to the root ElementBox.
    • For every child that can be casted to ElementBox iterate through its children too. And recursively do the same for all children castable to ElementBox and so on.
    • For every child box that can be casted to TextBox, see if they bound rectangle is in an area shared by two pages, if it is, then move it down until it fits in the second page only. (actually no property of the box is modified, at printing, the page bounds are moved to make the TextBox go to the second page)

    The idea is good but I did a test using System.out.println and for some reason the only TextBoxes hitted by that search are the ones that are inmediate children of the root box. It never retrieved a TextBox child of any paragraph, header, div, etc.

    Maybe I didn't understan how CSSBox tree structure is organized. The proposed method should hit all TextBox in document, isn't it?

     
  • hhan

    hhan - 2015-01-05

    For some reason this worked:

    If I start the search not at the ElementBox obtained by BrowserCanvas.getRootBox() but at the element box parent of it: BrowserCanvas.getRootBox().getParent() then my recursive search works, I can see in the output that all elements of the document are visited.

    Should not BrowserCanvas.getRootBox() returns a box that has no parent and is at the top of the hierarchy?

     
  • Radek Burget

    Radek Burget - 2015-01-06

    Hi! The getRootBox() method actually returns the box that corresponds to the document root element (usually the html element). In some cases (e.g. when absolute positioning is used) some boxes may not be child boxes of this box. I recommend to use the BrowserCanvas.getViewport() method. You may take a look at the TextBoxes demo for reference.

    The approach you mention is good when you print text only. If you want to print other element parts such as backgrounds and borders, I would recommend to create your own BoxRenderer implementation. This should ensure that the individual parts are painted in an appropriate order. SVGRenderer may be used as a good example.

     
  • hhan

    hhan - 2015-01-06

    Thank you, I'm looking into implementing a BoxRenderer.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.