[groff] 01/01: [gropdf] Handle both types in one document.

2024-04-17 Thread Deri James
deri pushed a commit to branch master
in repository groff.

commit 0f96f3a281d82bb0c1a59e5e819d2a22bede6c5d
Author: Deri James 
AuthorDate: Thu Apr 18 01:17:22 2024 +0100

[gropdf] Handle both types in one document.

The different format of font described in previous commit fails if
document contains fonts of both formats. The reason is because the
regexes included the /o flag (compile once) for speed, but if the
format changed (from RD to -|) in a different font, parsing failed.

Now the regexes are compiled once for each font.

* src/devices/gropdf/gropdf.pl: Use qr// to compile regexes once
per font.
---
 ChangeLog| 14 ++
 src/devices/gropdf/gropdf.pl |  9 ++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1cce0827d..a88d52c5d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2024-04-17  Deri James  
+
+   [gropdf] Handle both types in one document.
+
+   The different format of font described in previous commit fails if
+   document contains fonts of both formats. The reason is because the
+   regexes included the /o flag (compile once) for speed, but if the
+   format changed (from RD to -|) in a different font, parsing failed.
+
+   Now the regexes are compiled once for each font.
+
+   * src/devices/gropdf/gropdf.pl: Use qr// to compile regexes once
+   per font.
+
 2024-04-17  Deri James  
 
[gropdf] Improve font parsing.
diff --git a/src/devices/gropdf/gropdf.pl b/src/devices/gropdf/gropdf.pl
index 240f1025c..59434a8ec 100644
--- a/src/devices/gropdf/gropdf.pl
+++ b/src/devices/gropdf/gropdf.pl
@@ -4601,6 +4601,7 @@ sub map_subrs
 my $stage=0;
 my $lin=$lines->[0];
 my $i=0;
+my ($RDre,$NDre);
 
 for (my $j=0; $j<=$#{$lines}; $lin=$lines->[++$j] )
 {
@@ -4612,6 +4613,8 @@ sub map_subrs
{
$sec{'#Subrs'}=$j;
$stage=1;
+   $RDre=qr/\Q$RD\E/;
+   $NDre=qr/\Q$ND\E/;
}
elsif ($lin=~m/^\/(.+?)\s+\{string currentfile exch readstring 
pop\}\s*executeonly def/)
{
@@ -4638,7 +4641,7 @@ sub map_subrs
$stage=2;
$i=0;
}
-   elsif ($lin=~m/^\s*dup\s+(\d+)\s+(\d+)\s+\Q$RD\E (.*)/os)
+   elsif ($lin=~m/^\s*dup\s+(\d+)\s+(\d+)\s+$RDre (.*)/s)
{
my $n=$1;
my $l=$2;
@@ -4666,7 +4669,7 @@ sub map_subrs
 #  subs_call($s,"#$n");
$lines->[$i]=["#$n",$l,$s,$NP];
}
-   elsif ($lin=~m/^\Q$ND\E/o)
+   elsif ($lin=~m/^$NDre/)
{}
else
{
@@ -4680,7 +4683,7 @@ sub map_subrs
$sec{'#Pad'}=$j;
$stage=3;
}
-   elsif ($lin=~m/^\s*\/([-.\w]*)\s+(\d+)\s+\Q$RD\E (.*)/os)
+   elsif ($lin=~m/^\s*\/([-.\w]*)\s+(\d+)\s+$RDre (.*)/s)
{
my $n=$1;
my $l=$2;

___
Groff-commit mailing list
Groff-commit@gnu.org
https://lists.gnu.org/mailman/listinfo/groff-commit


[groff] 01/01: Improve font parsing.

2024-04-17 Thread Deri James
deri pushed a commit to branch master
in repository groff.

commit 5589bcd18776c6d236c53226a0ee30506180e6b6
Author: Deri James 
AuthorDate: Wed Apr 17 19:25:03 2024 +0100

Improve font parsing.

The usual (for fontforge converted ttf fonts) is to use the RD, ND
and NP operators within charstring definitions, however these are
just named in the private subrs dictionary so could be assigned
any name.

A debian .pfb version of a google .ttf font (which has not passed
through fontforge) used -| |- and | as the 3 equivalent. In
addition it used a different lenIV value for the eexec encryption (4)
and the charstring encryption (0) (didn't know you could do that).

* src/devices/gropdf/gropdf.pl: Make RD, ND and NP variables set
from parsing the private subrs. Honour lenIV=0 when encrypting
charstrings.
---
 ChangeLog| 18 ++
 src/devices/gropdf/gropdf.pl | 39 +--
 2 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e56b92eb1..1cce0827d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2024-04-17  Deri James  
+
+   [gropdf] Improve font parsing.
+
+   The usual (for fontforge converted ttf fonts) is to use the RD, ND
+   and NP operators within charstring definitions, however these are
+   just named in the private subrs dictionary so could be assigned
+   any name.
+
+   A debian .pfb version of a google .ttf font (which has not passed
+   through fontforge) used -| |- and | as the 3 equivalent. In
+   addition it used a different lenIV value for the eexec encryption (4)
+   and the charstring encryption (0) (didn't know you could do that).
+
+   * src/devices/gropdf/gropdf.pl: Make RD, ND and NP variables set
+   from parsing the private subrs. Honour lenIV=0 when encrypting
+   charstrings.
+
 2024-04-16  Deri James  
 
[gropdf] Problem with '(' and '\' (\[rs])
diff --git a/src/devices/gropdf/gropdf.pl b/src/devices/gropdf/gropdf.pl
index 5543b0f1f..240f1025c 100644
--- a/src/devices/gropdf/gropdf.pl
+++ b/src/devices/gropdf/gropdf.pl
@@ -361,6 +361,9 @@ my $textenccmap=''; # CMap for groff text.enc encoding
 my @XOstream=();
 my @PageAnnots={};
 my $noslide=0;
+my $ND='ND';
+my $NP='NP';
+my $RD='RD';
 my $transition={PAGE => {Type => '/Trans', S => '', D => 1, Dm => '/H', M => 
'/I', Di => 0, SS => 1.0, B => 0},
BLOCK => {Type => '/Trans', S => '', D => 1, Dm => '/H', M => 
'/I', Di => 0, SS => 1.0, B => 0}};
 my $firstpause=0;
@@ -4577,7 +4580,7 @@ sub encrypt_exec
 sub encrypt_char
 {
 my $la=shift;
-unshift(@{$la},0x44,0x65,0x72,0x69);
+unshift(@{$la},0x44,0x65,0x72,0x69) if $lenIV;
 my $res;
 my $cypher;
 my $cr=C_DEF;
@@ -4610,6 +4613,22 @@ sub map_subrs
$sec{'#Subrs'}=$j;
$stage=1;
}
+   elsif ($lin=~m/^\/(.+?)\s+\{string currentfile exch readstring 
pop\}\s*executeonly def/)
+   {
+   $RD=$1;
+   }
+   elsif ($lin=~m/^\/(.+?)\s+\{noaccess def\}\s*executeonly def/)
+   {
+   $ND=$1;
+   }
+   elsif ($lin=~m/^\/(.+?)\s+\{noaccess put\}\s*executeonly def/)
+   {
+   $NP=$1;
+   }
+   elsif ($lin=~m'^/lenIV\s+(\d+)')
+   {
+   $lenIV=$1;
+   }
}
elsif ($stage == 1)
{
@@ -4619,7 +4638,7 @@ sub map_subrs
$stage=2;
$i=0;
}
-   elsif ($lin=~m/^\s*dup\s+(\d+)\s+(\d+)\s+RD (.*)/s)
+   elsif ($lin=~m/^\s*dup\s+(\d+)\s+(\d+)\s+\Q$RD\E (.*)/os)
{
my $n=$1;
my $l=$2;
@@ -4645,9 +4664,9 @@ sub map_subrs
 
 #  $s=decrypt_char($s);
 #  subs_call($s,"#$n");
-   $lines->[$i]=["#$n",$l,$s,'NP'];
+   $lines->[$i]=["#$n",$l,$s,$NP];
}
-   elsif ($lin=~m/^ND/)
+   elsif ($lin=~m/^\Q$ND\E/o)
{}
else
{
@@ -4661,7 +4680,7 @@ sub map_subrs
$sec{'#Pad'}=$j;
$stage=3;
}
-   elsif ($lin=~m/^\s*\/([-.\w]*)\s+(\d+)\s+RD (.*)/s)
+   elsif ($lin=~m/^\s*\/([-.\w]*)\s+(\d+)\s+\Q$RD\E (.*)/os)
{
my $n=$1;
my $l=$2;
@@ -4690,7 +4709,7 @@ sub map_subrs
}
else
{
-   $lines->[$i]=["/$n",$l,$s,'ND'];
+   $lines->[$i]=["/$n",$l,$s,$ND];
}
 
$i=0;
@@ -4886,7 +4905,7 @@ sub encrypt
 
next if !defined($lin);
 
-   if (ref($lin) eq 'ARRAY' and $lin->[TYPE] eq 'NP')
+   if (ref($lin) eq 'ARRAY' and $lin->[TYPE] eq $NP)
{
foreach my $sub (@subrused)
{
@@ -4894,7 +4913,7 @@ sub encrypt