00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef BFONT_H
00013 #define BFONT_H
00014
00015
00016 #include <nds.h>
00017 #include <string>
00018 #include <vector>
00019
00020
00021 #include "BImage.h"
00022 #include "BWidget.h"
00023 #include "BGraphics.h"
00024 #include "BVirtualFile.h"
00025
00040 class BFont {
00041 public:
00042 typedef struct {
00043 unsigned int start;
00044 unsigned int width;
00045 } GlyphDescription;
00046
00047 typedef enum {
00048 FLAG_REGULAR = 0,
00049 FLAG_BOLD = 1,
00050 FLAG_OBLIQUE = 2
00051 } FontFlag;
00052
00053 typedef struct {
00054 uint8 formatflags, encodingflags;
00055 std::string name, fullname;
00056 uint8 height;
00057 uint8 fontflags;
00058 } FontHeader;
00059
00060 typedef struct {
00061 uint32 start, end;
00062 } CharacterRange;
00063
00071 static bool readFontHeader(BVirtualFile& f, FontHeader& header);
00072
00079 BFont(const BImage& img, const GlyphDescription* indices,
00080 int firstchar, int lastchar);
00081
00085 BFont(BVirtualFile& f);
00086 ~BFont();
00087
00089 int widthOfGlyph(uint32 glyph);
00091 int widthOfString(const std::string& str);
00093 int height() { return img->height(); }
00094
00106 BPoint drawGlyph(BImage& img, const BPoint& point, uint32 glyph,
00107 uint16 color, const BRect& clip,
00108 BWidget::TextDirection dir = BWidget::DIR_LEFTTORIGHT);
00109
00111 BPoint drawString(BImage& img, const BPoint& point, const std::string& str,
00112 uint16 color, const BRect& clip,
00113 BWidget::TextDirection dir = BWidget::DIR_LEFTTORIGHT);
00114
00116 const FontHeader& fontHeader() { return header; }
00117
00125 BFont* otherSizeFont(int delta);
00126
00127 private:
00128 BImage* img;
00129
00130 std::vector<CharacterRange> ranges;
00131 std::vector<GlyphDescription> indices;
00132
00133 FontHeader header;
00134
00135 bool readFontBody(BVirtualFile& f, const FontHeader& header);
00136
00137 static inline uint8 read8(BVirtualFile& f)
00138 {
00139 uint8 byte;
00140 f.read(&byte, 1);
00141 return byte;
00142 }
00143
00144 static inline uint16 read16(BVirtualFile& f)
00145 {
00146 return read8(f) << 8 | read8(f);
00147 }
00148
00149 static inline uint32 read32(BVirtualFile& f)
00150 {
00151 return read8(f) << 24 | read8(f) << 16 | read8(f) << 8 | read8(f);
00152 }
00153
00154 static inline std::string readstring(BVirtualFile& f)
00155 {
00156 uint8 len = read8(f);
00157 char* buf = new char[len+1];
00158 memset(buf, 0, len+1);
00159 f.read(buf, len);
00160 std::string str = buf;
00161 delete buf;
00162 return str;
00163 }
00164
00165 inline int indexOfGlyph(uint32 glyph)
00166 {
00167 int index = 0;
00168 for(unsigned int i=0; i<ranges.size(); i++)
00169 {
00170 if(glyph >= ranges[i].start && glyph <= ranges[i].end)
00171 return index+(glyph-ranges[i].start);
00172 index += (ranges[i].end-ranges[i].start)+1;
00173 }
00174 return -1;
00175 }
00176 };
00177
00178 #endif