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

remove Snooty "font tweaks" #46

Merged
merged 2 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 57 additions & 71 deletions dfxgui/dfxguitextdisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ To contact the author, use the contact form at http://destroyfx.org/
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string_view>

#include "dfxguieditor.h"
#include "dfxguimisc.h"
#include "dfxmath.h"

using DGFontTweaks = internal::DGFontTweaks;

//-----------------------------------------------------------------------------
static constexpr VSTGUI::CHoriTxtAlign DFXGUI_TextAlignmentToVSTGUI(dfx::TextAlignment inTextAlignment) noexcept
Expand All @@ -53,25 +53,43 @@ static constexpr VSTGUI::CHoriTxtAlign DFXGUI_TextAlignmentToVSTGUI(dfx::TextAli
}

//-----------------------------------------------------------------------------
static constexpr bool DFXGUI_IsBitmapFont(DGFontTweaks inFontTweaks) noexcept
static bool DFXGUI_IsBitmapFont(char const* inFontName) noexcept
{
switch (inFontTweaks)
if (inFontName)
{
case DGFontTweaks::SNOOTY10PX:
case DGFontTweaks::PASEMENT9PX:
std::string_view const fontNameView(inFontName);
if (fontNameView.compare(dfx::kFontName_Snooty10px) == 0)
{
return true;
}
if (fontNameView.compare(dfx::kFontName_Pasement9px) == 0)
{
return true;
default:
return false;
}
}
return false;
}

//-----------------------------------------------------------------------------
// Constructor-time setup. Returns computed font tweaks enum to be saved for draw-time tweaking.
[[nodiscard]] static DGFontTweaks DFXGUI_ConfigureTextDisplay(DfxGuiEditor* inOwnerEditor,
VSTGUI::CTextLabel* inTextDisplay,
dfx::TextAlignment inTextAlignment,
float inFontSize, DGColor inFontColor,
char const* inFontName)
VSTGUI::CPoint detail::GetTextViewPlatformOffset(char const* inFontName) noexcept
{
#if TARGET_OS_WIN32
// HACK: on Windows, render Snooty 1 pixel lower vertically
if (inFontName && (std::string(inFontName) == dfx::kFontName_Snooty10px))
{
return VSTGUI::CPoint(0, 1);
}
#endif
return {};
}

//-----------------------------------------------------------------------------
// common constructor-time setup
static void DFXGUI_ConfigureTextDisplay(DfxGuiEditor* inOwnerEditor,
VSTGUI::CTextLabel* inTextDisplay,
dfx::TextAlignment inTextAlignment,
float inFontSize, DGColor inFontColor,
char const* inFontName)
{
inTextDisplay->setTransparency(true);

Expand All @@ -83,45 +101,21 @@ static constexpr bool DFXGUI_IsBitmapFont(DGFontTweaks inFontTweaks) noexcept
inTextDisplay->setFont(fontDesc);
}

DGFontTweaks const fontTweaks = [inFontName]
{
if (inFontName)
{
if (strcmp(inFontName, dfx::kFontName_Snooty10px) == 0)
{
return DGFontTweaks::SNOOTY10PX;
}
if (strcmp(inFontName, dfx::kFontName_Pasement9px) == 0)
{
return DGFontTweaks::PASEMENT9PX;
}
}
return DGFontTweaks::NONE;
}();
inTextDisplay->setAntialias(!DFXGUI_IsBitmapFont(fontTweaks));
inTextDisplay->setAntialias(!DFXGUI_IsBitmapFont(inFontName));

return fontTweaks;
auto adjustedRegion = inTextDisplay->getViewSize();
adjustedRegion.offset(detail::GetTextViewPlatformOffset(inFontName));
inTextDisplay->setViewSize(adjustedRegion, false);
}

//-----------------------------------------------------------------------------
static DGRect DFXGUI_GetTextDrawRegion(DGFontTweaks inFontTweaks, DGRect const& inRegion)
static DGRect DFXGUI_GetTextDrawRegion(DGRect const& inRegion, bool inIsBitmapFont)
{
auto textArea = inRegion;
if (DFXGUI_IsBitmapFont(inFontTweaks))
if (inIsBitmapFont)
{
textArea.makeIntegral();
}
if (inFontTweaks == DGFontTweaks::SNOOTY10PX)
{
// TODO tom: It looks like we want this offset on windows too,
// to match Sophia's screenshots, but doing so clips the top
// of the text. Don't know why. Maybe we should just be adjusting
// the coordinates in the client GUI code?
#if TARGET_OS_MAC
textArea.offset(0, -2);
#endif
textArea.setHeight(textArea.getHeight() + 2);
}
return textArea;
}

