col.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.10 or later.
  11. package excelize
  12. import (
  13. "bytes"
  14. "encoding/xml"
  15. "errors"
  16. "math"
  17. "strconv"
  18. "strings"
  19. "github.com/mohae/deepcopy"
  20. )
  21. // Define the default cell size and EMU unit of measurement.
  22. const (
  23. defaultColWidthPixels float64 = 64
  24. defaultRowHeightPixels float64 = 20
  25. EMU int = 9525
  26. )
  27. // Cols defines an iterator to a sheet
  28. type Cols struct {
  29. err error
  30. curCol, totalCol, stashCol, totalRow int
  31. sheet string
  32. cols []xlsxCols
  33. f *File
  34. sheetXML []byte
  35. }
  36. // GetCols return all the columns in a sheet by given worksheet name (case
  37. // sensitive). For example:
  38. //
  39. // cols, err := f.Cols("Sheet1")
  40. // if err != nil {
  41. // fmt.Println(err)
  42. // return
  43. // }
  44. // for cols.Next() {
  45. // col, err := cols.Rows()
  46. // if err != nil {
  47. // fmt.Println(err)
  48. // }
  49. // for _, rowCell := range col {
  50. // fmt.Print(rowCell, "\t")
  51. // }
  52. // fmt.Println()
  53. // }
  54. //
  55. func (f *File) GetCols(sheet string) ([][]string, error) {
  56. cols, err := f.Cols(sheet)
  57. if err != nil {
  58. return nil, err
  59. }
  60. results := make([][]string, 0, 64)
  61. for cols.Next() {
  62. col, _ := cols.Rows()
  63. results = append(results, col)
  64. }
  65. return results, nil
  66. }
  67. // Next will return true if the next col element is found.
  68. func (cols *Cols) Next() bool {
  69. cols.curCol++
  70. return cols.curCol <= cols.totalCol
  71. }
  72. // Error will return an error when the next col element is found.
  73. func (cols *Cols) Error() error {
  74. return cols.err
  75. }
  76. // Rows return the current column's row values.
  77. func (cols *Cols) Rows() ([]string, error) {
  78. var (
  79. err error
  80. inElement string
  81. cellCol, cellRow int
  82. rows []string
  83. )
  84. if cols.stashCol >= cols.curCol {
  85. return rows, err
  86. }
  87. d := cols.f.sharedStringsReader()
  88. decoder := cols.f.xmlNewDecoder(bytes.NewReader(cols.sheetXML))
  89. for {
  90. token, _ := decoder.Token()
  91. if token == nil {
  92. break
  93. }
  94. switch startElement := token.(type) {
  95. case xml.StartElement:
  96. inElement = startElement.Name.Local
  97. if inElement == "c" {
  98. for _, attr := range startElement.Attr {
  99. if attr.Name.Local == "r" {
  100. if cellCol, cellRow, err = CellNameToCoordinates(attr.Value); err != nil {
  101. return rows, err
  102. }
  103. blank := cellRow - len(rows)
  104. for i := 1; i < blank; i++ {
  105. rows = append(rows, "")
  106. }
  107. if cellCol == cols.curCol {
  108. colCell := xlsxC{}
  109. _ = decoder.DecodeElement(&colCell, &startElement)
  110. val, _ := colCell.getValueFrom(cols.f, d)
  111. rows = append(rows, val)
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return rows, nil
  119. }
  120. // Cols returns a columns iterator, used for streaming/reading data for a
  121. // worksheet with a large data. For example:
  122. //
  123. // cols, err := f.Cols("Sheet1")
  124. // if err != nil {
  125. // fmt.Println(err)
  126. // return
  127. // }
  128. // for cols.Next() {
  129. // col, err := cols.Rows()
  130. // if err != nil {
  131. // fmt.Println(err)
  132. // }
  133. // for _, rowCell := range col {
  134. // fmt.Print(rowCell, "\t")
  135. // }
  136. // fmt.Println()
  137. // }
  138. //
  139. func (f *File) Cols(sheet string) (*Cols, error) {
  140. name, ok := f.sheetMap[trimSheetName(sheet)]
  141. if !ok {
  142. return nil, ErrSheetNotExist{sheet}
  143. }
  144. if f.Sheet[name] != nil {
  145. output, _ := xml.Marshal(f.Sheet[name])
  146. f.saveFileList(name, replaceRelationshipsNameSpaceBytes(output))
  147. }
  148. var (
  149. inElement string
  150. cols Cols
  151. cellCol int
  152. err error
  153. )
  154. cols.sheetXML = f.readXML(name)
  155. decoder := f.xmlNewDecoder(bytes.NewReader(cols.sheetXML))
  156. for {
  157. token, _ := decoder.Token()
  158. if token == nil {
  159. break
  160. }
  161. switch startElement := token.(type) {
  162. case xml.StartElement:
  163. inElement = startElement.Name.Local
  164. if inElement == "row" {
  165. for _, attr := range startElement.Attr {
  166. if attr.Name.Local == "r" {
  167. if cols.totalRow, err = strconv.Atoi(attr.Value); err != nil {
  168. return &cols, err
  169. }
  170. }
  171. }
  172. }
  173. if inElement == "c" {
  174. for _, attr := range startElement.Attr {
  175. if attr.Name.Local == "r" {
  176. if cellCol, _, err = CellNameToCoordinates(attr.Value); err != nil {
  177. return &cols, err
  178. }
  179. if cellCol > cols.totalCol {
  180. cols.totalCol = cellCol
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. cols.f = f
  188. cols.sheet = trimSheetName(sheet)
  189. return &cols, nil
  190. }
  191. // GetColVisible provides a function to get visible of a single column by given
  192. // worksheet name and column name. For example, get visible state of column D
  193. // in Sheet1:
  194. //
  195. // visible, err := f.GetColVisible("Sheet1", "D")
  196. //
  197. func (f *File) GetColVisible(sheet, col string) (bool, error) {
  198. visible := true
  199. colNum, err := ColumnNameToNumber(col)
  200. if err != nil {
  201. return visible, err
  202. }
  203. xlsx, err := f.workSheetReader(sheet)
  204. if err != nil {
  205. return false, err
  206. }
  207. if xlsx.Cols == nil {
  208. return visible, err
  209. }
  210. for c := range xlsx.Cols.Col {
  211. colData := &xlsx.Cols.Col[c]
  212. if colData.Min <= colNum && colNum <= colData.Max {
  213. visible = !colData.Hidden
  214. }
  215. }
  216. return visible, err
  217. }
  218. // SetColVisible provides a function to set visible columns by given worksheet
  219. // name, columns range and visibility.
  220. //
  221. // For example hide column D on Sheet1:
  222. //
  223. // err := f.SetColVisible("Sheet1", "D", false)
  224. //
  225. // Hide the columns from D to F (included):
  226. //
  227. // err := f.SetColVisible("Sheet1", "D:F", false)
  228. //
  229. func (f *File) SetColVisible(sheet, columns string, visible bool) error {
  230. var max int
  231. colsTab := strings.Split(columns, ":")
  232. min, err := ColumnNameToNumber(colsTab[0])
  233. if err != nil {
  234. return err
  235. }
  236. if len(colsTab) == 2 {
  237. max, err = ColumnNameToNumber(colsTab[1])
  238. if err != nil {
  239. return err
  240. }
  241. } else {
  242. max = min
  243. }
  244. if max < min {
  245. min, max = max, min
  246. }
  247. xlsx, err := f.workSheetReader(sheet)
  248. if err != nil {
  249. return err
  250. }
  251. colData := xlsxCol{
  252. Min: min,
  253. Max: max,
  254. Width: 9, // default width
  255. Hidden: !visible,
  256. CustomWidth: true,
  257. }
  258. if xlsx.Cols == nil {
  259. cols := xlsxCols{}
  260. cols.Col = append(cols.Col, colData)
  261. xlsx.Cols = &cols
  262. return nil
  263. }
  264. xlsx.Cols.Col = flatCols(colData, xlsx.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  265. fc.BestFit = c.BestFit
  266. fc.Collapsed = c.Collapsed
  267. fc.CustomWidth = c.CustomWidth
  268. fc.OutlineLevel = c.OutlineLevel
  269. fc.Phonetic = c.Phonetic
  270. fc.Style = c.Style
  271. fc.Width = c.Width
  272. return fc
  273. })
  274. return nil
  275. }
  276. // GetColOutlineLevel provides a function to get outline level of a single
  277. // column by given worksheet name and column name. For example, get outline
  278. // level of column D in Sheet1:
  279. //
  280. // level, err := f.GetColOutlineLevel("Sheet1", "D")
  281. //
  282. func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
  283. level := uint8(0)
  284. colNum, err := ColumnNameToNumber(col)
  285. if err != nil {
  286. return level, err
  287. }
  288. xlsx, err := f.workSheetReader(sheet)
  289. if err != nil {
  290. return 0, err
  291. }
  292. if xlsx.Cols == nil {
  293. return level, err
  294. }
  295. for c := range xlsx.Cols.Col {
  296. colData := &xlsx.Cols.Col[c]
  297. if colData.Min <= colNum && colNum <= colData.Max {
  298. level = colData.OutlineLevel
  299. }
  300. }
  301. return level, err
  302. }
  303. // SetColOutlineLevel provides a function to set outline level of a single
  304. // column by given worksheet name and column name. The value of parameter
  305. // 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2:
  306. //
  307. // err := f.SetColOutlineLevel("Sheet1", "D", 2)
  308. //
  309. func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
  310. if level > 7 || level < 1 {
  311. return errors.New("invalid outline level")
  312. }
  313. colNum, err := ColumnNameToNumber(col)
  314. if err != nil {
  315. return err
  316. }
  317. colData := xlsxCol{
  318. Min: colNum,
  319. Max: colNum,
  320. OutlineLevel: level,
  321. CustomWidth: true,
  322. }
  323. xlsx, err := f.workSheetReader(sheet)
  324. if err != nil {
  325. return err
  326. }
  327. if xlsx.Cols == nil {
  328. cols := xlsxCols{}
  329. cols.Col = append(cols.Col, colData)
  330. xlsx.Cols = &cols
  331. return err
  332. }
  333. xlsx.Cols.Col = flatCols(colData, xlsx.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  334. fc.BestFit = c.BestFit
  335. fc.Collapsed = c.Collapsed
  336. fc.CustomWidth = c.CustomWidth
  337. fc.Hidden = c.Hidden
  338. fc.Phonetic = c.Phonetic
  339. fc.Style = c.Style
  340. fc.Width = c.Width
  341. return fc
  342. })
  343. return err
  344. }
  345. // SetColStyle provides a function to set style of columns by given worksheet
  346. // name, columns range and style ID.
  347. //
  348. // For example set style of column H on Sheet1:
  349. //
  350. // err = f.SetColStyle("Sheet1", "H", style)
  351. //
  352. // Set style of columns C:F on Sheet1:
  353. //
  354. // err = f.SetColStyle("Sheet1", "C:F", style)
  355. //
  356. func (f *File) SetColStyle(sheet, columns string, styleID int) error {
  357. xlsx, err := f.workSheetReader(sheet)
  358. if err != nil {
  359. return err
  360. }
  361. var c1, c2 string
  362. var min, max int
  363. cols := strings.Split(columns, ":")
  364. c1 = cols[0]
  365. min, err = ColumnNameToNumber(c1)
  366. if err != nil {
  367. return err
  368. }
  369. if len(cols) == 2 {
  370. c2 = cols[1]
  371. max, err = ColumnNameToNumber(c2)
  372. if err != nil {
  373. return err
  374. }
  375. } else {
  376. max = min
  377. }
  378. if max < min {
  379. min, max = max, min
  380. }
  381. if xlsx.Cols == nil {
  382. xlsx.Cols = &xlsxCols{}
  383. }
  384. xlsx.Cols.Col = flatCols(xlsxCol{
  385. Min: min,
  386. Max: max,
  387. Width: 9,
  388. Style: styleID,
  389. }, xlsx.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  390. fc.BestFit = c.BestFit
  391. fc.Collapsed = c.Collapsed
  392. fc.CustomWidth = c.CustomWidth
  393. fc.Hidden = c.Hidden
  394. fc.OutlineLevel = c.OutlineLevel
  395. fc.Phonetic = c.Phonetic
  396. fc.Width = c.Width
  397. return fc
  398. })
  399. return nil
  400. }
  401. // SetColWidth provides a function to set the width of a single column or
  402. // multiple columns. For example:
  403. //
  404. // f := excelize.NewFile()
  405. // err := f.SetColWidth("Sheet1", "A", "H", 20)
  406. //
  407. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error {
  408. min, err := ColumnNameToNumber(startcol)
  409. if err != nil {
  410. return err
  411. }
  412. max, err := ColumnNameToNumber(endcol)
  413. if err != nil {
  414. return err
  415. }
  416. if min > max {
  417. min, max = max, min
  418. }
  419. xlsx, err := f.workSheetReader(sheet)
  420. if err != nil {
  421. return err
  422. }
  423. col := xlsxCol{
  424. Min: min,
  425. Max: max,
  426. Width: width,
  427. CustomWidth: true,
  428. }
  429. if xlsx.Cols == nil {
  430. cols := xlsxCols{}
  431. cols.Col = append(cols.Col, col)
  432. xlsx.Cols = &cols
  433. return err
  434. }
  435. xlsx.Cols.Col = flatCols(col, xlsx.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  436. fc.BestFit = c.BestFit
  437. fc.Collapsed = c.Collapsed
  438. fc.Hidden = c.Hidden
  439. fc.OutlineLevel = c.OutlineLevel
  440. fc.Phonetic = c.Phonetic
  441. fc.Style = c.Style
  442. return fc
  443. })
  444. return err
  445. }
  446. // flatCols provides a method for the column's operation functions to flatten
  447. // and check the worksheet columns.
  448. func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) []xlsxCol {
  449. fc := []xlsxCol{}
  450. for i := col.Min; i <= col.Max; i++ {
  451. c := deepcopy.Copy(col).(xlsxCol)
  452. c.Min, c.Max = i, i
  453. fc = append(fc, c)
  454. }
  455. inFlat := func(colID int, cols []xlsxCol) (int, bool) {
  456. for idx, c := range cols {
  457. if c.Max == colID && c.Min == colID {
  458. return idx, true
  459. }
  460. }
  461. return -1, false
  462. }
  463. for _, column := range cols {
  464. for i := column.Min; i <= column.Max; i++ {
  465. if idx, ok := inFlat(i, fc); ok {
  466. fc[idx] = replacer(fc[idx], column)
  467. continue
  468. }
  469. c := deepcopy.Copy(column).(xlsxCol)
  470. c.Min, c.Max = i, i
  471. fc = append(fc, c)
  472. }
  473. }
  474. return fc
  475. }
  476. // positionObjectPixels calculate the vertices that define the position of a
  477. // graphical object within the worksheet in pixels.
  478. //
  479. // +------------+------------+
  480. // | A | B |
  481. // +-----+------------+------------+
  482. // | |(x1,y1) | |
  483. // | 1 |(A1)._______|______ |
  484. // | | | | |
  485. // | | | | |
  486. // +-----+----| OBJECT |-----+
  487. // | | | | |
  488. // | 2 | |______________. |
  489. // | | | (B2)|
  490. // | | | (x2,y2)|
  491. // +-----+------------+------------+
  492. //
  493. // Example of an object that covers some of the area from cell A1 to B2.
  494. //
  495. // Based on the width and height of the object we need to calculate 8 vars:
  496. //
  497. // colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
  498. //
  499. // We also calculate the absolute x and y position of the top left vertex of
  500. // the object. This is required for images.
  501. //
  502. // The width and height of the cells that the object occupies can be
  503. // variable and have to be taken into account.
  504. //
  505. // The values of col_start and row_start are passed in from the calling
  506. // function. The values of col_end and row_end are calculated by
  507. // subtracting the width and height of the object from the width and
  508. // height of the underlying cells.
  509. //
  510. // colStart # Col containing upper left corner of object.
  511. // x1 # Distance to left side of object.
  512. //
  513. // rowStart # Row containing top left corner of object.
  514. // y1 # Distance to top of object.
  515. //
  516. // colEnd # Col containing lower right corner of object.
  517. // x2 # Distance to right side of object.
  518. //
  519. // rowEnd # Row containing bottom right corner of object.
  520. // y2 # Distance to bottom of object.
  521. //
  522. // width # Width of object frame.
  523. // height # Height of object frame.
  524. //
  525. // xAbs # Absolute distance to left side of object.
  526. // yAbs # Absolute distance to top side of object.
  527. //
  528. func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
  529. xAbs := 0
  530. yAbs := 0
  531. // Calculate the absolute x offset of the top-left vertex.
  532. for colID := 1; colID <= col; colID++ {
  533. xAbs += f.getColWidth(sheet, colID)
  534. }
  535. xAbs += x1
  536. // Calculate the absolute y offset of the top-left vertex.
  537. // Store the column change to allow optimisations.
  538. for rowID := 1; rowID <= row; rowID++ {
  539. yAbs += f.getRowHeight(sheet, rowID)
  540. }
  541. yAbs += y1
  542. // Adjust start column for offsets that are greater than the col width.
  543. for x1 >= f.getColWidth(sheet, col) {
  544. x1 -= f.getColWidth(sheet, col)
  545. col++
  546. }
  547. // Adjust start row for offsets that are greater than the row height.
  548. for y1 >= f.getRowHeight(sheet, row) {
  549. y1 -= f.getRowHeight(sheet, row)
  550. row++
  551. }
  552. // Initialise end cell to the same as the start cell.
  553. colEnd := col
  554. rowEnd := row
  555. width += x1
  556. height += y1
  557. // Subtract the underlying cell widths to find end cell of the object.
  558. for width >= f.getColWidth(sheet, colEnd+1) {
  559. colEnd++
  560. width -= f.getColWidth(sheet, colEnd)
  561. }
  562. // Subtract the underlying cell heights to find end cell of the object.
  563. for height >= f.getRowHeight(sheet, rowEnd) {
  564. height -= f.getRowHeight(sheet, rowEnd)
  565. rowEnd++
  566. }
  567. // The end vertices are whatever is left from the width and height.
  568. x2 := width
  569. y2 := height
  570. return col, row, xAbs, yAbs, colEnd, rowEnd, x2, y2
  571. }
  572. // getColWidth provides a function to get column width in pixels by given
  573. // sheet name and column index.
  574. func (f *File) getColWidth(sheet string, col int) int {
  575. xlsx, _ := f.workSheetReader(sheet)
  576. if xlsx.Cols != nil {
  577. var width float64
  578. for _, v := range xlsx.Cols.Col {
  579. if v.Min <= col && col <= v.Max {
  580. width = v.Width
  581. }
  582. }
  583. if width != 0 {
  584. return int(convertColWidthToPixels(width))
  585. }
  586. }
  587. // Optimisation for when the column widths haven't changed.
  588. return int(defaultColWidthPixels)
  589. }
  590. // GetColWidth provides a function to get column width by given worksheet name
  591. // and column index.
  592. func (f *File) GetColWidth(sheet, col string) (float64, error) {
  593. colNum, err := ColumnNameToNumber(col)
  594. if err != nil {
  595. return defaultColWidthPixels, err
  596. }
  597. xlsx, err := f.workSheetReader(sheet)
  598. if err != nil {
  599. return defaultColWidthPixels, err
  600. }
  601. if xlsx.Cols != nil {
  602. var width float64
  603. for _, v := range xlsx.Cols.Col {
  604. if v.Min <= colNum && colNum <= v.Max {
  605. width = v.Width
  606. }
  607. }
  608. if width != 0 {
  609. return width, err
  610. }
  611. }
  612. // Optimisation for when the column widths haven't changed.
  613. return defaultColWidthPixels, err
  614. }
  615. // InsertCol provides a function to insert a new column before given column
  616. // index. For example, create a new column before column C in Sheet1:
  617. //
  618. // err := f.InsertCol("Sheet1", "C")
  619. //
  620. func (f *File) InsertCol(sheet, col string) error {
  621. num, err := ColumnNameToNumber(col)
  622. if err != nil {
  623. return err
  624. }
  625. return f.adjustHelper(sheet, columns, num, 1)
  626. }
  627. // RemoveCol provides a function to remove single column by given worksheet
  628. // name and column index. For example, remove column C in Sheet1:
  629. //
  630. // err := f.RemoveCol("Sheet1", "C")
  631. //
  632. // Use this method with caution, which will affect changes in references such
  633. // as formulas, charts, and so on. If there is any referenced value of the
  634. // worksheet, it will cause a file error when you open it. The excelize only
  635. // partially updates these references currently.
  636. func (f *File) RemoveCol(sheet, col string) error {
  637. num, err := ColumnNameToNumber(col)
  638. if err != nil {
  639. return err
  640. }
  641. xlsx, err := f.workSheetReader(sheet)
  642. if err != nil {
  643. return err
  644. }
  645. for rowIdx := range xlsx.SheetData.Row {
  646. rowData := &xlsx.SheetData.Row[rowIdx]
  647. for colIdx := range rowData.C {
  648. colName, _, _ := SplitCellName(rowData.C[colIdx].R)
  649. if colName == col {
  650. rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
  651. break
  652. }
  653. }
  654. }
  655. return f.adjustHelper(sheet, columns, num, -1)
  656. }
  657. // convertColWidthToPixels provieds function to convert the width of a cell
  658. // from user's units to pixels. Excel rounds the column width to the nearest
  659. // pixel. If the width hasn't been set by the user we use the default value.
  660. // If the column is hidden it has a value of zero.
  661. func convertColWidthToPixels(width float64) float64 {
  662. var padding float64 = 5
  663. var pixels float64
  664. var maxDigitWidth float64 = 7
  665. if width == 0 {
  666. return pixels
  667. }
  668. if width < 1 {
  669. pixels = (width * 12) + 0.5
  670. return math.Ceil(pixels)
  671. }
  672. pixels = (width*maxDigitWidth + 0.5) + padding
  673. return math.Ceil(pixels)
  674. }