Bitmap: Use abs(pitch) * rows as the bitmap length #164
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current calculation (width * rows) assumes that one pixel equals one byte which is not the case when rendering in monochrome mode. This can cause the length of the resulting slice to either be too long (risking segfaults) or too short (causing panics or partial glyphs) when using the monochrome mode.
When in monochrome mode freetype renders one bit per pixel, padded to the nearest multiple of 2 bytes (http://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html#FT_Bitmap). For example a glyph with dimensions 1x5 pixels (width x height) will use 10 bytes with only the most significant bit in the first byte in each row corresponding to the glyph itself. The old calculation assumes that this glyph will use 5 bytes in monochrome mode.
A glyph with the dimensions 5x5 pixels also uses a buffer of 10 bytes, but currently it will receive a length of 25.
If the pitch is negative the buffer pointer is still pointing to the start of the allocated buffer and the length is the same as if it was a positive number of the same magnitude. The difference is that the start of the buffer points at the last row of the glyph ("On the opposite, if the pitch is negative, the first bytes of the pixel buffer are part of the lower bitmap row."), rendering the glyph by stepping abs(pitch) forwards in the buffer will render the glyph upside down ("In all cases, the pitch is an offset to add to a bitmap pointer in order to go down one row.").