public Data(byte[] bytes) { Stream s = new MemoryStream(bytes); BinaryReader br = new BinaryReader(s); header = new Header(br); indecies = new short[256]; for (int i = 0; i < 256; ++i) indecies[i] = br.ReadInt16(); metrics = new Metric[header.metricCount]; for (int i = 0; i < header.metricCount; ++i) metrics[i] = new Metric(br); List<byte> bs = new List<byte>(); while (true) { byte b = br.ReadByte(); if (b == 0) break; bs.Add(b); } textureName = Encoding.UTF8.GetString(bs.ToArray()); }
public virtual bool SetText(string text, Color[] colors) { bool result = true; mText = text; if (text == null || text.Length == 0) { mEmpty = true; mMesh.Clear(); return(result); } mEmpty = false; int chars = text.Length; Vector3[] vertices = new Vector3[chars * 4]; Vector2[] uv = new Vector2[chars * 4]; int[] triangles = new int[chars * 6]; Color32[] vertexColors = new Color32[chars * 4]; float scale = mSize / (float)mData.header.fontSize; float x = mLeftMargin; float y = -(float)mData.header.fontAscent * scale; float sheetWidth = (float)mData.header.sheetWidth; float sheetHeight = (float)mData.header.sheetHeight; int lastAscii = -1; int lastIndex = -1; int vertexBegin = 0; float left = mWidth; float right = 0; float top = mHeight; float bottom = 0; List <LineContext> lines = new List <LineContext>(); for (int i = 0; i < text.Length; ++i) { char c = text[i]; if (c == '\n') { // LINEFEED x = mLeftMargin; y -= mLineSpacing; lastAscii = -1; FeedLine(lines, ref vertexBegin, i, ref left, ref right); continue; } else if (c == ' ') { // SPACE x += mAsciiSpaceAdvance; lastAscii = -1; continue; } else if (c == '\t') { // TAB x += mTabSpacing; lastAscii = -1; continue; } else if (c == '\u3000') { // JIS X 0208 SPACE x += mNonAsciiSpaceAdvance; lastAscii = -1; continue; } if (IsAscii(c)) { // ASCII if (lastAscii == -1) { // Save index for Auto linefeed lastAscii = i; } } else { // non-ASCII lastAscii = -1; } Metric metric = SearchMetric(c); if (metric == null) { // not found result = false; continue; } float advance = metric.advance * mSize + mLetterSpacing; float px = x + advance; if (mWidth != 0 && px > mWidth - mRightMargin) { // Auto linefeed. int index = lastAscii; lastAscii = -1; x = mLeftMargin; y -= mLineSpacing; if (index != -1 && IsAscii(c)) { // ASCII int nextIndex = index - 1; if (lastIndex != nextIndex) { i = nextIndex; lastIndex = i; right = vertices[(i - 1) * 4].x; FeedLine(lines, ref vertexBegin, i, ref left, ref right); continue; } } else { FeedLine(lines, ref vertexBegin, i, ref left, ref right); } } float x0 = x + (float)metric.bearingX * scale; float x1 = x0 + (float)metric.width * scale; float y0 = y + (float)metric.bearingY * scale; float y1 = y0 - (float)metric.height * scale; if (left > x0) { left = x0; } if (right < x1) { right = x1; } if (top > y0) { top = y0; } if (bottom < y1) { bottom = y1; } x += advance; float w = 2.0f * sheetWidth; float u0 = (float)(2 * metric.u + 1) / w; float u1 = u0 + (float)(metric.width * 2 - 2) / w; float h = 2.0f * sheetHeight; float v0 = (float)(2 * (sheetHeight - metric.v) + 1) / h; float v1 = (v0 - (float)(metric.height * 2 + 2) / h); int vertexOffset = i * 4; vertices[vertexOffset + 0] = new Vector3(x1, y0, 0); vertices[vertexOffset + 1] = new Vector3(x1, y1, 0); vertices[vertexOffset + 2] = new Vector3(x0, y0, 0); vertices[vertexOffset + 3] = new Vector3(x0, y1, 0); uv[vertexOffset + 0] = new Vector2(u1, v0); uv[vertexOffset + 1] = new Vector2(u1, v1); uv[vertexOffset + 2] = new Vector2(u0, v0); uv[vertexOffset + 3] = new Vector2(u0, v1); int triangleOffset = i * 6; triangles[triangleOffset + 0] = 0 + vertexOffset; triangles[triangleOffset + 1] = 1 + vertexOffset; triangles[triangleOffset + 2] = 2 + vertexOffset; triangles[triangleOffset + 3] = 2 + vertexOffset; triangles[triangleOffset + 4] = 1 + vertexOffset; triangles[triangleOffset + 5] = 3 + vertexOffset; for (int n = 0; n < 4; ++n) { vertexColors[vertexOffset + n] = colors[i]; } } FeedLine(lines, ref vertexBegin, text.Length, ref left, ref right); if (mWidth != 0 && mAlign != Align.LEFT) { foreach (LineContext line in lines) { float tw = line.right - line.left; float offset; if (mAlign == Align.CENTER) { offset = (mWidth - mRightMargin - tw) / 2.0f; } else { // Align.RIGHT offset = mWidth - mRightMargin - tw; } for (int i = line.vertexBegin; i < line.vertexEnd; ++i) { for (int n = 0; n < 4; ++n) { vertices[i * 4 + n].x += offset; } } } } if (mHeight != 0 && mVerticalAlign != VerticalAlign.TOP) { float th = bottom - top; float offset; if (mVerticalAlign == VerticalAlign.MIDDLE) { offset = (mHeight - th) / 2.0f; } else { // VerticalAlign.BOTTOM offset = mHeight - th; } for (int i = 0; i < vertices.Length; ++i) { vertices[i].y -= offset; } } mMesh.Clear(); mMesh.vertices = vertices; mMesh.uv = uv; mMesh.triangles = triangles; mMesh.colors32 = vertexColors; mMesh.RecalculateBounds(); //mMesh.Optimize(); return(result); }