Skip to content

Commit

Permalink
Add option to use scaled fonts relative to 96 DPI
Browse files Browse the repository at this point in the history
- Using the property -Dcom.archimatetool.adjustFontSize=true we can scaled fonts up relative to 96 DPI in cases where the native DPI is not 96

- This could be on Mac (72 DPI) or on some Windows scaling settings
  • Loading branch information
Phillipus committed Oct 9, 2023
1 parent 5682068 commit 870278b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ protected void setFont() {

@Override
public void setFont(Font f) {
if(PlatformUtils.isWindows()) {
f = FontFactory.getAdjustedWindowsFont(f);
// Possible font scaling for non 96 DPI on Windows or if property set
if(PlatformUtils.isWindows() || "true".equals(System.getProperty(FontFactory.ADJUST_FONT_SIZE_PROPERTY))) { //$NON-NLS-1$
f = FontFactory.getScaledFont96DPI(f);
}

super.setFont(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ protected void setLabelFont() {
String fontName = getModelConnection().getFont();
Font font = FontFactory.get(fontName);

// Adjust for Windows DPI
if(PlatformUtils.isWindows()) {
font = FontFactory.getAdjustedWindowsFont(font);
// Possible font scaling for non 96 DPI on Windows or if property set
if(PlatformUtils.isWindows() || "true".equals(System.getProperty(FontFactory.ADJUST_FONT_SIZE_PROPERTY))) { //$NON-NLS-1$
font = FontFactory.getScaledFont96DPI(font);
}

getConnectionLabel().setFont(font);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
*
* @author Phillip Beauvoir
*/
@SuppressWarnings("nls")
public final class FontFactory {

public static final String ADJUST_FONT_SIZE_PROPERTY = "com.archimatetool.adjustFontSize";

private static final String DEFAULT_VIEW_FONT_NAME = "defaultViewFont"; //$NON-NLS-1$

/**
Expand All @@ -34,13 +37,13 @@ public final class FontFactory {
private static FontRegistry FontRegistry = new FontRegistry();

/**
* Temporary Font Registry to hold adjusted size fonts on Windows
* Temporary Font Registry to hold scaled fonts
*/
private static FontRegistry windowsFontRegistry = new FontRegistry();
private static FontRegistry ScaledFontRegistry = new FontRegistry();

public static Font SystemFontBold = JFaceResources.getFontRegistry().getBold(""); //$NON-NLS-1$
public static Font SystemFontBold = JFaceResources.getFontRegistry().getBold("");

public static Font SystemFontItalic = JFaceResources.getFontRegistry().getItalic(""); //$NON-NLS-1$
public static Font SystemFontItalic = JFaceResources.getFontRegistry().getItalic("");

/**
* @param fontName
Expand Down Expand Up @@ -112,19 +115,19 @@ public static void setDefaultUserViewFont(FontData fd) {
*/
public static FontData getDefaultViewOSFontData() {
// Default
FontData fd = new FontData("Sans", 9, SWT.NORMAL); //$NON-NLS-1$
FontData fd = new FontData("Sans", 9, SWT.NORMAL);

// Windows
if(PlatformUtils.isWindows()) {
fd = new FontData("Segoe UI", 9, SWT.NORMAL); //$NON-NLS-1$
fd = new FontData("Segoe UI", 9, SWT.NORMAL);
}
// Linux
else if(PlatformUtils.isLinux()) {
fd = new FontData("Sans", 9, SWT.NORMAL); //$NON-NLS-1$
fd = new FontData("Sans", 9, SWT.NORMAL);
}
// Mac
else if(PlatformUtils.isMac()) {
fd = new FontData("Lucida Grande", 12, SWT.NORMAL); //$NON-NLS-1$
fd = new FontData("Lucida Grande", 12, SWT.NORMAL);
}

return fd;
Expand All @@ -136,25 +139,29 @@ private static void setDefaultViewOSFontData() {
}

/**
* On Windows OS if the system DPI is not 96 DPI we need to adjust the size
* of the font on the diagram View.
* @param font
* @return The adjusted font
* Return a font scaled from 96 DPI if the current DPI is not 96
* This can happen on Windows if the DPI is not 96 or on Mac if we set a property.
* @return The adjusted font if DPI is not 96 or the same font, or null
*/
public static Font getAdjustedWindowsFont(Font font) {
if(font != null && PlatformUtils.isWindows()) {
int DPI = font.getDevice().getDPI().y;
if(DPI != 96) {
FontData[] fd = font.getFontData();
String fontName = fd[0].toString();
if(!windowsFontRegistry.hasValueFor(fontName)) {
float factor = (float)96 / DPI;
int newHeight = (int)(fd[0].getHeight() * factor);
fd[0].setHeight(newHeight);
windowsFontRegistry.put(fontName, fd);
}
font = windowsFontRegistry.get(fontName);
public static Font getScaledFont96DPI(Font font) {
if(font == null) {
return font;
}

int DPI = font.getDevice().getDPI().y;

if(DPI != 96) {
FontData[] fd = font.getFontData();
String fontName = fd[0].toString();

if(!ScaledFontRegistry.hasValueFor(fontName)) {
float factor = (float)96 / DPI;
int newHeight = (int)(fd[0].getHeight() * factor);
fd[0].setHeight(newHeight);
ScaledFontRegistry.put(fontName, fd);
}

font = ScaledFontRegistry.get(fontName);
}

return font;
Expand Down

0 comments on commit 870278b

Please sign in to comment.