00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(_XRB_FONT_HPP_)
00012 #define _XRB_FONT_HPP_
00013
00014 #include "xrb.hpp"
00015
00016 #include <string>
00017 #include <vector>
00018
00019 #include "xrb_enums.hpp"
00020 #include "xrb_ntuple.hpp"
00021 #include "xrb_screencoord.hpp"
00022
00023 namespace Xrb
00024 {
00025
00026 class RenderContext;
00027 class Serializer;
00028 class Texture;
00029
00030
00031
00032
00033
00034 typedef Sint32 FontCoord;
00035 typedef Vector<FontCoord, 2> FontCoordVector2;
00036
00037 #define FONTCOORD_LOWER_BOUND SINT32_LOWER_BOUND
00038 #define FONTCOORD_UPPER_BOUND SINT32_UPPER_BOUND
00039
00040 inline FontCoord ScreenToFontCoord (ScreenCoord v)
00041 {
00042 return FontCoord(v) << 6;
00043 }
00044 inline ScreenCoord FontToScreenCoord (FontCoord v)
00045 {
00046 ASSERT1((v >> 6) >= SCREENCOORD_LOWER_BOUND && (v >> 6) <= SCREENCOORD_UPPER_BOUND);
00047 return ScreenCoord(v >> 6);
00048 }
00049
00050 FontCoordVector2 ScreenToFontCoordVector2 (ScreenCoordVector2 const &v);
00051 ScreenCoordVector2 FontToScreenCoordVector2 (FontCoordVector2 const &v);
00052
00058 class Font
00059 {
00060 public:
00061
00064 struct LineFormat
00065 {
00066 char const *m_ptr;
00067 ScreenCoord m_width;
00068 Uint32 m_glyph_count;
00069 };
00070
00071 typedef std::vector<LineFormat> LineFormatVector;
00072
00073 static Font *Create (std::string const &font_face_path, Sint32 pixel_height);
00074
00075 virtual ~Font () { }
00076
00077
00078
00079 std::string const &FontFacePath () const { return m_font_face_path; }
00080 ScreenCoord PixelHeight () const { return m_pixel_height; }
00081 ScreenCoord GlyphWidth (char const *glyph) const
00082 {
00083 FontCoordVector2 pen_position_26_6(FontCoordVector2::ms_zero);
00084 MoveThroughGlyph(&pen_position_26_6, ScreenCoordVector2::ms_zero, glyph, NULL);
00085 return Abs(FontToScreenCoord(pen_position_26_6[Dim::X]));
00086 }
00087
00088
00089
00090 ScreenCoordRect StringRect (char const *string) const;
00091 ScreenCoordRect StringRect (LineFormatVector const &line_format_vector) const;
00092
00093
00094
00095 void DrawString (
00096 RenderContext const &render_context,
00097 ScreenCoordVector2 const &initial_pen_position,
00098 char const *string) const;
00099
00100
00101
00102 void GenerateLineFormatVector (
00103 char const *source_string,
00104 LineFormatVector *dest_line_format_vector) const;
00105
00106
00107 void DrawLineFormattedText (
00108 RenderContext const &render_context,
00109 ScreenCoordRect const &draw_rect,
00110 char const *source_string,
00111 LineFormatVector const &line_format_vector,
00112 Alignment2 const &alignment) const;
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 virtual void MoveThroughGlyph (
00137 FontCoordVector2 *pen_position_26_6,
00138 ScreenCoordVector2 const &initial_pen_position,
00139 char const *current_glyph,
00140 char const *next_glyph,
00141 Uint32 *remaining_glyph_count = NULL,
00142 FontCoord *major_space_26_6 = NULL) const = 0;
00143
00144
00145 virtual void GenerateWordWrappedString (
00146 std::string const &source_string,
00147 std::string *dest_string,
00148 ScreenCoordVector2 const &text_area_size) const = 0;
00149
00150 protected:
00151
00152 Font (std::string const &font_face_path, ScreenCoord pixel_height)
00153 :
00154 m_font_face_path(font_face_path),
00155 m_pixel_height(pixel_height)
00156 { }
00157
00158
00159
00160
00161
00162
00163 virtual void DrawGlyphSetup (RenderContext const &render_context) const = 0;
00164
00165 virtual void DrawGlyphShutdown (RenderContext const &render_context) const = 0;
00166
00167
00168 virtual void DrawGlyph (
00169 RenderContext const &render_context,
00170 char const *glyph,
00171 FontCoordVector2 const &pen_position_26_6) const = 0;
00172
00173 private:
00174
00175
00176 void DrawStringPrivate (
00177 RenderContext const &render_context,
00178 ScreenCoordVector2 const &initial_pen_position,
00179 char const *string,
00180 char const *string_terminator = NULL,
00181 Uint32 remaining_glyph_count = 0,
00182 ScreenCoord remaining_space = 0) const;
00183 void TrackBoundingBox (
00184 FontCoordVector2 *pen_position_span_26_6,
00185 FontCoordVector2 const &pen_position_26_6) const;
00186
00187 std::string const m_font_face_path;
00188 ScreenCoord const m_pixel_height;
00189 };
00190
00191
00192
00193
00194
00195 }
00196
00197 #endif // !defined(_XRB_FONT_HPP_)