Bug #176842. gtk2 UI fonts are wonky. Use the proper default font for gtk2. Make sure to use the gtk2 dpi and xft dpi when they are available and fall back to the physical dpi if they aren't set for xft and gtk2 builds only. r=bryner,sr=shaver,a=asa

This commit is contained in:
blizzard%redhat.com 2002-11-01 22:06:30 +00:00
Родитель 37dab08d79
Коммит ba1a8da285
3 изменённых файлов: 158 добавлений и 39 удалений

Просмотреть файл

@ -66,10 +66,25 @@
#include "gdksuperwin.h"
#endif /* MOZ_WIDGET_GTK */
#ifdef MOZ_WIDGET_GTK2
#include <pango/pango.h>
#endif
#ifdef MOZ_ENABLE_XFT
#include "nsFontMetricsUtils.h"
#include <X11/Xlib.h>
#include <X11/Xdefs.h>
#include <X11/Xft/Xft.h>
static PRInt32 GetXftDPI(void);
#endif
#include <X11/Xatom.h>
#include "nsDeviceContextSpecG.h"
static PRInt32 GetOSDPI(void);
static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
#define GDK_DEFAULT_FONT1 "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1"
@ -95,7 +110,7 @@ class nsSystemFontsGTK {
const nsFont& GetButtonFont() { return mButtonFont; }
private:
nsresult GetSystemFontInfo(GtkStyle *aStyle, nsFont* aFont,
nsresult GetSystemFontInfo(GtkWidget *aWidget, nsFont* aFont,
float aPixelsToTwips) const;
/*
@ -604,9 +619,7 @@ NS_IMETHODIMP nsDeviceContextGTK::GetDepth(PRUint32& aDepth)
nsresult
nsDeviceContextGTK::SetDPI(PRInt32 aPrefDPI)
{
// Set OSVal to what the operating system thinks the logical resolution is.
float screenWidthIn = float(::gdk_screen_width_mm()) / 25.4f;
PRInt32 OSVal = NSToCoordRound(float(::gdk_screen_width()) / screenWidthIn);
PRInt32 OSVal = GetOSDPI();
if (aPrefDPI > 0) {
// If there's a valid pref value for the logical resolution,
@ -689,8 +702,7 @@ nsSystemFontsGTK::nsSystemFontsGTK(float aPixelsToTwips)
gtk_widget_realize(parent);
gtk_widget_realize(label);
GtkStyle *style = gtk_widget_get_style(label);
GetSystemFontInfo(style, &mDefaultFont, aPixelsToTwips);
GetSystemFontInfo(label, &mDefaultFont, aPixelsToTwips);
gtk_widget_destroy(window); // no unref, windows are different
@ -705,8 +717,7 @@ nsSystemFontsGTK::nsSystemFontsGTK(float aPixelsToTwips)
gtk_widget_set_rc_style(entry);
gtk_widget_realize(entry);
style = gtk_widget_get_style(entry);
GetSystemFontInfo(style, &mFieldFont, aPixelsToTwips);
GetSystemFontInfo(entry, &mFieldFont, aPixelsToTwips);
gtk_widget_destroy(window); // no unref, windows are different
@ -725,8 +736,7 @@ nsSystemFontsGTK::nsSystemFontsGTK(float aPixelsToTwips)
gtk_widget_realize(menu);
gtk_widget_realize(accel_label);
style = gtk_widget_get_style(accel_label);
GetSystemFontInfo(style, &mMenuFont, aPixelsToTwips);
GetSystemFontInfo(accel_label, &mMenuFont, aPixelsToTwips);
gtk_widget_unref(menu);
@ -746,8 +756,7 @@ nsSystemFontsGTK::nsSystemFontsGTK(float aPixelsToTwips)
gtk_widget_realize(button);
gtk_widget_realize(label);
style = gtk_widget_get_style(label);
GetSystemFontInfo(style, &mButtonFont, aPixelsToTwips);
GetSystemFontInfo(label, &mButtonFont, aPixelsToTwips);
gtk_widget_destroy(window); // no unref, windows are different
@ -784,7 +793,9 @@ ListFontProps(XFontStruct *aFont, Display *aDisplay)
if (pos < 0) \
return ; \
}
#ifdef MOZ_WIDGET_GTK
static void
AppendFontFFREName(nsString& aString, const char* aXLFDName)
{
@ -819,12 +830,7 @@ AppendFontName(XFontStruct* aFontStruct, nsString& aString, Display *aDisplay)
{
unsigned long pr = 0;
// we first append the FFRE name to reconstruct font more faithfully
#ifdef MOZ_WIDGET_GTK
unsigned long font_atom = gdk_atom_intern("FONT", FALSE);
#endif
#ifdef MOZ_WIDGET_GTK2
unsigned long font_atom = gdk_x11_get_xatom_by_name("FONT");
#endif
::XGetFontProperty(aFontStruct, font_atom, &pr);
if(pr) {
char* xlfdName = ::XGetAtomName(aDisplay, pr);
@ -884,15 +890,12 @@ GetFontSize(XFontStruct *aFontStruct, float aPixelsToTwips)
}
nsresult
nsSystemFontsGTK::GetSystemFontInfo(GtkStyle *aStyle, nsFont* aFont, float aPixelsToTwips) const
nsSystemFontsGTK::GetSystemFontInfo(GtkWidget *aWidget, nsFont* aFont,
float aPixelsToTwips) const
{
#ifdef MOZ_WIDGET_GTK
GdkFont *theFont = aStyle->font;
#endif /* MOZ_WIDGET_GTK */
GtkStyle *style = gtk_widget_get_style(aWidget);
#ifdef MOZ_WIDGET_GTK2
GdkFont *theFont = gtk_style_get_font(aStyle);
#endif /* MOZ_WIDGET_GTK2 */
GdkFont *theFont = style->font;
aFont->style = NS_FONT_STYLE_NORMAL;
aFont->weight = NS_FONT_WEIGHT_NORMAL;
@ -901,10 +904,8 @@ nsSystemFontsGTK::GetSystemFontInfo(GtkStyle *aStyle, nsFont* aFont, float aPixe
// do we have the default_font defined by GTK/GDK then
// we use it, if not then we load helvetica, if not then
// we load fixed font else we error out.
#ifdef MOZ_WIDGET_GTK
if (!theFont)
theFont = default_font; // GTK default font
#endif
if (!theFont)
theFont = ::gdk_font_load( GDK_DEFAULT_FONT1 );
@ -953,3 +954,111 @@ nsSystemFontsGTK::GetSystemFontInfo(GtkStyle *aStyle, nsFont* aFont, float aPixe
}
return NS_OK;
}
#endif /* MOZ_WIDGET_GTK */
#ifdef MOZ_WIDGET_GTK2
nsresult
nsSystemFontsGTK::GetSystemFontInfo(GtkWidget *aWidget, nsFont* aFont,
float aPixelsToTwips) const
{
GtkSettings *settings = gtk_widget_get_settings(aWidget);
aFont->style = NS_FONT_STYLE_NORMAL;
aFont->decorations = NS_FONT_DECORATION_NONE;
gchar *fontname;
g_object_get(settings, "gtk-font-name", &fontname, NULL);
PangoFontDescription *desc;
desc = pango_font_description_from_string(fontname);
aFont->name.Truncate();
aFont->name.AppendWithConversion(pango_font_description_get_family(desc));
aFont->weight = pango_font_description_get_weight(desc);
gint size;
size = pango_font_description_get_size(desc);
aFont->size = NSIntPointsToTwips(size / PANGO_SCALE);
return NS_OK;
}
#endif /* MOZ_WIDGET_GTK2 */
#ifdef MOZ_WIDGET_GTK
/* static */
PRInt32
GetOSDPI(void)
{
#ifdef MOZ_ENABLE_XFT
// try to get it from xft
if (NS_IsXftEnabled()) {
PRInt32 xftdpi = GetXftDPI();
if (xftdpi)
return xftdpi;
}
#endif /* MOZ_ENABLE_XFT */
// Set OSVal to what the operating system thinks the logical resolution is.
float screenWidthIn = float(::gdk_screen_width_mm()) / 25.4f;
return NSToCoordRound(float(::gdk_screen_width()) / screenWidthIn);
}
#endif /* MOZ_WIDGET_GTK */
#ifdef MOZ_WIDGET_GTK2
/* static */
PRInt32
GetOSDPI(void)
{
GtkSettings *settings = gtk_settings_get_default();
// first try to get the gtk2 dpi
gint dpi = 0;
// See if there's a gtk-xft-dpi object on the settings object - note
// that we don't have to free the spec since it isn't addrefed
// before being returned. It's just part of an internal object.
// The gtk-xft-dpi setting is included in rh8 and might be included
// in later versions of gtk, so we conditionally check for it.
GParamSpec *spec;
spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(settings)),
"gtk-xft-dpi");
if (spec) {
g_object_get(G_OBJECT(settings),
"gtk-xft-dpi", &dpi,
NULL);
}
if (dpi)
return NSToCoordRound(dpi / PANGO_SCALE);
// try to get it from xft
PRInt32 xftdpi = GetXftDPI();
if (xftdpi)
return xftdpi;
// fall back to the physical resolution
float screenWidthIn = float(::gdk_screen_width_mm()) / 25.4f;
return NSToCoordRound(float(::gdk_screen_width()) / screenWidthIn);
}
#endif /* MOZ_WIDGET_GTK2 */
#ifdef MOZ_ENABLE_XFT
/* static */
PRInt32
GetXftDPI(void)
{
char *val = XGetDefault(GDK_DISPLAY(), "Xft", "dpi");
if (val) {
char *e;
double d = strtod(val, &e);
if (e != val)
return NSToCoordRound(d);
}
return 0;
}
#endif /* MOZ_ENABLE_XFT */

