From 3f8910d80c108171c14f047df8d07d85d5840454 Mon Sep 17 00:00:00 2001 From: Xpol Wan Date: Tue, 26 Apr 2016 13:57:53 +0800 Subject: [PATCH] Remove last internal use of deprecated `FileUtils::getFileData()`. --- cocos/platform/winrt/CCFreeTypeFont.cpp | 99 ++++++++++++++----------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/cocos/platform/winrt/CCFreeTypeFont.cpp b/cocos/platform/winrt/CCFreeTypeFont.cpp index a443dd85fb..fdc13fe32d 100644 --- a/cocos/platform/winrt/CCFreeTypeFont.cpp +++ b/cocos/platform/winrt/CCFreeTypeFont.cpp @@ -8,10 +8,10 @@ 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 @@ -42,14 +42,14 @@ static FT_Library s_FreeTypeLibrary = nullptr; const std::string CCFreeTypeFont::DEFAULT_FONT = "arial"; -CCFreeTypeFont::CCFreeTypeFont() +CCFreeTypeFont::CCFreeTypeFont() :m_space(" ") , m_face(nullptr) { } -CCFreeTypeFont::~CCFreeTypeFont() +CCFreeTypeFont::~CCFreeTypeFont() { reset(); } @@ -92,14 +92,14 @@ unsigned char* CCFreeTypeFont::initWithString(const char * text, const FontDefin #if 0 // check the cache for the font file buffer auto ittFontNames = s_fontsNames.find(textDefinition._fontName); - if(ittFontNames != s_fontsNames.end()) + if(ittFontNames != s_fontsNames.end()) { pBuffer = ittFontNames->second.pBuffer; size = ittFontNames->second.size; - } + } #endif // 0 - + if(!pBuffer) { // attempt to load font from Resources fonts folder @@ -130,7 +130,7 @@ unsigned char* CCFreeTypeFont::initWithString(const char * text, const FontDefin FontBufferInfo info; info.pBuffer = pBuffer; info.size = size; - s_fontsNames[textDefinition._fontName]=info; + s_fontsNames[textDefinition._fontName]=info; #endif // 0 } @@ -183,7 +183,7 @@ unsigned char* CCFreeTypeFont::getBitmap(Device::TextAlign eAlignMask, int &widt m_width = m_inWidth ? m_inWidth : m_textWidth; m_height = m_inHeight ? m_inHeight : m_textHeight; - + unsigned int size = m_width * m_height * 4; unsigned char* pBuffer = new unsigned char[size]; dataLength = size; @@ -210,7 +210,7 @@ FT_Vector CCFreeTypeFont::getPenForAlignment(FTLineInfo* pInfo, Device::TextAlig { FT_Error error = 0; FT_Vector pen; - + int top; int stringWidth = pInfo->bbox.xMax - pInfo->bbox.xMin; int maxLineNumber = totalLines - 1; @@ -221,19 +221,19 @@ FT_Vector CCFreeTypeFont::getPenForAlignment(FTLineInfo* pInfo, Device::TextAlig { case Device::TextAlign::TOP: // Horizontal center and vertical top. pen.x = ((m_width - stringWidth) / 2) - pInfo->bbox.xMin; - pen.y = pInfo->bbox.yMax + (lineNumber * m_lineHeight); + pen.y = pInfo->bbox.yMax + (lineNumber * m_lineHeight); break; - + case Device::TextAlign::TOP_LEFT: // Horizontal left and vertical top. pen.x -=pInfo->bbox.xMin; - pen.y = pInfo->bbox.yMax + (lineNumber * m_lineHeight); + pen.y = pInfo->bbox.yMax + (lineNumber * m_lineHeight); break; case Device::TextAlign::TOP_RIGHT: // Horizontal right and vertical top. pen.x = m_width - stringWidth - pInfo->bbox.xMin; - pen.y = pInfo->bbox.yMax + (lineNumber * m_lineHeight); + pen.y = pInfo->bbox.yMax + (lineNumber * m_lineHeight); break; - + case Device::TextAlign::BOTTOM_RIGHT: // Horizontal right and vertical bottom. pen.x = m_width - stringWidth - pInfo->bbox.xMin; pen.y = m_height + pInfo->bbox.yMin - ((maxLineNumber - lineNumber) * m_lineHeight); @@ -253,20 +253,20 @@ FT_Vector CCFreeTypeFont::getPenForAlignment(FTLineInfo* pInfo, Device::TextAlig case Device::TextAlign::CENTER: // Horizontal center and vertical center pen.x = ((m_width - stringWidth) / 2) - pInfo->bbox.xMin; top = (m_height - m_textHeight) / 2; - pen.y = top + (lineNumber * m_lineHeight) + pInfo->bbox.yMax; + pen.y = top + (lineNumber * m_lineHeight) + pInfo->bbox.yMax; break; case Device::TextAlign::RIGHT: // Horizontal right and vertical center. pen.x = m_width - stringWidth - pInfo->bbox.xMin; top = (m_height - m_textHeight) / 2; - pen.y = top + (lineNumber * m_lineHeight) + pInfo->bbox.yMax; + pen.y = top + (lineNumber * m_lineHeight) + pInfo->bbox.yMax; break; case Device::TextAlign::LEFT: // Horizontal left and vertical center. default: pen.x -=pInfo->bbox.xMin; top = (m_height - m_textHeight) / 2; - pen.y = top + (lineNumber * m_lineHeight) + pInfo->bbox.yMax; + pen.y = top + (lineNumber * m_lineHeight) + pInfo->bbox.yMax; break; } @@ -316,10 +316,10 @@ void CCFreeTypeFont::draw_bitmap(unsigned char* pBuffer, FT_Bitmap* bitmap, FT_ pBuffer[index++] = value * fontAlpha; } } - } + } } -void CCFreeTypeFont::endLine() +void CCFreeTypeFont::endLine() { if(m_currentLine) { @@ -329,7 +329,7 @@ void CCFreeTypeFont::endLine() } } -void CCFreeTypeFont::newLine() +void CCFreeTypeFont::newLine() { m_currentLine = new FTLineInfo(); m_currentLine->width = 0; @@ -338,24 +338,24 @@ void CCFreeTypeFont::newLine() } -FT_Error CCFreeTypeFont::addWord(const std::string& word) +FT_Error CCFreeTypeFont::addWord(const std::string& word) { std::vector glyphs; // glyphs for the word FT_BBox bbox; // bounding box containing all of the glyphs in the word int maxWidth = m_inWidth ? m_inWidth : m_windowWidth; std::string newWord; - if(m_currentLine->width > 0) + if(m_currentLine->width > 0) { newWord = ' ' + word; - } + } else { newWord = word; } FT_Error error = initWordGlyphs(glyphs, newWord, m_currentLine->pen); - if(!error) + if(!error) { compute_bbox(glyphs, &bbox); if(m_currentLine->width == 0 || bbox.xMax <= maxWidth) @@ -371,7 +371,7 @@ FT_Error CCFreeTypeFont::addWord(const std::string& word) } m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin; } - else + else { endLine(); newLine(); @@ -381,7 +381,7 @@ FT_Error CCFreeTypeFont::addWord(const std::string& word) return error; } -FT_Error CCFreeTypeFont::initGlyphs(const char* text) +FT_Error CCFreeTypeFont::initGlyphs(const char* text) { FT_Error error = 0; std::stringstream stringStream(text); @@ -396,7 +396,7 @@ FT_Error CCFreeTypeFont::initGlyphs(const char* text) m_lines.clear(); - while(std::getline(stringStream, line) && !error) + while(std::getline(stringStream, line) && !error) { newLine(); @@ -422,14 +422,14 @@ FT_Error CCFreeTypeFont::initGlyphs(const char* text) // if linewidth > m_inWidth // get words for line -void CCFreeTypeFont::initWords(const char* text) +void CCFreeTypeFont::initWords(const char* text) { std::stringstream stringStream(text); std::string line; vector lines; vector words; - while(std::getline(stringStream, line)) + while(std::getline(stringStream, line)) { lines.push_back(line); } @@ -453,9 +453,9 @@ void CCFreeTypeFont::initWords(const char* text) } } -FT_Error CCFreeTypeFont::initWordGlyphs(std::vector& glyphs, const std::string& text, FT_Vector& pen) +FT_Error CCFreeTypeFont::initWordGlyphs(std::vector& glyphs, const std::string& text, FT_Vector& pen) { - FT_GlyphSlot slot = m_face->glyph; + FT_GlyphSlot slot = m_face->glyph; FT_UInt glyph_index; FT_UInt previous = 0; FT_Error error = 0; @@ -464,15 +464,15 @@ FT_Error CCFreeTypeFont::initWordGlyphs(std::vector& glyphs, const std:: wchar_t * pwszBuffer = nullptr; int num_chars = text.size(); - int nBufLen = num_chars + 1; + int nBufLen = num_chars + 1; pwszBuffer = new wchar_t[nBufLen]; if(!pwszBuffer) { return -1; } - + memset(pwszBuffer,0,nBufLen); - num_chars = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), num_chars, pwszBuffer, nBufLen); + num_chars = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), num_chars, pwszBuffer, nBufLen); pwszBuffer[num_chars] = '\0'; glyphs.clear(); @@ -571,12 +571,22 @@ void CCFreeTypeFont::compute_bbox(std::vector& glyphs, FT_BBox *abbox) bbox.xMax = 0; bbox.yMax = 0; } - + /* return string bbox */ *abbox = bbox; } -unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size) +namespace { + inline unsigned char* takeBuffer(Data& data, ssize_t* size = nullptr) { + auto buffer = data.getBytes(); + if (size) + *size = data.getSize(); + data.fastSet(nullptr, 0); + return buffer; + } +} + +unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size) { @@ -611,10 +621,12 @@ unsigned char* CCFreeTypeFont::loadFont(const char *pFontName, ssize_t *size) return nullptr; } - return FileUtils::getInstance()->getFileData(fullpath.c_str(), "rb", size); + Data d; + FileUtils::getInstance()->getContents(fullpath, &d); + return takeBuffer(d, size); } -unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, ssize_t *size) +unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, ssize_t *size) { std::string aName(pFontName); unsigned char* pBuffer = nullptr; @@ -631,9 +643,9 @@ unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, ssize_t *si BOOL exists; std::wstring fontNameW; const void *fontFileReferenceKey = nullptr; - UINT32 fontFileReferenceKeySize; + UINT32 fontFileReferenceKeySize; void *fragmentContext = nullptr; - + for (unsigned int i = 0; i < aName.length(); ++i) { aName[i] = tolower(aName[i]); @@ -649,7 +661,7 @@ unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, ssize_t *si hr = writeFactory->GetSystemFontCollection(&fontCollection, TRUE); } - //get the font + //get the font if(SUCCEEDED(hr)) { hr = fontCollection->FindFamilyName(fontNameW.c_str(), &index, &exists); @@ -704,7 +716,7 @@ unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, ssize_t *si if(SUCCEEDED(hr)) { pBuffer = (unsigned char*)malloc((size_t)fileSize); - memcpy(pBuffer, fragmentStart, (size_t)fileSize); + memcpy(pBuffer, fragmentStart, (size_t)fileSize); *size = (unsigned long)fileSize; } } @@ -750,4 +762,3 @@ unsigned char* CCFreeTypeFont::loadSystemFont(const char *pFontName, ssize_t *si } NS_CC_END -