1#!/usr/bin/env python
2
3# This example demonstrates the use of multiline 2D text using
4# vtkTextMappers.  It shows several justifications as well as
5# single-line and multiple-line text inputs.
6
7import vtk
8
9font_size = 14
10
11# Create the text mappers and the associated Actor2Ds.
12
13# The font and text properties (except justification) are the same for
14# each single line mapper. Let's create a common text property object
15singleLineTextProp = vtk.vtkTextProperty()
16singleLineTextProp.SetFontSize(font_size)
17singleLineTextProp.SetFontFamilyToArial()
18singleLineTextProp.BoldOff()
19singleLineTextProp.ItalicOff()
20singleLineTextProp.ShadowOff()
21
22# The font and text properties (except justification) are the same for
23# each multi line mapper. Let's create a common text property object
24multiLineTextProp = vtk.vtkTextProperty()
25multiLineTextProp.ShallowCopy(singleLineTextProp)
26multiLineTextProp.BoldOn()
27multiLineTextProp.ItalicOn()
28multiLineTextProp.ShadowOn()
29multiLineTextProp.SetLineSpacing(0.8)
30
31# The text is on a single line and bottom-justified.
32singleLineTextB = vtk.vtkTextMapper()
33singleLineTextB.SetInput("Single line (bottom)")
34tprop = singleLineTextB.GetTextProperty()
35tprop.ShallowCopy(singleLineTextProp)
36tprop.SetVerticalJustificationToBottom()
37tprop.SetColor(1, 0, 0)
38singleLineTextActorB = vtk.vtkActor2D()
39singleLineTextActorB.SetMapper(singleLineTextB)
40singleLineTextActorB.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
41singleLineTextActorB.GetPositionCoordinate().SetValue(0.05, 0.85)
42
43# The text is on a single line and center-justified (vertical
44# justification).
45singleLineTextC = vtk.vtkTextMapper()
46singleLineTextC.SetInput("Single line (centered)")
47tprop = singleLineTextC.GetTextProperty()
48tprop.ShallowCopy(singleLineTextProp)
49tprop.SetVerticalJustificationToCentered()
50tprop.SetColor(0, 1, 0)
51singleLineTextActorC = vtk.vtkActor2D()
52singleLineTextActorC.SetMapper(singleLineTextC)
53singleLineTextActorC.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
54singleLineTextActorC.GetPositionCoordinate().SetValue(0.05, 0.75)
55
56# The text is on a single line and top-justified.
57singleLineTextT = vtk.vtkTextMapper()
58singleLineTextT.SetInput("Single line (top)")
59tprop = singleLineTextT.GetTextProperty()
60tprop.ShallowCopy(singleLineTextProp)
61tprop.SetVerticalJustificationToTop()
62tprop.SetColor(0, 0, 1)
63singleLineTextActorT = vtk.vtkActor2D()
64singleLineTextActorT.SetMapper(singleLineTextT)
65singleLineTextActorT.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
66singleLineTextActorT.GetPositionCoordinate().SetValue(0.05, 0.65)
67
68# The text is on multiple lines and left- and top-justified.
69textMapperL = vtk.vtkTextMapper()
70textMapperL.SetInput("This is\nmulti-line\ntext output\n(left-top)")
71tprop = textMapperL.GetTextProperty()
72tprop.ShallowCopy(multiLineTextProp)
73tprop.SetJustificationToLeft()
74tprop.SetVerticalJustificationToTop()
75tprop.SetColor(1, 0, 0)
76textActorL = vtk.vtkActor2D()
77textActorL.SetMapper(textMapperL)
78textActorL.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
79textActorL.GetPositionCoordinate().SetValue(0.05, 0.5)
80
81# The text is on multiple lines and center-justified (both horizontal and
82# vertical).
83textMapperC = vtk.vtkTextMapper()
84textMapperC.SetInput("This is\nmulti-line\ntext output\n(centered)")
85tprop = textMapperC.GetTextProperty()
86tprop.ShallowCopy(multiLineTextProp)
87tprop.SetJustificationToCentered()
88tprop.SetVerticalJustificationToCentered()
89tprop.SetColor(0, 1, 0)
90textActorC = vtk.vtkActor2D()
91textActorC.SetMapper(textMapperC)
92textActorC.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
93textActorC.GetPositionCoordinate().SetValue(0.5, 0.5)
94
95# The text is on multiple lines and right- and bottom-justified.
96textMapperR = vtk.vtkTextMapper()
97textMapperR.SetInput("This is\nmulti-line\ntext output\n(right-bottom)")
98tprop = textMapperR.GetTextProperty()
99tprop.ShallowCopy(multiLineTextProp)
100tprop.SetJustificationToRight()
101tprop.SetVerticalJustificationToBottom()
102tprop.SetColor(0, 0, 1)
103textActorR = vtk.vtkActor2D()
104textActorR.SetMapper(textMapperR)
105textActorR.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
106textActorR.GetPositionCoordinate().SetValue(0.95, 0.5)
107
108# Draw the grid to demonstrate the placement of the text.
109
110# Set up the necessary points.
111Pts = vtk.vtkPoints()
112Pts.InsertNextPoint(0.05, 0.0, 0.0)
113Pts.InsertNextPoint(0.05, 1.0, 0.0)
114Pts.InsertNextPoint(0.5, 0.0, 0.0)
115Pts.InsertNextPoint(0.5, 1.0, 0.0)
116Pts.InsertNextPoint(0.95, 0.0, 0.0)
117Pts.InsertNextPoint(0.95, 1.0, 0.0)
118Pts.InsertNextPoint(0.0, 0.5, 0.0)
119Pts.InsertNextPoint(1.0, 0.5, 0.0)
120Pts.InsertNextPoint(0.00, 0.85, 0.0)
121Pts.InsertNextPoint(0.50, 0.85, 0.0)
122Pts.InsertNextPoint(0.00, 0.75, 0.0)
123Pts.InsertNextPoint(0.50, 0.75, 0.0)
124Pts.InsertNextPoint(0.00, 0.65, 0.0)
125Pts.InsertNextPoint(0.50, 0.65, 0.0)
126# Set up the lines that use these points.
127Lines = vtk.vtkCellArray()
128Lines.InsertNextCell(2)
129Lines.InsertCellPoint(0)
130Lines.InsertCellPoint(1)
131Lines.InsertNextCell(2)
132Lines.InsertCellPoint(2)
133Lines.InsertCellPoint(3)
134Lines.InsertNextCell(2)
135Lines.InsertCellPoint(4)
136Lines.InsertCellPoint(5)
137Lines.InsertNextCell(2)
138Lines.InsertCellPoint(6)
139Lines.InsertCellPoint(7)
140Lines.InsertNextCell(2)
141Lines.InsertCellPoint(8)
142Lines.InsertCellPoint(9)
143Lines.InsertNextCell(2)
144Lines.InsertCellPoint(10)
145Lines.InsertCellPoint(11)
146Lines.InsertNextCell(2)
147Lines.InsertCellPoint(12)
148Lines.InsertCellPoint(13)
149# Create a grid that uses these points and lines.
150Grid = vtk.vtkPolyData()
151Grid.SetPoints(Pts)
152Grid.SetLines(Lines)
153# Set up the coordinate system.
154normCoords = vtk.vtkCoordinate()
155normCoords.SetCoordinateSystemToNormalizedViewport()
156
157# Set up the mapper and actor (2D) for the grid.
158mapper = vtk.vtkPolyDataMapper2D()
159mapper.SetInputData(Grid)
160mapper.SetTransformCoordinate(normCoords)
161gridActor = vtk.vtkActor2D()
162gridActor.SetMapper(mapper)
163gridActor.GetProperty().SetColor(0.1, 0.1, 0.1)
164
165# Create the Renderer, RenderWindow, and RenderWindowInteractor
166ren = vtk.vtkRenderer()
167renWin = vtk.vtkRenderWindow()
168renWin.AddRenderer(ren)
169iren = vtk.vtkRenderWindowInteractor()
170iren.SetRenderWindow(renWin)
171
172# Add the actors to the renderer; set the background and size; zoom in
173# closer to the image; render
174ren.AddActor2D(textActorL)
175ren.AddActor2D(textActorC)
176ren.AddActor2D(textActorR)
177ren.AddActor2D(singleLineTextActorB)
178ren.AddActor2D(singleLineTextActorC)
179ren.AddActor2D(singleLineTextActorT)
180ren.AddActor2D(gridActor)
181
182ren.SetBackground(1, 1, 1)
183renWin.SetSize(500, 300)
184ren.GetActiveCamera().Zoom(1.5)
185
186iren.Initialize()
187renWin.Render()
188iren.Start()
189