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);
	}
}

@@ -356,16 +356,68 @@
 %3C%3E();%0A%09%0A
+%09public static Font TIMES_ROMAN = new TimesRoman();%0A
 %09static 
@@ -445,32 +445,27 @@
 Roman%22, 
-new TimesRoman()
+TIMES_ROMAN
 );%0A%09%7D%0A%09%0A
@@ -498,36 +498,38 @@
 er size, String 
-text
+string
 ) %7B%0A%09%09int fullWi
@@ -549,36 +549,38 @@
 r(byte letter : 
-text
+string
 .getBytes()) %7B%0A%09
@@ -585,19 +585,22 @@
 %0A%09%09%09
-fullW
+Integer w
 idth 
-+
 = wi
@@ -617,16 +617,68 @@
 etter);%0A
+%09%09%09if(width != null) %7B%0A%09%09%09%09fullWidth += width;%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09re
@@ -776,20 +776,22 @@
  String 
-text
+string
 ) %7B%0A%09%09in
@@ -835,12 +835,14 @@
 r : 
-text
+string
 .get

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.