Commits

Alex Szpakowski committed cb4ca2a

Fixed tab characters (‘\t’) in text to be drawn properly with love.graphics.print.

Comments (0)

Files changed (3)

src/modules/font/GlyphData.h

 	int advance;
 	int bearingX;
 	int bearingY;
-	int spacing;
 };
 
 /**

src/modules/graphics/opengl/Font.cpp

 	, lineHeight(1)
 	, mSpacing(1)
 	, filter(filter)
+	, useSpacesAsTab(false)
 {
 	this->filter.mipmap = Texture::FILTER_NONE;
 
 	textureWidth = TEXTURE_WIDTHS[textureSizeIndex];
 	textureHeight = TEXTURE_HEIGHTS[textureSizeIndex];
 
-	love::font::GlyphData *gd = 0;
+	love::font::GlyphData *gd = nullptr;
 
 	try
 	{
-		gd = r->getGlyphData(32);
+		gd = r->getGlyphData(32); // Space character.
 		type = (gd->getFormat() == love::font::GlyphData::FORMAT_LUMINANCE_ALPHA) ? FONT_TRUETYPE : FONT_IMAGE;
 
+		if (!r->hasGlyph(9)) // No tab character in the Rasterizer.
+			useSpacesAsTab = true;
+
 		loadVolatile();
 	}
 	catch (love::Exception &)
 
 Font::Glyph *Font::addGlyph(uint32 glyph)
 {
-	love::font::GlyphData *gd = rasterizer->getGlyphData(glyph);
+	love::font::GlyphData *gd = nullptr;
+
+	// Use spaces for the tab 'glyph'.
+	if (glyph == 9 && useSpacesAsTab)
+	{
+		love::font::GlyphData *spacegd = rasterizer->getGlyphData(32);
+
+		love::font::GlyphMetrics gm = {};
+		gm.advance = spacegd->getAdvance() * SPACES_PER_TAB;
+		gm.bearingX = spacegd->getBearingX();
+		gm.bearingY = spacegd->getBearingY();
+		love::font::GlyphData::Format f = spacegd->getFormat();
+
+		spacegd->release();
+
+		gd = new love::font::GlyphData(glyph, gm, f);
+	}
+	else
+		gd = rasterizer->getGlyphData(glyph);
+
 	int w = gd->getWidth();
 	int h = gd->getHeight();
 

src/modules/graphics/opengl/Font.h

 	FontType type;
 	Texture::Filter filter;
 
+	int textureX, textureY;
+	int rowHeight;
+
+	bool useSpacesAsTab;
+
 	static const int NUM_TEXTURE_SIZES = 7;
 	static const int TEXTURE_WIDTHS[NUM_TEXTURE_SIZES];
 	static const int TEXTURE_HEIGHTS[NUM_TEXTURE_SIZES];
 
 	static const int TEXTURE_PADDING = 1;
 
-	int textureX, textureY;
-	int rowHeight;
+	// This will be used if the Rasterizer doesn't have a tab character itself.
+	static const int SPACES_PER_TAB = 4;
 
 }; // Font