Struct Encoding

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

Represent a text encoding used in PDF. An encoding maintains the connection between unicode code points, bytes in PDF strings, and glyph names.

Currently, only WIN_ANSI_ENCODING, SYMBOL_ENCODING, and ZAPFDINGBATS_ENCODING are supported, and they are provided as built-in.

§Example

use pdf_canvas::{BuiltinFont, FontSource};
assert_eq!("WinAnsiEncoding",
           BuiltinFont::Helvetica.get_encoding().get_name());
assert_eq!("SymbolEncoding",
           BuiltinFont::Symbol.get_encoding().get_name());

Implementations§

Source§

impl Encoding

Source

pub fn get_name(&self) -> String

The name of the encoding, as used in the font object.

Source

pub fn get_code(&self, name: &str) -> Option<u8>

Get the encoded code point from a type1 character name. Character names are case sensitive and contains only ascii letters. If the name is not available in the encoding, or is not a proper character name, None is returned.

§Example
use pdf_canvas::{BuiltinFont, FontSource};
let enc = BuiltinFont::Helvetica.get_encoding();
assert_eq!(Some(32),  enc.get_code("space"));
assert_eq!(Some(65),  enc.get_code("A"));
assert_eq!(Some(229), enc.get_code("aring"));
assert_eq!(None,      enc.get_code("Lslash"));
assert_eq!(None,      enc.get_code(""));
assert_eq!(None,      enc.get_code("☺"));
Source

pub fn encode_char(&self, ch: char) -> Option<u8>

Get the encoded code point from a (unicode) character. If the character is not available in the encoding, None is returned.

§Example
use pdf_canvas::{BuiltinFont, FontSource};
let enc = BuiltinFont::Helvetica.get_encoding();
assert_eq!(Some(b' '),  enc.encode_char(' '));
assert_eq!(Some(b'A'),  enc.encode_char('A'));
assert_eq!(Some(b'\\'),  enc.encode_char('\\'));
assert_eq!(Some(229), enc.encode_char('å'));
assert_eq!(None,      enc.encode_char('Ł'));
assert_eq!(None,      enc.encode_char(char::from(0)));
assert_eq!(None,      enc.encode_char('☺'));
Source

pub fn encode_string(&self, text: &str) -> Vec<u8>

Convert a rust string to a vector of bytes in the encoding.

§Example
use pdf_canvas::{BuiltinFont, FontSource};
let enc = BuiltinFont::Helvetica.get_encoding();
let symb_enc = BuiltinFont::Symbol.get_encoding();
assert_eq!(vec![b'A', b'B', b'C'], enc.encode_string("ABC"));
assert_eq!(vec![b'R', 228, b'k',
                b's', b'm', 246, b'r', b'g', 229, b's'],
           enc.encode_string("Räksmörgås"));
assert_eq!(vec![b'C', b'o', b'f', b'f', b'e', b'e', b' ',
                128, b'1', b'.', b'2', b'0'],
           enc.encode_string("Coffee €1.20"));
assert_eq!(vec![b'a', b' ', 206, b' ', 194],
           symb_enc.encode_string("α ∈ ℜ"));

Trait Implementations§

Source§

impl Clone for Encoding

Source§

fn clone(&self) -> Encoding

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encoding

Source§

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

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

impl Hash for Encoding

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Encoding

Source§

fn eq(&self, other: &Encoding) -> 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 Eq for Encoding

Source§

impl StructuralPartialEq for Encoding

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.