FontFaceHandle.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 ROCKETCOREFONTFACEHANDLE_H
00029 #define ROCKETCOREFONTFACEHANDLE_H
00030 
00031 #include <Rocket/Core/ReferenceCountable.h>
00032 #include "UnicodeRange.h"
00033 #include <Rocket/Core/Font.h>
00034 #include <Rocket/Core/FontEffect.h>
00035 #include <Rocket/Core/FontGlyph.h>
00036 #include <Rocket/Core/Geometry.h>
00037 #include <Rocket/Core/String.h>
00038 #include <Rocket/Core/Texture.h>
00039 #include <ft2build.h>
00040 #include FT_FREETYPE_H
00041 
00042 namespace Rocket {
00043 namespace Core {
00044 
00045 class FontFaceLayer;
00046 
00051 class FontFaceHandle : public ReferenceCountable
00052 {
00053 public:
00054         FontFaceHandle();
00055         virtual ~FontFaceHandle();
00056 
00062         bool Initialise(FT_Face ft_face, const String& charset, int size);
00063 
00066         int GetCharacterWidth() const;
00067 
00070         int GetSize() const;
00073         int GetXHeight() const;
00076         int GetLineHeight() const;
00077 
00080         int GetBaseline() const;
00081 
00084         const FontGlyphMap& GetGlyphs() const;
00085 
00090         int GetStringWidth(const WString& string, word prior_character = 0) const;
00091 
00095         int GenerateLayerConfiguration(FontEffectMap& font_effects);
00101         bool GenerateLayerTexture(const byte*& texture_data, Vector2i& texture_dimensions, int layer_id, int texture_id);
00102 
00109         int GenerateString(GeometryList& geometry, const WString& string, const Vector2f& position, const Colourb& colour, int layer_configuration = 0) const;
00116         void GenerateLine(Geometry* geometry, const Vector2f& position, int width, Font::Line height, const Colourb& colour) const;
00117 
00120         const String& GetRawCharset() const;
00123         const UnicodeRangeList& GetCharset() const;
00124 
00125 protected:
00127         virtual void OnReferenceDeactivate();
00128 
00129 private:
00130         void GenerateMetrics(FT_Face ft_face);
00131 
00132         void BuildGlyphMap(FT_Face ft_face, const UnicodeRange& unicode_range);
00133         void BuildGlyph(FontGlyph& glyph, FT_GlyphSlot ft_glyph);
00134 
00135         void BuildKerning(FT_Face ft_face);
00136         int GetKerning(word lhs, word rhs) const;
00137 
00138         // Generates (or shares) a layer derived from a font effect.
00139         FontFaceLayer* GenerateLayer(FontEffect* font_effect);
00140 
00141         typedef std::map< word, int > GlyphKerningMap;
00142         typedef std::map< word, GlyphKerningMap > FontKerningMap;
00143 
00144         FontGlyphMap glyphs;
00145         FontKerningMap kerning;
00146 
00147         typedef std::map< const FontEffect*, FontFaceLayer* > FontLayerMap;
00148         typedef std::map< String, FontFaceLayer* > FontLayerCache;
00149         typedef std::vector< FontFaceLayer* > LayerConfiguration;
00150         typedef std::vector< LayerConfiguration > LayerConfigurationList;
00151 
00152         // The list of all font layers, index by the effect that instanced them.
00153         FontFaceLayer* base_layer;
00154         FontLayerMap layers;
00155         // Each font layer that generated geometry or textures, indexed by the respective generation
00156         // key.
00157         FontLayerCache layer_cache;
00158 
00159         // All configurations currently in use on this handle. New configurations will be generated as
00160         // required.
00161         LayerConfigurationList layer_configurations;
00162 
00163         // The average advance (in pixels) of all of this face's glyphs.
00164         int average_advance;
00165 
00166         int size;
00167         int x_height;
00168         int line_height;
00169         int baseline;
00170 
00171         float underline_position;
00172         float underline_thickness;
00173 
00174         String raw_charset;
00175         UnicodeRangeList charset;
00176 };
00177 
00178 }
00179 }
00180 
00181 #endif