Skip to content

Commit

Permalink
Fixed the ROMFS, Removed some fields in fs.list(), Updated sftdlib
Browse files Browse the repository at this point in the history
fs.list() now returns a table with tables containing the fields "name", "fileSize", and "isDirectory"
  • Loading branch information
firew0lf committed Apr 16, 2016
1 parent 6b65df0 commit b798818
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 143 deletions.
13 changes: 10 additions & 3 deletions libs/sftdlib/libsftd/include/sftd.h
Expand Up @@ -110,9 +110,19 @@ void sftd_draw_wtextf(sftd_font *font, int x, int y, unsigned int color, unsigne
* @param font the font used to calculate the width
* @param size the font size
* @param text a pointer to the text that will be used to calculate the length
* @return the width in pixels
*/
int sftd_get_text_width(sftd_font *font, unsigned int size, const char *text);

/**
* @brief Returns the width of the given wide text in pixels
* @param font the font used to calculate the width
* @param size the font size
* @param text a pointer to the wide text that will be used to calculate the length
* @return the width in pixels
*/
int sftd_get_wtext_width(sftd_font *font, unsigned int size, const wchar_t *text);

/**
* @brief Draws text using a font. The text will wrap after the pixels specified in lineWidth.
* @param font the font to use
Expand Down Expand Up @@ -149,9 +159,6 @@ void sftd_calc_bounding_box(int *boundingWidth, int *boundingHeight, sftd_font *
*/
void sftd_draw_textf_wrap(sftd_font *font, int x, int y, unsigned int color, unsigned int size, unsigned int lineWidth, const char *text, ...);

// (ctruLua addition) Based on sftd_draw_wtext, returns the width of the text drawn.
int sftd_width_wtext(sftd_font *font, unsigned int size, const wchar_t *text);

#ifdef __cplusplus
}
#endif
Expand Down
134 changes: 74 additions & 60 deletions libs/sftdlib/libsftd/source/sftd.c
Expand Up @@ -227,6 +227,13 @@ void sftd_draw_text(sftd_font *font, int x, int y, unsigned int color, unsigned
FT_ULong flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL;

while (*text) {
if(*text == '\n') {
pen_x = x;
pen_y += size;
text++;
continue;
}

glyph_index = FTC_CMapCache_Lookup(font->cmapcache, (FTC_FaceID)font, charmap_index, *text);

if (use_kerning && previous && glyph_index) {
Expand Down Expand Up @@ -304,6 +311,13 @@ void sftd_draw_wtext(sftd_font *font, int x, int y, unsigned int color, unsigned
FT_ULong flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL;

while (*text) {
if(*text == '\n') {
pen_x = x;
pen_y += size;
text++;
continue;
}

glyph_index = FTC_CMapCache_Lookup(font->cmapcache, (FTC_FaceID)font, charmap_index, *text);

if (use_kerning && previous && glyph_index) {
Expand Down Expand Up @@ -417,6 +431,66 @@ int sftd_get_text_width(sftd_font *font, unsigned int size, const char *text)
return pen_x;
}

int sftd_get_wtext_width(sftd_font *font, unsigned int size, const wchar_t *text)
{
FTC_FaceID face_id = (FTC_FaceID)font;
FT_Face face;
FTC_Manager_LookupFace(font->ftcmanager, face_id, &face);

FT_Int charmap_index;
charmap_index = FT_Get_Charmap_Index(face->charmap);

FT_Glyph glyph;
FT_Bool use_kerning = FT_HAS_KERNING(face);
FT_UInt glyph_index, previous = 0;
int pen_x = 0;
int pen_y = size;

FTC_ScalerRec scaler;
scaler.face_id = face_id;
scaler.width = size;
scaler.height = size;
scaler.pixel = 1;

FT_ULong flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL;

while (*text) {
glyph_index = FTC_CMapCache_Lookup(font->cmapcache, (FTC_FaceID)font, charmap_index, *text);

if (use_kerning && previous && glyph_index) {
FT_Vector delta;
FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
pen_x += delta.x >> 6;
}

if (!texture_atlas_exists(font->tex_atlas, glyph_index)) {
FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL);

if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph, size)) {
continue;
}
}

bp2d_rectangle rect;
int bitmap_left, bitmap_top;
int advance_x, advance_y;
int glyph_size;

texture_atlas_get(font->tex_atlas, glyph_index,
&rect, &bitmap_left, &bitmap_top,
&advance_x, &advance_y, &glyph_size);

const float draw_scale = size/(float)glyph_size;

pen_x += (advance_x >> 16) * draw_scale;
pen_y += (advance_y >> 16) * draw_scale;

previous = glyph_index;
text++;
}
return pen_x;
}

void sftd_draw_text_wrap(sftd_font *font, int x, int y, unsigned int color, unsigned int size, unsigned int lineWidth, const char *text)
{
FTC_FaceID face_id = (FTC_FaceID)font;
Expand Down Expand Up @@ -610,63 +684,3 @@ void sftd_draw_textf_wrap(sftd_font *font, int x, int y, unsigned int color, uns
sftd_draw_text_wrap(font, x, y, color, size, lineWidth, buffer);
va_end(args);
}

// (ctruLua addition) Based on sftd_draw_wtext, returns the width of the text drawn.
int sftd_width_wtext(sftd_font *font, unsigned int size, const wchar_t *text)
{
FTC_FaceID face_id = (FTC_FaceID)font;
FT_Face face;
FTC_Manager_LookupFace(font->ftcmanager, face_id, &face);

FT_Int charmap_index;
charmap_index = FT_Get_Charmap_Index(face->charmap);

FT_Glyph glyph;
FT_Bool use_kerning = FT_HAS_KERNING(face);
FT_UInt glyph_index, previous = 0;
int pen_x = 0;

FTC_ScalerRec scaler;
scaler.face_id = face_id;
scaler.width = size;
scaler.height = size;
scaler.pixel = 1;

FT_ULong flags = FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL;

while (*text) {
glyph_index = FTC_CMapCache_Lookup(font->cmapcache, (FTC_FaceID)font, charmap_index, *text);

if (use_kerning && previous && glyph_index) {
FT_Vector delta;
FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
pen_x += delta.x >> 6;
}

if (!texture_atlas_exists(font->tex_atlas, glyph_index)) {
FTC_ImageCache_LookupScaler(font->imagecache, &scaler, flags, glyph_index, &glyph, NULL);

if (!atlas_add_glyph(font->tex_atlas, glyph_index, (FT_BitmapGlyph)glyph, size)) {
continue;
}
}

bp2d_rectangle rect;
int bitmap_left, bitmap_top;
int advance_x, advance_y;
int glyph_size;

texture_atlas_get(font->tex_atlas, glyph_index,
&rect, &bitmap_left, &bitmap_top,
&advance_x, &advance_y, &glyph_size);

const float draw_scale = size/(float)glyph_size;

pen_x += (advance_x >> 16) * draw_scale;

previous = glyph_index;
text++;
}

return pen_x;
}
1 change: 1 addition & 0 deletions libs/sftdlib/libsftd/source/texture_atlas.c
Expand Up @@ -15,6 +15,7 @@ texture_atlas *texture_atlas_create(int width, int height, sf2d_texfmt format, s
rect.h = height;

atlas->tex = sf2d_create_texture(width, height, format, place);
sf2d_texture_set_params(atlas->tex, GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR));
sf2d_texture_tile32(atlas->tex);

atlas->bp_root = bp2d_create(&rect);
Expand Down
2 changes: 1 addition & 1 deletion source/font.c
Expand Up @@ -95,7 +95,7 @@ static int font_object_width(lua_State *L) {
len = mbstowcs(wtext, text, len);
*(wtext+len) = 0x0; // text end

lua_pushinteger(L, sftd_width_wtext(font->font, size, wtext));
lua_pushinteger(L, sftd_get_wtext_width(font->font, size, wtext));

return 1;
}
Expand Down

1 comment on commit b798818

@Ihamfp
Copy link
Member

@Ihamfp Ihamfp commented on b798818 Apr 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to update the ctrulib, or fs.list() will crash in the sdmc.

Please sign in to comment.