Skip to content

Commit

Permalink
font: Drop some code paths that have already been transitioned
Browse files Browse the repository at this point in the history
This impacts font::draw_text(), font::text_area(),
font::word_wrap_text().
  • Loading branch information
irydacea committed Mar 13, 2021
1 parent ec94da3 commit ffffd07
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 247 deletions.
203 changes: 0 additions & 203 deletions src/font/marked-up_text.cpp
Expand Up @@ -144,68 +144,6 @@ std::string del_tags(const std::string& text){
return utils::join(lines, "\n");
}

SDL_Rect text_area(const std::string& text, int size, int style)
{
const SDL_Rect area {0,0,10000,10000};
return draw_text(nullptr, area, size, font::NORMAL_COLOR, text, 0, 0, false, style);
}

SDL_Rect draw_text(surface& dst, const SDL_Rect& area, int size,
const color_t& color, const std::string& txt,
int x, int y, bool use_tooltips, int style)
{
// Make sure there's always at least a space,
// so we can ensure that we can return a rectangle for height
static const std::string blank_text(" ");
const std::string& text = txt.empty() ? blank_text : txt;

SDL_Rect res;
res.x = x;
res.y = y;
res.w = 0;
res.h = 0;

std::string::const_iterator i1 = text.begin();
std::string::const_iterator i2 = std::find(i1,text.end(),'\n');
while(true) {
color_t col = color;
int sz = size;
int text_style = style;

i1 = parse_markup(i1,i2,&sz,&col,&text_style);

if(i1 != i2) {
std::string new_string = utils::unescape(std::string(i1, i2));

const SDL_Rect rect = draw_text_line(dst, area, sz, col, new_string, x, y, use_tooltips, text_style);
if(rect.w > res.w) {
res.w = rect.w;
}

res.h += rect.h;
y += rect.h;
}

if(i2 == text.end()) {
break;
}

i1 = i2+1;
i2 = std::find(i1,text.end(),'\n');
}

return res;
}

SDL_Rect draw_text(CVideo* gui, const SDL_Rect& area, int size,
const color_t& color, const std::string& txt,
int x, int y, bool use_tooltips, int style)
{
surface null_surf = surface(nullptr);

return draw_text(gui != nullptr ? gui->getSurface() : null_surf, area, size, color, txt, x, y, use_tooltips, style);
}

