Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLakshmi Shanmugam2018-03-01 07:50:46 -0500
committerLakshmi Shanmugam2018-03-02 07:21:24 -0500
commit0aa46cfe2c773c9552f2f2a8c79e442cda905931 (patch)
treecb7049ddfebaa1fe2394d0a0caf8ea51f5194107
parente385fcbbec9141cfa9f82f55c4d03c505943c11e (diff)
downloadeclipse.platform.swt-0aa46cfe2c773c9552f2f2a8c79e442cda905931.tar.gz
eclipse.platform.swt-0aa46cfe2c773c9552f2f2a8c79e442cda905931.tar.xz
eclipse.platform.swt-0aa46cfe2c773c9552f2f2a8c79e442cda905931.zip
Bug 508600: [API] The average character width of a font is not integer
Added new API getAverageCharacterWidth() which returns the width in floating point. Change-Id: I4767aa07f0be6ead821d1db18cd8508864225056
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/FontMetrics.java49
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/GC.java2
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/TextLayout.java4
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/FontMetrics.java13
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java12
5 files changed, 70 insertions, 10 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/FontMetrics.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/FontMetrics.java
index 1f550484d3..e4896f84fa 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/FontMetrics.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/FontMetrics.java
@@ -22,12 +22,35 @@ package org.eclipse.swt.graphics;
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*/
public final class FontMetrics {
- int ascent, descent, averageCharWidth, leading, height;
+ int ascent, descent, leading, height;
+ double averageCharWidth;
FontMetrics() {
}
-public static FontMetrics cocoa_new(int ascent, int descent, int averageCharWidth, int leading, int height) {
+public static FontMetrics cocoa_new (int ascent, int descent, int averageCharWidth, int leading, int height) {
+ FontMetrics fontMetrics = new FontMetrics();
+ fontMetrics.ascent = ascent;
+ fontMetrics.descent = descent;
+ fontMetrics.averageCharWidth = averageCharWidth;
+ fontMetrics.leading = leading;
+ fontMetrics.height = height;
+ return fontMetrics;
+}
+
+/**
+ * Invokes platform specific functionality to allocate a new FontMetrics.
+ * <p>
+ * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
+ * API for <code>FontMetrics</code>. It is marked public only so that it
+ * can be shared within the packages provided by SWT. It is not
+ * available on all platforms, and should never be called from
+ * application code.
+ * </p>
+ *
+ * @noreference This method is not intended to be referenced by clients.
+ */
+public static FontMetrics cocoa_new (int ascent, int descent, double averageCharWidth, int leading, int height) {
FontMetrics fontMetrics = new FontMetrics();
fontMetrics.ascent = ascent;
fontMetrics.descent = descent;
@@ -52,9 +75,8 @@ public boolean equals (Object object) {
if (object == this) return true;
if (!(object instanceof FontMetrics)) return false;
FontMetrics metrics = (FontMetrics)object;
- return ascent == metrics.ascent && descent == metrics.descent &&
- averageCharWidth == metrics.averageCharWidth && leading == metrics.leading &&
- height == metrics.height;
+ return ascent == metrics.ascent && descent == metrics.descent && leading == metrics.leading &&
+ height == metrics.height && (Double.compare (averageCharWidth, metrics.averageCharWidth) == 0);
}
/**
@@ -74,12 +96,25 @@ public int getAscent() {
* of the font described by the receiver.
*
* @return the average character width of the font
+ * @since 3.107
*/
-public int getAverageCharWidth() {
+public double getAverageCharacterWidth () {
return averageCharWidth;
}
/**
+ * Returns the average character width, measured in pixels,
+ * of the font described by the receiver.
+ *
+ * @return the average character width of the font
+ * @deprecated Use getAverageCharacterWidth() instead
+ */
+@Deprecated
+public int getAverageCharWidth() {
+ return (int) averageCharWidth;
+}
+
+/**
* Returns the descent of the font described by the receiver. A
* font's <em>descent</em> is the distance from the baseline to the
* bottom of actual characters, not including any of the leading area,
@@ -129,7 +164,7 @@ public int getLeading() {
*/
@Override
public int hashCode() {
- return ascent ^ descent ^ averageCharWidth ^ leading ^ height;
+ return ascent ^ descent ^ Double.hashCode (averageCharWidth) ^ leading ^ height;
}
String getName () {
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/GC.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/GC.java
index d4f84a7059..3bf08c3495 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/GC.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/GC.java
@@ -2571,7 +2571,7 @@ public FontMetrics getFontMetrics() {
NSLayoutManager layoutManager = data.layoutManager;
layoutManager.glyphRangeForTextContainer(data.textContainer);
NSRect rect = layoutManager.usedRectForTextContainer(data.textContainer);
- int avgWidth = (int) Math.ceil(rect.width) / s.length();
+ double avgWidth = Math.ceil(rect.width) / s.length();
int ascent = (int)layoutManager.defaultBaselineOffsetForFont(data.font.handle);
int height = (int)layoutManager.defaultLineHeightForFont(data.font.handle);
data.font.metrics = FontMetrics.cocoa_new(ascent, height - ascent, avgWidth, 0, height);
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/TextLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/TextLayout.java
index ea2ef68cdb..544f7cf40e 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/TextLayout.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/TextLayout.java
@@ -1044,11 +1044,11 @@ public FontMetrics getLineMetrics (int lineIndex) {
int descent = (int)layoutManager.defaultLineHeightForFont(font.handle) - ascent;
ascent = Math.max(ascent, this.ascent);
descent = Math.max(descent, this.descent);
- return FontMetrics.cocoa_new(ascent, descent, 0, 0, ascent + descent);
+ return FontMetrics.cocoa_new(ascent, descent, 0.0, 0, ascent + descent);
}
Rectangle rect = getLineBounds(lineIndex);
int baseline = (int)layoutManager.typesetter().baselineOffsetInLayoutManager(layoutManager, getLineOffsets()[lineIndex]);
- return FontMetrics.cocoa_new(rect.height - baseline, baseline, 0, 0, rect.height);
+ return FontMetrics.cocoa_new(rect.height - baseline, baseline, 0.0, 0, rect.height);
} finally {
if (pool != null) pool.release();
}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/FontMetrics.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/FontMetrics.java
index 1bceafb880..c5515c8af9 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/FontMetrics.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/FontMetrics.java
@@ -62,7 +62,20 @@ public int getAscent() {
* of the font described by the receiver.
*
* @return the average character width of the font
+ * @since 3.107
*/
+public double getAverageCharacterWidth() {
+ return getAverageCharWidth();
+}
+
+/**
+ * Returns the average character width, measured in pixels,
+ * of the font described by the receiver.
+ *
+ * @return the average character width of the font
+ * @deprecated Use getAverageCharacterWidth() instead
+ */
+@Deprecated
public int getAverageCharWidth() {
return averageCharWidthInPoints;
}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java
index 7ab6e61747..3fe3d85590 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java
@@ -100,6 +100,18 @@ public int getAscent() {
* of the font described by the receiver.
*
* @return the average character width of the font
+ * @since 3.107
+ */
+public double getAverageCharacterWidth() {
+ return getAverageCharWidth();
+}
+
+/**
+ * Returns the average character width, measured in pixels,
+ * of the font described by the receiver.
+ *
+ * @return the average character width of the font
+ * @deprecated Use getAverageCharacterWidth() instead
*/
public int getAverageCharWidth() {
return DPIUtil.autoScaleDown(handle.tmAveCharWidth);

Back to the top