gFont.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/gFont.h"
  24. #include "core/resourceManager.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/strings/unicode.h"
  27. #include "core/strings/findMatch.h"
  28. #include "core/strings/stringFunctions.h"
  29. #include "core/util/endian.h"
  30. #include "core/util/safeDelete.h"
  31. #include "console/console.h"
  32. #include "console/engineAPI.h"
  33. #include "platform/threads/mutex.h"
  34. #include "zlib/zlib.h"
  35. GFX_ImplementTextureProfile(GFXFontTextureProfile,
  36. GFXTextureProfile::DiffuseMap,
  37. GFXTextureProfile::PreserveSize |
  38. GFXTextureProfile::Static |
  39. GFXTextureProfile::KeepBitmap |
  40. GFXTextureProfile::NoMipmap,
  41. GFXTextureProfile::NONE);
  42. template<> void *Resource<GFont>::create(const Torque::Path &path)
  43. {
  44. #ifdef TORQUE_DEBUG_RES_MANAGER
  45. Con::printf( "Resource<GFont>::create - [%s]", path.getFullPath().c_str() );
  46. #endif
  47. return GFont::load( path );
  48. }
  49. template<> ResourceBase::Signature Resource<GFont>::signature()
  50. {
  51. return MakeFourCC('f','o','n','t');
  52. }
  53. /// Used for repacking in GFont::importStrip.
  54. struct GlyphMap
  55. {
  56. U32 charId;
  57. GBitmap *bitmap;
  58. };
  59. static S32 QSORT_CALLBACK GlyphMapCompare(const void *a, const void *b)
  60. {
  61. S32 ha = ((GlyphMap *) a)->bitmap->getHeight();
  62. S32 hb = ((GlyphMap *) b)->bitmap->getHeight();
  63. return hb - ha;
  64. }
  65. const U32 GFont::csm_fileVersion = 3;
  66. String GFont::getFontCacheFilename(const String &faceName, U32 size)
  67. {
  68. return String::ToString("%s/%s %d (%s).uft",
  69. Con::getVariable("$GUI::fontCacheDirectory"), faceName.c_str(), size, getCharSetName(0));
  70. }
  71. GFont* GFont::load( const Torque::Path& path )
  72. {
  73. FileStream stream;
  74. stream.open( path.getFullPath(), Torque::FS::File::Read );
  75. if ( stream.getStatus() != Stream::Ok )
  76. return NULL;
  77. GFont *ret = new GFont;
  78. ret->mGFTFile = path;
  79. if(!ret->read(stream))
  80. {
  81. Con::errorf( "GFont::load - error reading '%s'", path.getFullPath().c_str() );
  82. SAFE_DELETE(ret);
  83. }
  84. else
  85. {
  86. PlatformFont *platFont = createPlatformFont(ret->getFontFaceName(), ret->getFontSize(), ret->getFontCharSet());
  87. if ( platFont == NULL )
  88. {
  89. Con::errorf( "GFont::load - error creating platform font for '%s'", path.getFullPath().c_str() );
  90. SAFE_DELETE(ret);
  91. }
  92. else
  93. ret->setPlatformFont(platFont);
  94. }
  95. return ret;
  96. }
  97. Resource<GFont> GFont::create(const String &faceName, U32 size, const char *cacheDirectory, U32 charset /* = TGE_ANSI_CHARSET */)
  98. {
  99. if( !cacheDirectory )
  100. cacheDirectory = Con::getVariable( "$GUI::fontCacheDirectory" );
  101. const Torque::Path path( String::ToString("%s/%s %d (%s).uft",
  102. cacheDirectory, faceName.c_str(), size, getCharSetName(charset)) );
  103. Resource<GFont> ret;
  104. // If the file already exists attempt to load it
  105. if (Torque::FS::IsFile(path.getFullPath().c_str()))
  106. {
  107. ret = ResourceManager::get().load(path);
  108. if (ret != NULL)
  109. {
  110. ret->mGFTFile = path;
  111. return ret;
  112. }
  113. }
  114. // Otherwise attempt to have the platform generate a new font
  115. PlatformFont *platFont = createPlatformFont(faceName, size, charset);
  116. if (platFont == NULL)
  117. {
  118. String fontName;
  119. #ifdef _XBOX
  120. //AssertFatal( false, "Font creation is not supported on the Xbox platform. Create the font files (*.uft) using the Windows/MacOS build of the project." );
  121. if(!faceName.equal("arial", String::NoCase) || size != 14)
  122. {
  123. return create("Arial", 14, cacheDirectory, charset);
  124. }
  125. return ret;
  126. #endif
  127. // Couldn't load the requested font. This probably will be common
  128. // since many unix boxes don't have arial or lucida console installed.
  129. // Attempt to map the font name into a font we're pretty sure exist
  130. // Lucida Console is a common code & console font on windows, and
  131. // Monaco is the recommended code & console font on mac.
  132. if (faceName.equal("arial", String::NoCase))
  133. fontName = "Helvetica";
  134. else if (faceName.equal("lucida console", String::NoCase))
  135. fontName = "Monaco";
  136. else if (faceName.equal("monaco", String::NoCase))
  137. fontName = "Courier";
  138. else
  139. return ret;
  140. return create(fontName, size, cacheDirectory, charset);
  141. }
  142. // Create the actual GFont and set some initial properties
  143. GFont *font = new GFont;
  144. font->mPlatformFont = platFont;
  145. font->mGFTFile = path;
  146. font->mFaceName = faceName;
  147. font->mSize = size;
  148. font->mCharSet = charset;
  149. font->mHeight = platFont->getFontHeight();
  150. font->mBaseline = platFont->getFontBaseLine();
  151. font->mAscent = platFont->getFontBaseLine();
  152. font->mDescent = platFont->getFontHeight() - platFont->getFontBaseLine();
  153. // Flag it to save when we exit
  154. font->mNeedSave = true;
  155. // Load the newly created font into the ResourceManager
  156. ret.setResource(ResourceManager::get().load(path), font);
  157. return ret;
  158. }
  159. //-------------------------------------------------------------------------
  160. GFont::GFont()
  161. {
  162. VECTOR_SET_ASSOCIATION(mCharInfoList);
  163. VECTOR_SET_ASSOCIATION(mTextureSheets);
  164. std::fill_n(mRemapTable, Font_Table_MAX,-1);
  165. mCurX = mCurY = mCurSheet = -1;
  166. mPlatformFont = NULL;
  167. mSize = 0;
  168. mCharSet = 0;
  169. mHeight = 0;
  170. mBaseline = 0;
  171. mAscent = 0;
  172. mDescent = 0;
  173. mNeedSave = false;
  174. mMutex = Mutex::createMutex();
  175. }
  176. GFont::~GFont()
  177. {
  178. if(mNeedSave)
  179. {
  180. AssertFatal( mGFTFile.getFullPath().isNotEmpty(), "GFont::~GFont - path not set" );
  181. FileStream stream;
  182. stream.open(mGFTFile, Torque::FS::File::Write);
  183. if(stream.getStatus() == Stream::Ok)
  184. write(stream);
  185. stream.close();
  186. }
  187. S32 i;
  188. for(i = 0;i < mCharInfoList.size();i++)
  189. {
  190. SAFE_DELETE_ARRAY(mCharInfoList[i].bitmapData);
  191. }
  192. for(i=0; i<mTextureSheets.size(); i++)
  193. mTextureSheets[i] = NULL;
  194. SAFE_DELETE(mPlatformFont);
  195. Mutex::destroyMutex(mMutex);
  196. }
  197. void GFont::dumpInfo() const
  198. {
  199. // Number and extent of mapped characters?
  200. U32 mapCount = 0, mapBegin=0xFFFF, mapEnd=0;
  201. for(U32 i=0; i<0x10000; i++)
  202. {
  203. if(mRemapTable[i] != -1)
  204. {
  205. mapCount++;
  206. if(i<mapBegin) mapBegin = i;
  207. if(i>mapEnd) mapEnd = i;
  208. }
  209. }
  210. // Let's write out all the info we can on this font.
  211. Con::printf(" '%s' %dpt", mFaceName.c_str(), mSize);
  212. Con::printf(" - %d texture sheets, %d mapped characters.", mTextureSheets.size(), mapCount);
  213. if(mapCount)
  214. Con::printf(" - Codepoints range from 0x%x to 0x%x.", mapBegin, mapEnd);
  215. else
  216. Con::printf(" - No mapped codepoints.");
  217. Con::printf(" - Platform font is %s.", (mPlatformFont ? "present" : "not present") );
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool GFont::loadCharInfo(const UTF16 ch)
  221. {
  222. PROFILE_SCOPE(GFont_loadCharInfo);
  223. if(mRemapTable[ch] != -1)
  224. return true; // Not really an error
  225. if(mPlatformFont && mPlatformFont->isValidChar(ch))
  226. {
  227. Mutex::lockMutex(mMutex); // the CharInfo returned by mPlatformFont is static data, must protect from changes.
  228. PlatformFont::CharInfo &ci = mPlatformFont->getCharInfo(ch);
  229. if(ci.bitmapData)
  230. addBitmap(ci);
  231. mCharInfoList.push_back(ci);
  232. mRemapTable[ch] = mCharInfoList.size() - 1;
  233. mNeedSave = true;
  234. Mutex::unlockMutex(mMutex);
  235. return true;
  236. }
  237. return false;
  238. }
  239. void GFont::addBitmap(PlatformFont::CharInfo &charInfo)
  240. {
  241. U32 nextCurX = U32(mCurX + charInfo.width ); /*7) & ~0x3;*/
  242. U32 nextCurY = U32(mCurY + mPlatformFont->getFontHeight()); // + 7) & ~0x3;
  243. // These are here for postmortem debugging.
  244. bool routeA = false, routeB = false;
  245. if(mCurSheet == -1 || nextCurY >= TextureSheetSize)
  246. {
  247. routeA = true;
  248. addSheet();
  249. // Recalc our nexts.
  250. nextCurX = U32(mCurX + charInfo.width); // + 7) & ~0x3;
  251. nextCurY = U32(mCurY + mPlatformFont->getFontHeight()); // + 7) & ~0x3;
  252. }
  253. if( nextCurX >= TextureSheetSize)
  254. {
  255. routeB = true;
  256. mCurX = 0;
  257. mCurY = nextCurY;
  258. // Recalc our nexts.
  259. nextCurX = U32(mCurX + charInfo.width); // + 7) & ~0x3;
  260. nextCurY = U32(mCurY + mPlatformFont->getFontHeight()); // + 7) & ~0x3;
  261. }
  262. // Check the Y once more - sometimes we advance to a new row and run off
  263. // the end.
  264. if(nextCurY >= TextureSheetSize)
  265. {
  266. routeA = true;
  267. addSheet();
  268. // Recalc our nexts.
  269. nextCurX = U32(mCurX + charInfo.width); // + 7) & ~0x3;
  270. nextCurY = U32(mCurY + mPlatformFont->getFontHeight()); // + 7) & ~0x3;
  271. }
  272. charInfo.bitmapIndex = mCurSheet;
  273. charInfo.xOffset = mCurX;
  274. charInfo.yOffset = mCurY;
  275. mCurX = nextCurX;
  276. S32 x, y;
  277. GBitmap *bmp = mTextureSheets[mCurSheet].getBitmap();
  278. AssertFatal(bmp->getFormat() == GFXFormatA8, "GFont::addBitmap - cannot added characters to non-greyscale textures!");
  279. for(y = 0;y < charInfo.height;y++)
  280. for(x = 0;x < charInfo.width;x++)
  281. *bmp->getAddress(x + charInfo.xOffset, y + charInfo.yOffset) = charInfo.bitmapData[y * charInfo.width + x];
  282. mTextureSheets[mCurSheet].refresh();
  283. }
  284. void GFont::addSheet()
  285. {
  286. GBitmap *bitmap = new GBitmap(TextureSheetSize, TextureSheetSize, false, GFXFormatA8 );
  287. // Set everything to transparent.
  288. U8 *bits = bitmap->getWritableBits();
  289. dMemset(bits, 0, sizeof(U8) *TextureSheetSize*TextureSheetSize);
  290. GFXTexHandle handle = GFXTexHandle( bitmap, &GFXFontTextureProfile, true, avar("%s() - (line %d)", __FUNCTION__, __LINE__) );
  291. mTextureSheets.increment();
  292. mTextureSheets.last() = handle;
  293. mCurX = 0;
  294. mCurY = 0;
  295. mCurSheet = mTextureSheets.size() - 1;
  296. }
  297. //-----------------------------------------------------------------------------
  298. const PlatformFont::CharInfo &GFont::getCharInfo(const UTF16 in_charIndex)
  299. {
  300. PROFILE_SCOPE(GFont_getCharInfo);
  301. AssertFatal(in_charIndex, "GFont::getCharInfo - can't get info for char 0!");
  302. if(mRemapTable[in_charIndex] == -1)
  303. loadCharInfo(in_charIndex);
  304. AssertFatal(mRemapTable[in_charIndex] != -1, "No remap info for this character");
  305. return mCharInfoList[mRemapTable[in_charIndex]];
  306. }
  307. const PlatformFont::CharInfo &GFont::getDefaultCharInfo()
  308. {
  309. static PlatformFont::CharInfo c;
  310. // c is initialized by the CharInfo default constructor.
  311. return c;
  312. }
  313. //-----------------------------------------------------------------------------
  314. U32 GFont::getStrWidth(const UTF8* in_pString)
  315. {
  316. AssertFatal(in_pString != NULL, "GFont::getStrWidth: String is NULL, height is undefined");
  317. // If we ain't running debug...
  318. if (in_pString == NULL || *in_pString == '\0')
  319. return 0;
  320. return getStrNWidth(in_pString, dStrlen(in_pString));
  321. }
  322. U32 GFont::getStrWidthPrecise(const UTF8* in_pString)
  323. {
  324. AssertFatal(in_pString != NULL, "GFont::getStrWidth: String is NULL, height is undefined");
  325. // If we ain't running debug...
  326. if (in_pString == NULL)
  327. return 0;
  328. return getStrNWidthPrecise(in_pString, dStrlen(in_pString));
  329. }
  330. //-----------------------------------------------------------------------------
  331. U32 GFont::getStrNWidth(const UTF8 *str, U32 n)
  332. {
  333. // UTF8 conversion is expensive. Avoid converting in a tight loop.
  334. FrameTemp<UTF16> str16(n + 1);
  335. convertUTF8toUTF16N(str, str16, n + 1);
  336. return getStrNWidth(str16, dStrlen(str16));
  337. }
  338. U32 GFont::getStrNWidth(const UTF16 *str, U32 n)
  339. {
  340. AssertFatal(str != NULL, "GFont::getStrNWidth: String is NULL");
  341. if (str == NULL || str[0] == '\0' || n == 0)
  342. return 0;
  343. U32 totWidth = 0;
  344. UTF16 curChar;
  345. U32 charCount;
  346. for(charCount = 0; charCount < n; charCount++)
  347. {
  348. curChar = str[charCount];
  349. if(curChar == '\0')
  350. break;
  351. if(isValidChar(curChar))
  352. {
  353. const PlatformFont::CharInfo& rChar = getCharInfo(curChar);
  354. totWidth += rChar.xIncrement;
  355. }
  356. else if (curChar == dT('\t'))
  357. {
  358. const PlatformFont::CharInfo& rChar = getCharInfo(dT(' '));
  359. totWidth += rChar.xIncrement * TabWidthInSpaces;
  360. }
  361. }
  362. return(totWidth);
  363. }
  364. U32 GFont::getStrNWidthPrecise(const UTF8 *str, U32 n)
  365. {
  366. FrameTemp<UTF16> str16(n + 1);
  367. convertUTF8toUTF16N(str, str16, n + 1);
  368. return getStrNWidthPrecise(str16, dStrlen(str16));
  369. }
  370. U32 GFont::getStrNWidthPrecise(const UTF16 *str, U32 n)
  371. {
  372. AssertFatal(str != NULL, "GFont::getStrNWidth: String is NULL");
  373. if (str == NULL || str[0] == '\0' || n == 0)
  374. return(0);
  375. U32 totWidth = 0;
  376. UTF16 curChar;
  377. U32 charCount = 0;
  378. for(charCount = 0; charCount < n; charCount++)
  379. {
  380. curChar = str[charCount];
  381. if(curChar == '\0')
  382. break;
  383. if(isValidChar(curChar))
  384. {
  385. const PlatformFont::CharInfo& rChar = getCharInfo(curChar);
  386. totWidth += rChar.xIncrement;
  387. }
  388. else if (curChar == dT('\t'))
  389. {
  390. const PlatformFont::CharInfo& rChar = getCharInfo(dT(' '));
  391. totWidth += rChar.xIncrement * TabWidthInSpaces;
  392. }
  393. }
  394. UTF16 endChar = str[getMin(charCount,n-1)];
  395. if (isValidChar(endChar))
  396. {
  397. const PlatformFont::CharInfo& rChar = getCharInfo(endChar);
  398. if (rChar.width != rChar.xIncrement)
  399. totWidth += (rChar.width - rChar.xIncrement);
  400. }
  401. return(totWidth);
  402. }
  403. U32 GFont::getBreakPos(const UTF16 *str16, U32 slen, U32 width, bool breakOnWhitespace)
  404. {
  405. // Some early out cases.
  406. if(slen==0)
  407. return 0;
  408. U32 ret = 0;
  409. U32 lastws = 0;
  410. UTF16 c;
  411. U32 charCount = 0;
  412. for( charCount=0; charCount < slen; charCount++)
  413. {
  414. c = str16[charCount];
  415. if(c == '\0')
  416. break;
  417. if(c == dT('\t'))
  418. c = dT(' ');
  419. if(!isValidChar(c))
  420. {
  421. ret++;
  422. continue;
  423. }
  424. if(c == dT(' '))
  425. lastws = ret+1;
  426. const PlatformFont::CharInfo& rChar = getCharInfo(c);
  427. if(rChar.width > width || rChar.xIncrement > width)
  428. {
  429. if(lastws && breakOnWhitespace)
  430. return lastws;
  431. return ret;
  432. }
  433. width -= rChar.xIncrement;
  434. ret++;
  435. }
  436. return ret;
  437. }
  438. void GFont::wrapString(const UTF8 *txt, U32 lineWidth, Vector<U32> &startLineOffset, Vector<U32> &lineLen)
  439. {
  440. // TODO: Is this error still true?
  441. //Con::errorf("GFont::wrapString(): Not yet converted to be UTF-8 safe");
  442. startLineOffset.clear();
  443. lineLen.clear();
  444. if (!txt || !txt[0] || lineWidth < getCharWidth('W')) //make sure the line width is greater then a single character
  445. return;
  446. U32 len = dStrlen(txt);
  447. U32 startLine;
  448. for (U32 i = 0; i < len;)
  449. {
  450. U32 wide = 0;
  451. startLine = i;
  452. startLineOffset.push_back(startLine);
  453. // loop until the string is too large
  454. bool needsNewLine = false;
  455. U32 lineStrWidth = 0;
  456. for (; i < len; i++)
  457. {
  458. if( txt[ i ] == '\n' )
  459. {
  460. needsNewLine = true;
  461. break;
  462. }
  463. else if(isValidChar(txt[i]))
  464. {
  465. lineStrWidth += getCharInfo(txt[i]).xIncrement;
  466. if(txt[i] < 0) // symbols which code > 127
  467. {
  468. wide++; i++;
  469. }
  470. if( lineStrWidth > lineWidth )
  471. {
  472. needsNewLine = true;
  473. break;
  474. }
  475. }
  476. }
  477. if (!needsNewLine)
  478. {
  479. // we are done!
  480. lineLen.push_back(i - startLine - wide);
  481. return;
  482. }
  483. S32 j;
  484. // Did we hit a hardwrap (newline character) in the string.
  485. bool hardwrap = ( txt[i] == '\n' );
  486. if ( hardwrap )
  487. {
  488. j = i;
  489. }
  490. // determine where to put the newline
  491. // we need to backtrack until we find a space character
  492. // we don't do this for hardwrap(s)
  493. else
  494. {
  495. for (j = i - 1; j >= startLine; j--)
  496. {
  497. if ( dIsspace(txt[j]) || txt[i] == '\n' )
  498. break;
  499. }
  500. if (j < startLine)
  501. {
  502. // the line consists of a single word!
  503. // So, just break up the word
  504. j = i - 1;
  505. }
  506. }
  507. lineLen.push_back(j - startLine - wide);
  508. i = j;
  509. // Now we need to increment through any space characters at the
  510. // beginning of the next line.
  511. // We don't skip spaces after a hardwrap because they were obviously intended.
  512. for (i++; i < len; i++)
  513. {
  514. if ( txt[i] == '\n' )
  515. continue;
  516. if ( dIsspace( txt[i] ) && !hardwrap )
  517. continue;
  518. break;
  519. }
  520. }
  521. }
  522. //-----------------------------------------------------------------------------
  523. bool GFont::read(Stream& io_rStream)
  524. {
  525. // Handle versioning
  526. U32 version;
  527. io_rStream.read(&version);
  528. if(version != csm_fileVersion)
  529. return false;
  530. char buf[256];
  531. io_rStream.readString(buf);
  532. mFaceName = buf;
  533. io_rStream.read(&mSize);
  534. io_rStream.read(&mCharSet);
  535. io_rStream.read(&mHeight);
  536. io_rStream.read(&mBaseline);
  537. io_rStream.read(&mAscent);
  538. io_rStream.read(&mDescent);
  539. U32 size = 0;
  540. io_rStream.read(&size);
  541. mCharInfoList.setSize(size);
  542. U32 i;
  543. for(i = 0; i < size; i++)
  544. {
  545. PlatformFont::CharInfo *ci = &mCharInfoList[i];
  546. io_rStream.read(&ci->bitmapIndex);
  547. io_rStream.read(&ci->xOffset);
  548. io_rStream.read(&ci->yOffset);
  549. io_rStream.read(&ci->width);
  550. io_rStream.read(&ci->height);
  551. io_rStream.read(&ci->xOrigin);
  552. io_rStream.read(&ci->yOrigin);
  553. io_rStream.read(&ci->xIncrement);
  554. ci->bitmapData = NULL;
  555. }
  556. U32 numSheets = 0;
  557. io_rStream.read(&numSheets);
  558. for(i = 0; i < numSheets; i++)
  559. {
  560. GBitmap *bmp = new GBitmap;
  561. /*String path = String::ToString("%s/%s %d %d (%s).png", Con::getVariable("$GUI::fontCacheDirectory"), mFaceName.c_str(), mSize, i, getCharSetName(mCharSet));
  562. if(!bmp->readBitmap("png", path))
  563. {
  564. delete bmp;
  565. return false;
  566. }*/
  567. U32 len;
  568. io_rStream.read(&len);
  569. if (!bmp->readBitmapStream("png", io_rStream, len))
  570. {
  571. delete bmp;
  572. return false;
  573. }
  574. GFXTexHandle handle = GFXTexHandle(bmp, &GFXFontTextureProfile, true, avar("%s() - Read Font Sheet for %s %d (line %d)", __FUNCTION__, mFaceName.c_str(), mSize, __LINE__));
  575. //handle.setFilterNearest();
  576. mTextureSheets.push_back(handle);
  577. }
  578. // Read last position info
  579. io_rStream.read(&mCurX);
  580. io_rStream.read(&mCurY);
  581. io_rStream.read(&mCurSheet);
  582. // Read the remap table.
  583. U32 minGlyph, maxGlyph;
  584. io_rStream.read(&minGlyph);
  585. io_rStream.read(&maxGlyph);
  586. if(maxGlyph >= minGlyph)
  587. {
  588. // Length of buffer..
  589. U32 buffLen;
  590. io_rStream.read(&buffLen);
  591. // Read the buffer.
  592. FrameTemp<S32> inBuff(buffLen);
  593. io_rStream.read(buffLen, inBuff);
  594. // Decompress.
  595. uLongf destLen = (static_cast<unsigned long long>(maxGlyph) - minGlyph + 1) * sizeof(S32);
  596. uncompress((Bytef*)&mRemapTable[minGlyph], &destLen, (Bytef*)(S32*)inBuff, buffLen);
  597. AssertISV(destLen == (maxGlyph-minGlyph+1)*sizeof(S32), "GFont::read - invalid remap table data!");
  598. // Make sure we've got the right endianness.
  599. for(i = minGlyph; i <= maxGlyph; i++)
  600. mRemapTable[i] = convertBEndianToHost(mRemapTable[i]);
  601. }
  602. return (io_rStream.getStatus() == Stream::Ok);
  603. }
  604. bool GFont::write(Stream& stream)
  605. {
  606. // Handle versioning
  607. stream.write(csm_fileVersion);
  608. // Write font info
  609. stream.write(mFaceName);
  610. stream.write(mSize);
  611. stream.write(mCharSet);
  612. stream.write(mHeight);
  613. stream.write(mBaseline);
  614. stream.write(mAscent);
  615. stream.write(mDescent);
  616. // Write char info list
  617. stream.write(U32(mCharInfoList.size()));
  618. U32 i;
  619. for(i = 0; i < mCharInfoList.size(); i++)
  620. {
  621. const PlatformFont::CharInfo *ci = &mCharInfoList[i];
  622. stream.write(ci->bitmapIndex);
  623. stream.write(ci->xOffset);
  624. stream.write(ci->yOffset);
  625. stream.write(ci->width);
  626. stream.write(ci->height);
  627. stream.write(ci->xOrigin);
  628. stream.write(ci->yOrigin);
  629. stream.write(ci->xIncrement);
  630. }
  631. stream.write(mTextureSheets.size());
  632. for (i = 0; i < mTextureSheets.size(); i++)
  633. {
  634. /*String path = String::ToString("%s/%s %d %d (%s).png", Con::getVariable("$GUI::fontCacheDirectory"), mFaceName.c_str(), mSize, i, getCharSetName(mCharSet));
  635. mTextureSheets[i].getBitmap()->writeBitmap("png", path);*/
  636. mTextureSheets[i].getBitmap()->writeBitmapStream("png", stream);
  637. }
  638. stream.write(mCurX);
  639. stream.write(mCurY);
  640. stream.write(mCurSheet);
  641. // Get the min/max we have values for, and only write that range out.
  642. S32 minGlyph = S32_MAX, maxGlyph = 0;
  643. for(i = 0; i < 65536; i++)
  644. {
  645. if(mRemapTable[i] != -1)
  646. {
  647. if(i>maxGlyph) maxGlyph = i;
  648. if(i<minGlyph) minGlyph = i;
  649. }
  650. }
  651. stream.write(minGlyph);
  652. stream.write(maxGlyph);
  653. // Skip it if we don't have any glyphs to do...
  654. if(maxGlyph >= minGlyph)
  655. {
  656. // Put everything big endian, to be consistent. Do this inplace.
  657. for(i = minGlyph; i <= maxGlyph; i++)
  658. mRemapTable[i] = convertHostToBEndian(mRemapTable[i]);
  659. {
  660. // Compress.
  661. const U32 buffSize = 128 * 1024;
  662. FrameTemp<S32> outBuff(buffSize);
  663. uLongf destLen = buffSize * sizeof(S32);
  664. compress2((Bytef*)(S32*)outBuff, &destLen, (Bytef*)(S32*)&mRemapTable[minGlyph], (static_cast<unsigned long long>(maxGlyph) - minGlyph + 1) * sizeof(S32), 9);
  665. // Write out.
  666. stream.write((U32)destLen);
  667. stream.write(destLen, outBuff);
  668. }
  669. // Put us back to normal.
  670. for(i = minGlyph; i <= maxGlyph; i++)
  671. mRemapTable[i] = convertBEndianToHost(mRemapTable[i]);
  672. }
  673. return (stream.getStatus() == Stream::Ok);
  674. }
  675. void GFont::exportStrip(const char *fileName, U32 padding, U32 kerning)
  676. {
  677. // Figure dimensions of our strip by iterating over all the char infos.
  678. U32 totalHeight = 0;
  679. U32 totalWidth = 0;
  680. S32 heightMin=0, heightMax=0;
  681. for(S32 i=0; i<mCharInfoList.size(); i++)
  682. {
  683. totalWidth += mCharInfoList[i].width + kerning + 2*padding;
  684. heightMin = getMin((S32)heightMin, (S32)getBaseline() - (S32)mCharInfoList[i].yOrigin);
  685. heightMax = getMax((S32)heightMax, (S32)getBaseline() - (S32)mCharInfoList[i].yOrigin + (S32)mCharInfoList[i].height);
  686. }
  687. totalHeight = heightMax - heightMin + 2*padding;
  688. // Make the bitmap.
  689. GBitmap gb(totalWidth, totalHeight, false, mTextureSheets[0].getBitmap()->getFormat());
  690. dMemset(gb.getWritableBits(), 0, sizeof(U8) * totalHeight * totalWidth );
  691. // Ok, copy some rects, taking into account padding, kerning, offset.
  692. U32 curWidth = kerning + padding;
  693. for(S32 i=0; i<mCharInfoList.size(); i++)
  694. {
  695. // Skip invalid stuff.
  696. if(mCharInfoList[i].bitmapIndex == -1 || mCharInfoList[i].height == 0 || mCharInfoList[i].width == 0)
  697. continue;
  698. // Copy the rect.
  699. U32 bitmap = mCharInfoList[i].bitmapIndex;
  700. RectI ri(mCharInfoList[i].xOffset, mCharInfoList[i].yOffset, mCharInfoList[i].width, mCharInfoList[i].height );
  701. Point2I outRi(curWidth, padding + getBaseline() - mCharInfoList[i].yOrigin);
  702. gb.copyRect(mTextureSheets[bitmap].getBitmap(), ri, outRi);
  703. // Advance.
  704. curWidth += mCharInfoList[i].width + kerning + 2*padding;
  705. }
  706. // Done!
  707. gb.writeBitmap("png", fileName);
  708. }
  709. void GFont::setPlatformFont(PlatformFont *inPlatformFont)
  710. {
  711. AssertFatal( mPlatformFont == NULL, "Trying to set platform font which already exists");
  712. mPlatformFont = inPlatformFont;
  713. }
  714. void GFont::importStrip(const char *fileName, U32 padding, U32 kerning)
  715. {
  716. // Wipe our texture sheets, and reload bitmap data from the specified file.
  717. // Also deal with kerning.
  718. // Also, we may have to load RGBA instead of RGB.
  719. // Wipe our texture sheets.
  720. mCurSheet = mCurX = mCurY = 0;
  721. mTextureSheets.clear();
  722. // Now, load the font strip.
  723. Resource<GBitmap> strip = GBitmap::load(fileName);
  724. if(!strip)
  725. {
  726. Con::errorf("GFont::importStrip - could not load file '%s'!", fileName);
  727. return;
  728. }
  729. // And get parsing and copying - load up all the characters as separate
  730. // GBitmaps, sort, then pack. Not terribly efficient but this is basically
  731. // on offline task anyway.
  732. // Ok, snag some glyphs.
  733. Vector<GlyphMap> glyphList;
  734. glyphList.reserve(mCharInfoList.size());
  735. U32 curWidth = 0;
  736. for(S32 i=0; i<mCharInfoList.size(); i++)
  737. {
  738. // Skip invalid stuff.
  739. if(mCharInfoList[i].bitmapIndex == -1 || mCharInfoList[i].height == 0 || mCharInfoList[i].width == 0)
  740. continue;
  741. // Allocate a new bitmap for this glyph, taking into account kerning and padding.
  742. glyphList.increment();
  743. GlyphMap& lastGlyphMap = glyphList.last();
  744. lastGlyphMap.bitmap = new GBitmap(mCharInfoList[i].width + kerning + 2 * padding, mCharInfoList[i].height + 2 * padding, false, strip->getFormat());
  745. lastGlyphMap.charId = i;
  746. // Copy the rect.
  747. RectI ri(curWidth, getBaseline() - mCharInfoList[i].yOrigin, lastGlyphMap.bitmap->getWidth(), lastGlyphMap.bitmap->getHeight());
  748. Point2I outRi(0,0);
  749. lastGlyphMap.bitmap->copyRect(strip, ri, outRi);
  750. // Update glyph attributes.
  751. mCharInfoList[i].width = lastGlyphMap.bitmap->getWidth();
  752. mCharInfoList[i].height = lastGlyphMap.bitmap->getHeight();
  753. mCharInfoList[i].xOffset -= kerning + padding;
  754. mCharInfoList[i].xIncrement += kerning;
  755. mCharInfoList[i].yOffset -= padding;
  756. // Advance.
  757. curWidth += ri.extent.x;
  758. }
  759. // Ok, we have a big list of glyphmaps now. So let's sort them, then pack them.
  760. dQsort(glyphList.address(), glyphList.size(), sizeof(GlyphMap), GlyphMapCompare);
  761. // They're sorted by height, so now we can do some sort of awesome packing.
  762. Point2I curSheetSize(256, 256);
  763. Vector<U32> sheetSizes;
  764. S32 curY = 0;
  765. S32 curX = 0;
  766. S32 curLnHeight = 0;
  767. S32 maxHeight = 0;
  768. for(U32 i = 0; i < glyphList.size(); i++)
  769. {
  770. PlatformFont::CharInfo *ci = &mCharInfoList[glyphList[i].charId];
  771. if(ci->height > maxHeight)
  772. maxHeight = ci->height;
  773. if(curX + ci->width > curSheetSize.x)
  774. {
  775. curY += curLnHeight;
  776. curX = 0;
  777. curLnHeight = 0;
  778. }
  779. if(curY + ci->height > curSheetSize.y)
  780. {
  781. sheetSizes.push_back(curSheetSize.y);
  782. curX = 0;
  783. curY = 0;
  784. curLnHeight = 0;
  785. }
  786. if(ci->height > curLnHeight)
  787. curLnHeight = ci->height;
  788. ci->bitmapIndex = sheetSizes.size();
  789. ci->xOffset = curX;
  790. ci->yOffset = curY;
  791. curX += ci->width;
  792. }
  793. // Terminate the packing loop calculations.
  794. curY += curLnHeight;
  795. if(curY < 64)
  796. curSheetSize.y = 64;
  797. else if(curY < 128)
  798. curSheetSize.y = 128;
  799. sheetSizes.push_back(curSheetSize.y);
  800. if(getHeight() + padding * 2 > maxHeight)
  801. maxHeight = getHeight() + padding * 2;
  802. // Allocate texture pages.
  803. for(S32 i=0; i<sheetSizes.size(); i++)
  804. {
  805. GBitmap *bitmap = new GBitmap(TextureSheetSize, TextureSheetSize, false, strip->getFormat());
  806. // Set everything to transparent.
  807. U8 *bits = bitmap->getWritableBits();
  808. dMemset(bits, 0, sizeof(U8) *TextureSheetSize*TextureSheetSize * strip->getBytesPerPixel());
  809. GFXTexHandle handle = GFXTexHandle( bitmap, &GFXFontTextureProfile, true, avar("%s() - Font Sheet for %s (line %d)", __FUNCTION__, fileName, __LINE__) );
  810. mTextureSheets.increment();
  811. mTextureSheets.last() = handle;
  812. }
  813. // Alright, we're ready to copy bits!
  814. for(S32 i=0; i<glyphList.size(); i++)
  815. {
  816. // Copy each glyph into the appropriate place.
  817. PlatformFont::CharInfo *ci = &mCharInfoList[glyphList[i].charId];
  818. U32 bi = ci->bitmapIndex;
  819. mTextureSheets[bi]->getBitmap()->copyRect(glyphList[i].bitmap, RectI(0,0, glyphList[i].bitmap->getWidth(),glyphList[i].bitmap->getHeight()), Point2I(ci->xOffset, ci->yOffset));
  820. }
  821. // Ok, all done! Just refresh some textures and we're set.
  822. for(S32 i=0; i<sheetSizes.size(); i++)
  823. mTextureSheets[i].refresh();
  824. }
  825. DefineEngineFunction( populateFontCacheString, void, ( const char *faceName, S32 fontSize, const char *string ),,
  826. "Populate the font cache for the specified font with characters from the specified string.\n"
  827. "@param faceName The name of the font face.\n"
  828. "@param fontSize The size of the font in pixels.\n"
  829. "@param string The string to populate.\n"
  830. "@ingroup Font\n" )
  831. {
  832. Resource<GFont> f = GFont::create(faceName, fontSize, Con::getVariable("$GUI::fontCacheDirectory"));
  833. if(f == NULL)
  834. {
  835. Con::errorf("populateFontCacheString - could not load font '%s %d'", faceName, fontSize);
  836. return;
  837. }
  838. if(!f->hasPlatformFont())
  839. {
  840. Con::errorf("populateFontCacheString - font '%s %d' has no platform font. Cannot generate more characters.", faceName, fontSize);
  841. return;
  842. }
  843. // This has the side effect of generating character info, including the bitmaps.
  844. f->getStrWidthPrecise( string );
  845. }
  846. DefineEngineFunction( populateFontCacheRange, void, ( const char *faceName, S32 fontSize, U32 rangeStart, U32 rangeEnd ),,
  847. "Populate the font cache for the specified font with Unicode code points in the specified range.\n"
  848. "@param faceName The name of the font face.\n"
  849. "@param fontSize The size of the font in pixels.\n"
  850. "@param rangeStart The start Unicode point.\n"
  851. "@param rangeEnd The end Unicode point.\n"
  852. "@note We only support BMP-0, so code points range from 0 to 65535.\n"
  853. "@ingroup Font\n" )
  854. {
  855. Resource<GFont> f = GFont::create(faceName, fontSize, Con::getVariable("$GUI::fontCacheDirectory"));
  856. if(f == NULL)
  857. {
  858. Con::errorf("populateFontCacheRange - could not load font '%s %d'", faceName, fontSize);
  859. return;
  860. }
  861. if(rangeStart > rangeEnd)
  862. {
  863. Con::errorf("populateFontCacheRange - range start is after end");
  864. return;
  865. }
  866. if(!f->hasPlatformFont())
  867. {
  868. Con::errorf("populateFontCacheRange - font '%s %d' has no platform font Cannot generate more characters.", faceName, fontSize);
  869. return;
  870. }
  871. // This has the side effect of generating character info, including the bitmaps.
  872. Con::printf(" o Populating font '%s'", faceName);
  873. for(U32 i=rangeStart; i<rangeEnd; i++)
  874. {
  875. if(f->isValidChar(i))
  876. f->getCharWidth(i);
  877. else
  878. Con::warnf("populateFontCacheRange - skipping invalid char 0x%x", i);
  879. }
  880. // All done!
  881. }
  882. DefineEngineFunction( dumpFontCacheStatus, void, (),,
  883. "Dumps to the console a full description of all cached fonts, along with "
  884. "info on the codepoints each contains.\n"
  885. "@ingroup Font\n" )
  886. {
  887. Resource<GFont> theFont = ResourceManager::get().startResourceList( Resource<GFont>::signature() );
  888. Con::printf("--------------------------------------------------------------------------");
  889. Con::printf(" Font Cache Usage Report");
  890. while( theFont != NULL )
  891. {
  892. theFont->dumpInfo();
  893. theFont = ResourceManager::get().nextResource();
  894. }
  895. }
  896. DefineEngineFunction( writeFontCache, void, (),,
  897. "Force all cached fonts to serialize themselves to the cache.\n"
  898. "@ingroup Font\n" )
  899. {
  900. Resource<GFont> theFont = ResourceManager::get().startResourceList( Resource<GFont>::signature() );
  901. Con::printf("--------------------------------------------------------------------------");
  902. Con::printf(" Writing font cache to disk");
  903. while( theFont != NULL )
  904. {
  905. const String fileName( theFont.getPath() );
  906. FileStream stream;
  907. stream.open(fileName, Torque::FS::File::Write);
  908. if(stream.getStatus() == Stream::Ok)
  909. {
  910. Con::printf(" o Writing '%s' to disk...", fileName.c_str());
  911. theFont->write(stream);
  912. }
  913. else
  914. {
  915. Con::errorf(" o Could not open '%s' for write", fileName.c_str());
  916. }
  917. theFont = ResourceManager::get().nextResource();
  918. }
  919. }
  920. DefineEngineFunction( populateAllFontCacheString, void, ( const char *string ),,
  921. "Populate the font cache for all fonts with characters from the specified string.\n"
  922. "@ingroup Font\n" )
  923. {
  924. Resource<GFont> theFont = ResourceManager::get().startResourceList( Resource<GFont>::signature() );
  925. Con::printf("Populating font cache with string '%s'", string);
  926. while( theFont != NULL )
  927. {
  928. if(theFont->hasPlatformFont())
  929. {
  930. // This has the side effect of generating character info, including the bitmaps.
  931. theFont->getStrWidthPrecise( string );
  932. }
  933. else
  934. {
  935. const String fileName( theFont.getPath() );
  936. Con::errorf("populateAllFontCacheString - font '%s' has no platform font. Cannot generate more characters.", fileName.c_str());
  937. }
  938. theFont = ResourceManager::get().nextResource();
  939. }
  940. }
  941. DefineEngineFunction( populateAllFontCacheRange, void, ( U32 rangeStart, U32 rangeEnd ),,
  942. "Populate the font cache for all fonts with Unicode code points in the specified range.\n"
  943. "@param rangeStart The start Unicode point.\n"
  944. "@param rangeEnd The end Unicode point.\n"
  945. "@note We only support BMP-0, so code points range from 0 to 65535.\n"
  946. "@ingroup Font\n" )
  947. {
  948. if(rangeStart > rangeEnd)
  949. {
  950. Con::errorf("populateAllFontCacheRange - range start is after end!");
  951. return;
  952. }
  953. Resource<GFont> theFont = ResourceManager::get().startResourceList( Resource<GFont>::signature() );
  954. Con::printf("Populating font cache with range 0x%x to 0x%x", rangeStart, rangeEnd);
  955. while( theFont != NULL )
  956. {
  957. const String fileName( theFont.getPath() );
  958. if(theFont->hasPlatformFont())
  959. {
  960. // This has the side effect of generating character info, including the bitmaps.
  961. Con::printf(" o Populating font '%s'", fileName.c_str());
  962. for(U32 i=rangeStart; i<rangeEnd; i++)
  963. {
  964. if(theFont->isValidChar(i))
  965. theFont->getCharWidth(i);
  966. else
  967. Con::warnf("populateAllFontCacheRange - skipping invalid char 0x%x", i);
  968. }
  969. }
  970. else
  971. {
  972. Con::errorf("populateAllFontCacheRange - font '%s' has no platform font. Cannot generate more characters.", fileName.c_str());
  973. }
  974. theFont = ResourceManager::get().nextResource();
  975. }
  976. }
  977. DefineEngineFunction( exportCachedFont, void,
  978. ( const char *faceName, S32 fontSize, const char *fileName, S32 padding, S32 kerning ),,
  979. "Export specified font to the specified filename as a PNG. The "
  980. "image can then be processed in Photoshop or another tool and "
  981. "reimported using importCachedFont. Characters in the font are "
  982. "exported as one long strip.\n"
  983. "@param faceName The name of the font face.\n"
  984. "@param fontSize The size of the font in pixels.\n"
  985. "@param fileName The file name and path for the output PNG.\n"
  986. "@param padding The padding between characters.\n"
  987. "@param kerning The kerning between characters.\n"
  988. "@ingroup Font\n" )
  989. {
  990. // Tell the font to export itself.
  991. Resource<GFont> f = GFont::create(faceName, fontSize, Con::getVariable("$GUI::fontCacheDirectory"));
  992. if(f == NULL)
  993. {
  994. Con::errorf("exportCachedFont - could not load font '%s %d'", faceName, fontSize);
  995. return;
  996. }
  997. f->exportStrip(fileName, padding, kerning);
  998. }
  999. DefineEngineFunction( importCachedFont, void,
  1000. ( const char *faceName, S32 fontSize, const char *fileName, S32 padding, S32 kerning ),,
  1001. "Import an image strip from exportCachedFont. Call with the "
  1002. "same parameters you called exportCachedFont.\n"
  1003. "@param faceName The name of the font face.\n"
  1004. "@param fontSize The size of the font in pixels.\n"
  1005. "@param fileName The file name and path for the input PNG.\n"
  1006. "@param padding The padding between characters.\n"
  1007. "@param kerning The kerning between characters.\n"
  1008. "@ingroup Font\n" )
  1009. {
  1010. // Tell the font to import itself.
  1011. Resource<GFont> f = GFont::create(faceName, fontSize, Con::getVariable("$GUI::fontCacheDirectory"));
  1012. if(f == NULL)
  1013. {
  1014. Con::errorf("importCachedFont - could not load font '%s %d'", faceName, fontSize);
  1015. return;
  1016. }
  1017. f->importStrip(fileName, padding, kerning);
  1018. }
  1019. DefineEngineFunction( duplicateCachedFont, void,
  1020. ( const char *oldFontName, S32 oldFontSize, const char *newFontName ),,
  1021. "Copy the specified old font to a new name. The new copy will not have a "
  1022. "platform font backing it, and so will never have characters added to it. "
  1023. "But this is useful for making copies of fonts to add postprocessing effects "
  1024. "to via exportCachedFont.\n"
  1025. "@param oldFontName The name of the font face to copy.\n"
  1026. "@param oldFontSize The size of the font to copy.\n"
  1027. "@param newFontName The name of the new font face.\n"
  1028. "@ingroup Font\n" )
  1029. {
  1030. String newFontFile = GFont::getFontCacheFilename(newFontName, oldFontSize);
  1031. // Load the original font.
  1032. Resource<GFont> font = GFont::create(oldFontName, oldFontSize, Con::getVariable("$GUI::fontCacheDirectory"));
  1033. // Deal with inexplicably missing or failed to load fonts.
  1034. if (font == NULL)
  1035. {
  1036. Con::errorf(" o Couldn't find font : %s", oldFontName);
  1037. return;
  1038. }
  1039. // Ok, dump info!
  1040. FileStream stream;
  1041. stream.open( newFontFile, Torque::FS::File::Write );
  1042. if(stream.getStatus() == Stream::Ok)
  1043. {
  1044. Con::printf( " o Writing duplicate font '%s' to disk...", newFontFile.c_str() );
  1045. font->write(stream);
  1046. stream.close();
  1047. }
  1048. else
  1049. {
  1050. Con::errorf( " o Could not open '%s' for write", newFontFile.c_str() );
  1051. }
  1052. }