1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
// example2.cpp
// This file demonstrates how to render a coloured glyph with a differently
// coloured outline.
//
// Written Feb. 2009 by Erik Möller,
// with slight modifications by Werner Lemberg
//
// Public domain.
//
// Eric uses similar code in real applications; see
//
// http://www.timetrap.se
// http://www.emberwind.se
//
// for more.
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_STROKER_H
#include <vector>
#include <fstream>
#include <iostream>
#ifdef _MSC_VER
#define MIN __min
#define MAX __max
#else
#define MIN std::min
#define MAX std::max
#endif
// Define some fixed size types.
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
// Try to figure out what endian this machine is using. Note that the test
// below might fail for cross compilation; additionally, multi-byte
// characters are implementation-defined in C preprocessors.
#if (('1234' >> 24) == '1')
#elif (('4321' >> 24) == '1')
#define BIG_ENDIAN
#else
#error "Couldn't determine the endianness!"
#endif
// A simple 32-bit pixel.
union Pixel32
{
Pixel32()
: integer(0) { }
Pixel32(uint8 bi, uint8 gi, uint8 ri, uint8 ai = 255)
{
b = bi;
g = gi;
r = ri;
a = ai;
}
uint32 integer;
struct
{
#ifdef BIG_ENDIAN
uint8 a, r, g, b;
#else // BIG_ENDIAN
uint8 b, g, r, a;
#endif // BIG_ENDIAN
};
};
struct Vec2
{
Vec2() { }
Vec2(float a, float b)
: x(a), y(b) { }
float x, y;
};
struct Rect
{
Rect() { }
Rect(float left, float top, float right, float bottom)
: xmin(left), xmax(right), ymin(top), ymax(bottom) { }
void Include(const Vec2 &r)
{
xmin = MIN(xmin, r.x);
ymin = MIN(ymin, r.y);
xmax = MAX(xmax, r.x);
ymax = MAX(ymax, r.y);
}
float Width() const { return xmax - xmin + 1; }
float Height() const { return ymax - ymin + 1; }
float xmin, xmax, ymin, ymax;
};
// TGA Header struct to make it simple to dump a TGA to disc.
#if defined(_MSC_VER) || defined(__GNUC__)
#pragma pack(push, 1)
#pragma pack(1) // Dont pad the following struct.
#endif
struct TGAHeader
{
uint8 idLength, // Length of optional identification sequence.
paletteType, // Is a palette present? (1=yes)
imageType; // Image data type (0=none, 1=indexed, 2=rgb,
// 3=grey, +8=rle packed).
uint16 firstPaletteEntry, // First palette index, if present.
numPaletteEntries; // Number of palette entries, if present.
uint8 paletteBits; // Number of bits per palette entry.
uint16 x, // Horiz. pixel coord. of lower left of image.
y, // Vert. pixel coord. of lower left of image.
width, // Image width in pixels.
height; // Image height in pixels.
uint8 depth, // Image color depth (bits per pixel).
descriptor; // Image attribute flags.
};
#if defined(_MSC_VER) || defined(__GNUC__)
#pragma pack(pop)
#endif
bool
WriteTGA(const std::string &filename,
const Pixel32 *pxl,
uint16 width,
uint16 height)
{
std::ofstream file(filename.c_str(), std::ios::binary);
if (file)
{
TGAHeader header;
memset(&header, 0, sizeof(TGAHeader));
header.imageType = 2;
header.width = width;
header.height = height;
header.depth = 32;
header.descriptor = 0x20;
file.write((const char *)&header, sizeof(TGAHeader));
file.write((const char *)pxl, sizeof(Pixel32) * width * height);
return true;
}
return false;
}
// A horizontal pixel span generated by the FreeType renderer.
struct Span
{
Span() { }
Span(int _x, int _y, int _width, int _coverage)
: x(_x), y(_y), width(_width), coverage(_coverage) { }
int x, y, width, coverage;
};
typedef std::vector<Span> Spans;
// Each time the renderer calls us back we just push another span entry on
// our list.
void
RasterCallback(const int y,
const int count,
const FT_Span * const spans,
void * const user)
{
Spans *sptr = (Spans *)user;
for (int i = 0; i < count; ++i)
sptr->push_back(Span(spans[i].x, y, spans[i].len, spans[i].coverage));
}
// Set up the raster parameters and render the outline.
void
RenderSpans(FT_Library &library,
FT_Outline * const outline,
Spans *spans)
{
FT_Raster_Params params;
memset(¶ms, 0, sizeof(params));
params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT;
params.gray_spans = RasterCallback;
params.user = spans;
FT_Outline_Render(library, outline, ¶ms);
}
// Render the specified character as a colored glyph with a colored outline
// and dump it to a TGA.
void
WriteGlyphAsTGA(FT_Library &library,
const std::string &fileName,
wchar_t ch,
FT_Face &face,
int size,
const Pixel32 &fontCol,
const Pixel32 outlineCol,
float outlineWidth)
{
// Set the size to use.
if (FT_Set_Char_Size(face, size << 6, size << 6, 90, 90) == 0)
{
// Load the glyph we are looking for.
FT_UInt gindex = FT_Get_Char_Index(face, ch);
if (FT_Load_Glyph(face, gindex, FT_LOAD_NO_BITMAP) == 0)
{
// Need an outline for this to work.
if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
// Render the basic glyph to a span list.
Spans spans;
RenderSpans(library, &face->glyph->outline, &spans);
// Next we need the spans for the outline.
Spans outlineSpans;
// Set up a stroker.
FT_Stroker stroker;
FT_Stroker_New(library, &stroker);
FT_Stroker_Set(stroker,
(int)(outlineWidth * 64),
FT_STROKER_LINECAP_ROUND,
FT_STROKER_LINEJOIN_ROUND,
0);
FT_Glyph glyph;
if (FT_Get_Glyph(face->glyph, &glyph) == 0)
{
FT_Glyph_StrokeBorder(&glyph, stroker, 0, 1);
// Again, this needs to be an outline to work.
if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
{
// Render the outline spans to the span list
FT_Outline *o =
&reinterpret_cast<FT_OutlineGlyph>(glyph)->outline;
RenderSpans(library, o, &outlineSpans);
}
// Clean up afterwards.
FT_Stroker_Done(stroker);
FT_Done_Glyph(glyph);
// Now we need to put it all together.
if (!spans.empty())
{
// Figure out what the bounding rect is for both the span lists.
Rect rect(spans.front().x,
spans.front().y,
spans.front().x,
spans.front().y);
for (Spans::iterator s = spans.begin();
s != spans.end(); ++s)
{
rect.Include(Vec2(s->x, s->y));
rect.Include(Vec2(s->x + s->width - 1, s->y));
}
for (Spans::iterator s = outlineSpans.begin();
s != outlineSpans.end(); ++s)
{
rect.Include(Vec2(s->x, s->y));
rect.Include(Vec2(s->x + s->width - 1, s->y));
}
#if 0
// This is unused in this test but you would need this to draw
// more than one glyph.
float bearingX = face->glyph->metrics.horiBearingX >> 6;
float bearingY = face->glyph->metrics.horiBearingY >> 6;
float advance = face->glyph->advance.x >> 6;
#endif
// Get some metrics of our image.
int imgWidth = rect.Width(),
imgHeight = rect.Height(),
imgSize = imgWidth * imgHeight;
// Allocate data for our image and clear it out to transparent.
Pixel32 *pxl = new Pixel32[imgSize];
memset(pxl, 0, sizeof(Pixel32) * imgSize);
// Loop over the outline spans and just draw them into the
// image.
for (Spans::iterator s = outlineSpans.begin();
s != outlineSpans.end(); ++s)
for (int w = 0; w < s->width; ++w)
pxl[(int)((imgHeight - 1 - (s->y - rect.ymin)) * imgWidth
+ s->x - rect.xmin + w)] =
Pixel32(outlineCol.r, outlineCol.g, outlineCol.b,
s->coverage);
// Then loop over the regular glyph spans and blend them into
// the image.
for (Spans::iterator s = spans.begin();
s != spans.end(); ++s)
for (int w = 0; w < s->width; ++w)
{
Pixel32 &dst =
pxl[(int)((imgHeight - 1 - (s->y - rect.ymin)) * imgWidth
+ s->x - rect.xmin + w)];
Pixel32 src = Pixel32(fontCol.r, fontCol.g, fontCol.b,
s->coverage);
dst.r = (int)(dst.r + ((src.r - dst.r) * src.a) / 255.0f);
dst.g = (int)(dst.g + ((src.g - dst.g) * src.a) / 255.0f);
dst.b = (int)(dst.b + ((src.b - dst.b) * src.a) / 255.0f);
dst.a = MIN(255, dst.a + src.a);
}
// Dump the image to disk.
WriteTGA(fileName, pxl, imgWidth, imgHeight);
delete [] pxl;
}
}
}
}
}
}
int
main(int argc,
char **argv)
{
if (argc != 3)
{
std::cerr << "Render letter `B' of given font as a TGA image.\n";
std::cerr << "\n";
std::cerr << "usage: example2 <font> <TGA-file>\n";
return 1;
}
// Initialize FreeType.
FT_Library library;
FT_Init_FreeType(&library);
// Open up a font file.
std::ifstream fontFile(argv[1], std::ios::binary);
if (fontFile)
{
// Read the entire file to a memory buffer.
fontFile.seekg(0, std::ios::end);
std::fstream::pos_type fontFileSize = fontFile.tellg();
fontFile.seekg(0);
unsigned char *fontBuffer = new unsigned char[fontFileSize];
fontFile.read((char *)fontBuffer, fontFileSize);
// Create a face from a memory buffer. Be sure not to delete the memory
// buffer until you are done using that font as FreeType will reference
// it directly.
FT_Face face;
FT_New_Memory_Face(library, fontBuffer, fontFileSize, 0, &face);
// Dump out a single glyph to a tga.
WriteGlyphAsTGA(library,
argv[2],
L'B',
face,
100,
Pixel32(255, 90, 30),
Pixel32(255, 255, 255),
3.0f);
// Now that we are done it is safe to delete the memory.
delete [] fontBuffer;
}
// Clean up the library
FT_Done_FreeType(library);
return 1;
}
// Local Variables:
// coding: utf-8
// End:
|