Exemple #1
0
void TTFont::constructor(const char *filename, float size, bool smoothing)
{
	face_ = NULL;

	G_FILE* fis = g_fopen(filename, "rb");
	if (fis == NULL)
	{
		throw GiderosException(GStatus(6000, filename));		// Error #6000: %s: No such file or directory.
		return;
	}

	memset(&stream_, 0, sizeof(stream_));

	g_fseek(fis, 0, SEEK_END);
	stream_.size = g_ftell(fis);
	g_fseek(fis, 0, SEEK_SET);
	stream_.descriptor.pointer = fis;
	stream_.read = read;
	stream_.close = close;

	FT_Open_Args args;
	memset(&args, 0, sizeof(args));
	args.flags = FT_OPEN_STREAM;
	args.stream = &stream_;

	if (FT_Open_Face(FT_Library_Singleton::instance(), &args, 0, &face_))
		throw GiderosException(GStatus(6012, filename));		// Error #6012: %s: Error while reading font file.

    float scalex = application_->getLogicalScaleX();
    float scaley = application_->getLogicalScaleY();

	const int RESOLUTION = 72;
	if (FT_Set_Char_Size(face_, 0L, (int)floor(size * 64 + 0.5f), (int)floor(RESOLUTION * scalex + 0.5f), (int)floor(RESOLUTION * scaley + 0.5f)))
	{
		FT_Done_Face(face_);
		face_ = NULL;
        throw GiderosException(GStatus(6017, filename));		// Error #6017: Invalid font size.
	}

	ascender_ = face_->size->metrics.ascender >> 6;
	height_ = face_->size->metrics.height >> 6;

    smoothing_ = smoothing;
}
Exemple #2
0
int Font::getTextureGlyphsFormat(const char *file)
{
    G_FILE *fis = g_fopen(file, "rt");

    if (!fis)
        throw GiderosException(GStatus(6000, file));	// Error #6000: %s: No such file or directory.

    bool old = (g_fgetc(fis) == '0');

    g_fclose(fis);

    return old ? 0 : 1;
}
Exemple #3
0
void Font::readTextureGlyphsOld(const char* file)
{
	G_FILE* fis = g_fopen(file, "rt");

	if (!fis)
		throw GiderosException(GStatus(6000, file));	// Error #6000: %s: No such file or directory.

    fontInfo_.textureGlyphs.clear();

	int doMipmaps_;
	g_fscanf(fis, "%d", &doMipmaps_);
	while (1)
	{
		TextureGlyph glyph;
		int chr;
		g_fscanf(fis, "%d", &chr);

		if (g_feof(fis))
			break;

		glyph.chr = chr;
		g_fscanf(fis, "%d %d", &glyph.x, &glyph.y);
		g_fscanf(fis, "%d %d", &glyph.width, &glyph.height);
		g_fscanf(fis, "%d %d", &glyph.left, &glyph.top);
		g_fscanf(fis, "%d %d", &glyph.advancex, &glyph.advancey);

        fontInfo_.textureGlyphs[chr] = glyph;
	}

	g_fclose(fis);

    fontInfo_.kernings.clear();
    fontInfo_.isSetTextColorAvailable = true;


    // mimic height & ascender
    int ascender = 0;
    int descender = 0;
    std::map<wchar32_t, TextureGlyph>::iterator iter, e = fontInfo_.textureGlyphs.end();
    for (iter = fontInfo_.textureGlyphs.begin(); iter != e; ++iter)
    {
        ascender = std::max(ascender, iter->second.top);
        descender = std::max(descender, iter->second.height - iter->second.top);
    }

    fontInfo_.height = (ascender + descender) * 1.25;
    fontInfo_.ascender = ascender * 1.25;
}
Exemple #4
0
void TexturePack::readTextureList(const char* texturelistfile,
								  std::vector<Rect>& textures_,
								  std::map<std::string, int>& filenameMap_,
								  int* pwidth, int* pheight)
{
	G_FILE* fis = g_fopen(texturelistfile, "rt");

	if (fis == NULL)
	{
		throw GiderosException(GStatus(6000, texturelistfile));		// Error #6000: %s: No such file or directory.
		return;
	}

	textures_.clear();
	filenameMap_.clear();

	int width = 0;
	int height = 0;

	char line[1024];

	while (true)
	{
		line[0] = line[1023] = 0;
		if (g_fgets(line, 1024, fis) == NULL)
			break;

		char* c;
		if ((c = strrchr(line, 0xa)))
			*c = '\0';
		if ((c = strrchr(line, 0xd)))
			*c = '\0';

		if (line[0] == '\0')
			break;

		std::vector<std::string> result;
		pystring::split(line, result, ",");

		for (std::size_t i = 0; i < result.size(); ++i)
		{
			if (result[i].empty() == false)
				result[i] = pystring::strip(result[i]);
		}

		if (result.size() >= 9)
		{
			Rect rect;

			rect.x = atoi(result[1].c_str());
			rect.y = atoi(result[2].c_str());
			rect.width = atoi(result[3].c_str());
			rect.height = atoi(result[4].c_str());
			rect.dx1 = atoi(result[5].c_str());
			rect.dy1 = atoi(result[6].c_str());
			rect.dx2 = atoi(result[7].c_str());
			rect.dy2 = atoi(result[8].c_str());

			filenameMap_[result[0]] = textures_.size();
			textures_.push_back(rect);

			width += rect.width + rect.dx1 + rect.dx2;
			height += rect.height + rect.dy1 + rect.dy2;
		}
	}

	g_fclose(fis);

	if (pwidth)
		*pwidth = width;
	if (pheight)
		*pheight = height;

	if (textures_.empty() == true)
	{
		throw GiderosException(GStatus(6008, texturelistfile));		// Error #6008: %s: File does not contain texture region information.
		return;
	}
}
Exemple #5
0
void Font::readTextureGlyphsNew(const char *file)
{
    G_FILE *fis = g_fopen(file, "rt");

    if (!fis)
        throw GiderosException(GStatus(6000, file));	// Error #6000: %s: No such file or directory.

    std::string line;
    while (readLine(fis, &line))
    {
        if (startsWith(line, "common"))
        {
            if (!getArg(line, "lineHeight", &fontInfo_.height))
                goto error;
            if (!getArg(line, "base", &fontInfo_.ascender))
                goto error;

            fontInfo_.isSetTextColorAvailable = false;
            int alphaChnl, redChnl, greenChnl, blueChnl;
            if (getArg(line, "alphaChnl", &alphaChnl) &&
                getArg(line, "redChnl",   &redChnl)   &&
                getArg(line, "greenChnl", &greenChnl) &&
                getArg(line, "blueChnl",  &blueChnl))
            {
                if (alphaChnl == 0 && redChnl == 4 && greenChnl == 4 && blueChnl == 4)
                {
                    fontInfo_.isSetTextColorAvailable = true;
                }
            }
        }
        else if (startsWith(line, "char"))
        {
            TextureGlyph textureGlyph;

            if (!getArg(line, "id", &textureGlyph.chr))
                goto error;
            if (!getArg(line, "x", &textureGlyph.x))
                goto error;
            if (!getArg(line, "y", &textureGlyph.y))
                goto error;
            if (!getArg(line, "width", &textureGlyph.width))
                goto error;
            if (!getArg(line, "height", &textureGlyph.height))
                goto error;
            if (!getArg(line, "xoffset", &textureGlyph.left))
                goto error;
            if (!getArg(line, "yoffset", &textureGlyph.top))
                goto error;
            if (!getArg(line, "xadvance", &textureGlyph.advancex))
                goto error;

            textureGlyph.top = fontInfo_.ascender - textureGlyph.top;
            textureGlyph.advancex <<= 6;

            fontInfo_.textureGlyphs[textureGlyph.chr] = textureGlyph;
        }
        else if (startsWith(line, "kerning"))
        {
            wchar32_t first, second;
            int amount;

            if (!getArg(line, "first", &first))
                goto error;
            if (!getArg(line, "second", &second))
                goto error;
            if (!getArg(line, "amount", &amount))
                goto error;

            amount <<= 6;

            fontInfo_.kernings[std::make_pair(first, second)] = amount;
        }
    }

    g_fclose(fis);
    return;

error:
    g_fclose(fis);
    throw GiderosException(GStatus(6016, file));    // Error #6016: %s: Error while reading FNT file.
}