thumbnail

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package thumbnail draws a thumbnail image of a BDF document: the top of the first page of a text document, the whole first slide or drawing, or the top-left corner of a spreadsheet.

The layout follows the kind of document (Meta.Source, then the view):

  • Crop (Word, HTML, Markdown, Excel, CSV, Parquet, scores from MML, MIDI and MusicXML, font files, whose overview starts with the name and a specimen, audio files, whose card starts with the cover; PDF and TIFF pages taller than wide): a square from the top-left corner of the first page, as wide as the page, scaled to Size × Size. A scroll view is cut from its top; a sheet from cell A1, with gridlines: the square is Size pixels at SheetDPI, so that a smaller thumbnail shows fewer cells instead of smaller ones, from MinSheetSide to MaxSheetSide units and no larger than the sheet.
  • Fit (PowerPoint, Visio, draw.io, CAD drawings and plots, circuit boards, Illustrator, Photoshop, images, EPUB covers; other PDF and TIFF pages): the whole first page scaled so that its longer side is Size pixels.

A thumbnail shows the first page of a view only: a document converted for its thumbnail alone needs no more than that page, converted with converter.Options{Pages: converter.PageList(1)} (the whole of a long PDF takes seconds, its first page milliseconds).

Thumbnails show the content of a document outside the document: whoever makes one of a password-protected input should keep it as protected as the input (the bdf command does not make one unless asked to).

Index

Constants

View Source
const (
	MinSheetSide = 96
	MaxSheetSide = 480
)

MinSheetSide and MaxSheetSide bound the square of a sheet a thumbnail shows, in units: a small thumbnail still shows a few rows and columns (6 rows of 15 units), a large one shows the corner at A1 in more detail.

View Source
const (
	PNG  = "png"
	JPEG = "jpeg"
	WebP = "webp"
)

Formats of Encode.

View Source
const DefaultSheetDPI = 72

DefaultSheetDPI is the default resolution of a sheet in a thumbnail: a unit (1/72 inch) is a pixel.

View Source
const DefaultSize = 256

DefaultSize is the default side of a thumbnail in pixels.

Variables

This section is empty.

Functions

func Encode

func Encode(w io.Writer, img *image.RGBA, format string) error

Encode writes a thumbnail as PNG, JPEG (quality 85) or WebP (lossy, quality 80).

func FormatOf

func FormatOf(name string) string

FormatOf returns the image format a file name asks for by its extension (.png, .jpg or .jpeg, .webp), or "".

Types

type Mode

type Mode int

Mode is how a page becomes a thumbnail.

const (
	// Auto picks Crop or Fit from the kind of document.
	Auto Mode = iota
	// Crop cuts a square from the top-left corner of the first page.
	Crop
	// Fit scales the whole first page into the thumbnail.
	Fit
)

func ParseMode

func ParseMode(s string) (Mode, error)

ParseMode reads a mode name: auto, crop or fit.

func (Mode) String

func (m Mode) String() string

type Options

type Options struct {
	// Size is the side of a cropped thumbnail and the longer side of a
	// fitted one, in pixels (default DefaultSize).
	Size int
	// Mode picks the layout (default Auto).
	Mode Mode
	// View is the id of the view to draw (default: the first one).
	View string
	// SheetDPI is the resolution a sheet is drawn at (default
	// DefaultSheetDPI): the square from A1 is Size / SheetDPI inches,
	// bounded by MinSheetSide and MaxSheetSide. A lower value shows more
	// cells, smaller.
	SheetDPI float64
	// Raster configures the drawing: fonts and background.
	Raster imagebdf.Options
}

Options configures a thumbnail.

type Result

type Result struct {
	Image *image.RGBA
	// Mode is the layout used: Crop or Fit.
	Mode Mode
	// Warnings lists what the drawing left out (see imagebdf).
	Warnings []string
}

Result is a thumbnail and how it was made.

func Make

func Make(doc *bdf.Document, opts *Options) (res *Result, err error)

Make draws the thumbnail of a document. What a document asks for beyond the limits of package imagebdf is left out, with a warning; a document that cannot be drawn at all is an error, and so is a panic of the drawing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL