1 /*  GRAPHITE2 LICENSING
2 
3     Copyright 2010, SIL International
4     All rights reserved.
5 
6     This library is free software; you can redistribute it and/or modify
7     it under the terms of the GNU Lesser General Public License as published
8     by the Free Software Foundation; either version 2.1 of License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14     Lesser General Public License for more details.
15 
16     You should also have received a copy of the GNU Lesser General Public
17     License along with this library in the file named "LICENSE".
18     If not, write to the Free Software Foundation, 51 Franklin Street,
19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
20     internet at http://www.fsf.org/licenses/lgpl.html.
21 
22 Alternatively, the contents of this file may be used under the terms of the
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
24 License, as published by the Free Software Foundation, either version 2
25 of the License or (at your option) any later version.
26 */
27 #pragma once
28 
29 #include "inc/GlyphFace.h"
30 #include "graphite2/Font.h"
31 
32 namespace graphite2 {
33 
34 class Segment;
35 class Face;
36 class FeatureVal;
37 
38 
39 class GlyphFaceCacheHeader
40 {
41 public:
42     bool initialize(const Face & face, const bool dumb_font);    //return result indicates success. Do not use if failed.
numGlyphs()43     unsigned short numGlyphs() const { return m_nGlyphs; }
numAttrs()44     unsigned short numAttrs() const { return m_numAttrs; }
45 
46 private:
47 friend class Face;
48 friend class GlyphFace;
49     const byte* m_pHead,
50     		  * m_pHHea,
51     		  * m_pHmtx,
52     		  * m_pGlat,
53     		  * m_pGloc,
54     		  * m_pGlyf,
55     		  * m_pLoca;
56     size_t		m_lHmtx,
57     			m_lGlat,
58     			m_lGlyf,
59     			m_lLoca;
60 
61     uint32			m_fGlat;
62     unsigned short 	m_numAttrs,					// number of glyph attributes per glyph
63     				m_nGlyphsWithGraphics,		//i.e. boundary box and advance
64     				m_nGlyphsWithAttributes,
65     				m_nGlyphs;					// number of glyphs in the font. Max of the above 2.
66     bool 			m_locFlagsUse32Bit;
67 };
68 
69 class GlyphFaceCache : public GlyphFaceCacheHeader
70 {
71 public:
72     static GlyphFaceCache* makeCache(const GlyphFaceCacheHeader& hdr /*, EGlyphCacheStrategy requested */);
73 
74     GlyphFaceCache(const GlyphFaceCacheHeader& hdr);
75     ~GlyphFaceCache();
76 
glyphSafe(unsigned short glyphid)77     const GlyphFace *glyphSafe(unsigned short glyphid) const { return glyphid<numGlyphs()?glyph(glyphid):NULL; }
glyphAttr(uint16 gid,uint8 gattr)78     uint16 glyphAttr(uint16 gid, uint8 gattr) const { if (gattr>=numAttrs()) return 0; const GlyphFace*p=glyphSafe(gid); return p?p->getAttr(gattr):0; }
79 
new(size_t s,const GlyphFaceCacheHeader & hdr)80     void * operator new (size_t s, const GlyphFaceCacheHeader& hdr)
81     {
82         return malloc(s + sizeof(GlyphFace*)*hdr.numGlyphs());
83     }
84     // delete in case an exception is thrown in constructor
delete(void * p,const GlyphFaceCacheHeader &)85     void operator delete(void* p, const GlyphFaceCacheHeader& ) throw()
86     {
87         free(p);
88     }
89 
90     const GlyphFace *glyph(unsigned short glyphid) const;      //result may be changed by subsequent call with a different glyphid
91     void loadAllGlyphs();
92 
93     CLASS_NEW_DELETE
94 
95 private:
glyphPtrDirect(unsigned short glyphid)96     GlyphFace **glyphPtrDirect(unsigned short glyphid) const { return (GlyphFace **)((const char*)(this)+sizeof(GlyphFaceCache)+sizeof(GlyphFace*)*glyphid);}
97 
98 private:      //defensive
99     GlyphFaceCache(const GlyphFaceCache&);
100     GlyphFaceCache& operator=(const GlyphFaceCache&);
101 };
102 
103 } // namespace graphite2
104