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

Add API to get paragraph bounds if visible #587

Merged
merged 1 commit into from
Sep 21, 2017
Merged

Add API to get paragraph bounds if visible #587

merged 1 commit into from
Sep 21, 2017

Conversation

JordanMartinez
Copy link
Contributor

Resolves #537

@JordanMartinez JordanMartinez merged commit 45589ef into FXMisc:master Sep 21, 2017
@JordanMartinez
Copy link
Contributor Author

For now, this will do. If we want to change how the bounds works on short/long paragraphs, we can update it in a later PR.

@JordanMartinez JordanMartinez deleted the paragraphBounds branch September 21, 2017 00:44
@garybentley
Copy link

While useful this only works if the paragraph is already fully visible in the view. In my case I am trying to write ParagraphGraphicFactory and would like to display an image at an offset from the top of the paragraph. However the following code:

area.setParagraphGraphicFactory (new IntFunction<Node> ()
{

   @Override
   public Node apply (int idx)
   {

       Optional<Bounds> pb  = area.getParagraphBoundsOnScreen (idx);

   }

});

will either throw an exception:

Caused by: java.lang.NullPointerException
at org.fxmisc.richtext.GenericStyledArea.getParagraphBoundsOnScreen(GenericStyledArea.java:1372)
at java.util.Optional.map(Optional.java:215)
at org.fxmisc.richtext.GenericStyledArea.getParagraphBoundsOnScreen(GenericStyledArea.java:1362)

or return Optional.empty if the paragraph is only partially visible.

Is there any way to get the paragraph bounds if it is only partially visible?

My use case here is that I have an offset (int) into the text string. I would like to display an icon to the left of the start of the line where the offset is, thus if I have the following text (a single paragraph):

To Sherlock Holmes she is always the woman.
[*] I have seldom heard him mention her under any other name.
In his eyes she eclipses and predominates the whole of her sex. It was not that he felt any emotion akin to love for Irene Adler.

I would like to display an icon (for example) at the start of the line "I have seldom..."

If getting the paragraph bounds is not possible, is it possible to get the text offset of the paragraph within the overall text and then the bounds of a particular character(s) within the paragraph?

@JordanMartinez
Copy link
Contributor Author

Could you open a new issue for this? I'd rather continue the conversation there. Once opened, I'll copy/paste my response below:

I'm pretty sure the code does return the bounds of paragraph, even if it's only partially visible. Since we don't have a test verifying that, I could be wrong.
I'm not sure why it's throwing an NPE. However, it might be due to the area going through an update. If so, that might be the culprit.

If getting the paragraph bounds is not possible, is it possible to get the text offset of the paragraph within the overall text and then the bounds of a particular character(s) within the paragraph?

You can use area.getAbsolutePosition(int paragraphIndex, int columnIndex) and area.getCharacterBoundsOnScreen(int startPosition, int endPosition):

int pos = area.getAbsolutePosition(paragraphIndex, columnIndex)
Bounds b = area.getCharacterBoundsOnScreen(pos, pos + 1);

However, since this goes through the same method, I'm not sure if your "partially visible" issue will arise again.

Unfortunately, there's no area.getAbsolutePosition(int paragraphIndex, int lineIndex, int columnIndex) method yet.

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

Successfully merging this pull request may close these issues.

2 participants