mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-25 23:29:47 +07:00
all codes are writen in Kotlin (#1452)
This commit is contained in:

committed by
Yair Morgenstern

parent
5d75c3d65e
commit
e937ea0af1
@ -1,78 +0,0 @@
|
||||
package core.java.nativefont;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Typeface;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.backends.android.AndroidApplication;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.Pixmap;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Created by tian on 2016/10/2.
|
||||
*/
|
||||
|
||||
public class NativeFontAndroid implements NativeFontListener {
|
||||
private HashMap<String, Typeface> fontFaces = new HashMap<String, Typeface>();
|
||||
private AndroidApplication androidApplication = (AndroidApplication) Gdx.app;
|
||||
@Override
|
||||
public Pixmap getFontPixmap(String txt, NativeFontPaint vpaint) {
|
||||
Paint paint = new Paint();
|
||||
|
||||
if (!vpaint.getTTFName().equals("")) {
|
||||
// Typeface fontFace = fontFaces.get(vpaint.getTTFName());
|
||||
Gdx.app.log("app",Gdx.files.internal(vpaint.getTTFName()
|
||||
+ (vpaint.getTTFName().endsWith(".ttf") ? ""
|
||||
: ".ttf")).file().getPath());
|
||||
Typeface fontFace = Typeface.createFromAsset(androidApplication.getAssets(),vpaint.getTTFName()
|
||||
+ (vpaint.getTTFName().endsWith(".ttf") ? ""
|
||||
: ".ttf"));
|
||||
fontFaces.put(vpaint.getTTFName(), fontFace);
|
||||
paint.setTypeface(fontFace);
|
||||
}
|
||||
paint.setAntiAlias(true);
|
||||
paint.setTextSize(vpaint.getTextSize());
|
||||
Paint.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(Paint.Style.FILL_AND_STROKE); // 描边种类
|
||||
paint.setFakeBoldText(true); // 外层text采用粗体
|
||||
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(Bitmap.CompressFormat.PNG, 100, buffer);
|
||||
byte[] encodedData = buffer.toByteArray();
|
||||
Pixmap pixmap = new Pixmap(encodedData, 0, encodedData.length);
|
||||
bitmap = null;
|
||||
canvas = null;
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
private int getColor(Color color) {
|
||||
return (((((int) (color.a * 255.0f)) << 24) | (((int) (color.r * 255.0f)) << 16)) | (((int) (color.g * 255.0f)) << 8)) | ((int) (color.b * 255.0f));
|
||||
}
|
||||
}
|
70
android/src/core/java/nativefont/NativeFontAndroid.kt
Executable file
70
android/src/core/java/nativefont/NativeFontAndroid.kt
Executable file
@ -0,0 +1,70 @@
|
||||
package core.java.nativefont
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Typeface
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.backends.android.AndroidApplication
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.Pixmap
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by tian on 2016/10/2.
|
||||
*/
|
||||
class NativeFontAndroid : NativeFontListener {
|
||||
private val fontFaces = HashMap<String, Typeface>()
|
||||
private val androidApplication = Gdx.app as AndroidApplication
|
||||
override fun getFontPixmap(txt: String, vpaint: NativeFontPaint): Pixmap {
|
||||
val paint = Paint()
|
||||
if (vpaint.tTFName != "") {
|
||||
Gdx.app.log("app", Gdx.files.internal(vpaint.tTFName +
|
||||
if (vpaint.tTFName.endsWith(".ttf")) "" else ".ttf").file().path)
|
||||
val fontFace = Typeface.createFromAsset(androidApplication.assets, vpaint.tTFName +
|
||||
if (vpaint.tTFName.endsWith(".ttf")) "" else ".ttf")
|
||||
fontFaces[vpaint.tTFName] = fontFace
|
||||
paint.typeface = fontFace
|
||||
}
|
||||
paint.isAntiAlias = true
|
||||
paint.textSize = vpaint.textSize.toFloat()
|
||||
val fm = paint.fontMetrics
|
||||
var w = paint.measureText(txt).toInt()
|
||||
var h = (fm.descent - fm.ascent).toInt()
|
||||
if (w == 0) {
|
||||
h = vpaint.textSize
|
||||
w = h
|
||||
}
|
||||
var bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
|
||||
var canvas: Canvas? = Canvas(bitmap)
|
||||
// 如果是描边类型
|
||||
if (vpaint.strokeColor != null) { // 绘制外层
|
||||
paint.color = getColor(vpaint.strokeColor)
|
||||
paint.strokeWidth = vpaint.strokeWidth.toFloat() // 描边宽度
|
||||
paint.style = Paint.Style.FILL_AND_STROKE // 描边种类
|
||||
paint.isFakeBoldText = true // 外层text采用粗体
|
||||
canvas!!.drawText(txt, 0f, -fm.ascent, paint)
|
||||
paint.isFakeBoldText = false
|
||||
} else {
|
||||
paint.isUnderlineText = vpaint.underlineText
|
||||
paint.isStrikeThruText = vpaint.strikeThruText
|
||||
paint.isFakeBoldText = vpaint.fakeBoldText
|
||||
}
|
||||
// 绘制内层
|
||||
paint.strokeWidth = 0f
|
||||
paint.color = getColor(vpaint.color)
|
||||
canvas!!.drawText(txt, 0f, -fm.ascent, paint)
|
||||
val buffer = ByteArrayOutputStream()
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, buffer)
|
||||
val encodedData = buffer.toByteArray()
|
||||
val pixmap = Pixmap(encodedData, 0, encodedData.size)
|
||||
buffer.close()
|
||||
bitmap.recycle()
|
||||
return pixmap
|
||||
}
|
||||
|
||||
private fun getColor(color: Color?): Int {
|
||||
return (color!!.a * 255.0f).toInt() shl 24 or ((color.r * 255.0f).toInt() shl 16) or ((color.g * 255.0f).toInt() shl 8) or (color.b * 255.0f).toInt()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user