Skip to main content

EmbeddedFont

Struct EmbeddedFont 

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

A font dictionary this session added, ready to name from a content stream.

EmbeddedFont::object is the /Font resource crate::content::ResourceTable::realize will allocate a name for. EmbeddedFont::encode turns Unicode into the character codes a Tj/TJ of that font expects.

Implementations§

Source§

impl EmbeddedFont

Source

pub fn object(&self) -> ObjRef

The /Font dictionary to name from a page resource.

Source

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

Character codes for text in this font’s encoding.

Unmappable characters become .notdef (code 0). A composite font writes two-byte big-endian GIDs (Identity-H); a simple or standard font writes one byte.

A font from EditDoc::embed_cid_font writes two-byte big-endian CIDs, and finds them by inverting the caller’s /ToUnicode CMap rather than by consulting the program’s cmap. That is not a convenience: under a caller-supplied CMap the program’s cmap does not describe the file’s code space, and the CMap is the document’s only statement of what its codes mean. A character the CMap does not reach is code 0, as everywhere else.

Source

pub fn encode_checked(&self, text: &str) -> Result<Vec<u8>, MissingGlyph>

Character codes for text, refusing a character this font cannot draw.

The same encoding as EmbeddedFont::encode, with the .notdef fallback replaced by an error. This is what a caller who is placing text wants: a watermark whose degree sign silently became a blank is worse than one that refused to be written.

use pdfrum_edit::{EditDoc, StandardFont};
use pdfrum_parser::{LoadOptions, load};
use std::sync::Arc;

let bytes: Arc<[u8]> = Arc::from(&include_bytes!("../../tests/files/hello.pdf")[..]);
let doc = load(bytes, &LoadOptions::default())?;
let mut edit = EditDoc::new(&doc);
let font = edit.standard_font(StandardFont::Helvetica)?;

assert!(font.encode_checked("Hi").is_ok());
// WinAnsi has no Han: refused rather than drawn blank.
assert_eq!(font.encode_checked("\u{4e00}").unwrap_err().character, '\u{4e00}');
§Errors

MissingGlyph naming the first character with no glyph, and its byte offset in text.

Trait Implementations§

Source§

impl Clone for EmbeddedFont

Source§

fn clone(&self) -> EmbeddedFont

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for EmbeddedFont

Source§

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

Formats the value using the given formatter. Read more

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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.