46 static const GlyphSet GetGlyphSetFromLayout(
49 const size_t num_glyphs = layout.GetGlyphCount();
50 for (
size_t i = 0; i < num_glyphs; ++i)
51 glyph_set.insert(layout.GetGlyph(i).glyph_index);
82 const size_t num_glyphs = layout.GetGlyphCount();
83 base::AllocVector<uint16> indices(
85 indices.reserve(6 * num_glyphs);
87 for (
size_t i = 0; i < num_glyphs; ++i) {
88 indices.push_back(static_cast<uint16>(4 * i + 0));
89 indices.push_back(static_cast<uint16>(4 * i + 1));
90 indices.push_back(static_cast<uint16>(4 * i + 2));
91 indices.push_back(static_cast<uint16>(4 * i + 0));
92 indices.push_back(static_cast<uint16>(4 * i + 2));
93 indices.push_back(static_cast<uint16>(4 * i + 3));
98 base::DataContainer::CreateAndCopy<uint16>(
99 &indices[0], indices.size(),
102 index_buffer->SetData(container,
sizeof(indices[0]), indices.size(),
108 const gfx::AttributeArray& attr_array) {
110 if (attr_array.GetBufferAttributeCount() >= 1U) {
111 const gfx::Attribute& attr = attr_array.GetBufferAttribute(0);
113 bo = attr.GetValue<gfx::BufferObjectElement>().buffer_object;
118 static bool CanBufferObjectBeReused(
const gfx::BufferObject& bo,
119 size_t num_vertices) {
120 return (bo.GetCount() == num_vertices &&
121 bo.GetData().Get() &&
123 bo.GetData().Get() &&
124 bo.GetData()->GetData());
141 : font_image_(font_image),
142 shader_manager_(shader_manager),
143 allocator_(base::AllocationManager::GetNonNullAllocator(allocator)),
158 const GlyphSet glyph_set = GetGlyphSetFromLayout(
161 font_image_->FindImageData(glyph_set);
172 if (!node_->GetShaderProgram().Get())
173 node_->SetShaderProgram(BuildShaderProgram());
174 if (!node_->GetStateTable().Get())
175 node_->SetStateTable(BuildStateTable(allocator_));
183 if (node_->GetShapes().empty())
185 UpdateShape(layout, usage_mode, node_->GetShapes()[0].Get());
193 if (!registry_.
Get())
195 std::string id_string;
196 std::string vertex_source;
197 std::string fragment_source;
201 if (shader_manager_.
Get()) {
203 program = shader_manager_->CreateShaderProgram(
204 id_string, registry_,
207 id_string +
" vertex shader", vertex_source)),
210 id_string +
" fragment shader", fragment_source)));
214 program->SetLabel(id_string);
215 program->SetVertexShader(
217 program->SetFragmentShader(
225 if (font_image_.
Get()) {
227 texture = image_data_->
texture;
234 if (node && index < node->GetUniforms().size()) {
247 math::Point3f positions[4],
256 for (
int i = 0; i < 4; ++i) {
258 text_extents_.ExtendByPoint(positions[i]);
261 const float u_min = texcoord_rect.GetMinPoint()[0];
262 const float u_max = texcoord_rect.GetMaxPoint()[0];
264 const float v_min = texcoord_rect.GetMaxPoint()[1];
265 const float v_max = texcoord_rect.GetMinPoint()[1];
266 texture_coords[0].Set(u_min, v_min);
267 texture_coords[1].Set(u_max, v_min);
268 texture_coords[2].Set(u_max, v_max);
269 texture_coords[3].Set(u_min, v_max);
272 for (
int i = 0; i < 4; ++i) {
273 positions[i] = math::Point3f::Zero();
274 texture_coords[i] = math::Point2f::Zero();
279 bool Builder::UpdateAttributeArray(
286 size_t vertex_size = 0U;
287 size_t num_vertices = 0U;
288 text_extents_.MakeEmpty();
297 bool reuse_buffer =
false;
304 reuse_buffer = CanBufferObjectBeReused(*bo, num_vertices);
309 base::DataContainer& container = *bo->GetData();
310 char* old_vertex_data = container.GetMutableData<
char>();
312 memcpy(old_vertex_data, &vertex_data[0], vertex_size * num_vertices);
316 base::DataContainer::CreateAndCopy<char>(
317 &vertex_data[0], vertex_size * num_vertices,
319 bo->SetData(container, vertex_size, num_vertices, usage_mode);
322 if (!attr_array->GetBufferAttributeCount()) {
329 void Builder::UpdateShape(
335 if (!shape->GetAttributeArray().Get())
336 shape->SetAttributeArray(
342 const bool buffer_reused =
343 UpdateAttributeArray(layout, usage_mode, shape->GetAttributeArray());
344 if (!shape->GetIndexBuffer().Get() || !buffer_reused) {
345 shape->SetIndexBuffer(BuildIndexBuffer(
bool IsInvalidReference(const T &value)
IsInvalidReference() returns true if a passed const reference of type T has an address of InvalidRefe...
kShortTerm is used for objects that are very transient in nature, such as scratch memory used to comp...
gfx::TexturePtr texture
Font glyph texture.
void StoreGlyphVertices(const Layout &layout, size_t glyph_index, math::Point3f positions[4], math::Point2f texture_coords[4])
Fills in the position and texture_coords for the 4 vertices of the indexed Layout glyph quad...
virtual void BindAttributes(const gfx::AttributeArrayPtr &attr_array, const gfx::BufferObjectPtr &buffer_object)=0
Binds attributes for the Builder's shader program.
base::AllocatorPtr allocator_
The Allocator for the FreeTypeManager and all its Fonts.
bool UpdateFontImageTextureUniform(size_t index, gfx::Node *node)
Modifies the indexed Texture uniform in the node if necessary to contain the current FontImage image...
virtual base::AllocVector< char > BuildVertexData(const Layout &layout, size_t *vertex_size, size_t *num_vertices)=0
Returns a vector of data that represents vertex data, the size of a vertex, the number of vertices...
A ShaderProgram represents an OpenGL shader program that can be applied to shapes.
Simple composer that just returns the string passed to its constructor.
const base::AllocatorPtr & GetAllocator()
Returns the Allocator passed to the constructor.
const Glyph & GetGlyph(size_t i) const
Returns the indexed glyph.
virtual const AllocatorPtr & GetAllocatorForLifetime(AllocationLifetime lifetime) const
Returns the correct Allocator to use to allocate memory with a specific lifetime. ...
Range< 2, float > Range2f
base::ReferentPtr< StateTable >::Type StateTablePtr
Convenience typedef for shared pointer to a StateTable.
SharedPtr< Allocator > AllocatorPtr
base::ReferentPtr< IndexBuffer >::Type IndexBufferPtr
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
A BufferObject describes a generic array of data used, for example, to describe the vertices in a Sha...
static const AllocatorPtr & GetNonNullAllocator(const AllocatorPtr &allocator)
This convenience function can be used where a non-NULL Allocator pointer is needed.
virtual void GetShaderStrings(std::string *id_string, std::string *vertex_source, std::string *fragment_source)=0
Returns the strings needed for shader definition.
base::AllocSet< GlyphIndex > GlyphSet
A Shape object represents a shape (vertices + indices) to draw.
static bool GetTextureCoords(const ImageData &image_data, GlyphIndex glyph_index, math::Range2f *rectangle)
Convenience function that sets rectangle to the texture coordinate rectangle to use for the indexed g...
bool Build(const Layout &layout, gfx::BufferObject::UsageMode usage_mode)
Builds an Ion Node representing the text string defined by a Layout, using the FontImage passed to th...
size_t GetGlyphCount() const
Returns the number of glyphs added to the layout.
Copyright 2016 Google Inc.
A Layout instance specifies how glyphs are arranged to form text.
#define DCHECK_EQ(val1, val2)
TexturePtr texture
The Texture to add sub-image data to.
base::ReferentPtr< AttributeArray >::Type AttributeArrayPtr
Convenience typedef for shared pointer to a AttributeArray.
void Reset(T *new_shared)
Changes the pointer to point to the given shared, which may be NULL.
Builder(const FontImagePtr &font_image, const gfxutils::ShaderManagerPtr &shader_manager, const base::AllocatorPtr &allocator)
The constructor is protected because this is an abstract class.
A Shader represents an OpenGL shader stage.
base::ReferentPtr< DataContainer >::Type DataContainerPtr
const gfx::TexturePtr GetFontImageTexture()
Returns a Texture that contains the FontImage image.
base::ReferentPtr< BufferObject >::Type BufferObjectPtr
Convenience typedef for shared pointer to a BufferObject.
virtual void UpdateUniforms(const gfx::ShaderInputRegistryPtr ®istry, gfx::Node *node)=0
Adds or updates uniforms for the shaders in the node.
FontImage::ImageData image_data
The wrapped ImageData instance.
A Glyph represents one character glyph in the layout.
A Node instance represents a node in a scene graph.
Data for each image in the FontImage.
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
~Builder() override
The destructor is protected because all base::Referent classes must have protected or private destruc...
virtual const gfx::ShaderInputRegistryPtr GetShaderInputRegistry()=0
Functions that derived classes must implement.
#define DCHECK_LT(val1, val2)
This class can be used in place of std::vector to allow an Ion Allocator to be used for memory alloca...