Fix text alignments for the Styled paragraph component

This commit is contained in:
Adrian-George Bostan 2018-09-24 18:05:07 +03:00
parent c4f0bdb402
commit 4330ca3973

View File

@ -449,8 +449,8 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
return ctx, err return ctx, err
} }
num++
fontLine = append(fontLine, fontName) fontLine = append(fontLine, fontName)
num++
} }
fonts = append(fonts, fontLine) fonts = append(fonts, fontLine)
@ -535,28 +535,34 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
objs = append(objs, core.MakeFloat(-shift)) objs = append(objs, core.MakeFloat(-shift))
} }
if len(objs) > 0 {
cc.Add_Tf(defaultFontName, defaultFontSize). cc.Add_Tf(defaultFontName, defaultFontSize).
Add_TL(defaultFontSize * p.lineHeight). Add_TL(defaultFontSize * p.lineHeight).
Add_TJ(objs...) Add_TJ(objs...)
}
// Render line text chunks // Render line text chunks
for k, chunk := range line { for k, chunk := range line {
style := &chunk.Style style := &chunk.Style
r, g, b := style.Color.ToRGB() r, g, b := style.Color.ToRGB()
encStr := "" fontName := defaultFontName
objs = []core.PdfObject{} fontSize := defaultFontSize
if p.alignment != TextAlignmentJustify || isLastLine { if p.alignment != TextAlignmentJustify || isLastLine {
spaceMetrics, found := style.Font.GetGlyphCharMetrics("space") spaceMetrics, found := style.Font.GetGlyphCharMetrics("space")
if !found { if !found {
return ctx, errors.New("The font does not have a space glyph") return ctx, errors.New("The font does not have a space glyph")
} }
fontName = fonts[idx][k]
fontSize = style.FontSize
spaceWidth = spaceMetrics.Wx spaceWidth = spaceMetrics.Wx
} }
for _, r := range chunk.Text { encStr := ""
glyph, found := p.encoder.RuneToGlyph(r) for _, rn := range chunk.Text {
glyph, found := p.encoder.RuneToGlyph(rn)
if !found { if !found {
common.Log.Debug("Rune 0x%x not supported by text encoder", r) common.Log.Debug("Rune 0x%x not supported by text encoder", r)
return ctx, errors.New("Unsupported rune in text encoding") return ctx, errors.New("Unsupported rune in text encoding")
@ -569,22 +575,28 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
} }
if len(encStr) > 0 { if len(encStr) > 0 {
objs = append(objs, core.MakeString(encStr))
encStr = ""
}
objs = append(objs, core.MakeFloat(-spaceWidth))
} else {
encStr += string(p.encoder.Encode(string(r)))
}
}
if len(encStr) > 0 {
objs = append(objs, core.MakeString(encStr))
}
cc.Add_rg(r, g, b). cc.Add_rg(r, g, b).
Add_Tf(fonts[idx][k], style.FontSize). Add_Tf(fonts[idx][k], style.FontSize).
Add_TL(style.FontSize * p.lineHeight). Add_TL(style.FontSize * p.lineHeight).
Add_TJ(objs...) Add_TJ([]core.PdfObject{core.MakeString(encStr)}...)
encStr = ""
}
cc.Add_Tf(fontName, fontSize).
Add_TL(fontSize * p.lineHeight).
Add_TJ([]core.PdfObject{core.MakeFloat(-spaceWidth)}...)
} else {
encStr += p.encoder.Encode(string(rn))
}
}
if len(encStr) > 0 {
cc.Add_rg(r, g, b).
Add_Tf(fonts[idx][k], style.FontSize).
Add_TL(style.FontSize * p.lineHeight).
Add_TJ([]core.PdfObject{core.MakeString(encStr)}...)
}
} }
} }
cc.Add_ET() cc.Add_ET()