Merge pull request #301 from olliwang/font
Support fallback fonts (code refactoring for #299)
This commit is contained in:
commit
c02bc9d5ed
92
example/LICENSE_OFL.txt
Normal file
92
example/LICENSE_OFL.txt
Normal file
@ -0,0 +1,92 @@
|
||||
This Font Software is licensed under the SIL Open Font License,
|
||||
Version 1.1.
|
||||
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
http://scripts.sil.org/OFL
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font
|
||||
creation efforts of academic and linguistic communities, and to
|
||||
provide a free and open framework in which fonts may be shared and
|
||||
improved in partnership with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply to
|
||||
any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software
|
||||
components as distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to,
|
||||
deleting, or substituting -- in part or in whole -- any of the
|
||||
components of the Original Version, by changing formats or by porting
|
||||
the Font Software to a new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION & CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed,
|
||||
modify, redistribute, and sell modified and unmodified copies of the
|
||||
Font Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components, in
|
||||
Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the
|
||||
corresponding Copyright Holder. This restriction only applies to the
|
||||
primary font name as presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created using
|
||||
the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
BIN
example/NotoEmoji-Regular.ttf
Normal file
BIN
example/NotoEmoji-Regular.ttf
Normal file
Binary file not shown.
@ -834,6 +834,13 @@ int loadDemoData(NVGcontext* vg, DemoData* data)
|
||||
printf("Could not add font bold.\n");
|
||||
return -1;
|
||||
}
|
||||
data->fontEmoji = nvgCreateFont(vg, "emoji", "../example/NotoEmoji-Regular.ttf");
|
||||
if (data->fontEmoji == -1) {
|
||||
printf("Could not add font emoji.\n");
|
||||
return -1;
|
||||
}
|
||||
nvgAddFallbackFontId(vg, data->fontNormal, data->fontEmoji);
|
||||
nvgAddFallbackFontId(vg, data->fontBold, data->fontEmoji);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -853,7 +860,7 @@ void drawParagraph(NVGcontext* vg, float x, float y, float width, float height,
|
||||
{
|
||||
NVGtextRow rows[3];
|
||||
NVGglyphPosition glyphs[100];
|
||||
const char* text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.";
|
||||
const char* text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.🎉";
|
||||
const char* start;
|
||||
const char* end;
|
||||
int nrows, i, nglyphs, j, lnum = 0;
|
||||
@ -1191,8 +1198,8 @@ static void flipHorizontal(unsigned char* image, int w, int h, int stride)
|
||||
{
|
||||
int i = 0, j = h-1, k;
|
||||
while (i < j) {
|
||||
unsigned char* ri = &image[i * stride];
|
||||
unsigned char* rj = &image[j * stride];
|
||||
unsigned char* ri = &image[i * stride];
|
||||
unsigned char* rj = &image[j * stride];
|
||||
for (k = 0; k < w*4; k++) {
|
||||
unsigned char t = ri[k];
|
||||
ri[k] = rj[k];
|
||||
@ -1217,4 +1224,3 @@ void saveScreenShot(int w, int h, int premult, const char* name)
|
||||
stbi_write_png(name, w, h, 4, image, w*4);
|
||||
free(image);
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
struct DemoData {
|
||||
int fontNormal, fontBold, fontIcons;
|
||||
int fontNormal, fontBold, fontIcons, fontEmoji;
|
||||
int images[12];
|
||||
};
|
||||
typedef struct DemoData DemoData;
|
||||
@ -23,4 +23,4 @@ void saveScreenShot(int w, int h, int premult, const char* name);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // DEMO_H
|
||||
#endif // DEMO_H
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 248 KiB After Width: | Height: | Size: 966 KiB |
@ -193,15 +193,17 @@ int fons__tt_buildGlyphBitmap(FONSttFontImpl *font, int glyph, float size, float
|
||||
{
|
||||
FT_Error ftError;
|
||||
FT_GlyphSlot ftGlyph;
|
||||
FT_Fixed advFixed;
|
||||
FONS_NOTUSED(scale);
|
||||
|
||||
ftError = FT_Set_Pixel_Sizes(font->font, 0, (FT_UInt)(size * (float)font->font->units_per_EM / (float)(font->font->ascender - font->font->descender)));
|
||||
if (ftError) return 0;
|
||||
ftError = FT_Load_Glyph(font->font, glyph, FT_LOAD_RENDER);
|
||||
if (ftError) return 0;
|
||||
ftError = FT_Get_Advance(font->font, glyph, FT_LOAD_NO_SCALE, (FT_Fixed*)advance);
|
||||
ftError = FT_Get_Advance(font->font, glyph, FT_LOAD_NO_SCALE, &advFixed);
|
||||
if (ftError) return 0;
|
||||
ftGlyph = font->font->glyph;
|
||||
*advance = (int)advFixed;
|
||||
*lsb = (int)ftGlyph->metrics.horiBearingX;
|
||||
*x0 = ftGlyph->bitmap_left;
|
||||
*x1 = *x0 + ftGlyph->bitmap.width;
|
||||
@ -324,6 +326,9 @@ int fons__tt_getGlyphKernAdvance(FONSttFontImpl *font, int glyph1, int glyph2)
|
||||
#ifndef FONS_MAX_STATES
|
||||
# define FONS_MAX_STATES 20
|
||||
#endif
|
||||
#ifndef FONS_MAX_FALLBACKS
|
||||
# define FONS_MAX_FALLBACKS 20
|
||||
#endif
|
||||
|
||||
static unsigned int fons__hashint(unsigned int a)
|
||||
{
|
||||
@ -371,6 +376,8 @@ struct FONSfont
|
||||
int cglyphs;
|
||||
int nglyphs;
|
||||
int lut[FONS_HASH_LUT_SIZE];
|
||||
int fallbacks[FONS_MAX_FALLBACKS];
|
||||
int nfallbacks;
|
||||
};
|
||||
typedef struct FONSfont FONSfont;
|
||||
|
||||
@ -755,6 +762,16 @@ static FONSstate* fons__getState(FONScontext* stash)
|
||||
return &stash->states[stash->nstates-1];
|
||||
}
|
||||
|
||||
int fonsAddFallbackFont(FONScontext* stash, int base, int fallback)
|
||||
{
|
||||
FONSfont* baseFont = stash->fonts[base];
|
||||
if (baseFont->nfallbacks < FONS_MAX_FALLBACKS) {
|
||||
baseFont->fallbacks[baseFont->nfallbacks++] = fallback;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fonsSetSize(FONScontext* stash, float size)
|
||||
{
|
||||
fons__getState(stash)->size = size;
|
||||
@ -1041,6 +1058,15 @@ static FONSglyph* fons__getGlyph(FONScontext* stash, FONSfont* font, unsigned in
|
||||
// Could not find glyph, create it.
|
||||
scale = fons__tt_getPixelHeightScale(&font->font, size);
|
||||
g = fons__tt_getGlyphIndex(&font->font, codepoint);
|
||||
// Try to find the glyph in fallback fonts.
|
||||
if (g == 0) {
|
||||
for (i = 0; i < font->nfallbacks; ++i) {
|
||||
FONSglyph* fallbackGlyph = fons__getGlyph(stash, stash->fonts[font->fallbacks[i]], codepoint, isize, iblur);
|
||||
if (fallbackGlyph != NULL && fallbackGlyph->index != 0) {
|
||||
return fallbackGlyph;
|
||||
}
|
||||
}
|
||||
}
|
||||
fons__tt_buildGlyphBitmap(&font->font, g, size, scale, &advance, &lsb, &x0, &y0, &x1, &y1);
|
||||
gw = x1-x0 + pad*2;
|
||||
gh = y1-y0 + pad*2;
|
||||
|
12
src/nanovg.c
12
src/nanovg.c
@ -2267,6 +2267,18 @@ int nvgFindFont(NVGcontext* ctx, const char* name)
|
||||
return fonsGetFontByName(ctx->fs, name);
|
||||
}
|
||||
|
||||
|
||||
int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont)
|
||||
{
|
||||
if(baseFont == -1 || fallbackFont == -1) return 0;
|
||||
return fonsAddFallbackFont(ctx->fs, baseFont, fallbackFont);
|
||||
}
|
||||
|
||||
int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont)
|
||||
{
|
||||
return nvgAddFallbackFontId(ctx, nvgFindFont(ctx, baseFont), nvgFindFont(ctx, fallbackFont));
|
||||
}
|
||||
|
||||
// State setting
|
||||
void nvgFontSize(NVGcontext* ctx, float size)
|
||||
{
|
||||
|
@ -546,6 +546,12 @@ int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int
|
||||
// Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
|
||||
int nvgFindFont(NVGcontext* ctx, const char* name);
|
||||
|
||||
// Adds a fallback font by handle.
|
||||
int nvgAddFallbackFontId(NVGcontext* ctx, int baseFont, int fallbackFont);
|
||||
|
||||
// Adds a fallback font by name.
|
||||
int nvgAddFallbackFont(NVGcontext* ctx, const char* baseFont, const char* fallbackFont);
|
||||
|
||||
// Sets the font size of current text style.
|
||||
void nvgFontSize(NVGcontext* ctx, float size);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user