gpt4 book ai didi

perl - 改进我的 Perl 算法以合并 postscript 显示命令

转载 作者:行者123 更新时间:2023-12-04 05:22:54 24 4
gpt4 key购买 nike

Matlab R2007b 的后记输出是有问题的。我发现文本字符串被分割成许多 ' moveto ' 和 'show' 命令在 postscript 输出 (simprintdiag) 中。这会导致在排版为 PDF 时出现问题,有时可以将额外的空白插入到标签中(因此您无法双击它们,并且在搜索中找不到!)。

为了避免这个问题,我编写了一个 Perl 脚本将这些拆分的“显示”命令重新连接在一起,但是,它有一些问题,我需要一些帮助。

  • 像“(0) s”这样的显示命令没有正确重复,并出现在下一个块中。
  • 即使不需要更改,输入 postscript 文件也始终由脚本修改。
  • 一开始有一个技巧可以绕过连续的显示命令。
  • 它不是很快,并且考虑到一些项目有超过 2000 个 postscript 文件,欢迎任何速度改进。

  • 我下面代码中的 DATA 有四个在 mt 和 s 命令中拆分文本字符串的示例。我已经在最后包含了最终输出应该是什么样子。该脚本使用了这样一个事实,即我们的文本是从左到右书写的,或者在附言中,使用移动的 X 线和固定的 Y 线。因此,可以得出结论,具有相同 Y 线的连续 mt 命令是相同的文本字符串。

    感激地收到任何帮助。

    谢谢:)

    我的 Perl 脚本:
    use strict;
    use warnings;

    my $debug=1;

    #
    ## Slurp the input file into a variable
    my $ps_in;
    while(<DATA>) {
    $ps_in .= $_; # Take a copy of input file
    }


    #
    ## HACK
    ## The main PS fix algorithm only works with show commands on a single
    ## line! Fix the input contents now by joining all show commands that
    ## occur over multiple lines. Examples of this are:
    ## 272 63 mt
    ## (main is an externally linked function of the ACC feature ru\
    ## nning every ) s
    ## 991 63 mt
    ## (100) s
    my $buf;
    my $no_show_split;
    open(my $fh_ps, "<", \$ps_in );
    while(<$fh_ps>) {
    if( /^(.*)\\$/ ) { # Match on all lines ending with backslash \
    $buf .= $1;
    }
    else {
    if( $buf ) {
    $no_show_split .= $buf;
    undef($buf);
    }
    $no_show_split .= $_;
    }
    }
    close $fh_ps;

    #
    ## Reopen our ps input, now the show splits have been removed
    open($fh_ps,"<",\$no_show_split );

    my $moveto_line = qr/^\s*\d+\s+(\d+)\s+(mt|moveto)/; # Example '2831 738 mt'
    my $show_line = qr/^\((.+)\)\s+(s|show)/; # Example '(chris) s'
    my $ycrd; # Y-axis cords
    my $pstxt; # Text to display
    my $mtl; # Moveto line
    my $print_text;
    my $fixes=0;
    my $ps_condensed;

    while(<$fh_ps>) {

    if( $print_text ) {
    $ps_condensed .= "$mtl\n";
    $ps_condensed .= "($pstxt) s\n";
    print "($pstxt) s\n====================\n" if $debug;
    undef($ycrd);
    undef($pstxt);
    $print_text=0;
    ++$fixes;
    }

    if( /$moveto_line/ ) {
    chomp;

    if( !$ycrd ) {
    $mtl=$_; # Store this line for print later
    $ycrd=$1; # Match on y-axis value
    redo; # Redo this iteration so we can read the show line in
    }
    elsif( $1 == $ycrd ) {
    <$fh_ps> =~ /$show_line/; # Read in the show line
    $pstxt .= $1; # Built up string we want
    print " $mtl -->$1<--\n" if $debug;
    }
    else {
    $print_text=1; # Dropped out matching on y-cord so force a print
    redo; # Need to redo this line again
    }
    }
    else {
    if( $pstxt ) { # Print if we have something in buffer
    $print_text=1;
    redo;
    }
    $ps_condensed .= $_;
    }

    } # End While Loop
    close $fh_ps;

    print $ps_condensed;


    __DATA__
    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 60 FMSR

    11214 11653 mt
    (0) s
    4.5 w
    156 0 2204 19229 2 MP stroke
    156 0 2204 19084 2 MP stroke

    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 120 FMSR

    8913 14971 mt
    (Function) s
    9405 14971 mt
    (-) s
    9441 14971 mt
    (Call) s
    9009 15127 mt
    (Generator) s
    6 w


    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 120 FMSR

    4962 4747 mt
    (trigger) s
    5322 4747 mt
    (_) s
    5394 4747 mt
    (scheduler) s
    5934 4747 mt
    (_) s
    6006 4747 mt
    (100) s
    6222 4747 mt
    (ms) s
    6378 4747 mt
    (_) s
    6450 4747 mt
    (task) s
    6654 4747 mt
    (_) s
    6726 4747 mt
    (06) s
    6 w
    gr

    24 10 10 24 0 4 -10 24 -24 10 5806 11736 14 MP stroke
    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 120 FMSR

    5454 11947 mt
    (Chris_\
    did_this_example_) s
    5874 11947 mt
    (to_test) s
    5946 11947 mt
    (_out) s
    6 w

    最终的“浓缩”后记应该是这样的:
    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 60 FMSR

    11214 11653 mt
    (0) s
    4.5 w
    156 0 2204 19229 2 MP stroke
    156 0 2204 19084 2 MP stroke

    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 120 FMSR

    8913 14971 mt
    (Function-Call) s
    9009 15127 mt
    (Generator) s
    6 w


    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 120 FMSR

    4962 4747 mt
    (trigger_scheduler_100ms_task_06) s
    6 w
    gr

    24 10 10 24 0 4 -10 24 -24 10 5806 11736 14 MP stroke
    %%IncludeResource: font Helvetica
    /Helvetica /WindowsLatin1Encoding 120 FMSR

    5454 11947 mt
    (Chris_did_this_example_to_test_out) s
    6 w

    最佳答案

    我认为以下对你有用。

    注意事项:

  • 用习语吞下所有数据:do { local $/; <DATA> } ;
  • 使用单个正则表达式修复行尾的反斜杠

  • use strict;
    use warnings;

    my $data = do { local $/; <DATA> };
    $data =~ s,\\\n,,g;

    my $out = "";
    my $s = "";
    my $y;

    for my $line (split("\n", $data)) {
    if (defined($y) && $line =~ m/^\((.*)\)\s+s\s*$/) {
    $s .= $1;
    next;
    } elsif ($line =~ m/^(\d+)\s+(\d+)\s+mt\s*$/) {
    if (defined($y) && $y == $2) {
    next;
    } else {
    $y = $2;
    }
    } else {
    $y = undef;
    }
    if (length($s)) {
    $out .= "($s) s\n";
    $s = "";
    }
    $out .= "$line\n";
    }

    print $out;

    关于perl - 改进我的 Perl 算法以合并 postscript 显示命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13517016/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com