Skip to content

Commit

Permalink
use regex to strip html tags. now works with nesting bold, and italic
Browse files Browse the repository at this point in the history
  • Loading branch information
daelsepara committed Sep 8, 2021
1 parent 10d0fea commit 6ce6f9e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 139 deletions.
10 changes: 5 additions & 5 deletions src/LegendaryKingdoms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26168,7 +26168,7 @@ bool processStory(SDL_Window *window, SDL_Renderer *renderer, Party::Base &party
}
else if (story->Text)
{
text = Glyphs::createText(story->Text, FONT_GARAMOND, font_size, clrDB, listwidth);
text = Glyphs::FormattedText(story->Text, FONT_GARAMOND, font_size, clrDB, listwidth);
}

auto compact = (text && text->h <= text_bounds - 2 * text_space) || !text;
Expand Down Expand Up @@ -27391,7 +27391,7 @@ bool encyclopediaScreen(SDL_Window *window, SDL_Renderer *renderer, Book::Type b
}
else if (Topics::ALL[topic].Text.length() > 0)
{
text = Glyphs::createText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
text = Glyphs::FormattedText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
}

auto compact = (text && text->h <= (text_bounds - 2 * text_space - infoh)) || !text;
Expand Down Expand Up @@ -27629,7 +27629,7 @@ bool encyclopediaScreen(SDL_Window *window, SDL_Renderer *renderer, Book::Type b
}
else if (Topics::ALL[topic].Text.length() > 0)
{
text = Glyphs::createText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
text = Glyphs::FormattedText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
}

if (splash)
Expand Down Expand Up @@ -27699,7 +27699,7 @@ bool encyclopediaScreen(SDL_Window *window, SDL_Renderer *renderer, Book::Type b
}
else if (Topics::ALL[topic].Text.length() > 0)
{
text = Glyphs::createText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
text = Glyphs::FormattedText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
}

if (splash)
Expand Down Expand Up @@ -27771,7 +27771,7 @@ bool encyclopediaScreen(SDL_Window *window, SDL_Renderer *renderer, Book::Type b
}
else if (Topics::ALL[topic].Text.length() > 0)
{
text = Glyphs::createText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
text = Glyphs::FormattedText(Topics::ALL[topic].Text.c_str(), FONT_GARAMOND, font_size, clrDB, listwidth);
}

