Skip to content

Commit

Permalink
- got rid of most of the font system's baggage that's no longer needed.
Browse files Browse the repository at this point in the history
Mainly this means that it's no longer necessary to reorder the palette to get working translated glyphs, so the FFontChar1 class and the TranslatedPic member and its invasive handling could all be cleaned out.
All font operations now take place on true color data, with the sole execption of FSpecialFont's 'notranslate' handling.
  • Loading branch information
coelckers committed May 25, 2021
1 parent 9769f34 commit 8b1757e
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 155 deletions.
15 changes: 6 additions & 9 deletions source/common/2d/v_drawtext.cpp
Expand Up @@ -173,9 +173,8 @@ void DrawChar(F2DDrawer *drawer, FFont* font, int normalcolor, double x, double

FGameTexture* pic;
int dummy;
bool redirected;

if (NULL != (pic = font->GetChar(character, normalcolor, &dummy, &redirected)))
if (NULL != (pic = font->GetChar(character, normalcolor, &dummy)))
{
DrawParms parms;
Va_List tags;
Expand All @@ -188,7 +187,7 @@ void DrawChar(F2DDrawer *drawer, FFont* font, int normalcolor, double x, double
}
bool palettetrans = (normalcolor == CR_UNDEFINED && parms.TranslationId != 0);
PalEntry color = 0xffffffff;
if (!palettetrans) parms.TranslationId = redirected ? -1 : font->GetColorTranslation((EColorRange)normalcolor, &color);
if (!palettetrans) parms.TranslationId = font->GetColorTranslation((EColorRange)normalcolor, &color);
parms.color = PalEntry((color.a * parms.color.a) / 255, (color.r * parms.color.r) / 255, (color.g * parms.color.g) / 255, (color.b * parms.color.b) / 255);
drawer->AddTexture(pic, parms);
}
Expand All @@ -204,17 +203,16 @@ void DrawChar(F2DDrawer *drawer, FFont *font, int normalcolor, double x, double

FGameTexture *pic;
int dummy;
bool redirected;

if (NULL != (pic = font->GetChar(character, normalcolor, &dummy, &redirected)))
if (NULL != (pic = font->GetChar(character, normalcolor, &dummy)))
{
DrawParms parms;
uint32_t tag = ListGetInt(args);
bool res = ParseDrawTextureTags(drawer, pic, x, y, tag, args, &parms, false);
if (!res) return;
bool palettetrans = (normalcolor == CR_UNDEFINED && parms.TranslationId != 0);
PalEntry color = 0xffffffff;
if (!palettetrans) parms.TranslationId = redirected ? -1 : font->GetColorTranslation((EColorRange)normalcolor, &color);
if (!palettetrans) parms.TranslationId = font->GetColorTranslation((EColorRange)normalcolor, &color);
parms.color = PalEntry((color.a * parms.color.a) / 255, (color.r * parms.color.r) / 255, (color.g * parms.color.g) / 255, (color.b * parms.color.b) / 255);
drawer->AddTexture(pic, parms);
}
Expand Down Expand Up @@ -316,11 +314,10 @@ void DrawTextCommon(F2DDrawer *drawer, FFont *font, int normalcolor, double x, d
continue;
}

bool redirected = false;
if (NULL != (pic = font->GetChar(c, currentcolor, &w, &redirected)))
if (NULL != (pic = font->GetChar(c, currentcolor, &w)))
{
// if palette translation is used, font colors will be ignored.
if (!palettetrans) parms.TranslationId = redirected? -1 : trans;
if (!palettetrans) parms.TranslationId = trans;
SetTextureParms(drawer, &parms, pic, cx, cy);
if (parms.cellx)
{
Expand Down
57 changes: 10 additions & 47 deletions source/common/fonts/font.cpp
Expand Up @@ -333,23 +333,13 @@ FFont::FFont (const char *name, const char *nametemplate, const char *filetempla
TexMan.AddGameTexture(tex);
Chars[i].OriginalPic = tex;

if (!noTranslate)
{
Chars[i].TranslatedPic = MakeGameTexture(new FImageTexture(new FFontChar1(orig->GetImage())), nullptr, ETextureType::FontChar);
Chars[i].TranslatedPic->CopySize(pic, true);
TexMan.AddGameTexture(Chars[i].TranslatedPic);
}
else
{
Chars[i].TranslatedPic = tex;
}
if (sysCallbacks.FontCharCreated) sysCallbacks.FontCharCreated(pic, Chars[i].OriginalPic, Chars[i].TranslatedPic);
if (sysCallbacks.FontCharCreated) sysCallbacks.FontCharCreated(pic, Chars[i].OriginalPic, Chars[i].OriginalPic);

Chars[i].XMove = (int)Chars[i].TranslatedPic->GetDisplayWidth();
Chars[i].XMove = (int)Chars[i].OriginalPic->GetDisplayWidth();
}
else
{
Chars[i].TranslatedPic = nullptr;
Chars[i].OriginalPic = nullptr;
Chars[i].XMove = INT_MIN;
}
}
Expand All @@ -360,7 +350,7 @@ FFont::FFont (const char *name, const char *nametemplate, const char *filetempla
{
SpaceWidth = spacewidth;
}
else if ('N' - FirstChar >= 0 && 'N' - FirstChar < count && Chars['N' - FirstChar].TranslatedPic != nullptr)
else if ('N' - FirstChar >= 0 && 'N' - FirstChar < count && Chars['N' - FirstChar].OriginalPic != nullptr)
{
SpaceWidth = (Chars['N' - FirstChar].XMove + 1) / 2;
}
Expand Down Expand Up @@ -444,11 +434,7 @@ void FFont::ReadSheetFont(TArray<FolderEntry> &folderdata, int width, int height
Chars[i].OriginalPic = (*lump)->GetUseType() == ETextureType::FontChar? (*lump) : MakeGameTexture(pic, nullptr, ETextureType::FontChar);
Chars[i].OriginalPic->SetUseType(ETextureType::FontChar);
Chars[i].OriginalPic->CopySize(*lump, true);
Chars[i].TranslatedPic = MakeGameTexture(new FImageTexture(new FFontChar1(pic->GetImage())), nullptr, ETextureType::FontChar);
Chars[i].TranslatedPic->CopySize(*lump, true);
Chars[i].TranslatedPic->SetUseType(ETextureType::FontChar);
if (Chars[i].OriginalPic != *lump) TexMan.AddGameTexture(Chars[i].OriginalPic);
TexMan.AddGameTexture(Chars[i].TranslatedPic);
}
Chars[i].XMove = width;
}
Expand Down Expand Up @@ -596,16 +582,10 @@ void FFont::RecordAllTextureColors(uint32_t *usedcolors)
{
for (unsigned int i = 0; i < Chars.Size(); i++)
{
if (Chars[i].TranslatedPic)
if (Chars[i].OriginalPic)
{
FFontChar1 *pic = static_cast<FFontChar1 *>(Chars[i].TranslatedPic->GetTexture()->GetImage());
if (pic)
{
// The remap must be temporarily reset here because this can be called on an initialized font.
auto sr = pic->ResetSourceRemap();
RecordTextureColors(pic, usedcolors);
pic->SetSourceRemap(sr);
}
auto pic = Chars[i].OriginalPic->GetTexture()->GetImage();
if (pic) RecordTextureColors(pic, usedcolors);
}
}
}
Expand Down Expand Up @@ -772,7 +752,6 @@ int FFont::GetColorTranslation (EColorRange range, PalEntry *color) const
return -1;
else if (range >= NumTextColors)
range = CR_UNTRANSLATED;
//if (range == CR_UNTRANSLATED && !translateUntranslated) return nullptr;
return Translations[range];
}

Expand Down Expand Up @@ -867,7 +846,7 @@ int FFont::GetCharCode(int code, bool needpic) const
//
//==========================================================================

FGameTexture *FFont::GetChar (int code, int translation, int *const width, bool *redirected) const
FGameTexture *FFont::GetChar (int code, int translation, int *const width) const
{
code = GetCharCode(code, true);
int xmove = SpaceWidth;
Expand All @@ -885,24 +864,8 @@ FGameTexture *FFont::GetChar (int code, int translation, int *const width, bool
if (code < 0) return nullptr;


if ((translation == CR_UNTRANSLATED || translation == CR_UNDEFINED || translation >= NumTextColors) && !forceremap)
{
bool redirect = Chars[code].OriginalPic && Chars[code].OriginalPic != Chars[code].TranslatedPic;
if (redirected) *redirected = redirect;
if (redirect)
{
assert(Chars[code].OriginalPic->GetUseType() == ETextureType::FontChar);
return Chars[code].OriginalPic;
}
}
if (redirected) *redirected = false;
if (IsLuminosityTranslation(Translations[translation]))
{
assert(Chars[code].OriginalPic->GetUseType() == ETextureType::FontChar);
return Chars[code].OriginalPic;
}
assert(Chars[code].TranslatedPic->GetUseType() == ETextureType::FontChar);
return Chars[code].TranslatedPic;
assert(Chars[code].OriginalPic->GetUseType() == ETextureType::FontChar);
return Chars[code].OriginalPic;
}

//==========================================================================
Expand Down
10 changes: 4 additions & 6 deletions source/common/fonts/hexfont.cpp
Expand Up @@ -301,10 +301,9 @@ class FHexFont : public FFont
{
auto offset = hexdata.glyphmap[i];
int size = hexdata.glyphdata[offset] / 16;
Chars[i - FirstChar].TranslatedPic = MakeGameTexture(new FImageTexture(new FHexFontChar(&hexdata.glyphdata[offset + 1], size, size * 9, 16)), nullptr, ETextureType::FontChar);
Chars[i - FirstChar].OriginalPic = Chars[i - FirstChar].TranslatedPic;
Chars[i - FirstChar].OriginalPic = MakeGameTexture(new FImageTexture(new FHexFontChar(&hexdata.glyphdata[offset + 1], size, size * 9, 16)), nullptr, ETextureType::FontChar);
Chars[i - FirstChar].XMove = size * spacing;
TexMan.AddGameTexture(Chars[i - FirstChar].TranslatedPic);
TexMan.AddGameTexture(Chars[i - FirstChar].OriginalPic);
}
else Chars[i - FirstChar].XMove = spacing;

Expand Down Expand Up @@ -369,10 +368,9 @@ class FHexFont2 : public FFont
{
auto offset = hexdata.glyphmap[i];
int size = hexdata.glyphdata[offset] / 16;
Chars[i - FirstChar].TranslatedPic = MakeGameTexture(new FImageTexture(new FHexFontChar2(&hexdata.glyphdata[offset + 1], size, 2 + size * 8, 18)), nullptr, ETextureType::FontChar);
Chars[i - FirstChar].OriginalPic = Chars[i - FirstChar].TranslatedPic;
Chars[i - FirstChar].OriginalPic = MakeGameTexture(new FImageTexture(new FHexFontChar2(&hexdata.glyphdata[offset + 1], size, 2 + size * 8, 18)), nullptr, ETextureType::FontChar);
Chars[i - FirstChar].XMove = size * spacing;
TexMan.AddGameTexture(Chars[i - FirstChar].TranslatedPic);
TexMan.AddGameTexture(Chars[i - FirstChar].OriginalPic);
}
else Chars[i - FirstChar].XMove = spacing;

Expand Down
18 changes: 7 additions & 11 deletions source/common/fonts/singlelumpfont.cpp
Expand Up @@ -188,8 +188,8 @@ void FSingleLumpFont::LoadTranslations()

for(unsigned int i = 0;i < count;++i)
{
if(Chars[i].TranslatedPic)
static_cast<FFontChar2*>(Chars[i].TranslatedPic->GetTexture()->GetImage())->SetSourceRemap(Palette);
if(Chars[i].OriginalPic)
static_cast<FFontChar2*>(Chars[i].OriginalPic->GetTexture()->GetImage())->SetSourceRemap(Palette);
}

Translations.Resize(NumTextColors);
Expand Down Expand Up @@ -325,14 +325,12 @@ void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
Chars[i].XMove = widths2[i];
if (destSize <= 0)
{
Chars[i].TranslatedPic = nullptr;
Chars[i].OriginalPic = nullptr;
}
else
{
Chars[i].TranslatedPic = MakeGameTexture(new FImageTexture(new FFontChar2 (lump, int(data_p - data), widths2[i], FontHeight)), nullptr, ETextureType::FontChar);
Chars[i].OriginalPic = Chars[i].TranslatedPic;
TexMan.AddGameTexture(Chars[i].TranslatedPic);
Chars[i].OriginalPic = MakeGameTexture(new FImageTexture(new FFontChar2 (lump, int(data_p - data), widths2[i], FontHeight)), nullptr, ETextureType::FontChar);
TexMan.AddGameTexture(Chars[i].OriginalPic);
do
{
int8_t code = *data_p++;
Expand Down Expand Up @@ -445,7 +443,6 @@ void FSingleLumpFont::LoadBMF(int lump, const uint8_t *data)
-(int8_t)chardata[chari+3], // x offset
-(int8_t)chardata[chari+4] // y offset
)), nullptr, ETextureType::FontChar);
Chars[chardata[chari] - FirstChar].TranslatedPic = tex;
Chars[chardata[chari] - FirstChar].OriginalPic = tex;
TexMan.AddGameTexture(tex);
}
Expand Down Expand Up @@ -488,12 +485,11 @@ void FSingleLumpFont::CheckFON1Chars()
{
int destSize = SpaceWidth * FontHeight;

if (!Chars[i].TranslatedPic)
if (!Chars[i].OriginalPic)
{
Chars[i].TranslatedPic = MakeGameTexture(new FImageTexture(new FFontChar2(Lump, int(data_p - data), SpaceWidth, FontHeight)), nullptr, ETextureType::FontChar);
Chars[i].OriginalPic = Chars[i].TranslatedPic;
Chars[i].OriginalPic = MakeGameTexture(new FImageTexture(new FFontChar2(Lump, int(data_p - data), SpaceWidth, FontHeight)), nullptr, ETextureType::FontChar);
Chars[i].XMove = SpaceWidth;
TexMan.AddGameTexture(Chars[i].TranslatedPic);
TexMan.AddGameTexture(Chars[i].OriginalPic);
}

// Advance to next char's data and count the used colors.
Expand Down
5 changes: 2 additions & 3 deletions source/common/fonts/singlepicfont.cpp
Expand Up @@ -45,7 +45,7 @@ class FSinglePicFont : public FFont
FSinglePicFont(const char *picname);

// FFont interface
FGameTexture *GetChar(int code, int translation, int *const width, bool *redirected = nullptr) const override;
FGameTexture *GetChar(int code, int translation, int *const width) const override;
int GetCharWidth (int code) const override;

protected:
Expand Down Expand Up @@ -94,10 +94,9 @@ FSinglePicFont::FSinglePicFont(const char *picname) :
//
//==========================================================================

FGameTexture *FSinglePicFont::GetChar (int code, int translation, int *const width, bool *redirected) const
FGameTexture *FSinglePicFont::GetChar (int code, int translation, int *const width) const
{
*width = SpaceWidth;
if (redirected) *redirected = false;
if (code == 'a' || code == 'A')
{
return TexMan.GetGameTexture(PicNum, true);
Expand Down
16 changes: 4 additions & 12 deletions source/common/fonts/specialfont.cpp
Expand Up @@ -108,26 +108,18 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FGameTexture
Chars[i].OriginalPic = MakeGameTexture(pic->GetTexture(), nullptr, ETextureType::FontChar);
Chars[i].OriginalPic->CopySize(pic, true);
TexMan.AddGameTexture(Chars[i].OriginalPic);

if (!noTranslate)
{
Chars[i].TranslatedPic = MakeGameTexture(new FImageTexture(new FFontChar1 (charlumps[i]->GetTexture()->GetImage())), nullptr, ETextureType::FontChar);
Chars[i].TranslatedPic->CopySize(charlumps[i], true);
TexMan.AddGameTexture(Chars[i].TranslatedPic);
}
else Chars[i].TranslatedPic = Chars[i].OriginalPic;
Chars[i].XMove = (int)Chars[i].TranslatedPic->GetDisplayWidth();
if (sysCallbacks.FontCharCreated) sysCallbacks.FontCharCreated(pic, Chars[i].OriginalPic, Chars[i].TranslatedPic);
Chars[i].XMove = (int)Chars[i].OriginalPic->GetDisplayWidth();
if (sysCallbacks.FontCharCreated) sysCallbacks.FontCharCreated(pic, Chars[i].OriginalPic, Chars[i].OriginalPic);
}
else
{
Chars[i].TranslatedPic = nullptr;
Chars[i].OriginalPic = nullptr;
Chars[i].XMove = INT_MIN;
}
}

// Special fonts normally don't have all characters so be careful here!
if ('N'-first >= 0 && 'N'-first < count && Chars['N' - first].TranslatedPic != nullptr)
if ('N'-first >= 0 && 'N'-first < count && Chars['N' - first].OriginalPic != nullptr)
{
SpaceWidth = (Chars['N' - first].XMove + 1) / 2;
}
Expand Down
5 changes: 2 additions & 3 deletions source/common/fonts/v_font.h
Expand Up @@ -97,7 +97,7 @@ class FFont
FFont (const char *fontname, const char *nametemplate, const char *filetemplate, int first, int count, int base, int fdlump, int spacewidth=-1, bool notranslate = false, bool iwadonly = false, bool doomtemplate = false, GlyphSet *baseGlpyphs = nullptr);
virtual ~FFont ();

virtual FGameTexture *GetChar (int code, int translation, int *const width, bool *redirected = nullptr) const;
virtual FGameTexture *GetChar (int code, int translation, int *const width) const;
virtual int GetCharWidth (int code) const;
int GetColorTranslation (EColorRange range, PalEntry *color = nullptr) const;
int GetLump() const { return Lump; }
Expand Down Expand Up @@ -159,8 +159,7 @@ class FFont
bool forceremap = false;
struct CharData
{
FGameTexture *TranslatedPic = nullptr; // Texture for use with font translations.
FGameTexture *OriginalPic = nullptr; // Texture for use with CR_UNTRANSLATED or font colorization.
FGameTexture *OriginalPic = nullptr;
int XMove = INT_MIN;
};
TArray<CharData> Chars;
Expand Down
42 changes: 0 additions & 42 deletions source/common/textures/formats/fontchars.cpp
Expand Up @@ -41,48 +41,6 @@
#include "fontchars.h"
#include "engineerrors.h"

//==========================================================================
//
// FFontChar1 :: FFontChar1
//
// Used by fonts made from textures.
//
//==========================================================================

FFontChar1::FFontChar1(FImageSource* sourcelump)
: BaseTexture(sourcelump), SourceRemap(nullptr)
{
// now copy all the properties from the base texture
assert(BaseTexture != nullptr);
CopySize(*BaseTexture);
bUseGamePalette = false;
}

//==========================================================================
//
// FFontChar1 :: GetPixels
//
// Render style is not relevant for fonts. This must not use it!
//
//==========================================================================

TArray<uint8_t> FFontChar1::CreatePalettedPixels(int)
{
// Make the texture as normal, then remap it so that all the colors
// are at the low end of the palette
// Why? It only creates unnecessary work!
auto Pixels = BaseTexture->GetPalettedPixels(normal);

if (SourceRemap)
{
for (int x = 0; x < Width * Height; ++x)
{
Pixels[x] = SourceRemap[Pixels[x]];
}
}
return Pixels;
}

//==========================================================================
//
// FFontChar2 :: FFontChar2
Expand Down
16 changes: 0 additions & 16 deletions source/common/textures/formats/fontchars.h
@@ -1,21 +1,5 @@


// This is a font character that loads a texture and recolors it.
class FFontChar1 : public FImageSource
{
public:
FFontChar1 (FImageSource *sourcelump);
TArray<uint8_t> CreatePalettedPixels(int conversion) override;
void SetSourceRemap(const uint8_t *sourceremap) { SourceRemap = sourceremap; }
const uint8_t *ResetSourceRemap() { auto p = SourceRemap; SourceRemap = nullptr; return p; }
FImageSource *GetBase() const { return BaseTexture; }

protected:

FImageSource *BaseTexture;
const uint8_t *SourceRemap;
};

// This is a font character that reads RLE compressed data.
class FFontChar2 : public FImageSource
{
Expand Down
7 changes: 1 addition & 6 deletions source/core/statusbar2.cpp
Expand Up @@ -191,12 +191,7 @@ void UpdateStatusBar(SummaryInfo* info)
for (int i = 0; i < NumTextColors; i++)
{
FStringf buffer("This is font color %d", i);
//DrawText(twod, SmallFont, i, 40, i * 15, buffer, DTA_FullscreenScale, FSMode_Fit640x400, TAG_DONE);
//DrawText(twod, V_GetFont("doomfont"), i, 40, i * 15, buffer, DTA_FullscreenScale, FSMode_Fit640x400, TAG_DONE);
//DrawText(twod, NewSmallFont, i, 340, i * 15, buffer, DTA_FullscreenScale, FSMode_Fit640x400, TAG_DONE);

DrawText(twod, V_GetFont("condfont"), i, 40, i * 15, buffer, DTA_FullscreenScale, FSMode_Fit640x400, TAG_DONE);
DrawText(twod, NewConsoleFont, i, 340, i * 15, buffer, DTA_FullscreenScale, FSMode_Fit640x400, TAG_DONE);
DrawText(twod, BigFont, i, 340, i * 15, buffer, DTA_FullscreenScale, FSMode_Fit640x400, TAG_DONE);

}
#endif
Expand Down

0 comments on commit 8b1757e

Please sign in to comment.