-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdpi_darwin.go
More file actions
68 lines (58 loc) · 1.89 KB
/
dpi_darwin.go
File metadata and controls
68 lines (58 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//go:build darwin
package main
import (
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
var reLPResolution = regexp.MustCompile(`(?i)resolution.*?(\d{3})`)
// queryDriverDPI queries the CUPS driver for a printer's DPI on macOS.
// Uses lpoptions -p <name> -l to list available options and find Resolution.
func queryDriverDPI(printerName string) (int, bool) {
// Normalize printer name for CUPS (spaces become dashes)
cupsName := strings.ReplaceAll(printerName, " ", "-")
out, err := exec.Command("lpoptions", "-p", cupsName, "-l").Output()
if err != nil {
log.Printf("[dpi] lpoptions for %s failed: %v", cupsName, err)
return 0, false
}
response := string(out)
// Look for Resolution option line, e.g.:
// Resolution/Resolution: *203dpi 300dpi
// Resolution/Output Resolution: 203x203dpi *300x300dpi
for _, line := range strings.Split(response, "\n") {
lower := strings.ToLower(line)
if !strings.Contains(lower, "resolution") {
continue
}
// Find the default value (marked with *)
parts := strings.SplitN(line, ":", 2)
if len(parts) < 2 {
continue
}
options := strings.Fields(parts[1])
for _, opt := range options {
if strings.HasPrefix(opt, "*") {
// Extract numeric DPI from the default option, e.g. "*203dpi" or "*300x300dpi"
numStr := strings.TrimPrefix(opt, "*")
numStr = strings.Split(numStr, "x")[0]
numStr = strings.Split(numStr, "d")[0]
numStr = strings.Split(numStr, "D")[0]
if dpi, err := strconv.Atoi(numStr); err == nil && dpi > 0 {
log.Printf("[dpi] CUPS driver reports %s DPI=%d", cupsName, dpi)
return dpi, true
}
}
}
// Fallback: try regex on the whole line
if m := reLPResolution.FindStringSubmatch(line); len(m) > 1 {
if dpi, err := strconv.Atoi(m[1]); err == nil && dpi > 0 {
log.Printf("[dpi] CUPS driver (regex) reports %s DPI=%d", cupsName, dpi)
return dpi, true
}
}
}
return 0, false
}