Skip to main content

LabelBuilder

Struct LabelBuilder 

Source
pub struct LabelBuilder { /* private fields */ }
Expand description

Builder for ZPL II label strings.

Elements are added in order; the final ZPL string is produced by calling build. Every method that adds an element validates its arguments and returns a ZplError if any value is out of range.

§Example

use zpl_builder::{LabelBuilder, BarcodeType, Color, Orientation};

let zpl = LabelBuilder::new()
    .set_home_position(10, 10).unwrap()
    .add_text("Hello, printer!", 50, 50, 'A', 30, Orientation::Normal).unwrap()
    .add_graphical_box(10, 10, 500, 300, 3, Color::Black, 0).unwrap()
    .add_barcode(BarcodeType::Code128, "ABC-123", 50, 120, 2, 2.5, 80, Orientation::Normal).unwrap()
    .build();

assert!(zpl.starts_with("^XA"));
assert!(zpl.ends_with("^XZ"));

Implementations§

Source§

impl LabelBuilder

Source

pub fn new() -> Self

Create a new, empty label builder.

The home position defaults to (0, 0). Call set_home_position to change it.

Source

pub fn set_home_position( self, x_origin: u16, y_origin: u16, ) -> Result<Self, ZplError>

Set the label home position (^LH).

The home position is the axis reference point for the label. Any area below and to the right of this point is available for printing. It is recommended to call this before adding any elements.

§Arguments
  • x_origin — x-axis offset in dots. Range: 0–32000. Default: 0.
  • y_origin — y-axis offset in dots. Range: 0–32000. Default: 0.
§Errors

Returns ZplError::InvalidPosition if either value exceeds 32000.

§Example
let zpl = LabelBuilder::new()
    .set_home_position(50, 10).unwrap()
    .build();

assert!(zpl.contains("^LH50,10"));
Source

pub fn add_text( self, text: &str, x: u16, y: u16, font: char, font_size: u16, orientation: Orientation, ) -> Result<Self, ZplError>

Add a text field (^A / ^FD).

§Arguments
  • text — the string to print.
  • x — x-axis position in dots. Range: 0–32000.
  • y — y-axis position in dots. Range: 0–32000.
  • font — ZPL font name: a single ASCII uppercase letter (AZ) or a digit (19). The digit 0 (zero) is not valid.
  • font_size — font height in dots. Range: 10–32000.
  • orientation — field rotation. See Orientation.
§Errors
ErrorCondition
ZplError::InvalidPositionx or y > 32000
ZplError::InvalidFontfont is not AZ or 19
ZplError::InvalidFontSizefont_size < 10 or > 32000
§Example
let zpl = LabelBuilder::new()
    .add_text("Hello", 10, 10, 'A', 30, Orientation::Normal).unwrap()
    .build();

assert!(zpl.contains("^FDHello^FS"));
Source

pub fn add_barcode( self, _type: BarcodeType, data: &str, x: u16, y: u16, width: u8, width_ratio: f32, height: u16, orientation: Orientation, ) -> Result<Self, ZplError>

Add a barcode field (^BY / ^B*).

§Arguments
  • _type — barcode symbology. See BarcodeType.
  • data — the string to encode.
  • x — x-axis position in dots. Range: 0–32000.
  • y — y-axis position in dots. Range: 0–32000.
  • width — width of the narrowest bar in dots. Range: 1–10.
  • width_ratio — wide bar to narrow bar ratio. Range: 2.0–3.0, step 0.1. Has no effect on fixed-ratio symbologies.
  • height — barcode height in dots. Range: 1–32000.
  • orientation — field rotation. See Orientation.
§Errors
ErrorCondition
ZplError::InvalidPositionx or y > 32000
ZplError::InvalidBarcodeWidthwidth < 1 or > 10
ZplError::InvalidWidthRatiowidth_ratio outside 2.0–3.0
ZplError::InvalidBarcodeHeightheight < 1 or > 32000
§Example
let zpl = LabelBuilder::new()
    .add_barcode(BarcodeType::Code128, "ABC-123", 10, 10, 2, 2.5, 80, Orientation::Normal).unwrap()
    .build();

assert!(zpl.contains("^FDABC-123^FS"));
Source

pub fn add_graphical_box( self, x: u16, y: u16, width: u16, height: u16, thickness: u16, color: Color, rounding: u8, ) -> Result<Self, ZplError>

Add a graphical box (^GB).

Draws a rectangle outline. Setting width and height equal to thickness produces a filled square. Set rounding to 0 for sharp corners.

