PolyFontManager.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyFontManager.h"
  20. #include "PolyFont.h"
  21. #include FT_LCD_FILTER_H
  22. using namespace Polycode;
  23. FontManager::FontManager() {
  24. FT_Init_FreeType(&FTLibrary);
  25. FT_Library_SetLcdFilter(FTLibrary, FT_LCD_FILTER_LIGHT);
  26. }
  27. FontManager::~FontManager() {
  28. for(int i=0; i < fonts.size(); i++) {
  29. FontEntry entry = fonts[i];
  30. delete entry.font;
  31. }
  32. fonts.clear();
  33. FT_Done_FreeType(FTLibrary);
  34. }
  35. unsigned int FontManager::getNumFonts() const {
  36. return fonts.size();
  37. }
  38. FontEntry *FontManager::getFontEntryByIndex(const unsigned int index) {
  39. if(index < fonts.size()) {
  40. return &fonts[index];
  41. } else {
  42. return NULL;
  43. }
  44. }
  45. void FontManager::removeFontEntry(FontEntry *entry, bool deleteFont) {
  46. for(int i=0; i < fonts.size(); i++) {
  47. FontEntry *listEntry = &fonts[i];
  48. if(listEntry == entry) {
  49. if(deleteFont) {
  50. delete listEntry->font;
  51. }
  52. fonts.erase(fonts.begin()+i);
  53. return;
  54. }
  55. }
  56. }
  57. void FontManager::registerFont(const String& fontName, const String& fontPath) {
  58. Font *font = new Font(fontPath, FTLibrary);
  59. if(font->loaded) {
  60. FontEntry newEntry;
  61. newEntry.font = font;
  62. newEntry.fontName = fontName;
  63. font->setFontName(fontName);
  64. fonts.push_back(newEntry);
  65. } else {
  66. delete font;
  67. }
  68. }
  69. FontEntry *FontManager::getFontEntryByFontPath(const String &fontPath) {
  70. for(int i=0; i < fonts.size(); i++) {
  71. FontEntry *entry = &fonts[i];
  72. if(entry->font->getFontPath() == fontPath) {
  73. return entry;
  74. }
  75. }
  76. return NULL;
  77. }
  78. Font *FontManager::getFontByName(const String& fontName) {
  79. for(int i=0; i < fonts.size(); i++) {
  80. FontEntry entry = fonts[i];
  81. if(entry.fontName == fontName)
  82. return entry.font;
  83. }
  84. if(fonts.size() > 0) {
  85. FontEntry entry = fonts[0];
  86. return entry.font;
  87. }
  88. return NULL;
  89. }