From d94d86a3747bad05bdb342ba9c42927fa71d1ac8 Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Fri, 21 Apr 2000 21:36:10 +0000 Subject: [PATCH] Added language sensitive font selection (disabled as a default), bug 26182. git-svn-id: svn://10.0.0.236/trunk@66751 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/mailnews/mime/src/mimemoz2.cpp | 135 ++++++++++++++++++++++--- mozilla/mailnews/mime/src/mimemoz2.h | 4 +- mozilla/mailnews/mime/src/mimethtm.cpp | 2 +- mozilla/mailnews/mime/src/mimetpfl.cpp | 2 +- mozilla/mailnews/mime/src/mimetpla.cpp | 2 +- 5 files changed, 125 insertions(+), 20 deletions(-) diff --git a/mozilla/mailnews/mime/src/mimemoz2.cpp b/mozilla/mailnews/mime/src/mimemoz2.cpp index 867686c8b7a..762beeb7ac2 100644 --- a/mozilla/mailnews/mime/src/mimemoz2.cpp +++ b/mozilla/mailnews/mime/src/mimemoz2.cpp @@ -61,6 +61,8 @@ #include "nsIMIMEService.h" #include "nsIImapUrl.h" #include "nsMsgI18N.h" +#include "nsICharsetConverterManager.h" +#include "nsICharsetAlias.h" #include "nsIIOService.h" #include "nsIURI.h" @@ -1762,52 +1764,157 @@ ResetChannelCharset(MimeObject *obj) // Functions to set up mail/news font //////////////////////////////////////////////////////////// -int BeginMailNewsFont(MimeObject *obj, const char *prefFontName, const char *prefFontSize) +// input: part of pref key for font name and size, e.g. "font.name.serif.", "font.size.fixed." , +// output: HTML tag to the caller allocated buffer +static int DoLanguageSensitiveFont(MimeObject *obj, const char *prefixName, const char *prefixSize) +{ + nsCOMPtr aCharSets; + nsCOMPtr aLangGroup; + MimeInlineText *text = (MimeInlineText *) obj; + nsAutoString aCharset; + const PRUnichar* langGroup = nsnull; + nsCAutoString aPrefStr(prefixName); + nsresult rv; + int status = -1; + + + if (!text->charset || !(*text->charset)) + goto done; + + // no alias resolution for performance + // if we get alias then no font tag to be generated (i think that's acceptable). + aCharset.Assign(text->charset); + aCharset.ToLowerCase(); + + + aCharSets = do_GetService(NS_CHARSETCONVERTERMANAGER_PROGID); + if (aCharSets) { + // get a language, e.g. x-western, ja + rv = aCharSets->GetCharsetLangGroup(&aCharset, getter_AddRefs(aLangGroup)); + if (NS_FAILED(rv)) + goto done; + rv = aLangGroup->GetUnicode(&langGroup); + if (NS_FAILED(rv)) + goto done; + + // append the language to the pref string + aPrefStr.Append(langGroup); + + nsIPref *aPrefs = GetPrefServiceManager(obj->options); + + if (aPrefs) { + nsCAutoString fontName; + PRUnichar *unicode = nsnull; + PRInt32 fontSize; + PRInt32 screenRes; + + // get a font name from pref, could be non ascii (need charset conversion) + // this is not necessary if we insert this tag after the message is converted to UTF-8 + rv = aPrefs->CopyUnicharPref(aPrefStr, &unicode); + if (NS_FAILED(rv)) + goto done; + + nsAutoString tempStr(unicode); + PR_FREEIF(unicode); + + rv = nsMsgI18NConvertFromUnicode(aCharset, tempStr, fontName); + if (NS_FAILED(rv)) + goto done; + + // get a font size from pref + aPrefStr.Assign(prefixSize); + aPrefStr.Append(langGroup); + rv = aPrefs->GetIntPref(aPrefStr, &fontSize); + if (NS_FAILED(rv)) + goto done; + + // get a screen resolution + rv = aPrefs->GetIntPref("browser.screen_resolution", &screenRes); + if (NS_FAILED(rv)) + goto done; + + fontSize = fontSize * 72 / screenRes; + + char buf[256]; + PR_snprintf(buf, 256, "
\n", (const char *) fontName, fontSize); + + status = MimeObject_write(obj, buf, nsCRT::strlen(buf), PR_FALSE); + } + } + +done: + // if nothing has been written then write out just "
" to match with "
" + if (status == -1) + status = MimeObject_write(obj, "
\n", 6, PR_FALSE); + + return status; +} + +int BeginMailNewsFont(MimeObject *obj) { nsIPref *aPrefs = GetPrefServiceManager(obj->options); int status = -1; + nsresult rv; if (aPrefs) { + // check Content-Type: + PRBool bTEXT_HTML = PR_FALSE; + if (!nsCRT::strcasecmp(obj->content_type, TEXT_HTML)) + bTEXT_HTML = PR_TRUE; + else if (nsCRT::strcasecmp(obj->content_type, TEXT_PLAIN)) + goto done; // not supported type + + // if indicated by the pref, do language sensitive font selection (slower) + PRBool languageSensitiveFont = PR_FALSE; + rv = aPrefs->GetBoolPref("mailnews.language_sensitive_font", &languageSensitiveFont); + if (NS_SUCCEEDED(rv) && languageSensitiveFont) + return bTEXT_HTML ? + DoLanguageSensitiveFont(obj, "font.name.serif.", "font.size.variable.") : + DoLanguageSensitiveFont(obj, "font.name.monospace.", "font.size.fixed."); + + // otherwise, use the mailnews font setting from pref MimeInlineText *text = (MimeInlineText *) obj; - // todo: use folder charset instead of hard coded ISO-8859-1 - nsAutoString aCharset(text->charset ? text->charset : "ISO-8859-1"); + nsString aCharset; PRUnichar *unicode = nsnull; - char *fontName = nsnull; + nsCAutoString fontName; PRInt32 fontSize; PRInt32 screenRes; - nsresult rv; + + if (!text->charset || !(*text->charset)) + goto done; + aCharset.Assign(text->charset); // get a font name from pref, could be non ascii (need charset conversion) // this is not necessary if we insert this tag after the message is converted to UTF-8 - rv = aPrefs->CopyUnicharPref(prefFontName, &unicode); + rv = aPrefs->CopyUnicharPref(bTEXT_HTML ? "mailnews.font.name.html" : "mailnews.font.name.plain", &unicode); if (NS_FAILED(rv)) goto done; - rv = ConvertFromUnicode(aCharset, nsAutoString(unicode), &fontName); + nsAutoString tempStr(unicode); + PR_FREEIF(unicode); + rv = nsMsgI18NConvertFromUnicode(aCharset, tempStr, fontName); if (NS_FAILED(rv)) goto done; // get a font size from pref - rv = aPrefs->GetIntPref(prefFontSize, &fontSize); + rv = aPrefs->GetIntPref(bTEXT_HTML ? "mailnews.font.size.html" : "mailnews.font.size.plain", &fontSize); if (NS_FAILED(rv)) goto done; // get a screen resolution rv = aPrefs->GetIntPref("browser.screen_resolution", &screenRes); - if (NS_FAILED(rv)) - goto done; + if (NS_FAILED(rv)) + goto done; fontSize = fontSize * 72 / screenRes; char buf[256]; - PR_snprintf(buf, 256, "
\n", fontName, fontSize); + PR_snprintf(buf, 256, "
\n", (const char *) fontName, fontSize); status = MimeObject_write(obj, buf, nsCRT::strlen(buf), PR_FALSE); -done: - PR_FREEIF(unicode); - PR_FREEIF(fontName); } +done: // if nothing has been written then write out just "
" to match with "
" if (status == -1) status = MimeObject_write(obj, "
\n", 6, PR_FALSE); diff --git a/mozilla/mailnews/mime/src/mimemoz2.h b/mozilla/mailnews/mime/src/mimemoz2.h index 8c5e67aa29f..ae3bb985a5e 100644 --- a/mozilla/mailnews/mime/src/mimemoz2.h +++ b/mozilla/mailnews/mime/src/mimemoz2.h @@ -161,9 +161,7 @@ extern "C" char *MimeGetStringByID(PRInt32 stringID); // Utility to create a nsIURI object... extern "C" nsresult nsMimeNewURI(nsIURI** aInstancePtrResult, const char *aSpec, nsIURI *aBase); -// input pref keys for font name and size, e.g. "mailnews.font.name.mono", "mailnews.size.fixed", -// generate and write out HTML tag -extern "C" int BeginMailNewsFont(MimeObject *obj, const char *prefFontName, const char *prefFontSize); +extern "C" int BeginMailNewsFont(MimeObject *obj); extern "C" int EndMailNewsFont(MimeObject *obj); #ifdef __cplusplus diff --git a/mozilla/mailnews/mime/src/mimethtm.cpp b/mozilla/mailnews/mime/src/mimethtm.cpp index 25b53a54876..14410ebdcd0 100644 --- a/mozilla/mailnews/mime/src/mimethtm.cpp +++ b/mozilla/mailnews/mime/src/mimethtm.cpp @@ -58,7 +58,7 @@ MimeInlineTextHTML_parse_begin (MimeObject *obj) if (nsMimeOutput::nsMimeMessageBodyDisplay == obj->options->format_out || nsMimeOutput::nsMimeMessagePrintOutput == obj->options->format_out) - status = BeginMailNewsFont(obj, "mailnews.font.name.serif", "mailnews.size.variable"); + status = BeginMailNewsFont(obj); MimeInlineTextHTML *textHTML = (MimeInlineTextHTML *) obj; diff --git a/mozilla/mailnews/mime/src/mimetpfl.cpp b/mozilla/mailnews/mime/src/mimetpfl.cpp index ab83b2e811d..030eb0e3736 100644 --- a/mozilla/mailnews/mime/src/mimetpfl.cpp +++ b/mozilla/mailnews/mime/src/mimetpfl.cpp @@ -75,7 +75,7 @@ MimeInlineTextPlainFlowed_parse_begin (MimeObject *obj) if (nsMimeOutput::nsMimeMessageBodyDisplay == obj->options->format_out || nsMimeOutput::nsMimeMessagePrintOutput == obj->options->format_out) - status = BeginMailNewsFont(obj, "mailnews.font.name.mono", "mailnews.size.fixed"); + status = BeginMailNewsFont(obj); // Setup the data structure that is connected to the actual document // Saved in a linked list in case this is called with several documents diff --git a/mozilla/mailnews/mime/src/mimetpla.cpp b/mozilla/mailnews/mime/src/mimetpla.cpp index dc489357dad..c4901fd20b5 100644 --- a/mozilla/mailnews/mime/src/mimetpla.cpp +++ b/mozilla/mailnews/mime/src/mimetpla.cpp @@ -121,7 +121,7 @@ MimeInlineTextPlain_parse_begin (MimeObject *obj) if (nsMimeOutput::nsMimeMessageBodyDisplay == obj->options->format_out || nsMimeOutput::nsMimeMessagePrintOutput == obj->options->format_out) - status = BeginMailNewsFont(obj, "mailnews.font.name.mono", "mailnews.size.fixed"); + status = BeginMailNewsFont(obj); if (obj->options && obj->options->write_html_p &&