Revision Log
Revision: 1.4 - (view) (download) (as text)
| 1 : | ktanaka | 1.1 | #ifndef _BITMAP_FONT_H |
| 2 : | #define _BITMAP_FONT_H | ||
| 3 : | |||
| 4 : | #include <vector> | ||
| 5 : | #include <string> | ||
| 6 : | ktanaka | 1.2 | #include <algorithm> |
| 7 : | |||
| 8 : | typedef pair<int,int> pixel; | ||
| 9 : | ktanaka | 1.1 | |
| 10 : | class BitmapFont{ | ||
| 11 : | friend ostream& operator<<(ostream &os,const BitmapFont &bf); | ||
| 12 : | private: | ||
| 13 : | int width, height; | ||
| 14 : | vector<bool> bitmap; | ||
| 15 : | ktanaka | 1.2 | vector<pixel> pixels; |
| 16 : | ktanaka | 1.1 | public: |
| 17 : | int getWidth() const { return width; } | ||
| 18 : | int getHeight() const { return height; } | ||
| 19 : | ktanaka | 1.3 | int getSize() const { return width*height; } |
| 20 : | int getPixelSize() const { return pixels.size(); } | ||
| 21 : | ktanaka | 1.4 | pixel getPixelAt(int index) const { return pixels[index]; } |
| 22 : | ktanaka | 1.1 | BitmapFont(){} |
| 23 : | BitmapFont(const BitmapFont& b){ | ||
| 24 : | width=b.width; | ||
| 25 : | height=b.height; | ||
| 26 : | bitmap=b.bitmap; | ||
| 27 : | ktanaka | 1.2 | pixels=b.pixels; |
| 28 : | ktanaka | 1.1 | } |
| 29 : | BitmapFont(int w,int h) :width(w),height(h),bitmap(w*h,false){ | ||
| 30 : | ktanaka | 1.4 | } |
| 31 : | BitmapFont(int w,int h,const vector<pixel> &pxls) | ||
| 32 : | :width(w),height(h),pixels(pxls),bitmap(w*h,false){ | ||
| 33 : | vector<pixel>::iterator it; | ||
| 34 : | for(it=pixels.begin();it!=pixels.end();it++){ | ||
| 35 : | int x=it->first, y=it->second; | ||
| 36 : | assert(0<= x && x<width && 0<=y && y<height); | ||
| 37 : | bitmap[y*width+x]=true; | ||
| 38 : | } | ||
| 39 : | ktanaka | 1.1 | } |
| 40 : | // 暗黙のうちに幅が4ビットの倍数だと仮定している。 | ||
| 41 : | BitmapFont(int w,int h,const string& hex) :width(w),height(h),bitmap(){ | ||
| 42 : | for(string::const_iterator it=hex.begin();it!=hex.end();it++){ | ||
| 43 : | string s; | ||
| 44 : | s+= *it; | ||
| 45 : | long val=strtol(s.c_str(),0,16); | ||
| 46 : | assert(val>=0 && val<16); | ||
| 47 : | for(int i=0;i<4;i++){ | ||
| 48 : | bool pixel=false; | ||
| 49 : | ktanaka | 1.2 | if((val&(1<<(3-i)))!=0){ |
| 50 : | pixel=true; | ||
| 51 : | } | ||
| 52 : | ktanaka | 1.1 | bitmap.push_back(pixel); |
| 53 : | } | ||
| 54 : | } | ||
| 55 : | ktanaka | 1.2 | for(int y=0;y<height;y++) |
| 56 : | for(int x=0;x<width;x++) | ||
| 57 : | if(get(x,y)) | ||
| 58 : | pixels.push_back(pair<int,int>(x,y)); | ||
| 59 : | ktanaka | 1.1 | } |
| 60 : | bool get(int x, int y) const{ | ||
| 61 : | if(x<0 || x>=width || y<0 || y>=height){ | ||
| 62 : | return false; | ||
| 63 : | } | ||
| 64 : | return bitmap[y*width+x]; | ||
| 65 : | ktanaka | 1.2 | } |
| 66 : | ktanaka | 1.3 | int getIndex(const pixel &px) const{ |
| 67 : | vector<pixel>::const_iterator it=find(pixels.begin(),pixels.end(),px); | ||
| 68 : | ktanaka | 1.2 | if(it!=pixels.end()){ |
| 69 : | return it-pixels.begin(); | ||
| 70 : | } | ||
| 71 : | return -1; | ||
| 72 : | ktanaka | 1.1 | } |
| 73 : | }; | ||
| 74 : | #endif /* _BITMAP_FONT_H */ |
|
ktanaka Powered by ViewCVS 1.0-dev |
ViewCVS and CVS Help |