Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
vmrocha  
#1 Posted : Sunday, January 10, 2016 5:28:37 PM(UTC)
vmrocha

Rank: Member

Groups: Member
Joined: 1/10/2016(UTC)
Posts: 15

Thanks: 7 times
When I create an Art Text, for example a BridgeText and apply a Shadow effect to it, it does not take into consideration the effect. Is there a way to calculate the area affected by the shadow?

Code:
var artText = new BridgeText(txtText.Text, font, brush, 0, 0, intensity);
artText.Effect = new Shadow(RgbColor.Black, 2, 5, 10, 1);
var blackBox = artText.GetBlackBox();


// blackBox does not include the shadow effect.
Fedor  
#2 Posted : Monday, January 11, 2016 5:31:04 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
The GetBlackBox method returns an exact size of the text, i.e. it interprets the text as vector data. That's why the coordinates have float type - the black box may be located “between” pixels, etc.

Unlike it, the shadow effect is applied to the raster, after the vector data is rasterized. So we don’t know the exact size of a shadow before the image is flattened. That's why we ignore effects when calculating the blackbox.

However, you can still estimate the text size with the shadow effect (+/- couple pixels). The following code should give you an idea how to do it:

Code:
using System;
using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.AdvancedDrawing.Art;
using Aurigma.GraphicsMill.AdvancedDrawing.Effects;
using Aurigma.GraphicsMill.AdvancedDrawing;


namespace Forum_13972
{
	class Program
	{
		static void Main(string[] args)
		{
			var text = "Hello World!";
			var intensity = 0.2f;

			using (var bitmap = new Bitmap(600, 200, PixelFormat.Format24bppRgb, new RgbColor(0xff, 0xff, 0xff, 0xff)))
			using (var graphics = bitmap.GetAdvancedGraphics())
			{
				var font = graphics.CreateFont("Tahoma", 80);
				var brush = new SolidBrush(RgbColor.Blue);

				var artText = new BridgeText(text, font, brush, 0, 0, intensity);

				var shadowSize = 5f;
				var shadowOffsetX = 20f;
				var shadowOffsetY = 15f;

				artText.Effect = new Shadow(RgbColor.Red, shadowSize, shadowOffsetX, shadowOffsetY, 1);
				artText.Center = new System.Drawing.PointF(300f, 100f);

				graphics.DrawText(artText);

				//Text blackbox
				var textBlackBox = artText.GetBlackBox();
				graphics.DrawRectangle(new Pen(RgbColor.Black, 1f), textBlackBox);

				//Shadow blackbox
				var shadowBlackBox = new System.Drawing.RectangleF(
					textBlackBox.X - shadowSize + shadowOffsetX,
					textBlackBox.Y - shadowSize + shadowOffsetY,
					textBlackBox.Width + shadowSize * 2,
					textBlackBox.Height + shadowSize * 2);

				graphics.DrawRectangle(new Pen(RgbColor.Red, 1f), shadowBlackBox);

				//Union blackbox
				var unionBlackbox = System.Drawing.RectangleF.Union(textBlackBox, shadowBlackBox);

				graphics.DrawRectangle(new Pen(RgbColor.Green, 1f), unionBlackbox);

				bitmap.Save("../../../ArtText.png");
			}
		}
	}
}

Fedor attached the following image(s):
ArtText.png
Best regards,
Fedor Skvortsov
vmrocha  
#3 Posted : Monday, January 11, 2016 8:00:22 AM(UTC)
vmrocha

Rank: Member

Groups: Member
Joined: 1/10/2016(UTC)
Posts: 15

Thanks: 7 times
Hi Fedor,

Thanks for your reply.

The code that you sent works with bitmaps, but it does not work with PDFs. Specially when I increase the DPI. Do you think that the behavior is different for PDF files or I am missing something? Do you have any suggestion?

Here is the code that I wrote based on what you sent:

Code:
using System.Diagnostics;
using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.AdvancedDrawing;
using Aurigma.GraphicsMill.AdvancedDrawing.Art;
using Aurigma.GraphicsMill.AdvancedDrawing.Effects;
using Aurigma.GraphicsMill.Codecs;

