2 * Methods for dealing with opentype font tables
4 * Copyright 2014 Aric Stewart for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "dwrite_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
28 #define MS_TTCF_TAG DWRITE_MAKE_OPENTYPE_TAG('t','t','c','f')
29 #define MS_OTTO_TAG DWRITE_MAKE_OPENTYPE_TAG('O','T','T','O')
31 #ifdef WORDS_BIGENDIAN
32 #define GET_BE_WORD(x) (x)
33 #define GET_BE_DWORD(x) (x)
35 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
36 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)))
65 } CMAP_EncodingRecord
;
70 CMAP_EncodingRecord tables
[1];
77 } CMAP_SegmentedCoverage_group
;
85 CMAP_SegmentedCoverage_group groups
[1];
86 } CMAP_SegmentedCoverage
;
97 } CMAP_SegmentMapping_0
;
99 /* PANOSE is 10 bytes in size, need to pack the structure properly */
100 #include "pshpack2.h"
116 USHORT lowestRecPPEM
;
117 SHORT direction_hint
;
119 SHORT glyphdata_format
;
126 SHORT underlinePosition
;
127 SHORT underlineThickness
;
139 USHORT usWeightClass
;
142 SHORT ySubscriptXSize
;
143 SHORT ySubscriptYSize
;
144 SHORT ySubscriptXOffset
;
145 SHORT ySubscriptYOffset
;
146 SHORT ySuperscriptXSize
;
147 SHORT ySuperscriptYSize
;
148 SHORT ySuperscriptXOffset
;
149 SHORT ySuperscriptYOffset
;
150 SHORT yStrikeoutSize
;
151 SHORT yStrikeoutPosition
;
154 ULONG ulUnicodeRange1
;
155 ULONG ulUnicodeRange2
;
156 ULONG ulUnicodeRange3
;
157 ULONG ulUnicodeRange4
;
160 USHORT usFirstCharIndex
;
161 USHORT usLastCharIndex
;
162 /* According to the Apple spec, original version didn't have the below fields,
163 * version numbers were taken from the OpenType spec.
165 /* version 0 (TrueType 1.5) */
166 USHORT sTypoAscender
;
167 USHORT sTypoDescender
;
171 /* version 1 (TrueType 1.66) */
172 ULONG ulCodePageRange1
;
173 ULONG ulCodePageRange2
;
174 /* version 2 (OpenType 1.2) */
177 USHORT usDefaultChar
;
183 HRESULT
opentype_analyze_font(IDWriteFontFileStream
*stream
, UINT32
* font_count
, DWRITE_FONT_FILE_TYPE
*file_type
, DWRITE_FONT_FACE_TYPE
*face_type
, BOOL
*supported
)
185 /* TODO: Do font validation */
186 const void *font_data
;
191 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &font_data
, 0, sizeof(TTC_Header_V1
), &context
);
197 *file_type
= DWRITE_FONT_FILE_TYPE_UNKNOWN
;
199 *face_type
= DWRITE_FONT_FACE_TYPE_UNKNOWN
;
202 if (DWRITE_MAKE_OPENTYPE_TAG(tag
[0], tag
[1], tag
[2], tag
[3]) == MS_TTCF_TAG
)
204 const TTC_Header_V1
*header
= font_data
;
205 *font_count
= GET_BE_DWORD(header
->numFonts
);
206 *file_type
= DWRITE_FONT_FILE_TYPE_TRUETYPE_COLLECTION
;
208 *face_type
= DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION
;
211 else if (GET_BE_DWORD(*(DWORD
*)font_data
) == 0x10000)
214 *file_type
= DWRITE_FONT_FILE_TYPE_TRUETYPE
;
216 *face_type
= DWRITE_FONT_FACE_TYPE_TRUETYPE
;
219 else if (DWRITE_MAKE_OPENTYPE_TAG(tag
[0], tag
[1], tag
[2], tag
[3]) == MS_OTTO_TAG
)
221 *file_type
= DWRITE_FONT_FILE_TYPE_CFF
;
224 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
228 HRESULT
find_font_table(IDWriteFontFileStream
*stream
, UINT32 font_index
, UINT32 tag
, const void** table_data
, void** table_context
, UINT32
*table_size
, BOOL
* found
)
230 const CHAR
*first_data
;
233 TTC_SFNT_V1
*font_header
= NULL
;
235 TT_TableRecord
*table_record
= NULL
;
236 void *table_record_context
;
239 int table_offset
= 0;
243 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&first_data
, 0, 4, &first_context
);
246 if (DWRITE_MAKE_OPENTYPE_TAG(first_data
[0], first_data
[1], first_data
[2], first_data
[3]) == MS_TTCF_TAG
)
248 const TTC_Header_V1
*ttc_header
;
250 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&ttc_header
, 0, sizeof(*ttc_header
), &ttc_context
);
253 table_offset
= GET_BE_DWORD(ttc_header
->OffsetTable
[0]);
254 if (font_index
>= GET_BE_DWORD(ttc_header
->numFonts
))
257 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&font_header
, table_offset
, sizeof(*font_header
), &sfnt_context
);
258 IDWriteFontFileStream_ReleaseFileFragment(stream
, ttc_context
);
266 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&font_header
, 0, sizeof(*font_header
), &sfnt_context
);
268 IDWriteFontFileStream_ReleaseFileFragment(stream
, first_context
);
273 table_count
= GET_BE_WORD(font_header
->numTables
);
274 table_offset
+= sizeof(*font_header
);
275 for (i
= 0; i
< table_count
; i
++)
277 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&table_record
, table_offset
, sizeof(*table_record
), &table_record_context
);
280 if (DWRITE_MAKE_OPENTYPE_TAG(table_record
->tag
[0], table_record
->tag
[1], table_record
->tag
[2], table_record
->tag
[3]) == tag
)
282 IDWriteFontFileStream_ReleaseFileFragment(stream
, table_record_context
);
283 table_offset
+= sizeof(*table_record
);
286 IDWriteFontFileStream_ReleaseFileFragment(stream
, sfnt_context
);
287 if (SUCCEEDED(hr
) && i
< table_count
)
289 int offset
= GET_BE_DWORD(table_record
->offset
);
290 int length
= GET_BE_DWORD(table_record
->length
);
291 IDWriteFontFileStream_ReleaseFileFragment(stream
, table_record_context
);
294 *table_size
= length
;
295 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, table_data
, offset
, length
, table_context
);
305 static int compare_group(const void *a
, const void* b
)
307 const DWORD
*chr
= a
;
308 const CMAP_SegmentedCoverage_group
*group
= b
;
310 if (*chr
< GET_BE_DWORD(group
->startCharCode
))
312 if (*chr
> GET_BE_DWORD(group
->endCharCode
))
317 static void CMAP4_GetGlyphIndex(CMAP_SegmentMapping_0
* format
, DWORD utf32c
, LPWORD pgi
)
324 int segment_count
= GET_BE_WORD(format
->segCountX2
)/2;
325 /* This is correct because of the padding before startCode */
326 startCode
= (WORD
*)((BYTE
*)format
+ sizeof(CMAP_SegmentMapping_0
) + (sizeof(WORD
) * segment_count
));
327 idDelta
= (SHORT
*)(((BYTE
*)startCode
) + (sizeof(WORD
) * segment_count
));
328 idRangeOffset
= (WORD
*)(((BYTE
*)idDelta
) + (sizeof(WORD
) * segment_count
));
331 while(GET_BE_WORD(format
->endCode
[segment
]) < 0xffff)
333 if (utf32c
<= GET_BE_WORD(format
->endCode
[segment
]))
337 if (segment
>= segment_count
)
339 TRACE("Segment %i of %i\n",segment
, segment_count
);
340 if (GET_BE_WORD(startCode
[segment
]) > utf32c
)
342 TRACE("In range %i -> %i\n", GET_BE_WORD(startCode
[segment
]), GET_BE_WORD(format
->endCode
[segment
]));
343 if (GET_BE_WORD(idRangeOffset
[segment
]) == 0)
345 *pgi
= (SHORT
)(GET_BE_WORD(idDelta
[segment
])) + utf32c
;
349 WORD ro
= GET_BE_WORD(idRangeOffset
[segment
])/2;
350 WORD co
= (utf32c
- GET_BE_WORD(startCode
[segment
]));
351 WORD
*index
= (WORD
*)((BYTE
*)&idRangeOffset
[segment
] + (ro
+ co
));
352 *pgi
= GET_BE_WORD(*index
);
356 static void CMAP12_GetGlyphIndex(CMAP_SegmentedCoverage
* format
, DWORD utf32c
, LPWORD pgi
)
358 CMAP_SegmentedCoverage_group
*group
= NULL
;
360 group
= bsearch(&utf32c
, format
->groups
, GET_BE_DWORD(format
->nGroups
),
361 sizeof(CMAP_SegmentedCoverage_group
), compare_group
);
365 DWORD offset
= utf32c
- GET_BE_DWORD(group
->startCharCode
);
366 *pgi
= GET_BE_DWORD(group
->startGlyphID
) + offset
;
370 VOID
OpenType_CMAP_GetGlyphIndex(LPVOID data
, DWORD utf32c
, LPWORD pgi
, DWORD flags
)
373 CMAP_Header
*CMAP_Table
= NULL
;
375 if (flags
& GGI_MARK_NONEXISTING_GLYPHS
)
382 for (i
= 0; i
< GET_BE_WORD(CMAP_Table
->numTables
); i
++)
387 if (GET_BE_WORD(CMAP_Table
->tables
[i
].platformID
) != 3)
390 table
= (WORD
*)(((BYTE
*)CMAP_Table
) + GET_BE_DWORD(CMAP_Table
->tables
[i
].offset
));
391 type
= GET_BE_WORD(*table
);
392 TRACE("Type %i\n", type
);
393 /* Break when we find a handled type */
397 CMAP4_GetGlyphIndex((CMAP_SegmentMapping_0
*) table
, utf32c
, pgi
);
400 CMAP12_GetGlyphIndex((CMAP_SegmentedCoverage
*) table
, utf32c
, pgi
);
403 TRACE("Type %i unhandled.\n", type
);
408 VOID
get_font_properties(LPCVOID os2
, LPCVOID head
, LPCVOID post
, DWRITE_FONT_METRICS
*metrics
, DWRITE_FONT_STRETCH
*stretch
, DWRITE_FONT_WEIGHT
*weight
, DWRITE_FONT_STYLE
*style
)
410 TT_OS2_V2
*tt_os2
= (TT_OS2_V2
*)os2
;
411 TT_HEAD
*tt_head
= (TT_HEAD
*)head
;
412 TT_POST
*tt_post
= (TT_POST
*)post
;
414 /* default stretch, weight and style to normal */
415 *stretch
= DWRITE_FONT_STRETCH_NORMAL
;
416 *weight
= DWRITE_FONT_WEIGHT_NORMAL
;
417 *style
= DWRITE_FONT_STYLE_NORMAL
;
419 memset(metrics
, 0, sizeof(*metrics
));
421 /* DWRITE_FONT_STRETCH enumeration values directly match font data values */
424 if (GET_BE_WORD(tt_os2
->usWidthClass
) <= DWRITE_FONT_STRETCH_ULTRA_EXPANDED
)
425 *stretch
= GET_BE_WORD(tt_os2
->usWidthClass
);
427 *weight
= GET_BE_WORD(tt_os2
->usWeightClass
);
428 TRACE("stretch=%d, weight=%d\n", *stretch
, *weight
);
430 metrics
->ascent
= GET_BE_WORD(tt_os2
->sTypoAscender
);
431 metrics
->descent
= GET_BE_WORD(tt_os2
->sTypoDescender
);
432 metrics
->lineGap
= GET_BE_WORD(tt_os2
->sTypoLineGap
);
433 metrics
->capHeight
= GET_BE_WORD(tt_os2
->sCapHeight
);
434 metrics
->xHeight
= GET_BE_WORD(tt_os2
->sxHeight
);
435 metrics
->strikethroughPosition
= GET_BE_WORD(tt_os2
->yStrikeoutPosition
);
436 metrics
->strikethroughThickness
= GET_BE_WORD(tt_os2
->yStrikeoutSize
);
441 USHORT macStyle
= GET_BE_WORD(tt_head
->macStyle
);
442 metrics
->designUnitsPerEm
= GET_BE_WORD(tt_head
->unitsPerEm
);
443 if (macStyle
& 0x0002)
444 *style
= DWRITE_FONT_STYLE_ITALIC
;
450 metrics
->underlinePosition
= GET_BE_WORD(tt_post
->underlinePosition
);
451 metrics
->underlineThickness
= GET_BE_WORD(tt_post
->underlineThickness
);