Просмотреть файл

@ -196,13 +196,17 @@ nsFontMetricsXft::Init(const nsFont& aFont, nsIAtom* aLangGroup,
// Hang onto the device context
mDeviceContext = aContext;
float app2dev;
mDeviceContext->GetAppUnitsToDevUnits(app2dev);
mPointSize = NSTwipsToIntPoints(mFont->size);
mPixelSize = NSToIntRound(app2dev * mFont->size);
// Make sure to clamp the pixel size to something reasonable so we
// pixels -> twips ; twips -> points
float dev2app;
mDeviceContext->GetDevUnitsToAppUnits(dev2app);
nscoord screenTwips = NSIntPixelsToTwips(gdk_screen_height(), dev2app);
nscoord screenPoints = NSTwipsToIntPoints(screenTwips);
// Make sure to clamp the point size to something reasonable so we
// don't make the X server blow up.
mPixelSize = PR_MIN(gdk_screen_height() * FONT_MAX_FONT_SCALE, mPixelSize);
mPointSize = PR_MIN(screenPoints * FONT_MAX_FONT_SCALE, mPointSize);
// enumerate over the font names passed in
mFont->EnumerateFamilies(nsFontMetricsXft::EnumFontCallback, this);
@ -247,11 +251,17 @@ nsFontMetricsXft::Init(const nsFont& aFont, nsIAtom* aLangGroup,
res = prefService->GetIntPref(name.get(), &minimum);
if (NS_FAILED(res))
prefService->GetDefaultIntPref(name.get(), &minimum);
if (minimum < 0)
minimum = 0;
if (mPixelSize < minimum)
mPixelSize = minimum;
// convert the minimum size into points
float P2T;
mDeviceContext->GetDevUnitsToAppUnits(P2T);
minimum = NSTwipsToIntPoints(NSFloatPixelsToTwips(minimum, P2T));
if (mPointSize < minimum)
mPointSize = minimum;
}
if (NS_FAILED(RealizeFont()))
@ -948,7 +958,7 @@ nsFontMetricsXft::SetupFCPattern(void)
AddFFRE(mPattern, mGenericFont, PR_FALSE);
// add the pixel size
FcPatternAddInteger(mPattern, FC_PIXEL_SIZE, mPixelSize);
FcPatternAddInteger(mPattern, FC_SIZE, mPointSize);
// Add the slant type
FcPatternAddInteger(mPattern, FC_SLANT,
@ -1114,8 +1124,8 @@ nsFontMetricsXft::SetupMiniFont(void)
FcPatternAddString(pattern, FC_FAMILY, (FcChar8 *)"monospace");
FcPatternAddInteger(pattern, FC_PIXEL_SIZE,
(int)(0.5 * mPixelSize));
FcPatternAddInteger(pattern, FC_SIZE,
(int)(0.5 * mPointSize));
FcPatternAddInteger(pattern, FC_WEIGHT,
CalculateWeight(mFont->weight));

Просмотреть файл

@ -237,7 +237,7 @@ private:
nsCOMPtr<nsIAtom> mLangGroup;
nsCString *mGenericFont;
nsFont *mFont;
PRUint16 mPixelSize;
PRUint16 mPointSize;
nsCAutoString mDefaultFont;