typeset/examples/test/main.go
2024-09-20 00:13:01 -04:00

166 lines
7.8 KiB
Go

// Example test demonstrates a variety of ways that TypeSetter can arrange text.
package main
import "os"
import "image"
import "image/png"
import "image/draw"
import "image/color"
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/tomo/typeset"
import "golang.org/x/image/font/basicfont"
import "git.tebibyte.media/tomo/typeset/debug"
func main () {
img := image.NewRGBA(image.Rect(0, 0, 2048, 1024))
setter := typeset.TypeSetter { }
setter.SetWrap(true)
setter.SetFace(basicfont.Face7x13)
setter.SetText(lipsum)
setter.SetAlign(typeset.AlignStart, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignStart)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignStart)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Inset(4))
setter.SetText(dimple)
setter.SetAlign(typeset.AlignStart, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(512, 0)).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignStart)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(512, 0)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(512, 0)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignStart)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(512, 0)).Inset(4))
setter.SetAlign(typeset.AlignStart, typeset.AlignMiddle)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(0, 512)).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignMiddle)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(0, 512)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignMiddle)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(0, 512)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignMiddle)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(0, 512)).Inset(4))
setter.SetAlign(typeset.AlignStart, typeset.AlignEnd)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(512, 512)).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignEnd)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(512, 512)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignEnd)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(512, 512)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignEnd)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(512, 512)).Inset(4))
setter.SetText(haiku)
setter.SetWrap(false)
setter.SetAlign(typeset.AlignStart, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(1024, 0)).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignStart)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(1024, 0)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(1024, 0)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignStart)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(1024, 0)).Inset(4))
setter.SetAlign(typeset.AlignStart, typeset.AlignMiddle)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(1536, 0)).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignMiddle)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(1536, 0)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignMiddle)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(1536, 0)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignMiddle)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(1536, 0)).Inset(4))
setter.SetAlign(typeset.AlignStart, typeset.AlignEnd)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(1024, 512)).Inset(4))
setter.SetAlign(typeset.AlignMiddle, typeset.AlignEnd)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(1024, 512)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignEnd)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(1024, 512)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignEnd)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(1024, 512)).Inset(4))
setter.SetText(haikuAlt)
setter.SetAlign(typeset.AlignEven, typeset.AlignStart)
drawText(img, &setter, image.Rect( 0, 0, 256, 256).Add(image.Pt(1536, 512)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignMiddle)
drawText(img, &setter, image.Rect(256, 0, 512, 256).Add(image.Pt(1536, 512)).Inset(4))
setter.SetAlign(typeset.AlignEven, typeset.AlignEnd)
drawText(img, &setter, image.Rect( 0, 256, 256, 512).Add(image.Pt(1536, 512)).Inset(4))
setter.SetAlign(typeset.AlignEnd, typeset.AlignMiddle)
drawText(img, &setter, image.Rect(256, 256, 512, 512).Add(image.Pt(1536, 512)).Inset(4))
file, err := os.Create("output.png")
if err != nil { panic(err) }
defer file.Close()
err = png.Encode(file, img)
if err != nil { panic(err) }
}
type subDrawImage interface {
draw.Image
SubImage (image.Rectangle) image.Image
}
func drawText (destination subDrawImage, setter *typeset.TypeSetter, bounds image.Rectangle) {
whiteRectangle(destination, bounds)
subImage := destination.SubImage(bounds).(draw.Image)
metrics := setter.Face().Metrics()
bounds = bounds.Inset(16)
grayRectangle(destination, bounds)
size := fixed.P(bounds.Dx(), bounds.Dy())
setter.SetSize(size)
offset := fixed.Point26_6 {
X: fixed.I(bounds.Min.X),
Y: metrics.Ascent + fixed.I(bounds.Min.Y),
}
// typeset.Draw(destination, setter, offset, color.Black)
debug.DrawBounds(subImage, setter, offset)
typeset.Draw(subImage, setter, offset, color.Black)
}
func whiteRectangle (destination draw.Image, rect image.Rectangle) {
draw.Over.Draw(destination, rect, image.NewUniform(color.RGBA { R: 150, G: 150, B: 150, A: 255 }), image.Pt(0, 0))
}
func grayRectangle (destination draw.Image, rect image.Rectangle) {
draw.Over.Draw(destination, rect, image.NewUniform(color.RGBA { R: 200, G: 200, B: 200, A: 255 }), image.Pt(0, 0))
}
const lipsum = `Eum officia beatae harum. Rem aut praesentium possimus dignissimos ea sed. Recusandae sint rerum ut. Qui delectus rerum ut ut. Nobis non veritatis consequatur quia explicabo id. Et aut qui reiciendis esse voluptatem.
Eaque rem incidunt porro unde quia expedita quia. Deleniti repellat modi placeat. Et beatae aut voluptatem. Veritatis perspiciatis et aperiam sit modi sequi.
Accusantium et fugit expedita consequatur incidunt explicabo ea voluptatibus. Debitis consectetur veniam ut et esse aspernatur. Quas occaecati explicabo consequuntur. Quae dolorem ea harum ut tempora. Corporis ducimus et voluptatem. Corporis distinctio quia velit accusantium sunt omnis.
Libero blanditiis aut aut exercitationem modi. Eum corporis quam facere. Perferendis sit nulla et qui repellat eaque neque in. Expedita quidem similique sunt delectus similique non assumenda.
Hic rerum earum sapiente et itaque harum. Itaque amet voluptatem aliquid. Et qui excepturi animi voluptatem debitis necessitatibus atque animi. Nemo voluptates delectus quisquam non. Ipsam error voluptas similique dolores odit quos.`
const dimple = `I have been trying to remember this for a while
I could never place it
My brain kept saying Dimple when I tried to remember. Ever closer yet unobtainable. Censored, even. It did not want me to remember the dumple...`
const haiku = `An ocean voyage.
As waves break over the bow,
the sea welcomes me.
This is a very long line that will probably get cut off.`
const haikuAlt = `An ocean voyage.
As waves break over the bow,
the sea welcomes me.
This is a short ending`