Reformatage de l’alias pdf + intégration de pdf2grey dans pdf
This commit is contained in:
parent
3c990f7cff
commit
ca4b6e3c75
374
aliases/pdf.elv
374
aliases/pdf.elv
|
@ -1,107 +1,82 @@
|
||||||
use moi/util/format
|
edit:add-var pdf~ {|action @args|
|
||||||
use moi/util/pdf
|
|
||||||
use math
|
use math
|
||||||
use path
|
use path
|
||||||
use re
|
use re
|
||||||
use str
|
use str
|
||||||
|
use framagit.org/benjamin.vaudour/elv-lib/mods/common
|
||||||
|
use framagit.org/benjamin.vaudour/elv-lib/mods/format
|
||||||
|
use framagit.org/benjamin.vaudour/elv-lib/mods/map
|
||||||
|
use framagit.org/benjamin.vaudour/elv-lib/mods/pdf
|
||||||
|
|
||||||
fn -must-pdf {|file|
|
var -c~ = $common:cexec~
|
||||||
if (not (pdf:is-pdf $file)) {
|
var -cc~ = {|c f1| -c $c $f1 $nop~}
|
||||||
fail (printf '%s n’est pas un fichier pdf' $file)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -must-exist {|file|
|
fn must-pdf {|file| -cc (not (pdf:is-pdf $file)) { fail (printf '%s n’est pas un fichier pdf' $file) }}
|
||||||
if (pdf:not-exist $file) {
|
fn must-exist {|file| -cc (pdf:not-exist $file) { fail (printf '%s n’existe pas' $file) }}
|
||||||
fail (printf '%s n’existe pas' $file)
|
fn must-not-exist {|file| -cc (pdf:exist $file) { fail (printf '%s existe déjà' $file) }}
|
||||||
|
fn must-pdf-exist {|file|
|
||||||
|
must-pdf $file
|
||||||
|
must-exist $file
|
||||||
}
|
}
|
||||||
|
fn must-pdf-not-exist {|file|
|
||||||
|
must-pdf $file
|
||||||
|
must-not-exist $file
|
||||||
}
|
}
|
||||||
|
fn must-valid {|v cond| -cc (not ($cond $v)) { fail (printf '%s: paramètre invalide' $v) }}
|
||||||
|
|
||||||
fn -must-not-exist {|file|
|
fn out {|in suffix|
|
||||||
if (pdf:exist $file) {
|
|
||||||
fail (printf '%s existe déjà' $file)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -must-pdf-exist {|file|
|
|
||||||
-must-pdf $file
|
|
||||||
-must-exist $file
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -must-pdf-not-exist {|file|
|
|
||||||
-must-pdf $file
|
|
||||||
-must-not-exist $file
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -must-valid {|v cond|
|
|
||||||
if (not ($cond $v)) {
|
|
||||||
fail (printf '%s: paramètre invalide' $v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -out {|in suffix|
|
|
||||||
var out = (str:trim-suffix $in .pdf)
|
var out = (str:trim-suffix $in .pdf)
|
||||||
str:join '' [ $out $suffix ]
|
str:join '' [ $out $suffix ]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn help {|@args|
|
fn help {|@args| cat $E:HOME/.config/elvish/aliases/pdf.help}
|
||||||
cat $E:HOME/.config/elvish/aliases/pdf.help
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decrypt {|@args|
|
fn decrypt {|@args|
|
||||||
var l = (count $args)
|
var l = (count $args)
|
||||||
if (or (< $l 1) (> $l 3)) {
|
-cc (or (< $l 1) (> $l 3)) { fail 'decrypt doit contenir entre 1 et 3 paramètres' }
|
||||||
fail 'decrypt doit contenir entre 1 et 3 paramètres'
|
|
||||||
}
|
|
||||||
var in = $args[0]
|
var in = $args[0]
|
||||||
var out = (-out $in _decrypted.pdf)
|
var out = (out $in _decrypted.pdf)
|
||||||
var passwd = []
|
var passwd = []
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
if (> $l 1) {
|
if (> $l 1) {
|
||||||
set @passwd = $args[1]
|
set @passwd = $args[1]
|
||||||
if (> $l 2) {
|
if (> $l 2) {
|
||||||
set out = $args[2]
|
set out = $args[2]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
-must-pdf-not-exist $out
|
must-pdf-not-exist $out
|
||||||
pdf:decrypt &out=$out $in $@passwd
|
pdf:decrypt &out=$out $in $@passwd
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rotate {|@args|
|
fn rotate {|@args|
|
||||||
var l = (- (count $args) 1)
|
var l = (- (count $args) 1)
|
||||||
if (< $l 1) {
|
-cc (< $l 1) { fail 'rotate doit contenir au moins 1 paramètres' }
|
||||||
fail 'rotate doit contenir au moins 1 paramètres'
|
|
||||||
}
|
|
||||||
var in = $args[0]
|
var in = $args[0]
|
||||||
var out = (-out $in _rotated.pdf)
|
var out = (out $in _rotated.pdf)
|
||||||
if (pdf:is-pdf $args[$l]) {
|
if (pdf:is-pdf $args[$l]) {
|
||||||
set @args out = (all $args[1..])
|
set @args out = (all $args[1..])
|
||||||
}
|
}
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
-must-pdf-not-exist $out
|
must-pdf-not-exist $out
|
||||||
each {|e|
|
each {|e|
|
||||||
-must-valid $e $pdf:is-rotate-selection~
|
must-valid $e $pdf:is-rotate-selection~
|
||||||
} $args
|
} $args
|
||||||
pdf:rotate &keep=$true &out=$out $in $@args
|
pdf:rotate &keep=$true &out=$out $in $@args
|
||||||
}
|
}
|
||||||
|
|
||||||
fn merge {|@args|
|
fn merge {|@args|
|
||||||
var l = (- (count $args) 1)
|
var l = (- (count $args) 1)
|
||||||
if (< $l 1) {
|
-cc (< $l 1) { fail 'merge doit contenir au moins 2 paramètres' }
|
||||||
fail 'merge doit contenir au moins 2 paramètres'
|
|
||||||
}
|
|
||||||
var @selection out = $@args
|
var @selection out = $@args
|
||||||
-must-pdf-not-exist $out
|
must-pdf-not-exist $out
|
||||||
var has-in = $false
|
var has-in = $false
|
||||||
each {|e|
|
each {|e|
|
||||||
if (pdf:is-pdf $e) {
|
if (pdf:is-pdf $e) {
|
||||||
-must-exist $e
|
must-exist $e
|
||||||
set has-in = $true
|
set has-in = $true
|
||||||
} else {
|
} else {
|
||||||
if (not $has-in) {
|
-cc (not $has-in) { fail 'Une sélection doit être précédée d’un fichier d’entrée' }
|
||||||
fail 'Une sélection doit être précédée d’un fichier d’entrée'
|
must-valid $e $pdf:is-selection~
|
||||||
}
|
|
||||||
-must-valid $e $pdf:is-selection~
|
|
||||||
}
|
}
|
||||||
} $selection
|
} $selection
|
||||||
pdf:cat &out=$out $@selection
|
pdf:cat &out=$out $@selection
|
||||||
|
@ -109,30 +84,26 @@ fn merge {|@args|
|
||||||
|
|
||||||
fn unmerge {|@args|
|
fn unmerge {|@args|
|
||||||
var l = (- (count $args) 1)
|
var l = (- (count $args) 1)
|
||||||
if (< $l 1) {
|
-cc (< $l 1) { fail 'unmerge doit contenir au moins 2 paramètres' }
|
||||||
fail 'unmerge doit contenir au moins 2 paramètres'
|
|
||||||
}
|
|
||||||
var in = $args[0]
|
var in = $args[0]
|
||||||
var out = (-out $in _unmerged)
|
var out = (out $in _unmerged)
|
||||||
if (not (pdf:is-selection $args[$l])) {
|
if (not (pdf:is-selection $args[$l])) {
|
||||||
set @args out = (all $args[1..])
|
set @args out = (all $args[1..])
|
||||||
}
|
}
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
-must-not-exist $out
|
must-not-exist $out
|
||||||
each {|e|
|
each {|e|
|
||||||
-must-valid $e $pdf:is-selection~
|
must-valid $e $pdf:is-selection~
|
||||||
} $args
|
} $args
|
||||||
pdf:uncat $in $out $@args
|
pdf:uncat $in $out $@args
|
||||||
}
|
}
|
||||||
|
|
||||||
fn split {|@args|
|
fn split {|@args|
|
||||||
var l = (- (count $args) 1)
|
var l = (- (count $args) 1)
|
||||||
if (< $l 1) {
|
-cc (< $l 1) { fail 'split doit contenir au moins 2 paramètres' }
|
||||||
fail 'split doit contenir au moins 2 paramètres'
|
|
||||||
}
|
|
||||||
var out = $args[$l]
|
var out = $args[$l]
|
||||||
set args = $args[..$l]
|
set args = $args[..$l]
|
||||||
-must-not-exist $out
|
must-not-exist $out
|
||||||
var s = 1
|
var s = 1
|
||||||
if (pdf:is-number $args[0]) {
|
if (pdf:is-number $args[0]) {
|
||||||
set s @args = $@args
|
set s @args = $@args
|
||||||
|
@ -148,14 +119,11 @@ fn split {|@args|
|
||||||
|
|
||||||
fn split-at {|@args|
|
fn split-at {|@args|
|
||||||
var l = (count $args)
|
var l = (count $args)
|
||||||
if (!= $l 3) {
|
-cc (!= $l 3) { fail 'split-at doit contenir 3 paramètres' }
|
||||||
fail 'split-at doit contenir 3 paramètres'
|
|
||||||
}
|
|
||||||
var rg in out = $@args
|
var rg in out = $@args
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
-must-not-exist $out
|
must-not-exist $out
|
||||||
-must-valid $rg {|e| re:match '^\d+(,\d+)*$' $e }
|
must-valid $rg {|e| re:match '^\d+(,\d+)*$' $e }
|
||||||
|
|
||||||
var b sel = 1 []
|
var b sel = 1 []
|
||||||
str:split ',' $rg | order | each {|s|
|
str:split ',' $rg | order | each {|s|
|
||||||
var e = (- $s 1)
|
var e = (- $s 1)
|
||||||
|
@ -173,21 +141,17 @@ fn split-at {|@args|
|
||||||
|
|
||||||
fn zip {|@args|
|
fn zip {|@args|
|
||||||
var l = (- (count $args) 1)
|
var l = (- (count $args) 1)
|
||||||
if (< $l 1) {
|
-cc (< $l 1) { fail 'zip doit contenir au moins 2 paramètres' }
|
||||||
fail 'zip doit contenir au moins 2 paramètres'
|
|
||||||
}
|
|
||||||
var @selection out = $@args
|
var @selection out = $@args
|
||||||
-must-pdf-not-exist $out
|
must-pdf-not-exist $out
|
||||||
var has-in = $false
|
var has-in = $false
|
||||||
each {|e|
|
each {|e|
|
||||||
if (pdf:is-pdf $e) {
|
if (pdf:is-pdf $e) {
|
||||||
-must-exist $e
|
must-exist $e
|
||||||
set has-in = $true
|
set has-in = $true
|
||||||
} else {
|
} else {
|
||||||
if (not $has-in) {
|
-cc (not $has-in) { fail 'Une sélection doit être précédée d’un fichier d’entrée' }
|
||||||
fail 'Une sélection doit être précédée d’un fichier d’entrée'
|
must-valid $e $pdf:is-selection~
|
||||||
}
|
|
||||||
-must-valid $e $pdf:is-selection~
|
|
||||||
}
|
}
|
||||||
} $selection
|
} $selection
|
||||||
pdf:cat &collate=$true &out=$out $@selection
|
pdf:cat &collate=$true &out=$out $@selection
|
||||||
|
@ -195,19 +159,15 @@ fn zip {|@args|
|
||||||
|
|
||||||
fn unzip {|@args|
|
fn unzip {|@args|
|
||||||
var l = (- (count $args) 1)
|
var l = (- (count $args) 1)
|
||||||
if (< $l 1) {
|
-cc (< $l 1) { fail 'unzip doit contenir au moins 2 paramètres' }
|
||||||
fail 'unzip doit contenir au moins 2 paramètres'
|
|
||||||
}
|
|
||||||
var in @sel out = $@args
|
var in @sel out = $@args
|
||||||
var mod = 2
|
var mod = 2
|
||||||
if (pdf:is-number $in) {
|
if (pdf:is-number $in) {
|
||||||
set mod = $in
|
set mod = $in
|
||||||
if (eq (count $sel) 0) {
|
-cc (eq (count $sel) 0) { fail 'unzip doit contenir au moins 3 paramètres' }
|
||||||
fail 'unzip doit contenir au moins 3 paramètres'
|
|
||||||
}
|
|
||||||
set in @sel = $@sel
|
set in @sel = $@sel
|
||||||
}
|
}
|
||||||
-must-not-exist $out
|
must-not-exist $out
|
||||||
var t = (pdf:-t)
|
var t = (pdf:-t)
|
||||||
try {
|
try {
|
||||||
pdf:cat &out=$t $in $@sel
|
pdf:cat &out=$t $in $@sel
|
||||||
|
@ -216,12 +176,8 @@ fn unzip {|@args|
|
||||||
range 1 (+ 1 $mod) | each {|m|
|
range 1 (+ 1 $mod) | each {|m|
|
||||||
var sel = (range 1 (+ $n 1) | each {|i|
|
var sel = (range 1 (+ $n 1) | each {|i|
|
||||||
var s = (% $i $mod)
|
var s = (% $i $mod)
|
||||||
if (== $s 0) {
|
-cc (== $s 0) { set s = $mod }
|
||||||
set s = $mod
|
-cc (== $s $m) $i
|
||||||
}
|
|
||||||
if (== $s $m) {
|
|
||||||
put $i
|
|
||||||
}
|
|
||||||
} | each $to-string~ | str:join ',')
|
} | each $to-string~ | str:join ',')
|
||||||
pdf:cat &out=(printf '%s/%d.pdf' $out $m) $t $sel
|
pdf:cat &out=(printf '%s/%d.pdf' $out $m) $t $sel
|
||||||
}
|
}
|
||||||
|
@ -230,33 +186,20 @@ fn unzip {|@args|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn -pts2cm {|v|
|
fn pts2cm {|v| / (math:round (* $v 3.52778)) 100}
|
||||||
/ (math:round (* $v 3.52778)) 100
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -date {|raw|
|
fn -date {|raw|
|
||||||
var y m d = $raw[2..6] $raw[6..8] $raw[8..10]
|
var y m d = $raw[2..6] $raw[6..8] $raw[8..10]
|
||||||
var h n s = $raw[10..12] $raw[12..14] $raw[14..16]
|
var h n s = $raw[10..12] $raw[12..14] $raw[14..16]
|
||||||
var rest = []
|
var rest = []
|
||||||
if (> (count $raw[16..]) 0) {
|
-cc (> (count $raw[16..]) 0) { set @rest = (str:split "'" $raw[16..]) }
|
||||||
set @rest = (str:split "'" $raw[16..])
|
|
||||||
}
|
|
||||||
var tz = '+00:00'
|
var tz = '+00:00'
|
||||||
if (> (count $rest) 0) {
|
if (> (count $rest) 0) {
|
||||||
var tz = $rest[0]
|
var tz = $rest[0]
|
||||||
if (eq $tz[0] 'Z') {
|
-cc (eq $tz[0] 'Z') { set tz = $tz[1..] }
|
||||||
set tz = $tz[1..]
|
-cc (eq $tz '') { set tz = '00' }
|
||||||
}
|
-cc (and (not-eq $tz[0] '+') (not-eq $tz[0] '-')) { set tz = (printf '+%s' $tz) }
|
||||||
if (eq $tz '') {
|
|
||||||
set tz = '00'
|
|
||||||
}
|
|
||||||
if (and (not-eq $tz[0] '+') (not-eq $tz[0] '-')) {
|
|
||||||
set tz = (printf '+%s' $tz)
|
|
||||||
}
|
|
||||||
var mm = '00'
|
var mm = '00'
|
||||||
if (and (> (count $rest) 1) (pdf:is-number $rest[1])) {
|
-cc (and (> (count $rest) 1) (pdf:is-number $rest[1])) { set mm = $rest[1] }
|
||||||
set mm = $rest[1]
|
|
||||||
}
|
|
||||||
set tz = (printf '%s:%s' $tz $mm)
|
set tz = (printf '%s:%s' $tz $mm)
|
||||||
}
|
}
|
||||||
var date = (printf ^
|
var date = (printf ^
|
||||||
|
@ -268,54 +211,25 @@ fn -date {|raw|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn info {|@args|
|
fn info {|@args|
|
||||||
if (!= (count $args) 1) {
|
-cc (!= (count $args) 1) { fail 'info doit contenir un paramètre' }
|
||||||
fail 'info doit contenir un paramètre'
|
|
||||||
}
|
|
||||||
var in @_ = $@args
|
var in @_ = $@args
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
var json = (pdf:json $in)
|
var json = (pdf:json $in)
|
||||||
var objects = $json[objects]
|
var objects = $json[objects]
|
||||||
var trailer = $objects[trailer]
|
var trailer = $objects[trailer]
|
||||||
var infos = $objects[$trailer[/Info]]
|
var infos = $objects[$trailer[/Info]]
|
||||||
var pages = $json[pages]
|
var pages = $json[pages]
|
||||||
var p1 = $objects[$pages[0][object]]
|
var p1 = $objects[$pages[0][object]]
|
||||||
var w h = (all $p1[/MediaBox][2..] | each $-pts2cm~)
|
var w h = (all $p1[/MediaBox][2..] | each $pts2cm~)
|
||||||
var layout = 'Portrait'
|
var layout = (-c (> $w $h) 'Paysage' 'Portrait')
|
||||||
var title = ''
|
var title = (map:value-of $infos '/Title')
|
||||||
var subject = ''
|
var subject = (map:value-of $infos '/Subject')
|
||||||
var author = ''
|
var author = (map:value-of $infos '/Author')
|
||||||
var creator = ''
|
var creator = (map:value-of $infos '/Creator')
|
||||||
var producer = ''
|
var producer = (map:value-of $infos '/Producer')
|
||||||
var mdate = ''
|
var mdate = (-c (has-key $infos '/ModDate') { -date $infos[/ModDate] } '')
|
||||||
var crypted = 'Non'
|
var crypted = (-c (pdf:encryption $in)[encrypted] 'Oui' 'Non')
|
||||||
var hasform = 'Non'
|
var hasform = ( -c (pdf:form $in)[hasacroform] 'Oui' 'Non')
|
||||||
if (> $w $h) {
|
|
||||||
set layout = 'Paysage'
|
|
||||||
}
|
|
||||||
if (has-key $infos '/Title') {
|
|
||||||
set title = $infos[/Title]
|
|
||||||
}
|
|
||||||
if (has-key $infos '/Subject') {
|
|
||||||
set subject = $infos[/Subject]
|
|
||||||
}
|
|
||||||
if (has-key $infos '/Author') {
|
|
||||||
set author = $infos[/Author]
|
|
||||||
}
|
|
||||||
if (has-key $infos '/Creator') {
|
|
||||||
set creator = $infos[/Creator]
|
|
||||||
}
|
|
||||||
if (has-key $infos '/Producer') {
|
|
||||||
set producer = $infos[/Producer]
|
|
||||||
}
|
|
||||||
if (has-key $infos '/ModDate') {
|
|
||||||
set mdate = (-date $infos[/ModDate])
|
|
||||||
}
|
|
||||||
if (pdf:encryption $in)[encrypted] {
|
|
||||||
set crypted = 'Oui'
|
|
||||||
}
|
|
||||||
if (pdf:form $in)[hasacroform] {
|
|
||||||
set hasform = 'Oui'
|
|
||||||
}
|
|
||||||
var data = [
|
var data = [
|
||||||
[&k='Chemin' &v=(path:abs $in)]
|
[&k='Chemin' &v=(path:abs $in)]
|
||||||
[&k='Taille' &v=(format:size (stat -c '%s' $in))]
|
[&k='Taille' &v=(format:size (stat -c '%s' $in))]
|
||||||
|
@ -338,47 +252,31 @@ fn info {|@args|
|
||||||
var sep = (format:repeat (+ $props[0][size] $props[1][size] 3) '.')
|
var sep = (format:repeat (+ $props[0][size] $props[1][size] 3) '.')
|
||||||
var @part = $data[..5] $data[5..12] $data[12..]
|
var @part = $data[..5] $data[5..12] $data[12..]
|
||||||
each {|d|
|
each {|d|
|
||||||
format:list &with-header=$false &csep=' : ' &recompute=$false $props $d
|
format:list &with-header=$false &csep=' : ' &recompute=$false $props $@d
|
||||||
echo $sep
|
echo $sep
|
||||||
} $part
|
} $part
|
||||||
}
|
}
|
||||||
|
|
||||||
fn -list-attachments {|in|
|
fn list-attachments {|in| all (pdf:attachments $in)}
|
||||||
all (pdf:attachments $in)
|
fn display-attachments {|in|
|
||||||
}
|
var @data = (list-attachments $in)
|
||||||
|
|
||||||
fn -display-attachments {|in|
|
|
||||||
var @data = (-list-attachments $in)
|
|
||||||
printf "%d pièce(s)-jointe(s) trouvée(s):\n\n" (count $data)
|
printf "%d pièce(s)-jointe(s) trouvée(s):\n\n" (count $data)
|
||||||
all $data | each $echo~
|
each $echo~ $data
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
fn extract-attachments {|in out| list-attachments $in | each {|f| pdf:attachment $in $out/$f $f }}
|
||||||
fn -extract-attachments {|in out|
|
fn list-fonts {|in|
|
||||||
-list-attachments $in | each {|f|
|
|
||||||
pdf:attachment $in $out/$f $f
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn -list-fonts {|in|
|
|
||||||
pdf:fonts $in | each {|f|
|
pdf:fonts $in | each {|f|
|
||||||
if (has-key $f /FontDescriptor) {
|
-cc (has-key $f /FontDescriptor) { put [&font=$f &fd=$f[/FontDescriptor]] }
|
||||||
put [&font=$f &fd=$f[/FontDescriptor]]
|
|
||||||
}
|
|
||||||
} | each {|f|
|
} | each {|f|
|
||||||
if (has-key $f[fd] /FontName) {
|
-cc (has-key $f[fd] /FontName) { assoc $f name $f[fd][/FontName] }
|
||||||
assoc $f name $f[fd][/FontName]
|
|
||||||
}
|
|
||||||
} | each {|f|
|
} | each {|f|
|
||||||
var @_ n = (str:split '+' $f[name])
|
var @_ n = (str:split '+' $f[name])
|
||||||
var font = $f[font]
|
var font = $f[font]
|
||||||
var fd = $f[fd]
|
var fd = $f[fd]
|
||||||
var embed = (has-key $fd /FontFile2)
|
var embed = (has-key $fd /FontFile2)
|
||||||
var file = ''
|
var file = (-c $embed $fd[/FontFile2] '')
|
||||||
set n = (str:trim-prefix $n '/')
|
set n = (str:trim-prefix $n '/')
|
||||||
if $embed {
|
|
||||||
set file = $fd[/FontFile2]
|
|
||||||
}
|
|
||||||
put [
|
put [
|
||||||
&id=$font[id]
|
&id=$font[id]
|
||||||
&name=$n
|
&name=$n
|
||||||
|
@ -389,9 +287,8 @@ fn -list-fonts {|in|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn display-fonts {|in|
|
||||||
fn -display-fonts {|in|
|
var @data = (list-fonts $in)
|
||||||
var @data = (-list-fonts $in)
|
|
||||||
printf "%d police(s) trouvée(s):\n\n" (count $data)
|
printf "%d police(s) trouvée(s):\n\n" (count $data)
|
||||||
var props = [
|
var props = [
|
||||||
(format:column &align=center id ID)
|
(format:column &align=center id ID)
|
||||||
|
@ -401,26 +298,19 @@ fn -display-fonts {|in|
|
||||||
(format:column &align=center embed Téléchargeable)
|
(format:column &align=center embed Téléchargeable)
|
||||||
(format:column &align=center file Fichier)
|
(format:column &align=center file Fichier)
|
||||||
]
|
]
|
||||||
format:list $props $data
|
format:list $props $@data
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
fn extract-fonts {|in out|
|
||||||
fn -extract-fonts {|in out|
|
list-fonts $in | each {|f|
|
||||||
-list-fonts $in | each {|f|
|
-cc $f[embed] { pdf:filtered-stream $in (printf '%s/%s.ttf' $out $f[name]) $f[file] }
|
||||||
if $f[embed] {
|
|
||||||
pdf:filtered-stream $in (printf '%s/%s.ttf' $out $f[name]) $f[file]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
fn list-images {|in|
|
||||||
|
|
||||||
fn -list-images {|in|
|
|
||||||
pdf:images $in | each {|i|
|
pdf:images $in | each {|i|
|
||||||
var c = ''
|
|
||||||
var w = (format:int $i[/Width])
|
var w = (format:int $i[/Width])
|
||||||
var h = (format:int $i[/Height])
|
var h = (format:int $i[/Height])
|
||||||
if (has-key $i /ColorSpace) {
|
var c = (-c (has-key $i /ColorSpace) { put $i[/ColorSpace][0] } '')
|
||||||
set c = $i[/ColorSpace][0]
|
|
||||||
}
|
|
||||||
put [
|
put [
|
||||||
&id=$i[id]
|
&id=$i[id]
|
||||||
&page=(format:int $i[page])
|
&page=(format:int $i[page])
|
||||||
|
@ -441,12 +331,9 @@ fn -list-images {|in|
|
||||||
var id = $img[object]
|
var id = $img[object]
|
||||||
var o = (pdf:-object &extend=$true $objects $id)
|
var o = (pdf:-object &extend=$true $objects $id)
|
||||||
var s = $o[/Length]
|
var s = $o[/Length]
|
||||||
var c = ''
|
var c = (-c (has-key $o /ColorSpace) { put $o[/ColorSpace][0] } '')
|
||||||
var w = (format:int $o[/Width])
|
var w = (format:int $o[/Width])
|
||||||
var h = (format:int $o[/Height])
|
var h = (format:int $o[/Height])
|
||||||
if (has-key $o /ColorSpace) {
|
|
||||||
set c = $o[/ColorSpace][0]
|
|
||||||
}
|
|
||||||
put [
|
put [
|
||||||
&id=$id
|
&id=$id
|
||||||
&page=$pn
|
&page=$pn
|
||||||
|
@ -462,9 +349,8 @@ fn -list-images {|in|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn display-images {|in|
|
||||||
fn -display-images {|in|
|
var @data = (list-images $in)
|
||||||
var @data = (-list-images $in)
|
|
||||||
printf "%d image(s) trouvée(s):\n\n" (count $data)
|
printf "%d image(s) trouvée(s):\n\n" (count $data)
|
||||||
var props = [
|
var props = [
|
||||||
(format:column &align=right page Page)
|
(format:column &align=right page Page)
|
||||||
|
@ -476,54 +362,45 @@ fn -display-images {|in|
|
||||||
(format:column &align=right bpc BPC)
|
(format:column &align=right bpc BPC)
|
||||||
(format:column &align=center filter Filtre)
|
(format:column &align=center filter Filtre)
|
||||||
]
|
]
|
||||||
format:list $props $data
|
format:list $props $@data
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
fn extract-images {|in out|
|
||||||
fn -extract-images {|in out|
|
|
||||||
# En attendant de savoir décoder correctement les images, on utilise poppler
|
# En attendant de savoir décoder correctement les images, on utilise poppler
|
||||||
pdfimages -all $in $out/Im
|
pdfimages -all $in $out/Im
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list {|@args|
|
fn list {|@args|
|
||||||
if (!= (count $args) 2) {
|
-cc (!= (count $args) 2) { fail 'list doit comporter 2 paramètres' }
|
||||||
fail 'list doit comporter 2 paramètres'
|
|
||||||
}
|
|
||||||
var types in = $@args
|
var types in = $@args
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
var display = [
|
var display = [
|
||||||
&a=$-display-attachments~
|
&a=$display-attachments~
|
||||||
&f=$-display-fonts~
|
&f=$display-fonts~
|
||||||
&i=$-display-images~
|
&i=$display-images~
|
||||||
]
|
]
|
||||||
str:split ',' $types | each {|t|
|
str:split ',' $types | each {|t|
|
||||||
set t = (str:to-lower $t)
|
set t = (str:to-lower $t)
|
||||||
-must-valid $t {|e|
|
must-valid $t {|e| and (> (count $e) 0) (has-key $display $e[0])}
|
||||||
and (> (count $e) 0) (has-key $display $e[0])
|
|
||||||
}
|
|
||||||
$display[$t[0]] $in
|
$display[$t[0]] $in
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract {|@args|
|
fn extract {|@args|
|
||||||
if (!= (count $args) 3) {
|
-cc (!= (count $args) 3) { fail 'extract doit comporter 3 paramètres' }
|
||||||
fail 'extract doit comporter 3 paramètres'
|
|
||||||
}
|
|
||||||
var types in out = $@args
|
var types in out = $@args
|
||||||
-must-pdf-exist $in
|
must-pdf-exist $in
|
||||||
-must-not-exist $out
|
must-not-exist $out
|
||||||
var extr = [
|
var extr = [
|
||||||
&a=$-extract-attachments~
|
&a=$extract-attachments~
|
||||||
&f=$-extract-fonts~
|
&f=$extract-fonts~
|
||||||
&i=$-extract-images~
|
&i=$extract-images~
|
||||||
]
|
]
|
||||||
try {
|
try {
|
||||||
mkdir $out
|
mkdir $out
|
||||||
str:split ',' $types | each {|t|
|
str:split ',' $types | each {|t|
|
||||||
set t = (str:to-lower $t)
|
set t = (str:to-lower $t)
|
||||||
-must-valid $t {|e|
|
must-valid $t {|e| and (> (count $e) 0) (has-key $extr $e[0])}
|
||||||
and (> (count $e) 0) (has-key $extr $e[0])
|
|
||||||
}
|
|
||||||
$extr[$t[0]] $in $out
|
$extr[$t[0]] $in $out
|
||||||
}
|
}
|
||||||
} except e {
|
} except e {
|
||||||
|
@ -532,7 +409,33 @@ fn extract {|@args|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
edit:add-var pdf~ {|action @args|
|
fn gray {|@args|
|
||||||
|
-cc (!= (count $args) 2) { fail 'gray doit comporter 2 paramètres' }
|
||||||
|
var in out = $@args
|
||||||
|
must-pdf-exist $in
|
||||||
|
must-pdf-not-exist $out
|
||||||
|
convert -colorspace gray $in $out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mono {|@args|
|
||||||
|
var c = (count $args)
|
||||||
|
-cc (or (< $c 2) (> $c 3)) { fail 'mono doit comporter entre 2 et 3 paramètres' }
|
||||||
|
var t = $nil
|
||||||
|
if (pdf:is-number $args[0]) {
|
||||||
|
set t @args = $@args
|
||||||
|
-cc (or (< $t 0) (> $t 100)) { fail le taux doit être compris entre 0 et 100 }
|
||||||
|
-cc (< $c 2) { fail 'mono doit comporter 3 paramètres' }
|
||||||
|
}
|
||||||
|
var in out = $@args
|
||||||
|
must-pdf-exist $in
|
||||||
|
must-pdf-not-exist $out
|
||||||
|
if $t {
|
||||||
|
convert -colorspace gray -threshold $t'%' $in $out
|
||||||
|
} else {
|
||||||
|
convert -monochrome $in $out
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var actions = [
|
var actions = [
|
||||||
&help=$help~
|
&help=$help~
|
||||||
&decrypt=$decrypt~
|
&decrypt=$decrypt~
|
||||||
|
@ -549,9 +452,10 @@ edit:add-var pdf~ {|action @args|
|
||||||
&info=$info~
|
&info=$info~
|
||||||
&list=$list~
|
&list=$list~
|
||||||
&extract=$extract~
|
&extract=$extract~
|
||||||
|
&gray=$gray~
|
||||||
|
&mono=$mono~
|
||||||
|
#&compress=$compress~
|
||||||
]
|
]
|
||||||
if (not (has-key $actions $action)) {
|
-cc (not (has-key $actions $action)) { fail (printf '%s: action inconnue' $action) }
|
||||||
fail (printf '%s: action inconnue' $action)
|
|
||||||
}
|
|
||||||
$actions[$action] $@args
|
$actions[$action] $@args
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,11 @@ Commandes:
|
||||||
Retourne un fichier déchiffré. Si aucun mot de passe n’est donné, tente le déchiffrement
|
Retourne un fichier déchiffré. Si aucun mot de passe n’est donné, tente le déchiffrement
|
||||||
avec un mot de passe à blanc
|
avec un mot de passe à blanc
|
||||||
Fichier de sortie par défaut : <inputfile>_decrypted.pdf
|
Fichier de sortie par défaut : <inputfile>_decrypted.pdf
|
||||||
|
gray: <inputfile> <outputfile>
|
||||||
|
Convertit le fichier d’entrée en nuances de gris
|
||||||
|
mono: [<split>] <inputfile> <outputfile>
|
||||||
|
Convertit le fichier d’entrée en noir et blanc, au taux de coupure (exprimé en pourcentage, entre 0 et 100)
|
||||||
|
Défauts : <split> = 50
|
||||||
list: (a(ttachments)|i(mages)|f(onts)) <inputfile>
|
list: (a(ttachments)|i(mages)|f(onts)) <inputfile>
|
||||||
Affiche la liste des fichiers inclus en filtrant selon le type (pièce-jointe, image ou police)
|
Affiche la liste des fichiers inclus en filtrant selon le type (pièce-jointe, image ou police)
|
||||||
extract: (a(ttachments)|i(mages)|f(onts)) <inputfile> <outputdir>
|
extract: (a(ttachments)|i(mages)|f(onts)) <inputfile> <outputdir>
|
||||||
|
|
|
@ -1,82 +0,0 @@
|
||||||
edit:add-var pdf2grey~ {|@argv|
|
|
||||||
use re
|
|
||||||
use str
|
|
||||||
var m = [
|
|
||||||
&nb=$false
|
|
||||||
&split=50
|
|
||||||
]
|
|
||||||
|
|
||||||
fn _help {||
|
|
||||||
echo 'Usage: pdf2gray [-hn (<n>)] <input>.pdf <output>.pdf'
|
|
||||||
echo ''
|
|
||||||
echo 'Options:'
|
|
||||||
echo ' -h Affiche cette aide'
|
|
||||||
echo ' -n [0-100] Convertit en noir et blanc. Si un nombre est spécifié, indique le pourcentage de coupure (par défaut: 50)'
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_pdf {|e|
|
|
||||||
re:match '.*\.pdf$' $e
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_int {|e|
|
|
||||||
re:match '^\d+$' $e
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_split {|e|
|
|
||||||
and (>= $e 0) (<= $e 100)
|
|
||||||
}
|
|
||||||
|
|
||||||
while (> (count $argv) 0) {
|
|
||||||
var e @argv = $@argv
|
|
||||||
if (or (eq $e '-h') (eq $e '--help')) {
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
} elif (eq $e -n) {
|
|
||||||
set m[nb] = $true
|
|
||||||
if (== (count $e) 0) {
|
|
||||||
echo 'Il manque des arguments après -s'
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
} elif (is_int $argv[0]) {
|
|
||||||
set e @argv = $@argv
|
|
||||||
if (is_split $e) {
|
|
||||||
set m[split] = $e
|
|
||||||
} else {
|
|
||||||
echo 'L’argument (optionnel) après -s doit être compris entre 0 et 100'
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elif (not (is_pdf $e)) {
|
|
||||||
echo 'Argument non reconnu: '$e
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
} elif (not (has-key $m input)) {
|
|
||||||
set m[input] = $e
|
|
||||||
} else {
|
|
||||||
set m[output] = $e
|
|
||||||
if (> (count $argv) 0) {
|
|
||||||
echo 'Trop d’arguments: '$@argv
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (not (has-key $m input)) {
|
|
||||||
echo 'Manque le input'
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
} elif (not (has-key $m output)) {
|
|
||||||
echo 'Manque le output'
|
|
||||||
_help
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var params = [-colorspace gray]
|
|
||||||
if $m[nb] {
|
|
||||||
set @params = (all $params) -threshold $m[split]'%'
|
|
||||||
}
|
|
||||||
set @params = $@params $m[input] $m[output]
|
|
||||||
echo 'Commande: convert '(str:join ' ' $params)
|
|
||||||
convert $@params
|
|
||||||
}
|
|
Loading…
Reference in New Issue