Skip to content

Commit aff062a

Browse files
committed
WIP fix for #160 - Configurable Line Height. This currently makes the line height configurable when word wrap is NOT enabled. Line number list is also updated. Fold indicators do not currently work properly - they only update in the demo app because I've tied '8' and '9' keypresses to tweaking the line height for quick testing. I think what still needs to be done is: fold indicator immediate updating, fold indicator rendering of 'active' fold line, and *everything* when word wrap is enabled
1 parent de0604f commit aff062a

File tree

7 files changed

+118
-26
lines changed

7 files changed

+118
-26
lines changed

RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/RSyntaxTextArea.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,18 @@ public boolean forceReparsing(Parser parser) {
10441044
}
10451045

10461046

1047+
/**
1048+
* Overridden to return account for all fonts being used
1049+
* when determining the line height.
1050+
*
1051+
* @return The line height.
1052+
*/
1053+
@Override
1054+
public int getActualLineHeight() {
1055+
return lineHeight;
1056+
}
1057+
1058+
10471059
/**
10481060
* Returns whether bracket matching should be animated.
10491061
*
@@ -1411,17 +1423,6 @@ public int getLastVisibleOffset() {
14111423
}
14121424

14131425

1414-
/**
1415-
* Returns the height to use for a line of text in this text area.
1416-
*
1417-
* @return The height of a line of text in this text area.
1418-
*/
1419-
@Override
1420-
public int getLineHeight() {
1421-
return lineHeight;
1422-
}
1423-
1424-
14251426
public LinkGenerator getLinkGenerator() {
14261427
return linkGenerator;
14271428
}

RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/SyntaxView.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,13 @@ public void paint(Graphics g, Shape a) {
736736
int heightAbove = clip.y - alloc.y;
737737
int linesAbove = Math.max(0, heightAbove / lineHeight);
738738

739+
// Top and bottom spacing around font if line height > font size
740+
int verticalSpacing = (lineHeight - host.getActualLineHeight()) / 2;
741+
739742
FoldManager fm = host.getFoldManager();
740743
linesAbove += fm.getHiddenLineCountAbove(linesAbove, true);
741744
Rectangle lineArea = lineToRect(a, linesAbove);
742-
int y = lineArea.y + ascent;
745+
int y = lineArea.y + verticalSpacing + ascent;
743746
int x = lineArea.x;
744747
Element map = getElement();
745748
int lineCount = map.getElementCount();
@@ -790,8 +793,8 @@ public void paint(Graphics g, Shape a) {
790793
Color c = RSyntaxUtilities.getFoldedLineBottomColor(host);
791794
if (c!=null) {
792795
g.setColor(c);
793-
g.drawLine(x,y+lineHeight-ascent-1,
794-
host.getWidth(),y+lineHeight-ascent-1);
796+
int lineY = y - verticalSpacing - ascent + lineHeight - 1;
797+
g.drawLine(x, lineY, host.getWidth(), lineY);
795798
}
796799

797800
// Skip to next line to paint, taking extra care for lines with

RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/FoldIndicator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ protected void paintComponent(Graphics g) {
485485
int topLine = (visibleRect.y-textAreaInsets.top)/cellHeight;
486486
int y = topLine*cellHeight +
487487
(cellHeight-collapsedFoldIcon.getIconHeight())/2;
488-
y += textAreaInsets.top;
488+
y += textAreaInsets.top - 1;
489489

490490
// Get the first and last lines to paint.
491491
FoldManager fm = rsta.getFoldManager();
@@ -521,7 +521,7 @@ protected void paintComponent(Graphics g) {
521521
if (paintingOutlineLine) {
522522
g.setColor(getForeground());
523523
int w2 = width / 2;
524-
g.drawLine(w2, y + cellHeight / 2, w2, y + cellHeight);
524+
g.drawLine(w2, y, w2, y + cellHeight / 2);
525525
}
526526
}
527527
if (mouseOverFoldIcon) {

RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/Gutter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,10 +1224,12 @@ public void propertyChange(PropertyChangeEvent e) {
12241224

12251225
String name = e.getPropertyName();
12261226

1227-
// If they change the text area's font, we need to update cell
1228-
// heights to match the font's height.
1227+
// If they change the text area's font, or any other property
1228+
// related to line height, we need to update cell heights to
1229+
// match the font's height.
12291230
if ("font".equals(name) ||
1230-
RSyntaxTextArea.SYNTAX_SCHEME_PROPERTY.equals(name)) {
1231+
RSyntaxTextArea.SYNTAX_SCHEME_PROPERTY.equals(name) ||
1232+
RTextArea.LINE_HEIGHT_MULTIPLIER_PROPERTY.equals(name)) {
12311233
for (int i=0; i<getComponentCount(); i++) {
12321234
AbstractGutterComponent agc =
12331235
(AbstractGutterComponent)getComponent(i);

RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/LineNumberList.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ protected void paintComponent(Graphics g) {
377377
return;
378378
}
379379

380+
// Top and bottom spacing around font if line height > font size
381+
int verticalSpacing = (cellHeight - textArea.getActualLineHeight()) / 2;
382+
380383
// Get where to start painting (top of the row), and where to paint
381384
// the line number (drawString expects y==baseline).
382385
// We need to be "scrolled up" just enough for the missing part of
@@ -388,7 +391,7 @@ protected void paintComponent(Graphics g) {
388391
}
389392
int topLine = (visibleRect.y-textAreaInsets.top)/cellHeight;
390393
int actualTopY = topLine*cellHeight + textAreaInsets.top;
391-
int y = actualTopY + ascent;
394+
int y = actualTopY + verticalSpacing + ascent;
392395

393396
// Get the actual first line to paint, taking into account folding.
394397
FoldManager fm = null;
@@ -726,9 +729,9 @@ public void setTextArea(RTextArea textArea) {
726729

727730

728731
/**
729-
* Changes the height of the cells in the JList so that they are as tall as
730-
* font. This function should be called whenever the user changes the Font
731-
* of <code>textArea</code>.
732+
* Changes the height of the cells in this component so that they are as
733+
* tall as the font. This function should be called whenever the user
734+
* changes the Font of <code>textArea</code>.
732735
*/
733736
private void updateCellHeights() {
734737
if (textArea!=null) {
@@ -744,8 +747,8 @@ private void updateCellHeights() {
744747

745748

746749
/**
747-
* Changes the width of the cells in the JList so you can see every digit
748-
* of each.
750+
* Changes the width of the cells in this component so you can see every
751+
* digit of each.
749752
*/
750753
void updateCellWidths() {
751754

RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/RTextAreaBase.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public abstract class RTextAreaBase extends JTextArea {
4848
public static final String CURRENT_LINE_HIGHLIGHT_COLOR_PROPERTY = "RTA.currentLineHighlightColor";
4949
public static final String CURRENT_LINE_HIGHLIGHT_FADE_PROPERTY = "RTA.currentLineHighlightFade";
5050
public static final String HIGHLIGHT_CURRENT_LINE_PROPERTY = "RTA.currentLineHighlight";
51+
public static final String LINE_HEIGHT_MULTIPLIER_PROPERTY = "RTA.lineHeightMultiplier";
5152
public static final String ROUNDED_SELECTION_PROPERTY = "RTA.roundedSelection";
5253

5354
private boolean tabsEmulatedWithSpaces; // If true, tabs will be expanded to spaces.
@@ -60,6 +61,7 @@ public abstract class RTextAreaBase extends JTextArea {
6061
private int marginSizeInChars; // How many 'm' widths the margin line is over.
6162
private boolean fadeCurrentLineHighlight; // "Fade effect" for current line highlight.
6263
private boolean roundedSelectionEdges;
64+
private float lineHeightMultiplier;
6365
private int previousCaretY;
6466
int currentCaretY; // Used to know when to highlight current line.
6567

@@ -311,6 +313,21 @@ protected void forceCurrentLineHighlightRepaint() {
311313
}
312314

313315

316+
/**
317+
* Returns the actual height of a line of text in this editor. This
318+
* is essentially the font height. There may be additional space
319+
* between lines; to get the actual, full height of a line, see
320+
* {@link #getLineHeight()}.
321+
*
322+
* @return The height of a line of text, not including any extra
323+
* spacing.
324+
* @see #getLineHeight()
325+
*/
326+
public int getActualLineHeight() {
327+
return getRowHeight();
328+
}
329+
330+
314331
/**
315332
* Returns the <code>java.awt.Color</code> used as the background color for
316333
* this text area. If a <code>java.awt.Image</code> image is currently
@@ -532,11 +549,32 @@ public final int getLineEndOffsetOfCurrentLine() {
532549

533550
/**
534551
* Returns the height of a line of text in this text area.
552+
* This may be larger than the height of the font if extra
553+
* spacing is requested between lines.<p>
554+
* This is equal to the font's height times the line height
555+
* multiplier.
535556
*
536557
* @return The height of a line of text.
558+
* @see #getLineHeightMultiplier()
559+
* @see #getActualLineHeight()
537560
*/
538561
public int getLineHeight() {
539-
return getRowHeight();
562+
return (int)(getActualLineHeight() * lineHeightMultiplier);
563+
}
564+
565+
566+
/**
567+
* Returns a multiplier used to determine a line's height.
568+
* This is multiplied by the font height to determine how
569+
* tall each line of text is. This can be used to add
570+
* extra whitespace between each line.
571+
*
572+
* @return The line height multiplier.
573+
* @see #setLineHeightMultiplier(float)
574+
* #see #getLineHeight()
575+
*/
576+
public float getLineHeightMultiplier() {
577+
return lineHeightMultiplier;
540578
}
541579

542580

@@ -631,6 +669,7 @@ protected void init() {
631669
enableEvents(AWTEvent.COMPONENT_EVENT_MASK|AWTEvent.KEY_EVENT_MASK);
632670

633671
// Defaults for various properties.
672+
lineHeightMultiplier = 1;
634673
setHighlightCurrentLine(true);
635674
setCurrentLineHighlightColor(getDefaultCurrentLineHighlightColor());
636675
setMarginLineEnabled(false);
@@ -972,6 +1011,35 @@ public void setHighlightCurrentLine(boolean highlight) {
9721011
}
9731012

9741013

1014+
/**
1015+
* Sets the line height multiplier.
1016+
* This is multiplied by the font height to determine how
1017+
* tall each line of text is. This can be used to add
1018+
* extra whitespace between each line.
1019+
*
1020+
* @param multiplier The new multiplier value. This
1021+
* should be between {@code 0.9} and {code 3},
1022+
* inclusive.
1023+
* @see #getLineHeightMultiplier()
1024+
* @see #getLineHeight()
1025+
*/
1026+
public void setLineHeightMultiplier(float multiplier) {
1027+
1028+
if (multiplier < 0.9 || multiplier > 3) {
1029+
throw new IllegalArgumentException("Invalid multiplier: " +
1030+
multiplier);
1031+
}
1032+
1033+
if (multiplier != lineHeightMultiplier) {
1034+
float old = lineHeightMultiplier;
1035+
lineHeightMultiplier = multiplier;
1036+
firePropertyChange(LINE_HEIGHT_MULTIPLIER_PROPERTY, old,
1037+
multiplier);
1038+
repaint();
1039+
}
1040+
}
1041+
1042+
9751043
/**
9761044
* Sets whether word wrap is enabled. This is overridden so that
9771045
* the "current line highlight" gets updated if it needs to be.

RSyntaxTextAreaDemo/src/main/java/org/fife/ui/rsyntaxtextarea/demo/DemoRootPane.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,21 @@ private RSyntaxTextArea createTextArea() {
261261

262262
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_E, ctrlShift), "copyAsStyledTextEclipse");
263263
am.put("copyAsStyledTextEclipse", createCopyAsStyledTextAction("eclipse"));
264+
265+
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_8, 0), "inc");
266+
am.put("inc", new AbstractAction() {
267+
@Override
268+
public void actionPerformed(ActionEvent e) {
269+
textArea.setLineHeightMultiplier(textArea.getLineHeightMultiplier() + 0.1f);
270+
}
271+
});
272+
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_9, 0), "dec");
273+
am.put("dec", new AbstractAction() {
274+
@Override
275+
public void actionPerformed(ActionEvent e) {
276+
textArea.setLineHeightMultiplier(textArea.getLineHeightMultiplier() - 0.1f);
277+
}
278+
});
264279
} catch (IOException ioe) {
265280
ioe.printStackTrace();
266281
}

0 commit comments

Comments
 (0)