fix: align text annotations in captures
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -519,7 +521,8 @@ func (a *App) captureSelectedPNG(result CaptureResult, pc *pendingCapture) ([]by
|
||||
return nil, err
|
||||
}
|
||||
if len(result.Annotations) > 0 {
|
||||
cropped, err = application.ApplyAnnotations(cropped, result.Annotations, pc.Display.Scale)
|
||||
scaleX, scaleY := annotationScalesForCapture(cropped, rect, pc.Display.Scale)
|
||||
cropped, err = application.ApplyAnnotationsWithScale(cropped, result.Annotations, scaleX, scaleY)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -527,6 +530,33 @@ func (a *App) captureSelectedPNG(result CaptureResult, pc *pendingCapture) ([]by
|
||||
return cropped, nil
|
||||
}
|
||||
|
||||
func annotationScalesForCapture(pngBytes []byte, rect RegionRect, fallback float64) (float64, float64) {
|
||||
if fallback <= 0 {
|
||||
fallback = 1
|
||||
}
|
||||
scaleX := fallback
|
||||
scaleY := fallback
|
||||
|
||||
cfg, err := png.DecodeConfig(bytes.NewReader(pngBytes))
|
||||
if err != nil {
|
||||
return scaleX, scaleY
|
||||
}
|
||||
if rect.W > 0 && cfg.Width > 0 {
|
||||
scaleX = saneAnnotationScale(float64(cfg.Width)/float64(rect.W), fallback)
|
||||
}
|
||||
if rect.H > 0 && cfg.Height > 0 {
|
||||
scaleY = saneAnnotationScale(float64(cfg.Height)/float64(rect.H), fallback)
|
||||
}
|
||||
return scaleX, scaleY
|
||||
}
|
||||
|
||||
func saneAnnotationScale(scale, fallback float64) float64 {
|
||||
if scale >= 0.25 && scale <= 8 {
|
||||
return scale
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func (a *App) chooseSaveDirectory() (string, error) {
|
||||
return wruntime.OpenDirectoryDialog(a.ctx, wruntime.OpenDialogOptions{
|
||||
Title: "Save screenshot to folder",
|
||||
|
||||
Reference in New Issue
Block a user