]> code.delx.au - refind/blob - libeg/text.c
Version 0.6.6 release.
[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/global.h"
39
40 #include "egemb_font.h"
41 #define FONT_NUM_CHARS 96
42
43 static EG_IMAGE *BaseFontImage = NULL;
44 static EG_IMAGE *DarkFontImage = NULL;
45 static EG_IMAGE *LightFontImage = NULL;
46
47 static UINTN FontCellWidth = 7;
48
49 //
50 // Text rendering
51 //
52
53 static VOID egPrepareFont() {
54 if (BaseFontImage == NULL) {
55 BaseFontImage = egPrepareEmbeddedImage(&egemb_font, TRUE);
56 }
57 if (BaseFontImage != NULL)
58 FontCellWidth = BaseFontImage->Width / FONT_NUM_CHARS;
59 } // VOID egPrepareFont();
60
61 UINTN egGetFontHeight(VOID) {
62 egPrepareFont();
63 return BaseFontImage->Height;
64 } // UINTN egGetFontHeight()
65
66 UINTN egGetFontCellWidth(VOID) {
67 return FontCellWidth;
68 }
69
70 UINTN egComputeTextWidth(IN CHAR16 *Text) {
71 UINTN Width = 0;
72
73 egPrepareFont();
74 if (Text != NULL)
75 Width = FontCellWidth * StrLen(Text);
76 return Width;
77 } // UINTN egComputeTextWidth()
78
79 VOID egMeasureText(IN CHAR16 *Text, OUT UINTN *Width, OUT UINTN *Height)
80 {
81 egPrepareFont();
82
83 if (Width != NULL)
84 *Width = StrLen(Text) * FontCellWidth;
85 if (Height != NULL)
86 *Height = BaseFontImage->Height;
87 }
88
89 VOID egRenderText(IN CHAR16 *Text, IN OUT EG_IMAGE *CompImage, IN UINTN PosX, IN UINTN PosY, IN UINT8 BGBrightness)
90 {
91 EG_IMAGE *FontImage;
92 EG_PIXEL *BufferPtr;
93 EG_PIXEL *FontPixelData;
94 UINTN BufferLineOffset, FontLineOffset;
95 UINTN TextLength;
96 UINTN i, c;
97
98 egPrepareFont();
99
100 // clip the text
101 if (Text)
102 TextLength = StrLen(Text);
103 else
104 TextLength = 0;
105
106 if (TextLength * FontCellWidth + PosX > CompImage->Width)
107 TextLength = (CompImage->Width - PosX) / FontCellWidth;
108
109 if (BGBrightness < 128) {
110 if (LightFontImage == NULL) {
111 LightFontImage = egCopyImage(BaseFontImage);
112 if (LightFontImage == NULL)
113 return;
114 for (i = 0; i < (LightFontImage->Width * LightFontImage->Height); i++) {
115 LightFontImage->PixelData[i].r = 255 - LightFontImage->PixelData[i].r;
116 LightFontImage->PixelData[i].g = 255 - LightFontImage->PixelData[i].g;
117 LightFontImage->PixelData[i].b = 255 - LightFontImage->PixelData[i].b;
118 } // for
119 } // if
120 FontImage = LightFontImage;
121 } else {
122 if (DarkFontImage == NULL)
123 DarkFontImage = egCopyImage(BaseFontImage);
124 if (DarkFontImage == NULL)
125 return;
126 FontImage = DarkFontImage;
127 } // if/else
128
129 // render it
130 BufferPtr = CompImage->PixelData;
131 BufferLineOffset = CompImage->Width;
132 BufferPtr += PosX + PosY * BufferLineOffset;
133 FontPixelData = FontImage->PixelData;
134 FontLineOffset = FontImage->Width;
135 for (i = 0; i < TextLength; i++) {
136 c = Text[i];
137 if (c < 32 || c >= 127)
138 c = 95;
139 else
140 c -= 32;
141 egRawCompose(BufferPtr, FontPixelData + c * FontCellWidth,
142 FontCellWidth, FontImage->Height,
143 BufferLineOffset, FontLineOffset);
144 BufferPtr += FontCellWidth;
145 }
146 }
147
148 // Load a font bitmap from the specified file
149 VOID egLoadFont(IN CHAR16 *Filename) {
150 if (BaseFontImage)
151 egFreeImage(BaseFontImage);
152
153 BaseFontImage = egLoadImage(SelfDir, Filename, TRUE);
154 if (BaseFontImage == NULL)
155 Print(L"Note: Font image file %s is invalid! Using default font!\n");
156 egPrepareFont();
157 } // BOOLEAN egLoadFont()
158
159 /* EOF */