25 #include <Overlay/OgreFont.h>
26 #include <OgreHardwarePixelBuffer.h>
27 #include <OgreMaterial.h>
28 #include <OgreTechnique.h>
29 #include <OgreTexture.h>
30 #include <OgreTextureManager.h>
31 #include <Overlay/OgreFontManager.h>
36 void SaveImage(TexturePtr TextureToSave, String filename)
38 HardwarePixelBufferSharedPtr readbuffer;
39 readbuffer = TextureToSave->getBuffer(0, 0);
40 readbuffer->lock(HardwareBuffer::HBL_NORMAL);
41 const PixelBox& readrefpb = readbuffer->getCurrentLock();
42 uchar* readrefdata =
static_cast<uchar*
>(readrefpb.data);
45 img = img.loadDynamicImage(readrefdata, TextureToSave->getWidth(),
46 TextureToSave->getHeight(), TextureToSave->getFormat());
52 void WriteToTexture(
const String& str, TexturePtr destTexture, Ogre::Box destRectangle, Ogre::Font* Reffont,
const ColourValue& color,
int fontSize,
int fontDPI,
char justify,
bool wordwrap)
56 if (destTexture->getHeight() < destRectangle.bottom)
57 destRectangle.bottom = destTexture->getHeight();
58 if (destTexture->getWidth() < destRectangle.right)
59 destRectangle.right = destTexture->getWidth();
62 std::string fontname =
"WTTFont_" +
TOSTRING(fontSize + fontDPI) +
"_" + Reffont->getSource();
63 FontPtr font = FontManager::getSingleton().getByName(fontname);
68 font = FontManager::getSingleton().create(fontname,
"General");
69 font->setType(FT_TRUETYPE);
70 font->setSource(Reffont->getSource());
72 font->setTrueTypeSize(fontSize);
73 font->setTrueTypeResolution(fontDPI);
75 LOG(
"[WriteToTexture] Created font: " + fontname);
78 TexturePtr fontTexture = (TexturePtr)TextureManager::getSingleton().getByName(font->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName());
80 HardwarePixelBufferSharedPtr fontBuffer = fontTexture->getBuffer();
81 HardwarePixelBufferSharedPtr destBuffer = destTexture->getBuffer();
83 PixelBox destPb = destBuffer->lock(destRectangle, HardwareBuffer::HBL_NORMAL);
88 size_t nBuffSize = fontBuffer->getSizeInBytes();
89 uint8* buffer = (uint8*)calloc(nBuffSize,
sizeof(uint8));
92 PixelBox fontPb(fontBuffer->getWidth(), fontBuffer->getHeight(), fontBuffer->getDepth(), fontBuffer->getFormat(), buffer);
93 fontBuffer->blitToMemory(fontPb);
95 uint8* fontData =
static_cast<uint8*
>(fontPb.data);
96 uint8* destData =
static_cast<uint8*
>(destPb.data);
98 const size_t fontPixelSize = PixelUtil::getNumElemBytes(fontPb.format);
99 const size_t destPixelSize = PixelUtil::getNumElemBytes(destPb.format);
101 const size_t fontRowPitchBytes = fontPb.rowPitch * fontPixelSize;
102 const size_t destRowPitchBytes = destPb.rowPitch * destPixelSize;
105 GlyphTexCoords =
new Box[str.size()];
107 Ogre::Font::UVRect glypheTexRect;
108 size_t charheight = 0;
109 size_t charwidth = 0;
111 for (
unsigned int i = 0; i < str.size(); i++)
113 if ((str[i] !=
'\t') && (str[i] !=
'\n') && (str[i] !=
' '))
115 glypheTexRect = font->getGlyphTexCoords(str[i]);
116 GlyphTexCoords[i].left = glypheTexRect.left * fontTexture->getSrcWidth();
117 GlyphTexCoords[i].top = glypheTexRect.top * fontTexture->getSrcHeight();
118 GlyphTexCoords[i].right = glypheTexRect.right * fontTexture->getSrcWidth();
119 GlyphTexCoords[i].bottom = glypheTexRect.bottom * fontTexture->getSrcHeight();
121 if (GlyphTexCoords[i].getHeight() > charheight)
122 charheight = GlyphTexCoords[i].getHeight();
123 if (GlyphTexCoords[i].getWidth() > charwidth)
124 charwidth = GlyphTexCoords[i].getWidth();
130 size_t lineend = destRectangle.getWidth();
131 bool carriagreturn =
true;
132 for (
unsigned int strindex = 0; strindex < str.size(); strindex++)
134 switch (str[strindex])
136 case ' ': cursorX += charwidth;
138 case '\t': cursorX += charwidth * 3;
140 case '\n': cursorY += charheight;
141 carriagreturn =
true;
146 if ((cursorX + GlyphTexCoords[strindex].getWidth() > lineend) && !carriagreturn)
148 cursorY += charheight;
149 carriagreturn =
true;
156 size_t textwidth = 0;
157 size_t wordwidth = 0;
159 while (l < str.size() && str[l] !=
'\n')
165 case ' ': wordwidth = charwidth;
168 case '\t': wordwidth = charwidth * 3;
171 case '\n': l = str.size();
175 while (l < str.size() && str[l] !=
' ' && str[l] !=
'\t' && str[l] !=
'\n')
177 wordwidth += GlyphTexCoords[l].getWidth();
182 wordwidth += GlyphTexCoords[l].getWidth();
186 if ((textwidth + wordwidth) <= destRectangle.getWidth())
187 textwidth += (wordwidth);
192 if ((textwidth == 0) && (wordwidth > destRectangle.getWidth()))
193 textwidth = destRectangle.getWidth();
197 case 'c': cursorX = (destRectangle.getWidth() - textwidth) / 2;
198 lineend = destRectangle.getWidth() - cursorX;
201 case 'r': cursorX = (destRectangle.getWidth() - textwidth);
202 lineend = destRectangle.getWidth();
205 default: cursorX = 0;
210 carriagreturn =
false;
214 if ((cursorY + charheight) > destRectangle.getHeight())
218 for (
size_t i = 0; i < GlyphTexCoords[strindex].getHeight(); i++)
219 for (
size_t j = 0; j < GlyphTexCoords[strindex].getWidth(); j++)
221 float alpha = color.a * (fontData[(i + GlyphTexCoords[strindex].top) * fontRowPitchBytes + (j + GlyphTexCoords[strindex].left) * fontPixelSize + 1] / 255.0);
222 float invalpha = 1.0 - alpha;
223 size_t offset = (i + cursorY) * destRowPitchBytes + (j + cursorX) * destPixelSize;
225 PixelUtil::unpackColour(&pix, destPb.format, &destData[offset]);
226 pix = (pix * invalpha) + (color * alpha);
227 PixelUtil::packColour(pix, destPb.format, &destData[offset]);
230 cursorX += GlyphTexCoords[strindex].getWidth();
236 delete[] GlyphTexCoords;
238 destBuffer->unlock();