feat: frame font

This commit is contained in:
Hinnka committed 2026-01-25 11:40:26 +08:00
1 parent d78dc58d9b
commit bad317bb7a
7 files changed
+45 -33

No files matched your search

Binary file not shown.
@@ -17,9 +17,10 @@
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "START",
"fontSize": 16,
"fontSize": 20,
"color": "#000000",
"fontWeight": "BOLD",
"fontFamily": "SlacksideOne.ttf",
"line": -1
},
{
@@ -45,7 +46,7 @@
"textType": "APERTURE",
"alignment": "END",
"fontSize": 16,
"color": "#000000",
"color": "#333333",
"fontWeight": "BOLD",
"line": 0
},
@@ -54,7 +55,7 @@
"textType": "FOCAL_LENGTH_35MM",
"alignment": "END",
"fontSize": 16,
"color": "#000000",
"color": "#333333",
"fontWeight": "BOLD",
"line": 0
},
@@ -63,7 +64,7 @@
"textType": "SHUTTER_SPEED",
"alignment": "END",
"fontSize": 16,
"color": "#000000",
"color": "#333333",
"fontWeight": "BOLD",
"line": 0
},
@@ -72,7 +73,7 @@
"textType": "ISO",
"alignment": "END",
"fontSize": 16,
"color": "#000000",
"color": "#333333",
"fontWeight": "BOLD",
"line": 0
},
@@ -81,7 +82,7 @@
"textType": "DATETIME",
"alignment": "END",
"fontSize": 12,
"color": "#999999",
"color": "#666666",
"format": "yyyy.MM.dd HH:mm:ss",
"line": 1
}
@@ -26,16 +26,17 @@
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "CENTER",
"fontSize": 24,
"fontSize": 28,
"color": "#C6AE7A",
"fontWeight": "BOLD",
"fontFamily": "SlacksideOne.ttf",
"line": 1
},
{
"type": "text",
"textType": "FOCAL_LENGTH_35MM",
"alignment": "CENTER",
"fontSize": 16,
"fontSize": 14,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
@@ -44,7 +45,7 @@
"type": "text",
"textType": "APERTURE",
"alignment": "CENTER",
"fontSize": 16,
"fontSize": 14,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
@@ -53,7 +54,7 @@
"type": "text",
"textType": "SHUTTER_SPEED",
"alignment": "CENTER",
"fontSize": 16,
"fontSize": 14,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
@@ -62,7 +63,7 @@
"type": "text",
"textType": "ISO",
"alignment": "CENTER",
"fontSize": 16,
"fontSize": 14,
"color": "#C6AE7A",
"fontWeight": "NORMAL",
"line": 2
+7 -6
View File
@@ -10,7 +10,7 @@
"position": "OVERLAY",
"height": 150,
"backgroundColor": "#EE000000",
"padding": 20
"padding": 10
},
"elements": [
{
@@ -25,9 +25,10 @@
"type": "text",
"textType": "DEVICE_MODEL",
"alignment": "CENTER",
"fontSize": 16,
"fontSize": 30,
"color": "#FFFFFF",
"fontWeight": "MEDIUM",
"fontFamily": "SlacksideOne.ttf",
"line": 1
},
{
@@ -35,7 +36,7 @@
"textType": "FOCAL_LENGTH_35MM",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"color": "#FDFDFD",
"fontWeight": "NORMAL",
"line": 2
},
@@ -44,7 +45,7 @@
"textType": "APERTURE",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"color": "#FDFDFD",
"fontWeight": "NORMAL",
"line": 2
},
@@ -53,7 +54,7 @@
"textType": "SHUTTER_SPEED",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"color": "#FDFDFD",
"fontWeight": "NORMAL",
"line": 2
},
@@ -62,7 +63,7 @@
"textType": "ISO",
"alignment": "CENTER",
"fontSize": 14,
"color": "#AAAAAA",
"color": "#FDFDFD",
"fontWeight": "NORMAL",
"line": 2
}
@@ -96,6 +96,7 @@ sealed class FrameElement {
val fontSizeSp: Int = 14,
val color: Int = Color.DKGRAY,
val fontWeight: FontWeight = FontWeight.NORMAL,
val fontFamily: String? = null,
val format: String? = null,
val prefix: String? = null,
val suffix: String? = null,
@@ -184,18 +184,6 @@ class FrameRenderer(private val context: Context) {
// 叠加模式:绘制从全透明到半透明的渐变背景
val overlayTop = (originalBitmap.height - frameHeight).toFloat()
// 创建线性渐变:从顶部全透明到底部半透明
val gradientShader = LinearGradient(
0f, overlayTop,
0f, outputHeight.toFloat(),
Color.TRANSPARENT,
layout.backgroundColor,
Shader.TileMode.CLAMP
)
backgroundPaint.shader = gradientShader
canvas.drawRect(0f, overlayTop, outputWidth.toFloat(), outputHeight.toFloat(), backgroundPaint)
backgroundPaint.shader = null // 重置 shader
// 绘制水印内容
drawFrameContent(
canvas, template.elements, metadata,
@@ -255,7 +243,7 @@ class FrameRenderer(private val context: Context) {
val height = bottom - top
val logoLineSet = visibleElements.filterIsInstance<FrameElement.Logo>().map { it.line }.toSet()
val lineWeights = allLines.map { if (logoLineSet.contains(it)) 3f else 1.0f }
val lineWeights = allLines.map { if (logoLineSet.contains(it)) 2f else 1.0f }
val totalWeight = lineWeights.sum()
/**
@@ -452,7 +440,7 @@ class FrameRenderer(private val context: Context) {
is FrameElement.Text -> {
val text = getTextContent(element, metadata) ?: return 0f
textPaint.textSize = spToPx(element.fontSizeSp) * scale
textPaint.typeface = getTypeface(element.fontWeight)
textPaint.typeface = getTypeface(element.fontWeight, element.fontFamily)
textPaint.measureText(text) + dpToPx(8) * scale
}
@@ -533,7 +521,7 @@ class FrameRenderer(private val context: Context) {
textPaint.color = element.color
textPaint.textSize = spToPx(element.fontSizeSp) * scale
textPaint.typeface = getTypeface(element.fontWeight)
textPaint.typeface = getTypeface(element.fontWeight, element.fontFamily)
val textWidth = textPaint.measureText(text)
val textHeight = textPaint.descent() - textPaint.ascent()
@@ -970,7 +958,26 @@ class FrameRenderer(private val context: Context) {
}
}
private fun getTypeface(weight: FontWeight): Typeface {
private val typefaceCache = mutableMapOf<String, Typeface>()
private fun getTypeface(weight: FontWeight, fontFamily: String? = null): Typeface {
if (fontFamily != null) {
val cacheKey = "$fontFamily-$weight"
typefaceCache[cacheKey]?.let { return it }
try {
val base = Typeface.createFromAsset(context.assets, "fonts/$fontFamily")
val style = when (weight) {
FontWeight.BOLD -> Typeface.BOLD
else -> Typeface.NORMAL
}
val typeface = Typeface.create(base, style)
typefaceCache[cacheKey] = typeface
return typeface
} catch (e: Exception) {
PLog.e(TAG, "Failed to load font: fonts/$fontFamily", e)
}
}
return when (weight) {
FontWeight.NORMAL -> Typeface.DEFAULT
FontWeight.MEDIUM -> Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
@@ -194,6 +194,7 @@ object FrameTemplateParser {
fontSizeSp = obj.optInt("fontSize", 14),
color = parseColor(obj.optString("color", "#333333")),
fontWeight = FontWeight.valueOf(obj.optString("fontWeight", "NORMAL")),
fontFamily = obj.optString("fontFamily").takeIf { it.isNotEmpty() },
format = obj.optString("format").takeIf { it.isNotEmpty() },
prefix = obj.optString("prefix").takeIf { it.isNotEmpty() },
suffix = obj.optString("suffix").takeIf { it.isNotEmpty() },