§Arguments
  • x — x-axis position in dots. Range: 0–32000.
  • y — y-axis position in dots. Range: 0–32000.
  • width — box width in dots. Must be ≥ thickness. Range: thickness–32000.
  • height — box height in dots. Must be ≥ thickness. Range: thickness–32000.
  • thickness — line thickness in dots. Range: 1–32000.
  • color — line color. See Color.
  • rounding — corner rounding amount. Range: 0–8 (0 = sharp, 8 = most rounded).
§Errors
ErrorCondition
ZplError::InvalidPositionx or y > 32000
ZplError::InvalidThicknessthickness < 1 or > 32000
ZplError::InvalidBoxDimensionwidth or height < thickness or > 32000
ZplError::InvalidRoundingrounding > 8
§Example
let zpl = LabelBuilder::new()
    .add_graphical_box(10, 10, 200, 100, 3, Color::Black, 0).unwrap()
    .build();

assert!(zpl.contains("^GB200,100,3,B,0"));
Source

pub fn add_graphical_circle( self, x: u16, y: u16, diameter: u16, thickness: u16, color: Color, ) -> Result<Self, ZplError>

Add a graphical circle (^GC).

§Arguments
  • x — x-axis position in dots. Range: 0–32000.
  • y — y-axis position in dots. Range: 0–32000.
  • diameter — outer diameter in dots. Range: 3–4095.
  • thickness — line thickness in dots. Range: 1–32000. Set equal to diameter to produce a filled circle.
  • color — line color. See Color.
§Errors
ErrorCondition
ZplError::InvalidPositionx or y > 32000
ZplError::InvalidThicknessthickness < 1 or > 32000
ZplError::InvalidGraphicDimensiondiameter < 3 or > 4095
§Example
let zpl = LabelBuilder::new()
    .add_graphical_circle(50, 50, 100, 3, Color::Black).unwrap()
    .build();

assert!(zpl.contains("^GC100,3,B"));
Source

pub fn add_graphical_ellipse( self, x: u16, y: u16, width: u16, height: u16, thickness: u16, color: Color, ) -> Result<Self, ZplError>

Add a graphical ellipse (^GE).

§Arguments
  • x — x-axis position in dots. Range: 0–32000.
  • y — y-axis position in dots. Range: 0–32000.
  • width — ellipse width in dots. Range: 3–4095.
  • height — ellipse height in dots. Range: 3–4095.
  • thickness — line thickness in dots. Range: 1–32000.
  • color — line color. See Color.
§Errors
ErrorCondition
ZplError::InvalidPositionx or y > 32000
ZplError::InvalidThicknessthickness < 1 or > 32000
ZplError::InvalidGraphicDimensionwidth or height < 3 or > 4095
§Example
let zpl = LabelBuilder::new()
    .add_graphical_ellipse(20, 20, 200, 100, 2, Color::Black).unwrap()
    .build();

assert!(zpl.contains("^GE200,100,2,B"));
Source

pub fn add_graphical_diagonal_line( self, x: u16, y: u16, box_width: u16, box_height: u16, thickness: u16, color: Color, orientation: DiagonalOrientation, ) -> Result<Self, ZplError>

Add a diagonal line (^GD).

The line is drawn within a bounding box of size box_width × box_height. A square bounding box (box_width == box_height) produces a 45° angle.

§Arguments
  • x — x-axis position of the bounding box in dots. Range: 0–32000.
  • y — y-axis position of the bounding box in dots. Range: 0–32000.
  • box_width — width of the bounding box in dots. Must be ≥ thickness. Range: thickness–32000.
  • box_height — height of the bounding box in dots. Must be ≥ thickness. Range: thickness–32000.
  • thickness — line thickness in dots. Range: 1–32000.
  • color — line color. See Color.
  • orientation — direction of the line. See DiagonalOrientation.
§Errors
ErrorCondition
ZplError::InvalidPositionx or y > 32000
ZplError::InvalidThicknessthickness < 1 or > 32000
ZplError::InvalidBoxDimensionbox_width or box_height < thickness or > 32000
§Example
let zpl = LabelBuilder::new()
    .add_graphical_diagonal_line(10, 10, 100, 100, 2, Color::Black, DiagonalOrientation::RightLeaning).unwrap()
    .build();

assert!(zpl.contains("^GD100,100,2,B,R"));
Source

pub fn build(&self) -> String

Assemble and return the complete ZPL string.

The string starts with ^XA and ends with ^XZ. It can be sent directly to a Zebra printer over TCP, USB, or serial.

§Example
let zpl = LabelBuilder::new().build();
assert_eq!(zpl, "^XA\n^LH0,0\n^XZ");

Trait Implementations§

Source§

impl Debug for LabelBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LabelBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for LabelBuilder

Source§

fn eq(&self, other: &LabelBuilder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for LabelBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.