Skip to content

Instantly share code, notes, and snippets.

@exterm
Created July 26, 2012 08:08
Show Gist options
  • Save exterm/3180896 to your computer and use it in GitHub Desktop.
Save exterm/3180896 to your computer and use it in GitHub Desktop.
Clean Code.
def nb_lines(width, text)
text = "" if text.nil?
cw = @CurrentFont['cw']
width = @w - @rMargin - @x if (width == 0)
wmax = (width - 2 * @cMargin) * 1000 / @FontSize
s = text.gsub("\r", '')
nb = s.length
nb -= 1 if nb > 0 && s[nb-1] == "\n"
sep = -1
i, j, l = 0, 0, 0
nl = 1
while i < nb
c = s[i].chr
if (c == "\n")
i += 1
sep = -1
j = 1
l = 0
nl += 1
next
end
sep = i if (c == ' ')
l += cw[c[0].ord]
if (l > wmax)
if (sep == -1)
i += 1 if i == j
else
i = sep +1
end
sep = -1
j = i
l = 0
nl += 1
else
i += 1
end
end
nl
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment