renderer_i::texture sfml2_renderer::make_text_label(const std::u32string& text, const color& fill, const font& font, float point_size, text_style style) { const sfml2_font& tmp = dynamic_cast<const sfml2_font&>(*font); const sf::Font& sffont = tmp.sf_font(); sf::String str(reinterpret_cast<const sf::Uint32*>(text.c_str())); sf::Text label(str, sffont, (int)point_size); sf::RenderTexture rt; auto size = label.getLocalBounds(); if (!rt.create(size.width + 1, font->height(point_size) + 1)) throw std::runtime_error("cannot create sf::RenderTexture"); rt.clear(sf::Color::Transparent); label.move(0, -label.getLocalBounds().top); label.setColor(col(fill)); label.setStyle(static_cast<sf::Uint32>(style)); rt.draw(label); rt.display(); return texture{new sfml2_texture{rt.getTexture()}}; }
/* * Find the first */ bool getElement(const std::u32string& html, const std::u32string& tagStartWithAttrs, Position& pos) { size_t index; pos.start = -1; pos.len = -1; if(tagStartWithAttrs.length() == 0 || tagStartWithAttrs[0] != U'<') { fprintf(stderr, "invalid tagStartWithAttrs supplied: must start with <\n"); return false; } TagItems tagItems(tagStartWithAttrs); std::stack<int> stack; size_t tagEndLen = tagItems.tagEnd.length(); size_t tagStartLen = tagItems.tagStart.length(); size_t htmlLen = html.length(); // search for our tagStartWithAttrs if((index = html.find(tagStartWithAttrs)) != std::u32string::npos) { pos.start = index; index += tagStartWithAttrs.length(); while((index = html.find(U"<", index)) != std::u32string::npos) { // check if the tag is a start tag if(index + tagStartLen <= htmlLen) { if(memcmp(html.c_str() + index, tagItems.tagStart.c_str(), tagStartLen*sizeof(tagItems.tagStart[0])) == 0) { // an embedded start tag pushed to the stack stack.push(index); } } // check if the tag is an end tag if(index + tagEndLen <= htmlLen) { if(memcmp(html.c_str() + index, tagItems.tagEnd.c_str(), tagEndLen*sizeof(tagItems.tagEnd[0])) == 0) { if(stack.size()) { // close tag for the last embedded start tag stack.pop(); } else { // we have found the end tag to our initial tagStartWithAttrs pos.len = index - pos.start + tagEndLen; break; } } } ++index; } } return true; }
int findTags(const std::u32string& html, std::map<std::u32string, std::vector<Position> >& tags) { size_t index = 0; size_t htmlLen = html.length(); while((index = html.find(U"<", index)) != std::u32string::npos) { for(std::map<std::u32string, std::vector<Position> >::iterator itr = tags.begin(); itr != tags.end(); ++itr) { const std::u32string& str = itr->first; if(index + str.length() <= htmlLen) { if(memcmp(html.c_str() + index, str.c_str(), str.length()*sizeof(str[0])) == 0) { itr->second.push_back(Position(index, 0)); } } } ++index; } return 0; }
void k1(const std::u32string &s) { k1(s.c_str()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr] // CHECK-FIXES: {{^ }}k1(s);{{$}} }
void FontAtlas::conversionU32TOGB2312(const std::u32string& u32Text, std::unordered_map<unsigned int, unsigned int>& charCodeMap) { size_t strLen = u32Text.length(); auto gb2312StrSize = strLen * 2; auto gb2312Text = new (std::nothrow) char[gb2312StrSize]; memset(gb2312Text, 0, gb2312StrSize); switch (_fontFreeType->getEncoding()) { case FT_ENCODING_GB2312: { #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT std::u16string u16Text; cocos2d::StringUtils::UTF32ToUTF16(u32Text, u16Text); WideCharToMultiByte(936, NULL, (LPCWCH)u16Text.c_str(), strLen, (LPSTR)gb2312Text, gb2312StrSize, NULL, NULL); #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID conversionEncodingJNI((char*)u32Text.c_str(), gb2312StrSize, "UTF-32LE", gb2312Text, "GB2312"); #else if (_iconv == nullptr) { _iconv = iconv_open("GBK//TRANSLIT", "UTF-32LE"); } if (_iconv == (iconv_t)-1) { CCLOG("conversion from utf32 to gb2312 not available"); } else { char* pin = (char*)u32Text.c_str(); char* pout = gb2312Text; size_t inLen = strLen * 2; size_t outLen = gb2312StrSize; iconv(_iconv, (char**)&pin, &inLen, &pout, &outLen); } #endif } break; default: CCLOG("Unsupported encoding:%d", _fontFreeType->getEncoding()); break; } unsigned short gb2312Code = 0; unsigned char* dst = (unsigned char*)&gb2312Code; char32_t u32Code; for (size_t index = 0, gbIndex = 0; index < strLen; ++index) { u32Code = u32Text[index]; if (u32Code < 256) { charCodeMap[u32Code] = u32Code; gbIndex += 1; } else { dst[0] = gb2312Text[gbIndex + 1]; dst[1] = gb2312Text[gbIndex]; charCodeMap[u32Code] = gb2312Code; gbIndex += 2; } } delete[] gb2312Text; }