Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add font weight options #6048

Merged
8 commits merged into from May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/cascadia/SettingsSchema.md
Expand Up @@ -47,6 +47,7 @@ Properties listed below are specific to each unique profile.
| `cursorShape` | Optional | String | `bar` | Sets the cursor shape for the profile. Possible values: `"vintage"` ( ▃ ), `"bar"` ( ┃ ), `"underscore"` ( ▁ ), `"filledBox"` ( █ ), `"emptyBox"` ( ▯ ) |
| `fontFace` | Optional | String | `Cascadia Mono` | Name of the font face used in the profile. We will try to fallback to Consolas if this can't be found or is invalid. |
| `fontSize` | Optional | Integer | `12` | Sets the font size. |
| `fontWeight` | Optional | String | `normal` | Sets the weight (lightness or heaviness of the strokes) for the given font. Possible values: `"thin"`, `"extra-light"`, `"light"`, `"semi-light"`, `"normal"`, `"medium"`, `"semi-bold"`, `"bold"`, `"extra-bold"`, `"black"`, `"extra-black"`, or the corresponding numeric representation of OpenType font weight. |
| `foreground` | Optional | String | | Sets the foreground color of the profile. Overrides `foreground` set in color scheme if `colorscheme` is set. Uses hex color format: `#rgb` or `"#rrggbb"`. |
| `hidden` | Optional | Boolean | `false` | If set to true, the profile will not appear in the list of profiles. This can be used to hide default profiles and dynamically generated profiles, while leaving them in your settings file. |
| `historySize` | Optional | Integer | `9001` | The number of lines above the ones displayed in the window you can scroll back to. |
Expand Down
24 changes: 24 additions & 0 deletions doc/cascadia/profiles.schema.json
Expand Up @@ -531,6 +531,30 @@
"minimum": 1,
"type": "integer"
},
"fontWeight": {
"default": "normal",
"description": "Sets the weight (lightness or heaviness of the strokes) for the given font. Possible values:\n -\"thin\"\n -\"extra-light\"\n -\"light\"\n -\"semi-light\"\n -\"normal\" (default)\n -\"medium\"\n -\"semi-bold\"\n -\"bold\"\n -\"extra-bold\"\n -\"black\"\n -\"extra-black\" or the corresponding numeric representation of OpenType font weight.",
"oneOf": [
{
"enum": [
"thin",
"extra-light",
"light",
"semi-light",
"normal",
"medium",
"semi-bold",
"bold",
"extra-bold",
"black",
"extra-black"
],
"type": "string"
},
{
"type": "integer"
}
}
"foreground": {
"$ref": "#/definitions/Color",
"default": "#cccccc",
Expand Down
118 changes: 118 additions & 0 deletions src/cascadia/TerminalApp/Profile.cpp
Expand Up @@ -36,6 +36,7 @@ static constexpr std::string_view ConnectionTypeKey{ "connectionType" };
static constexpr std::string_view CommandlineKey{ "commandline" };
static constexpr std::string_view FontFaceKey{ "fontFace" };
static constexpr std::string_view FontSizeKey{ "fontSize" };
static constexpr std::string_view FontWeightKey{ "fontWeight" };
static constexpr std::string_view AcrylicTransparencyKey{ "acrylicOpacity" };
static constexpr std::string_view UseAcrylicKey{ "useAcrylic" };
static constexpr std::string_view ScrollbarStateKey{ "scrollbarState" };
Expand Down Expand Up @@ -66,6 +67,19 @@ static constexpr std::wstring_view CursorShapeUnderscore{ L"underscore" };
static constexpr std::wstring_view CursorShapeFilledbox{ L"filledBox" };
static constexpr std::wstring_view CursorShapeEmptybox{ L"emptyBox" };

// Possible values for Font Weight
static constexpr std::string_view FontWeightThin{ "thin" };
static constexpr std::string_view FontWeightExtraLight{ "extra-light" };
static constexpr std::string_view FontWeightLight{ "light" };
static constexpr std::string_view FontWeightSemiLight{ "semi-light" };
static constexpr std::string_view FontWeightNormal{ "normal" };
static constexpr std::string_view FontWeightMedium{ "medium" };
static constexpr std::string_view FontWeightSemiBold{ "semi-bold" };
static constexpr std::string_view FontWeightBold{ "bold" };
static constexpr std::string_view FontWeightExtraBold{ "extra-bold" };
static constexpr std::string_view FontWeightBlack{ "black" };
static constexpr std::string_view FontWeightExtraBlack{ "extra-black" };

// Possible values for Image Stretch Mode
static constexpr std::string_view ImageStretchModeNone{ "none" };
static constexpr std::string_view ImageStretchModeFill{ "fill" };
Expand Down Expand Up @@ -115,6 +129,7 @@ Profile::Profile(const std::optional<GUID>& guid) :
_startingDirectory{},
_fontFace{ DEFAULT_FONT_FACE },
_fontSize{ DEFAULT_FONT_SIZE },
/* _fontWeight is initialized below because the structure won't accept a uint16_t directly */
_acrylicTransparency{ 0.5 },
_useAcrylic{ false },
_scrollbarState{},
Expand All @@ -128,6 +143,10 @@ Profile::Profile(const std::optional<GUID>& guid) :
_retroTerminalEffect{},
_antialiasingMode{ TextAntialiasingMode::Grayscale }
{
winrt::Windows::UI::Text::FontWeight weight;
weight.Weight = DEFAULT_FONT_WEIGHT;
_fontWeight = weight;

}

Profile::~Profile()
Expand Down Expand Up @@ -180,6 +199,7 @@ TerminalSettings Profile::CreateTerminalSettings(const std::unordered_map<std::w

terminalSettings.FontFace(_fontFace);
terminalSettings.FontSize(_fontSize);
terminalSettings.FontWeight(_fontWeight);
terminalSettings.Padding(_padding);

terminalSettings.Commandline(_commandline);
Expand Down Expand Up @@ -474,6 +494,12 @@ void Profile::LayerJson(const Json::Value& json)

JsonUtils::GetInt(json, FontSizeKey, _fontSize);

if (json.isMember(JsonKey(FontWeightKey)))
{
auto fontWeight{ json[JsonKey(FontWeightKey)] };
_fontWeight = _ParseFontWeight(fontWeight);
}

JsonUtils::GetDouble(json, AcrylicTransparencyKey, _acrylicTransparency);

JsonUtils::GetBool(json, UseAcrylicKey, _useAcrylic);
Expand Down Expand Up @@ -740,6 +766,98 @@ std::wstring Profile::EvaluateStartingDirectory(const std::wstring& directory)
}
}

// Method Description:
// - Helper function for converting a user-specified font weight value to its corresponding enum
// Arguments:
// - The value from the settings.json file
// Return Value:
// - The corresponding value which maps to the string provided by the user
winrt::Windows::UI::Text::FontWeight Profile::_ParseFontWeight(const Json::Value& json)
{
if (json.isUInt())
{
auto fontWeight = json.asUInt();

// If it's a valid fontWeight, pass it through. Otherwise, ignore it.
switch (fontWeight)
{
case 100:
return winrt::Windows::UI::Text::FontWeights::Thin();
case 200:
return winrt::Windows::UI::Text::FontWeights::ExtraLight();
case 300:
return winrt::Windows::UI::Text::FontWeights::Light();
case 350:
return winrt::Windows::UI::Text::FontWeights::SemiLight();
case 400:
return winrt::Windows::UI::Text::FontWeights::Normal();
case 500:
return winrt::Windows::UI::Text::FontWeights::Medium();
case 600:
return winrt::Windows::UI::Text::FontWeights::SemiBold();
case 700:
return winrt::Windows::UI::Text::FontWeights::Bold();
case 800:
return winrt::Windows::UI::Text::FontWeights::ExtraBold();
case 900:
return winrt::Windows::UI::Text::FontWeights::Black();
case 950:
return winrt::Windows::UI::Text::FontWeights::ExtraBlack();
}
}

if (json.isString())
{
auto fontWeight = json.asString();
if (fontWeight == FontWeightThin)
{
return winrt::Windows::UI::Text::FontWeights::Thin();
}
else if (fontWeight == FontWeightExtraLight)
{
return winrt::Windows::UI::Text::FontWeights::ExtraLight();
}
else if (fontWeight == FontWeightLight)
{
return winrt::Windows::UI::Text::FontWeights::Light();
}
else if (fontWeight == FontWeightSemiLight)
{
return winrt::Windows::UI::Text::FontWeights::SemiLight();
}
else if (fontWeight == FontWeightNormal)
{
return winrt::Windows::UI::Text::FontWeights::Normal();
}
else if (fontWeight == FontWeightMedium)
{
return winrt::Windows::UI::Text::FontWeights::Medium();
}
else if (fontWeight == FontWeightSemiBold)
{
return winrt::Windows::UI::Text::FontWeights::SemiBold();
}
else if (fontWeight == FontWeightBold)
{
return winrt::Windows::UI::Text::FontWeights::Bold();
}
else if (fontWeight == FontWeightExtraBold)
{
return winrt::Windows::UI::Text::FontWeights::ExtraBold();
}
else if (fontWeight == FontWeightBlack)
{
return winrt::Windows::UI::Text::FontWeights::Black();
}
else if (fontWeight == FontWeightExtraBlack)
{
return winrt::Windows::UI::Text::FontWeights::ExtraBlack();
}
}

return winrt::Windows::UI::Text::FontWeights::Normal();
}

// Method Description:
// - Helper function for converting a user-specified closeOnExit value to its corresponding enum
// Arguments:
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/Profile.h
Expand Up @@ -114,6 +114,8 @@ class TerminalApp::Profile final
static std::tuple<winrt::Windows::UI::Xaml::HorizontalAlignment, winrt::Windows::UI::Xaml::VerticalAlignment> ParseImageAlignment(const std::string_view imageAlignment);
static std::tuple<winrt::Windows::UI::Xaml::HorizontalAlignment, winrt::Windows::UI::Xaml::VerticalAlignment> _ConvertJsonToAlignment(const Json::Value& json);

static winrt::Windows::UI::Text::FontWeight _ParseFontWeight(const Json::Value& json);

static CloseOnExitMode ParseCloseOnExitMode(const Json::Value& json);
static std::string_view _SerializeCloseOnExitMode(const CloseOnExitMode closeOnExitMode);

Expand Down Expand Up @@ -152,6 +154,7 @@ class TerminalApp::Profile final
std::wstring _fontFace;
std::optional<std::wstring> _startingDirectory;
int32_t _fontSize;
winrt::Windows::UI::Text::FontWeight _fontWeight;
double _acrylicTransparency;
bool _useAcrylic;

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TSFInputControl.cpp
Expand Up @@ -201,6 +201,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// things in DIPs, and the fontSize is in pixels.
TextBlock().FontSize(unscaledFontSizePx);
TextBlock().FontFamily(Media::FontFamily(fontArgs->FontFace()));
TextBlock().FontWeight(fontArgs->FontWeight());

// TextBlock's actual dimensions right after initialization is 0w x 0h. So,
// if an IME is displayed before TextBlock has text (like showing the emoji picker
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TSFInputControl.h
Expand Up @@ -27,6 +27,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
GETSET_PROPERTY(Windows::Foundation::Size, FontSize);

GETSET_PROPERTY(winrt::hstring, FontFace);

GETSET_PROPERTY(Windows::UI::Text::FontWeight, FontWeight);
};

