Exemplo n.º 1
0
int BaseFontBitmap::textHeightDraw(const byte *text, int x, int y, int width, TTextAlign align, bool draw, int maxHeight, int maxLength) {
	if (maxLength == 0) {
		return 0;
	}

	if (text == nullptr || text[0] == '\0') {
		return _tileHeight;
	}

	AnsiString str;

	if (_gameRef->_textEncoding == TEXT_UTF8) {
		WideString wstr = StringUtil::utf8ToWide(Utf8String((const char *)text));
		str = StringUtil::wideToAnsi(wstr);
	} else {
		str = AnsiString((const char *)text);
	}
	if (str.empty()) {
		return 0;
	}

	int lineLength = 0;
	int realLength = 0;
	int numLines = 0;

	int i;

	int index = -1;
	int start = 0;
	int end = 0;
	int last_end = 0;

	bool done = false;
	bool newLine = false;
	bool longLine = false;

	if (draw) {
		_gameRef->_renderer->startSpriteBatch();
	}

	while (!done) {
		if (maxHeight > 0 && (numLines + 1)*_tileHeight > maxHeight) {
			if (draw) {
				_gameRef->_renderer->endSpriteBatch();
			}
			return numLines * _tileHeight;
		}

		index++;

		if (str[index] == ' ' && (maxHeight < 0 || maxHeight / _tileHeight > 1)) {
			end = index - 1;
			realLength = lineLength;
		}

		if (str[index] == '\n') {
			end = index - 1;
			realLength = lineLength;
			newLine = true;
		}

		if (lineLength + getCharWidth(str[index]) > width && last_end == end) {
			end = index - 1;
			realLength = lineLength;
			newLine = true;
			longLine = true;
		}

		if ((int)str.size() == (index + 1) || (maxLength >= 0 && index == maxLength - 1)) {
			done = true;
			if (!newLine) {
				end = index;
				lineLength += getCharWidth(str[index]);
				realLength = lineLength;
			}
		} else {
			lineLength += getCharWidth(str[index]);
		}

		if ((lineLength > width) || done || newLine) {
			if (end < 0) {
				done = true;
			}
			int startX;
			switch (align) {
			case TAL_CENTER:
				startX = x + (width - realLength) / 2;
				break;
			case TAL_RIGHT:
				startX = x + width - realLength;
				break;
			case TAL_LEFT:
				startX = x;
				break;
			default:
				error("BaseFontBitmap::TextHeightDraw - Unhandled enum");
				break;
			}
			for (i = start; i < end + 1; i++) {
				if (draw) {
					drawChar(str[i], startX, y);
				}
				startX += getCharWidth(str[i]);
			}
			y += _tileHeight;
			last_end = end;
			if (longLine) {
				end--;
			}
			start = end + 2;
			index = end + 1;
			lineLength = 0;
			newLine = false;
			longLine = false;
			numLines++;
		}
	}

	if (draw) {
		_gameRef->_renderer->endSpriteBatch();
	}

	return numLines * _tileHeight;
}