Skip to content

Commit

Permalink
Fix issues raised by Codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelsousa committed May 16, 2018
1 parent 08579da commit 7920ede
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 52 deletions.
30 changes: 11 additions & 19 deletions afdko/Tools/SharedData/FDKScripts/fontPDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@

from __future__ import print_function, absolute_import

import os
import re
import time

from . import FDKUtils
from . import pdfgen
from . import pdfmetrics
from .pdfutils import LINEEND

__copyright__ = """Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
"""

Expand Down Expand Up @@ -265,19 +274,6 @@
--pointLabelSize 12 #Change the size of the point label text.
"""

import time
import os
import re
import sys

from . import FDKUtils
from . import pdfdoc
from . import pdfgen
from . import pdfgeom
from . import pdfmetrics
from . import pdfutils
from .pdfutils import LINEEND

inch = INCH = 72
cm = CM = inch / 2.54
kShowMetaTag = "drawMeta_"
Expand Down Expand Up @@ -1258,7 +1254,8 @@ def drawMeta_RowFont(self, params):
hintDir = rec[1]
rowDir = rec[2]
except (KeyError, TypeError):
rowFont = "RowFont: CID not in layout file"
hintDir = None
rowDir = None
else:
# it is name-keyed font that is not helpfully usiing cidXXXX names. Assume that it is in the
# std development heirarchy.
Expand All @@ -1277,7 +1274,6 @@ def drawMeta_WidthOnly(self, params):


def getTitleHeight(params):
pageTitleFont = params.pageTitleFont
pageTitleSize = params.pageTitleSize
cur_y = params.pageSize[1] - (params.pageTopMargin + pageTitleSize)
cur_y -= pageTitleSize*1.2
Expand Down Expand Up @@ -1483,24 +1479,20 @@ def getLayoutFromGPP(params, extraY, yTop):
scale = scale1
numAcross = numAcross1
numDown = numDown1
numGlyphs = numGlyphs1
else:
scale = scale2
numAcross = numAcross2
numDown = numDown2
numGlyphs = numGlyphs2
break
elif (numGlyphs2 >= glyphsPerPage):
scale = scale2
numAcross = numAcross2
numDown = numDown2
numGlyphs = numGlyphs2
break
elif (numGlyphs1 >= glyphsPerPage):
scale = scale1
numAcross = numAcross1
numDown = numDown1
numGlyphs = numGlyphs1
break

if tryCount > 0:
Expand Down
22 changes: 11 additions & 11 deletions afdko/Tools/SharedData/FDKScripts/pdfdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@
from types import *
from math import sin, cos, pi, ceil

from . import pdfutils
from .pdfutils import LINEEND # this constant needed in both

Log = sys.stderr # reassign this if you don't want err output to console

try:
import zlib
except:
Log.write("zlib not available, page compression not available\n")

from . import pdfmetrics
from . import pdfutils
from .pdfgeom import bezierArc
from .pdfutils import LINEEND # this constant needed in both

##############################################################
#
# Constants and declarations
Expand All @@ -53,11 +51,15 @@

kDefaultEncoding = '/MacRoman'
kDefaultFontType = "/Type1"
PDFError = 'PDFError'
AFMDIR = '.'

A4 = (595.27,841.89) #default page size


class PDFError(KeyError):
pass


class PDFDocument:
"""Responsible for linking and writing out the whole document.
Builds up a list of objects using add(key, object). Each of these
Expand Down Expand Up @@ -137,14 +139,13 @@ def setSubject(self, subject):
"embeds in PDF file"
self.info.subject = subject


def printXref(self):
self.startxref = sys.stdout.tell()
Log.write('xref\n')
Log.write("%s %s" % (0,len(self.objects) + 1) )
Log.write("%s %s" % (0, len(self.objects) + 1))
Log.write('0000000000 65535 f')
for pos in self.xref:
Log.write( '%0.10d 00000 n\n' % pos)
Log.write('%0.10d 00000 n\n' % pos)

def writeXref(self, f):
self.startxref = f.tell()
Expand All @@ -154,7 +155,6 @@ def writeXref(self, f):
for pos in self.xref:
f.write('%0.10d 00000 n' % pos + LINEEND)


def printTrailer(self):
print('trailer')
print('<< /Size %d /Root %d 0 R /Info %d 0 R>>' % (
Expand Down Expand Up @@ -313,7 +313,7 @@ def getInternalFontName(self, psfontname, encoding=kDefaultEncoding):
pdfFont = entry[1]
return "/%s" % (pdfFont.keyname)
except:
raise (PDFError, "Font %s not available in document" % psfontname)
raise PDFError("Font %s not available in document" % psfontname)

def getAvailableFonts(self):
# There may be more
Expand Down
45 changes: 23 additions & 22 deletions afdko/Tools/SharedData/FDKScripts/pdfgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,37 @@ def __init__(self,filename,pagesize=(595.27,841.89), bottomup = 1):
self._currentPageHasImages = 1
self._pageTransitionString = ''

self._pageCompression = 1 #on by default - turn off when debugging!
self._pageCompression = 1 # on by default - turn off when debugging!
self._pageNumber = 1 # keep a count
self._code = [] #where the current page's marking operators accumulate
# where the current page's marking operators accumulate
self._code = []

#PostScript has the origin at bottom left. It is easy to achieve a top-
#down coord system by translating to the top of the page and setting y
#scale to -1, but then text is inverted. So self.bottomup is used
#to also set the text matrix accordingly. You can now choose your
#drawing coordinates.
# PostScript has the origin at bottom left. It is easy to achieve a
# top-down coord system by translating to the top of the page and
# setting y scale to -1, but then text is inverted. So self.bottomup
# is used to also set the text matrix accordingly. You can now choose
# your drawing coordinates.
self.bottomup = bottomup
if self.bottomup:
#set initial font
#self._preamble = 'BT /F9 12 Tf 14.4 TL ET'
# set initial font
# self._preamble = 'BT /F9 12 Tf 14.4 TL ET'
self._preamble = '1 0 0 1 0 0 cm BT /F9 12 Tf 14.4 TL ET'
else:
#switch coordinates, flip text and set font
#self._preamble = '1 0 0 -1 0 %0.4f cm BT /F9 12 Tf 14.4 TL ET' % self._pagesize[1]
self._preamble = '1 0 0 -1 0 %0.4f cm BT /F9 12 Tf 14.4 TL ET' % self._pagesize[1]
# switch coordinates, flip text and set font
self._preamble = ('1 0 0 -1 0 %0.4f cm BT /F9 12 Tf 14.4 TL ET' %
self._pagesize[1])

#initial graphics state
# initial graphics state
self._x = 0
self._y = 0
self._fontname = 'Times-Roman'
self._fontsize = 12
self._textMode = 0 #track if between BT/ET
self._textMode = 0 # track if between BT/ET
self._leading = 14.4
self._currentMatrix = (1., 0., 0., 1., 0., 0.)
self._fillMode = 0 #even-odd
self._fillMode = 0 # even-odd

#text state
# text state
self._charSpace = 0
self._wordSpace = 0
self._horizScale = 100
Expand All @@ -158,7 +159,7 @@ def __init__(self,filename,pagesize=(595.27,841.89), bottomup = 1):
# line drawing
self._lineCap = 0
self._lineJoin = 0
self._lineDash = None #not done
self._lineDash = None # not done
self._lineWidth = 0
self._mitreLimit = 0

Expand All @@ -168,12 +169,12 @@ def __init__(self,filename,pagesize=(595.27,841.89), bottomup = 1):
def _escape(self, s):
"""PDF escapes are like Python ones, but brackets need slashes before them too.
Use Python's repr function and chop off the quotes first"""
#s = repr(s)[1:-1]
# s = repr(s)[1:-1]
s = string.replace(s, '(','\(')
s = string.replace(s, ')','\)')
return s

#info functions - non-standard
# info functions - non-standard
def setAuthor(self, author):
self._doc.setAuthor(author)

Expand All @@ -195,11 +196,11 @@ def showPage(self):
page.hasImages = self._currentPageHasImages
page.pageTransitionString = self._pageTransitionString
page.setCompression(self._pageCompression)
#print stream
# print stream
page.setStream([self._preamble] + self._code)
self._doc.addPage(page)

#now get ready for the next one
# now get ready for the next one
self._pageNumber = self._pageNumber + 1
self._code = [] # ready for more...
self._currentPageHasImages = 0
Expand Down Expand Up @@ -349,7 +350,7 @@ def arc(self, x1,y1, x2,y2, startAng=0, extent=90):
Jim Fitzsimmon's TeX tutorial <URL: http://www.tinaja.com/bezarc1.pdf>."""

pointList = pdfgeom.bezierArc(x1,y1, x2,y2, startAng, extent)
#move to first point
# move to first point
self._code.append('n %0.4f %0.4f m' % pointList[0][:2])
for curve in pointList:
self._code.append('%0.4f %0.4f %0.4f %0.4f %0.4f %0.4f c' % curve[2:])
Expand Down

0 comments on commit 7920ede

Please sign in to comment.