1/*
2
3PDF Writer printer driver.
4
5Copyright (c) 2001 OpenBeOS.
6
7Authors:
8	Philippe Houdoin
9	Simon Gauvin
10	Michael Pfeiffer
11
12Permission is hereby granted, free of charge, to any person obtaining a copy of
13this software and associated documentation files (the "Software"), to deal in
14the Software without restriction, including without limitation the rights to
15use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16of the Software, and to permit persons to whom the Software is furnished to do
17so, subject to the following conditions:
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28THE SOFTWARE.
29
30*/
31
32#ifndef FONTS_H
33#define FONTS_H
34
35#include <Archivable.h>
36#include <String.h>
37#include <List.h>
38#include "PrintUtils.h"
39
40enum font_encoding
41{
42	macroman_encoding,
43	// TrueType
44	tt_encoding0,
45	tt_encoding1,
46	tt_encoding2,
47	tt_encoding3,
48	tt_encoding4,
49	// Type 1
50	t1_encoding0,
51	t1_encoding1,
52	t1_encoding2,
53	t1_encoding3,
54	t1_encoding4,
55	// CJK
56	japanese_encoding,
57	chinese_cns1_encoding,
58	chinese_gb1_encoding,
59	korean_encoding,
60	first_cjk_encoding  = japanese_encoding,
61	no_of_cjk_encodings = korean_encoding - first_cjk_encoding + 1,
62	invalid_encoding = korean_encoding + 1,
63	user_defined_encoding_start
64};
65
66
67enum font_type
68{
69	true_type_type,
70	type1_type,
71	unknown_type
72};
73
74
75class FontFile : public BArchivable
76{
77private:
78	BString   fName;
79	BString   fPath;
80	int64     fSize;
81	font_type fType;
82	bool      fEmbed;
83	BString   fSubst;
84
85public:
86	FontFile() { }
87	FontFile(const char *n, const char *p, int64 s, font_type t, bool embed) : fName(n), fPath(p), fSize(s), fType(t), fEmbed(embed) { }
88	FontFile(BMessage *archive);
89	~FontFile() {};
90
91	static BArchivable* Instantiate(BMessage *archive);
92	status_t Archive(BMessage *archive, bool deep = true) const;
93
94	// accessors
95	const char*		Name() const  { return fName.String(); }
96	const char*		Path() const  { return fPath.String(); }
97	int64			Size() const  { return fSize; }
98	font_type		Type() const  { return fType; }
99	bool			Embed() const { return fEmbed; }
100	const char*     Subst() const { return fSubst.String(); }
101
102	// setters
103	void SetEmbed(bool embed)        { fEmbed = embed; }
104	void SetSubst(const char* subst) { fSubst = subst; }
105};
106
107
108class Fonts : public BArchivable {
109private:
110	TList<FontFile>  fFontFiles;
111
112	struct {
113		font_encoding encoding;
114		bool          active;
115	} fCJKOrder[no_of_cjk_encodings];
116
117	status_t	LookupFontFiles(BPath path);
118
119public:
120	Fonts();
121	Fonts(BMessage *archive);
122
123	static BArchivable* Instantiate(BMessage *archive);
124	status_t Archive(BMessage *archive, bool deep = true) const;
125
126	status_t	CollectFonts();
127	void        SetTo(BMessage *archive);
128
129	FontFile*	At(int i) const { return fFontFiles.ItemAt(i); }
130	int32		Length() const  { return fFontFiles.CountItems(); }
131
132	void        SetDefaultCJKOrder();
133	bool        SetCJKOrder(int i, font_encoding  enc, bool  active);
134	bool        GetCJKOrder(int i, font_encoding& enc, bool& active) const;
135};
136
137class UsedFont {
138private:
139	BString fFamily;
140	BString fStyle;
141	float   fSize;
142
143public:
144	UsedFont(const char* family, const char* style, float size) : fFamily(family), fStyle(style), fSize(size) {}
145
146	const char* GetFamily() const { return fFamily.String(); }
147	const char* GetStyle() const { return fStyle.String(); }
148	float GetSize() const { return fSize; }
149
150	bool Equals(const char* family, const char* style, float size) const { return strcmp(GetFamily(), family) == 0 && strcmp(GetStyle(), style) == 0 && fSize == size; }
151};
152
153
154class UserDefinedEncodings {
155public:
156	UserDefinedEncodings();
157	// returns true if new encoding and index pair
158	bool Get(uint16 unicode, uint8 &encoding, uint8 &index);
159
160private:
161	bool IsUsed(uint16 unicode) const { return (fUsedMask[unicode / 8] & (1 << (unicode % 8))) != 0; }
162	void SetUsed(uint16 unicode) { fUsedMask[unicode / 8] |= 1 << (unicode % 8); }
163
164	uint8 fUsedMask[65536/8]; // exists an encoding and index for this code point?
165	uint8 fEncoding[65536];   // the encoding for each code point
166	uint8 fIndex[65536];      // the index for each code point
167
168	uint8 fCurrentEncoding;
169	uint8 fCurrentIndex;
170};
171
172#endif // FONTS_H
173