Search in sources :

Example 1 with FreePaint

use of var3d.net.center.freefont.FreePaint in project var3dframe by Var3D.

the class VGame method create.

public void create() {
    // 数据存储实例化
    save = Gdx.app.getPreferences(getProjectName());
    // 劫持系统返回键
    Gdx.input.setCatchBackKey(true);
    // 触控实例化
    multiplexer = new InputMultiplexer();
    // 
    Gdx.input.setInputProcessor(multiplexer);
    stageTop = new StageTop(this);
    stageTop.setOff();
    isMusic = save.getBoolean("isMusic", true);
    isSound = save.getBoolean("isSound", true);
    // 全球化字体方案
    if (bundle == null)
        bundle = new VBundle(var3dListener);
    // 创建一个默认动态文本
    FreeBitmapFont font = new FreeBitmapFont(this, new FreePaint(getDefaultFontSize()));
    font.appendText("01234567890LoadingC" + getHeap());
    fonts.put("font", font);
    setStageLoad(StageLoad.class);
    init();
    var3dListener.create();
}
Also used : InputMultiplexer(com.badlogic.gdx.InputMultiplexer) FreePaint(var3d.net.center.freefont.FreePaint) FreeBitmapFont(var3d.net.center.freefont.FreeBitmapFont)

Example 2 with FreePaint

use of var3d.net.center.freefont.FreePaint in project var3dframe by Var3D.

the class Game method init.

@Override
public void init() {
    // openAutoScreenshots(5,StageTest.class);//开启自动截图
    // 不设置这一句无法使用R.strings.xxxxx的形式使用多语言,但可以使用其他在assets里的资源
    setResources(R.class);
    // openProtect("image");//加密image文件夹资源
    // unProtect("image");
    // showFps();//显示fps
    setFont("test", new FreeBitmapFont(this, new FreePaint(R.font.DroidSans)));
    // 设置入口界面
    setStage(StageTest.class);
}
Also used : FreePaint(var3d.net.center.freefont.FreePaint) FreeBitmapFont(var3d.net.center.freefont.FreeBitmapFont)

Example 3 with FreePaint

use of var3d.net.center.freefont.FreePaint in project var3dframe by Var3D.

the class VDesktopLauncher method getFontPixmap.

