Reorganize fx_font.h. - Move ScopedFontTransform, CFX_GlyphBitmap, and TextGlyphPos to their own headers. - Move PDF_GetStandardFontName() to CFX_FontMapper. - Move implementations next to headers. Change-Id: I814042068e0be7b5cfbcee381b00db814289ad09 Reviewed-on: https://pdfium-review.googlesource.com/c/50077 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 9c346db..29903a0 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -24,6 +24,7 @@ #include "core/fpdfapi/parser/cpdf_name.h" #include "core/fpdfapi/parser/cpdf_stream_acc.h" #include "core/fxcrt/fx_memory.h" +#include "core/fxge/cfx_fontmapper.h" #include "core/fxge/fx_font.h" #include "core/fxge/fx_freetype.h" #include "third_party/base/logging.h" @@ -283,7 +284,7 @@ // static CPDF_Font* CPDF_Font::GetStockFont(CPDF_Document* pDoc, ByteStringView name) { ByteString fontname(name); - int font_id = PDF_GetStandardFontName(&fontname); + int font_id = CFX_FontMapper::GetStandardFontName(&fontname); if (font_id < 0) return nullptr;
diff --git a/core/fpdfapi/font/cpdf_type1font.cpp b/core/fpdfapi/font/cpdf_type1font.cpp index 0a4a722..231aee3 100644 --- a/core/fpdfapi/font/cpdf_type1font.cpp +++ b/core/fpdfapi/font/cpdf_type1font.cpp
@@ -9,6 +9,7 @@ #include <algorithm> #include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fxge/cfx_fontmapper.h" #include "core/fxge/cfx_gemodule.h" #include "core/fxge/fx_font.h" #include "core/fxge/fx_freetype.h" @@ -84,7 +85,7 @@ } bool CPDF_Type1Font::Load() { - m_Base14Font = PDF_GetStandardFontName(&m_BaseFont); + m_Base14Font = CFX_FontMapper::GetStandardFontName(&m_BaseFont); if (!IsBase14Font()) return LoadCommon();
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index c6d2dbd..28e9b23 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -30,6 +30,7 @@ #include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fxcodec/JBig2_DocumentContext.h" #include "core/fxcrt/fx_codepage.h" +#include "core/fxge/cfx_fontmapper.h" #include "core/fxge/cfx_unicodeencoding.h" #include "core/fxge/fx_font.h" #include "third_party/base/ptr_util.h" @@ -626,7 +627,7 @@ CPDF_Font* CPDF_Document::AddStandardFont(const char* font, CPDF_FontEncoding* pEncoding) { ByteString name(font); - if (PDF_GetStandardFontName(&name) < 0) + if (CFX_FontMapper::GetStandardFontName(&name) < 0) return nullptr; return GetPageData()->GetStandardFont(name, pEncoding); }
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp index 4a6ee85..47ace7c 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.cpp +++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -53,12 +53,14 @@ #include "core/fxcrt/fx_system.h" #include "core/fxcrt/maybe_owned.h" #include "core/fxge/cfx_defaultrenderdevice.h" +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/cfx_graphstatedata.h" #include "core/fxge/cfx_pathdata.h" #include "core/fxge/cfx_renderdevice.h" #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/fx_font.h" #include "core/fxge/renderdevicedriver_iface.h" +#include "core/fxge/text_glyph_pos.h" #include "third_party/base/compiler_specific.h" #include "third_party/base/logging.h" #include "third_party/base/numerics/safe_math.h" @@ -1921,7 +1923,7 @@ if (glyphs.empty()) return true; - FX_RECT rect = FXGE_GetGlyphsBBox(glyphs, 0); + FX_RECT rect = GetGlyphsBBox(glyphs, 0); auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>(); if (!pBitmap->Create(rect.Width(), rect.Height(), FXDIB_8bppMask)) return true;
diff --git a/core/fpdfapi/render/cpdf_type3cache.cpp b/core/fpdfapi/render/cpdf_type3cache.cpp index 5a48bca..d427a9e 100644 --- a/core/fpdfapi/render/cpdf_type3cache.cpp +++ b/core/fpdfapi/render/cpdf_type3cache.cpp
@@ -14,9 +14,9 @@ #include "core/fpdfapi/font/cpdf_type3font.h" #include "core/fpdfapi/render/cpdf_type3glyphs.h" #include "core/fxcrt/fx_safe_types.h" +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/fx_dib.h" -#include "core/fxge/fx_font.h" #include "third_party/base/ptr_util.h" namespace {
diff --git a/core/fpdfapi/render/cpdf_type3glyphs.cpp b/core/fpdfapi/render/cpdf_type3glyphs.cpp index ef14d73..3f52e66 100644 --- a/core/fpdfapi/render/cpdf_type3glyphs.cpp +++ b/core/fpdfapi/render/cpdf_type3glyphs.cpp
@@ -9,6 +9,7 @@ #include <algorithm> #include <map> +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/fx_font.h" namespace {
diff --git a/core/fxge/BUILD.gn b/core/fxge/BUILD.gn index a00673b..6c9ed9a 100644 --- a/core/fxge/BUILD.gn +++ b/core/fxge/BUILD.gn
@@ -37,6 +37,8 @@ "cfx_fontmgr.h", "cfx_gemodule.cpp", "cfx_gemodule.h", + "cfx_glyphbitmap.cpp", + "cfx_glyphbitmap.h", "cfx_graphstate.cpp", "cfx_graphstate.h", "cfx_graphstatedata.cpp", @@ -96,10 +98,14 @@ "fx_font.h", "fx_freetype.h", "fx_ge_fontmap.cpp", - "fx_ge_text.cpp", "renderdevicedriver_iface.cpp", "renderdevicedriver_iface.h", + "scoped_font_transform.cpp", + "scoped_font_transform.h", + "scoped_font_transform.h", "systemfontinfo_iface.h", + "text_glyph_pos.cpp", + "text_glyph_pos.h", ] configs += [
diff --git a/core/fxge/apple/fx_apple_platform.cpp b/core/fxge/apple/fx_apple_platform.cpp index 5d385f8..d290ac4 100644 --- a/core/fxge/apple/fx_apple_platform.cpp +++ b/core/fxge/apple/fx_apple_platform.cpp
@@ -14,9 +14,10 @@ #include "core/fxge/cfx_facecache.h" #include "core/fxge/cfx_font.h" #include "core/fxge/cfx_gemodule.h" +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/cfx_renderdevice.h" +#include "core/fxge/cfx_substfont.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/fx_font.h" #include "core/fxge/fx_freetype.h" #include "third_party/base/span.h"
diff --git a/core/fxge/cfx_facecache.cpp b/core/fxge/cfx_facecache.cpp index f0d3b58..d868bf2 100644 --- a/core/fxge/cfx_facecache.cpp +++ b/core/fxge/cfx_facecache.cpp
@@ -15,11 +15,12 @@ #include "core/fxge/cfx_font.h" #include "core/fxge/cfx_fontmgr.h" #include "core/fxge/cfx_gemodule.h" +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/cfx_pathdata.h" #include "core/fxge/cfx_substfont.h" #include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/fx_font.h" #include "core/fxge/fx_freetype.h" +#include "core/fxge/scoped_font_transform.h" #include "third_party/base/numerics/safe_math.h" #include "third_party/base/ptr_util.h"
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp index 8b7d9f5..cd137bd 100644 --- a/core/fxge/cfx_font.cpp +++ b/core/fxge/cfx_font.cpp
@@ -21,6 +21,7 @@ #include "core/fxge/cfx_pathdata.h" #include "core/fxge/cfx_substfont.h" #include "core/fxge/fx_font.h" +#include "core/fxge/scoped_font_transform.h" #include "third_party/base/ptr_util.h" #define EM_ADJUST(em, a) (em == 0 ? (a) : (a)*1000 / em)
diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp index 0a38952..33bef51 100644 --- a/core/fxge/cfx_fontmapper.cpp +++ b/core/fxge/cfx_fontmapper.cpp
@@ -403,7 +403,7 @@ SubstName.Remove(' '); if (bTrueType && name.GetLength() > 0 && name[0] == '@') SubstName = name.Right(name.GetLength() - 1); - PDF_GetStandardFontName(&SubstName); + GetStandardFontName(&SubstName); if (SubstName == "Symbol" && !bTrueType) { pSubstFont->m_Family = "Chrome Symbol"; pSubstFont->m_Charset = FX_CHARSET_Symbol; @@ -423,7 +423,7 @@ Optional<size_t> pos = SubstName.Find(",", 0); if (pos.has_value()) { family = SubstName.Left(pos.value()); - PDF_GetStandardFontName(&family); + GetStandardFontName(&family); style = SubstName.Right(SubstName.GetLength() - (pos.value() + 1)); bHasComma = true; } else { @@ -727,7 +727,8 @@ m_pFontInfo->GetFaceIndex(hFont)); } -int PDF_GetStandardFontName(ByteString* name) { +// static +int CFX_FontMapper::GetStandardFontName(ByteString* name) { const auto* end = std::end(g_AltFontNames); const auto* found = std::lower_bound(std::begin(g_AltFontNames), end, name->c_str(),
diff --git a/core/fxge/cfx_fontmapper.h b/core/fxge/cfx_fontmapper.h index b5d9c31..f318476 100644 --- a/core/fxge/cfx_fontmapper.h +++ b/core/fxge/cfx_fontmapper.h
@@ -23,6 +23,8 @@ explicit CFX_FontMapper(CFX_FontMgr* mgr); ~CFX_FontMapper(); + static int GetStandardFontName(ByteString* name); + void SetSystemFontInfo(std::unique_ptr<SystemFontInfoIface> pFontInfo); SystemFontInfoIface* GetSystemFontInfo() { return m_pFontInfo.get(); } void AddInstalledFont(const ByteString& name, int charset);
diff --git a/core/fxge/cfx_glyphbitmap.cpp b/core/fxge/cfx_glyphbitmap.cpp new file mode 100644 index 0000000..cc28d51 --- /dev/null +++ b/core/fxge/cfx_glyphbitmap.cpp
@@ -0,0 +1,12 @@ +// Copyright 2019 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxge/cfx_glyphbitmap.h" + +#include "core/fxge/dib/cfx_dibitmap.h" + +CFX_GlyphBitmap::CFX_GlyphBitmap(int left, int top) + : m_Left(left), m_Top(top), m_pBitmap(pdfium::MakeRetain<CFX_DIBitmap>()) {} + +CFX_GlyphBitmap::~CFX_GlyphBitmap() = default;
diff --git a/core/fxge/cfx_glyphbitmap.h b/core/fxge/cfx_glyphbitmap.h new file mode 100644 index 0000000..81a0547 --- /dev/null +++ b/core/fxge/cfx_glyphbitmap.h
@@ -0,0 +1,34 @@ +// Copyright 2019 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXGE_CFX_GLYPHBITMAP_H_ +#define CORE_FXGE_CFX_GLYPHBITMAP_H_ + +#include <vector> + +#include "core/fxcrt/retain_ptr.h" + +class CFX_DIBitmap; + +class CFX_GlyphBitmap { + public: + CFX_GlyphBitmap(int left, int top); + ~CFX_GlyphBitmap(); + + CFX_GlyphBitmap(const CFX_GlyphBitmap&) = delete; + CFX_GlyphBitmap& operator=(const CFX_GlyphBitmap&) = delete; + + const RetainPtr<CFX_DIBitmap>& GetBitmap() const { return m_pBitmap; } + int left() const { return m_Left; } + int top() const { return m_Top; } + + private: + const int m_Left; + const int m_Top; + RetainPtr<CFX_DIBitmap> m_pBitmap; +}; + +#endif // CORE_FXGE_CFX_GLYPHBITMAP_H_
diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp index a61f5fc..0171beb 100644 --- a/core/fxge/cfx_renderdevice.cpp +++ b/core/fxge/cfx_renderdevice.cpp
@@ -16,12 +16,14 @@ #include "core/fxge/cfx_font.h" #include "core/fxge/cfx_fontmgr.h" #include "core/fxge/cfx_gemodule.h" +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/cfx_graphstatedata.h" #include "core/fxge/cfx_pathdata.h" #include "core/fxge/dib/cfx_dibitmap.h" #include "core/fxge/dib/cfx_imagerenderer.h" #include "core/fxge/fx_font.h" #include "core/fxge/renderdevicedriver_iface.h" +#include "core/fxge/text_glyph_pos.h" #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_ #include "third_party/skia/include/core/SkTypes.h" @@ -953,7 +955,7 @@ if (anti_alias < FXFT_RENDER_MODE_LCD && glyphs.size() > 1) AdjustGlyphSpace(&glyphs); - FX_RECT bmp_rect = FXGE_GetGlyphsBBox(glyphs, anti_alias); + FX_RECT bmp_rect = GetGlyphsBBox(glyphs, anti_alias); bmp_rect.Intersect(m_ClipBox); if (bmp_rect.IsEmpty()) return true;
diff --git a/core/fxge/fx_font.cpp b/core/fxge/fx_font.cpp index d03fb57..2654998 100644 --- a/core/fxge/fx_font.cpp +++ b/core/fxge/fx_font.cpp
@@ -4,9 +4,110 @@ #include "core/fxge/fx_font.h" +#include "core/fxge/cfx_glyphbitmap.h" #include "core/fxge/dib/cfx_dibitmap.h" +#include "core/fxge/text_glyph_pos.h" -CFX_GlyphBitmap::CFX_GlyphBitmap(int left, int top) - : m_Left(left), m_Top(top), m_pBitmap(pdfium::MakeRetain<CFX_DIBitmap>()) {} +namespace { -CFX_GlyphBitmap::~CFX_GlyphBitmap() = default; +ByteString GetStringFromTable(const uint8_t* string_ptr, + uint32_t string_ptr_length, + uint16_t offset, + uint16_t length) { + if (string_ptr_length < static_cast<uint32_t>(offset + length)) { + return ByteString(); + } + return ByteString(string_ptr + offset, length); +} + +} // namespace + +FX_RECT GetGlyphsBBox(const std::vector<TextGlyphPos>& glyphs, int anti_alias) { + FX_RECT rect(0, 0, 0, 0); + bool bStarted = false; + for (const TextGlyphPos& glyph : glyphs) { + const CFX_GlyphBitmap* pGlyph = glyph.m_pGlyph; + if (!pGlyph) + continue; + + FX_SAFE_INT32 char_left = glyph.m_Origin.x; + char_left += pGlyph->left(); + if (!char_left.IsValid()) + continue; + + FX_SAFE_INT32 char_width = pGlyph->GetBitmap()->GetWidth(); + if (anti_alias == FXFT_RENDER_MODE_LCD) + char_width /= 3; + if (!char_width.IsValid()) + continue; + + FX_SAFE_INT32 char_right = char_left + char_width; + if (!char_right.IsValid()) + continue; + + FX_SAFE_INT32 char_top = glyph.m_Origin.y; + char_top -= pGlyph->top(); + if (!char_top.IsValid()) + continue; + + FX_SAFE_INT32 char_height = pGlyph->GetBitmap()->GetHeight(); + if (!char_height.IsValid()) + continue; + + FX_SAFE_INT32 char_bottom = char_top + char_height; + if (!char_bottom.IsValid()) + continue; + + if (bStarted) { + rect.left = pdfium::base::ValueOrDieForType<int32_t>( + pdfium::base::CheckMin(rect.left, char_left)); + rect.right = pdfium::base::ValueOrDieForType<int32_t>( + pdfium::base::CheckMax(rect.right, char_right)); + rect.top = pdfium::base::ValueOrDieForType<int32_t>( + pdfium::base::CheckMin(rect.top, char_top)); + rect.bottom = pdfium::base::ValueOrDieForType<int32_t>( + pdfium::base::CheckMax(rect.bottom, char_bottom)); + continue; + } + + rect.left = char_left.ValueOrDie(); + rect.right = char_right.ValueOrDie(); + rect.top = char_top.ValueOrDie(); + rect.bottom = char_bottom.ValueOrDie(); + bStarted = true; + } + return rect; +} + +ByteString GetNameFromTT(const uint8_t* name_table, + uint32_t name_table_size, + uint32_t name_id) { + if (!name_table || name_table_size < 6) { + return ByteString(); + } + uint32_t name_count = GET_TT_SHORT(name_table + 2); + uint32_t string_offset = GET_TT_SHORT(name_table + 4); + // We will ignore the possibility of overlap of structures and + // string table as if it's all corrupt there's not a lot we can do. + if (name_table_size < string_offset) { + return ByteString(); + } + + const uint8_t* string_ptr = name_table + string_offset; + uint32_t string_ptr_size = name_table_size - string_offset; + name_table += 6; + name_table_size -= 6; + if (name_table_size < name_count * 12) { + return ByteString(); + } + + for (uint32_t i = 0; i < name_count; i++, name_table += 12) { + if (GET_TT_SHORT(name_table + 6) == name_id && + GET_TT_SHORT(name_table) == 1 && GET_TT_SHORT(name_table + 2) == 0) { + return GetStringFromTable(string_ptr, string_ptr_size, + GET_TT_SHORT(name_table + 10), + GET_TT_SHORT(name_table + 8)); + } + } + return ByteString(); +}
diff --git a/core/fxge/fx_font.h b/core/fxge/fx_font.h index 219fe9a..efaf411 100644 --- a/core/fxge/fx_font.h +++ b/core/fxge/fx_font.h
@@ -44,63 +44,20 @@ #define GET_TT_LONG(w) \ (uint32_t)(((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3]) -class CFX_DIBitmap; - #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_ class SkTypeface; using CFX_TypeFace = SkTypeface; #endif -// Sets the given transform on the font, and resets it to the identity when it -// goes out of scope. -class ScopedFontTransform { - public: - ScopedFontTransform(FT_Face face, FXFT_Matrix* matrix); - ~ScopedFontTransform(); +class TextGlyphPos; - private: - FT_Face m_Face; -}; - -class CFX_GlyphBitmap { - public: - CFX_GlyphBitmap(int left, int top); - ~CFX_GlyphBitmap(); - - CFX_GlyphBitmap(const CFX_GlyphBitmap&) = delete; - CFX_GlyphBitmap& operator=(const CFX_GlyphBitmap&) = delete; - - const RetainPtr<CFX_DIBitmap>& GetBitmap() const { return m_pBitmap; } - int left() const { return m_Left; } - int top() const { return m_Top; } - - private: - const int m_Left; - const int m_Top; - RetainPtr<CFX_DIBitmap> m_pBitmap; -}; - -class TextGlyphPos { - public: - TextGlyphPos(); - TextGlyphPos(const TextGlyphPos&); - ~TextGlyphPos(); - - const CFX_GlyphBitmap* m_pGlyph; - CFX_Point m_Origin; - CFX_PointF m_fOrigin; -}; - -FX_RECT FXGE_GetGlyphsBBox(const std::vector<TextGlyphPos>& glyphs, - int anti_alias); +FX_RECT GetGlyphsBBox(const std::vector<TextGlyphPos>& glyphs, int anti_alias); ByteString GetNameFromTT(const uint8_t* name_table, uint32_t name_table_size, uint32_t name); -int PDF_GetStandardFontName(ByteString* name); - inline bool FontStyleIsBold(uint32_t style) { return !!(style & FXFONT_BOLD); }
diff --git a/core/fxge/fx_ge_fontmap.cpp b/core/fxge/fx_ge_fontmap.cpp index 62db457..f60416c 100644 --- a/core/fxge/fx_ge_fontmap.cpp +++ b/core/fxge/fx_ge_fontmap.cpp
@@ -6,52 +6,8 @@ #include <memory> -#include "core/fxge/cfx_fontmapper.h" -#include "core/fxge/fx_font.h" #include "core/fxge/systemfontinfo_iface.h" -static ByteString GetStringFromTable(const uint8_t* string_ptr, - uint32_t string_ptr_length, - uint16_t offset, - uint16_t length) { - if (string_ptr_length < static_cast<uint32_t>(offset + length)) { - return ByteString(); - } - return ByteString(string_ptr + offset, length); -} - -ByteString GetNameFromTT(const uint8_t* name_table, - uint32_t name_table_size, - uint32_t name_id) { - if (!name_table || name_table_size < 6) { - return ByteString(); - } - uint32_t name_count = GET_TT_SHORT(name_table + 2); - uint32_t string_offset = GET_TT_SHORT(name_table + 4); - // We will ignore the possibility of overlap of structures and - // string table as if it's all corrupt there's not a lot we can do. - if (name_table_size < string_offset) { - return ByteString(); - } - - const uint8_t* string_ptr = name_table + string_offset; - uint32_t string_ptr_size = name_table_size - string_offset; - name_table += 6; - name_table_size -= 6; - if (name_table_size < name_count * 12) { - return ByteString(); - } - - for (uint32_t i = 0; i < name_count; i++, name_table += 12) { - if (GET_TT_SHORT(name_table + 6) == name_id && - GET_TT_SHORT(name_table) == 1 && GET_TT_SHORT(name_table + 2) == 0) { - return GetStringFromTable(string_ptr, string_ptr_size, - GET_TT_SHORT(name_table + 10), - GET_TT_SHORT(name_table + 8)); - } - } - return ByteString(); -} #ifdef PDF_ENABLE_XFA void* SystemFontInfoIface::MapFontByUnicode(uint32_t dwUnicode, int weight,
diff --git a/core/fxge/fx_ge_text.cpp b/core/fxge/fx_ge_text.cpp deleted file mode 100644 index df65e09..0000000 --- a/core/fxge/fx_ge_text.cpp +++ /dev/null
@@ -1,102 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include <algorithm> -#include <limits> -#include <vector> - -#include "core/fxcrt/fx_safe_types.h" -#include "core/fxge/cfx_pathdata.h" -#include "core/fxge/dib/cfx_dibitmap.h" -#include "core/fxge/fx_font.h" -#include "core/fxge/fx_freetype.h" -#include "core/fxge/renderdevicedriver_iface.h" - -namespace { - -void ResetTransform(FT_Face face) { - FXFT_Matrix matrix; - matrix.xx = 0x10000L; - matrix.xy = 0; - matrix.yx = 0; - matrix.yy = 0x10000L; - FXFT_Set_Transform(face, &matrix, 0); -} - -} // namespace - -TextGlyphPos::TextGlyphPos() : m_pGlyph(nullptr) {} - -TextGlyphPos::TextGlyphPos(const TextGlyphPos&) = default; - -TextGlyphPos::~TextGlyphPos() {} - -ScopedFontTransform::ScopedFontTransform(FT_Face face, FXFT_Matrix* matrix) - : m_Face(face) { - FXFT_Set_Transform(m_Face, matrix, 0); -} - -ScopedFontTransform::~ScopedFontTransform() { - ResetTransform(m_Face); -} - -FX_RECT FXGE_GetGlyphsBBox(const std::vector<TextGlyphPos>& glyphs, - int anti_alias) { - FX_RECT rect(0, 0, 0, 0); - bool bStarted = false; - for (const TextGlyphPos& glyph : glyphs) { - const CFX_GlyphBitmap* pGlyph = glyph.m_pGlyph; - if (!pGlyph) - continue; - - FX_SAFE_INT32 char_left = glyph.m_Origin.x; - char_left += pGlyph->left(); - if (!char_left.IsValid()) - continue; - - FX_SAFE_INT32 char_width = pGlyph->GetBitmap()->GetWidth(); - if (anti_alias == FXFT_RENDER_MODE_LCD) - char_width /= 3; - if (!char_width.IsValid()) - continue; - - FX_SAFE_INT32 char_right = char_left + char_width; - if (!char_right.IsValid()) - continue; - - FX_SAFE_INT32 char_top = glyph.m_Origin.y; - char_top -= pGlyph->top(); - if (!char_top.IsValid()) - continue; - - FX_SAFE_INT32 char_height = pGlyph->GetBitmap()->GetHeight(); - if (!char_height.IsValid()) - continue; - - FX_SAFE_INT32 char_bottom = char_top + char_height; - if (!char_bottom.IsValid()) - continue; - - if (bStarted) { - rect.left = pdfium::base::ValueOrDieForType<int32_t>( - pdfium::base::CheckMin(rect.left, char_left)); - rect.right = pdfium::base::ValueOrDieForType<int32_t>( - pdfium::base::CheckMax(rect.right, char_right)); - rect.top = pdfium::base::ValueOrDieForType<int32_t>( - pdfium::base::CheckMin(rect.top, char_top)); - rect.bottom = pdfium::base::ValueOrDieForType<int32_t>( - pdfium::base::CheckMax(rect.bottom, char_bottom)); - continue; - } - - rect.left = char_left.ValueOrDie(); - rect.right = char_right.ValueOrDie(); - rect.top = char_top.ValueOrDie(); - rect.bottom = char_bottom.ValueOrDie(); - bStarted = true; - } - return rect; -}
diff --git a/core/fxge/scoped_font_transform.cpp b/core/fxge/scoped_font_transform.cpp new file mode 100644 index 0000000..98c505e --- /dev/null +++ b/core/fxge/scoped_font_transform.cpp
@@ -0,0 +1,29 @@ +// Copyright 2019 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxge/scoped_font_transform.h" + +namespace { + +void ResetTransform(FT_Face face) { + FXFT_Matrix matrix; + matrix.xx = 0x10000L; + matrix.xy = 0; + matrix.yx = 0; + matrix.yy = 0x10000L; + FXFT_Set_Transform(face, &matrix, 0); +} + +} // namespace + +ScopedFontTransform::ScopedFontTransform(FT_Face face, FXFT_Matrix* matrix) + : m_Face(face) { + FXFT_Set_Transform(m_Face, matrix, 0); +} + +ScopedFontTransform::~ScopedFontTransform() { + ResetTransform(m_Face); +}
diff --git a/core/fxge/scoped_font_transform.h b/core/fxge/scoped_font_transform.h new file mode 100644 index 0000000..7dfc5b7 --- /dev/null +++ b/core/fxge/scoped_font_transform.h
@@ -0,0 +1,23 @@ +// Copyright 2019 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXGE_SCOPED_FONT_TRANSFORM_H_ +#define CORE_FXGE_SCOPED_FONT_TRANSFORM_H_ + +#include "core/fxge/fx_freetype.h" + +// Sets the given transform on the font, and resets it to the identity when it +// goes out of scope. +class ScopedFontTransform { + public: + ScopedFontTransform(FT_Face face, FXFT_Matrix* matrix); + ~ScopedFontTransform(); + + private: + FT_Face m_Face; +}; + +#endif // CORE_FXGE_SCOPED_FONT_TRANSFORM_H_
diff --git a/core/fxge/text_glyph_pos.cpp b/core/fxge/text_glyph_pos.cpp new file mode 100644 index 0000000..f0c8501 --- /dev/null +++ b/core/fxge/text_glyph_pos.cpp
@@ -0,0 +1,13 @@ +// Copyright 2019 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxge/text_glyph_pos.h" + +TextGlyphPos::TextGlyphPos() = default; + +TextGlyphPos::TextGlyphPos(const TextGlyphPos&) = default; + +TextGlyphPos::~TextGlyphPos() = default;
diff --git a/core/fxge/text_glyph_pos.h b/core/fxge/text_glyph_pos.h new file mode 100644 index 0000000..fa4ca05 --- /dev/null +++ b/core/fxge/text_glyph_pos.h
@@ -0,0 +1,25 @@ +// Copyright 2019 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXGE_TEXT_GLYPH_POS_H_ +#define CORE_FXGE_TEXT_GLYPH_POS_H_ + +#include "core/fxcrt/fx_coordinates.h" + +class CFX_GlyphBitmap; + +class TextGlyphPos { + public: + TextGlyphPos(); + TextGlyphPos(const TextGlyphPos&); + ~TextGlyphPos(); + + const CFX_GlyphBitmap* m_pGlyph = nullptr; + CFX_Point m_Origin; + CFX_PointF m_fOrigin; +}; + +#endif // CORE_FXGE_TEXT_GLYPH_POS_H_