hb-ft.cc 6.7 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
HB_BEGIN_DECLS


39 40
static hb_bool_t
hb_ft_get_contour_point (hb_font_t *font HB_UNUSED,
41
			 void *font_data,
42
			 hb_codepoint_t glyph,
43
			 unsigned int point_index,
44 45
			 hb_position_t *x,
			 hb_position_t *y,
46
			 void *user_data HB_UNUSED)
B
Behdad Esfahbod 已提交
47
{
48 49
  FT_Face ft_face = (FT_Face) font_data;
  int load_flags = FT_LOAD_DEFAULT;
50

51
  /* TODO: load_flags, embolden, etc */
52

53 54 55 56 57 58 59 60 61 62 63 64 65
  if (unlikely (FT_Load_Glyph (ft_face, glyph, load_flags)))
      return FALSE;

  if (unlikely (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE))
      return FALSE;

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

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

  return TRUE;
B
Behdad Esfahbod 已提交
66 67
}

68 69
static void
hb_ft_get_glyph_advance (hb_font_t *font HB_UNUSED,
70
			 void *font_data,
71 72
			 hb_codepoint_t glyph,
			 hb_position_t *x_advance,
73
			 hb_position_t *y_advance,
74
			 void *user_data HB_UNUSED)
75
{
76
  FT_Face ft_face = (FT_Face) font_data;
77 78 79 80 81 82 83 84 85 86 87 88 89
  int load_flags = FT_LOAD_DEFAULT;

  /* TODO: load_flags, embolden, etc */

  if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
  {
    *x_advance = ft_face->glyph->advance.x;
    *y_advance = ft_face->glyph->advance.y;
  }
}

static void
hb_ft_get_glyph_extents (hb_font_t *font HB_UNUSED,
90
			 void *font_data,
91
			 hb_codepoint_t glyph,
92
			 hb_glyph_extents_t *extents,
93
			 void *user_data HB_UNUSED)
94
{
95
  FT_Face ft_face = (FT_Face) font_data;
96 97 98 99 100 101 102 103 104 105 106 107 108 109
  int load_flags = FT_LOAD_DEFAULT;

  /* TODO: load_flags, embolden, etc */

  if (likely (!FT_Load_Glyph (ft_face, glyph, load_flags)))
  {
    /* XXX: A few negations should be in order here, not sure. */
    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;
  }
}

110 111
static hb_codepoint_t
hb_ft_get_glyph (hb_font_t *font HB_UNUSED,
112
		 void *font_data,
113 114
		 hb_codepoint_t unicode,
		 hb_codepoint_t variation_selector,
115
		 void *user_data HB_UNUSED)
B
Behdad Esfahbod 已提交
116

117 118
{
  FT_Face ft_face = (FT_Face) font_data;
B
Behdad Esfahbod 已提交
119

120 121 122 123 124 125 126
#ifdef HAVE_FT_FACE_GETCHARVARIANTINDEX
  if (unlikely (variation_selector)) {
    hb_codepoint_t glyph = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector);
    if (glyph)
      return glyph;
  }
#endif
B
Behdad Esfahbod 已提交
127

128
  return FT_Get_Char_Index (ft_face, unicode);
B
Behdad Esfahbod 已提交
129 130
}

131
static void
132
hb_ft_get_kerning (hb_font_t *font HB_UNUSED,
133
		   void *font_data,
134 135
		   hb_codepoint_t left_glyph,
		   hb_codepoint_t right_glyph,
136 137
		   hb_position_t *x_kern,
		   hb_position_t *y_kern,
138
		   void *user_data HB_UNUSED)
B
Behdad Esfahbod 已提交
139
{
140
  FT_Face ft_face = (FT_Face) font_data;
141 142 143
  FT_Vector kerning;

  /* TODO: Kern type? */
144
  if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerning))
145
    return;
146

147 148
  *x_kern = kerning.x;
  *y_kern = kerning.y;
B
Behdad Esfahbod 已提交
149 150 151
}

static hb_font_funcs_t ft_ffuncs = {
152 153
  HB_OBJECT_HEADER_STATIC,

B
Behdad Esfahbod 已提交
154
  TRUE, /* immutable */
155

B
Behdad Esfahbod 已提交
156
  {
157
    hb_ft_get_contour_point,
158 159
    hb_ft_get_glyph_advance,
    hb_ft_get_glyph_extents,
160
    hb_ft_get_glyph,
B
Behdad Esfahbod 已提交
161 162
    hb_ft_get_kerning
  }
B
Behdad Esfahbod 已提交
163 164 165 166 167 168 169 170 171 172
};

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


static hb_blob_t *
B
Behdad Esfahbod 已提交
173
get_table  (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
B
Behdad Esfahbod 已提交
174 175 176 177 178 179
{
  FT_Face ft_face = (FT_Face) user_data;
  FT_Byte *buffer;
  FT_ULong  length = 0;
  FT_Error error;

180
  if (unlikely (tag == HB_TAG_NONE))
181
    return NULL;
182

B
Behdad Esfahbod 已提交
183 184
  error = FT_Load_Sfnt_Table (ft_face, tag, 0, NULL, &length);
  if (error)
185
    return NULL;
B
Behdad Esfahbod 已提交
186

187
  buffer = (FT_Byte *) malloc (length);
B
Behdad Esfahbod 已提交
188
  if (buffer == NULL)
189
    return NULL;
B
Behdad Esfahbod 已提交
190 191 192

  error = FT_Load_Sfnt_Table (ft_face, tag, 0, buffer, &length);
  if (error)
193
    return NULL;
B
Behdad Esfahbod 已提交
194 195 196

  return hb_blob_create ((const char *) buffer, length,
			 HB_MEMORY_MODE_WRITABLE,
197
			 buffer, free);
B
Behdad Esfahbod 已提交
198 199 200 201 202 203 204 205 206
}


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

207
  if (ft_face->stream->read == NULL) {
B
Behdad Esfahbod 已提交
208 209 210 211
    hb_blob_t *blob;

    blob = hb_blob_create ((const char *) ft_face->stream->base,
			   (unsigned int) ft_face->stream->size,
212
			   /* TODO: Check FT_FACE_FLAG_EXTERNAL_STREAM? */
B
Behdad Esfahbod 已提交
213
			   HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE,
214
			   ft_face, destroy);
215
    face = hb_face_create (blob, ft_face->face_index);
B
Behdad Esfahbod 已提交
216 217
    hb_blob_destroy (blob);
  } else {
218
    face = hb_face_create_for_tables (get_table, ft_face, destroy);
B
Behdad Esfahbod 已提交
219 220 221 222 223
  }

  return face;
}

B
Behdad Esfahbod 已提交
224 225 226
static void
hb_ft_face_finalize (FT_Face ft_face)
{
227
  hb_face_destroy ((hb_face_t *) ft_face->generic.data);
B
Behdad Esfahbod 已提交
228 229
}

B
Behdad Esfahbod 已提交
230 231 232
hb_face_t *
hb_ft_face_create_cached (FT_Face ft_face)
{
233
  if (unlikely (!ft_face->generic.data || ft_face->generic.finalizer != (FT_Generic_Finalizer) hb_ft_face_finalize))
B
Behdad Esfahbod 已提交
234 235
  {
    if (ft_face->generic.finalizer)
B
Behdad Esfahbod 已提交
236
      ft_face->generic.finalizer (ft_face);
B
Behdad Esfahbod 已提交
237

B
Behdad Esfahbod 已提交
238
    ft_face->generic.data = hb_ft_face_create (ft_face, NULL);
B
Behdad Esfahbod 已提交
239
    ft_face->generic.finalizer = (FT_Generic_Finalizer) hb_ft_face_finalize;
B
Behdad Esfahbod 已提交
240
  }
B
Behdad Esfahbod 已提交
241

242
  return hb_face_reference ((hb_face_t *) ft_face->generic.data);
B
Behdad Esfahbod 已提交
243 244
}

B
Behdad Esfahbod 已提交
245 246 247 248 249 250

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

B
Behdad Esfahbod 已提交
253 254 255
  face = hb_ft_face_create (ft_face, destroy);
  font = hb_font_create (face);
  hb_face_destroy (face);
B
Behdad Esfahbod 已提交
256 257
  hb_font_set_funcs (font,
		     hb_ft_get_font_funcs (),
B
Behdad Esfahbod 已提交
258
		     ft_face, NULL);
B
Behdad Esfahbod 已提交
259
  hb_font_set_scale (font,
B
Behdad Esfahbod 已提交
260 261
		     ((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 已提交
262 263 264 265 266 267
  hb_font_set_ppem (font,
		    ft_face->size->metrics.x_ppem,
		    ft_face->size->metrics.y_ppem);

  return font;
}
B
Behdad Esfahbod 已提交
268 269 270


HB_END_DECLS