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