Optimize the code.

This commit is contained in:
Gen 2015-05-01 18:47:34 +08:00
parent c322eddffb
commit 0bc5b7a146

View File

@ -360,11 +360,10 @@ void Label::regenerate_word_cache() {
int width=autowrap?get_size().width:get_longest_line_width(); int width=autowrap?get_size().width:get_longest_line_width();
Ref<Font> font = get_font("font"); Ref<Font> font = get_font("font");
int current_word_size=0; int current_word_size=0;
int word_pos=0; int word_pos=0;
int line_width=0; int line_width=0;
int last_width=0;
int space_count=0; int space_count=0;
int space_width=font->get_char_size(' ').width; int space_width=font->get_char_size(' ').width;
line_count=1; line_count=1;
@ -375,14 +374,16 @@ void Label::regenerate_word_cache() {
for (int i=0;i<text.size()+1;i++) { for (int i=0;i<text.size()+1;i++) {
CharType current=i<text.length()?text[i]:' '; //always a space at the end, so the algo works CharType current=i<text.length()?text[i]:' '; //always a space at the end, so the algo works
if (uppercase) if (uppercase)
current=String::char_uppercase(current); current=String::char_uppercase(current);
bool not_latin = current>=33 && (current < 65||current >90) && (current<97||current>122) && (current<48||current>57);
bool insert_newline=false; bool insert_newline=false;
int char_width;
if (current<33) { if (current<33) {
if (current_word_size>0) { if (current_word_size>0) {
WordCache *wc = memnew( WordCache ); WordCache *wc = memnew( WordCache );
if (word_cache) { if (word_cache) {
@ -391,16 +392,16 @@ void Label::regenerate_word_cache() {
word_cache=wc; word_cache=wc;
} }
last=wc; last=wc;
wc->pixel_width=current_word_size; wc->pixel_width=current_word_size;
wc->char_pos=word_pos; wc->char_pos=word_pos;
wc->word_len=i-word_pos; wc->word_len=i-word_pos;
wc->space_count = space_count; wc->space_count = space_count;
current_word_size=0; current_word_size=0;
space_count=0; space_count=0;
} }
if (current=='\n') { if (current=='\n') {
insert_newline=true; insert_newline=true;
@ -419,73 +420,39 @@ void Label::regenerate_word_cache() {
} }
}else if ((current < 65||current >90) && (current<97||current>122) && (current<48||current>57)) {
if (current_word_size>0) {
WordCache *wc = memnew( WordCache );
if (word_cache) {
last->next=wc;
} else {
word_cache=wc;
}
last=wc;
wc->pixel_width=current_word_size;
wc->char_pos=word_pos;
wc->word_len=i-word_pos;
wc->space_count = space_count;
current_word_size=0;
space_count=0;
}
float current_width = font->get_char_size(current).width;
if ((autowrap && line_width+current_width>=width && last_width<width)) {
WordCache *wc = memnew( WordCache );
if (word_cache) {
last->next=wc;
} else {
word_cache=wc;
}
last=wc;
wc->pixel_width=0;
wc->char_pos=WordCache::CHAR_WRAPLINE;
line_width=0;
line_count++;
}
WordCache *wc = memnew( WordCache );
if (word_cache) {
last->next=wc;
} else {
word_cache=wc;
}
last=wc;
wc->pixel_width=current_width;
wc->char_pos=i;
wc->word_len=1;
wc->space_count=space_count;
current_word_size=0;
space_count=0;
total_char_cache++;
line_width+=wc->pixel_width;
} else { } else {
// latin characters // latin characters
if (current_word_size==0) { if (current_word_size==0) {
word_pos=i; word_pos=i;
} }
int char_width=font->get_char_size(current).width; char_width=font->get_char_size(current).width;
current_word_size+=char_width; current_word_size+=char_width;
line_width+=char_width; line_width+=char_width;
total_char_cache++; total_char_cache++;
} }
if ((autowrap && line_width>=width && last && last->char_pos >= 0) || insert_newline) { if ((autowrap && line_width>=width && (last && last->char_pos >= 0 || not_latin)) || insert_newline) {
if (not_latin) {
if (current_word_size>0) {
WordCache *wc = memnew( WordCache );
if (word_cache) {
last->next=wc;
} else {
word_cache=wc;
}
last=wc;
wc->pixel_width=current_word_size-char_width;
wc->char_pos=word_pos;
wc->word_len=i-word_pos;
wc->space_count = space_count;
current_word_size=char_width;
space_count=0;
word_pos=i;
}
}
WordCache *wc = memnew( WordCache ); WordCache *wc = memnew( WordCache );
if (word_cache) { if (word_cache) {
@ -500,12 +467,10 @@ void Label::regenerate_word_cache() {
line_width=current_word_size; line_width=current_word_size;
line_count++; line_count++;
space_count=0;
} }
last_width=line_width;
} }
//total_char_cache -= line_count + 1; // do not count new lines (including the first one) //total_char_cache -= line_count + 1; // do not count new lines (including the first one)