//calculate the size required for max chars using widest glyphs private Vector2 CalculateMaximumSize() { int width = 0; for (int i = 0; i < 128; i++) { if (!font.HasGlyph(i)) { continue; } PixelFont.GlyphInfo glyph = font.GetGlyph(i); if (glyph.width > width) { width = glyph.width; } } int totalWidth = (width + font.GlyphSpacing) * MaxCharacters; return(new Vector2(totalWidth, font.GlyphHeight)); }
void CreateNewLabelLayoutSingleLine() { // // Stage 1 - Encode glyphs and calculate final dimensions // // Use default UI font if none set if (font == null) { font = DaggerfallUI.DefaultFont; } // Start a new layout labelLayout = new LabelLayoutData(); List <GlyphLayoutData> glyphLayout = new List <GlyphLayoutData>(); // 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.Convert(Encoding.UTF8, Encoding.GetEncoding("ISO-8859-1"), Encoding.Default.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] = DaggerfallFont.SpaceCode; } // Calculate total width DaggerfallFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]); width += glyph.width + font.GlyphSpacing; } // Trim width if (maxWidth > 0 && width > maxWidth) { width = maxWidth; } // Create virtual layout area totalWidth = width; totalHeight = (int)(font.GlyphHeight); numTextLines = 1; labelLayout.width = totalWidth; labelLayout.height = totalHeight; // // Stage 2 - Add glyphs to layout // // Determine horizontal alignment offset 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 layout int xpos = (int)alignmentOffset; for (int i = startCharacterIndex; i < asciiBytes.Length; i++) { DaggerfallFont.GlyphInfo glyph = font.GetGlyph(asciiBytes[i]); if (xpos + glyph.width >= totalWidth) { break; } GlyphLayoutData glyphPos = new GlyphLayoutData() { x = xpos, y = 0, glyphRawAscii = asciiBytes[i], glyphWidth = glyph.width, }; glyphLayout.Add(glyphPos); xpos += glyph.width + font.GlyphSpacing; } labelLayout.glyphLayout = glyphLayout.ToArray(); this.Size = new Vector2(totalWidth * textScale, totalHeight * textScale); }