xref: /aosp_15_r20/external/harfbuzz_ng/util/hb-fc.cc (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 /*
2  * Copyright © 2014  Google, 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  * Google Author(s): Behdad Esfahbod
25  */
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 
30 #include "hb-fc.h"
31 
32 static hb_bool_t
hb_fc_get_glyph(hb_font_t * font,void * font_data,hb_codepoint_t unicode,hb_codepoint_t variation_selector,hb_codepoint_t * glyph,void * user_data)33 hb_fc_get_glyph (hb_font_t *font /*HB_UNUSED*/,
34 		 void *font_data,
35 		 hb_codepoint_t unicode,
36 		 hb_codepoint_t variation_selector,
37 		 hb_codepoint_t *glyph,
38 		 void *user_data /*HB_UNUSED*/)
39 
40 {
41   FcCharSet *cs = (FcCharSet *) font_data;
42 
43   if (variation_selector)
44   {
45     /* Fontconfig doesn't cache cmap-14 info.  However:
46      * 1. If the font maps the variation_selector, assume it's
47      *    supported,
48      * 2. If the font doesn't map it, still say it's supported,
49      *    but return 0.  This way, the caller will see the zero
50      *    and reject.  If we return unsupported here, then the
51      *    variation selector will be hidden and ignored.
52      */
53     if (FcCharSetHasChar (cs, unicode) &&
54 	FcCharSetHasChar (cs, variation_selector))
55     {
56       unsigned int var_num = 0;
57       if (variation_selector - 0xFE00u < 16)
58 	var_num = variation_selector - 0xFE00 + 1;
59       else if (variation_selector - 0xE0100u < (256 - 16))
60 	var_num = variation_selector - 0xE0100 + 17;
61       *glyph = (var_num << 21) | unicode;
62     }
63     else
64     {
65       *glyph = 0;
66     }
67     return true;
68   }
69 
70   *glyph = FcCharSetHasChar (cs, unicode) ? unicode : 0;
71   return *glyph != 0;
72 }
73 
74 static hb_font_funcs_t *
_hb_fc_get_font_funcs()75 _hb_fc_get_font_funcs ()
76 {
77   static const hb_font_funcs_t *fc_ffuncs;
78 
79   if (!fc_ffuncs)
80   {
81     hb_font_funcs_t *newfuncs = hb_font_funcs_create ();
82 
83     hb_font_funcs_set_glyph_func (newfuncs, hb_fc_get_glyph, nullptr, nullptr);
84 
85     /* XXX MT-unsafe */
86     if (fc_ffuncs)
87       hb_font_funcs_destroy (newfuncs);
88     else
89       fc_ffuncs = newfuncs;
90   }
91 
92   return const_cast<hb_font_funcs_t *> (fc_ffuncs);
93 }
94 
95 
96 hb_font_t *
hb_fc_font_create(FcPattern * fcfont)97 hb_fc_font_create (FcPattern *fcfont)
98 {
99   static hb_face_t *face;
100   hb_font_t *font;
101 
102   FcCharSet *cs;
103   if (FcResultMatch != FcPatternGetCharSet (fcfont, FC_CHARSET, 0, &cs))
104     return hb_font_get_empty ();
105 
106   if (!face) /* XXX MT-unsafe */
107     face = hb_face_create (hb_blob_get_empty (), 0);
108 
109   font = hb_font_create (face);
110 
111   hb_font_set_funcs (font,
112 		     _hb_fc_get_font_funcs (),
113 		     FcCharSetCopy (cs),
114 		     (hb_destroy_func_t) FcCharSetDestroy);
115 
116   return font;
117 }
118 
119 hb_bool_t
hb_fc_can_render(hb_font_t * font,const char * text)120 hb_fc_can_render (hb_font_t *font, const char *text)
121 {
122   static const char *ot[] = {"ot", nullptr};
123 
124   hb_buffer_t *buffer = hb_buffer_create ();
125   hb_buffer_add_utf8 (buffer, text, -1, 0, -1);
126 
127   /* XXX Do we need this?  I think Arabic and Hangul shapers are the
128    * only one that make any use of this.  The Hangul case is not really
129    * needed, and for Arabic we'll miss a very narrow set of fonts.
130    * Might be better to force generic shaper perhaps. */
131   hb_buffer_guess_segment_properties (buffer);
132 
133   if (!hb_shape_full (font, buffer, nullptr, 0, ot))
134     abort (); /* hb-ot shaper not enabled? */
135 
136   unsigned int len;
137   hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &len);
138   for (unsigned int i = 0; i < len; i++)
139    {
140     if (!info[i].codepoint)
141      {
142       return false;
143      }
144    }
145 
146   return true;
147 }
148