Skip to content

Commit 7725641

Browse files
committed
Use DrawableTextUtilities for Label and TextFlow figures
This adds a new getTextUtilities() method to the IFigure interface. The method is overriden by the RootFigure and returns an object of type DrawableTextUtilities, which is created for the canvas hooked to the LWS. When using this method, users are guaranteed that the font sizes returned calculated by this instance match the zoom level of the monitor they are painted on.
1 parent efc73d2 commit 7725641

File tree

8 files changed

+40
-40
lines changed

8 files changed

+40
-40
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
## Draw2D
55

6+
- The `getTextUtilities()` method has been moved from the `Label` and `TextFlow` class to the `IFigure` interface. By
7+
default, this method returns a `DrawableFigureCanvas` rather than `TextUtilities.INSTANCE`, which is an object created
8+
for the `FigureCanvas` containing the given figure.
9+
610
## GEF
711

812
## Zest

org.eclipse.draw2d/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %Plugin.name
44
Bundle-SymbolicName: org.eclipse.draw2d;singleton:=true
5-
Bundle-Version: 3.20.200.qualifier
5+
Bundle-Version: 3.21.0.qualifier
66
Bundle-Vendor: %Plugin.providerName
77
Bundle-Localization: plugin
88
Export-Package: org.eclipse.draw2d,

org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,17 @@ public final Dimension getSize() {
908908
return getBounds().getSize();
909909
}
910910

911+
/**
912+
* @see IFigure#getTextUtilities()
913+
*/
914+
@Override
915+
public TextUtilities getTextUtilities() {
916+
if (getParent() != null) {
917+
return getParent().getTextUtilities();
918+
}
919+
return TextUtilities.INSTANCE;
920+
}
921+
911922
/**
912923
* @see IFigure#getToolTip()
913924
*/

org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,15 @@ default Point getLocation() {
459459
*/
460460
Dimension getSize();
461461

462+
/**
463+
* Gets the {@link TextUtilities} instance to be used in measurement
464+
* calculations.
465+
*
466+
* @return a {@link TextUtilities} instance
467+
* @since 3.21
468+
*/
469+
TextUtilities getTextUtilities();
470+
462471
/**
463472
* Returns a IFigure that is the tooltip for this IFigure.
464473
*

org.eclipse.draw2d/src/org/eclipse/draw2d/Label.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2010 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -682,18 +682,6 @@ public void setTextPlacement(int where) {
682682
repaint();
683683
}
684684

685-
/**
686-
* Gets the <code>TextUtilities</code> instance to be used in measurement
687-
* calculations.
688-
*
689-
* @return a <code>TextUtilities</code> instance
690-
* @since 3.4
691-
*/
692-
@SuppressWarnings("static-method")
693-
public TextUtilities getTextUtilities() {
694-
return TextUtilities.INSTANCE;
695-
}
696-
697685
/**
698686
* Gets the string that will be appended to the text when the label is
699687
* truncated. By default, this returns an ellipsis.

org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -39,6 +39,7 @@
3939
import org.eclipse.swt.widgets.Listener;
4040

4141
import org.eclipse.draw2d.geometry.Rectangle;
42+
import org.eclipse.draw2d.internal.DrawableTextUtilities;
4243

4344
/**
4445
* The LightweightSystem is the link between SWT and Draw2d. It is the component
@@ -61,6 +62,7 @@ public class LightweightSystem {
6162
private EventDispatcher dispatcher;
6263
private UpdateManager manager = new DeferredUpdateManager();
6364
private int ignoreResize;
65+
private TextUtilities textUtilities;
6466

6567
/**
6668
* Constructs a LightweightSystem on Canvas <i>c</i>.
@@ -228,6 +230,7 @@ public void setControl(Canvas c) {
228230
return;
229231
}
230232
canvas = c;
233+
textUtilities = new DrawableTextUtilities(canvas);
231234
if ((c.getStyle() & SWT.DOUBLE_BUFFERED) != 0) {
232235
getUpdateManager().setGraphicsSource(new NativeGraphicsSource(canvas));
233236
} else {
@@ -326,6 +329,15 @@ public Color getForegroundColor() {
326329
return null;
327330
}
328331

332+
/** @see IFigure#getTextUtilities() */
333+
@Override
334+
public TextUtilities getTextUtilities() {
335+
if (textUtilities != null) {
336+
return textUtilities;
337+
}
338+
return TextUtilities.INSTANCE;
339+
}
340+
329341
/** @see IFigure#getUpdateManager() */
330342
@Override
331343
public UpdateManager getUpdateManager() {

org.eclipse.draw2d/src/org/eclipse/draw2d/text/TextFlow.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2010 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -20,7 +20,6 @@
2020

2121
import org.eclipse.draw2d.ColorConstants;
2222
import org.eclipse.draw2d.Graphics;
23-
import org.eclipse.draw2d.TextUtilities;
2423
import org.eclipse.draw2d.geometry.Dimension;
2524
import org.eclipse.draw2d.geometry.Point;
2625
import org.eclipse.draw2d.geometry.Rectangle;
@@ -726,16 +725,4 @@ protected FlowUtilities getFlowUtilities() {
726725
return FlowUtilities.INSTANCE;
727726
}
728727

729-
/**
730-
* Gets the <code>TextUtilities</code> instance to be used in measurement
731-
* calculations.
732-
*
733-
* @return a <code>TextUtilities</code> instance
734-
* @since 3.4
735-
*/
736-
@SuppressWarnings("static-method")
737-
protected TextUtilities getTextUtilities() {
738-
return TextUtilities.INSTANCE;
739-
}
740-
741728
}

org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@
8181
import org.eclipse.draw2d.LightweightSystem;
8282
import org.eclipse.draw2d.MarginBorder;
8383
import org.eclipse.draw2d.PositionConstants;
84-
import org.eclipse.draw2d.TextUtilities;
8584
import org.eclipse.draw2d.Triangle;
8685
import org.eclipse.draw2d.geometry.Dimension;
87-
import org.eclipse.draw2d.internal.DrawableTextUtilities;
8886

8987
import org.eclipse.gef.GraphicalViewer;
9088
import org.eclipse.gef.dnd.TemplateTransfer;
@@ -1116,7 +1114,6 @@ protected void updateState() {
11161114
private class TitleLabel extends Label {
11171115
protected static final Border BORDER = new MarginBorder(4, 3, 4, 3);
11181116
protected static final Border TOOL_TIP_BORDER = new MarginBorder(0, 2, 0, 2);
1119-
private TextUtilities textUtilities;
11201117

11211118
public TitleLabel(boolean isHorizontal) {
11221119
super(GEFMessages.Palette_Label, InternalImages.get(InternalImages.IMG_PALETTE));
@@ -1128,14 +1125,6 @@ public TitleLabel(boolean isHorizontal) {
11281125
setForegroundColor(ColorConstants.listForeground);
11291126
}
11301127

1131-
@Override
1132-
public TextUtilities getTextUtilities() {
1133-
if (textUtilities == null) {
1134-
textUtilities = new DrawableTextUtilities(paletteContainer);
1135-
}
1136-
return textUtilities;
1137-
}
1138-
11391128
@Override
11401129
public IFigure getToolTip() {
11411130
if (isTextTruncated()) {

0 commit comments

Comments
 (0)