Ejemplo n.º 1
0
        public Task <NSImage> LoadImageAsync(
            ImageSource imagesource,
            CancellationToken cancelationToken = default(CancellationToken),
            float scale = 1f)
        {
            NSImage image      = null;
            var     fontsource = imagesource as FontImageSource;

            if (fontsource != null)
            {
                var font = NSFont.FromFontName(fontsource.FontFamily ?? string.Empty, (float)fontsource.Size) ??
                           NSFont.SystemFontOfSize((float)fontsource.Size);
                var iconcolor = fontsource.Color.IsDefault ? _defaultColor : fontsource.Color;
                var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToNSColor());
                var imagesize = ((NSString)fontsource.Glyph).StringSize(attString.GetAttributes(0, out _));

                using (var context = new CGBitmapContext(IntPtr.Zero, (nint)imagesize.Width, (nint)imagesize.Height, 8, (nint)imagesize.Width * 4, NSColorSpace.GenericRGBColorSpace.ColorSpace, CGImageAlphaInfo.PremultipliedFirst))
                {
                    using (var ctline = new CTLine(attString))
                    {
                        ctline.Draw(context);
                    }

                    using (var cgImage = context.ToImage())
                    {
                        image = new NSImage(cgImage, imagesize);
                    }
                }
            }

            return(Task.FromResult(image));
        }
Ejemplo n.º 2
0
        public static NSMutableAttributedString ToMutable(this NSAttributedString attributedString, string text)
        {
            if (attributedString != null && attributedString.Length > 0)
            {
                NSRange range;
                return(new NSMutableAttributedString(text, attributedString.GetAttributes(0, out range)));
            }

            return(new NSMutableAttributedString(text));
        }
Ejemplo n.º 3
0
        public static IEnumerable <ISpan> AsSpans(this NSAttributedString text, int cursorPosition)
        {
            var  start = 0;
            bool queryTextSpanAlreadyUsed = false;

            while (start != text.Length)
            {
                var attributes = text.GetAttributes(start, out var longestEffectiveRange, new NSRange(start, text.Length - start));
                var length     = (int)longestEffectiveRange.Length;
                var end        = start + length;

                if (attributes.ContainsKey(ProjectId))
                {
                    var(projectId, projectName, projectColor, taskId, taskName) = attributes.GetProjectInformation();

                    yield return(new ProjectSpan(projectId, projectName, projectColor, taskId, taskName));
                }
                else if (attributes.ContainsKey(TagId))
                {
                    var(tagId, tagName) = attributes.GetTagInformation();
                    yield return(new TagSpan(tagId, tagName));
                }
                else if (length > 0)
                {
                    var subText = text.Substring(start, length).ToString().Substring(0, length);
                    if (queryTextSpanAlreadyUsed == false && cursorPosition.IsInRange(start, end))
                    {
                        queryTextSpanAlreadyUsed = true;
                        yield return(new QueryTextSpan(subText, cursorPosition - start));
                    }
                    else
                    {
                        yield return(new TextSpan(subText));
                    }
                }

                start = end;
            }
        }