freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Freetype] Rendering Fonts to Bitmap


From: Peter Montgomery
Subject: Re: [Freetype] Rendering Fonts to Bitmap
Date: Tue, 03 Sep 2002 15:20:18 -0700

Dexter,

Hi again.  Some thoughts on your problem:

1 - You say you can output to text using a weird method.  I assume this
is some sort of "ASCII art" output.  If so, that's good becuase it means
you are getting the right results from FreeType.  Just to be certain, I
would recommend you save the output from FreeType as a binary dump to
disk.  then open the file in PhotoShop or PaintShopPro as a "raw" image.
To do this, you need to make sure that you are telling FreeType to
generate 8 bit anti-aliased text.  You'll also need to know the size of
the bitmpa since you will have to tell the paint program the exact
dimensions so it can display it properly.

2 - If you get that working, then you have proof that your FReeType
calls are generating a proper bitmap.  Now you need to convert this to a
Windows style .BMP bitmap.  As I mentioned before, the scanlines in BMP
files are padded to the nearest 32 bit value in length.  To calculate
this, I have a macro I wrote to make the code a little cleaner.  This is
the macro:

#define NEAREST_LONGINT_BOUNDARY(a) (((a) + 3)& 0xfffffffc);

This macro assumes that the vsalue passed is a number of bytes, and it
returns the value rounded to the nearest 32 bit value.  So, if you give
it any value from 1 to 4, it will return 4.  If you give it 5 to 8, it
will return 8.  If you give it 9 to 12, it will return 12.

I then use the following code to calculate how many extra bytes are
needed at the end of each scanline.  That value is stored in "BmpX".
Lets assume you have a 31 X 31 pixel bitmap that FreeType has generated
for you.  The dimensions of the bitmap are stored in the variables
FreeTypeX and FreeTypeY.  You would use the following code:

 // Calculate # of pad bytes 'cuz BMPs pad scanlines to long boundaries
 BmpX = NEAREST_LONGINT_BOUNDARY(FreeTypeX );

With this information, you can now create a memory buffer big enough to
hold the FreeType bitmap which is also in the proper format for the BMP.
You'll need to calloc a buffer that's "BmpX" x "FreeTypeY" in size, and
then copy the FreeType bitmap into it a scanline at a time.  Make sure
you ONLY copy "FreeTypeX" bytes into each scanline.  Also remember to
advance

The following is not the most efficient code, but should serve to
explain what's going on:

char* CurrSrcPixel = start of the FreeType bitmap
char* CurrDstPixel = Start of the buffer you calloc'ed

for (CurrYX = 0; CurrY < FreeTypeY; CurrX++){
    for (CurrX = 0; CurrX < FreeTypeX;  CurrX++){
        // Copy the each scanline a pixel at a time
        CurrDstpixel[CurrX] = CurrSrcPixel[CurrY];
    }
    // Advance the pointer to the start of the the next scanline
    CurrSrcPixel  += FreeTypeX;
    CurrDstpixel += BmpX
}

When this loop finishes, you'll have a buffer that contains your
FreeType bitmap, but that is formatted properly for the Windows bitmap
format.

3 - Having said all of that, remember that even though you view it is a
B&W image, Windows views it as a 256 color image.  This means you'll
need to create a palette with RGB entries that go from 0 to 255.
Remember, palettes are RGBQUAD values, that are 32 bit integers.  Make
sure you set the R, G, and B values in each quad to the same value so
you get a B&W palette.  the "biBitCount" value will be 8.

Is this a lot of work?  You bet.  For a GUI, Windows makes displaying a
bitmap one of the most complicated things it does.  Let me know if this
helps or if I can give you any more information.

    Thanks,
    PeterM






reply via email to

[Prev in Thread] Current Thread [Next in Thread]