1 /**************************************************************************** 2 * 3 * pfrcmap.c 4 * 5 * FreeType PFR cmap handling (body). 6 * 7 * Copyright (C) 2002-2020 by 8 * David Turner, Robert Wilhelm, and Werner Lemberg. 9 * 10 * This file is part of the FreeType project, and may only be used, 11 * modified, and distributed under the terms of the FreeType project 12 * license, LICENSE.TXT. By continuing to use, modify, or distribute 13 * this file you indicate that you have read the license and 14 * understand and accept it fully. 15 * 16 */ 17 18 19 #include <freetype/internal/ftdebug.h> 20 #include "pfrcmap.h" 21 #include "pfrobjs.h" 22 23 #include "pfrerror.h" 24 25 26 FT_CALLBACK_DEF( FT_Error ) pfr_cmap_init(PFR_CMap cmap,FT_Pointer pointer)27 pfr_cmap_init( PFR_CMap cmap, 28 FT_Pointer pointer ) 29 { 30 FT_Error error = FT_Err_Ok; 31 PFR_Face face = (PFR_Face)FT_CMAP_FACE( cmap ); 32 33 FT_UNUSED( pointer ); 34 35 36 cmap->num_chars = face->phy_font.num_chars; 37 cmap->chars = face->phy_font.chars; 38 39 /* just for safety, check that the character entries are correctly */ 40 /* sorted in increasing character code order */ 41 { 42 FT_UInt n; 43 44 45 for ( n = 1; n < cmap->num_chars; n++ ) 46 { 47 if ( cmap->chars[n - 1].char_code >= cmap->chars[n].char_code ) 48 { 49 error = FT_THROW( Invalid_Table ); 50 goto Exit; 51 } 52 } 53 } 54 55 Exit: 56 return error; 57 } 58 59 60 FT_CALLBACK_DEF( void ) pfr_cmap_done(PFR_CMap cmap)61 pfr_cmap_done( PFR_CMap cmap ) 62 { 63 cmap->chars = NULL; 64 cmap->num_chars = 0; 65 } 66 67 68 FT_CALLBACK_DEF( FT_UInt ) pfr_cmap_char_index(PFR_CMap cmap,FT_UInt32 char_code)69 pfr_cmap_char_index( PFR_CMap cmap, 70 FT_UInt32 char_code ) 71 { 72 FT_UInt min = 0; 73 FT_UInt max = cmap->num_chars; 74 75 76 while ( min < max ) 77 { 78 PFR_Char gchar; 79 FT_UInt mid; 80 81 82 mid = min + ( max - min ) / 2; 83 gchar = cmap->chars + mid; 84 85 if ( gchar->char_code == char_code ) 86 return mid + 1; 87 88 if ( gchar->char_code < char_code ) 89 min = mid + 1; 90 else 91 max = mid; 92 } 93 return 0; 94 } 95 96 97 FT_CALLBACK_DEF( FT_UInt32 ) pfr_cmap_char_next(PFR_CMap cmap,FT_UInt32 * pchar_code)98 pfr_cmap_char_next( PFR_CMap cmap, 99 FT_UInt32 *pchar_code ) 100 { 101 FT_UInt result = 0; 102 FT_UInt32 char_code = *pchar_code + 1; 103 104 105 Restart: 106 { 107 FT_UInt min = 0; 108 FT_UInt max = cmap->num_chars; 109 FT_UInt mid; 110 PFR_Char gchar; 111 112 113 while ( min < max ) 114 { 115 mid = min + ( ( max - min ) >> 1 ); 116 gchar = cmap->chars + mid; 117 118 if ( gchar->char_code == char_code ) 119 { 120 result = mid; 121 if ( result != 0 ) 122 { 123 result++; 124 goto Exit; 125 } 126 127 char_code++; 128 goto Restart; 129 } 130 131 if ( gchar->char_code < char_code ) 132 min = mid + 1; 133 else 134 max = mid; 135 } 136 137 /* we didn't find it, but we have a pair just above it */ 138 char_code = 0; 139 140 if ( min < cmap->num_chars ) 141 { 142 gchar = cmap->chars + min; 143 result = min; 144 if ( result != 0 ) 145 { 146 result++; 147 char_code = gchar->char_code; 148 } 149 } 150 } 151 152 Exit: 153 *pchar_code = char_code; 154 return result; 155 } 156 157 158 FT_CALLBACK_TABLE_DEF const FT_CMap_ClassRec 159 pfr_cmap_class_rec = 160 { 161 sizeof ( PFR_CMapRec ), 162 163 (FT_CMap_InitFunc) pfr_cmap_init, /* init */ 164 (FT_CMap_DoneFunc) pfr_cmap_done, /* done */ 165 (FT_CMap_CharIndexFunc)pfr_cmap_char_index, /* char_index */ 166 (FT_CMap_CharNextFunc) pfr_cmap_char_next, /* char_next */ 167 168 (FT_CMap_CharVarIndexFunc) NULL, /* char_var_index */ 169 (FT_CMap_CharVarIsDefaultFunc)NULL, /* char_var_default */ 170 (FT_CMap_VariantListFunc) NULL, /* variant_list */ 171 (FT_CMap_CharVariantListFunc) NULL, /* charvariant_list */ 172 (FT_CMap_VariantCharListFunc) NULL /* variantchar_list */ 173 }; 174 175 176 /* END */ 177