$30 off During Our Annual Pro Sale. View Details »

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
Tweet

More Decks by Seiji Takahashi

Other Decks in Programming

Transcript

  1. Design pattern 

    for Image and text
    composition in Go
    Go Conference 2019 Spring

    @__timakin__

    View Slide

  2. SEIJI TAKAHASHI
    Software Engineer at Gunosy Inc.

    View Slide

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

    View Slide

  4. There are so much stuff to do when you
    composite image resources and
    generate a new one.

    View Slide

  5. Scope for this talk is …
    > Workflow of image composition
    > How to implement them along with the separation of
    concerns.

    View Slide

  6. Scope for this talk is …
    Timakin
    Timakin

    View Slide

  7. Not scope for this talk ...
    > The detail of the image package
    > Image processing except composition

    View Slide

  8. Image and font packages

    View Slide

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

    View Slide

  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.

    View Slide

  11. e.g. Draw a red rectangle

    View Slide

  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.

    View Slide

  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…

    View Slide

  14. Design pattern of image composition

    View Slide

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

    you have to
    • Initialize a frame
    • Draw images
    • Attach labels
    • Encode a jpeg output

    View Slide

  16. Interfaces
    Processor
    Initialize a frame
    Draw images
    Attach labels
    Encode a jpeg output
    Framer
    Drawer
    Labeler
    Encoder

    View Slide

  17. Roles of each structs
    Framer Drawer Labeler Encoder

    View Slide

  18. Processor
    > Loads fixture images and pass

    them to Drawer.
    > Includes Framer, Drawer,

    Encoder as fields.

    View Slide

  19. Loading fixtures
    > github.com/rakyll/statik is better if your fixtures are not
    so large to save the loading time.

    View Slide

  20. Insertion of dependent structs
    > Optional values
    • Main Thumbnail
    • Sub image
    • Background image
    • Labels
    • Color
    • Font family/size
    • Output path

    View Slide

  21. Framer
    > Initializes *image.RGBA 

    as a canvas.
    > Width & height, and 

    a background color are required.

    View Slide

  22. Fill the RGBA frame with colors

    View Slide

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

    View Slide

  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.

    View Slide

  25. Call drawing and labeling functions inside
    Sometimes you have to guard an
    unexpected-sized images, so drawing
    function often contains a resizer.

    View Slide

  26. Labeler
    > Labeler attaches a
    text label with the
    options.
    (coordinate, font-
    family, text
    alignment etc)

    View Slide

  27. Loading assets and font.Drawer
    > After loading font assets,
    call truetype font parser
    with options of size and
    color.

    View Slide

  28. A minor adjustment of text position 

    with fixed.PointXX_xx

    View Slide

  29. Encoder
    > Encoder just calls encode
    function the image package.
    > The role of this interface is to
    abstract the difference between
    each formats.

    View Slide

  30. View Slide

  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.

    View Slide

  32. Thank you for listening

    View Slide