2 pygments.formatters.rtf
3 ~~~~~~~~~~~~~~~~~~~~~~~
5 A formatter that generates RTF files.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
15__all__ = [
'RtfFormatter']
20 Format tokens as RTF markup. This formatter automatically outputs full RTF
21 documents with color information and other useful stuff. Perfect for Copy and
22 Paste into Microsoft(R) Word(R) documents.
24 Please note that ``encoding`` and ``outencoding`` options are ignored.
25 The RTF format is ASCII natively, but handles unicode characters correctly
26 thanks to escape sequences.
30 Additional options accepted:
33 The style to use, can be a string or a Style subclass (default:
37 The used font family, for example ``Bitstream Vera Sans``. Defaults to
38 some generic font which is supposed to have fixed width.
41 Size of the font used. Size is specified in half points. The
42 default is 24 half-points, giving a size 12 font.
52 Additional options accepted:
55 Name of the font used. Could for example be ``'Courier New'``
56 to further specify the default which is ``'\fmodern'``. The RTF
57 specification claims that ``\fmodern`` are "Fixed-pitch serif
58 and sans serif fonts". Hope every RTF implementation thinks
59 the same about modern...
64 self.
fontsize = get_int_opt(options,
'fontsize', 0)
68 .replace(
'{',
'\\{') \
85 elif (2**7) <= cn < (2**16):
91 buf.append(
'{\\u%d}{\\u%d}' % surrogatepair(cn))
93 return ''.join(buf).replace(
'\n',
'\\par\n')
98 '{\\fonttbl{\\f0\\fmodern\\fprq1\\fcharset0%s;}}'
106 for _, style
in self.
style:
107 for color
in style[
'color'], style[
'bgcolor'], style[
'border']:
108 if color
and color
not in color_mapping:
109 color_mapping[color] = offset
121 for ttype, value
in tokensource:
124 style = self.
style.style_for_token(ttype)
127 buf.append(
'\\cb%d' % color_mapping[style[
'bgcolor']])
129 buf.append(
'\\cf%d' % color_mapping[style[
'color']])
134 if style[
'underline']:
138 color_mapping[style[
'border']])