Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #2861

Getting Rid Of PostScript

Newsgroups comp.lang.postscript
Date 2016-10-05 18:24 -0700
Message-ID <52007eb3-d23e-4938-bb11-08ff341ed5a9@googlegroups.com> (permalink)
Subject Getting Rid Of PostScript
From Lawrence D’Oliveiro <lawrencedo99@gmail.com>

Show all headers | View raw


I recently reworked a Python script I created many years ago to lay out and print labels for audio CDs. This involves some intricate formatting of Unicode-encoded text, with leader tabs, left- and right-aligned fields, and also making sure that long track titles fitted into a fixed-width area.

Originally the script generated PostScript, which I fed through Ghostscript to generate a PNG file for printing. I just reworked the script to make direct Cairo calls to generate exactly the same layout.

Result? The code shrank from 386 lines to just 239. The single biggest saving was in the handling of Unicode-encoded text. My original routines for rendering the text originally looked like this:

    gs.write \
      (
        # theory of operation: the Decoding resource named Unicode is a dictionary
        # mapping glyphnames to Unicode character codes. However, multiple glyph names
        # can map to the same character code. I construct the inverse of this mapping,
        # where each character code maps to a dictionary with all the corresponding
        # glyph names as keys (and dummy values). To render a Unicode character,
        # I try all the glyph names corresponding to that character code until I
        # find a match in the CharStrings dictionary of the currentfont.
        "/UnicodeEncode 256 dict def\n" # initial size doesn't matter
        "/DefineCode\n" # glyphname charcode DefineCode -
          # inserts a correspondence between the specified glyph name and character
          # code into UnicodeEncode.
        "  {\n"
        "    UnicodeEncode 1 index known not\n"
        "       { % if\n"
        "        UnicodeEncode 1 index 1 dict put\n" # first glyph name for this character code
        "      }\n"
        "    if\n"
        "    UnicodeEncode exch get exch true put\n"
                # add another glyph name to dict for that character code
                # glyph name is key, value is unimportant
        "  }\n"
        "def\n"
        "/Unicode /Decoding findresource {DefineCode} forall\n"
          # mapping from Unicode character code to possible PostScript glyph names
        "/minus 45 DefineCode\n" # needed for Palatino-Roman
        "\n"
        "/UniShow\n" # chararray UniShow --
          # given an array of Unicode character codes, does a show of the corresponding
          # glyphs in turn. Assumes it's a Type 1 font.
        "  {\n"
        "      {\n"
        "        dup UnicodeEncode exch known\n"
        "          {\n"
        "            UnicodeEncode exch get\n"
        "            /.notdef exch\n" # in case no glyph name found
        "              { % forall\n"
        "                pop\n" # get rid of dummy value
        "                currentfont /CharStrings get 1 index known\n"
        "                  {\n"
        "                    exch pop\n" # get rid of .notdef, leave found key
        "                    exit\n"
        "                  }\n"
        "                  {\n"
        "                    pop\n"
        "                  }\n"
        "                ifelse\n"
        "              }\n"
        "            forall\n"
        "          }\n"
        "          {\n"
        "            pop /.notdef\n"
        "          }\n"
        "        ifelse\n"
        "        glyphshow\n"
        "      }\n"
        "    forall\n"
        "  }\n"
        "def\n"
        "\n"
        "/UniWidth\n" # chararray UniWidth xdelta ydelta
          # given an array of Unicode character codes, returns the width
          # of the complete string.
        "  {\n"
        "   currentpoint 3 2 roll\n" # save currentpoint (assumes there is one!)
        "   clippath pathbbox 4 2 roll pop pop\n"
        "   exch 100 add exch moveto\n" # guaranteed to be outside current clip
        "   currentpoint 3 2 roll UniShow currentpoint\n"
        "   3 2 roll sub 3 1 roll exch sub exch\n"
        "   4 2 roll moveto\n" # restore currentpoint
        "  }\n"
        "def\n"
        "\n"
        "/UniShowMaxFit\n" # chararray maxwidth UniShowMaxFit --
          # given an array of Unicode character codes, does a show of the corresponding
          # glyphs in turn, ensuring that the width of the string does not exceed
          # maxwidth.
        "  {\n"
        "    currentfont 3 1 roll\n" # save current font
        "    1 index UniWidth pop dup 2 index gt\n"
        "      {\n"
        "        div currentfont exch 1 matrix scale makefont setfont\n"
        "      }\n"
        "      {\n"
        "        pop pop\n"
        "      }\n"
        "    ifelse\n"
        "    UniShow\n"
        "    setfont\n" # restore previous font
        "  }\n"
        "def\n"
      )

