| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software |
| 18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "system.h" |
| 26 |
#include "mem.h" |
| 27 |
#include "error.h" |
| 28 |
#include "numbers.h" |
| 29 |
#include "dpxconf.h" |
| 30 |
|
| 31 |
#include "pdfobj.h" |
| 32 |
|
| 33 |
#include "sfnt.h" |
| 34 |
#include "tt_table.h" |
| 35 |
#include "tt_post.h" |
| 36 |
#include "tt_aux.h" |
| 37 |
|
| 38 |
ULONG ttc_read_offset (sfnt *sfont, ULONG ttc_idx) |
| 39 |
{ |
| 40 |
ULONG offset = 0, num_dirs = 0; |
| 41 |
|
| 42 |
if (sfont == NULL || sfont->stream == NULL) |
| 43 |
ERROR("file not opened"); |
| 44 |
|
| 45 |
if (sfont->type != SFNT_TYPE_TTC) |
| 46 |
ERROR("ttc_read_offset(): invalid font type"); |
| 47 |
|
| 48 |
sfnt_seek_set (sfont, 4); /* skip version tag */ |
| 49 |
|
| 50 |
/* version = */ sfnt_get_ulong(sfont); |
| 51 |
num_dirs = sfnt_get_ulong(sfont); |
| 52 |
if (ttc_idx > num_dirs - 1) |
| 53 |
ERROR("Invalid TTC index number"); |
| 54 |
|
| 55 |
sfnt_seek_set (sfont, 12 + ttc_idx * 4); |
| 56 |
offset = sfnt_get_ulong (sfont); |
| 57 |
|
| 58 |
return offset; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
Build FontDescriptor (except FontName) from TrueType tables: |
| 63 |
|
| 64 |
Most information found in FontDescriptor is used only when automatic |
| 65 |
font substitution is needed. (in the case of missing/broken font data) |
| 66 |
Some PDF viewers may ignore embedded TrueType glyph data. Especially, |
| 67 |
any embedded TrueType data for CID-keyed (CIDFontType 2) font is ignored |
| 68 |
by PDF viewers that only support PDF versions 1.2 or earlier. |
| 69 |
|
| 70 |
We use those tables to obtain various values of FontDescriptor. |
| 71 |
|
| 72 |
head: required |
| 73 |
|
| 74 |
xMin, xMax, yMin, yMax - FontBBox |
| 75 |
unitsPerEm - conversion to PDF unit (points). see PDFUNIT bellow. |
| 76 |
The head table must exist in any TrueType font. |
| 77 |
|
| 78 |
hhea: required |
| 79 |
|
| 80 |
When the OS/2 table (Windows and OS/2 only) is not available, |
| 81 |
Ascender and Descender values can be used to estimate Ascent |
| 82 |
and Descent. The hhea table is required for all TrueType fonts. |
| 83 |
MaxWidth can be obtained from this table. |
| 84 |
|
| 85 |
OS/2: required for Windows and OS/2 TrueType, and OpenType |
| 86 |
|
| 87 |
fsType - liscensing information |
| 88 |
sCapHeight - CapHeight (version 2 only) |
| 89 |
|
| 90 |
The sCapHeight is only available in newer TrueType fonts which has |
| 91 |
version 2 OS/2 table and generally not available. Instead, we can |
| 92 |
use height of uppercase letter `H'. But we don't use it, we simply |
| 93 |
use Ascent value. |
| 94 |
|
| 95 |
sTypoAscender, sTypoDescender - Ascent and Descent |
| 96 |
usWeightClass - roughly estimate StemV. |
| 97 |
|
| 98 |
Estimation is based on the following expression: |
| 99 |
|
| 100 |
stemv = (os2->usWeightClass/65)*(os2->usWeightClass/65)+50 |
| 101 |
|
| 102 |
. I've found this expression in some Adobe document (lost). We use |
| 103 |
this expression also, otherwise, we must analyze glyph data. |
| 104 |
|
| 105 |
xAvgCharWidth - AvgWidth (optional) |
| 106 |
sFamilyClass - Flags |
| 107 |
sFamilyClass and panose - Panose in Style dictionary (optional) |
| 108 |
|
| 109 |
post: required |
| 110 |
|
| 111 |
italicAngle - ItalicAngle |
| 112 |
|
| 113 |
*/ |
| 114 |
|
| 115 |
|
| 116 |
#ifndef PDFUNIT |
| 117 |
#define PDFUNIT(v) (ROUND((1000.0*(v))/(head->unitsPerEm),1)) |
| 118 |
#endif |
| 119 |
|
| 120 |
/* Flags: should not come here */ |
| 121 |
#define FIXEDWIDTH (1 << 0) /* Fixed-width font */ |
| 122 |
#define SERIF (1 << 1) /* Serif font */ |
| 123 |
#define SYMBOLIC (1 << 2) /* Symbolic font */ |
| 124 |
#define SCRIPT (1 << 3) /* Script font */ |
| 125 |
#define STANDARD (1 << 5) /* Uses the Adobe Standard Character Set */ |
| 126 |
#define ITALIC (1 << 6) /* Italic */ |
| 127 |
#define ALLCAP (1 << 16) /* All-cap font */ |
| 128 |
#define SMALLCAP (1 << 17) /* Small-cap font */ |
| 129 |
#define FORCEBOLD (1 << 18) /* Force bold at small text sizes */ |
| 130 |
pdf_obj *tt_get_fontdesc (sfnt *sfont, int *embed, int stemv, int type, const char* fontname) |
| 131 |
{ |
| 132 |
pdf_obj *descriptor = NULL; |
| 133 |
pdf_obj *bbox = NULL; |
| 134 |
int flag = SYMBOLIC; |
| 135 |
/* TrueType tables */ |
| 136 |
struct tt_head_table *head; |
| 137 |
struct tt_os2__table *os2; |
| 138 |
struct tt_post_table *post; |
| 139 |
|
| 140 |
if (!sfont) { |
| 141 |
ERROR("font file not opened"); |
| 142 |
} |
| 143 |
|
| 144 |
os2 = tt_read_os2__table(sfont); |
| 145 |
head = tt_read_head_table(sfont); |
| 146 |
post = tt_read_post_table(sfont); |
| 147 |
if (!post) { |
| 148 |
if (os2) |
| 149 |
RELEASE(os2); |
| 150 |
RELEASE(head); |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
descriptor = pdf_new_dict(); |
| 155 |
pdf_add_dict (descriptor, |
| 156 |
pdf_new_name ("Type"), |
| 157 |
pdf_new_name ("FontDescriptor")); |
| 158 |
|
| 159 |
if (*embed && os2) { |
| 160 |
/* |
| 161 |
License: |
| 162 |
|
| 163 |
"Preview & Print embedding" (0x004) requires the document containing |
| 164 |
Preview & Print font to be opened in read-only mode. However, licensing |
| 165 |
information are lost when fonts are embedded in PDF document and |
| 166 |
the only way to make the PDF document "read-only" is to encrypt it. |
| 167 |
But we have no support for encryption yet. We do not embed any fonts |
| 168 |
with "Preview & Print embedding" setting. |
| 169 |
|
| 170 |
2001/11/22: Changed to allow `Preview & Print' only fonts embedding |
| 171 |
|
| 172 |
2006/04/19: Added support for always_embed option |
| 173 |
*/ |
| 174 |
if (os2->fsType == 0x0000 || (os2->fsType & 0x0008)) { |
| 175 |
/* the least restrictive license granted takes precedence. */ |
| 176 |
*embed = 1; |
| 177 |
} else if (os2->fsType & 0x0004) { |
| 178 |
if (dpx_conf.verbose_level > 0) |
| 179 |
WARN("Font \"%s\" permits \"Preview & Print\" embedding only **\n", fontname); |
| 180 |
*embed = 1; |
| 181 |
} else { |
| 182 |
if (dpx_conf.ignore_font_license) { |
| 183 |
if (dpx_conf.verbose_level > 0) |
| 184 |
WARN("Font \"%s\" may be subject to embedding restrictions **\n", fontname); |
| 185 |
*embed = 1; |
| 186 |
} |
| 187 |
else { |
| 188 |
if (dpx_conf.verbose_level > 0) |
| 189 |
WARN("Embedding of font \"%s\" disabled due to license restrictions", fontname); |
| 190 |
*embed = 0; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if (os2) { |
| 196 |
pdf_add_dict (descriptor, |
| 197 |
pdf_new_name ("Ascent"), |
| 198 |
pdf_new_number (PDFUNIT(os2->sTypoAscender))); |
| 199 |
pdf_add_dict (descriptor, |
| 200 |
pdf_new_name ("Descent"), |
| 201 |
pdf_new_number (PDFUNIT(os2->sTypoDescender))); |
| 202 |
if (stemv < 0) /* if not given by the option '-v' */ |
| 203 |
stemv = (os2->usWeightClass/65.)*(os2->usWeightClass/65.)+50; |
| 204 |
pdf_add_dict (descriptor, |
| 205 |
pdf_new_name ("StemV"), |
| 206 |
pdf_new_number (stemv)); |
| 207 |
if (os2->version == 0x0002) { |
| 208 |
pdf_add_dict (descriptor, |
| 209 |
pdf_new_name("CapHeight"), |
| 210 |
pdf_new_number(PDFUNIT(os2->sCapHeight))); |
| 211 |
/* optional */ |
| 212 |
pdf_add_dict (descriptor, |
| 213 |
pdf_new_name("XHeight"), |
| 214 |
pdf_new_number(PDFUNIT(os2->sxHeight))); |
| 215 |
} else { /* arbitrary */ |
| 216 |
pdf_add_dict (descriptor, |
| 217 |
pdf_new_name("CapHeight"), |
| 218 |
pdf_new_number(PDFUNIT(os2->sTypoAscender))); |
| 219 |
} |
| 220 |
/* optional */ |
| 221 |
if (os2->xAvgCharWidth != 0) { |
| 222 |
pdf_add_dict (descriptor, |
| 223 |
pdf_new_name ("AvgWidth"), |
| 224 |
pdf_new_number (PDFUNIT(os2->xAvgCharWidth))); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/* BoundingBox (array) */ |
| 229 |
bbox = pdf_new_array (); |
| 230 |
pdf_add_array (bbox, pdf_new_number (PDFUNIT(head->xMin))); |
| 231 |
pdf_add_array (bbox, pdf_new_number (PDFUNIT(head->yMin))); |
| 232 |
pdf_add_array (bbox, pdf_new_number (PDFUNIT(head->xMax))); |
| 233 |
pdf_add_array (bbox, pdf_new_number (PDFUNIT(head->yMax))); |
| 234 |
pdf_add_dict (descriptor, pdf_new_name ("FontBBox"), bbox); |
| 235 |
|
| 236 |
/* post */ |
| 237 |
pdf_add_dict (descriptor, |
| 238 |
pdf_new_name ("ItalicAngle"), |
| 239 |
pdf_new_number(fixed(post->italicAngle))); |
| 240 |
|
| 241 |
/* Flags */ |
| 242 |
if (os2) { |
| 243 |
if (os2->fsSelection & (1 << 0)) |
| 244 |
flag |= ITALIC; |
| 245 |
if (os2->fsSelection & (1 << 5)) |
| 246 |
flag |= FORCEBOLD; |
| 247 |
if (((os2->sFamilyClass >> 8) & 0xff) != 8) |
| 248 |
flag |= SERIF; |
| 249 |
if (((os2->sFamilyClass >> 8) & 0xff) == 10) |
| 250 |
flag |= SCRIPT; |
| 251 |
if (post->isFixedPitch) |
| 252 |
flag |= FIXEDWIDTH; |
| 253 |
} |
| 254 |
|
| 255 |
pdf_add_dict (descriptor, |
| 256 |
pdf_new_name ("Flags"), |
| 257 |
pdf_new_number (flag)); |
| 258 |
|
| 259 |
/* insert panose if you want */ |
| 260 |
if (type == 0 && os2) { /* cid-keyed font - add panose */ |
| 261 |
pdf_obj *styledict = NULL; |
| 262 |
unsigned char panose[12]; |
| 263 |
|
| 264 |
panose[0] = os2->sFamilyClass >> 8; |
| 265 |
panose[1] = os2->sFamilyClass & 0xff; |
| 266 |
memcpy(panose+2, os2->panose, 10); |
| 267 |
|
| 268 |
styledict = pdf_new_dict (); |
| 269 |
pdf_add_dict (styledict, pdf_new_name ("Panose"), |
| 270 |
pdf_new_string (panose, 12)); |
| 271 |
pdf_add_dict (descriptor, pdf_new_name ("Style"), styledict); |
| 272 |
} |
| 273 |
|
| 274 |
RELEASE(head); |
| 275 |
if (os2) |
| 276 |
RELEASE(os2); |
| 277 |
tt_release_post_table(post); |
| 278 |
|
| 279 |
return descriptor; |
| 280 |
} |
| 281 |
|