-
Notifications
You must be signed in to change notification settings - Fork 53
Handle HiDPI scaling in GEF diagrams #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
org.eclipse.draw2d/src/org/eclipse/draw2d/internal/DrawableFigureUtilities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Yatta 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 | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Yatta - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.draw2d.internal; | ||
|
||
import java.util.Objects; | ||
|
||
import org.eclipse.swt.graphics.Font; | ||
import org.eclipse.swt.graphics.FontMetrics; | ||
import org.eclipse.swt.graphics.GC; | ||
import org.eclipse.swt.widgets.Control; | ||
|
||
import org.eclipse.draw2d.geometry.Dimension; | ||
|
||
/** | ||
* Provides miscellaneous Figure operations calculated with the zoom context of | ||
* the the provided {@code Drawable}. | ||
* | ||
* All GC related operations are mirrored from {@code FigureUtilities} | ||
*/ | ||
public class DrawableFigureUtilities { | ||
private final GC gc; | ||
private Font appliedFont; | ||
private FontMetrics metrics; | ||
|
||
public DrawableFigureUtilities(Control source) { | ||
gc = new GC(source); | ||
source.addDisposeListener(e -> { | ||
gc.dispose(); | ||
}); | ||
appliedFont = gc.getFont(); | ||
} | ||
|
||
/** | ||
* Returns the FontMetrics associated with the passed Font. | ||
* | ||
* @param f the font | ||
* @return the FontMetrics for the given font | ||
* @see GC#getFontMetrics() | ||
*/ | ||
public FontMetrics getFontMetrics(Font f) { | ||
setFont(f); | ||
if (metrics == null) { | ||
metrics = gc.getFontMetrics(); | ||
} | ||
return metrics; | ||
} | ||
|
||
/** | ||
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. Tab | ||
* expansion and carriage return processing are performed. | ||
* | ||
* @param s the string | ||
* @param f the font | ||
* @return the text's dimensions | ||
* @see GC#textExtent(String) | ||
*/ | ||
protected org.eclipse.swt.graphics.Point getTextDimension(String s, Font f) { | ||
setFont(f); | ||
return gc.textExtent(s); | ||
} | ||
|
||
/** | ||
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. No tab | ||
* expansion or carriage return processing will be performed. | ||
* | ||
* @param s the string | ||
* @param f the font | ||
* @return the string's dimensions | ||
* @see GC#stringExtent(java.lang.String) | ||
*/ | ||
protected org.eclipse.swt.graphics.Point getStringDimension(String s, Font f) { | ||
setFont(f); | ||
return gc.stringExtent(s); | ||
} | ||
|
||
/** | ||
* Returns the Dimensions of the given text, converting newlines and tabs | ||
* appropriately. | ||
* | ||
* @param text the text | ||
* @param f the font | ||
* @return the dimensions of the given text | ||
*/ | ||
public Dimension getTextExtents(String text, Font f) { | ||
return new Dimension(getTextDimension(text, f)); | ||
} | ||
|
||
/** | ||
* Returns the Dimensions of <i>s</i> in Font <i>f</i>. | ||
* | ||
* @param s the string | ||
* @param f the font | ||
* @return the dimensions of the given string | ||
*/ | ||
public Dimension getStringExtents(String s, Font f) { | ||
return new Dimension(getStringDimension(s, f)); | ||
} | ||
|
||
/** | ||
* Returns the Dimensions of the given text, converting newlines and tabs | ||
* appropriately. | ||
* | ||
* @param s the string | ||
* @param f the font | ||
* @param result the Dimension that will contain the result of this calculation | ||
*/ | ||
public void getTextExtents(String s, Font f, Dimension result) { | ||
org.eclipse.swt.graphics.Point pt = getTextDimension(s, f); | ||
result.width = pt.x; | ||
result.height = pt.y; | ||
} | ||
|
||
/** | ||
* Returns the width of <i>s</i> in Font <i>f</i>. | ||
* | ||
* @param s the string | ||
* @param f the font | ||
* @return the width | ||
*/ | ||
public int getTextWidth(String s, Font f) { | ||
return getTextDimension(s, f).x; | ||
} | ||
|
||
private void setFont(Font f) { | ||
if (Objects.equals(appliedFont, f)) { | ||
return; | ||
} | ||
gc.setFont(f); | ||
appliedFont = f; | ||
metrics = null; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
org.eclipse.draw2d/src/org/eclipse/draw2d/internal/DrawableTextUtilities.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 Yatta 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 | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Yatta - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.draw2d.internal; | ||
|
||
import org.eclipse.swt.graphics.Font; | ||
import org.eclipse.swt.graphics.FontMetrics; | ||
import org.eclipse.swt.widgets.Control; | ||
|
||
import org.eclipse.draw2d.TextUtilities; | ||
import org.eclipse.draw2d.geometry.Dimension; | ||
|
||
/** | ||
* Provides miscellaneous text operations calculated with the zoom context of | ||
* the the provided {@code Drawable}. | ||
*/ | ||
public class DrawableTextUtilities extends TextUtilities { | ||
|
||
private final DrawableFigureUtilities figureUtilities; | ||
|
||
public DrawableTextUtilities(Control source) { | ||
figureUtilities = new DrawableFigureUtilities(source); | ||
} | ||
|
||
/** | ||
* Returns the Dimensions of <i>s</i> in Font <i>f</i>. | ||
* | ||
* @param s the string | ||
* @param f the font | ||
* @return the dimensions of the given string | ||
*/ | ||
@Override | ||
public Dimension getStringExtents(String s, Font f) { | ||
return figureUtilities.getStringExtents(s, f); | ||
} | ||
|
||
/** | ||
* Returns the Dimensions of the given text, converting newlines and tabs | ||
* appropriately. | ||
* | ||
* @param s the text | ||
* @param f the font | ||
* @return the dimensions of the given text | ||
*/ | ||
@Override | ||
public Dimension getTextExtents(String s, Font f) { | ||
return figureUtilities.getTextExtents(s, f); | ||
} | ||
|
||
/** | ||
* Gets the font's ascent. | ||
* | ||
* @param font | ||
* @return the font's ascent | ||
*/ | ||
@Override | ||
public int getAscent(Font font) { | ||
FontMetrics fm = figureUtilities.getFontMetrics(font); | ||
return fm.getHeight() - fm.getDescent(); | ||
} | ||
|
||
/** | ||
* Gets the font's descent. | ||
* | ||
* @param font | ||
* @return the font's descent | ||
*/ | ||
@Override | ||
public int getDescent(Font font) { | ||
return figureUtilities.getFontMetrics(font).getDescent(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is a bit interesting. Whatever you do here, will break some scenarios, depending whether the GC that is used to render the Figure is on advanced mode or not. The "safest" approach would probably be to always and everywhere use a GC in advanced mode - at least when the flag is set to true. Is there anything speaking against that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've got some mixed feelings about using advanced mode everywhere. Here is fine, of course, given that the GC should only be used internally. But if there is ever a need to create a GC somewhere else, it's incredibly easy to forget about it and you suddenly end up with inconsistent and hard to debug problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it is out of your control anyway, isn't it? I cannot check in the code currently, but If you have a SWTGraphics with scaling, you'll get advanced Mode. If there is no scaling, you won't.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want to avoid situations where one now has to always do:
Or do you always (assuming the flag is set) in the SWTGraphics constructor? The problem is that I can't really predict what side-effects this might cause, so I'm uncomfortable with that idea.
On a different note: I assume this issue exists outside of Draw2D as well? Wouldn't it make more sense to enable this in SWT, rather than here? From what I remember, the GC instance is initialized with the Drawable it is created for.
There you could check whether auto-scaling is disabled for that widget and then activate advanced mode if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, that this shouldn't be on the side of consumers of GEF. Just wanted to point out, that it isn't (per se) tied to having auto-scaling disabled, but to when one of the methods of a GC is used, so that the advanced mode is enabled -> e.g drawPath or setTransform or any of the other methods. And that makes it actually very complicated. I tried to come up with some simple snippet and got to something like:
and the output is
So, e.g. one Figure draws a Path, the next one calculates and renders text differently.
I don't know, if this effect is similar problematic in GTK and cocoa or only in the windows GC, but this is really something to have a look at. Your idea with initializing it like that in SWT could be a way to go, we will need to think about it. But it is more messed up than I thought until now.