private void ExtendRectangle(BoundedText text, Graphics graphics, int recursionLevel = 0) { const int maxRecursionLevel = 20; if (!text.GetBlackBox().IsEmpty || recursionLevel >= maxRecursionLevel) { return; } // Calculate increment for the rectangle dimensions. It must be a positive even integer. var increment = (int)(System.Math.Min(graphics.Width, graphics.Height) * 0.1f); if (increment % 2 == 1) { increment += 1; } increment = System.Math.Max(increment, Common.ConvertPointsToPixels(graphics.DpiX, 2)); // Create an extended rectangle and move its top-left corner in accordance with the alignment var rect = new RectangleF(text.Rectangle.X, text.Rectangle.Y, text.Rectangle.Width + increment, text.Rectangle.Height + increment); if (text.Alignment == AdvancedTextAlignment.Center) { rect.X -= increment / 2f; } else if (text.Alignment == AdvancedTextAlignment.Right) { rect.X -= increment; } text.Rectangle = rect; ExtendRectangle(text, graphics, ++recursionLevel); }
public void pinning_tests_for_text_measurer_centered_large_middle_string() { var fontMetrics = new MonospacedFontMetrics(new Point(2, 3)); var textMeasurer = new BoundedText( new Point(100, 200), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("Short top string\nlooooooooooong.... middle.... string\nshort bottom", fontMetrics, Color.White))); textMeasurer.TopLeftOfText().Should().Be(new Point(14, 96)); // textMeasurer.Lines.Should().HaveCount(3); // textMeasurer.Lines[0].TextContent.Should().Be("Short top string"); textMeasurer.GetRectOfLine(0).Location.X.Should().Be(34); textMeasurer.GetRectOfLine(0).Location.Y.Should().Be(96); // textMeasurer.Lines[1].TextContent.Should().Be("looooooooooong.... middle.... string"); textMeasurer.GetRectOfLine(1).Location.X.Should().Be(14); textMeasurer.GetRectOfLine(1).Location.Y.Should().Be(99); // textMeasurer.Lines[2].TextContent.Should().Be("short bottom"); textMeasurer.GetRectOfLine(2).Location.X.Should().Be(38); textMeasurer.GetRectOfLine(2).Location.Y.Should().Be(102); }
/// <summary> /// Draws text wrapped around path /// </summary> private static void DrawTextWrappedAroundPath() { using (var bitmap = new Bitmap(400, 400, PixelFormat.Format24bppRgb, RgbColor.White)) using (var graphics = bitmap.GetAdvancedGraphics()) { var dummyText = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " + "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim " + "veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo " + "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum " + "dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, " + "sunt in culpa qui officia deserunt mollit anim id est laborum."; var boundedText = new BoundedText(dummyText, graphics.CreateFont("Verdana", 18f), new SolidBrush(RgbColor.Black)) { Rectangle = new System.Drawing.RectangleF(20f, 20f, 360f, 360f) }; // Adding paths which you need to wrap with the text var wrappingPath = new Path(); wrappingPath.DrawEllipse(200, 80, 200, 200); boundedText.WrappingPaths.Add(wrappingPath); graphics.DrawText(boundedText); // Drawing frames around text blocks (for demonstration purposes) graphics.FillPath(new SolidBrush(RgbColor.LightGreen), wrappingPath); graphics.DrawRectangle(new Pen(RgbColor.Red), boundedText.Rectangle); bitmap.Save("../../../../_Output/DrawTextWrappedAroundPath.png"); } }
/// <summary> /// Draws multilanguage text with font fallback support /// </summary> private static void DrawTextWithFontFallback() { using (var bitmap = new Bitmap(400, 200, PixelFormat.Format24bppRgb, RgbColor.White)) using (var graphics = bitmap.GetAdvancedGraphics()) { var fontRegistry = new CustomFontRegistry(); fontRegistry.Add("../../../../_Input/Fonts/Lobster.ttf"); fontRegistry.Add("../../../../_Input/Fonts/ARIALUNI.TTF"); fontRegistry.FallbackFonts.Add("Arial Unicode MS"); graphics.FontRegistry = fontRegistry; var dummyText = "Lorem ipsum dolor sit amet, ex mel latine pertinax. 載自大制節規信兵著旋避漂。"; var boundedText = new BoundedText(dummyText, graphics.CreateFont("Lobster", 32f), new SolidBrush(RgbColor.Black)) { Rectangle = new System.Drawing.RectangleF(20f, 20f, 360f, 360f) }; graphics.DrawText(boundedText); bitmap.Save("../../../../_Output/DrawTextWithFontFallback.png"); } }
public void bounded_text_can_have_zero_fragments() { var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide); var textList = textMeasurer.GetRenderedText(); textList.Should().HaveCount(0); }
public void pinning_tests_for_text_measurer_bottomright() { var fontMetrics = new MonospacedFontMetrics(new Point(2, 3)); var textMeasurer = new BoundedText( new Point(100, 200), Alignment.BottomRight, Overflow.Elide, new FormattedText(new FormattedTextFragment("This is a very long string. I thought about referencing some meme here in this string.\nBut then I changed my mind.", fontMetrics, Color.White))); textMeasurer.TopLeftOfText().Should().Be(new Point(12, 191)); // textMeasurer.Lines.Should().HaveCount(3); // textMeasurer.Lines[0].TextContent.Should().Be("This is a very long string. I thought about "); textMeasurer.GetRectOfLine(0).Location.X.Should().Be(12); textMeasurer.GetRectOfLine(0).Location.Y.Should().Be(191); // textMeasurer.Lines[1].TextContent.Should().Be("referencing some meme here in this string."); textMeasurer.GetRectOfLine(1).Location.X.Should().Be(16); textMeasurer.GetRectOfLine(1).Location.Y.Should().Be(194); // textMeasurer.Lines[2].TextContent.Should().Be("But then I changed my mind."); textMeasurer.GetRectOfLine(2).Location.X.Should().Be(46); textMeasurer.GetRectOfLine(2).Location.Y.Should().Be(197); }
public static string DrawResult(BoundedText textMeasurer) { var drawPanel = new AsciiDrawPanel(textMeasurer.TotalAvailableSize); drawPanel.DrawRectangle(new Rectangle(Point.Zero, textMeasurer.TotalAvailableSize), '#'); return(DrawRenderedText(drawPanel, textMeasurer.GetRenderedText())); }
public void occlude_entire_last_token() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("This is a test", fontMetrics, Color.White))); var textList = textMeasurer.GetRenderedText(occludedCharactersCount: 4); textList.Should().HaveCount(6); textList[5].Text.Should().Be(" "); }
public void occlude_way_too_much() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("Hi", fontMetrics, Color.White))); var textList = textMeasurer.GetRenderedText(occludedCharactersCount: 999999); textList.Should().HaveCount(0); }
public void occlude_zero_characters_by_default() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("This is a test", fontMetrics, Color.White))); var textList = textMeasurer.GetRenderedText(); textList.Should().HaveCount(7); textList[6].Text.Should().Be("test"); }
public void occlude_half_of_string() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("Ragglest the Fragglest", fontMetrics, Color.White))); var textList = textMeasurer.GetRenderedText(occludedCharactersCount: 12); textList.Should().HaveCount(3); textList[2].Text.Should().Be("t"); }
public void text_rendering_pinning_approval() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("I'm the rootinest tootinest gunslinger on this here side of the mississouri.\n\nSo watch out!", fontMetrics, Color.White))); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void newlines_are_not_counted_as_rendered_text() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText(new FormattedTextFragment("New\nLine", fontMetrics, Color.White))); var renderedText = textMeasurer.GetRenderedText(); renderedText.Should().HaveCount(2); renderedText[0].Text.Should().Be("New"); renderedText[1].Text.Should().Be("Line"); }
public override void Draw(SpriteBatch spriteBatch) { List <RenderableText> renderableTexts = BoundedText.GetRenderedText(transform.Position.ToPoint(), this.boundingRect.TopLeft.ToPoint(), OccludedIndex); foreach (var renderableText in renderableTexts) { renderableText.Draw(spriteBatch, DrawOffset, transform.Angle, transform.Depth + this.depthOffset); if (this.isDropShadowEnabled) { renderableText.DrawDropShadow(spriteBatch, this.dropShadowColor, DrawOffset, transform.Angle, transform.Depth + this.depthOffset); } } }
public void elide_words_many_short_words() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(50, 30), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("I'm about to walk\noff the edge of the string\nhere I go!\na b c d e f g", font, Color.Orange) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void allow_free_overflow() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(25, 25), Alignment.Center, Overflow.Ignore, new FormattedText( new FormattedTextFragment("It is OK that we overflow here", font, Color.Orange) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void elide_one_line_long_word() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(50, 4), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("let's goooooooooooooo!", font, Color.Orange) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void several_linebreaks_back_to_back() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(50, 30), Alignment.TopLeft, Overflow.Elide, new FormattedText( new FormattedTextFragment("One line\nTwo lines\n\nThree lines\n\n\nDone!", font, Color.Orange) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void can_get_used_space_elided() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(25, 25), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("How\nbig\nis\nthis?", font, Color.Orange) ) ); textMeasurer.UsedSize.Should().Be(new Point(20, 16)); }
public void can_get_used_space_not_elided() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(25, 25), Alignment.Center, Overflow.Ignore, new FormattedText( new FormattedTextFragment("Really big string\nHow big is this?", font, Color.Orange) ) ); textMeasurer.UsedSize.Should().Be(new Point(24, 24)); }
public void can_get_used_space_with_extra_room() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(100, 100), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("Really big string\nHow big is this?", font, Color.Orange) ) ); textMeasurer.UsedSize.Should().Be(new Point(68, 8)); }
public void elide_words_narrow_box() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(10, 30), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("O Nothing fits in this box", font, Color.Orange) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void can_get_used_space_with_forced_linebreak() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(100, 100), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("Really loooooonnnng string that will linebreak\nHow big is this?", font, Color.Orange) ) ); textMeasurer.UsedSize.Should().Be(new Point(80, 16)); }
private static void MergeLayers(PsdReader psdReader, Graphics graphics, Func <PsdTextFrame, string> getLayerText) { // Merge layers for (int i = 0; i < psdReader.Frames.Count; i++) { var frame = psdReader.Frames[i]; if (frame.Type == FrameType.Text) { var textFrame = (PsdTextFrame)frame; var layerText = getLayerText(textFrame); Text text; var font = graphics.CreateFont(textFrame.FontName, textFrame.FontSize); if (textFrame.Raw.TextBox.Width == 0 || textFrame.Raw.TextBox.Height == 0) { text = new PlainText(layerText, font) { Position = new System.Drawing.PointF(textFrame.Raw.TextBox.Left, textFrame.Raw.TextBox.Top) }; } else { text = new BoundedText(layerText, font) { Rectangle = textFrame.Raw.TextBox }; } text.Alignment = JustificationToTextAlignment(textFrame.Justification); text.Brush = new SolidBrush(textFrame.Color); text.Transform = textFrame.Transform; graphics.DrawText(text); } else if (frame.Type == FrameType.Raster) { using (var frameBitmap = frame.GetBitmap()) { graphics.DrawImage(frameBitmap, frame.X, frame.Y); } } } }
public void elide_words_on_next_line_exact_length() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(50, 30), Alignment.Center, Overflow.Elide, new FormattedText( // the typo in "aand" is intentional here new FormattedTextFragment("I'm about to walk\noff the edge of the string\nhere I go! aand more words words words", font, Color.Orange) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void multi_line_with_image() { var font = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText( new Point(50, 30), Alignment.TopLeft, Overflow.Elide, new FormattedText( new FormattedTextFragment("Wordswords\n", font, Color.Orange), new ImageTextFragment(new Point(8, 8), null), new FormattedTextFragment("words words", font, Color.LightBlue) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void text_measurer_generates_accurate_output() { var textMeasurer = new BoundedText( new Point(200, 200), Alignment.Center, Overflow.Ignore, new FormattedText(new FormattedTextFragment("Hello world", new MonospacedFontMetrics(new Point(2, 3)), Color.White))); // textMeasurer.Lines.Should().HaveCount(1); var expectedX = 89; textMeasurer.GetRectOfLine(0).Location.X.Should().Be(expectedX); var localTextPos = textMeasurer.TopLeftOfText(); localTextPos.Should().Be(new Point(expectedX, 99)); }
public void changing_fonts_can_cause_natural_linebreaks() { var small = new MonospacedFontMetrics(new Point(4, 4)); var medium = new MonospacedFontMetrics(new Point(6, 6)); var textMeasurer = new BoundedText( new Point(50, 30), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("Changing fonts can cause a natural line", small, Color.White), new FormattedTextFragment("break", medium, Color.White) ) ); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }
public void bounded_text_can_have_multiple_fragments() { var fontMetrics = new MonospacedFontMetrics(new Point(4, 4)); var textMeasurer = new BoundedText(new Point(60, 40), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("He", fontMetrics, Color.White), new FormattedTextFragment("llo ", fontMetrics, Color.White), new FormattedTextFragment("World!", fontMetrics, Color.White) )); var textList = textMeasurer.GetRenderedText(); textList.Should().HaveCount(4); textList[0].Text.Should().Be("He"); textList[1].Text.Should().Be("llo"); textList[2].Text.Should().Be(" "); textList[3].Text.Should().Be("World!"); textMeasurer.UsedSize.Should().Be(new Point(48, 4)); }
public void bounded_text_can_support_different_font_sizes() { var small = new MonospacedFontMetrics(new Point(4, 4)); var tall = new MonospacedFontMetrics(new Point(4, 8)); var medium = new MonospacedFontMetrics(new Point(6, 6)); var big = new MonospacedFontMetrics(new Point(12, 12)); var textMeasurer = new BoundedText( new Point(82, 20), Alignment.Center, Overflow.Elide, new FormattedText( new FormattedTextFragment("Go", small, Color.White), new FormattedTextFragment("big", big, Color.White), new FormattedTextFragment("or", tall, Color.White), new FormattedTextFragment("go", medium, Color.White), new FormattedTextFragment("home", small, Color.White) )); Approvals.Verify(TextMeasureUtils.DrawResult(textMeasurer)); }