Skip to content

Commit

Permalink
Merge pull request #1044 from alerque/issue-1012
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Aug 25, 2020
2 parents bf51586 + 88ac888 commit f371bff
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 54 deletions.
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ define runsile =
endef

_FORCED = $(and $(SILE_COVERAGE)$(CLEAN),force)
_TEST_DEPS = $(and $(filter tests/%,$@),$(addprefix .fonts/,$(TESTFONTFILES)))
_DOCS_DEPS = $(and $(filter documentation/%,$@),$(addprefix .fonts/,$(DOCSFONTFILES)))
_EXAM_DEPS = $(and $(filter examples/%,$@),$(addprefix .fonts/,$(EXAMFONTFILES)))
_TEST_DEPS = $(and $$(filter tests/%,$@),$(addprefix .fonts/,$(TESTFONTFILES)))
_DOCS_DEPS = $(and $$(filter documentation/%,$@),$(addprefix .fonts/,$(DOCSFONTFILES)))
_EXAM_DEPS = $(and $$(filter examples/%,$@),$(addprefix .fonts/,$(EXAMFONTFILES)))

# TODO: remove _BUILT_SUBDIRS hack and replace it with something sensible when
# these subdirs don't do crazy things like copying files outside of their own trees!
Expand Down
7 changes: 5 additions & 2 deletions core/cairo-output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ SILE.outputters.cairo = {
return self:setCursor(x, y)
end,

setCursor = function (self, x, y)
setCursor = function (self, x, y, relative)
_deprecationCheck(self)
move(cr, x, y)
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y - y
move(cr, cursorX, cursorY)
end,

setColor = function (self, color)
Expand Down
10 changes: 7 additions & 3 deletions core/debug-output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ SILE.outputters.debug = {
return self:setCursor(x, y)
end,

setCursor = function (self, x, y)
setCursor = function (self, x, y, relative)
_deprecationCheck(self)
x = SU.cast("number", x)
y = SU.cast("number", y)
if string.format("%.4f", x) ~= string.format("%.4f", cursorX) then writeline("Mx ", string.format("%.4f", x)); cursorX = x end
if string.format("%.4f", y) ~= string.format("%.4f", cursorY) then writeline("My ", string.format("%.4f", y)); cursorY = y end
local oldx, oldy = self:getCursor()
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y - y
if string.format("%.4f", oldx) ~= string.format("%.4f", cursorX) then writeline("Mx ", string.format("%.4f", x)) end
if string.format("%.4f", oldy) ~= string.format("%.4f", cursorY) then writeline("My ", string.format("%.4f", y)) end
end,

setColor = function (self, color)
Expand Down
88 changes: 53 additions & 35 deletions core/libtexpdf-output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ if (not SILE.outputters) then SILE.outputters = {} end
local cursorX = 0
local cursorY = 0

local font = 0
local started = false
local lastkey

local debugfont = SILE.font.loadDefaults({ family = "Gentium Plus", language = "en", size = 10 })

local function ensureInit ()
if not started then
pdf.init(SILE.outputFilename, SILE.documentState.paperSize[1], SILE.documentState.paperSize[2], SILE.full_version)
Expand All @@ -23,6 +24,10 @@ local _deprecationCheck = function (caller)
end
end

local glyph2string = function (glyph)
return string.char(math.floor(glyph % 2^32 / 2^8)) .. string.char(glyph % 0x100)
end

local _dl = 0.5

SILE.outputters.libtexpdf = {
Expand Down Expand Up @@ -67,12 +72,13 @@ SILE.outputters.libtexpdf = {
return self:setCursor(x, y)
end,

setCursor = function (self, x, y)
setCursor = function (self, x, y, relative)
_deprecationCheck(self)
x = SU.cast("number", x)
y = SU.cast("number", y)
cursorX = x
cursorY = SILE.documentState.paperSize[2] - y
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y + (relative and 0 or SILE.documentState.paperSize[2]) - y
end,

setColor = function (self, color)
Expand Down Expand Up @@ -103,6 +109,11 @@ SILE.outputters.libtexpdf = {
return self:drawHbox(value, width)
end,

_drawString = function(self, str, width)
local x, y = self:getCursor()
pdf.setstring(x, y, str, string.len(str), self._font, width)
end,

drawHbox = function (self, value, width)
_deprecationCheck(self)
width = SU.cast("number", width)
Expand All @@ -117,30 +128,42 @@ SILE.outputters.libtexpdf = {
-- position (cursorX + width - remember that the box's "width"
-- is actually the shaped x_advance).
if value.complex then
for i = 1, #(value.items) do
local glyph = value.items[i].gid
local buf = string.char(math.floor(glyph % 2^32 / 2^8)) .. string.char(glyph % 0x100)
pdf.setstring(cursorX + (value.items[i].x_offset or 0), cursorY + (value.items[i].y_offset or 0), buf, string.len(buf), font, value.items[i].glyphAdvance)
cursorX = cursorX + value.items[i].width
for i = 1, #value.items do
local buf = glyph2string(value.items[i].gid)
self:setCursor(value.items[i].x_offset or 0, value.items[i].y_offset or 0, true)
self:_drawString(buf, value.items[i].glyphAdvance)
self:setCursor(value.items[i].width, 0, true)
end
else
local buf = {}
for i = 1, #value.glyphString do
buf[i] = glyph2string(value.glyphString[i])
end
return
buf = table.concat(buf, "")
self:_drawString(buf, width)
end
local buf = {}
for i = 1, #(value.glyphString) do
local glyph = value.glyphString[i]
buf[#buf+1] = string.char(math.floor(glyph % 2^32 / 2^8))
buf[#buf+1] = string.char(glyph % 0x100)
end,

_debugfont = nil,

_withDebugFont = function (self, callback)
if not self._debugfont then
self._debugfont = self:setFont(debugfont)
end
buf = table.concat(buf, "")
pdf.setstring(cursorX, cursorY, buf, string.len(buf), font, width)
local oldfont = self._font
self._font = self._debugfont
callback()
self._font = oldfont
end,

_font = nil,

setFont = function (self, options)
_deprecationCheck(self)
ensureInit()
if SILE.font._key(options) == lastkey then return end
lastkey = SILE.font._key(options)
font = SILE.font.cache(options, SILE.shaper.getFace)
local key = SILE.font._key(options)
if key == lastkey then return end
local font = SILE.font.cache(options, SILE.shaper.getFace)
if options.direction == "TTB" then
font.layout_dir = 1
end
Expand All @@ -149,9 +172,10 @@ SILE.outputters.libtexpdf = {
else
pdf.setdirmode(0)
end
local pdffont = pdf.loadfont(font)
if pdffont < 0 then SU.error("Font loading error for "..options) end
font = pdffont
self._font = pdf.loadfont(font)
if self._font < 0 then SU.error("Font loading error for "..options) end
lastkey = key
return self._font
end,

drawImage = function (self, src, x, y, width, height)
Expand Down Expand Up @@ -218,23 +242,17 @@ SILE.outputters.libtexpdf = {
self:drawRule(frame:right()-_dl/2, frame:top()-_dl/2, _dl, frame:height()+_dl)
self:drawRule(frame:left()-_dl/2, frame:bottom()-_dl/2, frame:width()+_dl, _dl)
-- self:drawRule(frame:left() + frame:width()/2 - 5, (frame:top() + frame:bottom())/2+5, 10, 10)
local gentium = SILE.font.loadDefaults({family="Gentium Plus", language="en"})
local stuff = SILE.shaper:createNnodes(frame.id, gentium)
local stuff = SILE.shaper:createNnodes(frame.id, debugfont)
stuff = stuff[1].nodes[1].value.glyphString -- Horrible hack
local buf = {}
for i = 1, #stuff do
local glyph = stuff[i]
buf[#buf+1] = string.char(math.floor(glyph % 2^32 / 2^8))
buf[#buf+1] = string.char(glyph % 0x100)
buf[i] = glyph2string(stuff[i])
end
buf = table.concat(buf, "")
local oldfont = font
self:setFont(gentium)
pdf.setstring(frame:left():tonumber() - _dl/2, (SILE.documentState.paperSize[2] - frame:top()):tonumber() + _dl/2, buf, string.len(buf), font, 0)
if oldfont then
pdf.loadfont(oldfont)
font = oldfont
end
self:_withDebugFont(function ()
self:setCursor(frame:left():tonumber() - _dl/2, frame:top():tonumber() + _dl/2)
self:_drawString(buf, 0)
end)
self:popColor()
end,

Expand Down
7 changes: 4 additions & 3 deletions core/podofo-output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ SILE.outputters.podofo = {
return self:setCursor(x, y)
end,

setCursor = function (self, x, y)
setCursor = function (self, x, y, relative)
_deprecationCheck(self)
cursorX = x
cursorY = SILE.documentState.paperSize[2] - y
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y + SILE.documentState.paperSize[2] - y
end,

setColor = function (self, color)
Expand Down
18 changes: 10 additions & 8 deletions core/text-output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,31 @@ cursor = function (self)
return self:setCursor(x, y)
end,

setCursor = function (self, x, y)
setCursor = function (self, x, y, relative)
_deprecationCheck(self)
local bs = SILE.measurement("0.8bs"):tonumber()
local spc = SILE.measurement("0.8spc"):tonumber()
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
local newx, newy = offset.x + x, offset.y - y
if started then
if x < cursorX then
if newx < cursorX then
outfile:write("\n")
elseif y > cursorY then
if y - cursorY > bs then
elseif newy > cursorY then
if newy - cursorY > bs then
outfile:write("\n")
else
outfile:write("")
end
elseif x > cursorX then
if x - cursorX > spc then
elseif newx > cursorX then
if newx - cursorX > spc then
outfile:write(" ")
else
outfile:write("")
end
end
end
cursorY = y
cursorX = x
cursorY = newy
cursorX = newx
end,

setColor = function(self)
Expand Down
10 changes: 10 additions & 0 deletions tests/bug-1044.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Set paper size 297.6377985 419.5275636
Begin page
Mx 14.8819
My 27.9564
Set font Libertinus Serif;10;400;;normal;;LTR
T 53 66 (Ta)
Mx 27.9627
T 80 71 (of)
End page
Finish
8 changes: 8 additions & 0 deletions tests/bug-1044.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
\begin[papersize=a6]{document}
\nofolios
\neverindent
\font[family=Libertinus Serif]{Ta of}
% If complex shaping is skipped and glyphAdvance ignored when outputting
% a series of hboxes, the "Ta" kerning above will cause "of" to be positioned
% too far forward.
\end{document}

0 comments on commit f371bff

Please sign in to comment.