Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Pattern for Image and Text Composition in Go

Design Pattern for Image and Text Composition in Go

Go Conference Tokyo 2019 Spring

Seiji Takahashi

May 19, 2019

More Decks by Seiji Takahashi

Other Decks in Programming


  1. Design pattern 
 for Image and text composition in Go

    Go Conference 2019 Spring
  2. There are so much stuff to do when you composite

    image resources and generate a new one.
  3. Scope for this talk is … > Workflow of image

    composition > How to implement them along with the separation of concerns.
  4. Not scope for this talk ... > The detail of

    the image package > Image processing except composition
  5. package “image” > Package image implements a basic 2-D image

    library. • Encodes/decodes formats such as GIF, JPEG or PNG. > Provides below interfaces • An inspection of data in the pixels • Adds a color to the specified range • Composition, mask and quantization.
  6. golang.org/x/image/font > Package which defines an interface for font faces,

    for drawing text on an image. > font.Face is an interface that represents a content of the file of font face. > font.Drawer reads it and draws text on a destination image. > Opentype is not suitable for font.Drawer.
  7. github.com/golang/freetype/truetype > Package which provides a parser for the TTF

    and TTC file formats. > When you would like to flexibly set a font face to font.Drawer, this package will really help you. > A lot of .ttf/.ttc files don’t pass the parse process. • PostScript font type • Hiragino Sans…
  8. Workflow of composition > To generate this name card, 

    you have to • Initialize a frame • Draw images • Attach labels • Encode a jpeg output
  9. Processor > Loads fixture images and pass
 them to Drawer.

    > Includes Framer, Drawer,
 Encoder as fields.
  10. Insertion of dependent structs > Optional values • Main Thumbnail

    • Sub image • Background image • Labels • Color • Font family/size • Output path
  11. Framer > Initializes *image.RGBA 
 as a canvas. > Width

    & height, and 
 a background color are required.
  12. Call drawing and labeling functions inside > Labeler is called

    inside of Drawer. • The positions of the texts are calculated by relative value to the images.
  13. Call drawing and labeling functions inside Sometimes you have to

    guard an unexpected-sized images, so drawing function often contains a resizer.
  14. Labeler > Labeler attaches a text label with the options.

    (coordinate, font- family, text alignment etc)
  15. Loading assets and font.Drawer > After loading font assets, call

    truetype font parser with options of size and color.
  16. Encoder > Encoder just calls encode function the image package.

    > The role of this interface is to abstract the difference between each formats.
  17. Conclusion > Image and text compression becomes really messy if

    you don’t care about the separation of concerns. • Separation by the role becomes easy if you simplify the process. > Composition process deeply depends on a coordinate calculation. Designing optional values is the essential for a simple code.