| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package lib
- import "github.com/signintech/gopdf"
- const (
- ValignTop = 1
- ValignMiddle = 2
- ValignBottom = 3
- )
- const (
- AlignLeft = 4
- AlignCenter = 5
- AlignRight = 6
- )
- // RectFillColorWithWrap 支持文字换行的矩形填充函数
- func RectFillColorWithWrap(pdf *gopdf.GoPdf,
- text string,
- fontSize int,
- x, y, w, h float64,
- r, g, b uint8,
- align, valign int,
- ) {
- // 绘制背景矩形
- pdf.SetLineWidth(0.1)
- pdf.SetFillColor(r, g, b) //setup fill color
- pdf.SetLineType("") // 线条样式
- pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
- pdf.SetFillColor(0, 0, 0)
- // 测量文字宽度,判断是否需要换行
- textw, _ := pdf.MeasureTextWidth(text)
- // 如果文字宽度超过单元格宽度,进行换行处理
- if textw > w-4 { // 预留一些边距
- // 换行时使用较小的字体,为上下留出空白
- adjustedFontSize := fontSize - 2
- if adjustedFontSize < 8 {
- adjustedFontSize = 8 // 最小字体大小
- }
- // 设置调整后的字体大小
- currentFont := "wts" // 假设当前使用的字体名称
- pdf.SetFont(currentFont, "", adjustedFontSize)
- // 将文字按字符分割,逐个测量直到超出宽度
- runes := []rune(text)
- lines := []string{}
- currentLine := ""
- for _, r := range runes {
- testLine := currentLine + string(r)
- testWidth, _ := pdf.MeasureTextWidth(testLine)
- if testWidth > w-4 && currentLine != "" {
- // 当前行已满,开始新行
- lines = append(lines, currentLine)
- currentLine = string(r)
- } else {
- currentLine = testLine
- }
- }
- // 添加最后一行
- if currentLine != "" {
- lines = append(lines, currentLine)
- }
- // 计算行高和总高度,为上下留出空白
- lineHeight := float64(adjustedFontSize) + 1 // 行间距稍微小一点
- totalTextHeight := float64(len(lines)) * lineHeight
- padding := 2.0 // 上下各留2个单位的空白
- startY := y + padding
- if valign == ValignMiddle {
- startY = y + (h / 2) - (totalTextHeight / 2)
- } else if valign == ValignBottom {
- startY = y + h - totalTextHeight - padding
- }
- // 绘制每一行
- for i, line := range lines {
- lineY := startY + float64(i)*lineHeight
- lineX := x + 2 // 预留左边距
- if align == AlignCenter {
- lineWidth, _ := pdf.MeasureTextWidth(line)
- lineX = x + (w / 2) - (lineWidth / 2)
- } else if align == AlignRight {
- lineWidth, _ := pdf.MeasureTextWidth(line)
- lineX = x + w - lineWidth - 2
- }
- pdf.SetX(lineX)
- pdf.SetY(lineY)
- pdf.Cell(nil, line)
- }
- // 恢复原来的字体大小
- pdf.SetFont(currentFont, "", fontSize)
- } else {
- // 文字不需要换行,使用原来的逻辑
- if align == AlignCenter {
- x = x + (w / 2) - (textw / 2)
- } else if align == AlignRight {
- x = x + w - textw
- }
- pdf.SetX(x)
- if valign == ValignMiddle {
- y = y + (h / 2) - (float64(fontSize) / 2)
- } else if valign == ValignBottom {
- y = y + h - float64(fontSize)
- }
- pdf.SetY(y)
- pdf.Cell(nil, text)
- }
- }
- func RectFillColor(pdf *gopdf.GoPdf,
- text string,
- fontSize int,
- x, y, w, h float64,
- r, g, b uint8,
- align, valign int,
- ) {
- pdf.SetLineWidth(0.1)
- pdf.SetFillColor(r, g, b) //setup fill color
- pdf.SetLineType("") // 线条样式
- pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
- pdf.SetFillColor(0, 0, 0)
- if align == AlignCenter {
- textw, _ := pdf.MeasureTextWidth(text)
- x = x + (w / 2) - (textw / 2)
- } else if align == AlignRight {
- textw, _ := pdf.MeasureTextWidth(text)
- x = x + w - textw
- }
- pdf.SetX(x)
- if valign == ValignMiddle {
- y = y + (h / 2) - (float64(fontSize) / 2)
- } else if valign == ValignBottom {
- y = y + h - float64(fontSize)
- }
- pdf.SetY(y)
- pdf.Cell(nil, text)
- }
|