public RenderParagraph(TextSpan text,
                               TextAlign textAlign         = TextAlign.left,
                               TextDirection textDirection = TextDirection.ltr,
                               bool softWrap             = true,
                               TextOverflow overflow     = TextOverflow.clip,
                               float textScaleFactor     = 1.0f,
                               int?maxLines              = null,
                               Action onSelectionChanged = null,
                               Color selectionColor      = null
                               )
        {
            D.assert(maxLines == null || maxLines > 0);
            this._softWrap    = softWrap;
            this._overflow    = overflow;
            this._textPainter = new TextPainter(
                text,
                textAlign,
                textDirection,
                textScaleFactor,
                maxLines,
                overflow == TextOverflow.ellipsis ? _kEllipsis : ""
                );

            this._selection         = null;
            this.onSelectionChanged = onSelectionChanged;
            this.selectionColor     = selectionColor;

            this._resetHoverHandler();
        }
Beispiel #2
0
        public DefaultTextStyle(
            Widget child,
            TextStyle style,
            Key key                                  = null,
            TextAlign?textAlign                      = null,
            bool softWrap                            = true,
            TextOverflow overflow                    = TextOverflow.clip,
            int?maxLines                             = null,
            TextWidthBasis textWidthBasis            = TextWidthBasis.parent,
            ui.TextHeightBehavior textHeightBehavior = null

            ) : base(key: key, child: child)
        {
            D.assert(style != null);
            D.assert(maxLines == null || maxLines > 0);
            D.assert(child != null);

            this.style              = style;
            this.textAlign          = textAlign;
            this.softWrap           = softWrap;
            this.overflow           = overflow;
            this.maxLines           = maxLines;
            this.textWidthBasis     = textWidthBasis;
            this.textHeightBehavior = textHeightBehavior;
        }
Beispiel #3
0
 public int DrawText(Control control, int?reference, Rectangle bounds, string text, Font font, Color color,
                     HorizontalAlignment horizontalAlignment, VertictalAlignment vertictalAlignment, bool wordWrap = false,
                     TextOverflow textOverflow = TextOverflow.Clip)
 {
     return(RunDrawCall(control, reference, () => _renderer.DrawText(bounds, text, font, color,
                                                                     horizontalAlignment, vertictalAlignment, wordWrap, textOverflow)));
 }
Beispiel #4
0
 /// <summary>Creates a new <see cref="TextGlyph"/> instance from the given parameters.</summary>
 public TextGlyph(string text, Font font, TextOverflow textOverflow, LogicalDirection textOverflowDirection, Vector sharpnessVector) {
    Text = text;
    Font = font;
    TextOverflow = textOverflow;
    TextOverflowDirection = textOverflowDirection;
    SharpnessVector = sharpnessVector;
    _prepareVisualText(Text);
    var result = _buildGlyph(false, Double.PositiveInfinity);
    if (result != null) {
       _fullRun = result.GlyphRun;
       Size = new Size(result.Width, result.Height);
       Baseline = result.Baseline;
    }
 }
Beispiel #5
0
 public DefaultTextStyleTransition(
     Animation <TextStyle> style,
     Widget child,
     Key key               = null,
     TextAlign?textAlign   = null,
     bool softWrap         = true,
     TextOverflow overflow = TextOverflow.clip,
     int?maxLines          = null
     ) : base(key: key, listenable: style)
 {
     D.assert(style != null);
     D.assert(child != null);
     this.textAlign = textAlign;
     this.softWrap  = softWrap;
     this.overflow  = overflow;
     this.maxLines  = maxLines;
     this.child     = child;
 }
Beispiel #6
0
        public DefaultTextStyle(
            Key key               = null,
            TextStyle style       = null,
            TextAlign?textAlign   = null,
            bool softWrap         = true,
            TextOverflow overflow = TextOverflow.clip,
            int?maxLines          = null,
            Widget child          = null
            ) : base(key, child)
        {
            D.assert(style != null);
            D.assert(maxLines == null || maxLines > 0);
            D.assert(child != null);

            this.style     = style;
            this.textAlign = textAlign;
            this.softWrap  = softWrap;
            this.overflow  = overflow;
            this.maxLines  = maxLines;
        }
Beispiel #7
0
 public WealthyRenderParagraph(
     TextSpan text,
     TextAlign textAlign         = TextAlign.left,
     TextDirection textDirection = TextDirection.ltr,
     bool softWrap             = true,
     TextOverflow overflow     = TextOverflow.clip,
     float textScaleFactor     = 1,
     int?maxLines              = null,
     Action onSelectionChanged = null,
     Color selectionColor      = null) : base(
         text: text,
         textAlign: textAlign,
         textDirection: textDirection,
         softWrap: softWrap,
         overflow: overflow,
         textScaleFactor: textScaleFactor,
         maxLines: maxLines,
         onSelectionChanged: onSelectionChanged,
         selectionColor: selectionColor)
 {
 }