bool is_format_char(char c)
{
switch(c) {
Expand Down Expand Up @@ -278,30 +216,6 @@ bool is_cjk_char(const char32_t ch)
//Halfwidth and Fullwidth Forms
(ch >= 0xff00 && ch < 0xffef);
}
static void cut_word(std::string& line, std::string& word, int font_size, int style, int max_width)
{
std::string tmp = line;
utf8::iterator tc(word);
bool first = true;
font_size = preferences::font_scaled(font_size);

for(;tc != utf8::iterator::end(word); ++tc) {
tmp.append(tc.substr().first, tc.substr().second);
SDL_Rect tsize = line_size(tmp, font_size, style);
if(tsize.w > max_width) {
const std::string& w = word;
if(line.empty() && first) {
line += std::string(w.begin(), tc.substr().second);
word = std::string(tc.substr().second, w.end());
} else {
line += std::string(w.begin(), tc.substr().first);
word = std::string(tc.substr().first, w.end());
}
break;
}
first = false;
}
}

namespace {

Expand Down Expand Up @@ -396,121 +310,4 @@ inline bool break_after(const char32_t ch)

} // end of anon namespace

std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
int max_width, int max_height, int max_lines, bool partial_line)
{
VALIDATE(max_width > 0, _("The maximum text width is less than 1."));

utf8::iterator ch(unwrapped_text);
std::string current_word;
std::string current_line;
std::size_t line_width = 0;
std::size_t current_height = 0;
bool line_break = false;
bool first = true;
bool start_of_line = true;
std::string wrapped_text;
std::string format_string;
color_t color;
int font_sz = font_size;
int style = TTF_STYLE_NORMAL;
utf8::iterator end = utf8::iterator::end(unwrapped_text);

while(true) {
if(start_of_line) {
line_width = 0;
format_string.clear();
while(ch != end && *ch < static_cast<char32_t>(0x100)
&& is_format_char(*ch) && !ch.next_is_end()) {

format_string.append(ch.substr().first, ch.substr().second);
++ch;
}
// We need to parse the special format characters
// to give the proper font_size and style to line_size()
font_sz = font_size;
style = TTF_STYLE_NORMAL;
parse_markup(format_string.begin(),format_string.end(),&font_sz,&color,&style);
current_line.clear();
start_of_line = false;
}

// If there is no current word, get one
if(current_word.empty() && ch == end) {
break;
} else if(current_word.empty()) {
if(*ch == ' ' || *ch == '\n') {
current_word = *ch;
++ch;
} else {
char32_t previous = 0;
for(;ch != utf8::iterator::end(unwrapped_text) &&
*ch != ' ' && *ch != '\n'; ++ch) {

if(!current_word.empty() &&
break_before(*ch) &&
!no_break_after(previous))
break;

if(!current_word.empty() &&
break_after(previous) &&
!no_break_before(*ch))
break;

current_word.append(ch.substr().first, ch.substr().second);

previous = *ch;
}
}
}

if(current_word == "\n") {
line_break = true;
current_word.clear();
start_of_line = true;
} else {

const std::size_t word_width = line_size(current_word, preferences::font_scaled(font_sz), style).w;

line_width += word_width;

if(static_cast<long>(line_width) > max_width) {
if (!partial_line && static_cast<long>(word_width) > max_width) {
cut_word(current_line,
current_word, font_sz, style, max_width);
}
if(current_word == " ")
current_word = "";
line_break = true;
} else {
current_line += current_word;
current_word = "";
}
}

if(line_break || (current_word.empty() && ch == end)) {
SDL_Rect size = line_size(current_line, preferences::font_scaled(font_sz), style);
if(max_height > 0 && current_height + size.h >= std::size_t(max_height)) {
return wrapped_text;
}

if(!first) {
wrapped_text += '\n';
}

wrapped_text += format_string + current_line;
current_line.clear();
line_width = 0;
current_height += size.h;
line_break = false;
first = false;

if(--max_lines == 0) {
return wrapped_text;
}
}
}
return wrapped_text;
}

} // end namespace font
44 changes: 0 additions & 44 deletions src/font/marked-up_text.hpp
Expand Up @@ -35,40 +35,6 @@ std::string::const_iterator parse_markup(std::string::const_iterator i1,
int* font_size,
color_t* color, int* style);

/**
* Function to draw text on a surface.
*
* The text will be clipped to area. If the text runs outside of area
* horizontally, an ellipsis will be displayed at the end of it.
*
* If use_tooltips is true, then text with an ellipsis will have a tooltip
* set for it equivalent to the entire contents of the text.
*
* Some very basic 'markup' will be done on the text:
* - any line beginning in # will be displayed in BAD_COLOR (red)
* - any line beginning in @ will be displayed in GOOD_COLOR (green)
* - any line beginning in + will be displayed with size increased by 2
* - any line beginning in - will be displayed with size decreased by 2
* - any line beginning with 0x0n will be displayed in the color of side n
*
* The above special characters can be quoted using a C-style backslash.
*
* A bounding rectangle of the text is returned. If dst is nullptr, then the
* text will not be drawn, and a bounding rectangle only will be returned.
*/
SDL_Rect draw_text(surface& dst, const SDL_Rect& area, int size,
const color_t& color, const std::string& text,
int x, int y, bool use_tooltips = false, int style = 0);

/** wrapper of the previous function, gui can also be nullptr */
SDL_Rect draw_text(CVideo* gui, const SDL_Rect& area, int size,
const color_t& color, const std::string& text,
int x, int y, bool use_tooltips = false, int style = 0);


/** Calculate the size of a text (in pixels) if it were to be drawn. */
SDL_Rect text_area(const std::string& text, int size, int style=0);

/** Copy string, but without tags at the beginning */
std::string del_tags(const std::string& text);

Expand All @@ -88,14 +54,4 @@ bool is_format_char(char c);
*/
bool is_cjk_char(const char32_t ch);

/**
* Wrap text.
*
* - If the text exceeds the specified max width, wrap it on a word basis.
* - If this is not possible, e.g. the word is too big to fit, wrap it on a
* - char basis.
*/
std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
int max_width, int max_height = -1, int max_lines = -1, bool partial_line = false);

} // end namespace font

0 comments on commit ffffd07

Please sign in to comment.