1 /*
2 * This file is part of the internal font implementation. It should not be included by anyone other than
3 * FontMac.cpp, FontWin.cpp and Font.cpp.
4 *
5 * Copyright 2009, The Android Open Source Project
6 * Copyright (C) 2006 Apple Computer, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *
23 */
24
25 #include "config.h"
26 #include "FontPlatformData.h"
27
28 #include "SkPaint.h"
29 #include "SkTypeface.h"
30
31 //#define TRACE_FONTPLATFORMDATA_LIFE
32 //#define COUNT_FONTPLATFORMDATA_LIFE
33
34 #ifdef COUNT_FONTPLATFORMDATA_LIFE
35 static int gCount;
36 static int gMaxCount;
37
inc_count()38 static void inc_count()
39 {
40 if (++gCount > gMaxCount)
41 {
42 gMaxCount = gCount;
43 SkDebugf("---------- FontPlatformData %d\n", gMaxCount);
44 }
45 }
46
dec_count()47 static void dec_count() { --gCount; }
48 #else
49 #define inc_count()
50 #define dec_count()
51 #endif
52
53 #ifdef TRACE_FONTPLATFORMDATA_LIFE
54 #define trace(num) SkDebugf("FontPlatformData%d %p %g %d %d\n", num, mTypeface, mTextSize, mFakeBold, mFakeItalic)
55 #else
56 #define trace(num)
57 #endif
58
59 namespace WebCore {
60
FontPlatformData()61 FontPlatformData::FontPlatformData()
62 : mTypeface(NULL), mTextSize(0), mFakeBold(false), mFakeItalic(false)
63 {
64 inc_count();
65 trace(1);
66 }
67
FontPlatformData(const FontPlatformData & src)68 FontPlatformData::FontPlatformData(const FontPlatformData& src)
69 {
70 if (hashTableDeletedFontValue() != src.mTypeface) {
71 src.mTypeface->safeRef();
72 }
73
74 mTypeface = src.mTypeface;
75
76 mTextSize = src.mTextSize;
77 mFakeBold = src.mFakeBold;
78 mFakeItalic = src.mFakeItalic;
79
80 inc_count();
81 trace(2);
82 }
83
FontPlatformData(SkTypeface * tf,float textSize,bool fakeBold,bool fakeItalic)84 FontPlatformData::FontPlatformData(SkTypeface* tf, float textSize, bool fakeBold, bool fakeItalic)
85 : mTypeface(tf), mTextSize(textSize), mFakeBold(fakeBold), mFakeItalic(fakeItalic)
86 {
87 if (hashTableDeletedFontValue() != mTypeface) {
88 mTypeface->safeRef();
89 }
90
91 inc_count();
92 trace(3);
93 }
94
FontPlatformData(const FontPlatformData & src,float textSize)95 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
96 : mTypeface(src.mTypeface), mTextSize(textSize), mFakeBold(src.mFakeBold), mFakeItalic(src.mFakeItalic)
97 {
98 if (hashTableDeletedFontValue() != mTypeface) {
99 mTypeface->safeRef();
100 }
101
102 inc_count();
103 trace(4);
104 }
105
~FontPlatformData()106 FontPlatformData::~FontPlatformData()
107 {
108 dec_count();
109 #ifdef TRACE_FONTPLATFORMDATA_LIFE
110 SkDebugf("----------- ~FontPlatformData\n");
111 #endif
112
113 if (hashTableDeletedFontValue() != mTypeface) {
114 mTypeface->safeUnref();
115 }
116 }
117
operator =(const FontPlatformData & src)118 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
119 {
120 if (hashTableDeletedFontValue() != src.mTypeface) {
121 src.mTypeface->safeRef();
122 }
123 if (hashTableDeletedFontValue() != mTypeface) {
124 mTypeface->safeUnref();
125 }
126
127 mTypeface = src.mTypeface;
128 mTextSize = src.mTextSize;
129 mFakeBold = src.mFakeBold;
130 mFakeItalic = src.mFakeItalic;
131
132 return *this;
133 }
134
setupPaint(SkPaint * paint) const135 void FontPlatformData::setupPaint(SkPaint* paint) const
136 {
137 float ts = mTextSize;
138 if (!(ts > 0))
139 ts = 12;
140
141 paint->setAntiAlias(true);
142 paint->setSubpixelText(true);
143 paint->setTextSize(SkFloatToScalar(ts));
144 paint->setTypeface(mTypeface);
145 paint->setFakeBoldText(mFakeBold);
146 paint->setTextSkewX(mFakeItalic ? -SK_Scalar1/4 : 0);
147 paint->setTextEncoding(SkPaint::kUTF16_TextEncoding);
148 }
149
operator ==(const FontPlatformData & a) const150 bool FontPlatformData::operator==(const FontPlatformData& a) const
151 {
152 return mTypeface == a.mTypeface &&
153 mTextSize == a.mTextSize &&
154 mFakeBold == a.mFakeBold &&
155 mFakeItalic == a.mFakeItalic;
156 }
157
hash() const158 unsigned FontPlatformData::hash() const
159 {
160 unsigned h;
161
162 if (hashTableDeletedFontValue() == mTypeface) {
163 h = reinterpret_cast<unsigned>(mTypeface);
164 } else {
165 h = SkTypeface::UniqueID(mTypeface);
166 }
167
168 uint32_t sizeAsInt = *reinterpret_cast<const uint32_t*>(&mTextSize);
169
170 h ^= 0x01010101 * (((int)mFakeBold << 1) | (int)mFakeItalic);
171 h ^= sizeAsInt;
172 return h;
173 }
174
175 }
176
177