RandomGuy32

Font.determineGlyphWidths()

Jan 27th, 2019
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. byte[] gWidths = new byte[this.end - this.start + 1];
  2.  
  3. for (int i: gWidths) {
  4.     BufferedImage sheet = this.sheets.get(i / 128);
  5.  
  6.     for (int j = 0; j < 256; j++) {
  7.         // for each individual glyph...
  8.         int width = -1;
  9.  
  10.         //You could set width to -1 if the possibility exists that the glyph is completely black
  11.         //with that you could find out if the width is actually column 0 or non-existant.
  12.  
  13.         for (int k = 0; k < 64; k++) {
  14.             // for each individual pixel...
  15.             int x = (j % 16) * 8 + (k % 8);
  16.             int y = (j / 16) * 8 + (k / 8);
  17.  
  18.             //if you want to go through them by line and not by column, like this:
  19.             //0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
  20.             //17 18 19 20…
  21.             //
  22.             //use this code instead:
  23.             //
  24.             // int x = Math.floor(j / 8) * 8 + (k%8)
  25.             // int y = (j % 8) * 8 + Math.floor(k/8)
  26.  
  27.             // Util.log(x + "|" + y);
  28.  
  29.             // Util.log(this.sheets.get(0).getSource());
  30.             // Util.log(this.sheets.get(0).getWidth());
  31.             // Util.log(this.sheets.get(0).getHeight());
  32.  
  33.             boolean isWhite = sheet.getRGB(x, y) == (new Color(0xff, 0xff, 0xff)).getRGB();   // I don't trust any of this
  34.             //I merged the two for loops since it's not necessary to go reverse.
  35.             //Because of that we don't have a need to write isWhite into an array
  36.             //and can just define the boolean inside the loop.
  37.  
  38.             if(isWhite && (k % 8 > width)){
  39.                 //Now we know the pixel is white and the position in line is bigger than the previous value in width.
  40.                 //We need to know this because if width is, for example, currently 5, and this pixel is positioned
  41.                 //at 3, width should stay at 5 and not get overwritten by 3.
  42.                 width = k % 8;
  43.             }
  44.         }
  45.  
  46.         gWidths[i] = (byte) width;
  47.     }
  48. }
RAW Paste Data