Advertisement
Chiddix

TypeFace

Feb 3rd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.69 KB | None | 0 0
  1. package com.runescape.graphic;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.runescape.cache.Archive;
  6. import com.runescape.net.Buffer;
  7.  
  8. public class TypeFace extends Rasterizer {
  9.    
  10.     protected byte[][] characterPixels = new byte[256][];
  11.     protected int[] characterWidths = new int[256];
  12.     protected int[] characterHeights = new int[256];
  13.     protected int[] characterXOffsets = new int[256];
  14.     protected int[] characterYOffsets = new int[256];
  15.     public int[] characterScreenWidths = new int[256];
  16.     public int characterDefaultHeight;
  17.     protected Random random = new Random();
  18.     protected boolean strikeThrough = false;
  19.  
  20.     public TypeFace(boolean large, String archiveName, Archive archive) {
  21.         Buffer dataBuffer = new Buffer(archive.getFile(archiveName + ".dat"));
  22.         Buffer indexBuffer = new Buffer(archive.getFile("index.dat"));
  23.         indexBuffer.offset = dataBuffer.getUnsignedLEShort() + 7;
  24.         indexBuffer.getUnsignedByte(); // dummy
  25.  
  26.         for (int character = 0; character < 256; character++) {
  27.             characterXOffsets[character] = indexBuffer.getUnsignedByte();
  28.             characterYOffsets[character] = indexBuffer.getUnsignedByte();
  29.             int characterWidth = characterWidths[character] = indexBuffer.getUnsignedLEShort();
  30.             int characterHeight = characterHeights[character] = indexBuffer.getUnsignedLEShort();
  31.             int characterType = indexBuffer.getUnsignedByte();
  32.             int characterSize = characterWidth * characterHeight;
  33.             characterPixels[character] = new byte[characterSize];
  34.             if (characterType == 0) {
  35.                 for (int pixel = 0; pixel < characterSize; pixel++) {
  36.                     characterPixels[character][pixel] = dataBuffer.get();
  37.                 }
  38.             } else if (characterType == 1) {
  39.                 for (int characterX = 0; characterX < characterWidth; characterX++) {
  40.                     for (int characterY = 0; characterY < characterHeight; characterY++) {
  41.                         characterPixels[character][characterX + characterY * characterWidth] = dataBuffer.get();
  42.                     }
  43.                 }
  44.             }
  45.             if (characterHeight > characterDefaultHeight && character < 128) {
  46.                 characterDefaultHeight = characterHeight;
  47.             }
  48.             characterXOffsets[character] = 1;
  49.             characterScreenWidths[character] = characterWidth + 2;
  50.             int pixelCount = 0;
  51.             for (int characterY = characterHeight / 7; characterY < characterHeight; characterY++) {
  52.                 pixelCount += characterPixels[character][characterY * characterWidth];
  53.             }
  54.             if (pixelCount <= characterHeight / 7) {
  55.                 characterScreenWidths[character]--;
  56.                 characterXOffsets[character] = 0;
  57.             }
  58.             pixelCount = 0;
  59.             for (int characterY = characterHeight / 7; characterY < characterHeight; characterY++) {
  60.                 pixelCount += characterPixels[character][characterWidth - 1 + characterY * characterWidth];
  61.             }
  62.             if (pixelCount <= characterHeight / 7) {
  63.                 characterScreenWidths[character]--;
  64.             }
  65.         }
  66.         if (large) {
  67.             characterScreenWidths[32] = characterScreenWidths[73];
  68.         } else {
  69.             characterScreenWidths[32] = characterScreenWidths[105];
  70.         }
  71.     }
  72.  
  73.     public void drawStringRA(String string, int x, int y, int color) {
  74.         drawRegularString(string, x - getStringWidth(string), y, color);
  75.     }
  76.  
  77.     public void drawString(String string, int x, int y, int color) {
  78.         drawRegularString(string, x - getStringWidth(string) / 2, y, color);
  79.     }
  80.  
  81.     public void drawCenteredShadowedString(String string, int x, int y, int color, boolean shadowed) {
  82.         drawShadowedString(string, x - getStringEffectWidth(string) / 2, y, shadowed, color);
  83.     }
  84.  
  85.     public int getStringEffectWidth(String string) {
  86.         if (string == null) {
  87.             return 0;
  88.         }
  89.         int width = 0;
  90.         for (int character = 0; character < string.length(); character++) {
  91.             if (string.charAt(character) == '@' && character + 4 < string.length()
  92.                     && string.charAt(character + 4) == '@') {
  93.                 character += 4;
  94.             } else {
  95.                 width += characterScreenWidths[string.charAt(character)];
  96.             }
  97.         }
  98.         return width;
  99.     }
  100.  
  101.     public int getStringWidth(String string) {
  102.         if (string == null) {
  103.             return 0;
  104.         }
  105.         int width = 0;
  106.         for (int character = 0; character < string.length(); character++) {
  107.             width += characterScreenWidths[string.charAt(character)];
  108.         }
  109.         return width;
  110.     }
  111.  
  112.     public void drawRegularString(String string, int x, int y, int color) {
  113.         if (string != null) {
  114.             y -= characterDefaultHeight;
  115.             for (int index = 0; index < string.length(); index++) {
  116.                 char character = string.charAt(index);
  117.                 if (character != ' ') {
  118.                     drawCharacter(characterPixels[character], x + characterXOffsets[character], y
  119.                             + characterYOffsets[character], characterWidths[character], characterHeights[character],
  120.                             color);
  121.                 }
  122.                 x += characterScreenWidths[character];
  123.             }
  124.         }
  125.     }
  126.  
  127.     public void drawCenteredStringWaveY(String string, int x, int y, int wave, int color) {
  128.         if (string != null) {
  129.             x -= getStringWidth(string) / 2;
  130.             y -= characterDefaultHeight;
  131.             for (int index = 0; index < string.length(); index++) {
  132.                 char character = string.charAt(index);
  133.                 if (character != ' ') {
  134.                     drawCharacter(characterPixels[character], x + characterXOffsets[character], y
  135.                             + characterYOffsets[character] + (int) (Math.sin(index / 2.0 + wave / 5.0) * 5.0),
  136.                             characterWidths[character], characterHeights[character], color);
  137.                 }
  138.                 x += characterScreenWidths[character];
  139.             }
  140.         }
  141.     }
  142.  
  143.     public void drawCeneteredStringWaveXY(String string, int x, int y, int wave, int color) {
  144.         if (string != null) {
  145.             x -= getStringWidth(string) / 2;
  146.             y -= characterDefaultHeight;
  147.             for (int index = 0; index < string.length(); index++) {
  148.                 char character = string.charAt(index);
  149.                 if (character != ' ') {
  150.                     drawCharacter(characterPixels[character],
  151.                             x + characterXOffsets[character] + (int) (Math.sin(index / 5.0 + wave / 5.0) * 5.0), y
  152.                                     + characterYOffsets[character] + (int) (Math.sin(index / 3.0 + wave / 5.0) * 5.0),
  153.                             characterWidths[character], characterHeights[character], color);
  154.                 }
  155.                 x += characterScreenWidths[character];
  156.             }
  157.         }
  158.     }
  159.  
  160.     public void drawCenteredStringWaveXYMove(String string, int x, int y, int waveAmount, int waveSpeed, int color) {
  161.         if (string != null) {
  162.             double speed = 7.0 - waveSpeed / 8.0;
  163.             if (speed < 0.0) {
  164.                 speed = 0.0;
  165.             }
  166.             x -= getStringWidth(string) / 2;
  167.             y -= characterDefaultHeight;
  168.             for (int index = 0; index < string.length(); index++) {
  169.                 char character = string.charAt(index);
  170.                 if (character != ' ') {
  171.                     drawCharacter(characterPixels[character], x + characterXOffsets[character], y
  172.                             + characterYOffsets[character] + (int) (Math.sin(index / 1.5 + waveAmount) * speed),
  173.                             characterWidths[character], characterHeights[character], color);
  174.                 }
  175.                 x += characterScreenWidths[character];
  176.             }
  177.         }
  178.     }
  179.  
  180.     public void drawShadowedString(String string, int x, int y, boolean shadow, int color) {
  181.         strikeThrough = false;
  182.         int originalX = x;
  183.         if (string != null) {
  184.             y -= characterDefaultHeight;
  185.             for (int character = 0; character < string.length(); character++) {
  186.                 if (string.charAt(character) == '@' && character + 4 < string.length()
  187.                         && string.charAt(character + 4) == '@') {
  188.                     int stringColor = getColor(string.substring(character + 1, character + 4));
  189.                     if (stringColor != -1) {
  190.                         color = stringColor;
  191.                     }
  192.                     character += 4;
  193.                 } else {
  194.                     char c = string.charAt(character);
  195.                     if (c != ' ') {
  196.                         if (shadow) {
  197.                             drawCharacter(characterPixels[c], x + characterXOffsets[c] + 1, y + characterYOffsets[c]
  198.                                     + 1, characterWidths[c], characterHeights[c], 0);
  199.                         }
  200.                         drawCharacter(characterPixels[c], x + characterXOffsets[c], y + characterYOffsets[c],
  201.                                 characterWidths[c], characterHeights[c], color);
  202.                     }
  203.                     x += characterScreenWidths[c];
  204.                 }
  205.             }
  206.             if (!strikeThrough) {
  207.                 return;
  208.             }
  209.             Rasterizer.drawHorizontalLine(originalX, y + (int) (characterDefaultHeight * 0.7), x - originalX, 8388608);
  210.         }
  211.     }
  212.  
  213.     public void drawShadowedSeededAlphaString(String string, int x, int y, int seed, int color) {
  214.         if (string != null) {
  215.             random.setSeed(seed);
  216.             int alpha = 192 + (random.nextInt() & 0x1f);
  217.             y -= characterDefaultHeight;
  218.             for (int index = 0; index < string.length(); index++) {
  219.                 if (string.charAt(index) == '@' && index + 4 < string.length() && string.charAt(index + 4) == '@') {
  220.                     int stringColor = getColor(string.substring(index + 1, index + 4));
  221.                     if (stringColor != -1) {
  222.                         color = stringColor;
  223.                     }
  224.                     index += 4;
  225.                 } else {
  226.                     char c = string.charAt(index);
  227.                     if (c != ' ') {
  228.                         drawAlphaCharacter(192, x + characterXOffsets[c] + 1, characterPixels[c], characterWidths[c], y
  229.                                 + characterYOffsets[c] + 1, characterHeights[c], 0);
  230.                         drawAlphaCharacter(alpha, x + characterXOffsets[c], characterPixels[c], characterWidths[c], y
  231.                                 + characterYOffsets[c], characterHeights[c], color);
  232.                     }
  233.                     x += characterScreenWidths[c];
  234.                     if ((random.nextInt() & 0x3) == 0) {
  235.                         x++;
  236.                     }
  237.                 }
  238.             }
  239.         }
  240.     }
  241.  
  242.     public int getColor(String color) {
  243.         if (color.equals("red")) {
  244.             return 16711680;
  245.         }
  246.         if (color.equals("gre")) {
  247.             return 65280;
  248.         }
  249.         if (color.equals("blu")) {
  250.             return 255;
  251.         }
  252.         if (color.equals("yel")) {
  253.             return 16776960;
  254.         }
  255.         if (color.equals("cya")) {
  256.             return 65535;
  257.         }
  258.         if (color.equals("mag")) {
  259.             return 16711935;
  260.         }
  261.         if (color.equals("whi")) {
  262.             return 16777215;
  263.         }
  264.         if (color.equals("bla")) {
  265.             return 0;
  266.         }
  267.         if (color.equals("lre")) {
  268.             return 16748608;
  269.         }
  270.         if (color.equals("dre")) {
  271.             return 8388608;
  272.         }
  273.         if (color.equals("dbl")) {
  274.             return 128;
  275.         }
  276.         if (color.equals("or1")) {
  277.             return 16756736;
  278.         }
  279.         if (color.equals("or2")) {
  280.             return 16740352;
  281.         }
  282.         if (color.equals("or3")) {
  283.             return 16723968;
  284.         }
  285.         if (color.equals("gr1")) {
  286.             return 12648192;
  287.         }
  288.         if (color.equals("gr2")) {
  289.             return 8453888;
  290.         }
  291.         if (color.equals("gr3")) {
  292.             return 4259584;
  293.         }
  294.         if (color.equals("str")) {
  295.             strikeThrough = true;
  296.         }
  297.         if (color.equals("end")) {
  298.             strikeThrough = false;
  299.         }
  300.         return -1;
  301.     }
  302.  
  303.     private void drawCharacter(byte[] pixels, int x, int y, int width, int height, int color) {
  304.         int rasterizerPixel = x + y * Rasterizer.width;
  305.         int remainingWidth = Rasterizer.width - width;
  306.         int characterPixelOffset = 0;
  307.         int characterPixel = 0;
  308.         if (y < Rasterizer.topY) {
  309.             int offsetY = Rasterizer.topY - y;
  310.             height -= offsetY;
  311.             y = Rasterizer.topY;
  312.             characterPixel += offsetY * width;
  313.             rasterizerPixel += offsetY * Rasterizer.width;
  314.         }
  315.         if (y + height >= Rasterizer.bottomY) {
  316.             height -= y + height - Rasterizer.bottomY + 1;
  317.         }
  318.         if (x < Rasterizer.topX) {
  319.             int offsetX = Rasterizer.topX - x;
  320.             width -= offsetX;
  321.             x = Rasterizer.topX;
  322.             characterPixel += offsetX;
  323.             rasterizerPixel += offsetX;
  324.             characterPixelOffset += offsetX;
  325.             remainingWidth += offsetX;
  326.         }
  327.         if (x + width >= Rasterizer.bottomX) {
  328.             int endOffsetX = x + width - Rasterizer.bottomX + 1;
  329.             width -= endOffsetX;
  330.             characterPixelOffset += endOffsetX;
  331.             remainingWidth += endOffsetX;
  332.         }
  333.         if (width > 0 && height > 0) {
  334.             drawCharacterPixels(pixels, Rasterizer.pixels, characterPixel, rasterizerPixel, characterPixelOffset,
  335.                     remainingWidth, width, height, color);
  336.         }
  337.     }
  338.  
  339.     private void drawCharacterPixels(byte[] characterPixels, int[] rasterizerPixels, int characterPixel,
  340.             int rasterizerPixel, int characterPixelOffset, int rasterizerPixelOffset, int width, int height, int color) {
  341.         int negativeQuaterWidth = -(width >> 2);
  342.         int negativeFirstTwoWidthBits = -(width & 0x3);
  343.         for (int heightCounter = -height; heightCounter < 0; heightCounter++) {
  344.             for (int widthCounter = negativeQuaterWidth; widthCounter < 0; widthCounter++) {
  345.                 if (characterPixels[characterPixel++] != 0) {
  346.                     rasterizerPixels[rasterizerPixel++] = color;
  347.                 } else {
  348.                     rasterizerPixel++;
  349.                 }
  350.                 if (characterPixels[characterPixel++] != 0) {
  351.                     rasterizerPixels[rasterizerPixel++] = color;
  352.                 } else {
  353.                     rasterizerPixel++;
  354.                 }
  355.                 if (characterPixels[characterPixel++] != 0) {
  356.                     rasterizerPixels[rasterizerPixel++] = color;
  357.                 } else {
  358.                     rasterizerPixel++;
  359.                 }
  360.                 if (characterPixels[characterPixel++] != 0) {
  361.                     rasterizerPixels[rasterizerPixel++] = color;
  362.                 } else {
  363.                     rasterizerPixel++;
  364.                 }
  365.             }
  366.             for (int widthCounter = negativeFirstTwoWidthBits; widthCounter < 0; widthCounter++) {
  367.                 if (characterPixels[characterPixel++] != 0) {
  368.                     rasterizerPixels[rasterizerPixel++] = color;
  369.                 } else {
  370.                     rasterizerPixel++;
  371.                 }
  372.             }
  373.             rasterizerPixel += rasterizerPixelOffset;
  374.             characterPixel += characterPixelOffset;
  375.         }
  376.     }
  377.  
  378.     private void drawAlphaCharacter(int alpha, int x, byte[] characterPixels, int width, int y, int height, int color) {
  379.         int rasterizerPixel = x + y * Rasterizer.width;
  380.         int rasterizerPixelOffset = Rasterizer.width - width;
  381.         int characterPixelOffset = 0;
  382.         int characterPixel = 0;
  383.         if (y < Rasterizer.topY) {
  384.             int yOffset = Rasterizer.topY - y;
  385.             height -= yOffset;
  386.             y = Rasterizer.topY;
  387.             characterPixel += yOffset * width;
  388.             rasterizerPixel += yOffset * Rasterizer.width;
  389.         }
  390.         if (y + height >= Rasterizer.bottomY) {
  391.             height -= y + height - Rasterizer.bottomY + 1;
  392.         }
  393.         if (x < Rasterizer.topX) {
  394.             int xOffset = Rasterizer.topX - x;
  395.             width -= xOffset;
  396.             x = Rasterizer.topX;
  397.             characterPixel += xOffset;
  398.             rasterizerPixel += xOffset;
  399.             characterPixelOffset += xOffset;
  400.             rasterizerPixelOffset += xOffset;
  401.         }
  402.         if (x + width >= Rasterizer.bottomX) {
  403.             int widthoffset = x + width - Rasterizer.bottomX + 1;
  404.             width -= widthoffset;
  405.             characterPixelOffset += widthoffset;
  406.             rasterizerPixelOffset += widthoffset;
  407.         }
  408.         if (width > 0 && height > 0) {
  409.             drawCharacterPixelsAlpha(characterPixels, Rasterizer.pixels, characterPixel, rasterizerPixel, characterPixelOffset, rasterizerPixelOffset, width, height, color, alpha);
  410.         }
  411.     }
  412.  
  413.     private void drawCharacterPixelsAlpha(byte[] characterPixels, int[] rasterizerPixels, int characterPixel, int rasterizerPixel, int characterPixelOffset, int rasterizerPixelOffset, int width, int height,
  414.             int color, int alpha) {
  415.         color = ((color & 0xff00ff) * alpha & ~0xff00ff) + ((color & 0xff00) * alpha & 0xff0000) >> 8;
  416.         alpha = 256 - alpha;
  417.         for (int heightCounter = -height; heightCounter < 0; heightCounter++) {
  418.             for (int widthCounter = -width; widthCounter < 0; widthCounter++) {
  419.                 if (characterPixels[characterPixel++] != 0) {
  420.                     int rasterizerPixelColor = rasterizerPixels[rasterizerPixel];
  421.                     rasterizerPixels[rasterizerPixel++] = (((rasterizerPixelColor & 0xff00ff) * alpha & ~0xff00ff) + ((rasterizerPixelColor & 0xff00) * alpha & 0xff0000) >> 8)
  422.                             + color;
  423.                 } else {
  424.                     rasterizerPixel++;
  425.                 }
  426.             }
  427.             rasterizerPixel += rasterizerPixelOffset;
  428.             characterPixel += characterPixelOffset;
  429.         }
  430.     }
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement