From a31d0146dac6f09aa27eda40995aeca7d7adc293 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 19 May 2016 16:14:08 +1000 Subject: [PATCH] Loosen the SLOOP (Set LOOP) opcode precondition. This goes against spec, but matches the C Freetype implementation and works around a bug in DejaVuSansMono.ttf's hinted '2' glyph. --- truetype/hint.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/truetype/hint.go b/truetype/hint.go index 0315de5..13f785b 100644 --- a/truetype/hint.go +++ b/truetype/hint.go @@ -341,7 +341,14 @@ func (h *hinter) run(program []byte, pCurrent, pUnhinted, pInFontUnits []Point, case opSLOOP: top-- - if h.stack[top] <= 0 { + // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM05/Chap5.html#SLOOP + // says that "Setting the loop variable to zero is an error". In + // theory, the inequality on the next line should be "<=" instead + // of "<". In practice, some font files' bytecode, such as the '2' + // glyph in the DejaVuSansMono.ttf that comes with Ubuntu 14.04, + // issue SLOOP with a zero on top of the stack. Just like the C + // Freetype code, we allow the zero. + if h.stack[top] < 0 { return errors.New("truetype: hinting: invalid data") } h.gs.loop = h.stack[top]