Changeset - r27289:abeec00a698a
[Not reviewed]
master
0 1 0
Patric Stout - 13 months ago 2023-05-06 18:19:15
truebrain@openttd.org
Codechange: simplify how GetCharPosition() works
1 file changed with 25 insertions and 22 deletions:
0 comments (0 inline, 0 general)
src/gfx_layout.cpp
Show inline comments
 
@@ -213,41 +213,44 @@ Dimension Layouter::GetBounds()
 
 */
 
Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const
 
{
 
	const auto &line = this->front();
 

	
 
	/* Pointer to the end-of-string marker? Return total line width. */
 
	if (ch == this->string.end()) {
 
		Point p = { line->GetWidth(), 0 };
 
		return p;
 
	}
 

	
 
	/* Find the code point index which corresponds to the char
 
	 * pointer into our UTF-8 source string. */
 
	size_t index = 0;
 
	auto str = this->string.begin();
 
	while (str < ch && str != this->string.end()) {
 
	while (str < ch) {
 
		WChar c = Utf8Consume(str);
 
		index += this->front()->GetInternalCharLength(c);
 
		index += line->GetInternalCharLength(c);
 
	}
 

	
 
	/* We couldn't find the code point index. */
 
	if (str != ch) {
 
		return { 0, 0 };
 
	}
 

	
 
	if (str == ch) {
 
		/* Valid character. */
 
		const auto &line = this->front();
 
	/* Valid character. */
 

	
 
		/* Pointer to the end-of-string/line marker? Return total line width. */
 
		if (ch == this->string.end() || *ch == '\0' || *ch == '\n') {
 
			Point p = { line->GetWidth(), 0 };
 
			return p;
 
		}
 
	/* Scan all runs until we've found our code point index. */
 
	for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
 
		const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
 

	
 
		/* Scan all runs until we've found our code point index. */
 
		for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
 
			const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
 

	
 
			for (int i = 0; i < run.GetGlyphCount(); i++) {
 
				/* Matching glyph? Return position. */
 
				if ((size_t)run.GetGlyphToCharMap()[i] == index) {
 
					Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
 
					return p;
 
				}
 
		for (int i = 0; i < run.GetGlyphCount(); i++) {
 
			/* Matching glyph? Return position. */
 
			if ((size_t)run.GetGlyphToCharMap()[i] == index) {
 
				Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
 
				return p;
 
			}
 
		}
 
	}
 

	
 
	Point p = { 0, 0 };
 
	return p;
 
	NOT_REACHED();
 
}
 

	
 
/**
0 comments (0 inline, 0 general)