Merge pull request #336 from adrg/text-rendering-mode

Add text style rendering mode
This commit is contained in:
Gunnsteinn Hall 2019-01-31 16:48:37 +00:00 committed by GitHub
commit bb1aa7152a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 0 deletions

View File

@ -41,6 +41,39 @@ const (
TextAlignmentJustify
)
// TextRenderingMode determines whether showing text shall cause glyph
// outlines to be stroked, filled, used as a clipping boundary, or some
// combination of the three.
// See section 9.3 "Text State Parameters and Operators" and
// Table 106 (pp. 254-255 PDF32000_2008).
type TextRenderingMode int
const (
// TextRenderingModeFill (default) - Fill text.
TextRenderingModeFill TextRenderingMode = iota
// TextRenderingModeStroke - Stroke text.
TextRenderingModeStroke
// TextRenderingModeFillStroke - Fill, then stroke text.
TextRenderingModeFillStroke
// TextRenderingModeInvisible - Neither fill nor stroke text (invisible).
TextRenderingModeInvisible
// TextRenderingModeFillClip - Fill text and add to path for clipping.
TextRenderingModeFillClip
// TextRenderingModeStrokeClip - Stroke text and add to path for clipping.
TextRenderingModeStrokeClip
// TextRenderingModeFillStrokeClip - Fill, then stroke text and add to path for clipping.
TextRenderingModeFillStrokeClip
// TextRenderingModeClip - Add text to path for clipping.
TextRenderingModeClip
)
// Relative and absolute positioning types.
type positioning int

View File

@ -656,6 +656,9 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
fontName := defaultFontName
fontSize := defaultFontSize
// Set chunk rendering mode.
cc.Add_Tr(int64(style.RenderingMode))
if p.alignment != TextAlignmentJustify || isLastLine {
spaceMetrics, found := style.Font.GetRuneMetrics(' ')
if !found {
@ -744,6 +747,9 @@ func drawStyledParagraphOnBlock(blk *Block, p *StyledParagraph, ctx DrawContext)
}
currX += chunkWidth
// Reset rendering mode.
cc.Add_Tr(int64(TextRenderingModeFill))
}
currY -= height

View File

@ -436,3 +436,75 @@ Sed imperdiet sodales lacus sed sollicitudin. In porta tortor quis augue tempor,
t.Fatalf("Fail: %v\n", err)
}
}
func TestStyledParagraphRenderingModes(t *testing.T) {
fontRegular := newStandard14Font(t, fonts.HelveticaName)
c := New()
c.NewPage()
// Showcase rendering modes.
tmodes := []TextRenderingMode{
TextRenderingModeFill,
TextRenderingModeStroke,
TextRenderingModeFillStroke,
TextRenderingModeInvisible,
TextRenderingModeFillClip,
TextRenderingModeStrokeClip,
TextRenderingModeFillStrokeClip,
TextRenderingModeClip,
}
tmodesDesc := []string{
"- Text rendering mode fill: ",
"- Text rendering mode stroke: ",
"- Text rendering mode fill and stroke: ",
"- Text rendering mode invisible: ",
"- Text rendering mode fill and clip: ",
"- Text rendering mode stroke and clip: ",
"- Text rendering mode fill, stroke and clip: ",
"- Text rendering mode clip: ",
}
p := c.NewStyledParagraph()
p.SetLineHeight(1.5)
for i, tmode := range tmodes {
chunk := p.Append(tmodesDesc[i])
chunk.Style.Font = fontRegular
chunk.Style.FontSize = 12
chunk = p.Append("This is some sample text\n")
chunk.Style.RenderingMode = tmode
chunk.Style.Font = fontRegular
chunk.Style.FontSize = 22
}
err := c.Draw(p)
if err != nil {
t.Fatalf("Error drawing: %v", err)
}
// Invisible, manually positioned paragraph.
p = c.NewStyledParagraph()
p.SetPos(150, 500)
chunk := p.Append("Invisible text >>> ")
chunk.Style.Font = fontRegular
chunk.Style.FontSize = 12
chunk = p.Append("Some invisible text manually positioned")
chunk.Style.Font = fontRegular
chunk.Style.FontSize = 15
chunk.Style.RenderingMode = TextRenderingModeInvisible
err = c.Draw(p)
if err != nil {
t.Fatalf("Error drawing: %v", err)
}
// Write output file.
err = c.WriteToFile(tempFile("styled_paragraph_rendering_mode.pdf"))
if err != nil {
t.Fatalf("Fail: %v\n", err)
}
}

View File

@ -19,6 +19,9 @@ type TextStyle struct {
// The size of the font.
FontSize float64
// The rendering mode.
RenderingMode TextRenderingMode
}
// newTextStyle creates a new text style object using the specified font.