Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Alignment Issue #551

Open
ChandrasekarML opened this issue Feb 6, 2025 · 1 comment
Open

Text Alignment Issue #551

ChandrasekarML opened this issue Feb 6, 2025 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@ChandrasekarML
Copy link

"While changing the text value, OLDSTRING = 'TITLE1' and NEWSTRING = 'HELLO WORLD', we are experiencing an issue where the alignment point of the old data is assigned after the changes. The geometric text alignment point is not updated according to the new string's insertion point."

Please find the below sample code, and we are facing difficulties finding the alignment calculation for different font styles.

public static float MeasureTextWidth(string text, string fontName, float fontSize, float widthFactor)
{
// Create a System.Drawing.Font object based on the given font and size
using (System.Drawing.Font font = new System.Drawing.Font(fontName, fontSize))
using (Graphics g = Graphics.FromImage(new Bitmap(1, 1)))
{
// Set anti-aliasing for smoother text rendering
g.TextRenderingHint = TextRenderingHint.AntiAlias;

      // Measure the width of the string
      SizeF textSize = g.MeasureString(text, font);

      // Apply the width factor for scaling
      return textSize.Width * widthFactor;
  }

}

// OLDSTRING = TITLE1, NEWSTRING = HELLO WORLD

clonedEntity1.Value = NEWSTRING;
var insertionPoint = clonedEntity1.AlignmentPoint;
var height = clonedEntity1.Height;
var widthFactor = clonedEntity1.WidthFactor;
var fontName = clonedEntity1.Style?.Name ?? "Arial";

// Measure text width
// double textWidth = MeasureTextWidth(clonedEntity1.Value, fontName, (float)height) * widthFactor;
// Measure text size using System.Drawing
double textWidth = MeasureTextWidth(clonedEntity1.Value, clonedEntity1.Style?.Name ?? "Arial", (float)clonedEntity1.Height, (float)clonedEntity1.WidthFactor);

// Calculate center alignment
var maxPoint = new netDxf.Vector2(insertionPoint.X - (textWidth / 2), insertionPoint.Y);

// Adjust alignment based on font
double adjustment = clonedEntity1.Style.Name switch
{
"ROMANS" => 1.45,
"ARIAL" => 0.0485,
"ISOCP" => 0.35800,
_ => 0
};

// Set AlignmentPoint (NOT InsertPoint)
clonedEntity1.InsertPoint = new XYZ(maxPoint.X + adjustment, clonedEntity1.InsertPoint.Y, 0);
clonedEntity1.HorizontalAlignment = TextHorizontalAlignment.Center;
clonedEntity1.VerticalAlignment = TextVerticalAlignmentType.Middle;

@ChandrasekarML ChandrasekarML added the bug Something isn't working label Feb 6, 2025
@DomCR
Copy link
Owner

DomCR commented Feb 7, 2025

Hi @ChandrasekarML,

I'm sorry to inform you bur right now there is no support for this particular feature.

Please refer to this related issue: #535

@DomCR DomCR added duplicate This issue or pull request already exists and removed bug Something isn't working labels Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants