Next / Previous / Contents / Shipman's homepage

13. pageDimFactory: Create a standard PageDim instance

fohelpers.py
# - - -   p a g e D i m F a c t o r y

def pageDimFactory ( paperType ):
    '''Create a new PageDim instance for a standard sheet size.
    '''

See Section 4.7, “pageDimFactory(): Some pre-built PageDim instances”. This function has a somewhat amusing history. Formerly, there was a dictionary named pageDimSet with three PageDim instances for the three sheet sizes. However, an earlier version of the fohelptest script set up even and odd page masters like this:

oddDims = pageDimSet["letter"]    # This failed!
oddDims.pageMargins.left="2.0in"
oddDims.pageMargins.right="1.0in"
evenDims = pageDimSet["letter"]
evenDims.pageMargins.left="1.0in"
evenDims.pageMargins.right="2.0in"

Unfortunately, the names oddDims and evenDims were bound to the same instance, so both page masters used the even-side page layout.

The fix is this function, which creates a new instance each time it is called. To aid in the modification of dimensions by adding and subtracting and multiplying, some dimensions are expressed in terms of instances of Section 9, “class FoDim: Dimensional arithmetic”.

fohelpers.py
    return paperTypeMap[paperType]()

def letterPage():
    return PageDim (
        pageBox=Box(FoDim("8.5", "in"), FoDim("11", "in")),
        pageMargins=MarginSet(top=FoDim("0.5", "in"),
            bot=FoDim("0.5", "in"), left=FoDim("0.5", "in"),
            right=FoDim("0.5", "in")),
        frameMargins=MarginSet(top=FoDim("3", "pc"),
            bot=FoDim("3", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")),
        bodyMargins=MarginSet(top=FoDim("1", "pc"),
            bot=FoDim("1", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")))

def letterLandPage():
    return PageDim (
        pageBox=Box(FoDim("11", "in"), FoDim("8.5", "in")),
        pageMargins=MarginSet(top=FoDim("0.5", "in"),
            bot=FoDim("0.5", "in"), left=FoDim("0.5", "in"),
            right=FoDim("0.5", "in")),
        frameMargins=MarginSet(top=FoDim("3", "pc"),
            bot=FoDim("3", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")),
        bodyMargins=MarginSet(top=FoDim("1", "pc"),
            bot=FoDim("1", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")))

def legalPage():
    return PageDim (
        pageBox=Box(FoDim("8.5", "in"), FoDim("14", "in")),
        pageMargins=MarginSet(top=FoDim("0.5", "in"),
            bot=FoDim("0.5", "in"), left=FoDim("0.5", "in"),
            right=FoDim("0.5" "in")),
        frameMargins=MarginSet(top=FoDim("3", "pc"),
            bot=FoDim("3", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")),
        bodyMargins=MarginSet(top=FoDim("1", "pc"),
            bot=FoDim("1", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")))

def indexCardPage():
    return PageDim (
        pageBox=Box(FoDim("3", "in"), FoDim("5", "in")),
        pageMargins=MarginSet(top=FoDim("0.1", "in"),
            bot=FoDim("0.1", "in"), left=FoDim("0.25", "in"),
            right=FoDim("0.25", "in")),
        frameMargins=MarginSet(top=FoDim("1.5", "pc"),
            bot=FoDim("1", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")), 
        bodyMargins=MarginSet(top=FoDim("1", "pc"),
            bot=FoDim("1", "pc"), left=FoDim("0", "pc"),
            right=FoDim("0", "pc")) )

paperTypeMap = {
  PAPER_LETTER: letterPage,
  PAPER_LETTER_LAND: letterLandPage,
  PAPER_LEGAL: legalPage,
  PAPER_3X5: indexCardPage }