Esempio n. 1
0
 public DrawingContext(Component parent)
 {
     vertex        = new FillHelper();
     parser        = new TexFormulaParser();
     monoContainer = parent;
     hasInit       = true;
 }
Esempio n. 2
0
 public bool Parse(string input)
 {
     if (!hasInit)
     {
         vertex = new FillHelper();
         parser = new TexFormulaParser();
     }
     try
     {
         parsingComplete = false;
         string[] strings = input.Split(newLineChar, StringSplitOptions.RemoveEmptyEntries);
         if (parsed.Count > 0)
         {
             for (int i = 0; i < parsed.Count; i++)
             {
                 parsed[i].Flush();
             }
         }
         parsed.Clear();
         for (int i = 0; i < strings.Length; i++)
         {
             parsed.Add(parser.Parse(strings[i]));
         }
         parsingComplete = true;
     }
     catch
     {
         return(false);
     }
     return(true);
 }
        protected void ApplyShadowZeroAlloc(FillHelper verts, Color32 color, int start, int end, float z, bool justReplace)
        {
            UIVertex uIVertex = new UIVertex();

            for (int i = start; i < end; i++)
            {
                verts.PopulateUIVertex(ref uIVertex, i);
                Vector3 position = uIVertex.position;
                position.z        = z;
                uIVertex.position = position;
                uIVertex.color    = Color.Lerp(uIVertex.color, color, m_BodyAlpha);
                if (justReplace)
                {
                    verts.SetUIVertex(uIVertex, i);
                }
                else
                {
                    verts.AddVert(uIVertex);
                    if (i % 4 == 0)
                    {
                        var t = verts.currentVertCount - 1;
                        verts.AddTriangle(t + 0, t + 1, t + 3);
                        verts.AddTriangle(t + 3, t + 1, t + 2);
                    }
                }
            }
        }
Esempio n. 4
0
        private void FixTMP(FillHelper verts)
        {
            var scale = context.monoContainer.transform.lossyScale.y * factor;

            for (int i = 0; i < verts.vertexcount; i++)
            {
                verts.m_Uv1S[i] = new Vector2(verts.m_Uv1S[i].x, scale);
            }
        }
Esempio n. 5
0
        public bool Parse(string input, out string errResult, int renderFont = -1)
        {
                #if TEXDRAW_PROFILE
            Profiler.BeginSample("Parsing");
                        #endif

            if (!hasInit)
            {
                vertex = new FillHelper();
                parser = new TexFormulaParser();
            }
            try
            {
                TexUtility.RenderFont    = -2;
                TexUtility.RawRenderFont = renderFont;
                parsingComplete          = false;
                string[] strings = input.Split(newLineChar, StringSplitOptions.None);
                if (parsed.Count > 0)
                {
                    for (int i = 0; i < parsed.Count; i++)
                    {
                        parsed[i].Flush();
                    }
                }
                parsed.Clear();
                for (int i = 0; i < strings.Length; i++)
                {
                    parsed.Add(parser.Parse(strings[i]));
                }
                parsingComplete = true;
            }
            catch (Exception ex)
            {
                errResult = ex.Message;
                                #if TEXDRAW_PROFILE
                Profiler.EndSample();
                                #endif
                // throw ex;
                return(false);
            }
            errResult = String.Empty;
                        #if TEXDRAW_PROFILE
            Profiler.EndSample();
                        #endif
            return(true);
        }
Esempio n. 6
0
        private void RenderUV3Line(FillHelper verts, Rect uv, int idx)
        {
            // After render... uhh it's done... except if there's autoFill per line in there... which need to be done right now ...
            switch (autoFill)
            {
            case Filling.PerLine:
                for (int j = idx; j < verts.vertexcount; j++)
                {
                    verts.SetUV2(InverseLerp(uv, verts.m_Positions[j]), j);
                }
                break;

                /*case 5:
                 *  //Toughest filling method: Per word - Not yet available
                 *  var boxes = box.Box.Children;
                 *  r = new Rect(offset, box.RenderSize);
                 *  for (int j = lastVerts; j < verts.currentVertCount; j++) {
                 *      verts.m_Uv2S[j] = InverseLerp(r, verts.m_Positions[j]);
                 *  }
                 *  break;*/
            }
        }
Esempio n. 7
0
        public void Render()
        {
            if (hasRect && (rectArea.size.x <= 0 || rectArea.size.y <= 0))
            {
                return;
            }

            // At first, calculate starting offset
            CalculateRenderedArea(size, alignment, rectArea);

            // We'll start from top (line-per-line). This means offset will subtracted decrementally..
            float scaleFactor = scale * factor, paragraph = 0, x, y;

            offset.y += size.y;
            FillHelper verts = context.vertex;

            for (int i = 0; i < formulas.Count; i++)
            {
                var   box = formulas[i];
                int   lastVerts = verts.vertexcount;
                float alignX = alignment.x, sizeX = size.x;

                if (box.usingMetaRules)
                {
                    var r    = rectArea;
                    var meta = box.metaRules;
                    sizeX    = box.Width * scaleFactor;
                    alignX   = meta.GetAlignment(alignment.x);
                    r.width -= box.PenaltyWidth * scaleFactor;
                    // We only need to adjust horizontal offset
                    offset.x = CalculateXMetric(sizeX, alignX, r.width
                                                , r.x + r.width / 2, meta.left, meta.right, box.partOfPreviousLine == 0 ? meta.leading : 0);
                }

                if (box.Box.totalHeight == 0)
                {
                    continue;                           // empty meta block
                }
                // Get ready to render

                if (box.partOfPreviousLine == 0)
                {
                    offset.y -= paragraph * scaleFactor;
                }
                x = offset.x + alignX * (sizeX - scaleFactor * box.Width);
                y = offset.y -= box.Height * scaleFactor;
                box.Render(context, x, y, scaleFactor);
                offset.y -= (box.Depth + box.PenaltySpacing) * scaleFactor;
                if (box.partOfPreviousLine == 0)
                {
                    paragraph = box.PenaltyParagraph;
                }

                if (autoFill == Filling.PerLine)
                {
                    RenderUV3Line(verts, new Rect(new Vector2(x, offset.y),
                                                  new Vector2(box.Width, box.Depth + box.Height) * scaleFactor), lastVerts);
                }
            }
            if (autoFill > 0)
            {
                RenderUV3(verts);
            }
#if TEXDRAW_TMP
            FixTMP(verts);
#endif
        }
Esempio n. 8
0
        private void RenderUV3(FillHelper verts)
        {
            var count = verts.vertexcount;

            switch (autoFill)
            {
            case Filling.Rectangle:
                Rect r;
                if (hasRect)
                {
                    r = rectArea;
                }
                else
                {
                    r = new Rect(-VecScale(size, alignment), size);
                }
                for (int i = 0; i < count; i++)
                {
                    verts.SetUV2(InverseLerp(r, verts.m_Positions[i]), i);
                }
                break;

            case Filling.WholeText:
                if (hasRect)
                {
                    r = new Rect(-(
                                     VecScale(size, (alignment)) +
                                     VecScale(rectArea.size, VecNormal(alignment)) +
                                     -(rectArea.center)), size);
                }
                else
                {
                    r = new Rect(-VecScale(size, alignment), size);
                }
                for (int i = 0; i < count; i++)
                {
                    verts.SetUV2(InverseLerp(r, verts.m_Positions[i]), i);
                }
                break;

            case Filling.WholeTextSquared:
                if (hasRect)
                {
                    r = new Rect(-(
                                     VecScale(size, (alignment)) +
                                     VecScale(rectArea.size, VecNormal(alignment)) +
                                     -(rectArea.center)), size);
                }
                else
                {
                    r = new Rect(-VecScale(size, alignment), size);
                }

                var max    = Mathf.Max(r.width, r.height);
                var center = r.center;
                r.size   = Vector2.one * max;
                r.center = center;
                for (int i = 0; i < count; i++)
                {
                    verts.SetUV2(InverseLerp(r, verts.m_Positions[i]), i);
                }
                break;

            case Filling.PerCharacter:
                for (int i = 0; i < count; i++)
                {
                    int l = i % 4;
                    verts.SetUV2(new Vector2(l == 0 | l == 3 ? 0 : 1, l < 2 ? 0 : 1), i);
                }
                break;

            case Filling.PerCharacterSquared:
                for (int i = 0; i < count; i += 4)
                {
                    Vector2 sz = verts.m_Positions[i + 2] - verts.m_Positions[i];
                    if (sz.x <= 0 || sz.y <= 0)
                    {
                        for (int l = 0; l < 4; l++)
                        {
                            verts.SetUV2(new Vector2(l == 0 | l == 3 ? 0 : 1, l < 2 ? 0 : 1), i);
                        }
                        continue;
                    }
                    float xMin, xMax, yMin, yMax;
                    if (sz.x > sz.y)
                    {
                        var h = sz.y / sz.x;
                        xMin = 0;
                        xMax = 1;
                        yMin = (1 - h) / 2;
                        yMax = 1 - yMin;
                    }
                    else
                    {
                        var v = sz.x / sz.y;
                        yMin = 0;
                        yMax = 1;
                        xMin = (1 - v) / 2;
                        xMax = 1 - xMin;
                    }
                    for (int l = 0; l < 4; l++)
                    {
                        verts.SetUV2(new Vector2(l == 0 | l == 3 ? xMin : xMax, l < 2 ? yMin : yMax), i + l);
                    }
                }
                break;

            case Filling.LocalContinous:
                var ratio = 1 / (factor * scale);
                for (int i = 0; i < count; i++)
                {
                    verts.SetUV2(verts.m_Positions[i] * ratio, i);
                }
                break;

            case Filling.WorldContinous:
                ratio = 1 / (factor * scale);
                var transform = context.monoContainer.transform;
                for (int i = 0; i < count; i++)
                {
                    verts.SetUV2(transform.TransformPoint(verts.m_Positions[i]) * ratio, i);
                }
                break;
            }
        }