Beispiel #8
0
 public RenderParagraph(TextSpan text,
                        TextAlign textAlign         = TextAlign.left,
                        TextDirection textDirection = TextDirection.ltr,
                        bool softWrap         = true,
                        TextOverflow overflow = TextOverflow.clip,
                        float textScaleFactor = 1.0f,
                        int?maxLines          = null
                        )
 {
     D.assert(maxLines == null || maxLines > 0);
     this._softWrap    = softWrap;
     this._overflow    = overflow;
     this._textPainter = new TextPainter(
         text,
         textAlign,
         textDirection,
         textScaleFactor,
         maxLines,
         overflow == TextOverflow.ellipsis ? _kEllipsis : ""
         );
 }
Beispiel #9
0
 public WealthyTextWrapper(
     Key key                   = null,
     TextSpan text             = null,
     TextAlign textAlign       = TextAlign.left,
     bool softWrap             = true,
     TextOverflow overflow     = TextOverflow.clip,
     float textScaleFactor     = 1F,
     int?maxLines              = null,
     Action onSelectionChanged = null,
     Color selectionColor      = null
     ) : base(
         key: key,
         text: text,
         textAlign: textAlign,
         softWrap: softWrap,
         overflow: overflow,
         textScaleFactor: textScaleFactor,
         maxLines: maxLines,
         onSelectionChanged: onSelectionChanged,
         selectionColor: selectionColor
         )
 {
 }
Beispiel #10
0
 public RenderParagraph(
     InlineSpan text             = null,
     TextAlign textAlign         = TextAlign.start,
     TextDirection textDirection = TextDirection.ltr,
     bool softWrap                            = true,
     TextOverflow overflow                    = TextOverflow.clip,
     float textScaleFactor                    = 1.0f,
     int?maxLines                             = null,
     Locale locale                            = null,
     StrutStyle strutStyle                    = null,
     TextWidthBasis textWidthBasis            = TextWidthBasis.parent,
     ui.TextHeightBehavior textHeightBehavior = null,
     List <RenderBox> children                = null
     )
 {
     D.assert(maxLines == null || maxLines > 0);
     D.assert(text != null);
     D.assert(text.debugAssertIsValid());
     D.assert(maxLines == null || maxLines > 0);
     _softWrap    = softWrap;
     _overflow    = overflow;
     _textPainter = new TextPainter(
         text: text,
         textAlign: textAlign,
         textDirection: textDirection,
         textScaleFactor: textScaleFactor,
         maxLines: maxLines,
         ellipsis: overflow == TextOverflow.ellipsis ? _kEllipsis : null,
         locale: locale,
         strutStyle: strutStyle,
         textWidthBasis: textWidthBasis,
         textHeightBehavior: textHeightBehavior
         );
     addAll(children);
     _extractPlaceholderSpans(text);
 }
Beispiel #11
0
 /// <summary>Creates a new <see cref="TextGlyph"/> instance from the given parameters.</summary>
 public TextGlyph(string text, Font font, TextOverflow textOverflow, LogicalDirection textOverflowDirection)
    : this(text, font, textOverflow, textOverflowDirection, new Vector()) {
 }
