Skip to content

Commit 5dddb32

Browse files
committed
Col clamp fix. Console DPI fix.
1 parent 7c962ed commit 5dddb32

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

IDE/src/util/ConsoleProvider.bf

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ class WinNativeConsoleProvider : ConsoleProvider
305305

306306
[CLink]
307307
public static extern Windows.IntBool FillConsoleOutputAttribute(Windows.Handle handle, uint16 attribute, int32 length, POINT writeCoord, out int32 numCharsWritten);
308+
309+
public static function int32 (Windows.HWnd hwnd) sGetDpiForWindow;
308310
#endif
309311

310312
ScreenInfo mScreenInfo ~ delete _;
@@ -360,6 +362,16 @@ class WinNativeConsoleProvider : ConsoleProvider
360362
public override bool CursorVisible => (mScreenInfo != null) ? (mScreenInfo.mCursorInfo.mVisible != 0) : true;
361363
public override float CursorHeight => (mScreenInfo != null) ? (mScreenInfo.mCursorInfo.mSize / 100.0f) : 0.15f;
362364

365+
366+
public this()
367+
{
368+
if (sGetDpiForWindow == null)
369+
{
370+
var lib = Windows.LoadLibraryA("user32.dll");
371+
sGetDpiForWindow = (.)Windows.GetProcAddress(lib, "GetDpiForWindow");
372+
}
373+
}
374+
363375
public ~this()
364376
{
365377
mCmdSpawn?.Kill();
@@ -389,6 +401,8 @@ class WinNativeConsoleProvider : ConsoleProvider
389401
if (mScreenInfo.mFullCharInfo != null)
390402
{
391403
int bufRow = row + mScreenInfo.mScrollTop;
404+
if (col >= mScreenInfo.mInfo.mWidth)
405+
return default;
392406
int idx = bufRow * mScreenInfo.mInfo.mWidth + col;
393407
int maxIdx = (int)mScreenInfo.mInfo.mWidth * mScreenInfo.mInfo.mHeight;
394408
if ((idx < 0) || (idx >= maxIdx))
@@ -397,8 +411,12 @@ class WinNativeConsoleProvider : ConsoleProvider
397411
return .() { mChar = info.mChar, mAttributes = info.mAttributes };
398412
}
399413

400-
int idx = row * mScreenInfo.mInfo.mWindowRect.Width + col;
401-
int maxIdx = (int)mScreenInfo.mInfo.mWindowRect.Width * mScreenInfo.mInfo.mWindowRect.Height;
414+
int cols = mScreenInfo.mInfo.mWindowRect.Width;
415+
int rows = mScreenInfo.mInfo.mWindowRect.Height;
416+
if (col >= cols)
417+
return default;
418+
int idx = row * cols + col;
419+
int maxIdx = (int)cols * rows;
402420
if ((idx < 0) || (idx >= maxIdx))
403421
return default;
404422
var info = mScreenInfo.mCharInfo[idx];
@@ -539,7 +557,24 @@ class WinNativeConsoleProvider : ConsoleProvider
539557
uint32 style = (.)Windows.GetWindowLong(window, Windows.GWL_STYLE);
540558
uint32 styleEx = (.)Windows.GetWindowLong(window, Windows.GWL_EXSTYLE);
541559

542-
Windows.Rect rect = .(0, 0, (.)(cols * fontInfo.mSize.x), (.)(rows * fontInfo.mSize.y));
560+
float colsF = cols;
561+
float rowsF = rows;
562+
563+
float scale = 1.0f;
564+
if (sGetDpiForWindow != null)
565+
{
566+
int32 dpi = sGetDpiForWindow(window);
567+
if ((dpi > 0) && (dpi != 96))
568+
{
569+
scale = dpi / 96.0f;
570+
fontInfo.mSize.x = (.)(fontInfo.mSize.x * scale + 0.05f);
571+
fontInfo.mSize.y = (.)(fontInfo.mSize.y * scale + 0.05f);
572+
colsF -= 0.85f;
573+
rowsF -= 0.85f;
574+
}
575+
}
576+
577+
Windows.Rect rect = .(0, 0, (.)(colsF * fontInfo.mSize.x), (.)(rowsF * fontInfo.mSize.y));
543578
Windows.AdjustWindowRectEx(ref rect, style, false, styleEx);
544579

545580
Windows.SetWindowPos(window, default, 0, 0, rect.Width, rect.Height,

0 commit comments

Comments
 (0)