1 // Copyright (c) 2010 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 "app/l10n_util.h"
10 #include "app/l10n_util_win.h"
11 #include "base/i18n/rtl.h"
12 #include "base/string_number_conversions.h"
13 //#include "base/string_util.h"
14 #include "base/win_util.h"
16 #include "grit/app_locale_settings.h"
20 void AdjustLogFont(const std::wstring
& font_family
,
21 double font_size_scaler
,
23 DCHECK(font_size_scaler
> 0);
24 font_size_scaler
= std::max(std::min(font_size_scaler
, 2.0), 0.7);
25 logfont
->lfHeight
= static_cast<long>(font_size_scaler
*
26 static_cast<double>(abs(logfont
->lfHeight
)) + 0.5) *
27 (logfont
->lfHeight
> 0 ? 1 : -1);
29 // TODO(jungshik): We may want to check the existence of the font.
30 // If it's not installed, we shouldn't adjust the font.
31 if (font_family
!= L
"default") {
32 int name_len
= std::min(static_cast<int>(font_family
.size()),
34 memcpy(logfont
->lfFaceName
, font_family
.data(), name_len
* sizeof(WORD
));
35 logfont
->lfFaceName
[name_len
] = 0;
39 bool IsFontPresent(const wchar_t* font_name
) {
40 HFONT hfont
= CreateFont(12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 HGDIOBJ oldFont
= static_cast<HFONT
>(SelectObject(dc
, hfont
));
46 WCHAR actual_font_name
[LF_FACESIZE
];
47 DWORD size_ret
= GetTextFace(dc
, LF_FACESIZE
, actual_font_name
);
48 actual_font_name
[LF_FACESIZE
- 1] = 0;
49 SelectObject(dc
, oldFont
);
52 // We don't have to worry about East Asian fonts with locale-dependent
54 return wcscmp(font_name
, actual_font_name
) == 0;
61 int GetExtendedStyles() {
62 return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL
| WS_EX_RTLREADING
;
65 int GetExtendedTooltipStyles() {
66 return !base::i18n::IsRTL() ? 0 : WS_EX_LAYOUTRTL
;
69 void HWNDSetRTLLayout(HWND hwnd
) {
70 DWORD ex_style
= ::GetWindowLong(hwnd
, GWL_EXSTYLE
);
72 // We don't have to do anything if the style is already set for the HWND.
73 if (!(ex_style
& WS_EX_LAYOUTRTL
)) {
74 ex_style
|= WS_EX_LAYOUTRTL
;
75 ::SetWindowLong(hwnd
, GWL_EXSTYLE
, ex_style
);
77 // Right-to-left layout changes are not applied to the window immediately
78 // so we should make sure a WM_PAINT is sent to the window by invalidating
79 // the entire window rect.
80 ::InvalidateRect(hwnd
, NULL
, true);
84 bool IsLocaleSupportedByOS(const std::string
& locale
) {
85 // Block Amharic on Windows XP unless 'Abyssinica SIL' font is present.
86 // On Win XP, no Ethiopic/Amahric font is availabel out of box. We hard-coded
87 // 'Abyssinica SIL' in the resource bundle to use in the UI. Check
88 // for its presence to determine whether or not to support Amharic UI on XP.
89 return (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA
||
90 !LowerCaseEqualsASCII(locale
, "am") || IsFontPresent(L
"Abyssinica SIL"));
93 bool NeedOverrideDefaultUIFont(std::wstring
* override_font_family
,
94 double* font_size_scaler
) {
95 // This is rather simple-minded to deal with the UI font size
96 // issue for some Indian locales (ml, bn, hi) for which
97 // the default Windows fonts are too small to be legible. For those
98 // locales, IDS_UI_FONT_FAMILY is set to an actual font family to
99 // use while for other locales, it's set to 'default'.
101 // XP and Vista or later have different font size issues and
102 // we need separate ui font specifications.
103 int ui_font_family_id
= IDS_UI_FONT_FAMILY
;
104 int ui_font_size_scaler_id
= IDS_UI_FONT_SIZE_SCALER
;
105 if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA
) {
106 ui_font_family_id
= IDS_UI_FONT_FAMILY_XP
;
107 ui_font_size_scaler_id
= IDS_UI_FONT_SIZE_SCALER_XP
;
110 std::wstring ui_font_family
= GetString(ui_font_family_id
);
112 if (!base::StringToInt(l10n_util::GetString(ui_font_size_scaler_id
),
116 // We use the OS default in two cases:
117 // 1) The resource bundle has 'default' and '100' for font family and
119 // 2) The resource bundle is not available for some reason and
120 // ui_font_family is empty.
121 if (ui_font_family
== L
"default" && scaler100
== 100 ||
122 ui_font_family
.empty())
124 if (override_font_family
&& font_size_scaler
) {
125 override_font_family
->swap(ui_font_family
);
126 *font_size_scaler
= scaler100
/ 100.0;
131 void AdjustUIFont(LOGFONT
* logfont
) {
132 std::wstring ui_font_family
;
133 double ui_font_size_scaler
;
134 if (NeedOverrideDefaultUIFont(&ui_font_family
, &ui_font_size_scaler
))
135 AdjustLogFont(ui_font_family
, ui_font_size_scaler
, logfont
);
138 void AdjustUIFontForWindow(HWND hwnd
) {
139 std::wstring ui_font_family
;
140 double ui_font_size_scaler
;
141 if (NeedOverrideDefaultUIFont(&ui_font_family
, &ui_font_size_scaler
)) {
143 if (GetObject(GetWindowFont(hwnd
), sizeof(logfont
), &logfont
)) {
144 AdjustLogFont(ui_font_family
, ui_font_size_scaler
, &logfont
);
145 HFONT hfont
= CreateFontIndirect(&logfont
);
147 SetWindowFont(hwnd
, hfont
, FALSE
);
152 } // namespace l10n_util