Home | History | Annotate | Download | only in l10n
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/base/l10n/l10n_util_win.h"
      6 
      7 #include <windowsx.h>
      8 #include <algorithm>
      9 #include <iterator>
     10 
     11 #include "base/i18n/rtl.h"
     12 #include "base/lazy_instance.h"
     13 #include "base/strings/string_number_conversions.h"
     14 #include "base/win/i18n.h"
     15 #include "base/win/windows_version.h"
     16 #include "grit/app_locale_settings.h"
     17 #include "ui/base/l10n/l10n_util.h"
     18 #include "ui/base/win/dpi.h"
     19 
     20 namespace {
     21 
     22 void AdjustLogFont(const string16& font_family,
     23                    double font_size_scaler,
     24                    double dpi_scale,
     25                    LOGFONT* logfont) {
     26   DCHECK(font_size_scaler > 0);
     27   font_size_scaler = std::max(std::min(font_size_scaler, 2.0), 0.7);
     28   // Font metrics are computed in pixels and scale in high-DPI mode.
     29   // Normalized by the DPI scale factor in order to work in DIP with
     30   // Views/Aura. Call with dpi_scale=1 to keep the size in pixels.
     31   font_size_scaler /= dpi_scale;
     32   logfont->lfHeight = static_cast<long>(font_size_scaler *
     33       static_cast<double>(abs(logfont->lfHeight)) + 0.5) *
     34       (logfont->lfHeight > 0 ? 1 : -1);
     35 
     36   // TODO(jungshik): We may want to check the existence of the font.
     37   // If it's not installed, we shouldn't adjust the font.
     38   if (font_family != L"default") {
     39     int name_len = std::min(static_cast<int>(font_family.size()),
     40                             LF_FACESIZE -1);
     41     memcpy(logfont->lfFaceName, font_family.data(), name_len * sizeof(WORD));
     42     logfont->lfFaceName[name_len] = 0;
     43   }
     44 }
     45 
     46 bool IsFontPresent(const wchar_t* font_name) {
     47   HFONT hfont = CreateFont(12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     48                            font_name);
     49   if (hfont == NULL)
     50     return false;
     51   HDC dc = GetDC(0);
     52   HGDIOBJ oldFont = static_cast<HFONT>(SelectObject(dc, hfont));
     53   WCHAR actual_font_name[LF_FACESIZE];
     54   DWORD size_ret = GetTextFace(dc, LF_FACESIZE, actual_font_name);
     55   actual_font_name[LF_FACESIZE - 1] = 0;
     56   SelectObject(dc, oldFont);
     57   DeleteObject(hfont);
     58   ReleaseDC(0, dc);
     59   // We don't have to worry about East Asian fonts with locale-dependent
     60   // names here.
     61   return wcscmp(font_name, actual_font_name) == 0;
     62 }
     63 
     64 class OverrideLocaleHolder {
     65  public:
     66   OverrideLocaleHolder() {}
     67   const std::vector<std::string>& value() const { return value_; }
     68   void swap_value(std::vector<std::string>* override_value) {
     69     value_.swap(*override_value);
     70   }
     71  private:
     72   std::vector<std::string> value_;
     73   DISALLOW_COPY_AND_ASSIGN(OverrideLocaleHolder);
     74 };
     75 
     76 base::LazyInstance<OverrideLocaleHolder> override_locale_holder =
     77     LAZY_INSTANCE_INITIALIZER;
     78 
     79 }  // namespace
     80 
     81 namespace l10n_util {
     82 
     83 int GetExtendedStyles() {
     84   return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL | WS_EX_RTLREADING;
     85 }
     86 
     87 int GetExtendedTooltipStyles() {
     88   return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL;
     89 }
     90 
     91 void HWNDSetRTLLayout(HWND hwnd) {
     92   DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
     93 
     94   // We don't have to do anything if the style is already set for the HWND.
     95   if (!(ex_style & WS_EX_LAYOUTRTL)) {
     96     ex_style |= WS_EX_LAYOUTRTL;
     97     ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
     98 
     99     // Right-to-left layout changes are not applied to the window immediately
    100     // so we should make sure a WM_PAINT is sent to the window by invalidating
    101     // the entire window rect.
    102     ::InvalidateRect(hwnd, NULL, true);
    103   }
    104 }
    105 
    106 bool IsLocaleSupportedByOS(const std::string& locale) {
    107   // Block Amharic on Windows XP unless 'Abyssinica SIL' font is present.
    108   // On Win XP, no Ethiopic/Amahric font is availabel out of box. We hard-coded
    109   // 'Abyssinica SIL' in the resource bundle to use in the UI. Check
    110   // for its presence to determine whether or not to support Amharic UI on XP.
    111   return (base::win::GetVersion() >= base::win::VERSION_VISTA ||
    112       !LowerCaseEqualsASCII(locale, "am") || IsFontPresent(L"Abyssinica SIL"));
    113 }
    114 
    115 bool NeedOverrideDefaultUIFont(string16* override_font_family,
    116                                double* font_size_scaler) {
    117   // This is rather simple-minded to deal with the UI font size
    118   // issue for some Indian locales (ml, bn, hi) for which
    119   // the default Windows fonts are too small to be legible.  For those
    120   // locales, IDS_UI_FONT_FAMILY is set to an actual font family to
    121   // use while for other locales, it's set to 'default'.
    122 
    123   // XP and Vista or later have different font size issues and
    124   // we need separate ui font specifications.
    125   int ui_font_family_id = IDS_UI_FONT_FAMILY;
    126   int ui_font_size_scaler_id = IDS_UI_FONT_SIZE_SCALER;
    127   if (base::win::GetVersion() < base::win::VERSION_VISTA) {
    128     ui_font_family_id = IDS_UI_FONT_FAMILY_XP;
    129     ui_font_size_scaler_id = IDS_UI_FONT_SIZE_SCALER_XP;
    130   }
    131 
    132   string16 ui_font_family = GetStringUTF16(ui_font_family_id);
    133   int scaler100;
    134   if (!base::StringToInt(l10n_util::GetStringUTF16(ui_font_size_scaler_id),
    135                          &scaler100))
    136     return false;
    137 
    138   // We use the OS default in two cases:
    139   // 1) The resource bundle has 'default' and '100' for font family and
    140   //    font scaler.
    141   // 2) The resource bundle is not available for some reason and
    142   //    ui_font_family is empty.
    143   if (ui_font_family == L"default" && scaler100 == 100 ||
    144       ui_font_family.empty())
    145     return false;
    146   if (override_font_family && font_size_scaler) {
    147     override_font_family->swap(ui_font_family);
    148     *font_size_scaler = scaler100 / 100.0;
    149   }
    150   return true;
    151 }
    152 
    153 void AdjustUIFont(LOGFONT* logfont) {
    154   AdjustUIFontForDIP(ui::GetDPIScale(), logfont);
    155 }
    156 
    157 void AdjustUIFontForDIP(float dpi_scale, LOGFONT* logfont) {
    158   string16 ui_font_family = L"default";
    159   double ui_font_size_scaler = 1;
    160   if (NeedOverrideDefaultUIFont(&ui_font_family, &ui_font_size_scaler) ||
    161       dpi_scale != 1) {
    162     AdjustLogFont(ui_font_family, ui_font_size_scaler, dpi_scale, logfont);
    163   }
    164 }
    165 
    166 void AdjustUIFontForWindow(HWND hwnd) {
    167   string16 ui_font_family;
    168   double ui_font_size_scaler;
    169   if (NeedOverrideDefaultUIFont(&ui_font_family, &ui_font_size_scaler)) {
    170     LOGFONT logfont;
    171     if (GetObject(GetWindowFont(hwnd), sizeof(logfont), &logfont)) {
    172       double dpi_scale = 1;
    173       AdjustLogFont(ui_font_family, ui_font_size_scaler, dpi_scale, &logfont);
    174       HFONT hfont = CreateFontIndirect(&logfont);
    175       if (hfont)
    176         SetWindowFont(hwnd, hfont, FALSE);
    177     }
    178   }
    179 }
    180 
    181 void OverrideLocaleWithUILanguageList() {
    182   std::vector<std::wstring> ui_languages;
    183   if (base::win::i18n::GetThreadPreferredUILanguageList(&ui_languages)) {
    184     std::vector<std::string> ascii_languages;
    185     ascii_languages.reserve(ui_languages.size());
    186     std::transform(ui_languages.begin(), ui_languages.end(),
    187                    std::back_inserter(ascii_languages), &WideToASCII);
    188     override_locale_holder.Get().swap_value(&ascii_languages);
    189   } else {
    190     NOTREACHED() << "Failed to determine the UI language for locale override.";
    191   }
    192 }
    193 
    194 const std::vector<std::string>& GetLocaleOverrides() {
    195   return override_locale_holder.Get().value();
    196 }
    197 
    198 }  // namespace l10n_util
    199