mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-30 13:48:51 +08:00
Merge branch 'v3' into refactor_2
This commit is contained in:
commit
d9dfa57225
@ -1855,6 +1855,7 @@ func TestTableInSubchapter(t *testing.T) {
|
||||
cell.SetIndent(5)
|
||||
|
||||
p = NewParagraph("Bezt business bureu")
|
||||
p.SetEnableWrap(false)
|
||||
p.SetFont(fontRegular)
|
||||
p.SetFontSize(10)
|
||||
p.SetColor(ColorGreen)
|
||||
|
@ -139,13 +139,14 @@ func TestDivInline(t *testing.T) {
|
||||
style.Color = ColorRGBFrom8bit(0, 0, 255)
|
||||
|
||||
s := NewStyledParagraph("This styled paragraph should ", style)
|
||||
s.SetEnableWrap(false)
|
||||
|
||||
style.Color = ColorRGBFrom8bit(255, 0, 0)
|
||||
s.Append("fit", style)
|
||||
|
||||
style.Color = ColorRGBFrom8bit(0, 255, 0)
|
||||
style.Font = fontBold
|
||||
s.Append(" right in.", style)
|
||||
s.Append(" in.", style)
|
||||
|
||||
div.Add(s)
|
||||
|
||||
|
@ -85,7 +85,7 @@ func NewParagraph(text string) *Paragraph {
|
||||
|
||||
// TODO: Can we wrap intellectually, only if given width is known?
|
||||
|
||||
p.enableWrap = false
|
||||
p.enableWrap = true
|
||||
p.defaultWrap = true
|
||||
p.SetColor(ColorRGBFrom8bit(0, 0, 0))
|
||||
p.alignment = TextAlignmentLeft
|
||||
@ -187,13 +187,12 @@ func (p *Paragraph) GetMargins() (float64, float64, float64, float64) {
|
||||
// text can extend to prior to wrapping over to next line.
|
||||
func (p *Paragraph) SetWidth(width float64) {
|
||||
p.wrapWidth = width
|
||||
p.enableWrap = true
|
||||
p.wrapText()
|
||||
}
|
||||
|
||||
// Width returns the width of the Paragraph.
|
||||
func (p *Paragraph) Width() float64 {
|
||||
if p.enableWrap {
|
||||
if p.enableWrap && int(p.wrapWidth) > 0 {
|
||||
return p.wrapWidth
|
||||
}
|
||||
return p.getTextWidth() / 1000.0
|
||||
@ -239,7 +238,7 @@ func (p *Paragraph) getTextWidth() float64 {
|
||||
// Simple algorithm to wrap the text into lines (greedy algorithm - fill the lines).
|
||||
// XXX/TODO: Consider the Knuth/Plass algorithm or an alternative.
|
||||
func (p *Paragraph) wrapText() error {
|
||||
if !p.enableWrap {
|
||||
if !p.enableWrap || int(p.wrapWidth) <= 0 {
|
||||
p.textLines = []string{p.text}
|
||||
return nil
|
||||
}
|
||||
@ -367,7 +366,7 @@ func (p *Paragraph) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawContext,
|
||||
}
|
||||
} else {
|
||||
// Absolute.
|
||||
if p.wrapWidth == 0 {
|
||||
if int(p.wrapWidth) <= 0 {
|
||||
// Use necessary space.
|
||||
p.SetWidth(p.getTextWidth())
|
||||
}
|
||||
|
@ -193,7 +193,7 @@ func (p *StyledParagraph) SetWidth(width float64) {
|
||||
|
||||
// Width returns the width of the Paragraph.
|
||||
func (p *StyledParagraph) Width() float64 {
|
||||
if p.enableWrap {
|
||||
if p.enableWrap && int(p.wrapWidth) > 0 {
|
||||
return p.wrapWidth
|
||||
}
|
||||
|
||||
@ -310,7 +310,7 @@ func (p *StyledParagraph) getTextHeight() float64 {
|
||||
// fill the lines.
|
||||
// XXX/TODO: Consider the Knuth/Plass algorithm or an alternative.
|
||||
func (p *StyledParagraph) wrapText() error {
|
||||
if !p.enableWrap {
|
||||
if !p.enableWrap || int(p.wrapWidth) <= 0 {
|
||||
p.lines = [][]TextChunk{p.chunks}
|
||||
return nil
|
||||
}
|
||||
@ -462,7 +462,7 @@ func (p *StyledParagraph) GeneratePageBlocks(ctx DrawContext) ([]*Block, DrawCon
|
||||
}
|
||||
} else {
|
||||
// Absolute.
|
||||
if p.wrapWidth == 0 {
|
||||
if int(p.wrapWidth) <= 0 {
|
||||
// Use necessary space.
|
||||
p.SetWidth(p.getTextWidth())
|
||||
}
|
||||
@ -600,17 +600,20 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
|
||||
|
||||
// Add line shifts.
|
||||
objs := []core.PdfObject{}
|
||||
|
||||
wrapWidth := p.wrapWidth * 1000.0
|
||||
if p.alignment == TextAlignmentJustify {
|
||||
// Not to justify last line.
|
||||
// Do not justify last line.
|
||||
if spaces > 0 && !isLastLine {
|
||||
spaceWidth = (p.wrapWidth*1000.0 - width) / float64(spaces) / defaultFontSize
|
||||
spaceWidth = (wrapWidth - width) / float64(spaces) / defaultFontSize
|
||||
}
|
||||
} else if p.alignment == TextAlignmentCenter {
|
||||
// Start with a shift.
|
||||
shift := (p.wrapWidth*1000.0 - width - spaceWidth) / 2 / defaultFontSize
|
||||
// Start with an offset of half of the remaining line space.
|
||||
shift := (wrapWidth - width - spaceWidth) / 2 / defaultFontSize
|
||||
objs = append(objs, core.MakeFloat(-shift))
|
||||
} else if p.alignment == TextAlignmentRight {
|
||||
shift := (p.wrapWidth*1000.0 - width - spaceWidth) / defaultFontSize
|
||||
// Push the text at the end of the line.
|
||||
shift := (wrapWidth - width - spaceWidth) / defaultFontSize
|
||||
objs = append(objs, core.MakeFloat(-shift))
|
||||
}
|
||||
|
||||
|
@ -691,15 +691,15 @@ func (cell *TableCell) SetContent(vd VectorDrawable) error {
|
||||
switch t := vd.(type) {
|
||||
case *Paragraph:
|
||||
if t.defaultWrap {
|
||||
// Default paragraph settings in table: no wrapping.
|
||||
t.enableWrap = false // No wrapping.
|
||||
// Enable wrapping by default.
|
||||
t.enableWrap = true
|
||||
}
|
||||
|
||||
cell.content = vd
|
||||
case *StyledParagraph:
|
||||
if t.defaultWrap {
|
||||
// Default styled paragraph settings in table: no wrapping.
|
||||
t.enableWrap = false // No wrapping.
|
||||
// Enable wrapping by default.
|
||||
t.enableWrap = true
|
||||
}
|
||||
|
||||
cell.content = vd
|
||||
|
Loading…
x
Reference in New Issue
Block a user