reportlab/platypus/para.py
author rgbecker
Wed, 10 Nov 2004 18:35:10 +0000
changeset 2408 1c5e79611b59
parent 2200 be0cfccc662a
child 2513 68fc0a67e057
permissions -rw-r--r--
flowables: add SlowPageBreak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     1
"""new experimental paragraph implementation
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     2
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     3
Intended to allow support for paragraphs in paragraphs, hotlinks,
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     4
embedded flowables, and underlining.  The main entry point is the
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     5
function
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     6
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     7
def Paragraph(text, style, bulletText=None, frags=None)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     8
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
     9
Which is intended to be plug compatible with the "usual" platypus
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    10
paragraph except that it supports more functionality.
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    11
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    12
In this implementation you may embed paragraphs inside paragraphs
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    13
to create hierarchically organized documents.
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    14
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    15
This implementation adds the following paragraph-like tags (which
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    16
support the same attributes as paragraphs, for font specification, etc).
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    17
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    18
- Unnumberred lists (ala html):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    19
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    20
    <ul>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    21
        <li>first one</li>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    22
        <li>second one</li>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    23
    </ul>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    24
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    25
Also <ul type="disc"> (default) or <ul type="circle">, <ul type="square">.
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    26
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    27
- Numberred lists (ala html):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    28
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    29
    <ol>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    30
        <li>first one</li>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    31
        <li>second one</li>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    32
    </ol>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    33
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    34
Also <ul type="1"> (default) or <ul type="a">, <ul type="A">.
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    35
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    36
- Display lists (ala HTML):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    37
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    38
For example
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    39
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    40
<dl bulletFontName="Helvetica-BoldOblique" spaceBefore="10" spaceAfter="10">
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    41
<dt>frogs</dt> <dd>Little green slimy things. Delicious with <b>garlic</b></dd>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    42
<dt>kittens</dt> <dd>cute, furry, not edible</dd>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    43
<dt>bunnies</dt> <dd>cute, furry,. Delicious with <b>garlic</b></dd>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    44
</dl>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    45
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    46
ALSO the following additional internal paragraph markup tags are supported
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    47
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    48
<u>underlined text</u>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    49
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    50
<a href="http://www.reportlab.com">hyperlinked text</a>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    51
<a href="http://www.reportlab.com" color="blue">hyperlinked text</a>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    52
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    53
<link destination="end" >Go to the end (go to document internal destination)</link>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    54
<link destination="start" color="cyan">Go to the beginning</link>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    55
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    56
<setLink destination="start" color="magenta">This is the document start
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    57
  (define document destination inside paragraph, color is optional)</setLink>
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    58
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    59
"""
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    60
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    61
from reportlab.pdfbase.pdfmetrics import stringWidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    62
from reportlab.lib.utils import fp_str
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    63
from reportlab.platypus.flowables import Flowable
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    64
from reportlab.lib import colors
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    65
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
    66
