Vuo  2.0.0
VuoSceneText.c
Go to the documentation of this file.
1 
10 #include "VuoSceneText.h"
11 #include "VuoImageText.h"
12 #include "module.h"
13 
14 #ifdef VUO_COMPILER
16  "title" : "VuoSceneText",
17  "dependencies" : [
18  "VuoImageText",
19  "VuoSceneObject",
20  "VuoFont",
21  "VuoMesh"
22  ]
23  });
24 #endif
25 
33 {
34  unsigned int vertexCount = 4;
35 
36  VuoPoint4d *positions = (VuoPoint4d *)malloc(sizeof(VuoPoint4d) * vertexCount);
37  positions[0] = VuoPoint4d_make(-.5,-.5,0,1);
38  positions[1] = VuoPoint4d_make( .5,-.5,0,1);
39  positions[2] = VuoPoint4d_make(-.5, .5,0,1);
40  positions[3] = VuoPoint4d_make( .5, .5,0,1);
41 
42  VuoPoint4d *textureCoordinates = (VuoPoint4d *)malloc(sizeof(VuoPoint4d) * vertexCount);
43  textureCoordinates[0] = VuoPoint4d_make(0,0,0,1);
44  textureCoordinates[1] = VuoPoint4d_make(1,0,0,1);
45  textureCoordinates[2] = VuoPoint4d_make(0,1,0,1);
46  textureCoordinates[3] = VuoPoint4d_make(1,1,0,1);
47 
48  unsigned int elementCount = 6;
49  unsigned int *elements = (unsigned int *)malloc(sizeof(unsigned int) * elementCount);
50  // Order the elements so that the diagonal edge of each triangle
51  // is last, so that vuo.shader.make.wireframe can optionally omit them.
52  elements[0] = 2;
53  elements[1] = 0;
54  elements[2] = 1;
55  elements[3] = 1;
56  elements[4] = 3;
57  elements[5] = 2;
58 
59  float horizontal = VuoAnchor_getHorizontal(anchor) == VuoHorizontalAlignment_Left ? .5f : (VuoAnchor_getHorizontal(anchor) == VuoHorizontalAlignment_Right ? -.5f : 0.f);
60  float vertical = VuoAnchor_getVertical(anchor) == VuoVerticalAlignment_Top ? -.5f : (VuoAnchor_getVertical(anchor) == VuoVerticalAlignment_Bottom ? .5f : 0.f);
61 
62  for(int i = 0; i < vertexCount; i++)
63  {
64  positions[i].x += horizontal;
65  positions[i].y += vertical;
66  }
67 
68  VuoSubmesh sm = VuoSubmesh_makeFromBuffers(vertexCount,
69  positions, NULL, NULL, NULL, textureCoordinates,
70  elementCount, elements, VuoMesh_IndividualTriangles);
72 }
73 
84 VuoSceneObject VuoSceneText_make(const VuoText text, const VuoFont font, const VuoBoolean scaleWithScene, const VuoReal wrapWidth, const VuoAnchor anchor)
85 {
86  VuoSceneObject so = VuoSceneObject_makeText(text, font, scaleWithScene, wrapWidth);
87  so.mesh = makeAnchoredQuad(anchor);
88  return so;
89 }
90 
96 {
97  VuoPoint2d mesh0 = (VuoPoint2d){ so.mesh->submeshes[0].positions[0].x, so.mesh->submeshes[0].positions[0].y };
100 
101  // (We know from VuoSceneText_make() that the mesh's first coordinate
102  // is 0.0, -0.5, or -1.0, depending on whether the anchor is left/top, center, or right/bottom, respectively.)
103  if (VuoReal_areEqual(mesh0.x, 0))
104  h = VuoHorizontalAlignment_Left;
105  else if (VuoReal_areEqual(mesh0.x, -.5))
106  h = VuoHorizontalAlignment_Center;
107  else //if (VuoReal_areEqual(mesh0.x, -1))
108  h = VuoHorizontalAlignment_Right;
109 
110  if (VuoReal_areEqual(mesh0.y, -1))
111  v = VuoVerticalAlignment_Top;
112  else if (VuoReal_areEqual(mesh0.y, -.5))
113  v = VuoVerticalAlignment_Center;
114  else //if (VuoReal_areEqual(mesh0.y, 0))
115  v = VuoVerticalAlignment_Bottom;
116 
117  return VuoAnchor_make(h, v);
118 }
119 
124 VuoPoint2d VuoSceneText_getAnchorOffset(VuoSceneObject so, float verticalScale, float rotationZ, float wrapWidth, int viewportWidth, int backingScaleFactor)
125 {
127  if (a == VuoAnchor_make(VuoHorizontalAlignment_Center, VuoVerticalAlignment_Center))
128  // Already in center.
129  return (VuoPoint2d){0, 0};
130 
131  VuoImageTextData td = VuoImage_getTextImageData(so.text, so.font, backingScaleFactor, verticalScale, rotationZ, false);
132  VuoLocal(td);
133  VuoPoint2d *corners = td->transformedCorners;
134 
135  // Convert the corner pixel coodrinates to scene coordinates.
136  for (int i = 0; i < 4; ++i)
137  {
138  corners[i].x *= -2. / viewportWidth;
139  corners[i].y *= -2. / viewportWidth;
140  }
141 
142  // Move the origin to the anchor point.
143  double width = fmax(fabs(corners[2].x - corners[0].x), fabs(corners[3].x - corners[1].x));
144  double height = fmax(fabs(corners[2].y - corners[0].y), fabs(corners[3].y - corners[1].y));
145  if (a == VuoAnchor_make(VuoHorizontalAlignment_Left, VuoVerticalAlignment_Top))
146  return (VuoPoint2d){
147  corners[0].x,
148  -corners[0].y
149  };
150  else if (a == VuoAnchor_make(VuoHorizontalAlignment_Left, VuoVerticalAlignment_Center))
151  return (VuoPoint2d){
152  (corners[0].x + corners[3].x)/2,
153  -((corners[0].y + corners[3].y)/2 + height/2.)
154  };
155  else if (a == VuoAnchor_make(VuoHorizontalAlignment_Left, VuoVerticalAlignment_Bottom))
156  return (VuoPoint2d){
157  corners[3].x,
158  -(corners[3].y + height)
159  };
160  else if (a == VuoAnchor_make(VuoHorizontalAlignment_Center, VuoVerticalAlignment_Top))
161  return (VuoPoint2d){
162  (corners[0].x + corners[1].x)/2 + width/2.,
163  -(corners[0].y + corners[1].y)/2
164  };
165  else if (a == VuoAnchor_make(VuoHorizontalAlignment_Center, VuoVerticalAlignment_Bottom))
166  return (VuoPoint2d){
167  (corners[2].x + corners[3].x)/2 + width/2.,
168  -((corners[2].y + corners[3].y)/2 + height)
169  };
170  else if (a == VuoAnchor_make(VuoHorizontalAlignment_Right, VuoVerticalAlignment_Top))
171  return (VuoPoint2d){
172  corners[1].x + width,
173  -corners[1].y
174  };
175  else if (a == VuoAnchor_make(VuoHorizontalAlignment_Right, VuoVerticalAlignment_Center))
176  return (VuoPoint2d){
177  (corners[1].x + corners[2].x)/2 + width,
178  -((corners[1].y + corners[2].y)/2 + height/2.)
179  };
180  else //if (a == VuoAnchor_make(VuoHorizontalAlignment_Right, VuoVerticalAlignment_Bottom))
181  return (VuoPoint2d){
182  corners[2].x + width,
183  -(corners[2].y + height)
184  };
185 }