Skip to content

Commit

Permalink
Issue FXMisc#594: Properly calculate the background and underline sha…
Browse files Browse the repository at this point in the history
…pes for non-consecutive ranges
  • Loading branch information
afester committed Sep 28, 2017
1 parent 28a01b3 commit e59bf89
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.fxmisc.richtext.style;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.fxmisc.flowless.Cell;
import org.fxmisc.flowless.VirtualFlow;
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.nitorcreations.junit.runners.NestedRunner;

import javafx.scene.layout.Region;
import javafx.scene.shape.Path;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;

@RunWith(NestedRunner.class)
public class StylingTests extends InlineCssTextAreaAppTest {

private final static String firstWord = "Hello ";
private final static String styledWord1 = "World";
private final static String moreText = " and also the ";
private final static String styledWord2 = "Sun";
private final static String remainingLine = " and Moon";


/**
*
* @param index The index of the desired paragraph box
* @return The paragraph box for the paragraph at the specified index
*/
private Region getParagraphBox(int index) {
VirtualFlow flow = (VirtualFlow) area.getChildrenUnmodifiable().get(index);
Cell gsa = (Cell) flow.getCell(0);

// get the ParagraphBox (protected subclass of Region)
return (Region) gsa.getNode();
}


/**
* @return A list of text nodes which render the current text.
*/
private List<Text> getTextNodes() {
// get the ParagraphBox (protected subclass of Region)
Region paragraphBox = getParagraphBox(0);

// get the ParagraphText (protected subclass of TextFlow)
TextFlow tf = (TextFlow) paragraphBox.getChildrenUnmodifiable().get(0);

List<Text> result = new ArrayList<>();
tf.getChildrenUnmodifiable().filtered(n -> n instanceof Text).forEach(n -> result.add((Text) n));
return result;
}

/**
* @return A list of nodes which render the underlines for the current text.
*/
private List<Path> getUnderlinePaths() {
// get the ParagraphBox (protected subclass of Region)
Region paragraphBox = getParagraphBox(0);

// get the ParagraphText (protected subclass of TextFlow)
TextFlow tf = (TextFlow) paragraphBox.getChildrenUnmodifiable().get(0);

List<Path> result = new ArrayList<>();
tf.getChildrenUnmodifiable().filtered(n -> n instanceof Path).forEach(n -> result.add((Path) n));

This comment has been minimized.

Copy link
@JordanMartinez

JordanMartinez Sep 28, 2017

Won't this return the background shapes and border shapes as well if we add more tests that test for those things, too.

return result.subList(2, result.size());
}


@Test
public void simpleStyling() {
// setup
interact(() -> {
area.replaceText(firstWord + styledWord1 + remainingLine);
});

// expected: one text node which contains the complete text
List<Text> textNodes = getTextNodes();
assertEquals(1, textNodes.size());

// Set word "World" to bold
interact(() -> {
area.setStyle(firstWord.length(), firstWord.length() + styledWord1.length(), "-fx-font-weight: bold;");
});

// expected: three text nodes
textNodes = getTextNodes();
assertEquals(3, textNodes.size());

Text first = textNodes.get(0);
assertEquals("Hello ", first.getText());
assertEquals("Regular", first.getFont().getStyle());

Text second = textNodes.get(1);
assertEquals("World", second.getText());
assertEquals("Bold", second.getFont().getStyle());

Text third = textNodes.get(2);
assertEquals(" and Moon", third.getText());
assertEquals("Regular", third.getFont().getStyle());
}


@Test
public void underlineStyling() {
// setup
interact(() -> {
area.replaceText(firstWord + styledWord1 + moreText + styledWord2 + remainingLine);
// "Hello World and also the Sun and Moon"
});

// expected: one text node which contains the complete text
List<Text> textNodes = getTextNodes();
assertEquals(1, textNodes.size());
assertEquals(firstWord + styledWord1 + moreText + styledWord2 + remainingLine,
textNodes.get(0).getText());

interact(() -> {
final int start1 = firstWord.length();
final int end1 = start1 + styledWord1.length();
area.setStyle(start1, end1,
"-rtfx-underline-color: red; -rtfx-underline-dash-array: 2 2; -rtfx-underline-width: 1; -rtfx-underline-cap: butt;");

final int start2 = end1 + moreText.length();
final int end2 = start2 + styledWord2.length();
System.err.printf("%d / %d\n", start2, end2);
area.setStyle(start2, end2,
"-rtfx-underline-color: red; -rtfx-underline-dash-array: 2 2; -rtfx-underline-width: 1; -rtfx-underline-cap: butt;");

// "Hello World and also the Sun and Moon"
// ----- ---
});

// expected: five text nodes
textNodes = getTextNodes();
assertEquals(5, textNodes.size());

Text first = textNodes.get(0);
assertEquals(firstWord, first.getText());
Text second = textNodes.get(1);
assertEquals(styledWord1, second.getText());
Text third = textNodes.get(2);
assertEquals(moreText, third.getText());
Text fourth = textNodes.get(3);
assertEquals(styledWord2, fourth.getText());
Text fifth = textNodes.get(4);
assertEquals(remainingLine, fifth.getText());

// determine the underline paths - need to be two of them!
List<Path> underlineNodes = getUnderlinePaths();
assertEquals(2, underlineNodes.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,12 @@ private void updateSharedShapeRange(T value, int start, int end) {
int lastIndex = ranges.size() - 1;
Tuple2<T, IndexRange> lastShapeValueRange = ranges.get(lastIndex);
T lastShapeValue = lastShapeValueRange._1;
if (lastShapeValue.equals(value)) {

// calculate smallest possible position which is consecutive to the given start position
final int prevEndNext = lastShapeValueRange.get2().getEnd() + 1;
if (start <= prevEndNext && // Consecutive?
lastShapeValue.equals(value)) { // Same style?

IndexRange lastRange = lastShapeValueRange._2;
IndexRange extendedRange = new IndexRange(lastRange.getStart(), end);
ranges.set(lastIndex, Tuples.t(lastShapeValue, extendedRange));
Expand Down

0 comments on commit e59bf89

Please sign in to comment.