Exemple #1
0
void Label::regenerate_word_cache() {
	
	while (word_cache) {
		
		WordCache *current=word_cache;
		word_cache=current->next;
		memdelete( current );
	}
	
	
	int width=autowrap?get_size().width:get_longest_line_width();
	Ref<Font> font = get_font("font");
	
	int current_word_size=0;
	int word_pos=0;
	int line_width=0;
	int last_width=0;
	line_count=1;
	total_char_cache=0;
	
	WordCache *last=NULL;
	
	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
		
		if (uppercase)
			current=String::char_uppercase(current);

		bool insert_newline=false;
		
		if (current<33) {
			
			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;
				current_word_size=0;
				
			}
			
			if (current=='\n') {
				
				insert_newline=true;
			} else {
				total_char_cache++;
			}


		} else {
			
			if (current_word_size==0) {
				if (line_width>0) // add a space before the new word if a word existed before
					line_width+=font->get_char_size(' ').width;
				word_pos=i;
			}
			
			int char_width=font->get_char_size(current).width;
			current_word_size+=char_width;
			line_width+=char_width;
			total_char_cache++;
			
		}
		
		if ((autowrap && line_width>=width && last_width<width) || insert_newline) {
			
			WordCache *wc = memnew( WordCache );
			if (word_cache) {
				last->next=wc;
			} else {
				word_cache=wc;
			}
			last=wc;
			
			wc->pixel_width=0;
			wc->char_pos=insert_newline?WordCache::CHAR_NEWLINE:WordCache::CHAR_WRAPLINE;

			line_width=current_word_size;
			line_count++;

			
		}
		
		last_width=line_width;
		
	}
		
	
	if (!autowrap) {
		
		minsize.width=width;
		minsize.height=font->get_height()*line_count;
		set_page( line_count );
		
	} else {
	
		set_page( get_size().height / font->get_height() );
	}
	
	set_max(line_count);
	
	word_cache_dirty=false;
		
}
Exemple #2
0
void Label::regenerate_word_cache() {

	while (word_cache) {

		WordCache *current=word_cache;
		word_cache=current->next;
		memdelete( current );
	}


	int width=autowrap?get_size().width:get_longest_line_width();
	Ref<Font> font = get_font("font");

	int current_word_size=0;
	int word_pos=0;
	int line_width=0;
	int space_count=0;
	int space_width=font->get_char_size(' ').width;
	int line_spacing = get_constant("line_spacing");
	line_count=1;
	total_char_cache=0;

	WordCache *last=NULL;

	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

		if (uppercase)
			current=String::char_uppercase(current);

		// ranges taken from http://www.unicodemap.org/
		// if your language is not well supported, consider helping improve
		// the unicode support in Godot.
		bool separatable = (current>=0x2E08 && current<=0xFAFF) || (current>=0xFE30 && current<=0xFE4F);
				//current>=33 && (current < 65||current >90) && (current<97||current>122) && (current<48||current>57);
		bool insert_newline=false;
		int char_width;

		if (current<33) {

			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;

			}


			if (current=='\n') {
				insert_newline=true;
			} else {
				total_char_cache++;
			}

			if (i<text.length() && text[i] == ' ') {
				total_char_cache--;  // do not count spaces
				if (line_width > 0 || last==NULL || last->char_pos!=WordCache::CHAR_WRAPLINE) {
					space_count++;
					line_width+=space_width;
				}else {
					space_count=0;
				}
			}


		} else {
			// latin characters
			if (current_word_size==0) {
				word_pos=i;
			}

			char_width=font->get_char_size(current,text[i+1]).width;
			current_word_size+=char_width;
			line_width+=char_width;
			total_char_cache++;

		}

		if ((autowrap && (line_width >= width) && ((last && last->char_pos >= 0) || separatable)) || insert_newline) {
			if (separatable) {
				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 );
			if (word_cache) {
				last->next=wc;
			} else {
				word_cache=wc;
			}
			last=wc;

			wc->pixel_width=0;
			wc->char_pos=insert_newline?WordCache::CHAR_NEWLINE:WordCache::CHAR_WRAPLINE;

			line_width=current_word_size;
			line_count++;
			space_count=0;

		}

	}

	if (!autowrap) {
		minsize.width=width;
		if (max_lines_visible > 0 && line_count > max_lines_visible) {
			minsize.height=(font->get_height() * max_lines_visible) + (line_spacing * (max_lines_visible - 1));
		} else {
			minsize.height=(font->get_height() * line_count)+(line_spacing * (line_count - 1));
		}
	}

	word_cache_dirty=false;

}
Exemple #3
0
void Label::regenerate_word_cache() {
	
	while (word_cache) {
		
		WordCache *current=word_cache;
		word_cache=current->next;
		memdelete( current );
	}
	
	
	int width=autowrap?get_size().width:get_longest_line_width();
	Ref<Font> font = get_font("font");

	int current_word_size=0;
	int word_pos=0;
	int line_width=0;
	int space_count=0;
	int space_width=font->get_char_size(' ').width;
	line_count=1;
	total_char_cache=0;
	
	WordCache *last=NULL;
	
	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

		if (uppercase)
			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;
		int char_width;

		if (current<33) {

			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;

			}


			if (current=='\n') {
				insert_newline=true;
			} else {
				total_char_cache++;
			}

			if (i<text.length() && text[i] == ' ') {
				total_char_cache--;  // do not count spaces
				if (line_width > 0 || last==NULL || last->char_pos!=WordCache::CHAR_WRAPLINE) {
					space_count++;
					line_width+=space_width;
				}else {
					space_count=0;
				}
			}


		} else {
			// latin characters
			if (current_word_size==0) {
				word_pos=i;
			}
			
			char_width=font->get_char_size(current).width;
			current_word_size+=char_width;
			line_width+=char_width;
			total_char_cache++;
			
		}

		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 );
			if (word_cache) {
				last->next=wc;
			} else {
				word_cache=wc;
			}
			last=wc;

			wc->pixel_width=0;
			wc->char_pos=insert_newline?WordCache::CHAR_NEWLINE:WordCache::CHAR_WRAPLINE;

			line_width=current_word_size;
			line_count++;
			space_count=0;

		}
		
	}
	
	//total_char_cache -= line_count + 1; // do not count new lines (including the first one)
	
	if (!autowrap) {
		
		minsize.width=width;
		minsize.height=font->get_height()*line_count;
		set_page( line_count );
		
	} else {
	
		set_page( get_size().height / font->get_height() );
	}
	
	set_max(line_count);
	
	word_cache_dirty=false;

}