Merge with MPC-HC 6d1472b2f18266d92e5bc068667de348c0cd3b3b.
[xy_vsfilter.git] / src / subtitles / libssf / Renderer.h
blobb126feaadff23ba96760f188cfeff9e691d41fe2
1 /*
2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #pragma once
24 #include "StringMap.h"
25 #include "Glyph.h"
27 namespace ssf
29 template<class T>
30 class Cache
32 protected:
33 StringMapW<T> m_key2obj;
34 CAtlList<CStringW> m_objs;
35 size_t m_limit;
37 public:
38 Cache(size_t limit) {m_limit = max(1, limit);}
39 virtual ~Cache() {RemoveAll();}
41 void RemoveAll()
43 POSITION pos = m_key2obj.GetStartPosition();
44 while(pos) delete m_key2obj.GetNextValue(pos);
45 m_key2obj.RemoveAll();
46 m_objs.RemoveAll();
49 void Add(const CStringW& key, T& obj, bool fFlush = true)
51 if(StringMapW<T>::CPair* p = m_key2obj.Lookup(key)) delete p->m_value;
52 else m_objs.AddTail(key);
54 m_key2obj[key] = obj;
56 if(fFlush) Flush();
59 void Flush()
61 while(m_objs.GetCount() > m_limit)
63 CStringW key = m_objs.RemoveHead();
64 ASSERT(m_key2obj.Lookup(key));
65 delete m_key2obj[key];
66 m_key2obj.RemoveKey(key);
70 bool Lookup(const CStringW& key, T& val)
72 return m_key2obj.Lookup(key, val) && val;
75 void Invalidate(const CStringW& key)
77 T val;
78 if(m_key2obj.Lookup(key, val))
80 delete val;
81 m_key2obj[key] = NULL;
86 class FontCache : public Cache<FontWrapper*>
88 public:
89 FontCache() : Cache(20) {}
90 FontWrapper* Create(HDC hDC, const LOGFONT& lf);
93 class GlyphPathCache : public Cache<GlyphPath*>
95 public:
96 GlyphPathCache() : Cache(100) {}
97 GlyphPath* Create(HDC hDC, const FontWrapper* f, WCHAR c);
100 class Row : public CAutoPtrList<Glyph>
102 public:
103 int ascent, descent, border, width;
104 Row() {ascent = descent = border = width = 0;}
107 class RenderedSubtitle
109 public:
110 CRect m_spdrc;
111 CRect m_clip;
112 CAutoPtrList<Glyph> m_glyphs;
114 RenderedSubtitle(const CRect& spdrc, const CRect& clip) : m_spdrc(spdrc), m_clip(clip) {}
115 virtual ~RenderedSubtitle() {}
117 CRect Draw(SubPicDesc& spd) const;
120 class RenderedSubtitleCache : public Cache<RenderedSubtitle*>
122 public:
123 RenderedSubtitleCache() : Cache(10) {}
126 class SubRect
128 public:
129 CRect rect;
130 float layer;
131 SubRect() {}
132 SubRect(const CRect& r, float l) : rect(r), layer(l) {}
135 class SubRectAllocator : public StringMapW<SubRect>
137 CSize vs;
138 CRect vr;
139 public:
140 void UpdateTarget(const CSize& vs, const CRect& vr);
141 void GetRect(CRect& rect, const Subtitle* s, const Align& align, int tlb, int brb);
144 class Renderer
146 HDC m_hDC;
148 FontCache m_fc;
149 GlyphPathCache m_gpc;
150 RenderedSubtitleCache m_rsc;
151 SubRectAllocator m_sra;
153 public:
154 Renderer();
155 virtual ~Renderer();
157 void NextSegment(const CAutoPtrList<Subtitle>& subs);
158 RenderedSubtitle* Lookup(const Subtitle* s, const CSize& vs, const CRect& vr);