Fix scale issues
[profile/ivi/org.tizen.video-player.git] / src / hb-font.cc
1 /*
2  * Copyright (C) 2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-private.h"
28
29 #include "hb-font-private.hh"
30 #include "hb-blob-private.h"
31 #include "hb-open-file-private.hh"
32
33 #include "hb-ot-layout-private.hh"
34
35 #include <string.h>
36
37
38 /*
39  * hb_font_funcs_t
40  */
41
42 static hb_codepoint_t
43 hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
44                        hb_face_t *face HB_UNUSED,
45                        const void *user_data HB_UNUSED,
46                        hb_codepoint_t unicode HB_UNUSED,
47                        hb_codepoint_t variation_selector HB_UNUSED)
48 { return 0; }
49
50 static hb_bool_t
51 hb_font_get_contour_point_nil (hb_font_t *font HB_UNUSED,
52                                hb_face_t *face HB_UNUSED,
53                                const void *user_data HB_UNUSED,
54                                unsigned int point_index HB_UNUSED,
55                                hb_codepoint_t glyph HB_UNUSED,
56                                hb_position_t *x HB_UNUSED,
57                                hb_position_t *y HB_UNUSED)
58 { return false; }
59
60 static void
61 hb_font_get_glyph_metrics_nil (hb_font_t *font HB_UNUSED,
62                                hb_face_t *face HB_UNUSED,
63                                const void *user_data HB_UNUSED,
64                                hb_codepoint_t glyph HB_UNUSED,
65                                hb_glyph_metrics_t *metrics HB_UNUSED)
66 { }
67
68 static hb_position_t
69 hb_font_get_kerning_nil (hb_font_t *font HB_UNUSED,
70                          hb_face_t *face HB_UNUSED,
71                          const void *user_data HB_UNUSED,
72                          hb_codepoint_t first_glyph HB_UNUSED,
73                          hb_codepoint_t second_glyph HB_UNUSED)
74 { return 0; }
75
76 hb_font_funcs_t _hb_font_funcs_nil = {
77   HB_REFERENCE_COUNT_INVALID, /* ref_count */
78
79   TRUE,  /* immutable */
80
81   hb_font_get_glyph_nil,
82   hb_font_get_contour_point_nil,
83   hb_font_get_glyph_metrics_nil,
84   hb_font_get_kerning_nil
85 };
86
87 hb_font_funcs_t *
88 hb_font_funcs_create (void)
89 {
90   hb_font_funcs_t *ffuncs;
91
92   if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
93     return &_hb_font_funcs_nil;
94
95   return ffuncs;
96 }
97
98 hb_font_funcs_t *
99 hb_font_funcs_reference (hb_font_funcs_t *ffuncs)
100 {
101   HB_OBJECT_DO_REFERENCE (ffuncs);
102 }
103
104 unsigned int
105 hb_font_funcs_get_reference_count (hb_font_funcs_t *ffuncs)
106 {
107   HB_OBJECT_DO_GET_REFERENCE_COUNT (ffuncs);
108 }
109
110 void
111 hb_font_funcs_destroy (hb_font_funcs_t *ffuncs)
112 {
113   HB_OBJECT_DO_DESTROY (ffuncs);
114
115   free (ffuncs);
116 }
117
118 hb_font_funcs_t *
119 hb_font_funcs_copy (hb_font_funcs_t *other_ffuncs)
120 {
121   hb_font_funcs_t *ffuncs;
122
123   if (!HB_OBJECT_DO_CREATE (hb_font_funcs_t, ffuncs))
124     return &_hb_font_funcs_nil;
125
126   *ffuncs = *other_ffuncs;
127
128   /* re-init refcount */
129   HB_OBJECT_DO_INIT (ffuncs);
130   ffuncs->immutable = FALSE;
131
132   return ffuncs;
133 }
134
135 void
136 hb_font_funcs_make_immutable (hb_font_funcs_t *ffuncs)
137 {
138   if (HB_OBJECT_IS_INERT (ffuncs))
139     return;
140
141   ffuncs->immutable = TRUE;
142 }
143
144
145 void
146 hb_font_funcs_set_glyph_func (hb_font_funcs_t *ffuncs,
147                               hb_font_get_glyph_func_t glyph_func)
148 {
149   if (ffuncs->immutable)
150     return;
151
152   ffuncs->get_glyph = glyph_func ? glyph_func : hb_font_get_glyph_nil;
153 }
154
155 void
156 hb_font_funcs_set_contour_point_func (hb_font_funcs_t *ffuncs,
157                                       hb_font_get_contour_point_func_t contour_point_func)
158 {
159   if (ffuncs->immutable)
160     return;
161
162   ffuncs->get_contour_point = contour_point_func ? contour_point_func : hb_font_get_contour_point_nil;
163 }
164
165 void
166 hb_font_funcs_set_glyph_metrics_func (hb_font_funcs_t *ffuncs,
167                                       hb_font_get_glyph_metrics_func_t glyph_metrics_func)
168 {
169   if (ffuncs->immutable)
170     return;
171
172   ffuncs->get_glyph_metrics = glyph_metrics_func ? glyph_metrics_func : hb_font_get_glyph_metrics_nil;
173 }
174
175 void
176 hb_font_funcs_set_kerning_func (hb_font_funcs_t *ffuncs,
177                                 hb_font_get_kerning_func_t kerning_func)
178 {
179   if (ffuncs->immutable)
180     return;
181
182   ffuncs->get_kerning = kerning_func ? kerning_func : hb_font_get_kerning_nil;
183 }
184
185
186 hb_codepoint_t
187 hb_font_get_glyph (hb_font_t *font, hb_face_t *face,
188                    hb_codepoint_t unicode, hb_codepoint_t variation_selector)
189 {
190   return font->klass->get_glyph (font, face, font->user_data,
191                                  unicode, variation_selector);
192 }
193
194 hb_bool_t
195 hb_font_get_contour_point (hb_font_t *font, hb_face_t *face,
196                            unsigned int point_index,
197                            hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y)
198 {
199   *x = 0; *y = 0;
200   return font->klass->get_contour_point (font, face, font->user_data,
201                                          point_index,
202                                          glyph, x, y);
203 }
204
205 void
206 hb_font_get_glyph_metrics (hb_font_t *font, hb_face_t *face,
207                            hb_codepoint_t glyph, hb_glyph_metrics_t *metrics)
208 {
209   memset (metrics, 0, sizeof (*metrics));
210   return font->klass->get_glyph_metrics (font, face, font->user_data,
211                                          glyph, metrics);
212 }
213
214 hb_position_t
215 hb_font_get_kerning (hb_font_t *font, hb_face_t *face,
216                      hb_codepoint_t first_glyph, hb_codepoint_t second_glyph)
217 {
218   return font->klass->get_kerning (font, face, font->user_data,
219                                    first_glyph, second_glyph);
220 }
221
222
223 /*
224  * hb_face_t
225  */
226
227 static hb_face_t _hb_face_nil = {
228   HB_REFERENCE_COUNT_INVALID, /* ref_count */
229
230   NULL, /* get_table */
231   NULL, /* destroy */
232   NULL, /* user_data */
233
234   NULL, /* head_blob */
235   NULL, /* head_table */
236
237   NULL  /* ot_layout */
238 };
239
240
241 hb_face_t *
242 hb_face_create_for_tables (hb_get_table_func_t  get_table,
243                            hb_destroy_func_t    destroy,
244                            void                *user_data)
245 {
246   hb_face_t *face;
247
248   if (!HB_OBJECT_DO_CREATE (hb_face_t, face)) {
249     if (destroy)
250       destroy (user_data);
251     return &_hb_face_nil;
252   }
253
254   face->get_table = get_table;
255   face->destroy = destroy;
256   face->user_data = user_data;
257
258   face->ot_layout = _hb_ot_layout_new (face);
259
260   return face;
261 }
262
263
264 typedef struct _hb_face_for_data_closure_t {
265   hb_blob_t *blob;
266   unsigned int  index;
267 } hb_face_for_data_closure_t;
268
269 static hb_face_for_data_closure_t _hb_face_for_data_closure_nil = {
270   &_hb_blob_nil,
271   0
272 };
273
274 static hb_face_for_data_closure_t *
275 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
276 {
277   hb_face_for_data_closure_t *closure;
278
279   closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t));
280   if (unlikely (!closure))
281     return &_hb_face_for_data_closure_nil;
282
283   closure->blob = hb_blob_reference (blob);
284   closure->index = index;
285
286   return closure;
287 }
288
289 static void
290 _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
291 {
292   if (likely (closure != &_hb_face_for_data_closure_nil)) {
293     hb_blob_destroy (closure->blob);
294     free (closure);
295   }
296 }
297
298 static hb_blob_t *
299 _hb_face_for_data_get_table (hb_tag_t tag, void *user_data)
300 {
301   hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
302
303   const OpenTypeFontFile &ot_file = *Sanitizer<OpenTypeFontFile>::lock_instance (data->blob);
304   const OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
305
306   const OpenTypeTable &table = ot_face.get_table_by_tag (tag);
307
308   hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
309
310   hb_blob_unlock (data->blob);
311
312   return blob;
313 }
314
315 hb_face_t *
316 hb_face_create_for_data (hb_blob_t    *blob,
317                          unsigned int  index)
318 {
319   hb_face_t *face;
320
321   if (!HB_OBJECT_DO_CREATE (hb_face_t, face))
322     return &_hb_face_nil;
323
324   face->get_table = _hb_face_for_data_get_table;
325   face->destroy = (hb_destroy_func_t) _hb_face_for_data_closure_destroy;
326   hb_blob_reference (blob);
327   face->user_data = _hb_face_for_data_closure_create (Sanitizer<OpenTypeFontFile>::sanitize (blob), index);
328   hb_blob_destroy (blob);
329
330   face->head_blob = Sanitizer<head>::sanitize (hb_face_get_table (face, HB_OT_TAG_head));
331   face->head_table = Sanitizer<head>::lock_instance (face->head_blob);
332
333   face->ot_layout = _hb_ot_layout_new (face);
334
335   return face;
336 }
337
338
339 hb_face_t *
340 hb_face_reference (hb_face_t *face)
341 {
342   HB_OBJECT_DO_REFERENCE (face);
343 }
344
345 unsigned int
346 hb_face_get_reference_count (hb_face_t *face)
347 {
348   HB_OBJECT_DO_GET_REFERENCE_COUNT (face);
349 }
350
351 void
352 hb_face_destroy (hb_face_t *face)
353 {
354   HB_OBJECT_DO_DESTROY (face);
355
356   _hb_ot_layout_free (face->ot_layout);
357
358   hb_blob_unlock (face->head_blob);
359   hb_blob_destroy (face->head_blob);
360
361   if (face->destroy)
362     face->destroy (face->user_data);
363
364   free (face);
365 }
366
367 hb_blob_t *
368 hb_face_get_table (hb_face_t *face,
369                    hb_tag_t   tag)
370 {
371   hb_blob_t *blob;
372
373   if (unlikely (!face || !face->get_table))
374     return &_hb_blob_nil;
375
376   blob = face->get_table (tag, face->user_data);
377
378   return blob? blob : &_hb_blob_nil;
379 }
380
381
382 /*
383  * hb_font_t
384  */
385
386 static hb_font_t _hb_font_nil = {
387   HB_REFERENCE_COUNT_INVALID, /* ref_count */
388
389   0, /* x_scale */
390   0, /* y_scale */
391
392   0, /* x_ppem */
393   0, /* y_ppem */
394
395   NULL, /* klass */
396   NULL, /* destroy */
397   NULL  /* user_data */
398 };
399
400 hb_font_t *
401 hb_font_create (void)
402 {
403   hb_font_t *font;
404
405   if (!HB_OBJECT_DO_CREATE (hb_font_t, font))
406     return &_hb_font_nil;
407
408   font->klass = &_hb_font_funcs_nil;
409
410   return font;
411 }
412
413 hb_font_t *
414 hb_font_reference (hb_font_t *font)
415 {
416   HB_OBJECT_DO_REFERENCE (font);
417 }
418
419 unsigned int
420 hb_font_get_reference_count (hb_font_t *font)
421 {
422   HB_OBJECT_DO_GET_REFERENCE_COUNT (font);
423 }
424
425 void
426 hb_font_destroy (hb_font_t *font)
427 {
428   HB_OBJECT_DO_DESTROY (font);
429
430   hb_font_funcs_destroy (font->klass);
431   if (font->destroy)
432     font->destroy (font->user_data);
433
434   free (font);
435 }
436
437 void
438 hb_font_set_funcs (hb_font_t         *font,
439                    hb_font_funcs_t   *klass,
440                    hb_destroy_func_t  destroy,
441                    void              *user_data)
442 {
443   if (HB_OBJECT_IS_INERT (font))
444     return;
445
446   if (font->destroy)
447     font->destroy (font->user_data);
448
449   if (!klass)
450     klass = &_hb_font_funcs_nil;
451
452   hb_font_funcs_reference (klass);
453   hb_font_funcs_destroy (font->klass);
454   font->klass = klass;
455   font->destroy = destroy;
456   font->user_data = user_data;
457 }
458
459 void
460 hb_font_set_scale (hb_font_t *font,
461                    unsigned int x_scale,
462                    unsigned int y_scale)
463 {
464   if (HB_OBJECT_IS_INERT (font))
465     return;
466
467   font->x_scale = x_scale;
468   font->y_scale = y_scale;
469 }
470
471 void
472 hb_font_set_ppem (hb_font_t *font,
473                   unsigned int x_ppem,
474                   unsigned int y_ppem)
475 {
476   if (HB_OBJECT_IS_INERT (font))
477     return;
478
479   font->x_ppem = x_ppem;
480   font->y_ppem = y_ppem;
481 }
482