?? font.c
字號:
/* Ming, an SWF output library Copyright (C) 2002 Opaque Industries - http://www.opaque.net/ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* $Id: font.c,v 1.58 2008/06/25 16:17:49 krechert Exp $ */#include <math.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include "font.h"#include "method.h"#include "utf8.h"#include "character.h"#include "error.h"#include "movie.h"#include "libming.h"#include "shape.h"#include "fdbfont.h"#include "ttffont.h"#include "ming_config.h"struct textList{ struct textList* next; SWFTextRecord text;};struct SWFFontCharacter_s{ struct SWFCharacter_s character; SWFFont font; byte flags; /* add all font charactes. usefull for textfield */ byte dump; // list of text records that reference this font struct textList* textList; struct textList* currentList; // list of chars used in text objects that reference this font- // idx into this table is stored in text records int nGlyphs; unsigned short* codeTable; SWFOutput out;};voidSWFFont_buildReverseMapping(SWFFont font){ int i; if ( font->flags & SWF_FONT_WIDECODES ) { font->codeToGlyph.wideMap = (unsigned short**)malloc(256 * sizeof(unsigned short*)); for ( i=0; i<256; ++i ) font->codeToGlyph.wideMap[i] = NULL; for ( i=0; i<font->nGlyphs; ++i ) { unsigned short charcode = font->glyphToCode[i]; byte high = charcode >> 8; byte low = charcode & 0xff; if ( font->codeToGlyph.wideMap[high] == NULL ) { font->codeToGlyph.wideMap[high] = (unsigned short*)malloc(256 * sizeof(unsigned short)); memset(font->codeToGlyph.wideMap[high], 0, 256 * sizeof(unsigned short)); } font->codeToGlyph.wideMap[high][low] = i; } } else { font->codeToGlyph.charMap = (byte*) malloc(256 * sizeof(char)); memset(font->codeToGlyph.charMap, 0, 256 * sizeof(char)); for ( i=0; i<font->nGlyphs; ++i ) font->codeToGlyph.charMap[font->glyphToCode[i]] = i; }}static voidSWFFontCharacter_resolveTextCodes(SWFFontCharacter);static voidSWFFontCharacter_dumpTable(SWFFontCharacter);static intcompleteSWFFontCharacter(SWFBlock block){ SWFFontCharacter inst = (SWFFontCharacter)block; SWFFont font = inst->font; SWFOutput buffer; int i, tablen, offset; char c, *string; if(inst->dump) SWFFontCharacter_dumpTable(inst); else SWFFontCharacter_resolveTextCodes(inst); SWF_assert(!inst->out); inst->out = newSWFOutput(); SWFOutput_writeUInt16(inst->out, CHARACTERID(inst)); SWFOutput_writeUInt8(inst->out, inst->flags); SWFOutput_writeUInt8(inst->out, font->langCode); SWFOutput_writeUInt8(inst->out, strlen(font->name)); string = font->name; while ( (c = *(string++)) != 0 ) SWFOutput_writeUInt8(inst->out, c); SWFOutput_writeUInt16(inst->out, inst->nGlyphs); tablen = (inst->nGlyphs+1) * (inst->flags & SWF_FONT_WIDEOFFSETS ? 4 : 2); buffer = newSWFOutput(); for (i = 0; i < inst->nGlyphs; ++i) { SWFShape shape = font->shapes[inst->codeTable[i]]; offset = SWFOutput_getLength(buffer) + tablen; SWFOutput_writeGlyphShape(buffer, shape); if(inst->flags & SWF_FONT_WIDEOFFSETS) SWFOutput_writeUInt32(inst->out, offset); else SWFOutput_writeUInt16(inst->out, offset); } // codeTableOffset offset = SWFOutput_getLength(buffer) + tablen; if(inst->flags & SWF_FONT_WIDEOFFSETS) SWFOutput_writeUInt32(inst->out, offset); else SWFOutput_writeUInt16(inst->out, offset); /* user buffer from here! */ SWFOutput_setNext(inst->out, buffer); for (i = 0; i < inst->nGlyphs; ++i) { unsigned short code = font->glyphToCode[inst->codeTable[i]]; if (inst->flags & SWF_FONT_WIDECODES) SWFOutput_writeUInt16(buffer, code); else SWFOutput_writeUInt8(buffer, code); } /* write font layout */ if (inst->flags & SWF_FONT_HASLAYOUT ) { SWFOutput_writeUInt16(buffer, font->ascent); SWFOutput_writeUInt16(buffer, font->descent); SWFOutput_writeUInt16(buffer, font->leading); for (i = 0; i < inst->nGlyphs; ++i) SWFOutput_writeSInt16(buffer, font->advances[inst->codeTable[i]]); for (i = 0; i < inst->nGlyphs; ++i) { SWFOutput_writeRect(buffer, SWFFont_getGlyphBounds(font, inst->codeTable[i])); SWFOutput_byteAlign(buffer); } SWFOutput_writeUInt16(buffer, 0); /* no kerning */ } return SWFOutput_getLength(inst->out);}voidwriteSWFFontCharacterToMethod(SWFBlock block, SWFByteOutputMethod method, void *data){ SWFFontCharacter inst = (SWFFontCharacter)block; SWFOutput_writeToMethod(inst->out, method, data);}voiddestroySWFFont(SWFFont font){ if(font->shapes) { int i = 0; for(i = 0; i < font->nGlyphs; i++) destroySWFShape(font->shapes[i]); free(font->shapes); } if ( font->flags & SWF_FONT_WIDECODES ) { if ( font->codeToGlyph.wideMap != NULL ) { int i; for ( i = 0; i < 256; ++i ) { if ( font->codeToGlyph.wideMap[i] != NULL ) free(font->codeToGlyph.wideMap[i]); } free(font->codeToGlyph.wideMap); } } else { if ( font->codeToGlyph.charMap != NULL ) free(font->codeToGlyph.charMap); } if ( font->name != NULL ) free(font->name); if ( font->kernTable.k != NULL ) // union of pointers ... free(font->kernTable.k); if ( font->glyphToCode != NULL ) free(font->glyphToCode); if ( font->advances != NULL ) free(font->advances); free(font);}voiddestroySWFFontCharacter(SWFFontCharacter font){ struct textList* text = font->textList; while ( text != NULL ) { struct textList* next = text->next; free(text); text = next; } if ( font->codeTable != NULL ) free(font->codeTable); if( font->out != NULL) destroySWFOutput(font->out); free(font);}SWFFontnewSWFFont(){ SWFFont font = (SWFFont) malloc(sizeof(struct SWFFont_s)); SWFBlockInit((SWFBlock)font); BLOCK(font)->type = SWF_MINGFONT; BLOCK(font)->writeBlock = NULL; BLOCK(font)->complete = NULL; BLOCK(font)->dtor = (destroySWFBlockMethod) destroySWFFont; font->name = NULL; font->flags = 0; font->langCode = 0; font->nGlyphs = 0; font->glyphToCode = NULL; font->advances = NULL; font->ascent = 0; font->descent = 0; font->leading = 0; font->kernCount = 0; font->kernTable.k = NULL; font->shapes = NULL; return font;}// file magic: fonts:0 string \000\001\000\000\000 TrueType font datastatic inline int true_type_check(char *header){ if(header[0] == 0 && header[1] == 1 && header[2] == 0 && header[3] == 0 && header[4] == 0) return 1; return 0;}static inline int fdb_check(char *header){ if(header[0] == 'f' && header[1] == 'd' && header[2] == 'b' && header[3] == '0') return 1; return 0;}static inline int ttc_check(char *header){ if(header[0] == 't' && header[1] == 't' && header[2] == 'c') return 1; return 0;}/* load a collection of fonts (experimental) * currently only TTC (TTF collection) format is supported */SWFFontCollection newSWFFontCollection_fromFile(const char *filename /* filename */){ FILE *file; char header[5]; file = fopen(filename, "rb"); if(file == NULL) { SWF_warn("open font file failed\n"); return NULL; } if(fread(header, 5, 1, file) < 1) { fclose(file); return NULL; } rewind(file); if(ttc_check(header)) { fclose(file);#if USE_FREETYPE return loadTTFCollection(filename);#else SWF_warn("SWFFont:: new SWFFont (ttf): " "freetype is not available.\n"); return NULL;#endif } else { SWF_warn("Unknown font file\n"); fclose(file); return NULL; }}/* load a font from file * This function creates a new SWFFont object from a font file. It accepts * and autodetects FDB and TTF fonts. * returns a SWFFont object if a valid fontfile is found, NULL otherwise */SWFFont newSWFFont_fromFile(const char *filename /* filename for fontfile */){ FILE *file; char header[5]; file = fopen(filename, "rb"); if(file == NULL) { SWF_warn("open font file failed\n"); return NULL; } if(fread(header, 5, 1, file) < 1) { fclose(file); return NULL; } rewind(file); if(true_type_check(header)) { fclose(file);#if USE_FREETYPE return loadSWFFontTTF(filename); #else SWF_warn("SWFFont:: new SWFFont (ttf): " "freetype is not available.\n"); return NULL;#endif } else if(fdb_check(header)) { SWFFont font = loadSWFFont_fromFdbFile(file); fclose(file); return font; } else { SWF_warn("Unknown font file\n"); fclose(file); return NULL; }}SWFFontCharacternewSWFFontCharacter(SWFFont font){ SWFFontCharacter inst; inst = (SWFFontCharacter) malloc(sizeof(struct SWFFontCharacter_s)); SWFCharacterInit((SWFCharacter)inst); BLOCK(inst)->type = SWF_DEFINEFONT2; BLOCK(inst)->writeBlock = writeSWFFontCharacterToMethod; BLOCK(inst)->complete = completeSWFFontCharacter; BLOCK(inst)->dtor = (destroySWFBlockMethod) destroySWFFontCharacter; CHARACTERID(inst) = ++SWF_gNumCharacters; inst->font = font; inst->flags = font->flags; // (unsigned char)(font->flags & /*(~SWF_FONT_HASLAYOUT) &*/ (~SWF_FONT_WIDEOFFSETS)); inst->nGlyphs = 0; inst->codeTable = NULL; inst->dump = 0; inst->textList = NULL; inst->currentList = NULL; inst->out = NULL; return inst;}SWFFontCharacternewSWFDummyFontCharacter(){ SWFFontCharacter ret = (SWFFontCharacter) malloc(sizeof (struct SWFFontCharacter_s)); SWFCharacterInit((SWFCharacter) ret); BLOCK(ret)->type = SWF_DEFINEFONT; BLOCK(ret)->complete = completeSWFImportCharacter; BLOCK(ret)->writeBlock = NULL; BLOCK(ret)->dtor = NULL; CHARACTERID(ret) = ++SWF_gNumCharacters; ret->flags = SWF_FONT_HASLAYOUT; ret->nGlyphs = 1; ret->codeTable = NULL; ret->out = NULL; return ret;} static intSWFFont_findGlyphCode(SWFFont font, unsigned short c){ if ( font->flags & SWF_FONT_WIDECODES ) { byte high = c >> 8; byte low = c & 0xff; if ( font->codeToGlyph.wideMap[high] != NULL ) return font->codeToGlyph.wideMap[high][low]; else return -1; } else { if ( (c & 0xff00) == 0 ) return font->codeToGlyph.charMap[(byte)c]; else return -1; }}voidSWFFontCharacter_addTextToList(SWFFontCharacter font, SWFTextRecord text){ struct textList* textList = (struct textList* )malloc(sizeof(struct textList)); textList->next = font->textList; textList->text = text;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -