Compare commits
	
		
			4 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a3481a7d2d | |||
| a8e6f98a89 | |||
| ab805403b9 | |||
| 1e98d351ce | 
| @@ -1,5 +1,5 @@ | ||||
| package goext | ||||
|  | ||||
| const GoextVersion = "0.0.498" | ||||
| const GoextVersion = "0.0.502" | ||||
|  | ||||
| const GoextVersionTimestamp = "2024-08-07T17:26:35+0200" | ||||
| const GoextVersionTimestamp = "2024-08-07T19:35:23+0200" | ||||
|   | ||||
| @@ -293,7 +293,7 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) { | ||||
| 		} else if ln == BreakToRight { | ||||
| 			b.Rect(b.GetX()-xBefore, *height, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0)) | ||||
| 		} else if ln == BreakToBelow { | ||||
| 			b.Rect(b.GetPageWidth(), *height, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0)) | ||||
| 			b.Rect(b.GetPageWidth()-xBefore-b.GetMarginRight(), *height, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0)) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -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 { | ||||
| @@ -71,8 +53,8 @@ func (b *TableBuilder) Widths(v ...string) *TableBuilder { | ||||
| 	return b | ||||
| } | ||||
|  | ||||
| func (b *TableBuilder) DefaultStyle(s TableCellStyleOpt) *TableBuilder { | ||||
| 	b.defaultCellStyle = &s | ||||
| func (b *TableBuilder) DefaultStyle(s *TableCellStyleOpt) *TableBuilder { | ||||
| 	b.defaultCellStyle = s | ||||
| 	return b | ||||
| } | ||||
|  | ||||
| @@ -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, | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										187
									
								
								wpdf/wpdfTableCell.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										187
									
								
								wpdf/wpdfTableCell.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,187 @@ | ||||
| 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 NewTableCellStyleOpt() *TableCellStyleOpt { | ||||
| 	return &TableCellStyleOpt{} | ||||
| } | ||||
|  | ||||
| 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 | ||||
| } | ||||
							
								
								
									
										52
									
								
								wpdf/wpdfTableRow.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								wpdf/wpdfTableRow.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
| } | ||||
| @@ -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)) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user