Esempio n. 9
0
        public void Render()
        {
            if (hasRect && autoFit > 0 && (rectArea.size.x <= 0 || rectArea.size.y <= 0))
            {
                return;
            }
            // At first, calculate starting offset
            CalculateRenderedArea(size, alignment, rectArea);
            var lastIsCustomAlign = false;
            //Layout bug fix.
            float shift;
            float lastOffsetX = offset.x;
            float postYOffset = 0f, postYParagraph = 0f; //

            // We'll start from top (line-per-line). This means offset will subtracted decrementally..
            offset.y += size.y;
            TexRenderer box;
            var         scaleFactor = scale * factor;
            FillHelper  verts       = context.vertex;

            for (int i = 0; i < formulas.Count; i++)
            {
                int lastVerts = verts.currentVertCount;

                box = formulas[i];
                // Recalculate offsets
                var alignX = alignment.x;
                var sizeX  = size.x;
                offset.y -= (box.Box.totalHeight +
                             (i > 0 && formulas[i - 1].Box.totalHeight > 0 ? spaceSize + postYOffset : 0) +
                             (box.partOfPreviousLine == 0 ? postYParagraph : 0)) * scaleFactor;
                box.Scale *= factor;
                shift      = (box.Box.height - box.Box.depth) * box.Scale;

                // Recalculate again if something defined in box's meta
                if (box.usingMetaRules)
                {
                    float lastY = offset.y;
                    var   meta  = box.metaRules;
                    alignX = meta.GetAlignment(alignment.x);
                    sizeX  = (box.Box.width) * scaleFactor;
                    var rectModify = rectArea;
                    rectModify.width -= (meta.right + meta.left + box.partOfPreviousLine > 0 ? 0 : meta.leading) * scaleFactor;

                    // We only need to adjust horizontal offset
                    CalculateRenderedArea(new Vector2(sizeX, 0), new Vector2(alignX, alignment.y), rectModify);
                    offset.x += ((1 - alignX) * meta.left
                                 - (alignX) * meta.right
                                 + (box.partOfPreviousLine > 0 ? 0 : (-(alignX * 2 - 1)) * meta.leading)
                                 ) * scaleFactor;
                    sizeX            -= rectArea.xMax - rectModify.xMax;
                    offset.y          = lastY;          // Don't want thing messed.
                    postYOffset       = (meta.spacing); // Additional spacing
                    postYParagraph    = box.Box.totalHeight > 0 ? meta.paragraph : 0;
                    lastIsCustomAlign = true;
                }
                else if (lastIsCustomAlign)
                {
                    // This is just for restoring things from beginning (if necessary)
                    postYOffset       = 0;
                    postYParagraph    = 0;
                    offset.x          = lastOffsetX;
                    lastIsCustomAlign = false;
                }


                // Get ready to render
                float x = offset.x + /*box.Box.shift * box.Scale +*/ alignX * (sizeX - box.Scale * box.Box.width);
                float y = offset.y - shift;
                box.Render(context, x, y);

                // After render... uhh it's done... except if there's autoFill per line in there... which need to be done right now ...
                switch (autoFill)
                {
                case Filling.PerLine:
                    Rect r = new Rect(new Vector2(x, offset.y), box.RenderSize);
                    for (int j = lastVerts; j < verts.currentVertCount; j++)
                    {
                        verts.SetUV2(inverseLerp(r, verts.m_Positions[j]), j);
                    }
                    break;

                    /*case 5:
                     *  //Toughest filling method: Per word - Not yet available
                     *  var boxes = box.Box.Children;
                     *  r = new Rect(offset, box.RenderSize);
                     *  for (int j = lastVerts; j < verts.currentVertCount; j++) {
                     *      verts.m_Uv2S[j] = inverseLerp(r, verts.m_Positions[j]);
                     *  }
                     *  break;*/
                }
            }
            if (autoFill > 0)
            {
                RenderUV3(verts);
            }
        }