Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitmap: Use abs(pitch) * rows as the bitmap length #164

Merged
merged 1 commit into from
Jul 26, 2015

Conversation

m4rw3r
Copy link
Contributor

@m4rw3r m4rw3r commented Jul 25, 2015

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.").

@bvssvni
Copy link
Member

bvssvni commented Jul 26, 2015

Thanks!

bvssvni added a commit that referenced this pull request Jul 26, 2015
Bitmap: Use abs(pitch) * rows as the bitmap length
@bvssvni bvssvni merged commit 721fb3a into PistonDevelopers:master Jul 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants