unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.55k stars 250 forks source link

[BUG] Creator: Text placement incorrect when letter spacing is adjusted. #293

Closed gunnsth closed 4 years ago

gunnsth commented 4 years ago

Description

A bug was reported via email:

If I change the letter spacing, the result is incorrect line breaks, not at the end of the line, and the paragraph height is incorrectly calculated

Expected Behavior

Expected text to naturally within the area provided.

Actual Behavior

image

Steps to reproduce the behavior:

  1. Run the provided code example
  2. Observe the output

Attachments

Include a self-contained reproducible code snippet and PDF file that demonstrates the issue.

package main

import (
    "bytes"

    "github.com/unidoc/unipdf/v3/creator"
)

func main() {
    c := creator.New()
    c.SetPageSize(creator.PageSize{180, 90})

    posX, posY := 10., 10.
    p := c.NewStyledParagraph()
    p.SetPos(posX, posY)
    p.SetWidth(116)
    chunk := p.Append("s o m e t e x t s o m e t e x t s o m e t e x t s o m e t e x t s o m e t e x t")
    chunk.Style.FontSize = 6
    chunk.Style.CharSpacing = 6

    border := c.NewRectangle(posX, posY, p.Width(), p.Height())
    border.SetBorderWidth(.3)
    border.SetBorderColor(creator.ColorRed)

    if err := c.Draw(border); err != nil {
        panic(err)
    }

    if err := c.Draw(p); err != nil {
        panic(err)
    }

    var buf bytes.Buffer
    c.Write(&buf)
    filestore.Save("result.pdf", &buf)
}

or view in the playground: https://play.unidoc.io/p/97cc1cabdc995e56