bool TTBMFont::addFontGlyph(int fontnum,FT_UInt glyphIndex,wchar32_t chr) { FT_Error error; FT_Face face=fontFaces_[fontnum].face; error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT); if (error) return false; int top, left, width, height; if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) { FT_BBox bbox; if (stroker) { FT_Glyph glyph; error = FT_Get_Glyph(face->glyph, &glyph); if (error) return false; error = FT_Glyph_StrokeBorder(&glyph, stroker, false, true); if (error) return false; FT_OutlineGlyph oGlyph = reinterpret_cast<FT_OutlineGlyph>(glyph); FT_Outline_Get_CBox(&oGlyph->outline, &bbox); FT_Done_Glyph(glyph); } else FT_Outline_Get_CBox(&face->glyph->outline, &bbox); bbox.xMin &= ~63; bbox.yMin &= ~63; bbox.xMax = (bbox.xMax + 63) & ~63; bbox.yMax = (bbox.yMax + 63) & ~63; width = (bbox.xMax - bbox.xMin) >> 6; height = (bbox.yMax - bbox.yMin) >> 6; top = bbox.yMax >> 6; left = bbox.xMin >> 6; } else if (face->glyph->format == FT_GLYPH_FORMAT_BITMAP) {
// 让一个字体槽加粗,并且填充其他的大小属性 void New_GlyphSlot_Embolden( FT_GlyphSlot slot, const FT_Pos str_x, const FT_Pos str_y) { FT_Library library; FT_Face face; FT_Error error; FT_BBox newBox, oldBox; FT_Pos xstr = (FT_Pos)str_x; FT_Pos ystr = (FT_Pos)str_y; CC_ASSERT(slot != NULL); library = slot->library; face = slot->face; if ( slot->format != FT_GLYPH_FORMAT_OUTLINE && slot->format != FT_GLYPH_FORMAT_BITMAP ) return; if ( slot->format == FT_GLYPH_FORMAT_OUTLINE ) { FT_Outline_Get_CBox(&slot->outline , &oldBox); error = New_FT_Outline_Embolden( &slot->outline, xstr , ystr); if ( error ) return; FT_Outline_Get_CBox(&slot->outline , &newBox); xstr = (newBox.xMax - newBox.xMin) - (oldBox.xMax - oldBox.xMin); ystr = (newBox.yMax - newBox.yMin) - (oldBox.yMax - oldBox.yMin); } else if ( slot->format == FT_GLYPH_FORMAT_BITMAP ) { xstr = FT_PIX_FLOOR( xstr ); if ( xstr == 0 ) xstr = 1 << 6; ystr = FT_PIX_FLOOR( ystr ); error = FT_Bitmap_Embolden( library, &slot->bitmap, xstr, ystr ); if ( error ) return; } if ( slot->advance.x ) slot->advance.x += xstr; if ( slot->advance.y ) slot->advance.y += ystr; slot->metrics.width += xstr; slot->metrics.height += ystr; slot->metrics.horiBearingY += ystr; slot->metrics.horiAdvance += xstr; slot->metrics.vertBearingX -= xstr / 2; slot->metrics.vertBearingY += ystr; slot->metrics.vertAdvance += ystr; if ( slot->format == FT_GLYPH_FORMAT_BITMAP ) slot->bitmap_top += ystr >> 6; }
static void profile_outline( FT_Outline* outline, long repeat ) { FT_BBox bbox; long count; long time0; time0 = get_time(); for ( count = repeat; count > 0; count-- ) FT_Outline_Get_CBox( outline, &bbox ); time0 = get_time() - time0; printf( "time = %5.2f cbox = [%.2f %.2f %.2f %.2f]\n", ((double)time0/10000.0), XVAL( bbox.xMin ), XVAL( bbox.yMin ), XVAL( bbox.xMax ), XVAL( bbox.yMax ) ); time0 = get_time(); for ( count = repeat; count > 0; count-- ) FT_Outline_Get_BBox( outline, &bbox ); time0 = get_time() - time0; printf( "time = %5.2f bbox = [%.2f %.2f %.2f %.2f]\n", ((double)time0/10000.0), XVAL( bbox.xMin ), XVAL( bbox.yMin ), XVAL( bbox.xMax ), XVAL( bbox.yMax ) ); }
ft_outline_glyph_bbox( FT_Glyph outline_glyph, FT_BBox* bbox ) { FT_OutlineGlyph glyph = (FT_OutlineGlyph)outline_glyph; FT_Outline_Get_CBox( &glyph->outline, bbox ); }
static PyObject* Py_Outline_get_cbox(Py_Outline* self, PyObject* args, PyObject* kwds) { FT_BBox bbox; FT_Outline_Get_CBox(&self->x, &bbox); return Py_BBox_cnew(&bbox, 1.0); }
/* return the glyph's control box */ static void ft_smooth_get_cbox( FT_Renderer render, FT_GlyphSlot slot, FT_BBox* cbox ) { FT_MEM_SET( cbox, 0, sizeof ( *cbox ) ); if ( slot->format == render->glyph_format ) FT_Outline_Get_CBox( &slot->outline, cbox ); }
/* convert a slot's glyph image into a bitmap */ static FT_Error ft_smooth_render_generic( FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector* origin, FT_Render_Mode required_mode ) { FT_Error error; FT_Outline* outline = NULL; FT_BBox cbox; FT_UInt width, height, pitch; #ifndef FT_CONFIG_OPTION_SUBPIXEL_RENDERING FT_UInt height_org, width_org; #endif FT_Bitmap* bitmap; FT_Memory memory; FT_Int hmul = mode == FT_RENDER_MODE_LCD; FT_Int vmul = mode == FT_RENDER_MODE_LCD_V; FT_Pos x_shift, y_shift, x_left, y_top; FT_Raster_Params params; /* check glyph image format */ if ( slot->format != render->glyph_format ) { error = Smooth_Err_Invalid_Argument; goto Exit; } /* check mode */ if ( mode != required_mode ) return Smooth_Err_Cannot_Render_Glyph; outline = &slot->outline; /* translate the outline to the new origin if needed */ if ( origin ) FT_Outline_Translate( outline, origin->x, origin->y ); /* compute the control box, and grid fit it */ FT_Outline_Get_CBox( outline, &cbox ); cbox.xMin = FT_PIX_FLOOR( cbox.xMin ); cbox.yMin = FT_PIX_FLOOR( cbox.yMin ); cbox.xMax = FT_PIX_CEIL( cbox.xMax ); cbox.yMax = FT_PIX_CEIL( cbox.yMax ); if ( cbox.xMin < 0 && cbox.xMax > FT_INT_MAX + cbox.xMin ) { FT_ERROR(( "ft_smooth_render_generic: glyph too large:" " xMin = %d, xMax = %d\n", cbox.xMin >> 6, cbox.xMax >> 6 )); return Smooth_Err_Raster_Overflow; }
/* return the glyph's control box */ static void ft_raster1_get_cbox(FT_Renderer render, FT_GlyphSlot slot, FT_BBox *cbox) { MEM_Set(cbox, 0, sizeof(*cbox)); if(slot->format == render->glyph_format) { FT_Outline_Get_CBox(&slot->outline, cbox); } }
Bitmap *outline_to_bitmap(ASS_Library *library, FT_Library ftlib, FT_Outline *outline, int bord) { Bitmap *bm; int w, h; int error; FT_BBox bbox; FT_Bitmap bitmap; FT_Outline_Get_CBox(outline, &bbox); // move glyph to origin (0, 0) bbox.xMin &= ~63; bbox.yMin &= ~63; FT_Outline_Translate(outline, -bbox.xMin, -bbox.yMin); // bitmap size bbox.xMax = (bbox.xMax + 63) & ~63; bbox.yMax = (bbox.yMax + 63) & ~63; w = (bbox.xMax - bbox.xMin) >> 6; h = (bbox.yMax - bbox.yMin) >> 6; // pen offset bbox.xMin >>= 6; bbox.yMax >>= 6; if (w * h > 8000000) { ass_msg(library, MSGL_WARN, "Glyph bounding box too large: %dx%dpx", w, h); return NULL; } // allocate and set up bitmap bm = alloc_bitmap(w + 2 * bord, h + 2 * bord); bm->left = bbox.xMin - bord; bm->top = -bbox.yMax - bord; bitmap.width = w; bitmap.rows = h; bitmap.pitch = bm->stride; bitmap.buffer = bm->buffer + bord + bm->stride * bord; bitmap.num_grays = 256; bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; // render into target bitmap if ((error = FT_Outline_Get_Bitmap(ftlib, outline, &bitmap))) { ass_msg(library, MSGL_WARN, "Failed to rasterize glyph: %d\n", error); ass_free_bitmap(bm); return NULL; } return bm; }
void TTFont::getBounds(const wchar32_t *text, float letterSpacing, int *pminx, int *pminy, int *pmaxx, int *pmaxy) const { float scalex = application_->getLogicalScaleX(); int minx = 0x7fffffff; int miny = 0x7fffffff; int maxx = -0x7fffffff; int maxy = -0x7fffffff; int size = 0; for (const wchar32_t *t = text; *t; ++t, ++size) ; int x = 0, y = 0; FT_UInt prev = 0; for (int i = 0; i < size; ++i) { FT_UInt glyphIndex = FT_Get_Char_Index(face_, text[i]); if (glyphIndex == 0) continue; if (FT_Load_Glyph(face_, glyphIndex, FT_LOAD_DEFAULT)) continue; int top, left, width, height; if (face_->glyph->format == FT_GLYPH_FORMAT_OUTLINE) { FT_BBox bbox; FT_Outline_Get_CBox(&face_->glyph->outline, &bbox); bbox.xMin &= ~63; bbox.yMin &= ~63; bbox.xMax = (bbox.xMax + 63) & ~63; bbox.yMax = (bbox.yMax + 63) & ~63; width = (bbox.xMax - bbox.xMin) >> 6; height = (bbox.yMax - bbox.yMin) >> 6; top = bbox.yMax >> 6; left = bbox.xMin >> 6; } else if (face_->glyph->format == FT_GLYPH_FORMAT_BITMAP)
static void dump_outline( FT_Outline* outline ) { FT_BBox bbox; /* compute and display cbox */ FT_Outline_Get_CBox( outline, &bbox ); printf( "cbox = [%.2f %.2f %.2f %.2f]\n", XVAL( bbox.xMin ), XVAL( bbox.yMin ), XVAL( bbox.xMax ), XVAL( bbox.yMax ) ); /* compute and display bbox */ FT_Outline_Get_BBox( outline, &bbox ); printf( "bbox = [%.2f %.2f %.2f %.2f]\n", XVAL( bbox.xMin ), XVAL( bbox.yMin ), XVAL( bbox.xMax ), XVAL( bbox.yMax ) ); }
/* * \brief Finish a drawing. This only sets the horizontal advance according * to the glyph's bbox at the moment. */ static void drawing_finish(ASS_Drawing *drawing, int raw_mode) { int i, offset; FT_BBox bbox; FT_Outline *ol = &drawing->glyph->outline; // Close the last contour drawing_close_shape(drawing); #if 0 // Dump points for (i = 0; i < ol->n_points; i++) { printf("point (%d, %d)\n", (int) ol->points[i].x, (int) ol->points[i].y); } // Dump contours for (i = 0; i < ol->n_contours; i++) printf("contour %d\n", ol->contours[i]); #endif ass_msg(drawing->library, MSGL_V, "Parsed drawing with %d points and %d contours", ol->n_points, ol->n_contours); if (raw_mode) return; FT_Outline_Get_CBox(&drawing->glyph->outline, &bbox); drawing->glyph->root.advance.x = d6_to_d16(bbox.xMax - bbox.xMin); drawing->desc = double_to_d6(-drawing->pbo * drawing->scale_y); drawing->asc = bbox.yMax - bbox.yMin + drawing->desc; // Place it onto the baseline offset = (bbox.yMax - bbox.yMin) + double_to_d6(-drawing->pbo * drawing->scale_y); for (i = 0; i < ol->n_points; i++) ol->points[i].y += offset; }
static FT_Error LoadTrueTypeChar(Font *fnt, int idx, Boolean hint, Boolean quiet) { FT_Error error; int flags; flags = FT_LOAD_DEFAULT; if (hint) flags |= FT_LOAD_FORCE_AUTOHINT; error = FT_Load_Glyph(face, idx, flags); if (!error) { if (fnt->efactor != 1.0 || fnt->slant != 0.0 ) FT_Outline_Transform(&face->glyph->outline, &matrix1); if (fnt->rotate) { FT_Outline_Transform(&face->glyph->outline, &matrix2); error = FT_Outline_Get_BBox(&face->glyph->outline, &bbox); /* we need the non- grid-fitted bbox */ if (!error) FT_Outline_Translate(&face->glyph->outline, face->glyph->metrics.vertBearingY - bbox.xMin, -fnt->y_offset * ppem * 64); } } if (!error) error = FT_Outline_Get_BBox(&face->glyph->outline, &bbox); if (!error) { FT_Outline_Get_CBox(&face->glyph->outline, &bbox); /* for the case of BBox != CBox */ SetRasterArea(quiet); } return error; }
/* convert a slot's glyph image into a bitmap */ static FT_Error ft_raster1_render(FT_Renderer render, FT_GlyphSlot slot, FT_UInt mode, FT_Vector *origin) { FT_Error error; FT_Outline *outline; FT_BBox cbox; FT_UInt width, height, pitch; FT_Bitmap *bitmap; FT_Memory memory; FT_Raster_Params params; /* check glyph image format */ if(slot->format != render->glyph_format) { error = FT_Err_Invalid_Argument; goto Exit; } /* check rendering mode */ if(mode != ft_render_mode_mono) { /* raster1 is only capable of producing monochrome bitmaps */ if(render->clazz == &ft_raster1_renderer_class) { return FT_Err_Cannot_Render_Glyph; } } else { /* raster5 is only capable of producing 5-gray-levels bitmaps */ if(render->clazz == &ft_raster5_renderer_class) { return FT_Err_Cannot_Render_Glyph; } } outline = &slot->outline; /* translate the outline to the new origin if needed */ if(origin) { FT_Outline_Translate(outline, origin->x, origin->y); } /* compute the control box, and grid fit it */ FT_Outline_Get_CBox(outline, &cbox); cbox.xMin &= -64; cbox.yMin &= -64; cbox.xMax = (cbox.xMax + 63) & - 64; cbox.yMax = (cbox.yMax + 63) & - 64; width = (cbox.xMax - cbox.xMin) >> 6; height = (cbox.yMax - cbox.yMin) >> 6; bitmap = &slot->bitmap; memory = render->root.memory; /* release old bitmap buffer */ if(slot->flags & ft_glyph_own_bitmap) { FREE(bitmap->buffer); slot->flags &= ~ft_glyph_own_bitmap; } /* allocate new one, depends on pixel format */ if(!(mode & ft_render_mode_mono)) { /* we pad to 32 bits, only for backwards compatibility with FT 1.x */ pitch = (width + 3) & - 4; bitmap->pixel_mode = ft_pixel_mode_grays; bitmap->num_grays = 256; } else { pitch = (width + 7) >> 3; bitmap->pixel_mode = ft_pixel_mode_mono; } bitmap->width = width; bitmap->rows = height; bitmap->pitch = pitch; if(ALLOC(bitmap->buffer, (FT_ULong)pitch * height)) { goto Exit; } slot->flags |= ft_glyph_own_bitmap; /* translate outline to render it into the bitmap */ FT_Outline_Translate(outline, -cbox.xMin, -cbox.yMin); /* set up parameters */ params.target = bitmap; params.source = outline; params.flags = 0; if(bitmap->pixel_mode == ft_pixel_mode_grays) { params.flags |= ft_raster_flag_aa; } /* render outline into the bitmap */ error = render->raster_render(render->raster, ¶ms); if(error) { goto Exit; } slot->format = ft_glyph_format_bitmap; slot->bitmap_left = cbox.xMin >> 6; slot->bitmap_top = cbox.yMax >> 6; Exit: return error; }
pfr_slot_load( FT_GlyphSlot pfrslot, /* PFR_Slot */ FT_Size pfrsize, /* PFR_Size */ FT_UInt gindex, FT_Int32 load_flags ) { PFR_Slot slot = (PFR_Slot)pfrslot; PFR_Size size = (PFR_Size)pfrsize; FT_Error error; PFR_Face face = (PFR_Face)pfrslot->face; PFR_Char gchar; FT_Outline* outline = &pfrslot->outline; FT_ULong gps_offset; if ( gindex > 0 ) gindex--; /* check that the glyph index is correct */ FT_ASSERT( gindex < face->phy_font.num_chars ); /* try to load an embedded bitmap */ if ( ( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP ) ) == 0 ) { error = pfr_slot_load_bitmap( slot, size, gindex ); if ( error == 0 ) goto Exit; } if ( load_flags & FT_LOAD_SBITS_ONLY ) { error = PFR_Err_Invalid_Argument; goto Exit; } gchar = face->phy_font.chars + gindex; pfrslot->format = FT_GLYPH_FORMAT_OUTLINE; outline->n_points = 0; outline->n_contours = 0; gps_offset = face->header.gps_section_offset; /* load the glyph outline (FT_LOAD_NO_RECURSE isn't supported) */ error = pfr_glyph_load( &slot->glyph, face->root.stream, gps_offset, gchar->gps_offset, gchar->gps_size ); if ( !error ) { FT_BBox cbox; FT_Glyph_Metrics* metrics = &pfrslot->metrics; FT_Pos advance; FT_Int em_metrics, em_outline; FT_Bool scaling; scaling = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); /* copy outline data */ *outline = slot->glyph.loader->base.outline; outline->flags &= ~FT_OUTLINE_OWNER; outline->flags |= FT_OUTLINE_REVERSE_FILL; if ( size && pfrsize->metrics.y_ppem < 24 ) outline->flags |= FT_OUTLINE_HIGH_PRECISION; /* compute the advance vector */ metrics->horiAdvance = 0; metrics->vertAdvance = 0; advance = gchar->advance; em_metrics = face->phy_font.metrics_resolution; em_outline = face->phy_font.outline_resolution; if ( em_metrics != em_outline ) advance = FT_MulDiv( advance, em_outline, em_metrics ); if ( face->phy_font.flags & PFR_PHY_VERTICAL ) metrics->vertAdvance = advance; else metrics->horiAdvance = advance; pfrslot->linearHoriAdvance = metrics->horiAdvance; pfrslot->linearVertAdvance = metrics->vertAdvance; /* make-up vertical metrics(?) */ metrics->vertBearingX = 0; metrics->vertBearingY = 0; /* Apply the font matrix, if any. */ /* TODO: Test existing fonts with unusual matrix */ /* whether we have to adjust Units per EM. */ { FT_Matrix font_matrix; font_matrix.xx = face->log_font.matrix[0] << 8; font_matrix.yx = face->log_font.matrix[1] << 8; font_matrix.xy = face->log_font.matrix[2] << 8; font_matrix.yy = face->log_font.matrix[3] << 8; FT_Outline_Transform( outline, &font_matrix ); } /* scale when needed */ if ( scaling ) { FT_Int n; FT_Fixed x_scale = pfrsize->metrics.x_scale; FT_Fixed y_scale = pfrsize->metrics.y_scale; FT_Vector* vec = outline->points; /* scale outline points */ for ( n = 0; n < outline->n_points; n++, vec++ ) { vec->x = FT_MulFix( vec->x, x_scale ); vec->y = FT_MulFix( vec->y, y_scale ); } /* scale the advance */ metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); } /* compute the rest of the metrics */ FT_Outline_Get_CBox( outline, &cbox ); metrics->width = cbox.xMax - cbox.xMin; metrics->height = cbox.yMax - cbox.yMin; metrics->horiBearingX = cbox.xMin; metrics->horiBearingY = cbox.yMax - metrics->height; } Exit: return error; }
/* convert a slot's glyph image into a bitmap */ static FT_Error ft_raster1_render( FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector* origin ) { FT_Error error; FT_Outline* outline; FT_BBox cbox, cbox0; FT_UInt width, height, pitch; FT_Bitmap* bitmap; FT_Memory memory; FT_Raster_Params params; /* check glyph image format */ if ( slot->format != render->glyph_format ) { error = Raster_Err_Invalid_Argument; goto Exit; } /* check rendering mode */ #ifndef FT_CONFIG_OPTION_PIC if ( mode != FT_RENDER_MODE_MONO ) { /* raster1 is only capable of producing monochrome bitmaps */ if ( render->clazz == &ft_raster1_renderer_class ) return Raster_Err_Cannot_Render_Glyph; } else { /* raster5 is only capable of producing 5-gray-levels bitmaps */ if ( render->clazz == &ft_raster5_renderer_class ) return Raster_Err_Cannot_Render_Glyph; } #else /* FT_CONFIG_OPTION_PIC */ /* When PIC is enabled, we cannot get to the class object */ /* so instead we check the final character in the class name */ /* ("raster5" or "raster1"). Yes this is a hack. */ /* The "correct" thing to do is have different render function */ /* for each of the classes. */ if ( mode != FT_RENDER_MODE_MONO ) { /* raster1 is only capable of producing monochrome bitmaps */ if ( render->clazz->root.module_name[6] == '1' ) return Raster_Err_Cannot_Render_Glyph; } else { /* raster5 is only capable of producing 5-gray-levels bitmaps */ if ( render->clazz->root.module_name[6] == '5' ) return Raster_Err_Cannot_Render_Glyph; } #endif /* FT_CONFIG_OPTION_PIC */ outline = &slot->outline; /* translate the outline to the new origin if needed */ if ( origin ) FT_Outline_Translate( outline, origin->x, origin->y ); /* compute the control box, and grid fit it */ FT_Outline_Get_CBox( outline, &cbox0 ); /* undocumented but confirmed: bbox values get rounded */ #if 1 cbox.xMin = FT_PIX_ROUND( cbox0.xMin ); cbox.yMin = FT_PIX_ROUND( cbox0.yMin ); cbox.xMax = FT_PIX_ROUND( cbox0.xMax ); cbox.yMax = FT_PIX_ROUND( cbox0.yMax ); #else cbox.xMin = FT_PIX_FLOOR( cbox.xMin ); cbox.yMin = FT_PIX_FLOOR( cbox.yMin ); cbox.xMax = FT_PIX_CEIL( cbox.xMax ); cbox.yMax = FT_PIX_CEIL( cbox.yMax ); #endif /* in the event either width or height round to 0, */ /* try explicitly rounding up/down. In the case of */ /* glyphs containing only one very narrow feature, */ /* this give the drop-out compensation in the */ /* in the scan conversion code to do its stuff. */ width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 ); if ( width == 0 ) { cbox.xMin = FT_PIX_FLOOR( cbox0.xMin ); cbox.xMax = FT_PIX_CEIL( cbox0.xMax ); width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 ); }
cff_slot_load( CFF_GlyphSlot glyph, CFF_Size size, FT_UInt glyph_index, FT_Int32 load_flags ) { FT_Error error; CFF_Decoder decoder; PS_Decoder psdecoder; TT_Face face = (TT_Face)glyph->root.face; FT_Bool hinting, scaled, force_scaling; CFF_Font cff = (CFF_Font)face->extra.data; PSAux_Service psaux = (PSAux_Service)face->psaux; const CFF_Decoder_Funcs decoder_funcs = psaux->cff_decoder_funcs; FT_Matrix font_matrix; FT_Vector font_offset; force_scaling = FALSE; /* in a CID-keyed font, consider `glyph_index' as a CID and map */ /* it immediately to the real glyph_index -- if it isn't a */ /* subsetted font, glyph_indices and CIDs are identical, though */ if ( cff->top_font.font_dict.cid_registry != 0xFFFFU && cff->charset.cids ) { /* don't handle CID 0 (.notdef) which is directly mapped to GID 0 */ if ( glyph_index != 0 ) { glyph_index = cff_charset_cid_to_gindex( &cff->charset, glyph_index ); if ( glyph_index == 0 ) return FT_THROW( Invalid_Argument ); } } else if ( glyph_index >= cff->num_glyphs ) return FT_THROW( Invalid_Argument ); if ( load_flags & FT_LOAD_NO_RECURSE ) load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; glyph->x_scale = 0x10000L; glyph->y_scale = 0x10000L; if ( size ) { glyph->x_scale = size->root.metrics.x_scale; glyph->y_scale = size->root.metrics.y_scale; } #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS /* try to load embedded bitmap if any */ /* */ /* XXX: The convention should be emphasized in */ /* the documents because it can be confusing. */ if ( size ) { CFF_Face cff_face = (CFF_Face)size->root.face; SFNT_Service sfnt = (SFNT_Service)cff_face->sfnt; FT_Stream stream = cff_face->root.stream; if ( size->strike_index != 0xFFFFFFFFUL && sfnt->load_eblc && ( load_flags & FT_LOAD_NO_BITMAP ) == 0 ) { TT_SBit_MetricsRec metrics; error = sfnt->load_sbit_image( face, size->strike_index, glyph_index, (FT_UInt)load_flags, stream, &glyph->root.bitmap, &metrics ); if ( !error ) { FT_Bool has_vertical_info; FT_UShort advance; FT_Short dummy; glyph->root.outline.n_points = 0; glyph->root.outline.n_contours = 0; glyph->root.metrics.width = (FT_Pos)metrics.width << 6; glyph->root.metrics.height = (FT_Pos)metrics.height << 6; glyph->root.metrics.horiBearingX = (FT_Pos)metrics.horiBearingX << 6; glyph->root.metrics.horiBearingY = (FT_Pos)metrics.horiBearingY << 6; glyph->root.metrics.horiAdvance = (FT_Pos)metrics.horiAdvance << 6; glyph->root.metrics.vertBearingX = (FT_Pos)metrics.vertBearingX << 6; glyph->root.metrics.vertBearingY = (FT_Pos)metrics.vertBearingY << 6; glyph->root.metrics.vertAdvance = (FT_Pos)metrics.vertAdvance << 6; glyph->root.format = FT_GLYPH_FORMAT_BITMAP; if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) { glyph->root.bitmap_left = metrics.vertBearingX; glyph->root.bitmap_top = metrics.vertBearingY; } else { glyph->root.bitmap_left = metrics.horiBearingX; glyph->root.bitmap_top = metrics.horiBearingY; } /* compute linear advance widths */ (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 0, glyph_index, &dummy, &advance ); glyph->root.linearHoriAdvance = advance; has_vertical_info = FT_BOOL( face->vertical_info && face->vertical.number_Of_VMetrics > 0 ); /* get the vertical metrics from the vmtx table if we have one */ if ( has_vertical_info ) { (void)( (SFNT_Service)face->sfnt )->get_metrics( face, 1, glyph_index, &dummy, &advance ); glyph->root.linearVertAdvance = advance; } else { /* make up vertical ones */ if ( face->os2.version != 0xFFFFU ) glyph->root.linearVertAdvance = (FT_Pos) ( face->os2.sTypoAscender - face->os2.sTypoDescender ); else glyph->root.linearVertAdvance = (FT_Pos) ( face->horizontal.Ascender - face->horizontal.Descender ); } return error; } } } #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */ /* return immediately if we only want the embedded bitmaps */ if ( load_flags & FT_LOAD_SBITS_ONLY ) return FT_THROW( Invalid_Argument ); /* if we have a CID subfont, use its matrix (which has already */ /* been multiplied with the root matrix) */ /* this scaling is only relevant if the PS hinter isn't active */ if ( cff->num_subfonts ) { FT_Long top_upm, sub_upm; FT_Byte fd_index = cff_fd_select_get( &cff->fd_select, glyph_index ); if ( fd_index >= cff->num_subfonts ) fd_index = (FT_Byte)( cff->num_subfonts - 1 ); top_upm = (FT_Long)cff->top_font.font_dict.units_per_em; sub_upm = (FT_Long)cff->subfonts[fd_index]->font_dict.units_per_em; font_matrix = cff->subfonts[fd_index]->font_dict.font_matrix; font_offset = cff->subfonts[fd_index]->font_dict.font_offset; if ( top_upm != sub_upm ) { glyph->x_scale = FT_MulDiv( glyph->x_scale, top_upm, sub_upm ); glyph->y_scale = FT_MulDiv( glyph->y_scale, top_upm, sub_upm ); force_scaling = TRUE; } } else { font_matrix = cff->top_font.font_dict.font_matrix; font_offset = cff->top_font.font_dict.font_offset; } glyph->root.outline.n_points = 0; glyph->root.outline.n_contours = 0; /* top-level code ensures that FT_LOAD_NO_HINTING is set */ /* if FT_LOAD_NO_SCALE is active */ hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); glyph->hint = hinting; glyph->scaled = scaled; glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; /* by default */ { #ifdef CFF_CONFIG_OPTION_OLD_ENGINE PS_Driver driver = (PS_Driver)FT_FACE_DRIVER( face ); #endif FT_Byte* charstring; FT_ULong charstring_len; decoder_funcs->init( &decoder, face, size, glyph, hinting, FT_LOAD_TARGET_MODE( load_flags ), cff_get_glyph_data, cff_free_glyph_data ); /* this is for pure CFFs */ if ( load_flags & FT_LOAD_ADVANCE_ONLY ) decoder.width_only = TRUE; decoder.builder.no_recurse = (FT_Bool)( load_flags & FT_LOAD_NO_RECURSE ); /* now load the unscaled outline */ error = cff_get_glyph_data( face, glyph_index, &charstring, &charstring_len ); if ( error ) goto Glyph_Build_Finished; error = decoder_funcs->prepare( &decoder, size, glyph_index ); if ( error ) goto Glyph_Build_Finished; #ifdef CFF_CONFIG_OPTION_OLD_ENGINE /* choose which CFF renderer to use */ if ( driver->hinting_engine == FT_HINTING_FREETYPE ) error = decoder_funcs->parse_charstrings_old( &decoder, charstring, charstring_len, 0 ); else #endif { psaux->ps_decoder_init( &psdecoder, &decoder, FALSE ); error = decoder_funcs->parse_charstrings( &psdecoder, charstring, charstring_len ); /* Adobe's engine uses 16.16 numbers everywhere; */ /* as a consequence, glyphs larger than 2000ppem get rejected */ if ( FT_ERR_EQ( error, Glyph_Too_Big ) ) { /* this time, we retry unhinted and scale up the glyph later on */ /* (the engine uses and sets the hardcoded value 0x10000 / 64 = */ /* 0x400 for both `x_scale' and `y_scale' in this case) */ hinting = FALSE; force_scaling = TRUE; glyph->hint = hinting; error = decoder_funcs->parse_charstrings( &psdecoder, charstring, charstring_len ); } } cff_free_glyph_data( face, &charstring, charstring_len ); if ( error ) goto Glyph_Build_Finished; #ifdef FT_CONFIG_OPTION_INCREMENTAL /* Control data and length may not be available for incremental */ /* fonts. */ if ( face->root.internal->incremental_interface ) { glyph->root.control_data = NULL; glyph->root.control_len = 0; } else #endif /* FT_CONFIG_OPTION_INCREMENTAL */ /* We set control_data and control_len if charstrings is loaded. */ /* See how charstring loads at cff_index_access_element() in */ /* cffload.c. */ { CFF_Index csindex = &cff->charstrings_index; if ( csindex->offsets ) { glyph->root.control_data = csindex->bytes + csindex->offsets[glyph_index] - 1; glyph->root.control_len = (FT_Long)charstring_len; } } Glyph_Build_Finished: /* save new glyph tables, if no error */ if ( !error ) decoder.builder.funcs.done( &decoder.builder ); /* XXX: anything to do for broken glyph entry? */ } #ifdef FT_CONFIG_OPTION_INCREMENTAL /* Incremental fonts can optionally override the metrics. */ if ( !error && face->root.internal->incremental_interface && face->root.internal->incremental_interface->funcs->get_glyph_metrics ) { FT_Incremental_MetricsRec metrics; metrics.bearing_x = decoder.builder.left_bearing.x; metrics.bearing_y = 0; metrics.advance = decoder.builder.advance.x; metrics.advance_v = decoder.builder.advance.y; error = face->root.internal->incremental_interface->funcs->get_glyph_metrics( face->root.internal->incremental_interface->object, glyph_index, FALSE, &metrics ); decoder.builder.left_bearing.x = metrics.bearing_x; decoder.builder.advance.x = metrics.advance; decoder.builder.advance.y = metrics.advance_v; } #endif /* FT_CONFIG_OPTION_INCREMENTAL */ if ( !error ) { /* Now, set the metrics -- this is rather simple, as */ /* the left side bearing is the xMin, and the top side */ /* bearing the yMax. */ /* For composite glyphs, return only left side bearing and */ /* advance width. */ if ( load_flags & FT_LOAD_NO_RECURSE ) { FT_Slot_Internal internal = glyph->root.internal; glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x; glyph->root.metrics.horiAdvance = decoder.glyph_width; internal->glyph_matrix = font_matrix; internal->glyph_delta = font_offset; internal->glyph_transformed = 1; } else { FT_BBox cbox; FT_Glyph_Metrics* metrics = &glyph->root.metrics; FT_Bool has_vertical_info; if ( face->horizontal.number_Of_HMetrics ) { FT_Short horiBearingX = 0; FT_UShort horiAdvance = 0; ( (SFNT_Service)face->sfnt )->get_metrics( face, 0, glyph_index, &horiBearingX, &horiAdvance ); metrics->horiAdvance = horiAdvance; metrics->horiBearingX = horiBearingX; glyph->root.linearHoriAdvance = horiAdvance; } else { /* copy the _unscaled_ advance width */ metrics->horiAdvance = decoder.glyph_width; glyph->root.linearHoriAdvance = decoder.glyph_width; } glyph->root.internal->glyph_transformed = 0; has_vertical_info = FT_BOOL( face->vertical_info && face->vertical.number_Of_VMetrics > 0 ); /* get the vertical metrics from the vmtx table if we have one */ if ( has_vertical_info ) { FT_Short vertBearingY = 0; FT_UShort vertAdvance = 0; ( (SFNT_Service)face->sfnt )->get_metrics( face, 1, glyph_index, &vertBearingY, &vertAdvance ); metrics->vertBearingY = vertBearingY; metrics->vertAdvance = vertAdvance; } else { /* make up vertical ones */ if ( face->os2.version != 0xFFFFU ) metrics->vertAdvance = (FT_Pos)( face->os2.sTypoAscender - face->os2.sTypoDescender ); else metrics->vertAdvance = (FT_Pos)( face->horizontal.Ascender - face->horizontal.Descender ); } glyph->root.linearVertAdvance = metrics->vertAdvance; glyph->root.format = FT_GLYPH_FORMAT_OUTLINE; glyph->root.outline.flags = 0; if ( size && size->root.metrics.y_ppem < 24 ) glyph->root.outline.flags |= FT_OUTLINE_HIGH_PRECISION; glyph->root.outline.flags |= FT_OUTLINE_REVERSE_FILL; /* apply the font matrix, if any */ if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L || font_matrix.xy != 0 || font_matrix.yx != 0 ) { FT_Outline_Transform( &glyph->root.outline, &font_matrix ); metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, font_matrix.xx ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, font_matrix.yy ); } if ( font_offset.x || font_offset.y ) { FT_Outline_Translate( &glyph->root.outline, font_offset.x, font_offset.y ); metrics->horiAdvance += font_offset.x; metrics->vertAdvance += font_offset.y; } if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling ) { /* scale the outline and the metrics */ FT_Int n; FT_Outline* cur = &glyph->root.outline; FT_Vector* vec = cur->points; FT_Fixed x_scale = glyph->x_scale; FT_Fixed y_scale = glyph->y_scale; /* First of all, scale the points */ if ( !hinting || !decoder.builder.hints_funcs ) for ( n = cur->n_points; n > 0; n--, vec++ ) { vec->x = FT_MulFix( vec->x, x_scale ); vec->y = FT_MulFix( vec->y, y_scale ); } /* Then scale the metrics */ metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); } /* compute the other metrics */ FT_Outline_Get_CBox( &glyph->root.outline, &cbox ); metrics->width = cbox.xMax - cbox.xMin; metrics->height = cbox.yMax - cbox.yMin; metrics->horiBearingX = cbox.xMin; metrics->horiBearingY = cbox.yMax; if ( has_vertical_info ) metrics->vertBearingX = metrics->horiBearingX - metrics->horiAdvance / 2; else { if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) ft_synthesize_vertical_metrics( metrics, metrics->vertAdvance ); } } } return error; }
pfr_slot_load( PFR_Slot slot, PFR_Size size, FT_UInt gindex, FT_Int32 load_flags ) { FT_Error error; PFR_Face face = (PFR_Face)slot->root.face; PFR_Char gchar; FT_Outline* outline = &slot->root.outline; FT_ULong gps_offset; if (gindex > 0) gindex--; /* check that the glyph index is correct */ FT_ASSERT( gindex < face->phy_font.num_chars ); /* try to load an embedded bitmap */ if ( ( load_flags & ( FT_LOAD_NO_SCALE | FT_LOAD_NO_BITMAP ) ) == 0 ) { error = pfr_slot_load_bitmap( slot, size, gindex ); if ( error == 0 ) goto Exit; } gchar = face->phy_font.chars + gindex; slot->root.format = FT_GLYPH_FORMAT_OUTLINE; outline->n_points = 0; outline->n_contours = 0; gps_offset = face->header.gps_section_offset; /* load the glyph outline (FT_LOAD_NO_RECURSE isn't supported) */ error = pfr_glyph_load( &slot->glyph, face->root.stream, gps_offset, gchar->gps_offset, gchar->gps_size ); if ( !error ) { FT_BBox cbox; FT_Glyph_Metrics* metrics = &slot->root.metrics; FT_Pos advance; FT_Int em_metrics, em_outline; FT_Bool scaling; scaling = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); /* copy outline data */ *outline = slot->glyph.loader->base.outline; outline->flags &= ~FT_OUTLINE_OWNER; outline->flags |= FT_OUTLINE_REVERSE_FILL; if ( size && size->root.metrics.y_ppem < 24 ) outline->flags |= FT_OUTLINE_HIGH_PRECISION; /* compute the advance vector */ metrics->horiAdvance = 0; metrics->vertAdvance = 0; advance = gchar->advance; em_metrics = face->phy_font.metrics_resolution; em_outline = face->phy_font.outline_resolution; if ( em_metrics != em_outline ) advance = FT_MulDiv( advance, em_outline, em_metrics ); if ( face->phy_font.flags & PFR_PHY_VERTICAL ) metrics->vertAdvance = advance; else metrics->horiAdvance = advance; slot->root.linearHoriAdvance = metrics->horiAdvance; slot->root.linearVertAdvance = metrics->vertAdvance; /* make-up vertical metrics(?) */ metrics->vertBearingX = 0; metrics->vertBearingY = 0; /* scale when needed */ if ( scaling ) { FT_Int n; FT_Fixed x_scale = size->root.metrics.x_scale; FT_Fixed y_scale = size->root.metrics.y_scale; FT_Vector* vec = outline->points; /* scale outline points */ for ( n = 0; n < outline->n_points; n++, vec++ ) { vec->x = FT_MulFix( vec->x, x_scale ); vec->y = FT_MulFix( vec->y, y_scale ); } /* scale the advance */ metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); } /* compute the rest of the metrics */ FT_Outline_Get_CBox( outline, &cbox ); metrics->width = cbox.xMax - cbox.xMin; metrics->height = cbox.yMax - cbox.yMin; metrics->horiBearingX = cbox.xMin; metrics->horiBearingY = cbox.yMax - metrics->height; } Exit: return error; }
void SkScalerContext_FreeType_Base::generateGlyphImage( FT_Face face, const SkGlyph& glyph, const SkMatrix& bitmapTransform) { const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag); const bool doVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag); switch ( face->glyph->format ) { case FT_GLYPH_FORMAT_OUTLINE: { FT_Outline* outline = &face->glyph->outline; int dx = 0, dy = 0; if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { dx = SkFixedToFDot6(glyph.getSubXFixed()); dy = SkFixedToFDot6(glyph.getSubYFixed()); // negate dy since freetype-y-goes-up and skia-y-goes-down dy = -dy; } memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); if (SkMask::kLCD16_Format == glyph.fMaskFormat) { FT_Outline_Translate(outline, dx, dy); FT_Error err = FT_Render_Glyph(face->glyph, doVert ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD); if (err) { SK_TRACEFTR(err, "Could not render glyph."); return; } SkMask mask; glyph.toMask(&mask); #ifdef SK_SHOW_TEXT_BLIT_COVERAGE memset(mask.fImage, 0x80, mask.fBounds.height() * mask.fRowBytes); #endif FT_GlyphSlotRec& ftGlyph = *face->glyph; if (!SkIRect::Intersects(mask.fBounds, SkIRect::MakeXYWH( ftGlyph.bitmap_left, -ftGlyph.bitmap_top, ftGlyph.bitmap.width, ftGlyph.bitmap.rows))) { return; } // If the FT_Bitmap extent is larger, discard bits of the bitmap outside the mask. // If the SkMask extent is larger, shrink mask to fit bitmap (clearing discarded). unsigned char* origBuffer = ftGlyph.bitmap.buffer; // First align the top left (origin). if (-ftGlyph.bitmap_top < mask.fBounds.fTop) { int32_t topDiff = mask.fBounds.fTop - (-ftGlyph.bitmap_top); ftGlyph.bitmap.buffer += ftGlyph.bitmap.pitch * topDiff; ftGlyph.bitmap.rows -= topDiff; ftGlyph.bitmap_top = -mask.fBounds.fTop; } if (ftGlyph.bitmap_left < mask.fBounds.fLeft) { int32_t leftDiff = mask.fBounds.fLeft - ftGlyph.bitmap_left; ftGlyph.bitmap.buffer += leftDiff; ftGlyph.bitmap.width -= leftDiff; ftGlyph.bitmap_left = mask.fBounds.fLeft; } if (mask.fBounds.fTop < -ftGlyph.bitmap_top) { mask.fImage += mask.fRowBytes * (-ftGlyph.bitmap_top - mask.fBounds.fTop); mask.fBounds.fTop = -ftGlyph.bitmap_top; } if (mask.fBounds.fLeft < ftGlyph.bitmap_left) { mask.fImage += sizeof(uint16_t) * (ftGlyph.bitmap_left - mask.fBounds.fLeft); mask.fBounds.fLeft = ftGlyph.bitmap_left; } // Origins aligned, clean up the width and height. int ftVertScale = (doVert ? 3 : 1); int ftHoriScale = (doVert ? 1 : 3); if (mask.fBounds.height() * ftVertScale < SkToInt(ftGlyph.bitmap.rows)) { ftGlyph.bitmap.rows = mask.fBounds.height() * ftVertScale; } if (mask.fBounds.width() * ftHoriScale < SkToInt(ftGlyph.bitmap.width)) { ftGlyph.bitmap.width = mask.fBounds.width() * ftHoriScale; } if (SkToInt(ftGlyph.bitmap.rows) < mask.fBounds.height() * ftVertScale) { mask.fBounds.fBottom = mask.fBounds.fTop + ftGlyph.bitmap.rows / ftVertScale; } if (SkToInt(ftGlyph.bitmap.width) < mask.fBounds.width() * ftHoriScale) { mask.fBounds.fRight = mask.fBounds.fLeft + ftGlyph.bitmap.width / ftHoriScale; } if (fPreBlend.isApplicable()) { copyFT2LCD16<true>(ftGlyph.bitmap, mask, doBGR, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); } else { copyFT2LCD16<false>(ftGlyph.bitmap, mask, doBGR, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); } // Restore the buffer pointer so FreeType can properly free it. ftGlyph.bitmap.buffer = origBuffer; } else { FT_BBox bbox; FT_Bitmap target; FT_Outline_Get_CBox(outline, &bbox); /* what we really want to do for subpixel is offset(dx, dy) compute_bounds offset(bbox & !63) but that is two calls to offset, so we do the following, which achieves the same thing with only one offset call. */ FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63), dy - ((bbox.yMin + dy) & ~63)); target.width = glyph.fWidth; target.rows = glyph.fHeight; target.pitch = glyph.rowBytes(); target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage); target.pixel_mode = compute_pixel_mode( (SkMask::Format)fRec.fMaskFormat); target.num_grays = 256; FT_Outline_Get_Bitmap(face->glyph->library, outline, &target); #ifdef SK_SHOW_TEXT_BLIT_COVERAGE for (int y = 0; y < glyph.fHeight; ++y) { for (int x = 0; x < glyph.fWidth; ++x) { uint8_t& a = ((uint8_t*)glyph.fImage)[(glyph.rowBytes() * y) + x]; a = SkTMax<uint8_t>(a, 0x20); } } #endif } } break; case FT_GLYPH_FORMAT_BITMAP: { FT_Pixel_Mode pixel_mode = static_cast<FT_Pixel_Mode>(face->glyph->bitmap.pixel_mode); SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat); // Assume that the other formats do not exist. SkASSERT(FT_PIXEL_MODE_MONO == pixel_mode || FT_PIXEL_MODE_GRAY == pixel_mode || FT_PIXEL_MODE_BGRA == pixel_mode); // These are the only formats this ScalerContext should request. SkASSERT(SkMask::kBW_Format == maskFormat || SkMask::kA8_Format == maskFormat || SkMask::kARGB32_Format == maskFormat || SkMask::kLCD16_Format == maskFormat); // If no scaling needed, directly copy glyph bitmap. if (bitmapTransform.isIdentity()) { SkMask dstMask; glyph.toMask(&dstMask); copyFTBitmap(face->glyph->bitmap, dstMask); break; } // Otherwise, scale the bitmap. // Copy the FT_Bitmap into an SkBitmap (either A8 or ARGB) SkBitmap unscaledBitmap; // TODO: mark this as sRGB when the blits will be sRGB. unscaledBitmap.allocPixels(SkImageInfo::Make(face->glyph->bitmap.width, face->glyph->bitmap.rows, SkColorType_for_FTPixelMode(pixel_mode), kPremul_SkAlphaType)); SkMask unscaledBitmapAlias; unscaledBitmapAlias.fImage = reinterpret_cast<uint8_t*>(unscaledBitmap.getPixels()); unscaledBitmapAlias.fBounds.set(0, 0, unscaledBitmap.width(), unscaledBitmap.height()); unscaledBitmapAlias.fRowBytes = unscaledBitmap.rowBytes(); unscaledBitmapAlias.fFormat = SkMaskFormat_for_SkColorType(unscaledBitmap.colorType()); copyFTBitmap(face->glyph->bitmap, unscaledBitmapAlias); // Wrap the glyph's mask in a bitmap, unless the glyph's mask is BW or LCD. // BW requires an A8 target for resizing, which can then be down sampled. // LCD should use a 4x A8 target, which will then be down sampled. // For simplicity, LCD uses A8 and is replicated. int bitmapRowBytes = 0; if (SkMask::kBW_Format != maskFormat && SkMask::kLCD16_Format != maskFormat) { bitmapRowBytes = glyph.rowBytes(); } SkBitmap dstBitmap; // TODO: mark this as sRGB when the blits will be sRGB. dstBitmap.setInfo(SkImageInfo::Make(glyph.fWidth, glyph.fHeight, SkColorType_for_SkMaskFormat(maskFormat), kPremul_SkAlphaType), bitmapRowBytes); if (SkMask::kBW_Format == maskFormat || SkMask::kLCD16_Format == maskFormat) { dstBitmap.allocPixels(); } else { dstBitmap.setPixels(glyph.fImage); } // Scale unscaledBitmap into dstBitmap. SkCanvas canvas(dstBitmap); #ifdef SK_SHOW_TEXT_BLIT_COVERAGE canvas.clear(0x33FF0000); #else canvas.clear(SK_ColorTRANSPARENT); #endif canvas.translate(-glyph.fLeft, -glyph.fTop); canvas.concat(bitmapTransform); canvas.translate(face->glyph->bitmap_left, -face->glyph->bitmap_top); SkPaint paint; paint.setFilterQuality(kMedium_SkFilterQuality); canvas.drawBitmap(unscaledBitmap, 0, 0, &paint); // If the destination is BW or LCD, convert from A8. if (SkMask::kBW_Format == maskFormat) { // Copy the A8 dstBitmap into the A1 glyph.fImage. SkMask dstMask; glyph.toMask(&dstMask); packA8ToA1(dstMask, dstBitmap.getAddr8(0, 0), dstBitmap.rowBytes()); } else if (SkMask::kLCD16_Format == maskFormat) { // Copy the A8 dstBitmap into the LCD16 glyph.fImage. uint8_t* src = dstBitmap.getAddr8(0, 0); uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage); for (int y = dstBitmap.height(); y --> 0;) { for (int x = 0; x < dstBitmap.width(); ++x) { dst[x] = grayToRGB16(src[x]); } dst = (uint16_t*)((char*)dst + glyph.rowBytes()); src += dstBitmap.rowBytes(); } } } break; default: SkDEBUGFAIL("unknown glyph format"); memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); return; } // We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum, // it is optional #if defined(SK_GAMMA_APPLY_TO_A8) if (SkMask::kA8_Format == glyph.fMaskFormat && fPreBlend.isApplicable()) { uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage; unsigned rowBytes = glyph.rowBytes(); for (int y = glyph.fHeight - 1; y >= 0; --y) { for (int x = glyph.fWidth - 1; x >= 0; --x) { dst[x] = fPreBlend.fG[dst[x]]; } dst += rowBytes; } } #endif }
/* convert a slot's glyph image into a bitmap */ static FT_Error ft_smooth_render( FT_Renderer render, FT_GlyphSlot slot, FT_UInt mode, FT_Vector* origin ) { FT_Error error; FT_Outline* outline = NULL; FT_BBox cbox; FT_UInt width, height, pitch; FT_Bitmap* bitmap; FT_Memory memory; FT_Raster_Params params; /* check glyph image format */ if ( slot->format != render->glyph_format ) { error = Smooth_Err_Invalid_Argument; goto Exit; } /* check mode */ if ( mode != ft_render_mode_normal ) return Smooth_Err_Cannot_Render_Glyph; outline = &slot->outline; /* translate the outline to the new origin if needed */ if ( origin ) FT_Outline_Translate( outline, origin->x, origin->y ); /* compute the control box, and grid fit it */ FT_Outline_Get_CBox( outline, &cbox ); cbox.xMin &= -64; cbox.yMin &= -64; cbox.xMax = ( cbox.xMax + 63 ) & -64; cbox.yMax = ( cbox.yMax + 63 ) & -64; width = ( cbox.xMax - cbox.xMin ) >> 6; height = ( cbox.yMax - cbox.yMin ) >> 6; bitmap = &slot->bitmap; memory = render->root.memory; /* release old bitmap buffer */ if ( slot->flags & FT_GLYPH_OWN_BITMAP ) { FT_FREE( bitmap->buffer ); slot->flags &= ~FT_GLYPH_OWN_BITMAP; } /* allocate new one, depends on pixel format */ pitch = width; bitmap->pixel_mode = ft_pixel_mode_grays; bitmap->num_grays = 256; bitmap->width = width; bitmap->rows = height; bitmap->pitch = pitch; if ( FT_ALLOC( bitmap->buffer, (FT_ULong)pitch * height ) ) goto Exit; slot->flags |= FT_GLYPH_OWN_BITMAP; /* translate outline to render it into the bitmap */ FT_Outline_Translate( outline, -cbox.xMin, -cbox.yMin ); /* set up parameters */ params.target = bitmap; params.source = outline; params.flags = ft_raster_flag_aa; /* render outline into the bitmap */ error = render->raster_render( render->raster, ¶ms ); FT_Outline_Translate( outline, cbox.xMin, cbox.yMin ); if ( error ) goto Exit; slot->format = ft_glyph_format_bitmap; slot->bitmap_left = cbox.xMin >> 6; slot->bitmap_top = cbox.yMax >> 6; Exit: if ( outline && origin ) FT_Outline_Translate( outline, -origin->x, -origin->y ); return error; }
cid_slot_load_glyph( FT_GlyphSlot cidglyph, /* CID_GlyphSlot */ FT_Size cidsize, /* CID_Size */ FT_UInt glyph_index, FT_Int32 load_flags ) { CID_GlyphSlot glyph = (CID_GlyphSlot)cidglyph; FT_Error error; T1_DecoderRec decoder; CID_Face face = (CID_Face)cidglyph->face; FT_Bool hinting; PSAux_Service psaux = (PSAux_Service)face->psaux; FT_Matrix font_matrix; FT_Vector font_offset; if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) { error = CID_Err_Invalid_Argument; goto Exit; } if ( load_flags & FT_LOAD_NO_RECURSE ) load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; glyph->x_scale = cidsize->metrics.x_scale; glyph->y_scale = cidsize->metrics.y_scale; cidglyph->outline.n_points = 0; cidglyph->outline.n_contours = 0; hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 && ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); cidglyph->format = FT_GLYPH_FORMAT_OUTLINE; error = psaux->t1_decoder_funcs->init( &decoder, cidglyph->face, cidsize, cidglyph, 0, /* glyph names -- XXX */ 0, /* blend == 0 */ hinting, FT_LOAD_TARGET_MODE( load_flags ), cid_load_glyph ); if ( error ) goto Exit; /* TODO: initialize decoder.len_buildchar and decoder.buildchar */ /* if we ever support CID-keyed multiple master fonts */ /* set up the decoder */ decoder.builder.no_recurse = FT_BOOL( ( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ) ); error = cid_load_glyph( &decoder, glyph_index ); if ( error ) goto Exit; font_matrix = decoder.font_matrix; font_offset = decoder.font_offset; /* save new glyph tables */ psaux->t1_decoder_funcs->done( &decoder ); /* now set the metrics -- this is rather simple, as */ /* the left side bearing is the xMin, and the top side */ /* bearing the yMax */ cidglyph->outline.flags &= FT_OUTLINE_OWNER; cidglyph->outline.flags |= FT_OUTLINE_REVERSE_FILL; /* for composite glyphs, return only left side bearing and */ /* advance width */ if ( load_flags & FT_LOAD_NO_RECURSE ) { FT_Slot_Internal internal = cidglyph->internal; cidglyph->metrics.horiBearingX = FIXED_TO_INT( decoder.builder.left_bearing.x ); cidglyph->metrics.horiAdvance = FIXED_TO_INT( decoder.builder.advance.x ); internal->glyph_matrix = font_matrix; internal->glyph_delta = font_offset; internal->glyph_transformed = 1; } else { FT_BBox cbox; FT_Glyph_Metrics* metrics = &cidglyph->metrics; FT_Vector advance; /* copy the _unscaled_ advance width */ metrics->horiAdvance = FIXED_TO_INT( decoder.builder.advance.x ); cidglyph->linearHoriAdvance = FIXED_TO_INT( decoder.builder.advance.x ); cidglyph->internal->glyph_transformed = 0; /* make up vertical ones */ metrics->vertAdvance = ( face->cid.font_bbox.yMax - face->cid.font_bbox.yMin ) >> 16; cidglyph->linearVertAdvance = metrics->vertAdvance; cidglyph->format = FT_GLYPH_FORMAT_OUTLINE; if ( cidsize->metrics.y_ppem < 24 ) cidglyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; /* apply the font matrix */ FT_Outline_Transform( &cidglyph->outline, &font_matrix ); FT_Outline_Translate( &cidglyph->outline, font_offset.x, font_offset.y ); advance.x = metrics->horiAdvance; advance.y = 0; FT_Vector_Transform( &advance, &font_matrix ); metrics->horiAdvance = advance.x + font_offset.x; advance.x = 0; advance.y = metrics->vertAdvance; FT_Vector_Transform( &advance, &font_matrix ); metrics->vertAdvance = advance.y + font_offset.y; if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ) { /* scale the outline and the metrics */ FT_Int n; FT_Outline* cur = decoder.builder.base; FT_Vector* vec = cur->points; FT_Fixed x_scale = glyph->x_scale; FT_Fixed y_scale = glyph->y_scale; /* First of all, scale the points */ if ( !hinting || !decoder.builder.hints_funcs ) for ( n = cur->n_points; n > 0; n--, vec++ ) { vec->x = FT_MulFix( vec->x, x_scale ); vec->y = FT_MulFix( vec->y, y_scale ); } /* Then scale the metrics */ metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); } /* compute the other metrics */ FT_Outline_Get_CBox( &cidglyph->outline, &cbox ); metrics->width = cbox.xMax - cbox.xMin; metrics->height = cbox.yMax - cbox.yMin; metrics->horiBearingX = cbox.xMin; metrics->horiBearingY = cbox.yMax; if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) { /* make up vertical ones */ ft_synthesize_vertical_metrics( metrics, metrics->vertAdvance ); } } Exit: return error; }
static FT_Error af_loader_load_g( AF_Loader loader, AF_Scaler scaler, FT_UInt glyph_index, FT_Int32 load_flags, FT_UInt depth ) { FT_Error error; FT_Face face = loader->face; FT_GlyphLoader gloader = loader->gloader; AF_ScriptMetrics metrics = loader->metrics; AF_GlyphHints hints = &loader->hints; FT_GlyphSlot slot = face->glyph; FT_Slot_Internal internal = slot->internal; FT_Int32 flags; flags = load_flags | FT_LOAD_LINEAR_DESIGN; error = FT_Load_Glyph( face, glyph_index, flags ); if ( error ) goto Exit; loader->transformed = internal->glyph_transformed; if ( loader->transformed ) { FT_Matrix inverse; loader->trans_matrix = internal->glyph_matrix; loader->trans_delta = internal->glyph_delta; inverse = loader->trans_matrix; FT_Matrix_Invert( &inverse ); FT_Vector_Transform( &loader->trans_delta, &inverse ); } switch ( slot->format ) { case FT_GLYPH_FORMAT_OUTLINE: /* translate the loaded glyph when an internal transform is needed */ if ( loader->transformed ) FT_Outline_Translate( &slot->outline, loader->trans_delta.x, loader->trans_delta.y ); /* copy the outline points in the loader's current */ /* extra points which are used to keep original glyph coordinates */ error = FT_GLYPHLOADER_CHECK_POINTS( gloader, slot->outline.n_points + 4, slot->outline.n_contours ); if ( error ) goto Exit; FT_ARRAY_COPY( gloader->current.outline.points, slot->outline.points, slot->outline.n_points ); FT_ARRAY_COPY( gloader->current.outline.contours, slot->outline.contours, slot->outline.n_contours ); FT_ARRAY_COPY( gloader->current.outline.tags, slot->outline.tags, slot->outline.n_points ); gloader->current.outline.n_points = slot->outline.n_points; gloader->current.outline.n_contours = slot->outline.n_contours; /* compute original horizontal phantom points (and ignore */ /* vertical ones) */ loader->pp1.x = hints->x_delta; loader->pp1.y = hints->y_delta; loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance, hints->x_scale ) + hints->x_delta; loader->pp2.y = hints->y_delta; /* be sure to check for spacing glyphs */ if ( slot->outline.n_points == 0 ) goto Hint_Metrics; /* now load the slot image into the auto-outline and run the */ /* automatic hinting process */ if ( metrics->clazz->script_hints_apply ) metrics->clazz->script_hints_apply( hints, &gloader->current.outline, metrics ); /* we now need to adjust the metrics according to the change in */ /* width/positioning that occurred during the hinting process */ if ( scaler->render_mode != FT_RENDER_MODE_LIGHT ) { FT_Pos old_rsb, old_lsb, new_lsb; FT_Pos pp1x_uh, pp2x_uh; AF_AxisHints axis = &hints->axis[AF_DIMENSION_HORZ]; AF_Edge edge1 = axis->edges; /* leftmost edge */ AF_Edge edge2 = edge1 + axis->num_edges - 1; /* rightmost edge */ if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) ) { old_rsb = loader->pp2.x - edge2->opos; old_lsb = edge1->opos; new_lsb = edge1->pos; /* remember unhinted values to later account */ /* for rounding errors */ pp1x_uh = new_lsb - old_lsb; pp2x_uh = edge2->pos + old_rsb; /* prefer too much space over too little space */ /* for very small sizes */ if ( old_lsb < 24 ) pp1x_uh -= 8; if ( old_rsb < 24 ) pp2x_uh += 8; loader->pp1.x = FT_PIX_ROUND( pp1x_uh ); loader->pp2.x = FT_PIX_ROUND( pp2x_uh ); if ( loader->pp1.x >= new_lsb && old_lsb > 0 ) loader->pp1.x -= 64; if ( loader->pp2.x <= edge2->pos && old_rsb > 0 ) loader->pp2.x += 64; slot->lsb_delta = loader->pp1.x - pp1x_uh; slot->rsb_delta = loader->pp2.x - pp2x_uh; } else { FT_Pos pp1x = loader->pp1.x; FT_Pos pp2x = loader->pp2.x; loader->pp1.x = FT_PIX_ROUND( pp1x ); loader->pp2.x = FT_PIX_ROUND( pp2x ); slot->lsb_delta = loader->pp1.x - pp1x; slot->rsb_delta = loader->pp2.x - pp2x; } } else { FT_Pos pp1x = loader->pp1.x; FT_Pos pp2x = loader->pp2.x; loader->pp1.x = FT_PIX_ROUND( pp1x + hints->xmin_delta ); loader->pp2.x = FT_PIX_ROUND( pp2x + hints->xmax_delta ); slot->lsb_delta = loader->pp1.x - pp1x; slot->rsb_delta = loader->pp2.x - pp2x; } /* good, we simply add the glyph to our loader's base */ FT_GlyphLoader_Add( gloader ); break; case FT_GLYPH_FORMAT_COMPOSITE: { FT_UInt nn, num_subglyphs = slot->num_subglyphs; FT_UInt num_base_subgs, start_point; FT_SubGlyph subglyph; start_point = gloader->base.outline.n_points; /* first of all, copy the subglyph descriptors in the glyph loader */ error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs ); if ( error ) goto Exit; FT_ARRAY_COPY( gloader->current.subglyphs, slot->subglyphs, num_subglyphs ); gloader->current.num_subglyphs = num_subglyphs; num_base_subgs = gloader->base.num_subglyphs; /* now read each subglyph independently */ for ( nn = 0; nn < num_subglyphs; nn++ ) { FT_Vector pp1, pp2; FT_Pos x, y; FT_UInt num_points, num_new_points, num_base_points; /* gloader.current.subglyphs can change during glyph loading due */ /* to re-allocation -- we must recompute the current subglyph on */ /* each iteration */ subglyph = gloader->base.subglyphs + num_base_subgs + nn; pp1 = loader->pp1; pp2 = loader->pp2; num_base_points = gloader->base.outline.n_points; error = af_loader_load_g( loader, scaler, subglyph->index, load_flags, depth + 1 ); if ( error ) goto Exit; /* recompute subglyph pointer */ subglyph = gloader->base.subglyphs + num_base_subgs + nn; if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS ) { pp1 = loader->pp1; pp2 = loader->pp2; } else { loader->pp1 = pp1; loader->pp2 = pp2; } num_points = gloader->base.outline.n_points; num_new_points = num_points - num_base_points; /* now perform the transformation required for this subglyph */ if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE | FT_SUBGLYPH_FLAG_XY_SCALE | FT_SUBGLYPH_FLAG_2X2 ) ) { FT_Vector* cur = gloader->base.outline.points + num_base_points; FT_Vector* limit = cur + num_new_points; for ( ; cur < limit; cur++ ) FT_Vector_Transform( cur, &subglyph->transform ); } /* apply offset */ if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) ) { FT_Int k = subglyph->arg1; FT_UInt l = subglyph->arg2; FT_Vector* p1; FT_Vector* p2; if ( start_point + k >= num_base_points || l >= (FT_UInt)num_new_points ) { error = AF_Err_Invalid_Composite; goto Exit; } l += num_base_points; /* for now, only use the current point coordinates; */ /* we eventually may consider another approach */ p1 = gloader->base.outline.points + start_point + k; p2 = gloader->base.outline.points + start_point + l; x = p1->x - p2->x; y = p1->y - p2->y; } else { x = FT_MulFix( subglyph->arg1, hints->x_scale ) + hints->x_delta; y = FT_MulFix( subglyph->arg2, hints->y_scale ) + hints->y_delta; x = FT_PIX_ROUND( x ); y = FT_PIX_ROUND( y ); } { FT_Outline dummy = gloader->base.outline; dummy.points += num_base_points; dummy.n_points = (short)num_new_points; FT_Outline_Translate( &dummy, x, y ); } } } break; default: /* we don't support other formats (yet?) */ error = AF_Err_Unimplemented_Feature; } Hint_Metrics: if ( depth == 0 ) { FT_BBox bbox; FT_Vector vvector; vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX; vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY; vvector.x = FT_MulFix( vvector.x, metrics->scaler.x_scale ); vvector.y = FT_MulFix( vvector.y, metrics->scaler.y_scale ); /* transform the hinted outline if needed */ if ( loader->transformed ) { FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix ); FT_Vector_Transform( &vvector, &loader->trans_matrix ); } #if 1 /* we must translate our final outline by -pp1.x and compute */ /* the new metrics */ if ( loader->pp1.x ) FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 ); #endif FT_Outline_Get_CBox( &gloader->base.outline, &bbox ); bbox.xMin = FT_PIX_FLOOR( bbox.xMin ); bbox.yMin = FT_PIX_FLOOR( bbox.yMin ); bbox.xMax = FT_PIX_CEIL( bbox.xMax ); bbox.yMax = FT_PIX_CEIL( bbox.yMax ); slot->metrics.width = bbox.xMax - bbox.xMin; slot->metrics.height = bbox.yMax - bbox.yMin; slot->metrics.horiBearingX = bbox.xMin; slot->metrics.horiBearingY = bbox.yMax; slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x ); slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y ); /* for mono-width fonts (like Andale, Courier, etc.) we need */ /* to keep the original rounded advance width; ditto for */ /* digits if all have the same advance width */ #if 0 if ( !FT_IS_FIXED_WIDTH( slot->face ) ) slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; else slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance, x_scale ); #else if ( scaler->render_mode != FT_RENDER_MODE_LIGHT && ( FT_IS_FIXED_WIDTH( slot->face ) || ( af_face_globals_is_digit( loader->globals, glyph_index ) && metrics->digits_have_same_width ) ) ) { slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance, metrics->scaler.x_scale ); /* Set delta values to 0. Otherwise code that uses them is */ /* going to ruin the fixed advance width. */ slot->lsb_delta = 0; slot->rsb_delta = 0; } else { /* non-spacing glyphs must stay as-is */ if ( slot->metrics.horiAdvance ) slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; } #endif slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance, metrics->scaler.y_scale ); slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance ); slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance ); /* now copy outline into glyph slot */ FT_GlyphLoader_Rewind( internal->loader ); error = FT_GlyphLoader_CopyPoints( internal->loader, gloader ); if ( error ) goto Exit; /* reassign all outline fields except flags to protect them */ slot->outline.n_contours = internal->loader->base.outline.n_contours; slot->outline.n_points = internal->loader->base.outline.n_points; slot->outline.points = internal->loader->base.outline.points; slot->outline.tags = internal->loader->base.outline.tags; slot->outline.contours = internal->loader->base.outline.contours; slot->format = FT_GLYPH_FORMAT_OUTLINE; } Exit: return error; }
/* convert a slot's glyph image into a bitmap */ static FT_Error ft_smooth_render_generic( FT_Renderer render, FT_GlyphSlot slot, FT_Render_Mode mode, const FT_Vector* origin, FT_Render_Mode required_mode ) { FT_Error error; FT_Outline* outline = NULL; FT_BBox cbox; FT_UInt width, height, height_org, width_org, pitch; FT_Bitmap* bitmap; FT_Memory memory; FT_Int hmul = mode == FT_RENDER_MODE_LCD; FT_Int vmul = mode == FT_RENDER_MODE_LCD_V; FT_Pos x_shift, y_shift, x_left, y_top; FT_Raster_Params params; /* check glyph image format */ if ( slot->format != render->glyph_format ) { error = Smooth_Err_Invalid_Argument; goto Exit; } /* check mode */ if ( mode != required_mode ) return Smooth_Err_Cannot_Render_Glyph; outline = &slot->outline; /* translate the outline to the new origin if needed */ if ( origin ) FT_Outline_Translate( outline, origin->x, origin->y ); /* compute the control box, and grid fit it */ FT_Outline_Get_CBox( outline, &cbox ); cbox.xMin = FT_PIX_FLOOR( cbox.xMin ); cbox.yMin = FT_PIX_FLOOR( cbox.yMin ); cbox.xMax = FT_PIX_CEIL( cbox.xMax ); cbox.yMax = FT_PIX_CEIL( cbox.yMax ); width = (FT_UInt)( ( cbox.xMax - cbox.xMin ) >> 6 ); height = (FT_UInt)( ( cbox.yMax - cbox.yMin ) >> 6 ); bitmap = &slot->bitmap; memory = render->root.memory; width_org = width; height_org = height; /* release old bitmap buffer */ if ( slot->internal->flags & FT_GLYPH_OWN_BITMAP ) { FT_FREE( bitmap->buffer ); slot->internal->flags &= ~FT_GLYPH_OWN_BITMAP; } /* allocate new one */ pitch = width; if ( hmul ) { width = width * 3; pitch = FT_PAD_CEIL( width, 4 ); } if ( vmul ) height *= 3; x_shift = (FT_Int) cbox.xMin; y_shift = (FT_Int) cbox.yMin; x_left = (FT_Int)( cbox.xMin >> 6 ); y_top = (FT_Int)( cbox.yMax >> 6 ); #ifdef FT_CONFIG_OPTION_SUBPIXEL_RENDERING if ( slot->library->lcd_filter_func ) { FT_Int extra = slot->library->lcd_extra; if ( hmul ) { x_shift -= 64 * ( extra >> 1 ); width += 3 * extra; pitch = FT_PAD_CEIL( width, 4 ); x_left -= extra >> 1; } if ( vmul ) { y_shift -= 64 * ( extra >> 1 ); height += 3 * extra; y_top += extra >> 1; } }
FT_LOCAL_DEF FT_Error CID_Load_Glyph( CID_GlyphSlot glyph, CID_Size size, FT_Int glyph_index, FT_Int load_flags ) { FT_Error error; T1_Decoder decoder; CID_Face face = (CID_Face)glyph->root.face; FT_Bool hinting; PSAux_Interface* psaux = (PSAux_Interface*)face->psaux; FT_Matrix font_matrix; FT_Vector font_offset; if ( load_flags & FT_LOAD_NO_RECURSE ) load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; glyph->x_scale = size->root.metrics.x_scale; glyph->y_scale = size->root.metrics.y_scale; glyph->root.outline.n_points = 0; glyph->root.outline.n_contours = 0; hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 && ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); glyph->root.format = ft_glyph_format_outline; { error = psaux->t1_decoder_funcs->init( &decoder, (FT_Face)face, (FT_Size)size, (FT_GlyphSlot)glyph, 0, /* glyph names -- XXX */ 0, /* blend == 0 */ hinting, cid_load_glyph ); /* set up the decoder */ decoder.builder.no_recurse = FT_BOOL( ( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ) ); error = cid_load_glyph( &decoder, glyph_index ); font_matrix = decoder.font_matrix; font_offset = decoder.font_offset; /* save new glyph tables */ psaux->t1_decoder_funcs->done( &decoder ); } /* now, set the metrics -- this is rather simple, as */ /* the left side bearing is the xMin, and the top side */ /* bearing the yMax */ if ( !error ) { glyph->root.outline.flags &= ft_outline_owner; glyph->root.outline.flags |= ft_outline_reverse_fill; /* for composite glyphs, return only left side bearing and */ /* advance width */ if ( load_flags & FT_LOAD_NO_RECURSE ) { FT_Slot_Internal internal = glyph->root.internal; glyph->root.metrics.horiBearingX = decoder.builder.left_bearing.x; glyph->root.metrics.horiAdvance = decoder.builder.advance.x; internal->glyph_matrix = font_matrix; internal->glyph_delta = font_offset; internal->glyph_transformed = 1; } else { FT_BBox cbox; FT_Glyph_Metrics* metrics = &glyph->root.metrics; /* copy the _unscaled_ advance width */ metrics->horiAdvance = decoder.builder.advance.x; glyph->root.linearHoriAdvance = decoder.builder.advance.x; glyph->root.internal->glyph_transformed = 0; /* make up vertical metrics */ metrics->vertBearingX = 0; metrics->vertBearingY = 0; metrics->vertAdvance = 0; glyph->root.linearVertAdvance = 0; glyph->root.format = ft_glyph_format_outline; if ( size && size->root.metrics.y_ppem < 24 ) glyph->root.outline.flags |= ft_outline_high_precision; /* apply the font matrix */ FT_Outline_Transform( &glyph->root.outline, &font_matrix ); FT_Outline_Translate( &glyph->root.outline, font_offset.x, font_offset.y ); if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ) { /* scale the outline and the metrics */ FT_Int n; FT_Outline* cur = decoder.builder.base; FT_Vector* vec = cur->points; FT_Fixed x_scale = glyph->x_scale; FT_Fixed y_scale = glyph->y_scale; /* First of all, scale the points */ if ( !hinting ) for ( n = cur->n_points; n > 0; n--, vec++ ) { vec->x = FT_MulFix( vec->x, x_scale ); vec->y = FT_MulFix( vec->y, y_scale ); } FT_Outline_Get_CBox( &glyph->root.outline, &cbox ); /* Then scale the metrics */ metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); metrics->vertBearingX = FT_MulFix( metrics->vertBearingX, x_scale ); metrics->vertBearingY = FT_MulFix( metrics->vertBearingY, y_scale ); if ( hinting ) { metrics->horiAdvance = ( metrics->horiAdvance + 32 ) & -64; metrics->vertAdvance = ( metrics->vertAdvance + 32 ) & -64; metrics->vertBearingX = ( metrics->vertBearingX + 32 ) & -64; metrics->vertBearingY = ( metrics->vertBearingY + 32 ) & -64; } } /* compute the other metrics */ FT_Outline_Get_CBox( &glyph->root.outline, &cbox ); /* grid fit the bounding box if necessary */ if ( hinting ) { cbox.xMin &= -64; cbox.yMin &= -64; cbox.xMax = ( cbox.xMax + 63 ) & -64; cbox.yMax = ( cbox.yMax + 63 ) & -64; } metrics->width = cbox.xMax - cbox.xMin; metrics->height = cbox.yMax - cbox.yMin; metrics->horiBearingX = cbox.xMin; metrics->horiBearingY = cbox.yMax; } } return error; }
void TtfFont::PlotString(const std::string &str, SBezierList *sbl, Vector origin, Vector u, Vector v) { ssassert(fontFace != NULL, "Expected font face to be loaded"); FT_Pos dx = 0; for(char32_t chr : ReadUTF8(str)) { uint32_t gid = FT_Get_Char_Index(fontFace, chr); if (gid == 0) { dbp("freetype: CID-to-GID mapping for CID 0x%04x failed: %s; using CID as GID", chr, ft_error_string(gid)); } FT_F26Dot6 scale = fontFace->units_per_EM; if(int fterr = FT_Set_Char_Size(fontFace, scale, scale, 72, 72)) { dbp("freetype: cannot set character size: %s", ft_error_string(fterr)); return; } /* * Stupid hacks: * - if we want fake-bold, use FT_Outline_Embolden(). This actually looks * quite good. * - if we want fake-italic, apply a shear transform [1 s s 1 0 0] here using * FT_Set_Transform. This looks decent at small font sizes and bad at larger * ones, antialiasing mitigates this considerably though. */ if(int fterr = FT_Load_Glyph(fontFace, gid, FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING)) { dbp("freetype: cannot load glyph (gid %d): %s", gid, ft_error_string(fterr)); return; } /* A point that has x = xMin should be plotted at (dx0 + lsb); fix up * our x-position so that the curve-generating code will put stuff * at the right place. * * There's no point in getting the glyph BBox here - not only can it be * needlessly slow sometimes, but because we're about to render a single glyph, * what we want actually *is* the CBox. * * This is notwithstanding that this makes extremely little sense, this * looks like a workaround for either mishandling the start glyph on a line, * or as a really hacky pseudo-track-kerning (in which case it works better than * one would expect! especially since most fonts don't set track kerning). */ FT_BBox cbox; FT_Outline_Get_CBox(&fontFace->glyph->outline, &cbox); FT_Pos bx = dx - cbox.xMin; // Yes, this is what FreeType calls left-side bearing. // Then interchangeably uses that with "left-side bearing". Sigh. bx += fontFace->glyph->metrics.horiBearingX; OutlineData data = {}; data.origin = origin; data.u = u; data.v = v; data.beziers = sbl; data.factor = 1.0f/(float)scale; data.bx = bx; if(int fterr = FT_Outline_Decompose(&fontFace->glyph->outline, &outline_funcs, &data)) { dbp("freetype: bezier decomposition failed (gid %d): %s", gid, ft_error_string(fterr)); } // And we're done, so advance our position by the requested advance // width, plus the user-requested extra advance. dx += fontFace->glyph->advance.x; } }
GlyphData* getGlyphData(Font font, const unsigned int glyphIndex) { GlyphMap::iterator it = font->mGlyphs.find(glyphIndex); GlyphData* tempGlyph = (it!= font->mGlyphs.end())?&it->second:0; if (NULL==tempGlyph) { FT_Error err = FT_Load_Glyph(font->ftFace, glyphIndex, FT_LOAD_NO_HINTING); FT_GlyphSlot ftGlyph = font->ftFace->glyph; if (!err && ftGlyph) { err = FT_Render_Glyph(ftGlyph, FT_RENDER_MODE_NORMAL); if (!err && ftGlyph->format == FT_GLYPH_FORMAT_BITMAP) { if (font->textureIDList.empty()) { font->textureIDList.push_back(createTexture()); font->xOffset = font->yOffset = font->padding; } if (font->xOffset > (TEXTURE_WIDTH-font->glyphWidth)) { font->xOffset = font->padding; font->yOffset += font->glyphHeight; if (font->yOffset > (TEXTURE_HEIGHT - font->glyphHeight)) { font->textureIDList.push_back(createTexture()); font->yOffset = font->padding; } } tempGlyph = &font->mGlyphs[glyphIndex]; tempGlyph->glTextureID = font->textureIDList.back(); FT_BBox bbox; FT_Outline_Get_CBox(&(ftGlyph->outline), &bbox); tempGlyph->xmin = (float)(bbox.xMin)/64.0f; tempGlyph->ymin = (float)(bbox.yMin)/64.0f; tempGlyph->xmax = (float)(bbox.xMax)/64.0f; tempGlyph->ymax = (float)(bbox.yMax)/64.0f; tempGlyph->xadvance = ftGlyph->advance.x/64.0f; tempGlyph->yadvance = ftGlyph->advance.y/64.0f; FT_Bitmap bitmap = ftGlyph->bitmap; tempGlyph->width = bitmap.width; tempGlyph->height = bitmap.rows; if( tempGlyph->width && tempGlyph->height) { glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glBindTexture(GL_TEXTURE_2D, tempGlyph->glTextureID); glTexSubImage2D( GL_TEXTURE_2D, 0, font->xOffset, font->yOffset, tempGlyph->width, tempGlyph->height, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.buffer ); glPopClientAttrib(); } tempGlyph->u0 = (float)(font->xOffset)/(float)(TEXTURE_WIDTH); tempGlyph->v0 = (float)(font->yOffset)/(float)(TEXTURE_HEIGHT); tempGlyph->u1 = (float)(font->xOffset+tempGlyph->width) / (float)(TEXTURE_WIDTH); tempGlyph->v1 = (float)(font->yOffset+tempGlyph->height) / (float)(TEXTURE_HEIGHT); tempGlyph->xoffset = (float)ftGlyph->bitmap_left; tempGlyph->yoffset = (float)ftGlyph->bitmap_top; font->xOffset += tempGlyph->xmax - tempGlyph->xmin + font->padding; } } } return tempGlyph; }
af_loader_load_glyph( AF_Loader loader, AF_Module module, FT_Face face, FT_UInt glyph_index, FT_Int32 load_flags ) { FT_Error error; FT_Size size = face->size; FT_Size_Internal size_internal = size->internal; FT_GlyphSlot slot = face->glyph; FT_Slot_Internal slot_internal = slot->internal; FT_GlyphLoader gloader = slot_internal->loader; AF_GlyphHints hints = loader->hints; AF_ScalerRec scaler; AF_StyleMetrics style_metrics; FT_UInt style_options = AF_STYLE_NONE_DFLT; AF_StyleClass style_class; AF_WritingSystemClass writing_system_class; if ( !size ) return FT_THROW( Invalid_Size_Handle ); FT_ZERO( &scaler ); if ( !size_internal->autohint_metrics.x_scale || size_internal->autohint_mode != FT_LOAD_TARGET_MODE( load_flags ) ) { /* switching between hinting modes usually means different scaling */ /* values; this later on enforces recomputation of everything */ /* related to the current size */ size_internal->autohint_mode = FT_LOAD_TARGET_MODE( load_flags ); size_internal->autohint_metrics = size->metrics; #ifdef AF_CONFIG_OPTION_TT_SIZE_METRICS { FT_Size_Metrics* size_metrics = &size_internal->autohint_metrics; /* set metrics to integer values and adjust scaling accordingly; */ /* this is the same setup as with TrueType fonts, cf. function */ /* `tt_size_reset' in file `ttobjs.c' */ size_metrics->ascender = FT_PIX_ROUND( FT_MulFix( face->ascender, size_metrics->y_scale ) ); size_metrics->descender = FT_PIX_ROUND( FT_MulFix( face->descender, size_metrics->y_scale ) ); size_metrics->height = FT_PIX_ROUND( FT_MulFix( face->height, size_metrics->y_scale ) ); size_metrics->x_scale = FT_DivFix( size_metrics->x_ppem << 6, face->units_per_EM ); size_metrics->y_scale = FT_DivFix( size_metrics->y_ppem << 6, face->units_per_EM ); size_metrics->max_advance = FT_PIX_ROUND( FT_MulFix( face->max_advance_width, size_metrics->x_scale ) ); } #endif /* AF_CONFIG_OPTION_TT_SIZE_METRICS */ } /* * TODO: This code currently doesn't support fractional advance widths, * i.e., placing hinted glyphs at anything other than integer * x-positions. This is only relevant for the warper code, which * scales and shifts glyphs to optimize blackness of stems (hinting on * the x-axis by nature places things on pixel integers, hinting on the * y-axis only, i.e., LIGHT mode, doesn't touch the x-axis). The delta * values of the scaler would need to be adjusted. */ scaler.face = face; scaler.x_scale = size_internal->autohint_metrics.x_scale; scaler.x_delta = 0; scaler.y_scale = size_internal->autohint_metrics.y_scale; scaler.y_delta = 0; scaler.render_mode = FT_LOAD_TARGET_MODE( load_flags ); scaler.flags = 0; /* note that the fallback style can't be changed anymore */ /* after the first call of `af_loader_load_glyph' */ error = af_loader_reset( loader, module, face ); if ( error ) goto Exit; #ifdef FT_OPTION_AUTOFIT2 /* XXX: undocumented hook to activate the latin2 writing system. */ if ( load_flags & ( 1UL << 20 ) ) style_options = AF_STYLE_LTN2_DFLT; #endif /* * Glyphs (really code points) are assigned to scripts. Script * analysis is done lazily: For each glyph that passes through here, * the corresponding script analyzer is called, but returns immediately * if it has been run already. */ error = af_face_globals_get_metrics( loader->globals, glyph_index, style_options, &style_metrics ); if ( error ) goto Exit; style_class = style_metrics->style_class; writing_system_class = af_writing_system_classes[style_class->writing_system]; loader->metrics = style_metrics; if ( writing_system_class->style_metrics_scale ) writing_system_class->style_metrics_scale( style_metrics, &scaler ); else style_metrics->scaler = scaler; if ( writing_system_class->style_hints_init ) { error = writing_system_class->style_hints_init( hints, style_metrics ); if ( error ) goto Exit; } /* * Do the main work of `af_loader_load_glyph'. Note that we never have * to deal with composite glyphs as those get loaded into * FT_GLYPH_FORMAT_OUTLINE by the recursed `FT_Load_Glyph' function. * In the rare cases where FT_LOAD_NO_RECURSE is set, it implies * FT_LOAD_NO_SCALE and as such the auto-hinter is never called. */ load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_TRANSFORM | FT_LOAD_LINEAR_DESIGN; load_flags &= ~FT_LOAD_RENDER; error = FT_Load_Glyph( face, glyph_index, load_flags ); if ( error ) goto Exit; /* * Apply stem darkening (emboldening) here before hints are applied to * the outline. Glyphs are scaled down proportionally to the * emboldening so that curve points don't fall outside their * precomputed blue zones. * * Any emboldening done by the font driver (e.g., the CFF driver) * doesn't reach here because the autohinter loads the unprocessed * glyphs in font units for analysis (functions `af_*_metrics_init_*') * and then above to prepare it for the rasterizers by itself, * independently of the font driver. So emboldening must be done here, * within the autohinter. * * All glyphs to be autohinted pass through here one by one. The * standard widths can therefore change from one glyph to the next, * depending on what script a glyph is assigned to (each script has its * own set of standard widths and other metrics). The darkening amount * must therefore be recomputed for each size and * `standard_{vertical,horizontal}_width' change. * * Ignore errors and carry on without emboldening. * */ /* stem darkening only works well in `light' mode */ if ( scaler.render_mode == FT_RENDER_MODE_LIGHT && ( !face->internal->no_stem_darkening || ( face->internal->no_stem_darkening < 0 && !module->no_stem_darkening ) ) ) af_loader_embolden_glyph_in_slot( loader, face, style_metrics ); loader->transformed = slot_internal->glyph_transformed; if ( loader->transformed ) { FT_Matrix inverse; loader->trans_matrix = slot_internal->glyph_matrix; loader->trans_delta = slot_internal->glyph_delta; inverse = loader->trans_matrix; if ( !FT_Matrix_Invert( &inverse ) ) FT_Vector_Transform( &loader->trans_delta, &inverse ); } switch ( slot->format ) { case FT_GLYPH_FORMAT_OUTLINE: /* translate the loaded glyph when an internal transform is needed */ if ( loader->transformed ) FT_Outline_Translate( &slot->outline, loader->trans_delta.x, loader->trans_delta.y ); /* compute original horizontal phantom points */ /* (and ignore vertical ones) */ loader->pp1.x = hints->x_delta; loader->pp1.y = hints->y_delta; loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance, hints->x_scale ) + hints->x_delta; loader->pp2.y = hints->y_delta; /* be sure to check for spacing glyphs */ if ( slot->outline.n_points == 0 ) goto Hint_Metrics; /* now load the slot image into the auto-outline */ /* and run the automatic hinting process */ if ( writing_system_class->style_hints_apply ) { error = writing_system_class->style_hints_apply( glyph_index, hints, &gloader->base.outline, style_metrics ); if ( error ) goto Exit; } /* we now need to adjust the metrics according to the change in */ /* width/positioning that occurred during the hinting process */ if ( scaler.render_mode != FT_RENDER_MODE_LIGHT ) { AF_AxisHints axis = &hints->axis[AF_DIMENSION_HORZ]; if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) ) { AF_Edge edge1 = axis->edges; /* leftmost edge */ AF_Edge edge2 = edge1 + axis->num_edges - 1; /* rightmost edge */ FT_Pos old_rsb = loader->pp2.x - edge2->opos; /* loader->pp1.x is always zero at this point of time */ FT_Pos old_lsb = edge1->opos; /* - loader->pp1.x */ FT_Pos new_lsb = edge1->pos; /* remember unhinted values to later account */ /* for rounding errors */ FT_Pos pp1x_uh = new_lsb - old_lsb; FT_Pos pp2x_uh = edge2->pos + old_rsb; /* prefer too much space over too little space */ /* for very small sizes */ if ( old_lsb < 24 ) pp1x_uh -= 8; if ( old_rsb < 24 ) pp2x_uh += 8; loader->pp1.x = FT_PIX_ROUND( pp1x_uh ); loader->pp2.x = FT_PIX_ROUND( pp2x_uh ); if ( loader->pp1.x >= new_lsb && old_lsb > 0 ) loader->pp1.x -= 64; if ( loader->pp2.x <= edge2->pos && old_rsb > 0 ) loader->pp2.x += 64; slot->lsb_delta = loader->pp1.x - pp1x_uh; slot->rsb_delta = loader->pp2.x - pp2x_uh; } else { FT_Pos pp1x = loader->pp1.x; FT_Pos pp2x = loader->pp2.x; loader->pp1.x = FT_PIX_ROUND( pp1x + hints->xmin_delta ); loader->pp2.x = FT_PIX_ROUND( pp2x + hints->xmax_delta ); slot->lsb_delta = loader->pp1.x - pp1x; slot->rsb_delta = loader->pp2.x - pp2x; } } /* `light' mode uses integer advance widths */ /* but sets `lsb_delta' and `rsb_delta' */ else { FT_Pos pp1x = loader->pp1.x; FT_Pos pp2x = loader->pp2.x; loader->pp1.x = FT_PIX_ROUND( pp1x ); loader->pp2.x = FT_PIX_ROUND( pp2x ); slot->lsb_delta = loader->pp1.x - pp1x; slot->rsb_delta = loader->pp2.x - pp2x; } break; default: /* we don't support other formats (yet?) */ error = FT_THROW( Unimplemented_Feature ); } Hint_Metrics: { FT_BBox bbox; FT_Vector vvector; vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX; vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY; vvector.x = FT_MulFix( vvector.x, style_metrics->scaler.x_scale ); vvector.y = FT_MulFix( vvector.y, style_metrics->scaler.y_scale ); /* transform the hinted outline if needed */ if ( loader->transformed ) { FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix ); FT_Vector_Transform( &vvector, &loader->trans_matrix ); } /* we must translate our final outline by -pp1.x and compute */ /* the new metrics */ if ( loader->pp1.x ) FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 ); FT_Outline_Get_CBox( &gloader->base.outline, &bbox ); bbox.xMin = FT_PIX_FLOOR( bbox.xMin ); bbox.yMin = FT_PIX_FLOOR( bbox.yMin ); bbox.xMax = FT_PIX_CEIL( bbox.xMax ); bbox.yMax = FT_PIX_CEIL( bbox.yMax ); slot->metrics.width = bbox.xMax - bbox.xMin; slot->metrics.height = bbox.yMax - bbox.yMin; slot->metrics.horiBearingX = bbox.xMin; slot->metrics.horiBearingY = bbox.yMax; slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x ); slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y ); /* for mono-width fonts (like Andale, Courier, etc.) we need */ /* to keep the original rounded advance width; ditto for */ /* digits if all have the same advance width */ if ( scaler.render_mode != FT_RENDER_MODE_LIGHT && ( FT_IS_FIXED_WIDTH( slot->face ) || ( af_face_globals_is_digit( loader->globals, glyph_index ) && style_metrics->digits_have_same_width ) ) ) { slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance, style_metrics->scaler.x_scale ); /* Set delta values to 0. Otherwise code that uses them is */ /* going to ruin the fixed advance width. */ slot->lsb_delta = 0; slot->rsb_delta = 0; } else { /* non-spacing glyphs must stay as-is */ if ( slot->metrics.horiAdvance ) slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x; } slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance, style_metrics->scaler.y_scale ); slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance ); slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance ); slot->format = FT_GLYPH_FORMAT_OUTLINE; } Exit: return error; }
GlyphBLF *blf_glyph_add(FontBLF *font, unsigned int index, unsigned int c) { FT_GlyphSlot slot; GlyphBLF *g; FT_Error err; FT_Bitmap bitmap, tempbitmap; const bool is_sharp = (U.text_render & USER_TEXT_DISABLE_AA) != 0; int flags = FT_LOAD_TARGET_NORMAL | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP; FT_BBox bbox; unsigned int key; g = blf_glyph_search(font->glyph_cache, c); if (g) return g; /* glyphs are dynamically created as needed by font rendering. this means that * to make font rendering thread safe we have to do locking here. note that this * must be a lock for the whole library and not just per font, because the font * renderer uses a shared buffer internally */ BLI_spin_lock(font->ft_lib_mutex); /* search again after locking */ g = blf_glyph_search(font->glyph_cache, c); if (g) { BLI_spin_unlock(font->ft_lib_mutex); return g; } if (font->flags & BLF_HINTING) flags &= ~FT_LOAD_NO_HINTING; if (is_sharp) err = FT_Load_Glyph(font->face, (FT_UInt)index, FT_LOAD_TARGET_MONO); else err = FT_Load_Glyph(font->face, (FT_UInt)index, flags); if (err) { BLI_spin_unlock(font->ft_lib_mutex); return NULL; } /* get the glyph. */ slot = font->face->glyph; if (is_sharp) { err = FT_Render_Glyph(slot, FT_RENDER_MODE_MONO); /* Convert result from 1 bit per pixel to 8 bit per pixel */ /* Accum errors for later, fine if not interested beyond "ok vs any error" */ FT_Bitmap_New(&tempbitmap); err += FT_Bitmap_Convert(font->ft_lib, &slot->bitmap, &tempbitmap, 1); /* Does Blender use Pitch 1 always? It works so far */ err += FT_Bitmap_Copy(font->ft_lib, &tempbitmap, &slot->bitmap); err += FT_Bitmap_Done(font->ft_lib, &tempbitmap); } else { err = FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL); } if (err || slot->format != FT_GLYPH_FORMAT_BITMAP) { BLI_spin_unlock(font->ft_lib_mutex); return NULL; } g = (GlyphBLF *)MEM_callocN(sizeof(GlyphBLF), "blf_glyph_add"); g->c = c; g->idx = (FT_UInt)index; g->xoff = -1; g->yoff = -1; bitmap = slot->bitmap; g->width = (int)bitmap.width; g->height = (int)bitmap.rows; if (g->width && g->height) { if (is_sharp) { /* Font buffer uses only 0 or 1 values, Blender expects full 0..255 range */ int i; for (i = 0; i < (g->width * g->height); i++) { bitmap.buffer[i] = bitmap.buffer[i] ? 255 : 0; } } g->bitmap = (unsigned char *)MEM_mallocN((size_t)(g->width * g->height), "glyph bitmap"); memcpy((void *)g->bitmap, (void *)bitmap.buffer, (size_t)(g->width * g->height)); } g->advance = ((float)slot->advance.x) / 64.0f; g->advance_i = (int)g->advance; g->pos_x = (float)slot->bitmap_left; g->pos_y = (float)slot->bitmap_top; g->pitch = slot->bitmap.pitch; FT_Outline_Get_CBox(&(slot->outline), &bbox); g->box.xmin = ((float)bbox.xMin) / 64.0f; g->box.xmax = ((float)bbox.xMax) / 64.0f; g->box.ymin = ((float)bbox.yMin) / 64.0f; g->box.ymax = ((float)bbox.yMax) / 64.0f; key = blf_hash(g->c); BLI_addhead(&(font->glyph_cache->bucket[key]), g); BLI_spin_unlock(font->ft_lib_mutex); return g; }
T1_Load_Glyph( FT_GlyphSlot t1glyph, /* T1_GlyphSlot */ FT_Size t1size, /* T1_Size */ FT_UInt glyph_index, FT_Int32 load_flags ) { T1_GlyphSlot glyph = (T1_GlyphSlot)t1glyph; FT_Error error; #ifdef __REACTOS__ T1_DecoderRec *decoder = malloc(sizeof(T1_DecoderRec)); if (!decoder) return FT_THROW( Out_Of_Memory ); /* Ugly but it allows us to reduce the diff */ #define decoder (*decoder) { #else T1_DecoderRec decoder; #endif T1_Face face = (T1_Face)t1glyph->face; FT_Bool hinting; FT_Bool scaled; FT_Bool force_scaling = FALSE; T1_Font type1 = &face->type1; PSAux_Service psaux = (PSAux_Service)face->psaux; const T1_Decoder_Funcs decoder_funcs = psaux->t1_decoder_funcs; FT_Matrix font_matrix; FT_Vector font_offset; FT_Data glyph_data; FT_Bool must_finish_decoder = FALSE; #ifdef FT_CONFIG_OPTION_INCREMENTAL FT_Bool glyph_data_loaded = 0; #endif #ifdef FT_CONFIG_OPTION_INCREMENTAL if ( glyph_index >= (FT_UInt)face->root.num_glyphs && !face->root.internal->incremental_interface ) #else if ( glyph_index >= (FT_UInt)face->root.num_glyphs ) #endif /* FT_CONFIG_OPTION_INCREMENTAL */ { error = FT_THROW( Invalid_Argument ); goto Exit; } FT_TRACE1(( "T1_Load_Glyph: glyph index %d\n", glyph_index )); FT_ASSERT( ( face->len_buildchar == 0 ) == ( face->buildchar == NULL ) ); if ( load_flags & FT_LOAD_NO_RECURSE ) load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING; if ( t1size ) { glyph->x_scale = t1size->metrics.x_scale; glyph->y_scale = t1size->metrics.y_scale; } else { glyph->x_scale = 0x10000L; glyph->y_scale = 0x10000L; } t1glyph->outline.n_points = 0; t1glyph->outline.n_contours = 0; hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 && ( load_flags & FT_LOAD_NO_HINTING ) == 0 ); scaled = FT_BOOL( ( load_flags & FT_LOAD_NO_SCALE ) == 0 ); glyph->hint = hinting; glyph->scaled = scaled; t1glyph->format = FT_GLYPH_FORMAT_OUTLINE; error = decoder_funcs->init( &decoder, t1glyph->face, t1size, t1glyph, (FT_Byte**)type1->glyph_names, face->blend, FT_BOOL( hinting ), FT_LOAD_TARGET_MODE( load_flags ), T1_Parse_Glyph ); if ( error ) goto Exit; must_finish_decoder = TRUE; decoder.builder.no_recurse = FT_BOOL( ( load_flags & FT_LOAD_NO_RECURSE ) != 0 ); decoder.num_subrs = type1->num_subrs; decoder.subrs = type1->subrs; decoder.subrs_len = type1->subrs_len; decoder.subrs_hash = type1->subrs_hash; decoder.buildchar = face->buildchar; decoder.len_buildchar = face->len_buildchar; /* now load the unscaled outline */ error = T1_Parse_Glyph_And_Get_Char_String( &decoder, glyph_index, &glyph_data, &force_scaling ); if ( error ) goto Exit; #ifdef FT_CONFIG_OPTION_INCREMENTAL glyph_data_loaded = 1; #endif hinting = glyph->hint; font_matrix = decoder.font_matrix; font_offset = decoder.font_offset; /* save new glyph tables */ decoder_funcs->done( &decoder ); must_finish_decoder = FALSE; /* now, set the metrics -- this is rather simple, as */ /* the left side bearing is the xMin, and the top side */ /* bearing the yMax */ if ( !error ) { t1glyph->outline.flags &= FT_OUTLINE_OWNER; t1glyph->outline.flags |= FT_OUTLINE_REVERSE_FILL; /* for composite glyphs, return only left side bearing and */ /* advance width */ if ( load_flags & FT_LOAD_NO_RECURSE ) { FT_Slot_Internal internal = t1glyph->internal; t1glyph->metrics.horiBearingX = FIXED_TO_INT( decoder.builder.left_bearing.x ); t1glyph->metrics.horiAdvance = FIXED_TO_INT( decoder.builder.advance.x ); internal->glyph_matrix = font_matrix; internal->glyph_delta = font_offset; internal->glyph_transformed = 1; } else { FT_BBox cbox; FT_Glyph_Metrics* metrics = &t1glyph->metrics; /* copy the _unscaled_ advance width */ metrics->horiAdvance = FIXED_TO_INT( decoder.builder.advance.x ); t1glyph->linearHoriAdvance = FIXED_TO_INT( decoder.builder.advance.x ); t1glyph->internal->glyph_transformed = 0; if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) { /* make up vertical ones */ metrics->vertAdvance = ( face->type1.font_bbox.yMax - face->type1.font_bbox.yMin ) >> 16; t1glyph->linearVertAdvance = metrics->vertAdvance; } else { metrics->vertAdvance = FIXED_TO_INT( decoder.builder.advance.y ); t1glyph->linearVertAdvance = FIXED_TO_INT( decoder.builder.advance.y ); } t1glyph->format = FT_GLYPH_FORMAT_OUTLINE; if ( t1size && t1size->metrics.y_ppem < 24 ) t1glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION; #if 1 /* apply the font matrix, if any */ if ( font_matrix.xx != 0x10000L || font_matrix.yy != 0x10000L || font_matrix.xy != 0 || font_matrix.yx != 0 ) { FT_Outline_Transform( &t1glyph->outline, &font_matrix ); metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, font_matrix.xx ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, font_matrix.yy ); } if ( font_offset.x || font_offset.y ) { FT_Outline_Translate( &t1glyph->outline, font_offset.x, font_offset.y ); metrics->horiAdvance += font_offset.x; metrics->vertAdvance += font_offset.y; } #endif if ( ( load_flags & FT_LOAD_NO_SCALE ) == 0 || force_scaling ) { /* scale the outline and the metrics */ FT_Int n; FT_Outline* cur = decoder.builder.base; FT_Vector* vec = cur->points; FT_Fixed x_scale = glyph->x_scale; FT_Fixed y_scale = glyph->y_scale; /* First of all, scale the points, if we are not hinting */ if ( !hinting || ! decoder.builder.hints_funcs ) for ( n = cur->n_points; n > 0; n--, vec++ ) { vec->x = FT_MulFix( vec->x, x_scale ); vec->y = FT_MulFix( vec->y, y_scale ); } /* Then scale the metrics */ metrics->horiAdvance = FT_MulFix( metrics->horiAdvance, x_scale ); metrics->vertAdvance = FT_MulFix( metrics->vertAdvance, y_scale ); } /* compute the other metrics */ FT_Outline_Get_CBox( &t1glyph->outline, &cbox ); metrics->width = cbox.xMax - cbox.xMin; metrics->height = cbox.yMax - cbox.yMin; metrics->horiBearingX = cbox.xMin; metrics->horiBearingY = cbox.yMax; if ( load_flags & FT_LOAD_VERTICAL_LAYOUT ) { /* make up vertical ones */ ft_synthesize_vertical_metrics( metrics, metrics->vertAdvance ); } }
/* * Class: FreetypeFont * Method: loadGlyph0 * Signature: (JI)V */ JNIEXPORT void JNICALL Java_sage_FreetypeFont_loadGlyph0 (JNIEnv *env, jobject jo, jlong fontPtr, jint glyphCode) { //FT_Face face = (FT_Face) facePtr; FTDataStruct* fontData = (FTDataStruct*)(intptr_t) fontPtr; FT_Activate_Size(fontData->sizePtr); int error = FT_Load_Glyph(fontData->facePtr, glyphCode, FT_LOAD_FORCE_AUTOHINT); if ((fontData->style & FT_STYLE_FLAG_BOLD) != 0) { // Apply bold effect if (fontData->facePtr->glyph->format == FT_GLYPH_FORMAT_OUTLINE) { /* some reasonable strength */ FT_Pos strength = FT_MulFix(fontData->facePtr->units_per_EM, fontData->facePtr->size->metrics.y_scale ) / 42; FT_BBox bbox_before, bbox_after; // The bounding box code was what XBMC was using to do this calculation; but the // examples in the freetype library use the *4 math below which then doesn't clip // the text when we render it. // FT_Outline_Get_CBox(&fontData->facePtr->glyph->outline, &bbox_before); FT_Outline_Embolden(&fontData->facePtr->glyph->outline, strength); // ignore error // FT_Outline_Get_CBox(&fontData->facePtr->glyph->outline, &bbox_after); // FT_Pos dx = bbox_after.xMax - bbox_before.xMax; // FT_Pos dy = bbox_after.yMax - bbox_before.yMax; FT_Pos dx = strength * 4; FT_Pos dy = dx; if (fontData->facePtr->glyph->advance.x) fontData->facePtr->glyph->advance.x += dx; if (fontData->facePtr->glyph->advance.y) fontData->facePtr->glyph->advance.y += dy; fontData->facePtr->glyph->metrics.width += dx; fontData->facePtr->glyph->metrics.height += dy; fontData->facePtr->glyph->metrics.horiBearingY += dy; fontData->facePtr->glyph->metrics.horiAdvance += dx; fontData->facePtr->glyph->metrics.vertBearingX -= dx / 2; fontData->facePtr->glyph->metrics.vertBearingY += dy; fontData->facePtr->glyph->metrics.vertAdvance += dy; } } if ((fontData->style & FT_STYLE_FLAG_ITALIC) != 0) { // Apply italics effect if (fontData->facePtr->glyph->format == FT_GLYPH_FORMAT_OUTLINE) { /* For italic, simply apply a shear transform, with an angle */ /* of about 12 degrees. */ FT_Matrix transform; transform.xx = 0x10000L; transform.yx = 0x00000L; transform.xy = 0x06000L; transform.yy = 0x10000L; FT_BBox bbox_before, bbox_after; FT_Outline_Get_CBox(&fontData->facePtr->glyph->outline, &bbox_before); FT_Outline_Transform(&fontData->facePtr->glyph->outline, &transform); FT_Outline_Get_CBox(&fontData->facePtr->glyph->outline, &bbox_after); FT_Pos dx = bbox_after.xMax - bbox_before.xMax; FT_Pos dy = bbox_after.yMax - bbox_before.yMax; fontData->facePtr->glyph->metrics.width += dx; fontData->facePtr->glyph->metrics.height += dy; } } }