本文共 4393 字,大约阅读时间需要 14 分钟。
通过一步步的分析和实践,我解决了在使用Skia进行文字排版时遇到的多个问题。以下是实现正确排版的详细步骤:
首先,确保文本字符被正确编码。对于中文字符,使用SkPaint::kUTF16_TextEncoding
编码,这将每个中文字符转换为代码点以便于处理。对于英文字符,直接使用SkPaint::kUTF8_TextEncoding
或其他单字节编码格式。
使用paint.textToGlyphs
接口,将文本转换为图形形状。每个字符将转换为其对应的图形ID,这可以被drawText
接口直接使用,从而绘制正确的字符形状。这样,不管字符是单字节还是双字节,都可以被正确处理。
使用paint.measureText
方法获取每个字符的宽度和高度。对于中文字符,由于占用两个字节,可能会显示为一个较宽的字符形状,这需要在排版时被正确计算进去。
由于Skia默认的drawText
方法在居中排版时可能缺少精度,手写计算居中位置是更好的选择。对于每个字符,计算其中心点,根据当前文本位置进行绘制,这样即使每个字符的宽度不同,居中效果仍能得到较好的控制。
使用循环结构遍历文本字符串,每次调用breakText
获取字符长度,然后根据字符长度计算行间距。确保每个线条的间距一致,并且不会超过画布的高度限制。
对于包含大量符号或特殊字符的文本,使用textToGlyphs
确保每个字符单独处理。这样,即使是复杂的符号组合,也能被正确解析并正确显示。
处理较长或复杂文本时,可以考虑优化字符处理逻辑,减少使用textToGlyphs
的次数,或采用预转换的方式优化性能。对于性能敏感的应用可以考虑批处理字符绘制。
以下是基于上述步骤优化后的代码结构:
#include "stdafx.h"#include "SkBitmap.h"#include "SkDevice.h"#include "SkPaint.h"#include "SkRect.h"#include "SkImageEncoder.h"#include "SkTypeface.h"#include "SkCanvas.h"#include "iostream"using namespace std;void setTypeText(wchar_t text[], SkPaint& paint, float LineSpacing, char Alignment) { const int width = 512; const int height = 512; bool flag = true; SkBitmap bitmap; SkImageInfo ii = SkImageInfo::Make(width, height, kBGRA_8888_SkColorType, kPremul_SkAlphaType); bitmap.allocPixels(ii, ii.minRowBytes()); bitmap.allocPixels(); SkCanvas canvas(bitmap); canvas.clear(0x00000000); int len = wcslen(text); SkRect bounds; paint.measureText(text, len, &bounds); float y = bounds.fBottom - bounds.fTop; while (flag) { int partialBytes = paint.breakText(text, len, width); if (partialBytes < len) { paint.measureText(text, partialBytes, &bounds); float testwidth = bounds.fRight - bounds.fLeft; switch (Alignment) { case 'L': case 'l': canvas.drawImageSubset(text, 0, partialBytes, 0, y, paint); break; case 'R': case 'r': canvas.drawImageSubset(text, 1, width - testwidth, 0, y, paint); break; case 'C': case 'c': canvas.drawText(text, partialBytes, (width - testwidth) / 2.0f, y, paint); break; default: canvas.drawImageSubset(text, 0, partialBytes, 0, y, paint); break; } ConversionText(text, partialBytes, len); y += (bounds.fBottom - bounds.fTop) + LineSpacing; if (y > height) { flag = false; } } else { paint.measureText(text, len, &bounds); float testwidth = bounds.fRight - bounds.fLeft; switch (Alignment) { case 'L': case 'l': canvas.drawImageSubset(text, 0, len, 0, y, paint); break; case 'R': case 'r': canvas.drawImageSubset(text, 1, width - testwidth, 0, y, paint); break; case 'C': case 'c': canvas.drawText(text, len, (width - testwidth) / 2.0f, y, paint); break; default: canvas.drawImageSubset(text, 0, len, 0, y, paint); break; } flag = false; } } SkFILEWStream stream("D:\\text.jpg"); SkEncodeImage(&stream, bitmap, SkEncodedImageFormat::kPNG, 100);}void ConversionText(wchar_t text[], int partialBytes, int len) { int j = 0; for (int i = partialBytes; i < len; i++) { text[j] = text[i]; j++; } text[j] = '\0';}int main(int argc, _TCHAR argv[]) { SkPaint paint; paint.setTextSize(70.0); paint.setAntiAlias(true); paint.setColor(0xff4281A4); paint.setStyle(SkPaint::kFill_Style); paint.setTextSkewX(-0.3f); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); wchar_t text[] = L"这是一个处理复杂文本排版的例子!包含中文和英文混合使用的情况,确保每个字符都能正确显示和排版。"; setTypeText(text, paint, 50.0, 'c'); return 0;}
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding)
确保所有字符被正确转换为图形ID。canvas.drawImageSubset
逐个绘制字符图形,确保每个字符单独处理,不受字节大小限制。通过以上步骤,我成功实现了复杂文本的排版解决方案,能够正确处理不同的语言字符和多种排版对齐方式。
转载地址:http://ycdzk.baihongyu.com/