private bool KerningParser(string line) { if (!line.StartsWith("kerning")) { return(false); } string[] array = line.Split(new char[] { ' ', '=' }); int num = this.FindField("first", array, 1); int value = int.Parse(array[num + 1]); num = this.FindField("second", array, num); int value2 = int.Parse(array[num + 1]); num = this.FindField("amount", array, num); int num2 = int.Parse(array[num + 1]); SpriteChar spriteChar = this.GetSpriteChar(Convert.ToChar(value2)); if (spriteChar.kernings == null) { spriteChar.kernings = new Dictionary <char, float>(); } spriteChar.origKernings = new Dictionary <char, float>(); spriteChar.kernings.Add(Convert.ToChar(value), (float)num2); spriteChar.origKernings.Add(Convert.ToChar(value), (float)num2); return(true); }
void ReadKerningPairsBlock(BinaryReader br, int blockSize) { int count = blockSize / 10; kerningsCount = count; for (int i = 0; i < count; i++) { int first = (int)br.ReadUInt32(); int second = (int)br.ReadUInt32(); int amount = br.ReadInt16(); // Now add the kerning info to the appropriate character: SpriteChar ch = GetSpriteChar(Convert.ToChar(second)); if (ch.kernings == null) { #if UNITY_IPHONE && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9) ch.kernings = new Hashtable(); ch.origKernings = new Hashtable(); #else ch.kernings = new Dictionary <int, float>(); ch.origKernings = new Dictionary <int, float>(); #endif } ch.kernings.Add(first, (float)amount); ch.origKernings.Add(first, (float)amount); } }
void ReadCharsBlock(BinaryReader br, int blockSize) { int count = blockSize / 20; chars = new SpriteChar[count + 1]; for (int i = 0; i < count; i++) { chars[i] = new SpriteChar(); chars[i].id = (int)br.ReadUInt32(); float x = br.ReadUInt16() / (float)texWidth; float y = 1f - br.ReadUInt16() / (float)texHeight; float width = br.ReadUInt16() / (float)texWidth; float height = br.ReadUInt16() / (float)texHeight; chars[i].xOffset = br.ReadInt16(); chars[i].yOffset = -br.ReadInt16(); chars[i].xAdvance = br.ReadInt16(); // Build our character's UVs: chars[i].UVs.x = x; chars[i].UVs.y = y - height; chars[i].UVs.xMax = x + width; chars[i].UVs.yMax = y; br.ReadBytes(2); charMap.Add(Convert.ToInt32(chars[i].id), i); } }
// Parses the kernings bool KerningParser(string line) { if (!line.StartsWith("kerning")) { return(false); } int first, second, amount; string[] vals = line.Split(new char[] { ' ', '=' }); int index = FindField("first", vals, 1); first = int.Parse(vals[index + 1]); index = FindField("second", vals, index); second = int.Parse(vals[index + 1]); index = FindField("amount", vals, index); amount = int.Parse(vals[index + 1]); // Now add the kerning info to the appropriate character: SpriteChar ch = GetSpriteChar(Convert.ToChar(second)); if (ch.kernings == null) #if UNITY_IPHONE && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9) { ch.kernings = new Hashtable(); } #else { ch.kernings = new Dictionary <char, float>(); } #endif ch.kernings.Add(Convert.ToChar(first), (float)amount); return(true); }
public void InitializeFont() { if (fontConfig != null && fontMaterials.Length > 0) { _fontFile = FontLoader.LoadFromString(fontConfig.text); if (_fontFile.Pages.Count != fontMaterials.Length) { Debug.LogError("Materials count don't match!"); return; } _indexLists = new List <List <int> >(); for (int i = 0; i < _fontFile.Pages.Count; ++i) { _indexLists.Add(new List <int>()); } _textureSize = new Vector2(_fontFile.Common.ScaleW, _fontFile.Common.ScaleH); _charMap = new Dictionary <int, SpriteChar>(); for (int i = 0; i < _fontFile.Chars.Count; ++i) { SpriteChar sc = new SpriteChar(); Rect rect = new Rect(_fontFile.Chars[i].X, _fontFile.Chars[i].Y, _fontFile.Chars[i].Width, _fontFile.Chars[i].Height); sc.fontChar = _fontFile.Chars[i]; sc.rect = rect; _charMap.Add(_fontFile.Chars[i].ID, sc); } Debug.Log("Font Initialized"); } }
// 指定の文字でスプライトフォントを作成する public void SetText(string text) { int i = 0; foreach (char c in text) { GameObject obj = null; if (i < transform.childCount) { // 作成済みであればそれを使う obj = transform.GetChild(i).gameObject; } else { // SpriteCharをプレハブから取得 GameObject prefab = Resources.Load("Prefabs/SpriteChar") as GameObject; Vector3 pos = new Vector3(i * transform.localScale.x * FONT_SIZE + i * padding, 0, 0); obj = Instantiate(prefab) as GameObject; // 子に設定する obj.transform.parent = transform; obj.transform.localPosition = pos; obj.transform.localScale = new Vector3(1, 1, 1); } // 文字を対応するスプライト番号に変換する int idx = _decode.IndexOf(c); // SpriteCharを取得してスプライトを変更する SpriteChar sc = obj.GetComponent <SpriteChar>(); sc.SetChar(idx); i++; } }
//void Update() //{ // if (Input.GetKeyUp(KeyCode.F11)) // { // File.Delete(path); // LoadQuotesFromFile(); // } //} public void ShowQuote(SpriteChar talker) { //bubble.transform.SetParent(talker.bubbleHinge, false); //bubble.transform.localPosition = Vector3.zero; bubble.transform.position = talker.bubbleHinge.position; SetQuoteText(); talker.currentState = SpriteChar.State.Standing; talker.navMeshAgent.isStopped = true; }
/// <summary> /// Gets how wide the specified character /// would be, in pixels, when displayed. /// </summary> /// <param name="prevChar">The character previous to that being measured.</param> /// <param name="str">The character to measure.</param> /// <returns>The width, in pixels, of the character, as displayed (includes the xAdvance).</returns> public float GetWidth(char prevChar, char c) { SpriteChar chr = GetSpriteChar(c); if (chr == null) { return(0); } return(chr.xAdvance + chr.GetKerning(prevChar)); }
// Parses a character definition line bool CharParser(string line, int charNum) { if (!line.StartsWith("char")) { return(false); } float x, y, width, height; string[] vals = line.Split(new char[] { ' ', '=' }); int index = FindField("id", vals, 1); chars[charNum] = new SpriteChar(); chars[charNum].id = int.Parse(vals[index + 1]); index = FindField("x", vals, index); x = float.Parse(vals[index + 1]) / (float)texWidth; index = FindField("y", vals, index); y = 1f - float.Parse(vals[index + 1]) / (float)texHeight; index = FindField("width", vals, index); width = float.Parse(vals[index + 1]) / (float)texWidth; index = FindField("height", vals, index); height = float.Parse(vals[index + 1]) / (float)texHeight; index = FindField("xoffset", vals, index); chars[charNum].xOffset = float.Parse(vals[index + 1]); index = FindField("yoffset", vals, index); chars[charNum].yOffset = -float.Parse(vals[index + 1]); index = FindField("xadvance", vals, index); chars[charNum].xAdvance = int.Parse(vals[index + 1]); // tzz added for channel char field index = FindField("chnl", vals, index); if (index != -1) { chars[charNum].channel = int.Parse(vals[index + 1]); } // Build our character's UVs: chars[charNum].UVs.x = x + bleedCompUV.x; chars[charNum].UVs.y = y - height + bleedCompUV.y; chars[charNum].UVs.xMax = x + width + bleedCompUVMax.x; chars[charNum].UVs.yMax = y + bleedCompUVMax.y; charMap.Add(Convert.ToChar(chars[charNum].id), charNum); return(true); }
/// <summary> /// Returns the xAdvance of the specified character. /// 0 is returned if the character isn't supported. /// </summary> /// <param name="c">The character to look up.</param> /// <returns>The xAdvance of the character.</returns> public float GetAdvance(char c) { SpriteChar ch = GetSpriteChar(c); if (ch == null) { return(0); } else { return(ch.xAdvance); } }
public float GetWidth(string str) { if (str.Length < 1) { return(0f); } float num = this.GetSpriteChar(str[0]).xAdvance; for (int i = 1; i < str.Length; i++) { SpriteChar spriteChar = this.GetSpriteChar(str[i]); num += spriteChar.xAdvance + spriteChar.GetKerning(str[i - 1]); } return(num); }
public float GetWidth(StringBuilder sb, int start, int end) { if (start >= sb.Length || end < start) { return(0f); } end = Mathf.Clamp(end, 0, sb.Length - 1); float num = this.GetSpriteChar(sb[start]).xAdvance; for (int i = start + 1; i <= end; i++) { SpriteChar spriteChar = this.GetSpriteChar(sb[i]); num += spriteChar.xAdvance + spriteChar.GetKerning(sb[i - 1]); } return(num); }
private void ReadKerningPairsBlock(BinaryReader br, int blockSize) { int num = blockSize / 10; this.kerningsCount = num; for (int i = 0; i < num; i++) { int value = (int)br.ReadUInt32(); int value2 = (int)br.ReadUInt32(); int num2 = (int)br.ReadInt16(); SpriteChar spriteChar = this.GetSpriteChar(Convert.ToChar(value2)); if (spriteChar.kernings == null) { spriteChar.kernings = new Dictionary <char, float>(); } spriteChar.origKernings = new Dictionary <char, float>(); spriteChar.kernings.Add(Convert.ToChar(value), (float)num2); spriteChar.origKernings.Add(Convert.ToChar(value), (float)num2); } }
// Sets up the vertices, etc, // of a character. protected void BuildCharacter(int vertNum, int charNum, Vector3 upperLeft, ref SpriteChar ch) { // 0 ___ 3 // Verts: | /| // 1|/__|2 vertices[vertNum] = upperLeft; vertices[vertNum + 1].x = upperLeft.x; vertices[vertNum + 1].y = upperLeft.y - ch.UVs.height * worldUnitsPerUV.y; vertices[vertNum + 1].z = upperLeft.z; vertices[vertNum + 2] = vertices[vertNum + 1]; vertices[vertNum + 2].x += ch.UVs.width * worldUnitsPerUV.x; vertices[vertNum + 3] = vertices[vertNum + 2]; vertices[vertNum + 3].y = vertices[vertNum].y; //------- UVs -------- UVs[vertNum].x = ch.UVs.x; UVs[vertNum].y = ch.UVs.yMax; UVs[vertNum + 1].x = ch.UVs.x; UVs[vertNum + 1].y = ch.UVs.y; UVs[vertNum + 2].x = ch.UVs.xMax; UVs[vertNum + 2].y = ch.UVs.y; UVs[vertNum + 3].x = ch.UVs.xMax; UVs[vertNum + 3].y = ch.UVs.yMax; //------- Colors -------- meshColors[vertNum] = colors[charNum]; meshColors[vertNum + 1] = colors[charNum]; meshColors[vertNum + 2] = colors[charNum]; meshColors[vertNum + 3] = colors[charNum]; // Clip the character: if (clipped) { // Clip the character horizontally: if (vertices[vertNum].x < localClipRect.x) { if (vertices[vertNum + 2].x < localClipRect.x) { // Clip the whole character: vertices[vertNum].x = vertices[vertNum + 1].x = vertices[vertNum + 2].x; return; } else { // Trim the character: float clipPct = (localClipRect.x - vertices[vertNum].x) / (vertices[vertNum + 2].x - vertices[vertNum].x); vertices[vertNum].x = vertices[vertNum + 1].x = localClipRect.x; UVs[vertNum].x = UVs[vertNum + 1].x = Mathf.Lerp(UVs[vertNum].x, UVs[vertNum + 2].x, clipPct); } } else if (vertices[vertNum + 2].x > localClipRect.xMax) { if (vertices[vertNum].x > localClipRect.xMax) { // Clip the whole character: vertices[vertNum + 2].x = vertices[vertNum + 3].x = vertices[vertNum].x; return; } else { // Trim the character: float clipPct = (localClipRect.xMax - vertices[vertNum].x) / (vertices[vertNum + 2].x - vertices[vertNum].x); vertices[vertNum + 2].x = vertices[vertNum + 3].x = localClipRect.xMax; UVs[vertNum + 2].x = UVs[vertNum + 3].x = Mathf.Lerp(UVs[vertNum].x, UVs[vertNum + 2].x, clipPct); } } // Clip the character vertically: if (vertices[vertNum].y > localClipRect.yMax) { if (vertices[vertNum + 2].y > localClipRect.yMax) { // Clip the whole character: vertices[vertNum].y = vertices[vertNum + 3].y = vertices[vertNum + 2].y; return; } else { // Trim the character: float clipPct = (vertices[vertNum].y - localClipRect.yMax) / (vertices[vertNum].y - vertices[vertNum + 1].y); vertices[vertNum].y = vertices[vertNum + 3].y = localClipRect.yMax; UVs[vertNum].y = UVs[vertNum + 3].y = Mathf.Lerp(UVs[vertNum].y, UVs[vertNum + 1].y, clipPct); } } else if (vertices[vertNum + 2].y < localClipRect.y) { if (vertices[vertNum].y < localClipRect.y) { // Clip the whole character: vertices[vertNum + 1].y = vertices[vertNum + 2].y = vertices[vertNum].y; return; } else { // Trim the character: float clipPct = (vertices[vertNum].y - localClipRect.y) / (vertices[vertNum].y - vertices[vertNum + 1].y); vertices[vertNum + 1].y = vertices[vertNum + 2].y = localClipRect.y; UVs[vertNum + 1].y = UVs[vertNum + 2].y = Mathf.Lerp(UVs[vertNum].y, UVs[vertNum + 1].y, clipPct); } } } }
public float GetWidth(char prevChar, char c) { SpriteChar spriteChar = this.GetSpriteChar(c); return(spriteChar.xAdvance + spriteChar.GetKerning(prevChar)); }
public void InitializeFont() { if (fontConfig != null && fontMaterials.Length > 0) { _fontFile = FontLoader.LoadFromString(fontConfig.text); if (_fontFile.Pages.Count != fontMaterials.Length) { Debug.LogError("Materials count don't match!"); return; } _indexLists = new List<List<int>>(); for (int i = 0; i < _fontFile.Pages.Count; ++i) _indexLists.Add(new List<int>()); _textureSize = new Vector2(_fontFile.Common.ScaleW, _fontFile.Common.ScaleH); _charMap = new Dictionary<int, SpriteChar>(); for (int i = 0; i < _fontFile.Chars.Count; ++i) { SpriteChar sc = new SpriteChar(); Rect rect = new Rect(_fontFile.Chars[i].X, _fontFile.Chars[i].Y, _fontFile.Chars[i].Width, _fontFile.Chars[i].Height); sc.fontChar = _fontFile.Chars[i]; sc.rect = rect; _charMap.Add(_fontFile.Chars[i].ID, sc); } Debug.Log("Font Initialized"); } }