int32_t FreeTypeFont::getTextLength(const std::u32string& text, const size_t size) { if (!face) { return -1; } int32_t textLength = 0; size_t textIndex = 0; FontCacheEntry* fontCacheEntry; while (textIndex < text.length()) { fontCacheEntry = getGlyph(text[textIndex], size); if (fontCacheEntry == nullptr) { return -1; } textLength += fontCacheEntry->advanceX; textIndex++; } return textLength; }
void operator()(const std::u32string& str) { m_serializer->acquire(sizeof(std::uint32_t) + str.size()); write_int(m_serializer, static_cast<std::uint32_t>(str.size())); for (char32_t c : str) { // force writer to use exactly 32 bit write_int(m_serializer, static_cast<std::uint32_t>(c)); } }
inline void designer< T, C, N >::drawText( T x, T y, const std::u32string &text ) { // Set the pen position setPenPosition( x, y ); // Draw the unicode string drawText( text.begin(), text.end() ); }
void FreeTypeFont::doFillGlyphCache(GlyphCache& cache, const std::u32string& characters) { /** @bug Crash when atlas is too small */ /* Get glyph codes from characters */ std::vector<FT_UInt> charIndices; charIndices.resize(characters.size()+1); charIndices[0] = 0; std::transform(characters.begin(), characters.end(), charIndices.begin()+1, [this](const char32_t c) { return FT_Get_Char_Index(ftFont, c); }); /* Remove duplicates (e.g. uppercase and lowercase mapped to same glyph) */ std::sort(charIndices.begin(), charIndices.end()); charIndices.erase(std::unique(charIndices.begin(), charIndices.end()), charIndices.end()); /* Sizes of all characters */ std::vector<Vector2i> charSizes; charSizes.reserve(charIndices.size()); for(FT_UInt c: charIndices) { CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(ftFont, c, FT_LOAD_DEFAULT) == 0); charSizes.push_back(Vector2i(ftFont->glyph->metrics.width, ftFont->glyph->metrics.height)/64); } /* Create texture atlas */ const std::vector<Range2Di> charPositions = cache.reserve(charSizes); /* Render all characters to the atlas and create character map */ Containers::Array<char> pixmap{Containers::ValueInit, std::size_t(cache.textureSize().product())}; for(std::size_t i = 0; i != charPositions.size(); ++i) { /* Load and render glyph */ /** @todo B&W only if radius != 0 */ FT_GlyphSlot glyph = ftFont->glyph; CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Load_Glyph(ftFont, charIndices[i], FT_LOAD_DEFAULT) == 0); CORRADE_INTERNAL_ASSERT_OUTPUT(FT_Render_Glyph(glyph, FT_RENDER_MODE_NORMAL) == 0); /* Copy rendered bitmap to texture image */ const FT_Bitmap& bitmap = glyph->bitmap; CORRADE_INTERNAL_ASSERT(std::abs(Int(bitmap.width)-charPositions[i].sizeX()) <= 2); CORRADE_INTERNAL_ASSERT(std::abs(Int(bitmap.rows)-charPositions[i].sizeY()) <= 2); for(Int yin = 0, yout = charPositions[i].bottom(), ymax = bitmap.rows; yin != ymax; ++yin, ++yout) for(Int xin = 0, xout = charPositions[i].left(), xmax = bitmap.width; xin != xmax; ++xin, ++xout) pixmap[yout*cache.textureSize().x() + xout] = bitmap.buffer[(bitmap.rows-yin-1)*bitmap.width + xin]; /* Insert glyph parameters into cache */ cache.insert(charIndices[i], Vector2i(glyph->bitmap_left, glyph->bitmap_top-charPositions[i].sizeY()), charPositions[i]); } /* Set cache image */ #ifndef MAGNUM_TARGET_GLES2 Image2D image(PixelFormat::Red, PixelType::UnsignedByte, cache.textureSize(), std::move(pixmap)); #else Image2D image(Context::current() && Context::current()->isExtensionSupported<Extensions::GL::EXT::texture_rg>() ? PixelFormat::Red : PixelFormat::Luminance, PixelType::UnsignedByte, cache.textureSize(), std::move(pixmap)); #endif cache.setImage({}, image); }
TagItems(const std::u32string& tagStartWithAttrs) { size_t tmp; if(tagStartWithAttrs.length() > 0 && (tmp = tagStartWithAttrs.find(' ')) != std::u32string::npos) { tagStart = tagStartWithAttrs.substr(0, tmp); tagEnd = tagStart + U">"; tagEnd.insert(1, U"/"); } else { tagStart = tagStartWithAttrs; tagEnd = tagStartWithAttrs + U">"; tagEnd.insert(1, U"/"); } }
VkBool32 FreeTypeFont::prepareText(const ICommandBuffersSP& cmdBuffer, const std::u32string& text, const size_t size) { if (!face || !cmdBuffer.get()) { return VK_FALSE; } size_t textIndex = 0; FontCacheEntry* fontCacheEntry; while (textIndex < text.length()) { fontCacheEntry = getGlyph(cmdBuffer, text[textIndex], size); if (fontCacheEntry == nullptr) { return VK_FALSE; } textIndex++; } return VK_TRUE; }
void FontAtlas::findNewCharacters(const std::u32string& u32Text, std::unordered_map<unsigned int, unsigned int>& charCodeMap) { std::u32string newChars; FT_Encoding charEncoding = _fontFreeType->getEncoding(); //find new characters if (_letterDefinitions.empty()) { // fixed #16169: new android project crash in android 5.0.2 device (Nexus 7) when use 3.12. // While using clang compiler with gnustl_static on android, the copy assignment operator of `std::u32string` // will affect the memory validity, it means after `newChars` is destroyed, the memory of `u32Text` holds // will be a dead region. `u32text` represents the variable in `Label::_utf32Text`, when somewhere // allocates memory by `malloc, realloc, new, new[]`, the generated memory address may be the same // as `Label::_utf32Text` holds. If doing a `memset` or other memory operations, the orignal `Label::_utf32Text` // will be in an unknown state. Meanwhile, a bunch lots of logic which depends on `Label::_utf32Text` // will be broken. // newChars = u32Text; // Using `append` method is a workaround for this issue. So please be carefuly while using the assignment operator // of `std::u32string`. newChars.append(u32Text); } else { auto length = u32Text.length(); newChars.reserve(length); for (size_t i = 0; i < length; ++i) { auto outIterator = _letterDefinitions.find(u32Text[i]); if (outIterator == _letterDefinitions.end()) { newChars.push_back(u32Text[i]); } } } if (!newChars.empty()) { switch (charEncoding) { case FT_ENCODING_UNICODE: { for (auto u32Code : newChars) { charCodeMap[u32Code] = u32Code; } break; } case FT_ENCODING_GB2312: { conversionU32TOGB2312(newChars, charCodeMap); break; } default: CCLOG("FontAtlas::findNewCharacters: Unsupported encoding:%d", charEncoding); break; } } }
bool UTF8ToUTF32(const std::string& utf8, std::u32string& outUtf32) { if (utf8.empty()) { outUtf32.clear(); return true; } bool ret = false; const size_t utf32Bytes = (utf8.length() + 1) * sizeof(char32_t); char32_t* utf32 = (char32_t*)malloc(utf32Bytes); memset(utf32, 0, utf32Bytes); char* utf32ptr = reinterpret_cast<char*>(utf32); const UTF8* error = NULL; if (llvm::ConvertUTF8toWide(4, utf8, utf32ptr, error)) { outUtf32 = utf32; ret = true; } free(utf32); return ret; }
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 ofxEditorSyntax::setPunctuationChars(const std::u32string &chars) { if(chars.length() == 0) { ofLogWarning("ofxEditorSyntax") << "empty punctuation string"; return; } punctuationChars = chars; }
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()}}; }
//-------------------------------------------------------------- void ofxEditorSyntax::setOperatorChars(const std::u32string &chars) { if(chars.length() == 0) { ofLogWarning("ofxEditorSyntax") << "empty operator string"; return; } operatorChars = chars; }
Text::Text(Mat4f const & transformation, std::string const & fontname, std::u32string const & text) : dynamicHint(text.empty()), modelMat(transformation), fontname(fontname), string(text), vao(nullptr), vbo(nullptr), ibo(nullptr) { init(); }
std::string utf32_to_utf8(const std::u32string & str_u32){ std::string str_u8; str_u8.reserve(str_u32.length()); // just a guess. for(const uint32_t u32 : str_u32) str_u8.append(utf32_to_utf8(u32)); return str_u8; }
Rect FTFont::GetSize(std::u32string const& txt) const { int const s = Font::Default()->GetSize(txt).width; if (s == -1) { Output::Warning("Text contains invalid chars. Is the encoding correct?"); return Rect(0, 0, pixel_size() * txt.length() / 2, pixel_size()); } else { return Rect(0, 0, s, pixel_size()); } }
bool iterator::split( const std::u32string &s ) { size_t n = s.size(); if ( _value.compare( 0, n, s ) == 0 ) { _next.insert( 0, _value.substr( n ) ); _value.resize( n ); return true; } return false; }
int string::compare(const std::u32string & s) const noexcept { size_type sz = size(); size_type len = s.size(); size_type n = std::min(sz, len); auto pos = cbegin(); auto spos = s.cbegin(); for ( ; n; pos++, spos++ ) { if ( traits_type::lt(*pos, *spos) ) return -1; if ( traits_type::lt(*spos, *pos) ) return 1; } if ( sz < len ) return -1; if ( sz > len ) return 1; return 0; }
// static bool EditableMapObject::ValidateName(string const & name) { if (name.empty()) return true; if (strings::IsASCIIString(name)) return regex_match(name, regex(R"(^[ A-Za-z0-9.,?!@#$%&()\-\+:;"'`]+$)")); std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter; std::u32string u32name; try { u32name = converter.from_bytes(name); } catch (std::range_error const &) { // Cannot convert, for ex. it is possible for some emoji. return false; } std::u32string const excludedSymbols = U"^~§><{}[]*=_±\n\t\r\v\f|√•÷׶°"; for (auto const ch : u32name) { // Exclude arrows, mathematical symbols, borders, geometric shapes. if (ch >= U'\U00002190' && ch <= U'\U00002BFF') return false; // Exclude format controls, musical symbols, emoticons, ornamental and pictographs, // ancient and exotic alphabets. if (ch >= U'\U0000FFF0' && ch <= U'\U0001F9FF') return false; if (find(excludedSymbols.cbegin(), excludedSymbols.cend(), ch) != excludedSymbols.cend()) return false; } return true; }
//Вспомогательная функция подсчета объема "текста" в элементах-непосредственных потомках size_t textSize(GumboNode *a) { assert(a != nullptr && a->type == GUMBO_NODE_ELEMENT); size_t l_size = 0; for (size_t i = 0; i < a->v.element.children.length; i++) { GumboNode *const l_child = static_cast<GumboNode *>(a->v.element.children.data[i]); if (l_child->type != GUMBO_NODE_ELEMENT) continue; if (l_child->v.element.tag == GUMBO_TAG_SCRIPT || l_child->v.element.tag == GUMBO_TAG_STYLE || l_child->v.element.tag == GUMBO_TAG_TEXTAREA) continue; for (size_t j = 0; j < l_child->v.element.children.length; j++) { GumboNode *const l_one = static_cast<GumboNode *>(l_child->v.element.children.data[j]); if (l_one->type != GUMBO_NODE_TEXT) continue; const std::u32string l_utf32text(Encoding::utf8to32(l_one->v.text.text)); l_size += l_utf32text.size(); } } return l_size; }
/* * 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; }
// ------------------------------------------------------------------ vaca::String FileReader::readLine(const std::u32string& delim) { char32_t decChar; size_t countDelim = delim.length(); size_t equalChar = 0; std::u32string tmp; if (countDelim) while (readChar(decChar)) { if (decChar == delim[equalChar]) { ++equalChar; if (countDelim == equalChar) break; } else { tmp.push_back(decChar); } } return to_String(tmp); }
grammar load_grammar(std::string const & nameOfMain, std::u32string const & document, std::map<std::string, associativity> const & associativities, std::set<std::string> const & longestNames) { (void)dont_care; parser p; abstract_syntax_graph asg = p.parse(builtins::wirth, document); std::string check = asg.to_dot(); permutation const & top = *asg.permutations[asg.root].begin(); std::vector<state_machine> machines; std::map<std::string, std::shared_ptr<details::behavior_node>> trees; for (match const & entry : top) { if (&entry.r == &productionDfa) { std::shared_ptr<details::behavior_node> behavior = process_production(document, entry, asg); match const & namePart = (*asg.permutations[entry].begin())[0]; std::string name = to_utf8(document.substr(namePart.document_position, namePart.consumed_character_count)); recognizer const * dontCare; if (builtins::resolve_builtin(name, dontCare)) { throw std::logic_error((name + " is a reserved name.").c_str()); // name is reserved for a builtin } trees[name] = behavior; } } return grammar(nameOfMain, trees, associativities, longestNames); }
VkBool32 FreeTypeFont::renderText(const ICommandBuffersSP& cmdBuffer, const std::u32string& text, const size_t size, const glm::vec4& color) { if (!face || !cmdBuffer.get()) { return VK_FALSE; } // // TODO: Create once vertex buffer etc. like in example 3. // TODO: Create for each letter a descriptor set. // size_t textIndex = 0; FontCacheEntry* fontCacheEntry; while (textIndex < text.length()) { // Do not use the render command buffer for creation- fontCacheEntry = getGlyph(text[textIndex], size); if (fontCacheEntry == nullptr) { return VK_FALSE; } // TODO: Bind descriptor set. // TODO: Draw triangle square. textIndex++; } return VK_TRUE; }
glyph_text graphics_context::to_glyph_text(const std::u32string& aText, const font& aFont) const { return to_glyph_text(aText.begin(), aText.end(), aFont); }
static std::u32string string_to_source(const std::u32string& string) { std::u32string result; result.reserve(string.length() + 2); result.append(1, '"'); for (const auto& c : string) { switch (c) { case 010: result.append(1, '\\'); result.append(1, 'b'); break; case 011: result.append(1, '\\'); result.append(1, 't'); break; case 012: result.append(1, '\\'); result.append(1, 'n'); break; case 014: result.append(1, '\\'); result.append(1, 'f'); break; case 015: result.append(1, '\\'); result.append(1, 'r'); break; case '"': case '\\': case '/': result.append(1, '\\'); result.append(1, c); break; default: if (!peelo::unicode::isprint(c)) { char buffer[7]; std::snprintf(buffer, 7, "\\u%04x", c); for (const char* p = buffer; *p; ++p) { result.append(1, static_cast<char32_t>(*p)); } } else { result.append(1, c); } } } result.append(1, '"'); return result; }
void utf8::decode(const char* str, size_t len, std::u32string& decoded) { decoded.clear(); while (len) decoded.push_back(decode(str, len)); }
void utf8::decode(const char* str, std::u32string& decoded) { decoded.clear(); for (char32_t chr; (chr = decode(str)); ) decoded.push_back(chr); }
// Convert UTF-32 string to UTF-8 string std::string luna2d::utf::FromUtf32(const std::u32string& string) { std::string ret; utf32to8(string.begin(), string.end(), std::back_inserter(ret)); return std::move(ret); }
errorcode node::follow_soft(const std::u32string& pointer, node*& current) throw(std::bad_alloc) { current = this; size_t ptr = 0; while(ptr < pointer.length()) { size_t p = pointer.find_first_of(U"/", ptr); if(p > pointer.length()) p = pointer.length(); std::u32string c; auto e = jsonptr_unescape_soft(pointer, ptr, p, c); if(e != ERR_OK) return e; if(current->vtype == array) { if(c == U"-") return ERR_POINTER_BAD_APPEND; size_t idx; if(!parse_size_t(c, idx)) return ERR_POINTER_BAD_INDEX; e = current->index_soft(idx, current); if(e != ERR_OK) return e; } else if(current->vtype == object) { e = current->field_soft(c, 0, current); if(e != ERR_OK) return e; } else return ERR_NOT_ARRAY_NOR_OBJECT; ptr = p + 1; } return ERR_OK; }
int string::compare(size_type pos1, size_type n1, const std::u32string& str, size_type pos2, size_type n2) const { if ( n1 == 0 && n2 > 0 ) return -1; size_type sz = (n1 == npos ? size() - pos1 : n1); size_type len = (n2 == npos ? str.size() - pos2 : n2); size_type n = std::min(sz, len); auto pos = cbegin()+pos1; auto spos = str.cbegin(); for ( ; n; pos++, spos++ ) { if ( traits_type::lt(*pos, *spos) ) return -1; if ( traits_type::lt(*spos, *pos) ) return 1; } if ( sz < len ) return -1; if ( sz > len ) return 1; return 0; }