word wrap implemented
This commit is contained in:
parent
315dff9066
commit
e48cc03e84
40
context.go
40
context.go
@ -36,6 +36,14 @@ const (
|
||||
FillRuleEvenOdd
|
||||
)
|
||||
|
||||
type Align int
|
||||
|
||||
const (
|
||||
AlignLeft Align = iota
|
||||
AlignCenter
|
||||
AlignRight
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
width int
|
||||
height int
|
||||
@ -504,11 +512,29 @@ func (dc *Context) DrawStringAnchored(s string, x, y, ax, ay float64) {
|
||||
d.DrawString(s)
|
||||
}
|
||||
|
||||
func (dc *Context) DrawStringWrapped(s string, x, y, w float64) {
|
||||
lines := wordWrap(dc, s, w)
|
||||
// DrawStringWrapped word-wraps the specified string to the given max width
|
||||
// and then draws it at the specified anchor point using the given line
|
||||
// spacing and text alignment.
|
||||
func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align) {
|
||||
lines := dc.WordWrap(s, width)
|
||||
h := float64(len(lines)) * dc.fontHeight * lineSpacing
|
||||
h -= (lineSpacing - 1) * dc.fontHeight
|
||||
x -= ax * width
|
||||
y -= ay * h
|
||||
switch align {
|
||||
case AlignLeft:
|
||||
ax = 0
|
||||
case AlignCenter:
|
||||
ax = 0.5
|
||||
x += width / 2
|
||||
case AlignRight:
|
||||
ax = 1
|
||||
x += width
|
||||
}
|
||||
ay = 1
|
||||
for _, line := range lines {
|
||||
dc.DrawStringAnchored(line, x, y, 0, 0)
|
||||
y += dc.fontHeight * 1.5
|
||||
dc.DrawStringAnchored(line, x, y, ax, ay)
|
||||
y += dc.fontHeight * lineSpacing
|
||||
}
|
||||
}
|
||||
|
||||
@ -522,6 +548,12 @@ func (dc *Context) MeasureString(s string) (w, h float64) {
|
||||
return float64(a >> 6), dc.fontHeight
|
||||
}
|
||||
|
||||
// WordWrap wraps the specified string to the given max width and current
|
||||
// font face.
|
||||
func (dc *Context) WordWrap(s string, w float64) []string {
|
||||
return wordWrap(dc, s, w)
|
||||
}
|
||||
|
||||
// Transformation Matrix Operations
|
||||
|
||||
// Identity resets the current transformation matrix to the identity matrix.
|
||||
|
36
examples/wrap.go
Normal file
36
examples/wrap.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import "github.com/fogleman/gg"
|
||||
|
||||
const TEXT = "Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."
|
||||
|
||||
func main() {
|
||||
const W = 1024
|
||||
const H = 1024
|
||||
const P = 16
|
||||
dc := gg.NewContext(W, H)
|
||||
dc.SetRGB(1, 1, 1)
|
||||
dc.Clear()
|
||||
dc.DrawLine(W/2, 0, W/2, H)
|
||||
dc.DrawLine(0, H/2, W, H/2)
|
||||
dc.DrawRectangle(P, P, W-P-P, H-P-P)
|
||||
dc.SetRGBA(0, 0, 1, 0.25)
|
||||
dc.SetLineWidth(3)
|
||||
dc.Stroke()
|
||||
dc.SetRGB(0, 0, 0)
|
||||
dc.LoadFontFace("/Library/Fonts/Arial Bold.ttf", 18)
|
||||
dc.DrawStringWrapped("UPPER LEFT", P, P, 0, 0, 0, 1.5, gg.AlignLeft)
|
||||
dc.DrawStringWrapped("UPPER RIGHT", W-P, P, 1, 0, 0, 1.5, gg.AlignRight)
|
||||
dc.DrawStringWrapped("BOTTOM LEFT", P, H-P, 0, 1, 0, 1.5, gg.AlignLeft)
|
||||
dc.DrawStringWrapped("BOTTOM RIGHT", W-P, H-P, 1, 1, 0, 1.5, gg.AlignRight)
|
||||
dc.DrawStringWrapped("UPPER MIDDLE", W/2, P, 0.5, 0, 0, 1.5, gg.AlignCenter)
|
||||
dc.DrawStringWrapped("LOWER MIDDLE", W/2, H-P, 0.5, 1, 0, 1.5, gg.AlignCenter)
|
||||
dc.DrawStringWrapped("LEFT MIDDLE", P, H/2, 0, 0.5, 0, 1.5, gg.AlignLeft)
|
||||
dc.DrawStringWrapped("RIGHT MIDDLE", W-P, H/2, 1, 0.5, 0, 1.5, gg.AlignRight)
|
||||
dc.LoadFontFace("/Library/Fonts/Arial.ttf", 12)
|
||||
dc.DrawStringWrapped(TEXT, W/2-P, H/2-P, 1, 1, W/3, 1.75, gg.AlignLeft)
|
||||
dc.DrawStringWrapped(TEXT, W/2+P, H/2-P, 0, 1, W/3, 2, gg.AlignLeft)
|
||||
dc.DrawStringWrapped(TEXT, W/2-P, H/2+P, 1, 0, W/3, 2.25, gg.AlignLeft)
|
||||
dc.DrawStringWrapped(TEXT, W/2+P, H/2+P, 0, 0, W/3, 2.5, gg.AlignLeft)
|
||||
dc.SavePNG("out.png")
|
||||
}
|
29
wrap.go
29
wrap.go
@ -29,29 +29,26 @@ func wordWrap(m measureStringer, s string, width float64) []string {
|
||||
var result []string
|
||||
for _, line := range strings.Split(s, "\n") {
|
||||
fields := splitOnSpace(line)
|
||||
widths := make([]float64, len(fields))
|
||||
for i, field := range fields {
|
||||
widths[i], _ = m.MeasureString(field)
|
||||
if len(fields)%2 == 1 {
|
||||
fields = append(fields, "")
|
||||
}
|
||||
start := 0
|
||||
total := 0.0
|
||||
x := ""
|
||||
for i := 0; i < len(fields); i += 2 {
|
||||
if total+widths[i] > width {
|
||||
if i == start {
|
||||
end := i + 2
|
||||
result = append(result, strings.Join(fields[start:end], ""))
|
||||
start, total = end, 0
|
||||
w, _ := m.MeasureString(x + fields[i])
|
||||
if w > width {
|
||||
if x == "" {
|
||||
result = append(result, fields[i])
|
||||
x = ""
|
||||
continue
|
||||
} else {
|
||||
end := i
|
||||
result = append(result, strings.Join(fields[start:end], ""))
|
||||
start, total = end, 0
|
||||
result = append(result, x)
|
||||
x = ""
|
||||
}
|
||||
}
|
||||
total += widths[i] + widths[i+1]
|
||||
x += fields[i] + fields[i+1]
|
||||
}
|
||||
if start < len(fields) {
|
||||
result = append(result, strings.Join(fields[start:], ""))
|
||||
if x != "" {
|
||||
result = append(result, x)
|
||||
}
|
||||
}
|
||||
for i, line := range result {
|
||||
|
Loading…
Reference in New Issue
Block a user