first commit
This commit is contained in:
56
include/framework/font/font_cache.h
Normal file
56
include/framework/font/font_cache.h
Normal file
@ -0,0 +1,56 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
------------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#ifndef __FONTCACHE__
|
||||
#define __FONTCACHE__
|
||||
|
||||
|
||||
#include "font/font_factory.h"
|
||||
#include "font/font_extended.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
#include "memory/nweak_ptr.h"
|
||||
#include "container/nlist.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
|
||||
/// Extended font alias.
|
||||
struct FontAlias
|
||||
{
|
||||
String alias;
|
||||
sFontEx font;
|
||||
};
|
||||
|
||||
/*
|
||||
@short Font cache.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class FontCache : public SharedObject
|
||||
{
|
||||
AutoList <FontAlias *> font_aliases;
|
||||
AutoPtr <IFontFactory> font_factory;
|
||||
|
||||
public:
|
||||
|
||||
FontEx *GetFont(const char *) const;
|
||||
FontAlias *GetAlias(const char *) const;
|
||||
FontEx *GetAliasedFont(const char *) const;
|
||||
|
||||
FontEx *LoadFont(const char *, const char *alias = 0);
|
||||
|
||||
void DeleteAlias(const char *);
|
||||
void DeleteAllFont();
|
||||
|
||||
FontCache(IFontFactory *factory) : font_factory(factory) {}
|
||||
~FontCache();
|
||||
};
|
||||
|
||||
typedef SharedPtr <FontCache> sFontCache;
|
||||
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __FONTCACHE__
|
||||
70
include/framework/font/font_extended.h
Normal file
70
include/framework/font/font_extended.h
Normal file
@ -0,0 +1,70 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
------------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#ifndef __FONTEXTENDED__
|
||||
#define __FONTEXTENDED__
|
||||
|
||||
|
||||
#include "font/font_interface.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
#include "memory/nweak_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
|
||||
/*!
|
||||
@short Extended font to help normalization.
|
||||
@author Emmanuel Julien (ejulien@owloh.com)
|
||||
*/
|
||||
class FontEx : public IFont
|
||||
{
|
||||
sIFont font;
|
||||
WeakPtr <IFont> current_glyph_font;
|
||||
|
||||
int pixel_size;
|
||||
|
||||
public:
|
||||
|
||||
WeakPtr <IFont> fallback; ///< Fall-back font when a glyph is not found in the current font.
|
||||
|
||||
float size_multiplier, ///< Offsets to ease TTF normalization.
|
||||
tracking_offset,
|
||||
leading_offset;
|
||||
|
||||
/// Return the font name.
|
||||
const char *GetName() const { return font->GetName(); }
|
||||
/// Return the bounding rect for a given string.
|
||||
iRect GetTextBoundRect(const char *text) const { return font->GetTextBoundRect(text); }
|
||||
|
||||
/// Set font size in pixels.
|
||||
bool SetPixelSize(int);
|
||||
/// Get font height in pixels.
|
||||
int GetHeight() const;
|
||||
/// Get font current glyph advance.
|
||||
int GetAdvance() const;
|
||||
|
||||
/// Return true if the font supports kerning.
|
||||
bool HasKerning() const;
|
||||
/// Return the kerning for a glyph pair.
|
||||
int GetKerning(uint previous_glyph, uint glyph) const;
|
||||
|
||||
/// Load the glyph corresponding to a UTF-32 codepoint.
|
||||
bool LoadGlyph(uint codepoint, bool for_render);
|
||||
/// Render currently loaded glyph to a picture.
|
||||
bool RenderCurrentGlyph(Picture &picture, const iPoint &position, const iRect &clip, const Color &color);
|
||||
|
||||
/// Set a fallback font to query in case a glyph cannot be found in this font.
|
||||
void SetFallback(IFont *f) { fallback = f; }
|
||||
|
||||
FontEx(IFont *f) : font(f), size_multiplier(1), tracking_offset(1), leading_offset(1) {}
|
||||
};
|
||||
|
||||
typedef SharedPtr <FontEx> sFontEx;
|
||||
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __FONTEXTENDED__
|
||||
26
include/framework/font/font_factory.h
Normal file
26
include/framework/font/font_factory.h
Normal file
@ -0,0 +1,26 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
------------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#ifndef __FONTFACTORY__
|
||||
#define __FONTFACTORY__
|
||||
|
||||
|
||||
namespace GS {
|
||||
struct IFont;
|
||||
|
||||
/// Font factory.
|
||||
struct IFontFactory
|
||||
{
|
||||
/// Load a font.
|
||||
virtual IFont *LoadFont(const char *) = 0;
|
||||
|
||||
virtual ~IFontFactory() {}
|
||||
};
|
||||
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __FONTFACTORY__
|
||||
60
include/framework/font/font_interface.h
Normal file
60
include/framework/font/font_interface.h
Normal file
@ -0,0 +1,60 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __FONTINTERFACE__
|
||||
#define __FONTINTERFACE__
|
||||
|
||||
|
||||
#include "color/color.h"
|
||||
#include "geometry/rect.h"
|
||||
#include "memory/nshared_ptr.h"
|
||||
#include "nstring/nstring.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
class Picture;
|
||||
|
||||
/*!
|
||||
@short Font interface.
|
||||
@author Emmanuel Julien (ejulien@owloh.com)
|
||||
*/
|
||||
struct IFont : public SharedObject
|
||||
{
|
||||
/// Return the font name.
|
||||
virtual const char *GetName() const = 0;
|
||||
|
||||
/// Return the bounding rect for a given string.
|
||||
virtual iRect GetTextBoundRect(const char *) const = 0;
|
||||
|
||||
/// Set font size in pixels.
|
||||
virtual bool SetPixelSize(int) = 0;
|
||||
/// Get font height in pixels.
|
||||
virtual int GetHeight() const = 0;
|
||||
/// Get font current glyph advance.
|
||||
virtual int GetAdvance() const = 0;
|
||||
|
||||
/// Return true if the font supports kerning.
|
||||
virtual bool HasKerning() const = 0;
|
||||
/// Return the kerning for a glyph codepoint.
|
||||
virtual int GetKerning(uint previous_codepoint, uint codepoint) const = 0;
|
||||
|
||||
/// Load the glyph corresponding to a UTF-32 codepoint.
|
||||
virtual bool LoadGlyph(uint codepoint, bool for_render) = 0;
|
||||
/// Render currently loaded glyph to a picture.
|
||||
virtual bool RenderCurrentGlyph(Picture &picture, const iPoint &position, const iRect &clip, const Color &color = Color::White) = 0;
|
||||
|
||||
/// Set a fallback font to query in case a glyph cannot be found in this font.
|
||||
virtual void SetFallback(IFont *) {}
|
||||
|
||||
virtual ~IFont() {}
|
||||
};
|
||||
|
||||
typedef SharedPtr <IFont> sIFont;
|
||||
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __FONTINTERFACE__
|
||||
122
include/framework/font/font_renderer.h
Normal file
122
include/framework/font/font_renderer.h
Normal file
@ -0,0 +1,122 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __FONTRENDERER__
|
||||
#define __FONTRENDERER__
|
||||
|
||||
|
||||
#include "font/font_extended.h"
|
||||
#include "font/font_cache.h"
|
||||
#include "math/vector.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
class Picture;
|
||||
struct SubString;
|
||||
|
||||
/// Text widget state structure.
|
||||
class TextState
|
||||
{
|
||||
int size;
|
||||
float tracking, ///< Text tracking (horizontal offset between two characters).
|
||||
leading; ///< Text heading (vertical offset between lines).
|
||||
|
||||
public:
|
||||
|
||||
enum Format
|
||||
{
|
||||
Line = 0,
|
||||
Paragraph,
|
||||
Column
|
||||
};
|
||||
|
||||
enum Alignment
|
||||
{
|
||||
Left = 0,
|
||||
Right,
|
||||
Center,
|
||||
Justify
|
||||
};
|
||||
|
||||
sFontEx font;
|
||||
Color color;
|
||||
|
||||
Format format; ///< Text format.
|
||||
Alignment alignment; ///< Text alignment.
|
||||
int column_width; ///< Text column width.
|
||||
|
||||
int GetSize() const { return font.IsValid() ? (int)(size * font->size_multiplier) : size; }
|
||||
float GetTracking() const { return font.IsValid() ? tracking + font->tracking_offset : tracking; }
|
||||
float GetLeading() const { return font.IsValid() ? leading + font->leading_offset : leading; }
|
||||
|
||||
void SetSize(int s) { size = s; }
|
||||
void SetTracking(float t) { tracking = t; }
|
||||
void SetLeading(float l) { leading = l; }
|
||||
|
||||
TextState()
|
||||
{
|
||||
size = 16;
|
||||
color = Color::Black;
|
||||
|
||||
format = Line;
|
||||
alignment = Left;
|
||||
|
||||
column_width = 80;
|
||||
tracking = 0;
|
||||
leading = 0;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
@short Font renderer.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class FontRenderer
|
||||
{
|
||||
public:
|
||||
|
||||
enum CommandCode
|
||||
{
|
||||
CommandNone = 0,
|
||||
CommandColor,
|
||||
CommandFont,
|
||||
CommandSize,
|
||||
|
||||
CommandParseError
|
||||
};
|
||||
|
||||
static float default_text_tracking;
|
||||
static float default_text_heading;
|
||||
|
||||
/// Renderer command.
|
||||
struct Command
|
||||
{
|
||||
CommandCode code;
|
||||
Vector4 vector;
|
||||
sIFont font;
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
/// Check current string for a command.
|
||||
static const char *CheckCommand(const char *string, Command &command);
|
||||
/// Get the next sub-string from the current text buffer.
|
||||
static const char *FetchSubString(const char *string, SubString &sstr, TextState &state, int max_width, int max_character);
|
||||
/// Draw a substring to a picture.
|
||||
static void DrawSubString(SubString &sub_string, Picture &output, TextState &state, const iRect &out_rect, const iRect &clip_rect, int justification);
|
||||
|
||||
public:
|
||||
|
||||
/// Format a string using a text state, return the required output rectangle.
|
||||
static iRect Format(const char *text, const TextState &in_state, const iRect &out_rect);
|
||||
/// Compose a string using a text state.
|
||||
static iRect Compose(Picture &output, const char *text, const TextState &state, const iRect &out_rect, const iRect &clip_rect);
|
||||
};
|
||||
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __FONTRENDERER__
|
||||
Reference in New Issue
Block a user