Slide 1

Slide 1 text

Design pattern 
 for Image and text composition in Go Go Conference 2019 Spring
 @__timakin__

Slide 2

Slide 2 text

SEIJI TAKAHASHI Software Engineer at Gunosy Inc.

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Scope for this talk is … Timakin Timakin

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Image and font packages

Slide 9

Slide 9 text

Quick Question Have you ever used the image package?

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

e.g. Draw a red rectangle

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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…

Slide 14

Slide 14 text

Design pattern of image composition

Slide 15

Slide 15 text

Workflow of composition > To generate this name card, 
 you have to • Initialize a frame • Draw images • Attach labels • Encode a jpeg output

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Roles of each structs Framer Drawer Labeler Encoder

Slide 18

Slide 18 text

Processor > Loads fixture images and pass
 them to Drawer. > Includes Framer, Drawer,
 Encoder as fields.

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Framer > Initializes *image.RGBA 
 as a canvas. > Width & height, and 
 a background color are required.

Slide 22

Slide 22 text

Fill the RGBA frame with colors

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

A minor adjustment of text position 
 with fixed.PointXX_xx

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

Thank you for listening