Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc waterfallplot and proofpdf fixes #638

Merged
merged 5 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 11 additions & 23 deletions python/afdko/fontpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@

miguelsousa marked this conversation as resolved.
Show resolved Hide resolved
from __future__ import print_function, absolute_import

from math import ceil
import os
import re
import time

from afdko import fdkutils
from afdko import pdfgen
from afdko import pdfmetrics
from afdko import fdkutils, pdfgen, pdfmetrics
from afdko.pdfutils import LINEEND

__copyright__ = """Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
Expand Down Expand Up @@ -1301,24 +1300,12 @@ def doTitle(rt_canvas, pdfFont, params, numGlyphs, numPages = None):
rt_canvas.drawString(params.pageLeftMargin, cur_y, title)
rt_canvas.drawRightString(rightMarginPos, cur_y, time.asctime())
cur_y -= pageTitleSize*1.2
path = repr(params.rt_filePath) # Can be non-ASCII
if numPages == None:
numPages = numGlyphs // params.glyphsPerPage
if (numGlyphs % params.glyphsPerPage) > 0:
numPages +=1
pageString = ' %d of %s' % (rt_canvas.getPageNumber(), numPages)
pathWidth = pdfmetrics.stringwidth(path, pageTitleFont) * 0.001 * pageTitleSize
pageStringWidth = pdfmetrics.stringwidth(pageString, pageTitleFont) * 0.001 * pageTitleSize
adjustedWidth = 0
while (params.pageLeftMargin + pathWidth + pageStringWidth) > rightMarginPos:
adjustedWidth = 1
path = path[1:]
pathWidth = pdfmetrics.stringwidth( "..." + path, pageTitleFont) * 0.001 * pageTitleSize

if adjustedWidth:
path = "..." + path[3:]
if pageIncludeTitle:
rt_canvas.drawString(params.pageLeftMargin, cur_y, path)
rt_canvas.drawRightString(rightMarginPos, cur_y, pageString)
cur_y -= pageTitleSize/2
if pageIncludeTitle:
Expand Down Expand Up @@ -1855,11 +1842,12 @@ def doWaterfall(params, glyphList, fontInfo, wi, cur_y, cur_x, pdfFont, yTop, nu
fontInfo.setEncoding(fontInfo, glyphList)
rt_canvas.addFont(embeddedFontPSName, fontInfo.encoding, fontInfo, getFontDescriptorItems, getEncodingInfo)
leading = None
codeRange = range(fontInfo.firstChar, fontInfo.lastChar)
text = map(lambda charCode: chr(charCode), codeRange)
text = "".join(text)
text= text.replace("\\", "\\\\")
#print text, codeRange, glyphList
text = ''
for char_int in range(fontInfo.firstChar, fontInfo.lastChar):
if char_int == 92: # backslash
text += '\\\\'
else:
text += chr(char_int)
for gSize in params.waterfallRange:
cur_y -= gSize*1.2
if ((cur_y) < params.pageBottomMargin):
Expand Down Expand Up @@ -1952,9 +1940,9 @@ def makeWaterfallPDF(params, pdfFont, doProgressBar):
if glyphList:
glyphLists.append(glyphList)

numWaterfallsOnPage = pageHeight/float(waterfallHeight)
numWaterfallsOnPage = int(pageHeight / waterfallHeight)
numWaterFalls = len(glyphLists)
numPages = int(round(0.5 + float(numWaterFalls)/numWaterfallsOnPage))
numPages = int(ceil(float(numWaterFalls) / numWaterfallsOnPage))
if numPages == 0:
numPages = 1
doTitle(rt_canvas, pdfFont, params, numGlyphs, numPages)
Expand All @@ -1966,7 +1954,7 @@ def makeWaterfallPDF(params, pdfFont, doProgressBar):
progressBarInstance.EndProgress()
rt_canvas.showPage()
rt_canvas.save()
return


def makeFontSetPDF(pdfFontList, params, doProgressBar=True):
"""
Expand Down
6 changes: 2 additions & 4 deletions python/afdko/pdfdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,15 @@ def SaveToFileObject(self, fileobj):
the file. Keep track of the file position at each point for
use in the index at the end"""
f = fileobj
i = 1
self.xref = []
f.write(tobytes("%PDF-1.2" + LINEEND, encoding='utf-8')) # for CID support
f.write(tobytes(b"%\xed\xec\xb6\xbe\r\n", encoding='utf-8'))
for obj in self.objects:
for i, obj in enumerate(self.objects, 1):
pos = f.tell()
self.xref.append(pos)
f.write(tobytes(str(i) + ' 0 obj' + LINEEND, encoding='utf-8'))
obj.save(f)
f.write(tobytes('endobj' + LINEEND, encoding='utf-8'))
i = i + 1
self.writeXref(f)
self.writeTrailer(f)
f.write(tobytes('%%EOF', encoding='utf-8')) # no lineend needed on this one!
Expand Down Expand Up @@ -573,7 +571,7 @@ def save(self, file):
else:
file.write(tobytes('<< /Length %d >>' % length + LINEEND, encoding='utf-8'))
file.write(tobytes('stream' + LINEEND, encoding='utf-8'))
file.write(tobytes(data_to_write + LINEEND, encoding='utf-8'))
file.write(tobytes(data_to_write + LINEEND, encoding='latin-1'))
file.write(tobytes('endstream' + LINEEND, encoding='utf-8'))

class PDFImage(PDFObject):
Expand Down
4 changes: 1 addition & 3 deletions python/afdko/pdfgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,9 +1013,7 @@ def textLine(self, text=''):
self._y = self._y - self._leading
else:
self._y = self._y + self._leading
textList = ["(", text, ") Tj T*"]
text = "".join(textList)
self._code.append(text)
self._code.append('(%s) Tj T*' % text)

def textLines(self, stuff, trim=1):
"""prints multi-line or newlined strings, moving down. One
Expand Down
27 changes: 8 additions & 19 deletions python/afdko/proofpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,23 +848,18 @@ def proofMakePDF(pathList, params, txPath):
if tempPathCFF:
miguelsousa marked this conversation as resolved.
Show resolved Hide resolved
os.remove(tempPathCFF)


logMsg( "Wrote proof file %s. End time: %s." % (pdfFilePath, time.asctime()))
if pdfFilePath and params.openPDFWhenDone:
if curSystem == "Windows":
curdir = os.getcwdu()
basedir, pdfName = os.path.split(pdfFilePath)
os.chdir(basedir)
command = "start %s" % (pdfName)
print(command)
command = 'start "" "%s"' % pdfFilePath
fdkutils.runShellCmdLogging(command)
os.chdir(curdir)
elif os.name == "Linux":
command = "xdg-open \"" + pdfFilePath + "\"" + " &"
elif curSystem == "Linux":
command = 'xdg-open "%s" &' % pdfFilePath
fdkutils.runShellCmdLogging(command)
else:
command = "open \"" + pdfFilePath + "\"" + " &"
command = 'open "%s" &' % pdfFilePath
fdkutils.runShellCmdLogging(command)

else:
tmpList = []
for path in pathList:
Expand Down Expand Up @@ -908,19 +903,13 @@ def proofMakePDF(pathList, params, txPath):
logMsg("Wrote proof file %s. End time: %s." % (pdfFilePath, time.asctime()))
if pdfFilePath and params.openPDFWhenDone:
if curSystem == "Windows":
curdir = os.getcwdu()
basedir, pdfName = os.path.split(pdfFilePath)
os.chdir(basedir)
command = "start %s" % (pdfName)
print(command)
command = 'start "" "%s"' % pdfFilePath
fdkutils.runShellCmdLogging(command)
os.chdir(curdir)
elif curSystem == "Linux":
command = "xdg-open \"" + pdfFilePath + "\"" + " &"
print(command)
command = 'xdg-open "%s" &' % pdfFilePath
fdkutils.runShellCmdLogging(command)
else:
command = "open \"" + pdfFilePath + "\"" + " &"
command = 'open "%s" &' % pdfFilePath
fdkutils.runShellCmdLogging(command)


Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/proofpdf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,19 @@ def test_fontplot2_lf_option(font_filename, glyphs):
expected_path = get_expected_path(pdf_filename)
assert differ([expected_path, save_path,
'-s', '/CreationDate', '-e', 'macroman'])


@pytest.mark.parametrize('filename', ['SourceSansPro-Black',
'SourceSansPro-BlackIt'])
def test_waterfallplot(filename):
font_filename = '{}.otf'.format(filename)
pdf_filename = '{}.pdf'.format(filename)
font_path = get_input_path(font_filename)
save_path = get_temp_file_path()
expected_path = get_expected_path(pdf_filename)
runner(['-t', 'waterfallplot',
'-o', 'o', '_{}'.format(save_path), 'dno', '-f', font_path, '-a'])
assert differ([expected_path, save_path,
'-s', '/CreationDate',
'-r', r'^BT 1 0 0 1 \d{3}\.\d+ 742\.0000 Tm', # timestamp
'-e', 'macroman'])