From c0ce68f0fb2ba5bc1d21db7cdcfe4ee9d5d57f72 Mon Sep 17 00:00:00 2001 From: "tague%netscape.com" Date: Wed, 16 Jun 1999 04:12:14 +0000 Subject: [PATCH] Fixed #5438 - Mac Buttons not displaying Japanese correctly. --- widget/src/mac/nsMacControl.cpp | 135 +++++++++++++++++++++++++++++++- widget/src/mac/nsMacControl.h | 2 + 2 files changed, 133 insertions(+), 4 deletions(-) diff --git a/widget/src/mac/nsMacControl.cpp b/widget/src/mac/nsMacControl.cpp index d2b845983e10..5102fa822303 100644 --- a/widget/src/mac/nsMacControl.cpp +++ b/widget/src/mac/nsMacControl.cpp @@ -29,6 +29,10 @@ #include #endif +#include +#include +#include +#include //------------------------------------------------------------------------- // @@ -106,10 +110,7 @@ PRBool nsMacControl::OnPaint(nsPaintEvent &aEvent) // update title if (mLabel != mLastLabel) { - mLastLabel = mLabel; - Str255 aStr; - StringToStr255(mLabel, aStr); - ::SetControlTitle(mControl, aStr); + NSStringSetControlTitle(mControl,mLabel); } // update bounds @@ -400,3 +401,129 @@ void nsMacControl::Str255ToString(const Str255& aStr255, nsString& aText) aText = buffer; } + +//------------------------------------------------------------------------- +// +// +//------------------------------------------------------------------------- + +void nsMacControl::NSStringSetControlTitle(ControlHandle theControl, nsString title) +{ + TextStyle theStyle; + ScriptCode fontScript; + OSErr err; + UnicodeToTextRunInfo unicodeTextRunInfo; + const PRUnichar* unicodeText; + char* scriptRunText; + size_t unicodeTextLengthInBytes, unicodeTextReadInBytes, + scriptRunTextSizeInBytes, scriptRunTextLengthInBytes, + scriptCodeRunListLength; + ScriptCodeRun convertedTextScript; + + NS_PRECONDITION(mFontMetrics != nsnull, "nsMacControl::NSStringSetControlTitle: no Font Metrics"); + + // + // determine the script of the font that the control is supposed to be drawn in + // + nsFontMetricsMac::GetNativeTextStyle(*mFontMetrics, *mContext, theStyle); + fontScript = ::FontToScript(theStyle.tsFont); + + // + // create a Unicode Conveter object (from Unicode -> font) + // + err = ::CreateUnicodeToTextRunInfoByScriptCode(1,&fontScript,&unicodeTextRunInfo); + NS_ASSERTION(err==noErr,"nsMacControl::NSStringSetControlTitle: CreateUnicodeToTextRunInfoByScriptCode failed."); + if (err!=noErr) { return; } + + // + // get the Unicode text and prepare buffers + // + unicodeText = title.GetUnicode(); + unicodeTextLengthInBytes = title.Length() * sizeof(PRUnichar); + scriptRunTextSizeInBytes = unicodeTextLengthInBytes * 2; + scriptRunText = new char[scriptRunTextSizeInBytes]; + + + // + // convert from Unicode to script run + // + err = ::ConvertFromUnicodeToScriptCodeRun(unicodeTextRunInfo, + unicodeTextLengthInBytes,unicodeText, + 0, /* no flags */ + 0,NULL,NULL,NULL, /* no offset arrays */ + scriptRunTextSizeInBytes,&unicodeTextReadInBytes,&scriptRunTextLengthInBytes, + scriptRunText, + 1 /* count of scrip runs*/,&scriptCodeRunListLength,&convertedTextScript); + if (err!=noErr) + { + // + // the font script is not capable of rendering this string, we need to find an installed + // script that can + // + err = ::CreateUnicodeToTextRunInfoByScriptCode(0,NULL,&unicodeTextRunInfo); + NS_ASSERTION(err==noErr,"nsMacControl::NSStringSetControlTitle: CreateUnicodeToTextRunInfoByScriptCode failed."); + if (err!=noErr) { return; } + + // + // convert from Unicode to script run + // + err = ::ConvertFromUnicodeToScriptCodeRun(unicodeTextRunInfo, + unicodeTextLengthInBytes,unicodeText, + 0, /* no flags */ + 0,NULL,NULL,NULL, /* no offset arrays */ + scriptRunTextSizeInBytes,&unicodeTextReadInBytes,&scriptRunTextLengthInBytes, + scriptRunText, + 1 /* count of scrip runs*/,&scriptCodeRunListLength,&convertedTextScript); + NS_ASSERTION(err==noErr,"nsMacControl::NSStringSetControlTitle: CreateUnicodeToTextRunInfoByScriptCode failed."); + if (err!=noErr) { delete [] scriptRunText; return;} + } + + scriptRunText[scriptRunTextLengthInBytes] = 0; // null terminate + + if (convertedTextScript.script!=fontScript) + SetupMacControlFontForScript(convertedTextScript.script); + + // + // set the control title + // + ::SetControlTitle(theControl,c2pstr(scriptRunText)); + delete [] scriptRunText; + +} +//------------------------------------------------------------------------- +// +// +//------------------------------------------------------------------------- +void nsMacControl::SetupMacControlFontForScript(short theScript) +{ + short themeFontID; + Str255 themeFontName; + SInt16 themeFontSize; + Style themeFontStyle; + TextStyle theStyle; + OSErr err; + + NS_PRECONDITION(mFontMetrics != nsnull, "No font metrics in SetupMacControlFont"); + NS_PRECONDITION(mContext != nsnull, "No context metrics in SetupMacControlFont"); + + nsFontMetricsMac::GetNativeTextStyle(*mFontMetrics, *mContext, theStyle); + + // + // take the script and select and override font + // + err = ::GetThemeFont(kThemeSystemFont,theScript,themeFontName,&themeFontSize,&themeFontStyle); + NS_ASSERTION(err==noErr,"nsMenu::NSStringNewMenu: GetThemeFont failed."); + ::GetFNum(themeFontName,&themeFontID); + + // if needed, impose a min size of 9pt on the control font + if (theStyle.tsSize < 9) + theStyle.tsSize = 9; + + ControlFontStyleRec fontStyleRec; + fontStyleRec.flags = (kControlUseFontMask | kControlUseFaceMask | kControlUseSizeMask); + fontStyleRec.font = themeFontID; + fontStyleRec.size = theStyle.tsSize; + fontStyleRec.style = theStyle.tsFace; + ::SetControlFontStyle(mControl, &fontStyleRec); +} + \ No newline at end of file diff --git a/widget/src/mac/nsMacControl.h b/widget/src/mac/nsMacControl.h index ae7fc4d6a4e7..6c01abe5e5d8 100644 --- a/widget/src/mac/nsMacControl.h +++ b/widget/src/mac/nsMacControl.h @@ -62,6 +62,8 @@ protected: virtual void GetRectForMacControl(nsRect &outRect); void SetupMacControlFont(); void ControlChanged(PRInt32 aNewValue); + void NSStringSetControlTitle(ControlHandle theControl, nsString title); + void SetupMacControlFontForScript(short theScript); nsString mLabel; PRBool mWidgetArmed;