?? fontgrabber.java
字號:
import java.io.*;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
/**
* FontGrabber - Build an Image with a text and bitmap fonts.
* @version 1.0
* @author S.H.
* @company JavaZOOM.
* @date 01/2000.
*
* Use of this source and binary code is permitted only for non-commercial purposes.
* Modification is allowed with this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
public class fontgrabber
{
private int[] theFonts;
private int imageW;
private int imageH;
private int fontWidth;
private int fontHeight;
private int Yspacing;
private MemoryImageSource theImageProd = null;
private int pixels[];
private String theText;
/**
* Constructor.
*
* @param fi Font file (RGB data + Descriptor).
* @param theTxt The text to build.
* @param BgValue Background color to apply.
*/
public fontgrabber(fontinfo fi, String theTxt, int BgValue)
{
fontWidth = fi.getWidth();
fontHeight = fi.getHeight();
Yspacing = fi.getYSpacing();
theText = theTxt;
theFonts = fi.getData();
imageW = fi.getImageWidth();
imageH = fi.getImageHeight();
String alphaIndex = fi.getIndex();
/*-- We create the TextBanner by grabbing font letters in the image fonts --*/
pixels = new int[theText.length() * fontWidth * fontHeight];
int SpacePosition = 0;
int offsetSp = 0;
/*-- We search the space position in the Alphabet index --*/
while ( (offsetSp < alphaIndex.length()) && (alphaIndex.charAt(offsetSp) != ' ') )
{offsetSp++;}
if (offsetSp < alphaIndex.length()) SpacePosition = offsetSp;
for (int offsetT = 0;offsetT < theText.length(); offsetT++)
{
int xPos = 0;
int yPos = 0;
int reste = 0;
int entie = 0;
int offsetA = 0;
int FontPerLine = (int) Math.rint((imageW/fontWidth));
/*-- We search the letter's position in the Alphabet index --*/
while ( (offsetA < alphaIndex.length()) && (theText.charAt(offsetT) != alphaIndex.charAt(offsetA)) )
{
offsetA++;
}
/*-- We deduce its image's position (Int forced) --*/
if (offsetA < alphaIndex.length())
{
reste = offsetA % FontPerLine;
entie = (offsetA - reste);
xPos = reste * fontWidth;
yPos = ((entie/FontPerLine) * fontHeight) + ((entie/FontPerLine)*Yspacing);
}
else
/*-- If the letter is not indexed the space (if available) is selected --*/
{
reste = SpacePosition % FontPerLine;
entie = (SpacePosition- reste);
xPos = reste * fontWidth;
yPos = ((entie/FontPerLine) * fontHeight) + ((entie/FontPerLine)*Yspacing);
}
/*-- We grab the letter in the font image and put it in a pixel array --*/
for (int l=0;l<fontHeight;l++)
{
for (int k=0;k<fontWidth;k++)
{
pixels[l*(theText.length()*fontWidth) + offsetT*fontWidth + k] = theFonts[(yPos+l)*imageW + xPos + k];
}
}
}
/*-- Change 0x000000 to bgColor --*/
int Couleur = BgValue;
for (int k=0;k<theText.length() * fontWidth * fontHeight;k++)
{
if ( ((pixels[k])&(0x00ffffff)) == 0) pixels[k] = Couleur;
}
/*-- We create the final Producer --*/
theImageProd = new MemoryImageSource(theText.length()*fontWidth, fontHeight, pixels, 0, theText.length()*fontWidth);
}
/**
* Returns an ImageProducer.
*/
public MemoryImageSource getImageProd()
{
return theImageProd;
}
/**
* Returns the pixel array matching to the Image.
*/
public int[] getPixels()
{
return pixels;
}
/**
* Returns Image Width.
*/
public int getPixelsW()
{
return theText.length()*fontWidth;
}
/**
* Returns Image Height
*/
public int getPixelsH()
{
return fontHeight;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -