hb-ft.cc 9.2 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2 3
 * Copyright © 2009  Red Hat, Inc.
 * Copyright © 2009  Keith Stribley
B
Behdad Esfahbod 已提交
4
 *
B
Behdad Esfahbod 已提交
5
 *  This is part of HarfBuzz, a text shaping library.
B
Behdad Esfahbod 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Red Hat Author(s): Behdad Esfahbod
 */

28
#include "hb-private.hh"
B
Behdad Esfahbod 已提交
29 30 31

#include "hb-ft.h"

32
#include "hb-font-private.hh"
B
Behdad Esfahbod 已提交
33 34 35

#include FT_TRUETYPE_TABLES_H

B
Behdad Esfahbod 已提交
36 37


38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* TODO:
 *
 * In general, this file does a fine job of what it's supposed to do.
 * There are, however, things that need more work:
 *
 *   - We don't handle any load_flags.  That definitely has API implications. :(
 *     I believe hb_ft_font_create() should take load_flags input.
 *
 *   - We don't handle / allow for emboldening / obliqueing.
 *
 *   - In the future, we should add constructors to create fonts in font space.
 *
 *   - I believe transforms are not correctly implemented.  FreeType does not
 *     provide any API to get to the transform/delta set on the face. :(
 *
 *   - Always use FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH?
 */


57
static hb_bool_t
58 59 60 61 62 63 64
hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
		 void *font_data,
		 hb_codepoint_t unicode,
		 hb_codepoint_t variation_selector,
		 hb_codepoint_t *glyph,
		 void *user_data HB_UNUSED)

B
Behdad Esfahbod 已提交
65
{
66
  FT_Face ft_face = (FT_Face) font_data;
67

68 69 70 71 72 73 74 75 76 77 78 79
#ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
  if (unlikely (variation_selector)) {
    *glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
    if (*glyph)
      return TRUE;
  }
#endif

  *glyph = FT_Get_Char_Index (ft_face, unicode);
  return *glyph != 0;
}

80
static hb_position_t
81 82 83 84 85 86 87
hb_ft_get_glyph_h_advance (hb_font_t *font HB_UNUSED,
			   void *font_data,
			   hb_codepoint_t glyph,
			   void *user_data HB_UNUSED)
{
  FT_Face ft_face = (FT_Face) font_data;
  int load_flags = FT_LOAD_DEFAULT;
88

89
  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
90
    return 0;
91

92
  return ft_face->glyph->metrics.horiAdvance;
93
}
94

95
static hb_position_t
96 97 98 99 100 101 102
hb_ft_get_glyph_v_advance (hb_font_t *font HB_UNUSED,
			   void *font_data,
			   hb_codepoint_t glyph,
			   void *user_data HB_UNUSED)
{
  FT_Face ft_face = (FT_Face) font_data;
  int load_flags = FT_LOAD_DEFAULT;
103

104
  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
105
    return 0;
106

B
Behdad Esfahbod 已提交
107 108
  /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
   * have a Y growing upward.  Hence the extra negation. */
109
  return -ft_face->glyph->metrics.vertAdvance;
B
Behdad Esfahbod 已提交
110 111
}

B
Behdad Esfahbod 已提交
112 113 114 115
static hb_bool_t
hb_ft_get_glyph_h_origin (hb_font_t *font HB_UNUSED,
			  void *font_data HB_UNUSED,
			  hb_codepoint_t glyph HB_UNUSED,
B
Behdad Esfahbod 已提交
116 117
			  hb_position_t *x HB_UNUSED,
			  hb_position_t *y HB_UNUSED,
B
Behdad Esfahbod 已提交
118 119 120 121 122 123
			  void *user_data HB_UNUSED)
{
  /* We always work in the horizontal coordinates. */
  return TRUE;
}

124 125 126 127
static hb_bool_t
hb_ft_get_glyph_v_origin (hb_font_t *font HB_UNUSED,
			  void *font_data,
			  hb_codepoint_t glyph,
B
Behdad Esfahbod 已提交
128 129
			  hb_position_t *x,
			  hb_position_t *y,
130
			  void *user_data HB_UNUSED)
131
{
132
  FT_Face ft_face = (FT_Face) font_data;
133 134
  int load_flags = FT_LOAD_DEFAULT;

135 136
  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
    return FALSE;
137

B
Behdad Esfahbod 已提交
138 139 140 141 142
  /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
   * have a Y growing upward.  Hence the extra negation. */
  *x = ft_face->glyph->metrics.horiBearingX -   ft_face->glyph->metrics.vertBearingX;
  *y = ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY);

