0025982: Wrong result obtained by General Fuse operator.
[occt.git] / src / OpenGl / OpenGl_Text.cxx
CommitLineData
b311480e 1// Created on: 2011-07-13
2// Created by: Sergey ZERCHANINOV
a174a3c5 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
2166f0fa 16#include <OpenGl_AspectText.hxx>
30f0ad28 17#include <OpenGl_GlCore11.hxx>
a174a3c5 18#include <OpenGl_GraphicDriver.hxx>
30f0ad28 19#include <OpenGl_ShaderManager.hxx>
20#include <OpenGl_ShaderProgram.hxx>
21#include <OpenGl_ShaderStates.hxx>
22#include <OpenGl_Text.hxx>
c827ea3a 23#include <OpenGl_Utils.hxx>
bf75be98 24#include <OpenGl_Workspace.hxx>
2166f0fa 25
a174a3c5 26#include <Font_FontMgr.hxx>
27#include <TCollection_HAsciiString.hxx>
28
a174a3c5 29#ifdef HAVE_GL2PS
30 #include <gl2ps.h>
31#endif
32
33namespace
2166f0fa 34{
c827ea3a 35 static const GLdouble THE_IDENTITY_MATRIX[16] =
a174a3c5 36 {
c827ea3a 37 1.0, 0.0, 0.0, 0.0,
38 0.0, 1.0, 0.0, 0.0,
39 0.0, 0.0, 1.0, 0.0,
40 0.0, 0.0, 0.0, 1.0
a174a3c5 41 };
42
43#ifdef HAVE_GL2PS
44 static char const* TheFamily[] = {"Helvetica", "Courier", "Times"};
45 static char const* TheItalic[] = {"Oblique", "Oblique", "Italic"};
46 static char const* TheBase[] = {"", "", "-Roman"};
47
48 //! Convert font name used for rendering to some "good" font names
49 //! that produce good vector text.
50 static void getGL2PSFontName (const char* theSrcFont,
51 char* thePsFont)
52 {
53 if (strstr (theSrcFont, "Symbol"))
54 {
55 sprintf (thePsFont, "%s", "Symbol");
56 return;
57 }
58 else if (strstr (theSrcFont, "ZapfDingbats"))
59 {
60 sprintf (thePsFont, "%s", "WingDings");
61 return;
62 }
2166f0fa 63
a174a3c5 64 int aFontId = 0;
65 bool isBold = false;
66 bool isItalic = false;
67 if (strstr (theSrcFont, "Courier"))
68 {
69 aFontId = 1;
70 }
71 else if (strstr (theSrcFont, "Times"))
72 {
73 aFontId = 2;
74 }
2166f0fa 75
a174a3c5 76 if (strstr (theSrcFont, "Bold"))
77 {
78 isBold = true;
79 }
80 if (strstr (theSrcFont, "Italic")
81 || strstr (theSrcFont, "Oblique"))
82 {
83 isItalic = true;
84 }
2166f0fa 85
a174a3c5 86 if (isBold)
87 {
a174a3c5 88 if (isItalic)
89 {
8b224a09 90 sprintf (thePsFont, "%s-Bold%s", TheFamily[aFontId], TheItalic[aFontId]);
91 }
92 else
93 {
94 sprintf (thePsFont, "%s-Bold", TheFamily[aFontId]);
a174a3c5 95 }
96 }
97 else if (isItalic)
98 {
99 sprintf (thePsFont, "%s-%s", TheFamily[aFontId], TheItalic[aFontId]);
100 }
101 else
102 {
103 sprintf (thePsFont, "%s%s", TheFamily[aFontId], TheBase[aFontId]);
104 }
105 }
2166f0fa 106
a174a3c5 107 static void exportText (const NCollection_String& theText,
108 const Standard_Boolean theIs2d,
109 const OpenGl_AspectText& theAspect,
110 const Standard_Integer theHeight)
111 {
2166f0fa 112
a174a3c5 113 char aPsFont[64];
114 getGL2PSFontName (theAspect.FontName().ToCString(), aPsFont);
2166f0fa 115
ca3c13d1 116 #if !defined(GL_ES_VERSION_2_0)
a174a3c5 117 if (theIs2d)
118 {
119 glRasterPos2f (0.0f, 0.0f);
120 }
121 else
122 {
123 glRasterPos3f (0.0f, 0.0f, 0.0f);
124 }
125
126 GLubyte aZero = 0;
127 glBitmap (1, 1, 0, 0, 0, 0, &aZero);
ca3c13d1 128 #endif
a174a3c5 129
130 // Standard GL2PS's alignment isn't used, because it doesn't work correctly
131 // for all formats, therefore alignment is calculated manually relative
132 // to the bottom-left corner, which corresponds to the GL2PS_TEXT_BL value
105aae76 133 gl2psTextOpt (theText.ToCString(), aPsFont, (GLshort)theHeight, GL2PS_TEXT_BL, theAspect.Angle());
a174a3c5 134 }
135#endif
136
137};
138
139// =======================================================================
140// function : OpenGl_Text
141// purpose :
142// =======================================================================
143OpenGl_Text::OpenGl_Text()
144: myWinX (0.0f),
145 myWinY (0.0f),
146 myWinZ (0.0f),
147 myScaleHeight (1.0f),
148 myPoint (0.0f, 0.0f, 0.0f),
149 myIs2d (false)
150{
151 myParams.Height = 10;
152 myParams.HAlign = Graphic3d_HTA_LEFT;
153 myParams.VAlign = Graphic3d_VTA_BOTTOM;
2166f0fa
SK
154}
155
a174a3c5 156// =======================================================================
157// function : OpenGl_Text
158// purpose :
159// =======================================================================
b64d84be 160OpenGl_Text::OpenGl_Text (const Standard_Utf8Char* theText,
161 const OpenGl_Vec3& thePoint,
162 const OpenGl_TextParam& theParams)
a174a3c5 163: myWinX (0.0f),
164 myWinY (0.0f),
165 myWinZ (0.0f),
166 myScaleHeight (1.0f),
167 myExportHeight (1.0f),
168 myParams (theParams),
b64d84be 169 myString (theText),
a174a3c5 170 myPoint (thePoint),
171 myIs2d (false)
172{
173 //
174}
2166f0fa 175
a174a3c5 176// =======================================================================
177// function : SetPosition
178// purpose :
179// =======================================================================
180void OpenGl_Text::SetPosition (const OpenGl_Vec3& thePoint)
2166f0fa 181{
a174a3c5 182 myPoint = thePoint;
2166f0fa
SK
183}
184
a174a3c5 185// =======================================================================
186// function : SetFontSize
187// purpose :
188// =======================================================================
189void OpenGl_Text::SetFontSize (const Handle(OpenGl_Context)& theCtx,
190 const Standard_Integer theFontSize)
191{
192 if (myParams.Height != theFontSize)
193 {
10b9c7df 194 Release (theCtx.operator->());
a174a3c5 195 }
196 myParams.Height = theFontSize;
197}
198
199// =======================================================================
200// function : Init
201// purpose :
202// =======================================================================
203void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
204 const Standard_Utf8Char* theText,
205 const OpenGl_Vec3& thePoint)
206{
10b9c7df 207 releaseVbos (theCtx.operator->());
a174a3c5 208 myIs2d = false;
209 myPoint = thePoint;
210 myString.FromUnicode (theText);
211}
212
213// =======================================================================
214// function : Init
215// purpose :
216// =======================================================================
217void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
218 const Standard_Utf8Char* theText,
219 const OpenGl_Vec3& thePoint,
220 const OpenGl_TextParam& theParams)
221{
222 if (myParams.Height != theParams.Height)
223 {
10b9c7df 224 Release (theCtx.operator->());
a174a3c5 225 }
226 else
227 {
10b9c7df 228 releaseVbos (theCtx.operator->());
a174a3c5 229 }
230 myIs2d = false;
231 myParams = theParams;
232 myPoint = thePoint;
233 myString.FromUnicode (theText);
234}
2166f0fa 235
a174a3c5 236// =======================================================================
237// function : Init
238// purpose :
239// =======================================================================
240void OpenGl_Text::Init (const Handle(OpenGl_Context)& theCtx,
241 const TCollection_ExtendedString& theText,
242 const OpenGl_Vec2& thePoint,
243 const OpenGl_TextParam& theParams)
2166f0fa 244{
a174a3c5 245 if (myParams.Height != theParams.Height)
246 {
10b9c7df 247 Release (theCtx.operator->());
a174a3c5 248 }
249 else
250 {
10b9c7df 251 releaseVbos (theCtx.operator->());
a174a3c5 252 }
253 myIs2d = true;
254 myParams = theParams;
255 myPoint.xy() = thePoint;
256 myPoint.z() = 0.0f;
257 myString.FromUnicode ((Standard_Utf16Char* )theText.ToExtString());
258}
259
260// =======================================================================
261// function : ~OpenGl_Text
262// purpose :
263// =======================================================================
264OpenGl_Text::~OpenGl_Text()
265{
266 //
267}
268
269// =======================================================================
270// function : releaseVbos
271// purpose :
272// =======================================================================
10b9c7df 273void OpenGl_Text::releaseVbos (OpenGl_Context* theCtx)
a174a3c5 274{
275 for (Standard_Integer anIter = 0; anIter < myVertsVbo.Length(); ++anIter)
276 {
277 Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.ChangeValue (anIter);
278 Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.ChangeValue (anIter);
279
10b9c7df 280 if (theCtx)
a174a3c5 281 {
282 theCtx->DelayedRelease (aVerts);
283 theCtx->DelayedRelease (aTCrds);
284 }
285 aVerts.Nullify();
286 aTCrds.Nullify();
287 }
288 myTextures.Clear();
289 myVertsVbo.Clear();
290 myTCrdsVbo.Clear();
a174a3c5 291}
292
293// =======================================================================
294// function : Release
295// purpose :
296// =======================================================================
10b9c7df 297void OpenGl_Text::Release (OpenGl_Context* theCtx)
a174a3c5 298{
299 releaseVbos (theCtx);
300 if (!myFont.IsNull())
301 {
302 Handle(OpenGl_Context) aCtx = theCtx;
303 const TCollection_AsciiString aKey = myFont->ResourceKey();
304 myFont.Nullify();
10b9c7df 305 if (aCtx)
306 aCtx->ReleaseResource (aKey, Standard_True);
a174a3c5 307 }
308}
309
310// =======================================================================
311// function : StringSize
312// purpose :
313// =======================================================================
314void OpenGl_Text::StringSize (const Handle(OpenGl_Context)& theCtx,
315 const NCollection_String& theText,
316 const OpenGl_AspectText& theTextAspect,
317 const OpenGl_TextParam& theParams,
318 Standard_ShortReal& theWidth,
319 Standard_ShortReal& theAscent,
320 Standard_ShortReal& theDescent)
321{
322 theWidth = 0.0f;
323 theAscent = 0.0f;
324 theDescent = 0.0f;
325 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, theParams.Height);
326 Handle(OpenGl_Font) aFont = FindFont (theCtx, theTextAspect, theParams.Height, aFontKey);
327 if (aFont.IsNull() || !aFont->IsValid())
328 {
2166f0fa 329 return;
a174a3c5 330 }
331
332 theAscent = aFont->Ascender();
333 theDescent = aFont->Descender();
334
335 GLfloat aWidth = 0.0f;
336 for (NCollection_Utf8Iter anIter = theText.Iterator(); *anIter != 0;)
337 {
338 const Standard_Utf32Char aCharThis = *anIter;
339 const Standard_Utf32Char aCharNext = *++anIter;
340
341 if (aCharThis == '\x0D' // CR (carriage return)
342 || aCharThis == '\a' // BEL (alarm)
343 || aCharThis == '\f' // FF (form feed) NP (new page)
344 || aCharThis == '\b' // BS (backspace)
345 || aCharThis == '\v') // VT (vertical tab)
346 {
347 continue; // skip unsupported carriage control codes
348 }
349 else if (aCharThis == '\x0A') // LF (line feed, new line)
350 {
351 theWidth = Max (theWidth, aWidth);
352 aWidth = 0.0f;
353 continue;
354 }
355 else if (aCharThis == ' ')
356 {
357 aWidth += aFont->AdvanceX (aCharThis, aCharNext);
358 continue;
359 }
360 else if (aCharThis == '\t')
361 {
362 aWidth += aFont->AdvanceX (' ', aCharNext) * 8.0f;
363 continue;
364 }
365
366 aWidth += aFont->AdvanceX (aCharThis, aCharNext);
367 }
368 theWidth = Max (theWidth, aWidth);
369
370 Handle(OpenGl_Context) aCtx = theCtx;
371 aFont.Nullify();
372 aCtx->ReleaseResource (aFontKey, Standard_True);
373}
374
375// =======================================================================
376// function : Render
377// purpose :
378// =======================================================================
379void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
380{
25ef750e 381 const OpenGl_AspectText* aTextAspect = theWorkspace->AspectText (Standard_True);
382 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
383 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
30f0ad28 384
b990e557 385 // Bind custom shader program or generate default version
4e1523ef 386 if (aCtx->core20fwd != NULL)
30f0ad28 387 {
b990e557 388 aCtx->ShaderManager()->BindProgram (
389 aTextAspect, aTextAspect->ShaderProgramRes (aCtx));
30f0ad28 390 }
391
a174a3c5 392 // use highlight color or colors from aspect
393 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
394 {
395 render (theWorkspace->PrinterContext(),
30f0ad28 396 aCtx,
a174a3c5 397 *aTextAspect,
398 *theWorkspace->HighlightColor,
399 *theWorkspace->HighlightColor);
400 }
401 else
402 {
403 render (theWorkspace->PrinterContext(),
30f0ad28 404 aCtx,
a174a3c5 405 *aTextAspect,
406 aTextAspect->Color(),
407 aTextAspect->SubtitleColor());
408 }
2166f0fa 409
a174a3c5 410 // restore aspects
411 if (!aPrevTexture.IsNull())
412 {
413 theWorkspace->EnableTexture (aPrevTexture);
414 }
b34efb62 415
416 // restore Z buffer settings
417 if (theWorkspace->UseZBuffer() && theWorkspace->UseDepthTest())
418 {
419 glEnable (GL_DEPTH_TEST);
420 }
a174a3c5 421}
2166f0fa 422
a174a3c5 423// =======================================================================
424// function : Render
425// purpose :
426// =======================================================================
427void OpenGl_Text::Render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
428 const Handle(OpenGl_Context)& theCtx,
429 const OpenGl_AspectText& theTextAspect) const
430{
431 render (thePrintCtx, theCtx, theTextAspect, theTextAspect.Color(), theTextAspect.SubtitleColor());
432}
2166f0fa 433
a174a3c5 434// =======================================================================
435// function : setupMatrix
436// purpose :
437// =======================================================================
438void OpenGl_Text::setupMatrix (const Handle(OpenGl_PrinterContext)& thePrintCtx,
ca3c13d1 439 const Handle(OpenGl_Context)& theCtx,
a174a3c5 440 const OpenGl_AspectText& theTextAspect,
441 const OpenGl_Vec3 theDVec) const
442{
c827ea3a 443 OpenGl_Mat4d aModViewMat;
444
a174a3c5 445 if (myIs2d)
bf75be98 446 {
c827ea3a 447 OpenGl_Utils::Translate<GLdouble> (aModViewMat, myPoint.x() + theDVec.x(), myPoint.y() + theDVec.y(), 0.f);
448 OpenGl_Utils::Scale<GLdouble> (aModViewMat, 1.f, -1.f, 1.f);
449 OpenGl_Utils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.f, 0.f, 1.f);
2166f0fa
SK
450 }
451 else
452 {
a174a3c5 453 // align coordinates to the nearest integer
454 // to avoid extra interpolation issues
455 GLdouble anObjX, anObjY, anObjZ;
c827ea3a 456 OpenGl_Utils::UnProject<Standard_Real> (std::floor (myWinX + theDVec.x()),
457 std::floor (myWinY + theDVec.y()),
458 myWinZ + theDVec.z(),
459 OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
460 OpenGl_Mat4d::Map (myProjMatrix),
461 myViewport,
462 anObjX,
463 anObjY,
464 anObjZ);
465
466 OpenGl_Utils::Translate<GLdouble> (aModViewMat, anObjX, anObjY, anObjZ);
467 OpenGl_Utils::Rotate<GLdouble> (aModViewMat, theTextAspect.Angle(), 0.0, 0.0, 1.0);
468
a174a3c5 469 if (!theTextAspect.IsZoomable())
470 {
471 #ifdef _WIN32
472 // if the context has assigned printer context, use it's parameters
473 if (!thePrintCtx.IsNull())
474 {
475 // get printing scaling in x and y dimensions
476 GLfloat aTextScalex = 1.0f, aTextScaley = 1.0f;
477 thePrintCtx->GetScale (aTextScalex, aTextScaley);
478
479 // text should be scaled in all directions with same
480 // factor to save its proportions, so use height (y) scaling
481 // as it is better for keeping text/3d graphics proportions
c827ea3a 482 OpenGl_Utils::Scale<GLdouble> (aModViewMat, aTextScaley, aTextScaley, aTextScaley);
a174a3c5 483 }
484 #endif
c827ea3a 485 OpenGl_Utils::Scale<GLdouble> (aModViewMat, myScaleHeight, myScaleHeight, myScaleHeight);
a174a3c5 486 }
487 }
c827ea3a 488
489 theCtx->WorldViewState.SetCurrent<Standard_Real> (aModViewMat);
490 theCtx->ApplyWorldViewMatrix();
b990e557 491
492 if (!theCtx->ActiveProgram().IsNull())
493 {
494 // Upload updated state to shader program
495 theCtx->ShaderManager()->PushState (theCtx->ActiveProgram());
496 }
a174a3c5 497}
498
499// =======================================================================
500// function : drawText
501// purpose :
502// =======================================================================
35e08fe8 503
504void OpenGl_Text::drawText (const Handle(OpenGl_PrinterContext)& ,
a174a3c5 505 const Handle(OpenGl_Context)& theCtx,
35e08fe8 506 #ifdef HAVE_GL2PS
a174a3c5 507 const OpenGl_AspectText& theTextAspect) const
35e08fe8 508 #else
509 const OpenGl_AspectText& ) const
510 #endif
a174a3c5 511{
512#ifdef HAVE_GL2PS
513 if (theCtx->IsFeedback())
514 {
515 // position of the text and alignment is calculated by transformation matrix
516 exportText (myString, myIs2d, theTextAspect, (Standard_Integer )myExportHeight);
517 return;
2166f0fa 518 }
a174a3c5 519#endif
2166f0fa 520
7d3e64ef 521 if (myVertsVbo.Length() != myTextures.Length()
522 || myTextures.IsEmpty())
2166f0fa 523 {
7d3e64ef 524 return;
2166f0fa 525 }
7d3e64ef 526
527 for (Standard_Integer anIter = 0; anIter < myTextures.Length(); ++anIter)
a174a3c5 528 {
7d3e64ef 529 const GLuint aTexId = myTextures.Value (anIter);
530 glBindTexture (GL_TEXTURE_2D, aTexId);
a174a3c5 531
7d3e64ef 532 const Handle(OpenGl_VertexBuffer)& aVerts = myVertsVbo.Value (anIter);
533 const Handle(OpenGl_VertexBuffer)& aTCrds = myTCrdsVbo.Value (anIter);
534 aVerts->BindAttribute (theCtx, Graphic3d_TOA_POS);
535 aTCrds->BindAttribute (theCtx, Graphic3d_TOA_UV);
2166f0fa 536
7d3e64ef 537 glDrawArrays (GL_TRIANGLES, 0, GLsizei(aVerts->GetElemsNb()));
2166f0fa 538
b990e557 539 aTCrds->UnbindAttribute (theCtx, Graphic3d_TOA_UV);
7d3e64ef 540 aVerts->UnbindAttribute (theCtx, Graphic3d_TOA_POS);
a174a3c5 541 }
7d3e64ef 542 glBindTexture (GL_TEXTURE_2D, 0);
a174a3c5 543}
2166f0fa 544
a174a3c5 545// =======================================================================
546// function : FontKey
547// purpose :
548// =======================================================================
549TCollection_AsciiString OpenGl_Text::FontKey (const OpenGl_AspectText& theAspect,
550 const Standard_Integer theHeight)
551{
552 const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
553 return theAspect.FontName()
554 + TCollection_AsciiString(":") + Standard_Integer(anAspect)
555 + TCollection_AsciiString(":") + theHeight;
556}
557
558// =======================================================================
559// function : FindFont
560// purpose :
561// =======================================================================
562Handle(OpenGl_Font) OpenGl_Text::FindFont (const Handle(OpenGl_Context)& theCtx,
563 const OpenGl_AspectText& theAspect,
564 const Standard_Integer theHeight,
565 const TCollection_AsciiString theKey)
566{
567 Handle(OpenGl_Font) aFont;
568 if (theHeight < 2)
2166f0fa 569 {
a174a3c5 570 return aFont; // invalid parameters
571 }
2166f0fa 572
a174a3c5 573 if (!theCtx->GetResource (theKey, aFont))
574 {
575 Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
576 const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theAspect.FontName());
577 const Font_FontAspect anAspect = (theAspect.FontAspect() != Font_FA_Undefined) ? theAspect.FontAspect() : Font_FA_Regular;
578 Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, anAspect, theHeight);
579 if (aRequestedFont.IsNull())
2166f0fa 580 {
a174a3c5 581 return aFont;
582 }
583
584 Handle(Font_FTFont) aFontFt = new Font_FTFont (NULL);
585 if (!aFontFt->Init (aRequestedFont->FontPath()->ToCString(), theHeight))
586 {
587 return aFont;
588 }
589
a174a3c5 590 aFont = new OpenGl_Font (aFontFt, theKey);
b34efb62 591
592 if (!aFont->Init (theCtx))
a174a3c5 593 {
b34efb62 594 // out of resources?
a174a3c5 595 }
a174a3c5 596
b34efb62 597 theCtx->ShareResource (theKey, aFont);
a174a3c5 598 }
599 return aFont;
600}
601
602// =======================================================================
603// function : render
604// purpose :
605// =======================================================================
606void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
607 const Handle(OpenGl_Context)& theCtx,
608 const OpenGl_AspectText& theTextAspect,
609 const TEL_COLOUR& theColorText,
610 const TEL_COLOUR& theColorSubs) const
611{
612 if (myString.IsEmpty())
613 {
614 return;
615 }
616
617 const TCollection_AsciiString aFontKey = FontKey (theTextAspect, myParams.Height);
618 if (!myFont.IsNull()
619 && !myFont->ResourceKey().IsEqual (aFontKey))
620 {
621 // font changed
10b9c7df 622 const_cast<OpenGl_Text* > (this)->Release (theCtx.operator->());
a174a3c5 623 }
624
625 if (myFont.IsNull())
626 {
627 myFont = FindFont (theCtx, theTextAspect, myParams.Height, aFontKey);
628 if (myFont.IsNull())
629 {
630 return;
631 }
632 }
633
634 if (myTextures.IsEmpty())
635 {
636 OpenGl_TextFormatter aFormatter;
637 aFormatter.SetupAlignment (myParams.HAlign, myParams.VAlign);
638 aFormatter.Reset();
639 aFormatter.Append (theCtx, myString, *myFont.operator->());
640 aFormatter.Format();
641
7d3e64ef 642 aFormatter.Result (theCtx, myTextures, myVertsVbo, myTCrdsVbo);
a174a3c5 643 aFormatter.BndBox (myBndBox);
644 }
645
646 if (myTextures.IsEmpty())
647 {
648 return;
649 }
650
651 myExportHeight = 1.0f;
652 myScaleHeight = 1.0f;
653
c827ea3a 654 theCtx->WorldViewState.Push();
655
c827ea3a 656 myModelMatrix.Convert (theCtx->WorldViewState.Current() * theCtx->ModelWorldState.Current());
657
a174a3c5 658 if (!myIs2d)
659 {
a174a3c5 660 glGetIntegerv (GL_VIEWPORT, myViewport);
c827ea3a 661 myProjMatrix.Convert (theCtx->ProjectionState.Current());
662
663 OpenGl_Utils::Project<Standard_Real> (myPoint.x(),
664 myPoint.y(),
665 myPoint.z(),
666 myModelMatrix,
667 myProjMatrix,
668 myViewport,
669 myWinX,
670 myWinY,
671 myWinZ);
a174a3c5 672
673 // compute scale factor for constant text height
674 GLdouble x1, y1, z1;
c827ea3a 675 OpenGl_Utils::UnProject<Standard_Real> (myWinX,
676 myWinY,
677 myWinZ,
678 OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
679 myProjMatrix,
680 myViewport,
681 x1,
682 y1,
683 z1);
a174a3c5 684
685 GLdouble x2, y2, z2;
686 const GLdouble h = (GLdouble )myFont->FTFont()->PointSize();
c827ea3a 687 OpenGl_Utils::UnProject<Standard_Real> (myWinX,
688 myWinY + h,
689 myWinZ,
690 OpenGl_Mat4d::Map (THE_IDENTITY_MATRIX),
691 myProjMatrix,
692 myViewport,
693 x2,
694 y2,
695 z2);
a174a3c5 696
697 myScaleHeight = (y2 - y1) / h;
698 if (theTextAspect.IsZoomable())
699 {
700 myExportHeight = (float )h;
701 }
702 }
703 myExportHeight = (float )myFont->FTFont()->PointSize() / myExportHeight;
704
b990e557 705#if !defined(GL_ES_VERSION_2_0)
706
5322131b 707 glDisable (GL_LIGHTING);
a174a3c5 708
709 // setup depth test
710 if (!myIs2d
711 && theTextAspect.StyleType() != Aspect_TOST_ANNOTATION)
712 {
713 glEnable (GL_DEPTH_TEST);
714 }
715 else
716 {
717 glDisable (GL_DEPTH_TEST);
718 }
719
720 // setup alpha test
721 GLint aTexEnvParam = GL_REPLACE;
722 glGetTexEnviv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, &aTexEnvParam);
723 if (aTexEnvParam != GL_REPLACE)
724 {
725 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
726 }
727 glAlphaFunc (GL_GEQUAL, 0.285f);
728 glEnable (GL_ALPHA_TEST);
729
730 // setup blending
731 glEnable (GL_BLEND);
732 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
733
734 // activate texture unit
735 glDisable (GL_TEXTURE_1D);
736 glEnable (GL_TEXTURE_2D);
01ca42b2 737 if (theCtx->core15fwd != NULL)
a174a3c5 738 {
01ca42b2 739 theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
a174a3c5 740 }
741
742 // extra drawings
743 switch (theTextAspect.DisplayType())
744 {
2166f0fa 745 case Aspect_TODT_BLEND:
a174a3c5 746 {
747 glEnable (GL_COLOR_LOGIC_OP);
748 glLogicOp (GL_XOR);
2166f0fa 749 break;
a174a3c5 750 }
2166f0fa
SK
751 case Aspect_TODT_SUBTITLE:
752 {
ca3c13d1 753 theCtx->core11->glColor3fv (theColorSubs.rgb);
a174a3c5 754 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
755
756 glBindTexture (GL_TEXTURE_2D, 0);
757 glBegin (GL_QUADS);
758 glVertex2f (myBndBox.Left, myBndBox.Top);
759 glVertex2f (myBndBox.Right, myBndBox.Top);
760 glVertex2f (myBndBox.Right, myBndBox.Bottom);
761 glVertex2f (myBndBox.Left, myBndBox.Bottom);
2166f0fa
SK
762 glEnd();
763 break;
764 }
2166f0fa 765 case Aspect_TODT_DEKALE:
a174a3c5 766 {
b990e557 767 theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorSubs.rgb);
a174a3c5 768 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, +1.0f, 0.00001f));
769 drawText (thePrintCtx, theCtx, theTextAspect);
770 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, -1.0f, 0.00001f));
771 drawText (thePrintCtx, theCtx, theTextAspect);
772 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (-1.0f, +1.0f, 0.00001f));
773 drawText (thePrintCtx, theCtx, theTextAspect);
774 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (+1.0f, -1.0f, 0.00001f));
775 drawText (thePrintCtx, theCtx, theTextAspect);
776 break;
777 }
a6eb515f 778 case Aspect_TODT_DIMENSION:
a174a3c5 779 case Aspect_TODT_NORMAL:
780 {
2166f0fa
SK
781 break;
782 }
783 }
784
a174a3c5 785 // main draw call
b990e557 786 theCtx->SetColor4fv (*(const OpenGl_Vec4* )theColorText.rgb);
a174a3c5 787 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.0f));
788 drawText (thePrintCtx, theCtx, theTextAspect);
2166f0fa 789
a174a3c5 790 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aTexEnvParam);
543f0db0 791
792 if (theTextAspect.DisplayType() == Aspect_TODT_DIMENSION)
793 {
794 setupMatrix (thePrintCtx, theCtx, theTextAspect, OpenGl_Vec3 (0.0f, 0.0f, 0.00001f));
795
796 glDisable (GL_BLEND);
797 glDisable (GL_TEXTURE_2D);
798 glDisable (GL_ALPHA_TEST);
799 if (!myIs2d)
800 {
801 glDisable (GL_DEPTH_TEST);
802 }
803 glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
804
805 glClear (GL_STENCIL_BUFFER_BIT);
806 glEnable (GL_STENCIL_TEST);
807 glStencilFunc (GL_ALWAYS, 1, 0xFF);
808 glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
809
810 glBegin (GL_QUADS);
811 glVertex2f (myBndBox.Left, myBndBox.Top);
812 glVertex2f (myBndBox.Right, myBndBox.Top);
813 glVertex2f (myBndBox.Right, myBndBox.Bottom);
814 glVertex2f (myBndBox.Left, myBndBox.Bottom);
815 glEnd();
816
817 glStencilFunc (GL_ALWAYS, 0, 0xFF);
543f0db0 818
819 glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
820 }
821
b34efb62 822 // reset OpenGL state
823 glDisable (GL_BLEND);
824 glDisable (GL_ALPHA_TEST);
825 glDisable (GL_STENCIL_TEST);
826 glDisable (GL_COLOR_LOGIC_OP);
c827ea3a 827
828 // model view matrix was modified
829 theCtx->WorldViewState.Pop();
830 theCtx->ApplyModelViewMatrix();
ca3c13d1 831#endif
5e27df78 832}