struct TSFInputControl : TSFInputControlT<TSFInputControl>
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TSFInputControl.idl
Expand Up @@ -14,6 +14,7 @@ namespace Microsoft.Terminal.TerminalControl
{
String FontFace { get; set; };
Windows.Foundation.Size FontSize { get; set; };
Windows.UI.Text.FontWeight FontWeight { get; set; };
}

[default_interface]
Expand Down
16 changes: 11 additions & 5 deletions src/cascadia/TerminalControl/TermControl.cpp
Expand Up @@ -63,8 +63,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_autoScrollingPointerPoint{ std::nullopt },
_autoScrollTimer{},
_lastAutoScrollUpdateTime{ std::nullopt },
_desiredFont{ DEFAULT_FONT_FACE, 0, 10, { 0, DEFAULT_FONT_SIZE }, CP_UTF8 },
_actualFont{ DEFAULT_FONT_FACE, 0, 10, { 0, DEFAULT_FONT_SIZE }, CP_UTF8, false },
_desiredFont{ DEFAULT_FONT_FACE, 0, DEFAULT_FONT_WEIGHT, { 0, DEFAULT_FONT_SIZE }, CP_UTF8 },
_actualFont{ DEFAULT_FONT_FACE, 0, DEFAULT_FONT_WEIGHT, { 0, DEFAULT_FONT_SIZE }, CP_UTF8, false },
_touchAnchor{ std::nullopt },
_cursorTimer{},
_lastMouseClickTimestamp{},
Expand Down Expand Up @@ -263,13 +263,14 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// Initialize our font information.
const auto fontFace = _settings.FontFace();
const short fontHeight = gsl::narrow_cast<short>(_settings.FontSize());
const auto fontWeight = _settings.FontWeight();
// The font width doesn't terribly matter, we'll only be using the
// height to look it up
// The other params here also largely don't matter.
// The family is only used to determine if the font is truetype or
// not, but DX doesn't use that info at all.
// The Codepage is additionally not actually used by the DX engine at all.
_actualFont = { fontFace, 0, 10, { 0, fontHeight }, CP_UTF8, false };
_actualFont = { fontFace, 0, fontWeight.Weight, { 0, fontHeight }, CP_UTF8, false };
_desiredFont = { _actualFont };

