protected override void Write(AttributedTextRenderer renderer, EmphasisInline obj)
        {
            var start = renderer.Count;

            renderer.WriteChildren(obj);
            var length = renderer.Count - start;

            var bold          = IsBold(obj);
            var underline     = IsUnderline(obj);
            var italic        = IsItalic(obj);
            var strikethrough = IsStrikethrough(obj);
            var subscript     = IsSubscript(obj);
            var superscript   = IsSuperscript(obj);

            if (length > 0 && (bold || italic || strikethrough || subscript || superscript || underline))
            {
                var attributes = new TextAttributes();
                attributes.SetBold(bold);
                attributes.SetItalic(italic);
                attributes.SetStrikethrough(strikethrough);
                attributes.SetSubscript(subscript);
                attributes.SetSuperscript(superscript);
                attributes.SetUnderline(underline);
                renderer.AddTextRun(start, length, attributes);
            }
        }
Exemplo n.º 2
0
        private static bool HandleAttributes(
            IList <IAttributedTextRun> runs,
            TextWriter writer,
            NSAttributedString target,
            NSDictionary attrs,
            NSRange range)
        {
            var text = target.Substring(range.Location, range.Length).Value;

            var formatAttributes = new TextAttributes();
            var run = new AttributedTextRun((int)range.Location, (int)range.Length, formatAttributes);

            NSObject font;

            if (attrs.TryGetValue(NSStringAttributeKey.Font, out font))
            {
                var actualFont = (NSFont)font;
#if __MACOS__
                var fontName = actualFont.FontName;
#else
                var fontName = actualFont.Name;
#endif

                formatAttributes.SetFontSize((float)actualFont.PointSize);
                if (!fontName.StartsWith(".", System.StringComparison.Ordinal))
                {
                    formatAttributes.SetFontName(fontName);
                }
                else
                {
                    if (fontName.Contains("Italic"))
                    {
                        formatAttributes.SetItalic(true);
                    }

                    if (fontName.Contains("Bold"))
                    {
                        formatAttributes.SetBold(true);
                    }
                }
            }

            NSObject underline;
            if (attrs.TryGetValue(NSStringAttributeKey.UnderlineStyle, out underline))
            {
                var number = underline as NSNumber;
                if (number != null && number.Int32Value > 0)
                {
                    formatAttributes.SetUnderline(true);
                }
            }

            NSObject strikethrough;
            if (attrs.TryGetValue(NSStringAttributeKey.StrikethroughStyle, out strikethrough))
            {
                var number = strikethrough as NSNumber;
                if (number != null && number.Int32Value > 0)
                {
                    formatAttributes.SetStrikethrough(true);
                }
            }

#if MONOMAC
            NSObject superscript;
            if (attrs.TryGetValue(NSStringAttributeKey.Superscript, out superscript))
            {
                var number = superscript as NSNumber;
                if (number != null && number.Int32Value == -1)
                {
                    formatAttributes.SetSubscript(true);
                }
                else if (number != null && number.Int32Value == 1)
                {
                    formatAttributes.SetSuperscript(true);
                }
            }
#endif

            NSObject color;
            if (attrs.TryGetValue(NSStringAttributeKey.ForegroundColor, out color))
            {
                var colorObject = color as NSColor;
                if (colorObject != null)
                {
                    formatAttributes.SetForegroundColor(colorObject.ToHex());
                }
            }

            NSObject backgroundColor;
            if (attrs.TryGetValue(NSStringAttributeKey.BackgroundColor, out backgroundColor))
            {
                var colorObject = backgroundColor as NSColor;
                if (colorObject != null)
                {
                    formatAttributes.SetBackgroundColor(colorObject.ToHex());
                }
            }

#if MONOMAC
            NSObject paragraphStyleAsObject;
            if (attrs.TryGetValue(NSStringAttributeKey.ParagraphStyle, out paragraphStyleAsObject))
            {
                var paragraphStyle = (NSParagraphStyle)paragraphStyleAsObject;
                if (paragraphStyle.TextLists != null && paragraphStyle.TextLists.Length > 0)
                {
                    for (int i = 0; i < paragraphStyle.TextLists.Length; i++)
                    {
                        var textList     = paragraphStyle.TextLists [i];
                        var markerFormat = textList.MarkerFormat;

                        if ("{hyphen}".Equals(textList.MarkerFormat))
                        {
                            formatAttributes.SetUnorderedList(true);
                            formatAttributes.SetMarker(MarkerType.Hyphen);
                        }
                        else
                        {
                            formatAttributes.SetUnorderedList(true);
                            formatAttributes.SetMarker(MarkerType.ClosedCircle);
                        }
                    }
                }
            }
#endif

            if (run.Attributes.Count > 0)
            {
                runs.Add(run);
            }

            writer.Write(text);
            return(false);
        }