DaoxGlyph* DaoxFont_LoadGlyph( DaoxFont *self, size_t codepoint ) { DaoxGlyph *glyph = DaoxGlyph_New(); stbtt_vertex *vertices = NULL; int id = stbtt_FindGlyphIndex( & self->info, codepoint ); int i, num_verts = stbtt_GetGlyphShape( & self->info, id, & vertices ); stbtt_GetGlyphHMetrics( & self->info, id, & glyph->advanceWidth, & glyph->leftSideBearing ); DMap_Insert( self->glyphs, (void*) codepoint, glyph ); DaoxPath_SetRelativeMode( glyph->shape, 0 ); for(i=0; i<num_verts; ++i){ stbtt_vertex_type x = vertices[i].x; stbtt_vertex_type y = vertices[i].y; stbtt_vertex_type cx = vertices[i].cx; stbtt_vertex_type cy = vertices[i].cy; switch( vertices[i].type ){ case STBTT_vmove : if( i ) DaoxPath_Close( glyph->shape ); DaoxPath_MoveTo( glyph->shape, x, y ); break; case STBTT_vline : DaoxPath_LineTo( glyph->shape, x, y ); break; case STBTT_vcurve : DaoxPath_QuadTo( glyph->shape, cx, cy, x, y ); break; } } if( i ) DaoxPath_Close( glyph->shape ); stbtt_FreeShape( & self->info, vertices ); return glyph; }
bool FontRenderer::hasGlyph(const fm::Uint32 &letter) const { if (!m_fileContent) return false; return stbtt_FindGlyphIndex((stbtt_fontinfo*)m_stbFontInfo,letter) != 0; }
struct TEXT_FONT_GLYPH *textRenderGlyphToCache(TEXT_FONT *font, unsigned int glyph) { int glyph_index, x1, y1, x2, y2; struct TEXT_FONT_CACHE *next; next = font->cache; glyph_index = stbtt_FindGlyphIndex(&font->face, glyph); /* Just in case the coords aren't reported correctly o_O */ x1 = y1 = x2 = y2 = 0; // stbtt_GetCodepointBox(&font->face, glyph_index, &x1, &y1, &x2, &y2); stbtt_GetCodepointBitmapBox(&font->face, glyph, font->scale, font->scale, &x1, &y1, &x2, &y2); #if 0 x1 = font->scale * x1; y1 = font->scale * y1; x2 = font->scale * x2; y2 = font->scale * y2; #endif while (next != NULL) { if (textWillGlyphFit(next, x2 - x1, y2 - y1) == 0) break; next = next->next; } if (next == NULL) if ((next = textAppendCache(font, font->cache, font->tex_w, font->tex_h)) == NULL) return NULL; if (font->cache == NULL) font->cache = next; return textRenderGlyph(next, glyph, glyph_index, font); }
// Bake a given set of ranges of chars int TTFFontAsset::BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf) float pixel_height, // height of font in pixels unsigned char *pixels, int pw, int ph, // bitmap to be filled in const GlyphRanges& ranges, // characters to bake BakedChar *chardata) { stbtt_fontinfo f; stbtt_InitFont(&f, data, offset); STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels int x = 1, y = 1; int bottom_y = 1; glyphMap.clear(); float scale = stbtt_ScaleForPixelHeight(&f, pixel_height); int bakedIdx = 0; for (GlyphRanges::const_iterator iter = ranges.begin(); iter != ranges.end(); ++iter) { const Range<int>& range = *iter; int numItemsInRange = range.high - range.low; for (int i = 0; i <= numItemsInRange; i++, bakedIdx++) { int unicodeCodepoint = i + range.low; glyphMap.insert(std::make_pair(unicodeCodepoint, bakedIdx)); int advance, lsb, x0,y0,x1,y1,gw,gh; int g = stbtt_FindGlyphIndex(&f, unicodeCodepoint); stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb); stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1); gw = x1-x0; gh = y1-y0; if (x + gw + 1 >= pw) y = bottom_y, x = 1; // advance to next row if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row return -i; STBTT_assert(x+gw < pw); STBTT_assert(y+gh < ph); stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g); chardata[bakedIdx].x0 = (stbtt_int16) x; chardata[bakedIdx].y0 = (stbtt_int16) y; chardata[bakedIdx].x1 = (stbtt_int16) (x + gw); chardata[bakedIdx].y1 = (stbtt_int16) (y + gh); chardata[bakedIdx].xadvance = scale * advance; chardata[bakedIdx].xoff = (float) x0; chardata[bakedIdx].yoff = (float) y0; x = x + gw + 2; if (y+gh+2 > bottom_y) bottom_y = y+gh+2; } } return bottom_y; }
void UniFont::CacheGlyphDefault(const unsigned int* str, int index, int len, int cellCols, int cellRows, Texture* tex, bool* outIsDirty) { int codePoint = str[index]; int charSize = fontScale + 2; if (GetInfoForCodepoint(codePoint) == nullptr) { stbtt_fontinfo* font = nullptr; unsigned char* cBmp = nullptr; int cW = 0, cH = 0; for (int j = 0; j < fontInfos.count; j++) { if (fontInfos.data[j].low <= codePoint && codePoint <= fontInfos.data[j].high) { float pixelScale = stbtt_ScaleForPixelHeight(&fontInfos.data[j].info, fontScale); int glyphIdx = stbtt_FindGlyphIndex(&fontInfos.data[j].info, codePoint); cBmp = stbtt_GetCodepointBitmap(&fontInfos.data[j].info, 0, pixelScale, codePoint, &cW, &cH, 0, 0); if (cBmp) { font = &fontInfos.data[j].info; break; } } } if (font != nullptr) { *outIsDirty = true; int ascent, descent, lineGap; stbtt_GetFontVMetrics(font, &ascent, &descent, &lineGap); float pixelScale = stbtt_ScaleForPixelHeight(font, fontScale); int cellY = cacheCursor / cellCols; int cellX = cacheCursor % cellCols; int startX = cellX * charSize; int startY = cellY * charSize; GlyphBlit(tex->texMem, tex->width, tex->height, startX, startY, cBmp, cW, cH); free(cBmp); int advanceWidth = 0, leftSideBearing = 0; stbtt_GetCodepointHMetrics(font, codePoint, &advanceWidth, &leftSideBearing); int x0, y0, x1, y1; stbtt_GetCodepointBitmapBox(font, codePoint, pixelScale, pixelScale, &x0, &y0, &x1, &y1); CodepointInfo pointInfo = {}; pointInfo.codepoint = codePoint; pointInfo.x = startX; pointInfo.y = startY; pointInfo.w = cW; pointInfo.h = cH; pointInfo.xOffset = x0; pointInfo.yOffset = y0; pointInfo.xAdvance = pixelScale * advanceWidth; codepointListing.EnsureCapacity(cacheCursor + 1); codepointListing.count = BNS_MAX(codepointListing.count, cacheCursor + 1); codepointListing.data[cacheCursor] = pointInfo; cacheCursor = (cacheCursor + 1) % (cellCols * cellRows); } } }
std::vector<unsigned> getGlyphs(const std::string& text) override { std::vector<unsigned> res; for(auto cp : utils::utf8_to_codepoint(text)) { res.emplace_back(stbtt_FindGlyphIndex(&font_handle_, cp)); } return res; }
JNIEXPORT jint JNICALL Java_com_badlogic_gdx_graphics_g2d_stbtt_StbTrueType_findGlyphIndex(JNIEnv* env, jclass clazz, jlong info, jint unicodeCodepoint) { //@line:49 return stbtt_FindGlyphIndex((stbtt_fontinfo*)info, unicodeCodepoint); }
void GlFont::InitialiseFont(const unsigned char* ttf_buffer, float pixel_height, int tex_w, int tex_h) { font_height_px = pixel_height; this->tex_w = tex_w; this->tex_h = tex_h; font_bitmap = new unsigned char[tex_w*tex_h]; const int offset = 0; stbtt_fontinfo f; if (!stbtt_InitFont(&f, ttf_buffer, offset)) { throw std::runtime_error("Unable to initialise font"); } float scale = stbtt_ScaleForPixelHeight(&f, pixel_height); STBTT_memset(font_bitmap, 0, tex_w*tex_h); int x = 1; int y = 1; int bottom_y = 1; // Generate bitmap and char indices for (int i=0; i < NUM_CHARS; ++i) { int advance, lsb, x0,y0,x1,y1,gw,gh; int g = stbtt_FindGlyphIndex(&f, FIRST_CHAR + i); stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb); stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1); gw = x1-x0; gh = y1-y0; if (x + gw + 1 >= tex_w) y = bottom_y, x = 1; // advance to next row if (y + gh + 1 >= tex_h) // check if it fits vertically AFTER potentially moving to next row throw std::runtime_error("Unable to initialise font"); STBTT_assert(x+gw < tex_w); STBTT_assert(y+gh < tex_h); stbtt_MakeGlyphBitmap(&f, font_bitmap+x+y*tex_w, gw,gh,tex_w, scale,scale, g); // Adjust offset for edges of pixels chardata[i] = GlChar(tex_w,tex_h, x, y, gw, gh, scale*advance, x0 -0.5, -y0 -0.5); x = x + gw + 1; if (y+gh+1 > bottom_y) bottom_y = y+gh+1; } // Generate kern table for (int i=0; i < NUM_CHARS; ++i) { for (int j=0; j < NUM_CHARS; ++j) { kern_table[i*NUM_CHARS+j] = scale * stbtt_GetCodepointKernAdvance(&f,i,j); } } }
static uint32_t font_renderer_stb_unicode_update_atlas( stb_unicode_font_renderer_t *self, uint32_t charcode) { int advance_width, left_side_bearing; int id, glyph_index, offset_x, offset_y; struct font_glyph *glyph = NULL; uint8_t *dst = NULL; int x0 = 0; int y1 = 0; if(charcode > 0xFFFF) return 0; if(self->uc_to_id[charcode]) return self->uc_to_id[charcode]; id = font_renderer_stb_unicode_get_slot(self); self->id_to_uc[id] = charcode; self->uc_to_id[charcode] = id; self->atlas.dirty = true; glyph = &self->glyphs[id]; glyph_index = stbtt_FindGlyphIndex(&self->info, charcode); offset_x = (id % 16) * self->max_glyph_width; offset_y = (id / 16) * self->max_glyph_height; dst = self->atlas.buffer + offset_x + offset_y * self->atlas.width; stbtt_MakeGlyphBitmap(&self->info, dst, self->max_glyph_width, self->max_glyph_height, self->atlas.width, self->scale_factor, self->scale_factor, glyph_index); stbtt_GetGlyphHMetrics(&self->info, glyph_index, &advance_width, &left_side_bearing); stbtt_GetGlyphBox(&self->info, glyph_index, &x0, NULL, NULL, &y1); glyph->advance_x = advance_width * self->scale_factor; glyph->atlas_offset_x = offset_x; glyph->atlas_offset_y = offset_y; glyph->draw_offset_x = x0 * self->scale_factor; glyph->draw_offset_y = - y1 * self->scale_factor; glyph->width = self->max_glyph_width; glyph->height = self->max_glyph_height; return id; }
float text_show(float x, float y, char *str) { int ucs, gid; int kern; int left = 0; while (*str) { str += chartorune(&ucs, str); gid = stbtt_FindGlyphIndex(&text_font->info, ucs); kern = stbtt_GetGlyphKernAdvance(&text_font->info, left, gid); x += add_glyph(gid, x, y); x += kern * text_scale; left = gid; } return x; }
static float font_width_imp(struct font *font, float scale, char *str) { int ucs, gid; int advance, kern; float w = 0.0; int left = 0; while (*str) { str += chartorune(&ucs, str); gid = stbtt_FindGlyphIndex(&font->info, ucs); stbtt_GetGlyphHMetrics(&font->info, gid, &advance, NULL); kern = stbtt_GetGlyphKernAdvance(&font->info, left, gid); w += advance * scale; w += kern * scale; left = gid; } return w; }
stash::sth_glyph* stash::sth_font::get_glyph(unsigned int codepoint, short isize) { // Find code point and size. std::pair<unsigned int,int> key(codepoint, isize); if (glyphs.find( key ) != glyphs.end() ) return &glyphs.find( key )->second; // Could not find glyph, create it. int advance,lsb,x0,y0,x1,y1; int g = stbtt_FindGlyphIndex(&font, codepoint); float scale = stbtt_ScaleForPixelHeight(&font, isize/10.0f); stbtt_GetGlyphHMetrics(&font, g, &advance, &lsb); stbtt_GetGlyphBitmapBox(&font, g, scale,scale, &x0,&y0,&x1,&y1); int gw = x1-x0; int gh = y1-y0; // Find row where the glyph can be fit (vertically first, then horizontally) sth_row* br = 0; int rh = (gh+7) & ~7; for (size_t i = 0; i < rows->size() && !br; ++i) { if (rows->at(i).h == rh && rows->at(i).x+gw+1 <= tw) br = &rows->at(i); } // If no row found, add new. if (br == NULL) { short py = 0; // Check that there is enough space. if (rows->size()) { py = rows->back().y + rows->back().h+1; if (py+rh > th) return 0; } // Init and add row rows->push_back( sth_row(0,py,rh) ); br = &rows->back(); } // Init glyph. sth_glyph glyph; glyph.codepoint = codepoint; glyph.size = isize; glyph.x0 = br->x; glyph.y0 = br->y; glyph.x1 = glyph.x0+gw; glyph.y1 = glyph.y0+gh; glyph.xadv = scale * advance; glyph.xoff = (float)x0; glyph.yoff = (float)y0; // glyphs[ key ] = glyphs[ key ]; glyphs[ key ] = glyph; // Advance row location. br->x += gw+1; // Rasterize std::vector< unsigned char > bmp(gw*gh); if( bmp.size() ) { stbtt_MakeGlyphBitmap(&font, &bmp[0], gw,gh,gw, scale,scale, g); // Update texture glBindTexture(GL_TEXTURE_2D, tex); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexSubImage2D(GL_TEXTURE_2D, 0, glyph.x0,glyph.y0, gw,gh, GL_ALPHA,GL_UNSIGNED_BYTE,&bmp[0]); } return &glyphs[ key ]; }
int Face::indexForCodePoint(uint32 codepoint) { return stbtt_FindGlyphIndex(m_info, codepoint); }