/// <summary> /// Sets or changes the font style /// </summary> /// <param name="style">Font style</param> /// <param name="startIndex">The start index of initial character to apply the change to.</param> /// <param name="count">The number of characters the change should be applied to.</param> public void SetFontStyle(FontStyle style, int startIndex, int count) { int limit = ValidateRange(startIndex, count); for (int i = startIndex; i < limit;) { SpanRider formatRider = new SpanRider(_formatRuns, _latestPosition, i); i = Math.Min(limit, i + formatRider.Length); #pragma warning disable 6506 // Presharp warns that runProps is not validated, but it can never be null // because the rider is already checked to be in range GenericTextRunProperties runProps = formatRider.CurrentElement as GenericTextRunProperties; Invariant.Assert(runProps != null); Typeface oldTypeface = runProps.Typeface; if (oldTypeface.Style == style) continue; GenericTextRunProperties newProps = new GenericTextRunProperties( new Typeface(oldTypeface.FontFamily, style, oldTypeface.Weight, oldTypeface.Stretch), runProps.FontRenderingEmSize, runProps.FontHintingEmSize, runProps.TextDecorations, runProps.ForegroundBrush, runProps.BackgroundBrush, runProps.BaselineAlignment, runProps.CultureInfo, runProps.NumberSubstitution ); #pragma warning restore 6506 _latestPosition = _formatRuns.SetValue(formatRider.CurrentPosition, i - formatRider.CurrentPosition, newProps, formatRider.SpanPosition); InvalidateMetrics(); // invalidate cached metrics } }
/// <summary> /// Sets or changes the text decorations /// </summary> /// <param name="textDecorations">Text decorations</param> /// <param name="startIndex">The start index of initial character to apply the change to.</param> /// <param name="count">The number of characters the change should be applied to.</param> public void SetTextDecorations(TextDecorationCollection textDecorations, int startIndex, int count) { int limit = ValidateRange(startIndex, count); for (int i = startIndex; i < limit;) { SpanRider formatRider = new SpanRider(_formatRuns, _latestPosition, i); i = Math.Min(limit, i + formatRider.Length); #pragma warning disable 6506 // Presharp warns that runProps is not validated, but it can never be null // because the rider is already checked to be in range GenericTextRunProperties runProps = formatRider.CurrentElement as GenericTextRunProperties; Invariant.Assert(runProps != null); if (runProps.TextDecorations == textDecorations) continue; GenericTextRunProperties newProps = new GenericTextRunProperties( runProps.Typeface, runProps.FontRenderingEmSize, runProps.FontHintingEmSize, textDecorations, runProps.ForegroundBrush, runProps.BackgroundBrush, runProps.BaselineAlignment, runProps.CultureInfo, runProps.NumberSubstitution ); #pragma warning restore 6506 _latestPosition = _formatRuns.SetValue(formatRider.CurrentPosition, i - formatRider.CurrentPosition, newProps, formatRider.SpanPosition); } }
/// <summary> /// Construct a FormattedText object. /// </summary> /// <param name="textToFormat">String of text to be displayed.</param> /// <param name="culture">Culture of text.</param> /// <param name="flowDirection">Flow direction of text.</param> /// <param name="typeface">Type face used to display text.</param> /// <param name="emSize">Font em size in visual units (1/96 of an inch).</param> /// <param name="foreground">Foreground brush used to render text.</param> /// <param name="numberSubstitution">Number substitution behavior to apply to the text; can be null, /// in which case the default number number method for the text culture is used.</param> public FormattedText( string textToFormat, CultureInfo culture, FlowDirection flowDirection, Typeface typeface, double emSize, Brush foreground, NumberSubstitution numberSubstitution, TextFormattingMode textFormattingMode) { if (textToFormat == null) throw new ArgumentNullException("textToFormat"); if (typeface == null) throw new ArgumentNullException("typeface"); ValidateCulture(culture); ValidateFlowDirection(flowDirection, "flowDirection"); ValidateFontSize(emSize); _textFormattingMode = textFormattingMode; _text = textToFormat; GenericTextRunProperties runProps = new GenericTextRunProperties( typeface, emSize, 12.0f, // default hinting size null, // decorations foreground, null, // highlight background BaselineAlignment.Baseline, culture, numberSubstitution ); _latestPosition = _formatRuns.SetValue(0, _text.Length, runProps, _latestPosition); _defaultParaProps = new GenericTextParagraphProperties( flowDirection, TextAlignment.Left, false, false, runProps, TextWrapping.WrapWithOverflow, 0, // line height not specified 0 // indentation not specified ); InvalidateMetrics(); }