// set TSF Foreground
Expand Down Expand Up @@ -1678,7 +1679,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// Make sure we have a non-zero font size
const auto newSize = std::max<short>(gsl::narrow_cast<short>(fontSize), 1);
const auto fontFace = _settings.FontFace();
_actualFont = { fontFace, 0, 10, { 0, newSize }, CP_UTF8, false };
const auto fontWeight = _settings.FontWeight();
_actualFont = { fontFace, 0, fontWeight.Weight, { 0, newSize }, CP_UTF8, false };
_desiredFont = { _actualFont };

auto lock = _terminal->LockForWriting();
Expand Down Expand Up @@ -2197,13 +2199,14 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// Initialize our font information.
const auto fontFace = settings.FontFace();
const short fontHeight = gsl::narrow_cast<short>(settings.FontSize());
const auto fontWeight = settings.FontWeight();
// The font width doesn't terribly matter, we'll only be using the
// height to look it up
// The other params here also largely don't matter.
// The family is only used to determine if the font is truetype or
// not, but DX doesn't use that info at all.
// The Codepage is additionally not actually used by the DX engine at all.
FontInfo actualFont = { fontFace, 0, 10, { 0, fontHeight }, CP_UTF8, false };
FontInfo actualFont = { fontFace, 0, fontWeight.Weight, { 0, fontHeight }, CP_UTF8, false };
FontInfoDesired desiredFont = { actualFont };

