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

Side by Side Diff: core/fpdfapi/parser/cpdf_document.cpp

Issue 2520133002: Remove some WrapUnique() calls by returing unique_ptrs (Closed)
Patch Set: rebase Created 4 years, 1 month 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
« no previous file with comments | « core/fpdfapi/page/pageint.h ('k') | core/fpdfapi/parser/cpdf_parser.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfapi/parser/cpdf_document.h" 7 #include "core/fpdfapi/parser/cpdf_document.h"
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 flags |= PDFFONT_SYMBOLIC; 292 flags |= PDFFONT_SYMBOLIC;
293 else 293 else
294 flags |= PDFFONT_NONSYMBOLIC; 294 flags |= PDFFONT_NONSYMBOLIC;
295 return flags; 295 return flags;
296 } 296 }
297 297
298 void ProcessNonbCJK(CPDF_Dictionary* pBaseDict, 298 void ProcessNonbCJK(CPDF_Dictionary* pBaseDict,
299 bool bold, 299 bool bold,
300 bool italic, 300 bool italic,
301 CFX_ByteString basefont, 301 CFX_ByteString basefont,
302 CPDF_Array* pWidths) { 302 std::unique_ptr<CPDF_Array> pWidths) {
303 if (bold && italic) 303 if (bold && italic)
304 basefont += ",BoldItalic"; 304 basefont += ",BoldItalic";
305 else if (bold) 305 else if (bold)
306 basefont += ",Bold"; 306 basefont += ",Bold";
307 else if (italic) 307 else if (italic)
308 basefont += ",Italic"; 308 basefont += ",Italic";
309 pBaseDict->SetNewFor<CPDF_Name>("Subtype", "TrueType"); 309 pBaseDict->SetNewFor<CPDF_Name>("Subtype", "TrueType");
310 pBaseDict->SetNewFor<CPDF_Name>("BaseFont", basefont); 310 pBaseDict->SetNewFor<CPDF_Name>("BaseFont", basefont);
311 pBaseDict->SetNewFor<CPDF_Number>("FirstChar", 32); 311 pBaseDict->SetNewFor<CPDF_Number>("FirstChar", 32);
312 pBaseDict->SetNewFor<CPDF_Number>("LastChar", 255); 312 pBaseDict->SetNewFor<CPDF_Number>("LastChar", 255);
313 pBaseDict->SetFor("Widths", pdfium::WrapUnique(pWidths)); 313 pBaseDict->SetFor("Widths", std::move(pWidths));
314 } 314 }
315 315
316 std::unique_ptr<CPDF_Dictionary> CalculateFontDesc(CPDF_Document* pDoc, 316 std::unique_ptr<CPDF_Dictionary> CalculateFontDesc(
317 CFX_ByteString basefont, 317 CPDF_Document* pDoc,
318 int flags, 318 CFX_ByteString basefont,
319 int italicangle, 319 int flags,
320 int ascend, 320 int italicangle,
321 int descend, 321 int ascend,
322 CPDF_Array* bbox, 322 int descend,
323 int32_t stemV) { 323 std::unique_ptr<CPDF_Array> bbox,
324 int32_t stemV) {
324 auto pFontDesc = 325 auto pFontDesc =
325 pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()); 326 pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
326 pFontDesc->SetNewFor<CPDF_Name>("Type", "FontDescriptor"); 327 pFontDesc->SetNewFor<CPDF_Name>("Type", "FontDescriptor");
327 pFontDesc->SetNewFor<CPDF_Name>("FontName", basefont); 328 pFontDesc->SetNewFor<CPDF_Name>("FontName", basefont);
328 pFontDesc->SetNewFor<CPDF_Number>("Flags", flags); 329 pFontDesc->SetNewFor<CPDF_Number>("Flags", flags);
329 pFontDesc->SetFor("FontBBox", pdfium::WrapUnique(bbox)); 330 pFontDesc->SetFor("FontBBox", std::move(bbox));
330 pFontDesc->SetNewFor<CPDF_Number>("ItalicAngle", italicangle); 331 pFontDesc->SetNewFor<CPDF_Number>("ItalicAngle", italicangle);
331 pFontDesc->SetNewFor<CPDF_Number>("Ascent", ascend); 332 pFontDesc->SetNewFor<CPDF_Number>("Ascent", ascend);
332 pFontDesc->SetNewFor<CPDF_Number>("Descent", descend); 333 pFontDesc->SetNewFor<CPDF_Number>("Descent", descend);
333 pFontDesc->SetNewFor<CPDF_Number>("StemV", stemV); 334 pFontDesc->SetNewFor<CPDF_Number>("StemV", stemV);
334 return pFontDesc; 335 return pFontDesc;
335 } 336 }
336 337
337 } // namespace 338 } // namespace
338 339
339 CPDF_Document::CPDF_Document(std::unique_ptr<CPDF_Parser> pParser) 340 CPDF_Document::CPDF_Document(std::unique_ptr<CPDF_Parser> pParser)
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
876 int flags = 877 int flags =
877 CalculateFlags(pFont->IsBold(), pFont->IsItalic(), pFont->IsFixedWidth(), 878 CalculateFlags(pFont->IsBold(), pFont->IsItalic(), pFont->IsFixedWidth(),
878 false, false, charset == FXFONT_SYMBOL_CHARSET); 879 false, false, charset == FXFONT_SYMBOL_CHARSET);
879 880
880 CPDF_Dictionary* pBaseDict = NewIndirect<CPDF_Dictionary>(); 881 CPDF_Dictionary* pBaseDict = NewIndirect<CPDF_Dictionary>();
881 pBaseDict->SetNewFor<CPDF_Name>("Type", "Font"); 882 pBaseDict->SetNewFor<CPDF_Name>("Type", "Font");
882 std::unique_ptr<CFX_UnicodeEncoding> pEncoding( 883 std::unique_ptr<CFX_UnicodeEncoding> pEncoding(
883 new CFX_UnicodeEncoding(pFont)); 884 new CFX_UnicodeEncoding(pFont));
884 CPDF_Dictionary* pFontDict = pBaseDict; 885 CPDF_Dictionary* pFontDict = pBaseDict;
885 if (!bCJK) { 886 if (!bCJK) {
886 CPDF_Array* pWidths = new CPDF_Array; 887 auto pWidths = pdfium::MakeUnique<CPDF_Array>();
887 for (int charcode = 32; charcode < 128; charcode++) { 888 for (int charcode = 32; charcode < 128; charcode++) {
888 int glyph_index = pEncoding->GlyphFromCharCode(charcode); 889 int glyph_index = pEncoding->GlyphFromCharCode(charcode);
889 int char_width = pFont->GetGlyphWidth(glyph_index); 890 int char_width = pFont->GetGlyphWidth(glyph_index);
890 pWidths->AddNew<CPDF_Number>(char_width); 891 pWidths->AddNew<CPDF_Number>(char_width);
891 } 892 }
892 if (charset == FXFONT_ANSI_CHARSET || charset == FXFONT_DEFAULT_CHARSET || 893 if (charset == FXFONT_ANSI_CHARSET || charset == FXFONT_DEFAULT_CHARSET ||
893 charset == FXFONT_SYMBOL_CHARSET) { 894 charset == FXFONT_SYMBOL_CHARSET) {
894 pBaseDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding"); 895 pBaseDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding");
895 for (int charcode = 128; charcode <= 255; charcode++) { 896 for (int charcode = 128; charcode <= 255; charcode++) {
896 int glyph_index = pEncoding->GlyphFromCharCode(charcode); 897 int glyph_index = pEncoding->GlyphFromCharCode(charcode);
897 int char_width = pFont->GetGlyphWidth(glyph_index); 898 int char_width = pFont->GetGlyphWidth(glyph_index);
898 pWidths->AddNew<CPDF_Number>(char_width); 899 pWidths->AddNew<CPDF_Number>(char_width);
899 } 900 }
900 } else { 901 } else {
901 size_t i = CalculateEncodingDict(charset, pBaseDict); 902 size_t i = CalculateEncodingDict(charset, pBaseDict);
902 if (i < FX_ArraySize(g_FX_CharsetUnicodes)) { 903 if (i < FX_ArraySize(g_FX_CharsetUnicodes)) {
903 const uint16_t* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes; 904 const uint16_t* pUnicodes = g_FX_CharsetUnicodes[i].m_pUnicodes;
904 for (int j = 0; j < 128; j++) { 905 for (int j = 0; j < 128; j++) {
905 int glyph_index = pEncoding->GlyphFromCharCode(pUnicodes[j]); 906 int glyph_index = pEncoding->GlyphFromCharCode(pUnicodes[j]);
906 int char_width = pFont->GetGlyphWidth(glyph_index); 907 int char_width = pFont->GetGlyphWidth(glyph_index);
907 pWidths->AddNew<CPDF_Number>(char_width); 908 pWidths->AddNew<CPDF_Number>(char_width);
908 } 909 }
909 } 910 }
910 } 911 }
911 ProcessNonbCJK(pBaseDict, pFont->IsBold(), pFont->IsItalic(), basefont, 912 ProcessNonbCJK(pBaseDict, pFont->IsBold(), pFont->IsItalic(), basefont,
912 pWidths); 913 std::move(pWidths));
913 } else { 914 } else {
914 pFontDict = ProcessbCJK(pBaseDict, charset, bVert, basefont, 915 pFontDict = ProcessbCJK(pBaseDict, charset, bVert, basefont,
915 [pFont, &pEncoding](FX_WCHAR start, FX_WCHAR end, 916 [pFont, &pEncoding](FX_WCHAR start, FX_WCHAR end,
916 CPDF_Array* widthArr) { 917 CPDF_Array* widthArr) {
917 InsertWidthArray1(pFont, pEncoding.get(), start, 918 InsertWidthArray1(pFont, pEncoding.get(), start,
918 end, widthArr); 919 end, widthArr);
919 }); 920 });
920 } 921 }
921 int italicangle = 922 int italicangle =
922 pFont->GetSubstFont() ? pFont->GetSubstFont()->m_ItalicAngle : 0; 923 pFont->GetSubstFont() ? pFont->GetSubstFont()->m_ItalicAngle : 0;
923 FX_RECT bbox; 924 FX_RECT bbox;
924 pFont->GetBBox(bbox); 925 pFont->GetBBox(bbox);
925 CPDF_Array* pBBox = new CPDF_Array; 926 auto pBBox = pdfium::MakeUnique<CPDF_Array>();
926 pBBox->AddNew<CPDF_Number>(bbox.left); 927 pBBox->AddNew<CPDF_Number>(bbox.left);
927 pBBox->AddNew<CPDF_Number>(bbox.bottom); 928 pBBox->AddNew<CPDF_Number>(bbox.bottom);
928 pBBox->AddNew<CPDF_Number>(bbox.right); 929 pBBox->AddNew<CPDF_Number>(bbox.right);
929 pBBox->AddNew<CPDF_Number>(bbox.top); 930 pBBox->AddNew<CPDF_Number>(bbox.top);
930 int32_t nStemV = 0; 931 int32_t nStemV = 0;
931 if (pFont->GetSubstFont()) { 932 if (pFont->GetSubstFont()) {
932 nStemV = pFont->GetSubstFont()->m_Weight / 5; 933 nStemV = pFont->GetSubstFont()->m_Weight / 5;
933 } else { 934 } else {
934 static const FX_CHAR stem_chars[] = {'i', 'I', '!', '1'}; 935 static const FX_CHAR stem_chars[] = {'i', 'I', '!', '1'};
935 const size_t count = FX_ArraySize(stem_chars); 936 const size_t count = FX_ArraySize(stem_chars);
936 uint32_t glyph = pEncoding->GlyphFromCharCode(stem_chars[0]); 937 uint32_t glyph = pEncoding->GlyphFromCharCode(stem_chars[0]);
937 nStemV = pFont->GetGlyphWidth(glyph); 938 nStemV = pFont->GetGlyphWidth(glyph);
938 for (size_t i = 1; i < count; i++) { 939 for (size_t i = 1; i < count; i++) {
939 glyph = pEncoding->GlyphFromCharCode(stem_chars[i]); 940 glyph = pEncoding->GlyphFromCharCode(stem_chars[i]);
940 int width = pFont->GetGlyphWidth(glyph); 941 int width = pFont->GetGlyphWidth(glyph);
941 if (width > 0 && width < nStemV) 942 if (width > 0 && width < nStemV)
942 nStemV = width; 943 nStemV = width;
943 } 944 }
944 } 945 }
945 CPDF_Dictionary* pFontDesc = ToDictionary(AddIndirectObject( 946 CPDF_Dictionary* pFontDesc = ToDictionary(AddIndirectObject(
946 CalculateFontDesc(this, basefont, flags, italicangle, pFont->GetAscent(), 947 CalculateFontDesc(this, basefont, flags, italicangle, pFont->GetAscent(),
947 pFont->GetDescent(), pBBox, nStemV))); 948 pFont->GetDescent(), std::move(pBBox), nStemV)));
948 pFontDict->SetNewFor<CPDF_Reference>("FontDescriptor", this, 949 pFontDict->SetNewFor<CPDF_Reference>("FontDescriptor", this,
949 pFontDesc->GetObjNum()); 950 pFontDesc->GetObjNum());
950 return LoadFont(pBaseDict); 951 return LoadFont(pBaseDict);
951 } 952 }
952 953
953 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 954 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
954 CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTW* pLogFont, 955 CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTW* pLogFont,
955 bool bVert, 956 bool bVert,
956 bool bTranslateName) { 957 bool bTranslateName) {
957 LOGFONTA lfa; 958 LOGFONTA lfa;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 if (!bCJK) { 1015 if (!bCJK) {
1015 if (pLogFont->lfCharSet == FXFONT_ANSI_CHARSET || 1016 if (pLogFont->lfCharSet == FXFONT_ANSI_CHARSET ||
1016 pLogFont->lfCharSet == FXFONT_DEFAULT_CHARSET || 1017 pLogFont->lfCharSet == FXFONT_DEFAULT_CHARSET ||
1017 pLogFont->lfCharSet == FXFONT_SYMBOL_CHARSET) { 1018 pLogFont->lfCharSet == FXFONT_SYMBOL_CHARSET) {
1018 pBaseDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding"); 1019 pBaseDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding");
1019 } else { 1020 } else {
1020 CalculateEncodingDict(pLogFont->lfCharSet, pBaseDict); 1021 CalculateEncodingDict(pLogFont->lfCharSet, pBaseDict);
1021 } 1022 }
1022 int char_widths[224]; 1023 int char_widths[224];
1023 GetCharWidth(hDC, 32, 255, char_widths); 1024 GetCharWidth(hDC, 32, 255, char_widths);
1024 CPDF_Array* pWidths = new CPDF_Array; 1025 auto pWidths = pdfium::MakeUnique<CPDF_Array>();
1025 for (size_t i = 0; i < 224; i++) 1026 for (size_t i = 0; i < 224; i++)
1026 pWidths->AddNew<CPDF_Number>(char_widths[i]); 1027 pWidths->AddNew<CPDF_Number>(char_widths[i]);
1027 ProcessNonbCJK(pBaseDict, pLogFont->lfWeight > FW_MEDIUM, 1028 ProcessNonbCJK(pBaseDict, pLogFont->lfWeight > FW_MEDIUM,
1028 pLogFont->lfItalic != 0, basefont, pWidths); 1029 pLogFont->lfItalic != 0, basefont, std::move(pWidths));
1029 } else { 1030 } else {
1030 pFontDict = 1031 pFontDict =
1031 ProcessbCJK(pBaseDict, pLogFont->lfCharSet, bVert, basefont, 1032 ProcessbCJK(pBaseDict, pLogFont->lfCharSet, bVert, basefont,
1032 [&hDC](FX_WCHAR start, FX_WCHAR end, CPDF_Array* widthArr) { 1033 [&hDC](FX_WCHAR start, FX_WCHAR end, CPDF_Array* widthArr) {
1033 InsertWidthArray(hDC, start, end, widthArr); 1034 InsertWidthArray(hDC, start, end, widthArr);
1034 }); 1035 });
1035 } 1036 }
1036 CPDF_Array* pBBox = new CPDF_Array; 1037 auto pBBox = pdfium::MakeUnique<CPDF_Array>();
1037 for (int i = 0; i < 4; i++) 1038 for (int i = 0; i < 4; i++)
1038 pBBox->AddNew<CPDF_Number>(bbox[i]); 1039 pBBox->AddNew<CPDF_Number>(bbox[i]);
1039 std::unique_ptr<CPDF_Dictionary> pFontDesc = 1040 std::unique_ptr<CPDF_Dictionary> pFontDesc =
1040 CalculateFontDesc(this, basefont, flags, italicangle, ascend, descend, 1041 CalculateFontDesc(this, basefont, flags, italicangle, ascend, descend,
1041 pBBox, pLogFont->lfWeight / 5); 1042 std::move(pBBox), pLogFont->lfWeight / 5);
1042 pFontDesc->SetNewFor<CPDF_Number>("CapHeight", capheight); 1043 pFontDesc->SetNewFor<CPDF_Number>("CapHeight", capheight);
1043 pFontDict->SetNewFor<CPDF_Reference>( 1044 pFontDict->SetNewFor<CPDF_Reference>(
1044 "FontDescriptor", this, 1045 "FontDescriptor", this,
1045 AddIndirectObject(std::move(pFontDesc))->GetObjNum()); 1046 AddIndirectObject(std::move(pFontDesc))->GetObjNum());
1046 hFont = SelectObject(hDC, hFont); 1047 hFont = SelectObject(hDC, hFont);
1047 DeleteObject(hFont); 1048 DeleteObject(hFont);
1048 DeleteDC(hDC); 1049 DeleteDC(hDC);
1049 return LoadFont(pBaseDict); 1050 return LoadFont(pBaseDict);
1050 } 1051 }
1051 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 1052 #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
OLDNEW
« no previous file with comments | « core/fpdfapi/page/pageint.h ('k') | core/fpdfapi/parser/cpdf_parser.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698