This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
[gui][PATCH] Account for font style and default language in fontmetric calculations
- From: Thomas Fitzsimmons <fitzsim at redhat dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Mon, 14 Jun 2004 22:39:37 -0400
- Subject: [gui][PATCH] Account for font style and default language in fontmetric calculations
Hi,
This patch fixes two problems with font metric calculations in the GTK
peers. First, the AWT font style (plain, bold, italic) wasn't being
taken into account. Second, NULL was being passed as the language
parameter to pango_context_get_metrics. This produces metrics that
don't correspond with the font that is currently in use. This patch
fixes the problem by passing the current language (the return value of
gtk_get_default_language ()) when retrieving font metrics.
I committed this patch to java-gui-branch.
Tom
2004-06-14 Thomas Fitzsimmons <fitzsim@redhat.com>
* gnu/java/awt/peer/gtk/GdkFontMetrics.java (initState): Add
style parameter.
(GdkFontMetrics): Add style argument to initState call.
(stringWidth(String,int,int,String)): Add style parameter.
(stringWidth(String)): Add style argument to stringWidth call.
* jni/gtk-peer/gnu_java_awt_peer_gtk_GdkFontMetrics.c
(initState): Set pango font style and weight based on AWT style
parameter. Pass default GTK language to
pango_context_get_metrics. Use PANGO_PIXELS macro rather than
simply dividing by PANGO_SCALE.
(stringWidth): Set pango font style and weight based on AWT style
parameter.
Index: gnu/java/awt/peer/gtk/GdkFontMetrics.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/peer/gtk/GdkFontMetrics.java,v
retrieving revision 1.3
diff -u -r1.3 GdkFontMetrics.java
--- gnu/java/awt/peer/gtk/GdkFontMetrics.java 1 Dec 2003 23:12:07 -0000 1.3
+++ gnu/java/awt/peer/gtk/GdkFontMetrics.java 15 Jun 2004 02:00:24 -0000
@@ -50,19 +50,21 @@
MAX_ADVANCE = 4;
private int[] metrics;
- private native int[] initState (String fname, int size);
+ private native int[] initState (String fname, int style, int size);
public GdkFontMetrics (Font font)
{
super (font);
- metrics = initState (font.getName (), font.getSize ());
+ metrics = initState (font.getName (), font.getStyle (), font.getSize ());
}
- native public int stringWidth (String fname, int size, String str);
+ native public int stringWidth (String fname, int style, int size,
+ String str);
public int stringWidth (String str)
{
- return stringWidth (font.getName (), font.getSize (), str);
+ return stringWidth (font.getName (), font.getStyle (), font.getSize (),
+ str);
}
public int charWidth (char ch)
Index: jni/gtk-peer/gnu_java_awt_peer_gtk_GdkFontMetrics.c
===================================================================
RCS file: /cvs/gcc/gcc/libjava/jni/gtk-peer/gnu_java_awt_peer_gtk_GdkFontMetrics.c,v
retrieving revision 1.3.8.1
diff -u -r1.3.8.1 gnu_java_awt_peer_gtk_GdkFontMetrics.c
--- jni/gtk-peer/gnu_java_awt_peer_gtk_GdkFontMetrics.c 27 May 2004 18:40:20 -0000 1.3.8.1
+++ jni/gtk-peer/gnu_java_awt_peer_gtk_GdkFontMetrics.c 15 Jun 2004 02:00:24 -0000
@@ -47,7 +47,8 @@
#define NUM_METRICS 5
JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_gtk_GdkFontMetrics_initState
- (JNIEnv *env, jobject obj __attribute__((unused)), jstring fname, jint size)
+ (JNIEnv *env, jobject obj __attribute__((unused)),
+ jstring fname, jint style, jint size)
{
jintArray array;
jint *metrics;
@@ -66,19 +67,26 @@
font_desc = pango_font_description_from_string (font_name);
pango_font_description_set_size (font_desc, size * dpi_conversion_factor);
+ if (style & AWT_STYLE_BOLD)
+ pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
+
+ if (style & AWT_STYLE_ITALIC)
+ pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
+
context = gdk_pango_context_get();
pango_context_set_font_description (context, font_desc);
- pango_metrics = pango_context_get_metrics (context, font_desc, NULL);
+ pango_metrics = pango_context_get_metrics (context, font_desc,
+ gtk_get_default_language ());
metrics[ASCENT] =
- pango_font_metrics_get_ascent (pango_metrics) / PANGO_SCALE;
+ PANGO_PIXELS (pango_font_metrics_get_ascent (pango_metrics));
metrics[MAX_ASCENT] = metrics[ASCENT];
metrics[DESCENT] =
- pango_font_metrics_get_descent (pango_metrics) / PANGO_SCALE;
+ PANGO_PIXELS (pango_font_metrics_get_descent (pango_metrics));
metrics[MAX_DESCENT] = metrics[DESCENT];
metrics[MAX_ADVANCE] =
- pango_font_metrics_get_approximate_char_width (pango_metrics) / PANGO_SCALE;
+ PANGO_PIXELS (pango_font_metrics_get_approximate_char_width (pango_metrics));
pango_font_metrics_unref (pango_metrics);
@@ -94,7 +102,7 @@
JNIEXPORT jint JNICALL Java_gnu_java_awt_peer_gtk_GdkFontMetrics_stringWidth
(JNIEnv *env, jobject obj __attribute__((unused)),
- jstring fname, jint size, jstring str)
+ jstring fname, jint style, jint size, jstring str)
{
PangoFontDescription *font_desc;
PangoContext *context;
@@ -111,12 +119,19 @@
font_desc = pango_font_description_from_string (font_name);
pango_font_description_set_size (font_desc, size * dpi_conversion_factor);
+ if (style & AWT_STYLE_BOLD)
+ pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
+
+ if (style & AWT_STYLE_ITALIC)
+ pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
+
context = gdk_pango_context_get();
pango_context_set_font_description (context, font_desc);
layout = pango_layout_new (context);
pango_layout_set_text (layout, cstr, -1);
+
pango_layout_get_pixel_size (layout, &width, NULL);
pango_font_description_free (font_desc);