Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / filter / source / svg / js2hxx.py
blob38bcab12b417c160146e615e077e13a90f673a33
1 #!/usr/bin/env python
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
12 # License.
14 # Major Contributor(s):
15 # Copyright (C) 2011 Marco Cecchetti <mrcekets@gmail.com>
17 # All Rights Reserved.
19 # For minor contributions see the git repository.
21 import os, sys
23 MAX_LINES = 150
24 VARIABLE_NAME = 'aSVGScript'
26 def get_var_decl(n):
27 return 'static const char %s%d[] =' % ( VARIABLE_NAME, n )
29 script_name = os.path.basename( sys.argv[0] )
30 infile_name = sys.argv[1]
31 outfile_name = sys.argv[2]
34 # collect input JavaScript file lines
35 if( not os.path.isfile( infile_name ) ):
36 print ( '%s: error: file "%s" not found' % ( script_name, infile_name ) )
37 sys.exit( -1 )
39 infile = open( infile_name, 'r' )
40 in_lines = [line.rstrip() for line in infile.readlines()]
41 infile.close()
44 valid_lines=[]
45 is_multiline_comment = False
46 lineNumber = 0
47 emptyLineCount = 0
48 for line in in_lines:
49 lineNumber += 1
50 index = line.find('"')
51 if( index != -1 ):
52 print ( '%s: warning: processed file contains \'"\' at %d:%d' % ( script_name, lineNumber, index ) )
54 sline = line.strip()
56 # strip comment lines except multilines comments that begins with one '/' and exactly 5 '*'
57 if( is_multiline_comment and sline.endswith( '*/' ) ):
58 is_multiline_comment = False
59 continue
61 if( is_multiline_comment ):
62 continue
64 if( sline.startswith( '//' ) ):
65 continue
67 if( sline.startswith( '/*' ) and sline.endswith( '*/' ) ):
68 continue
70 if( ( sline.startswith( '/*' ) and not sline.startswith( '/*****' ) )
71 or sline.startswith( '/******' ) ):
72 is_multiline_comment = True
73 continue
75 # disable any debug printer
76 dline = line.replace( 'NAVDBG.on', 'NAVDBG.off' )
77 dline = dline.replace( 'ANIMDBG.on', 'ANIMDBG.off' )
78 dline = dline.replace( 'DebugPrinter.on', 'DebugPrinter.off' )
80 escaped_line = '%s' % dline
81 escaped_line = escaped_line.rstrip().lstrip()
83 # no more than 2 consecutive empty lines
84 if( escaped_line == '' ):
85 emptyLineCount += 1
86 else:
87 emptyLineCount = 0
89 if( emptyLineCount > 2 ):
90 continue
92 # append to some escape sequence another '\'
93 escaped_line = escaped_line.replace( '\\', '\\\\' )
94 escaped_line = escaped_line.replace( '\n', '\\n')
95 escaped_line = escaped_line.replace( '\t', '\\t' )
97 valid_lines.append( escaped_line )
100 # compute the number of needed fragments that is of C constant strings
101 total_valid_lines = len (valid_lines) + 2
102 total_fragments = total_valid_lines / MAX_LINES
103 if( ( total_valid_lines % MAX_LINES ) != 0 ):
104 total_fragments += 1
108 out_lines = []
109 out_lines.append( '' )
110 out_lines.append( '#define N_SVGSCRIPT_FRAGMENTS %d' % total_fragments )
111 out_lines.append( '' )
112 out_lines.append( get_var_decl( 0 ) )
113 out_lines.append( '"<![CDATA[\\n\\' )
114 i = 2
115 fragment = 0
116 for line in valid_lines:
117 out_lines.append( line + '\\n\\' )
118 if( i == MAX_LINES ):
119 i = 0
120 fragment += 1
121 out_lines.append( '";' )
122 out_lines.append( '' )
123 out_lines.append( get_var_decl( fragment ) )
124 out_lines.append( '"\\' )
125 i += 1
127 out_lines.append( ']]>";' )
128 out_lines.append( '' )
130 out_lines.append('static const char * g_SVGScripts[N_SVGSCRIPT_FRAGMENTS] = {')
131 for j in range(0, fragment+1):
132 out_lines.append(" %s%d," % (VARIABLE_NAME, j))
133 out_lines.append('};')
135 outfile = open( outfile_name, 'w' )
136 if( not os.path.isfile( outfile_name ) ):
137 print ( '%s: error: I cannot create file "%s"' % ( script_name, outfile_name ) )
138 sys.exit( -1 )
141 # C++ header
142 header_info = '/* !! This file is auto-generated, do not edit !! */'
144 outfile.write( header_info +'\n\n' )
146 for line in out_lines:
147 outfile.write( line + '\n' )
149 outfile.close()