]>
code.delx.au - refind/blob - libeg/text.c
3 * Text drawing functions
5 * Copyright (c) 2006 Christoph Pfisterer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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.
38 #include "../refind/global.h"
40 #include "egemb_font.h"
41 #define FONT_NUM_CHARS 96
43 static EG_IMAGE
*BaseFontImage
= NULL
;
44 static EG_IMAGE
*DarkFontImage
= NULL
;
45 static EG_IMAGE
*LightFontImage
= NULL
;
47 static UINTN FontCellWidth
= 7;
53 static VOID
egPrepareFont() {
54 if (BaseFontImage
== NULL
) {
55 BaseFontImage
= egPrepareEmbeddedImage(&egemb_font
, TRUE
);
57 if (BaseFontImage
!= NULL
)
58 FontCellWidth
= BaseFontImage
->Width
/ FONT_NUM_CHARS
;
59 } // VOID egPrepareFont();
61 UINTN
egGetFontHeight(VOID
) {
63 return BaseFontImage
->Height
;
64 } // UINTN egGetFontHeight()
66 UINTN
egGetFontCellWidth(VOID
) {
70 UINTN
egComputeTextWidth(IN CHAR16
*Text
) {
75 Width
= FontCellWidth
* StrLen(Text
);
77 } // UINTN egComputeTextWidth()
79 VOID
egMeasureText(IN CHAR16
*Text
, OUT UINTN
*Width
, OUT UINTN
*Height
)
84 *Width
= StrLen(Text
) * FontCellWidth
;
86 *Height
= BaseFontImage
->Height
;
89 VOID
egRenderText(IN CHAR16
*Text
, IN OUT EG_IMAGE
*CompImage
, IN UINTN PosX
, IN UINTN PosY
, IN UINT8 BGBrightness
)
93 EG_PIXEL
*FontPixelData
;
94 UINTN BufferLineOffset
, FontLineOffset
;
102 TextLength
= StrLen(Text
);
106 if (TextLength
* FontCellWidth
+ PosX
> CompImage
->Width
)
107 TextLength
= (CompImage
->Width
- PosX
) / FontCellWidth
;
109 if (BGBrightness
< 128) {
110 if (LightFontImage
== NULL
) {
111 LightFontImage
= egCopyImage(BaseFontImage
);
112 if (LightFontImage
== NULL
)
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
;
120 FontImage
= LightFontImage
;
122 if (DarkFontImage
== NULL
)
123 DarkFontImage
= egCopyImage(BaseFontImage
);
124 if (DarkFontImage
== NULL
)
126 FontImage
= DarkFontImage
;
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
++) {
137 if (c
< 32 || c
>= 127)
141 egRawCompose(BufferPtr
, FontPixelData
+ c
* FontCellWidth
,
142 FontCellWidth
, FontImage
->Height
,
143 BufferLineOffset
, FontLineOffset
);
144 BufferPtr
+= FontCellWidth
;
148 // Load a font bitmap from the specified file
149 VOID
egLoadFont(IN CHAR16
*Filename
) {
151 egFreeImage(BaseFontImage
);
153 BaseFontImage
= egLoadImage(SelfDir
, Filename
, TRUE
);
154 if (BaseFontImage
== NULL
)
155 Print(L
"Note: Font image file %s is invalid! Using default font!\n");
157 } // BOOLEAN egLoadFont()