gFont_ScriptBinding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. /*! @defgroup FontFunctions Font
  23. @ingroup TorqueScriptFunctions
  24. @{
  25. */
  26. /*!
  27. Populate the font cache for the specified font with characters from the specified string.
  28. @param faceName The font's name
  29. @param size The size of the font.
  30. @param string The string to use to fill font cache
  31. @return No return value.
  32. */
  33. ConsoleFunctionWithDocs(populateFontCacheString, ConsoleVoid, 4, 4, (faceName, size, string))
  34. {
  35. Resource<GFont> f = GFont::create(argv[1], dAtoi(argv[2]), Con::getVariable("$GUI::fontCacheDirectory"));
  36. if(f.isNull())
  37. {
  38. Con::errorf("populateFontCacheString - could not load font '%s %d'!", argv[1], dAtoi(argv[2]));
  39. return;
  40. }
  41. if(!f->hasPlatformFont())
  42. {
  43. Con::errorf("populateFontCacheString - font '%s %d' has no platform font! Cannot generate more characters.", argv[1], dAtoi(argv[2]));
  44. return;
  45. }
  46. // This has the side effect of generating character info, including the bitmaps.
  47. f->getStrWidthPrecise(argv[3]);
  48. }
  49. /*!
  50. Populate the font cache for the specified font with Unicode code points in the specified range.
  51. Note we only support BMP-0, so code points range from 0 to 65535.
  52. @param faceName The name of the font
  53. @param size The size of the font.
  54. @param rangeStart The initial Unicode point
  55. @param rangeEnd The final Unicode point in range
  56. @return No return value
  57. */
  58. ConsoleFunctionWithDocs(populateFontCacheRange, ConsoleVoid, 5, 5, (faceName, size, rangeStart, rangeEnd))
  59. {
  60. Resource<GFont> f = GFont::create(argv[1], dAtoi(argv[2]), Con::getVariable("$GUI::fontCacheDirectory"));
  61. if(f.isNull())
  62. {
  63. Con::errorf("populateFontCacheRange - could not load font '%s %d'!", argv[1], dAtoi(argv[2]));
  64. return;
  65. }
  66. U32 rangeStart = dAtoi(argv[3]);
  67. U32 rangeEnd = dAtoi(argv[4]);
  68. if(rangeStart > rangeEnd)
  69. {
  70. Con::errorf("populateFontCacheRange - range start is after end!");
  71. return;
  72. }
  73. if(!f->hasPlatformFont())
  74. {
  75. Con::errorf("populateFontCacheRange - font '%s %d' has no platform font! Cannot generate more characters.", argv[1], dAtoi(argv[2]));
  76. return;
  77. }
  78. // This has the side effect of generating character info, including the bitmaps.
  79. for(U32 i=rangeStart; i<rangeEnd; i++)
  80. {
  81. if(f->isValidChar(i))
  82. f->getCharWidth(i);
  83. else
  84. Con::warnf("populateFontCacheRange - skipping invalid char 0x%x", i);
  85. }
  86. // All done!
  87. }
  88. /*! Dump a full description
  89. of all cached fonts, along with info on the codepoints each contains.
  90. @return No return value
  91. */
  92. ConsoleFunctionWithDocs(dumpFontCacheStatus, ConsoleVoid, 1, 1, ())
  93. {
  94. FindMatch match("*.uft", 4096);
  95. ResourceManager->findMatches(&match);
  96. Con::printf("--------------------------------------------------------------------------");
  97. Con::printf(" Font Cache Usage Report (%d fonts found)", match.numMatches());
  98. for (U32 i = 0; i < (U32)match.numMatches(); i++)
  99. {
  100. char *curMatch = match.matchList[i];
  101. Resource<GFont> font = ResourceManager->load(curMatch);
  102. // Deal with inexplicably missing or failed to load fonts.
  103. if (font.isNull())
  104. {
  105. Con::errorf(" o Couldn't load font : %s", curMatch);
  106. continue;
  107. }
  108. // Ok, dump info!
  109. font->dumpInfo();
  110. }
  111. }
  112. /*! force all cached fonts to
  113. serialize themselves to the cache.
  114. @return No return value
  115. */
  116. ConsoleFunctionWithDocs(writeFontCache, ConsoleVoid, 1, 1, ())
  117. {
  118. FindMatch match("*.uft", 4096);
  119. ResourceManager->findMatches(&match);
  120. Con::printf("--------------------------------------------------------------------------");
  121. Con::printf(" Writing font cache to disk (%d fonts found)", match.numMatches());
  122. for (U32 i = 0; i < (U32)match.numMatches(); i++)
  123. {
  124. char *curMatch = match.matchList[i];
  125. Resource<GFont> font = ResourceManager->load(curMatch);
  126. // Deal with inexplicably missing or failed to load fonts.
  127. if (font.isNull())
  128. {
  129. Con::errorf(" o Couldn't find font : %s", curMatch);
  130. continue;
  131. }
  132. // Ok, dump info!
  133. FileStream stream;
  134. if(ResourceManager->openFileForWrite(stream, curMatch))
  135. {
  136. Con::printf(" o Writing '%s' to disk...", curMatch);
  137. font->write(stream);
  138. stream.close();
  139. }
  140. else
  141. {
  142. Con::errorf(" o Could not open '%s' for write!", curMatch);
  143. }
  144. }
  145. }
  146. /*! Force all cached fonts with a given font name in their paths to serialize themselves to the cache.
  147. @param fontName The name of the font to search for in the path. Will save if found.
  148. @return No return value
  149. */
  150. ConsoleFunctionWithDocs(writeSingleFontCache, ConsoleVoid, 2, 2, (fontName))
  151. {
  152. FindMatch match("*.uft", 4096);
  153. ResourceManager->findMatches(&match);
  154. Con::printf("--------------------------------------------------------------------------");
  155. Con::printf(" Writing font cache for font face %s to disk", argv[1]);
  156. for (U32 i = 0; i < (U32)match.numMatches(); i++)
  157. {
  158. char *curMatch = match.matchList[i];
  159. if(dStrstr(curMatch, argv[1]) != NULL )
  160. {
  161. Resource<GFont> font = ResourceManager->load(curMatch);
  162. // Deal with inexplicably missing or failed to load fonts.
  163. if (font.isNull())
  164. {
  165. Con::errorf(" o Couldn't find font : %s", curMatch);
  166. continue;
  167. }
  168. // Ok, dump info!
  169. FileStream stream;
  170. if (ResourceManager->openFileForWrite(stream, curMatch))
  171. {
  172. Con::printf(" o Writing '%s' to disk...", curMatch);
  173. font->write(stream);
  174. stream.close();
  175. }
  176. else
  177. {
  178. Con::errorf(" o Could not open '%s' for write!", curMatch);
  179. }
  180. }
  181. }
  182. }
  183. /*!
  184. Populate the font cache for all fonts with characters from the specified string.
  185. @param inString The string to use to set the font caches
  186. @return No return value.
  187. */
  188. ConsoleFunctionWithDocs(populateAllFontCacheString, ConsoleVoid, 2, 2, (string inString))
  189. {
  190. FindMatch match("*.uft", 4096);
  191. ResourceManager->findMatches(&match);
  192. Con::printf("Populating font cache with string '%s' (%d fonts found)", argv[1], match.numMatches());
  193. for (U32 i = 0; i < (U32)match.numMatches(); i++)
  194. {
  195. char *curMatch = match.matchList[i];
  196. Resource<GFont> font = ResourceManager->load(curMatch);
  197. // Deal with inexplicably missing or failed to load fonts.
  198. if (font.isNull())
  199. {
  200. Con::errorf(" o Couldn't load font : %s", curMatch);
  201. continue;
  202. }
  203. if(!font->hasPlatformFont())
  204. {
  205. Con::errorf("populateAllFontCacheString - font '%s' has no platform font! Cannot generate more characters.", curMatch);
  206. continue;
  207. }
  208. // This has the side effect of generating character info, including the bitmaps.
  209. font->getStrWidthPrecise(argv[1]);
  210. }
  211. }
  212. /*!
  213. Populate the font cache for all fonts with Unicode code points in the specified range.
  214. Note we only support BMP-0, so code points range from 0 to 65535.
  215. @param rangeStart, rangeEnd The range of the unicode points to populate caches with
  216. @return No return value
  217. */
  218. ConsoleFunctionWithDocs(populateAllFontCacheRange, ConsoleVoid, 3, 3, (rangeStart, rangeEnd))
  219. {
  220. U32 rangeStart = dAtoi(argv[1]);
  221. U32 rangeEnd = dAtoi(argv[2]);
  222. if(rangeStart > rangeEnd)
  223. {
  224. Con::errorf("populateAllFontCacheRange - range start is after end!");
  225. return;
  226. }
  227. FindMatch match("*.uft", 4096);
  228. ResourceManager->findMatches(&match);
  229. Con::printf("Populating font cache with range 0x%x to 0x%x (%d fonts found)", rangeStart, rangeEnd, match.numMatches());
  230. for (U32 i = 0; i < (U32)match.numMatches(); i++)
  231. {
  232. char *curMatch = match.matchList[i];
  233. Resource<GFont> font = ResourceManager->load(curMatch);
  234. // Deal with inexplicably missing or failed to load fonts.
  235. if (font.isNull())
  236. {
  237. Con::errorf(" o Couldn't load font : %s", curMatch);
  238. continue;
  239. }
  240. if(!font->hasPlatformFont())
  241. {
  242. Con::errorf("populateAllFontCacheRange - font '%s' has no platform font! Cannot generate more characters.", curMatch);
  243. continue;
  244. }
  245. // This has the side effect of generating character info, including the bitmaps.
  246. Con::printf(" o Populating font '%s'", curMatch);
  247. for(U32 i=rangeStart; i<rangeEnd; i++)
  248. {
  249. if(font->isValidChar(i))
  250. font->getCharWidth(i);
  251. else
  252. Con::warnf("populateAllFontCacheRange - skipping invalid char 0x%x", i);
  253. }
  254. }
  255. // All done!
  256. }
  257. /*!
  258. Export specified font to the specified filename as a PNG. The
  259. image can then be processed in Photoshop or another tool and
  260. reimported using importCachedFont. Characters in the font are
  261. exported as one long strip.
  262. @param fontName The name of the font to export.
  263. @param size The size of the font
  264. @param fileName The export file name.
  265. @param padding Desired padding settings.
  266. @param kerning Kerning settings (space between elements)
  267. @return No return value.
  268. */
  269. ConsoleFunctionWithDocs(exportCachedFont, ConsoleVoid, 6, 6, (fontName, size, fileName, padding, kerning))
  270. {
  271. // Read in some params.
  272. const char *fileName = argv[3];
  273. S32 padding = dAtoi(argv[4]);
  274. S32 kerning = dAtoi(argv[5]);
  275. // Tell the font to export itself.
  276. Resource<GFont> f = GFont::create(argv[1], dAtoi(argv[2]), Con::getVariable("$GUI::fontCacheDirectory"));
  277. if(f.isNull())
  278. {
  279. Con::errorf("populateFontCacheString - could not load font '%s %d'!", argv[1], dAtoi(argv[2]));
  280. return;
  281. }
  282. f->exportStrip(fileName, padding, kerning);
  283. }
  284. /*!
  285. Import an image strip from exportCachedFont. Call with the
  286. same parameters you called exportCachedFont.
  287. @param fontName The name of the font to import.
  288. @param size The size of the font
  289. @param fileName The imported file name.
  290. @param padding Desired padding settings.
  291. @param kerning Kerning settings (space between elements)
  292. @return No return value.
  293. */
  294. ConsoleFunctionWithDocs(importCachedFont, ConsoleVoid, 6, 6, (fontName, size, fileName, padding, kerning))
  295. {
  296. // Read in some params.
  297. const char *fileName = argv[3];
  298. S32 padding = dAtoi(argv[4]);
  299. S32 kerning = dAtoi(argv[5]);
  300. // Tell the font to import itself.
  301. Resource<GFont> f = GFont::create(argv[1], dAtoi(argv[2]), Con::getVariable("$GUI::fontCacheDirectory"));
  302. if(f.isNull())
  303. {
  304. Con::errorf("populateFontCacheString - could not load font '%s %d'!", argv[1], dAtoi(argv[2]));
  305. return;
  306. }
  307. f->importStrip(fileName, padding, kerning);
  308. }
  309. /*!
  310. Copy the specified old font to a new name. The new copy will not have a
  311. platform font backing it, and so will never have characters added to it.
  312. But this is useful for making copies of fonts to add postprocessing effects
  313. to via exportCachedFont.
  314. @param oldFontName The original font.
  315. @param oldFontSize The original font's size property.
  316. @param newFontName The name to set the copy to.
  317. @return No return value.
  318. */
  319. ConsoleFunctionWithDocs(duplicateCachedFont, ConsoleVoid, 4, 4, (oldFontName, oldFontSize, newFontName))
  320. {
  321. char newFontFile[256];
  322. GFont::getFontCacheFilename(argv[3], dAtoi(argv[2]), 256, newFontFile);
  323. // Load the original font.
  324. Resource<GFont> font = GFont::create(argv[1], dAtoi(argv[2]), Con::getVariable("$GUI::fontCacheDirectory"));
  325. // Deal with inexplicably missing or failed to load fonts.
  326. if (font.isNull())
  327. {
  328. Con::errorf(" o Couldn't find font : %s", newFontFile);
  329. return;
  330. }
  331. // Ok, dump info!
  332. FileStream stream;
  333. if(ResourceManager->openFileForWrite(stream, newFontFile))
  334. {
  335. Con::printf(" o Writing duplicate font '%s' to disk...", newFontFile);
  336. font->write(stream);
  337. stream.close();
  338. }
  339. else
  340. {
  341. Con::errorf(" o Could not open '%s' for write!", newFontFile);
  342. }
  343. }
  344. /*! @} */ // group FontFunctions