nss: upgrade to release 3.73
[LibreOffice.git] / vcl / source / fontsubset / fontsubset.cxx
blobbd663d8bc0d7f291d9bbd45908b4b6b173b8d083
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <osl/diagnose.h>
22 #include <sal/log.hxx>
24 #include <fontsubset.hxx>
25 #include <sft.hxx>
27 FontSubsetInfo::FontSubsetInfo()
28 : m_nAscent( 0)
29 , m_nDescent( 0)
30 , m_nCapHeight( 0)
31 , m_nFontType( FontType::NO_FONT)
32 , mpInFontBytes( nullptr)
33 , mnInByteLength( 0)
34 , meInFontType( FontType::NO_FONT)
35 , mpSftTTFont( nullptr)
36 , mnReqFontTypeMask( FontType::NO_FONT )
37 , mpOutFile(nullptr)
38 , mpReqFontName(nullptr)
39 , mpReqGlyphIds(nullptr)
40 , mpReqEncodedIds(nullptr)
41 , mnReqGlyphCount(0)
45 FontSubsetInfo::~FontSubsetInfo()
49 // prepare subsetting for fonts where the input font file is mapped
50 void FontSubsetInfo::LoadFont(
51 FontType eInFontType,
52 const unsigned char* pInFontBytes, int nInByteLength)
54 SAL_WARN_IF( (mpSftTTFont != nullptr), "vcl", "Subset from SFT and from mapped font-file requested");
55 meInFontType = eInFontType;
56 mpInFontBytes = pInFontBytes;
57 mnInByteLength = nInByteLength;
60 // prepare subsetting for fonts that are known to the SFT-parser
61 void FontSubsetInfo::LoadFont( vcl::TrueTypeFont* pSftTTFont )
63 SAL_WARN_IF( (mpInFontBytes != nullptr), "vcl", "Subset from SFT and from mapped font-file requested");
64 mpSftTTFont = pSftTTFont;
65 meInFontType = FontType::ANY_SFNT;
68 bool FontSubsetInfo::CreateFontSubset(
69 FontType nReqFontTypeMask,
70 FILE* pOutFile, const char* pReqFontName,
71 const sal_GlyphId* pReqGlyphIds, const sal_uInt8* pReqEncodedIds, int nReqGlyphCount,
72 sal_Int32* pOutGlyphWidths)
74 // prepare request details needed by all underlying subsetters
75 mnReqFontTypeMask = nReqFontTypeMask;
76 mpOutFile = pOutFile;
77 mpReqFontName = pReqFontName;
78 mpReqGlyphIds = pReqGlyphIds;
79 mpReqEncodedIds = pReqEncodedIds;
80 mnReqGlyphCount = nReqGlyphCount;
82 // TODO: move the glyphid/encid/notdef reshuffling from the callers to here
84 // dispatch to underlying subsetters
85 bool bOK = false;
87 // TODO: better match available input-type to possible subset-types
88 switch( meInFontType) {
89 case FontType::SFNT_TTF:
90 case FontType::SFNT_CFF:
91 case FontType::ANY_SFNT:
92 bOK = CreateFontSubsetFromSfnt( pOutGlyphWidths);
93 break;
94 case FontType::CFF_FONT:
95 bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
96 break;
97 case FontType::TYPE1_PFA:
98 case FontType::TYPE1_PFB:
99 case FontType::ANY_TYPE1:
100 bOK = CreateFontSubsetFromType1( pOutGlyphWidths);
101 break;
102 case FontType::NO_FONT:
103 default:
104 OSL_FAIL( "unhandled type in CreateFontSubset()");
105 break;
108 return bOK;
111 // TODO: move function to sft.cxx to replace dummy implementation
112 bool FontSubsetInfo::CreateFontSubsetFromSfnt( sal_Int32* pOutGlyphWidths )
114 // handle SFNT_CFF fonts
115 sal_uInt32 nCffLength = 0;
116 const sal_uInt8* pCffBytes = mpSftTTFont->table(vcl::O_CFF, nCffLength);
117 if (pCffBytes)
119 LoadFont( FontType::CFF_FONT, pCffBytes, nCffLength);
120 const bool bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
121 return bOK;
124 // handle SFNT_TTF fonts
125 // by forwarding the subset request to AG's sft subsetter
126 #if 1 // TODO: remove conversion tp 16bit glyphids when sft-subsetter has been updated
127 std::vector<sal_uInt16> aShortGlyphIds;
128 aShortGlyphIds.reserve(mnReqGlyphCount);
129 for (int i = 0; i < mnReqGlyphCount; ++i)
130 aShortGlyphIds.push_back(static_cast<sal_uInt16>(mpReqGlyphIds[i]));
131 // remove const_cast when sft-subsetter is const-correct
132 sal_uInt8* pEncArray = const_cast<sal_uInt8*>( mpReqEncodedIds );
133 #endif
134 vcl::SFErrCodes nSFTErr = vcl::SFErrCodes::BadArg;
135 if( mnReqFontTypeMask & FontType::TYPE42_FONT )
137 nSFTErr = CreateT42FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
138 aShortGlyphIds.data(), pEncArray, mnReqGlyphCount );
140 else if( mnReqFontTypeMask & FontType::TYPE3_FONT )
142 nSFTErr = CreateT3FromTTGlyphs( mpSftTTFont, mpOutFile, mpReqFontName,
143 aShortGlyphIds.data(), pEncArray, mnReqGlyphCount,
144 0 /* 0 = horizontal, 1 = vertical */ );
146 else if( mnReqFontTypeMask & FontType::SFNT_TTF )
148 // TODO: use CreateTTFromTTGlyphs()
149 // TODO: move functionality from callers here
152 return (nSFTErr != vcl::SFErrCodes::Ok);
155 // TODO: replace dummy implementation
156 bool FontSubsetInfo::CreateFontSubsetFromType1( const sal_Int32* /*pOutGlyphWidths*/)
158 SAL_WARN("vcl.fonts",
159 "CreateFontSubsetFromType1: replace dummy implementation.");
160 return false;
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */