summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/convttf.c166
1 files changed, 99 insertions, 67 deletions
diff --git a/tools/convttf.c b/tools/convttf.c
index 24cf14ab9c..b0284ec85f 100644
--- a/tools/convttf.c
+++ b/tools/convttf.c
@@ -22,10 +22,11 @@
22#include FT_FREETYPE_H 22#include FT_FREETYPE_H
23#include FT_GLYPH_H 23#include FT_GLYPH_H
24 24
25#include <stdbool.h>
26#include <stdio.h> 25#include <stdio.h>
27#ifdef WIN32 26#ifdef WIN32
28#include <windows.h> 27#ifdef _MSC_VER
28#define snprintf _snprintf
29#endif
29#else 30#else
30#include <stdlib.h> 31#include <stdlib.h>
31#include <unistd.h> 32#include <unistd.h>
@@ -220,7 +221,11 @@ char *basename(char *path)
220 /* remove prepended path and extension*/ 221 /* remove prepended path and extension*/
221 b = path; 222 b = path;
222 for (p=path; *p; ++p) { 223 for (p=path; *p; ++p) {
224#ifdef WIN32
225 if (*p == '/' || *p == '\\')
226#else
223 if (*p == '/') 227 if (*p == '/')
228#endif
224 b = p + 1; 229 b = p + 1;
225 } 230 }
226 strcpy(base, b); 231 strcpy(base, b);
@@ -301,8 +306,8 @@ otf_get_english_string(FT_Face face, int nameID, int dash_to_space,
301 int j, encid; 306 int j, encid;
302 FT_UInt i, nrec; 307 FT_UInt i, nrec;
303 FT_SfntName sfntName; 308 FT_SfntName sfntName;
304 unsigned char *s; 309 unsigned char *s = NULL;
305 unsigned short slen; 310 unsigned short slen = 0;
306 311
307 nrec = FT_Get_Sfnt_Name_Count(face); 312 nrec = FT_Get_Sfnt_Name_Count(face);
308 313
@@ -404,7 +409,7 @@ int get_ttc_table(char *path, struct ttc_table *ttcname )
404{ 409{
405 410
406 411
407 FT_Error error; 412 FT_Error err;
408 FT_Library library; 413 FT_Library library;
409 FT_Face face; 414 FT_Face face;
410 FT_Long i; 415 FT_Long i;
@@ -414,19 +419,19 @@ int get_ttc_table(char *path, struct ttc_table *ttcname )
414 ttcname->ttc_count = 0; 419 ttcname->ttc_count = 0;
415 420
416 /* Initialize engine */ 421 /* Initialize engine */
417 if ( ( error = FT_Init_FreeType( &library ) ) != 0 ) 422 if ( ( err = FT_Init_FreeType( &library ) ) != 0 )
418 { 423 {
419 panic( "Error while initializing engine" ); 424 panic( "Error while initializing engine" );
420 return error; 425 return err;
421 } 426 }
422 427
423 428
424 /* Load face */ 429 /* Load face */
425 error = FT_New_Face( library, path, (FT_Long) 0, &face ); 430 err = FT_New_Face( library, path, (FT_Long) 0, &face );
426 if ( error ) 431 if ( err )
427 { 432 {
428 arg_panic( "Could not find/open font", path ); 433 arg_panic( "Could not find/open font", path );
429 return error; 434 return err;
430 } 435 }
431 436
432 ttcname->ttc_count = face->num_faces; 437 ttcname->ttc_count = face->num_faces;
@@ -434,8 +439,8 @@ int get_ttc_table(char *path, struct ttc_table *ttcname )
434 439
435 for(i = 0; i < ttcname->ttc_count; i++) 440 for(i = 0; i < ttcname->ttc_count; i++)
436 { 441 {
437 error = FT_New_Face( library, path, i, &face ); 442 err = FT_New_Face( library, path, i, &face );
438 if ( error == FT_Err_Cannot_Open_Stream ) 443 if ( err == FT_Err_Cannot_Open_Stream )
439 arg_panic( "Could not find/open font", path ); 444 arg_panic( "Could not find/open font", path );
440 otf_get_english_string(face, BDFOTF_POSTSCRIPT_STRING, 0, xlfd, 445 otf_get_english_string(face, BDFOTF_POSTSCRIPT_STRING, 0, xlfd,
441 sizeof(xlfd)); 446 sizeof(xlfd));
@@ -599,49 +604,62 @@ void trim_glyph( FT_GlyphSlot glyph, int *empty_first_col,
599 604
600void convttf(char* path, char* destfile, FT_Long face_index) 605void convttf(char* path, char* destfile, FT_Long face_index)
601{ 606{
602 FT_Error error; 607 FT_Error err;
603 FT_Library library; 608 FT_Library library;
604 FT_Face face; 609 FT_Face face;
605 int w,h; 610 int w,h;
606 int row,col; 611 int row,col;
607 int empty_first_col, empty_last_col; 612 int empty_first_col, empty_last_col;
608 FT_Long charindex; 613 FT_Long charindex;
609 FT_Long index = 0; 614 FT_Long idx = 0;
610 FT_Long code; 615 FT_Long code;
611 FT_Long digit_width = 0; 616 FT_Long digit_width = 0;
617 float extra_space;
618 FT_Long char_count;
619 char use_long_offset;
620 int done = 0;
621 char char_name[1024];
622 int converted_char_count = 0;
623 int failed_char_count = 0;
612 624
613 int depth = 2; 625 int depth = 2;
614 unsigned char bit_shift = 1 << depth; 626 unsigned char bit_shift = 1u << depth;
615 unsigned char pixel_per_byte = CHAR_BIT / bit_shift; 627 unsigned char pixel_per_byte = CHAR_BIT / bit_shift;
628 struct font_struct export_font;
629 char pad[] = {0,0,0,0};
630 int skip,i;
631 FILE *file;
616 632
617 /* Initialize engine */ 633 /* Initialize engine */
618 if ( ( error = FT_Init_FreeType( &library ) ) != 0 ) 634 if ( ( err = FT_Init_FreeType( &library ) ) != 0 )
619 panic( "Error while initializing engine" ); 635 panic( "Error while initializing engine" );
620 636
621 /* Load face */ 637 /* Load face */
622 error = FT_New_Face( library, path, (FT_Long) face_index, &face ); 638 err = FT_New_Face( library, path, (FT_Long) face_index, &face );
623 if ( error == FT_Err_Cannot_Open_Stream ) 639 if ( err == FT_Err_Cannot_Open_Stream )
624 arg_panic( "Could not find/open font\n", path ); 640 arg_panic( "Could not find/open font\n", path );
625 else if ( error ) 641 else if ( err )
626 arg_panic( "Error while opening font\n", path ); 642 arg_panic( "Error while opening font\n", path );
627 643
628 644
629 setcharmap( face ); 645 setcharmap( face );
630 /* Set font header data */ 646 /* Set font header data */
631 struct font_struct export_font; 647
632 export_font.header.header[0] = 'R'; 648 export_font.header.header[0] = 'R';
633 export_font.header.header[1] = 'B'; 649 export_font.header.header[1] = 'B';
634 export_font.header.header[2] = '1'; 650 export_font.header.header[2] = '1';
635 export_font.header.header[3] = '2'; 651 export_font.header.header[3] = '2';
636 //export_font.header.height = 0; 652#if 0
637 //export_font.header.ascent = 0; 653 export_font.header.height = 0;
654 export_font.header.ascent = 0;
655#endif
638 656
639 float extra_space = (float)(between_row-trim_aa-trim_da); 657 extra_space = (float)(between_row-trim_aa-trim_da);
640 FT_Set_Char_Size( face, 0, pixel_size << 6, hv_resolution, hv_resolution ); 658 FT_Set_Char_Size( face, 0, pixel_size << 6, hv_resolution, hv_resolution );
641 export_font.header.ascent = 659 export_font.header.ascent =
642 ((face->size->metrics.ascender*(100-trim_ap)/100) >> 6) - trim_aa; 660 ((face->size->metrics.ascender*(100-trim_ap)/100) >> 6) - trim_aa;
643 661
644 export_font.header.height = 662 export_font.header.height =
645 (((face->size->metrics.ascender*(100-trim_ap)/100) - 663 (((face->size->metrics.ascender*(100-trim_ap)/100) -
646 (face->size->metrics.descender*(100-trim_dp)/100)) >> 6) + extra_space; 664 (face->size->metrics.descender*(100-trim_dp)/100)) >> 6) + extra_space;
647 665
@@ -652,7 +670,7 @@ void convttf(char* path, char* destfile, FT_Long face_index)
652 if ( limit_char == 0 ) limit_char = max_char; 670 if ( limit_char == 0 ) limit_char = max_char;
653 if ( limit_char > max_char ) limit_char = max_char; 671 if ( limit_char > max_char ) limit_char = max_char;
654 672
655 FT_Long char_count = 0; 673 char_count = 0;
656 674
657 675
658 676
@@ -669,9 +687,9 @@ void convttf(char* path, char* destfile, FT_Long face_index)
669 { 687 {
670 charindex = getcharindex( face, code); 688 charindex = getcharindex( face, code);
671 if ( !(charindex) ) continue; 689 if ( !(charindex) ) continue;
672 error = FT_Load_Glyph( face, charindex, 690 err = FT_Load_Glyph( face, charindex,
673 (FT_LOAD_RENDER | FT_LOAD_NO_BITMAP) ); 691 (FT_LOAD_RENDER | FT_LOAD_NO_BITMAP) );
674 if ( error ) continue; 692 if ( err ) continue;
675 693
676 w = glyph_width( face, code, digit_width ); 694 w = glyph_width( face, code, digit_width );
677 if (w == 0) continue; 695 if (w == 0) continue;
@@ -684,7 +702,7 @@ void convttf(char* path, char* destfile, FT_Long face_index)
684 702
685 703
686 char_count++; 704 char_count++;
687 index += (w*export_font.header.height + pixel_per_byte - 1)/pixel_per_byte; 705 idx += (w*export_font.header.height + pixel_per_byte - 1)/pixel_per_byte;
688 706
689 if (code >= lastchar) 707 if (code >= lastchar)
690 lastchar = code; 708 lastchar = code;
@@ -695,12 +713,12 @@ void convttf(char* path, char* destfile, FT_Long face_index)
695 export_font.header.defaultchar = firstchar; 713 export_font.header.defaultchar = firstchar;
696 export_font.header.firstchar = firstchar; 714 export_font.header.firstchar = firstchar;
697 export_font.header.size = lastchar - firstchar + 1; 715 export_font.header.size = lastchar - firstchar + 1;
698 export_font.header.nbits = index; 716 export_font.header.nbits = idx;
699 export_font.header.noffset = export_font.header.size; 717 export_font.header.noffset = export_font.header.size;
700 export_font.header.nwidth = export_font.header.size; 718 export_font.header.nwidth = export_font.header.size;
701 719
702 /* check if we need to use long offsets */ 720 /* check if we need to use long offsets */
703 char use_long_offset = (export_font.header.nbits >= 0xFFDB ); 721 use_long_offset = (export_font.header.nbits >= 0xFFDB );
704 722
705 /* allocate memory */ 723 /* allocate memory */
706 export_font.offset = NULL; 724 export_font.offset = NULL;
@@ -720,14 +738,26 @@ void convttf(char* path, char* destfile, FT_Long face_index)
720 /* for now we use the full height for each character */ 738 /* for now we use the full height for each character */
721 h = export_font.header.height; 739 h = export_font.header.height;
722 740
723 index = 0; 741 idx = 0;
724 int done = 0;
725 char char_name[1024];
726 int converted_char_count = 0;
727 int failed_char_count = 0;
728 742
729 for( code = firstchar; code <= lastchar; code++ ) 743 for( code = firstchar; code <= lastchar; code++ )
730 { 744 {
745 FT_GlyphSlot slot;
746 FT_Bitmap* source;
747 unsigned char* src;
748 unsigned char* tmpbuf;
749 int start_y;
750
751 int glyph_height;
752 int stride;
753 unsigned char* buf;
754 unsigned char* endbuf;
755
756 /* insert empty pixels on the left */
757 int col_off;
758 int numbits;
759 unsigned int field;
760
731 /* Get gylph index from the char and render it */ 761 /* Get gylph index from the char and render it */
732 charindex = getcharindex( face, code); 762 charindex = getcharindex( face, code);
733 if ( !charindex ) 763 if ( !charindex )
@@ -740,9 +770,9 @@ void convttf(char* path, char* destfile, FT_Long face_index)
740 continue; 770 continue;
741 } 771 }
742 772
743 error = FT_Load_Glyph( face, charindex , 773 err = FT_Load_Glyph( face, charindex ,
744 (FT_LOAD_RENDER | FT_LOAD_NO_BITMAP) ); 774 (FT_LOAD_RENDER | FT_LOAD_NO_BITMAP) );
745 if ( error ) { 775 if ( err ) {
746 continue; 776 continue;
747 } 777 }
748 if FT_HAS_GLYPH_NAMES( face ) 778 if FT_HAS_GLYPH_NAMES( face )
@@ -750,9 +780,11 @@ void convttf(char* path, char* destfile, FT_Long face_index)
750 else 780 else
751 char_name[0] = '\0'; 781 char_name[0] = '\0';
752 782
753 FT_GlyphSlot slot = face->glyph; 783 slot = face->glyph;
754 FT_Bitmap* source = &slot->bitmap; 784 source = &slot->bitmap;
755 //print_raw_glyph( face ); 785#if 0
786 print_raw_glyph( face );
787#endif
756 w = glyph_width( face, code, digit_width ); 788 w = glyph_width( face, code, digit_width );
757 if (w == 0) continue; 789 if (w == 0) continue;
758 empty_first_col = empty_last_col = 0; 790 empty_first_col = empty_last_col = 0;
@@ -761,26 +793,26 @@ void convttf(char* path, char* destfile, FT_Long face_index)
761 trim_glyph( face->glyph, &empty_first_col, &empty_last_col, &w ); 793 trim_glyph( face->glyph, &empty_first_col, &empty_last_col, &w );
762 794
763 if ( use_long_offset ) 795 if ( use_long_offset )
764 export_font.offset_long[code - firstchar] = index; 796 export_font.offset_long[code - firstchar] = idx;
765 else 797 else
766 export_font.offset[code - firstchar] = index; 798 export_font.offset[code - firstchar] = idx;
767 799
768 export_font.width[code - firstchar] = w; 800 export_font.width[code - firstchar] = w;
769 801
770 /* copy the glyph bitmap to a full sized glyph bitmap */ 802 /* copy the glyph bitmap to a full sized glyph bitmap */
771 unsigned char* src = source->buffer; 803 src = source->buffer;
772 unsigned char* tmpbuf = malloc(sizeof(unsigned char) * w * h); 804 tmpbuf = malloc(sizeof(unsigned char) * w * h);
773 memset(tmpbuf, 0xff, w*h); 805 memset(tmpbuf, 0xff, w*h);
774 int start_y = export_font.header.ascent - slot->bitmap_top; 806 start_y = export_font.header.ascent - slot->bitmap_top;
775 807
776 int glyph_height = source->rows; 808 glyph_height = source->rows;
777 int stride = source->pitch; 809 stride = source->pitch;
778 unsigned char* buf = tmpbuf; 810 buf = tmpbuf;
779 unsigned char* endbuf = tmpbuf + w*h; 811 endbuf = tmpbuf + w*h;
780 812
781 int error = 0; 813 err = 0;
782 /* insert empty pixels on the left */ 814 /* insert empty pixels on the left */
783 int col_off = w - stride; 815 col_off = w - stride;
784 if (col_off > 1) col_off /= 2; 816 if (col_off > 1) col_off /= 2;
785 if (col_off < 0) col_off = 0; 817 if (col_off < 0) col_off = 0;
786 818
@@ -796,16 +828,14 @@ void convttf(char* path, char* destfile, FT_Long face_index)
796 if (dst < endbuf && dst >= tmpbuf) 828 if (dst < endbuf && dst >= tmpbuf)
797 *dst = 0xff - *tsrc; 829 *dst = 0xff - *tsrc;
798 else { 830 else {
799 error = 1; 831 err = 1;
800 printf("Error! row: %3d col: %3d\n", row, col); 832 printf("Error! row: %3d col: %3d\n", row, col);
801 } 833 }
802 } 834 }
803 } 835 }
804 if(error) print_raw_glyph(face); 836 if(err) print_raw_glyph(face);
805 837
806 buf = tmpbuf; 838 buf = tmpbuf;
807 int numbits;
808 unsigned int field;
809 field = 0; 839 field = 0;
810 numbits = pixel_per_byte; 840 numbits = pixel_per_byte;
811 841
@@ -813,13 +843,13 @@ void convttf(char* path, char* destfile, FT_Long face_index)
813 { 843 {
814 for(col=0; col < w; col++) 844 for(col=0; col < w; col++)
815 { 845 {
816 unsigned int src = *buf++; 846 unsigned int src2 = *buf++;
817 unsigned int cur_col = (src + 8) / 17; 847 unsigned int cur_col = (src2 + 8) / 17;
818 field |= (cur_col << (bit_shift*(pixel_per_byte-numbits))); 848 field |= (cur_col << (bit_shift*(pixel_per_byte-numbits)));
819 849
820 if (--numbits == 0) 850 if (--numbits == 0)
821 { 851 {
822 export_font.chars_data[index++] = (unsigned char)field; 852 export_font.chars_data[idx++] = (unsigned char)field;
823 numbits = pixel_per_byte; 853 numbits = pixel_per_byte;
824 field = 0; 854 field = 0;
825 } 855 }
@@ -829,15 +859,19 @@ void convttf(char* path, char* destfile, FT_Long face_index)
829 /* Pad last byte */ 859 /* Pad last byte */
830 if (numbits != pixel_per_byte) 860 if (numbits != pixel_per_byte)
831 { 861 {
832 export_font.chars_data[index++] = (unsigned char)field; 862 export_font.chars_data[idx++] = (unsigned char)field;
833 } 863 }
834 864
835 if( dump_glyphs ) 865 if( dump_glyphs )
836 { 866 {
837 /* debug: dump char */ 867 /* debug: dump char */
838 printf("\n---Converted Glyph Dump---\n");
839 unsigned char bit_max = (1 << bit_shift) - 1; 868 unsigned char bit_max = (1 << bit_shift) - 1;
869 printf("\n---Converted Glyph Dump---\n");
870
840 if ( code > 32 && code < 255 ) { 871 if ( code > 32 && code < 255 ) {
872 unsigned char current_data;
873 unsigned char font_bits;
874
841 row = h; 875 row = h;
842 if(use_long_offset) 876 if(use_long_offset)
843 buf = &(export_font.chars_data[export_font.offset_long[ 877 buf = &(export_font.chars_data[export_font.offset_long[
@@ -845,8 +879,6 @@ void convttf(char* path, char* destfile, FT_Long face_index)
845 else 879 else
846 buf = &(export_font.chars_data[export_font.offset[ 880 buf = &(export_font.chars_data[export_font.offset[
847 code - firstchar]]); 881 code - firstchar]]);
848 unsigned char current_data;
849 unsigned char font_bits;
850 numbits = pixel_per_byte; 882 numbits = pixel_per_byte;
851 current_data = *buf; 883 current_data = *buf;
852 do 884 do
@@ -889,7 +921,7 @@ void convttf(char* path, char* destfile, FT_Long face_index)
889 char_name,converted_char_count,done); fflush(stdout); 921 char_name,converted_char_count,done); fflush(stdout);
890 } 922 }
891 923
892 FILE *file = fopen(destfile, "w"); 924 file = fopen(destfile, "w");
893 printf("Writing %s\n", destfile); 925 printf("Writing %s\n", destfile);
894 926
895 /* font info */ 927 /* font info */
@@ -909,8 +941,8 @@ void convttf(char* path, char* destfile, FT_Long face_index)
909 export_font.header.nbits, file); 941 export_font.header.nbits, file);
910 free(export_font.chars_data); 942 free(export_font.chars_data);
911 943
912 int skip,i; 944
913 char pad[] = {0,0,0,0}; 945
914 if ( use_long_offset ) 946 if ( use_long_offset )
915 { 947 {
916 skip = ((export_font.header.nbits + 3) & ~3) - 948 skip = ((export_font.header.nbits + 3) & ~3) -