Save 37% off PRO during our Black Friday Sale! »

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. Design pattern 
 for Image and text composition in Go

    Go Conference 2019 Spring
 @__timakin__
  2. SEIJI TAKAHASHI Software Engineer at Gunosy Inc.

  3. Agenda > Image and font packages > Design pattern for

    composition > Conclusions
  4. There are so much stuff to do when you composite

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

    composition > How to implement them along with the separation of concerns.
  6. Scope for this talk is … Timakin Timakin

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

    the image package > Image processing except composition
  8. Image and font packages

  9. Quick Question Have you ever used the image package?

  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.
  11. e.g. Draw a red rectangle

  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.
  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…
  14. Design pattern of image composition

  15. Workflow of composition > To generate this name card, 


    you have to • Initialize a frame • Draw images • Attach labels • Encode a jpeg output
  16. Interfaces Processor Initialize a frame Draw images Attach labels Encode

    a jpeg output Framer Drawer Labeler Encoder
  17. Roles of each structs Framer Drawer Labeler Encoder

  18. Processor > Loads fixture images and pass
 them to Drawer.

    > Includes Framer, Drawer,
 Encoder as fields.
  19. Loading fixtures > github.com/rakyll/statik is better if your fixtures are

    not so large to save the loading time.
  20. Insertion of dependent structs > Optional values • Main Thumbnail

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

    & height, and 
 a background color are required.
  22. Fill the RGBA frame with colors

  23. Drawer > Receiving the frame, drawer draws fixture and specified

    images.
  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.
  25. Call drawing and labeling functions inside Sometimes you have to

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

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

    truetype font parser with options of size and color.
  28. A minor adjustment of text position 
 with fixed.PointXX_xx

  29. Encoder > Encoder just calls encode function the image package.

    > The role of this interface is to abstract the difference between each formats.
  30. None
  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.
  32. Thank you for listening