font.c
来自「Ming is a library for generating Macrome」· C语言 代码 · 共 985 行 · 第 1/2 页
C
985 行
font->textList = textList;}static intfindCodeValue(unsigned short c, unsigned short* list, int start, int end){ int pos; if ( start == end ) return start; if ( c <= list[start] ) return start; pos = (start + end) / 2; if ( c < list[pos] ) return findCodeValue(c, list, start, pos); else if ( c > list[pos] ) return findCodeValue(c, list, pos+1, end); else return pos;}#define CODETABLE_INCREMENT 32voidSWFFontCharacter_addCharToTable(SWFFontCharacter font, unsigned short c){ // insert the char into font's codeTable if it isn't already there int p; p = findCodeValue(c, font->codeTable, 0, font->nGlyphs); if ( font->codeTable != NULL && p != font->nGlyphs && font->codeTable[p] == c ) return; if ( font->nGlyphs % CODETABLE_INCREMENT == 0 ) { font->codeTable = (unsigned short*) realloc(font->codeTable, (font->nGlyphs + CODETABLE_INCREMENT) * sizeof(unsigned short)); memset(font->codeTable+font->nGlyphs, 0, CODETABLE_INCREMENT*sizeof(unsigned short)); } // keep list sorted if ( p < font->nGlyphs ) { memmove(&font->codeTable[p + 1], &font->codeTable[p], (font->nGlyphs - p) * sizeof(*font->codeTable)); } font->codeTable[p] = c; ++font->nGlyphs;}voidSWFFontCharacter_addWideChars(SWFFontCharacter font, unsigned short *string, int len){ while(--len >= 0) SWFFontCharacter_addCharToTable(font, *string++);}/* * add all utf-8 characters in the given string to the SWF file. * The font-block in the resulting SWF file will include all the * utf-8 characters in the given string. */voidSWFFontCharacter_addUTF8Chars(SWFFontCharacter font, const char *string){ unsigned short *widestring; int len; len = UTF8ExpandString(string, &widestring); SWFFontCharacter_addWideChars(font, widestring, len); free(widestring);}/* * add all characters in the given string to the SWF file. * The font-block in the resulting SWF file will include all the * characters in the given string. */voidSWFFontCharacter_addChars(SWFFontCharacter font, const char *string){ while(*string) SWFFontCharacter_addCharToTable(font, *string++ & 0xff);}SWFFontSWFFontCharacter_getFont(SWFFontCharacter font){ return font->font;}voidSWFFontCharacter_exportCharacterRange(SWFFontCharacter font, unsigned short start, unsigned short end){ // insert the char into font's codeTable if it isn't already there //int p = findCodeValue(start, font->codeTable, 0, font->nGlyphs); //int q = findCodeValue(end, font->codeTable, 0, font->nGlyphs); // XXX}static intSWFFontCharacter_findCharCode(SWFFontCharacter font, unsigned short c){ // return the index in font->codeTable for the given character int p = findCodeValue(c, font->codeTable, 0, font->nGlyphs); if ( font->codeTable[p] == c ) return p; return -1;}static void SWFFontCharacter_dumpTable(SWFFontCharacter fc){ int i; SWFFont font = fc->font; for (i = 0; i < font->nGlyphs; ++i) { unsigned short charcode = font->glyphToCode[i]; SWFFontCharacter_addCharToTable(fc, charcode); } for (i = 0; i < fc->nGlyphs; ++i) fc->codeTable[i] = SWFFont_findGlyphCode(font, fc->codeTable[i]);}static voidSWFFontCharacter_resolveTextCodes(SWFFontCharacter font){ struct textList* text = font->textList; unsigned short* string; int len, i; // find out which characters from this font are being used while ( text != NULL ) { len = SWFTextRecord_getString(text->text, &string); for ( i=0; i<len; ++i ) SWFFontCharacter_addCharToTable(font, string[i]); text = text->next; } // translate text records' strings into lists of font char codes text = font->textList; while ( text != NULL ) { len = SWFTextRecord_getString(text->text, &string); for ( i=0; i<len; ++i ) { int code = SWFFontCharacter_findCharCode(font, string[i]); if ( code >= 0 ) string[i] = (unsigned short)code; // XXX - else? } text = text->next; } // translate codeTable's char codes into glyph codes for ( i=0; i<font->nGlyphs; ++i ) { int code; code = SWFFont_findGlyphCode(font->font, font->codeTable[i]); if(code < 0) { SWF_warn("SWFFontCharacter_resolveTextCodes: Character not found %i\n", font->codeTable[i]); SWF_warn("This is either an encoding error (likely)"); SWF_warn("or the used font does not provide all characters (unlikely)\n"); SWF_error("Stopped\n"); } font->codeTable[i] = code; }}/* * adds all characters/glyphs to the SWF * This function is usefull if an full export of the font is intended. */voidSWFFontCharacter_addAllChars(SWFFontCharacter fc){ fc->dump = 1;}const char*SWFFont_getName(SWFFont font){ return (const char*) font->name;}byteSWFFont_getFlags(SWFFont font){ return font->flags;}int SWFFont_getGlyphCount(SWFFont font){ return font->nGlyphs;}intSWFFontCharacter_getNGlyphs(SWFFontCharacter font){ return font->nGlyphs;}intSWFFont_getScaledWideStringWidth(SWFFont font, const unsigned short* string, int len){ /* return length of given string in whatever units these are we're using */ int i, j; int width = 0; int glyph, glyph2; for ( i=0; i<len; ++i ) { glyph = SWFFont_findGlyphCode(font, string[i]); if ( glyph == -1 ) continue; // XXX - ??? if ( font->advances ) width += font->advances[glyph]; // looking in kernTable // XXX - kernTable should be sorted to make this faster if ( i < len-1 && font->kernTable.k ) { glyph2 = SWFFont_findGlyphCode(font, string[i+1]); if ( glyph2 == -1 ) continue; // XXX - ??? j = font->kernCount; if(font->flags & SWF_FONT_WIDECODES) while ( --j >= 0 ) { if ( glyph == font->kernTable.w[j].code1 && glyph2 == font->kernTable.w[j].code2 ) { width += font->kernTable.w[j].adjustment; break; } } else while ( --j >= 0 ) { if ( glyph == font->kernTable.k[j].code1 && glyph2 == font->kernTable.k[j].code2 ) { width += font->kernTable.k[j].adjustment; break; } } } } return width;}intSWFFont_getScaledStringWidth(SWFFont font, const char* string){ unsigned short* widestr; int len = strlen(string); int n, width; widestr = (unsigned short*) malloc(2 * len); for(n = 0 ; n < len ; n++) widestr[n] = (unsigned char)string[n]; width = SWFFont_getScaledWideStringWidth(font, widestr, len); free(widestr); return width;}intSWFFont_getScaledUTF8StringWidth(SWFFont font, const char* string){ unsigned short* widestr; int len = UTF8ExpandString(string, &widestr); int width = SWFFont_getScaledWideStringWidth(font, widestr, len); free(widestr); return width;}/* get some font metrics */shortSWFFont_getScaledAscent(SWFFont font){ return font->ascent;}shortSWFFont_getScaledDescent(SWFFont font){ return font->descent;}shortSWFFont_getScaledLeading(SWFFont font){ return font->leading;}unsigned shortSWFFontCharacter_getGlyphCode(SWFFontCharacter font, unsigned short c){ return font->codeTable[c];}SWFRectSWFFont_getGlyphBounds(SWFFont font, unsigned short glyphcode){ if ( glyphcode >= font->nGlyphs ) SWF_error("SWFFont_getGlyphBounds: glyphcode >= nGlyphs"); // return &font->bounds[glyphcode]; return SWFCharacter_getBounds(CHARACTER(font->shapes[glyphcode])); // &font->bounds[glyphcode];}intSWFFont_getCharacterAdvance(SWFFont font, unsigned short glyphcode){ if ( font->advances ) { if ( glyphcode >= font->nGlyphs ) SWF_error("SWFFont_getCharacterAdvance: glyphcode >= nGlyphs"); return font->advances[glyphcode]; } return 0;}intSWFFont_getCharacterKern(SWFFont font, unsigned short code1, unsigned short code2){ int j = font->kernCount; if( !font->kernTable.k ) return 0; if ( code1 >= font->nGlyphs || code2 >= font->nGlyphs ) SWF_error("SWFFont_getCharacterKern: glyphcode >= nGlyphs"); // XXX - kernTable should be sorted to make this faster if(font->flags & SWF_FONT_WIDECODES) while ( --j >= 0 ) { if ( code1 == font->kernTable.w[j].code1 && code2 == font->kernTable.w[j].code2 ) { return font->kernTable.w[j].adjustment; } } else while ( --j >= 0 ) { if ( code1 == font->kernTable.k[j].code1 && code2 == font->kernTable.k[j].code2 ) { return font->kernTable.k[j].adjustment; } } return 0;}SWFShape SWFFont_getGlyph(SWFFont font, unsigned short c){ int index; index = SWFFont_findGlyphCode(font, c); if(index < 0) return NULL; return font->shapes[index]; }void SWFFontCollection_addFont(SWFFontCollection collection, SWFFont font){ if(!collection || !font) return; collection->fontList = (SWFFont*)realloc(collection->fontList, (collection->numFonts + 1) * sizeof(SWFFont)); collection->fontList[collection->numFonts] = font; collection->numFonts++;}void destroySWFFontCollection(SWFFontCollection collection){ int i; if(!collection) return; for(i = 0; i < collection->numFonts; i++) destroySWFFont(collection->fontList[i]); free(collection->fontList); free(collection);}SWFFontCollection newSWFFontCollection(){ SWFFontCollection collection; collection = (SWFFontCollection) malloc(sizeof(struct SWFFontCollection_s)); collection->fontList = NULL; collection->numFonts = 0; return collection;}int SWFFontCollection_getFontCount(SWFFontCollection collection){ if(collection == NULL) return -1; return collection->numFonts;}SWFFont SWFFontCollection_getFont(SWFFontCollection collection, int index){ if(index < 0 || index >= collection->numFonts) return NULL; return collection->fontList[index];}SWFFont *SWFFontCollection_getFonts(SWFFontCollection collection, int *count){ if(!collection) { *count = 0; return NULL; } *count = collection->numFonts; return collection->fontList;}/* * Local variables: * tab-width: 2 * c-basic-offset: 2 * End: */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?