public void PopulateSDFFontAsset() { // Char still have to follow what parsed Catalog says. var availableChars = TMP_FontAsset.GetCharactersArray(SDF_Asset); var newChars = new TexChar[parsedCatalogs.Length == 0 ? availableChars.Length : parsedCatalogs.Length]; var info = SDF_Asset.fontInfo; var padding = info.Padding; TexChar ch; chars = chars ?? new TexChar[0]; for (int i = 0; i < newChars.Length; i++) { if (parsedCatalogs.Length == 0) { ch = newChars[i] = chars.FirstOrDefault(j => j.characterIndex == availableChars[i]) ?? new TexChar(this, i, parsedCatalogs[i], true, 1); } else { ch = newChars[i] = chars.FirstOrDefault(j => j.characterIndex == parsedCatalogs[i]) ?? new TexChar(this, i, parsedCatalogs[i], true, 1); } if (!(ch.supported = parsedCatalogs.Length == 0 || ArrayUtility.Contains(availableChars, parsedCatalogs[i]))) { continue; } TMP_Glyph c = SDF_Asset.characterDictionary[parsedCatalogs[i]]; var factor = c.scale / info.PointSize; ch.depth = (c.height - c.yOffset + padding) * factor; ch.height = (c.yOffset + padding) * factor; ch.bearing = (-c.xOffset + padding) * factor; ch.italic = (c.width + c.xOffset + padding) * factor; ch.width = c.xAdvance * factor; var uv = new Rect(); uv.x = (c.x - padding) / info.AtlasWidth; uv.y = 1 - (c.y + c.height + padding) / info.AtlasHeight; uv.width = (c.width + 2 * padding) / info.AtlasWidth; uv.height = (c.height + 2 * padding) / info.AtlasHeight; //var uv = new Rect(c.x / info.AtlasWidth, c.y / info.AtlasHeight, // c.width / info.AtlasWidth, c.height / info.AtlasHeight); ch.sprite_uv = uv; ch.fontIndex = index; ch.index = i; } chars = newChars; font_lineHeight = Mathf.Max(0.2f, info.LineHeight / info.PointSize); sprite_xLength = 8; sprite_yLength = chars.Length / 8 + 1; sprite_alphaOnly = true; Sprite_Asset = SDF_Asset.atlas; }
public TexCharMetric GetCharMetric(TexChar Char, TexStyle style) { if (Char != null) { return(Char.GetMetric(TexUtility.SizeFactor(style))); } else { return(null); } }
public static SymbolAtom Get(TexChar ch, char tag) { if (ch == null) { return(null); } var atom = ObjPool <SymbolAtom> .Get(); atom.Type = ch.type; atom.Name = ch.symbolName; atom.IsDelimiter = (ch.extensionExist || ch.nextLargerExist) && !(ch.extensionHorizontal) && (ch.type >= CharType.Relation && ch.type <= CharType.CloseDelimiter); atom.Character = ch; atom.TaggedChar = tag; return(atom); }
public bool PushToDictionaries() { int lastHash = 0; try { ClearDictionary(); for (int i = 0; i < fontData.Length; i++) { var count = fontData[i].chars.Length; for (int j = 0; j < count; j++) { TexChar c = GetChar(i, j); c.CheckValidity(); lastHash = c.ToHash(); if (!string.IsNullOrEmpty(c.symbolName)) { symbolData.Add(c.symbolName, lastHash); } if (!string.IsNullOrEmpty(c.symbolAlt)) { symbolData.Add(c.symbolAlt, lastHash); } if (c.characterMap > 0) { charMapData.Add(TexChar.possibleCharMaps[c.characterMap], lastHash); } } } } catch (System.Exception ex) { if (ex is System.ArgumentException) { int pair; try { pair = GetChar(GetChar(lastHash).symbolName).ToHash(); } catch { return(false); } Debug.LogErrorFormat("Duplicate Definitions Exist at {0:X3} and {1:X3}, Please fix it now.\nError: {2}", lastHash, pair, ex.Message); } else { Debug.LogErrorFormat("Unknown Error at {0:X3}: {1}\n{2}\n\nStackTrace:\n{3}", lastHash, ex.GetType().Name, ex.Message, ex.StackTrace); } return(false); } return(true); }
public static CharBox Get(TexChar ch) { var box = ObjPool <CharBox> .Get(); var font = ch.font; box.ch = ch; box.i = font.index; switch (box.type = font.type) { case TexAssetType.Font: { var scl = TexContext.Scale; var c = box.c = ((TexFont)font).GenerateFont(ch.characterIndex, (int)(TexContext.Resolution * scl) + 1, TexContext.Style.value); float r = scl / c.size; box.Set(-c.minY * r, c.maxY * r, -c.minX * r, c.maxX * r, c.advance * r); } return(box); case TexAssetType.Sprite: { var b = (box.o = (TexSprite)font).GenerateMetric(ch.characterIndex); box.uv = b.uv; var s = b.size; box.Set(s.y, s.w, s.x, s.z, s.x + s.z); } return(box); #if TEXDRAW_TMP case TexAssetType.FontSigned: { var b = ((TexFontSigned)font).GenerateMetric(ch.characterIndex); box.uv = b.uv; var s = b.size; box.Set(s.y, s.w, s.x, s.z, s.x + s.z); } return(box); #endif default: return(null); } }
public void PopulateSprite() { if (sprite_xLength < 1 || sprite_yLength < 1 || sprite_scale <= 0.0e-5f) { SuggestTileSize(); } int maxCount = Mathf.Min(sprite_xLength * sprite_yLength, parsedCatalogs.Length == 0 ? int.MaxValue : parsedCatalogs.Length); var newChars = new TexChar[maxCount]; Vector2 size = new Vector2(1 / (float)sprite_xLength, 1 / (float)sprite_yLength); // Just for a placeholder (at least no null!) chars = chars ?? new TexChar[0]; for (int i = 0; i < maxCount; i++) { int x = i % sprite_xLength, y = i / sprite_xLength; TexChar ch; if (parsedCatalogs.Length == 0) { ch = newChars[i] = i < chars.Length ? chars[i] : new TexChar(this, i, (char)i, true, sprite_scale); } else { ch = newChars[i] = chars.FirstOrDefault(j => j.characterIndex == parsedCatalogs[i]) ?? new TexChar(this, i, parsedCatalogs[i], true, sprite_scale); } ch.depth = -sprite_lineOffset; ch.height = sprite_scale + sprite_lineOffset; ch.bearing = 0; ch.italic = sprite_scale; ch.width = sprite_scale; ch.sprite_uv = new Rect(Vector2.Scale(new Vector2(x, sprite_yLength - y - 1), size), size); ch.supported = i < maxCount; ch.fontIndex = index; ch.index = i; } chars = newChars; font_lineHeight = Mathf.Max(0.2f, sprite_scale * (Sprite_Asset.height / (float)sprite_yLength) / (Sprite_Asset.width / (float)sprite_xLength)); }
public override void Draw(DrawingContext drawingContext, float scale, float x, float y) { base.Draw(drawingContext, scale, x, y); // Draw character at given position. Vector2 vPos = new Vector2((x - bearing) * scale, (y - depth) * scale); Vector2 vSize = new Vector2((bearing + italic) * scale, totalHeight * scale); TexChar ch = character.ch; if (ch.font.type == TexFontType.Font) { drawingContext.Draw(ch.fontIndex, vPos, vSize, c.uvBottomRight, c.uvTopRight, c.uvTopLeft, c.uvBottomLeft); } else { Rect u = ch.sprite_uv; if (!ch.font.sprite_alphaOnly) { //Using RGB? then the color should be black //see the shader why it's happen to be like that Color tmpC = TexUtility.RenderColor; TexUtility.RenderColor = Color.black; drawingContext.Draw(ch.fontIndex, vPos, vSize, new Vector2(u.xMax, u.yMin), u.max, new Vector2(u.xMin, u.yMax), u.min); TexUtility.RenderColor = tmpC; } else { drawingContext.Draw(ch.fontIndex, vPos, vSize, new Vector2(u.xMax, u.yMin), u.max, new Vector2(u.xMin, u.yMax), u.min); } } }
static public int CharToHash(TexChar ch) { return(ch.ToHash()); }
public bool PushToDictionaries() { int lastHash = 0; try { symbols.Clear(); charmaps.Clear(); fontnames.Clear(); for (int i = 0; i < fonts.Length; i++) { var font = fonts[i]; font.index = i; font.ImportDictionary(); fontnames.Add(font.name, font); var count = font.chars.Length; for (int j = 0; j < count; j++) { TexChar c = font.chars[j]; c.index = j; c.fontIndex = i; lastHash = c.ToHash(); if (!string.IsNullOrEmpty(c.symbolName)) { symbols.Add(c.symbolName, lastHash); } if (!string.IsNullOrEmpty(c.symbolAlt)) { symbols.Add(c.symbolAlt, lastHash); } if (c.characterMap > 0) { charmaps.Add(TexChar.possibleCharMaps[c.characterMap], lastHash); } } } // now check charmaps full for (int i = 1; i < TexChar.possibleCharMaps.Length; i++) { if (!charmaps.ContainsKey(TexChar.possibleCharMaps[i])) { Debug.LogWarning("TEXDraw: Charmap is missing: " + TexChar.possibleCharMaps[i].ToString()); } } } catch (System.Exception ex) { if (ex is System.ArgumentException) { int pair; try { pair = GetChar(GetChar(lastHash).symbolName).ToHash(); } catch { return(false); } Debug.LogErrorFormat("TEXDraw: Duplicate Definitions Exist at {0:X3} and {1:X3}, Please fix it now.\nError: {2}", lastHash, pair, ex.Message); } else { Debug.LogErrorFormat("TEXDraw: Unknown Error at {0:X3}: {1}\n{2}\n\nStackTrace:\n{3}", lastHash, ex.GetType().Name, ex.Message, ex.StackTrace); } return(false); } return(true); }
static public int CharToHash(TexChar ch) { return(ch.index | ch.font.index << 8); }
public void PopulateCharacter() { //A GLITCH: Unity's Font.HasCharacter doesn't work properly on dynamic mode, we need to change it to Unicode first TrueTypeFontImporter fontData = (TrueTypeFontImporter)AssetImporter.GetAtPath(assetPath); if (!fontData) { assetPath = AssetDatabase.GetAssetPath(Font_Asset); fontData = (TrueTypeFontImporter)AssetImporter.GetAtPath(assetPath); } fontData.customCharacters = parsedCatalogs == null || parsedCatalogs.Length == 0 ? null : new System.String(parsedCatalogs); fontData.fontTextureCase = string.IsNullOrEmpty(fontData.customCharacters) ? FontTextureCase.Unicode : FontTextureCase.CustomSet; fontData.SaveAndReimport(); chars = chars ?? new TexChar[0]; TexChar ch; // Import all or on what parsedCatalogs says var availableChars = Font_Asset.characterInfo; var newChars = new TexChar[parsedCatalogs.Length == 0 ? availableChars.Length : parsedCatalogs.Length]; // Try to keep the character data not destroyed at all. for (int i = 0; i < newChars.Length; i++) { if (parsedCatalogs.Length == 0) { ch = newChars[i] = chars.FirstOrDefault(x => x.characterIndex == availableChars[i].index) ?? new TexChar(this, i, (char)availableChars[i].index, true, 1); } else { ch = newChars[i] = chars.FirstOrDefault(x => x.characterIndex == parsedCatalogs[i]) ?? new TexChar(this, i, parsedCatalogs[i], Font_Asset.HasCharacter(parsedCatalogs[i]), 1); } if (!ch.supported) { continue; } CharacterInfo c; Font_Asset.GetCharacterInfo(ch.characterIndex, out c, 0, FontStyle.Normal); float factor = c.size == 0 ? Font_Asset.fontSize : c.size; ch.depth = -c.minY / factor; ch.height = c.maxY / factor; ch.bearing = -c.minX / factor; ch.italic = c.maxX / factor; ch.width = c.advance / factor; ch.fontIndex = index; ch.index = i; } chars = newChars; font_lineHeight = Mathf.Max(Font_Asset.lineHeight / Font_Asset.fontSize, 0.2f); // Try to keep the character data not destroyed at all. for (int i = 0; i < newChars.Length; i++) { chars = newChars; } fontData.fontTextureCase = FontTextureCase.Dynamic; fontData.SaveAndReimport(); }