namespace GraphicsMillSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var text = "Hello World!";
            var intensity = 0.2f;
            var targetFile = ".\\test.pdf";
            var width = 1000;
            var height = 300;
            int dpi = 150;

            using (var pdfWriter = new PdfWriter(targetFile))
            {
                pdfWriter.AddPage(width, height, dpi, dpi);

                using (var graphics = pdfWriter.GetGraphics())
                {
                    var font = graphics.CreateFont("Tahoma", 80);
                    var brush = new SolidBrush(RgbColor.Blue);

                    var artText = new BridgeText(text, font, brush, 0, 0, intensity);

                    var shadowSize = 5f;
                    var shadowOffsetX = 20f;
                    var shadowOffsetY = 15f;

                    artText.Effect = new Shadow(RgbColor.Red, shadowSize, shadowOffsetX, shadowOffsetY, 1);
                    artText.Center = new System.Drawing.PointF(width / 2, height / 2);

                    graphics.DrawText(artText);

                    //Text blackbox
                    var textBlackBox = artText.GetBlackBox();
                    graphics.DrawRectangle(new Pen(RgbColor.Black, 1f), textBlackBox);

                    //Shadow blackbox
                    var shadowBlackBox = new System.Drawing.RectangleF(
                        textBlackBox.X - shadowSize + shadowOffsetX,
                        textBlackBox.Y - shadowSize + shadowOffsetY,
                        textBlackBox.Width + shadowSize * 2,
                        textBlackBox.Height + shadowSize * 2);

                    graphics.DrawRectangle(new Pen(RgbColor.Red, 1f), shadowBlackBox);

                    //Union blackbox
                    var unionBlackbox = System.Drawing.RectangleF.Union(textBlackBox, shadowBlackBox);

                    graphics.DrawRectangle(new Pen(RgbColor.Green, 1f), unionBlackbox);
                }
            }

            Process.Start(targetFile);
        }
    }
}
Fedor  
#4 Posted : Monday, January 11, 2016 9:05:26 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
I confirm the problem. The sample works correctly with DPI = 72 only.

Our engineer will check it today. I will keep you updated.
Best regards,
Fedor Skvortsov
thanks 1 user thanked Fedor for this useful post.
vmrocha on 1/11/2016(UTC)
Fedor  
#5 Posted : Monday, January 11, 2016 9:24:32 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
The Shadow.Size, Shadow.OffsetX, and Shadow.OffsetY properties are in points (not in pixels). That's why it works correctly with DPI = 72 only.

To fix the problem you should convert points to pixels:

Code:
using System;
using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.AdvancedDrawing.Art;
using Aurigma.GraphicsMill.AdvancedDrawing.Effects;
using Aurigma.GraphicsMill.AdvancedDrawing;
using Aurigma.GraphicsMill.Codecs;


namespace Forum_13972
{
	class Program
	{
		static void Main(string[] args)
		{
			var text = "Hello World!";
			var intensity = 0.2f;
			var targetFile = "../../../ArtText.pdf";
			var width = 1000;
			var height = 300;
			int dpi = 150;

			using (var pdfWriter = new PdfWriter(targetFile))
			{
				pdfWriter.AddPage(width, height, dpi, dpi);

				using (var graphics = pdfWriter.GetGraphics())
				{
					var font = graphics.CreateFont("Tahoma", 80);
					var brush = new SolidBrush(RgbColor.Blue);

					var artText = new BridgeText(text, font, brush, 0, 0, intensity);

					var shadowSizePt = 5f;
					var shadowOffsetXPt = 20f;
					var shadowOffsetYPt = 15f;

					artText.Effect = new Shadow(RgbColor.Red, shadowSizePt, shadowOffsetXPt, shadowOffsetYPt, 1);
					artText.Center = new System.Drawing.PointF(width / 2, height / 2);

					graphics.DrawText(artText);

					//Text blackbox
					var textBlackBox = artText.GetBlackBox();
					graphics.DrawRectangle(new Pen(RgbColor.Black, 1f), textBlackBox);

					var shadowSizePx = UnitConverter.ConvertUnitsToPixels(dpi, shadowSizePt, Unit.Point);
					var shadowOffsetXPx = UnitConverter.ConvertUnitsToPixels(dpi, shadowOffsetXPt, Unit.Point);
					var shadowOffsetYPx = UnitConverter.ConvertUnitsToPixels(dpi, shadowOffsetYPt, Unit.Point);

					//Shadow blackbox
					var shadowBlackBox = new System.Drawing.RectangleF(
						textBlackBox.X - shadowSizePx + shadowOffsetXPx,
						textBlackBox.Y - shadowSizePx + shadowOffsetYPx,
						textBlackBox.Width + shadowSizePx * 2,
						textBlackBox.Height + shadowSizePx * 2);

					graphics.DrawRectangle(new Pen(RgbColor.Red, 1f), shadowBlackBox);

					//Union blackbox
					var unionBlackbox = System.Drawing.RectangleF.Union(textBlackBox, shadowBlackBox);

					graphics.DrawRectangle(new Pen(RgbColor.Green, 1f), unionBlackbox);
				}
			}
		}
	}
}

Edited by user Monday, January 11, 2016 9:25:18 PM(UTC)  | Reason: Not specified

Best regards,
Fedor Skvortsov
thanks 1 user thanked Fedor for this useful post.
vmrocha on 1/12/2016(UTC)
vmrocha  
#6 Posted : Tuesday, January 12, 2016 10:58:49 AM(UTC)
vmrocha

Rank: Member

Groups: Member
Joined: 1/10/2016(UTC)
Posts: 15

Thanks: 7 times
Thank you, that worked for me! Dancing
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.