Skip to content

Commit 5f0fb20

Browse files
committed
Clean styled text renderer.
1 parent d5093b7 commit 5f0fb20

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/swt/custom/patch/StyledTextPatcher.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Copyright (c) 2015-2017 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package org.eclipse.swt.custom.patch;
212

313
import java.lang.reflect.Field;

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/swt/custom/patch/StyledTextRenderer.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
/**
2+
* Copyright (c) 2015-2017 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package org.eclipse.swt.custom.patch;
212

13+
import java.lang.reflect.InvocationTargetException;
314
import java.lang.reflect.Method;
415

516
import org.eclipse.swt.custom.StyledText;
617
import org.eclipse.swt.custom.patch.internal.StyledTextRendererEmulator;
718
import org.eclipse.swt.custom.provisional.ILineSpacingProvider;
819
import org.eclipse.swt.graphics.TextLayout;
920

21+
/**
22+
* Class which should replace the private class
23+
* {@link org.eclipse.swt.custom.StyledTextRenderer} to support line spacing for
24+
* a given line.
25+
*
26+
*/
1027
public class StyledTextRenderer extends StyledTextRendererEmulator {
1128

1229
private ILineSpacingProvider lineSpacingProvider;
@@ -24,27 +41,35 @@ public void setLineSpacingProvider(ILineSpacingProvider lineSpacingProvider) {
2441
@Override
2542
protected TextLayout getTextLayout(int lineIndex, int orientation, int width, int lineSpacing, Object obj,
2643
Method proceed, Object[] args) throws Exception {
44+
// Compute line spacing for the given line index.
2745
int newSpacing = lineSpacing;
28-
TextLayout layout = super.getTextLayout(lineIndex, orientation, width, lineSpacing, obj, proceed, args);
2946
if (lineSpacingProvider != null) {
3047
Integer spacing = lineSpacingProvider.getLineSpacing(lineIndex);
3148
if (spacing != null) {
3249
newSpacing = spacing;
3350
}
3451
}
52+
TextLayout layout = super.getTextLayout(lineIndex, orientation, width, lineSpacing, obj, proceed, args);
3553
if (layout.getSpacing() != newSpacing) {
36-
// Update spacing
54+
// Update line spacing
3755
layout.setSpacing(newSpacing);
56+
// System.err.println("spacing changed [" + lineIndex + "] to " +
57+
// newSpacing);
3858

3959
// invalidate text layout.
4060
// call styledText.setVariableLineHeight();
41-
Method m1 = styledText.getClass().getDeclaredMethod("setVariableLineHeight");
42-
m1.setAccessible(true);
43-
m1.invoke(styledText);
61+
setVariableLineHeight();
4462

4563
// recreate text layout.
4664
layout = super.getTextLayout(lineIndex, orientation, width, newSpacing, obj, proceed, args);
4765
}
4866
return layout;
4967
}
68+
69+
private void setVariableLineHeight()
70+
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
71+
Method m1 = styledText.getClass().getDeclaredMethod("setVariableLineHeight");
72+
m1.setAccessible(true);
73+
m1.invoke(styledText);
74+
}
5075
}

eclipse/codelens/org.eclipse.codelens/src/org/eclipse/swt/custom/patch/internal/StyledTextRendererEmulator.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1+
/**
2+
* Copyright (c) 2015-2017 Angelo ZERR.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <[email protected]> - initial API and implementation
10+
*/
111
package org.eclipse.swt.custom.patch.internal;
212

313
import java.lang.reflect.Method;
4-
5-
import org.eclipse.swt.custom.StyledText;
614
import org.eclipse.swt.graphics.TextLayout;
7-
815
import javassist.util.proxy.MethodHandler;
916

17+
/**
18+
* Javassist method handler to override getTextLayout of StyledTextRenderer.
19+
*
20+
*/
1021
public class StyledTextRendererEmulator implements MethodHandler {
1122

1223
@Override
@@ -26,7 +37,7 @@ protected TextLayout getTextLayout(int lineIndex, int orientation, int width, in
2637
args[0] = lineIndex;
2738
args[1] = orientation;
2839
args[2] = width;
29-
args[3] = lineSpacing;
40+
args[3] = lineSpacing;
3041
return (TextLayout) proceed.invoke(obj, args);
3142
}
3243

0 commit comments

Comments
 (0)