Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ required-features = []
name = "customfont"
required-features = []

[[example]]
name = "variable_font"
required-features = ["text_layout"]

[[example]]
name = "graphics"
required-features = []
Expand Down
67 changes: 67 additions & 0 deletions examples/variable_font.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{env, error::Error, fs};

use printpdf::{
FontVariationSettings, FontVariationTag, Mm, Op, PdfDocument, PdfFontHandle, PdfPage,
PdfSaveOptions, Point, Pt, TextItem,
};

fn main() -> Result<(), Box<dyn Error>> {
let font_path = env::args()
.nth(1)
.ok_or("usage: cargo run --example variable_font -- path/to/variable-font.ttf")?;
let font_bytes = fs::read(font_path)?;
let mut warnings = Vec::new();
let mut document = PdfDocument::new("Variable font instances");

let light = document.add_variable_font(
&font_bytes,
0,
&FontVariationSettings::new().with(FontVariationTag::WGHT, 300.0),
&mut warnings,
)?;
let bold = document.add_variable_font(
&font_bytes,
0,
&FontVariationSettings::new().with(FontVariationTag::WGHT, 800.0),
&mut warnings,
)?;

let page = PdfPage::new(
Mm(210.0),
Mm(297.0),
vec![
Op::StartTextSection,
Op::SetTextCursor {
pos: Point::new(Mm(20.0), Mm(260.0)),
},
Op::SetFont {
font: PdfFontHandle::External(light),
size: Pt(24.0),
},
Op::ShowText {
items: vec![TextItem::Text("Weight 300".into())],
},
Op::SetTextCursor {
pos: Point::new(Mm(20.0), Mm(240.0)),
},
Op::SetFont {
font: PdfFontHandle::External(bold),
size: Pt(24.0),
},
Op::ShowText {
items: vec![TextItem::Text("Weight 800".into())],
},
Op::EndTextSection,
],
);

let pdf = document
.with_pages(vec![page])
.save(&PdfSaveOptions::default(), &mut warnings);
fs::write("variable_font.pdf", pdf)?;

for warning in warnings {
eprintln!("{}", warning.msg);
}
Ok(())
}
Loading