@@ -14,7 +14,6 @@ package excelize
1414import (
1515 "bytes"
1616 "encoding/xml"
17- "math"
1817 "strconv"
1918 "strings"
2019
@@ -23,10 +22,10 @@ import (
2322
2423// Define the default cell size and EMU unit of measurement.
2524const (
26- defaultColWidth float64 = 9.140625
27- defaultColWidthPixels float64 = 64
28- defaultRowHeight float64 = 15
29- defaultRowHeightPixels float64 = 20
25+ defaultColWidth float64 = 10.5
26+ defaultColWidthPixels float64 = 84.0
27+ defaultRowHeight float64 = 15.6
28+ defaultRowHeightPixels float64 = 20.8
3029 EMU int = 9525
3130)
3231
@@ -621,40 +620,35 @@ func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol)
621620//
622621// width # Width of object frame.
623622// height # Height of object frame.
624- func (f * File ) positionObjectPixels (sheet string , col , row , x1 , y1 , width , height int ) (int , int , int , int , int , int ) {
623+ func (f * File ) positionObjectPixels (sheet string , col , row , width , height int , opts * GraphicOptions ) (int , int , int , int , int , int , int , int ) {
625624 colIdx , rowIdx := col - 1 , row - 1
626- // Adjust start column for offsets that are greater than the col width.
627- for x1 >= f .getColWidth (sheet , colIdx + 1 ) {
628- colIdx ++
629- x1 -= f .getColWidth (sheet , colIdx )
630- }
631-
632- // Adjust start row for offsets that are greater than the row height.
633- for y1 >= f .getRowHeight (sheet , rowIdx + 1 ) {
634- rowIdx ++
635- y1 -= f .getRowHeight (sheet , rowIdx )
636- }
637-
638625 // Initialized end cell to the same as the start cell.
639626 colEnd , rowEnd := colIdx , rowIdx
627+ x1 , y1 , x2 , y2 := opts .OffsetX , opts .OffsetY , width , height
628+ if opts .Positioning == "" || opts .Positioning == "twoCell" {
629+ // Using a twoCellAnchor, the maximum possible offset is limited by the
630+ // "from" cell dimensions. If these were to be exceeded the "toPoint" would
631+ // be calculated incorrectly, since the requested "fromPoint" is not possible
632+
633+ x1 = min (x1 , f .getColWidth (sheet , col ))
634+ y1 = min (y1 , f .getRowHeight (sheet , row ))
635+
636+ x2 += x1
637+ y2 += y1
638+ // Subtract the underlying cell widths to find end cell of the object.
639+ for x2 >= f .getColWidth (sheet , colEnd + 1 ) {
640+ colEnd ++
641+ x2 -= f .getColWidth (sheet , colEnd )
642+ }
640643
641- width += x1
642- height += y1
643-
644- // Subtract the underlying cell widths to find end cell of the object.
645- for width >= f .getColWidth (sheet , colEnd + 1 ) {
646- colEnd ++
647- width -= f .getColWidth (sheet , colEnd )
648- }
649-
650- // Subtract the underlying cell heights to find end cell of the object.
651- for height >= f .getRowHeight (sheet , rowEnd + 1 ) {
652- rowEnd ++
653- height -= f .getRowHeight (sheet , rowEnd )
644+ // Subtract the underlying cell heights to find end cell of the object.
645+ for y2 >= f .getRowHeight (sheet , rowEnd + 1 ) {
646+ rowEnd ++
647+ y2 -= f .getRowHeight (sheet , rowEnd )
648+ }
654649 }
655-
656650 // The end vertices are whatever is left from the width and height.
657- return colIdx , rowIdx , colEnd , rowEnd , width , height
651+ return colIdx , rowIdx , colEnd , rowEnd , x1 , y1 , x2 , y2
658652}
659653
660654// getColWidth provides a function to get column width in pixels by given
@@ -664,13 +658,14 @@ func (f *File) getColWidth(sheet string, col int) int {
664658 ws .mu .Lock ()
665659 defer ws .mu .Unlock ()
666660 if ws .Cols != nil {
667- var width float64
661+ width := - 1.0
668662 for _ , v := range ws .Cols .Col {
669663 if v .Min <= col && col <= v .Max && v .Width != nil {
670664 width = * v .Width
665+ break
671666 }
672667 }
673- if width != 0 {
668+ if width != - 1. 0 {
674669 return int (convertColWidthToPixels (width ))
675670 }
676671 }
@@ -801,16 +796,15 @@ func (f *File) RemoveCol(sheet, col string) error {
801796// pixel. If the width hasn't been set by the user we use the default value.
802797// If the column is hidden it has a value of zero.
803798func convertColWidthToPixels (width float64 ) float64 {
804- var padding float64 = 5
805799 var pixels float64
806- var maxDigitWidth float64 = 7
800+ var maxDigitWidth float64 = 8
807801 if width == 0 {
808802 return pixels
809803 }
810804 if width < 1 {
811805 pixels = (width * 12 ) + 0.5
812- return math . Ceil ( pixels )
806+ return float64 ( int ( pixels ) )
813807 }
814- pixels = (width * maxDigitWidth + 0.5 ) + padding
815- return math . Ceil ( pixels )
808+ pixels = (width * maxDigitWidth + 0.5 )
809+ return float64 ( int ( pixels ) )
816810}
0 commit comments