Skip to content

Commit

Permalink
Adjust for font height between platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Aug 13, 2024
1 parent 8e1c564 commit 19fe564
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ public void run() {

// Set default font on first selected object
FontData fontData = FontFactory.getDefaultUserViewFontData();
String rgbValue = null;

rgbValue = model.getFontColor();
String fontValue = model.getFont();
String fontValue = FontFactory.getPlatformDependentFontString(model.getFont());
if(fontValue != null) {
try {
fontData = new FontData(fontValue);
Expand All @@ -89,7 +86,7 @@ public void run() {
dialog.setText(Messages.FontAction_1);
dialog.setEffectsVisible(false); // Don't allow underline/strikeout on Windows. See https://github.com/archimatetool/archi/issues/851
dialog.setFontList(new FontData[] { fontData } );
dialog.setRGB(ColorFactory.convertStringToRGB(rgbValue));
dialog.setRGB(ColorFactory.convertStringToRGB(model.getFontColor()));

FontData selectedFontData = dialog.open();
if(selectedFontData != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ public IDiagramModelObjectFigure getFigure() {
*/
@Override
protected void applicationPreferencesChanged(PropertyChangeEvent event) {
// Default font or colour preferences changed
// Default font or colour preferences changed, or font scaling changed
if(IPreferenceConstants.DEFAULT_VIEW_FONT.equals(event.getProperty()) ||
event.getProperty().startsWith(IPreferenceConstants.DEFAULT_FILL_COLOR_PREFIX) ||
event.getProperty().equals(IPreferenceConstants.DEFAULT_ELEMENT_LINE_COLOR)) {
event.getProperty().equals(IPreferenceConstants.DEFAULT_ELEMENT_LINE_COLOR) ||
event.getProperty().equals(IPreferenceConstants.PLATFORM_FONT_SCALING)) {

refreshFigure();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ else if(IPreferenceConstants.CONNECTION_LABEL_STRATEGY.equals(property)) {
else if(IPreferenceConstants.VIEWPOINTS_GHOST_DIAGRAM_ELEMENTS.equals(property)) {
getFigure().refreshVisuals();
}
else if(IPreferenceConstants.PLATFORM_FONT_SCALING.equals(property)) {
getFigure().refreshVisuals();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected void setDisabledState(Graphics graphics) {
* Set the font to that in the model, or failing that, as per user's default
*/
protected void setFont() {
String fontName = fDiagramModelObject.getFont();
String fontName = FontFactory.getPlatformDependentFontString(fDiagramModelObject.getFont());
setFont(FontFactory.get(fontName));

// Need to do this after font change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected void setConnectionText() {
* Set the font in the label to that in the model, or failing that, as per user's default
*/
protected void setLabelFont() {
String fontName = getModelConnection().getFont();
String fontName = FontFactory.getPlatformDependentFontString(getModelConnection().getFont());
Font font = FontFactory.get(fontName);

// Possible font scaling for non 96 DPI on Windows or if property set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class GeneralPreferencePage
private Button fWarnOnDeleteButton;
private Button fShowSpecializationsModelTreeButton;

private Button fScaleImagesButton;

private Button fAddDocumentationNoteButton;
private Button fScaleImagesButton;
private Button fScaleFontsButton;

private Button fUseEdgeBrowserButton;

Expand Down Expand Up @@ -146,6 +146,11 @@ protected Control createContents(Composite parent) {
fScaleImagesButton.setLayoutData(createHorizontalGridData(2));
fScaleImagesButton.setToolTipText(Messages.GeneralPreferencePage_14);

fScaleFontsButton = new Button(otherGroup, SWT.CHECK);
fScaleFontsButton.setText(Messages.GeneralPreferencePage_25);
fScaleFontsButton.setLayoutData(createHorizontalGridData(2));
fScaleFontsButton.setToolTipText(Messages.GeneralPreferencePage_26);

// Internal Browser
Group browserGroup = new Group(client, SWT.NULL);
browserGroup.setText(Messages.GeneralPreferencePage_19);
Expand Down Expand Up @@ -222,6 +227,8 @@ private void setValues() {

fAddDocumentationNoteButton.setSelection(getPreferenceStore().getBoolean(ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE));

fScaleFontsButton.setSelection(getPreferenceStore().getBoolean(PLATFORM_FONT_SCALING));

if(fUseEdgeBrowserButton != null) {
fUseEdgeBrowserButton.setSelection(getPreferenceStore().getBoolean(EDGE_BROWSER));
}
Expand Down Expand Up @@ -254,6 +261,8 @@ public boolean performOk() {

getPreferenceStore().setValue(ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE, fAddDocumentationNoteButton.getSelection());

getPreferenceStore().setValue(PLATFORM_FONT_SCALING, fScaleFontsButton.getSelection());

if(fUseEdgeBrowserButton != null) {
getPreferenceStore().setValue(EDGE_BROWSER, fUseEdgeBrowserButton.getSelection());
}
Expand Down Expand Up @@ -288,6 +297,8 @@ protected void performDefaults() {

fAddDocumentationNoteButton.setSelection(getPreferenceStore().getDefaultBoolean(ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE));

fScaleFontsButton.setSelection(getPreferenceStore().getDefaultBoolean(PLATFORM_FONT_SCALING));

if(fUseEdgeBrowserButton != null) {
fUseEdgeBrowserButton.setSelection(getPreferenceStore().getDefaultBoolean(EDGE_BROWSER));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ public interface IPreferenceConstants {
// Whether to use a line offset in all cases for figures drawn on hi-res screens
String USE_FIGURE_LINE_OFFSET = "figureLineOffset";

// Whether to add a note to the documentation field of relationships that have been changed to Association when setting a new concept type
String ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE = "addDocumentationNoteOnRelationChange";

// Appearance

String DEFAULT_ARCHIMATE_FIGURE_WIDTH = "defaultArchiMateFigureWidth";
Expand Down Expand Up @@ -135,7 +132,10 @@ public interface IPreferenceConstants {

String USE_LABEL_EXPRESSIONS_IN_ANALYSIS_TABLE = "useLabelExpressionInAnalysisTable";

// Whether to add a note to the documentation field of relationships that have been changed to Association when setting a new concept type
String ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE = "addDocumentationNoteOnRelationChange";
String SCALE_IMAGE_EXPORT = "scaleImageExport";
String PLATFORM_FONT_SCALING = "platformFontScaling";

// Animation
String ANIMATE_VIEW = "animateView";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public class Messages extends NLS {

public static String GeneralPreferencePage_24;

public static String GeneralPreferencePage_25;

public static String GeneralPreferencePage_26;

public static String GeneralPreferencePage_3;

public static String GeneralPreferencePage_4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ public void initializeDefaultPreferences() {
// Windows at hi-res needs this set to true
store.setDefault(USE_FIGURE_LINE_OFFSET, PlatformUtils.isWindows() && ImageFactory.getDeviceZoom() > 100);

store.setDefault(ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE, false);

// Appearance

store.setDefault(DEFAULT_ARCHIMATE_FIGURE_WIDTH, 120);
Expand All @@ -131,8 +129,10 @@ public void initializeDefaultPreferences() {

store.setDefault(USE_LABEL_EXPRESSIONS_IN_ANALYSIS_TABLE, true);

store.setDefault(ADD_DOCUMENTATION_NOTE_ON_RELATION_CHANGE, false);
// Windows hi-res and Mac Retina use 200 zoom, Linux is 100
store.setDefault(SCALE_IMAGE_EXPORT, (!PlatformUtils.isLinux() && ImageFactory.getDeviceZoom() > 100) ? true : false);
store.setDefault(PLATFORM_FONT_SCALING, true);

// Animation
store.setDefault(ANIMATE_VIEW, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ GeneralPreferencePage_21=Enable JavaScript in Hints
GeneralPreferencePage_22=Allow External Hosts in Hints
GeneralPreferencePage_23=If a connected relation type is changed as a result of changing an Element's type,\na note will be added to the Relation's documentation field.
GeneralPreferencePage_24=Show Specializations in "New" menu
GeneralPreferencePage_25=Adjust font height for each platform
GeneralPreferencePage_26=Adjust font height when a font height has been set on another operating system platform
GeneralPreferencePage_3=Animation
GeneralPreferencePage_4=Animate Views
GeneralPreferencePage_5=Automatically create a backup file (*.bak) when saving
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,12 @@ else if(event.getProperty() == ColorChooser.PROP_COLORDEFAULT) {
};

/**
* Listen to default font change in Prefs
* Listen to default font change and font scaling in Prefs
*/
private IPropertyChangeListener prefsListener = new IPropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if(event.getProperty().startsWith(IPreferenceConstants.DEFAULT_VIEW_FONT)) {
update();
}
private IPropertyChangeListener prefsListener = event -> {
if(event.getProperty().startsWith(IPreferenceConstants.DEFAULT_VIEW_FONT)
|| IPreferenceConstants.PLATFORM_FONT_SCALING.equals(event.getProperty())) {
update();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static Font getScaledFont96DPI(Font font) {
FontData[] fd = font.getFontData();

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

fd[0].setHeight(newHeight);
String fontName = fd[0].toString();
Expand All @@ -168,6 +168,40 @@ public static Font getScaledFont96DPI(Font font) {
return font;
}

/**
* Return a FontData string with adjusted font height if the FontData string is not for the current platform.
* @param fontDataString The FontData string
* @return The Font Data String for the current platform or the same string if there is no change.
*/
public static String getPlatformDependentFontString(String fontDataString) {
if(StringUtils.isSet(fontDataString) && ArchiPlugin.PREFERENCES.getBoolean(IPreferenceConstants.PLATFORM_FONT_SCALING)) {
float factor = 0;

// On Mac but fontdata contains Windows or Linux
if(PlatformUtils.isMac() && (fontDataString.contains("|WINDOWS|") || fontDataString.contains("|GTK|"))) {
factor = (float)96 / 72;
}
// On Windows or Linux but font came from Mac
else if(!PlatformUtils.isMac() && fontDataString.contains("|COCOA|")) {
factor = (float)72 / 96;
}

if(factor != 0) {
try {
FontData fd = new FontData(fontDataString);
int newHeight = Math.round(fd.getHeight() * factor);
fd.setHeight(newHeight);
fontDataString = fd.toString();
}
catch(Exception ex) {
//ex.printStackTrace();
}
}
}

return fontDataString;
}

/**
* @param font
* @return The italic variant of the given font
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void chooseFont() {
protected void updateFont() {
fFontData = FontFactory.getDefaultUserViewFontData();

String fontValue = fFontObject.getFont();
String fontValue = FontFactory.getPlatformDependentFontString(fFontObject.getFont());
if(fontValue != null) {
try {
fFontData = new FontData(fontValue);
Expand Down Expand Up @@ -275,34 +275,23 @@ protected void updateButtonText() {
}

/**
* Adds a property change listener to this <code>ColorSelector</code>.
* Events are fired when the color in the control changes via the user
* clicking an selecting a new one in the color dialog. No event is fired in
* the case where <code>setColorValue(RGB)</code> is invoked.
*
* @param listener
* a property change listener
* Adds a property change listener to listen for font change events.
* @param listener a property change listener
*/
public void addListener(IPropertyChangeListener listener) {
addListenerObject(listener);
}

/**
* Removes the given listener from this <code>ColorSelector</code>. Has
* no effect if the listener is not registered.
*
* @param listener
* a property change listener
* Removes the given listener. Has no effect if the listener is not registered.
* @param listener a property change listener
*/
public void removeListener(IPropertyChangeListener listener) {
removeListenerObject(listener);
}

/**
* Fire the given event to listeners
* @param propertyName
* @param oldValue
* @param newValue
*/
private void fireActionListenerEvent(String propertyName, Object oldValue, Object newValue) {
final Object[] finalListeners = getListeners();
Expand Down
3 changes: 3 additions & 0 deletions com.archimatetool.help/help/Text/prefs_general.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ <h3>Other</h3>
When this is selected, images are exported and copied to the clipboard at twice their normal size. This is set on by default for Windows operating systems where display scaling is greater than 100%, and on Mac Retina devices. This is so that the exported image quality is not compromised since it is not possible to set a higher DPI in the image. This setting is usually not needed on Linux displays.</p>

<p>If the resulting image needs to be at its logical size you could edit it in an image editor and set the DPI to double its value. For example, on Mac change the DPI from to 72 to 144.</p>
<br/>

<p><strong>Adjust font height for each platform</strong><br/>
When this is selected, fonts are adjusted to match the DPI (dots per inch) setting for the current platform. For example, you might work on an Archi model on Windows and set the font height for an object to 10. However, when opening the model on Mac the font will appear too small because Mac uses 72 DPI while Windows uses 96 DPI. Conversely, you might set the font height on Mac but it appears too big on Windows. This setting adjusts for this difference so that font heights are the same on all platforms.</p>
<br/>

<h3>Internal Web Browser</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ Element writeFont(IFontAttribute fontObject, Element styleElement) {
try {
FontData fontData = null;

String fontString = fontObject.getFont();
String fontString = FontFactory.getPlatformDependentFontString(fontObject.getFont());
if(fontString != null) {
fontData = new FontData(fontString);
}
Expand Down

0 comments on commit 19fe564

Please sign in to comment.