mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-28 13:48:12 +00:00
Do not allow emoji in image editing if device doesn't support it.
This commit is contained in:
committed by
Cody Henthorne
parent
3328e43a40
commit
d409278dd5
40
core-util/src/main/java/org/signal/core/util/FontUtil.kt
Normal file
40
core-util/src/main/java/org/signal/core/util/FontUtil.kt
Normal file
@@ -0,0 +1,40 @@
|
||||
package org.signal.core.util
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.PorterDuff
|
||||
import kotlin.math.abs
|
||||
|
||||
|
||||
object FontUtil {
|
||||
private const val SAMPLE_EMOJI = "\uD83C\uDF0D" // 🌍
|
||||
|
||||
/**
|
||||
* Certain platforms cannot render emoji above a certain font size.
|
||||
*
|
||||
* This will attempt to render an emoji at the specified font size and tell you if it's possible.
|
||||
* It does this by rendering an emoji into a 1x1 bitmap and seeing if the resulting pixel is non-transparent.
|
||||
*
|
||||
* https://stackoverflow.com/a/50988748
|
||||
*/
|
||||
@JvmStatic
|
||||
fun canRenderEmojiAtFontSize(size: Float): Boolean {
|
||||
val bitmap: Bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
|
||||
val canvas = Canvas(bitmap)
|
||||
val paint = Paint()
|
||||
|
||||
paint.textSize = size
|
||||
paint.textAlign = Paint.Align.CENTER
|
||||
|
||||
val ascent: Float = abs(paint.ascent())
|
||||
val descent: Float = abs(paint.descent())
|
||||
val halfHeight = (ascent + descent) / 2.0f
|
||||
|
||||
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
|
||||
canvas.drawText(SAMPLE_EMOJI, 0.5f, 0.5f + halfHeight - descent, paint)
|
||||
|
||||
return bitmap.getPixel(0, 0) != 0
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package org.signal.core.util;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class ListUtil {
|
||||
private ListUtil() {}
|
||||
@@ -18,4 +20,15 @@ public final class ListUtil {
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> List<T> concat(Collection<T>... items) {
|
||||
final List<T> concat = new ArrayList<>(Stream.of(items).map(Collection::size).reduce(0, Integer::sum));
|
||||
|
||||
for (Collection<T> list : items) {
|
||||
concat.addAll(list);
|
||||
}
|
||||
|
||||
return concat;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user