comparison mupdf-source/scripts/wrap/rename.py @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 '''
2 Functions to create C++ names from MuPDF names.
3 '''
4
5 import os
6
7 import jlib
8
9 from . import util
10
11
12 def snake_to_camel( name, initial):
13 '''
14 Converts foo_bar to FooBar or fooBar.
15
16 >>> snake_to_camel( 'foo_bar', True)
17 FooBar
18 >>> snake_to_camel( 'foo_bar_q__a', False)
19 fooBarQ_A
20 '''
21 # libclang can treat size_t oddly, which we work around when parsing MuPDF
22 # headers, so we should not be given size_t.
23 #
24 assert name != 'size_t'
25 items = name.split( '_')
26 ret = ''
27 for i, item in enumerate( items):
28 if not item:
29 item = '_'
30 elif i or initial:
31 item = item[0].upper() + item[1:].lower()
32 ret += item
33 return ret
34
35
36 # Using camel case in function names seems to result in gcc errors when
37 # compiling the code created by swig -python. e.g. in _wrap_vthrow_fn()
38 #
39 # mupdfcpp_swig.cpp: In function PyObject* _wrap_vthrow_fn(PyObject*, PyObject*)
40 # mupdfcpp_swig.cpp:88571:15: error: invalid array assignment
41 # arg3 = *temp;
42
43
44 def internal( name):
45 '''
46 Used for internal names, e.g. exception types.
47 '''
48 return f'internal_{name}'
49
50 def error_class( error_enum):
51 '''
52 Name of generated class for MuPDF `FZ_ERROR_*` enum.
53 '''
54 assert error_enum.startswith( 'FZ_ERROR_')
55 return snake_to_camel( error_enum, initial=True)
56
57 def c_fn( fnname):
58 '''
59 Returns fully-qualified name of MuPDF C function `fnname()`.
60 '''
61 return f'::{fnname}'
62
63 def ll_fn( fnname):
64 '''
65 Returns name of low-level wrapper function for MuPDF C function `fnname()`,
66 adding a `ctx` arg and converting MuPDF exceptions into C++ exceptions.
67 '''
68 assert not fnname.startswith( 'll_'), f'fnname={fnname}'
69 return f'll_{fnname}'
70
71 if name.startswith( 'pdf_'):
72 return 'p' + name
73 ret = f'{util.clip( name, "fz_")}'
74 if ret in ('stdin', 'stdout', 'stderr'):
75 #log( 'appending underscore to {ret=}')
76 ret += '_'
77 return ret
78
79 def namespace():
80 return 'mupdf'
81
82 def namespace_ll_fn( fnname):
83 '''
84 Returns full-qualified name of low-level wrapper function for MuPDF C
85 function `fnname()`, adding a `ctx` arg and converting MuPDF exceptions
86 into C++ exceptions.
87 '''
88 return f'{namespace()}::{ll_fn(fnname)}'
89
90 def fn( fnname):
91 '''
92 Returns name of wrapper function for MuPDF C function `fnname()`, using
93 wrapper classes for args and return value.
94 '''
95 return fnname
96
97 def namespace_fn( fnname):
98 '''
99 Returns fully-qualified name of wrapper function for MuPDF C function
100 `fnname()`, using wrapper classes for args and return values.
101 '''
102 return f'{namespace()}::{fn(fnname)}'
103
104 def class_( structname):
105 '''
106 Returns name of class that wraps MuPDF struct `structname`.
107 '''
108 structname = util.clip( structname, 'struct ')
109
110 # Note that we can't return `structname` here because this will end up with
111 # SWIG complaining like:
112 #
113 # Error: 'pdf_xref' is multiply defined in the generated target language module
114 #
115 # - because SWIG internally puts everything into a single namespace.
116 #
117 #return structname
118
119 return snake_to_camel( structname, initial=True)
120 if structname.startswith( 'fz_'):
121 return snake_to_camel( util.clip( structname, 'fz_'), initial=True)
122 elif structname.startswith( 'pdf_'):
123 # Retain Pdf prefix.
124 return snake_to_camel( structname, initial=True)
125
126 def namespace_class( structname):
127 '''
128 Returns fully-qualified name of class that wraps MuPDF struct `structname`.
129 '''
130 return f'{namespace()}::{class_(structname)}'
131
132 def method( structname, fnname):
133 '''
134 Returns name of class method that wraps MuPDF function `fnname()`.
135 '''
136 return fnname
137 if structname:
138 structname = structname.lstrip( 'struct ')
139 assert structname is None or structname.startswith( ('fz_', 'pdf_'))
140 ret = util.clip( fnname, ('fz_', 'pdf_'))
141 if ret in ('stdin', 'stdout', 'stderr'):
142 jlib.log( 'appending underscore to {ret=}')
143 ret += '_'
144 return ret