int lengthOfLongestSubstring(string s) { if (s.empty()) return 0; std::array<int, 256> CharMap(256, -1); int MaxLength = 1; for (int index = 0; index < s.length(); ++index) { if (CharMap[s[index]] == -1) { CharMap[s[index]] = index; } else { MaxLength = std::max(index - CharMap[s[index]], MaxLength); CharMap[s[index]] = index; } } return MaxLength; }
TtfFont::TheGlyph &TtfFont::loadGlyph(uint32_t fontSize, char32_t character) { FT_Error error = 0; TheGlyph t_glyph; if(m_recentPixelSize != fontSize) { error = FT_Set_Pixel_Sizes(m_face, 0, fontSize); SDL_assert_release(error == 0); m_recentPixelSize = fontSize; } error = FT_Load_Char(m_face, character, FT_LOAD_RENDER); SDL_assert_release(error == 0); //FIXME: return dummy glypg instead FT_GlyphSlot glyph = m_face->glyph; FT_Bitmap &bitmap = glyph->bitmap; uint32_t width = bitmap.width; uint32_t height = bitmap.rows; uint8_t *image = new uint8_t[4 * width * height]; if(bitmap.pitch >= 0) { for(uint32_t w = 0; w < width; w++) { for(uint32_t h = 0; h < height; h++) { uint8_t color = bitmap.buffer[static_cast<uint32_t>(bitmap.pitch) * ((height - 1) - h) + w]; image[(4 * width) * (height - 1 - h) + (4 * w)] = color; image[(4 * width) * (height - 1 - h) + (4 * w + 1)] = color; image[(4 * width) * (height - 1 - h) + (4 * w + 2)] = color; image[(4 * width) * (height - 1 - h) + (4 * w + 3)] = color; } } } else if(bitmap.pitch < 0) { for(uint32_t w = 0; w < width; w++) { for(uint32_t h = 0; h < height; h++) { uint8_t color = *(bitmap.buffer - (static_cast<uint32_t>(bitmap.pitch) * (h)) + w); image[(4 * width)*h + (4 * w)] = color; image[(4 * width)*h + (4 * w + 1)] = color; image[(4 * width)*h + (4 * w + 2)] = color; image[(4 * width)*h + (4 * w + 3)] = color; } } } m_texturesBank.emplace_back(); PGE_Texture &texture = m_texturesBank.back(); texture.nOfColors = GL_RGBA; texture.format = GL_BGRA; GlRenderer::loadRawTextureP(texture, image, width, height); t_glyph.tx = &texture; t_glyph.width = width; t_glyph.height = height; t_glyph.left = glyph->bitmap_left; t_glyph.top = glyph->bitmap_top; t_glyph.advance = glyph->advance.x; t_glyph.glyph_width = glyph->advance.x; delete [] image; SizeCharMap::iterator fSize = m_charMap.find(fontSize); if(fSize == m_charMap.end()) { m_charMap.insert({fontSize, CharMap()}); fSize = m_charMap.find(fontSize); SDL_assert_release(fSize != m_charMap.end()); } fSize->second.insert({character, t_glyph}); CharMap::iterator rc = fSize->second.find(character); SDL_assert_release(rc != fSize->second.end()); return rc->second; }