package com.web4enterprise.pdf.core.font;

import java.util.HashMap;
import java.util.Map;

import com.web4enterprise.pdf.core.BoundingBox;

public abstract class Font {	
	protected static Map<String, Font> fonts = new HashMap<>();
	protected static Map<Byte, Integer> widths = new HashMap<>();
	protected static Map<Byte, BoundingBox> boxes = new HashMap<>();
	
	static {
		fonts.put("Times-Roman", new TimesRoman());
	}
	
	public int getWidth(Integer size, String text) {
		int fullWidth = 0;
		for(byte letter : text.getBytes()) {
			fullWidth += widths.get(letter);
		}
		return (int) Math.round(fullWidth * size / 1000.0f);
	}
	
	public int getHeight(Integer size, String text) {
		int greaterHeight = 0;
		for(byte letter : text.getBytes()) {
			BoundingBox letterBox = boxes.get(letter);
			int height = letterBox.getHeight();
			if(height > greaterHeight) {
				greaterHeight = height;
			}
		}
		return (int) Math.round(greaterHeight * size / 1000.0f);
	}
	
	public static Font getFont(String name) {
		return fonts.get(name);
	}
}

package com.web4enterprise.pdf.core.font;

import java.util.HashMap;
import java.util.Map;

import com.web4enterprise.pdf.core.BoundingBox;

public abstract class Font {	
	protected static Map<String, Font> fonts = new HashMap<>();
	protected static Map<Byte, Integer> widths = new HashMap<>();
	protected static Map<Byte, BoundingBox> boxes = new HashMap<>();
	
	public static Font TIMES_ROMAN = new TimesRoman();
	static {
		fonts.put("Times-Roman", TIMES_ROMAN);
	}
	
	public int getWidth(Integer size, String string) {
		int fullWidth = 0;
		for(byte letter : string.getBytes()) {
			Integer width = widths.get(letter);
			if(width != null) {
				fullWidth += width;
			}
		}
		return (int) Math.round(fullWidth * size / 1000.0f);
	}
	
	public int getHeight(Integer size, String string) {
		int greaterHeight = 0;
		for(byte letter : string.getBytes()) {
			BoundingBox letterBox = boxes.get(letter);
			int height = letterBox.getHeight();
			if(height > greaterHeight) {
				greaterHeight = height;
			}
		}
		return (int) Math.round(greaterHeight * size / 1000.0f);
	}
	
	public static Font getFont(String name) {
		return fonts.get(name);
	}
}

Handle case when getting the width of a text, if a letter is not recognized, the width is not incremented instead of throwing a NullPointerException.
Handle case when getting the width of a text, if a letter is not recognized, the width is not incremented instead of throwing a NullPointerException.
