-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring push_shape to make it less awkward. Still not happy.
(Also refactoring a bunch of stuff under pdfutils into its own files.)
- Loading branch information
Showing
21 changed files
with
207 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,3 @@ name = "magic" | |
|
||
[[bin]] | ||
name = "weekly" | ||
|
||
[[bin]] | ||
name = "remtest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::pdfutils::font_proxy::FontProxy; | ||
use crate::pdfutils::Instruction; | ||
use crate::Instructions; | ||
use printpdf::{IndirectFontRef, PdfDocumentReference}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct FontMap(HashMap<FontProxy, IndirectFontRef>); | ||
|
||
impl FontMap { | ||
pub fn resolve_fonts( | ||
mut self, | ||
doc: &PdfDocumentReference, | ||
instructions: &Instructions, | ||
) -> crate::Result<FontMap> { | ||
// Look for all of the fonts referenced in the Instructions, | ||
// add them to the PdfDocument, adding the fonts to map. | ||
instructions | ||
.instructions | ||
.iter() | ||
.filter_map(|i| match i { | ||
Instruction::Text(tv) => Some(tv.font), | ||
_ => None, | ||
}) | ||
.try_for_each::<_, crate::Result<()>>(|font| { | ||
let entry = self.0.entry(font); | ||
|
||
// Basically doing or_insert_with(), but I need to propagate an error. | ||
if let std::collections::hash_map::Entry::Vacant(ve) = entry { | ||
let indirect_font = doc.add_builtin_font(font.into())?; | ||
ve.insert(indirect_font); | ||
} | ||
Ok(()) | ||
})?; | ||
Ok(self) | ||
} | ||
|
||
pub fn lookup(&self, font_proxy: FontProxy) -> &IndirectFontRef { | ||
// unwrap: can we get rid of this? | ||
self.0.get(&font_proxy).unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use printpdf::BuiltinFont; | ||
|
||
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] | ||
pub enum FontProxy { | ||
// first bool is Bold, second bool is Italics | ||
Helvetica(bool, bool), | ||
Times(bool, bool), | ||
} | ||
|
||
impl FontProxy { | ||
pub fn times() -> FontProxy { | ||
FontProxy::Times(false, false) | ||
} | ||
pub fn helvetica() -> FontProxy { | ||
FontProxy::Helvetica(false, false) | ||
} | ||
pub fn times_bold() -> FontProxy { | ||
//FontProxy::Times(true, false) | ||
FontProxy::times().bold(true) | ||
} | ||
pub fn helvetica_bold() -> FontProxy { | ||
//FontProxy::Helvetica(true, false) | ||
FontProxy::helvetica().bold(true) | ||
} | ||
pub fn bold(&self, bold: bool) -> FontProxy { | ||
match self { | ||
FontProxy::Helvetica(_, it) => FontProxy::Helvetica(bold, *it), | ||
FontProxy::Times(_, it) => FontProxy::Times(bold, *it), | ||
} | ||
} | ||
} | ||
|
||
impl Default for FontProxy { | ||
fn default() -> Self { | ||
FontProxy::Times(false, false) | ||
} | ||
} | ||
|
||
impl From<FontProxy> for BuiltinFont { | ||
fn from(font_proxy: FontProxy) -> Self { | ||
match font_proxy { | ||
FontProxy::Helvetica(bold, italic) => { | ||
if bold && italic { | ||
BuiltinFont::HelveticaBoldOblique | ||
} else if bold { | ||
BuiltinFont::HelveticaBold | ||
} else if italic { | ||
BuiltinFont::HelveticaOblique | ||
} else { | ||
BuiltinFont::Helvetica | ||
} | ||
} | ||
FontProxy::Times(bold, italic) => { | ||
if bold && italic { | ||
BuiltinFont::TimesBoldItalic | ||
} else if bold { | ||
BuiltinFont::TimesBold | ||
} else if italic { | ||
BuiltinFont::TimesItalic | ||
} else { | ||
BuiltinFont::TimesRoman | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.