private CanvasTextLayout CreateTextLayout(ICanvasResourceCreator resourceCreator, float canvasWidth, float canvasHeight) { string testString; if (CurrentTextLengthOption == TextLengthOption.Short) { testString = "one two"; } else { testString = "This is some text which demonstrates Win2D's text-to-geometry option; there are sub-options of this test which apply lining options such as underline or strike-through. Additionally, this example applies different text directions to ensure glyphs are transformed correctly."; } if(ThumbnailGenerator.IsDrawingThumbnail) { testString = "a"; } for (int i = 0; i < testString.Length; ++i) { if (testString[i] == ' ') { int nextSpace = testString.IndexOf(' ', i + 1); int limit = nextSpace == -1 ? testString.Length : nextSpace; WordBoundary wb = new WordBoundary(); wb.Start = i + 1; wb.Length = limit - i - 1; everyOtherWordBoundary.Add(wb); i = limit; } } float sizeDim = Math.Min(canvasWidth, canvasHeight); CanvasTextFormat textFormat = new CanvasTextFormat() { FontSize = CurrentTextLengthOption == TextLengthOption.Paragraph? sizeDim * 0.09f : sizeDim * 0.3f, Direction = CurrentTextDirection, }; if (ThumbnailGenerator.IsDrawingThumbnail) { textFormat.FontSize = sizeDim * 0.75f; } CanvasTextLayout textLayout = new CanvasTextLayout(resourceCreator, testString, textFormat, canvasWidth, canvasHeight); if(CurrentTextEffectOption == TextEffectOption.UnderlineEveryOtherWord) { foreach(WordBoundary wb in everyOtherWordBoundary) { textLayout.SetUnderline(wb.Start, wb.Length, true); } } else if (CurrentTextEffectOption == TextEffectOption.StrikeEveryOtherWord) { foreach (WordBoundary wb in everyOtherWordBoundary) { textLayout.SetStrikethrough(wb.Start, wb.Length, true); } } textLayout.VerticalGlyphOrientation = CurrentVerticalGlyphOrientation; return textLayout; }
private CanvasTextLayout CreateTextLayout(ICanvasResourceCreator resourceCreator, float canvasWidth, float canvasHeight) { float sizeDim = Math.Min(canvasWidth, canvasHeight); CanvasTextFormat textFormat = new CanvasTextFormat() { FontSize = ThumbnailGenerator.IsDrawingThumbnail? sizeDim * 0.25f : sizeDim * 0.085f, Direction = CurrentTextDirection, }; CanvasTextLayout textLayout = new CanvasTextLayout(resourceCreator, testString, textFormat, canvasWidth, canvasHeight); if (CurrentTextEffectOption == TextEffectOption.UnderlineEveryOtherWord) { foreach (Utils.WordBoundary wb in everyOtherWordBoundary) { textLayout.SetUnderline(wb.Start, wb.Length, true); } } else if (CurrentTextEffectOption == TextEffectOption.StrikeEveryOtherWord) { foreach (Utils.WordBoundary wb in everyOtherWordBoundary) { textLayout.SetStrikethrough(wb.Start, wb.Length, true); } } textLayout.VerticalGlyphOrientation = CurrentVerticalGlyphOrientation; return textLayout; }
public void Draw(object ds, XText text, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r) { var _ds = ds as CanvasDrawingSession; var tbind = text.BindToTextProperty(db, r); if (string.IsNullOrEmpty(tbind)) return; var brush = ToColor(text.Style.Stroke); var fontWeight = FontWeights.Normal; if (text.Style.TextStyle.FontStyle.HasFlag(Test2d.FontStyle.Bold)) { fontWeight = FontWeights.Bold; } var fontStyle = Windows.UI.Text.FontStyle.Normal; if (text.Style.TextStyle.FontStyle.HasFlag(Test2d.FontStyle.Italic)) { fontStyle = Windows.UI.Text.FontStyle.Italic; } var format = new CanvasTextFormat() { FontFamily = text.Style.TextStyle.FontName, FontWeight = fontWeight, FontStyle = fontStyle, FontSize = (float)text.Style.TextStyle.FontSize, WordWrapping = CanvasWordWrapping.NoWrap }; var rect = Rect2.Create(text.TopLeft, text.BottomRight, dx, dy); var layout = new CanvasTextLayout(_ds, tbind, format, (float)rect.Width, (float)rect.Height); if (text.Style.TextStyle.FontStyle.HasFlag(Test2d.FontStyle.Underline)) { layout.SetUnderline(0, tbind.Length, true); } if (text.Style.TextStyle.FontStyle.HasFlag(Test2d.FontStyle.Strikeout)) { layout.SetStrikethrough(0, tbind.Length, true); } switch (text.Style.TextStyle.TextHAlignment) { case TextHAlignment.Left: layout.HorizontalAlignment = CanvasHorizontalAlignment.Left; break; case TextHAlignment.Center: layout.HorizontalAlignment = CanvasHorizontalAlignment.Center; break; case TextHAlignment.Right: layout.HorizontalAlignment = CanvasHorizontalAlignment.Right; break; } switch (text.Style.TextStyle.TextVAlignment) { case TextVAlignment.Top: layout.VerticalAlignment = CanvasVerticalAlignment.Top; break; case TextVAlignment.Center: layout.VerticalAlignment = CanvasVerticalAlignment.Center; break; case TextVAlignment.Bottom: layout.VerticalAlignment = CanvasVerticalAlignment.Bottom; break; } _ds.DrawTextLayout( layout, new N.Vector2( (float)rect.X, (float)rect.Y), brush); layout.Dispose(); format.Dispose(); }
private CanvasTextLayout CreateTextLayout(ICanvasResourceCreator resourceCreator, float canvasWidth, float canvasHeight) { string testString; if (CurrentTextLengthOption == TextLengthOption.Short) { testString = "one two"; } else { testString = "This is some text which demonstrates Win2D's text-to-geometry option; there are sub-options of this test which apply lining options such as underline or strike-through. Additionally, this example applies different text directions to ensure glyphs are transformed correctly."; } if (ThumbnailGenerator.IsDrawingThumbnail) { testString = "a"; } everyOtherWordBoundary = Utils.GetEveryOtherWord(testString); float sizeDim = Math.Min(canvasWidth, canvasHeight); CanvasTextFormat textFormat = new CanvasTextFormat() { FontSize = CurrentTextLengthOption == TextLengthOption.Paragraph? sizeDim * 0.09f : sizeDim * 0.3f, Direction = CurrentTextDirection, }; if (ThumbnailGenerator.IsDrawingThumbnail) { textFormat.FontSize = sizeDim * 0.75f; } CanvasTextLayout textLayout = new CanvasTextLayout(resourceCreator, testString, textFormat, canvasWidth, canvasHeight); if (CurrentTextEffectOption == TextEffectOption.UnderlineEveryOtherWord) { foreach (var wb in everyOtherWordBoundary) { textLayout.SetUnderline(wb.Start, wb.Length, true); } } else if (CurrentTextEffectOption == TextEffectOption.StrikeEveryOtherWord) { foreach (var wb in everyOtherWordBoundary) { textLayout.SetStrikethrough(wb.Start, wb.Length, true); } } textLayout.TrimmingGranularity = CanvasTextTrimmingGranularity.Character; textLayout.TrimmingSign = CanvasTrimmingSign.Ellipsis; textLayout.VerticalGlyphOrientation = CurrentVerticalGlyphOrientation; return textLayout; }