Skip to content

Commit

Permalink
Merge pull request #101 from Tatsu-syo/tatsu_20231112_font
Browse files Browse the repository at this point in the history
Add application font fallback.
  • Loading branch information
Tatsu-syo authored Feb 3, 2024
2 parents 1559006 + d40b224 commit f20d3d2
Show file tree
Hide file tree
Showing 18 changed files with 840 additions and 382 deletions.
4 changes: 2 additions & 2 deletions lang/ja-JP.lng
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
; noMeiryoUI (C) 2005,2012-2022 Tatsuhiko Shoji
; noMeiryoUI (C) 2005,2012-2023 Tatsuhiko Shoji
; The sources for noMeiryoUI are distributed under the MIT open source license

;In RESOURCE section,
;FONT_FACE key is dialog font name, other key is message.
[RESOURCE]
FONT_FACE=MS Pゴシック
FONT_FACE=BIZ UDPゴシック
FONT_CHARSET=1
TITLE=Meiryo UIも大っきらい!!
MENU_FILE=ファイル(&F)
Expand Down
48 changes: 34 additions & 14 deletions program/FontSel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The sources for noMeiryoUI are distributed under the MIT open source license
*/
#include "FontSel.h"
#include "util.h"
#include "langresource.h"

#include <algorithm>
#include <functional>
Expand Down Expand Up @@ -258,7 +259,7 @@ int getFont()
/**
* コンストラクタ
*/
FontSel::FontSel(HWND parent, int resource) : BaseDialog(parent, resource)
FontSel::FontSel(HWND parent, int resource, bool fontOnly) : BaseDialog(parent, resource)
{
displayFont = NULL;
m_fontNameList = NULL;
Expand All @@ -271,6 +272,7 @@ FontSel::FontSel(HWND parent, int resource) : BaseDialog(parent, resource)
m_strike = NULL;
previousFont = NULL;
m_point = 0;
m_fontonly = fontOnly;
}

/**
Expand Down Expand Up @@ -407,12 +409,34 @@ INT_PTR FontSel::OnInitDialog()
}
}

if (m_fontonly) {
disableWithoutFontselect();
}

applyResource();
adjustPosition();

return (INT_PTR)FALSE;
}

/**
* @brief フォント選択以外を無効化する。
*
*/
void FontSel::disableWithoutFontselect(void)
{
m_fontSizeList->EnableWindow(FALSE);
m_ChersetList->EnableWindow(FALSE);
m_styleList->EnableWindow(FALSE);
m_bold->EnableWindow(FALSE);
m_italic->EnableWindow(FALSE);
m_underline->EnableWindow(FALSE);
m_strike->EnableWindow(FALSE);
setChildEnabled(IDC_STATIC_STYLE, FALSE);
setChildEnabled(IDC_STATIC_SIZE, FALSE);
setChildEnabled(IDC_STATIC_CHARSET, FALSE);
}

/**
* ウインドウ位置を親ウインドウの中央に調整する。
*/
Expand Down Expand Up @@ -465,10 +489,6 @@ void FontSel::applyResource()
HDC hDC = GetDC(this->hWnd);
tstring font = langResource[0];

if (runningCountry == Japan) {
font = japan::getJapaneseFontFallback(langResource[0]);
}

