package devices.Oled.SSD1306;



// Source : https://lexus2k.github.io/ssd1306/ssd1306__fonts_8c_source.html
// Source : https://github.com/lynniemagoo/oled-font-pack/tree/master/fonts
// Source : http://www.rinkydinkelectronics.com/r_fonts.php

/**
 * Base Class for OLED Fonts 
 * Do not create variable from it in B4X
 * @author rdkartono
 *
 */
public abstract class FontPack {
	private final String fontname;
	private final int width;
	private final int height;
	private final int outer_width;
	private final int outer_height;
	private final int min_ascii_code;
	private final int max_ascii_code;
	protected int[] data;
	
	/**
	 * Initialize FontPack
	 * @param name Name of font
	 * @param width width of a character in pixel
	 * @param height height of a character in pixel
	 * @param outerwidth width plus extra pixel for spacing , of a character
	 * @param outerheight height plus extra pixel for spacing, of a character
	 * @param minchar minimum ASCII code value, usually 32 (space)
	 * @param maxchar maximum ASCII code value, usually 127 
	 */
	public FontPack(String name, int width, int height, int outerwidth, int outerheight, int minchar, int maxchar) {
		this.fontname = name;
		this.width = width;
		this.height = height;
		this.outer_width = outerwidth;
		this.outer_height = outerheight;
		this.min_ascii_code = minchar;
		this.max_ascii_code = maxchar;
	}
	
	/**
	 * Get Font Name
	 * @return font name
	 */
	public String getName() {
		return fontname;
	}
	
	/**
	 * Get width of a character
	 * @return width in pixel
	 */
	public int getWidth() {
		return width;
	}
	
	/**
	 * Get height of a character
	 * @return height in pixel
	 */
	public int getHeight() {
		return height;
	}
	
	/**
	 * Get width + extra pixel for spacing, of a character
	 * @return width in pixel
	 */
	public int getOuterWidth() {
		return outer_width;
	}
	
	/**
	 * Get Height + extra pixel for spacing, of a character
	 * @return height in pixel
	 */
	public int getOuterHeight() {
		return outer_height;
	}
	
	/**
	 * Get minimum supported ascii code, usually 32 , which is space
	 * @return ascii code
	 */
	public int getMinChar() {
		return min_ascii_code;
	}
	
	/**
	 * Get maximum supported ascii code, usually 127
	 * @return ascii code
	 */
	public int getMaxChar() {
		return max_ascii_code;
	}
	
	/**
	 * Draw a character on specific coordinate
	 * @param display OLED display to draw the character
	 * @param c character to display
	 * @param x top left coordinate
	 * @param y top left coordinate
	 * @param on if true, will turn ON the pixel at character X Y
	 */
	public void drawChar(OLEDDisplay display, char c, int x, int y, boolean on) {
		if (c < getMinChar()) return;
		if (c > getMaxChar()) return;
		if (display==null) return;
		if (x < 1) return;
		if (y < 1) return;
		if (x > display.getWidth()) return;
		if (y > display.getHeight()) return;
		
		//System.out.println("Drawing char "+c);
		c-= getMinChar();		// if c = min_ascii_code , will point to index 0 at data
		//System.out.println("Ascii code =  "+(int)c);
		for(int ii=0;ii<getWidth();ii++) {
			// ambil data 1 barix
			int line = data[(c * getWidth()) + ii];
			//System.out.println("ii="+ii+", line = "+Integer.toHexString(line));
			for(int jj=0;jj< getHeight(); jj++) {
				if ((line & 0x01)>0) {
					display.setPixel(x+ii, y+jj, on);
				}
				line >>= 1;
			}
		}
	}
	
}
