protected virtual void CreateLabelTexture() { if (font == null) { font = DaggerfallUI.DefaultFont; } // First pass encodes ASCII and calculates final dimensions int width = 0; asciiBytes = Encoding.ASCII.GetBytes(text); for (int i = 0; i < asciiBytes.Length; i++) { // Invalid ASCII bytes are cast to a space character if (!font.HasGlyph(asciiBytes[i])) { asciiBytes[i] = PixelFont.SpaceASCII; } // Calculate total width PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]); width += glyph.width + font.GlyphSpacing; } if (maxWidth > 0 && width > maxWidth) { width = maxWidth; } // Create target label texture totalWidth = width; totalHeight = font.GlyphHeight; labelTexture = CreateLabelTexture(totalWidth, totalHeight); if (labelTexture == null) { throw new Exception("TextLabel failed to create labelTexture."); } // Second pass adds glyphs to label texture int xpos = 0; for (int i = 0; i < asciiBytes.Length; i++) { PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]); if (xpos + glyph.width >= totalWidth) { break; } labelTexture.SetPixels32(xpos, 0, glyph.width, totalHeight, glyph.colors); xpos += glyph.width + font.GlyphSpacing; } labelTexture.Apply(false, true); labelTexture.filterMode = font.FilterMode; this.Size = new Vector2(totalWidth, totalHeight); }
void CreateLabelTextureSingleLine() { if (font == null) { font = DaggerfallUI.DefaultFont; } // set a local maxWidth that compensates for textScale int maxWidth = (int)(this.maxWidth / textScale); // First pass encodes ASCII and calculates final dimensions int width = 0; asciiBytes = Encoding.ASCII.GetBytes(text); for (int i = startCharacterIndex; i < asciiBytes.Length; i++) { // Invalid ASCII bytes are cast to a space character if (!font.HasGlyph(asciiBytes[i])) { asciiBytes[i] = PixelFont.SpaceASCII; } // Calculate total width PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]); width += glyph.width + font.GlyphSpacing; } if (maxWidth > 0 && width > maxWidth) { width = maxWidth; } // Destroy old texture if (labelTexture) { UnityEngine.Object.Destroy(labelTexture); } // Create target label texture totalWidth = width; totalHeight = (int)(font.GlyphHeight); numTextLines = 1; labelTexture = CreateLabelTexture(totalWidth, totalHeight); if (labelTexture == null) { throw new Exception("TextLabel failed to create labelTexture."); } float alignmentOffset; switch (horizontalTextAlignment) { default: case HorizontalTextAlignmentSetting.None: case HorizontalTextAlignmentSetting.Left: case HorizontalTextAlignmentSetting.Justify: alignmentOffset = 0.0f; break; case HorizontalTextAlignmentSetting.Center: alignmentOffset = (totalWidth - width) * 0.5f; break; case HorizontalTextAlignmentSetting.Right: alignmentOffset = totalWidth - width; break; } // Second pass adds glyphs to label texture int xpos = (int)alignmentOffset; for (int i = startCharacterIndex; i < asciiBytes.Length; i++) { PixelFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]); if (xpos + glyph.width >= totalWidth) { break; } labelTexture.SetPixels32(xpos, 0, glyph.width, totalHeight, glyph.colors); xpos += glyph.width + font.GlyphSpacing; } labelTexture.Apply(false, makeTextureNoLongerReadable); labelTexture.filterMode = font.FilterMode; this.Size = new Vector2(totalWidth * textScale, totalHeight * textScale); }