package receipt import ( "context" "fmt" "image" "image/color" "net/url" "path" "uberbringer/internal/pkg/escpos" ) // Preview is what a job prints, read back from its command stream. type Preview struct { // Columns is the number of Font A characters on a line. Columns int CodePage escpos.CodePage // LineCount is the number of lines the job prints, all copies counted: // text lines, fed lines, and one for each QR code, barcode and image. // Options.MaxLines limits it. LineCount int // Lines are the printed lines. Image lines name the image's source // instead of its size, since a preview loads no images. Lines []escpos.PreviewLine // Warnings name the elements with characters the code page lacks and // images that would fail to print. Warnings []string } // Preview validates and renders job as Render does, then reads the command // stream back with escpos.Interpret, so that it shows what the bytes say. It // neither fetches nor decodes images: each prints as a blank placeholder. A // job over Options.MaxLines fails with a *LineLimitError, as it does when it // prints. func (r *Renderer) Preview(ctx context.Context, job Job) (Preview, error) { if err := job.Validate(); err != nil { return Preview{}, err } if err := ctx.Err(); err != nil { return Preview{}, err } images, imageWarnings := r.placeholderImages(job) out, err := r.render(job, images) if err != nil { return Preview{}, err } lines, err := escpos.Interpret(out.stream, r.options.CodePage, r.options.Profile, out.raw) if err != nil { return Preview{}, fmt.Errorf("reading back the rendered receipt: %w", err) } labelImages(lines, job, r.options.Profile.DotsPerLine) var warnings []string for i := range job.Elements { if w, ok := imageWarnings[i]; ok { warnings = append(warnings, w) } if n := out.unknown[i]; n > 0 { noun := "characters" if n == 1 { noun = "character" } warnings = append(warnings, fmt.Sprintf("elements[%d]: %d %s not in %s printed as ?", i, n, noun, r.options.CodePage)) } } return Preview{ Columns: r.textLineDots() / r.options.Profile.FontAWidth, CodePage: r.options.CodePage, LineCount: out.lines, Lines: lines, Warnings: warnings, }, nil } // placeholderImages returns a blank one-row raster across the paper for // every image element, and a warning for each image that would fail to // print because it is wider than the paper. An optional image that is // wider shrinks to the paper instead. func (r *Renderer) placeholderImages(job Job) (map[int]*image.Paletted, map[int]string) { paper := r.options.Profile.DotsPerLine images := make(map[int]*image.Paletted) warnings := make(map[int]string) for i, el := range job.Elements { if el.Type != TypeImage { continue } if el.Width > paper && !el.Optional { warnings[i] = fmt.Sprintf("elements[%d] (image): width %d is wider than the %d-dot paper, so the job would fail", i, el.Width, paper) } images[i] = image.NewPaletted(image.Rect(0, 0, paper, 1), color.Palette{color.White, color.Black}) } return images, warnings } // labelImages replaces the size of each image placeholder with the source // of its element, in order, copy after copy. An optional image wider than // the paper, paper dots, is labelled with the width it prints at. func labelImages(lines []escpos.PreviewLine, job Job, paper int) { var sources []Element for _, el := range job.Elements { if el.Type == TypeImage { sources = append(sources, el) } } n := 0 for i := range lines { if lines[i].Kind != escpos.LineImage || len(sources) == 0 { continue } el := sources[n%len(sources)] n++ name, data := "base64", "base64" if el.URL != "" { name, data = imageName(el.URL), el.URL } lines[i].Text = fmt.Sprintf("[image: %s]", name) if width := el.Width; width > 0 { if el.Optional { width = min(width, paper) } lines[i].Text = fmt.Sprintf("[image %d dots: %s]", width, name) } lines[i].Data = data } } // imageName is the last path segment of an image URL, or its host. func imageName(raw string) string { u, err := url.Parse(raw) if err != nil { return raw } if name := path.Base(u.Path); name != "." && name != "/" { return name } return u.Host }