displayFont = CreateFont(
-MulDiv(APP_FONTSIZE, GetDeviceCaps(hDC, LOGPIXELSY), 72),
0,
Expand All @@ -478,7 +498,7 @@ void FontSel::applyResource()
FALSE,
FALSE,
FALSE,
_tstoi(langResource[70].c_str()),
_tstoi(langResource[0].c_str()),
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, // CLEARTYPE_QUALITY,
Expand All @@ -491,27 +511,27 @@ void FontSel::applyResource()
// アプリタイトル
setText(langResource[27].c_str());

setChildText(IDC_STATIC_NAME, langResource[28].c_str());
setChildText(IDC_STATIC_NAME, langResource[DLG_FONT_NAME].c_str());
setChildFont(IDC_STATIC_NAME, displayFont);
setChildText(IDC_STATIC_STYLE, langResource[29].c_str());
setChildText(IDC_STATIC_STYLE, langResource[DLG_STYLE].c_str());
setChildFont(IDC_STATIC_STYLE, displayFont);
setChildText(IDC_STATIC_SIZE, langResource[30].c_str());
setChildText(IDC_STATIC_SIZE, langResource[DLG_SIZE].c_str());
setChildFont(IDC_STATIC_SIZE, displayFont);

setChildText(IDC_CHECK_BOLD, langResource[DLG_CHECK_BOLD].c_str());
setChildFont(IDC_CHECK_BOLD, displayFont);
setChildText(IDC_CHECK_ITALIC, langResource[DLG_CHECK_ITALIC].c_str());
setChildFont(IDC_CHECK_ITALIC, displayFont);
setChildText(IDC_CHECK_UNDERLINE, langResource[31].c_str());
setChildText(IDC_CHECK_UNDERLINE, langResource[DLG_UNDERLINE].c_str());
setChildFont(IDC_CHECK_UNDERLINE, displayFont);
setChildText(IDC_CHECK_STRIKE, langResource[32].c_str());
setChildText(IDC_CHECK_STRIKE, langResource[DLG_STRIKE].c_str());
setChildFont(IDC_CHECK_STRIKE, displayFont);
setChildText(IDC_STATIC_CHARSET, langResource[33].c_str());
setChildText(IDC_STATIC_CHARSET, langResource[DLG_CHARSET].c_str());
setChildFont(IDC_STATIC_CHARSET, displayFont);

setChildText(IDOK, langResource[34].c_str());
setChildText(IDOK, langResource[DLG_OK].c_str());
setChildFont(IDOK, displayFont);
setChildText(IDCANCEL, langResource[35].c_str());
setChildText(IDCANCEL, langResource[DLG_CANCEL].c_str());
setChildFont(IDCANCEL, displayFont);

setChildFont(IDC_COMBO_NAME, displayFont);
Expand Down
4 changes: 3 additions & 1 deletion program/FontSel.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,21 @@ class FontSel :
LOGFONT selectedFont;
LOGFONT *previousFont;
double m_point;
bool m_fontonly;

void adjustPosition(void);
void applyResource();
void setCharset(void);
void setStyle();
INT_PTR onOK(void);
void disableWithoutFontselect(void);

protected:
INT_PTR OnInitDialog();
INT_PTR OnCommand(WPARAM wParam);

public:
FontSel(HWND parent, int resource);
FontSel(HWND parent, int resource, bool fontOnly = false);
virtual ~FontSel(void);

LOGFONT getSelectedFont() {
Expand Down
15 changes: 15 additions & 0 deletions program/TWR/BaseDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,18 @@ void BaseDialog::setChildFont(int id, HFONT font)
target = ::GetDlgItem(hWnd, id);
SendMessage(target, WM_SETFONT, (WPARAM)font, MAKELPARAM(TRUE, 0));
}

/**
* 子項目が有効かどうか設定する
*
* @param id 子項目のID
* @param enabled TRUE:有効 FALSE:無効
*/
void BaseDialog::setChildEnabled(int id, BOOL enabled)
{
HWND target;

target = ::GetDlgItem(hWnd, id);
::EnableWindow(target, enabled);
}

1 change: 1 addition & 0 deletions program/TWR/BaseDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class BaseDialog : public TwrWnd {
HWND GetDlgItemRaw(int item);
void setChildText(int id, const TCHAR *message);
void setChildFont(int id, HFONT font);
void setChildEnabled(int id, BOOL enabled);

virtual void UpdateData(bool toObj);

Expand Down
91 changes: 91 additions & 0 deletions program/country/default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#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;

memset(&lf, 0, sizeof(LOGFONT));
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("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);

};
18 changes: 18 additions & 0 deletions program/country/japan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace japan {
{
LOGFONT lf;

memset(&lf, 0, sizeof(LOGFONT));
lf.lfPitchAndFamily = 0;
lf.lfCharSet = DEFAULT_CHARSET;

Expand All @@ -59,23 +60,40 @@ namespace japan {
return found;
}

/**
* @brief 日本語版の使用するフォールバックフォントを取得する。
* @param settingString 言語ファイルのフォント名
* @return 選択されたフォント名
*/
tstring getJapaneseFontFallback(tstring &settingString)
{
int result = 1;

_neededString = settingString;

// Font in language file is found in your Windows?
result = findCharsetFont();
if (result == 0) {
return settingString;
}

// In early Windows 10
_neededString = _T("游ゴシック Medium");
result = findCharsetFont();
if (result == 0) {
return _neededString;
}

// In Windows 8.1
_neededString = _T("游ゴシック");
result = findCharsetFont();
if (result == 0) {
return _neededString;
}

// なければオーソドックスなMS UI Gothic
// メイリオは誤解を生むのであえて使わない。
// メイリオとMeiryo UIはいろいろな意味で別物なのに混同されることがあるので。
_neededString = _T("MS UI Gothic");

return _neededString;
Expand Down
Loading

0 comments on commit f20d3d2

Please sign in to comment.