When Retrier succeeds, record errors it encountered.
[chromium-blink-merge.git] / ui / base / l10n / l10n_util_win.cc
blobabb5169b385e34054106d2b1189bec04338c0d75
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.
5 #include "ui/base/l10n/l10n_util_win.h"
7 #include <windowsx.h>
8 #include <algorithm>
9 #include <iterator>
11 #include "base/i18n/rtl.h"
12 #include "base/lazy_instance.h"
13 #include "base/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"
20 namespace {
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);
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;
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;
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);
71 private:
72 std::vector<std::string> value_;
73 DISALLOW_COPY_AND_ASSIGN(OverrideLocaleHolder);
76 base::LazyInstance<OverrideLocaleHolder> override_locale_holder =
77 LAZY_INSTANCE_INITIALIZER;
79 } // namespace
81 namespace l10n_util {
83 int GetExtendedStyles() {
84 return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL | WS_EX_RTLREADING;
87 int GetExtendedTooltipStyles() {
88 return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL;
91 void HWNDSetRTLLayout(HWND hwnd) {
92 DWORD ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
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);
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);
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"));
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'.
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;
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;
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;
150 return true;
153 void AdjustUIFont(LOGFONT* logfont) {
154 #if defined(ENABLE_HIDPI)
155 float dpi_scale = ui::GetDPIScale();
156 #else
157 float dpi_scale = 1;
158 #endif
159 AdjustUIFontForDIP(dpi_scale, logfont);
162 void AdjustUIFontForDIP(float dpi_scale, LOGFONT* logfont) {
163 string16 ui_font_family = L"default";
164 double ui_font_size_scaler = 1;
165 if (NeedOverrideDefaultUIFont(&ui_font_family, &ui_font_size_scaler) ||
166 dpi_scale != 1) {
167 AdjustLogFont(ui_font_family, ui_font_size_scaler, dpi_scale, logfont);
171 void AdjustUIFontForWindow(HWND hwnd) {
172 string16 ui_font_family;
173 double ui_font_size_scaler;
174 if (NeedOverrideDefaultUIFont(&ui_font_family, &ui_font_size_scaler)) {
175 LOGFONT logfont;
176 if (GetObject(GetWindowFont(hwnd), sizeof(logfont), &logfont)) {
177 double dpi_scale = 1;
178 AdjustLogFont(ui_font_family, ui_font_size_scaler, dpi_scale, &logfont);
179 HFONT hfont = CreateFontIndirect(&logfont);
180 if (hfont)
181 SetWindowFont(hwnd, hfont, FALSE);
186 void OverrideLocaleWithUILanguageList() {
187 std::vector<std::wstring> ui_languages;
188 if (base::win::i18n::GetThreadPreferredUILanguageList(&ui_languages)) {
189 std::vector<std::string> ascii_languages;
190 ascii_languages.reserve(ui_languages.size());
191 std::transform(ui_languages.begin(), ui_languages.end(),
192 std::back_inserter(ascii_languages), &WideToASCII);
193 override_locale_holder.Get().swap_value(&ascii_languages);
194 } else {
195 NOTREACHED() << "Failed to determine the UI language for locale override.";
199 const std::vector<std::string>& GetLocaleOverrides() {
200 return override_locale_holder.Get().value();
203 } // namespace l10n_util