package preprocessor import ( "bufio" "bytes" "github.com/opencloud-eu/opencloud/services/webdav/pkg/thumbnail" "image" "image/draw" "image/png" "io" "math" "mime" "strings" "github.com/pkg/errors" "golang.org/x/image/font" "golang.org/x/image/font/opentype" "golang.org/x/image/math/fixed" ) // FileConverter is the interface for the file converter type FileConverter interface { Convert(r io.Reader) (*Image, error) // Name tells cached results of different extractors apart. Name() string } // Image is what a converter hands on: the bytes and the type they are in. // webdav does not decode them, the generator does the image work, so an // embedded cover reaches it the way a plain image file does. type Image struct { ContentType string Data []byte } // TxtToImageConverter is a converter for the text file type TxtToImageConverter struct { fontLoader *FontLoader } func (TxtToImageConverter) Name() string { return "oc-text" } // Convert reads the text file and renders it into a thumbnail image func (t TxtToImageConverter) Convert(r io.Reader) (*Image, error) { img := image.NewRGBA(image.Rect(0, 0, 640, 480)) imgBounds := img.Bounds() draw.Draw(img, imgBounds, image.White, image.Point{}, draw.Src) fontSizeAsInt := int(math.Ceil(t.fontLoader.GetFaceOptSize())) margin := 10 minX := fixed.I(imgBounds.Min.X + margin) maxX := fixed.I(imgBounds.Max.X - margin) maxY := fixed.I(imgBounds.Max.Y - margin) initialPoint := fixed.P(imgBounds.Min.X+margin, imgBounds.Min.Y+margin+fontSizeAsInt) canvas := &font.Drawer{ Dst: img, Src: image.Black, Dot: initialPoint, } scriptList := t.fontLoader.GetScriptList() textAnalyzer := NewTextAnalyzer(scriptList) taOpts := AnalysisOpts{ UseMergeMap: true, MergeMap: DefaultMergeMap, } scanner := bufio.NewScanner(r) Scan: // Label for the scanner loop, so we can break it easily for scanner.Scan() { txt := scanner.Text() height := fixed.I(fontSizeAsInt) // reset to default height textResult := textAnalyzer.AnalyzeString(txt, taOpts) textResult.MergeCommon(DefaultMergeMap) for _, sRange := range textResult.ScriptRanges { targetFontFace, err := t.fontLoader.LoadFaceForScript(sRange.TargetScript) if err != nil { return nil, err } // if the target script is "_unknown" it's expected that the loaded face // uses the default font faceHeight := targetFontFace.Face.Metrics().Height if faceHeight > height { height = faceHeight } canvas.Face = targetFontFace.Face initialByte := sRange.Low for _, sRangeSpace := range sRange.Spaces { if canvas.Dot.Y > maxY { break Scan } drawWord(canvas, textResult.Text[initialByte:sRangeSpace], minX, maxX, height, maxY) initialByte = sRangeSpace } if initialByte <= sRange.High { // some bytes left to be written if canvas.Dot.Y > maxY { break Scan } drawWord(canvas, textResult.Text[initialByte:sRange.High+1], minX, maxX, height, maxY) } } canvas.Dot.X = minX canvas.Dot.Y += height.Mul(fixed.Int26_6(1<<6 + 1<<5)) // height * 1.5 if canvas.Dot.Y > maxY { break } } if err := scanner.Err(); err != nil { return nil, err } // png, not jpeg: the render goes to the generator untouched and is // compressed once, in the size the client asked for var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { return nil, errors.Wrap(err, `could not encode the rendered text`) } return &Image{ContentType: "image/png", Data: buf.Bytes()}, nil } // Draw the word in the canvas. The mixX and maxX defines the drawable range // (X axis) where the word can be drawn (in case the word is too big and doesn't // fit in the canvas), and the incY defines the increment in the Y axis if we // need to draw the word in a new line // // Note that the word will likely start with a white space char func drawWord(canvas *font.Drawer, word string, minX, maxX, incY, maxY fixed.Int26_6) { // calculate the actual measurement of the string at a given X position measure := func(s string, dotX fixed.Int26_6) (min, max fixed.Int26_6) { bbox, _ := canvas.BoundString(s) return dotX + bbox.Min.X, dotX + bbox.Max.X } // first try to draw the whole word absMin, absMax := measure(word, canvas.Dot.X) if absMin >= minX && absMax <= maxX { canvas.DrawString(word) return } // try to draw the trimmed word in a new line trimmed := strings.TrimSpace(word) oldDot := canvas.Dot canvas.Dot.X = minX canvas.Dot.Y += incY if canvas.Dot.Y <= maxY { tMin, tMax := measure(trimmed, canvas.Dot.X) if tMin >= minX && tMax <= maxX { canvas.DrawString(trimmed) return } } // if the trimmed word is still too big, draw it char by char canvas.Dot = oldDot for _, char := range trimmed { s := string(char) _, cMax := measure(s, canvas.Dot.X) if cMax > maxX { canvas.Dot.X = minX canvas.Dot.Y += incY } // stop drawing if we exceed maxY if canvas.Dot.Y > maxY { return } // ensure that we don't start drawing before minX cMin, _ := measure(s, canvas.Dot.X) if cMin < minX { canvas.Dot.X += minX - cMin } canvas.DrawString(s) } } // Options carries what a converter needs from the caller. The zero value asks // for the defaults. type Options struct { FontMapFile string FontFace *opentype.FaceOptions Tika thumbnail.Tika Filename string } func toTika(opts Options, mimeType string) FileConverter { return TikaThumbnail{tikaURL: opts.Tika.URL, preset: opts.Tika.Preset, filename: opts.Filename, contentType: opts.Tika.ContentType(mimeType)} } // ForType says what has to happen to a file of this type before the generator // sees it: a converter to run, or nil when the file goes to the generator as it // is, which answers whether it can read it. Only types that passed the gate // get here: announced ones, and those the Tika list takes. // // Every type has exactly one taker, the first that applies: // 1. a type the Tika list names goes to Tika, an image too, which is how a raw // photo is extracted instead of being handed to a generator that cannot // read it // 2. plain text is rendered here // 3. an image goes to the generator // 4. what is left goes to Tika when it was told to take everything // 5. and otherwise to the generator // // So "*" never sends an image: images belong to the generator, and sending one // to Tika would move the file for nothing. func ForType(mimeType string, opts Options) FileConverter { // We can ignore the error here because the caller parses it as well, so we // only get here when the mimeType parses fine. mimeType, _, _ = mime.ParseMediaType(mimeType) if opts.Tika.ConfiguredFor(mimeType) { return toTika(opts, mimeType) } switch mimeType { case "text/plain": fontFace := opts.FontFace if fontFace == nil { fontFace = &opentype.FaceOptions{ Size: 12, DPI: 72, Hinting: font.HintingNone, } } fontLoader, err := NewFontLoader(opts.FontMapFile, fontFace) if err != nil { // if it couldn't create the FontLoader with the specified fontFileMap, // try to use the default font fontLoader, _ = NewFontLoader("", fontFace) } return TxtToImageConverter{fontLoader: fontLoader} } if strings.HasPrefix(mimeType, "image/") { return nil } if opts.Tika.TakesAll() { return toTika(opts, mimeType) } return nil }