Expand All @@ -144,9 +138,10 @@ DGTextDisplay::DGTextDisplay(DfxGuiEditor* inOwnerEditor,
char const* inFontName)
: DGControl<VSTGUI::CTextEdit>(inRegion, inOwnerEditor, inParamID, nullptr, inBackgroundImage),
mValueToTextProc(inTextProc ? inTextProc : valueToTextProc_Generic),
mValueToTextUserData(inUserData)
mValueToTextUserData(inUserData),
mIsBitmapFont(DFXGUI_IsBitmapFont(inFontName))
{
mFontTweaks = DFXGUI_ConfigureTextDisplay(inOwnerEditor, this, inTextAlignment, inFontSize, inFontColor, inFontName);
DFXGUI_ConfigureTextDisplay(inOwnerEditor, this, inTextAlignment, inFontSize, inFontColor, inFontName);

setValueToStringFunction(std::bind(&DGTextDisplay::valueToTextProcBridge, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
Expand Down Expand Up @@ -320,7 +315,7 @@ void DGTextDisplay::takeFocus()
//-----------------------------------------------------------------------------
void DGTextDisplay::drawPlatformText(VSTGUI::CDrawContext* inContext, VSTGUI::IPlatformString* inString, VSTGUI::CRect const& inRegion)
{
auto const textArea = DFXGUI_GetTextDrawRegion(mFontTweaks, inRegion);
auto const textArea = DFXGUI_GetTextDrawRegion(inRegion, mIsBitmapFont);
VSTGUI::CTextEdit::drawPlatformText(inContext, inString, textArea);
}

Expand Down Expand Up @@ -360,24 +355,6 @@ bool DGTextDisplay::textToValueProcBridge(VSTGUI::UTF8StringPtr inText, float& o
return valueFromText.has_value();
}

//-----------------------------------------------------------------------------
// This is called by VSTGUI when the text display transitions to the platform's text
// editor. We move the text editor up so that it lands where the text label was.
// (XXX presumably there is a less hacky way to do this??)
VSTGUI::CRect DGTextDisplay::platformGetSize() const
{
VSTGUI::CRect rect = DGControl<VSTGUI::CTextEdit>::platformGetSize();
if (mFontTweaks == DGFontTweaks::SNOOTY10PX)
{
#if TARGET_OS_MAC
rect.top -= 2;
#else
rect.top ++;
#endif
}
return rect;
}




Expand All @@ -388,9 +365,10 @@ VSTGUI::CRect DGTextDisplay::platformGetSize() const
DGStaticTextDisplay::DGStaticTextDisplay(DfxGuiEditor* inOwnerEditor, DGRect const& inRegion, DGImage* inBackgroundImage,
dfx::TextAlignment inTextAlignment, float inFontSize,
DGColor inFontColor, char const* inFontName)
: DGControl<VSTGUI::CTextLabel>(inRegion, nullptr, inBackgroundImage)
: DGControl<VSTGUI::CTextLabel>(inRegion, nullptr, inBackgroundImage),
mIsBitmapFont(DFXGUI_IsBitmapFont(inFontName))
{
mFontTweaks = DFXGUI_ConfigureTextDisplay(inOwnerEditor, this, inTextAlignment, inFontSize, inFontColor, inFontName);
DFXGUI_ConfigureTextDisplay(inOwnerEditor, this, inTextAlignment, inFontSize, inFontColor, inFontName);

setMouseEnabled(false);
}
Expand All @@ -416,7 +394,7 @@ void DGStaticTextDisplay::setCFText(CFStringRef inText)
//-----------------------------------------------------------------------------
void DGStaticTextDisplay::drawPlatformText(VSTGUI::CDrawContext* inContext, VSTGUI::IPlatformString* inString, VSTGUI::CRect const& inRegion)
{
auto const textArea = DFXGUI_GetTextDrawRegion(mFontTweaks, inRegion);
auto const textArea = DFXGUI_GetTextDrawRegion(inRegion, mIsBitmapFont);
VSTGUI::CTextLabel::drawPlatformText(inContext, inString, textArea);
}

Expand Down Expand Up @@ -504,6 +482,12 @@ DGHelpBox::DGHelpBox(DfxGuiEditor* inOwnerEditor, DGRect const& inRegion,
{
assert(inOwnerEditor);
assert(inTextForControlProc);

// HACK part 1: undo "view platform offset", because at the control level, that includes the background,
// but what we more narrowly want is to offset the individual regions of each line of text
auto adjustedRegion = getViewSize();
adjustedRegion.offset(-detail::GetTextViewPlatformOffset(getFont()->getName()));
setViewSize(adjustedRegion, false);
}

//-----------------------------------------------------------------------------
Expand All @@ -522,10 +506,12 @@ void DGHelpBox::draw(VSTGUI::CDrawContext* inContext)
image->draw(inContext, getViewSize());
}

constexpr VSTGUI::CCoord textHeight = 10;
auto const fontHeight = getFont()->getSize();
DGRect textArea(getViewSize());
textArea.setSize(textArea.getWidth() - mTextMargin.x, textHeight);
textArea.setSize(textArea.getWidth() - mTextMargin.x, fontHeight + 2);
textArea.offset(mTextMargin);
// HACK part 2: apply "view platform offset" to the text draw region itself
textArea.offset(detail::GetTextViewPlatformOffset(getFont()->getName()));

std::istringstream stream(text);
std::string line;
Expand All @@ -539,13 +525,13 @@ void DGHelpBox::draw(VSTGUI::CDrawContext* inContext)
drawPlatformText(inContext, VSTGUI::UTF8String(line).getPlatformString(), textArea);
textArea.offset(1, 0);
drawPlatformText(inContext, VSTGUI::UTF8String(line).getPlatformString(), textArea);
textArea.offset(-1, textHeight + mLineSpacing + 2);
textArea.offset(-1, fontHeight + mLineSpacing + 2);
setFontColor(entryFontColor);
}
else
{
drawPlatformText(inContext, VSTGUI::UTF8String(line).getPlatformString(), textArea);
textArea.offset(0, textHeight + mLineSpacing);
textArea.offset(0, fontHeight + mLineSpacing);
}
}

Expand Down
21 changes: 8 additions & 13 deletions dfxgui/dfxguitextdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ To contact the author, use the contact form at http://destroyfx.org/

#include "dfxguicontrol.h"

namespace internal
{
// Some fonts have tweaks that we apply on a platform-by-platform basis
// to get pixel-perfect alignment.
enum class DGFontTweaks

//-----------------------------------------------------------------------------
namespace detail
{
NONE,
SNOOTY10PX,
PASEMENT9PX,
};
} // namespace internal
VSTGUI::CPoint GetTextViewPlatformOffset(char const* inFontName) noexcept;
}


