1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestMathTextFreeTypeTextRenderer.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 "vtkRenderer.h"
20 #include "vtkRenderWindow.h"
21 #include "vtkRenderWindowInteractor.h"
22 #include "vtkStdString.h"
23 #include "vtkTextActor.h"
24 #include "vtkTextProperty.h"
25 
26 #include <iostream>
27 #include <string>
28 
29 //----------------------------------------------------------------------------
TestFontDPIScaling(int argc,char * argv[])30 int TestFontDPIScaling(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   vtkStdString str = "Sample multiline\ntext rendered\nusing FreeTypeTools.";
41 
42   vtkNew<vtkTextActor> actor1;
43   actor1->GetTextProperty()->SetFontSize(20);
44   actor1->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
45   actor1->GetTextProperty()->SetJustificationToLeft();
46   actor1->GetTextProperty()->SetVerticalJustificationToTop();
47   actor1->GetTextProperty()->SetFontFamilyToTimes();
48   actor1->SetInput(str.c_str());
49   actor1->SetPosition(10, 590);
50 
51   vtkNew<vtkTextActor> actor2;
52   actor2->GetTextProperty()->SetFontSize(20);
53   actor2->GetTextProperty()->SetColor(0.0, 1.0, 0.0);
54   actor2->GetTextProperty()->SetJustificationToRight();
55   actor2->GetTextProperty()->SetVerticalJustificationToTop();
56   actor2->GetTextProperty()->SetFontFamilyToCourier();
57   actor2->SetInput(str.c_str());
58   actor2->SetPosition(590, 590);
59 
60   vtkNew<vtkTextActor> actor3;
61   actor3->GetTextProperty()->SetFontSize(20);
62   actor3->GetTextProperty()->SetColor(0.0, 0.0, 1.0);
63   actor3->GetTextProperty()->SetJustificationToLeft();
64   actor3->GetTextProperty()->SetVerticalJustificationToBottom();
65   actor3->GetTextProperty()->SetItalic(1);
66   actor3->SetInput(str.c_str());
67   actor3->SetPosition(10, 10);
68 
69   vtkNew<vtkTextActor> actor4;
70   actor4->GetTextProperty()->SetFontSize(20);
71   actor4->GetTextProperty()->SetColor(0.3, 0.4, 0.5);
72   actor4->GetTextProperty()->SetJustificationToRight();
73   actor4->GetTextProperty()->SetVerticalJustificationToBottom();
74   actor4->GetTextProperty()->SetBold(1);
75   actor4->GetTextProperty()->SetShadow(1);
76   actor4->GetTextProperty()->SetShadowOffset(-3, 2);
77   actor4->SetInput(str.c_str());
78   actor4->SetPosition(590, 10);
79 
80   vtkNew<vtkTextActor> actor5;
81   actor5->GetTextProperty()->SetFontSize(20);
82   actor5->GetTextProperty()->SetColor(1.0, 1.0, 0.0);
83   actor5->GetTextProperty()->SetJustificationToCentered();
84   actor5->GetTextProperty()->SetVerticalJustificationToCentered();
85   actor5->GetTextProperty()->SetBold(1);
86   actor5->GetTextProperty()->SetItalic(1);
87   actor5->GetTextProperty()->SetShadow(1);
88   actor5->GetTextProperty()->SetShadowOffset(5, -8);
89   actor5->SetInput(str.c_str());
90   actor5->SetPosition(300, 300);
91 
92   vtkNew<vtkTextActor> actor6;
93   actor6->GetTextProperty()->SetFontSize(16);
94   actor6->GetTextProperty()->SetColor(1.0, 0.5, 0.2);
95   actor6->GetTextProperty()->SetJustificationToCentered();
96   actor6->GetTextProperty()->SetVerticalJustificationToCentered();
97   actor6->GetTextProperty()->SetOrientation(45);
98   actor6->SetInput(str.c_str());
99   actor6->SetPosition(300, 450);
100 
101   vtkNew<vtkTextActor> actor7;
102   actor7->GetTextProperty()->SetFontSize(16);
103   actor7->GetTextProperty()->SetColor(0.5, 0.2, 1.0);
104   actor7->GetTextProperty()->SetJustificationToLeft();
105   actor7->GetTextProperty()->SetVerticalJustificationToCentered();
106   actor7->GetTextProperty()->SetOrientation(45);
107   actor7->SetInput(str.c_str());
108   actor7->SetPosition(100, 156);
109 
110   vtkNew<vtkTextActor> actor8;
111   actor8->GetTextProperty()->SetFontSize(16);
112   actor8->GetTextProperty()->SetColor(0.8, 1.0, 0.3);
113   actor8->GetTextProperty()->SetJustificationToRight();
114   actor8->GetTextProperty()->SetVerticalJustificationToCentered();
115   actor8->GetTextProperty()->SetOrientation(45);
116   actor8->SetInput(str.c_str());
117   actor8->SetPosition(500, 249);
118 
119   // Mathtext tests
120 
121   // Test that escaped "$" are passed through to freetype:
122   vtkNew<vtkTextActor> actor9;
123   actor9->GetTextProperty()->SetFontSize(12);
124   actor9->GetTextProperty()->SetColor(0.2, 0.5, 1.0);
125   actor9->SetInput("Escaped dollar signs:\n\\$10, \\$20");
126   actor9->SetPosition(100, 450);
127 
128   vtkNew<vtkTextActor> actor10;
129   actor10->GetTextProperty()->SetFontSize(16);
130   actor10->GetTextProperty()->SetColor(0.5, 0.2, 1.0);
131   actor10->GetTextProperty()->SetJustificationToRight();
132   actor10->GetTextProperty()->SetOrientation(45);
133   actor10->SetInput("Test MathText $\\int_0^\\infty\\frac{2\\pi}"
134                     "{x - \\frac{z}{4}}\\,dx$");
135   actor10->SetPosition(588, 433);
136 
137   // Invalid latex markup -- should fallback to freetype.
138   vtkNew<vtkTextActor> actor11;
139   actor11->GetTextProperty()->SetFontSize(15);
140   actor11->GetTextProperty()->SetColor(1.0, 0.5, 0.2);
141   actor11->SetInput("Test FreeType fallback:\n$\\asdf$");
142   actor11->SetPosition(10, 350);
143 
144   // Both $...$ and \\$
145   vtkNew<vtkTextActor> actor12;
146   actor12->GetTextProperty()->SetFontSize(18);
147   actor12->GetTextProperty()->SetColor(0.0, 1.0, 0.7);
148   actor12->SetInput("Test MathText '\\$' $\\$\\sqrt[3]{8}$");
149   actor12->SetPosition(10, 300);
150 
151   // $...$ without other text.
152   vtkNew<vtkTextActor> actor13;
153   actor13->GetTextProperty()->SetFontSize(18);
154   actor13->GetTextProperty()->SetColor(0.2, 1.0, 1.0);
155   actor13->SetInput("$A = \\pi r^2$");
156   actor13->SetPosition(10, 250);
157 
158   // Numbers, using courier, Text that gets 'cut off'
159   vtkNew<vtkTextActor> actor14;
160   actor14->GetTextProperty()->SetFontSize(21);
161   actor14->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
162   actor14->GetTextProperty()->SetJustificationToCentered();
163   actor14->GetTextProperty()->SetVerticalJustificationToCentered();
164   actor14->GetTextProperty()->SetBold(1);
165   actor14->GetTextProperty()->SetItalic(1);
166   actor14->GetTextProperty()->SetFontFamilyToCourier();
167   actor14->SetInput("4.0");
168   actor14->SetPosition(500, 400);
169 
170   // UTF-8 freetype handling:
171   vtkNew<vtkTextActor> actor15;
172   actor15->GetTextProperty()->SetFontFamily(VTK_FONT_FILE);
173   actor15->GetTextProperty()->SetFontFile(uncodeFontFile.c_str());
174   actor15->GetTextProperty()->SetJustificationToCentered();
175   actor15->GetTextProperty()->SetVerticalJustificationToCentered();
176   actor15->GetTextProperty()->SetFontSize(18);
177   actor15->GetTextProperty()->SetColor(0.0, 1.0, 0.7);
178   actor15->SetInput("UTF-8 FreeType: \xce\xa8\xd2\x94\xd2\x96\xd1\x84\xd2\xbe");
179   actor15->SetPosition(300, 110);
180 
181   // Test for rotated kerning (PR#15301)
182   vtkNew<vtkTextActor> actor16;
183   actor16->GetTextProperty()->SetFontFile(uncodeFontFile.c_str());
184   actor16->GetTextProperty()->SetFontFamily(VTK_FONT_FILE);
185   actor16->GetTextProperty()->SetJustificationToCentered();
186   actor16->GetTextProperty()->SetVerticalJustificationToCentered();
187   actor16->GetTextProperty()->SetFontSize(18);
188   actor16->GetTextProperty()->SetOrientation(90);
189   actor16->GetTextProperty()->SetColor(0.0, 1.0, 0.7);
190   actor16->SetInput("oTeVaVoVAW");
191   actor16->SetPosition(300, 200);
192 
193   vtkNew<vtkRenderer> ren;
194   ren->SetBackground(0.1, 0.1, 0.1);
195   vtkNew<vtkRenderWindow> win;
196   win->SetDPI(96);
197   win->SetSize(600, 600);
198   win->AddRenderer(ren);
199   vtkNew<vtkRenderWindowInteractor> iren;
200   iren->SetRenderWindow(win);
201 
202   ren->AddActor(actor1);
203   ren->AddActor(actor2);
204   ren->AddActor(actor3);
205   ren->AddActor(actor4);
206   ren->AddActor(actor5);
207   ren->AddActor(actor6);
208   ren->AddActor(actor7);
209   ren->AddActor(actor8);
210   ren->AddActor(actor9);
211   ren->AddActor(actor10);
212   ren->AddActor(actor11);
213   ren->AddActor(actor12);
214   ren->AddActor(actor13);
215   ren->AddActor(actor14);
216   ren->AddActor(actor15);
217   ren->AddActor(actor16);
218 
219   win->SetMultiSamples(0);
220   win->Render();
221   win->GetInteractor()->Initialize();
222   win->GetInteractor()->Start();
223 
224   return EXIT_SUCCESS;
225 }
226