package readers.fonts ;
import java.awt.image.BufferedImage ;
import java.util.ArrayList ;
import java.awt.Color ;
import java.util.List ;
public class Glyph {
final static int TRANSPARENT = new Color ( 0 , 0 , 0 , 0 ) . getRGB ( ) ;
int width = 0 ;
int height = 0 ;
boolean [ ] data = new boolean [ ] { } ;
public static List < Glyph > split ( BufferedImage img ) {
int startX = - 1 ;
int endX = - 1 ;
List < Glyph > glyphs = new ArrayList < > ( ) ;
outer :
for ( int x = 0 ; x < img . getWidth ( ) ; x + + ) {
for ( int y = 0 ; y < img . getHeight ( ) ; y + + ) {
if ( img . getRGB ( x , y ) ! = TRANSPARENT ) {
if ( startX = = - 1 ) {
startX = x ;
}
continue outer ;
}
}
//If we got to here it means we found transparent on everything.
if ( startX ! = - 1 ) {
endX = x ;
//Create a Glyph out of this.
Glyph g = new Glyph ( ) ;
g . width = endX - startX ;
g . height = img . getHeight ( ) ;
int [ ] arr = img . getRGB ( startX , 0 , g . width , g . height , null , 0 , g . width ) ;
//Find the min and max Y.
int startY = g . height ;
int endY = 0 ;
for ( int X = 0 ; X < g . width ; X + + ) {
for ( int Y = 0 ; Y < g . height ; Y + + ) {
if ( arr [ Y * g . width + X ] ! = TRANSPARENT & & Y < startY ) {
startY = Y ;
}
if ( arr [ Y * g . width + X ] ! = TRANSPARENT & & Y > endY ) {
endY = Y ;
}
}
}
g . height = endY - startY ;
g . data = new boolean [ g . width * g . height ] ;
for ( int X = 0 ; X < g . width ; X + + ) {
for ( int Y = 0 ; Y < g . height ; Y + + ) {
g . data [ Y * g . width + X ] = arr [ ( Y + startY ) * g . width + X ] ! = TRANSPARENT ;
}
}
glyphs . add ( g ) ;
startX = - 1 ;
endX = - 1 ;
}
}
return glyphs ;
}
}