2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include "text-vertex-generator.h"
21 #include <dali/public-api/common/constants.h>
22 #include <dali/internal/event/text/glyph-status/glyph-status.h>
23 #include <dali/internal/event/text/special-characters.h>
26 #include <cmath> // for std::sin
34 namespace // unnamed namespace
37 typedef std::vector<TextVertex2D> VertexBuffer;
39 void RepositionData( VertexBuffer& buffer, Vector2 offset )
43 * As 0,0 is the middle of the actor, text will be displayed like this
45 * |-------------------------------|
49 * | (0,0)|---------- | (x)
53 * |-------------------------------|
56 * Below it is repositioned to the centre of the actor
57 * |-------------------------------|
61 * | Hello World------ | (x)
65 * |-------------------------------|
68 // move the vertices so 0,0 is the centre of the text string.
72 for (std::size_t i=0, size = buffer.size() ; i< size; ++i)
74 buffer[i].mX -= offset.x;
75 buffer[i].mY -= offset.y;
79 void AddVertex( VertexBuffer& vertexBuffer,
82 const float charWidth,
83 const float charHeight,
85 const Vector2& uvShadow )
95 * 2 triangles with clock wise winding: 0->1->2 and 0->2->3
100 // set U1,V1 for all vertices
109 vertexBuffer.push_back(v);
113 v.mY = yPos + charHeight;
116 vertexBuffer.push_back(v);
119 v.mX = xPos + charWidth;
120 v.mY = yPos + charHeight;
123 vertexBuffer.push_back(v);
126 v.mX = xPos + charWidth;
130 vertexBuffer.push_back(v);
134 * Adjust the vertex data for italics.
135 * Skews the vertices by a value
137 void AdjustForItalics( VertexBuffer& vertexBuffer,
138 const float italicsTopDisplacement,
139 const float italicsBottomDisplacement)
142 std::size_t index = vertexBuffer.size()-4;
143 TextVertex2D &v1 = vertexBuffer.at( index );
144 v1.mX+= italicsBottomDisplacement;
149 TextVertex2D &v2 = vertexBuffer.at( index );
150 v2.mX+= italicsTopDisplacement;
155 TextVertex2D &v3 = vertexBuffer.at( index );
156 v3.mX+= italicsTopDisplacement;
160 TextVertex2D &v4 = vertexBuffer.at( index );
161 v4.mX+= italicsBottomDisplacement;
164 void AddUnderline( VertexBuffer& vertexBuffer,
165 const float totalWidth,
166 const float thickness,
167 const float yPosition,
171 * Add an underline to a string of text.
174 * A thin vertical slice of the underline character is stretched to the
175 * length of the string.
177 * If we stretch the entire underline character (not a thin slice) then
178 * the rounded edges will be stretched as well, giving inconsistent results.
180 * Underline glyph Only use a thin slice for texturing
182 * |-------------------| (u0,v2)|--------|X|---------|(u2,v2)
184 * | /-----------\ | | /----|X|-----\ |
185 * | | underline | | | | |X| | |
186 * | \___________/ | | \____|X|_____/ |
189 * |-------------------| |--------|X|---------|
191 * (u0,v0) halfU (u2,v0)
193 * In calculation below
194 * HalfU = half way between u0 and u2. This gives a thin slice.
195 * So we use the texture-coordinates from (halfU, v0) -> (halfU, v2).
197 * End result is: A solid edge on the left / right side of the underline:
198 * A smooth (anti-aliased) edge on the top / bottom of the underline
203 // set U1,V1 for all vertices
207 float halfU = (uv.u0 + uv.u2)/2.0f;
223 vertexBuffer.push_back(v);
227 v.mY = yPosition + thickness;
230 vertexBuffer.push_back(v);
234 v.mY = yPosition + thickness;
237 vertexBuffer.push_back(v);
244 vertexBuffer.push_back(v);
247 void GetAdjustedSize(float &charWidth,
254 const GlyphMetric& glyph)
256 charWidth = (glyph.GetWidth() + padAdjustX * 2.0f) * scalar;
257 charHeight = (glyph.GetHeight() + padAdjustY * 2.0f) * scalar;
258 left = (glyph.GetLeft() - padAdjustX) * scalar;
259 top = (glyph.GetTop() + padAdjustY) * scalar;
264 void DebugVertexBuffer( VertexBuffer& buffer )
266 for (std::size_t i = 0, size = buffer.size(); i< size ; ++i)
268 TextVertex2D &v = buffer.at( i );
269 printf("%d: xyuv =, %f , %f, %f, %f \n", (unsigned int) i, v.mX,v.mY, v.mU, v.mV);
274 } // unnamed namespace
276 TextVertexBuffer* TextVertexGenerator::Generate(const TextArray& text,
277 const TextFormat& format,
278 const FontMetricsInterface& metrics,
279 const AtlasUvInterface& uvInterface,
283 TextVertexBuffer* textVertexBuffer = new TextVertexBuffer;
284 VertexBuffer &vertexBuffer(textVertexBuffer->mVertices);
286 const GlyphMetric* glyph( NULL );
289 float underlineWidth( 0.0f );
290 float totalWidth( 0.0f );
291 float charWidth( 0.0f );
292 float charHeight( 0.0f );
296 float scalar = metrics.GetUnitsToPixels( format.GetPointSize() );
298 // Italics displacement
299 // the text is rendered upside down
300 const float sinAngle = format.IsItalic() ? std::sin( format.GetItalicsAngle() ) : 0.0f;
302 // get the line height and ascender from the font
303 const float lineHeight( metrics.GetLineHeight() * scalar );
304 const float ascender( metrics.GetAscender() * scalar );
305 const float padAdjustX( metrics.GetPadAdjustX() );
306 const float padAdjustY( metrics.GetPadAdjustY() );
307 const float tileWidth( metrics.GetMaxWidth() * scalar );
308 const float tileHeight( metrics.GetMaxHeight() * scalar );
309 unsigned int textSize = text.size();
311 for (unsigned int i = 0; i < textSize; ++i)
313 // buffer is always filled starting from the first vector position. However text characters are visited from left to right or from right to left.
314 uint32_t charIndex = text[ ( format.IsLeftToRight() ? i : ( textSize - 1 - i ) ) ];
316 glyph = metrics.GetGlyph( charIndex );
318 if (charIndex >= SpecialCharacters::FIRST_VISIBLE_CHAR && glyph )
320 // get char size and offset adjusted for padding in the atlas
321 GetAdjustedSize(charWidth, charHeight, left, top, padAdjustX, padAdjustY, scalar, *glyph );
323 yPos = (ascender - top);
326 // a combination of character index and font id is used to uniquely identify the character
327 unsigned int encodedChar = GlyphStatus::GetEncodedValue( charIndex, fontId );
328 UvRect uv = uvInterface.GetUvCoordinates( encodedChar );
330 const Vector2 uvShadow( tileWidth / charWidth, tileHeight / charHeight );
332 AddVertex( vertexBuffer, xPos, yPos, charWidth, charHeight, uv, uvShadow );
334 if( format.IsItalic() )
336 float italicsTopDisplacement = ( top - charHeight ) * sinAngle;
337 float italicsBottomDisplacement = top * sinAngle;
338 AdjustForItalics( vertexBuffer, italicsTopDisplacement, italicsBottomDisplacement);
346 underlineWidth = std::max( underlineWidth, xPos + glyph->GetXAdvance() * scalar );
347 xPos += glyph->GetXAdvance() * scalar;
348 totalWidth = std::max(totalWidth, xPos);
352 if( format.IsUnderLined() )
354 unsigned int encodedChar = GlyphStatus::GetEncodedValue( SpecialCharacters::UNDERLINE_CHARACTER, fontId );
355 UvRect uv( uvInterface.GetUvCoordinates( encodedChar ));
357 glyph = metrics.GetGlyph( SpecialCharacters::UNDERLINE_CHARACTER );
361 // Adjust uv coordinates for scaling within atlas tile
362 GetAdjustedSize(charWidth, charHeight, left, top, padAdjustX, padAdjustY, scalar, *glyph );
364 // Get underline thickness and position.
365 // These values could be retrieved from the text-format, set to the text-actor through text-style,
366 // or retrieved directly from the font metrics.
367 float thickness = 0.f;
368 float position = 0.f;
370 if( fabs( format.GetUnderlineThickness() ) > Math::MACHINE_EPSILON_0 )
372 // Thickness and position retrieved from the format, which are passed to the
373 // text-actor through the text-style, it adds the vertical pad adjust used to fit some effects like glow or shadow..
374 thickness = -format.GetUnderlineThickness();
375 position = format.GetUnderlinePosition();
379 // Thickness and position retrieved from the font metrics.
380 // It adds the vertical pad adjust ( padAdjustY ) used to fit some effects like glow or shadow.
381 thickness = -( metrics.GetUnderlineThickness() + 2.f * padAdjustY ) * scalar;
382 position = ascender - ( metrics.GetUnderlinePosition() - padAdjustY ) * scalar;
384 AddUnderline( vertexBuffer, underlineWidth, thickness, position, uv );
388 textVertexBuffer->mVertexMax = Vector2(totalWidth,lineHeight);
390 RepositionData( vertexBuffer, textVertexBuffer->mVertexMax );
393 DebugVertexBuffer( vertexBuffer );
396 return textVertexBuffer;
399 } // namespace Internal