15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
2f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * Copyright (C) 2013 Apple Inc. All rights reserved.
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#ifndef HTMLSrcsetParser_h
3306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#define HTMLSrcsetParser_h
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
3506f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#include "wtf/text/WTFString.h"
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
37c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
39f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)enum { UninitializedDescriptor = -1 };
406f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
41f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)class DescriptorParsingResult {
42f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)public:
436f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    DescriptorParsingResult()
44f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        : m_density(UninitializedDescriptor)
45f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        , m_resourceWidth(UninitializedDescriptor)
46f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        , m_resourceHeight(UninitializedDescriptor)
476f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    {
486f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    }
496f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
50f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    bool hasDensity() const { return m_density >= 0; }
51f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    bool hasWidth() const { return m_resourceWidth >= 0; }
52f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    bool hasHeight() const { return m_resourceHeight >= 0; }
53f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
54f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    float density() const { ASSERT(hasDensity()); return m_density; }
55f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    unsigned resourceWidth() const { ASSERT(hasWidth()); return m_resourceWidth; }
56f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    unsigned resourceHeight() const { ASSERT(hasHeight()); return m_resourceHeight; }
57f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
58f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    void setResourceWidth(int width) { ASSERT(width >= 0); m_resourceWidth = (unsigned)width; }
59f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    void setResourceHeight(int height) { ASSERT(height >= 0); m_resourceHeight = (unsigned)height; }
60f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    void setDensity(float densityToSet) { ASSERT(densityToSet >= 0); m_density = densityToSet; }
61f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
62f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)private:
63f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    float m_density;
64f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    int m_resourceWidth;
65f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    int m_resourceHeight;
666f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch};
676f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
6806f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)class ImageCandidate {
699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)public:
706f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    enum OriginAttribute {
716f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        SrcsetOrigin,
726f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        SrcOrigin
736f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    };
746f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
7506f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    ImageCandidate()
76f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        : m_density(1.0)
775d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        , m_resourceWidth(UninitializedDescriptor)
785d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        , m_originAttribute(SrcsetOrigin)
799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
826f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    ImageCandidate(const String& source, unsigned start, unsigned length, const DescriptorParsingResult& result, OriginAttribute originAttribute)
8306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)        : m_string(source.createView(start, length))
84f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        , m_density(result.hasDensity()?result.density():UninitializedDescriptor)
85f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        , m_resourceWidth(result.hasWidth()?result.resourceWidth():UninitializedDescriptor)
866f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        , m_originAttribute(originAttribute)
879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
8906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
9006f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    String toString() const
9106f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    {
9209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return String(m_string.toString());
9309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
95d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    AtomicString url() const
9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    {
9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return AtomicString(m_string.toString());
9806f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    }
9906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
100f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    void setDensity(float factor)
1016f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    {
102f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        m_density = factor;
1036f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    }
1046f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
105f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    float density() const
1069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
107f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        return m_density;
1089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
10906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
1106f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    int resourceWidth() const
1116f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    {
1126f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        return m_resourceWidth;
1136f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    }
1146f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
1156f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    bool srcOrigin() const
1166f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    {
1176f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        return (m_originAttribute == SrcOrigin);
1186f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    }
1196f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
12006f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    inline bool isEmpty() const
12106f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    {
12206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)        return m_string.isEmpty();
12306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    }
12406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
1259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)private:
12606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    StringView m_string;
127f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    float m_density;
1286f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    int m_resourceWidth;
1296f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    OriginAttribute m_originAttribute;
1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
1319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13210f88d5669dbd969c059d61ba09fa37dd72ac559Ben MurdochImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, unsigned sourceSize, const String& srcsetAttribute);
13306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
13410f88d5669dbd969c059d61ba09fa37dd72ac559Ben MurdochImageCandidate bestFitSourceForImageAttributes(float deviceScaleFactor, unsigned sourceSize, const String& srcAttribute, const String& srcsetAttribute);
13506f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
13610f88d5669dbd969c059d61ba09fa37dd72ac559Ben MurdochString bestFitSourceForImageAttributes(float deviceScaleFactor, unsigned sourceSize, const String& srcAttribute, ImageCandidate& srcsetImageCandidate);
13706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
13806f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)}
1399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)#endif
141