// If the settings have negative or zero row or column counts, ignore those counts.
Expand Down Expand Up @@ -2497,6 +2500,9 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
eventArgs.FontSize(CharacterDimensions());
eventArgs.FontFace(_actualFont.GetFaceName());
::winrt::Windows::UI::Text::FontWeight weight;
weight.Weight = static_cast<uint16_t>(_actualFont.GetWeight());
eventArgs.FontWeight(weight);
}

// Method Description:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettings/IControlSettings.idl
Expand Up @@ -34,6 +34,7 @@ namespace Microsoft.Terminal.Settings

String FontFace;
Int32 FontSize;
Windows.UI.Text.FontWeight FontWeight;
String Padding;

IKeyBindings KeyBindings;
Expand Down
11 changes: 11 additions & 0 deletions src/cascadia/TerminalSettings/TerminalSettings.cpp
Expand Up @@ -34,6 +34,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
_padding{ DEFAULT_PADDING },
_fontFace{ DEFAULT_FONT_FACE },
_fontSize{ DEFAULT_FONT_SIZE },
_fontWeight{ DEFAULT_FONT_WEIGHT },
_backgroundImage{},
_backgroundImageOpacity{ 1.0 },
_backgroundImageStretchMode{ winrt::Windows::UI::Xaml::Media::Stretch::UniformToFill },
Expand Down Expand Up @@ -258,6 +259,16 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
_fontSize = value;
}

winrt::Windows::UI::Text::FontWeight TerminalSettings::FontWeight() noexcept
{
return _fontWeight;
}

void TerminalSettings::FontWeight(winrt::Windows::UI::Text::FontWeight value) noexcept
{
_fontWeight = value;
}

void TerminalSettings::BackgroundImage(hstring const& value)
{
_backgroundImage = value;
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettings/terminalsettings.h
Expand Up @@ -68,6 +68,8 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
void FontFace(hstring const& value);
int32_t FontSize() noexcept;
void FontSize(int32_t value) noexcept;
winrt::Windows::UI::Text::FontWeight FontWeight() noexcept;
void FontWeight(winrt::Windows::UI::Text::FontWeight value) noexcept;

hstring BackgroundImage();
void BackgroundImage(hstring const& value);
Expand Down Expand Up @@ -133,6 +135,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
double _tintOpacity;
hstring _fontFace;
int32_t _fontSize;
winrt::Windows::UI::Text::FontWeight _fontWeight;
hstring _padding;
hstring _backgroundImage;
double _backgroundImageOpacity;
Expand Down
1 change: 1 addition & 0 deletions src/inc/DefaultSettings.h
Expand Up @@ -30,6 +30,7 @@ constexpr short DEFAULT_HISTORY_SIZE = 9001;
// TODO GH 2674, don't disable this warning, move to std::wstring_view or something like that.
const std::wstring DEFAULT_FONT_FACE{ L"Cascadia Mono" };
constexpr int DEFAULT_FONT_SIZE = 12;
constexpr uint16_t DEFAULT_FONT_WEIGHT = 400; // normal

constexpr int DEFAULT_ROWS = 30;
constexpr int DEFAULT_COLS = 120;
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/dx/DxRenderer.cpp
Expand Up @@ -2003,7 +2003,7 @@ CATCH_RETURN();
try
{
std::wstring fontName(desired.GetFaceName());
DWRITE_FONT_WEIGHT weight = DWRITE_FONT_WEIGHT_NORMAL;
DWRITE_FONT_WEIGHT weight = static_cast<DWRITE_FONT_WEIGHT>(desired.GetWeight());
miniksa marked this conversation as resolved.
Show resolved Hide resolved
DWRITE_FONT_STYLE style = DWRITE_FONT_STYLE_NORMAL;
DWRITE_FONT_STRETCH stretch = DWRITE_FONT_STRETCH_NORMAL;
std::wstring localeName = _GetLocaleName();
Expand Down