<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>color font</title>
		<link>http://www.allegro.cc/forums/view/334616</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 14 Feb 2004 20:35:03 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m looking at the allegro color fonts, and I&#39;d like to know how they get their colors - cause I&#39;d like to be able to change the colors.  I&#39;ve tried set_palette(default_palette) but such a global approach doesn&#39;t seem to work - therefore I think there&#39;s a palette stored somewhere in the FONT, but I don&#39;t know where, or how to access it ! I can&#39;t find anything in the allegro.h or the allegro source code which helps me further.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GeomanNL)</author>
		<pubDate>Sat, 14 Feb 2004 18:50:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>i dont think they can be changed dynamically.<br />thats not what they were designed for.</p><p>however, you could write them to a temp map, apply a palette change, then blit to yoru destination map.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (A J)</author>
		<pubDate>Sat, 14 Feb 2004 19:29:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>ok ... well I must admit I made a mistake. setting the global palette does work, but due to a mistake in one of my routines the color wasn&#39;t set to -1, which is required for this... sorry.</p><p>EDIT:</p><p>for whoever is interested, here&#39;s some code for applying to truetype font to an allegro font, without fancy stuff. It requires the truetype library. It&#39;s useful cause the allegttf library is a little outdated.</p><p>#include &lt;allegro.h&gt;<br />#include &quot;allegro/internal/aintern.h&quot;<br />#include &quot;freetype/freetype.h&quot;</p><p>#include &quot;ttf.h&quot;</p><p>// copy ttf bitmap onto an allegro bitmap<br />static void my_draw_bitmap( FT_Bitmap *src, BITMAP *dest, int dx, int dy )<br />{<br />	if (src-&gt;rows == 0 || src-&gt;width == 0)<br />		return;</p><p>	int ix, iy;</p><p>	if (src-&gt;pixel_mode == ft_pixel_mode_mono)<br />	{</p><p>		// dunno if this works - untested<br />		int ix, iy;<br />		for ( iy = 0; iy &lt; src-&gt;rows; ++iy )<br />		{<br />			unsigned char *p = src-&gt;buffer + iy * src-&gt;pitch;<br />			<br />			for ( ix = 0; ix &lt; src-&gt;width; ++ix )<br />			{<br />				unsigned char v;<br />				v = p[ix &gt;&gt; 8];<br />				<br />				unsigned char mask;<br />				mask = 1 &lt;&lt; (ix &amp; 0x0F);<br />				<br />				if (v &amp; mask)<br />					putpixel(dest, ix+dx, iy+dy, 255);<br />			}<br />		}<br />		<br />	} else if ( src-&gt;pixel_mode == ft_pixel_mode_grays )<br />	{<br />		for ( iy = 0; iy &lt; src-&gt;rows; ++iy )<br />		{<br />			unsigned char *p = src-&gt;buffer + iy * src-&gt;pitch;<br />			<br />			for ( ix = 0; ix &lt; src-&gt;width; ++ix )<br />			{<br />				unsigned char v;<br />				v = p[ix];<br />				<br />				//if (v != 0)<br />				if (v &gt; 128)	// discard too transparent pixels (disregard anti-aliasing...)<br />					putpixel(dest, ix+dx, iy+dy, 255);<br />			}<br />		}<br />		<br />	} else {<br />		return;//tw_error(&quot;unsupported bitmap format&quot;);<br />	}</p><p>}</p><p>static void cleanup (FT_Library engine, FT_Face face)<br />{<br />	if (engine)<br />		FT_Done_FreeType(engine);<br />}</p><p>FONT* load_ttf_font (const char* filename, const int points, const int smooth)<br />{</p><p>	int points_w, points_h, begin, end;<br />	begin = 32;	// space character<br />	end = 128;	// ?<br />	points_w = points;<br />	points_h = points;</p><p>	FT_Library           engine = 0;<br />	FT_Face              face = 0;<br />	<br />	<br />	FT_Error error;</p><p>	// initialize the library<br />	error = FT_Init_FreeType(&amp;engine);<br />	if(error)<br />		return 0;<br />		<br />	// load a font<br />	error = FT_New_Face(engine, filename, 0, &amp;face);<br />	if(error)<br />	{<br />		cleanup(engine, face);<br />		return 0;<br />	}</p><p>	// set font size in pixels<br />	error = FT_Set_Pixel_Sizes(face, points_w, points_h);<br />	if(error)<br />	{<br />		cleanup(engine, face);<br />		return 0;<br />	}</p><p>	// in case there&#39;s no unicode (ascii) charmap loaded by default<br />	if (!face-&gt;charmap)<br />	{<br />		if (!face-&gt;num_charmaps)	// there are no charmaps !<br />		{<br />			cleanup(engine, face);<br />			return 0;<br />		}</p><p>		error = FT_Set_Charmap(face, face-&gt;charmaps[0]);	// just pick the first one .<br />		<br />		if (error)	// shouldn&#39;t occur<br />		{<br />			cleanup(engine, face);<br />			return 0;<br />		}<br />	}</p><p>	<br />	<br />	int c;<br />	<br />	AL_CONST int num = end - begin + 1;<br />	<br />	struct FONT *f;<br />	struct FONT_COLOR_DATA *fcd;<br />	<br />	// Allocate and setup the Allegro font<br />	// (copied from allegttf)<br />	f = (struct FONT*)calloc(1,sizeof(struct FONT));<br />	fcd = (struct FONT_COLOR_DATA*)calloc(1,sizeof(struct FONT_COLOR_DATA));<br />	fcd-&gt;begin = begin;<br />	fcd-&gt;end = end;<br />	fcd-&gt;bitmaps = (BITMAP**)calloc(num,sizeof(BITMAP*));<br />	fcd-&gt;next = NULL;<br />	f-&gt;data = (void*)fcd;<br />	f-&gt;vtable = font_vtable_color;<br />	</p><p>	FT_GlyphSlot  slot = face-&gt;glyph;  // a small shortcut<br />	int           pen_x, pen_y;<br />	<br />	pen_x = 0;<br />	pen_y = 0;</p><p>	int ymin, ymax, yorigin;<br />	<br />	ymin = face-&gt;bbox.yMin;		// the bounding box for all chars in this font.<br />	ymax = face-&gt;bbox.yMax;		// this actually defines the origin position...<br />	yorigin = points_h * double(ymax) / double(ymax - ymin);<br />	<br />	//char c;<br />	for ( c = begin; c &lt;= end; ++c )<br />	{<br />		// load glyph image into the slot (erase previous one)<br />		<br />		error = FT_Load_Char( face, c, FT_LOAD_RENDER );<br />		if (error)<br />			continue;<br />		<br />		// now, draw to our target surface<br />		BITMAP *bmp = 0;<br />		int w, h;</p><p>		w = slot-&gt;metrics.horiAdvance / 64;<br />		if (!w)<br />			w = 1;</p><p>		h = points_h;<br />		<br />		int dx, dy;</p><p>		dx = slot-&gt;bitmap_left;<br />		dy = yorigin - slot-&gt;bitmap_top;</p><p>		if (w &amp;&amp; h)<br />		{<br />			bmp = create_bitmap_ex(8, w, h);<br />			clear_to_color(bmp, 0);<br />			<br />			my_draw_bitmap( &amp;slot-&gt;bitmap, bmp, dx, dy );<br />		}</p><p>		fcd-&gt;bitmaps[c-begin] = bmp;<br />	}</p><p>	<br />	// set the font height<br />	f-&gt;height = points_h;<br />	<br />	// clean up the font stuff<br />	cleanup(engine, face);<br />	<br />	return f;<br />   }
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GeomanNL)</author>
		<pubDate>Sat, 14 Feb 2004 20:35:03 +0000</pubDate>
	</item>
</rss>