Beispiel #12
0
    public void Update()
    {
        if (this.Font == null || (this.Text == this.renderedText && this.WrapSize.x == this.renderedWidth && this.WrapSize.y == this.renderedHeight && this.Anchor == this.renderedAnchor && this.FontScale.x == this.renderedScaleX && this.FontScale.y == this.renderedScaleY && this.Overflow == this.renderedOverflow && this.Wrap == this.renderedWrap))
            return;

        this.UpdateText();

        Vector3 position = new Vector3(0, -this.FontScale.y, 0);
        Vector3 renderSize = this.FontScale;
        float maxWidth = this.Wrap && this.WrapSize.x > 0 ? this.WrapSize.x : float.MaxValue;
        string[] lines;
        Vector2 bounds = this.Font.GetTextRect(this.Text, new Vector2(renderSize.x, renderSize.y), maxWidth, out lines);
        Vector3 offset = new Vector3(0, 0, 0);
        if (this.Anchor == TextAnchor.MiddleCenter || this.Anchor == TextAnchor.MiddleLeft || this.Anchor == TextAnchor.MiddleRight)
            offset.y -= bounds.y / 2;
        if (this.Anchor == TextAnchor.LowerCenter || this.Anchor == TextAnchor.LowerLeft || this.Anchor == TextAnchor.LowerRight)
            offset.y -= bounds.y;

        if (this.Anchor == TextAnchor.UpperRight || this.Anchor == TextAnchor.MiddleRight || this.Anchor == TextAnchor.LowerRight)
            offset.x = bounds.x;
        if (this.Anchor == TextAnchor.UpperCenter || this.Anchor == TextAnchor.MiddleCenter || this.Anchor == TextAnchor.LowerCenter)
            offset.x = bounds.x / 2;

        List<int> tris = new List<int>();
        List<Vector3> vertices = new List<Vector3>();
        List<Vector2> uvs = new List<Vector2>();

        this.GenerateTextMesh(position - offset, this.Text, renderSize, maxWidth, ref tris, ref vertices, ref uvs);

        this.mesh.Clear();
        this.mesh.vertices = vertices.ToArray();
        this.mesh.uv = uvs.ToArray();
        this.mesh.subMeshCount = 1;
        this.mesh.SetTriangles(tris.ToArray(), 0);

        this.renderedText = this.Text;
        this.renderedScaleX = this.FontScale.x;
        this.renderedScaleY = this.FontScale.y;
        this.renderedWidth = this.WrapSize.x;
        this.renderedHeight = this.WrapSize.y;
        this.renderedAnchor = this.Anchor;
        this.renderedOverflow = this.Overflow;
        this.renderedWrap = this.Wrap;
    }
