Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: core/fxge/ge/cfx_facecache.cpp

Issue 2263623002: Refactor fx_font part 3 (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fxge/include/cfx_facecache.h"
8
9 #include "core/fxge/ge/fx_text_int.h"
10 #include "core/fxge/include/cfx_fontmgr.h"
11 #include "core/fxge/include/cfx_gemodule.h"
12 #include "core/fxge/include/cfx_pathdata.h"
13 #include "core/fxge/include/fx_freetype.h"
14
15 #ifdef _SKIA_SUPPORT_
16 #include "third_party/skia/include/core/SkStream.h"
17 #include "third_party/skia/include/core/SkTypeface.h"
18 #endif
19
20 namespace {
21
22 static void GammaAdjust(uint8_t* pData,
Lei Zhang 2016/08/19 18:24:46 No need for static inside anonymous namespace.
npm 2016/08/19 22:02:32 Done.
23 int nWid,
Lei Zhang 2016/08/19 18:24:46 Can we name these nWidth/nHeight?
Lei Zhang 2016/08/19 18:24:46 Maybe delete the unused parameters?
npm 2016/08/19 22:02:32 Done.
npm 2016/08/19 22:02:32 Done.
24 int nHei,
25 int src_pitch,
26 const uint8_t* gammaTable) {
27 int count = nHei * src_pitch;
28 for (int i = 0; i < count; i++)
29 pData[i] = gammaTable[pData[i]];
30 }
31
32 static void ContrastAdjust(uint8_t* pDataIn,
33 uint8_t* pDataOut,
34 int nWid,
35 int nHei,
36 int nSrcRowBytes,
37 int nDstRowBytes) {
38 int col, row, temp;
Lei Zhang 2016/08/19 18:24:46 1 decl per line
npm 2016/08/19 22:02:32 Done.
39 int max = 0, min = 255;
40 FX_FLOAT rate;
41 for (row = 0; row < nHei; row++) {
42 uint8_t* pRow = pDataIn + row * nSrcRowBytes;
43 for (col = 0; col < nWid; col++) {
44 temp = *pRow++;
45 if (temp > max)
Lei Zhang 2016/08/19 18:24:46 use std::min/max.
npm 2016/08/19 22:02:32 Done.
46 max = temp;
47 if (temp < min)
48 min = temp;
49 }
50 }
51 temp = max - min;
52 if (0 == temp || 255 == temp) {
Lei Zhang 2016/08/19 18:24:46 Flip to temp == 0
npm 2016/08/19 22:02:32 Done.
53 int rowbytes = FXSYS_abs(nSrcRowBytes) > nDstRowBytes
Lei Zhang 2016/08/19 18:24:46 std::min
npm 2016/08/19 22:02:32 Done.
54 ? nDstRowBytes
55 : FXSYS_abs(nSrcRowBytes);
56 for (row = 0; row < nHei; row++) {
57 FXSYS_memcpy(pDataOut + row * nDstRowBytes, pDataIn + row * nSrcRowBytes,
58 rowbytes);
59 }
60 return;
61 }
62 rate = 255.f / temp;
63 for (row = 0; row < nHei; row++) {
64 uint8_t* pSrcRow = pDataIn + row * nSrcRowBytes;
65 uint8_t* pDstRow = pDataOut + row * nDstRowBytes;
66 for (col = 0; col < nWid; col++) {
67 temp = (int)((*(pSrcRow++) - min) * rate + 0.5);
Lei Zhang 2016/08/19 18:24:46 Get rid of C-style casts.
npm 2016/08/19 22:02:32 Done.
68 if (temp > 255)
Lei Zhang 2016/08/19 18:24:46 More std::min/max
npm 2016/08/19 22:02:33 Done.
69 temp = 255;
70 else if (temp < 0)
71 temp = 0;
72 *pDstRow++ = (uint8_t)temp;
73 }
74 }
75 }
76 } // namespace
77
78 CFX_FaceCache::CFX_FaceCache(FXFT_Face face)
79 : m_Face(face)
80 #ifdef _SKIA_SUPPORT_
81 ,
82 m_pTypeface(nullptr)
83 #endif
84 {
85 }
86
87 CFX_FaceCache::~CFX_FaceCache() {
88 for (const auto& pair : m_SizeMap)
Lei Zhang 2016/08/19 18:24:46 Should |m_SizeMap| and |m_PathMap| hold std:unique
npm 2016/08/19 22:02:32 Done.
89 delete pair.second;
90 m_SizeMap.clear();
91 for (const auto& pair : m_PathMap)
92 delete pair.second;
93 m_PathMap.clear();
94 #ifdef _SKIA_SUPPORT_
95 SkSafeUnref(m_pTypeface);
96 #endif
97 }
98
99 CFX_GlyphBitmap* CFX_FaceCache::RenderGlyph(CFX_Font* pFont,
100 uint32_t glyph_index,
101 FX_BOOL bFontStyle,
102 const CFX_Matrix* pMatrix,
103 int dest_width,
104 int anti_alias) {
105 if (!m_Face)
106 return nullptr;
Lei Zhang 2016/08/19 18:24:46 blank line after
npm 2016/08/19 22:02:33 Done.
107 FXFT_Matrix ft_matrix;
108 ft_matrix.xx = (signed long)(pMatrix->GetA() / 64 * 65536);
109 ft_matrix.xy = (signed long)(pMatrix->GetC() / 64 * 65536);
110 ft_matrix.yx = (signed long)(pMatrix->GetB() / 64 * 65536);
111 ft_matrix.yy = (signed long)(pMatrix->GetD() / 64 * 65536);
112 FX_BOOL bUseCJKSubFont = FALSE;
Lei Zhang 2016/08/19 18:24:46 bool
npm 2016/08/19 22:02:32 Done.
113 const CFX_SubstFont* pSubstFont = pFont->GetSubstFont();
114 if (pSubstFont) {
115 bUseCJKSubFont = pSubstFont->m_bSubstCJK && bFontStyle;
116 int skew = 0;
117 if (bUseCJKSubFont)
118 skew = pSubstFont->m_bItalicCJK ? -15 : 0;
119 else
120 skew = pSubstFont->m_ItalicAngle;
121 if (skew) {
122 // |skew| is nonpositive so |-skew| is used as the index. We need to make
123 // sure |skew| != INT_MIN since -INT_MIN is undefined.
124 if (skew <= 0 && skew != std::numeric_limits<int>::min() &&
125 static_cast<size_t>(-skew) < CFX_Font::ANGLESKEW_ARRAY_SIZE) {
126 skew = -CFX_Font::g_AngleSkew[-skew];
127 } else {
128 skew = -58;
129 }
130 if (pFont->IsVertical())
131 ft_matrix.yx += ft_matrix.yy * skew / 100;
132 else
133 ft_matrix.xy -= ft_matrix.xx * skew / 100;
134 }
135 if (pSubstFont->m_SubstFlags & FXFONT_SUBST_MM) {
136 pFont->AdjustMMParams(glyph_index, dest_width,
137 pFont->GetSubstFont()->m_Weight);
138 }
139 }
140 ScopedFontTransform scoped_transform(m_Face, &ft_matrix);
141 int load_flags = (m_Face->face_flags & FT_FACE_FLAG_SFNT)
142 ? FXFT_LOAD_NO_BITMAP
143 : (FXFT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING);
144 int error = FXFT_Load_Glyph(m_Face, glyph_index, load_flags);
145 if (error) {
146 // if an error is returned, try to reload glyphs without hinting.
147 if (load_flags & FT_LOAD_NO_HINTING || load_flags & FT_LOAD_NO_SCALE)
148 return nullptr;
149
150 load_flags |= FT_LOAD_NO_HINTING;
151 error = FXFT_Load_Glyph(m_Face, glyph_index, load_flags);
152
153 if (error)
154 return nullptr;
155 }
156 int weight = 0;
157 if (bUseCJKSubFont)
158 weight = pSubstFont->m_WeightCJK;
159 else
160 weight = pSubstFont ? pSubstFont->m_Weight : 0;
161 if (pSubstFont && !(pSubstFont->m_SubstFlags & FXFONT_SUBST_MM) &&
162 weight > 400) {
163 uint32_t index = (weight - 400) / 10;
164 if (index >= CFX_Font::WEIGHTPOW_ARRAY_SIZE)
165 return nullptr;
166 int level = 0;
167 if (pSubstFont->m_Charset == FXFONT_SHIFTJIS_CHARSET) {
168 level =
169 CFX_Font::g_WeightPow_SHIFTJIS[index] * 2 *
170 (FXSYS_abs((int)(ft_matrix.xx)) + FXSYS_abs((int)(ft_matrix.xy))) /
171 36655;
172 } else {
173 level =
174 CFX_Font::g_WeightPow_11[index] *
175 (FXSYS_abs((int)(ft_matrix.xx)) + FXSYS_abs((int)(ft_matrix.xy))) /
176 36655;
177 }
178 FXFT_Outline_Embolden(FXFT_Get_Glyph_Outline(m_Face), level);
179 }
180 FXFT_Library_SetLcdFilter(CFX_GEModule::Get()->GetFontMgr()->GetFTLibrary(),
181 FT_LCD_FILTER_DEFAULT);
182 error = FXFT_Render_Glyph(m_Face, anti_alias);
183 if (error)
184 return nullptr;
185 int bmwidth = FXFT_Get_Bitmap_Width(FXFT_Get_Glyph_Bitmap(m_Face));
186 int bmheight = FXFT_Get_Bitmap_Rows(FXFT_Get_Glyph_Bitmap(m_Face));
187 if (bmwidth > 2048 || bmheight > 2048)
188 return nullptr;
189 int dib_width = bmwidth;
190 CFX_GlyphBitmap* pGlyphBitmap = new CFX_GlyphBitmap;
191 pGlyphBitmap->m_Bitmap.Create(
192 dib_width, bmheight,
193 anti_alias == FXFT_RENDER_MODE_MONO ? FXDIB_1bppMask : FXDIB_8bppMask);
194 pGlyphBitmap->m_Left = FXFT_Get_Glyph_BitmapLeft(m_Face);
195 pGlyphBitmap->m_Top = FXFT_Get_Glyph_BitmapTop(m_Face);
196 int dest_pitch = pGlyphBitmap->m_Bitmap.GetPitch();
197 int src_pitch = FXFT_Get_Bitmap_Pitch(FXFT_Get_Glyph_Bitmap(m_Face));
198 uint8_t* pDestBuf = pGlyphBitmap->m_Bitmap.GetBuffer();
199 uint8_t* pSrcBuf =
200 (uint8_t*)FXFT_Get_Bitmap_Buffer(FXFT_Get_Glyph_Bitmap(m_Face));
201 if (anti_alias != FXFT_RENDER_MODE_MONO &&
202 FXFT_Get_Bitmap_PixelMode(FXFT_Get_Glyph_Bitmap(m_Face)) ==
203 FXFT_PIXEL_MODE_MONO) {
204 int bytes = anti_alias == FXFT_RENDER_MODE_LCD ? 3 : 1;
205 for (int i = 0; i < bmheight; i++) {
206 for (int n = 0; n < bmwidth; n++) {
207 uint8_t data =
208 (pSrcBuf[i * src_pitch + n / 8] & (0x80 >> (n % 8))) ? 255 : 0;
209 for (int b = 0; b < bytes; b++)
210 pDestBuf[i * dest_pitch + n * bytes + b] = data;
211 }
212 }
213 } else {
214 FXSYS_memset(pDestBuf, 0, dest_pitch * bmheight);
215 if (anti_alias == FXFT_RENDER_MODE_MONO &&
216 FXFT_Get_Bitmap_PixelMode(FXFT_Get_Glyph_Bitmap(m_Face)) ==
217 FXFT_PIXEL_MODE_MONO) {
218 int rowbytes =
219 FXSYS_abs(src_pitch) > dest_pitch ? dest_pitch : FXSYS_abs(src_pitch);
220 for (int row = 0; row < bmheight; row++) {
221 FXSYS_memcpy(pDestBuf + row * dest_pitch, pSrcBuf + row * src_pitch,
222 rowbytes);
223 }
224 } else {
225 ContrastAdjust(pSrcBuf, pDestBuf, bmwidth, bmheight, src_pitch,
226 dest_pitch);
227 GammaAdjust(pDestBuf, bmwidth, bmheight, dest_pitch,
228 CFX_GEModule::Get()->GetTextGammaTable());
229 }
230 }
231 return pGlyphBitmap;
232 }
233
234 const CFX_PathData* CFX_FaceCache::LoadGlyphPath(CFX_Font* pFont,
235 uint32_t glyph_index,
236 int dest_width) {
237 if (!m_Face || glyph_index == (uint32_t)-1)
238 return nullptr;
239
240 uint32_t key = glyph_index;
241 if (pFont->GetSubstFont()) {
242 key += (((pFont->GetSubstFont()->m_Weight / 16) << 15) +
243 ((pFont->GetSubstFont()->m_ItalicAngle / 2) << 21) +
244 ((dest_width / 16) << 25) + (pFont->IsVertical() << 31));
245 }
246 auto it = m_PathMap.find(key);
247 if (it != m_PathMap.end())
248 return it->second;
249
250 CFX_PathData* pGlyphPath = pFont->LoadGlyphPath(glyph_index, dest_width);
251 m_PathMap[key] = pGlyphPath;
252 return pGlyphPath;
253 }
254
255 const CFX_GlyphBitmap* CFX_FaceCache::LoadGlyphBitmap(CFX_Font* pFont,
256 uint32_t glyph_index,
257 FX_BOOL bFontStyle,
258 const CFX_Matrix* pMatrix,
259 int dest_width,
260 int anti_alias,
261 int& text_flags) {
262 if (glyph_index == (uint32_t)-1)
263 return nullptr;
264 _CFX_UniqueKeyGen keygen;
265 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
266 if (pFont->GetSubstFont())
267 keygen.Generate(9, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
Lei Zhang 2016/08/19 18:24:46 Let's calculate the matrix values just once...
npm 2016/08/19 22:02:32 Done.
268 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
269 dest_width, anti_alias, pFont->GetSubstFont()->m_Weight,
270 pFont->GetSubstFont()->m_ItalicAngle, pFont->IsVertical());
271 else
272 keygen.Generate(6, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
273 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
274 dest_width, anti_alias);
275 #else
276 if (text_flags & FXTEXT_NO_NATIVETEXT) {
277 if (pFont->GetSubstFont())
278 keygen.Generate(9, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
279 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
280 dest_width, anti_alias, pFont->GetSubstFont()->m_Weight,
281 pFont->GetSubstFont()->m_ItalicAngle,
282 pFont->IsVertical());
283 else
284 keygen.Generate(6, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
285 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
286 dest_width, anti_alias);
287 } else {
288 if (pFont->GetSubstFont())
289 keygen.Generate(10, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
290 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
291 dest_width, anti_alias, pFont->GetSubstFont()->m_Weight,
292 pFont->GetSubstFont()->m_ItalicAngle, pFont->IsVertical(),
293 3);
294 else
295 keygen.Generate(7, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
296 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
297 dest_width, anti_alias, 3);
298 }
299 #endif
300 CFX_ByteString FaceGlyphsKey(keygen.m_Key, keygen.m_KeyLen);
301 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_ || defined _SKIA_SUPPORT_
302 return LookUpGlyphBitmap(pFont, pMatrix, FaceGlyphsKey, glyph_index,
303 bFontStyle, dest_width, anti_alias);
304 #else
305 if (text_flags & FXTEXT_NO_NATIVETEXT) {
306 return LookUpGlyphBitmap(pFont, pMatrix, FaceGlyphsKey, glyph_index,
307 bFontStyle, dest_width, anti_alias);
308 }
309 CFX_GlyphBitmap* pGlyphBitmap;
310 auto it = m_SizeMap.find(FaceGlyphsKey);
311 if (it != m_SizeMap.end()) {
312 CFX_SizeGlyphCache* pSizeCache = it->second;
313 auto it2 = pSizeCache->m_GlyphMap.find(glyph_index);
314 if (it2 != pSizeCache->m_GlyphMap.end())
315 return it2->second;
316
317 pGlyphBitmap = RenderGlyph_Nativetext(pFont, glyph_index, pMatrix,
318 dest_width, anti_alias);
319 if (pGlyphBitmap) {
320 pSizeCache->m_GlyphMap[glyph_index] = pGlyphBitmap;
321 return pGlyphBitmap;
322 }
323 } else {
324 pGlyphBitmap = RenderGlyph_Nativetext(pFont, glyph_index, pMatrix,
325 dest_width, anti_alias);
326 if (pGlyphBitmap) {
327 CFX_SizeGlyphCache* pSizeCache = new CFX_SizeGlyphCache;
328 m_SizeMap[FaceGlyphsKey] = pSizeCache;
329 pSizeCache->m_GlyphMap[glyph_index] = pGlyphBitmap;
330 return pGlyphBitmap;
331 }
332 }
333 if (pFont->GetSubstFont())
334 keygen.Generate(9, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
335 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
336 dest_width, anti_alias, pFont->GetSubstFont()->m_Weight,
337 pFont->GetSubstFont()->m_ItalicAngle, pFont->IsVertical());
338 else
339 keygen.Generate(6, (int)(pMatrix->a * 10000), (int)(pMatrix->b * 10000),
340 (int)(pMatrix->c * 10000), (int)(pMatrix->d * 10000),
341 dest_width, anti_alias);
342 CFX_ByteString FaceGlyphsKey2(keygen.m_Key, keygen.m_KeyLen);
343 text_flags |= FXTEXT_NO_NATIVETEXT;
344 return LookUpGlyphBitmap(pFont, pMatrix, FaceGlyphsKey2, glyph_index,
345 bFontStyle, dest_width, anti_alias);
346 #endif
347 }
348
349 #ifdef _SKIA_SUPPORT_
350 CFX_TypeFace* CFX_FaceCache::GetDeviceCache(CFX_Font* pFont) {
351 if (!m_pTypeface) {
352 m_pTypeface =
353 SkTypeface::MakeFromStream(
354 new SkMemoryStream(pFont->GetFontData(), pFont->GetSize()))
355 .release();
356 }
357 return m_pTypeface;
358 }
359 #endif
360
361 #if _FXM_PLATFORM_ != _FXM_PLATFORM_APPLE_
362 void CFX_FaceCache::InitPlatform() {}
363 #endif
364
365 CFX_GlyphBitmap* CFX_FaceCache::LookUpGlyphBitmap(
366 CFX_Font* pFont,
367 const CFX_Matrix* pMatrix,
368 const CFX_ByteString& FaceGlyphsKey,
369 uint32_t glyph_index,
370 FX_BOOL bFontStyle,
371 int dest_width,
372 int anti_alias) {
373 CFX_SizeGlyphCache* pSizeCache;
374 auto it = m_SizeMap.find(FaceGlyphsKey);
375 if (it == m_SizeMap.end()) {
376 pSizeCache = new CFX_SizeGlyphCache;
377 m_SizeMap[FaceGlyphsKey] = pSizeCache;
378 } else {
379 pSizeCache = it->second;
380 }
381 auto it2 = pSizeCache->m_GlyphMap.find(glyph_index);
382 if (it2 != pSizeCache->m_GlyphMap.end())
383 return it2->second;
384
385 CFX_GlyphBitmap* pGlyphBitmap = RenderGlyph(pFont, glyph_index, bFontStyle,
386 pMatrix, dest_width, anti_alias);
387 if (!pGlyphBitmap)
388 return nullptr;
389
390 pSizeCache->m_GlyphMap[glyph_index] = pGlyphBitmap;
391 return pGlyphBitmap;
392 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698