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 show_dialog.cpp


From: Philippe Plantier
Subject: [Wesnoth-cvs-commits] wesnoth/src font.cpp font.hpp show_dialog.cpp
Date: Sat, 02 Apr 2005 09:06:35 -0500

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     Philippe Plantier <address@hidden>      05/04/02 14:06:35

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

Log message:
        Made dialog text have a maximal height as well as a maximal width.
        Made dialog text use word_wrap_text instead of text_to_lines to wrap 
text.
        Rewrote word_wrap_text to support the notion of maximal height.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/font.cpp.diff?tr1=1.128&tr2=1.129&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/font.hpp.diff?tr1=1.53&tr2=1.54&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/show_dialog.cpp.diff?tr1=1.120&tr2=1.121&r1=text&r2=text

Patches:
Index: wesnoth/src/font.cpp
diff -u wesnoth/src/font.cpp:1.128 wesnoth/src/font.cpp:1.129
--- wesnoth/src/font.cpp:1.128  Sat Mar 26 17:10:32 2005
+++ wesnoth/src/font.cpp        Sat Apr  2 14:06:35 2005
@@ -1,4 +1,4 @@
-/* $Id: font.cpp,v 1.128 2005/03/26 17:10:32 silene Exp $ */
+/* $Id: font.cpp,v 1.129 2005/04/02 14:06:35 gruikya Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -104,17 +104,19 @@
 
        try {
                utils::utf8_iterator ch(utf8_text);
-               if(*ch < font_map.size() && font_map[*ch] >= 0) {
-                       current_chunk.subset = font_map[*ch];
+               if(size_t(*ch) < font_map.size() && font_map[size_t(*ch)] >= 0) 
{
+                       current_chunk.subset = font_map[size_t(*ch)];
                }
                for(utils::utf8_iterator end = 
utils::utf8_iterator::end(utf8_text); ch != end; ++ch) {
-                       if(*ch < font_map.size() && font_map[*ch] >= 0 && 
font_map[*ch] != current_chunk.subset) {
+                       if(size_t(*ch) < font_map.size() && 
+                                       font_map[size_t(*ch)] >= 0 && 
+                                       font_map[size_t(*ch)] != 
current_chunk.subset) {
                                //null-terminate ucs2_text so we can pass it to 
SDL_ttf later
                                current_chunk.ucs2_text.push_back(0);
                                chunks.push_back(current_chunk);
                                current_chunk.text = "";
                                current_chunk.ucs2_text.clear();
-                               current_chunk.subset = font_map[*ch];
+                               current_chunk.subset = font_map[size_t(*ch)];
                        }
                        current_chunk.ucs2_text.push_back((Uint16)*ch);
                        current_chunk.text.append(ch.substr().first, 
ch.substr().second);
@@ -187,7 +189,7 @@
        if(it != font_table.end())
                return it->second;
 
-       if(id.subset < 0 || id.subset >= font_names.size())
+       if(id.subset < 0 || size_t(id.subset) >= font_names.size())
                return NULL;
 
        TTF_Font* font = open_font(font_names[id.subset], id.size);
@@ -830,7 +832,7 @@
   return text;
 }
 
-int line_width(const std::string line, int font_size, int style)
+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);
@@ -838,75 +840,97 @@
        return s.width();
 }
 
+SDL_Rect line_size(const std::string& line, int font_size, int style)
+{
+       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();
+
+       return res;
+}
+
+namespace {
 
-std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 
int max_width)
+void cut_word(std::string& line, std::string& word, int size, int max_width)
 {
-       //std::cerr << "Wrapping word " << unwrapped_text << "\n";
-       
-       std::string wrapped_text; // the final result
+       std::string tmp = line;
+       utils::utf8_iterator tc(word);
 
-       size_t word_start_pos = 0;
-       std::string cur_word; // including start-whitespace
-       std::string cur_line; // the whole line so far
-
-       for(size_t c = 0; c < unwrapped_text.length(); c++) {
-
-               // Find the next word
-               bool forced_line_break = false;
-               if (c == unwrapped_text.length() - 1) {
-                       cur_word = unwrapped_text.substr(word_start_pos, c + 1 
- word_start_pos);
-                       word_start_pos = c + 1;
-               } else if (unwrapped_text[c] == '\n') {
-                       cur_word = unwrapped_text.substr(word_start_pos, c + 1 
- word_start_pos);
-                       word_start_pos = c + 1;
-                       forced_line_break = true;
-               } else if (unwrapped_text[c] == ' ') {
-                       cur_word = unwrapped_text.substr(word_start_pos, c - 
word_start_pos);
-                       word_start_pos = c;
-               } else {
-                       continue;
+       for(;tc != utils::utf8_iterator::end(word); ++tc) {
+               tmp.append(tc.substr().first, tc.substr().second);
+               SDL_Rect tsize = line_size(tmp, size);
+               if(tsize.w > max_width) {
+                       const std::string& w = word;
+                       line += std::string(w.begin(), tc.substr().first);
+                       word = std::string(tc.substr().first, w.end());
+                       break;
                }
+       }
+}
 
-               // Test if the line should be wrapped or not
-               if (line_width(cur_line + cur_word, font_size) > max_width) {
+}
 
-                       if (line_width(cur_word, font_size) > (max_width /*/ 
2*/)) {
-                               // The last word is too big to fit in a nice 
way, split it on a char basis
-                               utils::utf8_iterator i(cur_word);
+std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 
int max_width, int max_height)
+{
+       utils::utf8_iterator ch(unwrapped_text);
+       std::string current_word;
+       std::string current_line;
+       size_t current_height = 0;
+       bool line_break = false;
+       std::string wrapped_text;
        
-                               for (; i != 
utils::utf8_iterator::end(cur_word); ++i) {
-                                       std::string tmp = cur_line;
-                                       tmp.append(i.substr().first, 
i.substr().second);
-
-                                       if (line_width(tmp, font_size) > 
max_width) {
-                                               wrapped_text += cur_line + '\n';
-                                               cur_line = "";
-                                       }
-                                       cur_line.append(i.substr().first, 
i.substr().second);
-                               }
-
+       while(1) {
+               // If there is no current word, get one
+               if(current_word.empty() && ch == 
utils::utf8_iterator::end(unwrapped_text)) {
+                       break;
+               } else if(current_word.empty()) {
+                       if(*ch == ' ' || *ch == '\n') {
+                               current_word = *ch;
+                               ++ch;
                        } else {
-                               // Split the line on a word basis
-                               wrapped_text += cur_line + '\n';
-                               cur_line = remove_first_space(cur_word);
+                               for(;ch != 
utils::utf8_iterator::end(unwrapped_text) &&
+                                               *ch != ' ' && *ch != '\n'; 
++ch) {
 
+                                       current_word.append(ch.substr().first, 
ch.substr().second);
+                               }
                        }
-               } else {
-                       cur_line += cur_word;
                }
 
-               if (forced_line_break) {
-                       wrapped_text += cur_line;
-                       cur_line = "";
-                       forced_line_break = false;
+               if(current_word == "\n") {
+                       line_break = true;
+                       current_word = "";
+               } else {
+                       SDL_Rect size = line_size(current_line + current_word, 
font_size);
+
+                       if(size.w >= max_width) {
+                               SDL_Rect wsize = line_size(current_word, 
font_size);
+                               if(wsize.w >= max_width) {
+                                       cut_word(current_line, current_word, 
font_size, max_width);
+                               }
+                               if(current_word == " ")
+                                       current_word = "";
+                               line_break = true;
+                       } else {
+                               current_line += current_word;
+                               current_word = "";
+                       }
                }
-       }
 
-       // Don't forget to add the text left in cur_line
-       if (cur_line != "") {
-               wrapped_text += cur_line + '\n';
+               if(line_break || current_word.empty() && ch == 
utils::utf8_iterator::end(unwrapped_text)) {
+                       SDL_Rect size = line_size(current_line, font_size);
+                       if(max_height > 0 && current_height + size.h >= 
size_t(max_height))
+                               return wrapped_text;
+
+                       wrapped_text += "\n" + current_line;
+                       current_line = "";
+                       current_height += size.h;
+                       line_break = false;
+               }
        }
-
        return wrapped_text;
 }
 
Index: wesnoth/src/font.hpp
diff -u wesnoth/src/font.hpp:1.53 wesnoth/src/font.hpp:1.54
--- wesnoth/src/font.hpp:1.53   Wed Mar 23 20:46:58 2005
+++ wesnoth/src/font.hpp        Sat Apr  2 14:06:35 2005
@@ -1,4 +1,4 @@
-/* $Id: font.hpp,v 1.53 2005/03/23 20:46:58 ydirson Exp $ */
+/* $Id: font.hpp,v 1.54 2005/04/02 14:06:35 gruikya Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -110,14 +110,20 @@
 /// Determine the width of a line of text given a certain font size.
 /// The font type used is the default wesnoth font type.
 ///
-int line_width(const std::string line, int font_size, int 
style=TTF_STYLE_NORMAL);
+int line_width(const std::string& line, int font_size, int 
style=TTF_STYLE_NORMAL);
+
+///
+/// Determine the size of a line of text given a certain font size. Similar to
+/// line_width, but for both coordinates.
+///
+SDL_Rect line_size(const std::string& line, int font_size, int 
style=TTF_STYLE_NORMAL);
 
 ///
 /// If the text exceedes the specified max width, wrap it one a word basis.
 /// 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);
+std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 
int max_width, int max_height=-1);
 
 ///
 /// If the text excedes the specified max width, end it with an ellipsis (...)
Index: wesnoth/src/show_dialog.cpp
diff -u wesnoth/src/show_dialog.cpp:1.120 wesnoth/src/show_dialog.cpp:1.121
--- wesnoth/src/show_dialog.cpp:1.120   Sat Mar 26 17:10:32 2005
+++ wesnoth/src/show_dialog.cpp Sat Apr  2 14:06:35 2005
@@ -1,4 +1,4 @@
-/* $Id: show_dialog.cpp,v 1.120 2005/03/26 17:10:32 silene Exp $ */
+/* $Id: show_dialog.cpp,v 1.121 2005/04/02 14:06:35 gruikya Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -376,14 +376,17 @@
 
        menu_.set_numeric_keypress_selection(use_textbox == false);
 
-#ifdef USE_TINY_GUI
-       const int max_line_length = 30;
-#else
-       const int max_line_length = 54;
-#endif
+       const int left_padding = 10;
+       const int right_padding = 10;
+       const int image_h_padding = image != NULL ? 10 : 0;
+       const int top_padding = 10;
+       const int bottom_padding = 10;
 
-       std::string message = msg;
-       font::text_to_lines(message,max_line_length);
+       const std::string message = font::word_wrap_text(msg, message_font_size,
+                       screen.getx() / 2,
+                       screen.gety() / 2);
+
+       std::cerr << "Message is " << message << "\n";
 
        SDL_Rect text_size = { 0, 0, 0, 0 };
        if(!message.empty()) {
@@ -499,20 +502,15 @@
                }
        }
 
-       const int left_padding = 10;
-       const int right_padding = 10;
-       const int image_h_padding = image != NULL ? 10 : 0;
-       const int top_padding = 10;
-       const int bottom_padding = 10;
        const int menu_hpadding = text_size.h > 0 && menu_.height() > 0 ? 10 : 
0;
        const int padding_width = left_padding + right_padding + 
image_h_padding;
        const int padding_height = top_padding + bottom_padding + menu_hpadding;
-       const int caption_width = caption_size.w;
+       const size_t caption_width = caption_size.w;
        const int image_width = image != NULL ? image->w : 0;
        const int image_height = image != NULL ? image->h : 0;
        const int total_text_height = text_size.h + caption_size.h;
 
-       int text_width = text_size.w;
+       size_t text_width = text_size.w;
        if(caption_width > text_width)
                text_width = caption_width;
        if(menu_.width() > text_width)




reply via email to

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