Skip to content
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

AWT Font Size Handling in Offscreen Redering #81

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,43 @@ private String getTextsString(String text, Properties style, String indentation)
y += lineHeight;
}

} else if (Klighd.simulateSwtFontSizeInAwt()) {
// If simulation of SWT font sizes in AWT is active we can use px size and set text length

final int fontSize = getFont().getSize();
final float y = fontSize * pointToPxFactor;
final Double nextLength = this.nextTextLength;
this.nextTextLength = null;

// if we have a multi-line text and a configured nextTextLength
// we need to recalculate the designated textLength per line
// otherwise we can stick to the given 'nextTextLength' value
boolean noTextLengthPerLineCalcRequired = nextLength == null || isSingleLine;
final FontData fontData = noTextLengthPerLineCalcRequired ? null : getFontData(getFont());

for (final String line : lines) {
content.append("\n" + indentation);
content.append("<text x=\"0\" y=\"").append(y).append("\"");

final Double nextLineLength = noTextLengthPerLineCalcRequired ? nextLength :
// need to box the result here as the type of the ternary operation would be 'double' otherwise
// yielding NPEs if 'nextLength' is 'null'
Double.valueOf(PlacementUtil.estimateTextSize(fontData, line).getWidth());

// text length
content.append(textLength(nextLineLength));
// semantic data
content.append(isSingleLine ? " "
// style
+ style(style)
// semantic data
+ attributes(false) : ""
);
content.append(textLineAttributes(line, i++));
content.append(">");
content.append(line);
content.append("</text>");
}
} else {
// without a display just use the pt size as line height for multiline text

Expand Down Expand Up @@ -1098,10 +1135,10 @@ private Properties getFontProperties(Font font) {
* A font's size is specified in 'pt' which is a relative size where a height of 72pt
* corresponds to 1 inch. When a font is rendered on a screen however, these pts are converted
* to pixels and sizes of nodes and boxes are determined correspondingly. We thus use a px size
* if we are able to determine the device with wich the font was rendered (i.e. the device).
* if we are able to determine the device with which the font was rendered (i.e. the device).
*/
Float size = (Float) attributes.get(TextAttribute.SIZE);
if (display != null) {
if (display != null || Klighd.simulateSwtFontSizeInAwt()) {
result.put("font-size", fixedPrecision(size.floatValue() * pointToPxFactor) + "px");
} else {
result.put("font-size", fixedPrecision(size.floatValue()) + "pt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Klighd {
private static IKlighdStatusManager statusManager;

private static boolean suppressDisplayScaleCompensationWhileHandlingText = false;
private static boolean simulateSwtFontSizeInAwt = true;

static {
boolean isLinux = false;
Expand Down Expand Up @@ -128,7 +129,31 @@ public static boolean isSuppressDisplayScaleCompensationWhileHandlingText() {
* <code>true</code> if compensation shall be suppressed, <code>false</code> reverts
* to the default.
*/
public static void setSuppressDisplayScaleCompensationWhileHandlingText(boolean suppress) {
public static void setSuppressDisplayScaleCompensationWhileHandlingText(final boolean suppress) {
suppressDisplayScaleCompensationWhileHandlingText = suppress;
}

/**
* Returns the (global) setting on adjusting the font size estimation of AWT to match SWT sizes
* more closely. This also enables using px instead of pt when rendering SVGs.
*
* @return <code>true</code> if simulation is active and fonts will be scaled (default),
* <code>false</code> otherwise.
*/
public static boolean simulateSwtFontSizeInAwt() {
return simulateSwtFontSizeInAwt;
}

/**
* Changes the (global) setting on adjusting adjusting the font size estimation of AWT to match SWT sizes
* more closely. This also enables using px instead of pt when rendering SVGs.
* This setting will only affect non-UI applications, but may be switched off if default AWT font sizes and values
* in pt are preferred.
*
* @param simulate
* <code>true</code> if font sizes should be adjusted, <code>false</code> to deactivate this behavior.
*/
public static void setSimulateSwtFontSizeInAwt(final boolean simulate) {
simulateSwtFontSizeInAwt = simulate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public C getC() {
// CHECKSTYLEON Visibility

private static final KRenderingPackage KRENDERING_PACKAGE = KRenderingPackage.eINSTANCE;

private static final float PT_TO_PX_FACTOR = KlighdConstants.DEFAULT_DISPLAY_DPI / 72f;

/**
* Evaluates a position inside given parent bounds.
Expand Down Expand Up @@ -934,10 +934,11 @@ public static Bounds estimateTextSize(final KText kText) {
public static Bounds estimateTextSize(final KText kText, final String text) {
final Bounds testSize = getTestingTextSize(kText);

if (testSize != null)
if (testSize != null) {
return testSize;
else
} else {
return estimateTextSize(fontDataFor(kText, null), text);
}
}

/**
Expand Down Expand Up @@ -1092,6 +1093,11 @@ private static Bounds estimateTextSizeAWT(final FontData fontData, final String
textBounds = new Bounds(fm.getStringBounds(text, fmg));
}

if (Klighd.simulateSwtFontSizeInAwt()) {
textBounds.width *= PT_TO_PX_FACTOR;
textBounds.height *= PT_TO_PX_FACTOR;
}

return textBounds;
}

Expand Down