Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculating height of a paragraph (for the purpose of computing page size for printing) #132

Closed
djskrien opened this issue Mar 29, 2015 · 4 comments

Comments

@djskrien
Copy link

I'd like to be able to print the contents of a CodeArea that contains a hundred or more lines of code, which means I need to break it up into pages for printing. Each line of my code is one Paragraph. Therefore, using PageLayout.getPrintableHeight() to get the total number of points I have to work with on each page, and dividing that total by the number of points of height per paragraph, I can figure out how many paragraphs will fit on a page. But to do this, I need a way of getting the height of a paragraph. Is there a way to do it, given the font and font size of the paragraph, e.g., Courier 12? Or is there some other better way of breaking a CodeArea into pages for printing? (Also, should I have asked this question on StackOverflow instead of here?)

@TomasMikula
Copy link
Member

There isn't any support for breaking text into pages.

To calculate the height of text given the font, you could create a Text or TextFlow node, set the font, and get its preferred height. You could also try to look into Text how it computes its size, if you want to be able to compute the size based on just the font.

You could also dig into into CodeArea to get the VirtualFlow. Resize the CodeArea to the printable size and then scroll to the top (e.g. codeArea.moveTo(0)) and get the number of visible paragraphs as virtualFlow.visibleCells().size(). Note that the last visible cell may not be fully visible, however.

Or, also using the enclosed VirtualFlow, to get the height of the first visible paragraph, you could just do virtualFlow.visibleCells().get(0).getLayoutBounds().getHeight().

@djskrien
Copy link
Author

Thanks for the prompt response. I tried creating both a Text and a TextFlow node using my font and font size and then asking for its height, but it gave me the wrong height as far as printing is concerned. I'd like to try your VirtualFlow idea to get the height of the first visible paragraph, but can you give me a little more information on how to get a virtual flow from the CodeArea?

@TomasMikula
Copy link
Member

You probably also need to set some text for the Text or TextFlow, and maybe even add it to the scene graph, not sure.

Getting the VirtualFlow is not public API, so be advised that this may change in the future. Currently, it happens to be the first child of the first child of the CodeArea, so this is the direct way to get it:

VirtualFlow<?, ?> vf = (VirtualFlow<?, ?>)
        ((Parent) codeArea.getChildrenUnmodifiable().get(0))
                .getChildrenUnmodifiable().get(0);

A little more robust way is to look it up by its style class:

VirtualFlow<?, ?> vf = (VirtualFlow<?, ?>) codeArea.lookup(".virtual-flow");

Then,

double height = vf.visibleCells().get(0).getNode().getLayoutBounds().getHeight();

@djskrien
Copy link
Author

Thank you! The last way works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants