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 #include "OSGTextWIN32Backend.h"
00040 #include <OSGBaseTypes.h>
00041
00042
00043 #ifdef _WIN32
00044
00045
00046 #ifdef _MSC_VER
00047 # pragma warning (disable: 4786)
00048 #endif
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
00063
00064 using namespace std;
00065
00066
00067 OSG_BEGIN_NAMESPACE
00068
00069
00070
00071
00072
00073
00074 class TextWIN32VectorFace: public TextVectorFace
00075 {
00076 public:
00077
00078
00079 TextWIN32VectorFace(TextWIN32Backend *backend, HFONT hHoriFont, HFONT hVertFont);
00080
00081
00082 virtual ~TextWIN32VectorFace();
00083
00084
00085 virtual void layout(const wstring &text, const TextLayoutParam ¶m,
00086 TextLayoutResult &layoutResult);
00087
00088 protected:
00089
00090
00091 virtual auto_ptr<TextVectorGlyph> createGlyph(TextGlyph::Index glyphIndex);
00092
00093 private:
00094
00095
00096 TextWIN32Backend *_backend;
00097
00098
00099 HFONT _hHoriFont;
00100
00101
00102 HFONT _hVertFont;
00103 };
00104
00105
00106
00107
00108
00109
00110 class TextWIN32VectorGlyph: public TextVectorGlyph
00111 {
00112 public:
00113
00114
00115 TextWIN32VectorGlyph(Index glyphIndex, Real32 scale,
00116 const GLYPHMETRICS &hpgm, const GLYPHMETRICS &vhpgm,
00117 LPTTPOLYGONHEADER lpHeader, DWORD size);
00118
00119
00120 virtual ~TextWIN32VectorGlyph();
00121 };
00122
00123
00124
00125
00126
00127
00128 class TextWIN32PixmapFace: public TextPixmapFace
00129 {
00130 public:
00131
00132
00133 TextWIN32PixmapFace(TextWIN32Backend *backend, HFONT hHoriFont, HFONT hVertFont, UInt32 size);
00134
00135
00136 virtual ~TextWIN32PixmapFace();
00137
00138
00139 virtual void layout(const wstring &text, const TextLayoutParam ¶m,
00140 TextLayoutResult &layoutResult);
00141
00142 protected:
00143
00144
00145 virtual auto_ptr<TextPixmapGlyph> createGlyph(TextGlyph::Index glyphIndex);
00146
00147 private:
00148
00149
00150 TextWIN32Backend *_backend;
00151
00152
00153 HFONT _hHoriFont;
00154
00155
00156 HFONT _hVertFont;
00157 };
00158
00159
00160
00161
00162
00163
00164 class TextWIN32PixmapGlyph: public TextPixmapGlyph
00165 {
00166 public:
00167
00168
00169 TextWIN32PixmapGlyph(Index glyphIndex, const GLYPHMETRICS &hpgm,
00170 const GLYPHMETRICS &vpgm, UInt8 *pixmap);
00171
00172
00173 virtual ~TextWIN32PixmapGlyph();
00174 };
00175
00176
00177
00178
00179
00180
00181 class TextWIN32TXFFace: public TextTXFFace
00182 {
00183 public:
00184
00185
00186 TextWIN32TXFFace(const TextWIN32Backend *backend, HFONT hHoriFont, HFONT hVertFont,
00187 const TextTXFParam ¶m);
00188
00189
00190 virtual ~TextWIN32TXFFace();
00191 };
00192
00193
00194
00195
00196
00197
00198 class TextWIN32TXFGlyph: public TextTXFGlyph
00199 {
00200 public:
00201
00202
00203 TextWIN32TXFGlyph(Index glyphIndex, TextWIN32TXFFace *face, Real32 scale,
00204 const GLYPHMETRICS &hpgm, const GLYPHMETRICS &vpgm);
00205
00206
00207 virtual ~TextWIN32TXFGlyph();
00208 };
00209
00210
00211
00212
00213
00214
00215 TextWIN32Backend::TextWIN32Backend()
00216 : TextBackend(), _hDC(0)
00217 {
00218
00219 _hDC = CreateDC("DISPLAY", 0, 0, 0);
00220 if (_hDC == 0)
00221
00222 return;
00223 SetGraphicsMode(_hDC, GM_ADVANCED);
00224 }
00225
00226
00227
00228
00229
00230
00231 TextWIN32Backend::~TextWIN32Backend()
00232 {
00233
00234 DeleteDC(_hDC);
00235 }
00236
00237
00238
00239
00240
00241
00242 class EnumData
00243 {
00244 public:
00245 string fullname;
00246 UINT emSize;
00247 };
00248
00249
00250
00251
00252
00253
00254 static int CALLBACK enumFamCallBack(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm,
00255 DWORD fontType, LPARAM lParam)
00256 {
00257
00258 if ((fontType & RASTER_FONTTYPE) != 0)
00259 return 1;
00260
00261
00262 EnumData *enumData = reinterpret_cast<EnumData*>(lParam);
00263 enumData->fullname = (const char *)lpelf->elfFullName;
00264 enumData->emSize = lpntm->ntmSizeEM;
00265
00266
00267 return 0;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276 void TextWIN32Backend::createFonts(const string &family, UInt32 size, TextFace::Style style,
00277 HFONT &hHoriFont, HFONT &hVertFont)
00278 {
00279
00280 hHoriFont = hVertFont = 0;
00281
00282
00283 string f;
00284 if (family == "SERIF")
00285 f = "Times New Roman";
00286 else if (family == "SANS")
00287 f = "Arial";
00288 else if (family == "TYPEWRITER")
00289 f = "Courier New";
00290 else
00291 f = family;
00292
00293 if (size == 0)
00294 {
00295
00296 EnumData enumData;
00297 EnumFontFamilies(_hDC, (LPCTSTR)(f.c_str()), (FONTENUMPROC)enumFamCallBack,
00298 (LPARAM)&enumData);
00299 if (enumData.fullname.empty() == true)
00300 return;
00301 size = -enumData.emSize;
00302 }
00303
00304
00305 BYTE italic;
00306 LONG weight;
00307 switch (style)
00308 {
00309 default:
00310 FWARNING(("Invalid font style parameter.\n"));
00311
00312 case TextFace::STYLE_PLAIN:
00313 italic = FALSE;
00314 weight = FW_NORMAL;
00315 break;
00316 case TextFace::STYLE_BOLD:
00317 italic = FALSE;
00318 weight = FW_BOLD;
00319 break;
00320 case TextFace::STYLE_ITALIC:
00321 italic = TRUE;
00322 weight = FW_NORMAL;
00323 break;
00324 case TextFace::STYLE_BOLDITALIC:
00325 italic = TRUE;
00326 weight = FW_BOLD;
00327 break;
00328 }
00329
00330
00331 hHoriFont = CreateFont(size, 0, 0, 0, weight, italic, FALSE, FALSE,
00332 DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, CLIP_DEFAULT_PRECIS,
00333 ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
00334 f.c_str());
00335
00336
00337 hVertFont = CreateFont(size, 0, -900, 0, weight, italic, FALSE, FALSE,
00338 DEFAULT_CHARSET, OUT_TT_ONLY_PRECIS, CLIP_DEFAULT_PRECIS,
00339 ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
00340 f.c_str());
00341 }
00342
00343
00344
00345
00346
00347
00348 TextVectorFace *TextWIN32Backend::createVectorFace(const string &family,
00349 TextFace::Style style)
00350 {
00351
00352 HFONT hHoriFont, hVertFont;
00353 createFonts(family, 0, style, hHoriFont, hVertFont);
00354 if (hHoriFont == 0)
00355 return 0;
00356
00357
00358 return new TextWIN32VectorFace(this, hHoriFont, hVertFont);
00359 }
00360
00361
00362
00363
00364
00365
00366 TextPixmapFace *TextWIN32Backend::createPixmapFace(const string &family,
00367 TextFace::Style style,
00368 UInt32 size)
00369 {
00370
00371 HFONT hHoriFont, hVertFont;
00372 createFonts(family, size, style, hHoriFont, hVertFont);
00373 if (hHoriFont == 0)
00374 return 0;
00375
00376
00377 return new TextWIN32PixmapFace(this, hHoriFont, hVertFont, size);
00378 }
00379
00380
00381
00382
00383
00384
00385 TextTXFFace *TextWIN32Backend::createTXFFace(const string &family,
00386 TextFace::Style style,
00387 const TextTXFParam ¶m)
00388 {
00389
00390 HFONT hHoriFont, hVertFont;
00391 createFonts(family, param.size, style, hHoriFont, hVertFont);
00392 if (hHoriFont == 0)
00393 return 0;
00394
00395
00396 return new TextWIN32TXFFace(this, hHoriFont, hVertFont, param);
00397 }
00398
00399
00400
00401
00402
00403
00404 static int CALLBACK enumFamCallBack2(ENUMLOGFONT *lpelf, NEWTEXTMETRIC *lpntm,
00405 DWORD fontType, LPARAM lParam)
00406 {
00407
00408 if ((fontType & RASTER_FONTTYPE) != 0)
00409 return 1;
00410
00411
00412 set<string> *familySet = reinterpret_cast<set<string>*>(lParam);
00413 familySet->insert(lpelf->elfLogFont.lfFaceName);
00414
00415
00416 return 1;
00417 }
00418
00419
00420
00421
00422
00423
00424 void TextWIN32Backend::getFontFamilies(vector<string> &families)
00425 {
00426 families.clear();
00427
00428 set<string> familySet;
00429 EnumFontFamilies(_hDC, 0, (FONTENUMPROC)enumFamCallBack2, (LPARAM)&familySet);
00430
00431
00432 families.resize(familySet.size());
00433 copy(familySet.begin(), familySet.end(), families.begin());
00434 }
00435
00436
00437
00438
00439
00440
00441 static void getFontInfo(HDC hDC, HFONT hFont, string &family,
00442 TextFace::Style &style, Real32 &ascent, Real32 &descent)
00443 {
00444
00445 HGDIOBJ oldFont = SelectObject(hDC, hFont);
00446
00447
00448 int nameLength = GetTextFace(hDC, 0, 0);
00449 family.resize(nameLength);
00450 GetTextFace(hDC, nameLength, &(family[0]));
00451
00452 family.resize(nameLength - 1);
00453
00454
00455 TEXTMETRIC tm;
00456 BOOL result = GetTextMetrics(hDC, &tm);
00457 SelectObject(hDC, oldFont);
00458 if (result == FALSE)
00459 return;
00460
00461
00462 if (tm.tmWeight > (FW_NORMAL + FW_BOLD) / 2)
00463 style = tm.tmItalic != 0 ? TextFace::STYLE_BOLDITALIC : TextFace::STYLE_BOLD;
00464 else
00465 style = tm.tmItalic != 0 ? TextFace::STYLE_ITALIC : TextFace::STYLE_PLAIN;
00466
00467
00468 ascent = static_cast<Real32>(tm.tmAscent);
00469
00470
00471 descent = static_cast<Real32>(-tm.tmDescent);
00472 }
00473
00474
00475
00476
00477
00478
00479 TextWIN32VectorFace::TextWIN32VectorFace(TextWIN32Backend *backend,
00480 HFONT hHoriFont, HFONT hVertFont)
00481 : TextVectorFace(), _backend(backend), _hHoriFont(hHoriFont), _hVertFont(hVertFont)
00482 {
00483
00484 getFontInfo(_backend->_hDC, _hHoriFont, _family, _style,
00485 _horiAscent, _horiDescent);
00486
00487
00488 _scale = 1.f / (_horiAscent - _horiDescent);
00489 _horiAscent *= _scale;
00490 _horiDescent *= _scale;
00491
00492
00493 _vertAscent = -0.5f;
00494 _vertDescent = 0.5f;
00495 }
00496
00497
00498
00499
00500
00501
00502 TextWIN32VectorFace::~TextWIN32VectorFace()
00503 {
00504
00505 DeleteObject(_hHoriFont);
00506 DeleteObject(_hVertFont);
00507 }
00508
00509
00510
00511
00512
00513
00514
00515 static void convertUnicodeToUTF16(const wstring &text, vector<WCHAR> &utf16Text)
00516 {
00517 wstring::size_type i, textTotalLength = text.length();
00518 utf16Text.clear();
00519 utf16Text.reserve(textTotalLength);
00520 for (i = 0; i < textTotalLength; ++i)
00521 {
00522 wchar_t unicode = text[i];
00523 if (unicode < 0x10000)
00524 utf16Text.push_back(unicode);
00525 else if (unicode < 0x110000)
00526 {
00527 unsigned long u = unicode - 0x10000;
00528 utf16Text.push_back(0xd800 | (u >> 10));
00529 utf16Text.push_back(0xdc00 | (u & 0x3ff));
00530 }
00531 }
00532 }
00533
00534
00535
00536
00537
00538
00539 void TextWIN32VectorFace::layout(const wstring &text, const TextLayoutParam ¶m,
00540 TextLayoutResult &layoutResult)
00541 {
00542
00543 layoutResult.clear();
00544 if (param.horizontal == true)
00545 layoutResult.textBounds[1] = _horiAscent - _horiDescent;
00546 else
00547 layoutResult.textBounds[0] = _vertDescent - _vertAscent;
00548 layoutResult.lineBounds.push_back(layoutResult.textBounds);
00549
00550
00551 vector<WCHAR> utf16Text;
00552 convertUnicodeToUTF16(text, utf16Text);
00553 vector<WCHAR>::size_type len = utf16Text.size();
00554 if (len == 0)
00555 return;
00556
00557
00558 HGDIOBJ oldFont;
00559 if (param.horizontal == true)
00560 oldFont = SelectObject(_backend->_hDC, _hHoriFont);
00561 else
00562 oldFont = SelectObject(_backend->_hDC, _hVertFont);
00563
00564 GCP_RESULTSW results;
00565 ZeroMemory(&results, sizeof(results));
00566 results.lStructSize = sizeof(results);
00567 results.lpDx = new int[len];
00568 results.lpGlyphs = new WCHAR[len];
00569 results.lpGlyphs[0] = 0;
00570 results.nGlyphs = len;
00571 DWORD dwFlags = GCP_GLYPHSHAPE | GCP_LIGATE | GCP_REORDER | GCP_USEKERNING;
00572 int nMaxExtent = 0;
00573 Real32 length = param.getLength(0);
00574 if (length > 0)
00575 {
00576 dwFlags |= GCP_JUSTIFY | GCP_KASHIDA | GCP_MAXEXTENT;
00577 nMaxExtent = length / _scale;
00578 }
00579 DWORD result = GetCharacterPlacementW(_backend->_hDC, &(utf16Text[0]), len, nMaxExtent, &results, dwFlags);
00580 if (result != 0)
00581 {
00582 layoutResult.indices.reserve(results.nGlyphs);
00583 layoutResult.positions.reserve(results.nGlyphs);
00584 UINT j;
00585 Vec2f currPos;
00586 for (j = 0; j < results.nGlyphs; ++j)
00587 {
00588
00589 const TextGlyph &glyph = getGlyph(results.lpGlyphs[j]);
00590
00591
00592 Vec2f pos;
00593 if (param.horizontal == true)
00594 {
00595 pos = currPos;
00596 pos[0] += glyph.getHoriBearingX();
00597 pos[1] += glyph.getHoriBearingY();
00598 currPos[0] += results.lpDx[j] * _scale;
00599 }
00600 else
00601 {
00602 pos = currPos;
00603 pos[0] += glyph.getVertBearingX();
00604 pos[1] += glyph.getVertBearingY();
00605 currPos[1] -= results.lpDx[j] * _scale;
00606 }
00607 layoutResult.indices.push_back(results.lpGlyphs[j]);
00608 layoutResult.positions.push_back(pos);
00609 }
00610
00611
00612 adjustLineOrigin(param, currPos, layoutResult);
00613
00614
00615 if (param.horizontal == true)
00616 layoutResult.textBounds[0] = osgabs(currPos.x());
00617 else
00618 layoutResult.textBounds[1] = osgabs(currPos.y());
00619 assert(layoutResult.lineBounds.empty() == false);
00620 layoutResult.lineBounds.front() = layoutResult.textBounds;
00621 }
00622 delete [] results.lpDx;
00623 delete [] results.lpGlyphs;
00624
00625
00626 SelectObject(_backend->_hDC, oldFont);
00627 }
00628
00629
00630
00631
00632
00633
00634 auto_ptr<TextVectorGlyph> TextWIN32VectorFace::createGlyph(TextGlyph::Index glyphIndex)
00635 {
00636
00637 if (glyphIndex == TextGlyph::INVALID_INDEX)
00638 return auto_ptr<TextVectorGlyph>();
00639
00640
00641 HGDIOBJ oldFont = SelectObject(_backend->_hDC, _hVertFont);
00642
00643
00644 GLYPHMETRICS vpgm;
00645 MAT2 mat2 = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
00646 DWORD size = GetGlyphOutlineW(_backend->_hDC, glyphIndex, GGO_METRICS | GGO_GLYPH_INDEX,
00647 &vpgm, 0, 0, &mat2);
00648 if (size == GDI_ERROR)
00649 {
00650 SelectObject(_backend->_hDC, oldFont);
00651 return auto_ptr<TextVectorGlyph>();
00652 }
00653
00654
00655 SelectObject(_backend->_hDC, _hHoriFont);
00656
00657
00658 GLYPHMETRICS hpgm;
00659 size = GetGlyphOutlineW(_backend->_hDC, glyphIndex, GGO_NATIVE | GGO_GLYPH_INDEX, &hpgm, 0, 0, &mat2);
00660 if (size == GDI_ERROR)
00661 {
00662 SelectObject(_backend->_hDC, oldFont);
00663 return auto_ptr<TextVectorGlyph>();
00664 }
00665 LPTTPOLYGONHEADER lpHeader = (LPTTPOLYGONHEADER) new char[size];
00666 size = GetGlyphOutlineW(_backend->_hDC, glyphIndex, GGO_NATIVE | GGO_GLYPH_INDEX, &hpgm, size, lpHeader, &mat2);
00667
00668
00669 SelectObject(_backend->_hDC, oldFont);
00670
00671 if (size == GDI_ERROR)
00672 {
00673 delete [] lpHeader;
00674 return auto_ptr<TextVectorGlyph>();
00675 }
00676
00677
00678 auto_ptr<TextVectorGlyph> glyph(new TextWIN32VectorGlyph(glyphIndex, _scale,
00679 hpgm, vpgm, lpHeader, size));
00680 delete [] lpHeader;
00681 return glyph;
00682 }
00683
00684
00685
00686
00687
00688
00689 TextWIN32VectorGlyph::TextWIN32VectorGlyph(Index glyphIndex, Real32 scale,
00690 const GLYPHMETRICS &hpgm,
00691 const GLYPHMETRICS &vpgm,
00692 LPTTPOLYGONHEADER lpHeader,
00693 DWORD size)
00694 : TextVectorGlyph()
00695 {
00696 _glyphIndex = glyphIndex;
00697 _width = static_cast<Real32>(hpgm.gmBlackBoxX) * scale;
00698 _height = static_cast<Real32>(hpgm.gmBlackBoxY) * scale;
00699
00700
00701 _horiAdvance = static_cast<Real32>(hpgm.gmCellIncX) * scale;
00702 _horiBearingX = static_cast<Real32>(hpgm.gmptGlyphOrigin.x) * scale;
00703 _horiBearingY = static_cast<Real32>(hpgm.gmptGlyphOrigin.y) * scale;
00704
00705
00706
00707
00708
00709
00710
00711
00712 _vertAdvance = -_height;
00713 _vertBearingX = -_width / 2.f;
00714 _vertBearingY = 0.f;
00715
00716 Vec2f offset(_horiBearingX, _horiBearingY), p;
00717
00718
00719
00720
00721 WORD i;
00722 LPTTPOLYGONHEADER lpStart;
00723 LPTTPOLYCURVE lpCurve;
00724 POINTFX *endPoint = 0;
00725
00726 lpStart = lpHeader;
00727
00728
00729
00730
00731 while ((DWORD)lpHeader < (DWORD)(((LPSTR)lpStart) + size))
00732 {
00733 if (lpHeader->dwType == TT_POLYGON_TYPE)
00734
00735
00736 {
00737 _outline.push_back(TextVectorGlyph::Contour());
00738
00739
00740
00741
00742
00743
00744 POINTFX &startPoint = lpHeader->pfxStart;
00745 p.setValues(startPoint.x.value, startPoint.y.value);
00746 p *= scale;
00747 p -= offset;
00748 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00749
00750
00751
00752 lpCurve = (LPTTPOLYCURVE) (lpHeader + 1);
00753
00754
00755
00756 while ((DWORD)lpCurve < (DWORD)(((LPSTR)lpHeader) + lpHeader->cb))
00757 {
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772 if (lpCurve->wType == TT_PRIM_LINE)
00773 {
00774
00775 for (i = 0; i < lpCurve->cpfx; ++i)
00776 {
00777 p.setValues(lpCurve->apfx[i].x.value, lpCurve->apfx[i].y.value);
00778 p *= scale;
00779 p -= offset;
00780 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00781 }
00782 }
00783 else if (lpCurve->wType == TT_PRIM_QSPLINE)
00784 {
00785
00786
00787
00788
00789
00790 short x1, x2, y1, y2;
00791 for (i = 0; i < lpCurve->cpfx; ++i)
00792 {
00793 x2 = lpCurve->apfx[i].x.value;
00794 y2 = lpCurve->apfx[i].y.value;
00795 if (i >= lpCurve->cpfx - 1)
00796 {
00797 p.setValues(x2, y2);
00798 p *= scale;
00799 p -= offset;
00800 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00801 break;
00802 }
00803 if (i > 0)
00804 {
00805 x1 = (x1 + x2) >> 1;
00806 y1 = (y1 + y2) >> 1;
00807 p.setValues(x1, y1);
00808 p *= scale;
00809 p -= offset;
00810 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00811 }
00812 p.setValues(x2, y2);
00813 p *= scale;
00814 p -= offset;
00815 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_QUAD));
00816 x1 = x2;
00817 y1 = y2;
00818 }
00819 }
00820 else if (lpCurve->wType == TT_PRIM_CSPLINE)
00821 {
00822
00823
00824 for (i = 0; i < lpCurve->cpfx; ++i)
00825 {
00826 p.setValues(lpCurve->apfx[i].x.value, lpCurve->apfx[i].y.value);
00827 p *= scale;
00828 p -= offset;
00829 if (i >= lpCurve->cpfx - 1)
00830 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00831 else
00832 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_CUBIC));
00833 }
00834 }
00835 else
00836
00837 ;
00838
00839
00840 endPoint = &(lpCurve->apfx[lpCurve->cpfx - 1]);
00841
00842
00843 lpCurve = (LPTTPOLYCURVE)&(lpCurve->apfx[lpCurve->cpfx]);
00844 }
00845
00846
00847
00848 if (_outline.back().size() < 3)
00849 _outline.erase(_outline.end() - 1);
00850 else
00851
00852
00853
00854
00855 if ((startPoint.x.value != endPoint->x.value) || (startPoint.y.value != endPoint->y.value))
00856 {
00857 p.setValues(startPoint.x.value, startPoint.y.value);
00858 p *= scale;
00859 p -= offset;
00860 _outline.back().push_back(TextVectorGlyph::Point(p, TextVectorGlyph::Point::PT_ON));
00861 }
00862 }
00863 else
00864
00865 break;
00866
00867
00868
00869 lpHeader = (LPTTPOLYGONHEADER)(((LPSTR)lpHeader) + lpHeader->cb);
00870 }
00871 }
00872
00873
00874
00875
00876
00877
00878 TextWIN32VectorGlyph::~TextWIN32VectorGlyph() {}
00879
00880
00881
00882
00883
00884
00885 TextWIN32PixmapFace::TextWIN32PixmapFace(TextWIN32Backend *backend, HFONT hHoriFont, HFONT hVertFont, UInt32 size)
00886 : TextPixmapFace(), _backend(backend), _hHoriFont(hHoriFont), _hVertFont(hVertFont)
00887 {
00888
00889 getFontInfo(_backend->_hDC, _hHoriFont, _family, _style,
00890 _horiAscent, _horiDescent);
00891
00892
00893 _size = size;
00894
00895
00896 _vertAscent = -static_cast<Real32>(_size) / 2.f;
00897 _vertDescent = static_cast<Real32>(_size) / 2.f;
00898 }
00899
00900
00901
00902
00903
00904
00905 TextWIN32PixmapFace::~TextWIN32PixmapFace()
00906 {
00907
00908 DeleteObject(_hHoriFont);
00909 DeleteObject(_hVertFont);
00910 }
00911
00912
00913
00914
00915
00916
00917 void TextWIN32PixmapFace::layout(const wstring &text, const TextLayoutParam ¶m,
00918 TextLayoutResult &layoutResult)
00919 {
00920
00921 layoutResult.clear();
00922 if (param.horizontal == true)
00923 layoutResult.textBounds[1] = _horiAscent - _horiDescent;
00924 else
00925 layoutResult.textBounds[0] = _vertDescent - _vertAscent;
00926 layoutResult.lineBounds.push_back(layoutResult.textBounds);
00927
00928
00929 vector<WCHAR> utf16Text;
00930 convertUnicodeToUTF16(text, utf16Text);
00931 vector<WCHAR>::size_type len = utf16Text.size();
00932 if (len == 0)
00933 return;
00934
00935
00936 HGDIOBJ oldFont;
00937 if (param.horizontal == true)
00938 oldFont = SelectObject(_backend->_hDC, _hHoriFont);
00939 else
00940 oldFont = SelectObject(_backend->_hDC, _hVertFont);
00941
00942 GCP_RESULTSW results;
00943 ZeroMemory(&results, sizeof(results));
00944 results.lStructSize = sizeof(results);
00945 results.lpDx = new int[len];
00946 results.lpGlyphs = new WCHAR[len];
00947 results.lpGlyphs[0] = 0;
00948 results.nGlyphs = len;
00949 DWORD dwFlags = GCP_GLYPHSHAPE | GCP_LIGATE | GCP_REORDER | GCP_USEKERNING;
00950 int nMaxExtent = 0;
00951 Real32 length = param.getLength(0);
00952 if (length > 0)
00953 {
00954 dwFlags |= GCP_JUSTIFY | GCP_KASHIDA | GCP_MAXEXTENT;
00955 nMaxExtent = length;
00956 }
00957 DWORD result = GetCharacterPlacementW(_backend->_hDC, &(utf16Text[0]), len, nMaxExtent, &results, dwFlags);
00958 if (result != 0)
00959 {
00960 layoutResult.indices.reserve(results.nGlyphs);
00961 layoutResult.positions.reserve(results.nGlyphs);
00962 UINT j;
00963 Vec2f currPos;
00964 for (j = 0; j < results.nGlyphs; ++j)
00965 {
00966
00967 const TextGlyph &glyph = getGlyph(results.lpGlyphs[j]);
00968
00969
00970 Vec2f pos;
00971 if (param.horizontal == true)
00972 {
00973 pos[0] = currPos.x() + glyph.getHoriBearingX();
00974 pos[1] = currPos.y() + glyph.getHoriBearingY();
00975 currPos[0] += results.lpDx[j];
00976 }
00977 else
00978 {
00979 pos[0] = currPos.x() + glyph.getVertBearingX();
00980 pos[1] = currPos.y() + glyph.getVertBearingY();
00981 currPos[1] -= results.lpDx[j];
00982 }
00983 layoutResult.indices.push_back(results.lpGlyphs[j]);
00984 layoutResult.positions.push_back(pos);
00985 }
00986
00987
00988 adjustLineOrigin(param, currPos, layoutResult);
00989
00990
00991 if (param.horizontal == true)
00992 layoutResult.textBounds[0] = osgabs(currPos.x());
00993 else
00994 layoutResult.textBounds[1] = osgabs(currPos.y());
00995 assert(layoutResult.lineBounds.empty() == false);
00996 layoutResult.lineBounds.front() = layoutResult.textBounds;
00997 }
00998 delete [] results.lpDx;
00999 delete [] results.lpGlyphs;
01000
01001
01002 SelectObject(_backend->_hDC, oldFont);
01003 }
01004
01005
01006
01007
01008
01009
01010 auto_ptr<TextPixmapGlyph> TextWIN32PixmapFace::createGlyph(TextGlyph::Index glyphIndex)
01011 {
01012
01013 if (glyphIndex == TextGlyph::INVALID_INDEX)
01014 return auto_ptr<TextPixmapGlyph>();
01015
01016
01017 HGDIOBJ oldFont = SelectObject(_backend->_hDC, _hVertFont);
01018
01019
01020 GLYPHMETRICS vpgm;
01021 MAT2 mat2 = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
01022 DWORD size = GetGlyphOutlineW(_backend->_hDC, glyphIndex, GGO_METRICS | GGO_GLYPH_INDEX,
01023 &vpgm, 0, 0, &mat2);
01024 if (size == GDI_ERROR)
01025 {
01026 SelectObject(_backend->_hDC, oldFont);
01027 return auto_ptr<TextPixmapGlyph>();
01028 }
01029
01030
01031 SelectObject(_backend->_hDC, _hHoriFont);
01032
01033 GLYPHMETRICS hpgm;
01034 size = GetGlyphOutlineW(_backend->_hDC, glyphIndex, GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX,
01035 &hpgm, 0, 0, &mat2);
01036 if (size == GDI_ERROR)
01037 {
01038 SelectObject(_backend->_hDC, oldFont);
01039 return auto_ptr<TextPixmapGlyph>();
01040 }
01041 UInt8 *buffer;
01042 if (size == 0)
01043 buffer = 0;
01044 else
01045 {
01046
01047 buffer = new UInt8[size];
01048 size = GetGlyphOutlineW(_backend->_hDC, glyphIndex, GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX,
01049 &hpgm, size, buffer, &mat2);
01050 if (size == GDI_ERROR)
01051 {
01052 delete [] buffer;
01053 SelectObject(_backend->_hDC, oldFont);
01054 return auto_ptr<TextPixmapGlyph>();
01055 }
01056
01057
01058
01059 DWORD i;
01060 UInt8 *ptr = buffer;
01061 for (i = size; i > 0; --i)
01062 {
01063 *ptr = *ptr >= 64 ? 255 : *ptr << 2;
01064 ++ptr;
01065 }
01066 }
01067 SelectObject(_backend->_hDC, oldFont);
01068
01069
01070 return auto_ptr<TextPixmapGlyph>(new TextWIN32PixmapGlyph(glyphIndex, hpgm, vpgm, buffer));
01071 }
01072
01073
01074
01075
01076
01077
01078 TextWIN32PixmapGlyph::TextWIN32PixmapGlyph(Index glyphIndex, const GLYPHMETRICS &hpgm,
01079 const GLYPHMETRICS &vpgm, UInt8 *pixmap)
01080 : TextPixmapGlyph()
01081 {
01082 _glyphIndex = glyphIndex;
01083 _width = hpgm.gmBlackBoxX;
01084 _pitch = (hpgm.gmBlackBoxX + 3) & ~3;
01085 _height = hpgm.gmBlackBoxY;
01086 _pixmap = pixmap;
01087 flipPixmap();
01088
01089
01090 _horiAdvance = hpgm.gmCellIncX;
01091 _horiBearingX = hpgm.gmptGlyphOrigin.x;
01092 _horiBearingY = hpgm.gmptGlyphOrigin.y;
01093
01094
01095
01096
01097
01098
01099
01100
01101 _vertAdvance = -static_cast<Int32>(_height);
01102 _vertBearingX = -static_cast<Int32>(_width >> 1);
01103 _vertBearingY = 0;
01104 }
01105
01106
01107
01108
01109
01110
01111 TextWIN32PixmapGlyph::~TextWIN32PixmapGlyph() {}
01112
01113
01114
01115
01116
01117
01118 TextWIN32TXFFace::TextWIN32TXFFace(const TextWIN32Backend *backend, HFONT hHoriFont, HFONT hVertFont, const TextTXFParam ¶m)
01119 : TextTXFFace()
01120 {
01121
01122 getFontInfo(backend->_hDC, hHoriFont, _family, _style,
01123 _horiAscent, _horiDescent);
01124
01125
01126 _param = param;
01127
01128
01129 _scale = 1.f / (_horiAscent - _horiDescent);
01130 _horiAscent *= _scale;
01131 _horiDescent *= _scale;
01132
01133
01134 _vertAscent = -0.5f;
01135 _vertDescent = 0.5f;
01136
01137
01138 vector<WCHAR> utf16Text;
01139 convertUnicodeToUTF16(param.getCharacters(), utf16Text);
01140 vector<WCHAR>::size_type len = utf16Text.size();
01141 if (len == 0)
01142 {
01143 DeleteObject(hHoriFont);
01144 DeleteObject(hVertFont);
01145 return;
01146 }
01147
01148
01149 HGDIOBJ oldFont = SelectObject(backend->_hDC, hHoriFont);
01150
01151 GCP_RESULTSW results;
01152 ZeroMemory(&results, sizeof(results));
01153 results.lStructSize = sizeof(results);
01154 results.lpGlyphs = new WCHAR[len];
01155 results.nGlyphs = len;
01156 DWORD result = GetCharacterPlacementW(backend->_hDC, &(utf16Text[0]), len, 0, &results, 0);
01157 if (result == 0)
01158 {
01159 delete [] results.lpGlyphs;
01160 SelectObject(backend->_hDC, oldFont);
01161 DeleteObject(hHoriFont);
01162 DeleteObject(hVertFont);
01163 return;
01164 }
01165
01166
01167 assert(results.nGlyphs == param.getCharacters().length());
01168 UINT j;
01169 for (j = 0; j < results.nGlyphs; ++j)
01170 {
01171
01172 SelectObject(backend->_hDC, hVertFont);
01173
01174
01175 GLYPHMETRICS vpgm;
01176 MAT2 mat2 = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
01177 DWORD size = GetGlyphOutlineW(backend->_hDC, results.lpGlyphs[j], GGO_METRICS | GGO_GLYPH_INDEX,
01178 &vpgm, 0, 0, &mat2);
01179 if (size == GDI_ERROR)
01180 continue;
01181
01182
01183 SelectObject(backend->_hDC, hHoriFont);
01184
01185
01186 GLYPHMETRICS hpgm;
01187
01188 size = GetGlyphOutlineW(backend->_hDC, results.lpGlyphs[j], GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX,
01189 &hpgm, 0, 0, &mat2);
01190 if (size == GDI_ERROR)
01191 continue;
01192
01193 _glyphMap.insert(GlyphMap::value_type(param.getCharacters()[j], new TextWIN32TXFGlyph(param.getCharacters()[j], this, _scale, hpgm, vpgm)));
01194 }
01195
01196
01197 prepareTexture(param);
01198 assert(_texture != NullFC);
01199 assert(_texture->getSize() == static_cast<UInt32>(_texture->getWidth() * _texture->getHeight()));
01200
01201
01202 SelectObject(backend->_hDC, hHoriFont);
01203 vector<unsigned char> buffer;
01204 beginEditCP(_texture);
01205 for (j = 0; j < results.nGlyphs; ++j)
01206 {
01207
01208 GLYPHMETRICS hpgm;
01209 MAT2 mat2 = { { 0, 1 }, { 0, 0 }, { 0, 0 }, { 0, 1 } };
01210 DWORD size = GetGlyphOutlineW(backend->_hDC, results.lpGlyphs[j], GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX,
01211 &hpgm, 0, 0, &mat2);
01212 if ((size == GDI_ERROR) || (size == 0))
01213 continue;
01214 buffer.resize(size);
01215 size = GetGlyphOutlineW(backend->_hDC, results.lpGlyphs[j], GGO_GRAY8_BITMAP | GGO_GLYPH_INDEX,
01216 &hpgm, size, &(buffer[0]), &mat2);
01217 if (size == GDI_ERROR)
01218 continue;
01219
01220
01221 GlyphMap::iterator gIt = _glyphMap.find(param.getCharacters()[j]);
01222 if (gIt == _glyphMap.end())
01223 continue;
01224 assert(gIt->second != 0);
01225 TextTXFGlyph *glyph = gIt->second;
01226
01227
01228 unsigned char *src = &(buffer[0]);
01229 int bpl = (hpgm.gmBlackBoxX + 3) & ~3;
01230 src += bpl * (hpgm.gmBlackBoxY - 1);
01231 unsigned char *src2;
01232 UInt8 *dst = _texture->getData() + glyph->getX() + glyph->getY() * _texture->getWidth();
01233 UInt32 dstPitch = _texture->getWidth() - glyph->getPixmapWidth();
01234 UInt32 x, y;
01235 for (y = 0; y < glyph->getPixmapHeight(); ++y)
01236 {
01237 src2 = src;
01238 for (x = 0; x < glyph->getPixmapWidth(); ++x)
01239 {
01240
01241
01242 *dst++ = *src2 >= 64 ? 255 : *src2 << 2;
01243 ++src2;
01244 }
01245 src -= bpl;
01246 dst += dstPitch;
01247 }
01248 }
01249 endEditCP(_texture);
01250
01251 delete [] results.lpGlyphs;
01252
01253
01254 SelectObject(backend->_hDC, oldFont);
01255
01256
01257 DeleteObject(hHoriFont);
01258 DeleteObject(hVertFont);
01259 }
01260
01261
01262
01263
01264
01265
01266 TextWIN32TXFFace::~TextWIN32TXFFace() {}
01267
01268
01269
01270
01271
01272
01273 TextWIN32TXFGlyph::TextWIN32TXFGlyph(Index glyphIndex, TextWIN32TXFFace *face, Real32 scale,
01274 const GLYPHMETRICS &hpgm, const GLYPHMETRICS &vpgm)
01275 : TextTXFGlyph()
01276 {
01277 _glyphIndex = glyphIndex;
01278 _scale = scale;
01279 _width = hpgm.gmBlackBoxX;
01280 _height = hpgm.gmBlackBoxY;
01281
01282
01283 _horiAdvance = static_cast<Real32>(hpgm.gmCellIncX) * _scale;
01284 _horiBearingX = hpgm.gmptGlyphOrigin.x;
01285 _horiBearingY = hpgm.gmptGlyphOrigin.y;
01286
01287
01288
01289
01290
01291
01292
01293
01294 _vertBearingX = -static_cast<Int32>(_width >> 1);
01295 if (glyphIndex == 32)
01296 {
01297 _vertBearingY = -_horiBearingX;
01298 _vertAdvance = -_horiAdvance;
01299 }
01300 else
01301 {
01302 _vertBearingY = static_cast<Int32>(-(face->getHoriAscent() - face->getHoriDescent()) / _scale / 20.f);
01303 if (_vertBearingY > -1)
01304 _vertBearingY = -1;
01305 Real32 vertAdvanceOffset = static_cast<Real32>(_vertBearingY) * 2.f * _scale;
01306 _vertAdvance = -static_cast<Real32>(_height) * _scale + vertAdvanceOffset;
01307 }
01308 }
01309
01310
01311
01312
01313
01314
01315 TextWIN32TXFGlyph::~TextWIN32TXFGlyph() {}
01316
01317
01318 OSG_END_NAMESPACE
01319
01320
01321 #endif // _WIN32
01322
01323
01324
01325
01326 #ifdef OSG_SGI_CC
01327 #pragma set woff 1174
01328 #endif
01329
01330 #ifdef OSG_LINUX_ICC
01331 #pragma warning( disable : 177 )
01332 #endif
01333
01334 namespace
01335 {
01336 static OSG::Char8 cvsid_cpp[] = "@(#)$Id: OSGTextWIN32Backend.cpp,v 1.5 2007/03/30 14:53:22 pdaehne Exp $";
01337 static OSG::Char8 cvsid_hpp[] = OSGTEXTWIN32BACKEND_HEADER_CVSID;
01338 static OSG::Char8 cvsid_inl[] = OSGTEXTWIN32BACKEND_INLINE_CVSID;
01339 }
01340
01341 #ifdef __sgi
01342 #pragma reset woff 1174
01343 #endif