bool Stream::readLittle(std::string *str, std::size_t size) const { if (str) { unsigned int length; if (!readLittle(length) || length == 0 || length > size) { error = true; return false; } for (unsigned int i = 0; i < length; ++i) { if (!readLittle(str[i])) { error = true; return false; } } return true; } else { return false; } }
void SplinePath::read(DataSourceRef source) { auto stream = source->createStream(); int newPointsSize; stream->readLittle(&newPointsSize); points.reserve(size() + newPointsSize); // --- Vec2f point; for (int i = 0; i < newPointsSize; i++) { stream->readLittle(&point.x); stream->readLittle(&point.y); add(point); } }
bool Stream::readLittle(std::string &value) const { unsigned int length; if (readLittle(length)) { if (length) { value.resize(length); bool returnValue = readLittle(&value[0], length); error |= !returnValue; return returnValue; } else { return true; } } else { error = true; return false; } }
std::pair<FontData*, FontAtlas*> FontManager::fetchFontDataAndAtlas(InputSourceRef source) { auto in = source->loadDataSource()->createStream(); // CAN THROW string version; in->readFixedString(&version, 10); if (version != "XFONT.003") { throw runtime_error("Font: WRONG FORMAT"); } // --- int glyphCount; in->readLittle(&glyphCount); auto data = new FontData(glyphCount); in->readLittle(&data->baseSize); in->readLittle(&data->height); in->readLittle(&data->ascent); in->readLittle(&data->descent); in->readLittle(&data->spaceAdvance); in->readLittle(&data->strikethroughFactor); in->readLittle(&data->underlineOffset); in->readLittle(&data->lineThickness); int atlasWidth; int atlasHeight; int atlasPadding; int unitMargin; int unitPadding; in->readLittle(&atlasWidth); in->readLittle(&atlasHeight); in->readLittle(&atlasPadding); in->readLittle(&unitMargin); in->readLittle(&unitPadding); auto atlas = new FontAtlas(atlasWidth, atlasHeight); for (int i = 0; i < glyphCount; i++) { int glyphChar; int glyphWidth; int glyphHeight; int glyphLeftExtent; int glyphTopExtent; int glyphAtlasX; int glyphAtlasY; in->readLittle(&glyphChar); data->glyphs[(wchar_t)glyphChar] = i; in->readLittle(&data->advance[i]); in->readLittle(&glyphWidth); in->readLittle(&glyphHeight); in->readLittle(&glyphLeftExtent); in->readLittle(&glyphTopExtent); in->readLittle(&glyphAtlasX); in->readLittle(&glyphAtlasY); auto glyphData = new unsigned char[glyphWidth * glyphHeight]; in->readData(glyphData, glyphWidth * glyphHeight); atlas->addGlyph(glyphData, glyphAtlasX + atlasPadding + unitMargin, glyphAtlasY + atlasPadding + unitMargin, glyphWidth, glyphHeight); delete[] glyphData; data->w[i] = glyphWidth + unitPadding * 2; data->h[i] = glyphHeight + unitPadding * 2; data->le[i] = glyphLeftExtent - unitPadding; data->te[i] = glyphTopExtent + unitPadding; int x = glyphAtlasX + atlasPadding + unitMargin - unitPadding; int y = glyphAtlasY + atlasPadding + unitMargin - unitPadding; data->u1[i] = x / (float)atlasWidth; data->v1[i] = y / (float)atlasHeight; data->u2[i] = (x + data->w[i]) / (float)atlasWidth; data->v2[i] = (y + data->h[i]) / (float)atlasHeight; } return make_pair(data, atlas); }