Skip to content

Commit

Permalink
Implement application font fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsu-syo committed Nov 12, 2023
1 parent d126cfe commit cf8bfc7
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 14 deletions.
96 changes: 96 additions & 0 deletions program/country/default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <windows.h>
#include "default.h"

namespace default {

/**
* @brief 必要とされる文字列
*/
tstring _neededString;

/**
* Charsetに対するEnumFontFamiliesExのコールバック
*
* @param lpelfe 論理的なフォントデータ
* @param lpntme 物理的なフォントデータ
* @param FontType フォントの種類
* @param lParam アプリケーション定義のデータ
* @return 0:列挙を中止する 1:次のフォントを列挙する。
*/
int CALLBACK EnumFontCharsetProc(
ENUMLOGFONTEX *lpelfe, // 論理的なフォントデータ
NEWTEXTMETRICEX *lpntme, // 物理的なフォントデータ
DWORD FontType, // フォントの種類
LPARAM lParam // アプリケーション定義のデータ
)
{
if (_neededString == lpelfe->elfFullName) {
return 0;
}

return 1;

}

/**
* Charsetに対するフォントを取得する
*
* @return 0:フォントが存在する 1:フォントは存在しない
*/
int findCharsetFont()
{
LOGFONT lf;

lf.lfPitchAndFamily = 0;
lf.lfCharSet = DEFAULT_CHARSET;

HDC hDC;
hDC = GetDC(GetDesktopWindow());

int found = 1;
found = ::EnumFontFamiliesEx(
hDC,
&lf,
(FONTENUMPROC)EnumFontCharsetProc,
(LPARAM)0,
0
);

return found;
}

/**
* Write fallback font when font specified in language file is not found.
* For example in Japanese, use Yu Gothic -> MS UI Gothic
* Use this routine, unfortunately system font selection is too ugly such as Japan.
* If you want to use language and Windows version specific fallback, copy this source for your country
* and modify font name in language specific font name which is installed all Windows in your country.
*
* @brief Determine exist font for default languages.
* @param settingString Preferred font name
* @return Choiced font name
*/
tstring getDefaultFontFallback(tstring &settingString)
{
int result = 1;

_neededString = settingString;

result = findCharsetFont();
if (result == 0) {
return settingString;
}

_neededString = _T("MS Shell Dlg");
result = findCharsetFont();
if (result == 0) {
return _neededString;
}

_neededString = _T("Arial");

return _neededString;

}

}
16 changes: 16 additions & 0 deletions program/country/default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
noMeiryoUI (C) 2005,2012-2023 Tatsuhiko Shoji
The sources for noMeiryoUI are distributed under the MIT open source license
*/

#pragma once

#include <string.h>
#include <tchar.h>
#include <tstring.h>

namespace default {

extern tstring getDefaultFontFallback(tstring &settingString);

};
57 changes: 44 additions & 13 deletions program/noMeiryoUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ The sources for noMeiryoUI are distributed under the MIT open source license
#include "NCFileDialog.h"
#include "util.h"

// #include "country\japan.h"
#include "country\default.h"
#include "country\japan.h"
#include "country\korea.h"

//
Expand Down Expand Up @@ -95,6 +96,7 @@ void initializeLocale(void)
if (!readResult) {
has11Preset = false;
}

}

/**
Expand Down Expand Up @@ -137,12 +139,7 @@ void setResourceFileName(TCHAR * langFileName, TCHAR * helpFileName, char*system
}

// Language detection
if (wcsstr(langWork, L"ja-JP") != NULL) {
runningCountry = Japan;
}
if (wcsstr(langWork, L"ko-KR") != NULL) {
runningCountry = Korea;
}
setLocationInternalCode(langWork);

_tcscpy(findPath, iniPath);
TCHAR *p = _tcsrchr(langWork, _T('.'));
Expand Down Expand Up @@ -188,6 +185,23 @@ void setResourceFileName(TCHAR * langFileName, TCHAR * helpFileName, char*system
}
}

/**
* @brief Set internal lanuguage enumeration
* @param langWork Locale name
*/
void setLocationInternalCode(TCHAR langWork[85])
{
if (wcsstr(langWork, L"ja-JP") != NULL) {
runningCountry = Japan;
return;
}
if (wcsstr(langWork, L"ko-KR") != NULL) {
runningCountry = Korea;
return;
}
runningCountry = NoCountry;
}

