1/*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef CSSFontFace_h
27#define CSSFontFace_h
28
29#include "core/css/CSSFontFaceSource.h"
30#include "core/css/FontFace.h"
31#include "wtf/Forward.h"
32#include "wtf/PassRefPtr.h"
33#include "wtf/RefCounted.h"
34#include "wtf/Vector.h"
35
36namespace WebCore {
37
38class CSSSegmentedFontFace;
39class Document;
40class FontDescription;
41class SimpleFontData;
42class StyleRuleFontFace;
43
44// FIXME: Can this be a subclass of FontFace?
45class CSSFontFace : public RefCounted<CSSFontFace> {
46public:
47    static PassRefPtr<CSSFontFace> create(PassRefPtr<FontFace> fontFace) { return adoptRef(new CSSFontFace(fontFace)); }
48    static PassRefPtr<CSSFontFace> createFromStyleRule(Document*, const StyleRuleFontFace*);
49
50    class UnicodeRangeSet;
51
52    ~CSSFontFace();
53
54    FontFace* fontFace() const { return m_fontFace.get(); }
55
56    UnicodeRangeSet& ranges() { return m_ranges; }
57
58    void setSegmentedFontFace(CSSSegmentedFontFace*);
59    void clearSegmentedFontFace() { m_segmentedFontFace = 0; }
60
61    bool isLoaded() const;
62    bool isValid() const;
63
64    void addSource(PassOwnPtr<CSSFontFaceSource>);
65
66    void beginLoadIfNeeded(CSSFontFaceSource*);
67    void fontLoaded(CSSFontFaceSource*);
68
69    PassRefPtr<SimpleFontData> getFontData(const FontDescription&);
70
71    struct UnicodeRange {
72        UnicodeRange(UChar32 from, UChar32 to)
73            : m_from(from)
74            , m_to(to)
75        {
76        }
77
78        UChar32 from() const { return m_from; }
79        UChar32 to() const { return m_to; }
80        bool contains(UChar32 c) const { return m_from <= c && c <= m_to; }
81
82    private:
83        UChar32 m_from;
84        UChar32 m_to;
85    };
86
87    class UnicodeRangeSet {
88    public:
89        void add(UChar32 from, UChar32 to) { m_ranges.append(UnicodeRange(from, to)); }
90        bool intersectsWith(const String&) const;
91        bool isEntireRange() const { return m_ranges.isEmpty(); }
92        size_t size() const { return m_ranges.size(); }
93        const UnicodeRange& rangeAt(size_t i) const { return m_ranges[i]; }
94    private:
95        Vector<UnicodeRange> m_ranges; // If empty, represents the whole code space.
96    };
97
98    FontFace::LoadStatus loadStatus() const { return m_fontFace->loadStatus(); }
99    void willUseFontData(const FontDescription&);
100
101private:
102    CSSFontFace(PassRefPtr<FontFace> fontFace)
103        : m_segmentedFontFace(0)
104        , m_activeSource(0)
105        , m_fontFace(fontFace)
106    {
107        ASSERT(m_fontFace);
108    }
109    void setLoadStatus(FontFace::LoadStatus);
110
111    UnicodeRangeSet m_ranges;
112    CSSSegmentedFontFace* m_segmentedFontFace;
113    Vector<OwnPtr<CSSFontFaceSource> > m_sources;
114    CSSFontFaceSource* m_activeSource;
115    RefPtr<FontFace> m_fontFace;
116};
117
118}
119
120#endif
121