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 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 d695013
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.archimatetool.editor.ui.ImageFactory;
import com.archimatetool.editor.ui.factory.IGraphicalObjectUIProvider;
import com.archimatetool.editor.ui.factory.ObjectUIFactory;
import com.archimatetool.editor.utils.PlatformUtils;
import com.archimatetool.editor.utils.StringUtils;
import com.archimatetool.model.IArchimateElement;
import com.archimatetool.model.IDiagramModelArchimateObject;
Expand Down Expand Up @@ -164,12 +163,11 @@ protected void setFont() {
}

@Override
public void setFont(Font f) {
if(PlatformUtils.isWindows()) {
f = FontFactory.getAdjustedWindowsFont(f);
}
public void setFont(Font font) {
// Possible font scaling for non 96 DPI on Windows or if property set
font = FontFactory.checkScaledFont96DPI(font);

super.setFont(f);
super.setFont(font);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.archimatetool.editor.ui.ColorFactory;
import com.archimatetool.editor.ui.FontFactory;
import com.archimatetool.editor.ui.textrender.TextRenderer;
import com.archimatetool.editor.utils.PlatformUtils;
import com.archimatetool.editor.utils.StringUtils;
import com.archimatetool.model.IDiagramModelConnection;

Expand Down Expand Up @@ -201,10 +200,8 @@ 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
font = FontFactory.checkScaledFont96DPI(font);

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

// Property to set if we will check to adjust font scaling
public static final String ADJUST_FONT_SIZE_PROPERTY = "com.archimatetool.adjustFontSize";

// Whether to check to adjust font size on Windows non 96 DPI or if property set
private static final boolean CHECK_ADJUST_FONT_SIZE = PlatformUtils.isWindows() || "true".equals(System.getProperty(ADJUST_FONT_SIZE_PROPERTY));

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

/**
* Font Registry
*/
private static FontRegistry FontRegistry = new FontRegistry();

/**
* Temporary Font Registry to hold adjusted size fonts on Windows
*/
private static FontRegistry windowsFontRegistry = 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 +114,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 +138,31 @@ 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 checkScaledFont96DPI(Font font) {
if(font == null || !CHECK_ADJUST_FONT_SIZE) {
return font;
}

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

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

float factor = (float)96 / DPI;
int newHeight = (int)(fd[0].getHeight() * factor);

fd[0].setHeight(newHeight);
String fontName = fd[0].toString();

if(!FontRegistry.hasValueFor(fontName)) {
FontRegistry.put(fontName, fd);
}

font = FontRegistry.get(fontName);
}

return font;
Expand Down

0 comments on commit d695013

Please sign in to comment.