diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp new file mode 100644 index 000000000..ee869789b --- /dev/null +++ b/src/graphics/Font.cpp @@ -0,0 +1,2 @@ +#define INCLUDE_FONTDATA +#include "font.h" diff --git a/src/graphics/Font.h b/src/graphics/Font.h new file mode 100644 index 000000000..1e29d4626 --- /dev/null +++ b/src/graphics/Font.h @@ -0,0 +1,43 @@ +#include + +#include "common/String.h" +#include "font.h" + +class FontReader +{ + unsigned char *pointer; + int width; + int pixels; + int data; + + inline FontReader(unsigned char *_pointer): + pointer(_pointer + 1), + width(*_pointer), + pixels(0), + data(0) + {} + +public: + inline FontReader(String::value_type ch): + FontReader(ch <= 0xFF ? &font_data[font_ptrs[ch]] : &font_data[0]) + { + } + + inline int GetWidth() const + { + return width; + } + + inline int NextPixel() + { + if(!pixels) + { + data = *(pointer++); + pixels = 4; + } + int old = data; + pixels--; + data >>= 2; + return old & 0x3; + } +}; diff --git a/src/graphics/Graphics.cpp b/src/graphics/Graphics.cpp index 3e86925ea..3a187021c 100644 --- a/src/graphics/Graphics.cpp +++ b/src/graphics/Graphics.cpp @@ -5,8 +5,7 @@ #include "Config.h" #include "Misc.h" #include "Graphics.h" -#define INCLUDE_FONTDATA -#include "font.h" +#include "Font.h" #ifdef HIGH_QUALITY_RESAMPLE #include "resampler/resampler.h" #endif @@ -86,64 +85,31 @@ void VideoBuffer::Resize(int width, int height, bool resample, bool fixedRatio) } } -int VideoBuffer::SetCharacter(int x, int y, int c, int r, int g, int b, int a) +int VideoBuffer::SetCharacter(int x, int y, String::value_type c, int r, int g, int b, int a) { - int i, j, w, bn = 0, ba = 0; - unsigned char *rp = font_data + font_ptrs[c]; - w = *(rp++); - for (j=-2; j>= 2; - bn -= 2; - } - return x + w; + FontReader reader(c); + for (int j = -2; j < FONT_H - 2; j++) + for (int i = 0; i < reader.GetWidth(); i++) + SetPixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3); + return x + reader.GetWidth(); } -int VideoBuffer::BlendCharacter(int x, int y, int c, int r, int g, int b, int a) +int VideoBuffer::BlendCharacter(int x, int y, String::value_type c, int r, int g, int b, int a) { - int i, j, w, bn = 0, ba = 0; - unsigned char *rp = font_data + font_ptrs[c]; - w = *(rp++); - for (j=-2; j>= 2; - bn -= 2; - } - return x + w; + FontReader reader(c); + for (int j = -2; j < FONT_H - 2; j++) + for (int i = 0; i < reader.GetWidth(); i++) + BlendPixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3); + return x + reader.GetWidth(); } -int VideoBuffer::AddCharacter(int x, int y, int c, int r, int g, int b, int a) +int VideoBuffer::AddCharacter(int x, int y, String::value_type c, int r, int g, int b, int a) { - int i, j, w, bn = 0, ba = 0; - unsigned char *rp = font_data + font_ptrs[c]; - w = *(rp++); - for (j=-2; j>= 2; - bn -= 2; - } - return x + w; + FontReader reader(c); + for (int j = -2; j < FONT_H - 2; j++) + for (int i = 0; i < reader.GetWidth(); i++) + AddPixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3); + return x + reader.GetWidth(); } VideoBuffer::~VideoBuffer() @@ -582,14 +548,14 @@ int Graphics::textwidth(String str) s+=3; continue; } - x += font_data[font_ptrs[*s]]; + x += FontReader(*s).GetWidth(); } return x-1; } int Graphics::CharWidth(String::value_type c) { - return font_data[font_ptrs[(int)c]]; + return FontReader(c).GetWidth(); } int Graphics::textnwidth(String str, int n) @@ -610,7 +576,7 @@ int Graphics::textnwidth(String str, int n) s+=3; continue; } - x += font_data[font_ptrs[*s]]; + x += FontReader(*s).GetWidth(); n--; } return x-1; @@ -638,7 +604,7 @@ void Graphics::textnpos(String str, int n, int w, int *cx, int *cy) if (!n) { break; } - x += font_data[font_ptrs[*s]]; + x += FontReader(*s).GetWidth(); if (x>=w) { x = 0; @@ -668,7 +634,7 @@ int Graphics::textwidthx(String str, int w) s+=3; continue; } - cw = font_data[font_ptrs[*s]]; + cw = FontReader(*s).GetWidth(); if (x+(cw/2) >= w) break; x += cw; @@ -702,7 +668,7 @@ int Graphics::PositionAtCharIndex(String str, int charIndex, int & positionX, in charIndex-=4; continue; } - x += font_data[font_ptrs[*s]]; + x += FontReader(*s).GetWidth(); charIndex--; } positionX = x; @@ -732,7 +698,7 @@ int Graphics::CharIndexAtPosition(String str, int positionX, int positionY) charIndex+=4; continue; } - cw = font_data[font_ptrs[*s]]; + cw = FontReader(*s).GetWidth(); if ((x+(cw/2) >= positionX && y+FONT_H >= positionY) || y > positionY) break; x += cw; @@ -778,7 +744,7 @@ int Graphics::textwrapheight(String str, int width) } else { - cw = font_data[font_ptrs[*s]]; + cw = FontReader(*s).GetWidth(); if (x+cw>=width) { x = 0; @@ -821,7 +787,7 @@ void Graphics::textsize(String str, int & width, int & height) } else { - cWidth += font_data[font_ptrs[*s]]; + cWidth += FontReader(*s).GetWidth(); if(cWidth>lWidth) lWidth = cWidth; } diff --git a/src/graphics/Graphics.h b/src/graphics/Graphics.h index aa961dd12..3ed16ece5 100644 --- a/src/graphics/Graphics.h +++ b/src/graphics/Graphics.h @@ -84,9 +84,9 @@ public: b = 255; Buffer[y*(Width)+x] = PIXRGB(r,g,b); } - int SetCharacter(int x, int y, int c, int r, int g, int b, int a); - int BlendCharacter(int x, int y, int c, int r, int g, int b, int a); - int AddCharacter(int x, int y, int c, int r, int g, int b, int a); + int SetCharacter(int x, int y, String::value_type c, int r, int g, int b, int a); + int BlendCharacter(int x, int y, String::value_type c, int r, int g, int b, int a); + int AddCharacter(int x, int y, String::value_type c, int r, int g, int b, int a); ~VideoBuffer(); }; diff --git a/src/graphics/OpenGLGraphics.cpp b/src/graphics/OpenGLGraphics.cpp index f951b0dac..2651eb941 100644 --- a/src/graphics/OpenGLGraphics.cpp +++ b/src/graphics/OpenGLGraphics.cpp @@ -1,5 +1,5 @@ #include "Graphics.h" -#include "font.h" +#include "Font.h" #include "common/tpt-thread.h" #ifdef OGLI diff --git a/src/graphics/RasterDrawMethods.inl b/src/graphics/RasterDrawMethods.inl index 4bb1469bc..bd565f367 100644 --- a/src/graphics/RasterDrawMethods.inl +++ b/src/graphics/RasterDrawMethods.inl @@ -1,5 +1,5 @@ -#include "font.h" #include +#include "Font.h" int PIXELMETHODS_CLASS::drawtext_outline(int x, int y, String s, int r, int g, int b, int a) { @@ -105,44 +105,20 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, String str, int r, int g, int b, int PIXELMETHODS_CLASS::drawchar(int x, int y, String::value_type c, int r, int g, int b, int a) { - int i, j, w, bn = 0, ba = 0; - unsigned char *rp = font_data + font_ptrs[c]; - w = *(rp++); - for (j=-2; j>= 2; - bn -= 2; - } - return x + w; + FontReader reader(c); + for (int j = -2; j < FONT_H - 2; j++) + for (int i = 0; i < reader.GetWidth(); i++) + blendpixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3); + return x + reader.GetWidth(); } int PIXELMETHODS_CLASS::addchar(int x, int y, String::value_type c, int r, int g, int b, int a) { - int i, j, w, bn = 0, ba = 0; - unsigned char *rp = font_data + font_ptrs[c]; - w = *(rp++); - for (j=-2; j>= 2; - bn -= 2; - } - return x + w; + FontReader reader(c); + for (int j = -2; j < FONT_H - 2; j++) + for (int i = 0; i < reader.GetWidth(); i++) + addpixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3); + return x + reader.GetWidth(); } TPT_INLINE void PIXELMETHODS_CLASS::xor_pixel(int x, int y)