143
  return TRUE;
144 145
}

146
static hb_position_t
B
Behdad Esfahbod 已提交
147 148 149 150 151
hb_ft_get_glyph_h_kerning (hb_font_t *font HB_UNUSED,
			   void *font_data,
			   hb_codepoint_t left_glyph,
			   hb_codepoint_t right_glyph,
			   void *user_data HB_UNUSED)
152 153
{
  FT_Face ft_face = (FT_Face) font_data;
154
  FT_Vector kerningv;
155

156
  if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerningv))
157
    return 0;
158

159
  return kerningv.x;
160 161
}

162
static hb_position_t
B
Behdad Esfahbod 已提交
163 164 165 166 167
hb_ft_get_glyph_v_kerning (hb_font_t *font HB_UNUSED,
			   void *font_data HB_UNUSED,
			   hb_codepoint_t top_glyph HB_UNUSED,
			   hb_codepoint_t bottom_glyph HB_UNUSED,
			   void *user_data HB_UNUSED)
168 169
{
  /* FreeType API doesn't support vertical kerning */
170
  return 0;
171 172 173
}

static hb_bool_t
174
hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
175
			 void *font_data,
176
			 hb_codepoint_t glyph,
177
			 hb_glyph_extents_t *extents,
178
			 void *user_data HB_UNUSED)
179
{
180
  FT_Face ft_face = (FT_Face) font_data;
181 182
  int load_flags = FT_LOAD_DEFAULT;

183 184 185 186 187 188 189 190
  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
    return FALSE;

  extents->x_bearing = ft_face->glyph->metrics.horiBearingX;
  extents->y_bearing = ft_face->glyph->metrics.horiBearingY;
  extents->width = ft_face->glyph->metrics.width;
  extents->height = ft_face->glyph->metrics.height;
  return TRUE;
191 192
}

193
static hb_bool_t
B
Behdad Esfahbod 已提交
194 195 196 197 198 199 200
hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
			       void *font_data,
			       hb_codepoint_t glyph,
			       unsigned int point_index,
			       hb_position_t *x,
			       hb_position_t *y,
			       void *user_data HB_UNUSED)
201 202
{
  FT_Face ft_face = (FT_Face) font_data;
203
  int load_flags = FT_LOAD_DEFAULT;
B
Behdad Esfahbod 已提交
204

205 206
  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
      return FALSE;
B
Behdad Esfahbod 已提交
207

208 209
  if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
      return FALSE;
B
Behdad Esfahbod 已提交
210

211 212
  if (unlikely (point_index >= (unsigned int) ft_face->glyph->outline.n_points))
      return FALSE;
213

214 215
  *x = ft_face->glyph->outline.points[point_index].x;
  *y = ft_face->glyph->outline.points[point_index].y;
216

217
  return TRUE;
B
Behdad Esfahbod 已提交
218 219 220
}

static hb_font_funcs_t ft_ffuncs = {
221 222
  HB_OBJECT_HEADER_STATIC,

B
Behdad Esfahbod 已提交
223
  TRUE, /* immutable */
224

B
Behdad Esfahbod 已提交
225
  {
226
    hb_ft_get_glyph,
227 228
    hb_ft_get_glyph_h_advance,
    hb_ft_get_glyph_v_advance,
B
Behdad Esfahbod 已提交
229
    hb_ft_get_glyph_h_origin,
230
    hb_ft_get_glyph_v_origin,
B
Behdad Esfahbod 已提交
231 232
    hb_ft_get_glyph_h_kerning,
    hb_ft_get_glyph_v_kerning,
233
    hb_ft_get_glyph_extents,
B
Behdad Esfahbod 已提交
234
    hb_ft_get_glyph_contour_point,
B
Behdad Esfahbod 已提交
235
  }
B
Behdad Esfahbod 已提交
236 237 238 239 240 241 242 243 244 245
};

hb_font_funcs_t *
hb_ft_get_font_funcs (void)
{
  return &ft_ffuncs;
}


