Canvas.drawText()在Android上无法渲染大型表情符号

16
< p > Canvas.drawText() 在安卓系统上,在某个字体大小之上无法正确渲染表情符号

正确渲染在256像素以下: 表情符号正确渲染

错误渲染在256像素以上: 图片描述

(关于Google Chrome也有一个类似的问题,就像安卓一样,Chrome也使用Skia图形库,因此这似乎是Skia中的bug)。

显然,在我的设备上,在256像素以上的字体大小无法正确渲染表情符号。但我不确定这是否是所有设备的限制。

是否有一种方法可以知道表情符号消失的字体大小?或者是否有解决方法?


4
不仅 Canvas.drawText()TextView 也不能绘制大型表情符号。我自己也做了测试,发现最大尺寸为256,但不知道 Skia 中的错误。现在有了解释,感谢! - Jenix
还会在使用StaticLayout和DynamicLayout类渲染文本时失败。为了自己的目的,如果对您有用,作为解决方法,我首先将其绘制到较小的位图中,然后将较小的位图渲染到最终位图中,根据需要进行缩放。 - PerracoLabs
1个回答

10

我想出了一种测试(经验估计)最大字体大小的方法,以便继续渲染表情符号。

这个函数的工作方式是创建一个1x1的位图,并尝试在其中心绘制地球球体表情符号()。然后它检查单个像素,无论它是透明还是彩色。

我选择地球球体表情符号,因为我认为我们可以相当确信,任何艺术家都不会在地球中间钻一个洞。(否则我们就大麻烦了。)

测试采用二进制搜索方式进行,因此运行时间应该是对数级别的。

有趣的事实:我的两部测试手机上的最大字体大小都是256。)

public static float getMaxEmojiFontSize() {
    return getMaxEmojiFontSize(new Paint(), 8, 999999, 1);
}

/**
 * Emojis cannot be renderered above a certain font size due to a bug.
 * This function tries to estimate what the maximum font size is where emojis can still
 * be rendered.
 * @param   p           A Paint object to do the testing with.
 *                      A simple `new Paint()` should do.
 * @param   minTestSize From what size should we test if the emojis can be rendered.
 *                      We're assuming that at this size, emojis can be rendered.
 *                      A good value for this is 8.
 * @param   maxTestSize Until what size should we test if the emojis can be rendered.
 *                      This can be the max font size you're planning to use.
 * @param   maxError    How close should we be to the actual number with our estimation.
 *                      For example, if this is 10, and the result from this function is
 *                      240, then the maximum font size that still renders is guaranteed
 *                      to be under 250. (If maxTestSize is above 250.)
 */
public static float getMaxEmojiFontSize(Paint p, float minTestSize, float maxTestSize, float maxError) {
    Bitmap b = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    float sizeLowerLimit = minTestSize; // Start testing from this size
    float sizeUpperLimit = maxTestSize;
    Canvas c = new Canvas(b);
    float size;
    for (size = sizeLowerLimit; size < maxTestSize; size *= 2) {
        if (!canRenderEmoji(b, c, p, size)) {
            sizeUpperLimit = size;
            break;
        }
        sizeLowerLimit = size;
    }
    // We now have a lower and upper limit for the maximum emoji size.
    // Let's proceed with a binary search.
    while (sizeUpperLimit - sizeLowerLimit > maxError) {
        float middleSize = (sizeUpperLimit + sizeLowerLimit) / 2f;
        if (!canRenderEmoji(b, c, p, middleSize)) {
            sizeUpperLimit = middleSize;
        } else {
            sizeLowerLimit = middleSize;
        }
    }
    return sizeLowerLimit;
}

private static boolean canRenderEmoji(Bitmap b, Canvas can, Paint p, float size) {
    final String EMOJI = "\uD83C\uDF0D"; // the Earth Globe (Europe, Africa) emoji - should never be transparent in the center.
    can.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); // clear the canvas with transparent
    p.setTextSize(size);
    { // draw the emoji in the center
        float ascent = Math.abs(p.ascent());
        float descent = Math.abs(p.descent());
        float halfHeight = (ascent + descent) / 2.0f;
        p.setTextAlign(Paint.Align.CENTER);
        can.drawText(EMOJI, 0.5f, 0.5f + halfHeight - descent, p);
    }
    return b.getPixel(0, 0) != 0;
}

1
更新:最大字体大小似乎一直保持在256,直到Android 10。从Android 11开始,它似乎跳到了数千(在我的一些测试中为6000+,但不同设备上的值不同-可能与设备屏幕大小有关?)。表情符号被正确渲染(尽管在那个大小下它相当模糊)。上述算法仍然有效,但运行时间可能需要几秒钟,特别是在像示例中这样低的maxError下。 - Attila Tanyi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接