Struct code128::Code128

source ·
pub struct Code128 { /* private fields */ }
Expand description

A Code 128.

You can use the modules iterator and the size to compute a visualization. A module 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.

Pseudo code for visualization

Be aware that the standard demands a quiet zone of size 10 around the code. To compute the size of a block, multiply its width with the available space for the code (with a unit) divided by 20 + len(). Here 20 corresponds to the quiet zone.

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;
let mut pos = 10;
for module in code.modules() {
     let x = pos as f64 * line_width;
     let width = module.width as f64 * line_width;
     // print line at `x` pt, `width` pt wide
     pos += module.width + module.space;
}

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 modules(&self) -> impl Iterator<Item = Module> + '_

Get the sequence of modules this Code 128 consists of.

source

pub fn len(&self) -> usize

Get the total width of the code in units of the Module 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.