Newer
Older
James Cook
committed
/**
* @file llfontfreetype.cpp
* @brief Freetype font library wrapper
*
* $LicenseInfo:firstyear=2002&license=viewerlgpl$
James Cook
committed
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
James Cook
committed
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
James Cook
committed
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
James Cook
committed
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
James Cook
committed
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llfontfreetype.h"
#include "llfontgl.h"
James Cook
committed
// Freetype stuff
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_MODULE_H
#include FT_SYSTEM_H
James Cook
committed
#include "llerror.h"
#include "llimage.h"
//#include "llimagej2c.h"
#include "llmath.h" // Linden math
#include "llstring.h"
//#include "imdebug.h"
#include "llfontbitmapcache.h"
#include "llgl.h"
FT_Render_Mode gFontRenderMode = FT_RENDER_MODE_NORMAL;
LLFontManager *gFontManagerp = NULL;
FT_Library gFTLibrary = NULL;
//static
void LLFontManager::initClass()
{
Richard Linden
committed
if (!gFontManagerp)
{
gFontManagerp = new LLFontManager;
}
James Cook
committed
}
//static
void LLFontManager::cleanupClass()
{
delete gFontManagerp;
gFontManagerp = NULL;
}
LLFontManager::LLFontManager()
{
int error;
error = FT_Init_FreeType(&gFTLibrary);
if (error)
{
// Clean up freetype libs.
LL_ERRS() << "Freetype initialization failure!" << LL_ENDL;
James Cook
committed
FT_Done_FreeType(gFTLibrary);
}
}
LLFontManager::~LLFontManager()
{
FT_Done_FreeType(gFTLibrary);
}
LLFontGlyphInfo::LLFontGlyphInfo(U32 index)
: mGlyphIndex(index),
mWidth(0), // In pixels
mHeight(0), // In pixels
mXAdvance(0.f), // In pixels
mYAdvance(0.f), // In pixels
James Cook
committed
mXBitmapOffset(0), // Offset to the origin in the bitmap
mYBitmapOffset(0), // Offset to the origin in the bitmap
mXBearing(0), // Distance from baseline to left in pixels
mYBearing(0), // Distance from baseline to top in pixels
mBitmapNum(0) // Which bitmap in the bitmap cache contains this glyph
James Cook
committed
{
}
LLFontFreetype::LLFontFreetype()
: LLTrace::MemTrackable<LLFontFreetype>("LLFontFreetype"),
mFontBitmapCachep(new LLFontBitmapCache),
James Cook
committed
mAscender(0.f),
mDescender(0.f),
mLineHeight(0.f),
#ifdef LL_WINDOWS
pFileStream(NULL),
pFtStream(NULL),
#endif
James Cook
committed
mIsFallback(FALSE),
mFTFace(NULL),
mRenderGlyphCount(0),
mAddGlyphCount(0),
James Cook
committed
mPointSize(0)
{
mCharGlyphInfoMap.reserve(500);
mKerningCache.reserve(500);
James Cook
committed
}
LLFontFreetype::~LLFontFreetype()
{
// Clean up freetype libs.
if (mFTFace)
FT_Done_Face(mFTFace);
mFTFace = NULL;
// Delete glyph info
std::for_each(mCharGlyphInfoMap.begin(), mCharGlyphInfoMap.end(), DeletePairedPointer());
mCharGlyphInfoMap.clear();
James Cook
committed
#ifdef LL_WINDOWS
delete pFileStream; // closed by FT_Done_Face
delete pFtStream;
#endif
delete mFontBitmapCachep;
James Cook
committed
// mFallbackFonts cleaned up by LLPointer destructor
}
#ifdef LL_WINDOWS
unsigned long ft_read_cb(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count) {
if (count <= 0) return count;
llifstream *file_stream = static_cast<llifstream *>(stream->descriptor.pointer);
file_stream->seekg(offset, std::ios::beg);
file_stream->read((char*)buffer, count);
return file_stream->gcount();
}
void ft_close_cb(FT_Stream stream) {
llifstream *file_stream = static_cast<llifstream *>(stream->descriptor.pointer);
file_stream->close();
}
#endif
maxim_productengine
committed
BOOL LLFontFreetype::loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, S32 components, BOOL is_fallback, S32 face_n)
James Cook
committed
{
// Don't leak face objects. This is also needed to deal with
// changed font file names.
if (mFTFace)
{
FT_Done_Face(mFTFace);
mFTFace = NULL;
}
Richard Linden
committed
James Cook
committed
int error;
andreykproductengine
committed
#ifdef LL_WINDOWS
maxim_productengine
committed
error = ftOpenFace(filename, face_n);
andreykproductengine
committed
#else
James Cook
committed
error = FT_New_Face( gFTLibrary,
filename.c_str(),
0,
andreykproductengine
committed
&mFTFace);
#endif
James Cook
committed
andreykproductengine
committed
if (error)
James Cook
committed
{
#ifdef LL_WINDOWS
maxim_productengine
committed
clearFontStreams();
James Cook
committed
return FALSE;
}
mIsFallback = is_fallback;
F32 pixels_per_em = (point_size / 72.f)*vert_dpi; // Size in inches * dpi
error = FT_Set_Char_Size(mFTFace, /* handle to face object */
0, /* char_width in 1/64th of points */
(S32)(point_size*64), /* char_height in 1/64th of points */
(U32)horz_dpi, /* horizontal device resolution */
(U32)vert_dpi); /* vertical device resolution */
if (error)
{
// Clean up freetype libs.
FT_Done_Face(mFTFace);
#ifdef LL_WINDOWS
maxim_productengine
committed
clearFontStreams();
James Cook
committed
mFTFace = NULL;
return FALSE;
}
F32 y_max, y_min, x_max, x_min;
F32 ems_per_unit = 1.f/ mFTFace->units_per_EM;
F32 pixels_per_unit = pixels_per_em * ems_per_unit;
// Get size of bbox in pixels
y_max = mFTFace->bbox.yMax * pixels_per_unit;
y_min = mFTFace->bbox.yMin * pixels_per_unit;
x_max = mFTFace->bbox.xMax * pixels_per_unit;
x_min = mFTFace->bbox.xMin * pixels_per_unit;
mAscender = mFTFace->ascender * pixels_per_unit;
mDescender = -mFTFace->descender * pixels_per_unit;
mLineHeight = mFTFace->height * pixels_per_unit;
callum_linden
committed
S32 max_char_width = ll_round(0.5f + (x_max - x_min));
S32 max_char_height = ll_round(0.5f + (y_max - y_min));
James Cook
committed
mFontBitmapCachep->init(components, max_char_width, max_char_height);
claimMem(mFontBitmapCachep);
James Cook
committed
if (!mFTFace->charmap)
{
//LL_INFOS() << " no unicode encoding, set whatever encoding there is..." << LL_ENDL;
James Cook
committed
FT_Set_Charmap(mFTFace, mFTFace->charmaps[0]);
}
if (!mIsFallback)
{
// Add the default glyph
Richard Nelson
committed
addGlyphFromFont(this, 0, 0);
James Cook
committed
}
mName = filename;
James Cook
committed
mPointSize = point_size;
mStyle = LLFontGL::NORMAL;
if(mFTFace->style_flags & FT_STYLE_FLAG_BOLD)
{
mStyle |= LLFontGL::BOLD;
}
if(mFTFace->style_flags & FT_STYLE_FLAG_ITALIC)
{
mStyle |= LLFontGL::ITALIC;
}
James Cook
committed
return TRUE;
}
maxim_productengine
committed
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
S32 LLFontFreetype::getNumFaces(const std::string& filename)
{
if (mFTFace)
{
FT_Done_Face(mFTFace);
mFTFace = NULL;
}
S32 num_faces = 1;
#ifdef LL_WINDOWS
int error = ftOpenFace(filename, 0);
if (error)
{
return 0;
}
else
{
num_faces = mFTFace->num_faces;
}
FT_Done_Face(mFTFace);
clearFontStreams();
mFTFace = NULL;
#endif
return num_faces;
}
#ifdef LL_WINDOWS
S32 LLFontFreetype::ftOpenFace(const std::string& filename, S32 face_n)
{
S32 error = -1;
pFileStream = new llifstream(filename, std::ios::binary);
if (pFileStream->is_open())
{
std::streampos beg = pFileStream->tellg();
pFileStream->seekg(0, std::ios::end);
std::streampos end = pFileStream->tellg();
std::size_t file_size = end - beg;
pFileStream->seekg(0, std::ios::beg);
pFtStream = new LLFT_Stream();
pFtStream->base = 0;
pFtStream->pos = 0;
pFtStream->size = file_size;
pFtStream->descriptor.pointer = pFileStream;
pFtStream->read = ft_read_cb;
pFtStream->close = ft_close_cb;
FT_Open_Args args;
args.flags = FT_OPEN_STREAM;
args.stream = (FT_StreamRec*)pFtStream;
error = FT_Open_Face(gFTLibrary, &args, face_n, &mFTFace);
}
return error;
}
void LLFontFreetype::clearFontStreams()
{
if (pFileStream)
{
pFileStream->close();
}
delete pFileStream;
delete pFtStream;
pFileStream = NULL;
pFtStream = NULL;
}
#endif
James Cook
committed
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
void LLFontFreetype::setFallbackFonts(const font_vector_t &font)
{
mFallbackFonts = font;
}
const LLFontFreetype::font_vector_t &LLFontFreetype::getFallbackFonts() const
{
return mFallbackFonts;
}
F32 LLFontFreetype::getLineHeight() const
{
return mLineHeight;
}
F32 LLFontFreetype::getAscenderHeight() const
{
return mAscender;
}
F32 LLFontFreetype::getDescenderHeight() const
{
return mDescender;
}
F32 LLFontFreetype::getXAdvance(llwchar wch) const
{
if (mFTFace == NULL)
return 0.0;
// Return existing info only if it is current
LLFontGlyphInfo* gi = getGlyphInfo(wch);
Richard Nelson
committed
if (gi)
James Cook
committed
{
return gi->mXAdvance;
}
else
{
char_glyph_info_map_t::iterator found_it = mCharGlyphInfoMap.find((llwchar)0);
if (found_it != mCharGlyphInfoMap.end())
James Cook
committed
{
James Cook
committed
}
}
// Last ditch fallback - no glyphs defined at all.
return (F32)mFontBitmapCachep->getMaxCharWidth();
}
F32 LLFontFreetype::getXAdvance(const LLFontGlyphInfo* glyph) const
{
if (mFTFace == NULL)
return 0.0;
return glyph->mXAdvance;
}
James Cook
committed
F32 LLFontFreetype::getXKerning(llwchar char_left, llwchar char_right) const
{
if (mFTFace == NULL)
return 0.0;
Richard Nelson
committed
LLFontGlyphInfo* left_glyph_info = getGlyphInfo(char_left);;
James Cook
committed
U32 left_glyph = left_glyph_info ? left_glyph_info->mGlyphIndex : 0;
// Kern this puppy.
Richard Nelson
committed
LLFontGlyphInfo* right_glyph_info = getGlyphInfo(char_right);
James Cook
committed
U32 right_glyph = right_glyph_info ? right_glyph_info->mGlyphIndex : 0;
F32 kerning = 0.0f;
if (getKerningCache(left_glyph, right_glyph, kerning))
return kerning;
James Cook
committed
FT_Vector delta;
llverify(!FT_Get_Kerning(mFTFace, left_glyph, right_glyph, FT_KERNING_DEFAULT, &delta));
James Cook
committed
kerning = delta.x*(1.f/64.f);
setKerningCache(left_glyph, right_glyph, kerning);
return kerning;
James Cook
committed
}
F32 LLFontFreetype::getXKerning(const LLFontGlyphInfo* left_glyph_info, const LLFontGlyphInfo* right_glyph_info) const
{
return 0.0;
U32 left_glyph = left_glyph_info ? left_glyph_info->mGlyphIndex : 0;
U32 right_glyph = right_glyph_info ? right_glyph_info->mGlyphIndex : 0;
F32 kerning = 0.0f;
if (getKerningCache(left_glyph, right_glyph, kerning))
return kerning;
FT_Vector delta;
llverify(!FT_Get_Kerning(mFTFace, left_glyph, right_glyph, FT_KERNING_DEFAULT, &delta));
kerning = delta.x*(1.f/64.f);
setKerningCache(left_glyph, right_glyph, kerning);
return kerning;
James Cook
committed
BOOL LLFontFreetype::hasGlyph(llwchar wch) const
{
llassert(!mIsFallback);
Richard Nelson
committed
return(mCharGlyphInfoMap.find(wch) != mCharGlyphInfoMap.end());
James Cook
committed
}
Richard Nelson
committed
LLFontGlyphInfo* LLFontFreetype::addGlyph(llwchar wch) const
James Cook
committed
{
if (mFTFace == NULL)
return FALSE;
llassert(!mIsFallback);
//LL_DEBUGS() << "Adding new glyph for " << wch << " to font" << LL_ENDL;
James Cook
committed
FT_UInt glyph_index;
// Initialize char to glyph map
glyph_index = FT_Get_Char_Index(mFTFace, wch);
if (glyph_index == 0)
{
//LL_INFOS() << "Trying to add glyph from fallback font!" << LL_ENDL;
for (const auto& fallback_fontp : mFallbackFonts)
{
glyph_index = FT_Get_Char_Index(fallback_fontp->mFTFace, wch);
James Cook
committed
if (glyph_index)
{
return addGlyphFromFont(fallback_fontp, wch, glyph_index);
James Cook
committed
}
}
}
char_glyph_info_map_t::iterator iter = mCharGlyphInfoMap.find(wch);
Richard Nelson
committed
if (iter == mCharGlyphInfoMap.end())
James Cook
committed
{
Richard Nelson
committed
return addGlyphFromFont(this, wch, glyph_index);
James Cook
committed
}
Richard Nelson
committed
return NULL;
James Cook
committed
}
Richard Nelson
committed
LLFontGlyphInfo* LLFontFreetype::addGlyphFromFont(const LLFontFreetype *fontp, llwchar wch, U32 glyph_index) const
James Cook
committed
{
if (mFTFace == NULL)
Richard Nelson
committed
return NULL;
James Cook
committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
llassert(!mIsFallback);
fontp->renderGlyph(glyph_index);
S32 width = fontp->mFTFace->glyph->bitmap.width;
S32 height = fontp->mFTFace->glyph->bitmap.rows;
S32 pos_x, pos_y;
S32 bitmap_num;
mFontBitmapCachep->nextOpenPos(width, pos_x, pos_y, bitmap_num);
mAddGlyphCount++;
LLFontGlyphInfo* gi = new LLFontGlyphInfo(glyph_index);
gi->mXBitmapOffset = pos_x;
gi->mYBitmapOffset = pos_y;
gi->mBitmapNum = bitmap_num;
gi->mWidth = width;
gi->mHeight = height;
gi->mXBearing = fontp->mFTFace->glyph->bitmap_left;
gi->mYBearing = fontp->mFTFace->glyph->bitmap_top;
// Convert these from 26.6 units to float pixels.
gi->mXAdvance = fontp->mFTFace->glyph->advance.x / 64.f;
gi->mYAdvance = fontp->mFTFace->glyph->advance.y / 64.f;
insertGlyphInfo(wch, gi);
llassert(fontp->mFTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO
|| fontp->mFTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY);
if (fontp->mFTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO
|| fontp->mFTFace->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY)
{
U8 *buffer_data = fontp->mFTFace->glyph->bitmap.buffer;
S32 buffer_row_stride = fontp->mFTFace->glyph->bitmap.pitch;
U8 *tmp_graydata = NULL;
if (fontp->mFTFace->glyph->bitmap.pixel_mode
== FT_PIXEL_MODE_MONO)
{
// need to expand 1-bit bitmap to 8-bit graymap.
tmp_graydata = new U8[width * height];
S32 xpos, ypos;
for (ypos = 0; ypos < height; ++ypos)
{
S32 bm_row_offset = buffer_row_stride * ypos;
for (xpos = 0; xpos < width; ++xpos)
{
U32 bm_col_offsetbyte = xpos / 8;
U32 bm_col_offsetbit = 7 - (xpos % 8);
U32 bit =
!!(buffer_data[bm_row_offset
+ bm_col_offsetbyte
] & (1 << bm_col_offsetbit) );
tmp_graydata[width*ypos + xpos] =
255 * bit;
}
}
// use newly-built graymap.
buffer_data = tmp_graydata;
buffer_row_stride = width;
}
switch (mFontBitmapCachep->getNumComponents())
{
case 1:
mFontBitmapCachep->getImageRaw(bitmap_num)->setSubImage(pos_x,
pos_y,
width,
height,
buffer_data,
buffer_row_stride,
TRUE);
break;
case 2:
setSubImageLuminanceAlpha(pos_x,
pos_y,
bitmap_num,
width,
height,
buffer_data,
buffer_row_stride);
break;
default:
break;
}
if (tmp_graydata)
delete[] tmp_graydata;
} else {
// we don't know how to handle this pixel format from FreeType;
// omit it from the font-image.
}
Richard Nelson
committed
LLImageGL *image_gl = mFontBitmapCachep->getImageGL(bitmap_num);
LLImageRaw *image_raw = mFontBitmapCachep->getImageRaw(bitmap_num);
image_gl->setSubImage(image_raw, 0, 0, image_gl->getWidth(), image_gl->getHeight());
return gi;
James Cook
committed
}
LLFontGlyphInfo* LLFontFreetype::getGlyphInfo(llwchar wch) const
{
char_glyph_info_map_t::iterator iter = mCharGlyphInfoMap.find(wch);
if (iter != mCharGlyphInfoMap.end())
{
return iter->second;
}
Richard Nelson
committed
else
{
// this glyph doesn't yet exist, so render it and return the result
return addGlyph(wch);
}
James Cook
committed
}
void LLFontFreetype::insertGlyphInfo(llwchar wch, LLFontGlyphInfo* gi) const
{
char_glyph_info_map_t::iterator iter = mCharGlyphInfoMap.find(wch);
if (iter != mCharGlyphInfoMap.end())
{
delete iter->second;
iter->second = gi;
}
else
{
James Cook
committed
mCharGlyphInfoMap[wch] = gi;
}
}
void LLFontFreetype::renderGlyph(U32 glyph_index) const
{
if (mFTFace == NULL)
return;
llassert_always(! FT_Load_Glyph(mFTFace, glyph_index, FT_LOAD_DEFAULT | FT_LOAD_RENDER | FT_LOAD_TARGET_LIGHT) );
James Cook
committed
mRenderGlyphCount++;
}
void LLFontFreetype::reset(F32 vert_dpi, F32 horz_dpi)
{
loadFace(mName, mPointSize, vert_dpi ,horz_dpi, mFontBitmapCachep->getNumComponents(), mIsFallback);
James Cook
committed
if (!mIsFallback)
{
// This is the head of the list - need to rebuild ourself and all fallbacks.
if (mFallbackFonts.empty())
{
LL_WARNS() << "LLFontGL::reset(), no fallback fonts present" << LL_ENDL;
James Cook
committed
}
else
{
for (auto& font : mFallbackFonts)
{
font->reset(vert_dpi, horz_dpi);
James Cook
committed
}
}
}
}
void LLFontFreetype::resetBitmapCache()
{
for (auto& glyph_pair : mCharGlyphInfoMap)
{
disclaimMem(glyph_pair.second);
delete glyph_pair.second;
Richard Nelson
committed
mCharGlyphInfoMap.clear();
disclaimMem(mFontBitmapCachep);
James Cook
committed
mFontBitmapCachep->reset();
// Adding default glyph is skipped for fallback fonts here as well as in loadFace().
// This if was added as fix for EXT-4971.
if(!mIsFallback)
{
// Add the empty glyph
addGlyphFromFont(this, 0, 0);
}
James Cook
committed
}
void LLFontFreetype::destroyGL()
{
mFontBitmapCachep->destroyGL();
}
const std::string &LLFontFreetype::getName() const
{
return mName;
}
const LLFontBitmapCache* LLFontFreetype::getFontBitmapCache() const
James Cook
committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
{
return mFontBitmapCachep;
}
void LLFontFreetype::setStyle(U8 style)
{
mStyle = style;
}
U8 LLFontFreetype::getStyle() const
{
return mStyle;
}
void LLFontFreetype::setSubImageLuminanceAlpha(U32 x, U32 y, U32 bitmap_num, U32 width, U32 height, U8 *data, S32 stride) const
{
LLImageRaw *image_raw = mFontBitmapCachep->getImageRaw(bitmap_num);
llassert(!mIsFallback);
llassert(image_raw && (image_raw->getComponents() == 2));
U8 *target = image_raw->getData();
if (!data)
{
return;
}
if (0 == stride)
stride = width;
U32 i, j;
U32 to_offset;
U32 from_offset;
U32 target_width = image_raw->getWidth();
for (i = 0; i < height; i++)
{
to_offset = (y + i)*target_width + x;
from_offset = (height - 1 - i)*stride;
for (j = 0; j < width; j++)
{
*(target + to_offset*2 + 1) = *(data + from_offset);
to_offset++;
from_offset++;
}
}
}
static inline U64 kerning_cache_key(const U32 left_glyph, const U32 right_glyph)
{
return (((U64)left_glyph) << 32) | right_glyph;
}
bool LLFontFreetype::getKerningCache(U32 left_glyph, U32 right_glyph, F32& kerning) const
{
auto const& iter = mKerningCache.find(kerning_cache_key(left_glyph, right_glyph));
if (iter == mKerningCache.cend())
return false;
kerning = iter->second;
return true;
}
void LLFontFreetype::setKerningCache(U32 left_glyph, U32 right_glyph, F32 kerning) const
{
mKerningCache.emplace(kerning_cache_key(left_glyph, right_glyph), kerning);
}