Skip to content

Commit

Permalink
マウスホイールでのフォントサイズ変更をパーセンテージに基づいて行うようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
suconbu committed Jan 23, 2021
1 parent 6975209 commit 717ce6c
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 30 deletions.
2 changes: 2 additions & 0 deletions sakura/sakura.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@
<ClInclude Include="..\sakura_core\util\tchar_printf.h" />
<ClInclude Include="..\sakura_core\util\tchar_template.h" />
<ClInclude Include="..\sakura_core\util\window.h" />
<ClInclude Include="..\sakura_core\util\zoom.h" />
<ClInclude Include="..\sakura_core\version.h" />
<ClInclude Include="..\sakura_core\view\CCaret.h" />
<ClInclude Include="..\sakura_core\view\CEditView.h" />
Expand Down Expand Up @@ -921,6 +922,7 @@
<ClCompile Include="..\sakura_core\util\tchar_printf.cpp" />
<ClCompile Include="..\sakura_core\util\tchar_template.cpp" />
<ClCompile Include="..\sakura_core\util\window.cpp" />
<ClCompile Include="..\sakura_core\util\zoom.cpp" />
<ClCompile Include="..\sakura_core\view\CCaret.cpp" />
<ClCompile Include="..\sakura_core\view\CEditView.cpp" />
<ClCompile Include="..\sakura_core\view\CEditView_Cmdgrep.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions sakura/sakura.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,9 @@
<ClInclude Include="..\sakura_core\GrepInfo.h">
<Filter>Cpp Source Files</Filter>
</ClInclude>
<ClInclude Include="..\sakura_core\util\zoom.h">
<Filter>Cpp Source Files\util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\resource\auto_scroll_center.cur">
Expand Down Expand Up @@ -2279,6 +2282,9 @@
<ClCompile Include="..\sakura_core\GrepInfo.cpp">
<Filter>Cpp Source Files</Filter>
</ClCompile>
<ClCompile Include="..\sakura_core\util\zoom.cpp">
<Filter>Cpp Source Files\util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="..\resource\auto_scroll_center.bmp">
Expand Down
5 changes: 3 additions & 2 deletions sakura_core/String_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@
#define STR_MENU_KEYWORDINFO 34340
#define STR_MENU_OPENKEYWORDDIC 34341
#define STR_STATUS_ROW_COL 34342
#define STR_STATUS_FONTSIZE 35043
#define STR_STATUS_FONTZOOM_0 35043
#define STR_STATUS_FONTZOOM_1 35044
#define STR_INS_MODE_INS 34343
#define STR_INS_MODE_OVR 34344
#define STR_GREP_SEARCH_CONDITION 34345
Expand Down Expand Up @@ -1300,4 +1301,4 @@
#define STR_FILEDIALOG_MRU 35040
#define STR_FILEDIALOG_OPENFOLDER 35041

// Now using max number 35043 by STR_STATUS_FONTSIZE
// Now using max number 35044 by STR_STATUS_FONTZOOM_1
63 changes: 40 additions & 23 deletions sakura_core/cmd/CViewCommander_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "util/shell.h"
#include "CPropertyManager.h"
#include "util/window.h"
#include "util/zoom.h"


/*! ツールバーの表示/非表示
Expand Down Expand Up @@ -251,18 +253,31 @@ void CViewCommander::Command_FONT( void )
/*! フォントサイズ設定
@param fontSize フォントサイズ(1/10ポイント単位)
@param shift フォントサイズを拡大or縮小するための変更量(fontSize=0のとき有効)
@param mode フォントサイズ設定対象(0:フォント設定 1:タイプ別設定(なければ0と同じ) 2:一時適用フォント)
@note TrueTypeのみサポート
@date 2013.04.10 novice 新規作成
*/
void CViewCommander::Command_SETFONTSIZE( int fontSize, int shift, int mode )
{
// The point sizes recommended by "The Windows Interface: An Application Design Guide", 1/10ポイント単位
static const INT sizeTable[] = { 8*10, 9*10, 10*10, (INT)(10.5*10), 11*10, 12*10, 14*10, 16*10, 18*10, 20*10, 22*10, 24*10, 26*10, 28*10, 36*10, 48*10, 72*10 };
// 設定できるフォントサイズの下限/上限/最小単位(いずれも1/10ポイント単位)
const int nPointSizeMin = 10;
const int nPointSizeMax = 720;
const int nPointSizeUnit = 5;
// ズーム設定
static const ZoomSetting zoomSetting(
{
0.01, 0.011, 0.0125, 0.015, 0.0175, 0.02, 0.0225, 0.025, 0.0275, 0.03, 0.035, 0.04, 0.045, 0.05, 0.06, 0.07, 0.08, 0.09,
0.1, 0.11, 0.125, 0.15, 0.175, 0.2, 0.225, 0.25, 0.275, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9,
1.0, 1.1, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.5, 4.0, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0,
10.0, 11.0, 12.5, 15.0, 17.5, 20.0, 22.5, 25.0, 27.5, 30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 70.0, 80.0, 90
},
nPointSizeMin,
nPointSizeMax,
nPointSizeUnit );
const LOGFONT& lf = (mode == 0 ? GetDllShareData().m_Common.m_sView.m_lf
: GetEditWindow()->GetLogfont( mode == 2 ));
INT nPointSize;

// TrueTypeのみ対応
if( OUT_STROKE_PRECIS != lf.lfOutPrecision && OUT_PS_ONLY_PRECIS != lf.lfOutPrecision ) {
Expand All @@ -273,32 +288,33 @@ void CViewCommander::Command_SETFONTSIZE( int fontSize, int shift, int mode )
return;
}

const int nCurrentPointSize = (mode == 0) ?
GetDllShareData().m_Common.m_sView.m_nPointSize :
GetEditWindow()->GetFontPointSize( mode == 2 );
const int nOriginalPointSize = GetEditWindow()->GetFontPointSize( false );
double nCurrentZoom = (mode == 2 && GetDocument()->m_blfCurTemp) ? GetDocument()->m_nCurrentZoom : 1.0;
int nPointSize = nCurrentPointSize;

if( 0 != fontSize ){
// フォントサイズを直接選択する場合
nPointSize = t_max(sizeTable[0], t_min(sizeTable[_countof(sizeTable)-1], fontSize));
} else if( 0 != shift ) {
nPointSize = std::clamp( fontSize, nPointSizeMin, nPointSizeMax );
}else if( 0 != shift ){
// 現在のフォントに対して、縮小or拡大したフォント選択する場合
nPointSize = (mode == 0 ? GetDllShareData().m_Common.m_sView.m_nPointSize
: GetEditWindow()->GetFontPointSize( mode == 2 ));

// フォントの拡大or縮小するためのサイズ検索
int i;
for( i = 0; i < _countof(sizeTable); i++) {
if( nPointSize <= sizeTable[i] ){
int index = t_max(0, t_min((int)_countof(sizeTable) - 1, (int)(i + shift)));
int nNewPointSize = sizeTable[index];
// フォントサイズが変わらないので終了
if (nPointSize == nNewPointSize) {
return;
}
nPointSize = nNewPointSize;
break;
}
double nPointSizeF = 0.0;
if( !GetZoomedValue( zoomSetting, nOriginalPointSize, nCurrentZoom, shift, &nPointSizeF, &nCurrentZoom ) ){
return;
}
nPointSize = (int)nPointSizeF;

// フォントサイズが変わらないなら終了
if( nPointSize == nCurrentPointSize ){
return;
}
} else {
}else{
// フォントサイズが変わらないので終了
return;
}

// 新しいフォントサイズ設定
int lfHeight = DpiPointsToPixels(-nPointSize, 10);
int nTypeIndex = -1;
Expand All @@ -324,7 +340,8 @@ void CViewCommander::Command_SETFONTSIZE( int fontSize, int shift, int mode )
GetDocument()->m_lfCur = lf;
GetDocument()->m_lfCur.lfHeight = lfHeight;
GetDocument()->m_nPointSizeCur = nPointSize;
GetDocument()->m_nPointSizeOrg = GetEditWindow()->GetFontPointSize(false);
GetDocument()->m_nPointSizeOrg = nOriginalPointSize;
GetDocument()->m_nCurrentZoom = nCurrentZoom;
}

HWND hwndFrame;
Expand Down
1 change: 1 addition & 0 deletions sakura_core/doc/CEditDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class CEditDoc
int m_nPointSizeCur; // 一時設定フォントサイズ
bool m_blfCurTemp; // フォント設定適用中
int m_nPointSizeOrg; // 元のフォントサイズ
double m_nCurrentZoom; // 一時設定フォントのズーム倍率
bool m_bTabSpaceCurTemp; // タブ幅一時設定適用中 // 2013.05.30 Moca

HBITMAP m_hBackImg;
Expand Down
3 changes: 2 additions & 1 deletion sakura_core/sakura_rc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -3409,7 +3409,8 @@ BEGIN
STR_ERR_DLGEDITVW6 "■"
STR_MENU_KEYWORDINFO "キーワードの説明をクリップボードにコピー"
STR_MENU_OPENKEYWORDDIC "キーワード辞書を開く"
STR_STATUS_FONTSIZE "%4d %%"
STR_STATUS_FONTZOOM_0 "%4.0f %%"
STR_STATUS_FONTZOOM_1 "%4.1f %%"
STR_STATUS_ROW_COL "%5d 行 %4d 桁"
STR_INS_MODE_INS "挿入"
STR_INS_MODE_OVR "上書"
Expand Down
193 changes: 193 additions & 0 deletions sakura_core/util/zoom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*! @file
@brief ズーム倍率算出
*/
/*
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/

#include "StdAfx.h"
#include "zoom.h"
#include <algorithm>
#include <cmath>

/*!
@brief コンストラクタ
@param[in] iZoomFactors ズーム倍率の並び
@param[in] nValueMin 下限値
@param[in] nValueMax 上限値
@param[in] nValueUnit 値の最小単位
*/
ZoomSetting::ZoomSetting( std::initializer_list<double> iZoomFactors, double nValueMin, double nValueMax, double nValueUnit )
{
m_vZoomFactors.assign( iZoomFactors );
m_nValueMin = nValueMin;
m_nValueMax = nValueMax;
m_nValueUnit = nValueUnit;

// 正当性確認
m_bValid = (0 < m_vZoomFactors.size()) && (m_nValueMin <= m_nValueMax) && (0.0 <= m_nValueUnit);
if( m_bValid ){
for( size_t i = 1; i < m_vZoomFactors.size(); ++i ){
if( m_vZoomFactors[i] < m_vZoomFactors[i - 1] ){
m_bValid = false;
break;
}
}
}
}

/*!
@brief 値テーブル上における指定値の位置を取得
@param[in] vTable 値テーブル
@param[in] nValue 指定値
@return 位置
@note 「位置」は基本的にはテーブルのインデックスに相当しますが、
テーブル範囲外およびテーブル上の隣り合う二値間については 0.5 で表現します。
@note 例として vTable:{10, 20, 30} の場合の nValue と戻り値との関係を示します。
nValue | 0 | 5 | 10 | 15 | 25 | 30 | 35 | 40 |
-------|------|------|------|------|------|------|------|------|
戻り値 | -0.5 | -0.5 | 0.0 | 0.5 | 1.5 | 2.0 | 2.5 | 2.5 |
*/
static double GetPositionInTable( const std::vector<double>& vTable, double nValue )
{
double nPosition = (double)vTable.size() - 0.5;
for( size_t i = 0; i < vTable.size(); ++i ){
if( nValue <= vTable[i] ){
if( nValue == vTable[i] ){
nPosition = (double)i;
}else{
nPosition = (double)i - 0.5;
}
break;
}
}
return nPosition;
}

/*!
@brief 最小単位で丸めた値を取得
@param[in] nValue 対象値
@param[in] nUnit 最小単位(0.0 の場合は丸めなし)
@return 丸められた値
*/
static double GetQuantizedValue( double nValue, double nUnit )
{
return (0.0 < nUnit) ? std::floor( nValue / nUnit ) * nUnit : nValue;
}

/*!
@brief 基準値に対してズーム倍率を適用した値を取得
@param[in] nValue 対象値
@param[in] nUnit 最小単位(0.0 の場合は丸めなし)
@return 丸められた値
*/
bool GetZoomedValue( const ZoomSetting& zoomSetting, double nBaseValue, double nCurrentZoom, int nSteps, double* pnValueOut, double* pnZoomOut )
{
if( !zoomSetting.IsValid() || nSteps == 0 ){
return false;
}

const auto& vZoomFactors = zoomSetting.GetZoomFactors();
const bool bZoomUp = (0 < nSteps);
const int nTableIndexMin = 0;
const int nTableIndexMax = (int)vZoomFactors.size() - 1;

const double nPosition = GetPositionInTable( vZoomFactors, nCurrentZoom );
int nTableIndex = (int)(bZoomUp ? std::floor( nPosition ) : std::ceil( nPosition ));
if( (!bZoomUp && nTableIndex <= nTableIndexMin) || (bZoomUp && nTableIndexMax <= nTableIndex) ){
// 現在の倍率がすでに倍率テーブルの範囲外でかつ
// さらに外側へ移動しようとした場合は今の位置を維持
return false;
}

const double nCurrentValue = GetQuantizedValue( nBaseValue * nCurrentZoom, zoomSetting.GetValueUnit() );
const double nValueMin = std::min( {zoomSetting.GetValueMin(), nBaseValue, nCurrentValue} );
const double nValueMax = std::max( {zoomSetting.GetValueMax(), nBaseValue, nCurrentValue} );
double nNextValue = nCurrentValue;
double nNextZoom = nCurrentZoom;
double nLastValue = nCurrentValue;
double nLastZoom = nCurrentZoom;
bool bFindingOneMoreChange = false;

// 最小単位で丸めた後の値が変更前の値から変わらなければ
// 変わる位置までインデックスを動かしていく
nTableIndex += nSteps;
// 本当は while( true ) で良いが万一の暴走回避のため有限回
for( size_t i = 0; i < vZoomFactors.size(); ++i ){
int clampedIndex = std::clamp( nTableIndex, nTableIndexMin, nTableIndexMax );
nNextZoom = vZoomFactors[clampedIndex];
nNextValue = GetQuantizedValue( nBaseValue * nNextZoom, zoomSetting.GetValueUnit() );

if( bFindingOneMoreChange ){
if( nNextValue != nLastValue || clampedIndex != nTableIndex ){
nNextValue = nLastValue;
nNextZoom = nLastZoom;
break;
}
}else{
// 値の上下限を超過したら上下限に丸めて終わる
// 倍率は丸めた後のサイズで再計算
double clampedValue = std::clamp( nNextValue, nValueMin, nValueMax );
if( nNextValue != clampedValue ){
if( nBaseValue != 0.0 ){
nNextZoom = clampedValue / nBaseValue;
}
nNextValue = clampedValue;
break;
}

// 倍率テーブルの端まで到達していたら終わり
if( clampedIndex != nTableIndex ){
break;
}

bool bSizeChanged = (nNextValue != nCurrentValue);
if( bZoomUp ){
// 拡大側は値が変わったらすぐ終わる
if( bSizeChanged ){
break;
}
}else{
// 縮小側は一度値が変わった後もう一度値が変わる位置までインデックスを進めてから終わる
if( bSizeChanged ){
bFindingOneMoreChange = true;
}
}
}

nLastValue = nNextValue;
nLastZoom = nNextZoom;
nTableIndex += bZoomUp ? 1 : -1;
};

if( nCurrentValue == nNextValue ){
return false;
}

if( pnValueOut != NULL ){
*pnValueOut = nNextValue;
}
if( pnZoomOut != NULL ){
*pnZoomOut = nNextZoom;
}

return true;
}
Loading

0 comments on commit 717ce6c

Please sign in to comment.