From e372ad7333bdd6bb0c579cf074843ef0c6f3414f Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 9 Apr 2018 20:33:45 +0000 Subject: Use ByteStringView / pdfium::span in CPDF font as appropriate. Change-Id: I92c7ba605bf95a9023ad046b8dddebe0a0592802 Reviewed-on: https://pdfium-review.googlesource.com/29992 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- core/fpdfapi/font/cpdf_cidfont.cpp | 11 +++++------ core/fpdfapi/font/cpdf_cidfont.h | 7 +++---- core/fpdfapi/font/cpdf_cmap.cpp | 32 +++++++++++++++---------------- core/fpdfapi/font/cpdf_cmap.h | 4 ++-- core/fpdfapi/font/cpdf_font.cpp | 30 ++++++++++++++--------------- core/fpdfapi/font/cpdf_font.h | 9 ++++----- core/fpdfapi/page/cpdf_textobject.cpp | 15 +++++++-------- core/fpdfapi/render/cpdf_textrenderer.cpp | 6 +++--- 8 files changed, 53 insertions(+), 61 deletions(-) (limited to 'core/fpdfapi') diff --git a/core/fpdfapi/font/cpdf_cidfont.cpp b/core/fpdfapi/font/cpdf_cidfont.cpp index 7de4d7cd82..4d3ffdf41f 100644 --- a/core/fpdfapi/font/cpdf_cidfont.cpp +++ b/core/fpdfapi/font/cpdf_cidfont.cpp @@ -751,18 +751,17 @@ int CPDF_CIDFont::GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) { return pdata[0] * 256 + pdata[1]; } -uint32_t CPDF_CIDFont::GetNextChar(const char* pString, - int nStrLen, - int& offset) const { - return m_pCMap->GetNextChar(pString, nStrLen, offset); +uint32_t CPDF_CIDFont::GetNextChar(const ByteStringView& pString, + size_t& offset) const { + return m_pCMap->GetNextChar(pString, offset); } int CPDF_CIDFont::GetCharSize(uint32_t charcode) const { return m_pCMap->GetCharSize(charcode); } -int CPDF_CIDFont::CountChar(const char* pString, int size) const { - return m_pCMap->CountChar(pString, size); +size_t CPDF_CIDFont::CountChar(const ByteStringView& pString) const { + return m_pCMap->CountChar(pString); } int CPDF_CIDFont::AppendChar(char* str, uint32_t charcode) const { diff --git a/core/fpdfapi/font/cpdf_cidfont.h b/core/fpdfapi/font/cpdf_cidfont.h index 0bf5f7ace1..fe2e2fed4e 100644 --- a/core/fpdfapi/font/cpdf_cidfont.h +++ b/core/fpdfapi/font/cpdf_cidfont.h @@ -46,10 +46,9 @@ class CPDF_CIDFont : public CPDF_Font { int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override; uint32_t GetCharWidthF(uint32_t charcode) override; FX_RECT GetCharBBox(uint32_t charcode) override; - uint32_t GetNextChar(const char* pString, - int nStrLen, - int& offset) const override; - int CountChar(const char* pString, int size) const override; + uint32_t GetNextChar(const ByteStringView& pString, + size_t& offset) const override; + size_t CountChar(const ByteStringView& pString) const override; int AppendChar(char* str, uint32_t charcode) const override; bool IsVertWriting() const override; bool IsUnicodeCompatible() const override; diff --git a/core/fpdfapi/font/cpdf_cmap.cpp b/core/fpdfapi/font/cpdf_cmap.cpp index 81ad63344c..8e46a75112 100644 --- a/core/fpdfapi/font/cpdf_cmap.cpp +++ b/core/fpdfapi/font/cpdf_cmap.cpp @@ -337,10 +337,9 @@ uint16_t CPDF_CMap::CIDFromCharCode(uint32_t charcode) const { return it->m_StartCID + charcode - it->m_StartCode; } -uint32_t CPDF_CMap::GetNextChar(const char* pString, - int nStrLen, - int& offset) const { - auto* pBytes = reinterpret_cast(pString); +uint32_t CPDF_CMap::GetNextChar(const ByteStringView& pString, + size_t& offset) const { + auto pBytes = pString.span(); switch (m_CodingScheme) { case OneByte: { return pBytes[offset++]; @@ -370,7 +369,7 @@ uint32_t CPDF_CMap::GetNextChar(const char* pString, charcode = (charcode << 8) + codes[i]; return charcode; } - if (char_size == 4 || offset == nStrLen) + if (char_size == 4 || offset == pBytes.size()) return 0; codes[char_size++] = pBytes[offset++]; } @@ -402,33 +401,32 @@ int CPDF_CMap::GetCharSize(uint32_t charcode) const { return 1; } -int CPDF_CMap::CountChar(const char* pString, int size) const { +size_t CPDF_CMap::CountChar(const ByteStringView& pString) const { switch (m_CodingScheme) { case OneByte: - return size; + return pString.GetLength(); case TwoBytes: - return (size + 1) / 2; + return (pString.GetLength() + 1) / 2; case MixedTwoBytes: { - int count = 0; - for (int i = 0; i < size; i++) { + size_t count = 0; + for (size_t i = 0; i < pString.GetLength(); i++) { count++; - if (m_MixedTwoByteLeadingBytes[reinterpret_cast( - pString)[i]]) { + if (m_MixedTwoByteLeadingBytes[pString[i]]) i++; - } } return count; } case MixedFourBytes: { - int count = 0, offset = 0; - while (offset < size) { - GetNextChar(pString, size, offset); + size_t count = 0; + size_t offset = 0; + while (offset < pString.GetLength()) { + GetNextChar(pString, offset); count++; } return count; } } - return size; + return pString.GetLength(); } int CPDF_CMap::AppendChar(char* str, uint32_t charcode) const { diff --git a/core/fpdfapi/font/cpdf_cmap.h b/core/fpdfapi/font/cpdf_cmap.h index c6fdcae17b..96ccf02c0b 100644 --- a/core/fpdfapi/font/cpdf_cmap.h +++ b/core/fpdfapi/font/cpdf_cmap.h @@ -62,8 +62,8 @@ class CPDF_CMap : public Retainable { uint16_t CIDFromCharCode(uint32_t charcode) const; int GetCharSize(uint32_t charcode) const; - uint32_t GetNextChar(const char* pString, int nStrLen, int& offset) const; - int CountChar(const char* pString, int size) const; + uint32_t GetNextChar(const ByteStringView& pString, size_t& offset) const; + size_t CountChar(const ByteStringView& pString) const; int AppendChar(char* str, uint32_t charcode) const; void SetVertical(bool vert) { m_bVertical = vert; } diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 013cdded20..f636e9397a 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -122,8 +122,8 @@ bool CPDF_Font::IsUnicodeCompatible() const { return false; } -int CPDF_Font::CountChar(const char* pString, int size) const { - return size; +size_t CPDF_Font::CountChar(const ByteStringView& pString) const { + return pString.GetLength(); } int CPDF_Font::GlyphFromCharCodeExt(uint32_t charcode) { @@ -278,20 +278,18 @@ void CPDF_Font::CheckFontMetrics() { void CPDF_Font::LoadUnicodeMap() const { m_bToUnicodeLoaded = true; CPDF_Stream* pStream = m_pFontDict->GetStreamFor("ToUnicode"); - if (!pStream) { + if (!pStream) return; - } + m_pToUnicodeMap = pdfium::MakeUnique(); m_pToUnicodeMap->Load(pStream); } -uint32_t CPDF_Font::GetStringWidth(const char* pString, int size) { - int offset = 0; +uint32_t CPDF_Font::GetStringWidth(const ByteStringView& pString) { + size_t offset = 0; uint32_t width = 0; - while (offset < size) { - uint32_t charcode = GetNextChar(pString, size, offset); - width += GetCharWidthF(charcode); - } + while (offset < pString.GetLength()) + width += GetCharWidthF(GetNextChar(pString, offset)); return width; } @@ -346,13 +344,13 @@ std::unique_ptr CPDF_Font::Create(CPDF_Document* pDoc, return pFont->Load() ? std::move(pFont) : nullptr; } -uint32_t CPDF_Font::GetNextChar(const char* pString, - int nStrLen, - int& offset) const { - if (offset < 0 || nStrLen < 1) { +uint32_t CPDF_Font::GetNextChar(const ByteStringView& pString, + size_t& offset) const { + if (pString.IsEmpty()) return 0; - } - uint8_t ch = offset < nStrLen ? pString[offset++] : pString[nStrLen - 1]; + + uint8_t ch = offset < pString.GetLength() ? pString[offset++] + : pString[pString.GetLength() - 1]; return static_cast(ch); } diff --git a/core/fpdfapi/font/cpdf_font.h b/core/fpdfapi/font/cpdf_font.h index db99efdd1b..588fb66163 100644 --- a/core/fpdfapi/font/cpdf_font.h +++ b/core/fpdfapi/font/cpdf_font.h @@ -53,10 +53,9 @@ class CPDF_Font { virtual bool IsVertWriting() const; virtual bool IsUnicodeCompatible() const; - virtual uint32_t GetNextChar(const char* pString, - int nStrLen, - int& offset) const; - virtual int CountChar(const char* pString, int size) const; + virtual uint32_t GetNextChar(const ByteStringView& pString, + size_t& offset) const; + virtual size_t CountChar(const ByteStringView& pString) const; virtual int AppendChar(char* buf, uint32_t charcode) const; virtual int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) = 0; virtual int GlyphFromCharCodeExt(uint32_t charcode); @@ -75,7 +74,7 @@ class CPDF_Font { void GetFontBBox(FX_RECT& rect) const { rect = m_FontBBox; } int GetTypeAscent() const { return m_Ascent; } int GetTypeDescent() const { return m_Descent; } - uint32_t GetStringWidth(const char* pString, int size); + uint32_t GetStringWidth(const ByteStringView& pString); uint32_t FallbackFontFromCharcode(uint32_t charcode); int FallbackGlyphFromCharcode(int fallbackFont, uint32_t charcode); diff --git a/core/fpdfapi/page/cpdf_textobject.cpp b/core/fpdfapi/page/cpdf_textobject.cpp index 402bf2ef66..36a4722773 100644 --- a/core/fpdfapi/page/cpdf_textobject.cpp +++ b/core/fpdfapi/page/cpdf_textobject.cpp @@ -152,18 +152,17 @@ void CPDF_TextObject::SetSegments(const ByteString* pStrs, CPDF_Font* pFont = m_TextState.GetFont(); int nChars = 0; for (int i = 0; i < nsegs; ++i) - nChars += pFont->CountChar(pStrs[i].c_str(), pStrs[i].GetLength()); + nChars += pFont->CountChar(pStrs[i].AsStringView()); nChars += nsegs - 1; m_CharCodes.resize(nChars); m_CharPos.resize(nChars - 1); - int index = 0; + size_t index = 0; for (int i = 0; i < nsegs; ++i) { - const char* segment = pStrs[i].c_str(); - int len = pStrs[i].GetLength(); - int offset = 0; - while (offset < len) { - ASSERT(static_cast(index) < m_CharCodes.size()); - m_CharCodes[index++] = pFont->GetNextChar(segment, len, offset); + ByteStringView segment = pStrs[i].AsStringView(); + size_t offset = 0; + while (offset < segment.GetLength()) { + ASSERT(index < m_CharCodes.size()); + m_CharCodes[index++] = pFont->GetNextChar(segment, offset); } if (i != nsegs - 1) { m_CharPos[index - 1] = pKerning[i]; diff --git a/core/fpdfapi/render/cpdf_textrenderer.cpp b/core/fpdfapi/render/cpdf_textrenderer.cpp index 711dbfaf39..7aeddf0a61 100644 --- a/core/fpdfapi/render/cpdf_textrenderer.cpp +++ b/core/fpdfapi/render/cpdf_textrenderer.cpp @@ -83,18 +83,18 @@ void CPDF_TextRenderer::DrawTextString(CFX_RenderDevice* pDevice, if (pFont->IsType3Font()) return; - int nChars = pFont->CountChar(str.c_str(), str.GetLength()); + int nChars = pFont->CountChar(str.AsStringView()); if (nChars <= 0) return; - int offset = 0; + size_t offset = 0; std::vector codes; std::vector positions; codes.resize(nChars); positions.resize(nChars - 1); float cur_pos = 0; for (int i = 0; i < nChars; i++) { - codes[i] = pFont->GetNextChar(str.c_str(), str.GetLength(), offset); + codes[i] = pFont->GetNextChar(str.AsStringView(), offset); if (i) positions[i - 1] = cur_pos; cur_pos += pFont->GetCharWidthF(codes[i]) * font_size / 1000; -- cgit v1.2.3