/// <summary> /// Information of rendering a glyph. /// 绘制一个字符(串)所需要的信息。 /// </summary> /// <param name="characters">The glyph(a character or a string).此字符(串)。</param> /// <param name="leftTop">UV of left top.此字符的字形在纹理的横向偏移量(左上角uv)</param> /// <param name="rightBottom">UV of right bottom.此字符的字形在纹理的纵向偏移量(右下角uv)</param> /// <param name="textureIndex">Index of the the texture which this glyph belongs to in the 2D texture array.此字符所在的纹理,在二维纹理数组中的索引。</param> public GlyphInfo(string characters, vec2 leftTop, vec2 rightBottom, int textureIndex) { this.characters = characters; var leftBottom = new vec2(leftTop.x, rightBottom.y); var rightTop = new vec2(rightBottom.x, leftTop.y); this.quad = new QuadStruct(leftTop, leftBottom, rightBottom, rightTop); this.textureIndex = textureIndex; }
/// <summary> /// start from (0, 0) to the right. /// </summary> /// <param name="text"></param> /// <param name="server"></param> /// <param name="totalWidth"></param> /// <param name="totalHeight"></param> unsafe private void PositionPass(string text, GlyphServer server, out float totalWidth, out float totalHeight) { int textureWidth = server.TextureWidth; int textureHeight = server.TextureHeight; VertexBuffer buffer = this.positionBuffer; var positionArray = (QuadStruct *)buffer.MapBuffer(MapBufferAccess.ReadWrite); const float height = 2.0f; // let's say height is 2.0f. totalWidth = 0; totalHeight = height; int index = 0; foreach (var c in text) { GlyphInfo glyphInfo; float wByH = 0; if (server.GetGlyphInfo(c, out glyphInfo)) { float w = (glyphInfo.quad.rightBottom.x - glyphInfo.quad.leftBottom.x) * textureWidth; float h = (glyphInfo.quad.rightBottom.y - glyphInfo.quad.rightTop.y) * textureHeight; wByH = height * w / h; } else { // put an empty glyph(square) here. wByH = height * 1.0f / 1.0f; } var leftTop = new vec2(totalWidth, 1f); var leftBottom = new vec2(totalWidth, -1f); var rightBottom = new vec2(totalWidth + wByH, -1f); var rightTop = new vec2(totalWidth + wByH, 1f); positionArray[index++] = new QuadStruct(leftTop, leftBottom, rightBottom, rightTop); totalWidth += wByH; } // move to center. const float scale = 1f; for (int i = 0; i < text.Length; i++) { QuadStruct quad = positionArray[i]; var newPos = new QuadStruct( // y is already in [-1, 1], so just shrink x to [-1, 1] new vec2(quad.leftTop.x / totalWidth * 2.0f - 1f, quad.leftTop.y) * scale, new vec2(quad.leftBottom.x / totalWidth * 2.0f - 1f, quad.leftBottom.y) * scale, new vec2(quad.rightBottom.x / totalWidth * 2.0f - 1f, quad.rightBottom.y) * scale, new vec2(quad.rightTop.x / totalWidth * 2.0f - 1f, quad.rightTop.y) * scale ); positionArray[i] = newPos; } buffer.UnmapBuffer(); }