Canvas.drawText() 不会在 Android 上呈现大表情符号

2024-03-07

Canvas.drawText() 不渲染表情符号在 Android 上超过一定的字体大小。

Correct render at somewhere below 256 px: emoji correctly rendered

Incorrect render at above 256 px: enter image description here

(有一个类似的问题 https://stackoverflow.com/questions/29142118/why-do-emoji-not-render-above-a-certain-size-in-chrome关于 Google Chrome,就像 Android 一样,Chrome 也使用Skia图形库,所以这看起来像斯基亚中的错误.)

显然,表情符号无法在我的设备上呈现超过 256 px 的字体大小。但我不确定这是否是所有地方的限制。

有没有办法了解表情符号消失时的字体大小?或者有解决方法吗?


我提出了一个测试(经验估计)最大字体大小仍然可以渲染表情符号。

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

我选择地球仪表情符号是因为我认为我们可以相当确定没有艺术家会画出中间有洞的地球。 (否则我们就有大麻烦了。)

测试以二分搜索方式完成,因此运行时间应该是对数的。

(Fun fact:我的两部测试手机上的最大字体大小是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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Canvas.drawText() 不会在 Android 上呈现大表情符号 的相关文章

随机推荐