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

com.github.bloodshura.x.assets.font.Font Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/*
 * 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;

import com.github.bloodshura.x.assets.Asset;
import com.github.bloodshura.x.assets.image.Image;
import com.github.bloodshura.x.collection.map.XMap;
import com.github.bloodshura.x.collection.map.impl.XLinkedMap;
import com.github.bloodshura.x.collection.tuple.Pair;
import com.github.bloodshura.x.lang.Nameable;
import com.github.bloodshura.x.object.storable.StorableBase;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public abstract class Font extends StorableBase implements Asset, Nameable {
	private XMap cachedDerivationMap;

	@Nonnull
	@Override
	public abstract Font clone();

	@Nonnull
	public Font derive(@Nonnull FontStyle style) {
		return derive(getSize(), style);
	}

	@Nonnull
	public Font derive(int size) {
		return derive(size, getStyle());
	}

	@Nonnull
	public Font derive(int size, @Nonnull FontStyle style) {
		if (cachedDerivationMap != null) {
			for (Pair pair : cachedDerivationMap) {
				int entrySize = (int) pair.getLeft()[0];
				FontStyle entryStyle = (FontStyle) pair.getLeft()[1];

				if (entrySize == size && entryStyle == style) {
					return pair.getRight();
				}
			}
		}
		else {
			this.cachedDerivationMap = new XLinkedMap<>();
		}

		Font font = deriveNewFont(size, style);

		cachedDerivationMap.add(new Object[] { size, style }, font);

		return font;
	}

	@Nonnull
	public abstract java.awt.Font getAwtFont();

	@Nonnull
	public abstract String getFamily();

	public abstract int getHeight();

	public int getHeight(char character) {
		return getHeight(String.valueOf(character));
	}

	public int getHeight(@Nonnull CharSequence sequence) {
		return getHeight();
	}

	@Nonnull
	public abstract Image getImage();

	public abstract int getSize();

	@Nonnull
	public FontStyle getStyle() {
		return FontStyle.byId(getAwtFont().getStyle());
	}

	public int getWidth(char character) {
		return getWidth(String.valueOf(character));
	}

	public abstract int getWidth(@Nonnull CharSequence s);

	public abstract boolean isSupported(char character);

	@Nonnull
	public Font sizedFor(@Nonnull CharSequence s, double width) {
		Font font = this;
		int size = getSize();

		while (width < font.getWidth(s) + 2 && size > 5) {
			font = derive(size--);
		}

		return font;
	}

	@Nonnull
	public  E trimToWidth(@Nonnull E s, double width) {
		E result = s;

		while (getWidth(result) > width) {
			result = (E) result.subSequence(0, result.length() - 1);
		}

		return result;
	}

	@Nonnull
	public abstract String[] wrapString(@Nonnull String s, int width);

	@Nonnull
	protected abstract Font deriveNewFont(int size, @Nonnull FontStyle style);

	@Nonnull
	@Override
	protected Object[] stringValues() {
		return new Object[] { getFamily(), getSize(), getStyle() };
	}

	public static enum FontStyle {
		BOLD(0x01),
		BOLD_ITALIC(0x03),
		ITALIC(0x02),
		PLAIN(0x00);

		private final int id;

		private FontStyle(int id) {
			this.id = id;
		}

		public int getId() {
			return id;
		}

		@Nullable
		public static FontStyle byId(int id) {
			for (FontStyle style : values()) {
				if (style.getId() == id) {
					return style;
				}
			}

			return null;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy