| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <cstdlib> |
| #include <cmath> |
| #include <cstring> |
| #include <memory> |
| #include "pprdrv.h" |
| #include "truetype.h" |
| #include <algorithm> |
| #include <stack> |
| #include <list> |
|
|
| class GlyphToType3 |
| { |
| private: |
| GlyphToType3& operator=(const GlyphToType3& other); |
| GlyphToType3(const GlyphToType3& other); |
|
|
| |
| int llx,lly,urx,ury; |
| int advance_width; |
|
|
| |
| int *epts_ctr; |
| int num_pts, num_ctr; |
| FWord *xcoor, *ycoor; |
| BYTE *tt_flags; |
|
|
| int stack_depth; |
|
|
| void load_char(TTFONT* font, BYTE *glyph); |
| void stack(TTStreamWriter& stream, int new_elem); |
| void stack_end(TTStreamWriter& stream); |
| void PSConvert(TTStreamWriter& stream); |
| void PSCurveto(TTStreamWriter& stream, |
| FWord x0, FWord y0, |
| FWord x1, FWord y1, |
| FWord x2, FWord y2); |
| void PSMoveto(TTStreamWriter& stream, int x, int y); |
| void PSLineto(TTStreamWriter& stream, int x, int y); |
| void do_composite(TTStreamWriter& stream, struct TTFONT *font, BYTE *glyph); |
|
|
| public: |
| GlyphToType3(TTStreamWriter& stream, struct TTFONT *font, int charindex, bool embedded = false); |
| ~GlyphToType3(); |
| }; |
|
|
| |
| |
| |
| enum Flag { ON_PATH, OFF_PATH }; |
| struct FlaggedPoint |
| { |
| enum Flag flag; |
| FWord x; |
| FWord y; |
| FlaggedPoint(Flag flag_, FWord x_, FWord y_): flag(flag_), x(x_), y(y_) {}; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void GlyphToType3::stack(TTStreamWriter& stream, int new_elem) |
| { |
| if ( num_pts > 25 ) |
| { |
| if (stack_depth == 0) |
| { |
| stream.put_char('{'); |
| stack_depth=1; |
| } |
|
|
| stack_depth += new_elem; |
|
|
| if (stack_depth > 100) |
| { |
| stream.puts("}_e{"); |
| stack_depth = 3 + new_elem; |
| } |
| } |
| } |
|
|
| void GlyphToType3::stack_end(TTStreamWriter& stream) |
| { |
| if ( stack_depth ) |
| { |
| stream.puts("}_e"); |
| stack_depth=0; |
| } |
| } |
|
|
| |
| |
| |
| |
| void GlyphToType3::PSConvert(TTStreamWriter& stream) |
| { |
| int j, k; |
|
|
| |
| |
| |
| for(j = k = 0; k < num_ctr; k++) |
| { |
| |
| |
| |
| |
| |
| |
| std::list<FlaggedPoint> points; |
|
|
| |
| for (; j <= epts_ctr[k]; j++) |
| { |
| if (!(tt_flags[j] & 1)) { |
| points.push_back(FlaggedPoint(OFF_PATH, xcoor[j], ycoor[j])); |
| } else { |
| points.push_back(FlaggedPoint(ON_PATH, xcoor[j], ycoor[j])); |
| } |
| } |
|
|
| if (points.size() == 0) { |
| |
| continue; |
| } |
|
|
| |
| |
| FlaggedPoint prev = points.back(); |
| for (std::list<FlaggedPoint>::iterator it = points.begin(); |
| it != points.end(); |
| it++) |
| { |
| if (prev.flag == OFF_PATH && it->flag == OFF_PATH) |
| { |
| points.insert(it, |
| FlaggedPoint(ON_PATH, |
| (prev.x + it->x) / 2, |
| (prev.y + it->y) / 2)); |
| } |
| prev = *it; |
| } |
| |
| |
| |
| if (points.front().flag == OFF_PATH) |
| { |
| assert(points.back().flag == ON_PATH); |
| points.insert(points.begin(), points.back()); |
| } |
| else |
| { |
| assert(points.front().flag == ON_PATH); |
| points.push_back(points.front()); |
| } |
|
|
| |
| stack(stream, 3); |
| PSMoveto(stream, points.front().x, points.front().y); |
|
|
| |
| std::list<FlaggedPoint>::const_iterator it = points.begin(); |
| for (it++; it != points.end(); ) |
| { |
| const FlaggedPoint& point = *it; |
| if (point.flag == ON_PATH) |
| { |
| stack(stream, 3); |
| PSLineto(stream, point.x, point.y); |
| it++; |
| } else { |
| std::list<FlaggedPoint>::const_iterator prev = it, next = it; |
| prev--; |
| next++; |
| assert(prev->flag == ON_PATH); |
| assert(next->flag == ON_PATH); |
| stack(stream, 7); |
| PSCurveto(stream, |
| prev->x, prev->y, |
| point.x, point.y, |
| next->x, next->y); |
| it++; |
| it++; |
| } |
| } |
| } |
|
|
| |
| stack(stream, 1); |
| stream.puts("_cl"); |
| } |
|
|
| void GlyphToType3::PSMoveto(TTStreamWriter& stream, int x, int y) |
| { |
| stream.printf("%d %d _m\n", x, y); |
| } |
|
|
| void GlyphToType3::PSLineto(TTStreamWriter& stream, int x, int y) |
| { |
| stream.printf("%d %d _l\n", x, y); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| void GlyphToType3::PSCurveto(TTStreamWriter& stream, |
| FWord x0, FWord y0, |
| FWord x1, FWord y1, |
| FWord x2, FWord y2) |
| { |
| double sx[3], sy[3], cx[3], cy[3]; |
|
|
| sx[0] = x0; |
| sy[0] = y0; |
| sx[1] = x1; |
| sy[1] = y1; |
| sx[2] = x2; |
| sy[2] = y2; |
| cx[0] = (2*sx[1]+sx[0])/3; |
| cy[0] = (2*sy[1]+sy[0])/3; |
| cx[1] = (sx[2]+2*sx[1])/3; |
| cy[1] = (sy[2]+2*sy[1])/3; |
| cx[2] = sx[2]; |
| cy[2] = sy[2]; |
| stream.printf("%d %d %d %d %d %d _c\n", |
| (int)cx[0], (int)cy[0], (int)cx[1], (int)cy[1], |
| (int)cx[2], (int)cy[2]); |
| } |
|
|
| |
| |
| |
| |
| GlyphToType3::~GlyphToType3() |
| { |
| free(tt_flags); |
| free(xcoor); |
| free(ycoor); |
| free(epts_ctr); |
| } |
|
|
| |
| |
| |
| |
| |
| void GlyphToType3::load_char(TTFONT* font, BYTE *glyph) |
| { |
| int x; |
| BYTE c, ct; |
|
|
| |
| epts_ctr = (int *)calloc(num_ctr,sizeof(int)); |
| for (x = 0; x < num_ctr; x++) |
| { |
| epts_ctr[x] = getUSHORT(glyph); |
| glyph += 2; |
| } |
|
|
| |
| |
| num_pts = epts_ctr[num_ctr-1]+1; |
| #ifdef DEBUG_TRUETYPE |
| debug("num_pts=%d",num_pts); |
| stream.printf("%% num_pts=%d\n",num_pts); |
| #endif |
|
|
| |
| x = getUSHORT(glyph); |
| glyph += 2; |
| glyph += x; |
|
|
| |
| tt_flags = (BYTE *)calloc(num_pts,sizeof(BYTE)); |
| xcoor = (FWord *)calloc(num_pts,sizeof(FWord)); |
| ycoor = (FWord *)calloc(num_pts,sizeof(FWord)); |
|
|
| |
| |
| for (x = 0; x < num_pts; ) |
| { |
| tt_flags[x++] = c = *(glyph++); |
|
|
| if (c&8) |
| { |
| ct = *(glyph++); |
|
|
| if ( (x + ct) > num_pts ) |
| { |
| throw TTException("Error in TT flags"); |
| } |
|
|
| while (ct--) |
| { |
| tt_flags[x++] = c; |
| } |
| } |
| } |
|
|
| |
| for (x = 0; x < num_pts; x++) |
| { |
| if (tt_flags[x] & 2) |
| { |
| |
| c = *(glyph++); |
| xcoor[x] = (tt_flags[x] & 0x10) ? c : (-1 * (int)c); |
| } |
| else if (tt_flags[x] & 0x10) |
| { |
| xcoor[x] = 0; |
| } |
| else |
| { |
| xcoor[x] = getFWord(glyph); |
| glyph+=2; |
| } |
| } |
|
|
| |
| for (x = 0; x < num_pts; x++) |
| { |
| if (tt_flags[x] & 4) |
| { |
| |
| c = *(glyph++); |
| ycoor[x] = (tt_flags[x] & 0x20) ? c : (-1 * (int)c); |
| } |
| else if (tt_flags[x] & 0x20) |
| { |
| ycoor[x] = 0; |
| } |
| else |
| { |
| ycoor[x] = getUSHORT(glyph); |
| glyph+=2; |
| } |
| } |
|
|
| |
| for (x = 1; x < num_pts; x++) |
| { |
| xcoor[x] += xcoor[x-1]; |
| ycoor[x] += ycoor[x-1]; |
| } |
|
|
| for (x=0; x < num_pts; x++) |
| { |
| xcoor[x] = topost(xcoor[x]); |
| ycoor[x] = topost(ycoor[x]); |
| } |
|
|
| } |
|
|
| |
| |
| |
| void GlyphToType3::do_composite(TTStreamWriter& stream, struct TTFONT *font, BYTE *glyph) |
| { |
| USHORT flags; |
| USHORT glyphIndex; |
| int arg1; |
| int arg2; |
|
|
| |
| do |
| { |
| flags = getUSHORT(glyph); |
| glyph += 2; |
|
|
| glyphIndex = getUSHORT(glyph); |
| glyph += 2; |
|
|
| if (flags & ARG_1_AND_2_ARE_WORDS) |
| { |
| |
| arg1 = getSHORT(glyph); |
| glyph += 2; |
| arg2 = getSHORT(glyph); |
| glyph += 2; |
| } |
| else |
| { |
| |
| arg1 = *(signed char *)(glyph++); |
| arg2 = *(signed char *)(glyph++); |
| } |
|
|
| if (flags & WE_HAVE_A_SCALE) |
| { |
| glyph += 2; |
| } |
| else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) |
| { |
| glyph += 4; |
| } |
| else if (flags & WE_HAVE_A_TWO_BY_TWO) |
| { |
| glyph += 8; |
| } |
| else |
| { |
| } |
|
|
| |
| #ifdef DEBUG_TRUETYPE |
| stream.printf("%% flags=%d, arg1=%d, arg2=%d\n", |
| (int)flags,arg1,arg2); |
| #endif |
|
|
| |
| |
| if ( flags & ARGS_ARE_XY_VALUES ) |
| { |
| if ( arg1 != 0 || arg2 != 0 ) |
| stream.printf("gsave %d %d translate\n", topost(arg1), topost(arg2) ); |
| } |
| else |
| { |
| stream.printf("%% unimplemented shift, arg1=%d, arg2=%d\n",arg1,arg2); |
| } |
|
|
| |
| stream.printf("false CharStrings /%s get exec\n", |
| ttfont_CharStrings_getname(font, glyphIndex)); |
|
|
| |
| |
| if ( flags & ARGS_ARE_XY_VALUES && (arg1 != 0 || arg2 != 0) ) |
| { |
| stream.puts("grestore "); |
| } |
|
|
| } |
| while (flags & MORE_COMPONENTS); |
|
|
| } |
|
|
| |
| |
| |
| BYTE *find_glyph_data(struct TTFONT *font, int charindex) |
| { |
| ULONG off; |
| ULONG length; |
|
|
| |
| if (font->indexToLocFormat == 0) |
| { |
| off = getUSHORT( font->loca_table + (charindex * 2) ); |
| off *= 2; |
| length = getUSHORT( font->loca_table + ((charindex+1) * 2) ); |
| length *= 2; |
| length -= off; |
| } |
| else |
| { |
| off = getULONG( font->loca_table + (charindex * 4) ); |
| length = getULONG( font->loca_table + ((charindex+1) * 4) ); |
| length -= off; |
| } |
|
|
| if (length > 0) |
| { |
| return font->glyf_table + off; |
| } |
| else |
| { |
| return (BYTE*)NULL; |
| } |
|
|
| } |
|
|
| GlyphToType3::GlyphToType3(TTStreamWriter& stream, struct TTFONT *font, int charindex, bool embedded ) |
| { |
| BYTE *glyph; |
|
|
| tt_flags = NULL; |
| xcoor = NULL; |
| ycoor = NULL; |
| epts_ctr = NULL; |
| stack_depth = 0; |
|
|
| |
| glyph = find_glyph_data( font, charindex ); |
|
|
| |
| |
| if ( glyph == (BYTE*)NULL ) |
| { |
| llx=lly=urx=ury=0; |
| num_ctr=0; |
| } |
| else |
| { |
| |
| num_ctr = getSHORT(glyph); |
|
|
| |
| llx = getFWord(glyph + 2); |
| lly = getFWord(glyph + 4); |
| urx = getFWord(glyph + 6); |
| ury = getFWord(glyph + 8); |
|
|
| |
| glyph += 10; |
| } |
|
|
| |
| if (num_ctr > 0) |
| { |
| load_char(font, glyph); |
| } |
| else |
| { |
| num_pts=0; |
| } |
|
|
| |
| |
| if ( charindex < font->numberOfHMetrics ) |
| { |
| advance_width = getuFWord( font->hmtx_table + (charindex * 4) ); |
| } |
| else |
| { |
| advance_width = getuFWord( font->hmtx_table + ((font->numberOfHMetrics-1) * 4) ); |
| } |
|
|
| |
| |
| stack(stream, 7); |
| if (font->target_type == PS_TYPE_42_3_HYBRID) |
| { |
| stream.printf("pop gsave .001 .001 scale %d 0 %d %d %d %d setcachedevice\n", |
| topost(advance_width), |
| topost(llx), topost(lly), topost(urx), topost(ury) ); |
| } |
| else |
| { |
| stream.printf("%d 0 %d %d %d %d _sc\n", |
| topost(advance_width), |
| topost(llx), topost(lly), topost(urx), topost(ury) ); |
| } |
|
|
| |
| |
| if ( num_ctr > 0 ) |
| { |
| PSConvert(stream); |
| } |
| else if ( num_ctr < 0 ) |
| { |
| do_composite(stream, font, glyph); |
| } |
|
|
| if (font->target_type == PS_TYPE_42_3_HYBRID) |
| { |
| stream.printf("\ngrestore\n"); |
| } |
|
|
| stack_end(stream); |
| } |
|
|
| |
| |
| |
| void tt_type3_charproc(TTStreamWriter& stream, struct TTFONT *font, int charindex) |
| { |
| GlyphToType3 glyph(stream, font, charindex); |
| } |
|
|
| |
| |
| |
| |
| |
| void ttfont_add_glyph_dependencies(struct TTFONT *font, std::vector<int>& glyph_ids) |
| { |
| std::sort(glyph_ids.begin(), glyph_ids.end()); |
|
|
| std::stack<int> glyph_stack; |
| for (std::vector<int>::iterator i = glyph_ids.begin(); |
| i != glyph_ids.end(); ++i) |
| { |
| glyph_stack.push(*i); |
| } |
|
|
| while (glyph_stack.size()) |
| { |
| int gind = glyph_stack.top(); |
| glyph_stack.pop(); |
|
|
| BYTE* glyph = find_glyph_data( font, gind ); |
| if (glyph != (BYTE*)NULL) |
| { |
|
|
| int num_ctr = getSHORT(glyph); |
| if (num_ctr <= 0) |
| { |
|
|
| glyph += 10; |
| USHORT flags = 0; |
|
|
| do |
| { |
| flags = getUSHORT(glyph); |
| glyph += 2; |
| gind = (int)getUSHORT(glyph); |
| glyph += 2; |
|
|
| std::vector<int>::iterator insertion = |
| std::lower_bound(glyph_ids.begin(), glyph_ids.end(), gind); |
| if (insertion == glyph_ids.end() || *insertion != gind) |
| { |
| glyph_ids.insert(insertion, gind); |
| glyph_stack.push(gind); |
| } |
|
|
| if (flags & ARG_1_AND_2_ARE_WORDS) |
| { |
| glyph += 4; |
| } |
| else |
| { |
| glyph += 2; |
| } |
|
|
| if (flags & WE_HAVE_A_SCALE) |
| { |
| glyph += 2; |
| } |
| else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) |
| { |
| glyph += 4; |
| } |
| else if (flags & WE_HAVE_A_TWO_BY_TWO) |
| { |
| glyph += 8; |
| } |
| } |
| while (flags & MORE_COMPONENTS); |
| } |
| } |
| } |
| } |
|
|
| |
|
|