wesnoth-cvs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-cvs-commits] wesnoth/src font.cpp font.hpp help.cpp


From: David White
Subject: [Wesnoth-cvs-commits] wesnoth/src font.cpp font.hpp help.cpp
Date: Sat, 14 May 2005 15:18:31 -0400

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     David White <address@hidden>    05/05/14 19:18:31

Modified files:
        src            : font.cpp font.hpp help.cpp 

Log message:
        made help system faster and fixed formatting problems with it

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/font.cpp.diff?tr1=1.140&tr2=1.141&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/font.hpp.diff?tr1=1.55&tr2=1.56&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/help.cpp.diff?tr1=1.96&tr2=1.97&r1=text&r2=text

Patches:
Index: wesnoth/src/font.cpp
diff -u wesnoth/src/font.cpp:1.140 wesnoth/src/font.cpp:1.141
--- wesnoth/src/font.cpp:1.140  Tue May 10 22:15:57 2005
+++ wesnoth/src/font.cpp        Sat May 14 19:18:31 2005
@@ -1,4 +1,4 @@
-/* $Id: font.cpp,v 1.140 2005/05/10 22:15:57 Sirp Exp $ */
+/* $Id: font.cpp,v 1.141 2005/05/14 19:18:31 Sirp Exp $ */
 /* vim:set encoding=utf-8: */
 /*
    Copyright (C) 2003 by David White <address@hidden>
@@ -93,7 +93,13 @@
        ucs2_string ucs2_text;
 };
 
-std::vector<subset_id> font_map;
+std::vector<subset_id> font_map;
+
+//cache sizes of small text
+typedef std::map<std::string,SDL_Rect> line_size_cache_map;
+
+//map of styles -> sizes -> cache
+std::map<int,std::map<int,line_size_cache_map> > line_size_cache;
 
 //Splits the UTF-8 text into text_chunks using the same font.
 std::vector<text_chunk> split_text(std::string const & utf8_text) {
@@ -213,7 +219,8 @@
 
        font_table.clear();
        font_names.clear();
-       font_map.clear();
+       font_map.clear();
+       line_size_cache.clear();
 }
 
 struct font_style_setter
@@ -851,22 +858,33 @@
 }
 
 int line_width(const std::string& line, int font_size, int style)
-{
-       const SDL_Color col = { 0, 0, 0, 0 };
-       text_surface s(line, font_size, col, style);
-
-       return s.width();
+{
+       return line_size(line,font_size,style).w;
 }
 
 SDL_Rect line_size(const std::string& line, int font_size, int style)
-{
+{
+       const size_t max_cache_size = 12;
+       line_size_cache_map& cache = line_size_cache[style][font_size];
+
+       if(line.size() < max_cache_size) {
+               const line_size_cache_map::const_iterator i = cache.find(line);
+               if(i != cache.end()) {
+                       return i->second;
+               }
+       }
+
        SDL_Rect res;
 
        const SDL_Color col = { 0, 0, 0, 0 };
        text_surface s(line, font_size, col, style);
 
        res.w = s.width();
-       res.h = s.height();
+       res.h = s.height();
+
+       if(line.size() < max_cache_size) {
+               cache.insert(std::pair<std::string,SDL_Rect>(line,res));
+       }
 
        return res;
 }
@@ -956,11 +974,14 @@
 }
 }
 
-std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 
int max_width, int max_height)
-{
+std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 
int max_width, int max_height, int max_lines)
+{
+       assert(max_width > 0);
+
        utils::utf8_iterator ch(unwrapped_text);
        std::string current_word;
-       std::string current_line;
+       std::string current_line;
+       size_t line_width = 0;
        size_t current_height = 0;
        bool line_break = false;
        bool first = true;
@@ -970,7 +991,8 @@
        utils::utf8_iterator end = utils::utf8_iterator::end(unwrapped_text);
 
        while(1) {
-               if(start_of_line) {
+               if(start_of_line) {
+                       line_width = 0;
                        format_string = "";
                        while(ch != end && *ch < 0x100U && is_format_char(*ch)) 
{
                                format_string.append(ch.substr().first, 
ch.substr().second);
@@ -1007,18 +1029,22 @@
                                        previous = *ch;
                                }
                        }
-               }
+               }
 
                if(current_word == "\n") {
                        line_break = true;
                        current_word = "";
                        start_of_line = true;
-               } else {
-                       SDL_Rect size = line_size(current_line + current_word, 
font_size);
+               } else {
+
+                       const std::string word = format_string + current_word;
+
+                       const size_t word_width = line_size(word,font_size).w;
 
-                       if(size.w > max_width) {
-                               SDL_Rect wsize = line_size(current_word, 
font_size);
-                               if(wsize.w > max_width) {
+                       line_width += word_width;
+
+                       if(line_width > max_width) {
+                               if(word_width > max_width) {
                                        cut_word(current_line, current_word, 
font_size, max_width);
                                }
                                if(current_word == " ")
@@ -1032,16 +1058,24 @@
 
                if(line_break || current_word.empty() && ch == end) {
                        SDL_Rect size = line_size(current_line, font_size);
-                       if(max_height > 0 && current_height + size.h >= 
size_t(max_height))
-                               return wrapped_text;
+                       if(max_height > 0 && current_height + size.h >= 
size_t(max_height)) {
+                               return wrapped_text;
+                       }
+
+                       if(!first) {
+                               wrapped_text += '\n';
+                       }
 
-                       if(!first)
-                               wrapped_text += '\n';
                        wrapped_text += current_line;
-                       current_line = format_string;
+                       current_line = format_string;
+                       line_width = 0;
                        current_height += size.h;
                        line_break = false;
-                       first = false;
+                       first = false;
+
+                       if(--max_lines == 0) {
+                               return wrapped_text;
+                       }
                }
        }
        return wrapped_text;
Index: wesnoth/src/font.hpp
diff -u wesnoth/src/font.hpp:1.55 wesnoth/src/font.hpp:1.56
--- wesnoth/src/font.hpp:1.55   Sat May  7 21:53:42 2005
+++ wesnoth/src/font.hpp        Sat May 14 19:18:31 2005
@@ -1,4 +1,4 @@
-/* $Id: font.hpp,v 1.55 2005/05/07 21:53:42 ettin Exp $ */
+/* $Id: font.hpp,v 1.56 2005/05/14 19:18:31 Sirp Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -124,7 +124,7 @@
 /// If the 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);
+std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 
int max_width, int max_height=-1, int max_lines=-1);
 
 ///
 /// If the text excedes the specified max width, end it with an ellipsis (...)
Index: wesnoth/src/help.cpp
diff -u wesnoth/src/help.cpp:1.96 wesnoth/src/help.cpp:1.97
--- wesnoth/src/help.cpp:1.96   Tue Apr 26 20:45:12 2005
+++ wesnoth/src/help.cpp        Sat May 14 19:18:31 2005
@@ -1907,21 +1907,12 @@
                surface surf(font::get_rendered_text(first_part, font_size, 
color, state));
                if (!surf.null())
                        add_item(item(surf, curr_loc_.first, curr_loc_.second, 
first_part, ref_dst));
-               if (parts.size() > 1) {
-                       // Parts remain, remove the first part from the string 
and
-                       // add the remaining parts.
-                       std::string s = text;
-                       s.erase(0, first_part.size());
-                       if (s.length() < 1) {
-                               return;
-                       }
+               if (parts.size() > 1) {
+
+                       std::string& s = parts.back();
+
                        const std::string first_word_before = get_first_word(s);
                        const std::string first_word_after = 
get_first_word(remove_first_space(s));
-                       //std::cout << "before: '" << first_word_before << "'\n"
-                       //                << "after: '" << first_word_after << 
"'\n"
-                       //                << "before linewidth: " << 
font::line_width(first_word_before, font_size)
-                       //                << "\nafter linewidth: " << 
font::line_width(first_word_after, font_size)
-                       //                << "\nremaining width: " << 
get_remaining_width() << std::endl;
                        if (get_remaining_width() >= 
font::line_width(first_word_after, font_size, state)
                                && get_remaining_width()
                                < font::line_width(first_word_before, 
font_size, state)) {
@@ -2512,10 +2503,15 @@
 
 std::vector<std::string> split_in_width(const std::string &s, const int 
font_size,
                const unsigned width)
-{
-       std::string wrapped = font::word_wrap_text(s, font_size, width);
-       std::vector<std::string> parts = utils::split(wrapped, '\n', 0);
-       return parts;
+{
+       std::vector<std::string> res;
+       const std::string& first_line = font::word_wrap_text(s, font_size, 
width, -1, 1);
+       res.push_back(first_line);
+       if(s.size() > first_line.size()) {
+               res.push_back(s.substr(first_line.size()));
+       }
+
+       return res;
 }
 
 std::string remove_first_space(const std::string& text)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]