Beispiel #1
0
/**
 * Loads a file into memory
 */
static int load_mem_file(const char *filename, file_mem& fm)
{
   int         retval = -1;
   struct stat my_stat;
   FILE        *p_file;

   fm.raw.clear();
   fm.data.clear();
   fm.enc = ENC_ASCII;

   /* Grab the stat info for the file */
   if (stat(filename, &my_stat) < 0)
   {
      return(-1);
   }

#ifdef HAVE_UTIME_H
   /* Save off mtime */
   fm.utb.modtime = my_stat.st_mtime;
#endif

   /* Try to read in the file */
   p_file = fopen(filename, "rb");
   if (p_file == NULL)
   {
      return(-1);
   }

   fm.raw.resize(my_stat.st_size);
   if (my_stat.st_size == 0)
   {
      /* Empty file */
      retval = 0;
      fm.bom = false;
      fm.enc = ENC_ASCII;
      fm.data.clear();
   }
   else
   {
      /* read the raw data */
      if (fread(&fm.raw[0], fm.raw.size(), 1, p_file) != 1)
      {
         LOG_FMT(LERR, "%s: fread(%s) failed: %s (%d)\n",
                 __func__, filename, strerror(errno), errno);
         cpd.error_count++;
      }
      else if (!decode_unicode(fm.raw, fm.data, fm.enc, fm.bom))
      {
         LOG_FMT(LERR, "%s: failed to decode the file '%s'\n", __func__, filename);
      }
      else
      {
         LOG_FMT(LNOTE, "%s: '%s' encoding looks like %d\n", __func__, filename, fm.enc);
         retval = 0;
      }
   }
   fclose(p_file);
   return(retval);
}
Beispiel #2
0
static bool read_stdin(file_mem& fm)
{
   deque<UINT8> dq;
   char         buf[4096];
   int          len;
   int          idx;

   fm.raw.clear();
   fm.data.clear();
   fm.enc = ENC_ASCII;

   while (!feof(stdin))
   {
      len = fread(buf, 1, sizeof(buf), stdin);
      for (idx = 0; idx < len; idx++)
      {
         dq.push_back(buf[idx]);
      }
   }

   /* Copy the raw data from the deque to the vector */
   fm.raw.insert(fm.raw.end(), dq.begin(), dq.end());
   return(decode_unicode(fm.raw, fm.data, fm.enc, fm.bom));
}
Beispiel #3
0
qword_t *fontstudio_print_string(qword_t *q, int context, const unsigned char *str, int alignment, vertex_t *v0, color_t *c0, fsfont_t *font)
{

	int i = 0,j;

	unsigned short curchar = 0;

	int length;

	vertex_t v_pos = *v0;

	unsigned short utf8[2048];

	memset(utf8,0,sizeof(short)*2048);

	// Decodes the encoded string into unicode numbers U+xxxx
	// length is the number of characters in the string
	length = decode_unicode(str, utf8);

	//for (i = 0; i < length; i++)
	//{
	//	printf("utf8[%d] = %d\n", i, utf8[i]);
	//}

	// Converts the unicode numbers into the index numbers
	// used by the FontStudio ini
	convert_to_index(utf8,length,font);

	//for (i = 0; i < length; i++)
	//{
	//	printf("utf8[%d] = %d\n", i, utf8[i]);
	//}

	float line_num[100];
	int line = 0;

	float x_orig[100];
	x_orig[0] = 0;

	// line_num is used to keep track of number of characters per line
	line_num[0] = 0;

	switch (alignment)
	{
		default:
		case LEFT_ALIGN:
		{
			for (i = 0; i < length; i++)
			{

				while (utf8[i] == TAB || utf8[i] == NEWLINE)
				{
					if (utf8[i] == NEWLINE)
					{
						x_orig[line] = v_pos.x;
						line++;
						line_num[line] = 0;
					}
					i++;
				}

				if (i == length-1)
				{
					x_orig[line] = v_pos.x;
					line++;
				}

			}
			break;

		}
		case RIGHT_ALIGN:
		{
			for (i = 0; i < length; i++)
			{

				while (utf8[i] == TAB || utf8[i] == NEWLINE || utf8[i] == SPACE)
				{
					if (utf8[i] == NEWLINE)
					{
						x_orig[line] = v_pos.x - line_num[line]*font->scale;
						line++;
						line_num[line] = 0;
					}
					if (utf8[i] == TAB)
					{
						line_num[line] += font->spacewidth * 4;
					}
					if (utf8[i] == SPACE)
					{
						line_num[line] += font->spacewidth;
					}
					i++;
				}

				curchar = utf8[i];
				line_num[line] += font->chardata[curchar].A + font->chardata[curchar].B + font->chardata[curchar].C;

				if (i == length-1)
				{
					x_orig[line] = v_pos.x - line_num[line]*font->scale;
					line++;
				}

			}
			break;

		}
		case CENTER_ALIGN:
		{
			for (i = 0; i < length; i++)
			{

				while (utf8[i] == TAB || utf8[i] == NEWLINE || utf8[i] == SPACE)
				{
					if (utf8[i] == NEWLINE)
					{
						x_orig[line] = v_pos.x - (line_num[line]*font->scale)/2.0f;
						line++;
						line_num[line] = 0;
					}
					if (utf8[i] == TAB)
					{
						line_num[line] += font->spacewidth * 4;
					}
					if (utf8[i] == SPACE)
					{
						line_num[line] += font->spacewidth;
					}
					i++;
				}

				curchar = utf8[i];
				line_num[line] += font->chardata[curchar].A + font->chardata[curchar].B + font->chardata[curchar].C;

				if (i == length-1)
				{
					x_orig[line] = v_pos.x - (line_num[line]*font->scale)/2.0f;
					line++;
				}

			}
			break;

		}

	};

	line = 0;
	v_pos.x = x_orig[0];

	q = draw_prim_start(q,context,&charprim,c0);

	for (j = 0; j < length; j++)
	{

		while(utf8[j] == NEWLINE || utf8[j] == TAB || utf8[j] == SPACE)
		{
			if (utf8[j] == NEWLINE)
			{
				line++;
				v_pos.y += font->height*font->scale;
				v_pos.x = x_orig[line];
			}
			if (utf8[j] == TAB)
			{
				v_pos.x += font->spacewidth*font->scale * 4.0f;
			}
			if (utf8[j] == SPACE)
			{
				v_pos.x += font->spacewidth*font->scale;
			}
			j++;
		}

		v_pos.x += (font->chardata[utf8[j]].A*font->scale);

		q = draw_fontstudio_char(q,utf8[j],&v_pos,font);

		v_pos.x += (font->chardata[utf8[j]].B*font->scale) + (font->chardata[utf8[j]].C*font->scale);


	}

	q = draw_prim_end(q,2,DRAW_UV_REGLIST);

	return q;
}