File: multilinetext.py

package info (click to toggle)
skencil 0.6.17-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,136 kB
  • ctags: 8,589
  • sloc: python: 36,672; ansic: 16,571; sh: 151; makefile: 92
file content (280 lines) | stat: -rw-r--r-- 8,907 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


###Sketch Config
#type = PluginCompound
#class_name = 'MultilineText'
#menu_text = 'Multiline Text'
#standard_messages = 1
#custom_dialog = 'MultilineTextDlg'
###End

(''"Multiline Text")

import Tkinter
from Tkconstants import *
from Tkinter import StringVar, Entry, Label, Button, Frame, Text, END, \
    BOTH, LEFT, TOP, X, W, GROOVE, Frame, Label, StringVar, Radiobutton

import os
from string import split, rstrip
from types import StringType
from math import ceil

import Sketch.const
from Sketch import Scale, TrafoPlugin, SimpleText, Translation, Identity, \
    GetFont, StandardColors, SolidPattern, UndoAfter, _
from Sketch.UI.sketchdlg import SketchPanel

from Lib.multilinetext.TextEditor import TextEditor, FONTS, Style, \
     raw2styling, styling2raw
from Lib.multilinetext.chunker import Chunker



class MultilineText(TrafoPlugin):

    class_name = 'MultilineText'
    has_edit_mode       = 0
    is_Text             = 0
    is_SimpleText       = 0
    is_curve            = 0
    is_clip             = 0
    has_font            = 0
    has_fill            = 0
    has_line            = 0
    is_Group            = 1

    Ungroup = TrafoPlugin.GetObjects


    def __init__(self, text = '', styling = [],
                 trafo = None, loading = 0, duplicate = None):
        self.objects = []
        TrafoPlugin.__init__(self, trafo = trafo, duplicate = duplicate)

        if duplicate is not None:
            self.text = duplicate.text
            self.styling = duplicate.styling[:]
        else:
            self.text = text
            if loading: # XXX a bit ugly 
                raw = styling
                styling = raw2styling(raw)
            self.styling = styling
        if not loading:
            self.recompute()

    def recompute(self):
        chunker = Chunker(rstrip(self.text), self.styling, '\t')

        objects = []
        options = {}
        x = 0
        y = 0
        maxy = 0
        line = []
        while not chunker.eof():
            text, styles =  chunker.get()

            family = styles['family'].options
            color = styles['color'].options
            fill = SolidPattern(getattr(StandardColors, color))
            # XXX should be done in advance


            size = styles['size'].options
            bold = styles['bold'].options
            italic = styles['italic'].options

            if bold and italic:
                attr = ['Bold Italic', 'Bold Oblique', 'Demi Oblique', 'Demi Italic', 'Medium Italic', 'Demi Bold Italic']
            elif bold:
                attr = ['Bold', 'Demi', 'Medium', 'Demi Bold']
            elif italic:
                attr = ['Italic', 'Oblique', 'Book Oblique', 'Light Italic', 'Regular Oblique', 'Regular Italic']
            else:
                attr = ['Roman', 'Book', 'Light', 'Regular']

            supersub = styles['supersub'].options
            if len(text)>0:
                height = 1.2*size # XXX is that ok ?
                if maxy<height:
                    maxy = height

                if supersub != 'normal':
                    if supersub == 'superscript':
                        offset = size*0.5
                    else:
                        offset = -size*0.15
                    size = 0.5*size
                else:
                    offset = 0
                textObj = SimpleText(Translation(x,offset), text)
                objects.append(textObj)
                line.append(textObj)

                psfont = FONTS.ps(family=family, attr=attr)
                font = GetFont(psfont)

                textObj.SetProperties(font=font, font_size=size, 
                    fill_pattern=fill)

                left, bottom, right, top = textObj.coord_rect
                width = right-left

            else:
                width = 0

            reason = chunker.reason()
            if reason == '\n' or chunker.eof():
                if width > 0:
                    y = y-maxy
                else:
                    y = y-size
                maxy = 0
                x = 0
                for obj in line:
                    obj.Transform(self.trafo(Translation(0, y)))
                line = []
            elif reason == '\t':
                x = x+width+size
                #
                # XXX this is very provisional
                x = ceil(x/50.)*50.
            else:
                x = x+width

        # XXX this is a problem: what happens, if the MultilineText onbject
        #           is empty  ??
        #~ if len(objects)>0:
        self.set_objects(objects)

    def SetParameters(self, params):
        return TrafoPlugin.SetParameters(self, params)

    def Transform(self, trafo):
        return self.set_transformation(trafo(self.trafo))

    def Text(self):
        return self.text

    def Styling(self):
        return self.styling

    def SaveToFile(self, file):
        raw = styling2raw(self.styling)
        TrafoPlugin.SaveToFile(self, file, self.text, raw, self.trafo.coeff())

    def AsBezier(self):
        return self.objects[0].AsBezier()

    def Paths(self):
        return self.objects[0].Paths()


class MultilineTextDlg(SketchPanel):

    title = _("Multiline Text")
    class_name = "MultilineTextPanel"
    receivers = SketchPanel.receivers[:]

    def __init__(self, master, main_window, doc):
        SketchPanel.__init__(self, master, main_window, doc,
                             name = 'multilinetxtdlg')

    receivers.append((Sketch.const.SELECTION, 'Update'))
    def Update(self):
        """
        This method is used as a callback and
        is automatically called when the selection changes
        """
        # get selection
        selections = self.document.SelectedObjects()
        if len(selections) == 1:
            selected = selections[0]
            if selected.is_Plugin and selected.class_name == 'MultilineText':
                self.set_text(selected.text)
                self.set_styling(selected.styling)
                return
        self.set_text('')
        self.set_styling([])

    def init_from_doc(self):
        """
        Called whenever the document changes and from __init__
        """
        self.Update()

    def build_dlg(self):
        top = self.top
        self.text = ''

        text_field = TextEditor(top,
            background = 'white',
            width = 40,
            height = 20)
        text_field.pack(fill = BOTH, expand = 1)

        text_field.button_apply.configure(command=self.apply)
        text_field.warn = self.report_error
        self.text_field = text_field

    def report_error(self, title, message):
        app = self.main_window.application
        app.MessageBox(title = title,
                       message = message,
                       icon = 'warning')


    def get_text(self):
        tk = self.master.tk
        return tk.utf8_to_latin1(self.text_field.get('1.0', 'end')[:-1])

    def get_styling(self):
        return self.text_field.styling_get('1.0', 'end')

    def set_text(self, text):
        self.text_field.delete('1.0', 'end')
        self.text_field.insert('end', text)

    def set_styling(self, styling):
        if len(styling) == 0:
            self.text_field.style_removeall()
        else:
            self.text_field.styling_apply('1.0', styling)

    def apply(self):
        text = self.get_text()
        styling = self.get_styling()
        doc = self.document
        selections = doc.SelectedObjects()
        if len(selections) == 1:
            selected = selections[0]
            if selected.is_Plugin and selected.class_name =='MultilineText':
                doc.BeginTransaction(_("Set MultilineText Parameters"))
                try:
                    try:
                        params = {}
                        params["text"] = text
                        params["styling"] = styling
                        doc.AddUndo(selected.SetParameters(params))
                    except:
                        doc.AbortTransaction()
                finally:
                    doc.EndTransaction()
                return
        object = MultilineText(text, styling)
        self.main_window.PlaceObject(object)