OLD | NEW |
1 /**************************************************************************** | 1 /**************************************************************************** |
2 Copyright (c) 2013 The Chromium Authors | 2 Copyright (c) 2013 The Chromium Authors |
3 | 3 |
4 http://www.cocos2d-x.org | 4 http://www.cocos2d-x.org |
5 | 5 |
6 Permission is hereby granted, free of charge, to any person obtaining a copy | 6 Permission is hereby granted, free of charge, to any person obtaining a copy |
7 of this software and associated documentation files (the "Software"), to deal | 7 of this software and associated documentation files (the "Software"), to deal |
8 in the Software without restriction, including without limitation the rights | 8 in the Software without restriction, including without limitation the rights |
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 copies of the Software, and to permit persons to whom the Software is | 10 copies of the Software, and to permit persons to whom the Software is |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 struct TextLine { | 50 struct TextLine { |
51 int iLineWidth; | 51 int iLineWidth; |
52 wchar_t* text; | 52 wchar_t* text; |
53 }; | 53 }; |
54 | 54 |
55 NS_CC_BEGIN | 55 NS_CC_BEGIN |
56 | 56 |
57 class BitmapDC | 57 class BitmapDC |
58 { | 58 { |
59 public: | 59 public: |
60 BitmapDC() : m_pData(NULL), m_cachedSize(0), m_cachedFont(NULL) | 60 BitmapDC() : m_pData(NULL), m_cachedFont(NULL) |
61 { | 61 { |
62 libError = FT_Init_FreeType(&m_library); | 62 libError = FT_Init_FreeType(&m_library); |
63 iInterval = szFont_kenning; | 63 iInterval = szFont_kenning; |
64 reset(); | 64 reset(); |
65 } | 65 } |
66 | 66 |
67 ~BitmapDC() | 67 ~BitmapDC() |
68 { | 68 { |
69 if (m_cachedFont) | 69 if (m_cachedFont) |
70 FT_Done_Face(m_cachedFont); | 70 FT_Done_Face(m_cachedFont); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 336 |
337 std::string fontfile = pFontName; | 337 std::string fontfile = pFontName; |
338 std::string fontfileOrig = std::string(fontfile); | 338 std::string fontfileOrig = std::string(fontfile); |
339 | 339 |
340 std::string ext = fileNameExtension(fontfile); | 340 std::string ext = fileNameExtension(fontfile); |
341 if (ext.empty() || (ext != "ttf" && ext != "TTF")) | 341 if (ext.empty() || (ext != "ttf" && ext != "TTF")) |
342 { | 342 { |
343 fontfile += ".ttf" ; | 343 fontfile += ".ttf" ; |
344 } | 344 } |
345 | 345 |
346 iError = openFont(fontfile, fontSize, fontfileOrig); | 346 iError = openFont(fontfile, fontfileOrig); |
347 // try with fonts prefixed | 347 // try with fonts prefixed |
348 if (iError && !startsWith(fontfile,"fonts/") ) | 348 if (iError && !startsWith(fontfile,"fonts/") ) |
349 { | 349 { |
350 fontfile = std::string("fonts/") + fontfile; | 350 fontfile = std::string("fonts/") + fontfile; |
351 iError = openFont(fontfile, fontSize, fontfileOrig); | 351 iError = openFont(fontfile, fontfileOrig); |
352 } | 352 } |
353 | 353 |
354 if (iError) | 354 if (iError) |
355 { | 355 { |
356 // try lowercase version | 356 // try lowercase version |
357 std::transform(fontfile.begin(), fontfile.end(), fontfile.begin(), :
:tolower); | 357 std::transform(fontfile.begin(), fontfile.end(), fontfile.begin(), :
:tolower); |
358 iError = openFont(fontfile, fontSize, fontfileOrig); | 358 iError = openFont(fontfile, fontfileOrig); |
359 if (iError) | 359 if (iError) |
360 { | 360 { |
361 // try default font | 361 // try default font |
362 CCLOG("font missing (%s) falling back to default font", fontfile
Orig.c_str()); | 362 CCLOG("font missing (%s) falling back to default font", fontfile
Orig.c_str()); |
363 iError = openFont("fonts/Marker Felt.ttf", fontSize, fontfileOri
g); | 363 iError = openFont("fonts/Marker Felt.ttf", fontfileOrig); |
364 if (iError) | 364 if (iError) |
365 CCLOG("default font missing (fonts/Marker Felt.ttf)"); | 365 CCLOG("default font missing (fonts/Marker Felt.ttf)"); |
366 } | 366 } |
367 } | 367 } |
368 if (iError) | 368 if (iError) |
369 return false; | 369 return false; |
370 | 370 |
371 FT_Face face = m_cachedFont; | 371 FT_Face face = m_cachedFont; |
372 | 372 |
373 //select utf8 charmap | 373 //select utf8 charmap |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 int libError; | 416 int libError; |
417 vector<TextLine> vLines; | 417 vector<TextLine> vLines; |
418 int iInterval; | 418 int iInterval; |
419 int iMaxLineWidth; | 419 int iMaxLineWidth; |
420 int iMaxLineHeight; | 420 int iMaxLineHeight; |
421 private: | 421 private: |
422 bool startsWith(const std::string& str, const std::string& what); | 422 bool startsWith(const std::string& str, const std::string& what); |
423 /** | 423 /** |
424 * Attempt to open font file, and cache it if successful. | 424 * Attempt to open font file, and cache it if successful. |
425 */ | 425 */ |
426 int openFont(const std::string& fontName, uint fontSize, const std::string&
fontNameOrig); | 426 int openFont(const std::string& fontName, const std::string& fontNameOrig); |
427 std::string fileNameExtension(const std::string& pathName); | 427 std::string fileNameExtension(const std::string& pathName); |
428 | 428 |
429 uint m_cachedSize; | |
430 FT_Face m_cachedFont; | 429 FT_Face m_cachedFont; |
431 std::string m_cachedFontname; | 430 std::string m_cachedFontname; |
432 std::string m_cachedFontnameOrig; | 431 std::string m_cachedFontnameOrig; |
433 }; | 432 }; |
434 | 433 |
435 static BitmapDC& sharedBitmapDC() | 434 static BitmapDC& sharedBitmapDC() |
436 { | 435 { |
437 static BitmapDC s_BmpDC; | 436 static BitmapDC s_BmpDC; |
438 return s_BmpDC; | 437 return s_BmpDC; |
439 } | 438 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 bool BitmapDC::startsWith(const std::string& str, const std::string& what) | 493 bool BitmapDC::startsWith(const std::string& str, const std::string& what) |
495 { | 494 { |
496 bool result = false ; | 495 bool result = false ; |
497 if (what.size() <= str.size()) | 496 if (what.size() <= str.size()) |
498 { | 497 { |
499 result = (str.substr(0, what.size()) == what); | 498 result = (str.substr(0, what.size()) == what); |
500 } | 499 } |
501 return result ; | 500 return result ; |
502 } | 501 } |
503 | 502 |
504 int BitmapDC::openFont(const std::string& fontName, uint fontSize, const std::st
ring& fontNameOrig) | 503 int BitmapDC::openFont(const std::string& fontName, const std::string& fontNameO
rig) |
505 { | 504 { |
506 // try to satisfy request based on currently cached font. | 505 // try to satisfy request based on currently cached font. |
507 if (m_cachedSize == fontSize) | 506 if (fontNameOrig == m_cachedFontnameOrig) |
508 { | 507 return 0; |
509 if (fontNameOrig == m_cachedFontnameOrig) | |
510 return 0; | |
511 | 508 |
512 if (fontName == m_cachedFontname) | 509 if (fontName == m_cachedFontname) |
513 return 0; | 510 return 0; |
514 } | |
515 | 511 |
516 FT_Face face; | 512 FT_Face face; |
517 int iError = FT_New_Face(m_library, fontName.c_str(), 0, &face); | 513 int iError = FT_New_Face(m_library, fontName.c_str(), 0, &face); |
518 if (iError) | 514 if (iError) |
519 return iError; | 515 return iError; |
520 | 516 |
521 // free existing cached font | 517 // free existing cached font |
522 if (m_cachedFont) | 518 if (m_cachedFont) |
523 FT_Done_Face(m_cachedFont); | 519 FT_Done_Face(m_cachedFont); |
524 | 520 |
525 // cache newly loaded font | 521 // cache newly loaded font |
526 m_cachedFontnameOrig = fontNameOrig; | 522 m_cachedFontnameOrig = fontNameOrig; |
527 m_cachedFontname = fontName; | 523 m_cachedFontname = fontName; |
528 m_cachedFont = face; | 524 m_cachedFont = face; |
529 m_cachedSize = fontSize; | |
530 return 0; | 525 return 0; |
531 } | 526 } |
532 | 527 |
533 NS_CC_END | 528 NS_CC_END |
OLD | NEW |