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
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.
24 VARIABLE_NAME
= 'aSVGScript'
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
) )
39 infile
= open( infile_name
, 'r' )
40 in_lines
= [line
.rstrip() for line
in infile
.readlines()]
45 is_multiline_comment
= False
50 index
= line
.find('"')
52 print ( '%s: warning: processed file contains \'"\' at %d:%d' % ( script_name
, lineNumber
, index
) )
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
61 if( is_multiline_comment
):
64 if( sline
.startswith( '//' ) ):
67 if( sline
.startswith( '/*' ) and sline
.endswith( '*/' ) ):
70 if( ( sline
.startswith( '/*' ) and not sline
.startswith( '/*****' ) )
71 or sline
.startswith( '/******' ) ):
72 is_multiline_comment
= True
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
== '' ):
89 if( emptyLineCount
> 2 ):
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 ):
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\\' )
116 for line
in valid_lines
:
117 out_lines
.append( line
+ '\\n\\' )
118 if( i
== MAX_LINES
):
121 out_lines
.append( '";' )
122 out_lines
.append( '' )
123 out_lines
.append( get_var_decl( fragment
) )
124 out_lines
.append( '"\\' )
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
) )
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' )