tdewolff / canvas

Cairo in Go: vector to raster, SVG, PDF, EPS, WASM, OpenGL, Gio, etc.
MIT License
1.51k stars 103 forks source link

break line incorrect at the first line #265

Closed jilieryuyi closed 10 months ago

jilieryuyi commented 10 months ago

image

tdewolff commented 10 months ago

Thanks for the issue, but again, can you be more specific and informative? I suppose you mean that the first line breaks earlier than the second line; both lines should be of equal length?

A quick test locally works as it should, can you provide a small code example and include the font file?

jilieryuyi commented 10 months ago

Thanks for the issue, but again, can you be more specific and informative? I suppose you mean that the first line breaks earlier than the second line; both lines should be of equal length?

A quick test locally works as it should, can you provide a small code example and include the font file?

var fontLatin *canvas.FontFamily

func main() {

    t0 := time.Now()
    fontLatin = canvas.NewFontFamily("微软雅黑")

    if err := fontLatin.LoadSystemFont("微软雅黑", canvas.FontBold); err != nil {
        panic(err)
    }

    f, err := os.Create("document.pdf")
    if err != nil {
        panic(err)
    }
    defer f.Close()

    p := pdf.New(f, 210, 297, nil)

    c1 := canvas.New(210, 297)
    ctx1 := canvas.NewContext(c1)
    ctx1.SetFillColor(canvas.Beige)
    ctx1.DrawPath(0, 0, canvas.Rectangle(210, 297))
    drawDocument(ctx1)
    c1.RenderTo(p)

    if err := p.Close(); err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", time.Now().Sub(t0).Round(time.Millisecond))
}

var lorem = []string{
    `执行送出执行送出执行送出执行送出执行送出执行送出执行送出执行送出执行送出执行送出执行送出执行送出执行送出出执行送出执行送出执行送出执行送出出执行送出执行送出执行送出执行送出出执行送出执行送出执行送出执行送出`,
}

const lenna = "lenna.png"

var y = 290.0

func drawTextAndMoveDown(c *canvas.Context, x float64, text *canvas.Text) {
    c.DrawText(x, y, text)
    const spacing = 5
    y -= text.Bounds().H + spacing
}

func drawDocument(c *canvas.Context) {
    y = 290.0
    c.SetFillColor(canvas.Black)

    text12Face := fontLatin.Face(12.0, canvas.Black, canvas.FontRegular, canvas.FontNormal)
    var txt *canvas.Text
    for _, t := range lorem {
        txt = canvas.NewTextBox(text12Face, t, 170.0, 0.0, canvas.Left, canvas.Top, 5.0, 0.0)
        drawTextAndMoveDown(c, 20.0, txt)
    }
}
jilieryuyi commented 10 months ago

Annotate this line of code and it is now correct, Does this mean indenting the first line? image image

jilieryuyi commented 10 months ago

chang this code

var lorem = []string{
    `Thanks for the issue, but again, can you be more specific and informative? I suppose you mean that the first line breaks earlier than the second line; both lines should be of equal length?

A quick test locally works as it should, can you provide a small code example and include the font file?`,
}

image image

tdewolff commented 10 months ago

I can reproduce the problem now, it was caused by https://github.com/tdewolff/canvas/commit/a29ef3551aabcda0251c116bc1b7c0f789f9388e, that's more than a year old (oops!)

tdewolff commented 10 months ago

Should be fixed now!

jilieryuyi commented 10 months ago

How to set the first line indentation on the left? image