summaryrefslogtreecommitdiff
path: root/core/fxge/cfx_font.cpp
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-08-29 23:53:53 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-08-29 23:53:53 +0000
commit20c41a5a47b2460277897a1059789fc9e184e3ab (patch)
treea60b9695de7242be833c4119bb9815bd39b297ca /core/fxge/cfx_font.cpp
parent8020fd856805ed5c5f48a7a5f3a346ea6948355f (diff)
downloadpdfium-20c41a5a47b2460277897a1059789fc9e184e3ab.tar.xz
Use pdfium::span<>/UnownedPtr in CFX_Font.
Move some platform-specific ifdefs so the come last in each section as it is easier to read, perhaps. Change-Id: Ic1c2652c46ecebc63b66213735ed6d94737a7f32 Reviewed-on: https://pdfium-review.googlesource.com/41630 Commit-Queue: Tom Sepez <tsepez@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'core/fxge/cfx_font.cpp')
-rw-r--r--core/fxge/cfx_font.cpp196
1 files changed, 93 insertions, 103 deletions
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp
index bda576777e..3c2b48d4a3 100644
--- a/core/fxge/cfx_font.cpp
+++ b/core/fxge/cfx_font.cpp
@@ -54,11 +54,10 @@ unsigned long FTStreamRead(FXFT_Stream stream,
void FTStreamClose(FXFT_Stream stream) {}
-bool LoadFileImp(FXFT_Library library,
- FXFT_Face* Face,
- const RetainPtr<IFX_SeekableReadStream>& pFile,
- int32_t faceIndex,
- std::unique_ptr<FXFT_StreamRec>* stream) {
+FXFT_Face LoadFileImp(FXFT_Library library,
+ const RetainPtr<IFX_SeekableReadStream>& pFile,
+ int32_t faceIndex,
+ std::unique_ptr<FXFT_StreamRec>* stream) {
auto stream1 = pdfium::MakeUnique<FXFT_StreamRec>();
stream1->base = nullptr;
stream1->size = static_cast<unsigned long>(pFile->GetSize());
@@ -66,20 +65,21 @@ bool LoadFileImp(FXFT_Library library,
stream1->descriptor.pointer = static_cast<void*>(pFile.Get());
stream1->close = FTStreamClose;
stream1->read = FTStreamRead;
+
FXFT_Open_Args args;
args.flags = FT_OPEN_STREAM;
args.stream = stream1.get();
- if (FXFT_Open_Face(library, &args, faceIndex, Face))
- return false;
+
+ FXFT_Face face;
+ if (FXFT_Open_Face(library, &args, faceIndex, &face))
+ return nullptr;
+
if (stream)
*stream = std::move(stream1);
- return true;
-}
-#endif // PDF_ENABLE_XFA
-FXFT_Face FT_LoadFont(const uint8_t* pData, int size) {
- return CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(pData, size, 0);
+ return face;
}
+#endif // PDF_ENABLE_XFA
void Outline_CheckEmptyContour(OUTLINE_PARAMS* param) {
std::vector<FX_PATHPOINT>& points = param->m_pPath->GetPoints();
@@ -291,19 +291,7 @@ uint8_t CFX_Font::GetCharSetFromUnicode(uint16_t word) {
return FX_CHARSET_ANSI;
}
-CFX_Font::CFX_Font()
- :
- m_Face(nullptr),
- m_FaceCache(nullptr),
- m_pFontData(nullptr),
- m_pGsubData(nullptr),
- m_dwSize(0),
-#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
- m_pPlatformFont(nullptr),
-#endif
- m_bEmbedded(false),
- m_bVertical(false) {
-}
+CFX_Font::CFX_Font() = default;
#ifdef PDF_ENABLE_XFA
bool CFX_Font::LoadFile(const RetainPtr<IFX_SeekableReadStream>& pFile,
@@ -313,13 +301,13 @@ bool CFX_Font::LoadFile(const RetainPtr<IFX_SeekableReadStream>& pFile,
CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
pFontMgr->InitFTLibrary();
- FXFT_Library library = pFontMgr->GetFTLibrary();
std::unique_ptr<FXFT_StreamRec> stream;
- if (!LoadFileImp(library, &m_Face, pFile, nFaceIndex, &stream))
+ m_Face = LoadFileImp(pFontMgr->GetFTLibrary(), pFile, nFaceIndex, &stream);
+ if (!m_Face)
return false;
m_pOwnedStream = std::move(stream);
- FXFT_Set_Pixel_Sizes(m_Face, 0, 64);
+ FXFT_Set_Pixel_Sizes(m_Face.Get(), 0, 64);
return true;
}
@@ -336,6 +324,7 @@ void CFX_Font::SetSubstFont(std::unique_ptr<CFX_SubstFont> subst) {
#endif // PDF_ENABLE_XFA
CFX_Font::~CFX_Font() {
+ m_FontData = {}; // m_FontData can't outive m_Face.
if (m_Face)
DeleteFace();
@@ -347,10 +336,9 @@ CFX_Font::~CFX_Font() {
void CFX_Font::DeleteFace() {
ClearFaceCache();
if (m_bEmbedded)
- FXFT_Done_Face(m_Face);
+ FXFT_Done_Face(m_Face.Release());
else
- CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face);
- m_Face = nullptr;
+ CFX_GEModule::Get()->GetFontMgr()->ReleaseFace(m_Face.Release());
}
void CFX_Font::LoadSubst(const ByteString& face_name,
@@ -367,8 +355,8 @@ void CFX_Font::LoadSubst(const ByteString& face_name,
face_name, bTrueType, flags, weight, italic_angle, CharsetCP,
m_pSubstFont.get());
if (m_Face) {
- m_pFontData = FXFT_Get_Face_Stream_Base(m_Face);
- m_dwSize = FXFT_Get_Face_Stream_Size(m_Face);
+ m_FontData = {FXFT_Get_Face_Stream_Base(m_Face.Get()),
+ FXFT_Get_Face_Stream_Size(m_Face.Get())};
}
}
@@ -378,64 +366,64 @@ uint32_t CFX_Font::GetGlyphWidth(uint32_t glyph_index) {
if (m_pSubstFont && m_pSubstFont->m_bFlagMM)
AdjustMMParams(glyph_index, 0, 0);
int err = FXFT_Load_Glyph(
- m_Face, glyph_index,
+ m_Face.Get(), glyph_index,
FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
if (err)
return 0;
- int horiAdvance = FXFT_Get_Glyph_HoriAdvance(m_Face);
+ int horiAdvance = FXFT_Get_Glyph_HoriAdvance(m_Face.Get());
if (horiAdvance < 0 || horiAdvance > kThousandthMaxInt)
return 0;
- return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), horiAdvance);
+ return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face.Get()), horiAdvance);
}
-bool CFX_Font::LoadEmbedded(const uint8_t* data, uint32_t size) {
- std::vector<uint8_t> temp(data, data + size);
- m_pFontDataAllocation.swap(temp);
- m_Face = FT_LoadFont(m_pFontDataAllocation.data(), size);
- m_pFontData = m_pFontDataAllocation.data();
+bool CFX_Font::LoadEmbedded(pdfium::span<const uint8_t> src_span) {
+ m_pFontDataAllocation =
+ std::vector<uint8_t>(src_span.begin(), src_span.end());
+ m_Face = CFX_GEModule::Get()->GetFontMgr()->GetFixedFace(
+ m_pFontDataAllocation.data(), m_pFontDataAllocation.size(), 0);
m_bEmbedded = true;
- m_dwSize = size;
+ m_FontData = m_pFontDataAllocation;
return !!m_Face;
}
bool CFX_Font::IsTTFont() const {
- return m_Face && FXFT_Is_Face_TT_OT(m_Face) == FXFT_FACE_FLAG_SFNT;
+ return m_Face && FXFT_Is_Face_TT_OT(m_Face.Get()) == FXFT_FACE_FLAG_SFNT;
}
int CFX_Font::GetAscent() const {
if (!m_Face)
return 0;
- int ascender = FXFT_Get_Face_Ascender(m_Face);
+ int ascender = FXFT_Get_Face_Ascender(m_Face.Get());
if (ascender < kThousandthMinInt || ascender > kThousandthMaxInt)
return 0;
- return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), ascender);
+ return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face.Get()), ascender);
}
int CFX_Font::GetDescent() const {
if (!m_Face)
return 0;
- int descender = FXFT_Get_Face_Descender(m_Face);
+ int descender = FXFT_Get_Face_Descender(m_Face.Get());
if (descender < kThousandthMinInt || descender > kThousandthMaxInt)
return 0;
- return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face), descender);
+ return EM_ADJUST(FXFT_Get_Face_UnitsPerEM(m_Face.Get()), descender);
}
bool CFX_Font::GetGlyphBBox(uint32_t glyph_index, FX_RECT* pBBox) {
if (!m_Face)
return false;
- if (FXFT_Is_Face_Tricky(m_Face)) {
- int error = FXFT_Set_Char_Size(m_Face, 0, 1000 * 64, 72, 72);
+ if (FXFT_Is_Face_Tricky(m_Face.Get())) {
+ int error = FXFT_Set_Char_Size(m_Face.Get(), 0, 1000 * 64, 72, 72);
if (error)
return false;
- error = FXFT_Load_Glyph(m_Face, glyph_index,
+ error = FXFT_Load_Glyph(m_Face.Get(), glyph_index,
FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
if (error)
return false;
@@ -460,33 +448,34 @@ bool CFX_Font::GetGlyphBBox(uint32_t glyph_index, FX_RECT* pBBox) {
pBBox->top = cbox.yMax * 1000 / pixel_size_y;
pBBox->bottom = cbox.yMin * 1000 / pixel_size_y;
}
- pBBox->top = std::min(pBBox->top,
- static_cast<int32_t>(FXFT_Get_Face_Ascender(m_Face)));
- pBBox->bottom = std::max(
- pBBox->bottom, static_cast<int32_t>(FXFT_Get_Face_Descender(m_Face)));
+ pBBox->top = std::min(
+ pBBox->top, static_cast<int32_t>(FXFT_Get_Face_Ascender(m_Face.Get())));
+ pBBox->bottom =
+ std::max(pBBox->bottom,
+ static_cast<int32_t>(FXFT_Get_Face_Descender(m_Face.Get())));
FT_Done_Glyph(glyph);
- return FXFT_Set_Pixel_Sizes(m_Face, 0, 64) == 0;
+ return FXFT_Set_Pixel_Sizes(m_Face.Get(), 0, 64) == 0;
}
if (FXFT_Load_Glyph(
- m_Face, glyph_index,
+ m_Face.Get(), glyph_index,
FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH)) {
return false;
}
- int em = FXFT_Get_Face_UnitsPerEM(m_Face);
+ int em = FXFT_Get_Face_UnitsPerEM(m_Face.Get());
if (em == 0) {
- pBBox->left = FXFT_Get_Glyph_HoriBearingX(m_Face);
- pBBox->bottom = FXFT_Get_Glyph_HoriBearingY(m_Face);
- pBBox->top = pBBox->bottom - FXFT_Get_Glyph_Height(m_Face);
- pBBox->right = pBBox->left + FXFT_Get_Glyph_Width(m_Face);
+ pBBox->left = FXFT_Get_Glyph_HoriBearingX(m_Face.Get());
+ pBBox->bottom = FXFT_Get_Glyph_HoriBearingY(m_Face.Get());
+ pBBox->top = pBBox->bottom - FXFT_Get_Glyph_Height(m_Face.Get());
+ pBBox->right = pBBox->left + FXFT_Get_Glyph_Width(m_Face.Get());
} else {
- pBBox->left = FXFT_Get_Glyph_HoriBearingX(m_Face) * 1000 / em;
- pBBox->top =
- (FXFT_Get_Glyph_HoriBearingY(m_Face) - FXFT_Get_Glyph_Height(m_Face)) *
- 1000 / em;
- pBBox->right =
- (FXFT_Get_Glyph_HoriBearingX(m_Face) + FXFT_Get_Glyph_Width(m_Face)) *
- 1000 / em;
- pBBox->bottom = (FXFT_Get_Glyph_HoriBearingY(m_Face)) * 1000 / em;
+ pBBox->left = FXFT_Get_Glyph_HoriBearingX(m_Face.Get()) * 1000 / em;
+ pBBox->top = (FXFT_Get_Glyph_HoriBearingY(m_Face.Get()) -
+ FXFT_Get_Glyph_Height(m_Face.Get())) *
+ 1000 / em;
+ pBBox->right = (FXFT_Get_Glyph_HoriBearingX(m_Face.Get()) +
+ FXFT_Get_Glyph_Width(m_Face.Get())) *
+ 1000 / em;
+ pBBox->bottom = (FXFT_Get_Glyph_HoriBearingY(m_Face.Get())) * 1000 / em;
}
return true;
}
@@ -494,27 +483,27 @@ bool CFX_Font::GetGlyphBBox(uint32_t glyph_index, FX_RECT* pBBox) {
bool CFX_Font::IsItalic() const {
if (!m_Face)
return false;
- if (FXFT_Is_Face_Italic(m_Face) == FXFT_STYLE_FLAG_ITALIC)
+ if (FXFT_Is_Face_Italic(m_Face.Get()) == FXFT_STYLE_FLAG_ITALIC)
return true;
- ByteString str(FXFT_Get_Face_Style_Name(m_Face));
+ ByteString str(FXFT_Get_Face_Style_Name(m_Face.Get()));
str.MakeLower();
return str.Contains("italic");
}
bool CFX_Font::IsBold() const {
- return m_Face && FXFT_Is_Face_Bold(m_Face) == FXFT_STYLE_FLAG_BOLD;
+ return m_Face && FXFT_Is_Face_Bold(m_Face.Get()) == FXFT_STYLE_FLAG_BOLD;
}
bool CFX_Font::IsFixedWidth() const {
- return m_Face && FXFT_Is_Face_fixedwidth(m_Face) != 0;
+ return m_Face && FXFT_Is_Face_fixedwidth(m_Face.Get()) != 0;
}
ByteString CFX_Font::GetPsName() const {
if (!m_Face)
return ByteString();
- ByteString psName = FXFT_Get_Postscript_Name(m_Face);
+ ByteString psName = FXFT_Get_Postscript_Name(m_Face.Get());
if (psName.IsEmpty())
psName = "Untitled";
return psName;
@@ -524,7 +513,7 @@ ByteString CFX_Font::GetFamilyName() const {
if (!m_Face && !m_pSubstFont)
return ByteString();
if (m_Face)
- return ByteString(FXFT_Get_Face_Family_Name(m_Face));
+ return ByteString(FXFT_Get_Face_Family_Name(m_Face.Get()));
return m_pSubstFont->m_Family;
}
@@ -533,7 +522,7 @@ ByteString CFX_Font::GetFaceName() const {
if (!m_Face && !m_pSubstFont)
return ByteString();
if (m_Face) {
- ByteString style = ByteString(FXFT_Get_Face_Style_Name(m_Face));
+ ByteString style = ByteString(FXFT_Get_Face_Style_Name(m_Face.Get()));
ByteString facename = GetFamilyName();
if (facename.IsEmpty())
facename = "Untitled";
@@ -548,17 +537,17 @@ bool CFX_Font::GetBBox(FX_RECT* pBBox) {
if (!m_Face)
return false;
- int em = FXFT_Get_Face_UnitsPerEM(m_Face);
+ int em = FXFT_Get_Face_UnitsPerEM(m_Face.Get());
if (em == 0) {
- pBBox->left = FXFT_Get_Face_xMin(m_Face);
- pBBox->bottom = FXFT_Get_Face_yMax(m_Face);
- pBBox->top = FXFT_Get_Face_yMin(m_Face);
- pBBox->right = FXFT_Get_Face_xMax(m_Face);
+ pBBox->left = FXFT_Get_Face_xMin(m_Face.Get());
+ pBBox->bottom = FXFT_Get_Face_yMax(m_Face.Get());
+ pBBox->top = FXFT_Get_Face_yMin(m_Face.Get());
+ pBBox->right = FXFT_Get_Face_xMax(m_Face.Get());
} else {
- pBBox->left = FXFT_Get_Face_xMin(m_Face) * 1000 / em;
- pBBox->top = FXFT_Get_Face_yMin(m_Face) * 1000 / em;
- pBBox->right = FXFT_Get_Face_xMax(m_Face) * 1000 / em;
- pBBox->bottom = FXFT_Get_Face_yMax(m_Face) * 1000 / em;
+ pBBox->left = FXFT_Get_Face_xMin(m_Face.Get()) * 1000 / em;
+ pBBox->top = FXFT_Get_Face_yMin(m_Face.Get()) * 1000 / em;
+ pBBox->right = FXFT_Get_Face_xMax(m_Face.Get()) * 1000 / em;
+ pBBox->bottom = FXFT_Get_Face_yMax(m_Face.Get()) * 1000 / em;
}
return true;
}
@@ -582,7 +571,7 @@ void CFX_Font::AdjustMMParams(int glyph_index,
int weight) const {
ASSERT(dest_width >= 0);
FXFT_MM_Var pMasters = nullptr;
- FXFT_Get_MM_Var(m_Face, &pMasters);
+ FXFT_Get_MM_Var(m_Face.Get(), &pMasters);
if (!pMasters)
return;
@@ -598,34 +587,35 @@ void CFX_Font::AdjustMMParams(int glyph_index,
int min_param = FXFT_Get_MM_Axis_Min(FXFT_Get_MM_Axis(pMasters, 1)) / 65536;
int max_param = FXFT_Get_MM_Axis_Max(FXFT_Get_MM_Axis(pMasters, 1)) / 65536;
coords[1] = min_param;
- FXFT_Set_MM_Design_Coordinates(m_Face, 2, coords);
- FXFT_Load_Glyph(m_Face, glyph_index,
+ FXFT_Set_MM_Design_Coordinates(m_Face.Get(), 2, coords);
+ FXFT_Load_Glyph(m_Face.Get(), glyph_index,
FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
- int min_width = FXFT_Get_Glyph_HoriAdvance(m_Face) * 1000 /
- FXFT_Get_Face_UnitsPerEM(m_Face);
+ int min_width = FXFT_Get_Glyph_HoriAdvance(m_Face.Get()) * 1000 /
+ FXFT_Get_Face_UnitsPerEM(m_Face.Get());
coords[1] = max_param;
- FXFT_Set_MM_Design_Coordinates(m_Face, 2, coords);
- FXFT_Load_Glyph(m_Face, glyph_index,
+ FXFT_Set_MM_Design_Coordinates(m_Face.Get(), 2, coords);
+ FXFT_Load_Glyph(m_Face.Get(), glyph_index,
FXFT_LOAD_NO_SCALE | FXFT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH);
- int max_width = FXFT_Get_Glyph_HoriAdvance(m_Face) * 1000 /
- FXFT_Get_Face_UnitsPerEM(m_Face);
+ int max_width = FXFT_Get_Glyph_HoriAdvance(m_Face.Get()) * 1000 /
+ FXFT_Get_Face_UnitsPerEM(m_Face.Get());
if (max_width == min_width) {
- FXFT_Free(m_Face, pMasters);
+ FXFT_Free(m_Face.Get(), pMasters);
return;
}
int param = min_param + (max_param - min_param) * (dest_width - min_width) /
(max_width - min_width);
coords[1] = param;
}
- FXFT_Free(m_Face, pMasters);
- FXFT_Set_MM_Design_Coordinates(m_Face, 2, coords);
+ FXFT_Free(m_Face.Get(), pMasters);
+ FXFT_Set_MM_Design_Coordinates(m_Face.Get(), 2, coords);
}
CFX_PathData* CFX_Font::LoadGlyphPathImpl(uint32_t glyph_index,
uint32_t dest_width) const {
if (!m_Face)
return nullptr;
- FXFT_Set_Pixel_Sizes(m_Face, 0, 64);
+
+ FXFT_Set_Pixel_Sizes(m_Face.Get(), 0, 64);
FXFT_Matrix ft_matrix = {65536, 0, 0, 65536};
if (m_pSubstFont) {
if (m_pSubstFont->m_ItalicAngle) {
@@ -646,11 +636,11 @@ CFX_PathData* CFX_Font::LoadGlyphPathImpl(uint32_t glyph_index,
if (m_pSubstFont->m_bFlagMM)
AdjustMMParams(glyph_index, dest_width, m_pSubstFont->m_Weight);
}
- ScopedFontTransform scoped_transform(m_Face, &ft_matrix);
+ ScopedFontTransform scoped_transform(m_Face.Get(), &ft_matrix);
int load_flags = FXFT_LOAD_NO_BITMAP;
- if (!(m_Face->face_flags & FT_FACE_FLAG_SFNT) || !FT_IS_TRICKY(m_Face))
+ if (!(m_Face->face_flags & FT_FACE_FLAG_SFNT) || !FT_IS_TRICKY(m_Face.Get()))
load_flags |= FT_LOAD_NO_HINTING;
- if (FXFT_Load_Glyph(m_Face, glyph_index, load_flags))
+ if (FXFT_Load_Glyph(m_Face.Get(), glyph_index, load_flags))
return nullptr;
if (m_pSubstFont && !m_pSubstFont->m_bFlagMM &&
m_pSubstFont->m_Weight > 400) {
@@ -661,7 +651,7 @@ CFX_PathData* CFX_Font::LoadGlyphPathImpl(uint32_t glyph_index,
level = s_WeightPow_SHIFTJIS[index] * 2 * 65536 / 36655;
else
level = s_WeightPow[index] * 2;
- FXFT_Outline_Embolden(FXFT_Get_Glyph_Outline(m_Face), level);
+ FXFT_Outline_Embolden(FXFT_Get_Glyph_Outline(m_Face.Get()), level);
}
FXFT_Outline_Funcs funcs;
@@ -678,7 +668,7 @@ CFX_PathData* CFX_Font::LoadGlyphPathImpl(uint32_t glyph_index,
params.m_CurX = params.m_CurY = 0;
params.m_CoordUnit = 64 * 64.0;
- FXFT_Outline_Decompose(FXFT_Get_Glyph_Outline(m_Face), &funcs, &params);
+ FXFT_Outline_Decompose(FXFT_Get_Glyph_Outline(m_Face.Get()), &funcs, &params);
if (pPath->GetPoints().empty())
return nullptr;