Пример #1
0
        public static NSMutableAttributedString AttributedString(this Font font, string text, NSAttributedString attributedString = null)
        {
            var mutable = attributedString.ToMutable(text);

            font.Apply(mutable);

            return(mutable);
        }
Пример #2
0
 public static NSAttributedString AttributedString(this Font font, NSAttributedString attributedString)
 {
     if (font != null)
     {
         var str = attributedString as NSMutableAttributedString ?? new NSMutableAttributedString(attributedString);
         font.Apply(str);
         return(str);
     }
     return(attributedString);
 }
Пример #3
0
 public SizeF MeasureString(Font font, string text)
 {
     using (var layout = CreateLayout())
     {
         font.Apply(layout);
         layout.SetText(text);
         int width, height;
         layout.GetPixelSize(out width, out height);
         return(new SizeF(width, height));
     }
 }
Пример #4
0
        public void Load(Stream stream, RichTextAreaFormat format)
        {
            var range = new NSRange(0, Control.TextStorage.Length);

            switch (format)
            {
            case RichTextAreaFormat.Rtf:
            {
                var str = NSAttributedString.CreateWithRTF(NSData.FromStream(stream), out var docAttributes);
                NSMutableAttributedString mut = null;
                nint pos = 0;
                // when encountering an RTF without a defined foreground, use the system foreground for dark mode
                var textColor = TextColor.ToNSUI();
                while (pos < str.Length)
                {
                    var color = str.GetAttribute(NSStringAttributeKey.ForegroundColor, pos, out var effectiveRange);
                    pos = effectiveRange.Location + effectiveRange.Length;
                    if (color == null)
                    {
                        if (mut == null)
                        {
                            mut = new NSMutableAttributedString(str);
                        }

                        mut.AddAttribute(NSStringAttributeKey.ForegroundColor, textColor, effectiveRange);
                    }
                }

                if (mut != null)
                {
                    str = mut;
                }

                Control.TextStorage.Replace(range, str);
                break;
            }

            case RichTextAreaFormat.PlainText:
            {
                var str = new NSMutableAttributedString(new StreamReader(stream).ReadToEnd());
                Font.Apply(str);
                str.AddAttribute(NSStringAttributeKey.ForegroundColor, TextColor.ToNSUI(), new NSRange(0, str.Length));
                Control.TextStorage.Replace(range, str);
                break;
            }

            default:
                throw new NotSupportedException();
            }
            Callback.OnTextChanged(Widget, EventArgs.Empty);
        }
Пример #5
0
 public void DrawText(Font font, SolidBrush brush, float x, float y, string text)
 {
     using (var layout = CreateLayout())
     {
         font.Apply(layout);
         layout.SetText(text);
         Control.Save();
         Control.Color = brush.Color.ToCairo();
         Control.MoveTo(x, y);
         Pango.CairoHelper.LayoutPath(Control, layout);
         Control.Fill();
         Control.Restore();
     }
 }
Пример #6
0
        public void DrawText(Font font, SolidBrush brush, float x, float y, string text)
        {
            var oldAA = AntiAlias;

            AntiAlias = true;
            SetOffset(true);
            using (var layout = CreateLayout())
            {
                font.Apply(layout);
                layout.SetText(text);
                Control.Save();
                Control.SetSourceColor(brush.Color.ToCairo());
                Control.MoveTo(x, y);
                Pango.CairoHelper.LayoutPath(Control, layout);
                Control.Fill();
                Control.Restore();
            }
            AntiAlias = oldAA;
        }
Пример #7
0
        public override void HandleAutoSize()
        {
            Font.Apply(Core.internalGraphics);

            double w = Core.internalGraphics.TextWidth(text);
            double h = Core.internalGraphics.TextHeight(text);

            if (this.Display == DisplayMode.Flow)
            {
                for (int i = 0; i < flowChunks.Length; i++)
                {
                    w = Math.Max(w, (renderFlowChunkLocations[i].RealL - renderLocation.RealL) + flowChunks[i].W);
                    h = Math.Max(h, (renderFlowChunkLocations[i].RealT - renderLocation.RealT) + flowChunks[i].H);
                }
            }

            this.Path.W = w;
            this.Path.H = h;
        }
