Mercurial > hg > index.fcgi > lj > lj046
view src/fontdraw_engine.c @ 0:c84446dfb3f5
initial add
author | paulo@localhost |
---|---|
date | Fri, 13 Mar 2009 00:39:12 -0700 |
parents | |
children |
line source
1 /*
2 Variable width font drawing library for DS (and GBA)
4 Copyright 2007-2008 Damian Yerrick <pinoandchester@pineight.com>
6 This work is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any
8 damages arising from the use of this work.
10 Permission is granted to anyone to use this work for any purpose,
11 including commercial applications, and to alter it and redistribute
12 it freely, subject to the following restrictions:
14 1. The origin of this work must not be misrepresented; you must
15 not claim that you wrote the original work. If you use
16 this work in a product, an acknowledgment in the product
17 documentation would be appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must
19 not be misrepresented as being the original work.
20 3. This notice may not be removed or altered from any source
21 distribution.
23 "Source" is the preferred form of a work for making changes to it.
25 */
26 /*
28 Set FONTDRAW_SPLIT_COMPILE when compiling the time-sensitve parts
29 into a separate file to be placed in "fast" memory.
31 */
34 #ifdef FONTDRAW_SPLIT_COMPILE
35 typedef unsigned int u32;
36 extern const unsigned char vwfont_bin[];
38 typedef struct GlyphRec {
39 unsigned short dataOffset;
40 unsigned char glyphWidth;
41 unsigned char reserved;
42 } GlyphRec;
43 #endif
45 unsigned int fontdraw_putchar(u32 *dst, unsigned int colStride, int wid, int x, int glyph) {
46 glyph &= 0xFF;
47 if (glyph < vwfont_bin[1]) {
48 return 0;
49 }
50 glyph -= vwfont_bin[1];
51 if (vwfont_bin[2] != 0 && glyph >= vwfont_bin[2]) {
52 return 0;
53 }
54 const GlyphRec *glyphRec =
55 ((const GlyphRec *)(vwfont_bin + vwfont_bin[0])) + glyph;
56 const unsigned char *data = vwfont_bin + glyphRec->dataOffset;
57 unsigned int dataShift = 2;
58 unsigned int dataBits = *data++;
59 unsigned int pixelCode = dataBits & 0x03;
61 // Convert x to tile column address and bit address within tile
62 dst += colStride * (x >> 3);
63 x = (x & 0x07) << 2;
65 if (dataShift >= 8) {
66 dataShift = 2;
67 dataBits = *data++;
68 }
70 for (unsigned int height = vwfont_bin[3];
71 height > 0;
72 --height, ++dst) {
73 int eol = 0;
74 int xLine = x;
75 int widLeft = wid;
76 u32 *dstLine = dst;
77 u32 dstBits = *dstLine;
78 while (!eol) {
80 // Process a pixel instruction
81 if (pixelCode == 0) {
82 eol = 1;
83 } else if (widLeft > 0) {
85 // Change pixel and move to next pixel
86 if (pixelCode > 1) {
87 //dstBits &= ~(0x0F << xLine);
88 dstBits |= (pixelCode - 1) << xLine;
89 }
90 xLine += 4;
91 --widLeft;
92 }
94 // If finished with this tile, write back changed bits
95 if (xLine >= 32) {
96 xLine = 0;
97 *dstLine = dstBits;
98 dstLine += colStride;
99 dstBits = *dstLine;
100 }
102 // Decode next pixel instruction
103 if (dataShift >= 8) {
104 dataShift = 0;
105 dataBits = *data++;
106 }
108 // Decode a byte into pixel instructions
109 pixelCode = (dataBits >> dataShift) & 0x03;
110 dataShift += 2;
111 }
113 // Write back changed bits
114 if (xLine > 0) {
115 *dstLine = dstBits;
116 }
117 }
118 return glyphRec->glyphWidth;
119 }
121 void vwfRectfillColumn(u32 *dst, unsigned int colStride,
122 unsigned int l, unsigned int t,
123 unsigned int r, unsigned int b,
124 unsigned int c)
125 {
126 u32 mask = 0xffffffffU << (4 * l);
127 mask &= 0xffffffffU >> (4 * (8 - r));
128 c &= mask;
129 mask = ~mask;
131 for (; t < b; t++) {
132 dst[t] = (dst[t] & mask) | c;
133 }
134 }