private void ReadRun() { TextAttributes attributes = new TextAttributes(); var start = ReadInt(XmlNames.Start, 0); var length = ReadInt(XmlNames.Length, 0); if (_reader.HasAttributes) { _reader.MoveToElement(); while (_reader.MoveToNextAttribute()) { var attributeName = _reader.Name; var attributeValue = _reader.Value; if (!(XmlNames.Start.Equals(attributeName) || XmlNames.Length.Equals(attributeName))) { if (Enum.TryParse(attributeName, out TextAttribute key)) { attributes[key] = attributeValue; } } } _reader.MoveToElement(); } var run = new AttributedTextRun(start, length, attributes); _runs.Add(run); }
public static void Optimize(this List <IAttributedTextRun> runs, int textLength) { // Loop through the runs and make sure that they don't extend beyond the bounds of the text. for (int i = 0; i < runs.Count; i++) { var run = runs[i]; var end = run.GetEnd(); if (run.Start < 0 || end > textLength) { var start = Math.Max(run.Start, 0); var maxLength = textLength - start; var length = Math.Min(run.Length, maxLength); if (length > 0) { runs[i] = new AttributedTextRun(start, length, run.Attributes); } else { runs.RemoveAt(i--); } } } runs.Sort(AttributedTextRunComparer.Instance); // Loop through the runs and join the ones that overlap. IAttributedTextRun previous = null; for (int i = 0; i < runs.Count; i++) { var run = runs[i]; if (previous != null) { if (previous.IntersectsExactly(run)) { var combined = previous.Attributes.Union(run.Attributes); run = runs[i - 1] = new AttributedTextRun(run.Start, run.Length, combined); runs.RemoveAt(i--); } else if (previous.Intersects(run)) { var intersections = previous.CalculatedIntersections(run); runs.RemoveAt(i--); runs.RemoveAt(i); runs.InsertRange(i++, intersections); run = runs[i]; runs.Sort(AttributedTextRunComparer.Instance); } } previous = run; } }
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); }