Skip to content

Commit 53152b5

Browse files
Copilotjongalloway
andauthored
Apply alignment xAlign to template residual path; add geometry assertions to alignment tests
Agent-Logs-Url: https://github.com/jongalloway/MarpToPptx/sessions/b58f9402-a58a-4139-b0fa-95eb48c28bcb Co-authored-by: jongalloway <68539+jongalloway@users.noreply.github.com>
1 parent f72c11f commit 53152b5

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/MarpToPptx.Pptx/Rendering/OpenXmlPptxRenderer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,8 +1320,14 @@ element is HeadingElement h &&
13201320
}
13211321
break;
13221322
case ImageElement image:
1323+
var residualXAlign = image.Alignment switch
1324+
{
1325+
"left" => 0.0,
1326+
"right" => 1.0,
1327+
_ => 0.5,
1328+
};
13231329
AddImage(context, frame, image.Source, image.AltText, image.Caption,
1324-
explicitWidth: image.ExplicitWidth, explicitHeight: image.ExplicitHeight, sizePercent: image.SizePercent);
1330+
xAlign: residualXAlign, explicitWidth: image.ExplicitWidth, explicitHeight: image.ExplicitHeight, sizePercent: image.SizePercent);
13251331
break;
13261332
case VideoElement video:
13271333
AddVideo(context, frame, video.Source, video.AltText);

tests/MarpToPptx.Tests/PptxRendererTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,10 @@ public void Renderer_MarpitLeftAlignmentKeyword_RendersImageAtLeftEdgeOfFrame()
948948
// Marpit: ![left](image.jpg) floats the image to the left of the content area.
949949
// The "left" keyword is stripped from alt text; the image is placed at the left
950950
// edge of its allocated content frame (xAlign = 0.0).
951+
//
952+
// With default theme padding (Left=60 lu) and a 1×1 image in a 840×220 lu frame,
953+
// CalculateImagePlacement gives X = 60 lu, width = height = 220 lu.
954+
// In EMU (×12700): X = 762000, Cx = Cy = 2794000.
951955
using var workspace = TestWorkspace.Create();
952956

953957
workspace.WriteFile("photo.png", Convert.FromBase64String(OnePxPngBase64));
@@ -971,6 +975,12 @@ public void Renderer_MarpitLeftAlignmentKeyword_RendersImageAtLeftEdgeOfFrame()
971975
var picture = Assert.Single(slidePart.Slide!.Descendants<P.Picture>());
972976
var description = picture.NonVisualPictureProperties?.NonVisualDrawingProperties?.Description?.Value;
973977
Assert.Equal(string.Empty, description);
978+
979+
// Left alignment: image X should be at the content frame's left edge (frame.X = 60 lu = 762000 EMU).
980+
var transform = picture.ShapeProperties!.GetFirstChild<A.Transform2D>()!;
981+
Assert.Equal(762000L, transform.Offset!.X!.Value);
982+
Assert.Equal(2794000L, transform.Extents!.Cx!.Value);
983+
Assert.Equal(2794000L, transform.Extents!.Cy!.Value);
974984
}
975985

976986
[Fact]
@@ -979,6 +989,10 @@ public void Renderer_MarpitRightAlignmentKeyword_RendersImageAtRightEdgeOfFrame(
979989
// Marpit: ![right](image.jpg) floats the image to the right of the content area.
980990
// The "right" keyword is stripped from alt text; the image is placed at the right
981991
// edge of its allocated content frame (xAlign = 1.0).
992+
//
993+
// With default theme padding (Left=60 lu) and a 1×1 image in an 840×220 lu frame,
994+
// gapX = 840 - 220 = 620 lu, so X = 60 + 620 = 680 lu → 8636000 EMU.
995+
// Width = height = 220 lu → 2794000 EMU.
982996
using var workspace = TestWorkspace.Create();
983997

984998
workspace.WriteFile("photo.png", Convert.FromBase64String(OnePxPngBase64));
@@ -1002,6 +1016,13 @@ public void Renderer_MarpitRightAlignmentKeyword_RendersImageAtRightEdgeOfFrame(
10021016
var picture = Assert.Single(slidePart.Slide!.Descendants<P.Picture>());
10031017
var description = picture.NonVisualPictureProperties?.NonVisualDrawingProperties?.Description?.Value;
10041018
Assert.Equal(string.Empty, description);
1019+
1020+
// Right alignment: gapX = frameWidth - fittedWidth = 840 - 220 = 620 lu,
1021+
// so X = frame.X + gapX = 60 + 620 = 680 lu → 8636000 EMU.
1022+
var transform = picture.ShapeProperties!.GetFirstChild<A.Transform2D>()!;
1023+
Assert.Equal(8636000L, transform.Offset!.X!.Value);
1024+
Assert.Equal(2794000L, transform.Extents!.Cx!.Value);
1025+
Assert.Equal(2794000L, transform.Extents!.Cy!.Value);
10051026
}
10061027

10071028
[Fact]

0 commit comments

Comments
 (0)