Skip to content

Commit

Permalink
Remove unnecessary x/y calculations for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Melnitskiy committed Sep 12, 2020
1 parent 1111de2 commit 2bbe157
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 24 deletions.
23 changes: 2 additions & 21 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,26 +556,7 @@ func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol)
// width # Width of object frame.
// height # Height of object frame.
//
// xAbs # Absolute distance to left side of object.
// yAbs # Absolute distance to top side of object.
//
func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
xAbs := 0
yAbs := 0

// Calculate the absolute x offset of the top-left vertex.
for colID := 1; colID <= col; colID++ {
xAbs += f.getColWidth(sheet, colID)
}
xAbs += x1

// Calculate the absolute y offset of the top-left vertex.
// Store the column change to allow optimisations.
for rowID := 1; rowID <= row; rowID++ {
yAbs += f.getRowHeight(sheet, rowID)
}
yAbs += y1

func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int) {
// Adjust start column for offsets that are greater than the col width.
for x1 >= f.getColWidth(sheet, col) {
x1 -= f.getColWidth(sheet, col)
Expand Down Expand Up @@ -610,7 +591,7 @@ func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, heigh
// The end vertices are whatever is left from the width and height.
x2 := width
y2 := height
return col, row, xAbs, yAbs, colEnd, rowEnd, x2, y2
return col, row, colEnd, rowEnd, x2, y2
}

// getColWidth provides a function to get column width in pixels by given
Expand Down
2 changes: 1 addition & 1 deletion drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI

width = int(float64(width) * formatSet.XScale)
height = int(float64(height) * formatSet.YScale)
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
colStart, rowStart, colEnd, rowEnd, x2, y2 :=
f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.OffsetX, formatSet.OffsetY, width, height)
content, cNvPrID := f.drawingParser(drawingXML)
twoCellAnchor := xdrCellAnchor{}
Expand Down
2 changes: 1 addition & 1 deletion picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
}
col--
row--
colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
colStart, rowStart, colEnd, rowEnd, x2, y2 :=
f.positionObjectPixels(sheet, col, row, formatSet.OffsetX, formatSet.OffsetY, width, height)
content, cNvPrID := f.drawingParser(drawingXML)
twoCellAnchor := xdrCellAnchor{}
Expand Down
2 changes: 1 addition & 1 deletion shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
width := int(float64(formatSet.Width) * formatSet.Format.XScale)
height := int(float64(formatSet.Height) * formatSet.Format.YScale)

colStart, rowStart, _, _, colEnd, rowEnd, x2, y2 :=
colStart, rowStart, colEnd, rowEnd, x2, y2 :=
f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.Format.OffsetX, formatSet.Format.OffsetY,
width, height)
content, cNvPrID := f.drawingParser(drawingXML)
Expand Down

0 comments on commit 2bbe157

Please sign in to comment.