File indexing completed on 2024-04-28 15:22:43

0001 /*
0002  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
0003  * Copyright (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
0004  * Copyright (C) 2009 Germain Garand <germain@ebooksfrance.org>
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
0016  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0018  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
0019  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0020  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0021  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0022  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
0023  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifndef css_webfont_h
0029 #define css_webfont_h
0030 
0031 #include "dom/dom_string.h"
0032 #include "misc/shared.h"
0033 #include "misc/loader.h"
0034 #include "css/css_valueimpl.h"
0035 
0036 #include <wtf/HashMap.h>
0037 #include <wtf/RefPtr.h>
0038 #include <wtf/HashSet.h>
0039 #include <wtf/PassRefPtr.h>
0040 #include <wtf/RefCounted.h>
0041 #include <wtf/Vector.h>
0042 
0043 #include <QHash>
0044 
0045 namespace DOM
0046 {
0047 
0048 class CSSFontFace;
0049 class CSSFontFaceRuleImpl;
0050 class CSSFontSelector;
0051 class CSSSegmentedFontFace;
0052 class DocumentImpl;
0053 class DocLoader;
0054 class FontDef;
0055 
0056 enum {
0057     FontStyleNormalBit = 0,
0058     FontStyleItalicBit,
0059     FontVariantNormalBit,
0060     FontVariantSmallCapsBit,
0061     FontWeight100Bit,
0062     FontWeight200Bit,
0063     FontWeight300Bit,
0064     FontWeight400Bit,
0065     FontWeight500Bit,
0066     FontWeight600Bit,
0067     FontWeight700Bit,
0068     FontWeight800Bit,
0069     FontWeight900Bit,
0070     FontTraitsMaskWidth
0071 };
0072 
0073 enum FontTraitsMask {
0074     FontStyleNormalMask = 1 << FontStyleNormalBit,
0075     FontStyleItalicMask = 1 << FontStyleItalicBit,
0076     FontStyleMask = FontStyleNormalMask | FontStyleItalicMask,
0077 
0078     FontVariantNormalMask = 1 << FontVariantNormalBit,
0079     FontVariantSmallCapsMask = 1 << FontVariantSmallCapsBit,
0080     FontVariantMask = FontVariantNormalMask | FontVariantSmallCapsMask,
0081 
0082     FontWeight100Mask = 1 << FontWeight100Bit,
0083     FontWeight200Mask = 1 << FontWeight200Bit,
0084     FontWeight300Mask = 1 << FontWeight300Bit,
0085     FontWeight400Mask = 1 << FontWeight400Bit,
0086     FontWeight500Mask = 1 << FontWeight500Bit,
0087     FontWeight600Mask = 1 << FontWeight600Bit,
0088     FontWeight700Mask = 1 << FontWeight700Bit,
0089     FontWeight800Mask = 1 << FontWeight800Bit,
0090     FontWeight900Mask = 1 << FontWeight900Bit,
0091     FontWeightMask = FontWeight100Mask | FontWeight200Mask | FontWeight300Mask | FontWeight400Mask | FontWeight500Mask | FontWeight600Mask | FontWeight700Mask | FontWeight800Mask | FontWeight900Mask
0092 };
0093 
0094 class CSSSegmentedFontFace :  public khtml::Shared<CSSSegmentedFontFace>
0095 {
0096 public:
0097     CSSSegmentedFontFace(CSSFontSelector *);
0098     ~CSSSegmentedFontFace();
0099 
0100     bool isLoaded() const;
0101     bool isValid() const;
0102     CSSFontSelector *fontSelector() const
0103     {
0104         return m_fontSelector;
0105     }
0106 
0107     void fontLoaded(CSSFontFace *);
0108 
0109     void appendFontFace(CSSFontFace);
0110 
0111 //    FontData* getFontData(const FontDescription&);
0112 
0113 private:
0114 
0115 //    void pruneTable();
0116 
0117     CSSFontSelector *m_fontSelector;
0118 //    HashMap<unsigned, SegmentedFontData*> m_fontDataTable;
0119 //    WTF::Vector<WTF::RefPtr<CSSFontFace>, 1> m_fontFaces;
0120 };
0121 
0122 class CSSFontFaceSource : public khtml::CachedObjectClient
0123 {
0124 public:
0125     CSSFontFaceSource(const DOMString &, bool distant = false);
0126     virtual ~CSSFontFaceSource();
0127 
0128     bool isLoaded() const;
0129     bool isValid() const;
0130 
0131     DOMString string() const
0132     {
0133         return m_string;
0134     }
0135 
0136     void setFontFace(CSSFontFace *face)
0137     {
0138         m_face = face;
0139     }
0140 
0141     void notifyFinished(khtml::CachedObject *finishedObj) override;
0142     void refLoader();
0143 
0144 //    SimpleFontData* getFontData(const FontDef&, bool syntheticBold, bool syntheticItalic, CSSFontSelector*);
0145 //    void pruneTable();
0146 
0147 #if 0
0148     // ENABLE(SVG_FONTS)
0149     SVGFontFaceElement *svgFontFaceElement() const
0150     {
0151         return m_svgFontFaceElement;
0152     }
0153     void setSVGFontFaceElement(SVGFontFaceElement *element)
0154     {
0155         m_svgFontFaceElement = element;
0156     }
0157 #endif
0158 
0159 private:
0160     DOMString m_string; // URI for remote, built-in font name for local.
0161     khtml::CachedFont *m_font; // For remote fonts, a pointer to our cached resource.
0162     CSSFontFace *m_face; // Our owning font face.
0163     int m_id; // Qt identifier for the Application font.
0164     bool m_refed;
0165     bool m_distant;
0166 //    HashMap<unsigned, SimpleFontData*> m_fontDataTable; // The hash key is composed of size synthetic styles.
0167 
0168 #if 0
0169     // ENABLE(SVG_FONTS)
0170     SVGFontFaceElement *m_svgFontFaceElement;
0171     RefPtr<SVGFontElement> m_externalSVGFontElement;
0172 #endif
0173 };
0174 
0175 class CSSFontFace : public khtml::Shared<CSSFontFace>
0176 {
0177 public:
0178     CSSFontFace(FontTraitsMask traitsMask, CSSFontSelector *fs)
0179         : m_traitsMask(traitsMask), m_fontSelector(fs), m_refed(false), m_installed(false)
0180     {
0181     }
0182     ~CSSFontFace();
0183 
0184     FontTraitsMask traitsMask() const
0185     {
0186         return m_traitsMask;
0187     }
0188 
0189     /*
0190         struct UnicodeRange;
0191 
0192         void addRange(UChar32 from, UChar32 to) { m_ranges.append(UnicodeRange(from, to)); }
0193         const Vector<UnicodeRange>& ranges() const { return m_ranges; }
0194     */
0195     void addedToSegmentedFontFace(CSSSegmentedFontFace *);
0196     void removedFromSegmentedFontFace(CSSSegmentedFontFace *);
0197 
0198     bool isLoaded() const;
0199     bool isValid() const;
0200 
0201     void setInstalled();
0202     bool installed() const;
0203 
0204     void addSource(CSSFontFaceSource *);
0205 
0206     void fontLoaded(CSSFontFaceSource *);
0207     void addFamilyName(const DOMString &name)
0208     {
0209         m_names.append(name);
0210     }
0211     WTF::Vector<DOMString> familyNames() const
0212     {
0213         return m_names;
0214     }
0215     CSSFontSelector *fontSelector() const
0216     {
0217         return m_fontSelector;
0218     }
0219     void refLoaders(); // start loading all sources
0220 
0221 //    SimpleFontData* getFontData(const FontDef&, bool syntheticBold, bool syntheticItalic);
0222 
0223     /*
0224         struct UnicodeRange {
0225             UnicodeRange(UChar32 from, UChar32 to)
0226                 : m_from(from)
0227                 , m_to(to)
0228             {
0229             }
0230 
0231             UChar32 from() const { return m_from; }
0232             UChar32 to() const { return m_to; }
0233 
0234         private:
0235             UChar32 m_from;
0236             UChar32 m_to;
0237         };
0238     */
0239 
0240 private:
0241     FontTraitsMask m_traitsMask;
0242 //    Vector<UnicodeRange> m_ranges;
0243 //    HashSet<CSSSegmentedFontFace*> m_segmentedFontFaces;
0244     WTF::Vector<DOMString> m_names;
0245     WTF::Vector<CSSFontFaceSource *> m_sources;
0246     CSSFontSelector *m_fontSelector;
0247     bool m_refed;
0248     bool m_installed; // Successfully added into application font db
0249 };
0250 
0251 class CSSFontSelector : public khtml::Shared<CSSFontSelector>
0252 {
0253 public:
0254     CSSFontSelector(DocumentImpl *);
0255     virtual ~CSSFontSelector();
0256 
0257 //    virtual FontData* getFontData(const FontDef& fontDescription, const DOMString& familyName);
0258     void requestFamilyName(const DOMString &familyName);
0259     void clearDocument()
0260     {
0261         m_document = nullptr;
0262     }
0263     void addFontFaceRule(const CSSFontFaceRuleImpl *);
0264     void fontLoaded();
0265     virtual void fontCacheInvalidated();
0266     bool isEmpty() const;
0267     khtml::DocLoader *docLoader() const;
0268 
0269 private:
0270 
0271     DocumentImpl *m_document;
0272 //    HashMap<DOMString, Vector<RefPtr<CSSFontFace> >*, CaseFoldingHash> m_fontFaces;
0273 //    WTF::HashMap<DOMString, WTF::Vector<WTF::RefPtr<CSSFontFace> >*, CaseFoldingHash> m_locallyInstalledFontFaces;
0274     QHash<DOMString, CSSFontFace *> m_locallyInstalledFontFaces;
0275 //    HashMap<DOMString, HashMap<unsigned, RefPtr<CSSSegmentedFontFace> >*, CaseFoldingHash> m_fonts;
0276 };
0277 
0278 } // namespace DOM
0279 
0280 #endif // css_webfont_h