Пример #8
0
 void SetAttributes(bool force = false)
 {
     if (Widget.Loaded || force)
     {
         if (str.Length > 0)
         {
             var range = new NSRange(0, str.Length);
             var attr  = new NSMutableDictionary();
             font.Apply(attr);
             attr.Add(NSAttributedString.ParagraphStyleAttributeName, paragraphStyle);
             attr.Add(NSAttributedString.ForegroundColorAttributeName, textColor);
             str.SetAttributes(attr, range);
             if (underlineIndex >= 0)
             {
                 var num      = (NSNumber)str.GetAttribute(NSAttributedString.UnderlineStyleAttributeName, underlineIndex, out range);
                 var newStyle = (num != null && (NSUnderlineStyle)num.IntValue == NSUnderlineStyle.Single) ? NSUnderlineStyle.Double : NSUnderlineStyle.Single;
                 str.AddAttribute(NSAttributedString.UnderlineStyleAttributeName, new NSNumber((int)newStyle), new NSRange(underlineIndex, 1));
             }
         }
         Control.AttributedStringValue = str;
     }
 }
Пример #9
0
        public override Rectangle[] GetFlowChunks()
        {
            string[] words = text.Trim().Split(new char[] { ' ' });

            if (words.Length == 0)
            {
                return new Rectangle[] { new Rectangle(this.Padding.L + this.Padding.R, this.Padding.T + this.Padding.B) }
            }
            ;

            double spaceWidth = 0;

            flowChunks = new Rectangle[words.Length];

            Font.Apply(Core.internalGraphics);

            for (int i = 0; i < flowChunks.Length; i++)
            {
                flowChunks[i] = new Rectangle(Core.internalGraphics.TextWidth(words[i]) + spaceWidth, Core.internalGraphics.TextHeight(words[i]));
            }

            return(flowChunks);
        }
Пример #10
0
        void Setup(Pango.Layout layout)
        {
            Font.Apply(layout);
            layout.Width = (int)(MaximumSize.Width * Pango.Scale.PangoScale);
#if GTK3
            layout.Height = (int)(MaximumSize.Height * Pango.Scale.PangoScale);
#endif
            layout.Ellipsize = Trimming == FormattedTextTrimming.None ? Pango.EllipsizeMode.None : Pango.EllipsizeMode.End;
            switch (Wrap)
            {
            case FormattedTextWrapMode.None:
                // only draw one line!!
                layout.Wrap = Pango.WrapMode.Char;
#if GTK3
                layout.Height = (int)((double)layout.FontDescription.Size / (double)Pango.Scale.PangoScale);
#endif
                break;

            case FormattedTextWrapMode.Word:
                layout.Wrap = Pango.WrapMode.Word;
                break;

            case FormattedTextWrapMode.Character:
                layout.Wrap = Pango.WrapMode.Char;
                break;
            }
            switch (Alignment)
            {
            case FormattedTextAlignment.Left:
                layout.Alignment = Pango.Alignment.Left;
                break;

            case FormattedTextAlignment.Right:
                layout.Alignment = Pango.Alignment.Right;
                break;

            case FormattedTextAlignment.Center:
                layout.Alignment = Pango.Alignment.Center;
                break;

            case FormattedTextAlignment.Justify:
                layout.Alignment = Pango.Alignment.Left;
                layout.Justify   = true;
                break;
            }
            layout.SetText(Text);
            if (Wrap == FormattedTextWrapMode.None && layout.LineCount > 1)
            {
                // line includes the full last word so keep shrinking until it isn't wrapped
                var len = layout.GetLine(0).Length;
                while (len > 0 && layout.IsWrapped)
                {
                    layout.SetText(Text.Substring(0, len--));
                }
            }
            if (Trimming == FormattedTextTrimming.None && layout.LineCount > 1)
            {
                layout.GetPixelSize(out _, out var height);
                while (layout.LineCount > 1 && height > MaximumSize.Height)
                {
                    var lineCount = layout.LineCount;
                    var line      = layout.GetLine(lineCount - 1);
                    var len       = line.StartIndex;
                    layout.SetText(Text.Substring(0, len));
                    if (layout.LineCount == lineCount)
                    {
                        // need to trim off some characters or words from the current line
                        while (layout.LineCount == lineCount && --len > 0)
                        {
                            if (Wrap == FormattedTextWrapMode.Word)
                            {
                                // keep going till whitespace for wrapping.
                                while (len > 0 && !char.IsWhiteSpace(Text[len - 1]))
                                {
                                    len--;
                                }
                            }
                            layout.SetText(Text.Substring(0, len));
                        }
                    }
                    layout.GetPixelSize(out _, out height);
                }
            }
        }
Пример #11
0
 public override void Apply(Tesseract.Backends.IGraphics g)
 {
     font.Apply(g);
     g.Text(text, 0, 0);
 }