Skip to content

Commit e3143fa

Browse files
committed
Extract GC related operation in TextUtilities and FigureUtilities to be Drawable aware
This commit introduces two new utility classes to fully replace TextUtilities and partially replace FigureUtilities, when calculations on a GC are involded. Therefor DrawableTextUtilities or DrawableFigureUtilities can to be instantiated with a Drawable passed as context when a GC is instantiated.
1 parent b92c467 commit e3143fa

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Yatta - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.draw2d.internal;
14+
15+
import java.util.Objects;
16+
17+
import org.eclipse.swt.graphics.Font;
18+
import org.eclipse.swt.graphics.FontMetrics;
19+
import org.eclipse.swt.graphics.GC;
20+
import org.eclipse.swt.widgets.Control;
21+
22+
import org.eclipse.draw2d.geometry.Dimension;
23+
24+
/**
25+
* Provides miscellaneous Figure operations calculated with the zoom context of
26+
* the the provided {@code Drawable}.
27+
*
28+
* All GC related operations are mirrored from {@code FigureUtilities}
29+
*/
30+
public class DrawableFigureUtilities {
31+
private final GC gc;
32+
private Font appliedFont;
33+
private FontMetrics metrics;
34+
35+
public DrawableFigureUtilities(Control source) {
36+
gc = new GC(source);
37+
source.addDisposeListener(e -> {
38+
gc.dispose();
39+
});
40+
appliedFont = gc.getFont();
41+
}
42+
43+
/**
44+
* Returns the FontMetrics associated with the passed Font.
45+
*
46+
* @param f the font
47+
* @return the FontMetrics for the given font
48+
* @see GC#getFontMetrics()
49+
*/
50+
public FontMetrics getFontMetrics(Font f) {
51+
setFont(f);
52+
if (metrics == null) {
53+
metrics = gc.getFontMetrics();
54+
}
55+
return metrics;
56+
}
57+
58+
/**
59+
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. Tab
60+
* expansion and carriage return processing are performed.
61+
*
62+
* @param s the string
63+
* @param f the font
64+
* @return the text's dimensions
65+
* @see GC#textExtent(String)
66+
*/
67+
protected org.eclipse.swt.graphics.Point getTextDimension(String s, Font f) {
68+
setFont(f);
69+
return gc.textExtent(s);
70+
}
71+
72+
/**
73+
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. No tab
74+
* expansion or carriage return processing will be performed.
75+
*
76+
* @param s the string
77+
* @param f the font
78+
* @return the string's dimensions
79+
* @see GC#stringExtent(java.lang.String)
80+
*/
81+
protected org.eclipse.swt.graphics.Point getStringDimension(String s, Font f) {
82+
setFont(f);
83+
return gc.stringExtent(s);
84+
}
85+
86+
/**
87+
* Returns the Dimensions of the given text, converting newlines and tabs
88+
* appropriately.
89+
*
90+
* @param text the text
91+
* @param f the font
92+
* @return the dimensions of the given text
93+
*/
94+
public Dimension getTextExtents(String text, Font f) {
95+
return new Dimension(getTextDimension(text, f));
96+
}
97+
98+
/**
99+
* Returns the Dimensions of <i>s</i> in Font <i>f</i>.
100+
*
101+
* @param s the string
102+
* @param f the font
103+
* @return the dimensions of the given string
104+
*/
105+
public Dimension getStringExtents(String s, Font f) {
106+
return new Dimension(getStringDimension(s, f));
107+
}
108+
109+
/**
110+
* Returns the Dimensions of the given text, converting newlines and tabs
111+
* appropriately.
112+
*
113+
* @param s the string
114+
* @param f the font
115+
* @param result the Dimension that will contain the result of this calculation
116+
*/
117+
public void getTextExtents(String s, Font f, Dimension result) {
118+
org.eclipse.swt.graphics.Point pt = getTextDimension(s, f);
119+
result.width = pt.x;
120+
result.height = pt.y;
121+
}
122+
123+
/**
124+
* Returns the width of <i>s</i> in Font <i>f</i>.
125+
*
126+
* @param s the string
127+
* @param f the font
128+
* @return the width
129+
*/
130+
public int getTextWidth(String s, Font f) {
131+
return getTextDimension(s, f).x;
132+
}
133+
134+
private void setFont(Font f) {
135+
if (Objects.equals(appliedFont, f)) {
136+
return;
137+
}
138+
gc.setFont(f);
139+
appliedFont = f;
140+
metrics = null;
141+
}
142+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Yatta and others.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Yatta - initial API and implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.draw2d.internal;
15+
16+
import org.eclipse.swt.graphics.Font;
17+
import org.eclipse.swt.graphics.FontMetrics;
18+
import org.eclipse.swt.widgets.Control;
19+
20+
import org.eclipse.draw2d.TextUtilities;
21+
import org.eclipse.draw2d.geometry.Dimension;
22+
23+
/**
24+
* Provides miscellaneous text operations calculated with the zoom context of
25+
* the the provided {@code Drawable}.
26+
*/
27+
public class DrawableTextUtilities extends TextUtilities {
28+
29+
private final DrawableFigureUtilities figureUtilities;
30+
31+
public DrawableTextUtilities(Control source) {
32+
figureUtilities = new DrawableFigureUtilities(source);
33+
}
34+
35+
/**
36+
* Returns the Dimensions of <i>s</i> in Font <i>f</i>.
37+
*
38+
* @param s the string
39+
* @param f the font
40+
* @return the dimensions of the given string
41+
*/
42+
@Override
43+
public Dimension getStringExtents(String s, Font f) {
44+
return figureUtilities.getStringExtents(s, f);
45+
}
46+
47+
/**
48+
* Returns the Dimensions of the given text, converting newlines and tabs
49+
* appropriately.
50+
*
51+
* @param s the text
52+
* @param f the font
53+
* @return the dimensions of the given text
54+
*/
55+
@Override
56+
public Dimension getTextExtents(String s, Font f) {
57+
return figureUtilities.getTextExtents(s, f);
58+
}
59+
60+
/**
61+
* Gets the font's ascent.
62+
*
63+
* @param font
64+
* @return the font's ascent
65+
*/
66+
@Override
67+
public int getAscent(Font font) {
68+
FontMetrics fm = figureUtilities.getFontMetrics(font);
69+
return fm.getHeight() - fm.getDescent();
70+
}
71+
72+
/**
73+
* Gets the font's descent.
74+
*
75+
* @param font
76+
* @return the font's descent
77+
*/
78+
@Override
79+
public int getDescent(Font font) {
80+
return figureUtilities.getFontMetrics(font).getDescent();
81+
}
82+
}

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

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

8789
import org.eclipse.gef.GraphicalViewer;
8890
import org.eclipse.gef.dnd.TemplateTransfer;
@@ -1114,6 +1116,7 @@ protected void updateState() {
11141116
private class TitleLabel extends Label {
11151117
protected static final Border BORDER = new MarginBorder(4, 3, 4, 3);
11161118
protected static final Border TOOL_TIP_BORDER = new MarginBorder(0, 2, 0, 2);
1119+
private TextUtilities textUtilities;
11171120

11181121
public TitleLabel(boolean isHorizontal) {
11191122
super(GEFMessages.Palette_Label, InternalImages.get(InternalImages.IMG_PALETTE));
@@ -1125,6 +1128,14 @@ public TitleLabel(boolean isHorizontal) {
11251128
setForegroundColor(ColorConstants.listForeground);
11261129
}
11271130

1131+
@Override
1132+
public TextUtilities getTextUtilities() {
1133+
if (textUtilities == null) {
1134+
textUtilities = new DrawableTextUtilities(paletteContainer);
1135+
}
1136+
return textUtilities;
1137+
}
1138+
11281139
@Override
11291140
public IFigure getToolTip() {
11301141
if (isTextTruncated()) {

0 commit comments

Comments
 (0)