SKIA - Inaccurate value returned by measureText()

空扰寡人 提交于 2019-12-06 04:09:46

I believe what you are trying to match will come from "bounds"

SkRect bounds;
SkScalar textWidth = paint.measureText("some", 4, &bounds);

which is a minimum rectangle to fit a given text, whereas textWidth is slightly larger than that.

I faced this issue too. Dont know why exactly it happens, maybe because of kerning differences, but i came to this:

SizeF RenderTextAndroid::GetStringSizeF() {
    UpdateFont();
    const base::string16& text = GetLayoutText();
    std::vector<SkScalar> widths(text.length());
    paint_.getTextWidths(text.c_str(), GetStrByteLen(text), &widths[0], NULL);
    return SizeF(std::accumulate(widths.begin(), widths.end(), 0),
        font_metrics_.fBottom - font_metrics_.fTop);
}

Where UpdateFont just sets new parameters to SkPaint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!