From 3d920c4088b59ae666ac06d1218c58a2c8bd5550 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 9 Feb 2021 09:42:44 -0800 Subject: [PATCH] Fix tab spacing to use widest character as ref We had been using each 4th column of spaces. But we needed an absolute spacing rather than relative to the existing text to match spacing between the input text box and our stroke font --- common/gal/stroke_font.cpp | 21 ++++++++++----------- include/gal/stroke_font.h | 1 + 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/common/gal/stroke_font.cpp b/common/gal/stroke_font.cpp index e4f22bdea7..a34292df18 100644 --- a/common/gal/stroke_font.cpp +++ b/common/gal/stroke_font.cpp @@ -48,7 +48,7 @@ std::vector* g_newStrokeFontGlyphBoundingBoxes; ///< Bounding boxes of STROKE_FONT::STROKE_FONT( GAL* aGal ) : - m_gal( aGal ), m_glyphs( nullptr ), m_glyphBoundingBoxes( nullptr ) + m_gal( aGal ), m_glyphs( nullptr ), m_glyphBoundingBoxes( nullptr ), m_maxGlyphWidth( 1.0 ) { } @@ -151,6 +151,8 @@ bool STROKE_FONT::LoadNewStrokeFont( const char* const aNewStrokeFont[], int aNe // Compute the bounding box of the glyph g_newStrokeFontGlyphBoundingBoxes->emplace_back( computeBoundingBox( glyph, glyphWidth ) ); g_newStrokeFontGlyphs->push_back( glyph ); + m_maxGlyphWidth = std::max( m_maxGlyphWidth, + g_newStrokeFontGlyphBoundingBoxes->back().GetWidth() ); } m_glyphs = g_newStrokeFontGlyphs; @@ -334,6 +336,7 @@ void STROKE_FONT::drawSingleLineText( const UTF8& aText ) // Allocate only once (for performance) std::vector ptListScaled; + int char_count = 0; yOffset = 0; @@ -343,16 +346,11 @@ void STROKE_FONT::drawSingleLineText( const UTF8& aText ) // The choice of spaces is somewhat arbitrary but sufficient for aligning text if( *chIt == '\t' ) { - double space = glyphSize.x * m_glyphBoundingBoxes->at( 0 ).GetEnd().x; - - // We align to the 4th column (fmod) but only need to account for 3 of - // the four spaces here with the extra. This ensures that we have at - // least 1 space for the \t character - double addlSpace = 3.0 * space - std::fmod( xOffset, 4.0 * space ); - - // Add the remaining space (between 0 and 3 spaces) - // The fourth space is added by the 'dd' character - xOffset += addlSpace; + // We align to the 4th column. This is based on the monospace font used in the text input + // boxes. Here, we take the widest character as our baseline spacing and make tab stops + // at each fourth of this widest character + char_count = ( char_count / 4 + 1 ) * 4 - 1; + xOffset = m_maxGlyphWidth * baseGlyphSize.x * char_count; glyphSize = baseGlyphSize; yOffset = 0; @@ -491,6 +489,7 @@ void STROKE_FONT::drawSingleLineText( const UTF8& aText ) m_gal->DrawPolyline( &ptListScaled[0], ptCount ); } + char_count++; xOffset += glyphSize.x * bbox.GetEnd().x; } diff --git a/include/gal/stroke_font.h b/include/gal/stroke_font.h index d24f77da7f..ec22f7ac17 100644 --- a/include/gal/stroke_font.h +++ b/include/gal/stroke_font.h @@ -168,6 +168,7 @@ private: GAL* m_gal; ///< Pointer to the GAL const GLYPH_LIST* m_glyphs; ///< Glyph list const std::vector* m_glyphBoundingBoxes; ///< Bounding boxes of the glyphs + double m_maxGlyphWidth; ///< The widest glyph in our set ///< Factor that determines relative vertical position of the overbar. static const double OVERBAR_POSITION_FACTOR;