1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestMathTextFreeTypeTextRendererNoMath.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #include "vtkTextRenderer.h"
17 
18 #include "vtkNew.h"
19 #include "vtkRenderWindow.h"
20 #include "vtkRenderWindowInteractor.h"
21 #include "vtkRenderer.h"
22 #include "vtkStdString.h"
23 #include "vtkTextActor.h"
24 #include "vtkTextProperty.h"
25 
26 #include <iostream>
27 #include <string>
28 
29 //------------------------------------------------------------------------------
TestMathTextFreeTypeTextRendererNoMath(int argc,char * argv[])30 int TestMathTextFreeTypeTextRendererNoMath(int argc, char* argv[])
31 {
32   if (argc < 2)
33   {
34     cerr << "Missing font filename." << endl;
35     return EXIT_FAILURE;
36   }
37 
38   std::string uncodeFontFile(argv[1]);
39 
40   vtkNew<vtkTextRenderer> tren;
41   if (tren == nullptr)
42   {
43     std::cerr << "Object factory cannot find vtkTextRenderer override.\n";
44     return EXIT_FAILURE;
45   }
46 
47   if (strcmp(tren->GetClassName(), "vtkMathTextFreeTypeTextRenderer") != 0)
48   {
49     std::cerr << "Object factory returning unrecognized vtkTextRenderer "
50                  "override: "
51               << tren->GetClassName() << std::endl;
52     return EXIT_FAILURE;
53   }
54 
55   vtkStdString str = "Sample multiline\ntext rendered\nusing FreeTypeTools.";
56 
57   vtkNew<vtkTextActor> actor1;
58   actor1->GetTextProperty()->SetFontSize(20);
59   actor1->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
60   actor1->GetTextProperty()->SetJustificationToLeft();
61   actor1->GetTextProperty()->SetVerticalJustificationToTop();
62   actor1->GetTextProperty()->SetFontFamilyToTimes();
63   actor1->SetInput(str.c_str());
64   actor1->SetPosition(10, 590);
65 
66   vtkNew<vtkTextActor> actor2;
67   actor2->GetTextProperty()->SetFontSize(20);
68   actor2->GetTextProperty()->SetColor(0.0, 1.0, 0.0);
69   actor2->GetTextProperty()->SetJustificationToRight();
70   actor2->GetTextProperty()->SetVerticalJustificationToTop();
71   actor2->GetTextProperty()->SetFontFamilyToCourier();
72   actor2->SetInput(str.c_str());
73   actor2->SetPosition(590, 590);
74 
75   vtkNew<vtkTextActor> actor3;
76   actor3->GetTextProperty()->SetFontSize(20);
77   actor3->GetTextProperty()->SetColor(0.0, 0.0, 1.0);
78   actor3->GetTextProperty()->SetJustificationToLeft();
79   actor3->GetTextProperty()->SetVerticalJustificationToBottom();
80   actor3->GetTextProperty()->SetItalic(1);
81   actor3->SetInput(str.c_str());
82   actor3->SetPosition(10, 10);
83 
84   vtkNew<vtkTextActor> actor4;
85   actor4->GetTextProperty()->SetFontSize(20);
86   actor4->GetTextProperty()->SetColor(0.3, 0.4, 0.5);
87   actor4->GetTextProperty()->SetJustificationToRight();
88   actor4->GetTextProperty()->SetVerticalJustificationToBottom();
89   actor4->GetTextProperty()->SetBold(1);
90   actor4->GetTextProperty()->SetShadow(1);
91   actor4->GetTextProperty()->SetShadowOffset(-3, 2);
92   actor4->SetInput(str.c_str());
93   actor4->SetPosition(590, 10);
94 
95   vtkNew<vtkTextActor> actor5;
96   actor5->GetTextProperty()->SetFontSize(20);
97   actor5->GetTextProperty()->SetColor(1.0, 1.0, 0.0);
98   actor5->GetTextProperty()->SetJustificationToCentered();
99   actor5->GetTextProperty()->SetVerticalJustificationToCentered();
100   actor5->GetTextProperty()->SetBold(1);
101   actor5->GetTextProperty()->SetItalic(1);
102   actor5->GetTextProperty()->SetShadow(1);
103   actor5->GetTextProperty()->SetShadowOffset(5, -8);
104   actor5->SetInput(str.c_str());
105   actor5->SetPosition(300, 300);
106 
107   vtkNew<vtkTextActor> actor6;
108   actor6->GetTextProperty()->SetFontSize(16);
109   actor6->GetTextProperty()->SetColor(1.0, 0.5, 0.2);
110   actor6->GetTextProperty()->SetJustificationToCentered();
111   actor6->GetTextProperty()->SetVerticalJustificationToCentered();
112   actor6->GetTextProperty()->SetOrientation(45);
113   actor6->SetInput(str.c_str());
114   actor6->SetPosition(300, 450);
115 
116   vtkNew<vtkTextActor> actor7;
117   actor7->GetTextProperty()->SetFontSize(16);
118   actor7->GetTextProperty()->SetColor(0.5, 0.2, 1.0);
119   actor7->GetTextProperty()->SetJustificationToLeft();
120   actor7->GetTextProperty()->SetVerticalJustificationToCentered();
121   actor7->GetTextProperty()->SetOrientation(45);
122   actor7->SetInput(str.c_str());
123   actor7->SetPosition(100, 156);
124 
125   vtkNew<vtkTextActor> actor8;
126   actor8->GetTextProperty()->SetFontSize(16);
127   actor8->GetTextProperty()->SetColor(0.8, 1.0, 0.3);
128   actor8->GetTextProperty()->SetJustificationToRight();
129   actor8->GetTextProperty()->SetVerticalJustificationToCentered();
130   actor8->GetTextProperty()->SetOrientation(45);
131   actor8->SetInput(str.c_str());
132   actor8->SetPosition(500, 249);
133 
134   // Numbers, using courier, Text that gets 'cut off'
135   vtkNew<vtkTextActor> actor9;
136   actor9->GetTextProperty()->SetFontSize(21);
137   actor9->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
138   actor9->GetTextProperty()->SetJustificationToCentered();
139   actor9->GetTextProperty()->SetVerticalJustificationToCentered();
140   actor9->GetTextProperty()->SetBold(1);
141   actor9->GetTextProperty()->SetItalic(1);
142   actor9->GetTextProperty()->SetFontFamilyToCourier();
143   actor9->SetInput("4.0");
144   actor9->SetPosition(500, 400);
145 
146   // UTF-8 freetype handling:
147   vtkNew<vtkTextActor> actor10;
148   actor10->GetTextProperty()->SetFontFamily(VTK_FONT_FILE);
149   actor10->GetTextProperty()->SetFontFile(uncodeFontFile.c_str());
150   actor10->GetTextProperty()->SetJustificationToCentered();
151   actor10->GetTextProperty()->SetVerticalJustificationToCentered();
152   actor10->GetTextProperty()->SetFontSize(18);
153   actor10->GetTextProperty()->SetColor(0.0, 1.0, 0.7);
154   actor10->SetInput("UTF-8 FreeType: \xce\xa8\xd2\x94\xd2\x96\xd1\x84\xd2\xbe");
155   actor10->SetPosition(300, 110);
156 
157   // Boring rendering setup....
158 
159   vtkNew<vtkRenderer> ren;
160   ren->SetBackground(0.1, 0.1, 0.1);
161   vtkNew<vtkRenderWindow> win;
162   win->SetSize(600, 600);
163   win->AddRenderer(ren);
164   vtkNew<vtkRenderWindowInteractor> iren;
165   iren->SetRenderWindow(win);
166 
167   ren->AddActor(actor1);
168   ren->AddActor(actor2);
169   ren->AddActor(actor3);
170   ren->AddActor(actor4);
171   ren->AddActor(actor5);
172   ren->AddActor(actor6);
173   ren->AddActor(actor7);
174   ren->AddActor(actor8);
175   ren->AddActor(actor9);
176   ren->AddActor(actor10);
177 
178   win->SetMultiSamples(0);
179   win->Render();
180   win->GetInteractor()->Initialize();
181   win->GetInteractor()->Start();
182 
183   return EXIT_SUCCESS;
184 }
185