| 1 |
/* |
| 2 |
* ttflib.c |
| 3 |
* |
| 4 |
* This file is part of the ttf2pk package. |
| 5 |
* |
| 6 |
* Copyright 1997-1999, 2000 by |
| 7 |
* Loyer Frederic <[email protected]> |
| 8 |
* Werner Lemberg <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifdef HAVE_CONFIG_H |
| 12 |
#include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <math.h> /* libc ANSI */ |
| 19 |
#include <ctype.h> |
| 20 |
|
| 21 |
#include "pklib.h" /* for the `byte' type */ |
| 22 |
#include <ft2build.h> |
| 23 |
#include FT_FREETYPE_H |
| 24 |
#include FT_OUTLINE_H |
| 25 |
#include FT_BBOX_H |
| 26 |
#include "ttfenc.h" |
| 27 |
#include "ttflib.h" |
| 28 |
#include "errormsg.h" |
| 29 |
#include "newobj.h" |
| 30 |
#include "ttf2tfm.h" |
| 31 |
#include "ftlib.h" |
| 32 |
|
| 33 |
|
| 34 |
FT_BBox bbox; |
| 35 |
|
| 36 |
FT_Bitmap Bit; |
| 37 |
unsigned char *Bitp; |
| 38 |
|
| 39 |
int ppem; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static void |
| 44 |
SetRasterArea(int quiet) |
| 45 |
{ |
| 46 |
int temp1_x, temp1_y, temp2_x, temp2_y; |
| 47 |
|
| 48 |
|
| 49 |
temp1_x = bbox.xMin / 64; /* scaling F16.6 -> int */ |
| 50 |
temp1_y = bbox.yMin / 64; |
| 51 |
temp2_x = (bbox.xMax + 63) / 64; |
| 52 |
temp2_y = (bbox.yMax + 63) / 64; |
| 53 |
|
| 54 |
if (!quiet) |
| 55 |
printf(" off = (%d, %d)", 5 - temp1_x, 5 - temp1_y); |
| 56 |
|
| 57 |
if (!quiet) |
| 58 |
printf(" bbox = (%d, %d) <-> (%d, %d)\n", |
| 59 |
temp1_x, temp1_y, temp2_x, temp2_y); |
| 60 |
|
| 61 |
Bit.rows = temp2_y - temp1_y + 10; |
| 62 |
Bit.width = temp2_x - temp1_x + 10; |
| 63 |
|
| 64 |
Bit.pitch = (Bit.width + 7) / 8; /* convert to # of bytes */ |
| 65 |
|
| 66 |
/* |
| 67 |
* We allocate one more row to have valid pointers for comparison |
| 68 |
* purposes in pklib.c, making `gcc -fbounds-checking' happy. |
| 69 |
*/ |
| 70 |
if (Bitp) |
| 71 |
free(Bitp); |
| 72 |
Bitp = mymalloc(Bit.rows*Bit.pitch + Bit.pitch); |
| 73 |
Bit.buffer = Bitp; |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
CopyBit(FT_Bitmap *b2,FT_Bitmap *b1) |
| 78 |
{ |
| 79 |
int y; |
| 80 |
char *p1, *p2; |
| 81 |
|
| 82 |
p1 = (char *)b1->buffer; /* source */ |
| 83 |
p2 = (char *)b2->buffer; /* dest */ |
| 84 |
|
| 85 |
for (y = 0; y < b1->rows; y++) |
| 86 |
{ |
| 87 |
memcpy(p2, p1, b1->pitch); |
| 88 |
p1 += b1->pitch; |
| 89 |
p2 += b2->pitch; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#if 0 |
| 94 |
|
| 95 |
static void |
| 96 |
Output(FT_Bitmap Bit) |
| 97 |
{ |
| 98 |
int x; |
| 99 |
int y; |
| 100 |
int i; |
| 101 |
|
| 102 |
char *p, b; |
| 103 |
|
| 104 |
|
| 105 |
p = Bit.buffer; |
| 106 |
printf("====\n"); |
| 107 |
|
| 108 |
for (y = 0; y < Bit.rows; y++) |
| 109 |
{ |
| 110 |
printf("%3d:", y); |
| 111 |
for (x = 0; x < Bit.pitch; x++) |
| 112 |
{ |
| 113 |
b = *p++; |
| 114 |
for(i = 0x80; i; i >>= 1) |
| 115 |
printf((b & i) ? "x" : "."); |
| 116 |
} |
| 117 |
printf("\n"); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
#endif /* 0 */ |
| 122 |
|
| 123 |
|
| 124 |
void |
| 125 |
TTFopen(char *filename, Font *fnt, int new_dpi, int new_ptsize, Boolean quiet) |
| 126 |
{ |
| 127 |
dpi = new_dpi; |
| 128 |
ptsize = new_ptsize; |
| 129 |
ppem = (dpi * ptsize + 36) / 72; |
| 130 |
|
| 131 |
if (!quiet) |
| 132 |
printf("dpi = %d, ptsize = %d, ppem = %d\n\n", dpi, ptsize, ppem); |
| 133 |
|
| 134 |
FTopen(filename, fnt, False, quiet); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
static FT_Error |
| 139 |
LoadTrueTypeChar(Font *fnt, |
| 140 |
int idx, |
| 141 |
Boolean hint, |
| 142 |
Boolean quiet) |
| 143 |
{ |
| 144 |
FT_Error error; |
| 145 |
int flags; |
| 146 |
|
| 147 |
|
| 148 |
flags = FT_LOAD_DEFAULT; |
| 149 |
if (hint) |
| 150 |
flags |= FT_LOAD_FORCE_AUTOHINT; |
| 151 |
|
| 152 |
error = FT_Load_Glyph(face, idx, flags); |
| 153 |
if (!error) |
| 154 |
{ |
| 155 |
if (fnt->efactor != 1.0 || fnt->slant != 0.0 ) |
| 156 |
FT_Outline_Transform(&face->glyph->outline, &matrix1); |
| 157 |
if (fnt->rotate) |
| 158 |
{ |
| 159 |
FT_Outline_Transform(&face->glyph->outline, &matrix2); |
| 160 |
error = FT_Outline_Get_BBox(&face->glyph->outline, &bbox); /* we need the non- |
| 161 |
grid-fitted bbox */ |
| 162 |
if (!error) |
| 163 |
FT_Outline_Translate(&face->glyph->outline, |
| 164 |
face->glyph->metrics.vertBearingY - bbox.xMin, |
| 165 |
-fnt->y_offset * ppem * 64); |
| 166 |
} |
| 167 |
} |
| 168 |
if (!error) |
| 169 |
error = FT_Outline_Get_BBox(&face->glyph->outline, &bbox); |
| 170 |
if (!error) |
| 171 |
{ |
| 172 |
FT_Outline_Get_CBox(&face->glyph->outline, &bbox); /* for the case of BBox != CBox */ |
| 173 |
SetRasterArea(quiet); |
| 174 |
} |
| 175 |
return error; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
Boolean |
| 180 |
TTFprocess(Font *fnt, |
| 181 |
long Code, |
| 182 |
byte **bitmap, |
| 183 |
int *width, int *height, |
| 184 |
int *hoff, int *voff, |
| 185 |
Boolean hinting, |
| 186 |
Boolean quiet) |
| 187 |
{ |
| 188 |
int Num; |
| 189 |
FT_Error error; |
| 190 |
|
| 191 |
|
| 192 |
if (!bitmap || !width || !height || !hoff || !voff) |
| 193 |
oops("Invalid parameter in call to TTFprocess()"); |
| 194 |
|
| 195 |
if (Code >= 0x1000000) |
| 196 |
Num = Code & 0xFFFFFF; |
| 197 |
else |
| 198 |
{ |
| 199 |
Num = FT_Get_Char_Index(face, Code); |
| 200 |
|
| 201 |
/* now we try to get a vertical glyph form */ |
| 202 |
if (has_gsub) |
| 203 |
Num = Get_Vert(Num); |
| 204 |
} |
| 205 |
|
| 206 |
if ((error = LoadTrueTypeChar(fnt, Num, hinting, quiet)) == FT_Err_Ok) |
| 207 |
{ |
| 208 |
error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO); |
| 209 |
if (error) { |
| 210 |
warning("Cannot Render to Bitmap"); |
| 211 |
return False; |
| 212 |
} |
| 213 |
|
| 214 |
memset(Bit.buffer, 0, Bit.rows*Bit.pitch); |
| 215 |
CopyBit(&Bit, &face->glyph->bitmap); |
| 216 |
|
| 217 |
*bitmap = Bit.buffer; |
| 218 |
*width = Bit.width; |
| 219 |
*height = face->glyph->bitmap.rows; |
| 220 |
*hoff = -face->glyph->bitmap_left; |
| 221 |
*voff = *height + 1 - face->glyph->bitmap_top; |
| 222 |
/* Output(Bit); */ |
| 223 |
return True; |
| 224 |
} |
| 225 |
else |
| 226 |
return False; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/* |
| 231 |
* We collect first all glyphs addressed via the cmap. Then we fill the |
| 232 |
* array up with glyphs not in the cmap. |
| 233 |
* |
| 234 |
* If PSnames is set to `Only', we get the first 256 glyphs which have |
| 235 |
* names different from `.notdef', `.null', and `nonmarkingreturn'. |
| 236 |
* |
| 237 |
* For nicer output, we return the glyph names in an encoding array. |
| 238 |
*/ |
| 239 |
|
| 240 |
encoding * |
| 241 |
TTFget_first_glyphs(Font *fnt, long *array) |
| 242 |
{ |
| 243 |
unsigned int i, j, Num=0; |
| 244 |
unsigned int index_array[257]; /* we ignore glyph index 0 */ |
| 245 |
char buff[128]; |
| 246 |
const char *n; |
| 247 |
encoding *e = (encoding *)mymalloc(sizeof (encoding)); |
| 248 |
|
| 249 |
|
| 250 |
if (!array) |
| 251 |
oops("Invalid parameter in call to TTFget_first_glyphs()"); |
| 252 |
|
| 253 |
for (i = 0; i < 257; i++) |
| 254 |
index_array[i] = 0; |
| 255 |
|
| 256 |
j = 0; |
| 257 |
if (fnt->PSnames != Only) |
| 258 |
{ |
| 259 |
for (i = 0; i <= 0x16FFFF; i++) |
| 260 |
{ |
| 261 |
Num = FT_Get_Char_Index(face, i); |
| 262 |
if (Num == 0) |
| 263 |
continue; |
| 264 |
if (Num <= 256) |
| 265 |
index_array[Num] = 1; |
| 266 |
|
| 267 |
if (fnt->PSnames) |
| 268 |
{ |
| 269 |
(void)FT_Get_Glyph_Name(face,Num,buff,128); |
| 270 |
n = newstring(buff); |
| 271 |
} |
| 272 |
else |
| 273 |
n = code_to_adobename(i); |
| 274 |
if (strcmp(n, ".notdef") == 0) |
| 275 |
continue; |
| 276 |
if (strcmp(n, ".null") == 0) |
| 277 |
continue; |
| 278 |
if (strcmp(n, "nonmarkingreturn") == 0) |
| 279 |
continue; |
| 280 |
|
| 281 |
if (j < 256) |
| 282 |
{ |
| 283 |
array[j] = i; |
| 284 |
e->vec[j] = n; |
| 285 |
} |
| 286 |
else |
| 287 |
return e; |
| 288 |
j++; |
| 289 |
} |
| 290 |
|
| 291 |
if (!fnt->PSnames) |
| 292 |
{ |
| 293 |
for (i = 1; i < face->num_glyphs; i++) |
| 294 |
{ |
| 295 |
if (index_array[i] == 0) |
| 296 |
{ |
| 297 |
if (j < 256) |
| 298 |
{ |
| 299 |
array[j] = i | 0x1000000; |
| 300 |
e->vec[j] = code_to_adobename(i | 0x1000000); |
| 301 |
} |
| 302 |
else |
| 303 |
return e; |
| 304 |
j++; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
else |
| 310 |
{ |
| 311 |
for (i = 0; i < face->num_glyphs; i++) |
| 312 |
{ |
| 313 |
(void)FT_Get_Glyph_Name(face,Num,buff,128); |
| 314 |
n = newstring(buff); |
| 315 |
|
| 316 |
if (strcmp(n, ".notdef") == 0) |
| 317 |
continue; |
| 318 |
if (strcmp(n, ".null") == 0) |
| 319 |
continue; |
| 320 |
if (strcmp(n, "nonmarkingreturn") == 0) |
| 321 |
continue; |
| 322 |
|
| 323 |
if (j < 256) |
| 324 |
{ |
| 325 |
array[j] = i | 0x1000000; |
| 326 |
e->vec[j] = n; |
| 327 |
} |
| 328 |
else |
| 329 |
return e; |
| 330 |
j++; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return NULL; /* never reached */ |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
/* |
| 339 |
* This routine fills `array' with the subfont character codes; |
| 340 |
* additionally, it tests for valid glyph indices. |
| 341 |
*/ |
| 342 |
|
| 343 |
void |
| 344 |
TTFget_subfont(Font *fnt, long *array) |
| 345 |
{ |
| 346 |
int i, j, Num; |
| 347 |
|
| 348 |
|
| 349 |
if (!fnt || !array) |
| 350 |
oops("Invalid parameter in call to TTFget_subfont()"); |
| 351 |
|
| 352 |
for (i = 0; i <= 0xFF; i++) |
| 353 |
{ |
| 354 |
j = fnt->sf_code[i]; |
| 355 |
|
| 356 |
if (j < 0) |
| 357 |
array[i] = j; |
| 358 |
else |
| 359 |
{ |
| 360 |
Num = FT_Get_Char_Index(face, j); |
| 361 |
if (Num < 0) |
| 362 |
oops("cmap mapping failure."); |
| 363 |
else |
| 364 |
array[i] = j; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
long |
| 371 |
TTFsearch_PS_name(const char *name) |
| 372 |
{ |
| 373 |
unsigned int i; |
| 374 |
char buff[128]; |
| 375 |
|
| 376 |
|
| 377 |
for (i = 0; i < face->num_glyphs; i++) |
| 378 |
{ |
| 379 |
FT_Get_Glyph_Name(face, i, buff, 128); |
| 380 |
if (strcmp(name, buff) == 0) |
| 381 |
break; |
| 382 |
} |
| 383 |
|
| 384 |
if (i == face->num_glyphs) |
| 385 |
return -1L; |
| 386 |
else |
| 387 |
return (long)i; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
/* end */ |