FontFaceLayer.h

00001 /*
00002  * This source file is part of libRocket, the HTML/CSS Interface Middleware
00003  *
00004  * For the latest information, see http://www.librocket.com
00005  *
00006  * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  */
00027 
00028 #ifndef ROCKETCOREFONTFACELAYER_H
00029 #define ROCKETCOREFONTFACELAYER_H
00030 
00031 #include <Rocket/Core/FontGlyph.h>
00032 #include <Rocket/Core/Geometry.h>
00033 #include <Rocket/Core/GeometryUtilities.h>
00034 #include <Rocket/Core/String.h>
00035 #include <Rocket/Core/Texture.h>
00036 #include "TextureLayout.h"
00037 
00038 namespace Rocket {
00039 namespace Core {
00040 
00041 class FontEffect;
00042 class FontFaceHandle;
00043 
00051 class FontFaceLayer
00052 {
00053 public:
00054         FontFaceLayer();
00055         ~FontFaceLayer();
00056 
00063         bool Initialise(const FontFaceHandle* handle, FontEffect* effect = NULL, const FontFaceLayer* clone = NULL, bool deep_clone = false);
00064 
00070         bool GenerateTexture(const byte*& texture_data, Vector2i& texture_dimensions, int texture_id);
00076         inline void GenerateGeometry(Geometry* geometry, const word character_code, const Vector2f& position, const Colourb& colour) const
00077         {
00078                 CharacterMap::const_iterator iterator = characters.find(character_code);
00079                 if (iterator == characters.end())
00080                         return;
00081 
00082                 const Character& character = (*iterator).second;
00083 
00084                 // Generate the geometry for the character.
00085                 std::vector< Vertex >& character_vertices = geometry[character.texture_index].GetVertices();
00086                 std::vector< int >& character_indices = geometry[character.texture_index].GetIndices();
00087 
00088                 character_vertices.resize(character_vertices.size() + 4);
00089                 character_indices.resize(character_indices.size() + 6);
00090                 GeometryUtilities::GenerateQuad(&character_vertices[0] + (character_vertices.size() - 4), &character_indices[0] + (character_indices.size() - 6), Vector2f(position.x + character.origin.x, position.y + character.origin.y), character.dimensions, colour, character.texcoords[0], character.texcoords[1], character_vertices.size() - 4);
00091         }
00092 
00095         const FontEffect* GetFontEffect() const;
00096 
00100         const Texture* GetTexture(int index);
00103         int GetNumTextures() const;
00104 
00107         const Colourb& GetColour() const;
00108 
00109 private:
00110         struct Character
00111         {
00112                 // The offset, in pixels, of the baseline from the start of this character's geometry.
00113                 Vector2f origin;
00114                 // The width and height, in pixels, of this character's geometry.
00115                 Vector2f dimensions;
00116                 // The texture coordinates for the character's geometry.
00117                 Vector2f texcoords[2];
00118 
00119                 // The texture this character renders from.
00120                 int texture_index;
00121         };
00122 
00123         typedef std::map< word, Character > CharacterMap;
00124         typedef std::vector< Texture > TextureList;
00125 
00126         const FontFaceHandle* handle;
00127         FontEffect* effect;
00128 
00129         TextureLayout texture_layout;
00130 
00131         CharacterMap characters;
00132         TextureList textures;
00133         Colourb colour;
00134 };
00135 
00136 }
00137 }
00138 
00139 #endif