from types import StringType, UnicodeType, InstanceType, TupleType, ListType, FloatType
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    67
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    68
# SET THIS TO CAUSE A VIEWING BUG WITH ACROREAD 3 (for at least one input)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    69
# CAUSEERROR = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    70
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    71
debug = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    72
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    73
DUMPPROGRAM = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    74
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    75
TOOSMALLSPACE = 1e-5
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    76
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    77
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT, TA_JUSTIFY
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    78
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    79
# indent changes effect the next line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    80
# align changes effect the current line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    81
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    82
# need to fix spacing questions... if ends with space then space may be inserted
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    83
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    84
# NEGATIVE SPACE SHOULD NEVER BE EXPANDED (IN JUSTIFICATION, EG)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    85
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    86
class paragraphEngine:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    87
    # text origin of 0,0 is upper left corner
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    88
    def __init__(self, program = None):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    89
        from reportlab.lib.colors import black
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    90
        if program is None:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    91
            program = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    92
        self.lineOpHandlers = [] # for handling underlining and hyperlinking, etc
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    93
        self.program = program
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    94
        self.indent = self.rightIndent = 0.0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    95
        self.baseindent = 0.0 # adjust this to add more indentation for bullets, eg
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    96
        self.fontName = "Helvetica"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    97
        self.fontSize = 10
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    98
        self.leading = 12
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
    99
        self.fontColor = black
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   100
        self.x = self.y = self.rise = 0.0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   101
        from reportlab.lib.enums import TA_LEFT
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   102
        self.alignment = TA_LEFT
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   103
        self.textStateStack = []
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   104
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   105
    TEXT_STATE_VARIABLES = ("indent", "rightIndent", "fontName", "fontSize",
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   106
                            "leading", "fontColor", "lineOpHandlers", "rise",
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   107
                            "alignment")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   108
                            #"textStateStack")
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   109
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   110
    def pushTextState(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   111
        state = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   112
        for var in self.TEXT_STATE_VARIABLES:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   113
            val = getattr(self, var)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   114
            state.append(val)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   115
        #self.textStateStack.append(state)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   116
        self.textStateStack = self.textStateStack+[state] # fresh copy
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   117
        #print "push", self.textStateStack
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   118
        #print "push", len(self.textStateStack), state
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   119
        return state
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   120
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   121
    def popTextState(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   122
        state = self.textStateStack[-1]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   123
        self.textStateStack = self.textStateStack[:-1]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   124
        #print "pop", self.textStateStack
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   125
        state = state[:] # copy for destruction
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   126
        #print "pop", len(self.textStateStack), state
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   127
        #print "handlers before", self.lineOpHandlers
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   128
        for var in self.TEXT_STATE_VARIABLES:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   129
            val = state[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   130
            del state[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   131
            setattr(self, var, val)
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   132
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   133
    def format(self, maxwidth, maxheight, program, leading=0):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   134
        "return program with line operations added if at least one line fits"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   135
        # note: a generated formatted segment should not be formatted again
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   136
        startstate = self.__dict__.copy()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   137
        #remainder = self.cleanProgram(program)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   138
        remainder = program[:]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   139
        #program1 = remainder[:] # debug only
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   140
        lineprogram = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   141
        #if maxheight<TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   142
        #    raise ValueError, "attempt to format inside too small a height! "+repr(maxheight)
1983
19ee168f5aac Added Dirk Holtwick's patch and better entities/charrefs
rgbecker
parents: 1939
diff changeset
   143
        heightremaining = maxheight
19ee168f5aac Added Dirk Holtwick's patch and better entities/charrefs
rgbecker
parents: 1939
diff changeset
   144
        if leading: self.leading = leading
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   145
        room = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   146
        cursorcount = 0 # debug
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   147
        while remainder and room: #heightremaining>=self.leading and remainder:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   148
            #print "getting line with statestack", len(self.textStateStack)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   149
            #heightremaining = heightremaining - self.leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   150
            indent = self.indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   151
            rightIndent = self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   152
            linewidth = maxwidth - indent - rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   153
            beforelinestate = self.__dict__.copy()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   154
            if linewidth<TOOSMALLSPACE:
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   155
                raise ValueError, "indents %s %s too wide for space %s" % (self.indent, self.rightIndent, \
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   156
                                                                           maxwidth)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   157
            try:
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   158
                (lineIsFull, line, cursor, currentLength, \
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   159
                 usedIndent, maxLength, justStrings) = self.fitLine(remainder, maxwidth)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   160
            except:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   161
##                print "failed to fit line near", cursorcount # debug
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   162
##                for i in program1[max(0,cursorcount-10): cursorcount]:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   163
##                    print
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   164
##                    print i,
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   165
##                print "***" *8
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   166
##                for i in program1[cursorcount:cursorcount+20]:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   167
##                    print i
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   168
                raise
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   169
            cursorcount = cursorcount+cursor # debug
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   170
            leading = self.leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   171
            if heightremaining>leading:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   172
                heightremaining = heightremaining-leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   173
            else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   174
                room = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   175
                #self.resetState(beforelinestate)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   176
                self.__dict__.update(beforelinestate)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   177
                break # no room for this line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   178
##            if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   179
##                print "line", line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   180
##                if lineIsFull: print "is full"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   181
##                else: print "is partially full"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   182
##                print "consumes", cursor, "elements"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   183
##                print "covers", currentLength, "of", maxwidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   184
            alignment = self.alignment # last declared alignment for this line used
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   185
            # recompute linewidth using the used indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   186
            #linewidth = maxwidth - usedIndent - rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   187
            remainder = remainder[cursor:]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   188
            if not remainder:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   189
                # trim off the extra end of line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   190
                del line[-1]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   191
            # do justification if any
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   192
            #line = self.shrinkWrap(line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   193
            if alignment==TA_LEFT:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   194
                #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   195
                #    print "ALIGN LEFT"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   196
                if justStrings:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   197
                    line = stringLine(line, currentLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   198
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   199
                    line = self.shrinkWrap(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   200
                pass
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   201
            elif alignment==TA_CENTER:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   202
                #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   203
                #    print "ALIGN CENTER"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   204
                if justStrings:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   205
                    line = stringLine(line, currentLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   206
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   207
                    line = self.shrinkWrap(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   208
                line = self.centerAlign(line, currentLength, maxLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   209
            elif alignment==TA_RIGHT:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   210
                #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   211
                #    print "ALIGN RIGHT"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   212
                if justStrings:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   213
                    line = stringLine(line, currentLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   214
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   215
                    line = self.shrinkWrap(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   216
                line = self.rightAlign(line, currentLength, maxLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   217
            elif alignment==TA_JUSTIFY:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   218
                #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   219
                #    print "JUSTIFY"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   220
                if remainder and lineIsFull:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   221
                    if justStrings:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   222
                        line = simpleJustifyAlign(line, currentLength, maxLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   223
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   224
                        line = self.justifyAlign(line, currentLength, maxLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   225
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   226
                    if justStrings:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   227
                        line = stringLine(line, currentLength)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   228
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   229
                        line = self.shrinkWrap(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   230
                    if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   231
                        print "no justify because line is not full or end of para"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   232
            else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   233
                raise ValueError, "bad alignment "+repr(alignment)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   234
            if not justStrings:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   235
                line = self.cleanProgram(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   236
            lineprogram.extend(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   237
        laststate = self.__dict__.copy()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   238
        #self.resetState(startstate)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   239
        self.__dict__.update(startstate)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   240
        heightused = maxheight - heightremaining
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   241
        return (lineprogram, remainder, laststate, heightused)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   242
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   243
    def getState(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   244
        # inlined
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   245
        return self.__dict__.copy()
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   246
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   247
    def resetState(self, state):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   248
        # primarily inlined
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   249
        self.__dict__.update(state)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   250
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   251
##    def sizeOfWord(self, word):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   252
##        inlineThisFunctionForEfficiency
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   253
##        return float(stringWidth(word, self.fontName, self.fontSize))
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   254
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   255
    def fitLine(self, program, totalLength):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   256
        "fit words (and other things) onto a line"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   257
        # assuming word lengths and spaces have not been yet added
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   258
        # fit words onto a line up to maxlength, adding spaces and respecting extra space
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   259
        from reportlab.pdfbase.pdfmetrics import stringWidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   260
        usedIndent = self.indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   261
        maxLength = totalLength - usedIndent - self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   262
        done = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   263
        line = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   264
        cursor = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   265
        lineIsFull = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   266
        currentLength = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   267
        maxcursor = len(program)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   268
        needspace = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   269
        first = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   270
        terminated = None
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   271
        fontName = self.fontName
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   272
        fontSize = self.fontSize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   273
        spacewidth = stringWidth(" ", fontName, fontSize) #self.sizeOfWord(" ")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   274
        justStrings = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   275
        while not done and cursor<maxcursor:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   276
            opcode = program[cursor]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   277
            #if debug: print "opcode", cursor, opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   278
            topcode = type(opcode)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   279
            if topcode in (StringType, UnicodeType, InstanceType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   280
                lastneedspace = needspace
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   281
                needspace = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   282
                if topcode is InstanceType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   283
                    justStrings = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   284
                    width = opcode.width(self)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   285
                    needspace = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   286
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   287
                    saveopcode = opcode
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   288
                    opcode = opcode.strip()
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   289
                    if opcode:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   290
                        width = stringWidth(opcode, fontName, fontSize)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   291
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   292
                        width = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   293
                    if saveopcode and (width or currentLength):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   294
                        # ignore white space at margin
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   295
                        needspace = (saveopcode[-1]==" ")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   296
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   297
                        needspace = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   298
                fullwidth = width
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   299
                if lastneedspace:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   300
                    #spacewidth = stringWidth(" ", fontName, fontSize) #self.sizeOfWord(" ")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   301
                    fullwidth = width + spacewidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   302
                newlength = currentLength+fullwidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   303
                if newlength>maxLength and not first: # always do at least one thing
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   304
                    # this word won't fit
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   305
                    #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   306
                    #    print "WORD", opcode, "wont fit, width", width, "fullwidth", fullwidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   307
                    #    print "   currentLength", currentLength, "newlength", newlength, "maxLength", maxLength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   308
                    done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   309
                    lineIsFull = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   310
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   311
                    # fit the word: add a space then the word
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   312
                    if lastneedspace:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   313
                        line.append( spacewidth ) # expandable space: positive
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   314
                    if opcode:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   315
                        line.append( opcode )
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   316
                    if abs(width)>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   317
                        line.append( -width ) # non expanding space: negative
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   318
                        currentLength = newlength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   319
                    #print line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   320
                    #stop
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   321
                first = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   322
            elif topcode is FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   323
                justStrings = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   324
                aopcode = abs(opcode) # negative means non expanding
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   325
                if aopcode>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   326
                    nextLength = currentLength+aopcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   327
                    if nextLength>maxLength and not first: # always do at least one thing
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   328
                        #if debug: print "EXPLICIT spacer won't fit", maxLength, nextLength, opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   329
                        done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   330
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   331
                        if aopcode>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   332
                            currentLength = nextLength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   333
                            line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   334
                    first = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   335
            elif topcode is  TupleType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   336
                justStrings = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   337
                indicator = opcode[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   338
                #line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   339
                if indicator=="nextLine":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   340
                    # advance to nextLine
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   341
                    #(i, endallmarks) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   342
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   343
                    cursor = cursor+1 # consume this element
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   344
                    terminated = done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   345
                    #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   346
                    #    print "nextLine encountered"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   347
                elif indicator=="color":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   348
                    # change fill color
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   349
                    oldcolor = self.fontColor
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   350
                    (i, colorname) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   351
                    #print "opcode", opcode
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   352
                    if type(colorname) in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   353
                        color = self.fontColor = getattr(colors, colorname)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   354
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   355
                        color = self.fontColor = colorname # assume its something sensible :)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   356
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   357
                elif indicator=="face":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   358
                    # change font face
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   359
                    (i, fontname) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   360
                    fontName = self.fontName = fontname
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   361
                    spacewidth = stringWidth(" ", fontName, fontSize) #self.sizeOfWord(" ")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   362
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   363
                elif indicator=="size":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   364
                    # change font size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   365
                    (i, fontsize) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   366
                    size = abs(float(fontsize))
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   367
                    if type(fontsize) in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   368
                        if fontsize[:1]=="+":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   369
                            fontSize = self.fontSize = self.fontSize + size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   370
                        elif fontsize[:1]=="-":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   371
                            fontSize = self.fontSize = self.fontSize - size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   372
                        else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   373
                            fontSize = self.fontSize = size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   374
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   375
                        fontSize = self.fontSize = size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   376
                    spacewidth = stringWidth(" ", fontName, fontSize) #self.sizeOfWord(" ")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   377
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   378
                elif indicator=="leading":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   379
                    # change font leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   380
                    (i, leading) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   381
                    self.leading = leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   382
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   383
                elif indicator=="indent":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   384
                    # increase the indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   385
                    (i, increment) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   386
                    indent = self.indent = self.indent + increment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   387
                    if first:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   388
                        usedIndent = max(indent, usedIndent)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   389
                        maxLength = totalLength - usedIndent - self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   390
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   391
                elif indicator=="push":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   392
                    self.pushTextState()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   393
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   394
                elif indicator=="pop":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   395
                    try:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   396
                        self.popTextState()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   397
                    except:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   398
##                        print "stack fault near", cursor
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   399
##                        for i in program[max(0, cursor-10):cursor+10]:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   400
##                            if i==cursor:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   401
##                                print "***>>>",
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   402
##                            print i
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   403
                        raise
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   404
                    fontName = self.fontName
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   405
                    fontSize = self.fontSize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   406
                    spacewidth = stringWidth(" ", fontName, fontSize) #self.sizeOfWord(" ")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   407
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   408
                elif indicator=="bullet":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   409
                    (i, bullet, indent, font, size) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   410
                    # adjust for base indent (only at format time -- only execute once)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   411
                    indent = indent + self.baseindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   412
                    opcode = (i, bullet, indent, font, size)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   413
                    if not first:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   414
                        raise ValueError, "bullet not at beginning of line"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   415
                    bulletwidth = float(stringWidth(bullet, font, size))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   416
                    spacewidth = float(stringWidth(" ", font, size))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   417
                    bulletmin = indent+spacewidth+bulletwidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   418
                    # decrease the line size to allow bullet
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   419
                    usedIndent = max(bulletmin, usedIndent)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   420
                    if first:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   421
                        maxLength = totalLength - usedIndent - self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   422
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   423
                elif indicator=="rightIndent":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   424
                    # increase the right indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   425
                    (i, increment) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   426
                    self.rightIndent = self.rightIndent+increment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   427
                    if first:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   428
                        maxLength = totalLength - usedIndent - self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   429
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   430
                elif indicator=="rise":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   431
                    (i, rise) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   432
                    newrise = self.rise = self.rise+rise
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   433
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   434
                elif indicator=="align":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   435
                    (i, alignment) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   436
                    #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   437
                    #    print "SETTING ALIGNMENT", alignment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   438
                    self.alignment = alignment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   439
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   440
                elif indicator=="lineOperation":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   441
                    (i, handler) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   442
                    line.append(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   443
                    self.lineOpHandlers = self.lineOpHandlers + [handler] # fresh copy
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   444
                elif indicator=="endLineOperation":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   445
                    (i, handler) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   446
                    h = self.lineOpHandlers[:] # fresh copy
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   447
                    h.remove(handler)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   448
                    self.lineOpHandlers = h
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   449
                    line.append(opcode)
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   450
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   451
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   452
                    raise ValueError, "at format time don't understand indicator "+repr(indicator)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   453
            else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   454
                raise ValueError, "op must be string, float, instance, or tuple "+repr(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   455
            if not done:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   456
                cursor = cursor+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   457
                #first = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   458
##            if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   459
##                if done:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   460
##                    print "DONE FLAG IS SET"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   461
##                if cursor>=maxcursor:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   462
##                    print "AT END OF PROGRAM"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   463
        if not terminated:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   464
            line.append( ("nextLine", 0) )
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   465
        #print "fitline", line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   466
        return (lineIsFull, line, cursor, currentLength, usedIndent, maxLength, justStrings)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   467
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   468
    def centerAlign(self, line, lineLength, maxLength):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   469
        diff = maxLength-lineLength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   470
        shift = diff/2.0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   471
        if shift>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   472
            return self.insertShift(line, shift)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   473
        return line
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   474
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   475
    def rightAlign(self, line, lineLength, maxLength):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   476
        shift = maxLength-lineLength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   477
        #die
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   478
        if shift>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   479
            return self.insertShift(line, shift)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   480
        return line
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   481
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   482
    def insertShift(self, line, shift):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   483
        # insert shift just before first visible element in line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   484
        result = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   485
        first = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   486
        for e in line:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   487
            te = type(e)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   488
            if first and (te in (StringType, UnicodeType, InstanceType)):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   489
                result.append(shift)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   490
                first = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   491
            result.append(e)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   492
        return result
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   493
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   494
    def justifyAlign(self, line, lineLength, maxLength):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   495
        diff = maxLength-lineLength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   496
        # count EXPANDABLE SPACES AFTER THE FIRST VISIBLE
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   497
        spacecount = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   498
        visible = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   499
        for e in line:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   500
            te = type(e)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   501
            if te is FloatType and e>TOOSMALLSPACE and visible:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   502
                spacecount = spacecount+1
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   503
            elif te in (StringType, UnicodeType, InstanceType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   504
                visible = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   505
        #if debug: print "diff is", diff, "wordcount", wordcount #; die
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   506
        if spacecount<1:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   507
            return line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   508
        shift = diff/float(spacecount)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   509
        if shift<=TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   510
            #if debug: print "shift too small", shift
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   511
            return line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   512
        first = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   513
        visible = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   514
        result = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   515
        cursor = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   516
        nline = len(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   517
        while cursor<nline:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   518
            e = line[cursor]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   519
            te = type(e)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   520
            result.append(e)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   521
            if (te in (StringType, UnicodeType, InstanceType)):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   522
                visible = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   523
            elif te is FloatType and e>TOOSMALLSPACE and visible:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   524
                expanded = e+shift
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   525
                result[-1] = expanded
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   526
            cursor = cursor+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   527
        return result
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   528
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   529
##                if not first:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   530
##                    #if debug: print "shifting", shift, e
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   531
##                    #result.append(shift)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   532
##                    # add the shift in result before any start markers before e
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   533
##                    insertplace = len(result)-1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   534
##                    done = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   535
##                    myshift = shift
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   536
##                    while insertplace>0 and not done:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   537
##                        beforeplace = insertplace-1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   538
##                        beforething = result[beforeplace]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   539
##                        thingtype = type(beforething)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   540
##                        if thingtype is TupleType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   541
##                            indicator = beforething[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   542
##                            if indicator=="endLineOperation":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   543
##                                done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   544
##                            elif debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   545
##                                print "adding shift before", beforething
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   546
##                        elif thingtype is FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   547
##                            myshift = myshift + beforething
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   548
##                            del result[beforeplace]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   549
##                        else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   550
##                            done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   551
##                        if not done:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   552
##                            insertplace = beforeplace
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   553
##                    result.insert(insertplace, myshift)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   554
##                first = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   555
##            cursor = cursor+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   556
##        return result
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   557
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   558
    def shrinkWrap(self, line):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   559
        # for non justified text, collapse adjacent text/shift's into single operations
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   560
        result = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   561
        index = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   562
        maxindex = len(line)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   563
        while index<maxindex:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   564
            e = line[index]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   565
            te = type(e)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   566
            if te in (StringType, UnicodeType) and index<maxindex-1:
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   567
                # collect strings and floats
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   568
                thestrings = [e]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   569
                thefloats = 0.0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   570
                index = index+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   571
                nexte = line[index]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   572
                tnexte = type(nexte)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   573
                while index<maxindex and (tnexte in (FloatType, StringType, UnicodeType)):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   574
                    # switch to expandable space if appropriate
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   575
                    if tnexte is FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   576
                        if thefloats<0 and nexte>0:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   577
                            thefloats = -thefloats
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   578
                        if nexte<0 and thefloats>0:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   579
                            nexte = -nexte
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   580
                        thefloats = thefloats + nexte
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   581
                    elif tnexte in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   582
                        thestrings.append(nexte)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   583
                    index = index+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   584
                    if index<maxindex:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   585
                        nexte = line[index]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   586
                        tnexte = type(nexte)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   587
                # wrap up the result
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   588
                s = ' '.join(thestrings)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   589
                result.append(s)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   590
                result.append(float(thefloats))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   591
                # back up for unhandled element
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   592
                index = index-1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   593
            else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   594
                result.append(e)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   595
            index = index+1
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   596
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   597
        return result
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   598
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   599
    def cleanProgram(self, line):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   600
        "collapse adjacent spacings"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   601
        #return line # for debugging
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   602
        result = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   603
        last = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   604
        for e in line:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   605
            if type(e) is FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   606
                # switch to expandable space if appropriate
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   607
                if last<0 and e>0:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   608
                    last = -last
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   609
                if e<0 and last>0:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   610
                    e = -e
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   611
                last = float(last)+e
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   612
            else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   613
                if abs(last)>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   614
                    result.append(last)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   615
                result.append(e)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   616
                last = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   617
        if last:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   618
            result.append(last)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   619
        # now go backwards and delete all floats occurring after all visible elements
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   620
##        count = len(result)-1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   621
##        done = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   622
##        while count>0 and not done:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   623
##            e = result[count]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   624
##            te = type(e)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   625
##            if te is StringType or te is InstanceType or te is TupleType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   626
##                done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   627
##            elif te is FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   628
##                del result[count]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   629
##            count = count-1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   630
        # move end operations left and start operations left up to visibles
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   631
        change = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   632
        rline = range(len(result)-1)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   633
        while change:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   634
            #print line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   635
            change = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   636
            for index in rline:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   637
                nextindex = index+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   638
                this = result[index]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   639
                next = result[nextindex]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   640
                doswap = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   641
                tthis = type(this)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   642
                tnext = type(next)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   643
                # don't swap visibles
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   644
                if tthis in (StringType, UnicodeType) or \
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   645
                   tnext in (StringType, UnicodeType) or \
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   646
                   this is InstanceType or tnext is InstanceType:
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   647
                    doswap = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   648
                # only swap two tuples if the second one is an end operation and the first is something else
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   649
                elif tthis is TupleType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   650
                    thisindicator = this[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   651
                    if tnext is TupleType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   652
                        nextindicator = next[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   653
                        doswap = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   654
                        if (nextindicator=="endLineOperation" and thisindicator!="endLineOperation"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   655
                            and thisindicator!="lineOperation"):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   656
                            doswap = 1 # swap nonend<>end
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   657
                    elif tnext==FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   658
                        if thisindicator=="lineOperation":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   659
                            doswap = 1 # begin <> space
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   660
                if doswap:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   661
                    #print "swap", line[index],line[nextindex]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   662
                    result[index] = next
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   663
                    result[nextindex] = this
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   664
                    change = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   665
        return result
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   666
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   667
    def runOpCodes(self, program, canvas, textobject):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   668
        "render the line(s)"
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   669
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   670
        escape = canvas._escape
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   671
        code = textobject._code
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   672
        startstate = self.__dict__.copy()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   673
        font = None
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   674
        size = None
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   675
        # be sure to set them before using them (done lazily below)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   676
        #textobject.setFont(self.fontName, self.fontSize)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   677
        textobject.setFillColor(self.fontColor)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   678
        xstart = self.x
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   679
        thislineindent = self.indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   680
        thislinerightIndent = self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   681
        indented = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   682
        for opcode in program:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   683
            topcode = type(opcode)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   684
            if topcode in (StringType, UnicodeType, InstanceType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   685
                if not indented:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   686
                    if abs(thislineindent)>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   687
                        #if debug: print "INDENTING", thislineindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   688
                        #textobject.moveCursor(thislineindent, 0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   689
                        code.append('%s Td' % fp_str(thislineindent, 0))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   690
                        self.x = self.x + thislineindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   691
                    for handler in self.lineOpHandlers:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   692
                        #handler.end_at(x, y, self, canvas, textobject) # finish, eg, underlining this line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   693
                        handler.start_at(self.x, self.y, self, canvas, textobject) # start underlining the next
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   694
                indented = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   695
                # lazily set font (don't do it again if not needed)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   696
                if font!=self.fontName or size!=self.fontSize:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   697
                    font = self.fontName
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   698
                    size = self.fontSize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   699
                    textobject.setFont(font, size)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   700
                if topcode in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   701
                    #textobject.textOut(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   702
                    text = escape(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   703
                    code.append('(%s) Tj' % text)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   704
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   705
                    # drawable thing
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   706
                    opcode.execute(self, textobject, canvas)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   707
            elif topcode is FloatType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   708
                # use abs value (ignore expandable marking)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   709
                opcode = abs(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   710
                if opcode>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   711
                    #textobject.moveCursor(opcode, 0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   712
                    code.append('%s Td' % fp_str(opcode, 0))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   713
                    self.x = self.x + opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   714
            elif topcode is TupleType:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   715
                indicator = opcode[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   716
                if indicator=="nextLine":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   717
                    # advance to nextLine
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   718
                    (i, endallmarks) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   719
                    x = self.x
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   720
                    y = self.y
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   721
                    newy = self.y = self.y-self.leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   722
                    newx = self.x = xstart
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   723
                    thislineindent = self.indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   724
                    thislinerightIndent = self.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   725
                    indented = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   726
                    for handler in self.lineOpHandlers:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   727
                        handler.end_at(x, y, self, canvas, textobject) # finish, eg, underlining this line
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   728
                        #handler.start_at(newx, newy, self, canvas, textobject)) # start underlining the next
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   729
                    textobject.setTextOrigin(newx, newy)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   730
                elif indicator=="color":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   731
                    # change fill color
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   732
                    oldcolor = self.fontColor
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   733
                    (i, colorname) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   734
                    #print "opcode", opcode
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   735
                    if type(colorname) in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   736
                        color = self.fontColor = getattr(colors, colorname)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   737
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   738
                        color = self.fontColor = colorname # assume its something sensible :)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   739
                    #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   740
                    #    print color.red, color.green, color.blue
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   741
                    #    print dir(color)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   742
                    #print "color is", color
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   743
                    #from reportlab.lib.colors import green
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   744
                    #if color is green: print "color is green"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   745
                    if color!=oldcolor:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   746
                        textobject.setFillColor(color)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   747
                elif indicator=="face":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   748
                    # change font face
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   749
                    (i, fontname) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   750
                    self.fontName = fontname
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   751
                    #textobject.setFont(self.fontName, self.fontSize)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   752
                elif indicator=="size":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   753
                    # change font size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   754
                    (i, fontsize) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   755
                    size = abs(float(fontsize))
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   756
                    if type(fontsize) in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   757
                        if fontsize[:1]=="+":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   758
                            fontSize = self.fontSize = self.fontSize + size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   759
                        elif fontsize[:1]=="-":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   760
                            fontSize = self.fontSize = self.fontSize - size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   761
                        else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   762
                            fontSize = self.fontSize = size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   763
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   764
                        fontSize = self.fontSize = size
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   765
                    #(i, fontsize) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   766
                    self.fontSize = fontSize
1983
19ee168f5aac Added Dirk Holtwick's patch and better entities/charrefs
rgbecker
parents: 1939
diff changeset
   767
                    textobject.setFont(self.fontName, self.fontSize)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   768
                elif indicator=="leading":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   769
                    # change font leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   770
                    (i, leading) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   771
                    self.leading = leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   772
                elif indicator=="indent":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   773
                    # increase the indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   774
                    (i, increment) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   775
                    indent = self.indent = self.indent + increment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   776
                    thislineindent = max(thislineindent, indent)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   777
                elif indicator=="push":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   778
                    self.pushTextState()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   779
                elif indicator=="pop":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   780
                    oldcolor = self.fontColor
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   781
                    oldfont = self.fontName
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   782
                    oldsize = self.fontSize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   783
                    self.popTextState()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   784
                    #if CAUSEERROR or oldfont!=self.fontName or oldsize!=self.fontSize:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   785
                    #    textobject.setFont(self.fontName, self.fontSize)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   786
                    if oldcolor!=self.fontColor:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   787
                        textobject.setFillColor(self.fontColor)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   788
                elif indicator=="wordSpacing":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   789
                    (i, ws) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   790
                    textobject.setWordSpace(ws)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   791
                elif indicator=="bullet":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   792
                    (i, bullet, indent, font, size) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   793
                    if abs(self.x-xstart)>TOOSMALLSPACE:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   794
                        raise ValueError, "bullet not at beginning of line"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   795
                    bulletwidth = float(stringWidth(bullet, font, size))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   796
                    spacewidth = float(stringWidth(" ", font, size))
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   797
                    bulletmin = indent+spacewidth+bulletwidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   798
                    # decrease the line size to allow bullet as needed
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   799
                    if bulletmin > thislineindent:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   800
                        #if debug: print "BULLET IS BIG", bullet, bulletmin, thislineindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   801
                        thislineindent = bulletmin
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   802
                    textobject.moveCursor(indent, 0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   803
                    textobject.setFont(font, size)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   804
                    textobject.textOut(bullet)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   805
                    textobject.moveCursor(-indent, 0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   806
                    #textobject.textOut("M")
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   807
                    textobject.setFont(self.fontName, self.fontSize)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   808
                elif indicator=="rightIndent":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   809
                    # increase the right indent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   810
                    (i, increment) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   811
                    self.rightIndent = self.rightIndent+increment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   812
                elif indicator=="rise":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   813
                    (i, rise) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   814
                    newrise = self.rise = self.rise+rise
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   815
                    textobject.setRise(newrise)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   816
                elif indicator=="align":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   817
                    (i, alignment) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   818
                    self.alignment = alignment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   819
                elif indicator=="lineOperation":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   820
                    (i, handler) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   821
                    handler.start_at(self.x, self.y, self, canvas, textobject)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   822
                    #self.lineOpHandlers.append(handler)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   823
                    #if debug: print "adding", handler, self.lineOpHandlers
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   824
                    self.lineOpHandlers = self.lineOpHandlers + [handler] # fresh copy!
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   825
                elif indicator=="endLineOperation":
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   826
                    (i, handler) = opcode
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   827
                    handler.end_at(self.x, self.y, self, canvas, textobject)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   828
                    newh = self.lineOpHandlers = self.lineOpHandlers[:] # fresh copy
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   829
                    #if debug: print "removing", handler, self.lineOpHandlers
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   830
                    if handler in newh:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   831
                        self.lineOpHandlers.remove(handler)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   832
                    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   833
                        pass
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   834
                        #print "WARNING: HANDLER", handler, "NOT IN", newh
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   835
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   836
                    raise ValueError, "don't understand indicator "+repr(indicator)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   837
            else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   838
                raise ValueError, "op must be string float or tuple "+repr(opcode)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   839
        laststate = self.__dict__.copy()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   840
        #self.resetState(startstate)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   841
        self.__dict__.update(startstate)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   842
        return laststate
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   843
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   844
def stringLine(line, length):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   845
    "simple case: line with just strings and spacings which can be ignored"
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   846
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   847
    strings = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   848
    for x in line:
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   849
        if type(x) in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   850
            strings.append(x)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   851
    text = ' '.join(strings)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   852
    result = [text, float(length)]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   853
    nextlinemark = ("nextLine", 0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   854
    if line and line[-1]==nextlinemark:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   855
        result.append( nextlinemark )
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   856
    return result
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   857
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   858
def simpleJustifyAlign(line, currentLength, maxLength):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   859
    "simple justification with only strings"
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   860
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   861
    strings = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   862
    for x in line[:-1]:
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   863
        if type(x) in (StringType, UnicodeType):
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   864
            strings.append(x)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   865
    nspaces = len(strings)-1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   866
    slack = maxLength-currentLength
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   867
    text = ' '.join(strings)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   868
    if nspaces>0 and slack>0:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   869
        wordspacing = slack/float(nspaces)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   870
        result = [("wordSpacing", wordspacing), text, maxLength, ("wordSpacing", 0)]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   871
    else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   872
        result = [text, currentLength, ("nextLine", 0)]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   873
    nextlinemark = ("nextLine", 0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   874
    if line and line[-1]==nextlinemark:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   875
        result.append( nextlinemark )
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   876
    return result
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   877
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   878
from reportlab.lib.colors import black
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   879
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   880
def readBool(text):
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   881
    if text.upper() in ("Y", "YES", "TRUE", "1"):
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   882
        return 1
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   883
    elif text.upper() in ("N", "NO", "FALSE", "0"):
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   884
        return 0
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   885
    else:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   886
        raise RMLError, "true/false attribute has illegal value '%s'" % text
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   887
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   888
def readAlignment(text):
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   889
    up = text.upper()
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   890
    if up == 'LEFT':
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   891
        return TA_LEFT
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   892
    elif up == 'RIGHT':
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   893
        return TA_RIGHT
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   894
    elif up in ['CENTER', 'CENTRE']:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   895
        return TA_CENTER
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   896
    elif up == 'JUSTIFY':
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   897
        return TA_JUSTIFY
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   898
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   899
def readLength(text):
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   900
    """Read a dimension measurement: accept "3in", "5cm",
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   901
    "72 pt" and so on."""
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   902
    text = text.strip()
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   903
    try:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   904
        return float(text)
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   905
    except ValueError:
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   906
        text = text.lower()
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   907
        numberText, units = text[:-2],text[-2:]
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   908
        numberText = numberText.strip()
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   909
        try:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   910
            number = float(numberText)
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   911
        except ValueError:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   912
            raise ValueError, "invalid length attribute '%s'" % text
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   913
        try:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   914
            multiplier = {
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   915
                'in':72,
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   916
                'cm':28.3464566929,  #72/2.54; is this accurate?
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   917
                'mm':2.83464566929,
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   918
                'pt':1
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   919
                }[units]
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   920
        except KeyError:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   921
            raise RMLError, "invalid length attribute '%s'" % text
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   922
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   923
        return number * multiplier
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   924
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   925
def lengthSequence(s, converter=readLength):
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   926
    """from "(2, 1)" or "2,1" return [2,1], for example"""
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   927
    s = s.strip()
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   928
    if s[:1]=="(" and s[-1:]==")":
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   929
        s = s[1:-1]
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   930
    sl = s.split(',')
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   931
    sl = [s.strip() for s in sl]
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   932
    sl = [converter(s) for s in sl]
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   933
    return sl
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   934
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   935
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   936
def readColor(text):
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   937
    """Read color names or tuples, RGB or CMYK, and return a Color object."""
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   938
    if not text:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   939
        return None
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   940
    from reportlab.lib import colors
1986
6446b2bf5b94 Fixes to remove 'import as'
rgbecker
parents: 1983
diff changeset
   941
    from string import letters
6446b2bf5b94 Fixes to remove 'import as'
rgbecker
parents: 1983
diff changeset
   942
    if text[0] in letters:
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   943
        return colors.__dict__[text]
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   944
    tup = lengthSequence(text)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   945
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   946
    msg = "Color tuple must have 3 (or 4) elements for RGB (or CMYC)."
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   947
    assert 3 <= len(tup) <= 4, msg
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   948
    msg = "Color tuple must have all elements <= 1.0."
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   949
    for i in range(len(tup)):
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   950
        assert tup[i] <= 1.0, msg
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   951
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   952
    if len(tup) == 3:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   953
        colClass = colors.Color
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   954
    elif len(tup) == 4:
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   955
        colClass = colors.CMYKColor
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   956
    return apply(colClass, tup)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   957
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   958
class StyleAttributeConverters:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   959
    fontSize=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   960
    leading=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   961
    leftIndent=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   962
    rightIndent=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   963
    firstLineIndent=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   964
    alignment=[readAlignment]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   965
    spaceBefore=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   966
    spaceAfter=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   967
    bulletFontSize=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   968
    bulletIndent=[readLength]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   969
    textColor=[readColor]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   970
    backColor=[readColor]
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
   971
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   972
class SimpleStyle:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   973
    "simplified paragraph style without all the fancy stuff"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   974
    name = "basic"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   975
    fontName='Times-Roman'
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   976
    fontSize=10
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   977
    leading=12
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   978
    leftIndent=0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   979
    rightIndent=0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   980
    firstLineIndent=0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   981
    alignment=TA_LEFT
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   982
    spaceBefore=0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   983
    spaceAfter=0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   984
    bulletFontName='Times-Roman'
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   985
    bulletFontSize=10
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   986
    bulletIndent=0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   987
    textColor=black
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   988
    backColor=None
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   989
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   990
    def __init__(self, name, parent=None, **kw):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   991
        mydict = self.__dict__
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   992
        if parent:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   993
            for (a,b) in parent.__dict__.items():
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   994
                mydict[a]=b
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   995
        for (a,b) in kw.items():
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   996
            mydict[a] =  b
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
   997
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   998
    def addAttributes(self, dictionary):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
   999
        for key in dictionary.keys():
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1000
            value = dictionary[key]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1001
            if value is not None:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1002
                if hasattr(StyleAttributeConverters, key):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1003
                    converter = getattr(StyleAttributeConverters, key)[0]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1004
                    value = converter(value)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1005
                setattr(self, key, value)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1006
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1007
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1008
DEFAULT_ALIASES = {
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1009
    "h1.defaultStyle": "Heading1",
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1010
    "h2.defaultStyle": "Heading2",
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1011
    "h3.defaultStyle": "Heading3",
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1012
    "h4.defaultStyle": "Heading4",
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1013
    "h5.defaultStyle": "Heading5",
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1014
    "h6.defaultStyle": "Heading6",
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1015
    "title.defaultStyle": "Title",
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1016
    "subtitle.defaultStyle": "SubTitle",
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1017
    "para.defaultStyle": "Normal",
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1018
    "pre.defaultStyle": "Code",
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1019
    "ul.defaultStyle": "UnorderedList",
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1020
    "ol.defaultStyle": "OrderedList",
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1021
    "li.defaultStyle": "Definition",
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1022
    }
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1023
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1024
class FastPara(Flowable):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1025
    "paragraph with no special features (not even a single ampersand!)"
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1026
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1027
    def __init__(self, style, simpletext):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1028
        #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1029
        #    print "FAST", id(self)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1030
        if "&" in simpletext:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1031
            raise ValueError, "no ampersands please!"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1032
        self.style = style
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1033
        self.simpletext = simpletext
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1034
        self.lines = None
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1035
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1036
    def wrap(self, availableWidth, availableHeight):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1037
        simpletext = self.simpletext
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1038
        self.availableWidth = availableWidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1039
        style = self.style
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1040
        text = self.simpletext
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1041
        rightIndent = style.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1042
        leftIndent = style.leftIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1043
        leading = style.leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1044
        font = style.fontName
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1045
        size = style.fontSize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1046
        firstindent = style.firstLineIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1047
        #textcolor = style.textColor
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1048
        words = simpletext.split()
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1049
        lines = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1050
        from reportlab.pdfbase.pdfmetrics import stringWidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1051
        spacewidth = stringWidth(" ", font, size)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1052
        currentline = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1053
        currentlength = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1054
        firstmaxlength = availableWidth - rightIndent - firstindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1055
        maxlength = availableWidth - rightIndent - leftIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1056
        if maxlength<spacewidth:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1057
            return (spacewidth+rightIndent+firstindent, availableHeight) # need something wider than this!
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1058
        if availableHeight<leading:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1059
            return (availableWidth, leading) # need something longer
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1060
        if self.lines is None:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1061
            heightused = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1062
            cursor = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1063
            nwords = len(words)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1064
            done = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1065
            #heightused = leading # ???
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1066
            while cursor<nwords and not done:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1067
                thismaxlength = maxlength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1068
                if not lines:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1069
                    thismaxlength = firstmaxlength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1070
                thisword = words[cursor]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1071
                thiswordsize = stringWidth(thisword, font, size)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1072
                if currentlength:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1073
                    thiswordsize = thiswordsize+spacewidth
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1074
                nextlength = currentlength + thiswordsize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1075
                if not currentlength or nextlength<maxlength:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1076
                    # add the word
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1077
                    cursor = cursor+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1078
                    currentlength = nextlength
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1079
                    currentline.append(thisword)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1080
                    #print "currentline", currentline
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1081
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1082
                    # emit the line
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1083
                    lines.append( (' '.join(currentline), currentlength, len(currentline)) )
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1084
                    currentline = []
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1085
                    currentlength = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1086
                    heightused = heightused+leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1087
                    if heightused+leading>availableHeight:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1088
                        done = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1089
            if currentlength and not done:
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1090
                lines.append( (' '.join(currentline), currentlength, len(currentline) ))
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1091
                heightused = heightused+leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1092
            self.lines = lines
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1093
            self.height = heightused
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1094
            remainder = self.remainder = ' '.join(words[cursor:])
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1095
            #print "lines", lines
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1096
            #print "remainder is", remainder
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1097
        else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1098
            remainder = None
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1099
            heightused = self.height
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1100
            lines = self.lines
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1101
        if remainder:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1102
            result = (availableWidth, availableHeight+leading) # need to split
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1103
        else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1104
            result = (availableWidth, heightused)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1105
        #if debug: print "wrap is", (availableWidth, availableHeight), result, len(lines)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1106
        return result
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1107
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1108
    def split(self, availableWidth, availableHeight):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1109
        style = self.style
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1110
        leading = style.leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1111
        if availableHeight<leading:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1112
            return [] # not enough space for split
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1113
        lines = self.lines
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1114
        if lines is None:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1115
            raise ValueError, "must wrap before split"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1116
        remainder = self.remainder
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1117
        if remainder:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1118
            next = FastPara(style, remainder)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1119
            return [self,next]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1120
        else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1121
            return [self]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1122
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1123
    def draw(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1124
        style = self.style
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1125
        lines = self.lines
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1126
        rightIndent = style.rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1127
        leftIndent = style.leftIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1128
        leading = style.leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1129
        font = style.fontName
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1130
        size = style.fontSize
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1131
        alignment = style.alignment
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1132
        firstindent = style.firstLineIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1133
        c = self.canv
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1134
        escape = c._escape
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1135
        #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1136
        #    print "FAST", id(self), "page number", c.getPageNumber()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1137
        height = self.height
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1138
        #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1139
        #    c.rect(0,0,-1, height-size, fill=1, stroke=1)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1140
        c.translate(0, height-size)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1141
        textobject = c.beginText()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1142
        code = textobject._code
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1143
        #textobject.setTextOrigin(0,firstindent)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1144
        textobject.setFont(font, size)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1145
        if style.textColor:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1146
            textobject.setFillColor(style.textColor)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1147
        first = 1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1148
        y = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1149
        basicWidth = self.availableWidth - rightIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1150
        count = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1151
        nlines = len(lines)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1152
        while count<nlines:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1153
            (text, length, nwords) = lines[count]
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1154
            count = count+1
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1155
            thisindent = leftIndent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1156
            if first:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1157
                thisindent = firstindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1158
            if alignment==TA_LEFT:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1159
                x = thisindent
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1160
            elif alignment==TA_CENTER:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1161
                extra = basicWidth - length
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1162
                x = thisindent + extra/2.0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1163
            elif alignment==TA_RIGHT:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1164
                extra = basicWidth - length
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1165
                x = thisindent + extra
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1166
            elif alignment==TA_JUSTIFY:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1167
                x = thisindent
1785
6ea4b533da38 eliminated divide by zero but just introduced
aaron_watters
parents: 1784
diff changeset
  1168
                if count<nlines and nwords>1:
1784
6781a2f69e2c Slight improvement on Doug's fix
rgbecker
parents: 1783
diff changeset
  1169
                    # patch from doug@pennatus.com, 9 Nov 2002, no extraspace on last line
6781a2f69e2c Slight improvement on Doug's fix
rgbecker
parents: 1783
diff changeset
  1170
                    textobject.setWordSpace((basicWidth-length)/(nwords-1.0))
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1171
                else:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1172
                    textobject.setWordSpace(0.0)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1173
            textobject.setTextOrigin(x,y)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1174
            text = escape(text)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1175
            code.append('(%s) Tj' % text)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1176
            #textobject.textOut(text)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1177
            y = y-leading
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1178
        c.drawText(textobject)
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1179
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1180
    def getSpaceBefore(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1181
        #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1182
        #    print "got space before", self.spaceBefore
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1183
        return self.style.spaceBefore
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1184
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1185
    def getSpaceAfter(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1186
        #print "got space after", self.spaceAfter
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1187
        return self.style.spaceAfter
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1188
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1189
def defaultContext():
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1190
    result = {}
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1191
    from reportlab.lib.styles import getSampleStyleSheet
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1192
    styles = getSampleStyleSheet()
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1193
    for (stylenamekey, stylenamevalue) in DEFAULT_ALIASES.items():
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1194
        result[stylenamekey] = styles[stylenamevalue]
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1195
    return result
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1725
diff changeset
  1196
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1197
def buildContext(stylesheet=None):
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1198
    result = {}
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1199
    from reportlab.lib.styles import getSampleStyleSheet
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1200
    if stylesheet is not None:
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1201
        # Copy styles with the same name as aliases
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1202
        for (stylenamekey, stylenamevalue) in DEFAULT_ALIASES.items():
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1203
            if stylesheet.has_key(stylenamekey):
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1204
                result[stylenamekey] = stylesheet[stylenamekey]
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1205
        # Then make aliases
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1206
        for (stylenamekey, stylenamevalue) in DEFAULT_ALIASES.items():
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1207
            if stylesheet.has_key(stylenamevalue):
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1208
                result[stylenamekey] = stylesheet[stylenamevalue]
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1209
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1210
    styles = getSampleStyleSheet()
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1211
    # Then, fill in defaults if they were not filled yet.
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1212
    for (stylenamekey, stylenamevalue) in DEFAULT_ALIASES.items():
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1213
        if not result.has_key(stylenamekey) and styles.has_key(stylenamevalue):
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1214
            result[stylenamekey] = styles[stylenamevalue]
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1215
    return result
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1216
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1217
class Para(Flowable):
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1218
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1219
    spaceBefore = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1220
    spaceAfter = 0
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1221
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1222
    def __init__(self, style, parsedText=None, bulletText=None, state=None, context=None, baseindent=0):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1223
        #print id(self), "para", parsedText
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1224
        self.baseindent = baseindent
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1225
        self.context = buildContext(context)
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1226
        self.parsedText = parsedText
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1227
        self.bulletText = bulletText
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1228
        self.style1 = style # make sure Flowable doesn't use this unless wanted! call it style1 NOT style
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1229
        #self.spaceBefore = self.spaceAfter = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1230
        self.program = [] # program before layout
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1231
        self.formattedProgram = [] # after layout
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1232
        self.remainder = None # follow on paragraph if any
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1233
        self.state = state # initial formatting state (for completions)
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1234
        if not state:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1235
            self.spaceBefore = style.spaceBefore
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1236
            self.spaceAfter = style.spaceAfter
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1237
            #self.spaceBefore = "invalid value"
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1238
        #if hasattr(self, "spaceBefore") and debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1239
        #    print "spaceBefore is", self.spaceBefore, self.parsedText
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1240
        self.bold = 0
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1241
        self.italic = 0
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1242
        self.face = style.fontName
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1243
        self.size = style.fontSize
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1244
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1245
    def getSpaceBefore(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1246
        #if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1247
        #    print "got space before", self.spaceBefore
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1248
        return self.spaceBefore
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1249
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1250
    def getSpaceAfter(self):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1251
        #print "got space after", self.spaceAfter
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1252
        return self.spaceAfter
1939
1f6f0eda3214 Added Sidnei da Silva's patches
rgbecker
parents: 1886
diff changeset
  1253
1725
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1254
    def wrap(self, availableWidth, availableHeight):
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1255
        if debug:
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1256
            print "WRAPPING", id(self), availableWidth, availableHeight
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1257
            print "   ", self.formattedProgram
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1258
            print "   ", self.program
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1259
        self.availableHeight = availableHeight
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1260
        self.myengine = p = paragraphEngine()
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1261
        p.baseindent = self.baseindent # for shifting bullets as needed
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1262
        parsedText = self.parsedText
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1263
        formattedProgram = self.formattedProgram
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents:
diff changeset
  1264
        state = self.state
1b772062537b new experimental paragraph implementation which supports hyperlinking, underlining, embedded lists etc
aaron_watters
parents: