All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bloodshura.x.assets.font.glyph.GlyphableFontWrapper Maven / Gradle / Ivy

/*
 * Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 *
 * https://www.github.com/BloodShura
 */

package com.github.bloodshura.x.assets.font.glyph;

import javax.annotation.Nonnull;

// TODO Rework dis shet; 28-10-2015
public class GlyphableFontWrapper {
	private final Glyphable parent;

	public GlyphableFontWrapper(@Nonnull Glyphable parent) {
		this.parent = parent;
	}

	public int sizeStringToWidth(@Nonnull String s, int width) {
		int strLength = s.length();
		int strWidth = 0;
		int size = 0;
		int l = -1;
		boolean flag = false;

		do {
			if (size >= strLength) {
				break;
			}

			char c = s.charAt(size);

			switch (c) {
				case 167:
					if (size != strLength) {
						char c1 = s.charAt(++size);

						if (c1 == 'l' || c1 == 'L') {
							flag = true;
						}
						else if (c1 == 'r' || c1 == 'R') {
							flag = false;
						}
					}

					break;
				case 32:
					l = size;
				default:
					strWidth += parent.getGlyph(c).getWidth();

					if (flag) {
						strWidth++;
					}

					break;
			}

			if (c == 32767) {
				l = ++size;

				break;
			}

			if (strWidth > width) {
				break;
			}

			size++;
		}
		while (true);

		if (size != strLength && l != -1 && l < size) {
			return l;
		}

		return size;
	}

	@Nonnull
	public String wrapStringToWidth(@Nonnull String string, int width) {
		int i = sizeStringToWidth(string, width);

		if (string.length() <= i) {
			return string;
		}

		String s = string.substring(0, i);
		String s1 = string.substring(i + (string.charAt(i) == ' ' ? 1 : 0));

		return s + (char) 32767 + wrapStringToWidth(s1, width);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy