Skip to content

Commit

Permalink
Merge pull request #324 from afester/dottedUnderline
Browse files Browse the repository at this point in the history
Use the -rtfx prefix for RichTextFX specific underline properties, us the underline width as the toggle to enable/disable the underline.
  • Loading branch information
TomasMikula authored Jun 10, 2016
2 parents 7c32514 + caeac0c commit 9bb46ac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.underlined {
-fx-underline-color: red;
-fx-underline-dash-array: 2 2;
-fx-underline-width: 1;
-fx-underline-cap: butt;
-rtfx-underline-color: red;
-rtfx-underline-dash-array: 2 2;
-rtfx-underline-width: 1;
-rtfx-underline-cap: butt;
}
79 changes: 38 additions & 41 deletions richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.fxmisc.richtext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -235,55 +234,53 @@ private void updateBackground(TextExt text, int start, int end, int index) {
*/
private void updateUnderline(TextExt text, int start, int end, int index) {

// get all CSS properties for the underline

Paint underlineColor = text.underlineColorProperty().get();
Number underlineWidth = text.underlineWidthProperty().get();
if (underlineWidth != null && underlineWidth.doubleValue() > 0) {

Path underlineShape = underlineShapes.get(index);
underlineShape.setStrokeWidth(underlineWidth.doubleValue());

// get the dash array - JavaFX CSS parser seems to return either a Number[] array
// or a single value, depending on whether only one or more than one value has been
// specified in the CSS
Double[] underlineDashArray = null;
Object underlineDashArrayProp = text.underlineDashArrayProperty().get();
if (underlineDashArrayProp != null) {
if (underlineDashArrayProp.getClass().isArray()) {
Number[] numberArray = (Number[]) underlineDashArrayProp;
underlineDashArray = new Double[numberArray.length];
int idx = 0;
for (Number d : numberArray) {
underlineDashArray[idx++] = (Double) d;
// get remaining CSS properties for the underline style

Paint underlineColor = text.underlineColorProperty().get();

// get the dash array - JavaFX CSS parser seems to return either a Number[] array
// or a single value, depending on whether only one or more than one value has been
// specified in the CSS
Double[] underlineDashArray = null;
Object underlineDashArrayProp = text.underlineDashArrayProperty().get();
if (underlineDashArrayProp != null) {
if (underlineDashArrayProp.getClass().isArray()) {
Number[] numberArray = (Number[]) underlineDashArrayProp;
underlineDashArray = new Double[numberArray.length];
int idx = 0;
for (Number d : numberArray) {
underlineDashArray[idx++] = (Double) d;
}
} else {
underlineDashArray = new Double[1];
underlineDashArray[0] = ((Double) underlineDashArrayProp).doubleValue();
}
} else {
underlineDashArray = new Double[1];
underlineDashArray[0] = ((Double) underlineDashArrayProp).doubleValue();
}
}

StrokeLineCap underlineCap = text.underlineCapProperty().get();

// apply style and render the underline

Path underlineShape = underlineShapes.get(index);
if (underlineColor != null) {
underlineShape.setStroke(underlineColor);
}
if (underlineWidth != null) {
underlineShape.setStrokeWidth(underlineWidth.doubleValue());
}
if (underlineDashArray != null) {
underlineShape.getStrokeDashArray().addAll(underlineDashArray);
underlineShape.setStrokeLineCap(StrokeLineCap.BUTT);
}
if (underlineCap != null) {
underlineShape.setStrokeLineCap(underlineCap);
}

StrokeLineCap underlineCap = text.underlineCapProperty().get();

// apply style
if (underlineColor != null) {
underlineShape.setStroke(underlineColor);
}
if (underlineDashArray != null) {
underlineShape.getStrokeDashArray().addAll(underlineDashArray);
}
if (underlineCap != null) {
underlineShape.setStrokeLineCap(underlineCap);
}

if (underlineColor != null || underlineWidth != null) {

// Set path elements
PathElement[] shape = getUnderlineShape(start, end);
underlineShape.getElements().setAll(shape);
}

}


Expand Down
8 changes: 4 additions & 4 deletions richtextfx/src/main/java/org/fxmisc/richtext/TextExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public StyleableProperty<Paint> getStyleableProperty(TextExt node) {


private static final CssMetaData<TextExt, Paint> UNDERLINE_COLOR = new CssMetaData<TextExt, Paint>(
"-fx-underline-color",
"-rtfx-underline-color",
StyleConverter.getPaintConverter(),
Color.TRANSPARENT) {
@Override
Expand All @@ -191,7 +191,7 @@ public StyleableProperty<Paint> getStyleableProperty(TextExt node) {
};

private static final CssMetaData<TextExt, Number> UNDERLINE_WIDTH = new CssMetaData<TextExt, Number>(
"-fx-underline-width",
"-rtfx-underline-width",
StyleConverter.getSizeConverter(),
0) {

Expand All @@ -207,7 +207,7 @@ public StyleableProperty<Number> getStyleableProperty(TextExt node) {
};

private static final CssMetaData<TextExt, Number[]> UNDERLINE_DASH_ARRAY = new CssMetaData<TextExt, Number[]>(
"-fx-underline-dash-array",
"-rtfx-underline-dash-array",
SizeConverter.SequenceConverter.getInstance(),
new Double[0]) {

Expand All @@ -223,7 +223,7 @@ public StyleableProperty<Number[]> getStyleableProperty(TextExt node) {
};

private static final CssMetaData<TextExt, StrokeLineCap> UNDERLINE_CAP = new CssMetaData<TextExt, StrokeLineCap>(
"-fx-underline-cap",
"-rtfx-underline-cap",
new EnumConverter<StrokeLineCap>(StrokeLineCap.class),
StrokeLineCap.SQUARE) {

Expand Down

0 comments on commit 9bb46ac

Please sign in to comment.