The replacement Python code looks like this:

    def show_max_fit(ctx, text, maxwidth) :
        "shows text at the current position in ctx, temporarily adjusting the" \
        " font matrix so its width does not exceed maxwidth."
        save_font_matrix = None # to begin with
        width = ctx.text_extents(text).advance.x
        if width > maxwidth :
            save_font_matrix = ctx.font_matrix
            ctx.font_matrix *= Matrix.scale((maxwidth / width, 1))
        #end if
        ctx.show_text(text)
        if save_font_matrix != None :
            ctx.font_matrix = save_font_matrix
        #end if
    #end show_max_fit

Ah, but what about the “UniShow” and “UniWidth” routines, you are probably wondering? They were replaced with direct one-line Cairo calls. You can see instances of both of them in the “show_max_fit” routine.

Back to comp.lang.postscript | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-05 18:24 -0700
  Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-06 14:50 -0700
  Re: Getting Rid Of PostScript jdaw1 <jdawiseman@gmail.com> - 2016-10-06 15:45 -0700
    Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-06 17:44 -0700
    Re: Getting Rid Of PostScript Mark Carroll <mtbc@bcs.org> - 2016-10-07 08:47 +0100
      Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-07 15:08 -0700
        Re: Getting Rid Of PostScript luser droog <luser.droog@gmail.com> - 2016-10-07 18:47 -0700
        Re: Getting Rid Of PostScript Mark Carroll <mtbc@bcs.org> - 2016-10-08 12:34 +0100
  Re: Getting Rid Of PostScript luser droog <luser.droog@gmail.com> - 2016-10-07 18:39 -0700
    Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-07 19:07 -0700
      Re: Getting Rid Of PostScript luser droog <luser.droog@gmail.com> - 2016-10-12 15:32 -0700
        Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-12 17:06 -0700
        Re: Getting Rid Of PostScript rodd@panix.com (Rod Dorman) - 2016-10-13 19:07 +0000
          Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-13 15:02 -0700
            Re: Getting Rid Of PostScript rodd@panix.com (Rod Dorman) - 2016-10-14 20:13 +0000
              Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-14 14:39 -0700
              Re: Getting Rid Of PostScript Martin Leese <please@see.Web.for.e-mail.INVALID> - 2016-10-16 10:40 -0600
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-17 18:06 -0700
                Re: Getting Rid Of PostScript Alan <alan.isaac@gmail.com> - 2016-10-17 18:44 -0700
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-17 22:43 -0700
                Re: Getting Rid Of PostScript Martin Leese <please@see.Web.for.e-mail.INVALID> - 2016-10-18 10:13 -0600
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-18 15:28 -0700
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-18 15:35 -0700
                Re: Getting Rid Of PostScript Ross Presser <rpresser@gmail.com> - 2016-10-18 23:05 -0700
                Re: Getting Rid Of PostScript Carlos <angus@quovadis.com.ar> - 2016-10-19 19:23 +0200
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-19 17:17 -0700
                Re: Getting Rid Of PostScript Ross Presser <rpresser@gmail.com> - 2016-10-21 09:18 -0700
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-21 18:10 -0700
                Re: Getting Rid Of PostScript Ross Presser <rpresser@gmail.com> - 2016-10-21 09:14 -0700
                Re: Getting Rid Of PostScript Markus Triska <triska@metalevel.at> - 2016-10-19 20:55 +0200
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-19 17:57 -0700
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-24 16:54 -0700
                Re: Getting Rid Of PostScript Carlos <angus@quovadis.com.ar> - 2016-10-25 16:26 +0200
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-25 10:32 -0700
                Re: Getting Rid Of PostScript Carlos <angus@quovadis.com.ar> - 2016-10-25 21:06 +0200
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-11-14 15:15 -0800
                Re: Getting Rid Of PostScript Carlos <angus@quovadis.com.ar> - 2016-11-16 00:17 +0100
                Re: Getting Rid Of PostScript Carlos <angus@quovadis.com.ar> - 2016-11-21 18:37 +0100
                Re: Getting Rid Of PostScript Carlos <angus@quovadis.com.ar> - 2016-11-21 18:39 +0100
                Re: Getting Rid Of PostScript Markus Triska <triska@metalevel.at> - 2016-10-25 20:52 +0200
                Re: Getting Rid Of PostScript Alan <alan.isaac@gmail.com> - 2016-10-19 16:41 -0700
                Re: Getting Rid Of PostScript Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-10-19 17:15 -0700

csiph-web