GetCharacterInfo() private method

private GetCharacterInfo ( char ch, CharacterInfo &info ) : bool
ch char
info CharacterInfo
return bool
Beispiel #1
0
    ///////////////////////////////////////////////////////////////////////////////
    // build mesh
    ///////////////////////////////////////////////////////////////////////////////
    // ------------------------------------------------------------------
    // Desc: This only calculate result in one line
    // ------------------------------------------------------------------
    public static void BuildTextLine( Vector3[] _vertices, Vector2[] _uvs, 
                                       string _text, 
                                       Font _font, 
                                       int _lineHeight,
                                       int _fontSize, 
                                       int _wordSpacing, 
                                       int _letterSpacing )
    {
        int cur_x = 0;
        int cur_y = 0;

        // yes, Unity's GetCharacterInfo have y problem, you should get lowest character j's y-offset adjust it.
        CharacterInfo jCharInfo;
        _font.RequestCharactersInTexture ( "j", _fontSize, FontStyle.Normal );
        _font.GetCharacterInfo('j', out jCharInfo, _fontSize, FontStyle.Normal);
        float ttf_offset = (_fontSize + jCharInfo.vert.yMax);

        //
        for ( int i = 0; i < _text.Length; ++i ) {
            char cur_char = _text[i];

            // NOTE: we skip new-line operation, since we believe this function only have one-line text
            if ( cur_char == '\n' ) {
                continue;
            }

            // generate mesh
            CharacterInfo charInfo;
            _font.GetCharacterInfo ( cur_char, out charInfo, _fontSize );
            int idx = 4*i;

            // build vertices
            _vertices[idx + 0] = new Vector3(cur_x + charInfo.vert.xMin, cur_y - charInfo.vert.yMin + ttf_offset, 0.0f);
            _vertices[idx + 1] = new Vector3(cur_x + charInfo.vert.xMax, cur_y - charInfo.vert.yMin + ttf_offset, 0.0f);
            _vertices[idx + 2] = new Vector3(cur_x + charInfo.vert.xMax, cur_y - charInfo.vert.yMax + ttf_offset, 0.0f);
            _vertices[idx + 3] = new Vector3(cur_x + charInfo.vert.xMin, cur_y - charInfo.vert.yMax + ttf_offset, 0.0f);

            // build uv
            if ( charInfo.flipped ) {
                _uvs[idx + 0] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMin);
                _uvs[idx + 1] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMax);
                _uvs[idx + 2] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMax);
                _uvs[idx + 3] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMin);
            }
            else {
                _uvs[idx + 0] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMax);
                _uvs[idx + 1] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMax);
                _uvs[idx + 2] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMin);
                _uvs[idx + 3] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMin);
            }

            // advance x
            cur_x += (int)charInfo.width + _letterSpacing;
            if ( cur_char == ' ' )
                cur_x += _wordSpacing;
        }
    }
        public static void DrawText(this Texture2D tx, string sText, int offsetX, int offsetY, Font font1, Texture2D font1Tx, int fontSize) {
            CharacterInfo ci;
            char[] cText = sText.ToCharArray();

            //Material fontMat = font1.material;
            //Texture2D fontTx = (Texture2D) fontMat.mainTexture;
            Texture2D fontTx = font1Tx;

            int x, y, w, h;
            int posX = 0;

            font1.GetCharacterInfo('I', out ci);

            //int IHeight = (int) (ci.flipped ? (float) fontTx.width * (ci.uv.width) : (float) fontTx.height * (-ci.uv.height));

            for (int i = 0; i < cText.Length; i++) {
                font1.GetCharacterInfo(cText[i], out ci);

                x = (int)((float)fontTx.width * ci.uv.x);
                y = (int)((float)fontTx.height * (ci.uv.y + ci.uv.height));
                w = (int)((float)fontTx.width * ci.uv.width);
                h = (int)((float)fontTx.height * (-ci.uv.height));

                Color[] charPix = fontTx.GetPixels(x, y, w, h);

                if (ci.flipped) {
                    charPix = Colors_FlipRight(charPix, w, h);

                    x = posX;
                    //y = (IHeight - w) + w/2;
                    y = (int)-ci.vert.y;

                    int tmp = w;
                    w = h;
                    h = tmp;
                }
                else {
                    x = posX + (int)ci.vert.x;
                    y = (int)-ci.vert.y;
                }

                Color[] basePix = tx.GetPixels(offsetX + x, offsetY + y, w, h);
                Colors_Composite(ref basePix, charPix, w, h);


                tx.SetPixels(offsetX + x, offsetY + y, w, h, basePix);

                posX += (int)ci.width;
            }
        }
Beispiel #3
0
 static public int GetCharacterInfo(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 6)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 3, out a1);
             UnityEngine.CharacterInfo a2;
             System.Int32 a3;
             checkType(l, 5, out a3);
             UnityEngine.FontStyle a4;
             checkEnum(l, 6, out a4);
             var ret = self.GetCharacterInfo(a1, out a2, a3, a4);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a2);
             return(3);
         }
         else if (argc == 5)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 3, out a1);
             UnityEngine.CharacterInfo a2;
             System.Int32 a3;
             checkType(l, 5, out a3);
             var ret = self.GetCharacterInfo(a1, out a2, a3);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a2);
             return(3);
         }
         else if (argc == 4)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 3, out a1);
             UnityEngine.CharacterInfo a2;
             var ret = self.GetCharacterInfo(a1, out a2);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a2);
             return(3);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Beispiel #4
0
 static public int GetCharacterInfo(IntPtr l)
 {
     try{
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 5)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 2, out a1);
             UnityEngine.CharacterInfo a2;
             System.Int32 a3;
             checkType(l, 4, out a3);
             UnityEngine.FontStyle a4;
             checkEnum(l, 5, out a4);
             System.Boolean ret = self.GetCharacterInfo(a1, out a2, a3, a4);
             pushValue(l, ret);
             pushValue(l, a2);
             return(2);
         }
         else if (argc == 4)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 2, out a1);
             UnityEngine.CharacterInfo a2;
             System.Int32 a3;
             checkType(l, 4, out a3);
             System.Boolean ret = self.GetCharacterInfo(a1, out a2, a3);
             pushValue(l, ret);
             pushValue(l, a2);
             return(2);
         }
         else if (argc == 3)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 2, out a1);
             UnityEngine.CharacterInfo a2;
             System.Boolean            ret = self.GetCharacterInfo(a1, out a2);
             pushValue(l, ret);
             pushValue(l, a2);
             return(2);
         }
         LuaDLL.luaL_error(l, "No matched override function to call");
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
    static int GetCharacterInfo(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 3)
            {
                UnityEngine.Font          obj  = (UnityEngine.Font)ToLua.CheckObject(L, 1, typeof(UnityEngine.Font));
                char                      arg0 = (char)LuaDLL.luaL_checknumber(L, 2);
                UnityEngine.CharacterInfo arg1;
                bool                      o = obj.GetCharacterInfo(arg0, out arg1);
                LuaDLL.lua_pushboolean(L, o);
                ToLua.PushValue(L, arg1);
                return(2);
            }
            else if (count == 4)
            {
                UnityEngine.Font          obj  = (UnityEngine.Font)ToLua.CheckObject(L, 1, typeof(UnityEngine.Font));
                char                      arg0 = (char)LuaDLL.luaL_checknumber(L, 2);
                UnityEngine.CharacterInfo arg1;
                int  arg2 = (int)LuaDLL.luaL_checknumber(L, 4);
                bool o    = obj.GetCharacterInfo(arg0, out arg1, arg2);
                LuaDLL.lua_pushboolean(L, o);
                ToLua.PushValue(L, arg1);
                return(2);
            }
            else if (count == 5)
            {
                UnityEngine.Font          obj  = (UnityEngine.Font)ToLua.CheckObject(L, 1, typeof(UnityEngine.Font));
                char                      arg0 = (char)LuaDLL.luaL_checknumber(L, 2);
                UnityEngine.CharacterInfo arg1;
                int arg2 = (int)LuaDLL.luaL_checknumber(L, 4);
                UnityEngine.FontStyle arg3 = (UnityEngine.FontStyle)ToLua.CheckObject(L, 5, typeof(UnityEngine.FontStyle));
                bool o = obj.GetCharacterInfo(arg0, out arg1, arg2, arg3);
                LuaDLL.lua_pushboolean(L, o);
                ToLua.PushValue(L, arg1);
                return(2);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Font.GetCharacterInfo"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Beispiel #6
0
 static public int GetCharacterInfo__Char__O_CharacterInfo(IntPtr l)
 {
     try {
         UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
         System.Char      a1;
         checkType(l, 2, out a1);
         UnityEngine.CharacterInfo a2;
         var ret = self.GetCharacterInfo(a1, out a2);
         pushValue(l, true);
         pushValue(l, ret);
         pushValue(l, a2);
         return(3);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Beispiel #7
0
 static public int GetCharacterInfo__Char__O_CharacterInfo__Int32__FontStyle(IntPtr l)
 {
     try {
         UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
         System.Char      a1;
         checkType(l, 2, out a1);
         UnityEngine.CharacterInfo a2;
         System.Int32 a3;
         checkType(l, 4, out a3);
         UnityEngine.FontStyle a4;
         checkEnum(l, 5, out a4);
         var ret = self.GetCharacterInfo(a1, out a2, a3, a4);
         pushValue(l, true);
         pushValue(l, ret);
         pushValue(l, a2);
         return(3);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
    public TexturedGUILabel(Rect r1, string s, Font f)
    {
        if (f.material.mainTexture) { //make sure the font has a texture

            //Uncomment this to draw a Box the same size as the input rect, can help with visualising where the text is drawn
            //GUI.Box(r1,"");

            float w = 0; //spacing between draws
            float v = 0; //spacing between lines, but only if there is some way to determine the Fonts line spacing =(
            for (int i = 0; i < s.Length; i++) {
                char C = s[i]; //the char in the input string
                CharacterInfo c;
                if (f.GetCharacterInfo(C, out c)) { //Test if the chracter is in the font, and set c to its CharacterInfo
                    if (c.index == 32) {
                        w += c.vert.width;
                        w += ( c.width - c.vert.width );
                        continue;
                    }
                    if (c.flipped) { //If the character is flipped (rotated) need to rotate the gui matrix to draw it upright
                        Matrix4x4 MB = GUI.matrix;
                        GUIUtility.RotateAroundPivot(90f, new Vector2(r1.x + w + c.vert.x, r1.y - c.vert.y));
                        GUI.DrawTextureWithTexCoords(new Rect(r1.x + w + c.vert.x, v + r1.y - c.vert.y, -c.vert.height, -c.vert.width), f.material.mainTexture, new Rect(c.uv.x + c.uv.width, c.uv.y + c.uv.height, -c.uv.width, -c.uv.height));
                        GUI.matrix = MB;
                    } else { //Draw the font bitmap, using the UVs taken from the CharacterInfo
                        GUI.DrawTextureWithTexCoords(new Rect(r1.x + w + c.vert.x, v + r1.y - c.vert.y, c.vert.width, -c.vert.height), f.material.mainTexture, c.uv);
                    }
                    w += c.vert.width;
                    w += ( c.width - c.vert.width );
                } else {
                    if (C == 10) {
                        //These 2 lines would continue the label drawing on a new line, but theres no way to determine line spacing
                        //w = 0;
                        //v += p.floatValue;
                    }
                }
            }
        }
    }
Beispiel #9
0
        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
        }
 public static int CalculateOffsetToFit(Font font, string text)
 {
     if (((font == null) || string.IsNullOrEmpty(text)) || (current.lineWidth < 1))
     {
         return 0;
     }
     int finalSize = current.finalSize;
     font.RequestCharactersInTexture(text, finalSize, current.style);
     float finalLineWidth = current.finalLineWidth;
     int length = text.Length;
     while ((length > 0) && (finalLineWidth > 0f))
     {
         char ch = text[--length];
         if (font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style))
         {
             finalLineWidth -= mTempChar.width;
         }
     }
     if (finalLineWidth < 0f)
     {
         length++;
     }
     return length;
 }
Beispiel #11
0
	/// <summary>
	/// Get the printed size of the specified string. The returned value is in pixels.
	/// </summary>

	static public Vector2 CalculatePrintedSize (string text, Font font, int size, FontStyle style, bool encoding)
	{
		Vector2 v = Vector2.zero;

		if (font != null && !string.IsNullOrEmpty(text))
		{
			// When calculating printed size, get rid of all symbols first since they are invisible anyway
			if (encoding) text = NGUIText.StripSymbols(text);

			// Ensure we have characters to work with
			font.RequestCharactersInTexture(text, size, style);

			float x = 0f;
			float y = 0;
			float fs = size;
			float maxX = 0f;

			for (int i = 0, imax = text.Length; i < imax; ++i)
			{
				char c = text[i];

				// Start a new line
				if (c == '\n')
				{
					if (x > maxX) maxX = x;
					x = 0f;
					y += fs;
					continue;
				}

				// Skip invalid characters
				if (c < ' ') continue;

				if (font.GetCharacterInfo(c, out mTempChar, size, style))
					x += mTempChar.width;

				// Convert from pixel coordinates to local coordinates
				v.x = ((x > maxX) ? x : maxX);
				v.y = (y + fs);
			}
		}
		return v;
	}
Beispiel #12
0
	/// <summary>
	/// Text wrapping functionality. The 'width' and 'height' should be in pixels.
	/// </summary>

	static public bool WrapText (string text, Font font, int size, FontStyle style, int width,
		int height, int maxLines, bool encoding, out string finalText)
	{
		if (width < 1 || height < 1)
		{
			finalText = "";
			return false;
		}

		int maxLineCount = (maxLines > 0) ? maxLines : 999999;
		maxLineCount = Mathf.Min(maxLineCount, height / size);

		if (maxLineCount == 0)
		{
			finalText = "";
			return false;
		}

		// Ensure that we have the required characters to work with
		if (font != null) font.RequestCharactersInTexture(text, size, style);

		StringBuilder sb = new StringBuilder();
		int textLength = text.Length;
		int remainingWidth = width;
		int previousChar = 0;
		int start = 0;
		int offset = 0;
		bool lineIsEmpty = true;
		bool multiline = (maxLines != 1);
		int lineCount = 1;

		// Run through all characters
		for (; offset < textLength; ++offset)
		{
			char ch = text[offset];

			// New line character -- start a new line
			if (ch == '\n')
			{
				if (!multiline || lineCount == maxLineCount) break;
				remainingWidth = width;

				// Add the previous word to the final string
				if (start < offset) sb.Append(text.Substring(start, offset - start + 1));
				else sb.Append(ch);

				lineIsEmpty = true;
				++lineCount;
				start = offset + 1;
				previousChar = 0;
				continue;
			}

			// If this marks the end of a word, add it to the final string.
			if (ch == ' ' && previousChar != ' ' && start < offset)
			{
				sb.Append(text.Substring(start, offset - start + 1));
				lineIsEmpty = false;
				start = offset + 1;
				previousChar = ch;
			}

			// When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
			if (ParseSymbol(text, ref offset)) { --offset; continue; }

			int glyphWidth = 0;
			if (font.GetCharacterInfo(ch, out mTempChar, size, style))
				glyphWidth = Mathf.RoundToInt(mTempChar.width);

			remainingWidth -= glyphWidth;

			// Doesn't fit?
			if (remainingWidth < 0)
			{
				// Can't start a new line
				if (lineIsEmpty || !multiline || lineCount == maxLineCount)
				{
					// This is the first word on the line -- add it up to the character that fits
					sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

					if (!multiline || lineCount == maxLineCount)
					{
						start = offset;
						break;
					}
					EndLine(ref sb);

					// Start a brand-new line
					lineIsEmpty = true;
					++lineCount;

					if (ch == ' ')
					{
						start = offset + 1;
						remainingWidth = width;
					}
					else
					{
						start = offset;
						remainingWidth = width - glyphWidth;
					}
					previousChar = 0;
				}
				else
				{
					// Skip all spaces before the word
					while (start < textLength && text[start] == ' ') ++start;

					// Revert the position to the beginning of the word and reset the line
					lineIsEmpty = true;
					remainingWidth = width;
					offset = start - 1;
					previousChar = 0;

					if (!multiline || lineCount == maxLineCount) break;
					++lineCount;
					EndLine(ref sb);
					continue;
				}
			}
			else previousChar = ch;
		}

		if (start < offset) sb.Append(text.Substring(start, offset - start));
		finalText = sb.ToString();
		return (!multiline || offset == textLength || (maxLines > 0 && lineCount <= maxLines));
	}
 public static void Print(Font font, string text, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
 {
     if ((font != null) && !string.IsNullOrEmpty(text))
     {
         int finalSize = current.finalSize;
         int size = verts.size;
         float num3 = finalSize + current.finalSpacingY;
         float num4 = 0f;
         font.RequestCharactersInTexture("j", finalSize, current.style);
         font.GetCharacterInfo('j', out mTempChar, finalSize, current.style);
         num4 = finalSize + mTempChar.vert.yMax;
         font.RequestCharactersInTexture(text, finalSize, current.style);
         mColors.Add(Color.white);
         float num5 = 0f;
         float num6 = 0f;
         float num7 = 0f;
         float finalSpacingX = current.finalSpacingX;
         float num9 = 1f / current.pixelDensity;
         float num10 = finalSize;
         Vector3 zero = Vector3.zero;
         Vector3 vector2 = Vector3.zero;
         Vector2 vector3 = Vector2.zero;
         Vector2 vector4 = Vector2.zero;
         Color a = current.tint * current.gradientBottom;
         Color b = current.tint * current.gradientTop;
         Color32 tint = current.tint;
         int length = text.Length;
         for (int i = 0; i < length; i++)
         {
             char ch = text[i];
             if (ch == '\n')
             {
                 if (num5 > num7)
                 {
                     num7 = num5;
                 }
                 if (current.alignment != TextAlignment.Left)
                 {
                     Align(verts, size, num5 - finalSpacingX);
                     size = verts.size;
                 }
                 num5 = 0f;
                 num6 += num3;
             }
             else if (ch >= ' ')
             {
                 if (current.encoding && ParseSymbol(text, ref i, mColors, current.premultiply))
                 {
                     Color color4 = current.tint * mColors[mColors.size - 1];
                     tint = color4;
                     if (current.gradient)
                     {
                         a = current.gradientBottom * color4;
                         b = current.gradientTop * color4;
                     }
                     i--;
                 }
                 else if (font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style))
                 {
                     zero.x = num5 + mTempChar.vert.xMin;
                     zero.y = -((num6 - mTempChar.vert.yMax) + num4);
                     vector2.x = zero.x + mTempChar.vert.width;
                     vector2.y = zero.y - mTempChar.vert.height;
                     if (num9 != 1f)
                     {
                         zero = (Vector3) (zero * num9);
                         vector2 = (Vector3) (vector2 * num9);
                     }
                     vector3.x = mTempChar.uv.xMin;
                     vector3.y = mTempChar.uv.yMin;
                     vector4.x = mTempChar.uv.xMax;
                     vector4.y = mTempChar.uv.yMax;
                     num5 += mTempChar.width + finalSpacingX;
                     verts.Add(new Vector3(vector2.x, zero.y));
                     verts.Add(new Vector3(zero.x, zero.y));
                     verts.Add(new Vector3(zero.x, vector2.y));
                     verts.Add(new Vector3(vector2.x, vector2.y));
                     if (mTempChar.flipped)
                     {
                         uvs.Add(new Vector2(vector3.x, vector4.y));
                         uvs.Add(new Vector2(vector3.x, vector3.y));
                         uvs.Add(new Vector2(vector4.x, vector3.y));
                         uvs.Add(new Vector2(vector4.x, vector4.y));
                     }
                     else
                     {
                         uvs.Add(new Vector2(vector4.x, vector3.y));
                         uvs.Add(new Vector2(vector3.x, vector3.y));
                         uvs.Add(new Vector2(vector3.x, vector4.y));
                         uvs.Add(new Vector2(vector4.x, vector4.y));
                     }
                     if (current.gradient)
                     {
                         float t = num10 - (-mTempChar.vert.yMax + num4);
                         float num14 = t - mTempChar.vert.height;
                         t /= num10;
                         num14 /= num10;
                         s_c0 = Color.Lerp(a, b, t);
                         s_c1 = Color.Lerp(a, b, num14);
                         cols.Add(s_c0);
                         cols.Add(s_c0);
                         cols.Add(s_c1);
                         cols.Add(s_c1);
                     }
                     else
                     {
                         for (int j = 0; j < 4; j++)
                         {
                             cols.Add(tint);
                         }
                     }
                 }
             }
         }
         if ((current.alignment != TextAlignment.Left) && (size < verts.size))
         {
             Align(verts, size, num5 - finalSpacingX);
             size = verts.size;
         }
         mColors.Clear();
     }
 }
 static public int GetCharacterInfo(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
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 3)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 2, out a1);
             UnityEngine.CharacterInfo a2;
             var ret = self.GetCharacterInfo(a1, out a2);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a2);
             return(3);
         }
         else if (argc == 4)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 2, out a1);
             UnityEngine.CharacterInfo a2;
             System.Int32 a3;
             checkType(l, 4, out a3);
             var ret = self.GetCharacterInfo(a1, out a2, a3);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a2);
             return(3);
         }
         else if (argc == 5)
         {
             UnityEngine.Font self = (UnityEngine.Font)checkSelf(l);
             System.Char      a1;
             checkType(l, 2, out a1);
             UnityEngine.CharacterInfo a2;
             System.Int32 a3;
             checkType(l, 4, out a3);
             UnityEngine.FontStyle a4;
             a4 = (UnityEngine.FontStyle)LuaDLL.luaL_checkinteger(l, 5);
             var ret = self.GetCharacterInfo(a1, out a2, a3, a4);
             pushValue(l, true);
             pushValue(l, ret);
             pushValue(l, a2);
             return(3);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function GetCharacterInfo to call");
         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
 }
 public static bool WrapText(Font font, string text, out string finalText)
 {
     if (((current.lineWidth < 1) || (current.lineHeight < 1)) || string.IsNullOrEmpty(text))
     {
         finalText = string.Empty;
         return false;
     }
     int b = (current.maxLines <= 0) ? 0xf4240 : current.maxLines;
     int finalSize = current.finalSize;
     float num3 = (current.maxLines <= 0) ? current.finalLineHeight : Mathf.Min(current.finalLineHeight, (float) (finalSize * current.maxLines));
     float num4 = finalSize + current.finalSpacingY;
     b = Mathf.FloorToInt((num4 <= 0f) ? 0f : Mathf.Min((float) b, num3 / num4));
     if (b == 0)
     {
         finalText = string.Empty;
         return false;
     }
     if (font != null)
     {
         font.RequestCharactersInTexture(text, finalSize, current.style);
     }
     StringBuilder s = new StringBuilder();
     int length = text.Length;
     float finalLineWidth = current.finalLineWidth;
     float num7 = finalLineWidth;
     float finalSpacingX = current.finalSpacingX;
     int startIndex = 0;
     int index = 0;
     int num11 = 1;
     int num12 = 0;
     bool flag = true;
     while (index < length)
     {
         char ch = text[index];
         if (ch == '\n')
         {
             if (num11 == b)
             {
                 break;
             }
             num7 = finalLineWidth;
             if (startIndex < index)
             {
                 s.Append(text.Substring(startIndex, (index - startIndex) + 1));
             }
             else
             {
                 s.Append(ch);
             }
             flag = true;
             num11++;
             startIndex = index + 1;
             num12 = 0;
         }
         else
         {
             if (((ch == ' ') && (num12 != 0x20)) && (startIndex < index))
             {
                 s.Append(text.Substring(startIndex, (index - startIndex) + 1));
                 flag = false;
                 startIndex = index + 1;
                 num12 = ch;
             }
             if (ParseSymbol(text, ref index))
             {
                 index--;
             }
             else if (font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style))
             {
                 float num13 = finalSpacingX + mTempChar.width;
                 num7 -= num13;
                 if (num7 < 0f)
                 {
                     if (flag || (num11 == b))
                     {
                         s.Append(text.Substring(startIndex, Mathf.Max(0, index - startIndex)));
                         if (num11++ == b)
                         {
                             startIndex = index;
                             break;
                         }
                         EndLine(ref s);
                         flag = true;
                         if (ch == ' ')
                         {
                             startIndex = index + 1;
                             num7 = finalLineWidth;
                         }
                         else
                         {
                             startIndex = index;
                             num7 = finalLineWidth - num13;
                         }
                         num12 = 0;
                     }
                     else
                     {
                         while ((startIndex < length) && (text[startIndex] == ' '))
                         {
                             startIndex++;
                         }
                         flag = true;
                         num7 = finalLineWidth;
                         index = startIndex - 1;
                         num12 = 0;
                         if (num11++ == b)
                         {
                             break;
                         }
                         EndLine(ref s);
                     }
                 }
                 else
                 {
                     num12 = ch;
                 }
             }
         }
         index++;
     }
     if (startIndex < index)
     {
         s.Append(text.Substring(startIndex, index - startIndex));
     }
     finalText = s.ToString();
     return ((index == length) || (num11 <= Mathf.Min(current.maxLines, b)));
 }
 public static Vector2 CalculatePrintedSize(Font font, string text)
 {
     Vector2 zero = Vector2.zero;
     if ((font == null) || string.IsNullOrEmpty(text))
     {
         return zero;
     }
     if (current.encoding)
     {
         text = StripSymbols(text);
     }
     int finalSize = current.finalSize;
     font.RequestCharactersInTexture(text, finalSize, current.style);
     float num2 = 0f;
     float num3 = 0f;
     float num4 = 0f;
     float num5 = finalSize + current.finalSpacingY;
     float finalSpacingX = current.finalSpacingX;
     int length = text.Length;
     for (int i = 0; i < length; i++)
     {
         char ch = text[i];
         if (ch == '\n')
         {
             if (num2 > num4)
             {
                 num4 = num2;
             }
             num2 = 0f;
             num3 += num5;
         }
         else if ((ch >= ' ') && font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style))
         {
             num2 += mTempChar.width + finalSpacingX;
         }
     }
     zero.x = (num2 <= num4) ? num4 : num2;
     zero.y = num3 + finalSize;
     return (Vector2) (zero / current.pixelDensity);
 }
		//CharacterInfo(描画用の文字情報)の設定を試行
		public bool TrySetCharacterInfo( Font font )
		{
			if (IsNoFontData) return true;

			if (!font.GetCharacterInfo(charData.Char, out this.charInfo, FontSize, FontStyle))
			{
				return false;
			}
			else
			{
				this.width = WrapperUnityVersion.GetCharacterInfoWidth(ref charInfo);
				//ダッシュ
				if (CustomInfo.IsDash)
				{
					width *= CustomInfo.DashSize;
				}
				return true;
			}
		}
	/// <summary>
	/// Text wrapping functionality. The 'width' and 'height' should be in pixels.
	/// </summary>

	static public bool WrapText (Font font, string text, out string finalText)
	{
		if (current.lineWidth < 1 || current.lineHeight < 1 || string.IsNullOrEmpty(text))
		{
			finalText = "";
			return false;
		}

		int maxLineCount = (current.maxLines > 0) ? current.maxLines : 1000000;
		int size = current.finalSize;
		float height = (current.maxLines > 0) ?
			Mathf.Min(current.finalLineHeight, size * current.maxLines) :
			current.finalLineHeight;

		float sum = size + current.finalSpacingY;
		maxLineCount = Mathf.FloorToInt((sum > 0) ? Mathf.Min(maxLineCount, height / sum) : 0);

		if (maxLineCount == 0)
		{
			finalText = "";
			return false;
		}

		// Ensure that we have the required characters to work with
		if (font != null) font.RequestCharactersInTexture(text, size, current.style);

		StringBuilder sb = new StringBuilder();
		int textLength = text.Length;
		float lineWidth = current.finalLineWidth;
		float remainingWidth = lineWidth;
		float finalSpacingX = current.finalSpacingX;

		int start = 0;
		int offset = 0;
		int lineCount = 1;
		int previousChar = 0;
		bool lineIsEmpty = true;

		// Run through all characters
		for (; offset < textLength; ++offset)
		{
			char ch = text[offset];

			// New line character -- start a new line
			if (ch == '\n')
			{
				if (lineCount == maxLineCount) break;
				remainingWidth = lineWidth;

				// Add the previous word to the final string
				if (start < offset) sb.Append(text.Substring(start, offset - start + 1));
				else sb.Append(ch);

				lineIsEmpty = true;
				++lineCount;
				start = offset + 1;
				previousChar = 0;
				continue;
			}

			// If this marks the end of a word, add it to the final string.
			if (ch == ' ' && previousChar != ' ' && start < offset)
			{
				sb.Append(text.Substring(start, offset - start + 1));
				lineIsEmpty = false;
				start = offset + 1;
				previousChar = ch;
			}

			// When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them
			if (ParseSymbol(text, ref offset)) { --offset; continue; }

			// If the character is missing for any reason, skip it
			if (!font.GetCharacterInfo(ch, out mTempChar, size, current.style)) continue;

			float glyphWidth = finalSpacingX + mTempChar.width;
			remainingWidth -= glyphWidth;

			// Doesn't fit?
			if (remainingWidth < 0)
			{
				// Can't start a new line
				if (lineIsEmpty || lineCount == maxLineCount)
				{
					// This is the first word on the line -- add it up to the character that fits
					sb.Append(text.Substring(start, Mathf.Max(0, offset - start)));

					if (lineCount++ == maxLineCount)
					{
						start = offset;
						break;
					}
					EndLine(ref sb);

					// Start a brand-new line
					lineIsEmpty = true;

					if (ch == ' ')
					{
						start = offset + 1;
						remainingWidth = lineWidth;
					}
					else
					{
						start = offset;
						remainingWidth = lineWidth - glyphWidth;
					}
					previousChar = 0;
				}
				else
				{
					// Skip all spaces before the word
					while (start < textLength && text[start] == ' ') ++start;

					// Revert the position to the beginning of the word and reset the line
					lineIsEmpty = true;
					remainingWidth = lineWidth;
					offset = start - 1;
					previousChar = 0;

					if (lineCount++ == maxLineCount) break;
					EndLine(ref sb);
					continue;
				}
			}
			else previousChar = ch;
		}

		if (start < offset) sb.Append(text.Substring(start, offset - start));
		finalText = sb.ToString();
		return (offset == textLength) || (lineCount <= Mathf.Min(current.maxLines, maxLineCount));
	}
	/// <summary>
	/// Get the printed size of the specified string. The returned value is in pixels.
	/// </summary>

	static public Vector2 CalculatePrintedSize (Font font, string text)
	{
		Vector2 v = Vector2.zero;

		if (font != null && !string.IsNullOrEmpty(text))
		{
			// When calculating printed size, get rid of all symbols first since they are invisible anyway
			if (current.encoding) text = StripSymbols(text);

			// Ensure we have characters to work with
			int size = current.finalSize;
			font.RequestCharactersInTexture(text, size, current.style);

			float x = 0;
			float y = 0;
			float maxX = 0f;
			float lineHeight = size + current.finalSpacingY;
			float spacingX = current.finalSpacingX;
			int chars = text.Length;

			for (int i = 0; i < chars; ++i)
			{
				char c = text[i];

				// Start a new line
				if (c == '\n')
				{
					if (x > maxX) maxX = x;
					x = 0f;
					y += lineHeight;
					continue;
				}

				// Skip invalid characters
				if (c < ' ') continue;

				if (font.GetCharacterInfo(c, out mTempChar, size, current.style))
					x += mTempChar.width + spacingX;
			}

			// Padding is always between characters, so it's one less than the number of characters
			v.x = ((x > maxX) ? x : maxX);
			v.y = (y + size);
			v /= current.pixelDensity;
		}
		return v;
	}
	/// <summary>
	/// Calculate the character index offset required to print the end of the specified text.
	/// NOTE: This function assumes that the text has been stripped of all symbols.
	/// </summary>

	static public int CalculateOffsetToFit (Font font, string text)
	{
		if (font == null || string.IsNullOrEmpty(text) || current.lineWidth < 1) return 0;

		// Ensure we have the characters to work with
		int size = current.finalSize;
		font.RequestCharactersInTexture(text, size, current.style);

		float remainingWidth = current.finalLineWidth;
		int textLength = text.Length;
		int currentCharacterIndex = textLength;

		while (currentCharacterIndex > 0 && remainingWidth > 0f)
		{
			char c = text[--currentCharacterIndex];
			if (font.GetCharacterInfo(c, out mTempChar, size, current.style))
				remainingWidth -= mTempChar.width;
		}

		if (remainingWidth < 0f) ++currentCharacterIndex;
		return currentCharacterIndex;
	}
