Skip to content

Calculate Scaling for Font without Device#getDPI() #1802

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

Merged
merged 1 commit into from
Feb 10, 2025
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 @@ -304,9 +304,11 @@ void cacheLineData(StyledText styledText) {
}
}
}
Point screenDPI = styledText.getDisplay().getDPI();

Point printerDPI = printer.getDPI();
resources = new HashMap<> ();
int scaleFactorX = printerDPI.x / 100;
int scaleFactorY = printerDPI.y / 100;
for (int i = 0; i < lineCount; i++) {
Color color = printerRenderer.getLineBackground(i, null);
if (color != null) {
Expand All @@ -323,7 +325,7 @@ void cacheLineData(StyledText styledText) {
}
int indent = printerRenderer.getLineIndent(i, 0);
if (indent != 0) {
printerRenderer.setLineIndent(i, 1, indent * printerDPI.x / screenDPI.x);
printerRenderer.setLineIndent(i, 1, indent * scaleFactorX);
}
}
StyleRange[] styles = printerRenderer.styles;
Expand Down Expand Up @@ -367,17 +369,17 @@ void cacheLineData(StyledText styledText) {
if (!printOptions.printTextFontStyle) {
style.fontStyle = SWT.NORMAL;
}
style.rise = style.rise * printerDPI.y / screenDPI.y;
style.rise = style.rise * scaleFactorY;
GlyphMetrics metrics = style.metrics;
if (metrics != null) {
metrics.ascent = metrics.ascent * printerDPI.y / screenDPI.y;
metrics.descent = metrics.descent * printerDPI.y / screenDPI.y;
metrics.width = metrics.width * printerDPI.x / screenDPI.x;
metrics.ascent = metrics.ascent * scaleFactorY;
metrics.descent = metrics.descent * scaleFactorY;
metrics.width = metrics.width * scaleFactorX;
}
}
lineSpacing = styledText.lineSpacing * printerDPI.y / screenDPI.y;
lineSpacing = styledText.lineSpacing * scaleFactorY;
if (printOptions.printLineNumbers) {
printMargin = 3 * printerDPI.x / screenDPI.x;
printMargin = 3 * scaleFactorX;
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ protected void create(DeviceData deviceData) {
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
}

@Override
protected int getDeviceZoom() {
// Printer directly renders on pixel basis, so the zoom must be fixed to 100
return 100;
}

/**
* Invokes platform specific functionality to allocate a new GC handle.
* <p>
Expand Down Expand Up @@ -295,8 +301,9 @@ public long internal_new_GC(GCData data) {
data.style |= SWT.LEFT_TO_RIGHT;
}
data.device = this;
data.font = Font.win32_new(this, OS.GetCurrentObject(handle, OS.OBJ_FONT));
data.font = Font.win32_new(this, OS.GetCurrentObject(handle, OS.OBJ_FONT), getDeviceZoom());
isGCCreated = true;
data.nativeZoom = getDeviceZoom();
}
return handle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,20 @@ int computePixels(float height) {
}

float computePoints(LOGFONT logFont, long hFont) {
return computePoints(logFont, hFont, -1);
return computePoints(logFont, hFont, SWT.DEFAULT);
}

float computePoints(LOGFONT logFont, long hFont, int currentFontDPI) {
float computePoints(LOGFONT logFont, long hFont, int zoom) {
long hDC = internal_new_GC (null);
int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);

float conversionFactor = 72f;
if (isAutoScalable() && zoom != SWT.DEFAULT) {
// For auto scalable devices we need to use a dynamic
// DPI value that is extracted from the zoom
conversionFactor /= DPIUtil.mapZoomToDPI(zoom);
} else {
conversionFactor /= OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
}
int pixels = 0;
if (logFont.lfHeight > 0) {
/*
Expand All @@ -292,14 +300,7 @@ float computePoints(LOGFONT logFont, long hFont, int currentFontDPI) {
pixels = -logFont.lfHeight;
}
internal_dispose_GC (hDC, null);
float adjustedZoomFactor = 1.0f;
if (currentFontDPI > 0) {
// as Device::computePoints will always return point on the basis of the
// primary monitor zoom, a custom zoomFactor must be calculated if the font
// is used for a different zoom level
adjustedZoomFactor *= (float) logPixelsY / (float) currentFontDPI;
}
return adjustedZoomFactor * pixels * 72f / logPixelsY;
return pixels * conversionFactor;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public FontData[] getFontData() {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
LOGFONT logFont = new LOGFONT ();
OS.GetObject(handle, LOGFONT.sizeof, logFont);
float heightInPoints = device.computePoints(logFont, handle, DPIUtil.mapZoomToDPI(zoom));
float heightInPoints = device.computePoints(logFont, handle, zoom);
return new FontData[] {FontData.win32_new(logFont, heightInPoints)};
}

Expand Down Expand Up @@ -280,7 +280,7 @@ private static int extractZoom(Device device) {
if (device == null) {
return DPIUtil.getNativeDeviceZoom();
}
return DPIUtil.mapDPIToZoom(device._getDPIx());
return device.getDeviceZoom();
}

/**
Expand Down Expand Up @@ -331,8 +331,15 @@ public static Font win32_new(Device device, long handle) {
* @since 3.126
*/
public static Font win32_new(Device device, long handle, int zoom) {
Font font = win32_new(device, handle);
Font font = new Font(device);
font.zoom = zoom;
font.handle = handle;
/*
* When created this way, Font doesn't own its .handle, and
* for this reason it can't be disposed. Tell leak detector
* to just ignore it.
*/
font.ignoreNonDisposed();
return font;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ private Font getScaledFont(int targetZoom) {

private Font scaleFont(int zoom) {
FontData fontData = baseFont.getFontData()[0];
fontData.data.lfHeight = computePixels(zoom, fontData);
int baseZoom = computeZoom(fontData);
int zoomScaleFactor = Math.round(1.0f * zoom / baseZoom);
fontData.data.lfHeight *= zoomScaleFactor;
Font scaledFont = Font.win32_new(device, fontData, zoom);
addScaledFont(zoom, scaledFont);
return scaledFont;
Expand Down Expand Up @@ -168,24 +170,13 @@ private Font getOrCreateFont(ScaledFontContainer container, int zoom) {
}

private int computeZoom(FontData fontData) {
int dpi = device.getDPI().x;
int pixelsAtPrimaryMonitorZoom = computePixels(fontData.height);
int value = DPIUtil.mapDPIToZoom(dpi) * fontData.data.lfHeight / pixelsAtPrimaryMonitorZoom;
return value;
}

private int computePixels(int zoom, FontData fontData) {
int dpi = device.getDPI().x;
int adjustedLogFontHeight = computePixels(fontData.height);
int primaryZoom = DPIUtil.mapDPIToZoom(dpi);
if (zoom != primaryZoom) {
adjustedLogFontHeight *= (1f * zoom / primaryZoom);
int pixelHeight = fontData.data.lfHeight;
float currentPointHeight = fontData.height;
if (pixelHeight == 0 || Math.abs(currentPointHeight) < 0.001) {
// if there is no font yet available, we use a defined zoom
return 100;
}
return adjustedLogFontHeight;
}

private int computePixels(float height) {
int dpi = device.getDPI().x;
return -(int)(0.5f + (height * dpi / 72f));
float pointHeightOn100 = -(pixelHeight / 96f * 72f);
return Math.round(100.0f * pointHeightOn100 / currentPointHeight);
}
}
Loading