00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifdef _MSC_VER
00040 # pragma warning (disable: 4786)
00041 #endif
00042
00043 #include "OSGTextFT2Backend.h"
00044 #include <OSGBaseTypes.h>
00045
00046
00047 #ifdef FT2_LIB
00048
00049
00050 #include "OSGTextPixmapFace.h"
00051 #include "OSGTextPixmapGlyph.h"
00052 #include "OSGTextVectorFace.h"
00053 #include "OSGTextVectorGlyph.h"
00054 #include "OSGTextTXFFace.h"
00055 #include "OSGTextTXFGlyph.h"
00056 #include "OSGTextLayoutParam.h"
00057 #include "OSGTextLayoutResult.h"
00058
00059 #include <iostream>
00060 #include <algorithm>
00061 #include <set>
00062 #ifdef FONTCONFIG_LIB
00063 # include <fontconfig/fontconfig.h>
00064 #else
00065 # ifdef _WIN32
00066 # include <windows.h>
00067 # else
00068 # include <dirent.h>
00069 # endif
00070 #endif
00071
00072
00073 #ifdef _MSC_VER
00074 # include <freetype/ftoutln.h>
00075 #else
00076 # include FT_OUTLINE_H
00077 #endif
00078
00079
00080 using namespace std;
00081
00082
00083 OSG_BEGIN_NAMESPACE
00084
00085
00086
00087
00088
00089
00090 class TextFT2VectorFace: public TextVectorFace
00091 {
00092 public:
00093
00094
00095 TextFT2VectorFace(FT_Face face);
00096
00097
00098 virtual ~TextFT2VectorFace();
00099
00100
00101 virtual void layout(const wstring &text, const TextLayoutParam ¶m,
00102 TextLayoutResult &layoutResult);
00103
00104 protected:
00105
00106
00107 virtual auto_ptr<TextVectorGlyph> createGlyph(TextGlyph::Index glyphIndex);
00108
00109 private:
00110
00111
00112 FT_Face _face;
00113 };
00114
00115
00116
00117
00118
00119
00120 class TextFT2VectorGlyph: public TextVectorGlyph
00121 {
00122 public:
00123
00124
00125 TextFT2VectorGlyph(Index glyphIndex, Real32 scale, FT_GlyphSlot glyphSlot);
00126
00127
00128 virtual ~TextFT2VectorGlyph();
00129 };
00130
00131
00132
00133
00134
00135
00136 class TextFT2PixmapFace: public TextPixmapFace
00137 {
00138 public:
00139
00140
00141 TextFT2PixmapFace(FT_Face face, UInt32 size);
00142
00143
00144 virtual ~TextFT2PixmapFace();
00145
00146
00147 virtual void layout(const wstring &text, const TextLayoutParam ¶m,
00148 TextLayoutResult &layoutResult);
00149
00150 protected:
00151
00152
00153 virtual auto_ptr<TextPixmapGlyph> createGlyph(TextGlyph::Index glyphIndex);
00154
00155 private:
00156
00157
00158 FT_Face _face;
00159 };
00160
00161
00162
00163
00164
00165
00166 class TextFT2PixmapGlyph: public TextPixmapGlyph
00167 {
00168 public:
00169
00170
00171 TextFT2PixmapGlyph(Index glyphIndex, TextFT2PixmapFace *face, FT_GlyphSlot glyphSlot);
00172
00173
00174 virtual ~TextFT2PixmapGlyph();
00175 };
00176
00177
00178
00179
00180
00181
00182 class TextFT2TXFFace: public TextTXFFace
00183 {
00184 public:
00185
00186
00187 TextFT2TXFFace(FT_Face face, const TextTXFParam ¶m);
00188
00189
00190 virtual ~TextFT2TXFFace();
00191 };
00192
00193
00194
00195
00196
00197
00198 class TextFT2TXFGlyph: public TextTXFGlyph
00199 {
00200 public:
00201
00202
00203 TextFT2TXFGlyph(Index glyphIndex, TextFT2TXFFace *face,
00204 Real32 scale, FT_GlyphSlot glyphSlot);
00205
00206
00207 virtual ~TextFT2TXFGlyph();
00208 };
00209
00210
00211
00212
00213
00214
00215 TextFT2Backend::TextFT2Backend()
00216 : TextBackend(), _library(0)
00217 #ifndef FONTCONFIG_LIB
00218 ,
00219 # ifdef FONT_SEARCHPATH
00220 _pathList(FONT_SEARCHPATH),
00221 #else
00222 _pathList(),
00223 #endif
00224 _scanForFonts(true), _fontMap()
00225 #endif
00226 {
00227
00228 FT_Error error = FT_Init_FreeType(&_library);
00229 if (error)
00230
00231
00232 _library = 0;
00233
00234 #ifdef FONTCONFIG_LIB
00235
00236
00237 FcInit();
00238
00239
00240 #endif // FONTCONFIG_LIB
00241
00242 }
00243
00244
00245
00246
00247
00248
00249 TextFT2Backend::~TextFT2Backend()
00250 {
00251
00252 if (_library != 0)
00253 FT_Done_FreeType(_library);
00254
00255 #ifdef FONTCONFIG_LIB
00256
00257
00258
00259
00260
00261
00262
00263 #endif // FONTCONFIG_LIB
00264 }
00265
00266
00267
00268
00269
00270
00271 bool TextFT2Backend::findPath(const string &family, TextFace::Style style,
00272 string &path, int &index)
00273 {
00274
00275 path.erase();
00276 index = -1;
00277
00278 #ifdef FONTCONFIG_LIB
00279
00280
00281 string f;
00282 if (family == "SERIF")
00283 f = "serif";
00284 else if (family == "SANS")
00285 f = "sans-serif";
00286 else if (family == "TYPEWRITER")
00287 f = "monospace";
00288 else
00289 f = family;
00290
00291
00292 FcPattern *pattern = FcPatternCreate();
00293 if (pattern == 0)
00294 return false;
00295
00296
00297 if (FcPatternAddBool(pattern, FC_OUTLINE, FcTrue) == FcFalse)
00298 {
00299 FcPatternDestroy(pattern);
00300 return false;
00301 }
00302
00303
00304 if (FcPatternAddString(pattern, FC_FAMILY, (const FcChar8*)f.c_str()) == FcFalse)
00305 {
00306 FcPatternDestroy(pattern);
00307 return false;
00308 }
00309
00310
00311 int slant;
00312 int weight;
00313 switch (style)
00314 {
00315 default:
00316 FWARNING(("Invalid font style parameter.\n"));
00317
00318 case TextFace::STYLE_PLAIN:
00319 slant = FC_SLANT_ROMAN;
00320 weight = FC_WEIGHT_MEDIUM;
00321 break;
00322 case TextFace::STYLE_BOLD:
00323 slant = FC_SLANT_ROMAN;
00324 weight = FC_WEIGHT_BOLD;
00325 break;
00326 case TextFace::STYLE_ITALIC:
00327 slant = FC_SLANT_ITALIC;
00328 weight = FC_WEIGHT_MEDIUM;
00329 break;
00330 case TextFace::STYLE_BOLDITALIC:
00331 slant = FC_SLANT_ITALIC;
00332 weight = FC_WEIGHT_BOLD;
00333 break;
00334 }
00335 if (FcPatternAddInteger(pattern, FC_SLANT, slant) == FcFalse)
00336 {
00337 FcPatternDestroy(pattern);
00338 return false;
00339 }
00340 if (FcPatternAddInteger(pattern, FC_WEIGHT, weight) == FcFalse)
00341 {
00342 FcPatternDestroy(pattern);
00343 return false;
00344 }
00345
00346
00347 if (FcConfigSubstitute(0, pattern, FcMatchPattern) == FcFalse)
00348 {
00349 FcPatternDestroy(pattern);
00350 return false;
00351 }
00352 FcDefaultSubstitute(pattern);
00353
00354
00355 FcResult result;
00356 FcPattern *match = FcFontMatch(0, pattern, &result);
00357
00358 FcPatternDestroy(pattern);
00359 if (match == 0)
00360 return false;
00361
00362
00363 FcChar8 *pathPtr;
00364 if (FcPatternGetString(match, FC_FILE, 0, &pathPtr) != FcResultMatch)
00365 {
00366 FcPatternDestroy(match);
00367 return false;
00368 }
00369 path = (const char *)pathPtr;
00370
00371
00372 if (FcPatternGetInteger(match, FC_INDEX, 0, &index) != FcResultMatch)
00373 {
00374 FcPatternDestroy(match);
00375 return false;
00376 }
00377
00378
00379 FcPatternDestroy(match);
00380
00381 #else // !FONTCONFIG_LIB
00382
00383
00384 scanForFonts();
00385
00386
00387 FontMap::const_iterator it, bestMatchIt = _fontMap.end();
00388 pair<FontMap::const_iterator, FontMap::const_iterator> range;
00389
00390
00391 range = _fontMap.equal_range(family);
00392 for (it = range.first; it != range.second; ++it)
00393 {
00394 if (it->second.style == style)
00395 {
00396 bestMatchIt = it;
00397 break;
00398 }
00399 }
00400 if (bestMatchIt == _fontMap.end())
00401 return false;
00402 path = bestMatchIt->second.path;
00403 index = bestMatchIt->second.index;
00404
00405 #endif // !FONTCONFIG_LIB
00406
00407 return true;
00408 }
00409
00410
00411
00412
00413
00414
00415 FT_Face TextFT2Backend::createFace(const string &family, TextFace::Style style, FT_UInt size)
00416 {
00417
00418 if (_library == 0)
00419 return 0;
00420
00421
00422 string path;
00423 int index;
00424 if (findPath(family, style, path, index) == false)
00425 return 0;
00426
00427
00428 FT_Face face;
00429 FT_Error error = FT_New_Face(_library, path.c_str(), index, &face);
00430 if (error)
00431 return 0;
00432
00433
00434
00435 if(face->family_name == NULL)
00436 face->family_name = strdup(family.c_str());
00437
00438
00439
00440
00441 error = FT_Select_Charmap(face, FT_ENCODING_UNICODE);
00442
00443
00444
00445 if (size > 0)
00446 error = FT_Set_Pixel_Sizes(face, 0, size);
00447
00448
00449
00450 string::size_type pos = path.rfind('.');
00451 if (pos != string::npos)
00452 {
00453 string suffix = path.substr(pos + 1);
00454 transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
00455 if ((suffix == "pfb") || (suffix == "pfa"))
00456 {
00457 string addPath = path.substr(0, pos + 1);
00458 addPath.append("afm");
00459 error = FT_Attach_File(face, addPath.c_str());
00460
00461 }
00462 }
00463
00464 return face;
00465 }
00466
00467
00468
00469
00470
00471
00472 TextVectorFace*
00473 TextFT2Backend::createVectorFace(const string &family, TextFace::Style style)
00474 {
00475
00476 FT_Face face = createFace(family, style, 0);
00477 if (face == 0)
00478 return 0;
00479
00480
00481 return new TextFT2VectorFace(face);
00482 }
00483
00484
00485
00486
00487
00488
00489 TextPixmapFace*
00490 TextFT2Backend::createPixmapFace(const string &family, TextFace::Style style, UInt32 size)
00491 {
00492
00493 FT_Face face = createFace(family, style, size);
00494 if (face == 0)
00495 return 0;
00496
00497
00498 return new TextFT2PixmapFace(face, size);
00499 }
00500
00501
00502
00503
00504
00505
00506 TextTXFFace*
00507 TextFT2Backend::createTXFFace(const string &family, TextFace::Style style, const TextTXFParam ¶m)
00508 {
00509
00510 FT_Face face = createFace(family, style, param.size);
00511 if (face == 0)
00512 return 0;
00513
00514
00515 return new TextFT2TXFFace(face, param);
00516 }
00517
00518
00519
00520
00521
00522
00523 void TextFT2Backend::getFontFamilies(vector<string> &families)
00524 {
00525
00526 families.clear();
00527
00528 #ifdef FONTCONFIG_LIB
00529
00530
00531 FcPattern *pattern = FcPatternCreate();
00532 if (pattern == 0)
00533 return;
00534
00535
00536 FcObjectSet *objectSet = FcObjectSetBuild(FC_FAMILY, 0);
00537 if (objectSet == 0)
00538 {
00539 FcPatternDestroy(pattern);
00540 return;
00541 }
00542
00543
00544 FcFontSet *fontSet = FcFontList(0, pattern, objectSet);
00545 if (fontSet != 0)
00546 {
00547 int i;
00548 set<string> familySet;
00549 for (i = 0; i < fontSet->nfont; ++i)
00550 {
00551 FcChar8 *family;
00552 if (FcPatternGetString(fontSet->fonts[i], FC_FAMILY, 0, &family) == FcResultMatch)
00553 familySet.insert(reinterpret_cast<char*>(family));
00554 }
00555
00556
00557 families.resize(familySet.size());
00558 copy(familySet.begin(), familySet.end(), families.begin());
00559 FcFontSetDestroy(fontSet);
00560 }
00561
00562
00563 FcObjectSetDestroy(objectSet);
00564 FcPatternDestroy(pattern);
00565
00566 #else // !FONTCONFIG_LIB
00567
00568
00569 scanForFonts();
00570
00571
00572 families.reserve(_fontMap.size());
00573 FontMap::const_iterator it;
00574 for (it = _fontMap.begin(); it != _fontMap.end(); ++it)
00575 families.push_back(it->first);
00576 unique(families.begin(), families.end());
00577
00578 #endif // !FONTCONFIG_LIB
00579 }
00580
00581
00582
00583
00584
00585
00586 #ifndef FONTCONFIG_LIB
00587 void TextFT2Backend::scanForFonts()
00588 {
00589
00590 if (_scanForFonts == true)
00591 {
00592 _scanForFonts = false;
00593
00594
00595 string::size_type start = 0;
00596 while (true)
00597 {
00598 #ifdef _WIN32
00599 string::size_type end = _pathList.find(';', start);
00600 #else
00601 string::size_type end = _pathList.find(':', start);
00602 #endif
00603 string path = end == string::npos ? _pathList.substr(start) : _pathList.substr(start, end - start);
00604
00605
00606 string::size_type wsPos = path.find_first_not_of(" \t\r\n");
00607 if (wsPos != string::npos)
00608 {
00609 path.erase(0, wsPos);
00610 wsPos = path.find_last_not_of(" \t\r\n");
00611 path.erase(wsPos + 1);
00612
00613
00614 scanDir(path);
00615 }
00616
00617 if (end == string::npos)
00618 break;
00619 start = end + 1;
00620 }
00621 }
00622 }
00623 #endif // !FONTCONFIG_LIB
00624
00625
00626
00627
00628
00629
00630 #ifndef FONTCONFIG_LIB
00631 void TextFT2Backend::scanDir(const string &path)
00632 {
00633 #ifdef _WIN32
00634
00635
00636 string p = path;
00637 if ((p.length() < 1) || ((p[p.length() - 1] != '/') && (p[p.length() - 1] != '\\')))
00638 p.append(1, '\\');
00639
00640
00641 string pattern = p;
00642 pattern.append(1, '*');
00643 WIN32_FIND_DATA findFileData;
00644 HANDLE hFind = FindFirstFile(pattern.c_str(), &findFileData);
00645 if (hFind == INVALID_HANDLE_VALUE)
00646 return;
00647
00648 while (true)
00649 {
00650
00651 string name = findFileData.cFileName;
00652 string fullname = p;
00653 fullname.append(name);
00654
00655 if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00656 {
00657
00658 if ((name != ".") && (name != ".."))
00659 scanDir(fullname);
00660 }
00661 else
00662
00663 checkFile(fullname);
00664
00665
00666 if (FindNextFile(hFind, &findFileData) == FALSE)
00667 break;
00668 }
00669
00670
00671 FindClose(hFind);
00672
00673 #else // !_WIN32
00674
00675
00676 DIR *dir = opendir(path.c_str());
00677 if (dir == 0)
00678 return;
00679
00680
00681 string p = path;
00682 if ((p.length() < 1) || (p[p.length() - 1] != '/'))
00683 p.append(1, '/');
00684
00685 struct dirent entry, *result;
00686 while (true)
00687 {
00688
00689 if (readdir_r(dir, &entry, &result) != 0)
00690 return;
00691 if (result == 0)
00692 break;
00693
00694
00695 string name = entry.d_name;
00696 string fullname = p;
00697 fullname.append(name);
00698 switch (entry.d_type)
00699 {
00700 case DT_DIR:
00701
00702 if ((name != ".") && (name != ".."))
00703 scanDir(fullname);
00704 break;
00705 case DT_REG:
00706
00707 checkFile(fullname);
00708 break;
00709 default:
00710 break;
00711 }
00712 }
00713
00714
00715 closedir(dir);
00716
00717 #endif // !_WIN32
00718
00719 }
00720 #endif // !FONTCONFIG_LIB
00721
00722
00723
00724
00725
00726
00727 #ifndef FONTCONFIG_LIB
00728 void TextFT2Backend::checkFile(const string &fullname)
00729 {
00730
00731 FT_Long index = 0;
00732 FT_Long numFaces = 1;
00733 while (index < numFaces)
00734 {
00735 FT_Face face;
00736 FT_Error error = FT_New_Face(_library, fullname.c_str(), index, &face);
00737 if (error)
00738 break;
00739
00740
00741 numFaces = face->num_faces;
00742 FontInfo fontInfo;
00743 fontInfo.path = fullname;
00744 fontInfo.index = index;
00745 if (face->style_flags & FT_STYLE_FLAG_BOLD)
00746 fontInfo.style = face->style_flags & FT_STYLE_FLAG_ITALIC ? TextFace::STYLE_BOLDITALIC : TextFace::STYLE_BOLD;
00747 else
00748 fontInfo.style = face->style_flags & FT_STYLE_FLAG_ITALIC ? TextFace::STYLE_ITALIC : TextFace::STYLE_PLAIN;
00749 string family = face->family_name;
00750
00751 _fontMap.insert(FontMap::value_type(family, fontInfo));
00752 FT_Done_Face(face);
00753
00754 ++index;
00755 }
00756 }
00757 #endif // !FONTCONFIG_LIB
00758
00759
00760
00761
00762
00763
00764 static void getFaceInfo(FT_Face face, string &family, TextFace::Style &style)
00765 {
00766
00767 family = face->family_name;
00768
00769
00770 if (face->style_flags & FT_STYLE_FLAG_BOLD)
00771 style = face->style_flags & FT_STYLE_FLAG_ITALIC ? TextFace::STYLE_BOLDITALIC : TextFace::STYLE_BOLD;
00772 else
00773 style = face->style_flags & FT_STYLE_FLAG_ITALIC ? TextFace::STYLE_ITALIC : TextFace::STYLE_PLAIN;
00774 }
00775
00776
00777
00778
00779
00780
00781 TextFT2VectorFace::TextFT2VectorFace(FT_Face face)
00782 : TextVectorFace(), _face(face)
00783 {
00784
00785 getFaceInfo(_face, _family, _style);
00786
00787
00788 _scale = 1.f / static_cast<Real32>(_face->ascender - _face->descender);
00789
00790
00791 _horiAscent = static_cast<Real32>(_face->ascender) * _scale;
00792 _vertAscent = -0.5f;
00793
00794
00795 _horiDescent = static_cast<Real32>(_face->descender) * _scale;
00796 _vertDescent = 0.5f;
00797 }
00798
00799
00800
00801
00802
00803
00804 TextFT2VectorFace::~TextFT2VectorFace()
00805 {
00806
00807 FT_Done_Face(_face);
00808 }
00809
00810
00811
00812
00813
00814
00815 void TextFT2VectorFace::layout(const wstring &text, const TextLayoutParam ¶m,
00816 TextLayoutResult &layoutResult)
00817 {
00818
00819 layoutResult.clear();
00820
00821
00822 FT_UInt previousGlyphIndex = 0;
00823 Vec2f currPos;
00824 size_t i, len = text.length();
00825 layoutResult.indices.reserve(len);
00826 layoutResult.positions.reserve(len);
00827 vector<UInt32> spaceIndices;
00828 bool justify = param.getLength(0) > 0.f;
00829 for (i = 0; i < len; ++i)
00830 {
00831
00832 FT_UInt glyphIndex = FT_Get_Char_Index(_face, text[i]);
00833 const TextGlyph &glyph = getGlyph(glyphIndex);
00834 if ((justify == true) && (text[i] == ' '))
00835 spaceIndices.push_back(layoutResult.indices.size());
00836
00837
00838 Vec2f pos;
00839 if (param.horizontal == true)
00840 {
00841 if (param.leftToRight == true)
00842 {
00843 FT_Vector kerning;
00844 FT_Get_Kerning(_face, previousGlyphIndex, glyphIndex, ft_kerning_unscaled, &kerning);
00845 currPos[0] += static_cast<float>(kerning.x) * _scale;
00846 pos = currPos;
00847 pos[0] += glyph.getHoriBearingX();
00848 pos[1] += glyph.getHoriBearingY();
00849 currPos[0] += glyph.getHoriAdvance();
00850 }
00851 else
00852 {
00853 FT_Vector kerning;
00854 FT_Get_Kerning(_face, glyphIndex, previousGlyphIndex, ft_kerning_unscaled, &kerning);
00855 currPos[0] -= static_cast<float>(kerning.x) * _scale + glyph.getHoriAdvance();
00856 pos = currPos;
00857 pos[0] += glyph.getHoriBearingX();
00858 pos[1] += glyph.getHoriBearingY();
00859 }
00860 previousGlyphIndex = glyphIndex;
00861 }
00862 else
00863 {
00864 if (param.topToBottom == true)
00865 {
00866 pos = currPos;
00867 pos[0] += glyph.getVertBearingX();
00868 pos[1] += glyph.getVertBearingY();
00869 currPos[1] += glyph.getVertAdvance();
00870 }
00871 else
00872 {
00873 currPos[1] -= glyph.getVertAdvance();
00874 pos = currPos;
00875 pos[0] += glyph.getVertBearingX();
00876 pos[1] += glyph.getVertBearingY();
00877 }
00878 }
00879
00880 layoutResult.indices.push_back(glyphIndex);
00881 layoutResult.positions.push_back(pos);
00882 }
00883
00884
00885 if (justify == true)
00886 justifyLine(param, spaceIndices, currPos, layoutResult);
00887
00888
00889 adjustLineOrigin(param, currPos, layoutResult);
00890
00891
00892 if (param.horizontal == true)
00893 layoutResult.textBounds.setValues(osgabs(currPos.x()), _horiAscent - _horiDescent);
00894 else
00895 layoutResult.textBounds.setValues(_vertDescent - _vertAscent, osgabs(currPos.y()));
00896 layoutResult.lineBounds.push_back(layoutResult.textBounds);
00897 }
00898
00899
00900
00901
00902
00903
00904 typedef struct UserData
00905 {
00906 Real32 scale;
00907 Vec2f offset;
00908 TextVectorGlyph::Outline &outline;
00909 inline UserData(Real32 s, const Vec2f &off, TextVectorGlyph::Outline &o)
00910 : scale(s), offset(off), outline(o)
00911 {}
00912 }
00913 UserData;
00914
00915
00916
00917
00918
00919 #if ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR < 2))
00920 static int moveToFunc(FT_Vector *to, void *user)
00921 #else
00922 static int moveToFunc(const FT_Vector *to, void *user)
00923 #endif
00924 {
00925 UserData *userData = reinterpret_cast<UserData*>(user);
00926
00927
00928
00929 if (userData->outline.empty() == false)
00930 if (userData->outline.back().size() < 3)
00931 userData->outline.erase(userData->outline.end() - 1);
00932
00933
00934 userData->outline.push_back(TextVectorGlyph::Contour());
00935
00936
00937 Vec2f p(to->x, to->y);
00938 p *= userData->scale;
00939 p -= userData->offset;
00940 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00941
00942
00943 return 0;
00944 }
00945
00946
00947
00948
00949
00950
00951 #if ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR < 2))
00952 static int lineToFunc(FT_Vector *to, void *user)
00953 #else
00954 static int lineToFunc(const FT_Vector *to, void *user)
00955 #endif
00956 {
00957 UserData *userData = reinterpret_cast<UserData*>(user);
00958
00959
00960 Vec2f p(to->x, to->y);
00961 p *= userData->scale;
00962 p -= userData->offset;
00963 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00964
00965
00966 return 0;
00967 }
00968
00969
00970
00971
00972
00973
00974 #if ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR < 2))
00975 static int conicToFunc(FT_Vector *control, FT_Vector *to, void *user)
00976 #else
00977 static int conicToFunc(const FT_Vector *control, const FT_Vector *to, void *user)
00978 #endif
00979 {
00980 UserData *userData = reinterpret_cast<UserData*>(user);
00981
00982
00983 Vec2f p(control->x, control->y);
00984 p *= userData->scale;
00985 p -= userData->offset;
00986 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_QUAD));
00987 p.setValues(to->x, to->y);
00988 p *= userData->scale;
00989 p -= userData->offset;
00990 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00991
00992
00993 return 0;
00994 }
00995
00996
00997
00998
00999
01000
01001 #if ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR < 2))
01002 static int cubicToFunc(FT_Vector *control1, FT_Vector *control2, FT_Vector *to, void *user)
01003 #else
01004 static int cubicToFunc(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user)
01005 #endif
01006 {
01007 UserData *userData = reinterpret_cast<UserData*>(user);
01008
01009
01010 Vec2f p(control1->x, control1->y);
01011 p *= userData->scale;
01012 p -= userData->offset;
01013 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_CUBIC));
01014 p.setValues(control2->x, control2->y);
01015 p *= userData->scale;
01016 p -= userData->offset;
01017 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_CUBIC));
01018 p.setValues(to->x, to->y);
01019 p *= userData->scale;
01020 p -= userData->offset;
01021 userData->outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
01022
01023
01024 return 0;
01025 }
01026
01027
01028
01029
01030
01031
01032 auto_ptr<TextVectorGlyph> TextFT2VectorFace::createGlyph(TextGlyph::Index glyphIndex)
01033 {
01034
01035 if (glyphIndex == TextGlyph::INVALID_INDEX)
01036 return auto_ptr<TextVectorGlyph>();
01037
01038
01039 FT_Error error = FT_Load_Glyph(_face, glyphIndex, FT_LOAD_NO_SCALE);
01040 if (error)
01041 return auto_ptr<TextVectorGlyph>();
01042
01043
01044 FT_GlyphSlot glyphSlot = _face->glyph;
01045 if (glyphSlot->format != ft_glyph_format_outline)
01046 return auto_ptr<TextVectorGlyph>();
01047
01048
01049 return auto_ptr<TextVectorGlyph>(new TextFT2VectorGlyph(glyphIndex, _scale, glyphSlot));
01050 }
01051
01052
01053
01054
01055
01056
01057 TextFT2VectorGlyph::TextFT2VectorGlyph(Index glyphIndex, float scale, FT_GlyphSlot glyphSlot)
01058 : TextVectorGlyph()
01059 {
01060 _glyphIndex = glyphIndex;
01061 _width = static_cast<Real32>(glyphSlot->metrics.width) * scale;
01062 _height = static_cast<Real32>(glyphSlot->metrics.height) * scale;
01063
01064
01065 _horiAdvance = static_cast<Real32>(glyphSlot->metrics.horiAdvance) * scale;
01066 _horiBearingX = static_cast<Real32>(glyphSlot->metrics.horiBearingX) * scale;
01067 _horiBearingY = static_cast<Real32>(glyphSlot->metrics.horiBearingY) * scale;
01068
01069
01070
01071
01072
01073
01074
01075 _vertAdvance = -_height - 1.f / 10.f;
01076 _vertBearingX = -_width / 2.f;
01077 _vertBearingY = -1.f / 20.f;
01078
01079
01080 FT_Outline_Funcs func_interface =
01081 {
01082 moveToFunc,
01083 lineToFunc,
01084 conicToFunc,
01085 cubicToFunc,
01086 0,
01087 0
01088 };
01089 UserData userData(scale, Vec2f(_horiBearingX, _horiBearingY), _outline);
01090 FT_Outline_Decompose(&(glyphSlot->outline), &func_interface, &userData);
01091
01092
01093
01094 if (_outline.empty() == false)
01095 if (_outline.back().size() < 3)
01096 _outline.erase(_outline.end() - 1);
01097 }
01098
01099
01100
01101
01102
01103
01104 TextFT2VectorGlyph::~TextFT2VectorGlyph() {}
01105
01106
01107
01108
01109
01110
01111 TextFT2PixmapFace::TextFT2PixmapFace(FT_Face face, UInt32 size)
01112 : TextPixmapFace(), _face(face)
01113 {
01114
01115 getFaceInfo(_face, _family, _style);
01116
01117
01118 _size = size;
01119
01120
01121 _horiAscent = static_cast<Real32>(_face->size->metrics.ascender) / 64.f;
01122 _vertAscent = -static_cast<Real32>(_size) / 2.f;
01123
01124
01125 _horiDescent = static_cast<Real32>(_face->size->metrics.descender) / 64.f;
01126 _vertDescent = static_cast<Real32>(_size) / 2.f;
01127 }
01128
01129
01130
01131
01132
01133
01134 TextFT2PixmapFace::~TextFT2PixmapFace()
01135 {
01136
01137 FT_Done_Face(_face);
01138 }
01139
01140
01141
01142
01143
01144
01145 void TextFT2PixmapFace::layout(const wstring &text, const TextLayoutParam ¶m,
01146 TextLayoutResult &layoutResult)
01147 {
01148
01149 layoutResult.clear();
01150
01151
01152 FT_UInt previousGlyphIndex = 0;
01153 Vec2f currPos;
01154 size_t i, len = text.length();
01155 layoutResult.indices.reserve(len);
01156 layoutResult.positions.reserve(len);
01157 vector<UInt32> spaceIndices;
01158 bool justify = param.getLength(0) > 0.f;
01159 for (i = 0; i < len; ++i)
01160 {
01161
01162 FT_UInt glyphIndex = FT_Get_Char_Index(_face, text[i]);
01163 const TextGlyph &glyph = getGlyph(glyphIndex);
01164 if ((justify == true) && (text[i] == ' '))
01165 spaceIndices.push_back(layoutResult.indices.size());
01166
01167
01168 Vec2f pos;
01169 if (param.horizontal == true)
01170 {
01171 if (param.leftToRight == true)
01172 {
01173 FT_Vector kerning;
01174 FT_Get_Kerning(_face, previousGlyphIndex, glyphIndex, ft_kerning_default, &kerning);
01175 currPos[0] += static_cast<float>(kerning.x) / 64.f;
01176 pos[0] = currPos.x() + glyph.getHoriBearingX();
01177 pos[1] = currPos.y() + glyph.getHoriBearingY();
01178 currPos[0] += glyph.getHoriAdvance();
01179 }
01180 else
01181 {
01182 FT_Vector kerning;
01183 FT_Get_Kerning(_face, glyphIndex, previousGlyphIndex, ft_kerning_default, &kerning);
01184 currPos[0] -= static_cast<float>(kerning.x) / 64.f + glyph.getHoriAdvance();
01185 pos[0] = currPos[0] + glyph.getHoriBearingX();
01186 pos[1] = currPos[1] + glyph.getHoriBearingY();
01187 }
01188 previousGlyphIndex = glyphIndex;
01189 }
01190 else
01191 {
01192 if (param.topToBottom == true)
01193 {
01194 pos[0] = currPos.x() + glyph.getVertBearingX();
01195 pos[1] = currPos.y() + glyph.getVertBearingY();
01196 currPos[1] += glyph.getVertAdvance();
01197 }
01198 else
01199 {
01200 currPos[1] -= glyph.getVertAdvance();
01201 pos[0] = currPos.x() + glyph.getVertBearingX();
01202 pos[1] = currPos.y() + glyph.getVertBearingY();
01203 }
01204 }
01205
01206 layoutResult.indices.push_back(glyphIndex);
01207 layoutResult.positions.push_back(pos);
01208 }
01209
01210
01211 if (justify == true)
01212 justifyLine(param, spaceIndices, currPos, layoutResult);
01213
01214
01215 adjustLineOrigin(param, currPos, layoutResult);
01216
01217
01218 if (param.horizontal == true)
01219 layoutResult.textBounds.setValues(osgabs(currPos.x()), _horiAscent - _horiDescent);
01220 else
01221 layoutResult.textBounds.setValues(_vertDescent - _vertAscent, osgabs(currPos.y()));
01222 layoutResult.lineBounds.push_back(layoutResult.textBounds);
01223 }
01224
01225
01226
01227
01228
01229
01230 auto_ptr<TextPixmapGlyph> TextFT2PixmapFace::createGlyph(TextGlyph::Index glyphIndex)
01231 {
01232
01233 if (glyphIndex == TextGlyph::INVALID_INDEX)
01234 return auto_ptr<TextPixmapGlyph>();
01235
01236
01237 FT_Error error = FT_Load_Glyph(_face, glyphIndex, FT_LOAD_DEFAULT);
01238 if (error)
01239 return auto_ptr<TextPixmapGlyph>();
01240
01241
01242 FT_GlyphSlot glyphSlot = _face->glyph;
01243 if (glyphSlot->format != ft_glyph_format_bitmap)
01244 {
01245 error = FT_Render_Glyph(glyphSlot, ft_render_mode_normal);
01246 if (error)
01247 return auto_ptr<TextPixmapGlyph>();
01248 }
01249
01250
01251 return auto_ptr<TextPixmapGlyph>(new TextFT2PixmapGlyph(glyphIndex, this, glyphSlot));
01252 }
01253
01254
01255
01256
01257
01258
01259 TextFT2PixmapGlyph::TextFT2PixmapGlyph(Index glyphIndex, TextFT2PixmapFace *face, FT_GlyphSlot glyphSlot)
01260 : TextPixmapGlyph()
01261 {
01262 _glyphIndex = glyphIndex;
01263 _width = glyphSlot->bitmap.width;
01264 _pitch = glyphSlot->bitmap.pitch;
01265 _height = glyphSlot->bitmap.rows;
01266
01267
01268 _horiAdvance = static_cast<Real32>(glyphSlot->metrics.horiAdvance) / 64.f;
01269 _horiBearingX = glyphSlot->bitmap_left;
01270 _horiBearingY = glyphSlot->bitmap_top;
01271
01272
01273
01274
01275
01276
01277
01278 _vertBearingX = -static_cast<Int32>(_width >> 1);
01279 _vertBearingY = static_cast<Int32>(-(face->getHoriAscent() - face->getHoriDescent()) / 20.f);
01280 if (_vertBearingY > -1)
01281 _vertBearingY = -1;
01282 Int32 vertAdvanceOffset = _vertBearingY << 1;
01283 _vertAdvance = -static_cast<Int32>(_height) + vertAdvanceOffset;
01284
01285 unsigned int size = glyphSlot->bitmap.pitch * glyphSlot->bitmap.rows;
01286 if ((size == 0) || (glyphSlot->bitmap.buffer == 0))
01287 _pixmap = 0;
01288 else
01289 {
01290 _pixmap = new unsigned char[size];
01291 memcpy(_pixmap, glyphSlot->bitmap.buffer, size);
01292 flipPixmap();
01293 }
01294 }
01295
01296
01297
01298
01299
01300
01301 TextFT2PixmapGlyph::~TextFT2PixmapGlyph() {}
01302
01303
01304
01305
01306
01307
01308 TextFT2TXFFace::TextFT2TXFFace(FT_Face face, const TextTXFParam ¶m)
01309 {
01310
01311 getFaceInfo(face, _family, _style);
01312
01313
01314 _param = param;
01315
01316
01317 _horiAscent = static_cast<Real32>(face->size->metrics.ascender) / 64.f;
01318 _vertAscent = -0.5f;
01319
01320
01321 _horiDescent = static_cast<Real32>(face->size->metrics.descender) / 64.f;
01322 _vertDescent = 0.5f;
01323
01324
01325 _scale = 1.f / (_horiAscent - _horiDescent);
01326 _horiAscent *= _scale;
01327 _horiDescent *= _scale;
01328
01329
01330 wstring::const_iterator it;
01331 for (it = param.getCharacters().begin(); it != param.getCharacters().end(); ++it)
01332 {
01333 FT_UInt glyphIndex = FT_Get_Char_Index(face, *it);
01334 if (glyphIndex == 0)
01335 continue;
01336 FT_Error error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
01337 if (error)
01338 continue;
01339 FT_GlyphSlot glyphSlot = face->glyph;
01340 if (glyphSlot->format != ft_glyph_format_bitmap)
01341 {
01342 error = FT_Render_Glyph(glyphSlot, ft_render_mode_normal);
01343 if (error)
01344 continue;
01345 }
01346 _glyphMap.insert(GlyphMap::value_type(*it, new TextFT2TXFGlyph(*it, this, _scale, face->glyph)));
01347 }
01348
01349
01350 prepareTexture(param);
01351 assert(_texture != NullFC);
01352 assert(_texture->getSize() == static_cast<UInt32>(_texture->getWidth() * _texture->getHeight()));
01353
01354
01355 beginEditCP(_texture);
01356 for (it = param.getCharacters().begin(); it != param.getCharacters().end(); ++it)
01357 {
01358 GlyphMap::iterator gIt = _glyphMap.find(*it);
01359 if (gIt == _glyphMap.end())
01360 continue;
01361 assert(gIt->second != 0);
01362 TextTXFGlyph *glyph = gIt->second;
01363 FT_UInt glyphIndex = FT_Get_Char_Index(face, *it);
01364 if (glyphIndex == 0)
01365 continue;
01366 FT_Error error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
01367 if (error)
01368 continue;
01369 FT_GlyphSlot glyphSlot = face->glyph;
01370 if (glyphSlot->format != ft_glyph_format_bitmap)
01371 {
01372 error = FT_Render_Glyph(glyphSlot, ft_render_mode_normal);
01373 if (error)
01374 continue;
01375 }
01376 unsigned char *src = glyphSlot->bitmap.buffer;
01377 if (src == 0)
01378 continue;
01379 int bpl = glyphSlot->bitmap.pitch;
01380 src += bpl * (glyph->getPixmapHeight() - 1);
01381 unsigned char *src2;
01382 UInt8 *dst = _texture->getData() + glyph->getX() + glyph->getY() * _texture->getWidth();
01383 UInt32 dstPitch = _texture->getWidth() - glyph->getPixmapWidth();
01384 UInt32 x, y;
01385 for (y = 0; y < glyph->getPixmapHeight(); ++y)
01386 {
01387 src2 = src;
01388 for (x = 0; x < glyph->getPixmapWidth(); ++x)
01389 *dst++ = *src2++;
01390 src -= bpl;
01391 dst += dstPitch;
01392 }
01393 }
01394 endEditCP(_texture);
01395
01396
01397 FT_Done_Face(face);
01398 }
01399
01400
01401
01402
01403
01404
01405 TextFT2TXFFace::~TextFT2TXFFace() {}
01406
01407
01408
01409
01410
01411
01412 TextFT2TXFGlyph::TextFT2TXFGlyph(Index glyphIndex, TextFT2TXFFace *face,
01413 Real32 scale, FT_GlyphSlot glyphSlot)
01414 : TextTXFGlyph()
01415 {
01416 _glyphIndex = glyphIndex;
01417 _scale = scale;
01418 _width = glyphSlot->bitmap.width;
01419 _height = glyphSlot->bitmap.rows;
01420
01421
01422 _horiAdvance = static_cast<Real32>(glyphSlot->metrics.horiAdvance) / 64.f * _scale;
01423 _horiBearingX = glyphSlot->bitmap_left;
01424 _horiBearingY = glyphSlot->bitmap_top;
01425
01426
01427
01428
01429
01430
01431
01432 _vertBearingX = -static_cast<Int32>(_width >> 1);
01433 if (glyphIndex == 32)
01434 {
01435 _vertBearingY = -_horiBearingX;
01436 _vertAdvance = -_horiAdvance;
01437 }
01438 else
01439 {
01440 _vertBearingY = static_cast<Int32>(-(face->getHoriAscent() - face->getHoriDescent()) / _scale / 20.f);
01441 if (_vertBearingY > -1)
01442 _vertBearingY = -1;
01443 Real32 vertAdvanceOffset = static_cast<Real32>(_vertBearingY) * 2.f * _scale;
01444 _vertAdvance = -static_cast<Real32>(_height) * _scale + vertAdvanceOffset;
01445 }
01446 }
01447
01448
01449
01450
01451
01452
01453 TextFT2TXFGlyph::~TextFT2TXFGlyph() {}
01454
01455
01456 OSG_END_NAMESPACE
01457
01458
01459 #endif // FT2_LIB
01460
01461
01462
01463
01464
01465 #ifdef OSG_SGI_CC
01466 #pragma set woff 1174
01467 #endif
01468
01469 #ifdef OSG_LINUX_ICC
01470 #pragma warning( disable : 177 )
01471 #endif
01472
01473 namespace
01474 {
01475 static OSG::Char8 cvsid_cpp[] = "@(#)$Id: OSGTextFT2Backend.cpp,v 1.7 2007/03/30 14:53:22 pdaehne Exp $";
01476 static OSG::Char8 cvsid_hpp[] = OSGTEXTFT2BACKEND_HEADER_CVSID;
01477 static OSG::Char8 cvsid_inl[] = OSGTEXTFT2BACKEND_INLINE_CVSID;
01478 }
01479
01480 #ifdef __sgi
01481 #pragma reset woff 1174
01482 #endif