From 7725641308c2a9dad4f57b9db38494e0234eab41 Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Tue, 14 Oct 2025 20:50:50 +0200 Subject: [PATCH] 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. --- CHANGELOG.md | 4 ++++ org.eclipse.draw2d/META-INF/MANIFEST.MF | 2 +- .../src/org/eclipse/draw2d/Figure.java | 11 +++++++++++ .../src/org/eclipse/draw2d/IFigure.java | 9 +++++++++ .../src/org/eclipse/draw2d/Label.java | 14 +------------- .../src/org/eclipse/draw2d/LightweightSystem.java | 14 +++++++++++++- .../src/org/eclipse/draw2d/text/TextFlow.java | 15 +-------------- .../gef/ui/palette/FlyoutPaletteComposite.java | 11 ----------- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca08315a3..0e6e5c717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## Draw2D +- The `getTextUtilities()` method has been moved from the `Label` and `TextFlow` class to the `IFigure` interface. By + default, this method returns a `DrawableFigureCanvas` rather than `TextUtilities.INSTANCE`, which is an object created + for the `FigureCanvas` containing the given figure. + ## GEF ## Zest diff --git a/org.eclipse.draw2d/META-INF/MANIFEST.MF b/org.eclipse.draw2d/META-INF/MANIFEST.MF index 6d15f5a59..403cc1ae5 100644 --- a/org.eclipse.draw2d/META-INF/MANIFEST.MF +++ b/org.eclipse.draw2d/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Plugin.name Bundle-SymbolicName: org.eclipse.draw2d;singleton:=true -Bundle-Version: 3.20.200.qualifier +Bundle-Version: 3.21.0.qualifier Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin Export-Package: org.eclipse.draw2d, diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java index ad20651a0..9c31f6810 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java @@ -908,6 +908,17 @@ public final Dimension getSize() { return getBounds().getSize(); } + /** + * @see IFigure#getTextUtilities() + */ + @Override + public TextUtilities getTextUtilities() { + if (getParent() != null) { + return getParent().getTextUtilities(); + } + return TextUtilities.INSTANCE; + } + /** * @see IFigure#getToolTip() */ diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java index 024271478..e48fb0959 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/IFigure.java @@ -459,6 +459,15 @@ default Point getLocation() { */ Dimension getSize(); + /** + * Gets the {@link TextUtilities} instance to be used in measurement + * calculations. + * + * @return a {@link TextUtilities} instance + * @since 3.21 + */ + TextUtilities getTextUtilities(); + /** * Returns a IFigure that is the tooltip for this IFigure. * diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/Label.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/Label.java index 1877bbb7e..da4fde06b 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/Label.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/Label.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -682,18 +682,6 @@ public void setTextPlacement(int where) { repaint(); } - /** - * Gets the TextUtilities instance to be used in measurement - * calculations. - * - * @return a TextUtilities instance - * @since 3.4 - */ - @SuppressWarnings("static-method") - public TextUtilities getTextUtilities() { - return TextUtilities.INSTANCE; - } - /** * Gets the string that will be appended to the text when the label is * truncated. By default, this returns an ellipsis. diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java index b9a736c4a..7daf825dd 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2024 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -39,6 +39,7 @@ import org.eclipse.swt.widgets.Listener; import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.draw2d.internal.DrawableTextUtilities; /** * The LightweightSystem is the link between SWT and Draw2d. It is the component @@ -61,6 +62,7 @@ public class LightweightSystem { private EventDispatcher dispatcher; private UpdateManager manager = new DeferredUpdateManager(); private int ignoreResize; + private TextUtilities textUtilities; /** * Constructs a LightweightSystem on Canvas c. @@ -228,6 +230,7 @@ public void setControl(Canvas c) { return; } canvas = c; + textUtilities = new DrawableTextUtilities(canvas); if ((c.getStyle() & SWT.DOUBLE_BUFFERED) != 0) { getUpdateManager().setGraphicsSource(new NativeGraphicsSource(canvas)); } else { @@ -326,6 +329,15 @@ public Color getForegroundColor() { return null; } + /** @see IFigure#getTextUtilities() */ + @Override + public TextUtilities getTextUtilities() { + if (textUtilities != null) { + return textUtilities; + } + return TextUtilities.INSTANCE; + } + /** @see IFigure#getUpdateManager() */ @Override public UpdateManager getUpdateManager() { diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/text/TextFlow.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/text/TextFlow.java index ae6f1412c..9519f52dc 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/text/TextFlow.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/text/TextFlow.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -20,7 +20,6 @@ import org.eclipse.draw2d.ColorConstants; import org.eclipse.draw2d.Graphics; -import org.eclipse.draw2d.TextUtilities; import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.draw2d.geometry.Point; import org.eclipse.draw2d.geometry.Rectangle; @@ -726,16 +725,4 @@ protected FlowUtilities getFlowUtilities() { return FlowUtilities.INSTANCE; } - /** - * Gets the TextUtilities instance to be used in measurement - * calculations. - * - * @return a TextUtilities instance - * @since 3.4 - */ - @SuppressWarnings("static-method") - protected TextUtilities getTextUtilities() { - return TextUtilities.INSTANCE; - } - } \ No newline at end of file diff --git a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java index 18ac606b1..99f988225 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java +++ b/org.eclipse.gef/src/org/eclipse/gef/ui/palette/FlyoutPaletteComposite.java @@ -81,10 +81,8 @@ import org.eclipse.draw2d.LightweightSystem; import org.eclipse.draw2d.MarginBorder; import org.eclipse.draw2d.PositionConstants; -import org.eclipse.draw2d.TextUtilities; import org.eclipse.draw2d.Triangle; import org.eclipse.draw2d.geometry.Dimension; -import org.eclipse.draw2d.internal.DrawableTextUtilities; import org.eclipse.gef.GraphicalViewer; import org.eclipse.gef.dnd.TemplateTransfer; @@ -1116,7 +1114,6 @@ protected void updateState() { private class TitleLabel extends Label { protected static final Border BORDER = new MarginBorder(4, 3, 4, 3); protected static final Border TOOL_TIP_BORDER = new MarginBorder(0, 2, 0, 2); - private TextUtilities textUtilities; public TitleLabel(boolean isHorizontal) { super(GEFMessages.Palette_Label, InternalImages.get(InternalImages.IMG_PALETTE)); @@ -1128,14 +1125,6 @@ public TitleLabel(boolean isHorizontal) { setForegroundColor(ColorConstants.listForeground); } - @Override - public TextUtilities getTextUtilities() { - if (textUtilities == null) { - textUtilities = new DrawableTextUtilities(paletteContainer); - } - return textUtilities; - } - @Override public IFigure getToolTip() { if (isTextTruncated()) {