123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #ifndef _BITMAP_FONT_H_
- #include "bitmapFont/BitmapFont.h"
- #endif
- #ifndef _STRING_UNIT_H_
- #include "string/stringUnit.h"
- #endif
- #include <string>
- namespace font
- {
- BitmapFont::BitmapFont()
- {
- }
- bool BitmapFont::parseFont(Stream& io_rStream)
- {
- U32 numBytes = io_rStream.getStreamSize() - io_rStream.getPosition();
- while ((io_rStream.getStatus() != Stream::EOS) && numBytes > 0)
- {
- char Read[256];
- char Token[256];
- char *buffer = Con::getReturnBuffer(256);
- io_rStream.readLine((U8 *)buffer, 256);
- char temp[256];
- U32 tokenCount = StringUnit::getUnitCount(buffer, "\"");
- if (tokenCount > 1)
- {
- dSprintf(Token, 256, "%s", StringUnit::getUnit(buffer, 1, "\""));
- dSprintf(temp, 256, "tok1");
- dSprintf(buffer, 256, "%s", (char*)StringUnit::setUnit(buffer, 1, temp, "\""));
- }
- U32 wordCount = StringUnit::getUnitCount(buffer, " \t\n");
- dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, 0, " \t\n"));
- if (dStrcmp(Read, "info") == 0)
- {
- U32 currentWordCount = 1;
- while (currentWordCount < wordCount)
- {
- dSprintf(Read, 256, StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
- char temp[256];
- char Key[256];
- char Value[256];
- dSprintf(temp, 256, "%s", Read);
- dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
- dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
- if (dStrcmp(Value, "\"tok1\"") == 0) {
- dSprintf(Value, 256, "%s", Token);
- }
- if (dStrcmp(Key, "size") == 0)
- mSize = U16(dAtoi(Value));
- currentWordCount++;
- }
- }
- if (dStrcmp(Read, "common") == 0)
- {
- //this holds common data
- U32 currentWordCount = 1;
- while (currentWordCount < wordCount)
- {
- dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
- char temp[256];
- char Key[256];
- char Value[256];
- dSprintf(temp, 256, "%s", Read);
- dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
- dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
- if (dStrcmp(Value, "\"tok1\"") == 0) {
- dSprintf(Value, 256, "%s", Token);
- }
- if (dStrcmp(Key, "lineHeight") == 0)
- mLineHeight = U16(dAtoi(Value));
- else if (dStrcmp(Key, "base") == 0)
- mBaseline = U16(dAtoi(Value));
- else if (dStrcmp(Key, "scaleW") == 0)
- mWidth = U16(dAtoi(Value));
- else if (dStrcmp(Key, "scaleH") == 0)
- mHeight = U16(dAtoi(Value));
- else if (dStrcmp(Key, "pages") == 0)
- mPages = U16(dAtoi(Value));
- currentWordCount++;
- }
- }
- else if (dStrcmp(Read, "page") == 0)
- {
- //this is data for a page
- U32 currentWordCount = 1;
- while (currentWordCount < wordCount)
- {
- dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
- char temp[256];
- char Key[256];
- char Value[256];
- dSprintf(temp, 256, "%s", Read);
- dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
- dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
- if (dStrcmp(Value, "\"tok1\"") == 0) {
- dSprintf(Value, 256, "%s", Token);
- }
- //assign the correct value
- if (dStrcmp(Key, "file") == 0)
- {
- mPageName.push_back(StringTable->insert(Value));
- }
- currentWordCount++;
- }
- }
- else if (dStrcmp(Read, "char") == 0 && dStrcmp(Read, "chars") != 0)
- {
- //this is data for a character
- BitmapFontCharacter ci;
- U16 CharID = 0;
- U32 currentWordCount = 1;
- while (currentWordCount < wordCount)
- {
- dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
- char temp[256];
- char Key[256];
- char Value[256];
- dSprintf(temp, 256, "%s", Read);
- dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
- dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
- if (dStrcmp(Value, "\"tok1\"") == 0) {
- dSprintf(Value, 256, "%s", Token);
- }
- //assign the correct value
- if (dStrcmp(Key, "id") == 0)
- CharID = U32(dAtoi(Value));
- else if (dStrcmp(Key, "x") == 0)
- ci.mX = U32(dAtoi(Value));
- else if (dStrcmp(Key, "y") == 0)
- ci.mY = U32(dAtoi(Value));
- else if (dStrcmp(Key, "width") == 0)
- ci.mWidth = U32(dAtoi(Value));
- else if (dStrcmp(Key, "height") == 0)
- ci.mHeight = U32(dAtoi(Value));
- else if (dStrcmp(Key, "xoffset") == 0)
- ci.mXOffset = F32(dAtoi(Value));
- else if (dStrcmp(Key, "yoffset") == 0)
- ci.mYOffset = F32(dAtoi(Value));
- else if (dStrcmp(Key, "xadvance") == 0)
- ci.mXAdvance = F32(dAtoi(Value));
- else if (dStrcmp(Key, "page") == 0)
- ci.mPage = S32(dAtoi(Value));
- currentWordCount++;
- }
- ci.mCharID = CharID;
- ci.ProcessCharacter(mWidth, mHeight);
- mChar[CharID] = ci;
- }
- else if (dStrcmp(Read, "kerning") == 0 && dStrcmp(Read, "kernings") != 0)
- {
- //this is data for a kerning pair
- U16 first = 0;
- U16 second = 0;
- S16 amount = 0;
- U32 currentWordCount = 1;
- while (currentWordCount < wordCount)
- {
- dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
- char temp[256];
- char Key[256];
- char Value[256];
- dSprintf(temp, 256, "%s", Read);
- dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
- dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
- if (dStrcmp(Value, "\"tok1\"") == 0) {
- dSprintf(Value, 256, "%s", Token);
- }
- //assign the correct value
- if (dStrcmp(Key, "first") == 0)
- first = U16(dAtoi(Value));
- else if (dStrcmp(Key, "second") == 0)
- second = U16(dAtoi(Value));
- else if (dStrcmp(Key, "amount") == 0)
- amount = S16(dAtoi(Value));
-
- currentWordCount++;
- }
- AddKerning(first, second, amount);
- }
- }
- return (io_rStream.getStatus() == Stream::EOS);
- }
- TextureHandle BitmapFont::LoadTexture(StringTableEntry fileName)
- {
- // Debug Profiling.
- PROFILE_SCOPE(FontAsset_LoadTexture);
- // Get image texture.
- TextureHandle mImageTextureHandle;
- mImageTextureHandle.set(fileName, TextureHandle::BitmapTexture, true, false);
- // Is the texture valid?
- if (mImageTextureHandle.IsNull())
- {
- // No, so warn.
- Con::warnf("FontAsset could not load texture '%s'.", fileName);
- return mImageTextureHandle;
- }
- //Set the filter mode. For now we'll just support bilinear. In the future we should give the user an option.
- mImageTextureHandle.setFilter(GL_LINEAR);
- return mImageTextureHandle;
- }
- }
|