/**
* ダイアログプローシジャ
*
Expand Down Expand Up @@ -818,12 +832,9 @@ void NoMeiryoUI::applyResource()

tstring font = langResource[0];

#if 0
// Under construction
if (runningCountry == Japan) {
font = japan::getJapaneseFontFallback(langResource[0]);
}
#endif
// Get fallback font
font = getLanguageFallbackForCountry(langResource[0]);


HFONT displayFont = CreateFont(
-MulDiv(APP_FONTSIZE, GetDeviceCaps(hDC, LOGPIXELSY), 72),
Expand Down Expand Up @@ -919,6 +930,26 @@ void NoMeiryoUI::applyResource()
//DeleteObject(newFont);
}

/**
* @brief Get fallback font when font settings in language file didn't exist in Windows(For example older Windows)
* @param settingString Application display font name in language file.
* @return Font name adjusted by font installed in Windows.
*/
tstring NoMeiryoUI::getLanguageFallbackForCountry(tstring &settingString)
{
tstring font;

switch (runningCountry) {
case Japan:
font = japan::getJapaneseFontFallback(langResource[0]);
break;
default:
font = default::getDefaultFontFallback(langResource[0]);
break;
}

return font;
}

/**
* フォント表示を更新する。
Expand Down
3 changes: 3 additions & 0 deletions program/noMeiryoUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ BOOL CALLBACK MonitorNearWindowCallback(
BOOL CALLBACK setWindowSize(HWND hWnd, LPARAM lparam);
void setResourceFileName(TCHAR* langFileName, TCHAR* helpFileName, char* localeName, TCHAR *iniPath);

void setLocationInternalCode(TCHAR langWork[85]);

/** フォントのポイント数を保存する構造体 */
struct FontPoints {
/** タイトルバーフォントサイズ(Point) */
Expand Down Expand Up @@ -134,6 +136,7 @@ class NoMeiryoUI : public DialogAppliBase {
void getOption(TCHAR *lpCmdLine);
void parseOption(TCHAR *param, int argCount);
void applyResource();
tstring getLanguageFallbackForCountry(tstring &settingString);
void showVersion(void);
void adjustWindowSize(void);

Expand Down
2 changes: 2 additions & 0 deletions program/noMeiryoUI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="country\default.cpp" />
<ClCompile Include="country\japan.cpp" />
<ClCompile Include="country\korea.cpp" />
<ClCompile Include="FontSel.cpp" />
Expand All @@ -206,6 +207,7 @@
<ClCompile Include="util.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="country\default.h" />
<ClInclude Include="country\japan.h" />
<ClInclude Include="country\korea.h" />
<ClInclude Include="FontSel.h" />
Expand Down
6 changes: 6 additions & 0 deletions program/noMeiryoUI.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<ClCompile Include="country\japan.cpp">
<Filter>ソース ファイル\country</Filter>
</ClCompile>
<ClCompile Include="country\default.cpp">
<Filter>ソース ファイル\country</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="FontSel.h">
Expand Down Expand Up @@ -140,6 +143,9 @@
<ClInclude Include="country\japan.h">
<Filter>ヘッダー ファイル\country</Filter>
</ClInclude>
<ClInclude Include="country\default.h">
<Filter>ヘッダー ファイル\country</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="noMeiryoUI.ico">
Expand Down
2 changes: 1 addition & 1 deletion program/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ void readResourceFile(TCHAR *file);
int readFontResource8(TCHAR *file);
int readFontResource10(TCHAR *file);
int readFontResource11(TCHAR* file);
void setLocationInternalCode(TCHAR langWork[85]);
void adjustCenter(RECT parentRect, HWND parentHWnd, HWND myHWnd);
void getKoreanFontName(TCHAR *dispBuf);
void strreplace(TCHAR* buf, const TCHAR* source, const TCHAR* oldWord, const TCHAR* newWord, int bufLen);
BOOL isWin11OrLater(DWORD& buildNumber);
DWORD GetVersionForApp(DWORD &majorVersion, DWORD &minorVersion, DWORD& buildNumber);
Expand Down

0 comments on commit cf8bfc7

Please sign in to comment.