Design Pattern for Image and Text Composition in Go

Design Pattern for Image and Text Composition in Go

Go Conference Tokyo 2019 Spring

5bfed9aa3a9ebccb0c0f0cb65ee9e012?s=128

Seiji Takahashi

May 19, 2019
Tweet

Transcript

  1. 1.

    Design pattern 
 for Image and text composition in Go

    Go Conference 2019 Spring
 @__timakin__
  2. 4.

    There are so much stuff to do when you composite

    image resources and generate a new one.
  3. 5.

    Scope for this talk is … > Workflow of image

    composition > How to implement them along with the separation of concerns.
  4. 7.

    Not scope for this talk ... > The detail of

    the image package > Image processing except composition
  5. 10.

    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. 12.

    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. 13.

    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. 15.

    Workflow of composition > To generate this name card, 


    you have to • Initialize a frame • Draw images • Attach labels • Encode a jpeg output
  9. 16.
  10. 18.

    Processor > Loads fixture images and pass
 them to Drawer.

    > Includes Framer, Drawer,
 Encoder as fields.
  11. 20.

    Insertion of dependent structs > Optional values • Main Thumbnail

    • Sub image • Background image • Labels • Color • Font family/size • Output path
  12. 21.

    Framer > Initializes *image.RGBA 
 as a canvas. > Width

    & height, and 
 a background color are required.
  13. 24.

    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.
  14. 25.

    Call drawing and labeling functions inside Sometimes you have to

    guard an unexpected-sized images, so drawing function often contains a resizer.
  15. 26.

    Labeler > Labeler attaches a text label with the options.

    (coordinate, font- family, text alignment etc)
  16. 27.

    Loading assets and font.Drawer > After loading font assets, call

    truetype font parser with options of size and color.
  17. 29.

    Encoder > Encoder just calls encode function the image package.

    > The role of this interface is to abstract the difference between each formats.
  18. 30.
  19. 31.

    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.