summaryrefslogtreecommitdiffstats
path: root/DeDRM_plugin/stylexml2css.py
blob: 3721da42097da898ad1b82c354a1ef9c52789d90 (plain)
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
281
282
283
284
285
286
287
288
289
290
#! /usr/bin/python
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# For use with Topaz Scripts Version 2.6


import csv
import sys
import os
import getopt
import re
from struct import pack
from struct import unpack

debug = False

class DocParser(object):
    def __init__(self, flatxml, fontsize, ph, pw):
        self.flatdoc = flatxml.split(b'\n')
        self.fontsize = int(fontsize)
        self.ph = int(ph) * 1.0
        self.pw = int(pw) * 1.0

    stags = {
        b'paragraph' : 'p',
        b'graphic'   : '.graphic'
    }

    attr_val_map = {
        b'hang'            : 'text-indent: ',
        b'indent'          : 'text-indent: ',
        b'line-space'      : 'line-height: ',
        b'margin-bottom'   : 'margin-bottom: ',
        b'margin-left'     : 'margin-left: ',
        b'margin-right'    : 'margin-right: ',
        b'margin-top'      : 'margin-top: ',
        b'space-after'     : 'padding-bottom: ',
    }

    attr_str_map = {
        b'align-center' : 'text-align: center; margin-left: auto; margin-right: auto;',
        b'align-left'   : 'text-align: left;',
        b'align-right'  : 'text-align: right;',
        b'align-justify' : 'text-align: justify;',
        b'display-inline' : 'display: inline;',
        b'pos-left' : 'text-align: left;',
        b'pos-right' : 'text-align: right;',
        b'pos-center' : 'text-align: center; margin-left: auto; margin-right: auto;',
    }


    # find tag if within pos to end inclusive
    def findinDoc(self, tagpath, pos, end) :
        result = None
        docList = self.flatdoc
        cnt = len(docList)
        if end == -1 :
            end = cnt
        else:
            end = min(cnt,end)
        foundat = -1
        for j in range(pos, end):
            item = docList[j]
            if item.find(b'=') >= 0:
                (name, argres) = item.split(b'=',1)
            else :
                name = item
                argres = b''
            if (isinstance(tagpath,str)):
                tagpath = tagpath.encode('utf-8')
            if name.endswith(tagpath) :
                result = argres
                foundat = j
                break
        return foundat, result


    # return list of start positions for the tagpath
    def posinDoc(self, tagpath):
        startpos = []
        pos = 0
        res = b""
        while res != None :
            (foundpos, res) = self.findinDoc(tagpath, pos, -1)
            if res != None :
                startpos.append(foundpos)
            pos = foundpos + 1
        return startpos

    # returns a vector of integers for the tagpath
    def getData(self, tagpath, pos, end, clean=False):
        if clean:
            digits_only = re.compile(rb'''([0-9]+)''')
        argres=[]
        (foundat, argt) = self.findinDoc(tagpath, pos, end)
        if (argt != None) and (len(argt) > 0) :
            argList = argt.split(b'|')
            for strval in argList:
                if clean:
                    m = re.search(digits_only, strval)
                    if m != None:
                        strval = m.group()
                argres.append(int(strval))
        return argres

    def process(self):

        classlst = ''
        csspage = '.cl-center { text-align: center; margin-left: auto; margin-right: auto; }\n'
        csspage += '.cl-right { text-align: right; }\n'
        csspage += '.cl-left { text-align: left; }\n'
        csspage += '.cl-justify { text-align: justify; }\n'

        # generate a list of each <style> starting point in the stylesheet
        styleList= self.posinDoc(b'book.stylesheet.style')
        stylecnt = len(styleList)
        styleList.append(-1)

        # process each style converting what you can

        if debug: print('          ', 'Processing styles.')
        for j in range(stylecnt):
            if debug: print('          ', 'Processing style %d' %(j))
            start = styleList[j]
            end = styleList[j+1]

            (pos, tag) = self.findinDoc(b'style._tag',start,end)
            if tag == None :
                (pos, tag) = self.findinDoc(b'style.type',start,end)

            # Is this something we know how to convert to css
            if tag in self.stags :

                # get the style class
                (pos, sclass) = self.findinDoc(b'style.class',start,end)
                if sclass != None:
                    sclass = sclass.replace(b' ',b'-')
                    sclass = b'.cl-' + sclass.lower()
                else :
                    sclass = b''

                if debug: print('sclass', sclass)

                # check for any "after class" specifiers
                (pos, aftclass) = self.findinDoc(b'style._after_class',start,end)
                if aftclass != None:
                    aftclass = aftclass.replace(b' ',b'-')
                    aftclass = b'.cl-' + aftclass.lower()
                else :
                    aftclass = b''

                if debug: print('aftclass', aftclass)

                cssargs = {}

                while True :

                    (pos1, attr) = self.findinDoc(b'style.rule.attr', start, end)
                    (pos2, val) = self.findinDoc(b'style.rule.value', start, end)

                    if debug: print('attr', attr)
                    if debug: print('val', val)

                    if attr == None : break

                    if (attr == b'display') or (attr == b'pos') or (attr == b'align'):
                        # handle text based attributess
                        attr = attr + b'-' + val
                        if attr in self.attr_str_map :
                            cssargs[attr] = (self.attr_str_map[attr], b'')
                    else :
                        # handle value based attributes
                        if attr in self.attr_val_map :
                            name = self.attr_val_map[attr]
                            if attr in (b'margin-bottom', b'margin-top', b'space-after') :
                                scale = self.ph
                            elif attr in (b'margin-right', b'indent', b'margin-left', b'hang') :
                                scale = self.pw
                            elif attr == b'line-space':
                                scale = self.fontsize * 2.0
                            else:
                                print("Scale not defined!")
                                scale = 1.0

                            if not val:
                                val = 0

                            if not ((attr == b'hang') and (int(val) == 0)):
                                try:
                                    f = float(val)
                                except:
                                    print("Warning: unrecognised val, ignoring")
                                    val = 0
                                pv = float(val)/scale
                                cssargs[attr] = (self.attr_val_map[attr], pv)
                                keep = True

                    start = max(pos1, pos2) + 1

                # disable all of the after class tags until I figure out how to handle them
                if aftclass != "" : keep = False

                if keep :
                    if debug: print('keeping style')
                    # make sure line-space does not go below 100% or above 300% since
                    # it can be wacky in some styles
                    if b'line-space' in cssargs:
                        seg = cssargs[b'line-space'][0]
                        val = cssargs[b'line-space'][1]
                        if val < 1.0: val = 1.0
                        if val > 3.0: val = 3.0
                        del cssargs[b'line-space']
                        cssargs[b'line-space'] = (self.attr_val_map[b'line-space'], val)


                    # handle modifications for css style hanging indents
                    if b'hang' in cssargs:
                        hseg = cssargs[b'hang'][0]
                        hval = cssargs[b'hang'][1]
                        del cssargs[b'hang']
                        cssargs[b'hang'] = (self.attr_val_map[b'hang'], -hval)
                        mval = 0
                        mseg = 'margin-left: '
                        mval = hval
                        if b'margin-left' in cssargs:
                            mseg = cssargs[b'margin-left'][0]
                            mval = cssargs[b'margin-left'][1]
                            if mval < 0: mval = 0
                            mval = hval + mval
                        cssargs[b'margin-left'] = (mseg, mval)
                        if b'indent' in cssargs:
                            del cssargs[b'indent']

                    cssline = sclass + ' { '
                    for key in iter(cssargs):
                        mseg = cssargs[key][0]
                        mval = cssargs[key][1]
                        if mval == '':
                            cssline += mseg + ' '
                        else :
                            aseg = mseg + '%.1f%%;' % (mval * 100.0)
                            cssline += aseg + ' '

                    cssline += '}'

                    if sclass != '' :
                        classlst += sclass + '\n'

                    # handle special case of paragraph class used inside chapter heading
                    # and non-chapter headings
                    if sclass != '' :
                        ctype = sclass[4:7]
                        if ctype == 'ch1' :
                            csspage += 'h1' + cssline + '\n'
                        if ctype == 'ch2' :
                            csspage += 'h2' + cssline + '\n'
                        if ctype == 'ch3' :
                            csspage += 'h3' + cssline + '\n'
                        if ctype == 'h1-' :
                            csspage += 'h4' + cssline + '\n'
                        if ctype == 'h2-' :
                            csspage += 'h5' + cssline + '\n'
                        if ctype == 'h3_' :
                            csspage += 'h6' + cssline + '\n'

                    if cssline != ' { }':
                        csspage += self.stags[tag] + cssline + '\n'


        return csspage, classlst



def convert2CSS(flatxml, fontsize, ph, pw):

    print('          ', 'Using font size:',fontsize)
    print('          ', 'Using page height:', ph)
    print('          ', 'Using page width:', pw)

    # create a document parser
    dp = DocParser(flatxml, fontsize, ph, pw)
    if debug: print('          ', 'Created DocParser.')
    csspage = dp.process()
    if debug: print('          ', 'Processed DocParser.')
    return csspage


def getpageIDMap(flatxml):
    dp = DocParser(flatxml, 0, 0, 0)
    pageidnumbers = dp.getData('info.original.pid', 0, -1, True)
    return pageidnumbers