Example #1
0
        public static Size CalcSize(this StyleRuleSet ruleSet, string text, GUIState state)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }
            if (text == "")
            {
                return(Size.Zero);
            }

            var width  = -1d;
            var height = -1d;

            // apply font and text styles
            {
                var measureContext = TextMeshUtil.GetTextContext(text, new Size(4096, 4096), ruleSet, state);
                var actualSize     = measureContext.Measure();
                width  = actualSize.Width;
                height = actualSize.Height;
            }
            if (width < 0)
            {
                width = 0;
            }
            if (height < 0)
            {
                height = 0;
            }

            var size = new Size(Math.Ceiling(width), Math.Ceiling(height));

            return(size);
        }
Example #2
0
        public static Size CalcContentBoxSize(this StyleRuleSet ruleSet, string text, GUIState state)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            //Note text height shouldn't be set to zero but to the line height,
            //since it determines the height of inline controls.

            // apply font and text styles
            var measureContext = TextMeshUtil.GetTextContext(text, new Size(4096, 4096), ruleSet, state);
            var actualSize = measureContext.Measure();
            double width = actualSize.Width;
            double height = actualSize.Height;
            if (width < 0)
            {
                width = 0;
            }
            if (height < 0)
            {
                height = 0;
            }

            var size = new Size(Math.Ceiling(width), Math.Ceiling(height));
            return size;
        }
Example #3
0
        /// <summary>
        /// Append a text mesh to this drawlist
        /// </summary>
        public void AddText(Rect rect, string text, GUIState state)
        {
            //AddTextDrawCommand();

            var textMesh            = this.TextMesh;
            var oldIndexBufferCount = textMesh.IndexBuffer.Count;

            string fontFamily = "";
            double fontSize   = 18;
            Color  fontColor  = Color.Red;

            // get offset and scale from text layout
            var scale        = OSImplentation.TypographyTextContext.GetScale(fontFamily, fontSize);
            var textContext  = TextMeshUtil.GetTextContext(text, rect.Size, style, state) as OSImplentation.TypographyTextContext;
            var glyphOffsets = textContext.GlyphOffsets;

            int index = -1;

            // get glyph data from typeface
            FontStyle  fontStyle  = style.FontStyle;
            FontWeight fontWeight = style.FontWeight;

            foreach (var character in text)
            {
                index++;
                if (char.IsWhiteSpace(character))
                {
                    continue;
                }
                var glyphData = GlyphCache.Default.GetGlyph(character, fontFamily, fontStyle, fontWeight);
                if (glyphData == null)
                {
                    Typography.OpenFont.Glyph glyph = OSImplentation.TypographyTextContext.LookUpGlyph(fontFamily, character);
                    var polygons       = new List <List <Point> >();
                    var bezierSegments = new List <(Point, Point, Point)>();
                    Typography.OpenFont.GlyphLoader.Read(glyph, out polygons, out bezierSegments);
                    GlyphCache.Default.AddGlyph(character, fontFamily, fontStyle, fontWeight, polygons, bezierSegments);
                    glyphData = GlyphCache.Default.GetGlyph(character, fontFamily, fontStyle, fontWeight);
                    Debug.Assert(glyphData != null);
                }

                // append to drawlist
                Vector glyphOffset    = glyphOffsets[index];
                var    positionOffset = (Vector)rect.TopLeft;
                this.TextMesh.Append(positionOffset, glyphData, glyphOffset, scale, fontColor, false);
            }

            var newIndexBufferCount = textMesh.IndexBuffer.Count;

            // Update command
            var command = textMesh.Commands[textMesh.Commands.Count - 1];

            command.ElemCount += newIndexBufferCount - oldIndexBufferCount;
            textMesh.Commands[textMesh.Commands.Count - 1] = command;

            // TODO refactor this
        }
Example #4
0
 private void Start()
 {
     textMesh.text  = commendData.Comment;
     textMesh.color = commendData.CommentColor;
     endPos         = new Vector3(-TextMeshUtil.GetWidth(textMesh) * transform.localScale.x - 9, startPos.y, 0);
     if (commendData.IsBold)
     {
         textMesh.fontStyle = FontStyle.Bold;
     }
 }
Example #5
0
        public void ShowATextTrailingAligned()
        {
            Rect     rect  = new Rect(400, 300);
            string   text  = "New Text";
            GUIStyle style = "Label";

            style.Set <int>(GUIStyleName.TextAlignment, (int)TextAlignment.Trailing);
            TextMeshUtil.GetTextMesh(text, rect, style, GUIState.Normal);
            DrawContent(rect, text, style);
        }
