Created
July 26, 2012 08:08
-
-
Save exterm/3180896 to your computer and use it in GitHub Desktop.
Clean Code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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