]> code.delx.au - refind/blob - libeg/text.c
c723710411a583f9230c10070c205f29d6f5f6cf
[refind] / libeg / text.c
1 /*
2 * libeg/text.c
3 * Text drawing functions
4 *
5 * Copyright (c) 2006 Christoph Pfisterer
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the name of Christoph Pfisterer nor the names of the
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include "libegint.h"
38 //#include "../refind/screen.h"
39
40 #include "egemb_font.h"
41 #define FONT_CELL_WIDTH (7)
42 #define FONT_CELL_HEIGHT (12)
43
44 static EG_IMAGE *BlackFontImage = NULL;
45 static EG_IMAGE *WhiteFontImage = NULL;
46
47 //
48 // Text rendering
49 //
50
51 VOID egMeasureText(IN CHAR16 *Text, OUT UINTN *Width, OUT UINTN *Height)
52 {
53 if (Width != NULL)
54 *Width = StrLen(Text) * FONT_CELL_WIDTH;
55 if (Height != NULL)
56 *Height = FONT_CELL_HEIGHT;
57 }
58
59 VOID egRenderText(IN CHAR16 *Text, IN OUT EG_IMAGE *CompImage, IN UINTN PosX, IN UINTN PosY, IN UINT8 BGBrightness)
60 {
61 EG_IMAGE *FontImage;
62 EG_PIXEL *BufferPtr;
63 EG_PIXEL *FontPixelData;
64 UINTN BufferLineOffset, FontLineOffset;
65 UINTN TextLength;
66 UINTN i, c;
67
68 // clip the text
69 if (Text)
70 TextLength = StrLen(Text);
71 else
72 TextLength = 0;
73
74 if (TextLength * FONT_CELL_WIDTH + PosX > CompImage->Width)
75 TextLength = (CompImage->Width - PosX) / FONT_CELL_WIDTH;
76
77 if (BGBrightness < 128) {
78 if (WhiteFontImage == NULL) {
79 WhiteFontImage = egPrepareEmbeddedImage(&egemb_font, TRUE);
80 if (WhiteFontImage == NULL)
81 return;
82 for (i = 0; i < (WhiteFontImage->Width * WhiteFontImage->Height); i++) {
83 WhiteFontImage->PixelData[i].r = 255 - WhiteFontImage->PixelData[i].r;
84 WhiteFontImage->PixelData[i].g = 255 - WhiteFontImage->PixelData[i].g;
85 WhiteFontImage->PixelData[i].b = 255 - WhiteFontImage->PixelData[i].b;
86 // WhiteFontImage->PixelData[i].a = 255 - WhiteFontImage->PixelData[i].a;
87 } // for
88 } // if
89 FontImage = WhiteFontImage;
90 } else {
91 if (BlackFontImage == NULL)
92 BlackFontImage = egPrepareEmbeddedImage(&egemb_font, TRUE);
93 if (BlackFontImage == NULL)
94 return;
95 FontImage = BlackFontImage;
96 } // if/else
97
98 // // load the font
99 // if (FontImage == NULL) {
100 // FontImage = egPrepareEmbeddedImage(&egemb_font, TRUE);
101 // } // if font not yet loaded.
102
103 // render it
104 BufferPtr = CompImage->PixelData;
105 BufferLineOffset = CompImage->Width;
106 BufferPtr += PosX + PosY * BufferLineOffset;
107 FontPixelData = FontImage->PixelData;
108 FontLineOffset = FontImage->Width;
109 for (i = 0; i < TextLength; i++) {
110 c = Text[i];
111 if (c < 32 || c >= 127)
112 c = 95;
113 else
114 c -= 32;
115 egRawCompose(BufferPtr, FontPixelData + c * FONT_CELL_WIDTH,
116 FONT_CELL_WIDTH, FONT_CELL_HEIGHT,
117 BufferLineOffset, FontLineOffset);
118 BufferPtr += FONT_CELL_WIDTH;
119 }
120 }
121
122 /* EOF */