std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& encodedImage, const std::string& json) {
    const PremultipliedImage raster = decodeImage(encodedImage);

    JSDocument doc;
    doc.Parse<0>(json.c_str());
    if (doc.HasParseError()) {
        std::stringstream message;
        message << "Failed to parse JSON: " << rapidjson::GetParseError_En(doc.GetParseError()) << " at offset " << doc.GetErrorOffset();
        throw std::runtime_error(message.str());
    } else if (!doc.IsObject()) {
        throw std::runtime_error("Sprite JSON root must be an object");
    } else {
        std::vector<std::unique_ptr<style::Image>> images;
        for (const auto& property : doc.GetObject()) {
            const std::string name = { property.name.GetString(), property.name.GetStringLength() };
            const JSValue& value = property.value;

            if (value.IsObject()) {
                const uint16_t x = getUInt16(value, "x", 0);
                const uint16_t y = getUInt16(value, "y", 0);
                const uint16_t width = getUInt16(value, "width", 0);
                const uint16_t height = getUInt16(value, "height", 0);
                const double pixelRatio = getDouble(value, "pixelRatio", 1);
                const bool sdf = getBoolean(value, "sdf", false);

                auto image = createStyleImage(name, raster, x, y, width, height, pixelRatio, sdf);
                if (image) {
                    images.push_back(std::move(image));
                }
            }
        }
        return images;
    }
}
Example #2
0
JSValueRef DumpRenderTreeSupportGtk::nodesFromRect(JSContextRef context, JSValueRef value, int x, int y, unsigned top, unsigned right, unsigned bottom, unsigned left, bool ignoreClipping)
{
    JSLock lock(SilenceAssertionsOnly);
    ExecState* exec = toJS(context);
    if (!value)
        return JSValueMakeUndefined(context);
    JSValue jsValue = toJS(exec, value);
    if (!jsValue.inherits(&JSDocument::s_info))
       return JSValueMakeUndefined(context);

    JSDocument* jsDocument = static_cast<JSDocument*>(asObject(jsValue));
    Document* document = jsDocument->impl();
    RefPtr<NodeList> nodes = document->nodesFromRect(x, y, top, right, bottom, left, ignoreClipping);
    return toRef(exec, toJS(exec, jsDocument->globalObject(), nodes.get()));
}
SpriteParseResult parseSprite(const std::string& image, const std::string& json) {
    Sprites sprites;
    PremultipliedImage raster;

    try {
        raster = decodeImage(image);
    } catch (...) {
        return std::current_exception();
    }

    JSDocument doc;
    doc.Parse<0>(json.c_str());

    if (doc.HasParseError()) {
        std::stringstream message;
        message << "Failed to parse JSON: " << rapidjson::GetParseError_En(doc.GetParseError()) << " at offset " << doc.GetErrorOffset();
        return std::make_exception_ptr(std::runtime_error(message.str()));
    } else if (!doc.IsObject()) {
        return std::make_exception_ptr(std::runtime_error("Sprite JSON root must be an object"));
    } else {
        for (JSValue::ConstMemberIterator itr = doc.MemberBegin(); itr != doc.MemberEnd(); ++itr) {
            const std::string name = { itr->name.GetString(), itr->name.GetStringLength() };
            const JSValue& value = itr->value;

            if (value.IsObject()) {
                const uint16_t x = getUInt16(value, "x", 0);
                const uint16_t y = getUInt16(value, "y", 0);
                const uint16_t width = getUInt16(value, "width", 0);
                const uint16_t height = getUInt16(value, "height", 0);
                const double pixelRatio = getDouble(value, "pixelRatio", 1);
                const bool sdf = getBoolean(value, "sdf", false);

                auto sprite = createSpriteImage(raster, x, y, width, height, pixelRatio, sdf);
                if (sprite) {
                    sprites.emplace(name, sprite);
                }
            }
        }
    }

    return sprites;
}
Example #4
0
JSValue* JSDocumentPrototypeFunction::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)
{
    if (!thisObj->inherits(&JSDocument::info))
      return throwError(exec, TypeError);

    JSDocument* castedThisObj = static_cast<JSDocument*>(thisObj);
    Document* imp = static_cast<Document*>(castedThisObj->impl());

    switch (id) {
    case JSDocument::CreateElementFuncNum: {
        ExceptionCode ec = 0;
        String tagName = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createElement(tagName, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateDocumentFragmentFuncNum: {


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createDocumentFragment()));
        return result;
    }
    case JSDocument::CreateTextNodeFuncNum: {
        String data = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createTextNode(data)));
        return result;
    }
    case JSDocument::CreateCommentFuncNum: {
        String data = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createComment(data)));
        return result;
    }
    case JSDocument::CreateCDATASectionFuncNum: {
        ExceptionCode ec = 0;
        String data = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createCDATASection(data, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateProcessingInstructionFuncNum: {
        ExceptionCode ec = 0;
        String target = args[0]->toString(exec);
        String data = args[1]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createProcessingInstruction(target, data, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateAttributeFuncNum: {
        ExceptionCode ec = 0;
        String name = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createAttribute(name, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateEntityReferenceFuncNum: {
        ExceptionCode ec = 0;
        String name = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createEntityReference(name, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::GetElementsByTagNameFuncNum: {
        String tagname = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->getElementsByTagName(tagname)));
        return result;
    }
    case JSDocument::ImportNodeFuncNum: {
        ExceptionCode ec = 0;
        Node* importedNode = toNode(args[0]);
        bool deep = args[1]->toBoolean(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->importNode(importedNode, deep, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateElementNSFuncNum: {
        ExceptionCode ec = 0;
        String namespaceURI = valueToStringWithNullCheck(exec, args[0]);
        String qualifiedName = args[1]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createElementNS(namespaceURI, qualifiedName, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateAttributeNSFuncNum: {
        ExceptionCode ec = 0;
        String namespaceURI = valueToStringWithNullCheck(exec, args[0]);
        String qualifiedName = args[1]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createAttributeNS(namespaceURI, qualifiedName, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::GetElementsByTagNameNSFuncNum: {
        String namespaceURI = valueToStringWithNullCheck(exec, args[0]);
        String localName = args[1]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->getElementsByTagNameNS(namespaceURI, localName)));
        return result;
    }
    case JSDocument::GetElementByIdFuncNum: {
        String elementId = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->getElementById(elementId)));
        return result;
    }
    case JSDocument::AdoptNodeFuncNum: {
        ExceptionCode ec = 0;
        Node* source = toNode(args[0]);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->adoptNode(source, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::LoadFuncNum: {
        String url = args[0]->toString(exec);


        KJS::JSValue* result = jsBoolean(imp->load(url));
        return result;
    }
    case JSDocument::CreateEventFuncNum: {
        ExceptionCode ec = 0;
        String eventType = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createEvent(eventType, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateRangeFuncNum: {


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createRange()));
        return result;
    }
    case JSDocument::CreateNodeIteratorFuncNum: {
        ExceptionCode ec = 0;
        Node* root = toNode(args[0]);
        bool whatToShowOk;
        unsigned whatToShow = args[1]->toInt32(exec, whatToShowOk);
        if (!whatToShowOk) {
            setDOMException(exec, TYPE_MISMATCH_ERR);
            return jsUndefined();
        }
        NodeFilter* filter = toNodeFilter(args[2]);
        bool entityReferenceExpansion = args[3]->toBoolean(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createNodeIterator(root, whatToShow, filter, entityReferenceExpansion, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::CreateTreeWalkerFuncNum: {
        ExceptionCode ec = 0;
        Node* root = toNode(args[0]);
        bool whatToShowOk;
        unsigned whatToShow = args[1]->toInt32(exec, whatToShowOk);
        if (!whatToShowOk) {
            setDOMException(exec, TYPE_MISMATCH_ERR);
            return jsUndefined();
        }
        NodeFilter* filter = toNodeFilter(args[2]);
        bool entityReferenceExpansion = args[3]->toBoolean(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->createTreeWalker(root, whatToShow, filter, entityReferenceExpansion, ec)));
        setDOMException(exec, ec);
        return result;
    }
    case JSDocument::GetOverrideStyleFuncNum: {
        Element* element = toElement(args[0]);
        String pseudoElement = args[1]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->getOverrideStyle(element, pseudoElement)));
        return result;
    }
    case JSDocument::ExecCommandFuncNum: {
        String command = args[0]->toString(exec);
        bool userInterface = args[1]->toBoolean(exec);
        String value = valueToStringWithUndefinedOrNullCheck(exec, args[2]);


        KJS::JSValue* result = jsBoolean(imp->execCommand(command, userInterface, value));
        return result;
    }
    case JSDocument::QueryCommandEnabledFuncNum: {
        String command = args[0]->toString(exec);


        KJS::JSValue* result = jsBoolean(imp->queryCommandEnabled(command));
        return result;
    }
    case JSDocument::QueryCommandIndetermFuncNum: {
        String command = args[0]->toString(exec);


        KJS::JSValue* result = jsBoolean(imp->queryCommandIndeterm(command));
        return result;
    }
    case JSDocument::QueryCommandStateFuncNum: {
        String command = args[0]->toString(exec);


        KJS::JSValue* result = jsBoolean(imp->queryCommandState(command));
        return result;
    }
    case JSDocument::QueryCommandSupportedFuncNum: {
        String command = args[0]->toString(exec);


        KJS::JSValue* result = jsBoolean(imp->queryCommandSupported(command));
        return result;
    }
    case JSDocument::QueryCommandValueFuncNum: {
        String command = args[0]->toString(exec);


        KJS::JSValue* result = jsStringOrFalse(imp->queryCommandValue(command));
        return result;
    }
    case JSDocument::GetElementsByNameFuncNum: {
        String elementName = args[0]->toString(exec);


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->getElementsByName(elementName)));
        return result;
    }
    case JSDocument::ElementFromPointFuncNum: {
        bool xOk;
        int x = args[0]->toInt32(exec, xOk);
        if (!xOk) {
            setDOMException(exec, TYPE_MISMATCH_ERR);
            return jsUndefined();
        }
        bool yOk;
        int y = args[1]->toInt32(exec, yOk);
        if (!yOk) {
            setDOMException(exec, TYPE_MISMATCH_ERR);
            return jsUndefined();
        }


        KJS::JSValue* result = toJS(exec, WTF::getPtr(imp->elementFromPoint(x, y)));
        return result;
    }
    }
    return 0;
}