156
185
else if (font_info->flags & GRUB_FONT_FLAG_FORCEHINT)
157
186
flag |= FT_LOAD_FORCE_AUTOHINT;
159
if (FT_Load_Char (face, char_code, flag))
188
err = FT_Load_Glyph (face, glyph_idx, flag);
191
printf ("Freetype Error %d loading glyph 0x%x for U+0x%x%s",
192
err, glyph_idx, char_code & GRUB_FONT_CODE_CHAR_MASK,
193
char_code & GRUB_FONT_CODE_RIGHT_JOINED
194
? ((char_code & GRUB_FONT_CODE_LEFT_JOINED) ? " (medial)":
196
: ((char_code & GRUB_FONT_CODE_LEFT_JOINED) ? " (rightmost)":
199
if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
200
printf (": %s\n", ft_errmsgs[err]);
162
206
glyph = face->glyph;
164
208
if (font_info->flags & GRUB_FONT_FLAG_BOLD)
165
209
FT_GlyphSlot_Embolden (glyph);
167
p_glyph = &font_info->glyph;
168
while ((*p_glyph) && ((*p_glyph)->char_code > char_code))
212
printf ("%x\n", char_code);
215
cuttop = cutbottom = cutleft = cutright = 0;
170
p_glyph = &(*p_glyph)->next;
218
for (cuttop = 0; cuttop < glyph->bitmap.rows; cuttop++)
220
for (j = 0; j < glyph->bitmap.width; j++)
221
if (glyph->bitmap.buffer[j / 8 + cuttop * glyph->bitmap.pitch]
222
& (1 << (7 - (j & 7))))
224
if (j != glyph->bitmap.width)
228
for (cutbottom = glyph->bitmap.rows - 1; cutbottom >= 0; cutbottom--)
230
for (j = 0; j < glyph->bitmap.width; j++)
231
if (glyph->bitmap.buffer[j / 8 + cutbottom * glyph->bitmap.pitch]
232
& (1 << (7 - (j & 7))))
234
if (j != glyph->bitmap.width)
237
cutbottom = glyph->bitmap.rows - 1 - cutbottom;
238
if (cutbottom + cuttop >= glyph->bitmap.rows)
241
for (cutleft = 0; cutleft < glyph->bitmap.width; cutleft++)
243
for (j = 0; j < glyph->bitmap.rows; j++)
244
if (glyph->bitmap.buffer[cutleft / 8 + j * glyph->bitmap.pitch]
245
& (1 << (7 - (cutleft & 7))))
247
if (j != glyph->bitmap.rows)
250
for (cutright = glyph->bitmap.width - 1; cutright >= 0; cutright--)
252
for (j = 0; j < glyph->bitmap.rows; j++)
253
if (glyph->bitmap.buffer[cutright / 8 + j * glyph->bitmap.pitch]
254
& (1 << (7 - (cutright & 7))))
256
if (j != glyph->bitmap.rows)
259
cutright = glyph->bitmap.width - 1 - cutright;
260
if (cutright + cutleft >= glyph->bitmap.width)
173
/* Ignore duplicated glyph. */
174
if ((*p_glyph) && ((*p_glyph)->char_code == char_code))
177
width = glyph->bitmap.width;
178
height = glyph->bitmap.rows;
264
width = glyph->bitmap.width - cutleft - cutright;
265
height = glyph->bitmap.rows - cutbottom - cuttop;
180
267
bitmap_size = ((width * height + 7) / 8);
181
glyph_info = xmalloc (sizeof (struct grub_glyph_info) + bitmap_size);
268
glyph_info = xmalloc (sizeof (struct grub_glyph_info));
269
glyph_info->bitmap = xmalloc (bitmap_size);
182
270
glyph_info->bitmap_size = bitmap_size;
184
glyph_info->next = *p_glyph;
185
*p_glyph = glyph_info;
272
glyph_info->next = font_info->glyphs_unsorted;
273
font_info->glyphs_unsorted = glyph_info;
274
font_info->num_glyphs++;
187
276
glyph_info->char_code = char_code;
188
277
glyph_info->width = width;
189
278
glyph_info->height = height;
190
glyph_info->x_ofs = glyph->bitmap_left;
191
glyph_info->y_ofs = glyph->bitmap_top - height;
279
glyph_info->x_ofs = glyph->bitmap_left + cutleft;
280
glyph_info->y_ofs = glyph->bitmap_top - height - cuttop;
192
281
glyph_info->device_width = glyph->metrics.horiAdvance / 64;
194
283
if (width > font_info->max_width)
207
296
data = &glyph_info->bitmap[0] - 1;
208
for (j = 0; j < height; j++)
209
for (i = 0; i < width; i++)
297
for (j = cuttop; j < height + cuttop; j++)
298
for (i = cutleft; i < width + cutleft; i++)
210
299
add_pixel (&data, &mask,
211
300
glyph->bitmap.buffer[i / 8 + j * glyph->bitmap.pitch] &
212
301
(1 << (7 - (i & 7))));
304
struct glyph_replace *subst_rightjoin, *subst_leftjoin, *subst_medijoin;
308
struct glyph_replace *next;
309
grub_uint32_t from, to;
312
/* TODO: sort glyph_replace and use binary search if necessary. */
314
add_char (struct grub_font_info *font_info, FT_Face face,
315
grub_uint32_t char_code, int nocut)
318
struct glyph_replace *cur;
320
glyph_idx = FT_Get_Char_Index (face, char_code);
323
add_glyph (font_info, glyph_idx, face, char_code, nocut);
324
for (cur = subst_rightjoin; cur; cur = cur->next)
325
if (cur->from == glyph_idx)
327
add_glyph (font_info, cur->to, face,
328
char_code | GRUB_FONT_CODE_RIGHT_JOINED, nocut);
331
if (!cur && char_code >= GRUB_UNICODE_ARABIC_START
332
&& char_code < GRUB_UNICODE_ARABIC_END)
335
for (i = 0; grub_unicode_arabic_shapes[i].code; i++)
336
if (grub_unicode_arabic_shapes[i].code == char_code
337
&& grub_unicode_arabic_shapes[i].right_linked)
340
idx2 = FT_Get_Char_Index (face, grub_unicode_arabic_shapes[i]
343
add_glyph (font_info, idx2, face,
344
char_code | GRUB_FONT_CODE_RIGHT_JOINED, nocut);
350
for (cur = subst_leftjoin; cur; cur = cur->next)
351
if (cur->from == glyph_idx)
353
add_glyph (font_info, cur->to, face,
354
char_code | GRUB_FONT_CODE_LEFT_JOINED, nocut);
357
if (!cur && char_code >= GRUB_UNICODE_ARABIC_START
358
&& char_code < GRUB_UNICODE_ARABIC_END)
361
for (i = 0; grub_unicode_arabic_shapes[i].code; i++)
362
if (grub_unicode_arabic_shapes[i].code == char_code
363
&& grub_unicode_arabic_shapes[i].left_linked)
366
idx2 = FT_Get_Char_Index (face, grub_unicode_arabic_shapes[i]
369
add_glyph (font_info, idx2, face,
370
char_code | GRUB_FONT_CODE_LEFT_JOINED, nocut);
375
for (cur = subst_medijoin; cur; cur = cur->next)
376
if (cur->from == glyph_idx)
378
add_glyph (font_info, cur->to, face,
379
char_code | GRUB_FONT_CODE_LEFT_JOINED
380
| GRUB_FONT_CODE_RIGHT_JOINED, nocut);
383
if (!cur && char_code >= GRUB_UNICODE_ARABIC_START
384
&& char_code < GRUB_UNICODE_ARABIC_END)
387
for (i = 0; grub_unicode_arabic_shapes[i].code; i++)
388
if (grub_unicode_arabic_shapes[i].code == char_code
389
&& grub_unicode_arabic_shapes[i].both_linked)
392
idx2 = FT_Get_Char_Index (face, grub_unicode_arabic_shapes[i]
395
add_glyph (font_info, idx2, face,
396
char_code | GRUB_FONT_CODE_LEFT_JOINED
397
| GRUB_FONT_CODE_RIGHT_JOINED, nocut);
406
grub_uint32_t version;
407
grub_uint16_t scripts_off;
408
grub_uint16_t features_off;
409
grub_uint16_t lookups_off;
410
} __attribute__ ((packed));
417
#define FEATURE_FINA 0x66696e61
418
#define FEATURE_INIT 0x696e6974
419
#define FEATURE_MEDI 0x6d656469
420
#define FEATURE_AALT 0x61616c74
421
#define FEATURE_LIGA 0x6c696761
422
#define FEATURE_RLIG 0x726c6967
423
grub_uint32_t feature_tag;
424
grub_uint16_t offset;
425
} __attribute__ ((packed)) features[0];
426
} __attribute__ ((packed));
430
grub_uint16_t params;
431
grub_uint16_t lookupcount;
432
grub_uint16_t lookupindices[0];
433
} __attribute__ ((packed));
435
struct gsub_lookup_list
438
grub_uint16_t offsets[0];
439
} __attribute__ ((packed));
445
grub_uint16_t subtablecount;
446
grub_uint16_t subtables[0];
447
} __attribute__ ((packed));
449
struct gsub_substitution
452
grub_uint16_t coverage_off;
459
grub_uint16_t repl[0];
462
} __attribute__ ((packed));
464
struct gsub_coverage_list
468
grub_uint16_t glyphs[0];
469
} __attribute__ ((packed));
471
struct gsub_coverage_ranges
479
grub_uint16_t start_index;
480
} __attribute__ ((packed)) ranges[0];
481
} __attribute__ ((packed));
483
#define GSUB_SINGLE_SUBSTITUTION 1
485
#define GSUB_SUBSTITUTION_DELTA 1
486
#define GSUB_SUBSTITUTION_MAP 2
488
#define GSUB_COVERAGE_LIST 1
489
#define GSUB_COVERAGE_RANGE 2
491
#define GSUB_RTL_CHAR 1
494
add_subst (grub_uint32_t from, grub_uint32_t to, struct glyph_replace **target)
496
struct glyph_replace *new = xmalloc (sizeof (*new));
504
process_cursive (struct gsub_feature *feature,
505
struct gsub_lookup_list *lookups,
506
grub_uint32_t feattag)
510
struct glyph_replace **target;
511
struct gsub_substitution *sub;
513
auto inline void subst (grub_uint32_t glyph);
514
void subst (grub_uint32_t glyph)
516
grub_uint16_t substtype;
517
substtype = grub_be_to_cpu16 (sub->type);
519
if (substtype == GSUB_SUBSTITUTION_DELTA)
520
add_subst (glyph, glyph + grub_be_to_cpu16 (sub->delta), target);
521
else if (i >= grub_be_to_cpu16 (sub->count))
522
printf ("Out of range substitution (%d, %d)\n", i,
523
grub_be_to_cpu16 (sub->count));
525
add_subst (glyph, grub_be_to_cpu16 (sub->repl[i++]), target);
528
for (j = 0; j < grub_be_to_cpu16 (feature->lookupcount); j++)
530
int lookup_index = grub_be_to_cpu16 (feature->lookupindices[j]);
531
struct gsub_lookup *lookup;
532
if (lookup_index >= grub_be_to_cpu16 (lookups->count))
534
printf ("Out of range lookup: %d\n", lookup_index);
537
lookup = (struct gsub_lookup *)
538
((grub_uint8_t *) lookups
539
+ grub_be_to_cpu16 (lookups->offsets[lookup_index]));
540
if (grub_be_to_cpu16 (lookup->type) != GSUB_SINGLE_SUBSTITUTION)
542
printf ("Unsupported substitution type: %d\n",
543
grub_be_to_cpu16 (lookup->type));
546
if (grub_be_to_cpu16 (lookup->flag) & ~GSUB_RTL_CHAR)
548
printf ("Unsupported substitution flag: 0x%x\n",
549
grub_be_to_cpu16 (lookup->flag));
554
if (grub_be_to_cpu16 (lookup->flag) & GSUB_RTL_CHAR)
555
target = &subst_leftjoin;
557
target = &subst_rightjoin;
560
if (grub_be_to_cpu16 (lookup->flag) & GSUB_RTL_CHAR)
561
target = &subst_rightjoin;
563
target = &subst_leftjoin;
566
target = &subst_medijoin;
569
for (k = 0; k < grub_be_to_cpu16 (lookup->subtablecount); k++)
571
sub = (struct gsub_substitution *)
572
((grub_uint8_t *) lookup + grub_be_to_cpu16 (lookup->subtables[k]));
573
grub_uint16_t substtype;
574
substtype = grub_be_to_cpu16 (sub->type);
575
if (substtype != GSUB_SUBSTITUTION_MAP
576
&& substtype != GSUB_SUBSTITUTION_DELTA)
578
printf ("Unsupported substitution specification: %d\n",
582
void *coverage = (grub_uint8_t *) sub
583
+ grub_be_to_cpu16 (sub->coverage_off);
584
grub_uint32_t covertype;
585
covertype = grub_be_to_cpu16 (*(grub_uint16_t * __attribute__ ((packed))) coverage);
587
if (covertype == GSUB_COVERAGE_LIST)
589
struct gsub_coverage_list *cover = coverage;
591
for (l = 0; l < grub_be_to_cpu16 (cover->count); l++)
592
subst (grub_be_to_cpu16 (cover->glyphs[l]));
594
else if (covertype == GSUB_COVERAGE_RANGE)
596
struct gsub_coverage_ranges *cover = coverage;
598
for (l = 0; l < grub_be_to_cpu16 (cover->count); l++)
599
for (m = grub_be_to_cpu16 (cover->ranges[l].start);
600
m <= grub_be_to_cpu16 (cover->ranges[l].end); m++)
604
printf ("Unsupported coverage specification: %d\n", covertype);
216
add_font (struct grub_font_info *font_info, FT_Face face)
610
add_font (struct grub_font_info *font_info, FT_Face face, int nocut)
612
struct gsub_header *gsub = NULL;
613
FT_ULong gsub_len = 0;
615
if (!FT_Load_Sfnt_Table (face, TTAG_GSUB, 0, NULL, &gsub_len))
617
gsub = xmalloc (gsub_len);
618
if (FT_Load_Sfnt_Table (face, TTAG_GSUB, 0, (void *) gsub, &gsub_len))
627
struct gsub_features *features
628
= (struct gsub_features *) (((grub_uint8_t *) gsub)
629
+ grub_be_to_cpu16 (gsub->features_off));
630
struct gsub_lookup_list *lookups
631
= (struct gsub_lookup_list *) (((grub_uint8_t *) gsub)
632
+ grub_be_to_cpu16 (gsub->lookups_off));
634
int nfeatures = grub_be_to_cpu16 (features->count);
635
for (i = 0; i < nfeatures; i++)
637
struct gsub_feature *feature = (struct gsub_feature *)
638
((grub_uint8_t *) features
639
+ grub_be_to_cpu16 (features->features[i].offset));
640
grub_uint32_t feattag
641
= grub_be_to_cpu32 (features->features[i].feature_tag);
643
printf ("WARNING: unsupported feature parameters: %x\n",
644
grub_be_to_cpu16 (feature->params));
647
/* Used for retrieving all possible variants. Useless in grub. */
651
/* FIXME: Add ligature support. */
656
/* Cursive form variants. */
660
process_cursive (feature, lookups, feattag);
667
memcpy (str, &features->features[i].feature_tag,
668
sizeof (features->features[i].feature_tag));
670
for (j = 0; j < 4; j++)
671
if (!grub_isgraph (str[j]))
673
printf ("Unknown gsub feature 0x%x (%s)\n", feattag, str);
218
679
if (font_info->num_range)
340
write_font (struct grub_font_info *font_info, char *output_file)
802
write_font_ascii_bitmap (struct grub_font_info *font_info, char *output_file)
805
struct grub_glyph_info *glyph;
808
file = fopen (output_file, "wb");
810
grub_util_error ("Can\'t write to file %s.", output_file);
813
for (glyph = font_info->glyphs_sorted, num = 0; num < font_info->num_glyphs;
817
if (glyph->width != 8 || glyph->height != 16)
819
/* printf ("Width or height from glyph U+%04x not supported, skipping.\n", glyph->char_code); */
823
for (row = 0; row < glyph->height; row++)
826
fwrite (&glyph->bitmap[row], sizeof(glyph->bitmap[row]), 1, file);
828
fwrite (&correct_size, 1, 1, file);
835
write_font_width_spec (struct grub_font_info *font_info, char *output_file)
838
struct grub_glyph_info *glyph;
841
out = xmalloc (8192);
842
memset (out, 0, 8192);
844
file = fopen (output_file, "wb");
846
grub_util_error ("Can\'t write to file %s.", output_file);
848
for (glyph = font_info->glyphs_sorted;
849
glyph < font_info->glyphs_sorted + font_info->num_glyphs; glyph++)
850
if (glyph->width > 12)
851
out[glyph->char_code >> 3] |= (1 << (glyph->char_code & 7));
853
fwrite (out, 8192, 1, file);
859
write_font_pf2 (struct grub_font_info *font_info, char *output_file)
343
862
grub_uint32_t leng, data;
344
863
char style_name[20], *font_name;
345
struct grub_glyph_info *cur, *pre;
865
struct grub_glyph_info *cur;
348
867
file = fopen (output_file, "wb");
350
grub_util_error ("Can\'t write to file %s.", output_file);
869
grub_util_error ("can\'t write to file %s.", output_file);
354
873
leng = grub_cpu_to_be32 (4);
355
grub_util_write_image ("FILE", 4, file);
874
grub_util_write_image (FONT_FORMAT_SECTION_NAMES_FILE,
875
sizeof(FONT_FORMAT_SECTION_NAMES_FILE) - 1, file);
356
876
grub_util_write_image ((char *) &leng, 4, file);
357
grub_util_write_image ("PFF2", 4, file);
877
grub_util_write_image (FONT_FORMAT_PFF2_MAGIC, 4, file);
360
880
if (! font_info->name)
376
896
font_name = xasprintf ("%s %s %d", font_info->name, &style_name[1],
377
897
font_info->size);
379
write_string_section ("NAME", font_name, &offset, file);
380
write_string_section ("FAMI", font_info->name, &offset, file);
381
write_string_section ("WEIG",
899
write_string_section (FONT_FORMAT_SECTION_NAMES_FONT_NAME,
900
font_name, &offset, file);
901
write_string_section (FONT_FORMAT_SECTION_NAMES_FAMILY,
902
font_info->name, &offset, file);
903
write_string_section (FONT_FORMAT_SECTION_NAMES_WEIGHT,
382
904
(font_info->style & FT_STYLE_FLAG_BOLD) ?
383
905
"bold" : "normal",
385
write_string_section ("SLAN",
907
write_string_section (FONT_FORMAT_SECTION_NAMES_SLAN,
386
908
(font_info->style & FT_STYLE_FLAG_ITALIC) ?
387
909
"italic" : "normal",
390
write_be16_section ("PTSZ", font_info->size, &offset, file);
391
write_be16_section ("MAXW", font_info->max_width, &offset, file);
392
write_be16_section ("MAXH", font_info->max_height, &offset, file);
912
write_be16_section (FONT_FORMAT_SECTION_NAMES_POINT_SIZE,
913
font_info->size, &offset, file);
914
write_be16_section (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH,
915
font_info->max_width, &offset, file);
916
write_be16_section (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT,
917
font_info->max_height, &offset, file);
394
919
if (! font_info->desc)