static hb_blob_t *
B
Behdad Esfahbod 已提交
246
get_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
B
Behdad Esfahbod 已提交
247 248 249 250 251 252
{
  FT_Face ft_face = (FT_Face) user_data;
  FT_Byte *buffer;
  FT_ULong  length = 0;
  FT_Error error;

253
  if (unlikely (tag == HB_TAG_NONE))
254
    return NULL;
255

B
Behdad Esfahbod 已提交
256 257
  error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
  if (error)
258
    return NULL;
B
Behdad Esfahbod 已提交
259

260
  buffer = (FT_Byte *) malloc (length);
B
Behdad Esfahbod 已提交
261
  if (buffer == NULL)
262
    return NULL;
B
Behdad Esfahbod 已提交
263 264 265

  error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
  if (error)
266
    return NULL;
B
Behdad Esfahbod 已提交
267 268 269

  return hb_blob_create ((const char *) buffer, length,
			 HB_MEMORY_MODE_WRITABLE,
270
			 buffer, free);
B
Behdad Esfahbod 已提交
271 272 273 274 275 276 277 278 279
}


hb_face_t *
hb_ft_face_create (FT_Face           ft_face,
		   hb_destroy_func_t destroy)
{
  hb_face_t *face;

280
  if (ft_face->stream->read == NULL) {
B
Behdad Esfahbod 已提交
281 282 283 284
    hb_blob_t *blob;

    blob = hb_blob_create ((const char *) ft_face->stream->base,
			   (unsigned int) ft_face->stream->size,
285 286 287 288
			   /* TODO: We assume that it's mmap()'ed, but FreeType code
			    * suggests that there are cases we reach here but font is
			    * not mmapped.  For example, when mmap() fails.  No idea
			    * how to deal with it better here. */
B
Behdad Esfahbod 已提交
289
			   HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
290
			   ft_face, destroy);
291
    face = hb_face_create (blob, ft_face->face_index);
B
Behdad Esfahbod 已提交
292 293
    hb_blob_destroy (blob);
  } else {
294
    face = hb_face_create_for_tables (get_table, ft_face, destroy);
B
Behdad Esfahbod 已提交
295 296 297 298 299
  }

  return face;
}

B
Behdad Esfahbod 已提交
300 301 302
static void
hb_ft_face_finalize (FT_Face ft_face)
{
303
  hb_face_destroy ((hb_face_t *) ft_face->generic.data);
B
Behdad Esfahbod 已提交
304 305
}

B
Behdad Esfahbod 已提交
306 307 308
hb_face_t *
hb_ft_face_create_cached (FT_Face ft_face)
{
309
  if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
B
Behdad Esfahbod 已提交
310 311
  {
    if (ft_face->generic.finalizer)
B
Behdad Esfahbod 已提交
312
      ft_face->generic.finalizer (ft_face);
B
Behdad Esfahbod 已提交
313

B
Behdad Esfahbod 已提交
314
    ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
B
Behdad Esfahbod 已提交
315
    ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
B
Behdad Esfahbod 已提交
316
  }
B
Behdad Esfahbod 已提交
317

318
  return hb_face_reference ((hb_face_t *) ft_face->generic.data);
B
Behdad Esfahbod 已提交
319 320
}

B
Behdad Esfahbod 已提交
321 322 323 324 325 326

hb_font_t *
hb_ft_font_create (FT_Face           ft_face,
		   hb_destroy_func_t destroy)
{
  hb_font_t *font;
B
Behdad Esfahbod 已提交
327
  hb_face_t *face;
B
Behdad Esfahbod 已提交
328

B
Behdad Esfahbod 已提交
329 330 331
  face = hb_ft_face_create (ft_face, destroy);
  font = hb_font_create (face);
  hb_face_destroy (face);
B
Behdad Esfahbod 已提交
332 333
  hb_font_set_funcs (font,
		     hb_ft_get_font_funcs (),
B
Behdad Esfahbod 已提交
334
		     ft_face, NULL);
B
Behdad Esfahbod 已提交
335
  hb_font_set_scale (font,
B
Behdad Esfahbod 已提交
336 337
		     ((uint64_t) ft_face->size->metrics.x_scale * (uint64_t) ft_face->units_per_EM) >> 16,
		     ((uint64_t) ft_face->size->metrics.y_scale * (uint64_t) ft_face->units_per_EM) >> 16);
B
Behdad Esfahbod 已提交
338 339 340 341 342 343
  hb_font_set_ppem (font,
		    ft_face->size->metrics.x_ppem,
		    ft_face->size->metrics.y_ppem);

  return font;
}
B
Behdad Esfahbod 已提交
344 345