static public int constructor(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.CharacterInfo o; o = new UnityEngine.CharacterInfo(); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
static void Font_characterInfo(JSVCall vc) { if (vc.bGet) { UnityEngine.Font _this = (UnityEngine.Font)vc.csObj; var result = _this.characterInfo; var arrRet = result; for (int i = 0; arrRet != null && i < arrRet.Length; i++) { JSMgr.datax.setObject((int)JSApi.SetType.SaveAndTempTrace, arrRet[i]); JSApi.moveSaveID2Arr(i); } JSApi.setArrayS((int)JSApi.SetType.Rval, (arrRet != null ? arrRet.Length : 0), true); } else { UnityEngine.CharacterInfo[] arg0 = JSDataExchangeMgr.GetJSArg <UnityEngine.CharacterInfo[]>(() => { int jsObjID = JSApi.getObject((int)JSApi.GetType.Arg); int length = JSApi.getArrayLength(jsObjID); var ret = new UnityEngine.CharacterInfo[length]; for (var i = 0; i < length; i++) { JSApi.getElement(jsObjID, i); ret[i] = (UnityEngine.CharacterInfo)JSMgr.datax.getObject((int)JSApi.GetType.SaveAndRemove); } return(ret); }); UnityEngine.Font _this = (UnityEngine.Font)vc.csObj; _this.characterInfo = arg0; } }
/// <summary> /// Returns a Vector2 structure containing the width and height that would be /// required to render a single line of text at the given size and with the given style /// </summary> /// <param name="text">The single line of text to measure</param> /// <param name="size">The desired font size</param> /// <param name="style">The desired text style</param> /// <returns></returns> public Vector2 MeasureText(string text, int size, FontStyle style) { RequestCharacters(text, size, style); var multiplier = (float)size / (float)FontSize; var lineHeight = Mathf.CeilToInt(Baseline * multiplier); var measuredSize = new Vector2(0, lineHeight); UnityEngine.CharacterInfo glyph = new UnityEngine.CharacterInfo(); for (int i = 0; i < text.Length; i++) { BaseFont.GetCharacterInfo(text[i], out glyph, size, style); var width = Mathf.Ceil(glyph.vert.x + glyph.vert.width); if (text[i] == ' ') { width = Mathf.Ceil(glyph.width); } else if (text[i] == '\t') { width += size * 4; } measuredSize.x += width; } return(measuredSize); }
private void getGlyphData(UnityEngine.CharacterInfo[] result, string text, int size, FontStyle style) { // Ensure that there is enough space for the text if (text.Length > glyphBuffer.Length) { glyphBuffer = new UnityEngine.CharacterInfo[text.Length + 512]; } int i = 0; for ( ; i < text.Length; i++) { // Get the character info and copy it into the static buffer if (!baseFont.GetCharacterInfo(text[i], out result[i], size, style)) { // If the character info was not found, create a dummy // record in its place result[i] = new UnityEngine.CharacterInfo() { flipped = false, index = -1, size = size, style = style, width = size * 0.25f }; } } // Clear the glyph immediately after the requested data if (i < result.Length) { result[i] = new UnityEngine.CharacterInfo(); } }
static public int set_flipped(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); System.Boolean v; checkType(l, 2, out v); o.flipped = v; setBack(l, o); return(0); }
static public int set_vert(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); UnityEngine.Rect v; checkType(l, 2, out v); o.vert = v; setBack(l, o); return(0); }
static public int set_width(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); System.Single v; checkType(l, 2, out v); o.width = v; setBack(l, o); return(0); }
static public int set_size(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); System.Int32 v; checkType(l, 2, out v); o.size = v; setBack(l, o); return(0); }
static public int set_style(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); UnityEngine.FontStyle v; checkEnum(l, 2, out v); o.style = v; setBack(l, o); return(0); }
static public int constructor(IntPtr l) { try { UnityEngine.CharacterInfo o; o=new UnityEngine.CharacterInfo(); pushValue(l,true); pushValue(l,o); return 2; } catch(Exception e) { return error(l,e); } }
static public int constructor(IntPtr l) { try { UnityEngine.CharacterInfo o; o = new UnityEngine.CharacterInfo(); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } }
public static int constructor(IntPtr l) { try { UnityEngine.CharacterInfo o; o=new UnityEngine.CharacterInfo(); pushValue(l,o); return 1; } catch(Exception e) { LuaDLL.luaL_error(l, e.ToString()); return 0; } }
static public int constructor(IntPtr l) { try { UnityEngine.CharacterInfo o; o = new UnityEngine.CharacterInfo(); pushValue(l, o); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
/// <summary> /// コンストラクタ /// </summary> /// <param name="c">文字</param> /// <param name="info">文字情報</param> /// <param name="sprite">描画スプライト</param> /// <param name="offsetY">フォントに設定されているオフセット値Y</param> public FontRenderInfo(char c, CharacterInfo info, Sprite sprite, float offsetY, float fontSize) { this.c = c; this.charInfo = info; this.sprite = sprite; //中心を原点とした場合の、表示位置 WrapperUnityVersion.SetFontRenderInfo( c, ref info, offsetY, fontSize, out offset, out width, out kerningWidth ); if (kerningWidth == 0) { kerningWidth = width; } kerningOffsetX = kerningWidth / 2; }
static void CharacterInfo_width(JSVCall vc) { if (vc.bGet) { UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; var result = _this.width; JSApi.setSingle((int)JSApi.SetType.Rval, (System.Single)(result)); } else { System.Single arg0 = (System.Single)JSApi.getSingle((int)JSApi.GetType.Arg); UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; _this.width = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
static void CharacterInfo_vert(JSVCall vc) { if (vc.bGet) { UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; var result = _this.vert; JSMgr.datax.setObject((int)JSApi.SetType.Rval, result); } else { UnityEngine.Rect arg0 = (UnityEngine.Rect)JSMgr.datax.getObject((int)JSApi.GetType.Arg); UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; _this.vert = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
static void CharacterInfo_flipped(JSVCall vc) { if (vc.bGet) { UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; var result = _this.flipped; JSApi.setBooleanS((int)JSApi.SetType.Rval, (System.Boolean)(result)); } else { System.Boolean arg0 = (System.Boolean)JSApi.getBooleanS((int)JSApi.GetType.Arg); UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; _this.flipped = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
static void CharacterInfo_style(JSVCall vc) { if (vc.bGet) { UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; var result = _this.style; JSApi.setEnum((int)JSApi.SetType.Rval, (int)result); } else { UnityEngine.FontStyle arg0 = (UnityEngine.FontStyle)JSApi.getEnum((int)JSApi.GetType.Arg); UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; _this.style = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
static void CharacterInfo_uvTopLeft(JSVCall vc) { if (vc.bGet) { UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; var result = _this.uvTopLeft; JSApi.setVector2S((int)JSApi.SetType.Rval, result); } else { UnityEngine.Vector2 arg0 = (UnityEngine.Vector2)JSApi.getVector2S((int)JSApi.GetType.Arg); UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; _this.uvTopLeft = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
// properties static void CharacterInfo_advance(JSVCall vc) { if (vc.bGet) { UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; var result = _this.advance; JSApi.setInt32((int)JSApi.SetType.Rval, (System.Int32)(result)); } else { System.Int32 arg0 = (System.Int32)JSApi.getInt32((int)JSApi.GetType.Arg); UnityEngine.CharacterInfo _this = (UnityEngine.CharacterInfo)vc.csObj; _this.advance = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
/// <summary> /// Sets the Text property of this element and measures the size /// of the text as it would be rendered with the current style. /// Designed for internal use by elements that need to create /// text boxes dynamically, such as list items. /// </summary> /// <param name="text"></param> internal void SetText(string text) { this.Text = text; if (Style.Font == null) { return; } isWhitespace = whitespacePattern.IsMatch(this.Text); var effectiveText = Style.PreserveWhitespace || !isWhitespace ? this.Text : " "; var currentFontSize = Style.FontSize; var size = new Vector2(0, Style.LineHeight); Style.Font.RequestCharacters(effectiveText, Style.FontSize, Style.FontStyle); var glyph = new UnityEngine.CharacterInfo(); for (int i = 0; i < effectiveText.Length; i++) { if (!Style.Font.BaseFont.GetCharacterInfo(effectiveText[i], out glyph, currentFontSize, Style.FontStyle)) { continue; } var width = glyph.vert.x + glyph.vert.width; if (effectiveText[i] == ' ') { width = Mathf.Max(width, currentFontSize * 0.33f); } else if (effectiveText[i] == '\t') { width += currentFontSize * 3; } size.x += width; } this.Size = size; var font = Style.Font; var multiplier = (float)currentFontSize / (float)font.FontSize; this.Baseline = Mathf.CeilToInt(font.Baseline * multiplier); }
public void SpawnFriendly() { for (int i = 0; i < _SacrifceList.Count - 1; i++) { for (int j = 0; j < _SacrifceList.Count - 1; j++) { if (_SacrifceList[j] > _SacrifceList[j + 1]) { int temp = _SacrifceList[j]; _SacrifceList[j] = _SacrifceList[j + 1]; _SacrifceList[j + 1] = temp; } } } foreach (int id in _SacrifceList) { _ResourceManager.EventCheck(id, -1); } while (_SacrifceList.Count < 5) { _SacrifceList.Add(0); } spawnInfo = XMLManager.Instance.Load_CharacterData(_SacrifceList[0], _SacrifceList[1], _SacrifceList[2], _SacrifceList[3], _SacrifceList[4]); if (spawnInfo == null) { Reset(); return; } spawnFriendly = Instantiate(Resources.Load("Friendly/" + spawnInfo.ID), new Vector3(-3 - gameManager.friendlyList.Count, 0, -6), new Quaternion(0, 0, 0, 1)) as GameObject; gameManager.friendlyList.Add(spawnFriendly); spawnFriendly.GetComponent<Friendly>().hp = spawnInfo.HP; spawnFriendly.GetComponent<Friendly>().ap = spawnInfo.AP; text.GetComponent<SummonText>().Activate(spawnInfo.Name); Reset(); }
/// <summary> /// Write the specified value using the writer. /// </summary> /// <param name="value">Value.</param> /// <param name="writer">Writer.</param> public override void Write(object value, ISaveGameWriter writer) { UnityEngine.CharacterInfo characterInfo = (UnityEngine.CharacterInfo)value; writer.WriteProperty("index", characterInfo.index); writer.WriteProperty("size", characterInfo.size); writer.WriteProperty("style", characterInfo.style); writer.WriteProperty("advance", characterInfo.advance); writer.WriteProperty("glyphWidth", characterInfo.glyphWidth); writer.WriteProperty("glyphHeight", characterInfo.glyphHeight); writer.WriteProperty("bearing", characterInfo.bearing); writer.WriteProperty("minY", characterInfo.minY); writer.WriteProperty("maxY", characterInfo.maxY); writer.WriteProperty("minX", characterInfo.minX); writer.WriteProperty("maxX", characterInfo.maxX); writer.WriteProperty("uvBottomLeft", characterInfo.uvBottomLeft); writer.WriteProperty("uvBottomRight", characterInfo.uvBottomRight); writer.WriteProperty("uvTopRight", characterInfo.uvTopRight); writer.WriteProperty("uvTopLeft", characterInfo.uvTopLeft); }
/// <summary> /// Returns an array of float values, each one corresponding /// to the width of the character at the same position of the /// source text. NOTE: Does not do any markup processing, and /// must only be used on single-line plaintext. /// </summary> public float[] GetCharacterWidths(string text, int startIndex, int endIndex, out float totalWidth) { totalWidth = 0f; var font = (dfDynamicFont)Font; var fontSize = Mathf.CeilToInt(font.FontSize * TextScale); var output = new float[text.Length]; var last = 0f; var maxWidth = 0f; font.RequestCharacters(text, fontSize, FontStyle.Normal); UnityEngine.CharacterInfo glyph = new UnityEngine.CharacterInfo(); for (int i = startIndex; i <= endIndex; i++, last = maxWidth) { if (!font.BaseFont.GetCharacterInfo(text[i], out glyph, fontSize, FontStyle.Normal)) { continue; } if (text[i] == '\t') { maxWidth += TabSize; } else if (text[i] == ' ') { maxWidth += glyph.width; } else { maxWidth += (glyph.vert.x + glyph.vert.width); } output[i] = (maxWidth - last) * PixelRatio; } return(output); }
public void init(Font font, Material material) { info_ = new CharacterInfo[CHAR_SIZE]; for (var i = 0; i < info_.Length; ++i) { char ch = (char)(i+CHAR_START); font.GetCharacterInfo(ch, out info_[i]); } info8_ = info_[(int)'8'-CHAR_START]; put_string_work_ = new char[128]; vertices_ = new Vector3[2][] { new Vector3[FONT_CHAR_MAX*4], new Vector3[FONT_CHAR_MAX*4], }; uvs_ = new Vector2[2][] { new Vector2[FONT_CHAR_MAX*4], new Vector2[FONT_CHAR_MAX*4], }; var triangles = new int[FONT_CHAR_MAX*6]; for (var i = 0; i < FONT_CHAR_MAX; ++i) { triangles[i*6+0] = i*4+0; triangles[i*6+1] = i*4+1; triangles[i*6+2] = i*4+2; triangles[i*6+3] = i*4+2; triangles[i*6+4] = i*4+1; triangles[i*6+5] = i*4+3; } mesh_ = new Mesh(); mesh_.name = "font"; mesh_.MarkDynamic(); mesh_.vertices = vertices_[0]; mesh_.uv = uvs_[0]; mesh_.triangles = triangles; mesh_.bounds = new Bounds(Vector3.zero, Vector3.one * 99999999); material_ = material; material_.SetColor("_Colors0", new Color(0f, 0f, 0f)); // None material_.SetColor("_Colors1", new Color(1f, 0.4f, 0.4f)); // Red material_.SetColor("_Colors2", new Color(0.4f, 0.4f, 1f)); // Blue material_.SetColor("_Colors3", new Color(1f, 0.4f, 1f)); // Magenta material_.SetColor("_Colors4", new Color(0.4f, 1f, 0.4f)); // Green material_.SetColor("_Colors5", new Color(1f, 1f, 0.4f)); // Yellow material_.SetColor("_Colors6", new Color(0.4f, 1f, 1f)); // Cyan material_.SetColor("_Colors7", new Color(1f, 1f, 1f)); // White }
private static void addUVCoords(dfList <Vector2> uvs, UnityEngine.CharacterInfo glyph) { var region = glyph.uv; var uvLeft = region.x; var uvTop = region.y + region.height; var uvRight = uvLeft + region.width; var uvBottom = region.y; if (glyph.flipped) { uvs.Add(new Vector2(uvRight, uvBottom)); uvs.Add(new Vector2(uvRight, uvTop)); uvs.Add(new Vector2(uvLeft, uvTop)); uvs.Add(new Vector2(uvLeft, uvBottom)); } else { uvs.Add(new Vector2(uvLeft, uvTop)); uvs.Add(new Vector2(uvRight, uvTop)); uvs.Add(new Vector2(uvRight, uvBottom)); uvs.Add(new Vector2(uvLeft, uvBottom)); } }
private void Create() { if (tex == null) { Debug.LogWarning("创建失败,图片为空!"); return; } if (string.IsNullOrEmpty(fontPath)) { Debug.LogWarning("字体路径为空!"); return; } if (fontName == null) { Debug.LogWarning("创建失败,字体名称为空!"); return; } if (File.Exists(Application.dataPath + fontPath + fontName + ".fontsettings")) { Debug.LogError("创建失败,已存在同名字体文件"); return; } if (File.Exists(Application.dataPath + fontPath + fontName + ".mat")) { Debug.LogError("创建失败,已存在同名字体材质文件"); return; } var selectionPath = AssetDatabase.GetAssetPath(tex); if (selectionPath.Contains("/Resources/")) { var selectionExt = Path.GetExtension(selectionPath); if (selectionExt.Length == 0) { Debug.LogError("创建失败!"); return; } var fontPathName = fontPath + fontName + ".fontsettings"; var matPathName = fontPath + fontName + ".mat"; var lineSpace = 0.1f; //string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length).Replace("Assets/Resources/", ""); var loadPath = selectionPath.Replace(selectionExt, "").Substring(selectionPath.IndexOf("/Resources/", StringComparison.Ordinal) + "/Resources/".Length); var sprites = Resources.LoadAll <Sprite>(loadPath); if (sprites.Length > 0) { var mat = new Material(Shader.Find("GUI/Text Shader")); mat.SetTexture(MainTex, tex); var m_myFont = new Font { material = mat }; var characterInfo = new UnityEngine.CharacterInfo[sprites.Length]; foreach (var t in sprites) { if (t.rect.height > lineSpace) { lineSpace = t.rect.height; } } for (var i = 0; i < sprites.Length; i++) { var spr = sprites[i]; var info = new UnityEngine.CharacterInfo(); try { info.index = Convert.ToInt32(spr.name); } catch { Debug.LogError("创建失败,Sprite名称错误!"); return; } var rect = spr.rect; var pivot = spr.pivot.y / rect.height - 0.5f; if (pivot > 0) { pivot = -lineSpace / 2 - spr.pivot.y; } else if (pivot < 0) { pivot = -lineSpace / 2 + rect.height - spr.pivot.y; } else { pivot = -lineSpace / 2; } var offsetY = (int)(pivot + (lineSpace - rect.height) / 2); info.uvBottomLeft = new Vector2(rect.x / tex.width, rect.y / tex.height); info.uvBottomRight = new Vector2((rect.x + rect.width) / tex.width, rect.y / tex.height); info.uvTopLeft = new Vector2(rect.x / tex.width, (rect.y + rect.height) / tex.height); info.uvTopRight = new Vector2((rect.x + rect.width) / tex.width, (rect.y + rect.height) / tex.height); info.minX = 0; info.minY = -(int)rect.height - offsetY; info.maxX = (int)rect.width; info.maxY = -offsetY; info.advance = (int)rect.width; characterInfo[i] = info; } AssetDatabase.CreateAsset(mat, "Assets" + matPathName); AssetDatabase.CreateAsset(m_myFont, "Assets" + fontPathName); m_myFont.characterInfo = characterInfo; EditorUtility.SetDirty(m_myFont); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); //刷新资源 Debug.Log("创建字体成功"); } else { Debug.LogError("图集错误!"); } } else { Debug.LogError("创建失败,选择的图片不在Resources文件夹内!"); } }
public PackNode ( PackNode f ) { this.CI = f.CI; this.CIIndex = f.CIIndex; this.StartRect = f.StartRect; this.ResultRect = f.ResultRect; this.Height = f.Height; this.SameOrient = f.SameOrient; }
public static Rect GetUvRect(ref CharacterInfo info, Texture2D texture) { #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 float x = info.uv.x * texture.width; float w = info.uv.width * texture.width; float y = info.uv.y * texture.height; float h = info.uv.height * texture.height; return new Rect(x, y, w, h); #else //Fillped判定 if( Mathf.Approximately( info.uvTopLeft.x, info.uvTopRight.x ) ) { float x = info.uvBottomLeft.x; float w = info.uvTopLeft.x - x; float y = info.uvTopRight.y; float h = info.uvTopLeft.y - y; return new Rect(x * texture.width, y * texture.height, w * texture.width, h * texture.height); } else { float x = info.uvTopLeft.x; float w = info.uvTopRight.x - x; float y = info.uvTopLeft.y; float h = info.uvBottomLeft.y - y; return new Rect(x * texture.width, y * texture.height, w * texture.width, h * texture.height); } #endif }
//Get, rotate if needed, and set the characterinfo from infont to outfont void SetOutFont () { CharacterInfo[] OutCharacterInfo = new CharacterInfo[nodes.Count]; for (int i = 0; i < resultPackNodeList.Count; i++) { Rect newUvRect = new Rect( resultPackNodeList[i].ResultRect.x / packedFontMap.width, resultPackNodeList[i].ResultRect.y / packedFontMap.height, resultPackNodeList[i].ResultRect.width / packedFontMap.width, resultPackNodeList[i].ResultRect.height / packedFontMap.height ); if (resultPackNodeList[i].SameOrient) { if (resultPackNodeList[i].CI.flipped) { newUvRect.y = newUvRect.y + newUvRect.height; newUvRect.height = newUvRect.height * -1; } else { //The rect hasnt been rotated, and wasnt originally flipped anyway. } } else { if (resultPackNodeList[i].CI.flipped) { resultPackNodeList[i].CI.flipped = false; } else { newUvRect.y = newUvRect.y + newUvRect.height; newUvRect.height = newUvRect.height * -1; resultPackNodeList[i].CI.flipped = true; resultPackNodeList[i].CI.vert.width = resultPackNodeList[i].ResultRect.height; resultPackNodeList[i].CI.vert.height = resultPackNodeList[i].ResultRect.width * -1; } } resultPackNodeList[i].CI.uv = newUvRect; OutCharacterInfo[resultPackNodeList[i].CIIndex] = resultPackNodeList[i].CI; } packedFont.characterInfo = OutCharacterInfo; EditorUtility.SetDirty( packedFont ); EditorApplication.SaveAssets(); AssetDatabase.Refresh(); }
static public int get_size(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); pushValue(l, o.size); return(1); }
private void renderText( dfMarkupToken token, Color32 color, Vector3 position, dfRenderData renderData ) { try { //@Profiler.BeginSample( "Render text token" ); var font = (dfDynamicFont)Font; var fontSize = Mathf.CeilToInt( font.FontSize * TextScale ); var fontStyle = FontStyle.Normal; var glyph = new UnityEngine.CharacterInfo(); var descent = font.Descent; var verts = renderData.Vertices; var triangles = renderData.Triangles; var uvs = renderData.UV; var colors = renderData.Colors; var x = position.x; var y = position.y; // Set the render material in the output buffer *after* requesting // glyph data, which may result in CharacterInfo in the dfDynamicFont's // texture atlas being rebuilt. renderData.Material = font.Material; var topColor = applyOpacity( multiplyColors( color, DefaultColor ) ); var bottomColor = topColor; if( BottomColor.HasValue ) { bottomColor = applyOpacity( multiplyColors( color, BottomColor.Value ) ); } for( int i = 0; i < token.Length; i++ ) { if( i > 0 ) x += CharacterSpacing * TextScale; if( !font.baseFont.GetCharacterInfo( token[ i ], out glyph, fontSize, fontStyle ) ) continue; var yadjust = ( font.FontSize + glyph.vert.y ) - fontSize + descent; var quadLeft = ( x + glyph.vert.x ); var quadTop = ( y + yadjust ); var quadRight = ( quadLeft + glyph.vert.width ); var quadBottom = ( quadTop + glyph.vert.height ); var v0 = new Vector3( quadLeft, quadTop ) * PixelRatio; var v1 = new Vector3( quadRight, quadTop ) * PixelRatio; var v2 = new Vector3( quadRight, quadBottom ) * PixelRatio; var v3 = new Vector3( quadLeft, quadBottom ) * PixelRatio; if( Shadow ) { addTriangleIndices( verts, triangles ); var activeShadowOffset = (Vector3)ShadowOffset * PixelRatio; verts.Add( v0 + activeShadowOffset ); verts.Add( v1 + activeShadowOffset ); verts.Add( v2 + activeShadowOffset ); verts.Add( v3 + activeShadowOffset ); var activeShadowColor = applyOpacity( ShadowColor ); colors.Add( activeShadowColor ); colors.Add( activeShadowColor ); colors.Add( activeShadowColor ); colors.Add( activeShadowColor ); addUVCoords( uvs, glyph ); } if( Outline ) { for( int o = 0; o < OUTLINE_OFFSETS.Length; o++ ) { addTriangleIndices( verts, triangles ); var activeOutlineOffset = (Vector3)OUTLINE_OFFSETS[ o ] * OutlineSize * PixelRatio; verts.Add( v0 + activeOutlineOffset ); verts.Add( v1 + activeOutlineOffset ); verts.Add( v2 + activeOutlineOffset ); verts.Add( v3 + activeOutlineOffset ); var activeOutlineColor = applyOpacity( OutlineColor ); colors.Add( activeOutlineColor ); colors.Add( activeOutlineColor ); colors.Add( activeOutlineColor ); colors.Add( activeOutlineColor ); addUVCoords( uvs, glyph ); } } addTriangleIndices( verts, triangles ); verts.Add( v0 ); verts.Add( v1 ); verts.Add( v2 ); verts.Add( v3 ); colors.Add( topColor ); colors.Add( topColor ); colors.Add( bottomColor ); colors.Add( bottomColor ); addUVCoords( uvs, glyph ); x += Mathf.CeilToInt( glyph.vert.x + glyph.vert.width ); } } finally { //@Profiler.EndSample(); } }
private void renderText(dfMarkupToken token, Color32 color, Vector3 position, dfRenderData renderData) { try { //@Profiler.BeginSample( "Render text token" ); var font = (dfDynamicFont)Font; var fontSize = Mathf.CeilToInt(font.FontSize * TextScale); var fontStyle = FontStyle.Normal; var glyph = new UnityEngine.CharacterInfo(); var descent = font.Descent; var verts = renderData.Vertices; var triangles = renderData.Triangles; var uvs = renderData.UV; var colors = renderData.Colors; var x = position.x; var y = position.y; // Set the render material in the output buffer *after* requesting // glyph data, which may result in CharacterInfo in the dfDynamicFont's // texture atlas being rebuilt. renderData.Material = font.Material; var topColor = applyOpacity(multiplyColors(color, DefaultColor)); var bottomColor = topColor; if (BottomColor.HasValue) { bottomColor = applyOpacity(multiplyColors(color, BottomColor.Value)); } for (int i = 0; i < token.Length; i++) { if (i > 0) { x += CharacterSpacing * TextScale; } if (!font.baseFont.GetCharacterInfo(token[i], out glyph, fontSize, fontStyle)) { continue; } var yadjust = (font.FontSize + glyph.vert.y) - fontSize + descent; var quadLeft = (x + glyph.vert.x); var quadTop = (y + yadjust); var quadRight = (quadLeft + glyph.vert.width); var quadBottom = (quadTop + glyph.vert.height); var v0 = new Vector3(quadLeft, quadTop) * PixelRatio; var v1 = new Vector3(quadRight, quadTop) * PixelRatio; var v2 = new Vector3(quadRight, quadBottom) * PixelRatio; var v3 = new Vector3(quadLeft, quadBottom) * PixelRatio; if (Shadow) { addTriangleIndices(verts, triangles); var activeShadowOffset = (Vector3)ShadowOffset * PixelRatio; verts.Add(v0 + activeShadowOffset); verts.Add(v1 + activeShadowOffset); verts.Add(v2 + activeShadowOffset); verts.Add(v3 + activeShadowOffset); var activeShadowColor = applyOpacity(ShadowColor); colors.Add(activeShadowColor); colors.Add(activeShadowColor); colors.Add(activeShadowColor); colors.Add(activeShadowColor); addUVCoords(uvs, glyph); } if (Outline) { for (int o = 0; o < OUTLINE_OFFSETS.Length; o++) { addTriangleIndices(verts, triangles); var activeOutlineOffset = (Vector3)OUTLINE_OFFSETS[o] * OutlineSize * PixelRatio; verts.Add(v0 + activeOutlineOffset); verts.Add(v1 + activeOutlineOffset); verts.Add(v2 + activeOutlineOffset); verts.Add(v3 + activeOutlineOffset); var activeOutlineColor = applyOpacity(OutlineColor); colors.Add(activeOutlineColor); colors.Add(activeOutlineColor); colors.Add(activeOutlineColor); colors.Add(activeOutlineColor); addUVCoords(uvs, glyph); } } addTriangleIndices(verts, triangles); verts.Add(v0); verts.Add(v1); verts.Add(v2); verts.Add(v3); colors.Add(topColor); colors.Add(topColor); colors.Add(bottomColor); colors.Add(bottomColor); addUVCoords(uvs, glyph); x += Mathf.CeilToInt(glyph.vert.x + glyph.vert.width); } } finally { //@Profiler.EndSample(); } }
public bool GetCharacterInfo(char ch, out CharacterInfo info, int size, FontStyle style) { info = default(CharacterInfo); return default(bool); }
private void getGlyphData( UnityEngine.CharacterInfo[] result, string text, int size, FontStyle style ) { // Ensure that there is enough space for the text if( text.Length > glyphBuffer.Length ) { glyphBuffer = new UnityEngine.CharacterInfo[ text.Length + 512 ]; } int i = 0; for( ; i < text.Length; i++ ) { // Get the character info and copy it into the static buffer if( !baseFont.GetCharacterInfo( text[ i ], out result[ i ], size, style ) ) { // If the character info was not found, create a dummy // record in its place result[ i ] = new UnityEngine.CharacterInfo() { flipped = false, index = -1, size = size, style = style, width = size * 0.25f }; } } // Clear the glyph immediately after the requested data if( i < result.Length ) { result[ i ] = new UnityEngine.CharacterInfo(); } }
public bool GetCharacterInfo(char ch, out CharacterInfo info, int size) { FontStyle normal = FontStyle.Normal; return this.GetCharacterInfo(ch, out info, size, normal); }
bool GetCharacterInfo(char m_character, ref CustomCharacterInfo char_info) { if(m_character.Equals('\n') || m_character.Equals('\r')) { return true; } if(m_font != null) { if(!m_current_font_name.Equals(m_font.name)) { // Recalculate font's baseline value // Checks through all available alpha characters and uses the most common bottom y_axis value as the baseline for the font. #pragma warning disable Dictionary<float, int> baseline_values = new Dictionary<float, int>(); float baseline; foreach(CharacterInfo character in m_font.characterInfo) { // only check alpha characters (a-z, A-Z) if((character.index >= 97 && character.index < 123) || (character.index >= 65 && character.index < 91)) { baseline = -character.vert.y - character.vert.height; if(baseline_values.ContainsKey(baseline)) baseline_values[baseline] ++; else baseline_values[baseline] = 1; } } #pragma warning restore // Find most common baseline value used by the letters int idx=0; int highest_num=0, highest_idx=-1; float most_common_baseline = -1; foreach(int num in baseline_values.Values) { if(highest_idx == -1 || num > highest_num) { highest_idx = idx; highest_num = num; } idx++; } // Retrieve the most common value and use as baseline value idx=0; foreach(float baseline_key in baseline_values.Keys) { if(idx == highest_idx) { most_common_baseline = baseline_key; break; } idx++; } m_font_baseline = most_common_baseline; // Set font name to current, to ensure this check doesn't happen each time m_current_font_name = m_font.name; } CharacterInfo font_char_info = new CharacterInfo(); m_font.GetCharacterInfo(m_character, out font_char_info); #pragma warning disable char_info.flipped = font_char_info.flipped; char_info.uv = font_char_info.uv; char_info.vert = font_char_info.vert; char_info.width = font_char_info.width; // Scale char_info values char_info.vert.x /= FontScale; char_info.vert.y /= FontScale; char_info.vert.width /= FontScale; char_info.vert.height /= FontScale; char_info.width /= FontScale; if(font_char_info.width == 0) { // Invisible character info returned because character is not contained within the font Debug.LogWarning("Character '" + GetHumanReadableCharacterString(m_character) + "' not found. Check that font '" + m_font.name + "' supports this character."); } #pragma warning restore return true; } if(m_font_data_file != null) { if(m_custom_font_data == null || !m_font_data_file.name.Equals(m_current_font_data_file_name)) { // Setup m_custom_font_data for the custom font. #if !UNITY_WINRT if(m_font_data_file.text.Substring(0,5).Equals("<?xml")) { // Text file is in xml format m_current_font_data_file_name = m_font_data_file.name; m_custom_font_data = new CustomFontCharacterData(); XmlTextReader reader = new XmlTextReader(new StringReader(m_font_data_file.text)); int texture_width = 0; int texture_height = 0; int uv_x, uv_y; float width, height, xoffset, yoffset, xadvance; CustomCharacterInfo character_info; while(reader.Read()) { if(reader.IsStartElement()) { if(reader.Name.Equals("common")) { texture_width = int.Parse(reader.GetAttribute("scaleW")); texture_height = int.Parse(reader.GetAttribute("scaleH")); m_font_baseline = int.Parse(reader.GetAttribute("base")); } else if(reader.Name.Equals("char")) { uv_x = int.Parse(reader.GetAttribute("x")); uv_y = int.Parse(reader.GetAttribute("y")); width = float.Parse(reader.GetAttribute("width")); height = float.Parse(reader.GetAttribute("height")); xoffset = float.Parse(reader.GetAttribute("xoffset")); yoffset = float.Parse(reader.GetAttribute("yoffset")); xadvance = float.Parse(reader.GetAttribute("xadvance")); character_info = new CustomCharacterInfo(); character_info.flipped = false; character_info.uv = new Rect((float) uv_x / (float) texture_width, 1 - ((float)uv_y / (float)texture_height) - (float)height/(float)texture_height, (float)width/(float)texture_width, (float)height/(float)texture_height); character_info.vert = new Rect(xoffset,-yoffset,width, -height); character_info.width = xadvance; m_custom_font_data.m_character_infos.Add( int.Parse(reader.GetAttribute("id")), character_info); } } } } else #endif if(m_font_data_file.text.Substring(0,4).Equals("info")) { // Plain txt format m_current_font_data_file_name = m_font_data_file.name; m_custom_font_data = new CustomFontCharacterData(); int texture_width = 0; int texture_height = 0; int uv_x, uv_y; float width, height, xoffset, yoffset, xadvance; CustomCharacterInfo character_info; string[] data_fields; string[] text_lines = m_font_data_file.text.Split(new char[]{'\n'}); foreach(string font_data in text_lines) { if(font_data.Length >= 5 && font_data.Substring(0,5).Equals("char ")) { // character data line data_fields = ParseFieldData(font_data, new string[]{"id=", "x=", "y=", "width=", "height=", "xoffset=", "yoffset=", "xadvance="}); uv_x = int.Parse(data_fields[1]); uv_y = int.Parse(data_fields[2]); width = float.Parse(data_fields[3]); height = float.Parse(data_fields[4]); xoffset = float.Parse(data_fields[5]); yoffset = float.Parse(data_fields[6]); xadvance = float.Parse(data_fields[7]); character_info = new CustomCharacterInfo(); character_info.flipped = false; character_info.uv = new Rect((float) uv_x / (float) texture_width, 1 - ((float)uv_y / (float)texture_height) - (float)height/(float)texture_height, (float)width/(float)texture_width, (float)height/(float)texture_height); character_info.vert = new Rect(xoffset,-yoffset +1,width, -height); character_info.width = xadvance; m_custom_font_data.m_character_infos.Add( int.Parse(data_fields[0]), character_info); } else if(font_data.Length >= 6 && font_data.Substring(0,6).Equals("common")) { data_fields = ParseFieldData(font_data, new string[]{"scaleW=", "scaleH=", "base="}); texture_width = int.Parse(data_fields[0]); texture_height = int.Parse(data_fields[1]); m_font_baseline = int.Parse(data_fields[2]); } } } } if(m_custom_font_data.m_character_infos.ContainsKey((int) m_character)) { ((CustomCharacterInfo) m_custom_font_data.m_character_infos[(int)m_character]).ScaleClone(FontScale, ref char_info); return true; } } return false; }
public bool GetCharacterInfo(char ch, out CharacterInfo info, [DefaultValue("0")] int size, [DefaultValue("FontStyle.Normal")] FontStyle style);
[ExcludeFromDocs] public bool GetCharacterInfo(char ch, out CharacterInfo info) { return(GetCharacterInfo(ch, out info, 0, FontStyle.Normal)); }
private void renderText( string text ) { var font = Style.Font; var fontSize = Style.FontSize; var fontStyle = Style.FontStyle; var glyph = new UnityEngine.CharacterInfo(); var verts = renderData.Vertices; var triangles = renderData.Triangles; var uvs = renderData.UV; var colors = renderData.Colors; var multiplier = (float)fontSize / (float)font.FontSize; var descent = font.Descent * multiplier; var x = 0f; // Ensure that the baseFont's texture contains all characters before // rendering any text. font.RequestCharacters( text, fontSize, fontStyle ); // Set the render material in the output buffer *after* requesting // glyph data, which may result in CharacterInfo in the dfDynamicFont's // texture atlas being rebuilt. renderData.Material = font.Material; for( int i = 0; i < text.Length; i++ ) { if( !font.BaseFont.GetCharacterInfo( text[ i ], out glyph, fontSize, fontStyle ) ) continue; addTriangleIndices( verts, triangles ); var yadjust = ( font.FontSize + glyph.vert.y ) - fontSize + descent; var quadLeft = ( x + glyph.vert.x ); var quadTop = ( yadjust ); var quadRight = ( quadLeft + glyph.vert.width ); var quadBottom = ( quadTop + glyph.vert.height ); var v0 = new Vector3( quadLeft, quadTop ); var v1 = new Vector3( quadRight, quadTop ); var v2 = new Vector3( quadRight, quadBottom ); var v3 = new Vector3( quadLeft, quadBottom ); verts.Add( v0 ); verts.Add( v1 ); verts.Add( v2 ); verts.Add( v3 ); var glyphColor = Style.Color; colors.Add( glyphColor ); colors.Add( glyphColor ); colors.Add( glyphColor ); colors.Add( glyphColor ); var region = glyph.uv; var uvLeft = region.x; var uvTop = region.y + region.height; var uvRight = uvLeft + region.width; var uvBottom = region.y; if( glyph.flipped ) { uvs.Add( new Vector2( uvRight, uvBottom ) ); uvs.Add( new Vector2( uvRight, uvTop ) ); uvs.Add( new Vector2( uvLeft, uvTop ) ); uvs.Add( new Vector2( uvLeft, uvBottom ) ); } else { uvs.Add( new Vector2( uvLeft, uvTop ) ); uvs.Add( new Vector2( uvRight, uvTop ) ); uvs.Add( new Vector2( uvRight, uvBottom ) ); uvs.Add( new Vector2( uvLeft, uvBottom ) ); } x += Mathf.CeilToInt( glyph.vert.x + glyph.vert.width ); } }
/// <summary> /// Returns a Vector2 structure containing the width and height that would be /// required to render a single line of text at the given size and with the given style /// </summary> /// <param name="text">The single line of text to measure</param> /// <param name="size">The desired font size</param> /// <param name="style">The desired text style</param> /// <returns></returns> public Vector2 MeasureText( string text, int size, FontStyle style ) { RequestCharacters( text, size, style ); var multiplier = (float)size / (float)FontSize; var lineHeight = Mathf.CeilToInt( Baseline * multiplier ); var measuredSize = new Vector2( 0, lineHeight ); UnityEngine.CharacterInfo glyph = new UnityEngine.CharacterInfo(); for( int i = 0; i < text.Length; i++ ) { BaseFont.GetCharacterInfo( text[ i ], out glyph, size, style ); var width = Mathf.Ceil( glyph.vert.x + glyph.vert.width ); if( text[ i ] == ' ' ) { width = Mathf.Ceil( glyph.width ); } else if( text[ i ] == '\t' ) { width += size * 4; } measuredSize.x += width; } return measuredSize; }
public void DoXMLPase(ref string content) { XmlDocument xml = new XmlDocument(); xml.LoadXml(content); XmlNode info = xml.GetElementsByTagName("info")[0]; XmlNode common = xml.GetElementsByTagName("common")[0]; XmlNode page = xml.GetElementsByTagName("pages")[0].FirstChild; XmlNodeList chars = xml.GetElementsByTagName("chars")[0].ChildNodes; fontName = info.Attributes.GetNamedItem("face").InnerText; fontSize = ToInt(info, "size"); lineHeight = ToInt(common, "lineHeight"); lineBaseHeight = ToInt(common, "base"); textureWidth = ToInt(common, "scaleW"); textureHeight = ToInt(common, "scaleH"); textureName = page.Attributes.GetNamedItem("file").InnerText; charInfos = new CharacterInfo[chars.Count]; for (int i = 0; i < chars.Count; i++) { XmlNode charNode = chars[i]; charInfos[i] = CreateCharInfo( ToInt(charNode, "id"), ToInt(charNode, "x"), ToInt(charNode, "y"), ToInt(charNode, "width"), ToInt(charNode, "height"), ToInt(charNode, "xoffset"), ToInt(charNode, "yoffset"), ToInt(charNode, "xadvance")); } // kernings XmlNode kerningsNode = xml.GetElementsByTagName("kernings")[0]; if (kerningsNode != null && kerningsNode.HasChildNodes) { XmlNodeList kerns = kerningsNode.ChildNodes; kernings = new Kerning[kerns.Count]; for (int i = 0; i < kerns.Count; i++) { XmlNode kerningNode = kerns[i]; kernings[i] = new Kerning(); kernings[i].first = ToInt(kerningNode, "first"); kernings[i].second = ToInt(kerningNode, "second"); kernings[i].amount = ToInt(kerningNode, "amount"); } } }
/// <summary> /// Returns an array of float values, each one corresponding /// to the width of the character at the same position of the /// source text. NOTE: Does not do any markup processing, and /// must only be used on single-line plaintext. /// </summary> public float[] GetCharacterWidths( string text, int startIndex, int endIndex, out float totalWidth ) { totalWidth = 0f; var font = (dfDynamicFont)Font; var fontSize = Mathf.CeilToInt( font.FontSize * TextScale ); var output = new float[ text.Length ]; var last = 0f; var maxWidth = 0f; font.RequestCharacters( text, fontSize, FontStyle.Normal ); UnityEngine.CharacterInfo glyph = new UnityEngine.CharacterInfo(); for( int i = startIndex; i <= endIndex; i++, last = maxWidth ) { if( !font.BaseFont.GetCharacterInfo( text[ i ], out glyph, fontSize, FontStyle.Normal ) ) continue; if( text[ i ] == '\t' ) { maxWidth += TabSize; } else if( text[ i ] == ' ' ) { maxWidth += glyph.width; } else { maxWidth += ( glyph.vert.x + glyph.vert.width ); } output[ i ] = ( maxWidth - last ) * PixelRatio; } return output; }
private CharacterInfo CreateCharInfo(int id, int x, int y, int w, int h, int xo, int yo, int xadvance) { Rect uv = new Rect(); uv.x = (float)x / textureWidth; uv.y = (float)y / textureHeight; uv.width = (float)w / textureWidth; uv.height = (float)h / textureHeight; uv.y = 1f - uv.y - uv.height; Rect vert = new Rect(); vert.x = xo; vert.y = yo; vert.width = w; vert.height = h; vert.y = -vert.y; vert.height = -vert.height; CharacterInfo charInfo = new CharacterInfo(); charInfo.index = id; #if UNITY_5_3_OR_NEWER || UNITY_5_3 || UNITY_5_2 charInfo.uvBottomLeft = new Vector2(uv.xMin, uv.yMin); charInfo.uvBottomRight = new Vector2(uv.xMax, uv.yMin); charInfo.uvTopLeft = new Vector2(uv.xMin, uv.yMax); charInfo.uvTopRight = new Vector2(uv.xMax, uv.yMax); charInfo.minX = (int)vert.xMin; charInfo.maxX = (int)vert.xMax; charInfo.minY = (int)vert.yMax; charInfo.maxY = (int)vert.yMin; charInfo.bearing = (int)vert.x; charInfo.advance = xadvance; #else charInfo.uv = uv; charInfo.vert = vert; charInfo.width = xadvance; #endif return charInfo; }
static public int get_style(IntPtr l) { UnityEngine.CharacterInfo o = (UnityEngine.CharacterInfo)checkSelf(l); pushEnum(l, (int)o.style); return(1); }
public bool GetCharacterInfo(char ch, out CharacterInfo info) { info = default(CharacterInfo); return default(bool); }
/// <summary> /// Calculates the size, in pixels, required to render this /// token on screen. Does not account for scale. /// </summary> /// <param name="token"></param> private void calculateTokenRenderSize(dfMarkupToken token) { try { //@Profiler.BeginSample( "Calculate token render size" ); var totalWidth = 0f; var ch = '\0'; var font = (dfDynamicFont)Font; var glyph = new UnityEngine.CharacterInfo(); if (token.TokenType == dfMarkupTokenType.Text) { var fontSize = Mathf.CeilToInt(font.FontSize * TextScale); for (int i = 0; i < token.Length; i++) { // Dereference the original character and obtain character information ch = token[i]; font.baseFont.GetCharacterInfo(ch, out glyph, fontSize, FontStyle.Normal); // TODO: Implement 'tab stops' calculation if (ch == '\t') { totalWidth += this.TabSize; continue; } // Add the character width to the total totalWidth += (ch != ' ') ? (glyph.vert.x + glyph.vert.width) : (glyph.width + CharacterSpacing * TextScale); } if (token.Length > 2) { totalWidth += (token.Length - 2) * CharacterSpacing * TextScale; } token.Height = Font.LineHeight; token.Width = Mathf.CeilToInt(totalWidth); } else if (token.TokenType == dfMarkupTokenType.Whitespace) { var fontSize = Mathf.CeilToInt(font.FontSize * TextScale); var spacing = CharacterSpacing * TextScale; for (int i = 0; i < token.Length; i++) { // Dereference the original character ch = token[i]; // TODO: Implement 'tab stops' calculation if (ch == '\t') { totalWidth += this.TabSize; } else if (ch == ' ') { font.baseFont.GetCharacterInfo(ch, out glyph, fontSize, FontStyle.Normal); totalWidth += glyph.width + spacing; } } token.Height = Font.LineHeight; token.Width = Mathf.CeilToInt(totalWidth); } else if (token.TokenType == dfMarkupTokenType.StartTag) { if (token.Matches("sprite") && SpriteAtlas != null) { if (token.AttributeCount == 1) { var texture = SpriteAtlas.Texture; var lineHeight = font.Baseline * TextScale; var spriteName = token.GetAttribute(0).Value.Value; var sprite = SpriteAtlas[spriteName]; if (sprite != null) { var aspectRatio = (sprite.region.width * texture.width) / (sprite.region.height * texture.height); totalWidth = Mathf.CeilToInt(lineHeight * aspectRatio); } token.Height = Mathf.CeilToInt(lineHeight); token.Width = Mathf.CeilToInt(totalWidth); } } } } finally { //@Profiler.EndSample(); } }
private void renderText(string text) { var font = Style.Font; var fontSize = Style.FontSize; var fontStyle = Style.FontStyle; var glyph = new UnityEngine.CharacterInfo(); var verts = renderData.Vertices; var triangles = renderData.Triangles; var uvs = renderData.UV; var colors = renderData.Colors; var multiplier = (float)fontSize / (float)font.FontSize; var descent = font.Descent * multiplier; var x = 0f; // Ensure that the baseFont's texture contains all characters before // rendering any text. font.RequestCharacters(text, fontSize, fontStyle); // Set the render material in the output buffer *after* requesting // glyph data, which may result in CharacterInfo in the dfDynamicFont's // texture atlas being rebuilt. renderData.Material = font.Material; for (int i = 0; i < text.Length; i++) { if (!font.BaseFont.GetCharacterInfo(text[i], out glyph, fontSize, fontStyle)) { continue; } addTriangleIndices(verts, triangles); var yadjust = (font.FontSize + glyph.vert.y) - fontSize + descent; var quadLeft = (x + glyph.vert.x); var quadTop = (yadjust); var quadRight = (quadLeft + glyph.vert.width); var quadBottom = (quadTop + glyph.vert.height); var v0 = new Vector3(quadLeft, quadTop); var v1 = new Vector3(quadRight, quadTop); var v2 = new Vector3(quadRight, quadBottom); var v3 = new Vector3(quadLeft, quadBottom); verts.Add(v0); verts.Add(v1); verts.Add(v2); verts.Add(v3); var glyphColor = Style.Color; colors.Add(glyphColor); colors.Add(glyphColor); colors.Add(glyphColor); colors.Add(glyphColor); var region = glyph.uv; var uvLeft = region.x; var uvTop = region.y + region.height; var uvRight = uvLeft + region.width; var uvBottom = region.y; if (glyph.flipped) { uvs.Add(new Vector2(uvRight, uvBottom)); uvs.Add(new Vector2(uvRight, uvTop)); uvs.Add(new Vector2(uvLeft, uvTop)); uvs.Add(new Vector2(uvLeft, uvBottom)); } else { uvs.Add(new Vector2(uvLeft, uvTop)); uvs.Add(new Vector2(uvRight, uvTop)); uvs.Add(new Vector2(uvRight, uvBottom)); uvs.Add(new Vector2(uvLeft, uvBottom)); } x += Mathf.CeilToInt(glyph.vert.x + glyph.vert.width); } }
void UnityPack () { Texture2D[] texs = new Texture2D[nodes.Count]; Rect[] rects = new Rect[nodes.Count]; packedFontMap = new Texture2D( 1, 1 ); for (int i = 0; i < nodes.Count; i++) { texs[i] = new Texture2D( (int) nodes[i].rect.width, (int) nodes[i].rect.height ); texs[i].SetPixels( fontMap.GetPixels( (int) nodes[i].rect.x, (int) nodes[i].rect.y, (int) nodes[i].rect.width, (int) nodes[i].rect.height ) ); } rects = packedFontMap.PackTextures( texs, packBuffer ); for (int i = 0; i < nodes.Count; i++) { PackNode newNode = new PackNode( startPackNodeList[i] ); newNode.ResultRect = rects[i]; resultPackNodeList.Add( newNode ); } byte[] bytes = packedFontMap.EncodeToPNG(); System.IO.File.WriteAllBytes( packedfontMapPath, bytes ); CharacterInfo[] OutCharacterInfo = new CharacterInfo[nodes.Count]; for (int i = 0; i < resultPackNodeList.Count; i++) { Rect newUvRect = resultPackNodeList[i].ResultRect; if (resultPackNodeList[i].SameOrient) { if (resultPackNodeList[i].CI.flipped) { newUvRect.y = newUvRect.y + newUvRect.height; newUvRect.height = newUvRect.height * -1; } else { //The rect hasnt been rotated, and wasnt originally flipped anyway. } } else { if (resultPackNodeList[i].CI.flipped) { resultPackNodeList[i].CI.flipped = false; } else { newUvRect.y = newUvRect.y + newUvRect.height; newUvRect.height = newUvRect.height * -1; resultPackNodeList[i].CI.flipped = true; resultPackNodeList[i].CI.vert.width = resultPackNodeList[i].ResultRect.height; resultPackNodeList[i].CI.vert.height = resultPackNodeList[i].ResultRect.width * -1; } } resultPackNodeList[i].CI.uv = newUvRect; OutCharacterInfo[resultPackNodeList[i].CIIndex] = resultPackNodeList[i].CI; } packedFont.characterInfo = OutCharacterInfo; EditorUtility.SetDirty( packedFont ); EditorApplication.SaveAssets(); AssetDatabase.Refresh(); packedFont.material.mainTexture = (Texture2D) AssetDatabase.LoadAssetAtPath( packedfontMapPath, typeof( Texture2D ) ); Debug.Log( "Pack Complete, Result texture size: " + packedFontMap.width + "x" + packedFontMap.height + " = " + packedFontMap.width * packedFontMap.height + " Pixels" ); Debug.Log( "Pack fits inside " + packMaxSizeX + "x" + packMaxSizeY + " = " + packMaxSizeX * packMaxSizeY + " Pixels, and contains " + resultPackNodeList.Count + " characters" ); }
public extern bool GetCharacterInfo(char ch, out CharacterInfo info, [UnityEngine.Internal.DefaultValue("0")] int size, [UnityEngine.Internal.DefaultValue("FontStyle.Normal")] FontStyle style);
public PackNode () { this.CI = new CharacterInfo(); this.CIIndex = 0; this.StartRect = new Rect( 0, 0, 0, 0 ); this.ResultRect = new Rect( 0, 0, 0, 0 ); this.Height = 0; this.SameOrient = true; }
public bool GetCharacterInfo(char ch, out CharacterInfo info) { FontStyle style = FontStyle.Normal; int size = 0; return this.GetCharacterInfo(ch, out info, size, style); }
public static void SetFontRenderInfo(char c, ref CharacterInfo info, float offsetY, float fontSize, out Vector3 offset, out float width, out float kerningWidth) { #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 float x1 = info.vert.x + (info.vert.width) / 2; float y1 = info.vert.y + (info.vert.height) / 2 + offsetY; offset = new Vector3(x1, y1, 0); width = GetCharacterInfoWidth(ref info); //カーニングする場合の、表示位置 kerningWidth = info.vert.width; #else float x1 = info.minX + (info.maxX - info.minX) / 2; float y1 = info.maxY - (info.glyphHeight + fontSize) / 2 + offsetY + fontSize/5; offset = new Vector3(x1, y1, 0); width = GetCharacterInfoWidth(ref info); //カーニングする場合の、表示位置 kerningWidth = info.maxX - info.minX; #endif }
/// <summary> /// Calculates the size, in pixels, required to render this /// token on screen. Does not account for scale. /// </summary> /// <param name="token"></param> private void calculateTokenRenderSize( dfMarkupToken token ) { try { //@Profiler.BeginSample( "Calculate token render size" ); var totalWidth = 0f; var ch = '\0'; var font = (dfDynamicFont)Font; var glyph = new UnityEngine.CharacterInfo(); if( token.TokenType == dfMarkupTokenType.Text ) { var fontSize = Mathf.CeilToInt( font.FontSize * TextScale ); for( int i = 0; i < token.Length; i++ ) { // Dereference the original character and obtain character information ch = token[ i ]; font.baseFont.GetCharacterInfo( ch, out glyph, fontSize, FontStyle.Normal ); // TODO: Implement 'tab stops' calculation if( ch == '\t' ) { totalWidth += this.TabSize; continue; } // Add the character width to the total totalWidth += ( ch != ' ' ) ? ( glyph.vert.x + glyph.vert.width ) : ( glyph.width + CharacterSpacing * TextScale ); } if( token.Length > 2 ) { totalWidth += ( token.Length - 2 ) * CharacterSpacing * TextScale; } token.Height = Font.LineHeight; token.Width = Mathf.CeilToInt( totalWidth ); } else if( token.TokenType == dfMarkupTokenType.Whitespace ) { var fontSize = Mathf.CeilToInt( font.FontSize * TextScale ); var spacing = CharacterSpacing * TextScale; for( int i = 0; i < token.Length; i++ ) { // Dereference the original character ch = token[ i ]; // TODO: Implement 'tab stops' calculation if( ch == '\t' ) { totalWidth += this.TabSize; } else if( ch == ' ' ) { font.baseFont.GetCharacterInfo( ch, out glyph, fontSize, FontStyle.Normal ); totalWidth += glyph.width + spacing; } } token.Height = Font.LineHeight; token.Width = Mathf.CeilToInt( totalWidth ); } else if( token.TokenType == dfMarkupTokenType.StartTag ) { if( token.Matches( "sprite" ) && SpriteAtlas != null ) { if( token.AttributeCount == 1 ) { var texture = SpriteAtlas.Texture; var lineHeight = font.Baseline * TextScale; var spriteName = token.GetAttribute( 0 ).Value.Value; var sprite = SpriteAtlas[ spriteName ]; if( sprite != null ) { var aspectRatio = ( sprite.region.width * texture.width ) / ( sprite.region.height * texture.height ); totalWidth = Mathf.CeilToInt( lineHeight * aspectRatio ); } token.Height = Mathf.CeilToInt( lineHeight ); token.Width = Mathf.CeilToInt( totalWidth ); } } } } finally { //@Profiler.EndSample(); } }
public static float GetCharacterInfoWidth(ref CharacterInfo charInfo) { #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 return charInfo.width; #else return charInfo.advance; #endif }
/// <summary> /// Sets the Text property of this element and measures the size /// of the text as it would be rendered with the current style. /// Designed for internal use by elements that need to create /// text boxes dynamically, such as list items. /// </summary> /// <param name="text"></param> internal void SetText( string text ) { this.Text = text; if( Style.Font == null ) return; isWhitespace = whitespacePattern.IsMatch( this.Text ); var effectiveText = Style.PreserveWhitespace || !isWhitespace ? this.Text : " "; var currentFontSize = Style.FontSize; var size = new Vector2( 0, Style.LineHeight ); Style.Font.RequestCharacters( effectiveText, Style.FontSize, Style.FontStyle ); var glyph = new UnityEngine.CharacterInfo(); for( int i = 0; i < effectiveText.Length; i++ ) { if( !Style.Font.BaseFont.GetCharacterInfo( effectiveText[ i ], out glyph, currentFontSize, Style.FontStyle ) ) continue; var width = glyph.vert.x + glyph.vert.width; if( effectiveText[ i ] == ' ' ) { width = Mathf.Max( width, currentFontSize * 0.33f ); } else if( effectiveText[ i ] == '\t' ) { width += currentFontSize * 3; } size.x += width; } this.Size = size; var font = Style.Font; var multiplier = (float)currentFontSize / (float)font.FontSize; this.Baseline = Mathf.CeilToInt( font.Baseline * multiplier ); }
/// <summary> /// Read the data using the reader. /// </summary> /// <param name="reader">Reader.</param> public override object Read(ISaveGameReader reader) { UnityEngine.CharacterInfo characterInfo = new UnityEngine.CharacterInfo(); foreach (string property in reader.Properties) { switch (property) { case "index": characterInfo.index = reader.ReadProperty <System.Int32> (); break; case "size": characterInfo.size = reader.ReadProperty <System.Int32> (); break; case "style": characterInfo.style = reader.ReadProperty <UnityEngine.FontStyle> (); break; case "advance": characterInfo.advance = reader.ReadProperty <System.Int32> (); break; case "glyphWidth": characterInfo.glyphWidth = reader.ReadProperty <System.Int32> (); break; case "glyphHeight": characterInfo.glyphHeight = reader.ReadProperty <System.Int32> (); break; case "bearing": characterInfo.bearing = reader.ReadProperty <System.Int32> (); break; case "minY": characterInfo.minY = reader.ReadProperty <System.Int32> (); break; case "maxY": characterInfo.maxY = reader.ReadProperty <System.Int32> (); break; case "minX": characterInfo.minX = reader.ReadProperty <System.Int32> (); break; case "maxX": characterInfo.maxX = reader.ReadProperty <System.Int32> (); break; case "uvBottomLeft": characterInfo.uvBottomLeft = reader.ReadProperty <UnityEngine.Vector2> (); break; case "uvBottomRight": characterInfo.uvBottomRight = reader.ReadProperty <UnityEngine.Vector2> (); break; case "uvTopRight": characterInfo.uvTopRight = reader.ReadProperty <UnityEngine.Vector2> (); break; case "uvTopLeft": characterInfo.uvTopLeft = reader.ReadProperty <UnityEngine.Vector2> (); break; } } return(characterInfo); }
public extern bool GetCharacterInfo(char ch, out CharacterInfo info, [DefaultValue("0")] int size, [DefaultValue("FontStyle.Normal")] FontStyle style);