Beispiel #21
0
	/// <summary>
	/// Calculate the character index offset required to print the end of the specified text.
	/// NOTE: This function assumes that the text has been stripped of all symbols.
	/// </summary>

	static public int CalculateOffsetToFit (string text, Font font, int size, FontStyle style, int lineWidth)
	{
		if (font == null || string.IsNullOrEmpty(text) || lineWidth < 1) return 0;

		// Ensure we have the characters to work with
		font.RequestCharactersInTexture(text, size, style);
		
		int textLength = text.Length;
		int remainingWidth = lineWidth;
		int currentCharacterIndex = textLength;

		while (currentCharacterIndex > 0 && remainingWidth > 0)
		{
			char c = text[--currentCharacterIndex];
			if (font.GetCharacterInfo(c, out mTempChar, size, style))
				remainingWidth -= (int)mTempChar.width;
		}

		if (remainingWidth < 0) ++currentCharacterIndex;
		return currentCharacterIndex;
	}
	/// <summary>
	/// Print the specified text into the buffers.
	/// </summary>

	static public void Print (Font font, string text, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
	{
		if (font == null || string.IsNullOrEmpty(text)) return;

		int size = current.finalSize;
		int indexOffset = verts.size;
		float lineHeight = size + current.finalSpacingY;

		// We need to know the baseline first
		float baseline = 0f;
		font.RequestCharactersInTexture("j", size, current.style);
		font.GetCharacterInfo('j', out mTempChar, size, current.style);
		baseline = size + mTempChar.vert.yMax;

		// Ensure that the text we're about to print exists in the font's texture
		font.RequestCharactersInTexture(text, size, current.style);

		// Start with the white tint
		mColors.Add(Color.white);

		float x = 0f;
		float y = 0f;
		float maxX = 0f;
		float spacingX = current.finalSpacingX;
		float pixelSize = 1f / current.pixelDensity;
		float sizeF = size;
		
		Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
		Vector2 u0 = Vector2.zero, u1 = Vector2.zero;
		Color gb = current.tint * current.gradientBottom;
		Color gt = current.tint * current.gradientTop;
		Color32 uc = current.tint;
		int textLength = text.Length;

		for (int i = 0; i < textLength; ++i)
		{
			char c = text[i];

			if (c == '\n')
			{
				if (x > maxX) maxX = x;

				if (current.alignment != TextAlignment.Left)
				{
					Align(verts, indexOffset, x - spacingX);
					indexOffset = verts.size;
				}

				x = 0;
				y += lineHeight;
				continue;
			}

			if (c < ' ') continue;

			// Color changing symbol
			if (current.encoding && ParseSymbol(text, ref i, mColors, current.premultiply))
			{
				Color fc = current.tint * mColors[mColors.size - 1];
				uc = fc;

				if (current.gradient)
				{
					gb = current.gradientBottom * fc;
					gt = current.gradientTop * fc;
				}
				--i;
				continue;
			}

			if (!font.GetCharacterInfo(c, out mTempChar, size, current.style))
				continue;

			v0.x =  (x + mTempChar.vert.xMin);
			v0.y = -(y - mTempChar.vert.yMax + baseline);
			
			v1.x = v0.x + mTempChar.vert.width;
			v1.y = v0.y - mTempChar.vert.height;

			if (pixelSize != 1f)
			{
				v0 *= pixelSize;
				v1 *= pixelSize;
			}

			u0.x = mTempChar.uv.xMin;
			u0.y = mTempChar.uv.yMin;
			u1.x = mTempChar.uv.xMax;
			u1.y = mTempChar.uv.yMax;

			x += (mTempChar.width + spacingX);

			verts.Add(new Vector3(v1.x, v0.y));
			verts.Add(new Vector3(v0.x, v0.y));
			verts.Add(new Vector3(v0.x, v1.y));
			verts.Add(new Vector3(v1.x, v1.y));

			if (mTempChar.flipped)
			{
				uvs.Add(new Vector2(u0.x, u1.y));
				uvs.Add(new Vector2(u0.x, u0.y));
				uvs.Add(new Vector2(u1.x, u0.y));
				uvs.Add(new Vector2(u1.x, u1.y));
			}
			else
			{
				uvs.Add(new Vector2(u1.x, u0.y));
				uvs.Add(new Vector2(u0.x, u0.y));
				uvs.Add(new Vector2(u0.x, u1.y));
				uvs.Add(new Vector2(u1.x, u1.y));
			}

			if (current.gradient)
			{
				float min = sizeF - (-mTempChar.vert.yMax + baseline);
				float max = min - (mTempChar.vert.height);

				min /= sizeF;
				max /= sizeF;

				s_c0 = Color.Lerp(gb, gt, min);
				s_c1 = Color.Lerp(gb, gt, max);

				cols.Add(s_c0);
				cols.Add(s_c0);
				cols.Add(s_c1);
				cols.Add(s_c1);
			}
			else for (int b = 0; b < 4; ++b) cols.Add(uc);
		}

		if (current.alignment != TextAlignment.Left && indexOffset < verts.size)
		{
			Align(verts, indexOffset, x - spacingX);
			indexOffset = verts.size;
		}
		mColors.Clear();
	}
Beispiel #23
0
	/// <summary>
	/// Print the specified text into the buffers.
	/// </summary>

	static public void Print (string text, Font font, int size, FontStyle style, Color32 color,
		bool encoding, TextAlignment alignment, int lineWidth, bool premultiply,
		BetterList<Vector3> verts,
		BetterList<Vector2> uvs,
		BetterList<Color32> cols)
	{
		if (font == null || string.IsNullOrEmpty(text)) return;

		float baseline = 0f;

		// We need to know the baseline first
		font.RequestCharactersInTexture("j", size, style);
		font.GetCharacterInfo('j', out mTempChar, size, style);
		baseline = size + mTempChar.vert.yMax;

		// Ensure that the text we're about to print exists in the font's texture
		font.RequestCharactersInTexture(text, size, style);

		// Start with the specified color
		mColors.Add(color);

		int indexOffset = verts.size;
		int maxX = 0;
		int x = 0;
		int y = 0;
		int lineHeight = size;
		Vector3 v0 = Vector3.zero, v1 = Vector3.zero;
		Vector2 u0 = Vector2.zero, u1 = Vector2.zero;

		int textLength = text.Length;

		for (int i = 0; i < textLength; ++i)
		{
			char c = text[i];

			if (c == '\n')
			{
				if (x > maxX) maxX = x;

				if (alignment != TextAlignment.Left)
				{
					Align(verts, indexOffset, alignment, x, lineWidth);
					indexOffset = verts.size;
				}

				x = 0;
				y += lineHeight;
				continue;
			}

			if (c < ' ') continue;

			if (encoding && ParseSymbol(text, ref i, mColors, premultiply))
			{
				color = mColors[mColors.size - 1];
				--i;
				continue;
			}

			if (!font.GetCharacterInfo(c, out mTempChar, size, style))
				continue;

			v0.x =  (x + mTempChar.vert.xMin);
			v0.y = -(y - mTempChar.vert.yMax + baseline);

			v1.x = v0.x + mTempChar.vert.width;
			v1.y = v0.y - mTempChar.vert.height;

			u0.x = mTempChar.uv.xMin;
			u0.y = mTempChar.uv.yMin;
			u1.x = mTempChar.uv.xMax;
			u1.y = mTempChar.uv.yMax;

			x += (int)mTempChar.width;

			for (int b = 0; b < 4; ++b) cols.Add(color);

			if (mTempChar.flipped)
			{
				uvs.Add(new Vector2(u0.x, u1.y));
				uvs.Add(new Vector2(u0.x, u0.y));
				uvs.Add(new Vector2(u1.x, u0.y));
				uvs.Add(new Vector2(u1.x, u1.y));
			}
			else
			{
				uvs.Add(new Vector2(u1.x, u0.y));
				uvs.Add(new Vector2(u0.x, u0.y));
				uvs.Add(new Vector2(u0.x, u1.y));
				uvs.Add(new Vector2(u1.x, u1.y));
			}

			verts.Add(new Vector3(v1.x, v0.y));
			verts.Add(new Vector3(v0.x, v0.y));
			verts.Add(new Vector3(v0.x, v1.y));
			verts.Add(new Vector3(v1.x, v1.y));
		}

		if (alignment != TextAlignment.Left && indexOffset < verts.size)
		{
			Align(verts, indexOffset, alignment, x, lineWidth);
			indexOffset = verts.size;
		}
		mColors.Clear();
	}