Skip to content

Commit

Permalink
Fixed/improved pdf output.
Browse files Browse the repository at this point in the history
Will now construct the pdf in resources/latex_temp,
  then copy to the output location.
  • Loading branch information
eric authored and eric committed May 20, 2013
1 parent 4fdc5e7 commit 307877d
Show file tree
Hide file tree
Showing 9 changed files with 1,514 additions and 29 deletions.
77 changes: 61 additions & 16 deletions LatexOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import os
import os.path
import shutil

def LatexString(char):
latexChar = open(resource('resources','char.tex')).read()
Expand All @@ -22,41 +23,85 @@ def SaveLatexFile(char,filename):
with open(filename,'w') as f:
f.write(LatexString(char))

def CompileLatex(char,filename):
outputdir = os.path.dirname(filename)
jobname = os.path.splitext(os.path.basename(filename))[0]
latexChar = LatexString(char)
def FindExecutable():
"""Attempts to find the latex executable"""
if sys.platform=='win32':
executable = resource('resources','pdftex.exe')
if executable is None:
raise EnvironmentError("""Could not find resources\pdftex.exe""")
else:
executable = 'pdflatex'
with open(os.devnull,'w') as shutup:
if subprocess.call(['which',executable],stdout=shutup,stdin=shutup):
executable = resource('resources','pdflatex')
executable = 'pdflatex' if executable is None else executable
with open(os.devnull,'w') as devnull:
if subprocess.call(['which',executable],stdout=devnull,stdin=devnull):
raise EnvironmentError("""Could not find pdflatex. Please install using 'sudo apt-get texlive-latex-base'""")
args = [executable,
'-output-directory='+resource('resources'),
'-jobname='+jobname,
"'"+latexChar+"'"]
subprocess.call(args)
return executable

def WorkingDir():
"""
Finds the appropriate working directory for temporary files.
Makes it in the resources directory if not existing.
"""
folderName = 'latex_temp'
workingDir = resource('resources',folderName)
if workingDir is not None:
return workingDir
workingDir = os.path.join(resource('resources'),
folderName)
os.makedirs(workingDir)
return workingDir

def _escape(inString):
return multiple_replace({'_':'\\_',
'^':'\\textasciicircum{}',
'{':'\\{',
'}':'\\}',
'%':'\\%',
'\\':'\\textbackslash{}',
'&':'\\&',
'~':'\\textasciitilde{}'},
inString)

def CompileLatex(char,filename):
outputdir = os.path.dirname(filename)
workingdir = WorkingDir()
jobname = os.path.splitext(os.path.basename(filename))[0]
SaveLatexFile(char,os.path.join(workingdir,jobname+'.tex'))
executable = FindExecutable()
#Two passes, since latex uses temporary files strangely.
for i in range(2):
args = [executable, jobname+'.tex',
'-include-directory='+resource('resources','latex_includes')]
subprocess.call(args,cwd=workingdir)
shutil.copy(os.path.join(workingdir,jobname+'.pdf'),
os.path.join(outputdir))



def _statsString(char):
return '\n'.join('\\Stat{{{name}}}{{{value}}}{{{bonus}}}'.format(
name=st.Name,value=st.Value,bonus=st.Bonus())
name=_escape(st.Name),
value=_escape(str(st.Value)),
bonus=_escape(str(st.Bonus())))
for st in char.Stats)

def _skillOverviewString(char):
return '\n'.join('\\CommonSkill{{{name}}}{{{ranks}}}{{{bonus}}}'.format(
name=sk.Name,ranks=sk.Value,bonus=sk.Bonus())
name=_escape(sk.Name),
ranks=_escape(str(sk.Value)),
bonus=_escape(str(sk.Bonus())))
for sk in char.Skills if sk.CommonlyUsed)

def _resistanceString(char):
return '\n'.join('\\Resistance{{{name}}}{{{bonus}}}'.format(
name=res.Name,bonus=res.Bonus())
name=_escape(res.Name),
bonus=_escape(str(res.Bonus())))
for res in char.Resistances)

def _skillFullListString(char):
return '\n'.join('\\DetailSkill{{{name}}}{{{ranks}}}{{{bonus}}}{{{depth}}}'.format(
name=sk.Name,ranks=sk.Value,bonus=sk.Bonus(),depth=sk.Depth)
name=_escape(sk.Name),
ranks=_escape(str(sk.Value)),
bonus=_escape(str(sk.Bonus())),
depth=_escape(str(sk.Depth)))
for sk in char.Skills)
12 changes: 6 additions & 6 deletions MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,16 @@ def Export(self,*args):
t = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_SAVE,
buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE,gtk.RESPONSE_OK))
t.set_current_name(self.char.GetMisc('Name')+'.pdf'
if self.char.GetMisc('Name')
else 'char.pdf')
response = t.run()
filename = t.get_filename()
t.destroy()
if response==gtk.RESPONSE_OK:
if os.path.splitext(filename)[-1]=='.tex':
LatexOutput.SaveLatexFile(self.char,filename)
elif os.path.splitext(filename)[-1]=='.pdf':
LatexOutput.CompileLatex(self.char,filename)
else:
LatexOutput.CompileLatex(self.char,filename+'.pdf')
if filename[-4:]!='.pdf':
filename += '.pdf'
LatexOutput.CompileLatex(self.char,filename)
def MakeProfessionList(self):
self._profdict = LoadProfessions(resource('tables','Professions.txt'))
profBox = self.b.get_object('profBox')
Expand Down
19 changes: 13 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
This is a character generator designed for the Rolemaster Universal (RMU) role-playing system. Direct downloads of pyCharGen can be found at https://sourceforge.net/projects/pychargen/files/, which contain stand-alone executables for both Windows and Linux.
This is a character generator designed for the Rolemaster Universal (RMU) role-playing system. Direct downloads of pyCharGen can be found at https://sourceforge.net/projects/pychargen/files/ which contain stand-alone executables for both Windows and Linux.

Features
----------------
- Create and modify characters for use in RMU.
- Add items, whcih contribute bonuses to stats and skills.
- Save and load characters in a human-readable format.
- Export characters to pdf format


pyCharGen is written in python 2.7, and requires the following external packages to be run from source. Again, no external programs are needed when running from the packaged binaries, only when running from source.
- pygtk ("sudo apt-get install python-gtk2" in linux or http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.24/pygtk-all-in-one-2.24.2.win32-py2.7.msi for ubuntu)
- pyinstaller (http://pyinstaller.org)
- pywin32 (http://sourceforge.net/projects/pywin32/)
Note: Only pygtk.
If you wish to use the provided executables or to run from source, it is not needed.
- pygtk ("sudo apt-get install python-gtk2" in linux or http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.24/pygtk-all-in-one-2.24.2.win32-py2.7.msi for Windows)
- pdftex.exe for Windows, or pdflatex for Linux (Needed for pdf output of character)
- pyinstaller (http://pyinstaller.org) (Needed only for packaging standalone executable)
- pywin32 (http://sourceforge.net/projects/pywin32/) (Needed only for packaging standalone executable)
File renamed without changes.
File renamed without changes.
78 changes: 78 additions & 0 deletions resources/latex_includes/fullpage.sty
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%%
%% This is file `fullpage.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% fullpage.dtx (with options: `package')
%% =============================================
%% IMPORTANT NOTICE:
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN
%% archives in directory macros/latex/base/lppl.txt; either
%% version 1 of the License, or any later version.
%%
%% This is a generated file.
%% It may not be distributed without the original source file fullpage.dtx.
%%
%% Full documentation can be obtained by LaTeXing that original file.
%% Only a few abbreviated comments remain here to describe the usage.
%% =============================================
%% Copyright 1994-1999 Patrick W Daly
%% Max-Planck-Institut f\"ur Aeronomie
%% Max-Planck-Str. 2
%% D-37191 Katlenburg-Lindau
%% Germany
%% E-mail: [email protected]
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{fullpage}
[1999/02/23 1.1 (PWD)]
% Sets all 4 margins to be either 1 inch or 1.5 cm, and specifies
% the page style
% Call as \usepackage[options]{fullpage}
% Options = in (default) for 1 inch
% cm for 1.5 cm
% plain (default) sets \pagestyle{plain}
% empty sets \pagestyle{empty}
% headings sets \pagestyle{headings}
% myheadings sets \pagestyle{myheadings}
%-----------------------------------------------------------
\newlength{\FP@margin}
\DeclareOption{in}{\setlength{\FP@margin}{1in}}
\DeclareOption{cm}{\setlength{\FP@margin}{1.5cm}}
\newif\ifFP@plain
\newif\ifFP@empty
\DeclareOption{plain}{\FP@plaintrue \FP@emptyfalse
\pagestyle{plain}}
\DeclareOption{empty}{\FP@plaintrue \FP@emptytrue
\pagestyle{empty}}
\DeclareOption{headings}{\FP@plainfalse \FP@emptyfalse
\pagestyle{headings}}
\DeclareOption{myheadings}{\FP@plainfalse \FP@emptyfalse
\pagestyle{myheadings}}
\ExecuteOptions{in,plain}
\ProcessOptions*
\ifFP@plain
\setlength{\headheight}{0pt}
\setlength{\headsep}{0pt}
\fi
\ifFP@empty
\setlength{\footskip}{0pt}
\fi
\setlength{\textwidth}{\paperwidth}
\addtolength{\textwidth}{-2\FP@margin}
\setlength{\oddsidemargin}{\FP@margin}
\addtolength{\oddsidemargin}{-1in}
\setlength{\evensidemargin}{\oddsidemargin}
\setlength{\textheight}{\paperheight}
\addtolength{\textheight}{-\headheight}
\addtolength{\textheight}{-\headsep}
\addtolength{\textheight}{-\footskip}
\addtolength{\textheight}{-2\FP@margin}
\setlength{\topmargin}{\FP@margin}
\addtolength{\topmargin}{-1in}
%%
%% <<<<< End of generated file <<<<<<
%%
%% End of file `fullpage.sty'.
Loading

0 comments on commit 307877d

Please sign in to comment.