From ab805403b901ba7edc7fb8cc2185648f94299c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 7 Aug 2024 19:30:38 +0200 Subject: [PATCH] v0.0.500 --- goextVersion.go | 4 +- wpdf/wpdfTable.go | 42 ++++------ wpdf/wpdfTableCell.go | 183 ++++++++++++++++++++++++++++++++++++++++++ wpdf/wpdfTableRow.go | 52 ++++++++++++ wpdf/wpdf_test.go | 35 +++++--- 5 files changed, 276 insertions(+), 40 deletions(-) create mode 100644 wpdf/wpdfTableCell.go create mode 100644 wpdf/wpdfTableRow.go diff --git a/goextVersion.go b/goextVersion.go index 4e96f74..b281ecf 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.499" +const GoextVersion = "0.0.500" -const GoextVersionTimestamp = "2024-08-07T18:34:22+0200" +const GoextVersionTimestamp = "2024-08-07T19:30:38+0200" diff --git a/wpdf/wpdfTable.go b/wpdf/wpdfTable.go index 69f8b11..0ff905e 100644 --- a/wpdf/wpdfTable.go +++ b/wpdf/wpdfTable.go @@ -38,24 +38,6 @@ type TableBuilder struct { debug *bool } -type TableCell struct { - Content string - Style TableCellStyleOpt -} - -type TableCellStyleOpt struct { - MultiCell *bool - Ellipsize *bool - PaddingHorz *float64 - MinWidth *float64 - - PDFCellOpt -} - -type tableRow struct { - cells []TableCell -} - func (r tableRow) maxFontSize(defaultFontSize float64) float64 { mfs := defaultFontSize for _, cell := range r.cells { @@ -91,10 +73,10 @@ func (b *TableBuilder) AddRow(cells ...TableCell) *TableBuilder { return b } -func (b *TableBuilder) AddRowWithStyle(style TableCellStyleOpt, cells ...string) *TableBuilder { +func (b *TableBuilder) AddRowWithStyle(style *TableCellStyleOpt, cells ...string) *TableBuilder { tcels := make([]TableCell, 0, len(cells)) for _, cell := range cells { - tcels = append(tcels, TableCell{Content: cell, Style: style}) + tcels = append(tcels, TableCell{Content: cell, Style: *style}) } b.rows = append(b.rows, tableRow{cells: tcels}) @@ -113,6 +95,10 @@ func (b *TableBuilder) AddRowDefaultStyle(cells ...string) *TableBuilder { return b } +func (b *TableBuilder) BuildRow() *TableRowBuilder { + return &TableRowBuilder{tabbuilder: b, cells: make([]TableCell, 0)} +} + func (b *TableBuilder) Build() { builder := b.builder @@ -148,15 +134,15 @@ func (b *TableBuilder) Build() { str := cell.Content style := cell.Style - ellipsize := langext.Coalesce(style.Ellipsize, true) - cellPaddingHorz := langext.Coalesce(style.PaddingHorz, 2) + ellipsize := langext.Coalesce(style.ellipsize, true) + cellPaddingHorz := langext.Coalesce(style.paddingHorz, 2) bx := builder.GetX() by := builder.GetY() cellWidth := columnWidths[cellIdx] - if langext.Coalesce(style.MultiCell, true) { + if langext.Coalesce(style.multiCell, true) { builder.MultiCell(str, style.PDFCellOpt.Copy().ToMulti().Width(cellWidth).Debug(debug)) @@ -230,8 +216,8 @@ func (b *TableBuilder) calculateColumns() []float64 { for _, row := range b.rows { if len(row.cells) > colIdx { - ph := langext.Coalesce(row.cells[colIdx].Style.PaddingHorz, 2) - mw := langext.Coalesce(row.cells[colIdx].Style.MinWidth, 0) + ph := langext.Coalesce(row.cells[colIdx].Style.paddingHorz, 2) + mw := langext.Coalesce(row.cells[colIdx].Style.minWidth, 0) minWidth = max(minWidth, ph+mw) @@ -344,8 +330,8 @@ func defaultTableStyle() *TableCellStyleOpt { FillColorHex(uint32(0xF0F0F0)). TextColorHex(uint32(0x000000)). FillBackground(true), - MinWidth: langext.Ptr(float64(5)), - Ellipsize: langext.PTrue, - MultiCell: langext.PFalse, + minWidth: langext.Ptr(float64(5)), + ellipsize: langext.PTrue, + multiCell: langext.PFalse, } } diff --git a/wpdf/wpdfTableCell.go b/wpdf/wpdfTableCell.go new file mode 100644 index 0000000..a95025d --- /dev/null +++ b/wpdf/wpdfTableCell.go @@ -0,0 +1,183 @@ +package wpdf + +import ( + "gogs.mikescher.com/BlackForestBytes/goext/dataext" + "gogs.mikescher.com/BlackForestBytes/goext/langext" +) + +type TableCell struct { + Content string + Style TableCellStyleOpt +} + +type TableCellStyleOpt struct { + multiCell *bool + ellipsize *bool + paddingHorz *float64 + minWidth *float64 + fillHeight *bool + + PDFCellOpt +} + +func (o *TableCellStyleOpt) FillHeight(b bool) *TableCellStyleOpt { + o.fillHeight = &b + return o +} + +func (o *TableCellStyleOpt) MultiCell(v bool) *TableCellStyleOpt { + o.multiCell = &v + return o +} + +func (o *TableCellStyleOpt) Ellipsize(v bool) *TableCellStyleOpt { + o.ellipsize = &v + return o +} + +func (o *TableCellStyleOpt) PaddingHorz(v float64) *TableCellStyleOpt { + o.paddingHorz = &v + return o +} + +func (o *TableCellStyleOpt) MinWidth(v float64) *TableCellStyleOpt { + o.minWidth = &v + return o +} + +func (o *TableCellStyleOpt) CellStyle(v PDFCellOpt) *TableCellStyleOpt { + o.PDFCellOpt = v + return o +} + +func (o *TableCellStyleOpt) Width(v float64) *TableCellStyleOpt { + o.PDFCellOpt.width = &v + return o +} + +func (o *TableCellStyleOpt) Height(v float64) *TableCellStyleOpt { + o.PDFCellOpt.height = &v + return o +} + +func (o *TableCellStyleOpt) Border(v PDFBorder) *TableCellStyleOpt { + o.PDFCellOpt.border = &v + return o +} + +func (o *TableCellStyleOpt) LnPos(v PDFTextBreak) *TableCellStyleOpt { + o.PDFCellOpt.ln = &v + return o +} + +func (o *TableCellStyleOpt) Align(v PDFTextAlign) *TableCellStyleOpt { + o.PDFCellOpt.align = &v + return o +} + +func (o *TableCellStyleOpt) FillBackground(v bool) *TableCellStyleOpt { + o.PDFCellOpt.fill = &v + return o +} + +func (o *TableCellStyleOpt) Link(v int) *TableCellStyleOpt { + o.PDFCellOpt.link = &v + return o +} + +func (o *TableCellStyleOpt) LinkStr(v string) *TableCellStyleOpt { + o.PDFCellOpt.linkStr = &v + return o +} + +func (o *TableCellStyleOpt) Font(fontName PDFFontFamily, fontStyle PDFFontStyle, fontSize float64) *TableCellStyleOpt { + o.PDFCellOpt.fontNameOverride = &fontName + o.PDFCellOpt.fontStyleOverride = &fontStyle + o.PDFCellOpt.fontSizeOverride = &fontSize + return o +} + +func (o *TableCellStyleOpt) FontName(v PDFFontFamily) *TableCellStyleOpt { + o.PDFCellOpt.fontNameOverride = &v + return o +} + +func (o *TableCellStyleOpt) FontStyle(v PDFFontStyle) *TableCellStyleOpt { + o.PDFCellOpt.fontStyleOverride = &v + return o +} + +func (o *TableCellStyleOpt) FontSize(v float64) *TableCellStyleOpt { + o.PDFCellOpt.fontSizeOverride = &v + return o +} + +func (o *TableCellStyleOpt) Bold() *TableCellStyleOpt { + o.PDFCellOpt.fontStyleOverride = langext.Ptr(Bold) + return o +} + +func (o *TableCellStyleOpt) Italic() *TableCellStyleOpt { + o.PDFCellOpt.fontStyleOverride = langext.Ptr(Italic) + return o +} + +func (o *TableCellStyleOpt) LnAfter(v float64) *TableCellStyleOpt { + o.PDFCellOpt.extraLn = &v + return o +} + +func (o *TableCellStyleOpt) X(v float64) *TableCellStyleOpt { + o.PDFCellOpt.x = &v + return o +} + +func (o *TableCellStyleOpt) AutoWidth() *TableCellStyleOpt { + o.PDFCellOpt.autoWidth = langext.PTrue + return o +} + +func (o *TableCellStyleOpt) AutoWidthPaddingX(v float64) *TableCellStyleOpt { + o.PDFCellOpt.autoWidthPaddingX = &v + return o +} + +func (o *TableCellStyleOpt) TextColor(cr, cg, cb int) *TableCellStyleOpt { + o.PDFCellOpt.textColor = langext.Ptr(rgbToColor(cr, cg, cb)) + return o +} + +func (o *TableCellStyleOpt) TextColorHex(c uint32) *TableCellStyleOpt { + o.PDFCellOpt.textColor = langext.Ptr(hexToColor(c)) + return o +} + +func (o *TableCellStyleOpt) BorderColor(cr, cg, cb int) *TableCellStyleOpt { + o.PDFCellOpt.borderColor = langext.Ptr(rgbToColor(cr, cg, cb)) + return o +} + +func (o *TableCellStyleOpt) BorderColorHex(c uint32) *TableCellStyleOpt { + o.PDFCellOpt.borderColor = langext.Ptr(hexToColor(c)) + return o +} + +func (o *TableCellStyleOpt) FillColor(cr, cg, cb int) *TableCellStyleOpt { + o.PDFCellOpt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb)) + return o +} + +func (o *TableCellStyleOpt) FillColorHex(c uint32) *TableCellStyleOpt { + o.PDFCellOpt.fillColor = langext.Ptr(hexToColor(c)) + return o +} + +func (o *TableCellStyleOpt) Alpha(alpha float64, blendMode PDFBlendMode) *TableCellStyleOpt { + o.PDFCellOpt.alphaOverride = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode} + return o +} + +func (o *TableCellStyleOpt) Debug(v bool) *TableCellStyleOpt { + o.PDFCellOpt.debug = &v + return o +} diff --git a/wpdf/wpdfTableRow.go b/wpdf/wpdfTableRow.go new file mode 100644 index 0000000..d6f5976 --- /dev/null +++ b/wpdf/wpdfTableRow.go @@ -0,0 +1,52 @@ +package wpdf + +import "gogs.mikescher.com/BlackForestBytes/goext/langext" + +type tableRow struct { + cells []TableCell +} + +type TableRowBuilder struct { + tabbuilder *TableBuilder + defaultStyle *TableCellStyleOpt + cells []TableCell +} + +func (r *TableRowBuilder) RowStyle(style *TableCellStyleOpt) *TableRowBuilder { + r.defaultStyle = style + return r +} + +func (r *TableRowBuilder) Cell(cell string) *TableRowBuilder { + r.cells = append(r.cells, TableCell{Content: cell, Style: langext.Coalesce3(r.defaultStyle, r.tabbuilder.defaultCellStyle, TableCellStyleOpt{})}) + return r +} + +func (r *TableRowBuilder) Cells(cells ...string) *TableRowBuilder { + for _, cell := range cells { + r.cells = append(r.cells, TableCell{Content: cell, Style: langext.Coalesce3(r.defaultStyle, r.tabbuilder.defaultCellStyle, TableCellStyleOpt{})}) + } + return r +} + +func (r *TableRowBuilder) CellObject(cell TableCell) *TableRowBuilder { + r.cells = append(r.cells, cell) + return r +} + +func (r *TableRowBuilder) CellObjects(cells ...TableCell) *TableRowBuilder { + for _, cell := range cells { + r.cells = append(r.cells, cell) + } + return r +} + +func (r *TableRowBuilder) CellWithStyle(cell string, style TableCellStyleOpt) *TableRowBuilder { + r.cells = append(r.cells, TableCell{Content: cell, Style: style}) + return r +} + +func (r *TableRowBuilder) BuildRow() *TableBuilder { + r.tabbuilder.AddRow(r.cells...) + return r.tabbuilder +} diff --git a/wpdf/wpdf_test.go b/wpdf/wpdf_test.go index d00fde3..aaad329 100644 --- a/wpdf/wpdf_test.go +++ b/wpdf/wpdf_test.go @@ -2,9 +2,11 @@ package wpdf import ( _ "embed" + "fmt" "gogs.mikescher.com/BlackForestBytes/goext/imageext" "gogs.mikescher.com/BlackForestBytes/goext/langext" "os" + "path" "testing" ) @@ -38,7 +40,7 @@ func TestPDFBuilder(t *testing.T) { builder.Ln(4) - cellStyleHeader := TableCellStyleOpt{ + cellStyleHeader := &TableCellStyleOpt{ PDFCellOpt: *NewPDFCellOpt(). FontSize(float64(8)). BorderColorHex(uint32(0x666666)). @@ -48,12 +50,12 @@ func TestPDFBuilder(t *testing.T) { TextColorHex(uint32(0x000000)). Align(AlignHorzCenter). Bold(), - MinWidth: langext.Ptr(float64(5)), - Ellipsize: langext.PTrue, - MultiCell: langext.PFalse, + minWidth: langext.Ptr(float64(5)), + ellipsize: langext.PTrue, + multiCell: langext.PFalse, } - cellStyleMulti := TableCellStyleOpt{ + cellStyleMulti := &TableCellStyleOpt{ PDFCellOpt: *NewPDFCellOpt(). FontSize(float64(8)). BorderColorHex(uint32(0x666666)). @@ -61,9 +63,9 @@ func TestPDFBuilder(t *testing.T) { FillColorHex(uint32(0xC060C0)). FillBackground(true). TextColorHex(uint32(0x000000)), - MinWidth: langext.Ptr(float64(5)), - Ellipsize: langext.PFalse, - MultiCell: langext.PTrue, + minWidth: langext.Ptr(float64(5)), + ellipsize: langext.PFalse, + multiCell: langext.PTrue, } builder.Table(). @@ -74,7 +76,18 @@ func TestPDFBuilder(t *testing.T) { AddRowDefaultStyle("test", "hello", "123", "end"). AddRowDefaultStyle("123", "helasdsalo", "a", "enwqad"). AddRowDefaultStyle("123asd", "TrimMeTrimMeTrimMeTrimMe", "a", "enwqad"). - AddRowWithStyle(cellStyleMulti, "123", "helasdsalo", "a", "MultiCell: enwqad enw\nqad enwqad enwqad enwqad enwqad enwqad enwqad enwqad"). + AddRowWithStyle(cellStyleMulti, "123", "helasdsalo", "a", "MultiCell: enwqad enw\nqad enwqad enwqad enwqad enwqad"). + AddRowDefaultStyle("123", "helasdsalo", "a", "enwqad"). + Debug(false). + Build() + + builder.Table(). + Widths("auto", "20", "1fr", "20"). + PadX(2). + PadY(2). + BuildRow().RowStyle(cellStyleHeader).Cells("test", "hello", "123", "end").BuildRow(). + BuildRow().Cells("test", "hello", "123", "end").BuildRow(). + BuildRow().RowStyle(cellStyleMulti.FillHeight(true)).Cell("123").Cell("helasdsalo").Cell("a").Cell("MultiCell: enwqad enw\nqad enwqad enwqad enwqad enwqad").BuildRow(). AddRowDefaultStyle("123", "helasdsalo", "a", "enwqad"). Debug(false). Build() @@ -84,5 +97,7 @@ func TestPDFBuilder(t *testing.T) { t.Fatal(err) } - _ = os.WriteFile("wpdf_test.pdf", bin, 0644) + fn := "wpdf_test.pdf" + _ = os.WriteFile(fn, bin, 0644) + fmt.Println("file://" + path.Join(langext.Must(os.Getwd()), fn)) }