}
@Override
public void paint(Graphics2D graphics) {
if (glyphVector != null) {
TextPane textPane = (TextPane)getTextPaneSkin().getComponent();
FontRenderContext fontRenderContext = Platform.getFontRenderContext();
LineMetrics lm = getEffectiveFont().getLineMetrics("", fontRenderContext);
float ascent = lm.getAscent();
int strikethroughX = Math.round(lm.getAscent() + lm.getStrikethroughOffset());
int underlineX = Math.round(lm.getAscent() + lm.getUnderlineOffset());
boolean underline = getEffectiveUnderline();
boolean strikethrough = getEffectiveStrikethrough();
graphics.setFont(getEffectiveFont());
int selectionStart = textPane.getSelectionStart();
int selectionLength = textPane.getSelectionLength();
Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);
int documentOffset = getDocumentOffset();
Span characterRange = new Span(documentOffset, documentOffset + getCharacterCount() - 1);
int width = getWidth();
int height = getHeight();
if (selectionLength > 0
&& characterRange.intersects(selectionRange)) {
// Determine the selection bounds
int x0;
if (selectionRange.start > characterRange.start) {
Bounds leadingSelectionBounds = getCharacterBounds(selectionRange.start - documentOffset);
x0 = leadingSelectionBounds.x;
} else {
x0 = 0;
}
int x1;
if (selectionRange.end < characterRange.end) {
Bounds trailingSelectionBounds = getCharacterBounds(selectionRange.end - documentOffset);
x1 = trailingSelectionBounds.x + trailingSelectionBounds.width;
} else {
x1 = width;
}
Rectangle selection = new Rectangle(x0, 0, x1 - x0, height);
// Paint the unselected text
Area unselectedArea = new Area();
unselectedArea.add(new Area(new Rectangle(0, 0, width, height)));
unselectedArea.subtract(new Area(selection));
Graphics2D textGraphics = (Graphics2D)graphics.create();
textGraphics.setColor(getEffectiveForegroundColor());
textGraphics.clip(unselectedArea);
textGraphics.drawGlyphVector(glyphVector, 0, ascent);
if (underline) {
textGraphics.drawLine(x0, underlineX, x1 - x0, underlineX);
}
if (strikethrough) {
textGraphics.drawLine(x0, strikethroughX, x1 - x0, strikethroughX);
}
textGraphics.dispose();
// Paint the selection
Color selectionColor;
if (textPane.isFocused()) {
selectionColor = getTextPaneSkin().getSelectionColor();
} else {
selectionColor = getTextPaneSkin().getInactiveSelectionColor();
}
Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
selectedTextGraphics.setColor(textPane.isFocused() &&
textPane.isEditable() ? selectionColor : getTextPaneSkin().getInactiveSelectionColor());
selectedTextGraphics.clip(selection.getBounds());
selectedTextGraphics.drawGlyphVector(glyphVector, 0, ascent);
if (underline) {
selectedTextGraphics.drawLine(0, underlineX, width, underlineX);
}