//-----------------------------------------------------------------------------
class DGTextDisplay : public DGControl<VSTGUI::CTextEdit>
Expand Down Expand Up @@ -88,7 +84,6 @@ class DGTextDisplay : public DGControl<VSTGUI::CTextEdit>
protected:
void takeFocus() override;
void drawPlatformText(VSTGUI::CDrawContext* inContext, VSTGUI::IPlatformString* inString, VSTGUI::CRect const& inRegion) override;
VSTGUI::CRect platformGetSize() const override;

bool valueToTextProcBridge(float inValue, char outTextUTF8[kTextMaxLength], CParamDisplay* inUserData);
bool textToValueProcBridge(VSTGUI::UTF8StringPtr inText, float& outValue, VSTGUI::CTextEdit* textEdit);
Expand All @@ -101,7 +96,7 @@ class DGTextDisplay : public DGControl<VSTGUI::CTextEdit>
private:
static bool valueToTextProc_Generic(float inValue, char outTextUTF8[], void* inUserData);

internal::DGFontTweaks mFontTweaks = internal::DGFontTweaks::NONE;
bool const mIsBitmapFont;
};


Expand All @@ -126,7 +121,7 @@ class DGStaticTextDisplay : public DGControl<VSTGUI::CTextLabel>
void drawPlatformText(VSTGUI::CDrawContext* inContext, VSTGUI::IPlatformString* inString, VSTGUI::CRect const& inRegion) override;

private:
internal::DGFontTweaks mFontTweaks = internal::DGFontTweaks::NONE;
bool const mIsBitmapFont;
};