if (splash)
Expand Down
4 changes: 2 additions & 2 deletions src/LegendaryKingdoms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ SDL_Surface *createTextAndImage(const char *text, const char *image, const char

auto image_surface = createImage(image);

auto text_surface = Glyphs::createText(text, ttf, font_size, textColor, wrap);
auto text_surface = Glyphs::FormattedText(text, ttf, font_size, textColor, wrap);

if (image_surface && text_surface)
{
Expand Down Expand Up @@ -651,7 +651,7 @@ SDL_Surface *formattedHeaderButton(SDL_Window *window, const char *font, int fon
auto button = SDL_CreateRGBSurface(0, w, h, 32, bg == 0 ? 0x000000FF : 0, bg == 0 ? 0x0000FF00 : 0, bg == 0 ? 0x00FF0000 : 0, bg == 0 ? 0xFF000000 : 0);
#endif

auto text_surface = Glyphs::createText(text, font, font_size, color, w);
auto text_surface = Glyphs::FormattedText(text, font, font_size, color, w);

if (button && text_surface)
{
Expand Down
189 changes: 63 additions & 126 deletions src/glyphs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cstring>
#include <iostream>
#include <regex>

#include <SDL.h>
#include <SDL_ttf.h>
Expand All @@ -11,23 +12,34 @@

namespace Glyphs
{
// https://stackoverflow.com/questions/49333136/removing-html-tags-from-a-string-of-text
// Probably a slow implementation
void Sanitize(std::string &str)
{
for (auto a = 0; a < str.length(); a++)
std::regex tags("<[^<]*>");

str = std::regex_replace(str, tags, "");
}

// TODO: Use faster implementation, i.e. Render from Glyph Atlas with Kernering
void RenderText(const char *text, TTF_Font *font, SDL_Color textColor, SDL_Surface *surface, int x, int y)
{
if (font)
{
if (str[a] == '<')
SDL_Rect dst;

dst.w = surface->w;
dst.h = surface->h;
dst.x = x;
dst.y = y;

auto text_surface = TTF_RenderText_Blended(font, text, textColor);

if (text_surface)
{
for (int b = a; b < str.length(); b++)
{
if (str[b] == '>')
{
str.erase(a, (b - a + 1));
SDL_BlitSurface(text_surface, NULL, surface, &dst);

break;
}
}
SDL_FreeSurface(text_surface);

text_surface = NULL;
}
}
}
Expand Down Expand Up @@ -76,10 +88,10 @@ namespace Glyphs

if (c == '\r' || c == '\n' || c == '\t' || c == ' ')
{
auto subw = 0;

if (word)
{
auto subw = 0;

auto sub = copy.substr(start, i - start);

Glyphs::Sanitize(sub);
Expand All @@ -100,55 +112,32 @@ namespace Glyphs
{
x += subw;
}
}

if (c == '\t' || c == ' ')
{
x += space;

if (x > width)
{
x = 0;

lines += 1;
}
}
else if (c == '\n' || c == '\r')
{
x = 0;

lines += 1;
}

if (closeBold)
{
isBold = false;

closeBold = false;
}
if (c == '\t' || c == ' ')
{
x += space;
}

if (closeItalic)
{
isItalic = false;
if (x > width || c == '\n' || c == '\r')
{
x = 0;

closeItalic = false;
}
lines += 1;
}
else if (c == ' ' || c == '\t')
{
x += space;

if (x > width)
{
x = 0;
if (closeBold)
{
isBold = false;

lines += 1;
}
closeBold = false;
}
else if (c == '\n' || c == '\r')

if (closeItalic)
{
x = 0;
isItalic = false;

lines += 1;
closeItalic = false;
}
}
else if (c == '<')
Expand Down Expand Up @@ -223,32 +212,7 @@ namespace Glyphs
}
}

// TODO: Use faster implementation, i.e. Render from Glyph Atlas with Kernering
void RenderText(const char *text, TTF_Font *font, SDL_Color textColor, SDL_Surface *surface, int x, int y)
{
if (font)
{
SDL_Rect dst;

dst.w = surface->w;
dst.h = surface->h;
dst.x = x;
dst.y = y;

auto text_surface = TTF_RenderText_Blended(font, text, textColor);

if (text_surface)
{
SDL_BlitSurface(text_surface, NULL, surface, &dst);

SDL_FreeSurface(text_surface);

text_surface = NULL;
}
}
}

SDL_Surface *createText(const char *text, const char *ttf, int font_size, SDL_Color textColor, int wrap)
SDL_Surface *FormattedText(const char *text, const char *ttf, int font_size, SDL_Color textColor, int wrap)
{
TTF_Init();

Expand Down Expand Up @@ -346,70 +310,43 @@ namespace Glyphs

lines += 1;

y = lines * skip;

Glyphs::RenderText(sub.c_str(), font, textColor, surface, 0, y);
Glyphs::RenderText(sub.c_str(), font, textColor, surface, 0, lines * skip);
}
else
{
Glyphs::RenderText(sub.c_str(), font, textColor, surface, x, y);

x += subw;
}

if (c == '\t' || c == ' ')
{
x += space;

if (x > width)
{
x = 0;

lines += 1;
}
}
else if (c == '\n' || c == '\r')
{
x = 0;

lines += 1;
}

y = lines * skip;

if (closeBold)
{
isBold = false;

closeBold = false;
}

if (closeItalic)
{
isItalic = false;

closeItalic = false;
}
}
else if (c == '\t' || c == ' ')

if (c == '\t' || c == ' ')
{
x += space;

if (x > width)
{
x = 0;

lines += 1;
}
}
else if (c == '\n' || c == '\r')

if (x > width || c == '\n' || c == '\r')
{
x = 0;

lines += 1;
}

y = lines * skip;

if (closeBold)
{
isBold = false;

closeBold = false;
}

if (closeItalic)
{
isItalic = false;

closeItalic = false;
}
}
else if (c == '<')
{
Expand Down
Loading

0 comments on commit 6ce6f9e

Please sign in to comment.