public Pixmap getFontPixmap(String txt, FreePaint vpaint) {
    Font font = getFont(vpaint);
    FontMetrics fm = metrics.get(vpaint.getName());
    int strWidth = fm.stringWidth(txt);
    int strHeight = fm.getAscent() + fm.getDescent();
    if (strWidth == 0) {
        strWidth = strHeight = vpaint.getTextSize();
    }
    BufferedImage bi = new BufferedImage(strWidth, strHeight, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setFont(font);
    if (vpaint.getStrokeColor() != null) {
        // 描边
        GlyphVector v = font.createGlyphVector(fm.getFontRenderContext(), txt);
        Shape shape = v.getOutline();
        g.setColor(getColor(vpaint.getColor()));
        g.translate(0, fm.getAscent());
        g.fill(shape);
        g.setStroke(new BasicStroke(vpaint.getStrokeWidth()));
        g.setColor(getColor(vpaint.getStrokeColor()));
        g.draw(shape);
    } else if (vpaint.getUnderlineText() == true) {
        // 下划线
        AttributedString as = new AttributedString(txt);
        as.addAttribute(TextAttribute.FONT, font);
        as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        g.setColor(getColor(vpaint.getColor()));
        g.drawString(as.getIterator(), 0, fm.getAscent());
    } else if (vpaint.getStrikeThruText() == true) {
        // 删除线
        AttributedString as = new AttributedString(txt);
        as.addAttribute(TextAttribute.FONT, font);
        as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
        g.setColor(getColor(vpaint.getColor()));
        g.drawString(as.getIterator(), 0, fm.getAscent());
    } else {
        // 普通
        g.setColor(getColor(vpaint.getColor()));
        g.drawString(txt, 0, fm.getAscent());
    }
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try {
        ImageIO.write(bi, "png", buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Pixmap pixmap = new Pixmap(buffer.toByteArray(), 0, buffer.toByteArray().length);
    return pixmap;
}
Also used : BasicStroke(java.awt.BasicStroke) GlyphVector(java.awt.font.GlyphVector) Shape(java.awt.Shape) AttributedString(java.text.AttributedString) FontMetrics(java.awt.FontMetrics) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Font(java.awt.Font) FreePaint(var3d.net.center.freefont.FreePaint) BufferedImage(java.awt.image.BufferedImage) Pixmap(com.badlogic.gdx.graphics.Pixmap) Graphics2D(java.awt.Graphics2D)

Example 4 with FreePaint

use of var3d.net.center.freefont.FreePaint in project var3dframe by Var3D.

the class VAndroidLauncher method getFontPixmap.

public Pixmap getFontPixmap(String txt, FreePaint vpaint) {
    Paint paint = new Paint();
    if (!vpaint.getTTFName().equals("")) {
        // Typeface fontFace = fontFaces.get(vpaint.getTTFName());
        Typeface fontFace = Typeface.createFromAsset(getAssets(), vpaint.getTTFName() + (vpaint.getTTFName().endsWith(".ttf") ? "" : ".ttf"));
        fontFaces.put(vpaint.getTTFName(), fontFace);
        paint.setTypeface(fontFace);
    }
    paint.setAntiAlias(true);
    paint.setTextSize(vpaint.getTextSize());
    FontMetrics fm = paint.getFontMetrics();
    int w = (int) paint.measureText(txt);
    int h = (int) (fm.descent - fm.ascent);
    if (w == 0) {
        w = h = vpaint.getTextSize();
    }
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    // 如果是描边类型
    if (vpaint.getStrokeColor() != null) {
        // 绘制外层
        paint.setColor(getColor(vpaint.getStrokeColor()));
        // 描边宽度
        paint.setStrokeWidth(vpaint.getStrokeWidth());
        // 描边种类
        paint.setStyle(Style.FILL_AND_STROKE);
        // 外层text采用粗体
        paint.setFakeBoldText(true);
        canvas.drawText(txt, 0, -fm.ascent, paint);
        paint.setFakeBoldText(false);
    } else {
        paint.setUnderlineText(vpaint.getUnderlineText());
        paint.setStrikeThruText(vpaint.getStrikeThruText());
        paint.setFakeBoldText(vpaint.getFakeBoldText());
    }
    // 绘制内层
    paint.setStrokeWidth(0);
    paint.setColor(getColor(vpaint.getColor()));
    canvas.drawText(txt, 0, -fm.ascent, paint);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 100, buffer);
    byte[] encodedData = buffer.toByteArray();
    Pixmap pixmap = new Pixmap(encodedData, 0, encodedData.length);
    bitmap = null;
    canvas = null;
    return pixmap;
}
Also used : Bitmap(android.graphics.Bitmap) Typeface(android.graphics.Typeface) FontMetrics(android.graphics.Paint.FontMetrics) Canvas(android.graphics.Canvas) FreePaint(var3d.net.center.freefont.FreePaint) Paint(android.graphics.Paint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FreePaint(var3d.net.center.freefont.FreePaint) Paint(android.graphics.Paint) Pixmap(com.badlogic.gdx.graphics.Pixmap)

Aggregations

FreePaint (var3d.net.center.freefont.FreePaint)4 Pixmap (com.badlogic.gdx.graphics.Pixmap)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FreeBitmapFont (var3d.net.center.freefont.FreeBitmapFont)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Paint (android.graphics.Paint)1 FontMetrics (android.graphics.Paint.FontMetrics)1 Typeface (android.graphics.Typeface)1 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 BasicStroke (java.awt.BasicStroke)1 Font (java.awt.Font)1 FontMetrics (java.awt.FontMetrics)1 Graphics2D (java.awt.Graphics2D)1 Shape (java.awt.Shape)1 GlyphVector (java.awt.font.GlyphVector)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 AttributedString (java.text.AttributedString)1