terminal/src/renderer/base/fontinfo.cpp
Michael Niksa 7f5a19b627
Reduce instances of font fallback dialog (#9734)
Reduce instances of font fallback dialog through package font loading,
basic name trimming, and revised fallback test

- Adjusts the font dialog to only show when we attempt last-chance
  resolution from our hardcoded list of font names with a flag instead
  of with a string comparison by name
- Adds a resolution step to trim the font name by word from the end and
  retry to attempt to resolve a proper font that just has a weight
  suffix
- Adds a second font collection to font loading that will attempt to
  locate all TTF files sitting next to our binary, like in our package

- [x] Wrote my font preference in the JSON as `Cascadia Code Heavy` and
  watched it quietly resolve to just `Cascadia Code` without the dialog.
- [x] Put a font that isn't registered with the system into the layout
  directory for the package, set it as my desired font in Terminal, and
  watched it load just fine.
- [x] Try a font name with different casing and see if dialog doesn't
  pop anymore
- [x] Try a font with different (localized) names like MS ゴシック and
  see if dialog doesn't pop anymore
- [x] Check Win7 with WPF target

Closes #9375
2021-04-08 17:49:07 +00:00

101 lines
2.7 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "../inc/FontInfo.hpp"
bool operator==(const FontInfo& a, const FontInfo& b)
{
return (static_cast<FontInfoBase>(a) == static_cast<FontInfoBase>(b) &&
a._coordSize == b._coordSize &&
a._coordSizeUnscaled == b._coordSizeUnscaled);
}
FontInfo::FontInfo(const std::wstring_view faceName,
const unsigned char family,
const unsigned int weight,
const COORD coordSize,
const unsigned int codePage,
const bool fSetDefaultRasterFont /* = false */) :
FontInfoBase(faceName, family, weight, fSetDefaultRasterFont, codePage),
_coordSize(coordSize),
_coordSizeUnscaled(coordSize),
_didFallback(false)
{
ValidateFont();
}
FontInfo::FontInfo(const FontInfo& fiFont) :
FontInfoBase(fiFont),
_coordSize(fiFont.GetSize()),
_coordSizeUnscaled(fiFont.GetUnscaledSize())
{
}
COORD FontInfo::GetUnscaledSize() const
{
return _coordSizeUnscaled;
}
COORD FontInfo::GetSize() const
{
return _coordSize;
}
void FontInfo::SetFromEngine(const std::wstring_view faceName,
const unsigned char family,
const unsigned int weight,
const bool fSetDefaultRasterFont,
const COORD coordSize,
const COORD coordSizeUnscaled)
{
FontInfoBase::SetFromEngine(faceName,
family,
weight,
fSetDefaultRasterFont);
_coordSize = coordSize;
_coordSizeUnscaled = coordSizeUnscaled;
_ValidateCoordSize();
}
bool FontInfo::GetFallback() const noexcept
{
return _didFallback;
}
void FontInfo::SetFallback(const bool didFallback) noexcept
{
_didFallback = didFallback;
}
void FontInfo::ValidateFont()
{
_ValidateCoordSize();
}
void FontInfo::_ValidateCoordSize()
{
// a (0,0) font is okay for the default raster font, as we will eventually set the dimensions based on the font GDI
// passes back to us.
if (!IsDefaultRasterFontNoSize())
{
// Initialize X to 1 so we don't divide by 0
if (_coordSize.X == 0)
{
_coordSize.X = 1;
}
// If we have no font size, we want to use 8x12 by default
if (_coordSize.Y == 0)
{
_coordSize.X = 8;
_coordSize.Y = 12;
_coordSizeUnscaled = _coordSize;
}
}
}