Beispiel #13
0
        // TODO: LTR/RTL support
        public int DrawText(Rectangle bounds, string text, Font font, Color color,
                            HorizontalAlignment horizontalAlignment, VertictalAlignment vertictalAlignment,
                            bool wordWrap, TextOverflow textOverflow)
        {
            if (color.A == 0 || font.Size == 0 || string.IsNullOrWhiteSpace(text))
            {
                return(DenyDrawing());
            }

            FontGlyphs glyphs;

            try
            {
                glyphs = FontManager.Instance.GetFont(font);
            }
            catch (Exception ex)
            {
                var exception = new KeyNotFoundException("The given font is not available.", ex);
                exception.Data.Add("Font", font);
                throw exception;
            }

            var drawCommands    = new List <DrawCommand>(text.Length);
            var lines           = new Queue <TextLine>();
            var currentTextLine = new TextLine()
            {
                StartCommandIndex = 0,
                CommandCount      = 0,
                Width             = 0
            };
            int       x = bounds.X;
            int       y = bounds.Y + font.Size;
            int       lastWhiteSpacePosition = -1;
            int       widthToLastWhiteSpace  = 0;
            int       lastWhiteSpaceWidth    = 0;
            Rectangle?clipRect = textOverflow == TextOverflow.Allow ? (Rectangle?)null : bounds;

            void StartNextLine(TextLine?line = null)
            {
                AdjustLineX();
                x  = bounds.X;
                y += glyphs.LineHeight;
                lastWhiteSpacePosition = -1;
                lines.Enqueue(currentTextLine);
                currentTextLine = line ?? new TextLine()
                {
                    StartCommandIndex = currentTextLine.CurrentCommandIndex,
                    CommandCount      = 0,
                    Width             = 0
                };
            }

            void SplitLine()
            {
                var nextLine = new TextLine()
                {
                    // Note: The whitespace is not added as a draw command so the
                    // lastWhiteSpacePosition points to the draw command after
                    // the whitespace character.
                    StartCommandIndex = lastWhiteSpacePosition,
                    CommandCount      = currentTextLine.CommandCount - lastWhiteSpacePosition,
                    Width             = currentTextLine.Width - widthToLastWhiteSpace - lastWhiteSpaceWidth
                };

                currentTextLine.Width        = widthToLastWhiteSpace;
                currentTextLine.CommandCount = lastWhiteSpacePosition - currentTextLine.StartCommandIndex;
                StartNextLine(nextLine);
                x += nextLine.Width;

                // Reposition the glyphs in the line that has moved down.
                for (int i = 0; i < currentTextLine.CommandCount; ++i)
                {
                    drawCommands[currentTextLine.StartCommandIndex + i].Offset(-widthToLastWhiteSpace - lastWhiteSpaceWidth, glyphs.LineHeight);
                }
            }

            void AdjustLineX()
            {
                var lineDrawCommands = drawCommands.Skip(currentTextLine.StartCommandIndex).Take(currentTextLine.CommandCount);

                switch (horizontalAlignment)
                {
                case HorizontalAlignment.Left:
                default:
                    break;

                case HorizontalAlignment.Center:
                {
                    int offsetX = (bounds.Width - currentTextLine.Width) / 2;
                    if (offsetX != 0)
                    {
                        foreach (var drawCommand in lineDrawCommands)
                        {
                            drawCommand.Offset(offsetX, 0);
                        }
                    }
                    break;
                }

                case HorizontalAlignment.Right:
                {
                    int offsetX = bounds.Width - currentTextLine.Width;
                    if (offsetX != 0)
                    {
                        foreach (var drawCommand in lineDrawCommands)
                        {
                            drawCommand.Offset(offsetX, 0);
                        }
                    }
                    break;
                }

                case HorizontalAlignment.Justify:
                    if (currentTextLine.Width >= bounds.Width)
                    {
                        // If it doesn't fit into the bounds, we just center it.
                        int offsetX = (bounds.Width - currentTextLine.Width) / 2;
                        if (offsetX != 0)
                        {
                            foreach (var drawCommand in lineDrawCommands)
                            {
                                drawCommand.Offset(offsetX, 0);
                            }
                        }
                    }
                    else
                    {
                        // TODO: we don't have the whitespace here anymore :(
                        throw new NotImplementedException("Justify is not implemented yet.");
                    }
                    break;
                }
            }

            for (int i = 0; i < text.Length; ++i)
            {
                if (text[i] == '\n')
                {
                    StartNextLine();
                    continue;
                }
                else if (text[i] == '\r')
                {
                    if (i == text.Length - 1 || text[i + 1] != '\n')
                    {
                        StartNextLine();
                    }
                    continue;
                }

                int  lastX      = x;
                var  glyph      = DrawGlyph(ref x, y, text[i], color, glyphs, clipRect);
                int  glyphWidth = x - lastX;
                bool whiteSpace = text[i] <= 32 || char.IsWhiteSpace(text[i]);


                if (whiteSpace)
                {
                    lastWhiteSpacePosition = currentTextLine.CurrentCommandIndex;
                    widthToLastWhiteSpace  = currentTextLine.Width;
                    lastWhiteSpaceWidth    = glyphWidth;
                }

                int newLineWidth = currentTextLine.Width + glyphWidth;

                if (wordWrap && lastWhiteSpacePosition != -1 && newLineWidth > bounds.Width)
                {
                    // If this is a white space character we simply ignore it and break.
                    if (whiteSpace)
                    {
                        StartNextLine();
                        continue;
                    }
                    else
                    {
                        // Bring the current glyph to the right position.
                        glyph.Offset(-widthToLastWhiteSpace - lastWhiteSpaceWidth, glyphs.LineHeight);
                        SplitLine();
                        x += glyphWidth; // x has been reset in SplitLine!
                    }
                }

                if (glyph != null)
                {
                    drawCommands.Add(glyph);
                    ++currentTextLine.CommandCount;
                }

                currentTextLine.Width += glyphWidth;

                if (i == text.Length - 1)
                {
                    AdjustLineX();

                    lines.Enqueue(currentTextLine);

                    // Adjust y of all lines
                    int totalHeight = lines.Count * glyphs.LineHeight;

                    switch (vertictalAlignment)
                    {
                    case VertictalAlignment.Top:
                    default:
                        break;

                    case VertictalAlignment.Center:
                    {
                        int offsetY = (bounds.Height - totalHeight) / 2;
                        if (offsetY != 0)
                        {
                            foreach (var drawCommand in drawCommands)
                            {
                                drawCommand.Offset(0, offsetY);
                            }
                        }
                        break;
                    }

                    case VertictalAlignment.Bottom:
                    {
                        int offsetY = bounds.Height - totalHeight;
                        if (offsetY != 0)
                        {
                            foreach (var drawCommand in drawCommands)
                            {
                                drawCommand.Offset(0, offsetY);
                            }
                        }
                        break;
                    }
                    }
                }
            }

            if (drawCommands.Count == 0)
            {
                return(DenyDrawing()); // Nothing was drawn.
            }
            ++_displayLayer;

            return(AddDrawCommands(drawCommands.ToArray()));
        }
