LibreOffice Module vcl (master)  1
fontsubset.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <osl/diagnose.h>
22 #include <sal/log.hxx>
23 
24 #include <fontsubset.hxx>
25 #include <sft.hxx>
26 
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)
42 {
43 }
44 
46 {
47 }
48 
49 // prepare subsetting for fonts where the input font file is mapped
51  FontType eInFontType,
52  const unsigned char* pInFontBytes, int nInByteLength)
53 {
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;
58 }
59 
60 // prepare subsetting for fonts that are known to the SFT-parser
62 {
63  SAL_WARN_IF( (mpInFontBytes != nullptr), "vcl", "Subset from SFT and from mapped font-file requested");
64  mpSftTTFont = pSftTTFont;
66 }
67 
69  FontType nReqFontTypeMask,
70  FILE* pOutFile, const char* pReqFontName,
71  const sal_GlyphId* pReqGlyphIds, const sal_uInt8* pReqEncodedIds, int nReqGlyphCount,
72  sal_Int32* pOutGlyphWidths)
73 {
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;
81 
82  // TODO: move the glyphid/encid/notdef reshuffling from the callers to here
83 
84  // dispatch to underlying subsetters
85  bool bOK = false;
86 
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;
100  bOK = CreateFontSubsetFromType1( pOutGlyphWidths);
101  break;
102  case FontType::NO_FONT:
103  default:
104  OSL_FAIL( "unhandled type in CreateFontSubset()");
105  break;
106  }
107 
108  return bOK;
109 }
110 
111 // TODO: move function to sft.cxx to replace dummy implementation
112 bool FontSubsetInfo::CreateFontSubsetFromSfnt( sal_Int32* pOutGlyphWidths )
113 {
114  // handle SFNT_CFF fonts
115  int nCffLength = 0;
116  const sal_uInt8* pCffBytes = nullptr;
117  if( GetSfntTable( mpSftTTFont, vcl::O_CFF, &pCffBytes, &nCffLength))
118  {
119  LoadFont( FontType::CFF_FONT, pCffBytes, nCffLength);
120  const bool bOK = CreateFontSubsetFromCff( pOutGlyphWidths);
121  return bOK;
122  }
123 
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
136  {
138  aShortGlyphIds.data(), pEncArray, mnReqGlyphCount );
139  }
141  {
143  aShortGlyphIds.data(), pEncArray, mnReqGlyphCount,
144  0 /* 0 = horizontal, 1 = vertical */ );
145  }
147  {
148  // TODO: use CreateTTFromTTGlyphs()
149  // TODO: move functionality from callers here
150  }
151 
152  return (nSFTErr != vcl::SFErrCodes::Ok);
153 }
154 
155 // TODO: replace dummy implementation
156 bool FontSubsetInfo::CreateFontSubsetFromType1( const sal_Int32* /*pOutGlyphWidths*/)
157 {
158  fprintf(stderr,"CreateFontSubsetFromType1: replace dummy implementation\n");
159  return false;
160 }
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const sal_GlyphId * mpReqGlyphIds
Definition: fontsubset.hxx:83
CFF-container with PSType2 glyphs.
bool CreateFontSubset(FontType nOutFontTypeMask, FILE *pOutFile, const char *pOutFontName, const sal_GlyphId *pGlyphIds, const sal_uInt8 *pEncodedIds, int nReqGlyphCount, sal_Int32 *pOutGlyphWidths=nullptr)
Definition: fontsubset.cxx:68
SFErrCodes
Return value of OpenTTFont() and CreateT3FromTTGlyphs()
Definition: sft.hxx:60
bool GetSfntTable(TrueTypeFont const *ttf, int nSubtableIndex, const sal_uInt8 **ppRawBytes, int *pRawLength)
provide access to the raw data of a SFNT-container's subtable
Definition: sft.cxx:2292
constexpr int O_CFF
Definition: sft.hxx:720
bool CreateFontSubsetFromCff(sal_Int32 *pOutGlyphWidths)
Definition: cff.cxx:2038
void LoadFont(FontType eInFontType, const unsigned char *pFontBytes, int nByteLength)
Definition: fontsubset.cxx:50
sal_uInt16 sal_GlyphId
Definition: glyphitem.hxx:26
SFErrCodes CreateT42FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *psname, sal_uInt16 const *glyphArray, sal_uInt8 *encoding, int nGlyphs)
Generates a new PostScript Type42 font and dumps it to outf file.
Definition: sft.cxx:2147
vcl::TrueTypeFont * mpSftTTFont
Definition: fontsubset.hxx:77
const char * mpReqFontName
Definition: fontsubset.hxx:82
SFNT container with CFF-container.
FontType
< SFT's idea of a TTF font
Definition: fontsubset.hxx:32
PSType3 Postscript font.
unsigned const char * mpInFontBytes
Definition: fontsubset.hxx:74
int i
incorrect arguments
PSType1 Postscript Font Ascii.
SFNT container with TrueType glyphs.
FILE * mpOutFile
Definition: fontsubset.hxx:81
FontType mnReqFontTypeMask
allowed subset-target font types
Definition: fontsubset.hxx:80
const sal_uInt8 * mpReqEncodedIds
Definition: fontsubset.hxx:84
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
Sun Font Tools.
PSType42 wrapper for an SFNT_TTF.
FontType meInFontType
allowed mask of input font-types
Definition: fontsubset.hxx:76
PSType1 Postscript Font Binary.
static bool CreateFontSubsetFromType1(const sal_Int32 *pOutGlyphWidths)
Definition: fontsubset.cxx:156
SFErrCodes CreateT3FromTTGlyphs(TrueTypeFont *ttf, FILE *outf, const char *fname, sal_uInt16 const *glyphArray, sal_uInt8 *encoding, int nGlyphs, int wmode)
Generates a new PostScript Type 3 font and dumps it to outf file.
Definition: sft.cxx:1747
bool CreateFontSubsetFromSfnt(sal_Int32 *pOutGlyphWidths)
Definition: fontsubset.cxx:112