aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.rs
diff options
context:
space:
mode:
authorEmulator0002020-09-26 23:05:46 +0200
committerEmulator0002020-09-26 23:05:46 +0200
commit7fb301df6d21b403c5e39e2d9d8685fd7fc7988f (patch)
tree7e1d4f5b8cb2ba0f566b8cac3c52ae328444cdf0 /src/utils.rs
parent96f39fea9921292d5aa67f478bd3d8e947f127be (diff)
downloadpdf_form-7fb301df6d21b403c5e39e2d9d8685fd7fc7988f.tar.bz2
Fixed bad font parsing in case of other formats
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/utils.rs b/src/utils.rs
index 12db115..0aa342a 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -70,3 +70,58 @@ pub fn get_on_value(field: &Dictionary) -> String {
option.unwrap_or("Yes".into())
}
+
+pub fn parse_font(font_string: Option<&str>) -> ((&str, i32), (&str, i32, i32, i32, i32)) {
+ // The default font object (/Helv 12 Tf 0 g)
+ let default_font = ("Helv", 12);
+ let default_color = ("g", 0, 0, 0, 0);
+
+ // Build the font basing on the default appearance, if exists, if not,
+ // assume a default font (surely to be improved!)
+ match font_string {
+ Some(font_string) => {
+ let font = font_string
+ .trim_start_matches('/')
+ .split("Tf")
+ .collect::<Vec<_>>();
+
+ if font.len() < 2 {
+ (default_font, default_color)
+ } else {
+ let font_family = font[0].trim().split(' ').collect::<Vec<_>>();
+ let font_color = font[1].trim().split(' ').collect::<Vec<_>>();
+
+ let font = if font_family.len() >= 2 {
+ (font_family[0], font_family[1].parse::<i32>().unwrap_or(0))
+ } else {
+ default_font
+ };
+
+ let color = if font_color.len() == 2 {
+ ("g", font_color[0].parse::<i32>().unwrap_or(0), 0, 0, 0)
+ } else if font_color.len() == 4 {
+ (
+ "rg",
+ font_color[0].parse::<i32>().unwrap_or(0),
+ font_color[1].parse::<i32>().unwrap_or(0),
+ font_color[2].parse::<i32>().unwrap_or(0),
+ 0,
+ )
+ } else if font_color.len() == 5 {
+ (
+ "k",
+ font_color[0].parse::<i32>().unwrap_or(0),
+ font_color[1].parse::<i32>().unwrap_or(0),
+ font_color[2].parse::<i32>().unwrap_or(0),
+ font_color[3].parse::<i32>().unwrap_or(0),
+ )
+ } else {
+ default_color
+ };
+
+ (font, color)
+ }
+ }
+ _ => (default_font, default_color),
+ }
+}