Beispiel #14
0
        // rightVectorとdownVectorが正規直交していないと平行四辺形に歪むので注意
        int AddText(
            string text,
            Vector3 origin,
            Vector3 rightVector,
            Vector3 downVector,
            float fontSize,
            float boxWidth,
            float boxHeight,
            AlignX alignX,
            AlignY alignY,
            TextOverflow overflow,
            float lineSpacingRatio)
        {
            int verticesBegin = vertexCount;

            float width, height;
            var   scale = 0f;
            bool  wrap;
            float normalizedBoxWidth, normalizedBoxHeight;

            if (overflow == TextOverflow.Wrap)
            {
                UnityEngine.Debug.Assert(fontSize > 0f);
                scale = fontSize / (float)font.fontSize;
                normalizedBoxWidth  = boxWidth * scale;
                normalizedBoxHeight = boxHeight * scale;
                wrap = true;
            }
            else //if (overflow == TextOverflow.Scale)
            {
                normalizedBoxWidth = normalizedBoxHeight = float.MaxValue;
                wrap = false;
            }
            var drawnLines = AddTextNormalized(out width, out height, text, normalizedBoxWidth, normalizedBoxHeight, lineSpacingRatio, wrap);

            if (drawnLines <= 0) // 何も書いてないなら抜ける
            {
                return(drawnLines);
            }

            if (overflow == TextOverflow.Scale)
            {
                var scaleX = boxWidth / width;
                var scaleY = boxHeight / height;
                scale = Mathf.Min(scaleX, scaleY);
            }

            var matrix   = Matrix4x4.Translate(origin);
            var rotation = Matrix4x4.identity;

            rotation.m00 = rightVector.x;
            rotation.m10 = rightVector.y;
            rotation.m20 = rightVector.z;
            rotation.m01 = -downVector.x;
            rotation.m11 = -downVector.y;
            rotation.m21 = -downVector.z;
            var up = Vector3.Cross(downVector, rightVector);

            rotation.m02 = up.x;
            rotation.m12 = up.y;
            rotation.m22 = up.z;
            matrix      *= rotation;

            matrix *= Matrix4x4.Scale(new Vector3(scale, -scale, 1f));
            // TODO: 以下の計算はまだ仮
            float offsetX = 0f;

            switch (alignX)
            {
            case AlignX.Center: offsetX = -width * 0.5f; break;

            case AlignX.Right: offsetX = -width; break;
            }
            float offsetY = 0f;

            switch (alignY)
            {
            case AlignY.Center: offsetY = -height * 0.5f; break;

            case AlignY.Bottom: offsetY = -height; break;
            }
            matrix *= Matrix4x4.Translate(new Vector3(offsetX, offsetY, 0f));
            TransformVertices(verticesBegin, ref matrix);
            return(drawnLines);
        }
Beispiel #15
0
        public int AddText(
            string text,
            float leftX,
            float topY,
            float fontSize,
            float boxWidth,
            float boxHeight,
            AlignX alignX,
            AlignY alignY,
            TextOverflow overflow,
            float lineSpacingRatio)
        {
            int verticesBegin = vertexCount;

            float width, height;
            float scale = 0f;
            bool  wrap;
            float normalizedBoxWidth, normalizedBoxHeight;

            if (overflow == TextOverflow.Wrap)
            {
                UnityEngine.Debug.Assert(fontSize > 0f);
                scale = fontSize / (float)font.fontSize;
                normalizedBoxWidth  = boxWidth / scale;
                normalizedBoxHeight = boxHeight / scale;
                wrap = true;
            }
            else //if (overflow == TextOverflow.Scale)
            {
                normalizedBoxWidth = normalizedBoxHeight = float.MaxValue;
                wrap = false;
            }
            var drawnLines = AddTextNormalized(out width, out height, text, normalizedBoxWidth, normalizedBoxHeight, lineSpacingRatio, wrap);

            if (drawnLines <= 0) // 何も書いてないなら抜ける
            {
                return(drawnLines);
            }

            if (overflow == TextOverflow.Scale)
            {
                var scaleX = boxWidth / width;
                var scaleY = boxHeight / height;
                scale = Mathf.Min(scaleX, scaleY);
            }

            // TODO: 以下の計算はまだ仮
            var offset = Vector2.zero;

            switch (alignX)
            {
            case AlignX.Center: offset.x = -width * 0.5f; break;

            case AlignX.Right: offset.x = -width; break;
            }
            switch (alignY)
            {
            case AlignY.Center: offset.y = -height * 0.5f; break;

            case AlignY.Bottom: offset.y = -height; break;
            }
            offset   *= scale;
            offset.x += leftX;
            offset.y += topY;
            TransformVertices(verticesBegin, scale, ref offset);
            return(drawnLines);
        }
 void TextOverflowUpdateToggleIcon(TextOverflow resolveStyle)
 {
     m_TextOverflowButton.EnableInClassList("clip", resolveStyle == TextOverflow.Clip);
     m_TextOverflowButton.EnableInClassList("ellipsis", resolveStyle == TextOverflow.Ellipsis);
 }
 public FluentElement <T> TextOverflow(TextOverflow overflow)
 {
     x.style.textOverflow = overflow; return(x);
 }