diff --git a/Inc/SpriteFont.h b/Inc/SpriteFont.h index 839e277f8..0fdea051e 100644 --- a/Inc/SpriteFont.h +++ b/Inc/SpriteFont.h @@ -144,7 +144,9 @@ namespace DirectX // Spacing properties DIRECTX_TOOLKIT_API float __cdecl GetLineSpacing() const noexcept; - DIRECTX_TOOLKIT_API void __cdecl SetLineSpacing(float spacing); + DIRECTX_TOOLKIT_API void __cdecl SetLineSpacing(float spacing) noexcept; + + DIRECTX_TOOLKIT_API void __cdecl SetPixelAlignment(bool enable) noexcept; // Font properties DIRECTX_TOOLKIT_API wchar_t __cdecl GetDefaultCharacter() const noexcept; diff --git a/Src/SpriteFont.cpp b/Src/SpriteFont.cpp index 99a99510d..a096e489d 100644 --- a/Src/SpriteFont.cpp +++ b/Src/SpriteFont.cpp @@ -54,6 +54,7 @@ class SpriteFont::Impl std::vector glyphsIndex; Glyph const* defaultGlyph; float lineSpacing; + bool pixelAlignment; private: void CreateTextureResource(_In_ ID3D11Device* device, @@ -101,6 +102,7 @@ SpriteFont::Impl::Impl( bool forceSRGB) noexcept(false) : defaultGlyph(nullptr), lineSpacing(0), + pixelAlignment(false), utfBufferSize(0) { if (!device || !reader) @@ -175,6 +177,7 @@ SpriteFont::Impl::Impl( glyphs(iglyphs, iglyphs + glyphCount), defaultGlyph(nullptr), lineSpacing(ilineSpacing), + pixelAlignment(false), utfBufferSize(0) { if (!itexture || !iglyphs) @@ -480,6 +483,11 @@ void XM_CALLCONV SpriteFont::DrawString(_In_ SpriteBatch* spriteBatch, _In_z_ wc offset = XMVectorMultiplyAdd(glyphRect, axisIsMirroredTable[effects & 3], offset); } + if (pImpl->pixelAlignment) + { + offset = XMVectorRound(offset); + } + spriteBatch->Draw(pImpl->texture.Get(), position, &glyph->Subrect, color, rotation, offset, scale, effects, layerDepth); }, true); } @@ -610,12 +618,18 @@ float SpriteFont::GetLineSpacing() const noexcept } -void SpriteFont::SetLineSpacing(float spacing) +void SpriteFont::SetLineSpacing(float spacing) noexcept { pImpl->lineSpacing = spacing; } +void SpriteFont::SetPixelAlignment(bool enable) noexcept +{ + pImpl->pixelAlignment = enable; +} + + // Font properties wchar_t SpriteFont::GetDefaultCharacter() const noexcept {