Expand Down
16 changes: 11 additions & 5 deletions geometer/gui/geometereditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ enum {
pos_sliderincY = 35,

pos_sliderlabelX = 19,
pos_sliderlabelY = pos_sliderY - 1,
pos_sliderlabelY = pos_sliderY - 3,
pos_sliderlabelwidth = 32,
pos_sliderlabelheight = 10,
pos_sliderlabelheight = 12,

pos_finedownX = 27,
pos_finedownY = 263,
Expand All @@ -77,7 +77,7 @@ enum {
pos_finebuttonincX = 240,
pos_finebuttonincY = pos_sliderincY,

pos_displayheight = 10,
pos_displayheight = 12,
pos_displayX = 180,
pos_displayY = pos_sliderY - pos_displayheight,
pos_displaywidth = pos_sliderX + pos_sliderwidth - pos_displayX,
Expand Down Expand Up @@ -124,6 +124,10 @@ GeometerHelpBox::GeometerHelpBox(DfxGuiEditor * inOwnerEditor, DGRect const & in
dfx::kFontSize_Snooty10px, DGColor::kBlack, dfx::kFontName_Snooty10px),
helpCategory(HELP_CATEGORY_GENERAL), itemNum(HELP_EMPTY)
{
// HACK: duplicated from DGHelpBox (TODO: unify this code with DGHelpBox)
auto adjustedRegion = getViewSize();
adjustedRegion.offset(-detail::GetTextViewPlatformOffset(getFont()->getName()));
setViewSize(adjustedRegion, false);
}

//--------------------------------------------------------------------------
Expand All @@ -138,8 +142,10 @@ void GeometerHelpBox::draw(VSTGUI::CDrawContext * inContext) {
image->draw(inContext, getViewSize());

DGRect textpos(getViewSize());
textpos.setSize(textpos.getWidth() - 5, 10);
textpos.offset(4, 3);
textpos.setSize(textpos.getWidth() - 5, getFont()->getSize() + 2);
textpos.offset(4, 1);
// HACK: duplicated from DGHelpBox (TODO: unify this code with DGHelpBox)
textpos.offset(detail::GetTextViewPlatformOffset(getFont()->getName()));

auto const helpstrings = [this]() -> char const * {
switch (helpCategory) {
Expand Down
6 changes: 3 additions & 3 deletions rezsynth/gui/rezsyntheditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum
kHorizontalSliderHeight = 23,

kHorizontalDisplayWidth = kHorizontalSliderWidth / 2,
kHorizontalDisplayHeight = 10,
kHorizontalDisplayHeight = 12,
kHorizontalDisplayX = kHorizontalSliderX + kHorizontalSliderWidth - kHorizontalDisplayWidth - 3,
kHorizontalDisplayY = kHorizontalSliderY - kHorizontalDisplayHeight,

Expand All @@ -53,7 +53,7 @@ enum
kVerticalSliderNameWidth = 13,

kVerticalDisplayX = kVerticalSliderX - 21,
kVerticalDisplayY = kVerticalSliderY + kVerticalSliderHeight + 11,
kVerticalDisplayY = kVerticalSliderY + kVerticalSliderHeight + 9,
kVerticalDisplayWidth = 48,
kVerticalDisplayHeight = kHorizontalDisplayHeight,

Expand Down Expand Up @@ -378,7 +378,7 @@ long RezSynthEditor::OpenEditor()
// help display
pos.set(kHelpX, kHelpY, kHelpWidth, kHelpHeight);
mHelpBox = emplaceControl<DGHelpBox>(this, pos, std::bind(&RezSynthEditor::GetHelpForControl, this, std::placeholders::_1), nullptr, DGColor::kWhite);
mHelpBox->setTextMargin({11, 8});
mHelpBox->setTextMargin({11, 6});



Expand Down
4 changes: 2 additions & 2 deletions scrubby/gui/scrubbyeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ enum

kDisplayWidth = 90,
kDisplayWidth_big = 117, // for seek rate
kDisplayHeight = 10,
kDisplayHeight = 12,
kDisplayInsetX = 0,
kDisplayInsetX_leftAlign = 2,
kDisplayInsetY = 0,
Expand Down Expand Up @@ -522,7 +522,7 @@ long ScrubbyEditor::OpenEditor()
pos.set(kHelpX, kHelpY, helpBackgroundImage->getWidth(), helpBackgroundImage->getHeight());
mHelpBox = emplaceControl<DGHelpBox>(this, pos, std::bind(&ScrubbyEditor::GetHelpForControl, this, _1), helpBackgroundImage, DGColor::kWhite);
mHelpBox->setHeaderFontColor(DGColor::kBlack);
mHelpBox->setTextMargin({12, 4});
mHelpBox->setTextMargin({12, 2});



Expand Down
4 changes: 2 additions & 2 deletions skidder/gui/skiddereditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum
kSliderInc = 64,

kDisplayX = 334,
kDisplayY = kSliderY + 11,
kDisplayY = kSliderY + 10,
kDisplayWidth = 70,
kDisplayHeight = 12,

Expand Down Expand Up @@ -297,7 +297,7 @@ long SkidderEditor::OpenEditor()
// help display
pos.set(kHelpX, kHelpY, kHelpWidth, kHelpHeight);
mHelpBox = emplaceControl<DGHelpBox>(this, pos, std::bind(&SkidderEditor::GetHelpForControl, this, std::placeholders::_1), nullptr, DGColor::kWhite);
mHelpBox->setTextMargin({8, 12});
mHelpBox->setTextMargin({8, 10});
mHelpBox->setLineSpacing(3);


Expand Down
Loading