Example #6
0
        public void ShowATextWidthFixedHeightAutoSized()
        {
            string   text  = "New Text";
            GUIStyle style = "Label";

            style.Set <int>(GUIStyleName.TextAlignment, (int)TextAlignment.Leading);
            Size size = style.CalcSize(text, GUIState.Normal, new[] { GUILayout.Height(100) });
            Rect rect = new Rect(size);

            TextMeshUtil.GetTextMesh(text, rect, style, GUIState.Normal);
            DrawContent(rect, text, style);
        }
Example #7
0
        public void ShowATextAutoSized()
        {
            string text = "New Text";

            GUIStyle style = new GUIStyle();

            style.Set <double>(GUIStyleName.BorderTop, 10);
            style.Set <double>(GUIStyleName.BorderRight, 10);
            style.Set <double>(GUIStyleName.BorderBottom, 10);
            style.Set <double>(GUIStyleName.BorderLeft, 10);

            style.Set <double>(GUIStyleName.PaddingTop, 10);
            style.Set <double>(GUIStyleName.PaddingRight, 10);
            style.Set <double>(GUIStyleName.PaddingBottom, 10);
            style.Set <double>(GUIStyleName.PaddingLeft, 10);
            Size size = style.CalcSize(text, GUIState.Normal, new[] { GUILayout.Height(100) });
            Rect rect = new Rect(size);

            TextMeshUtil.GetTextMesh(text, rect, style, GUIState.Normal);
            DrawContent(rect, text, style);
        }
        public static void CreateTextMesh(
            TextMesh textMesh,
            PFrame.Tiny.Font font,
            Mesh mesh
            )
        {
            var Idx    = 0;
            var TriIdx = 0;

            var textLayoutData = TextMeshUtil.GetTextLayoutData(textMesh, font);
            var charNum        = textLayoutData.CharNum;
            //var text = textMesh.Text;
            //var charNum = text.LengthInBytes;
            int triCount  = 2 * 3 * charNum;
            int vertCount = 4 * charNum;

            var Tris   = new NativeArray <int>(triCount, Allocator.TempJob);
            var Verts  = new NativeArray <float3>(vertCount, Allocator.TempJob);
            var UVs    = new NativeArray <float2>(vertCount, Allocator.TempJob);
            var Colors = new NativeArray <Unity.Tiny.Color>(vertCount, Allocator.TempJob);

            TextMeshUtil.CreateTextMesh(textMesh, font, textLayoutData, (charInfo, pos, scale) =>
            {
                var posX = pos.x;
                var posY = pos.y;
                var posZ = pos.z;

                float minx = posX + charInfo.minX * scale;
                float maxx = posX + charInfo.maxX * scale;
                float miny = posY + charInfo.minY * scale;
                float maxy = posY + charInfo.maxY * scale;

                Verts[Idx + 0] = new float3(minx, miny, posZ);
                Verts[Idx + 1] = new float3(minx, maxy, posZ);
                Verts[Idx + 2] = new float3(maxx, maxy, posZ);
                Verts[Idx + 3] = new float3(maxx, miny, posZ);
                //float x = posX + charInfo.bearing * scale;
                //float y = posY + charInfo.minY * scale;
                //var width = charInfo.glyphWidth * scale;
                //var height = charInfo.glyphHeight * scale;

                //Verts[Idx + 0] = new float3(x, y, posZ);
                //Verts[Idx + 1] = new float3(x, y + height, posZ);
                //Verts[Idx + 2] = new float3(x + width, y + height, posZ);
                //Verts[Idx + 3] = new float3(x + width, y, posZ);

                UVs[Idx + 0] = charInfo.uvBottomLeft;
                UVs[Idx + 1] = charInfo.uvTopLeft;
                UVs[Idx + 2] = charInfo.uvTopRight;
                UVs[Idx + 3] = charInfo.uvBottomRight;

                Tris[TriIdx + 0] = Idx + 0;
                Tris[TriIdx + 1] = Idx + 1;
                Tris[TriIdx + 2] = Idx + 2;
                Tris[TriIdx + 3] = Idx + 0;
                Tris[TriIdx + 4] = Idx + 2;
                Tris[TriIdx + 5] = Idx + 3;

                Idx    += 4;
                TriIdx += 6;
            });

            mesh.SetVertices(Verts);
            mesh.SetUVs(0, UVs);
            mesh.SetIndices(Tris, 0, Tris.Length, MeshTopology.Triangles, 0, true, 0);
            mesh.SetColors(Colors);

            Verts.Dispose();
            Tris.Dispose();
            UVs.Dispose();
            Colors.Dispose();

            textLayoutData.Dispose();
        }