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