Skip to content

Commit 08ac195

Browse files
committed
Allow using HarfBuzz without FreeType
1 parent e9853f3 commit 08ac195

File tree

2 files changed

+205
-2
lines changed

2 files changed

+205
-2
lines changed

src/hb-coretext.cc

Lines changed: 202 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ release_table_data (void *user_data)
4545
}
4646

4747
static hb_blob_t *
48-
reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
48+
reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
4949
{
5050
CGFontRef cg_font = reinterpret_cast<CGFontRef> (user_data);
5151
CFDataRef cf_data = CGFontCopyTableForTag (cg_font, tag);
@@ -204,6 +204,207 @@ hb_coretext_font_get_ct_font (hb_font_t *font)
204204
return font_data->ct_font;
205205
}
206206

207+
static hb_bool_t
208+
hb_coretext_get_glyph (hb_font_t *font,
209+
void *font_data HB_UNUSED,
210+
hb_codepoint_t unicode,
211+
hb_codepoint_t variation_selector,
212+
hb_codepoint_t *glyph,
213+
void *user_data HB_UNUSED)
214+
{
215+
CTFontRef ct_font = hb_coretext_font_get_ct_font(font);
216+
const UniChar characters[] = { unicode };
217+
CGGlyph cg_glyph;
218+
CTFontGetGlyphsForCharacters(ct_font, characters, &cg_glyph, 1);
219+
*glyph = cg_glyph;
220+
return *glyph != 0;
221+
}
222+
223+
static hb_position_t
224+
hb_coretext_get_glyph_h_advance (hb_font_t *font,
225+
void *font_data HB_UNUSED,
226+
hb_codepoint_t glyph,
227+
void *user_data HB_UNUSED)
228+
{
229+
CTFontRef ct_font = hb_coretext_font_get_ct_font(font);
230+
231+
const CGGlyph glyphs[] = { glyph };
232+
CGSize advances[1];
233+
CTFontGetAdvancesForGlyphs(
234+
ct_font, kCTFontHorizontalOrientation, glyphs, advances, 1);
235+
236+
return hb_position_t(advances[0].width);
237+
}
238+
239+
static hb_position_t
240+
hb_coretext_get_glyph_v_advance (hb_font_t *font,
241+
void *font_data HB_UNUSED,
242+
hb_codepoint_t glyph,
243+
void *user_data HB_UNUSED)
244+
{
245+
CTFontRef ct_font = hb_coretext_font_get_ct_font(font);
246+
247+
const CGGlyph glyphs[] = { glyph };
248+
CGSize advances[1];
249+
CTFontGetAdvancesForGlyphs(
250+
ct_font, kCTFontHorizontalOrientation, glyphs, advances, 1);
251+
252+
return hb_position_t(advances[0].height);
253+
}
254+
255+
static hb_bool_t
256+
hb_coretext_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
257+
void *font_data HB_UNUSED,
258+
hb_codepoint_t glyph HB_UNUSED,
259+
hb_position_t *x HB_UNUSED,
260+
hb_position_t *y HB_UNUSED,
261+
void *user_data HB_UNUSED)
262+
{
263+
/* We always work in the horizontal coordinates. */
264+
return true;
265+
}
266+
267+
static hb_bool_t
268+
hb_coretext_get_glyph_v_origin (hb_font_t *font,
269+
void *font_data HB_UNUSED,
270+
hb_codepoint_t glyph,
271+
hb_position_t *x,
272+
hb_position_t *y,
273+
void *user_data HB_UNUSED)
274+
{
275+
CTFontRef ct_font = hb_coretext_font_get_ct_font(font);
276+
277+
const CGGlyph glyphs[] = { glyph };
278+
CGRect rect = CTFontGetBoundingRectsForGlyphs(
279+
ct_font, kCTFontHorizontalOrientation, glyphs, NULL, 1);
280+
281+
*x = hb_position_t(rect.origin.x);
282+
*y = hb_position_t(rect.origin.y);
283+
284+
return true;
285+
}
286+
287+
static hb_position_t
288+
hb_coretext_get_glyph_h_kerning (hb_font_t *font,
289+
void *font_data HB_UNUSED,
290+
hb_codepoint_t left_glyph,
291+
hb_codepoint_t right_glyph,
292+
void *user_data HB_UNUSED)
293+
{
294+
return 0;
295+
}
296+
297+
static hb_position_t
298+
hb_coretext_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
299+
void *font_data HB_UNUSED,
300+
hb_codepoint_t top_glyph HB_UNUSED,
301+
hb_codepoint_t bottom_glyph HB_UNUSED,
302+
void *user_data HB_UNUSED)
303+
{
304+
return 0;
305+
}
306+
307+
static hb_bool_t
308+
hb_coretext_get_glyph_extents (hb_font_t *font,
309+
void *font_data HB_UNUSED,
310+
hb_codepoint_t glyph,
311+
hb_glyph_extents_t *extents,
312+
void *user_data HB_UNUSED)
313+
{
314+
CTFontRef ct_font = hb_coretext_font_get_ct_font(font);
315+
316+
const CGGlyph glyphs[] = { glyph };
317+
CGRect rect = CTFontGetBoundingRectsForGlyphs(
318+
ct_font, kCTFontHorizontalOrientation, glyphs, NULL, 1);
319+
320+
extents->x_bearing = rect.origin.x;
321+
extents->y_bearing = rect.origin.y;
322+
extents->width = rect.size.width;
323+
extents->height = rect.size.height;
324+
325+
return true;
326+
}
327+
328+
static hb_bool_t
329+
hb_coretext_get_glyph_contour_point (hb_font_t *font,
330+
void *font_data HB_UNUSED,
331+
hb_codepoint_t glyph,
332+
unsigned int point_index,
333+
hb_position_t *x,
334+
hb_position_t *y,
335+
void *user_data HB_UNUSED)
336+
{
337+
return false;
338+
}
339+
340+
static hb_bool_t
341+
hb_coretext_get_glyph_name (hb_font_t *font,
342+
void *font_data HB_UNUSED,
343+
hb_codepoint_t glyph,
344+
char *name, unsigned int size,
345+
void *user_data HB_UNUSED)
346+
{
347+
CGFontRef cg_font = hb_coretext_face_get_cg_font(font->face);
348+
349+
CFStringRef cf_name_str = CGFontCopyGlyphNameForGlyph(cg_font, glyph);
350+
if (name) {
351+
hb_bool_t ret =
352+
CFStringGetCString(cf_name_str, name, size, kCFStringEncodingUTF8);
353+
CFRelease(name);
354+
return ret;
355+
}
356+
357+
return false;
358+
}
359+
360+
static hb_bool_t
361+
hb_coretext_get_glyph_from_name (hb_font_t *font,
362+
void *font_data HB_UNUSED,
363+
const char *name, int len, /* -1 means nul-terminated */
364+
hb_codepoint_t *glyph,
365+
void *user_data HB_UNUSED)
366+
{
367+
CTFontRef ct_font = hb_coretext_font_get_ct_font(font);
368+
CFStringRef cf_name_str;
369+
if (len == -1) {
370+
cf_name_str = CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
371+
} else {
372+
cf_name_str = CFStringCreateWithBytes(
373+
NULL, (const UInt8*) name, len, kCFStringEncodingUTF8, false);
374+
}
375+
376+
*glyph = CTFontGetGlyphWithName(ct_font, cf_name_str);
377+
CFRelease(cf_name_str);
378+
379+
return *glyph != 0;
380+
}
381+
382+
static hb_font_funcs_t *
383+
_hb_coretext_get_font_funcs (void)
384+
{
385+
static const hb_font_funcs_t coretext_ffuncs = {
386+
HB_OBJECT_HEADER_STATIC,
387+
388+
true, /* immutable */
389+
390+
{
391+
#define HB_FONT_FUNC_IMPLEMENT(name) hb_coretext_get_##name,
392+
HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
393+
#undef HB_FONT_FUNC_IMPLEMENT
394+
}
395+
};
396+
397+
return const_cast<hb_font_funcs_t *> (&coretext_ffuncs);
398+
}
399+
400+
void
401+
hb_coretext_font_set_funcs (hb_font_t *font)
402+
{
403+
hb_font_set_funcs (font,
404+
_hb_coretext_get_font_funcs (),
405+
NULL,
406+
(hb_destroy_func_t) NULL);
407+
}
207408

208409
/*
209410
* shaper

src/hb-coretext.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ HB_BEGIN_DECLS
4747
hb_face_t *
4848
hb_coretext_face_create (CGFontRef cg_font);
4949

50-
5150
CGFontRef
5251
hb_coretext_face_get_cg_font (hb_face_t *face);
5352

5453
CTFontRef
5554
hb_coretext_font_get_ct_font (hb_font_t *font);
5655

56+
/* Makes an hb_font_t use Core Text internally to implement font functions. */
57+
void
58+
hb_coretext_font_set_funcs (hb_font_t *font);
5759

5860
HB_END_DECLS
5961

0 commit comments

Comments
 (0)