Code128

Struct Code128 

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

A Code 128.

You can use the bars iterators bars() or bar_coordinates(), and the size to compute a visualization. A bars corresponds to a “black line” of the code and has a unitless width between one and four, as well as a free space after it, also sized between one and four.

A bar with width one is called a “module”. You can say a bar consists of one or more modules. The width of a module is often called “X”, denoting one unit in the “x-dimension” of the barcode. A multiple of a module’s width is usually written <multiplier>X, for example 1X or 2.5X.

§Pseudo code for visualization

The standard demands a quiet zone of size 10X (see above for notation) on the left and right side of the code. To compute the size of a bar, multiply its width with the available space for the code divided by the code’s length.

let code = Code128::encode(b"Code128 <3");
let available_space = 100.0; // unit is, say, "pt"
let line_width = available_space / code.len() as f64;
for bar in code.bar_coordinates() {
     let x = bar.x as f64 * line_width;
     let width = bar.width as f64 * line_width;
     // print line at `x` pt, `width` pt wide
}

Implementations§

Source§

impl Code128

Source

pub fn encode(data: &[u8]) -> Self

Encode the bytes as Code 128.

See the module documentation for hints on charsets.

Source

pub fn encode_str(text: &str) -> Option<Self>

Encode the string as Code 128 using Latin 1.

The functions returns None if the string includes characters not included in Latin 1.

The control characters of ASCII, 0x00 to 0x19, are also encoded.

Source

pub fn bars(&self) -> impl Iterator<Item = Bar> + '_

Get the sequence of bars this Code 128 consists of.

Source

pub fn bar_coordinates(&self) -> impl Iterator<Item = BarCoordinate> + '_

Get the coordinates of the bars this Code 128 consists of.

Source

pub fn len(&self) -> usize

Get the total width of the code in units of the Bar with the quiet zone included.

Source

pub fn is_empty(&self) -> bool

Whether this Code 128 encodes empty data.

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.