Skip to content

Commit

Permalink
update Width and Height of shape
Browse files Browse the repository at this point in the history
  • Loading branch information
ashahabov committed Nov 19, 2022
1 parent ffd7e00 commit 4c07fe6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 41 deletions.
13 changes: 6 additions & 7 deletions ShapeCrawler.Tests/ShapeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,16 @@ public void Id_returns_id()
id.Should().Be(9);
}

[Fact]
public void Y_Setter_updates_y_coordinate()
[Theory]
[SlideShapeData("001.pptx", 1, "TextBox 3")]
[SlideShapeData("001.pptx", 1, "Head 1")]
public void Y_Setter_sets_y_coordinate(IShape shape)
{
// Arrange
var autoShape = GetAutoShape("001.pptx", 1, 4);

// Act
autoShape.Y = 100;
shape.Y = 100;

// Assert
autoShape.Y.Should().Be(100);
shape.Y.Should().Be(100);
}

[Theory]
Expand Down
109 changes: 75 additions & 34 deletions ShapeCrawler/PowerPoint/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,81 @@ private void SetXCoordinate(int newXPixels)
aXfrm.Offset!.X = UnitConverter.HorizontalPixelToEmu(newXPixels);
}
}

private void SetYCoordinate(int newYPixels)
{
if (this.GroupShape is not null)
{
throw new RuntimeDefinedPropertyException("Y coordinate of grouped shape cannot be changed.");
}

var pSpPr = this.PShapeTreesChild.GetFirstChild<P.ShapeProperties>() !;
var aXfrm = pSpPr.Transform2D;
if (aXfrm is null)
{
var placeholder = (Placeholder)this.Placeholder!;
var referencedShape = placeholder.ReferencedShape;
var xEmu = UnitConverter.HorizontalPixelToEmu(referencedShape.X);
var yEmu = UnitConverter.HorizontalPixelToEmu(newYPixels);
var wEmu = UnitConverter.VerticalPixelToEmu(referencedShape.Width);
var hEmu = UnitConverter.VerticalPixelToEmu(referencedShape.Height);
pSpPr.AddAXfrm(xEmu, yEmu, wEmu, hEmu);
}
else
{
aXfrm.Offset!.Y = UnitConverter.HorizontalPixelToEmu(newYPixels);
}
}

private void SetHeight(int newHPixels)
{
if (this.GroupShape is not null)
{
throw new RuntimeDefinedPropertyException("Height coordinate of grouped shape cannot be changed.");
}

var pSpPr = this.PShapeTreesChild.GetFirstChild<P.ShapeProperties>() !;
var aXfrm = pSpPr.Transform2D;
if (aXfrm is null)
{
var placeholder = (Placeholder)this.Placeholder!;
var referencedShape = placeholder.ReferencedShape;
var xEmu = UnitConverter.HorizontalPixelToEmu(referencedShape.X);
var yEmu = UnitConverter.HorizontalPixelToEmu(referencedShape.X);
var wEmu = UnitConverter.VerticalPixelToEmu(referencedShape.Width);
var hEmu = UnitConverter.VerticalPixelToEmu(newHPixels);
pSpPr.AddAXfrm(xEmu, yEmu, wEmu, hEmu);
}
else
{
aXfrm.Extents!.Cy = UnitConverter.HorizontalPixelToEmu(newHPixels);
}
}

private void SetWidth(int newWPixels)
{
if (this.GroupShape is not null)
{
throw new RuntimeDefinedPropertyException("Width coordinate of grouped shape cannot be changed.");
}

var pSpPr = this.PShapeTreesChild.GetFirstChild<P.ShapeProperties>() !;
var aXfrm = pSpPr.Transform2D;
if (aXfrm is null)
{
var placeholder = (Placeholder)this.Placeholder!;
var referencedShape = placeholder.ReferencedShape;
var xEmu = UnitConverter.HorizontalPixelToEmu(referencedShape.X);
var yEmu = UnitConverter.HorizontalPixelToEmu(referencedShape.X);
var wEmu = UnitConverter.VerticalPixelToEmu(newWPixels);
var hEmu = UnitConverter.VerticalPixelToEmu(referencedShape.Height);
pSpPr.AddAXfrm(xEmu, yEmu, wEmu, hEmu);
}
else
{
aXfrm.Extents!.Cx = UnitConverter.HorizontalPixelToEmu(newWPixels);
}
}

private int GetXCoordinate()
{
Expand All @@ -179,18 +254,6 @@ private int GetXCoordinate()
return UnitConverter.HorizontalEmuToPixel(xEmu);
}

private void SetYCoordinate(long value)
{
if (this.GroupShape is not null)
{
throw new RuntimeDefinedPropertyException("Y coordinate of grouped shape cannot be changed.");
}

var aOffset = this.PShapeTreesChild.Descendants<A.Offset>().First();

aOffset.Y = UnitConverter.VerticalPixelToEmu(value);
}

private int GetYCoordinate()
{
var aOffset = this.PShapeTreesChild.Descendants<A.Offset>().FirstOrDefault();
Expand Down Expand Up @@ -223,17 +286,6 @@ private int GetWidthPixels()
return UnitConverter.HorizontalEmuToPixel(aExtents.Cx!);
}

private void SetWidth(int pixels)
{
var aExtents = this.PShapeTreesChild.Descendants<A.Extents>().FirstOrDefault();
if (aExtents == null)
{
throw new PlaceholderCannotBeChangedException();
}

aExtents.Cx = UnitConverter.HorizontalPixelToEmu(pixels);
}

private int GetHeightPixels()
{
var aExtents = this.PShapeTreesChild.Descendants<A.Extents>().FirstOrDefault();
Expand All @@ -245,17 +297,6 @@ private int GetHeightPixels()
return UnitConverter.VerticalEmuToPixel(aExtents!.Cy!);
}

private void SetHeight(int pixels)
{
var aExtents = this.PShapeTreesChild.Descendants<A.Extents>().FirstOrDefault();
if (aExtents == null)
{
throw new PlaceholderCannotBeChangedException();
}

aExtents.Cy = UnitConverter.VerticalPixelToEmu(pixels);
}

private SCGeometry GetGeometryType()
{
var spPr = this.PShapeTreesChild.Descendants<P.ShapeProperties>().First(); // TODO: optimize
Expand Down

0 comments on commit 4c07fe6

Please sign in to comment.