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 f13287e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void run() {
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 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 @@ -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)) {
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
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 f13287e

Please sign in to comment.