-def color_pdf(latex_file, bg_color, fg_color):
- use_preview_pdf_re = re.compile("(\s*\\\\usepackage\[[^]]+)((pdftex|xetex)\]{preview})")
-
- tmp = mkstemp()
-
- fg = ""
- if fg_color != "0.000000,0.000000,0.000000":
- fg = ' \\AtBeginDocument{\\let\\oldpreview\\preview\\renewcommand\\preview{\\oldpreview\\color[rgb]{%s}}}\n' % (fg_color)
-
- success = 0
- try:
- for line in open(latex_file, 'r').readlines():
- match = use_preview_pdf_re.match(line)
- if match == None:
- tmp.write(line)
- continue
- success = 1
- tmp.write(" \\usepackage{color}\n" \
- " \\pagecolor[rgb]{%s}\n" \
- "%s" \
- "%s\n" \
- % (bg_color, fg, match.group()))
- continue
-
- except:
- # Unable to open the file, but do nothing here because
- # the calling function will act on the value of 'success'.
- warning('Warning in color_pdf! Unable to open "%s"' % latex_file)
- warning(`sys.exc_type` + ',' + `sys.exc_value`)
-
- if success:
- copyfileobj(tmp, open(latex_file,"wb"), 1)
-
- return success
-
-