Stretch barcodes horizontally only; keep configured height.
Width still fills the printable area but vertical scale no longer grows with horizontal stretch. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -108,7 +108,7 @@ Directives start with `@` at the beginning of a line.
108
108
@barcode ITF 12345678 height=80
109
109
```
110
110
111
**Raster** (rendered as bitmap — works with any symbology below). Raster barcodes are always stretched to the full paper width for reliable scanning:
111
**Raster** (rendered as bitmap — works with any symbology below). Raster barcodes are stretched horizontally to the full paper width for reliable scanning; height follows the `height` attribute.
112
112
113
113
```markdown
114
114
@barcode RASTER CODE128 ${OrderId}
@@ -108,7 +108,7 @@ Directives start with `@` at the beginning of a line.108
@barcode ITF 12345678 height=80
108
@barcode ITF 12345678 height=80
109
```
109
```
110
110
111
**Raster** (rendered as bitmap — works with any symbology below). Raster barcodes are always stretched to the full paper width for reliable scanning:
111
**Raster** (rendered as bitmap — works with any symbology below). Raster barcodes are stretched horizontally to the full paper width for reliable scanning; height follows the `height` attribute.
112
112
113
```markdown
113
```markdown
114
@barcode RASTER CODE128 ${OrderId}
114
@barcode RASTER CODE128 ${OrderId}
@@ -84,9 +84,9 @@ pub fn render_barcode(
84
84
Ok(img.to_luma8())
85
85
}
86
86
87
/// Scale a graphic to exactly `target_width` dots (nearest-neighbor).
87
/// Stretch a barcode to exactly `target_width` dots, keeping the original height.
88
88
///
89
/// Barcodes are stretched to the full printable width so module bars stay
89
/// Barcodes are widened to the full printable width so module bars stay
90
90
/// wide enough to scan reliably.
91
91
pub fn scale_gray_to_print_width(gray: GrayImage, target_width: u32) -> GrayImage {
92
92
let (w, h) = gray.dimensions();
@@ -96,11 +96,10 @@ pub fn scale_gray_to_print_width(gray: GrayImage, target_width: u32) -> GrayImag
96
96
if w == target_width {
97
97
return gray;
98
98
}
99
let new_h = ((h as f32) * (target_width as f32 / w as f32)).max(1.0) as u32;
100
99
image::imageops::resize(
101
100
&gray,
102
101
target_width,
103
new_h,
102
h,
104
103
image::imageops::FilterType::Nearest,
105
104
)
106
105
}
@@ -170,9 +169,10 @@ mod tests {
170
169
fn barcode_stretches_to_print_width() {
171
170
let narrow = render_barcode(BarcodeSymbology::Code128, "X", 60).unwrap();
172
171
assert!(narrow.width() < 384);
172
let original_h = narrow.height();
173
173
let wide = scale_gray_to_print_width(narrow, 384);
174
174
assert_eq!(wide.width(), 384);
175
assert!(wide.height() > 60);
175
assert_eq!(wide.height(), original_h);
176
176
}
177
177
178
178
#[test]
@@ -84,9 +84,9 @@ pub fn render_barcode(84
Ok(img.to_luma8())
84
Ok(img.to_luma8())
85
}
85
}
86
86
87
/// Scale a graphic to exactly `target_width` dots (nearest-neighbor).
87
/// Stretch a barcode to exactly `target_width` dots, keeping the original height.
88
///
88
///
89
/// Barcodes are stretched to the full printable width so module bars stay
89
/// Barcodes are widened to the full printable width so module bars stay
90
/// wide enough to scan reliably.
90
/// wide enough to scan reliably.
91
pub fn scale_gray_to_print_width(gray: GrayImage, target_width: u32) -> GrayImage {
91
pub fn scale_gray_to_print_width(gray: GrayImage, target_width: u32) -> GrayImage {
92
let (w, h) = gray.dimensions();
92
let (w, h) = gray.dimensions();
@@ -96,11 +96,10 @@ pub fn scale_gray_to_print_width(gray: GrayImage, target_width: u32) -> GrayImag96
if w == target_width {
96
if w == target_width {
97
return gray;
97
return gray;
98
}
98
}
99
let new_h = ((h as f32) * (target_width as f32 / w as f32)).max(1.0) as u32;
100
image::imageops::resize(
99
image::imageops::resize(
101
&gray,
100
&gray,
102
target_width,
101
target_width,
103
new_h,
102
h,
104
image::imageops::FilterType::Nearest,
103
image::imageops::FilterType::Nearest,
105
)
104
)
106
}
105
}
@@ -170,9 +169,10 @@ mod tests {170
fn barcode_stretches_to_print_width() {
169
fn barcode_stretches_to_print_width() {
171
let narrow = render_barcode(BarcodeSymbology::Code128, "X", 60).unwrap();
170
let narrow = render_barcode(BarcodeSymbology::Code128, "X", 60).unwrap();
172
assert!(narrow.width() < 384);
171
assert!(narrow.width() < 384);
172
let original_h = narrow.height();
173
let wide = scale_gray_to_print_width(narrow, 384);
173
let wide = scale_gray_to_print_width(narrow, 384);
174
assert_eq!(wide.width(), 384);
174
assert_eq!(wide.width(), 384);
175
assert!(wide.height() > 60);
175
assert_eq!(wide.height(), original_h);
176
}
176
}
177
177
178
#[test]
178
#[test]