Skip to content

Commit

Permalink
Change text operators to accept f64 instead of i64
Browse files Browse the repository at this point in the history
According to the PDF Reference [0], Table 5.2, all text state operators
accept numbers, that means reals or integers.  Currently, printpdf only
accepts integers (i64).  This is not sufficient for all use cases.  For
example, for printing justified text, users will want to have
fine-grained control over character and word spacing.

Therefore, this patch changes all text operation methods to accept
floats instead of integers (f64 instead of i64).

[0] https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf
  • Loading branch information
robinkrahl committed Oct 18, 2020
1 parent 9a87586 commit 3d03597
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/builtin_fonts.rs
Expand Up @@ -18,6 +18,6 @@ fn main() {
let text = "Lorem ipsum";

let font = doc.add_builtin_font(BuiltinFont::TimesBoldItalic).unwrap();
current_layer.use_text(text, 48, Mm(10.0), Mm(200.0), &font);
current_layer.use_text(text, 48.0, Mm(10.0), Mm(200.0), &font);
doc.save(&mut BufWriter::new(File::create("test_builtin_fonts.pdf").unwrap())).unwrap();
}
16 changes: 8 additions & 8 deletions examples/fonts.rs
Expand Up @@ -15,7 +15,7 @@ fn main() {
let font = doc.add_external_font(&mut font_reader).unwrap();

// `use_text` is a wrapper around making a simple string
current_layer.use_text(text, 48, Mm(10.0), Mm(200.0), &font);
current_layer.use_text(text, 48.0, Mm(10.0), Mm(200.0), &font);

// text fill color = blue
let blue = Rgb::new(13.0 / 256.0, 71.0 / 256.0, 161.0 / 256.0, None);
Expand All @@ -31,11 +31,11 @@ fn main() {

// setup the general fonts.
// see the docs for these functions for details
current_layer.set_font(&font, 33);
current_layer.set_font(&font, 33.0);
current_layer.set_text_cursor(Mm(10.0), Mm(100.0));
current_layer.set_line_height(33);
current_layer.set_word_spacing(3000);
current_layer.set_character_spacing(10);
current_layer.set_line_height(33.0);
current_layer.set_word_spacing(3000.0);
current_layer.set_character_spacing(10.0);

// write two lines (one line break)
current_layer.write_text(text, &font);
Expand All @@ -44,14 +44,14 @@ fn main() {
current_layer.add_line_break();

current_layer.set_text_rendering_mode(TextRenderingMode::FillStroke);
current_layer.set_character_spacing(0);
current_layer.set_character_spacing(0.0);
current_layer.set_text_matrix(TextMatrix::Rotate(10.0));

// write one line, but write text2 in superscript
current_layer.write_text(text, &font);
current_layer.set_line_offset(10);
current_layer.set_line_offset(10.0);
current_layer.set_text_rendering_mode(TextRenderingMode::Stroke);
current_layer.set_font(&font, 18);
current_layer.set_font(&font, 18.0);
current_layer.write_text(text2, &font);

current_layer.end_text_section();
Expand Down
24 changes: 12 additions & 12 deletions src/types/pdf_layer.rs
Expand Up @@ -124,7 +124,7 @@ impl PdfLayerReference {
/// Set the current font, only valid in a `begin_text_section` to
/// `end_text_section` block
#[inline]
pub fn set_font(&self, font: &IndirectFontRef, font_size: i64)
pub fn set_font(&self, font: &IndirectFontRef, font_size: f64)
-> ()
{
self.internal_add_operation(Operation::new("Tf",
Expand Down Expand Up @@ -317,19 +317,19 @@ impl PdfLayerReference {
/// Sets the text line height inside a text block
/// (must be called within `begin_text_block` and `end_text_block`)
#[inline]
pub fn set_line_height(&self, height: i64) {
pub fn set_line_height(&self, height: f64) {
self.internal_add_operation(Operation::new("TL",
vec![lopdf::Object::Integer(height)]
vec![lopdf::Object::Real(height)]
));
}

/// Sets the character spacing inside a text block
/// Values are given in points. A value of 3 (pt) will increase
/// the spacing inside a word by 3pt.
#[inline]
pub fn set_character_spacing(&self, spacing: i64) {
pub fn set_character_spacing(&self, spacing: f64) {
self.internal_add_operation(Operation::new("Tc",
vec![lopdf::Object::Integer(spacing)]
vec![lopdf::Object::Real(spacing)]
));
}

Expand All @@ -342,9 +342,9 @@ impl PdfLayerReference {
/// However, the function itself is valid and _will work_
/// with builtin fonts.
#[inline]
pub fn set_word_spacing(&self, spacing: i64) {
pub fn set_word_spacing(&self, spacing: f64) {
self.internal_add_operation(Operation::new("Tw",
vec![lopdf::Object::Integer(spacing)]
vec![lopdf::Object::Real(spacing)]
));
}

Expand All @@ -353,9 +353,9 @@ impl PdfLayerReference {
/// 50 will reduce the width of the written text by half,
/// but stretch the text
#[inline]
pub fn set_text_scaling(&self, scaling: i64) {
pub fn set_text_scaling(&self, scaling: f64) {
self.internal_add_operation(Operation::new("Tz",
vec![lopdf::Object::Integer(scaling)]
vec![lopdf::Object::Real(scaling)]
));
}

Expand All @@ -365,9 +365,9 @@ impl PdfLayerReference {
/// number, for subscript, use a negative number. This does not
/// change the size of the font
#[inline]
pub fn set_line_offset(&self, offset: i64) {
pub fn set_line_offset(&self, offset: f64) {
self.internal_add_operation(Operation::new("Ts",
vec![lopdf::Object::Integer(offset)]
vec![lopdf::Object::Real(offset)]
));
}

Expand Down Expand Up @@ -515,7 +515,7 @@ impl PdfLayerReference {
///
/// [Windows-1252]: https://en.wikipedia.org/wiki/Windows-1252
#[inline]
pub fn use_text<S>(&self, text: S, font_size: i64,
pub fn use_text<S>(&self, text: S, font_size: f64,
x: Mm, y: Mm, font: &IndirectFontRef)
-> () where S: Into<String>
{
Expand Down

0 comments on commit 3d03597

Please sign in to comment.