经历过一番分析,我们对绘制的相关工作大体了解了。现在要问:具体绘制函数是哪个?代码内容是怎样的?
预备知识
绘制文字,说到底就是把字体文件中对应的图像数据画上去(代码上就是复制到某个缓冲区)。
图像,包含几个关键信息:图像宽和高,格式(每个像素是bit,还是byte)。结合宽和格式,就产生了一个每行几个字节的信息。
ByteBinary1Bit.c
DEFINE_BYTE_BINARY_SOLID_DRAWGLYPHLIST(ByteBinary1Bit)
DEFINE_BYTE_BINARY_SOLID_DRAWGLYPHLIST(AnyByteBinary.h)
在LoopMacros.h中有一个类似的DEFINE_SOLID_DRAWGLYPHLIST宏定义。二者几乎一样。
#define DEFINE_BYTE_BINARY_SOLID_DRAWGLYPHLIST(DST) \ void NAME_SOLID_DRAWGLYPHLIST(DST)(SurfaceDataRasInfo *pRasInfo, \ ImageRef *glyphs, \ jint totalGlyphs, jint fgpixel, \ jint argbcolor, \ jint clipLeft, jint clipTop, \ jint clipRight, jint clipBottom, \ NativePrimitive *pPrim, \ CompositeInfo *pCompInfo) \ { \ jint glyphCounter; \ jint scan = pRasInfo->scanStride; \ DST ## DataType *pPix; \ \ //totalGlyphs: 本次绘制几个文字 for (glyphCounter = 0; glyphCounter < totalGlyphs; glyphCounter++) { \ DeclareDrawGlyphListClipVars(pixels, rowBytes, width, height, \ left, top, right, bottom) \ ClipDrawGlyphList(DST, pixels, 1, rowBytes, width, height, \ left, top, right, bottom, \ clipLeft, clipTop, clipRight, clipBottom, \ glyphs, glyphCounter, continue) \ pPix = PtrCoord(pRasInfo->rasBase,left,DST ## PixelStride,top,scan); \ \ do { \ Declare ## DST ## InitialLoadVars(pRasInfo, pPix, DstPix, left) \ jint x = 0; \ do { \ InitialLoad ## DST(pPix, DstPix); \ if (pixels[x]) { \ Store ## DST ## PixelData(pPix, 0, fgpixel, DstPix); \ } \ ShiftBits ## DST(DstPix); \ } while (++x < width); \ FinalStore ## DST(pPix, DstPix); \ pPix = PtrAddBytes(pPix, scan); \ // rowBytes就是每行几个字节 pixels += rowBytes; \ } while (--height > 0); \ } \ }
终于,我们找到了真正的绘制函数。
DeclareDrawGlyphListClipVars(LoopMacros.h)
声明一些局部变量。
#define DeclareDrawGlyphListClipVars(PIXELS, ROWBYTES, WIDTH, HEIGHT, \ LEFT, TOP, RIGHT, BOTTOM) \ const jubyte * PIXELS; \ int ROWBYTES; \ int LEFT, TOP; \ int WIDTH, HEIGHT; \ int RIGHT, BOTTOM; ClipDrawGlyphList(LoopMacros.h)
初始化声明的局部变量。这里可以看到width/height是如何初始化的。
#define ClipDrawGlyphList(DST, PIXELS, BYTESPERPIXEL, ROWBYTES, WIDTH, HEIGHT,\ LEFT, TOP, RIGHT, BOTTOM, \ CLIPLEFT, CLIPTOP, CLIPRIGHT, CLIPBOTTOM, \ GLYPHS, GLYPHCOUNTER, NULLGLYPHCODE) \ PIXELS = (const jubyte *)GLYPHS[GLYPHCOUNTER].pixels; \ if (!PIXELS) { \ NULLGLYPHCODE; \ } \ ROWBYTES = GLYPHS[GLYPHCOUNTER].rowBytes; \ LEFT = GLYPHS[GLYPHCOUNTER].x; \ TOP = GLYPHS[GLYPHCOUNTER].y; \ WIDTH = GLYPHS[GLYPHCOUNTER].width; \ HEIGHT = GLYPHS[GLYPHCOUNTER].height; \ \ /* if any clipping required, modify parameters now */ \ RIGHT = LEFT + WIDTH; \ BOTTOM = TOP + HEIGHT; \ if (LEFT < CLIPLEFT) { \ /* Multiply needed for LCD text as PIXELS is really BYTES */ \ PIXELS += (CLIPLEFT - LEFT) * BYTESPERPIXEL ; \ LEFT = CLIPLEFT; \ } \ if (TOP < CLIPTOP) { \ PIXELS += (CLIPTOP - TOP) * ROWBYTES; \ TOP = CLIPTOP; \ } \ if (RIGHT > CLIPRIGHT) { \ RIGHT = CLIPRIGHT; \ } \ if (BOTTOM > CLIPBOTTOM) { \ BOTTOM = CLIPBOTTOM; \ } \ if (RIGHT <= LEFT || BOTTOM <= TOP) { \ NULLGLYPHCODE; \ } \ WIDTH = RIGHT - LEFT; \ HEIGHT = BOTTOM - TOP;
看到这里,我们不禁要问:字体的位图信息,是怎么来的呢?