float DrawSDFGlyphWithShadow(int code, Vector2 position, Vector2 scale, Color color, Vector2 shadowPosition, Color shadowColor) { SDFGlyphInfo glyph = sdfFontInfo.Value.glyphs[code]; if (shadowPosition != Vector2.zero && shadowColor != Color.clear) { DrawSDFGlyph(glyph, position + shadowPosition * sdfShadowPositionScale, scale, shadowColor); } return(DrawSDFGlyph(glyph, position, scale, color)); }
public float GetGlyphWidth(int code, Vector2 scale, float spacing = 0) { if (!IsSDFCapable) { GlyphInfo glyph = GetGlyph(code); return(glyph.width + spacing); } else { SDFGlyphInfo glyph = SDFInfo.glyphs[code]; float scalingRatio = GetSDFGlyphScalingRatio(scale.y); return(glyph.advance * scalingRatio / scale.x + spacing); } }
/// <summary> /// Load a TextMeshPro 2.0.x asset used for this to use for SDF rendering. /// Mods can use DaggerfallUI.Instance.Font0 through Font4 to access the instances used by other game windows. /// Custom font should be set only during startup. /// </summary> /// <param name="path">Path to a TextMeshPro 1.3.x font asset.</param> public void LoadSDFFontAsset(string path) { // Attempt to load a TextMeshPro font asset TMP_FontAsset tmpFont = Resources.Load <TMP_FontAsset>(path); if (!tmpFont) { return; } // Create font info SDFFontInfo fi = new SDFFontInfo(); fi.pointSize = tmpFont.faceInfo.pointSize; fi.atlasTexture = tmpFont.atlasTexture; fi.baseline = tmpFont.faceInfo.baseline; fi.glyphs = new Dictionary <int, SDFGlyphInfo>(); // Cache glyph info float atlasWidth = tmpFont.atlasTexture.width; float atlasHeight = tmpFont.atlasTexture.height; foreach (var kvp in tmpFont.characterLookupTable) { // Compose glyph rect inside of atlas TMP_Character character = kvp.Value; float atlasGlyphX = character.glyph.glyphRect.x / atlasWidth; float atlasGlyphY = character.glyph.glyphRect.y / atlasHeight; float atlasGlyphWidth = character.glyph.glyphRect.width / atlasWidth; float atlasGlyphHeight = character.glyph.glyphRect.height / atlasHeight; Rect atlasGlyphRect = new Rect(atlasGlyphX, atlasGlyphY, atlasGlyphWidth, atlasGlyphHeight); // Store information about this glyph SDFGlyphInfo glyphInfo = new SDFGlyphInfo() { code = (int)kvp.Key, rect = atlasGlyphRect, offset = new Vector2(character.glyph.metrics.horizontalBearingX, character.glyph.metrics.horizontalBearingY), size = new Vector2(character.glyph.metrics.width, character.glyph.metrics.height), advance = character.glyph.metrics.horizontalAdvance, }; fi.glyphs.Add((int)kvp.Key, glyphInfo); } // Set live font info sdfFontInfo = fi; }
/// <summary> /// Load a TextMeshPro 1.3.x asset used for this to use for SDF rendering. /// Mods can use DaggerfallUI.Instance.Font1 through Font4 to access the instances used by other game windows. /// Custom font should be set only during startup. /// </summary> /// <param name="path">Path to a TextMeshPro 1.3.x font asset.</param> public void LoadSDFFontAsset(string path) { // Attempt to load a TextMeshPro font asset TMP_FontAsset tmpFont = Resources.Load <TMP_FontAsset>(path); if (!tmpFont) { return; } // Create font info SDFFontInfo fi = new SDFFontInfo(); fi.pointSize = tmpFont.fontInfo.PointSize; fi.atlas = tmpFont.atlas; fi.baseline = tmpFont.fontInfo.Baseline; fi.glyphs = new Dictionary <int, SDFGlyphInfo>(); // Cache glyph info float atlasWidth = tmpFont.atlas.width; float atlasHeight = tmpFont.atlas.height; foreach (var kvp in tmpFont.characterDictionary) { // Compose glyph rect inside of atlas TMP_Glyph glyph = kvp.Value; float atlasGlyphX = glyph.x / atlasWidth; float atlasGlyphY = (atlasHeight - glyph.y - glyph.height) / atlasHeight; float atlasGlyphWidth = glyph.width / atlasWidth; float atlasGlyphHeight = glyph.height / atlasHeight; Rect atlasGlyphRect = new Rect(atlasGlyphX, atlasGlyphY, atlasGlyphWidth, atlasGlyphHeight); // Store information about this glyph SDFGlyphInfo glyphInfo = new SDFGlyphInfo() { code = kvp.Key, rect = atlasGlyphRect, offset = new Vector2(glyph.xOffset, glyph.yOffset), size = new Vector2(glyph.width, glyph.height), advance = glyph.xAdvance, }; fi.glyphs.Add(kvp.Key, glyphInfo); } // Set live font info sdfFontInfo = fi; }
public float DrawSDFGlyph(SDFGlyphInfo glyph, Vector2 position, Vector2 scale, Color color) { float scalingRatio = GetSDFGlyphScalingRatio(scale.y); // Handle space if (glyph.code == SpaceCode) { return(glyph.advance * scalingRatio); } // Compose target rect - this will change based on current display scale // Can use classic glyph height to approximate baseline vertical position float baseline = position.y - 2 * scale.y + GlyphHeight * scale.y + sdfFontInfo.Value.baseline; float xpos = position.x + glyph.offset.x * scalingRatio; float ypos = baseline - glyph.offset.y * scalingRatio; Rect targetRect = new Rect(xpos, ypos, glyph.size.x * scalingRatio, glyph.size.y * scalingRatio); // Draw glyph Graphics.DrawTexture(targetRect, sdfFontInfo.Value.atlasTexture, glyph.rect, 0, 0, 0, 0, color, DaggerfallUI.Instance.SDFFontMaterial); return(GetGlyphWidth(glyph, scale, GlyphSpacing)); }
void DrawSDFText( string text, Vector2 position, Vector2 scale, Color color) { float scalingRatio = GlyphHeight / sdfFontInfo.Value.pointSize * scale.y; byte[] utf32Bytes = Encoding.UTF32.GetBytes(text); for (int i = 0; i < utf32Bytes.Length; i += sizeof(int)) { // Get code and use ? for any character code not in dictionary int code = BitConverter.ToInt32(utf32Bytes, i); if (!sdfFontInfo.Value.glyphs.ContainsKey(code)) { code = ErrorCode; } // Get glyph data for this code SDFGlyphInfo glyph = sdfFontInfo.Value.glyphs[code]; // Handle space glyph by just advancing position if (code == SpaceCode) { position.x += glyph.advance * scalingRatio; continue; } // Compose target rect - this will change based on current display scale // Can use classic glyph height to approximate baseline vertical position float baseline = position.y - 2 * scale.y + GlyphHeight * scale.y + sdfFontInfo.Value.baseline; float xpos = position.x + glyph.offset.x * scalingRatio; float ypos = baseline - glyph.offset.y * scalingRatio; Rect targetRect = new Rect(xpos, ypos, glyph.size.x * scalingRatio, glyph.size.y * scalingRatio); // Draw glyph and advance position Graphics.DrawTexture(targetRect, sdfFontInfo.Value.atlas, glyph.rect, 0, 0, 0, 0, color, DaggerfallUI.Instance.SDFFontMaterial); position.x += glyph.advance * scalingRatio; } }
void DrawSDFText( string text, Vector2 position, Vector2 scale, Color color) { float glyphSpacing = GlyphSpacing; byte[] utf32Bytes = Encoding.UTF32.GetBytes(text); for (int i = 0; i < utf32Bytes.Length; i += sizeof(int)) { // Get code and use ? for any character code not in dictionary int code = BitConverter.ToInt32(utf32Bytes, i); if (!sdfFontInfo.Value.glyphs.ContainsKey(code)) { code = ErrorCode; } // Draw glyph and advance position SDFGlyphInfo glyph = sdfFontInfo.Value.glyphs[code]; DrawSDFGlyph(glyph, position, scale, color); position.x += GetGlyphWidth(glyph, scale, glyphSpacing) * scale.x; } }
public float GetGlyphWidth(SDFGlyphInfo glyph, Vector2 scale, float spacing = 0) { float scalingRatio = GetSDFGlyphScalingRatio(scale.y); return(glyph.advance * scalingRatio / scale.x + spacing); }
/// <summary> /// Calculate width of text using whichever font path is active (classic or SDF). /// </summary> /// <param name="text">Text to calculate width of.</param> /// <param name="scale">Scale to use when calculating width.</param> /// <returns>Width of string in scaled pixels.</returns> public float CalculateTextWidth(string text, Vector2 scale, int start = 0, int length = -1) { // Must have a string if (string.IsNullOrEmpty(text)) { return(0); } // Get automatic length from start position to end of text if (length < 0) { length = text.Length - start; } // Get substring if required if (start > 0 || length != text.Length) { text = text.Substring(start, length); } // Calculate width based on active font path float width = 0; if (!IsSDFCapable) { // Classic glyphs byte[] asciiBytes = Encoding.ASCII.GetBytes(text); for (int i = 0; i < asciiBytes.Length; i++) { // Get code and use ? for any character code not in dictionary int code = asciiBytes[i]; if (!HasGlyph(code)) { code = ErrorCode; } // Get glyph data for this code and increment width GlyphInfo glyph = GetGlyph(code); width += glyph.width + GlyphSpacing; } } else { // SDF glyphs float scalingRatio = GlyphHeight / sdfFontInfo.Value.pointSize * scale.y; byte[] utf32Bytes = Encoding.UTF32.GetBytes(text); for (int i = 0; i < utf32Bytes.Length; i += sizeof(int)) { // Get code and use ? for any character code not in dictionary int code = BitConverter.ToInt32(utf32Bytes, i); if (!sdfFontInfo.Value.glyphs.ContainsKey(code)) { code = ErrorCode; } // Get glyph data for this code and increment width SDFGlyphInfo glyph = sdfFontInfo.Value.glyphs[code]; width += glyph.advance * scalingRatio; } } return(width); }