Exemplo n.º 1
0
//-----------------------------------------------------------------------
void TrueTypeFont::Impl::LoadFont(AssetManager const& assets, std::string const& fontPath)
{
    auto binaryFile = assets.OpenStream(fontPath);
    if (!binaryFile.Stream) {
        POMDOG_THROW_EXCEPTION(std::runtime_error, "Failed to open file");
    }

    if (binaryFile.SizeInBytes <= 0) {
        POMDOG_THROW_EXCEPTION(std::runtime_error, "The file is too small");
    }

    Reset();

    using Pomdog::Detail::BinaryReader;
    ttfBinary = BinaryReader::ReadArray<std::uint8_t>(binaryFile.Stream, binaryFile.SizeInBytes);

    const auto offset = stbtt_GetFontOffsetForIndex(ttfBinary.data(), 0);
    if (!stbtt_InitFont(&fontInfo, ttfBinary.data(), offset)) {
        ttfBinary.clear();

        // FUS RO DAH
        POMDOG_THROW_EXCEPTION(std::runtime_error,
            "Failed to initialize truetype font");
    }
}
Exemplo n.º 2
0
ParticleClip ParticleLoader::LoadFromJson(AssetManager & assets, std::string const& assetName)
{
    POMDOG_ASSERT(!assetName.empty());

    using Detail::BinaryReader;

    auto binaryFile = assets.OpenStream(assetName);

    if (!binaryFile.Stream) {
        POMDOG_THROW_EXCEPTION(std::runtime_error, "Failed to open file");
    }

    if (binaryFile.SizeInBytes <= 0) {
        POMDOG_THROW_EXCEPTION(std::runtime_error, "The file is too small");
    }

    auto json = BinaryReader::ReadString<char>(binaryFile.Stream, binaryFile.SizeInBytes);
    POMDOG_ASSERT(!json.empty());

    if (json.empty()) {
        POMDOG_THROW_EXCEPTION(std::runtime_error, "The file is too small");
    }

    rapidjson::Document doc;
    doc.Parse(json.data());

    if (doc.HasParseError() || !doc.IsObject() || doc.MemberBegin() == doc.MemberEnd()) {
        // FUS RO DAH
        POMDOG_THROW_EXCEPTION(std::runtime_error, "Failed to parse JSON");
    }

    auto member = doc.MemberBegin();

    if (!member->name.IsString() || !member->value.IsObject()) {
        // FUS RO DAH
        POMDOG_THROW_EXCEPTION(std::runtime_error, "Invalid file format");
    }

    POMDOG_ASSERT(member->name.IsString());
    POMDOG_ASSERT(member->value.IsObject());

    return ReadParticleClip(member->value);
}