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
23 #include "dwrite_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(dwrite
);
27 #define MS_TTCF_TAG DWRITE_MAKE_OPENTYPE_TAG('t','t','c','f')
28 #define MS_OTTO_TAG DWRITE_MAKE_OPENTYPE_TAG('O','T','T','O')
30 #ifdef WORDS_BIGENDIAN
31 #define GET_BE_WORD(x) (x)
32 #define GET_BE_DWORD(x) (x)
34 #define GET_BE_WORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
35 #define GET_BE_DWORD(x) MAKELONG(GET_BE_WORD(HIWORD(x)), GET_BE_WORD(LOWORD(x)))
64 } CMAP_EncodingRecord
;
69 CMAP_EncodingRecord tables
[1];
76 } CMAP_SegmentedCoverage_group
;
84 CMAP_SegmentedCoverage_group groups
[1];
85 } CMAP_SegmentedCoverage
;
96 } CMAP_SegmentMapping_0
;
98 enum OPENTYPE_CMAP_TABLE_FORMAT
100 OPENTYPE_CMAP_TABLE_SEGMENT_MAPPING
= 4,
101 OPENTYPE_CMAP_TABLE_SEGMENTED_COVERAGE
= 12
104 /* PANOSE is 10 bytes in size, need to pack the structure properly */
105 #include "pshpack2.h"
121 USHORT lowestRecPPEM
;
122 SHORT direction_hint
;
124 SHORT glyphdata_format
;
131 SHORT underlinePosition
;
132 SHORT underlineThickness
;
144 USHORT usWeightClass
;
147 SHORT ySubscriptXSize
;
148 SHORT ySubscriptYSize
;
149 SHORT ySubscriptXOffset
;
150 SHORT ySubscriptYOffset
;
151 SHORT ySuperscriptXSize
;
152 SHORT ySuperscriptYSize
;
153 SHORT ySuperscriptXOffset
;
154 SHORT ySuperscriptYOffset
;
155 SHORT yStrikeoutSize
;
156 SHORT yStrikeoutPosition
;
159 ULONG ulUnicodeRange1
;
160 ULONG ulUnicodeRange2
;
161 ULONG ulUnicodeRange3
;
162 ULONG ulUnicodeRange4
;
165 USHORT usFirstCharIndex
;
166 USHORT usLastCharIndex
;
167 /* According to the Apple spec, original version didn't have the below fields,
168 * version numbers were taken from the OpenType spec.
170 /* version 0 (TrueType 1.5) */
171 USHORT sTypoAscender
;
172 USHORT sTypoDescender
;
176 /* version 1 (TrueType 1.66) */
177 ULONG ulCodePageRange1
;
178 ULONG ulCodePageRange2
;
179 /* version 2 (OpenType 1.2) */
182 USHORT usDefaultChar
;
188 HRESULT
opentype_analyze_font(IDWriteFontFileStream
*stream
, UINT32
* font_count
, DWRITE_FONT_FILE_TYPE
*file_type
, DWRITE_FONT_FACE_TYPE
*face_type
, BOOL
*supported
)
190 /* TODO: Do font validation */
191 const void *font_data
;
196 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, &font_data
, 0, sizeof(TTC_Header_V1
), &context
);
202 *file_type
= DWRITE_FONT_FILE_TYPE_UNKNOWN
;
204 *face_type
= DWRITE_FONT_FACE_TYPE_UNKNOWN
;
207 if (DWRITE_MAKE_OPENTYPE_TAG(tag
[0], tag
[1], tag
[2], tag
[3]) == MS_TTCF_TAG
)
209 const TTC_Header_V1
*header
= font_data
;
210 *font_count
= GET_BE_DWORD(header
->numFonts
);
211 *file_type
= DWRITE_FONT_FILE_TYPE_TRUETYPE_COLLECTION
;
213 *face_type
= DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION
;
216 else if (GET_BE_DWORD(*(DWORD
*)font_data
) == 0x10000)
219 *file_type
= DWRITE_FONT_FILE_TYPE_TRUETYPE
;
221 *face_type
= DWRITE_FONT_FACE_TYPE_TRUETYPE
;
224 else if (DWRITE_MAKE_OPENTYPE_TAG(tag
[0], tag
[1], tag
[2], tag
[3]) == MS_OTTO_TAG
)
226 *file_type
= DWRITE_FONT_FILE_TYPE_CFF
;
229 IDWriteFontFileStream_ReleaseFileFragment(stream
, context
);
233 HRESULT
opentype_get_font_table(IDWriteFontFileStream
*stream
, DWRITE_FONT_FACE_TYPE type
, UINT32 font_index
, UINT32 tag
,
234 const void **table_data
, void **table_context
, UINT32
*table_size
, BOOL
*found
)
237 TTC_SFNT_V1
*font_header
= NULL
;
239 TT_TableRecord
*table_record
= NULL
;
240 void *table_record_context
;
241 int table_count
, table_offset
= 0;
246 if (type
== DWRITE_FONT_FACE_TYPE_TRUETYPE_COLLECTION
) {
247 const TTC_Header_V1
*ttc_header
;
249 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&ttc_header
, 0, sizeof(*ttc_header
), &ttc_context
);
251 table_offset
= GET_BE_DWORD(ttc_header
->OffsetTable
[0]);
252 if (font_index
>= GET_BE_DWORD(ttc_header
->numFonts
))
255 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&font_header
, table_offset
, sizeof(*font_header
), &sfnt_context
);
256 IDWriteFontFileStream_ReleaseFileFragment(stream
, ttc_context
);
260 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&font_header
, 0, sizeof(*font_header
), &sfnt_context
);
265 table_count
= GET_BE_WORD(font_header
->numTables
);
266 table_offset
+= sizeof(*font_header
);
267 for (i
= 0; i
< table_count
; i
++)
269 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, (const void**)&table_record
, table_offset
, sizeof(*table_record
), &table_record_context
);
272 if (DWRITE_MAKE_OPENTYPE_TAG(table_record
->tag
[0], table_record
->tag
[1], table_record
->tag
[2], table_record
->tag
[3]) == tag
)
274 IDWriteFontFileStream_ReleaseFileFragment(stream
, table_record_context
);
275 table_offset
+= sizeof(*table_record
);
278 IDWriteFontFileStream_ReleaseFileFragment(stream
, sfnt_context
);
279 if (SUCCEEDED(hr
) && i
< table_count
)
281 int offset
= GET_BE_DWORD(table_record
->offset
);
282 int length
= GET_BE_DWORD(table_record
->length
);
283 IDWriteFontFileStream_ReleaseFileFragment(stream
, table_record_context
);
286 *table_size
= length
;
287 hr
= IDWriteFontFileStream_ReadFileFragment(stream
, table_data
, offset
, length
, table_context
);
297 static int compare_group(const void *a
, const void* b
)
299 const DWORD
*chr
= a
;
300 const CMAP_SegmentedCoverage_group
*group
= b
;
302 if (*chr
< GET_BE_DWORD(group
->startCharCode
))
304 if (*chr
> GET_BE_DWORD(group
->endCharCode
))
309 static void CMAP4_GetGlyphIndex(CMAP_SegmentMapping_0
* format
, UINT32 utf32c
, UINT16
*pgi
)
316 int segment_count
= GET_BE_WORD(format
->segCountX2
)/2;
317 /* This is correct because of the padding before startCode */
318 startCode
= (WORD
*)((BYTE
*)format
+ sizeof(CMAP_SegmentMapping_0
) + (sizeof(WORD
) * segment_count
));
319 idDelta
= (SHORT
*)(((BYTE
*)startCode
) + (sizeof(WORD
) * segment_count
));
320 idRangeOffset
= (WORD
*)(((BYTE
*)idDelta
) + (sizeof(WORD
) * segment_count
));
323 while(GET_BE_WORD(format
->endCode
[segment
]) < 0xffff)
325 if (utf32c
<= GET_BE_WORD(format
->endCode
[segment
]))
329 if (segment
>= segment_count
)
331 TRACE("Segment %i of %i\n",segment
, segment_count
);
332 if (GET_BE_WORD(startCode
[segment
]) > utf32c
)
334 TRACE("In range %i -> %i\n", GET_BE_WORD(startCode
[segment
]), GET_BE_WORD(format
->endCode
[segment
]));
335 if (GET_BE_WORD(idRangeOffset
[segment
]) == 0)
337 *pgi
= (SHORT
)(GET_BE_WORD(idDelta
[segment
])) + utf32c
;
341 WORD ro
= GET_BE_WORD(idRangeOffset
[segment
])/2;
342 WORD co
= (utf32c
- GET_BE_WORD(startCode
[segment
]));
343 WORD
*index
= (WORD
*)((BYTE
*)&idRangeOffset
[segment
] + (ro
+ co
));
344 *pgi
= GET_BE_WORD(*index
);
348 static void CMAP12_GetGlyphIndex(CMAP_SegmentedCoverage
* format
, UINT32 utf32c
, UINT16
*pgi
)
350 CMAP_SegmentedCoverage_group
*group
;
352 group
= bsearch(&utf32c
, format
->groups
, GET_BE_DWORD(format
->nGroups
),
353 sizeof(CMAP_SegmentedCoverage_group
), compare_group
);
357 DWORD offset
= utf32c
- GET_BE_DWORD(group
->startCharCode
);
358 *pgi
= GET_BE_DWORD(group
->startGlyphID
) + offset
;
362 void opentype_cmap_get_glyphindex(void *data
, UINT32 utf32c
, UINT16
*pgi
)
364 CMAP_Header
*CMAP_Table
= data
;
369 for (i
= 0; i
< GET_BE_WORD(CMAP_Table
->numTables
); i
++)
374 if (GET_BE_WORD(CMAP_Table
->tables
[i
].platformID
) != 3)
377 table
= (WORD
*)(((BYTE
*)CMAP_Table
) + GET_BE_DWORD(CMAP_Table
->tables
[i
].offset
));
378 type
= GET_BE_WORD(*table
);
379 TRACE("table type %i\n", type
);
383 case OPENTYPE_CMAP_TABLE_SEGMENT_MAPPING
:
384 CMAP4_GetGlyphIndex((CMAP_SegmentMapping_0
*) table
, utf32c
, pgi
);
386 case OPENTYPE_CMAP_TABLE_SEGMENTED_COVERAGE
:
387 CMAP12_GetGlyphIndex((CMAP_SegmentedCoverage
*) table
, utf32c
, pgi
);
390 TRACE("table type %i unhandled.\n", type
);
397 static UINT32
opentype_cmap_get_unicode_ranges_count(const CMAP_Header
*CMAP_Table
)
402 for (i
= 0; i
< GET_BE_WORD(CMAP_Table
->numTables
); i
++) {
406 if (GET_BE_WORD(CMAP_Table
->tables
[i
].platformID
) != 3)
409 table
= (WORD
*)(((BYTE
*)CMAP_Table
) + GET_BE_DWORD(CMAP_Table
->tables
[i
].offset
));
410 type
= GET_BE_WORD(*table
);
411 TRACE("table type %i\n", type
);
415 case OPENTYPE_CMAP_TABLE_SEGMENT_MAPPING
:
417 CMAP_SegmentMapping_0
*format
= (CMAP_SegmentMapping_0
*)table
;
418 count
+= GET_BE_WORD(format
->segCountX2
)/2;
421 case OPENTYPE_CMAP_TABLE_SEGMENTED_COVERAGE
:
423 CMAP_SegmentedCoverage
*format
= (CMAP_SegmentedCoverage
*)table
;
424 count
+= GET_BE_DWORD(format
->nGroups
);
428 FIXME("table type %i unhandled.\n", type
);
435 HRESULT
opentype_cmap_get_unicode_ranges(void *data
, UINT32 max_count
, DWRITE_UNICODE_RANGE
*ranges
, UINT32
*count
)
437 CMAP_Header
*CMAP_Table
= data
;
443 *count
= opentype_cmap_get_unicode_ranges_count(CMAP_Table
);
445 for (i
= 0; i
< GET_BE_WORD(CMAP_Table
->numTables
) && k
< max_count
; i
++)
451 if (GET_BE_WORD(CMAP_Table
->tables
[i
].platformID
) != 3)
454 table
= (WORD
*)(((BYTE
*)CMAP_Table
) + GET_BE_DWORD(CMAP_Table
->tables
[i
].offset
));
455 type
= GET_BE_WORD(*table
);
456 TRACE("table type %i\n", type
);
460 case OPENTYPE_CMAP_TABLE_SEGMENT_MAPPING
:
462 CMAP_SegmentMapping_0
*format
= (CMAP_SegmentMapping_0
*)table
;
463 UINT16 segment_count
= GET_BE_WORD(format
->segCountX2
)/2;
464 UINT16
*startCode
= (WORD
*)((BYTE
*)format
+ sizeof(CMAP_SegmentMapping_0
) + (sizeof(WORD
) * segment_count
));
466 for (j
= 0; j
< segment_count
&& GET_BE_WORD(format
->endCode
[j
]) < 0xffff && k
< max_count
; j
++, k
++) {
467 ranges
[k
].first
= GET_BE_WORD(startCode
[j
]);
468 ranges
[k
].last
= GET_BE_WORD(format
->endCode
[j
]);
472 case OPENTYPE_CMAP_TABLE_SEGMENTED_COVERAGE
:
474 CMAP_SegmentedCoverage
*format
= (CMAP_SegmentedCoverage
*)table
;
475 for (j
= 0; j
< GET_BE_DWORD(format
->nGroups
) && k
< max_count
; j
++, k
++) {
476 ranges
[k
].first
= GET_BE_DWORD(format
->groups
[j
].startCharCode
);
477 ranges
[k
].last
= GET_BE_DWORD(format
->groups
[j
].endCharCode
);
482 FIXME("table type %i unhandled.\n", type
);
486 return *count
> max_count
? E_NOT_SUFFICIENT_BUFFER
: S_OK
;
489 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
)
491 TT_OS2_V2
*tt_os2
= (TT_OS2_V2
*)os2
;
492 TT_HEAD
*tt_head
= (TT_HEAD
*)head
;
493 TT_POST
*tt_post
= (TT_POST
*)post
;
495 /* default stretch, weight and style to normal */
496 *stretch
= DWRITE_FONT_STRETCH_NORMAL
;
497 *weight
= DWRITE_FONT_WEIGHT_NORMAL
;
498 *style
= DWRITE_FONT_STYLE_NORMAL
;
500 memset(metrics
, 0, sizeof(*metrics
));
502 /* DWRITE_FONT_STRETCH enumeration values directly match font data values */
505 if (GET_BE_WORD(tt_os2
->usWidthClass
) <= DWRITE_FONT_STRETCH_ULTRA_EXPANDED
)
506 *stretch
= GET_BE_WORD(tt_os2
->usWidthClass
);
508 *weight
= GET_BE_WORD(tt_os2
->usWeightClass
);
509 TRACE("stretch=%d, weight=%d\n", *stretch
, *weight
);
511 metrics
->ascent
= GET_BE_WORD(tt_os2
->sTypoAscender
);
512 metrics
->descent
= GET_BE_WORD(tt_os2
->sTypoDescender
);
513 metrics
->lineGap
= GET_BE_WORD(tt_os2
->sTypoLineGap
);
514 metrics
->capHeight
= GET_BE_WORD(tt_os2
->sCapHeight
);
515 metrics
->xHeight
= GET_BE_WORD(tt_os2
->sxHeight
);
516 metrics
->strikethroughPosition
= GET_BE_WORD(tt_os2
->yStrikeoutPosition
);
517 metrics
->strikethroughThickness
= GET_BE_WORD(tt_os2
->yStrikeoutSize
);
522 USHORT macStyle
= GET_BE_WORD(tt_head
->macStyle
);
523 metrics
->designUnitsPerEm
= GET_BE_WORD(tt_head
->unitsPerEm
);
524 if (macStyle
& 0x0002)
525 *style
= DWRITE_FONT_STYLE_ITALIC
;
531 metrics
->underlinePosition
= GET_BE_WORD(tt_post
->underlinePosition
);
532 metrics
->underlineThickness
= GET_BE_WORD(tt_post
->underlineThickness
);