| #include <vector> |
#include <vector> |
| #include <string> |
#include <string> |
| #include <algorithm> |
#include <algorithm> |
| |
#include <cassert> |
| |
|
| typedef pair<int,int> pixel; |
typedef std::pair<int,int> pixel; |
| |
|
| class BitmapFont{ |
class BitmapFont{ |
| friend ostream& operator<<(ostream &os,const BitmapFont &bf); |
friend std::ostream& operator<<(std::ostream &os,const BitmapFont &bf); |
| private: |
private: |
| int width, height; |
int width, height; |
| vector<bool> bitmap; |
std::vector<bool> bitmap; |
| vector<pixel> pixels; |
std::vector<pixel> pixels; |
| public: |
public: |
| int getWidth() const { return width; } |
int getWidth() const { return width; } |
| int getHeight() const { return height; } |
int getHeight() const { return height; } |
| } |
} |
| BitmapFont(int w,int h) :width(w),height(h),bitmap(w*h,false){ |
BitmapFont(int w,int h) :width(w),height(h),bitmap(w*h,false){ |
| } |
} |
| BitmapFont(int w,int h,const vector<pixel> &pxls) |
BitmapFont(int w,int h,const std::vector<pixel> &pxls) |
| :width(w),height(h),pixels(pxls),bitmap(w*h,false){ |
:width(w),height(h),bitmap(w*h,false),pixels(pxls){ |
| vector<pixel>::iterator it; |
std::vector<pixel>::iterator it; |
| for(it=pixels.begin();it!=pixels.end();it++){ |
for(it=pixels.begin();it!=pixels.end();it++){ |
| int x=it->first, y=it->second; |
int x=it->first, y=it->second; |
| assert(0<= x && x<width && 0<=y && y<height); |
assert(0<= x && x<width && 0<=y && y<height); |
| } |
} |
| } |
} |
| // 暗黙のうちに幅が4ビットの倍数だと仮定している。 |
// 暗黙のうちに幅が4ビットの倍数だと仮定している。 |
| BitmapFont(int w,int h,const string& hex) :width(w),height(h),bitmap(){ |
BitmapFont(int w,int h,const std::string& hex) :width(w),height(h),bitmap(){ |
| for(string::const_iterator it=hex.begin();it!=hex.end();it++){ |
for(std::string::const_iterator it=hex.begin();it!=hex.end();it++){ |
| string s; |
std::string s; |
| s+= *it; |
s+= *it; |
| long val=strtol(s.c_str(),0,16); |
long val=strtol(s.c_str(),0,16); |
| assert(val>=0 && val<16); |
assert(val>=0 && val<16); |
| for(int y=0;y<height;y++) |
for(int y=0;y<height;y++) |
| for(int x=0;x<width;x++) |
for(int x=0;x<width;x++) |
| if(get(x,y)) |
if(get(x,y)) |
| pixels.push_back(pair<int,int>(x,y)); |
pixels.push_back(std::pair<int,int>(x,y)); |
| } |
} |
| bool get(int x, int y) const{ |
bool get(int x, int y) const{ |
| if(x<0 || x>=width || y<0 || y>=height){ |
if(x<0 || x>=width || y<0 || y>=height){ |
| return bitmap[y*width+x]; |
return bitmap[y*width+x]; |
| } |
} |
| int getIndex(const pixel &px) const{ |
int getIndex(const pixel &px) const{ |
| vector<pixel>::const_iterator it=find(pixels.begin(),pixels.end(),px); |
std::vector<pixel>::const_iterator it=find(pixels.begin(),pixels.end(),px); |
| if(it!=pixels.end()){ |
if(it!=pixels.end()){ |
| return it-pixels.begin(); |
return it-pixels.begin(); |
| } |
} |