📄 fad.fontext.c
字号:
/** * libFAD - Flash Animation Decode library * Copyright (C) 2005-2006 VGSystem Technologies, Inc. * * libFAD is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * source distribution. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: fad.fontext.c,v 1.42 2006/05/06 15:18:44 wrxzzj Exp $ */#include "fad.fontext.h"#include "fad.tags.h"#include "fad.bits.h"#include "fad.shape.h"//#define FAD_ERRORtypedef struct { s16_t ascent, descent, leading; s16_t* fat;} font_attr_t;typedef struct { fad_object_t base; u8_t flag, lc; u16_t glyphs_num; shape_record_t* glyphs_array; u8_t *ct, *name, *fa;} font_t;typedef struct { fad_object_t base; rect_t bound; /**tm: static text matrix*/ cairo_matrix_t tm; u8_t* tr; dynarray_t* dict; u8_t ver;} static_text_t;static void _static_text_get_rect(fad_object_t* fo, rect_t *rt) { static_text_t* st = (static_text_t* )fo; *rt = st->bound;}static u8_t _static_text_do_render(fad_object_t* fo, fad_render_t* render, dl_node_t *node) { static_text_t* st = (static_text_t* )fo; font_t* font = NULL; bits_t bits; u8_t flag = 0, glyph_count, glyph_nbits, advance_nbits; s16_t x = 0, y = 0; s32_t glyph_advance; double xoffset = st->bound.x0/20.00, yoffset = st->bound.y0/20.00; u32_t glyph_idx; u16_t th; cairo_matrix_t mx; u8_t r, g, b, a; bits_init(&bits); bits_buffer(&bits, st->tr); cairo_matrix_init_identity(&mx); glyph_nbits = bits_get_u8(&bits); advance_nbits = bits_get_u8(&bits); //FAD_ERROR("static text render start.\n"); while(flag = bits_get_u8(&bits)) { if(flag&0x08) { /**has font*/ u16_t id = 0; id = bits_get_u16(&bits); font = (font_t* )st->dict->get(st->dict, id); if(font == NULL) { //FAD_ERROR("font is unavailable for static text, glyph_nbits = %d, advance_nbits = %d, flag = %x, id = %d, tr = %x\n", glyph_nbits, advance_nbits, flag, id, st->tr); break; } //FAD_DEBUG("GET font id = %x, %x for static text\n", id, font); } if(flag&0x04) { /**has text color*/ u8_t* c = (u8_t* )bits_tell(&bits); r = *c; g = *(c+1); b = *(c+2); if(st->ver == 2) { a = *(c+3); bits_seek_nbytes(&bits, 4); } else { a = 0xff; bits_seek_nbytes(&bits, 3); } //FAD_ERROR("(rm = %d, ra = %d, gm = %d, ga = %d, bm = %d, ba = %d, am = %d, aa = %d.\n", // node->cx.base.rm, node->cx.base.ra, node->cx.base.gm, node->cx.base.ga, // node->cx.base.bm, node->cx.base.ba, node->cx.am, node->cx.aa); if(node->flag & 0x08) { r = COLOR_DO_CXFORM(r, node->cx.base.rm, node->cx.base.ra); g = COLOR_DO_CXFORM(g, node->cx.base.gm, node->cx.base.ga); b = COLOR_DO_CXFORM(b, node->cx.base.bm, node->cx.base.ba); a = COLOR_DO_CXFORM(a, node->cx.am, node->cx.aa); } else { /**if no cxform , just set alpha value to zero, find this problem when play mystery.swf*/ //a = 0x00; } FAD_DEBUG("has text color, r = %d, g = %d, b = %d, a = %d, st->ver = %d.\n", r, g, b, a, st->ver); cairo_set_source_rgba(render->cr, (double)r/0xff, (double)g/0xff, (double)b/0xff, (double)a/0xff); } if(flag&0x01) { xoffset = (st->bound.x0 + bits_get_s16(&bits))/20.00; FAD_ERROR("new static text xoffset = %f\n", xoffset); } if(flag&0x02) { yoffset = (double)bits_get_s16(&bits)/20.00; FAD_ERROR("new static text yoffset = %f\n", yoffset); } //FAD_ERROR("static text rectangle , xoffset = %f, yoffset = %f\n", xoffset, yoffset); if(flag&0x08) { th = bits_get_u16(&bits); //FAD_ERROR("static text, height = %d\n", th); mx.xx = mx.yy = (double)th/1024; } mx.y0 = yoffset; glyph_count = bits_get_u8(&bits); while(glyph_count--) { cairo_matrix_t tempMX; cairo_matrix_init_identity(&tempMX); glyph_idx = bits_get_ubits(&bits, glyph_nbits); glyph_advance = bits_get_sbits(&bits, advance_nbits); //FAD_ERROR("static text, advance = %d\n", glyph_advance); mx.x0 = xoffset; cairo_matrix_multiply (&tempMX, &mx, &node->mx); cairo_set_matrix(render->cr, &tempMX); shape_record_do_render(font->glyphs_array+glyph_idx, render, node);#ifdef LIBFAD_DO_RENDER cairo_fill(render->cr);#endif xoffset += (double)glyph_advance/20.00; } /**while(glyph_count--)*/ } /**while(flag = ...)*/ //FAD_ERROR("static text render end.\n"); bits_finish(&bits); return FAD_TRUE;}s32_t static_text_decode(fad_frame_t* frame, fad_stream_t* s) { static_text_t* st = NULL; u16_t id; st = calloc(1, sizeof(static_text_t)); if(st == NULL) { s->err = FAD_ERROR_MEM; return -1; } st->base.do_render = _static_text_do_render; st->base.get_rect = _static_text_get_rect; st->base.type = FO_TYPE_STATICTEXT; id = bits_get_u16(&s->bits); bits_get_rect(&s->bits, &st->bound); bits_get_matrix(&s->bits, &st->tm); st->tr = (u8_t* )bits_tell(&s->bits);// bits_dump(&s->bits, 4, stderr); st->dict = s->dict; if(s->tag_id == TAG_DEFINETEXT) st->ver = 1; else st->ver = 2; s->dict->put(s->dict, st, id); FAD_ERROR("append static text to dictionary, id = %d, tr = %x, matrix = (%f, %f, %f, %f, %f, %f)\n", id, st->tr, st->tm.xx, st->tm.yy, st->tm.yx, st->tm.xy, st->tm.x0, st->tm.y0); return 0;}s32_t font_v1_decode(fad_frame_t* frame, fad_stream_t* s) { font_t* font = NULL; u16_t id, i = 0; font = calloc(1, sizeof(font_t)); if(font == NULL) { s->err = FAD_ERROR_MEM; return -1; } id = bits_get_u16(&s->bits); font->glyphs_num = bits_get_u16(&s->bits); bits_seek_nbytes(&s->bits, font->glyphs_num-2); font->glyphs_num = font->glyphs_num>>1; font->glyphs_array = calloc(font->glyphs_num, sizeof(shape_record_t)); for(i = 0; i<font->glyphs_num; i++) { //FAD_ERROR("parsing idx = %d glyphs array.\n", i); shape_record_parse(&s->bits, font->glyphs_array+i); } s->dict->put(s->dict, font, id); FAD_ERROR("append font id = %x, glyphs_num = %d, ptr = %x , glyphs_array = %x to dictionary.\n", id, font->glyphs_num, font, font->glyphs_array); return 0;}s32_t font_v2_decode(fad_frame_t* frame, fad_stream_t* s) { font_t* font = NULL; u16_t id; u32_t ct_offset, i = 0; font = calloc(1, sizeof(font_t)); if(font == NULL) { s->err = FAD_ERROR_MEM; return -1; } id = bits_get_u16(&s->bits); font->flag = bits_get_u8(&s->bits); font->lc = bits_get_u8(&s->bits); font->name = (u8_t* )bits_tell(&s->bits); bits_seek_nbytes(&s->bits, *(font->name)+1); font->glyphs_num = bits_get_u16(&s->bits); font->glyphs_array = calloc(font->glyphs_num, sizeof(shape_record_t)); if(font->flag&0x08) { bits_seek_nbytes(&s->bits, font->glyphs_num<<2); ct_offset = bits_get_u32(&s->bits); } else { bits_seek_nbytes(&s->bits, font->glyphs_num<<1); ct_offset = bits_get_u16(&s->bits); } for(; i<font->glyphs_num; i++) { shape_record_parse(&s->bits, font->glyphs_array+i); } font->ct = (u8_t* )bits_tell(&s->bits); if(font->flag&0x04) bits_seek_nbytes(&s->bits, font->glyphs_num<<1); else bits_seek_nbytes(&s->bits, font->glyphs_num); if(font->flag&0x80) font->fa = (u8_t* )bits_tell(&s->bits); s->dict->put(s->dict, font, id); FAD_ERROR("append font2 to dictionary, id = %d, ptr = %x, flag = %x\n", id, font, font->flag); return 0;}s32_t fontinfo_decode(fad_frame_t* frame, fad_stream_t* s) { font_t* font = NULL; u8_t flag; u16_t id = bits_get_u16(&s->bits); font = (font_t* )s->dict->get(s->dict, id); if(font == NULL) return -1; font->name = (u8_t* )bits_tell(&s->bits); bits_seek_nbytes(&s->bits, *(font->name)+1); flag = bits_get_u8(&s->bits); if(flag&0x20) font->flag |= 0x20; if(flag&0x10) font->flag |= 0x40; if(flag&0x08) font->flag |= 0x10; if(flag&0x04) font->flag |= 0x02; if(flag&0x02) font->flag |= 0x01; if(flag&0x01) font->flag |= 0x04; if(s->tag_id == TAG_DEFINEFONTINFO2) font->lc = bits_get_u8(&s->bits); font->ct = (u8_t* )bits_tell(&s->bits); FAD_ERROR("append fontinfo to font(id = %x, ptr = %x), flag = %x\n", id, font, font->flag); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -