Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Modified 2025-2026 by Olcay Seygan for Zero Hour Reforged; see the git history.
// Modified 2026 by İlyas Akın for the macOS/Linux port; see NOTICE.md and the git history.
/***********************************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***********************************************************************************************
* *
* Project Name : WW3D *
* *
* $Archive:: /Commando/Code/ww3d2/render2dsentence.cpp $*
* *
* $Author:: Patrick $*
* *
* $Modtime:: 8/29/01 11:16a $*
* *
* $Revision:: 13 $*
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "render2dsentence.h"
#if !defined(_WIN32)
#include "glyphrasteriser.h"
#include <math.h>
#endif
#include "surfaceclass.h"
#include "texture.h"
#include "wwprofile.h"
#include "wwmemlog.h"
#include "dx8wrapper.h"
#include <math.h>
#ifdef _INTERNAL
// for occasional debugging...
//#pragma optimize("", off)
//#pragma message("************************************** WARNING, optimization disabled for debugging purposes")
#endif
////////////////////////////////////////////////////////////////////////////////////
// Local constants
////////////////////////////////////////////////////////////////////////////////////
#define no_TEST_PLACEMENT 1 // Shows alignment markers for text.
#define TEXTURE_OFFSET 2
////////////////////////////////////////////////////////////////////////////////////
//
// Render2DSentenceClass
//
////////////////////////////////////////////////////////////////////////////////////
Render2DSentenceClass::Render2DSentenceClass (void) :
Font (NULL),
Location (0.0F,0.0F),
Cursor (0.0F,0.0F),
TextureOffset (0, 0),
TextureStartX (0),
CurSurface (NULL),
CurrTextureSize (0),
MonoSpaced (false),
IsClippedEnabled (false),
ClipRect (0, 0, 0, 0),
BaseLocation (0, 0),
LockedPtr (NULL),
LockedStride (0),
TextureSizeHint (0),
WrapWidth (0),
Centered (false),
DrawExtents (0, 0, 0, 0),
ParseHotKey( false ),
useHardWordWrap( false)
{
Shader = Render2DClass::Get_Default_Shader ();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// ~Render2DSentenceClass
//
////////////////////////////////////////////////////////////////////////////////////
Render2DSentenceClass::~Render2DSentenceClass (void)
{
REF_PTR_RELEASE (Font);
Reset ();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Set_Font
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Set_Font (FontCharsClass *font)
{
Reset ();
REF_PTR_SET (Font, font);
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Reset_Polys
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Reset_Polys (void)
{
for (int index = 0; index < Renderers.Count (); index ++) {
Renderers[index].Renderer->Reset ();
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Reset
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Reset (void)
{
Reset_Keeping_Textures ();
Release_Spare_Textures ();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Reset_Keeping_Textures
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Reset_Keeping_Textures (void)
{
//
// Make sure we unlock the current surface (if necessary)
//
if (LockedPtr != NULL) {
CurSurface->Unlock ();
LockedPtr = NULL;
}
//
// Release our hold on the current surface
//
REF_PTR_RELEASE (CurSurface);
//
// Free each renderer, keeping its texture. Text set again (a clock, a counter, the corner
// readout's frame number every logic frame) mostly wants textures of the same size, and
// Build_Textures fills one of these rather than making a new one: a made one was a Direct3D 9
// texture and, on the Direct3D 11 frame, a new copy of it, most of what drawing a string cost.
// Each surface has its own renderer and texture, so none is kept twice.
//
while (Renderers.Count () > 0) {
TextureClass *texture = Renderers[0].Renderer->Peek_Texture ();
if (texture != NULL) {
texture->Add_Ref ();
SpareTextures.Add (texture);
}
delete Renderers[0].Renderer;
Renderers.Delete(0);
}
Cursor.Set (0, 0);
MonoSpaced = false;
ParseHotKey = false;
Release_Pending_Surfaces ();
Reset_Sentence_Data ();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Make_Additive
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Make_Additive (void)
{
Shader.Set_Dst_Blend_Func (ShaderClass::DSTBLEND_ONE);
Shader.Set_Src_Blend_Func (ShaderClass::SRCBLEND_ONE);
Shader.Set_Primary_Gradient (ShaderClass::GRADIENT_MODULATE);
Shader.Set_Secondary_Gradient (ShaderClass::SECONDARY_GRADIENT_DISABLE);
Set_Shader (Shader);
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Make_Additive
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Set_Shader (ShaderClass shader)
{
Shader = shader;
//
// Change each renderer's shader
//
for (int i = 0; i < Renderers.Count (); i ++) {
ShaderClass *curr_shader = Renderers[i].Renderer->Get_Shader ();
(*curr_shader) = Shader;
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Render
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Render (void)
{
//
// Build any textures that are pending
//
Build_Textures ();
//
// Ask each renderer to draw its contents
//
for (int i = 0; i < Renderers.Count (); i ++) {
Renderers[i].Renderer->Render ();
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Set_Base_Location
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Set_Base_Location (const Vector2 &loc)
{
Vector2 dif = loc - BaseLocation;
BaseLocation = loc;
for (int i = 0; i < Renderers.Count (); i ++) {
Renderers[i].Renderer->Move (dif);
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Set_Location
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Set_Location (const Vector2 &loc)
{
Location = loc;
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Get_Text_Extents
//
////////////////////////////////////////////////////////////////////////////////////
Vector2
Render2DSentenceClass::Get_Text_Extents (const WideChar *text)
{
Vector2 extent (0, Font->Get_Char_Height());
while (*text) {
WideChar ch = *text++;
if ( ch != (WideChar)'\n' ) {
extent.X += Font->Get_Char_Spacing( ch );
}
}
return extent;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Get_Formatted_Text_Extents
//
////////////////////////////////////////////////////////////////////////////////////
Vector2
Render2DSentenceClass::Get_Formatted_Text_Extents (const WideChar *text)
{
return Build_Sentence_Not_Centered(text, NULL, NULL, true);
}
////////////////////////////////////////////////////////////////////////////////////
//
// Reset_Sentence_Data
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Reset_Sentence_Data (void)
{
//
// Release our hold on each texture used in the sentence
//
for (int index = 0; index < SentenceData.Count (); index ++) {
REF_PTR_RELEASE (SentenceData[index].Surface);
}
if (SentenceData.Count()>0) {
SentenceData.Delete_All ();
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Release_Pending_Surfaces
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Release_Pending_Surfaces (void)
{
//
// Release our hold on each pending surface
//
for (int index = 0; index < PendingSurfaces.Count (); index ++) {
SurfaceClass *curr_surface = PendingSurfaces[index].Surface;
REF_PTR_RELEASE (curr_surface);
}
if (PendingSurfaces.Count()>0) PendingSurfaces.Delete_All ();
return;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Build_Textures
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Build_Textures (void)
{
WWMEMLOG(MEM_TEXTURE);
//
// Make sure we unlock the current surface
//
if (LockedPtr != NULL) {
CurSurface->Unlock ();
LockedPtr = NULL;
}
//
// Release our hold on the current surface
//
REF_PTR_RELEASE (CurSurface);
TextureOffset.Set (0, 0);
TextureStartX = 0;
//
// Convert all pending surfaces to textures
//
for (int index = 0; index < PendingSurfaces.Count (); index ++) {
PendingSurfaceStruct &surface_info = PendingSurfaces[index];
SurfaceClass *curr_surface = surface_info.Surface;
//
// Get the dimensions of the surface
//
SurfaceClass::SurfaceDescription desc;
curr_surface->Get_Description (desc);
//
// A texture the sentence's last text left (Reset) when one is this size, or a new one. The
// copy below writes the whole of it, so nothing of the old text survives.
//
TextureClass *new_texture = NULL;
for (int spare = 0; spare < SpareTextures.Count () && new_texture == NULL; spare ++) {
if (SpareTextures[spare]->Get_Width () == (int)desc.Width) {
new_texture = SpareTextures[spare];
SpareTextures.Delete (spare);
}
}
if (new_texture == NULL) {
new_texture = W3DNEW TextureClass (desc.Width, desc.Width, WW3D_FORMAT_A4R4G4B4, MIP_LEVELS_1);
new_texture->Get_Filter().Set_U_Addr_Mode(TextureFilterClass::TEXTURE_ADDRESS_CLAMP);
new_texture->Get_Filter().Set_V_Addr_Mode(TextureFilterClass::TEXTURE_ADDRESS_CLAMP);
new_texture->Get_Filter().Set_Min_Filter(TextureFilterClass::FILTER_TYPE_NONE);
new_texture->Get_Filter().Set_Mag_Filter(TextureFilterClass::FILTER_TYPE_NONE);
new_texture->Get_Filter().Set_Mip_Mapping(TextureFilterClass::FILTER_TYPE_NONE);
}
SurfaceClass *texture_surface = new_texture->Get_Surface_Level ();
//
// Copy the contents of the texture from the surface
//
DX8Wrapper::_Copy_DX8_Rects (curr_surface->Peek_D3D_Surface (), NULL, 0, texture_surface->Peek_D3D_Surface (), NULL);
REF_PTR_RELEASE (texture_surface);
//
// Assign this texture to any renderers that need it
//
for (int renderer_index = 0; renderer_index < surface_info.Renderers.Count (); renderer_index ++) {
Render2DClass *renderer = surface_info.Renderers[renderer_index];
renderer->Set_Texture (new_texture);
}
//
// Release our hold on the objects
//
REF_PTR_RELEASE (new_texture);
REF_PTR_RELEASE (curr_surface);
}
//
// Reset the list
//
if (PendingSurfaces.Count()>0) {
PendingSurfaces.Delete_All ();
}
// what the new text had no use for
Release_Spare_Textures ();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Release_Spare_Textures
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Release_Spare_Textures (void)
{
for (int index = 0; index < SpareTextures.Count (); index ++) {
REF_PTR_RELEASE (SpareTextures[index]);
}
if (SpareTextures.Count()>0) SpareTextures.Delete_All ();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Draw_Sentence
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Draw_Sentence (uint32 color)
{
Render2DClass *curr_renderer = NULL;
SurfaceClass *curr_surface = NULL;
DrawExtents.Set (0, 0, 0, 0);
int offset = 0;
//
// Loop over all the parts of the sentence
//
for (int index = 0; index < SentenceData.Count (); index ++) {
SentenceDataStruct &data = SentenceData[index];
//
// Has the surface changed?
//
if (data.Surface != curr_surface) {
curr_surface = data.Surface;
//
// Try to find a renderer that uses the same "texture"
//
bool found = false;
for (int renderer_index = 0; renderer_index < Renderers.Count (); renderer_index ++) {
if (Renderers[renderer_index].Surface == curr_surface) {
found = true;
curr_renderer = Renderers[renderer_index].Renderer;
break;
}
}
//
// Create a new renderer if we couldn't find an appropriate one
//
if (found == false) {
//
// Allocate a new renderer
//
curr_renderer = W3DNEW Render2DClass;
curr_renderer->Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution ());
ShaderClass *curr_shader = curr_renderer->Get_Shader ();
(*curr_shader) = Shader;
//
// Add it to our list
//
RendererDataStruct render_info;
render_info.Renderer = curr_renderer;
render_info.Surface = curr_surface;
Renderers.Add (render_info);
//
// Now, add this renderer to the surface pending list
//
for (int surface_index = 0; surface_index < PendingSurfaces.Count (); surface_index ++) {
PendingSurfaceStruct &surface_info = PendingSurfaces[surface_index];
if (surface_info.Surface == curr_surface) {
surface_info.Renderers.Add (curr_renderer);
}
}
}
}
//
// Get the dimensions of the surface
//
SurfaceClass::SurfaceDescription desc;
curr_surface->Get_Description (desc);
//
// Add a quad that contains this sentence chunk
//
RectClass screen_rect = data.ScreenRect;
screen_rect += Location;
RectClass uv_rect = data.UVRect;
//
// Clip the quad (as necessary)
//
bool add_quad = true;
if (IsClippedEnabled) {
//
// Check for completely clipped
//
if ( screen_rect.Right <= ClipRect.Left ||
screen_rect.Bottom <= ClipRect.Top)
{
add_quad = false;
} else {
//
// Clip the polygons to the specified area
//
RectClass clipped_rect;
clipped_rect.Left = max (screen_rect.Left, ClipRect.Left);
clipped_rect.Right = min (screen_rect.Right, ClipRect.Right);
clipped_rect.Top = max (screen_rect.Top, ClipRect.Top);
clipped_rect.Bottom = min (screen_rect.Bottom, ClipRect.Bottom);
//
// Clip the texture to the specified area
//
RectClass clipped_uv_rect;
float percent = ((clipped_rect.Left - screen_rect.Left) / screen_rect.Width ());
clipped_uv_rect.Left = uv_rect.Left + (uv_rect.Width () * percent);
percent = ((clipped_rect.Right - screen_rect.Left) / screen_rect.Width ());
clipped_uv_rect.Right = uv_rect.Left + (uv_rect.Width () * percent);
percent = ((clipped_rect.Top - screen_rect.Top) / screen_rect.Height ());
clipped_uv_rect.Top = uv_rect.Top + (uv_rect.Height () * percent);
percent = ((clipped_rect.Bottom - screen_rect.Top) / screen_rect.Height ());
clipped_uv_rect.Bottom = uv_rect.Top + (uv_rect.Height () * percent);
//
// Use the clipped rectangles to render
//
screen_rect = clipped_rect;
uv_rect = clipped_uv_rect;
if (screen_rect.Right <= screen_rect.Left ||
screen_rect.Bottom <= screen_rect.Top)
{
add_quad = false;
}
}
}
if (add_quad) {
//uv_rect.Bottom += 0.5f;
uv_rect *= 1.0F / ((float)desc.Width);
#ifdef TEST_PLACEMENT
screen_rect.Left += offset*3;
screen_rect.Right += offset*3;
#endif
offset++;
curr_renderer->Add_Quad (screen_rect, uv_rect, color);
//
// Add this rectangle to the total draw extents
//
if (DrawExtents.Width () == 0) {
DrawExtents = screen_rect;
} else {
DrawExtents += screen_rect;
}
}
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Record_Sentence_Chunk
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Record_Sentence_Chunk (void)
{
//
// Do we have anything to store?
//
int width = TextureOffset.I - TextureStartX;
if (width > 0) {
float char_height = Font->Get_Char_Height ();
//
// Build a structure that contains enough information
// to hold this portion of the sentence
//
SentenceDataStruct sentence_data;
sentence_data.Surface = CurSurface;
sentence_data.Surface->Add_Ref ();
sentence_data.ScreenRect.Left = Cursor.X;
sentence_data.ScreenRect.Right = Cursor.X + width;
sentence_data.ScreenRect.Top = Cursor.Y;
sentence_data.ScreenRect.Bottom = Cursor.Y + char_height;
sentence_data.UVRect.Left = TextureStartX;
sentence_data.UVRect.Top = TextureOffset.J;
sentence_data.UVRect.Right = TextureOffset.I;
sentence_data.UVRect.Bottom = TextureOffset.J + char_height;
//
// Add this information to our list
//
SentenceData.Add (sentence_data);
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Allocate_New_Surface
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Allocate_New_Surface (const WideChar *text, bool justCalcExtents)
{
if (!justCalcExtents)
{
//
// Unlock the last surface (if necessary)
//
if (LockedPtr != NULL) {
CurSurface->Unlock ();
LockedPtr = NULL;
}
}
//
// Calculate the width of the text
//
int text_width = 0;
for (int index = 0; text[index] != 0; index ++) {
text_width += Font->Get_Char_Spacing (text[index]);
}
int char_height = Font->Get_Char_Height ();
//
// Find the best texture size for the remaining text
//
//
// 64 to 512 square. EA stopped at 256, which fits a line of text up to 255 pixels tall; a
// font past that gave every candidate size a rows_per_texture of zero, so none of them was
// ever picked, the 256 default stood, and the glyph was blitted outside the surface it was
// locked into. 512 covers every point size loadFontData will now build.
//
CurrTextureSize = 256;
int best_tex_mem_usage = 999999999;
for (int pow2 = 6; pow2 <= 9; pow2 ++) {
int size = 1 << pow2;
int row_count = (text_width / size) + 1;
int rows_per_texture = size / (char_height + 1);
//
// Can we even fit one character on this texture?
//
if (rows_per_texture > 0) {
//
// How many textures (at this size) would it take to render
// the remaining text?
//
int texture_count = row_count / rows_per_texture;
texture_count = max (texture_count, 1);
//
// Is this the best usage of texture memory we've found yet?
//
int texture_mem_usage = (texture_count * size * size);
if (texture_mem_usage < best_tex_mem_usage) {
CurrTextureSize = size;
best_tex_mem_usage = texture_mem_usage;
}
}
}
//
// Use whichever is larger, the hint or the calculated size
//
CurrTextureSize = max (TextureSizeHint, CurrTextureSize);
if (!justCalcExtents)
{
//
// Release our extra hold on the old surface
//
REF_PTR_RELEASE (CurSurface);
//
// Create the new surface
//
CurSurface = NEW_REF (SurfaceClass, (CurrTextureSize, CurrTextureSize, WW3D_FORMAT_A4R4G4B4));
WWASSERT (CurSurface != NULL);
CurSurface->Add_Ref ();
//
// Add this surface to our list
//
PendingSurfaceStruct surface_info;
surface_info.Surface = CurSurface;
PendingSurfaces.Add (surface_info);
}
//
// Reset to the upper left corner
//
TextureOffset.Set (0, 0);
TextureStartX = 0;
return ;
}
float FindStartingXPos( const WideChar *text )
{
return 1;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Build_Sentence_Centered
//
////////////////////////////////////////////////////////////////////////////////////
void Render2DSentenceClass::Build_Sentence_Centered (const WideChar *text, int *hkX, int *hkY)
{
float char_height = Font->Get_Char_Height ();
int wordWidth = 0;
int notCenteredHotkeyX = 0;
int notCenteredHotkeyY = 0;
Vector2 extent = Build_Sentence_Not_Centered(text,¬CenteredHotkeyX, ¬CenteredHotkeyY, true); //Get_Formatted_Text_Extents(text);
//
// Start fresh
//
Reset_Sentence_Data ();
Cursor.Set (0, 0);
//
// Ensure we have a surface to start with
//
if (CurSurface == NULL) {
Allocate_New_Surface (text);
}
//
// Loop over all the characters in the string
//
bool end = false;
const WideChar *word;
int word_width = 0;
int line_width = 0;
int charCount = 0;
int wordCount = 0;
int hotKeyPosX = 0;
int hotKeyPosY = 0;
bool calcHotKeyX = false;
while (!end)
{
//
// Re-init everything for the next line
//
word = text;
word_width = 0;
line_width = 0;
charCount = 0;
wordCount = 0;
//
//first find the length of the line till we wrap
//
while ( 1 )
{
//
// read a word
//
int charWidth = 0;
while ((*word != 0) && (*word > u' ') && (*word != u'\n')) {
if( ParseHotKey && (*word == u'&') && (*word+1 != 0) && (*word+1 > u' ') && (*word+1 != u'\n'))
{
int offset = 0;
//Added By Saad
if (word_width != 0 )
{
const WideChar *word_back = word;
*word_back--;
if (*word_back == u' ')
{
line_width -= word_width;
offset =-1;
}
}
//
*word++;
calcHotKeyX = true;
}
charWidth = Font->Get_Char_Spacing (*word++);
word_width += charWidth;
wordCount++;
if (WrapWidth > 0 && word_width >= WrapWidth && useHardWordWrap)
break;
}
//
// If this word is unworthy to be on the current line, decrement the space and break
//
if(WrapWidth > 0 && (line_width + word_width >= WrapWidth))
{
//
//Take care of the case that the word is too big for the allocated space...
//If that's the case, drop out and process the word anyway
//
if(charCount == 0)
{
charCount +=wordCount - 1;
line_width += word_width - charWidth;
if(*word == 0)
end = true;
break;
}
charCount--;
break;
}
//
// if we reached the end of the text, set the values and break, also set the end flag
//
if( *word == 0 )
{
charCount +=wordCount;
line_width += word_width;
end = true;
break;
}
//
// otherwise, increment the counts
//
charCount +=wordCount + 1;
line_width += word_width;
//
// We were some a new line character break and process
//
if(*word != u' ')
break;
//
// add the space to our width
//
word_width = Font->Get_Char_Spacing (*word++);
wordCount = 0;
//Added By Saad
line_width += word_width;
}
//
// we now hold the length of the line and it's width lets set our cursor position to center it
//
Cursor.X = (int)((extent.X - line_width) / 2);
if(Cursor.X < 0)
Cursor.X = 0;
if(calcHotKeyX)
{
calcHotKeyX = false;
hotKeyPosX = Cursor.X + notCenteredHotkeyX;
}
for(int i = 0; i <= charCount; i++) {
WideChar ch = *text++;
//
// Determine how much horizontal space this character requires. A hotkey '&' is
// dropped and the letter after it drawn like any other: the grid keys are the
// shortcuts now, and the letter used to be left out here for a second renderer to
// paint in yellow, which drew nothing at all for a letter outside ASCII.
//
if(ParseHotKey && (ch == u'&') && (*text != 0) && (*text > u' ') && (*text != u'\n'))
{
ch = *text++;
}
float char_spacing = Font->Get_Char_Spacing (ch);
// Blit_Char writes the glyph's whole width, overlap and overhang columns included, so
// that is what has to fit: tested on the spacing alone, the last few columns of a glyph
// near the edge ran on into the next texel row and wiped the first letter of the line.
bool exceeded_texture_width = ((TextureOffset.I + Font->Get_Char_Width (ch)) >= CurrTextureSize);
bool encountered_break_char = (ch == u' ' || ch == u'\n' || ch == 0);
//
// Do we need to record this portion of the sentence to its own chunk?
//
if (exceeded_texture_width || encountered_break_char) {
Record_Sentence_Chunk ();
//
// Adjust the positions
//
Cursor.X += (TextureOffset.I - TextureStartX);
TextureStartX = TextureOffset.I;
//
// Adjust the output coordinates
//
if (ch == u' ') {
Cursor.X += char_spacing;
} else if ((ch == 0 )|| (ch == u'\n')) {
break;
}
//
// Did the text extend past the edge of the texture?
//
if (exceeded_texture_width) {
TextureStartX = 0;
TextureOffset.I = TextureStartX;
TextureOffset.J += char_height;
//
// Did the text extent completely off the texture?
//
if ((TextureOffset.J + char_height) >= CurrTextureSize) {
Allocate_New_Surface (text);
}
}
}
//
// Adjust the output coordinates
//
if (ch != u'\n' && ch != u' ') {
//
// Ensure the surface is locked
//
if (LockedPtr == NULL) {
LockedPtr = (uint16 *)CurSurface->Lock (&LockedStride);
WWASSERT (LockedPtr != NULL);
}
//
// Check to ensure the text will fit on this texture
//
WWASSERT (((TextureOffset.I + char_spacing) < CurrTextureSize) && ((TextureOffset.J + char_height) < CurrTextureSize));
//
// Blit the character to the surface
//
Font->Blit_Char (ch, LockedPtr, LockedStride, TextureOffset.I, TextureOffset.J);
TextureOffset.I += char_spacing;
}
}
//
// reset our cursor and add a line of text to the cursor position
//
Cursor.X = 0;
Cursor.Y += char_height;
line_width = 0;
}
if(hkX)
*hkX = hotKeyPosX;
if(hkX)
*hkY = hotKeyPosY;
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Build_Sentence_NotCentered
//
////////////////////////////////////////////////////////////////////////////////////
Vector2 Render2DSentenceClass::Build_Sentence_Not_Centered (const WideChar *text, int *hkX, int *hkY, bool justCalcExtents)
{
Vector2 cursor = Cursor;
int textureStartX = TextureStartX;
float maxX = 0;
int hotKeyPosX = 0;
int hotKeyPosY = 0;
bool calcHotKeyX = false;
Vector2i textureOffset = TextureOffset;
//
// Start fresh
//
if (!justCalcExtents)
{
Reset_Sentence_Data ();
}
Cursor.Set (0, 0);
//
// Ensure we have a surface to start with
//
if (CurSurface == NULL) {
Allocate_New_Surface (text, justCalcExtents);
}
TextureOffset.Set (TEXTURE_OFFSET, 0);
TextureStartX = TEXTURE_OFFSET;
float char_height = Font->Get_Char_Height ();
//
// Loop over all the characters in the string
//
while (text != NULL) {
WideChar ch = *text++;
bool wrapped_on_space = false;
//
// Determine how much horizontal space this character requires; a hotkey '&' is
// dropped and its letter drawn like the rest, as in Build_Sentence_Centered
//
if(ParseHotKey && (ch == u'&') && (*text != 0) && (*text > u' ') && (*text != u'\n'))
{
hotKeyPosY = Cursor.Y;
if (calcHotKeyX)
hotKeyPosX = 0;
else
hotKeyPosX = Cursor.X + TextureOffset.I -TextureStartX;//TextureOffset.I;
ch = *text++;
}
float char_spacing = Font->Get_Char_Spacing (ch);
// the whole width Blit_Char writes, as in Build_Sentence_Centered
bool exceeded_texture_width = ((TextureOffset.I + Font->Get_Char_Width (ch)) >= CurrTextureSize);
bool encountered_break_char = (ch == u' ' || ch == u'\n' || ch == 0);
bool wordBiggerThenLine = ((useHardWordWrap) && ( WrapWidth != 0 ) &&((Cursor.X + TextureOffset.I -TextureStartX + char_spacing) >= WrapWidth));
//
// Do we need to record this portion of the sentence to its own chunk?
//
if (exceeded_texture_width || encountered_break_char|| wordBiggerThenLine) {
if (!justCalcExtents)
{
Record_Sentence_Chunk ();
}
//
// Adjust the positions
//
Cursor.X += (TextureOffset.I - TextureStartX);
maxX = max(maxX, Cursor.X);
TextureStartX = TextureOffset.I;
//
// Adjust the output coordinates
//
if (ch == u' ') {
//Cursor.X += char_spacing;
//maxX = max(maxX, Cursor.X);
//
// Check to see if we need to wrap on this word-break
//
if (WrapWidth > 0) {
//
// Find the length of the next word
//
const WideChar *word = text;
float word_width = char_spacing;
while ((*word != 0) && (*word > u' ')) {
if(ParseHotKey && (*word == u'&') && (*word+1 != 0) && (*word+1 > u' ') && (*word+1 != u'\n'))
*word++;
word_width += Font->Get_Char_Spacing (*word++);
}
//
// Should we wrap the next word?
//
if ((Cursor.X + word_width) >= WrapWidth) {
Cursor.X = 0;
Cursor.Y += char_height;
calcHotKeyX = true;
// the space that broke the line is dropped, or the next line starts one space in
wrapped_on_space = true;
}
}
} else if (ch == u'\n') {
Cursor.X = 0;
Cursor.Y += char_height;
} else if (ch == 0) {
break;
} else if (wordBiggerThenLine){ // we've entered this loop because we're greater then the wordwrap so we need to force a wordwrap
Cursor.X = 0;
Cursor.Y += char_height;
}
//
// Did the text extend past the edge of the texture?
//
if (exceeded_texture_width) {
TextureStartX = TEXTURE_OFFSET;
TextureOffset.I = TextureStartX;
TextureOffset.J += char_height;
//
// Did the text extent completely off the texture?
//
if ((TextureOffset.J + char_height) >= CurrTextureSize) {
Allocate_New_Surface (text, justCalcExtents);
}
}
}
if (ch != u'\n' && !wrapped_on_space) {
//
// Ensure the surface is locked
//
if (!justCalcExtents)
{
if (LockedPtr == NULL) {
LockedPtr = (uint16 *)CurSurface->Lock (&LockedStride);
WWASSERT (LockedPtr != NULL);
}
}
//
// Check to ensure the text will fit on this texture
//
WWASSERT (((TextureOffset.I + char_spacing) < CurrTextureSize) && ((TextureOffset.J + char_height) < CurrTextureSize));
//
// Blit the character to the surface
//
if (!justCalcExtents)
{
Font->Blit_Char (ch, LockedPtr, LockedStride, TextureOffset.I, TextureOffset.J);
}
TextureOffset.I += char_spacing;
}
}
Vector2 extent;
extent.X = maxX + Font->Get_Extra_Overlap();
extent.Y = Cursor.Y + char_height;
Cursor = cursor;
TextureOffset = textureOffset;
TextureStartX = textureStartX;
if(hkX)
*hkX = hotKeyPosX;
if(hkX)
*hkY = hotKeyPosY;
return extent;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Build_Sentence
//
////////////////////////////////////////////////////////////////////////////////////
void
Render2DSentenceClass::Build_Sentence (const WideChar *text, int *hkX, int *hkY)
{
if (text == NULL) {
return ;
}
// With no render device there is nowhere to rasterize a glyph: Allocate_New_Surface would ask
// CreateImageSurface of a null device and take the process with it. Measure the text and stop
// there, which is what justCalcExtents already exists to do - callers size their windows off
// the extent and nothing is ever asked to draw the result.
if (DX8Wrapper::_Get_D3D_Device() == NULL) {
Build_Sentence_Not_Centered(text, hkX, hkY, true);
return;
}
// WideChar is char16_t, which the C library's wcschr does not take: look for the newline by hand.
bool has_newline = false;
for (const WideChar *p = text; *p != 0 && !has_newline; ++p)
has_newline = (*p == u'\n');
if(Centered && (WrapWidth > 0 || has_newline))
Build_Sentence_Centered(text, hkX, hkY);
else
Build_Sentence_Not_Centered(text, hkX, hkY);
return;
}
////////////////////////////////////////////////////////////////////////////////////
//
// FontCharsClass
//
////////////////////////////////////////////////////////////////////////////////////
FontCharsClass::FontCharsClass (void) :
#if defined(_WIN32)
OldGDIFont( NULL ),
OldGDIBitmap( NULL ),
GDIFont( NULL ),
GDIBitmap( NULL ),
GDIBitmapBits ( NULL ),
MemDC( NULL ),
#else
Rasteriser( NULL ),
#endif
CurrPixelOffset( 0 ),
PointSize( 0 ),
CharHeight( 0 ),
UnicodeCharArray( NULL ),
FirstUnicodeChar( 0xFFFF ),
LastUnicodeChar( 0 ),
IsBold (false)
{
AlternateUnicodeFont = NULL;
::memset( ASCIICharArray, 0, sizeof (ASCIICharArray) );
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// ~FontCharsClass
//
////////////////////////////////////////////////////////////////////////////////////
FontCharsClass::~FontCharsClass (void)
{
while ( BufferList.Count() ) {
// One FontCharsBuffer per entry (W3DNEW, class operator new from its own pool), so scalar
// delete. The original "delete []" bypassed the class operator delete for the global
// array delete: with a virtual destructor the compiler expects an array cookie in front
// of the object, so it freed (p - 4) through the DynamicMemoryAllocator and read a pixel
// of the previous buffer as the block's owning blob (0x04000000) - a NULL-pool freeBlock
// in every ~W3DDisplay, i.e. a silent crash on every exit from the game.
delete BufferList[0];
BufferList.Delete(0);
}
Free_GDI_Font();
Free_Character_Arrays();
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Get_Char_Data
//
////////////////////////////////////////////////////////////////////////////////////
const FontCharsClassCharDataStruct *
FontCharsClass::Get_Char_Data (WideChar ch)
{
const FontCharsClassCharDataStruct *retval = NULL;
if ( ch < 256 )
{
retval = ASCIICharArray[ch];
}
else if ( AlternateUnicodeFont && this != AlternateUnicodeFont )
{
return AlternateUnicodeFont->Get_Char_Data( ch );
}
else
{
Grow_Unicode_Array( ch );
retval = UnicodeCharArray[ch - FirstUnicodeChar];
}
//
// If the character wasn't found, then add it to our list
//
if ( retval == NULL ) {
retval = Store_GDI_Char( ch );
}
WWASSERT( retval->Value == ch );
return retval;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Get_Char_Width
//
////////////////////////////////////////////////////////////////////////////////////
int
FontCharsClass::Get_Char_Width (WideChar ch)
{
const FontCharsClassCharDataStruct * data = Get_Char_Data( ch );
if ( data != NULL ) {
return data->Width;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Get_Char_Spacing
//
////////////////////////////////////////////////////////////////////////////////////
int
FontCharsClass::Get_Char_Spacing (WideChar ch)
{
const FontCharsClassCharDataStruct * data = Get_Char_Data( ch );
if ( data != NULL ) {
if ( data->Width != 0 ) {
return data->Width - PixelOverlap - CharOverhang;
}
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Blit_Char
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Blit_Char (WideChar ch, uint16 *dest_ptr, int dest_stride, int x, int y)
{
const FontCharsClassCharDataStruct * data = Get_Char_Data( ch );
if ( data != NULL && data->Width != 0 ) {
//
// Setup the src and destination pointers
//
int dest_inc = (dest_stride >> 1);
uint16 *src_ptr = data->Buffer;
dest_ptr += (dest_inc * y) + x;
//
// Simply copy the data from the src buffer to the destination
//
for ( int row = 0; row < CharHeight; row ++ ) {
for ( int col = 0; col < data->Width; col ++ ) {
uint16 curData = *src_ptr;
if (col<PixelOverlap) {
curData |= dest_ptr[col];
}
dest_ptr[col] = curData;
src_ptr++;
}
dest_ptr += dest_inc;
}
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Store_GDI_Char
//
////////////////////////////////////////////////////////////////////////////////////
const FontCharsClassCharDataStruct *
FontCharsClass::Store_GDI_Char (WideChar ch)
{
#if defined(_WIN32)
int width = PointSize * 2;
int height = PointSize * 2;
//
// Draw the character into the memory DC
//
RECT rect = { 0, 0, width, height };
int xOrigin = 0;
if (ch == 'W') {
xOrigin = 1;
}
// GDI's wide API wants a WCHAR, which on Windows is WideChar's two bytes: the cast is honest there.
::ExtTextOutW( MemDC, xOrigin, 0, ETO_OPAQUE, &rect, reinterpret_cast<LPCWSTR>( &ch ), 1, NULL);
//
// Get the size of the character we just drew
//
SIZE char_size = { 0 };
::GetTextExtentPoint32W( MemDC, reinterpret_cast<LPCWSTR>( &ch ), 1, &char_size );
char_size.cx += PixelOverlap + xOrigin;
//
// Get a pointer to the surface that this character should use
//
Update_Current_Buffer( char_size.cx );
uint16* curr_buffer_p = BufferList[BufferList.Count () - 1]->Buffer;
curr_buffer_p += CurrPixelOffset;
//
// Copy the BMP contents to the buffer
//
int stride = (((width * 3) + 3) & ~3);
for (int row = 0; row < char_size.cy; row ++) {
//
// Compute the indices into the BMP and surface
//
int index = (row * stride);
//
// Loop over each column
//
for (int col = 0; col < char_size.cx; col ++) {
//
// Get the pixel color at this location
//
uint8 pixel_value = GDIBitmapBits[index];
index += 3;
#ifdef TEST_PLACEMENT
if (row==CharHeight-1&&col==0) {
pixel_value = 0xff;
}
if (row==CharHeight-2&&col==1) {
pixel_value = 0xff;
}
if (row==0&&col==0) {
pixel_value = 0xff;
}
if (row==1&&col==1) {
pixel_value = 0xff;
}
if (row==CharHeight-1&&col==char_size.cx-1-PixelOverlap) {
pixel_value = 0xff;
}
if (row==CharHeight-2&&col==char_size.cx-2-PixelOverlap) {
pixel_value = 0xff;
}
if (row==0&&col==char_size.cx-1-PixelOverlap) {
pixel_value = 0xff;
}
if (row==1&&col==char_size.cx-2-PixelOverlap) {
pixel_value = 0xff;
}
if (pixel_value == 0x00) {
pixel_value = 0x40;
}
#endif
uint16 pixel_color = 0;
if (pixel_value != 0) {
pixel_color = 0x0FFF;
}
//
// Convert the pixel intensity from 8bit to 4bit and
// store it in our buffer. Coverage goes through a square root first: GDI's
// antialiased coverage taken straight as alpha leaves every edge pixel of a
// small glyph two thirds transparent, and light lettering on the game's dark
// panels read as hairlines. Fully covered and empty pixels stay what they were.
//
uint8 alpha_value = (uint8)( sqrt( pixel_value / 255.0 ) * 15.0 + 0.5 );
*curr_buffer_p++ = pixel_color | (alpha_value << 12);
}
}
//
// Save information about this character in our list
//
FontCharsClassCharDataStruct *char_data = W3DNEW FontCharsClassCharDataStruct;
char_data->Value = ch;
char_data->Width = char_size.cx;
char_data->Buffer = BufferList[BufferList.Count () - 1]->Buffer + CurrPixelOffset;
//
// Insert this character into our array
//
if ( ch < 256 ) {
ASCIICharArray[ch] = char_data;
} else {
UnicodeCharArray[ch - FirstUnicodeChar] = char_data;
}
//
// Advance the character position
//
CurrPixelOffset += ((char_size.cx+PixelOverlap) * CharHeight);
//
// Return the index of the entry we just added
//
return char_data;
#else
/* GDI's steps with FreeType (D6): the character drawn into the (2 x PointSize) square box at
(xOrigin, 0) - 'W' one pixel in, as above - its width the one-character extent plus the overlap
and the origin, its rows the font's height, and each pixel's coverage through the same square
root into the same 4-bit alpha. Without a font (none could be found or opened) each glyph is
blank and zero wide, as it was before there was a rasteriser. */
const int box = PointSize * 2;
int xOrigin = 0;
if (ch == 'W') {
xOrigin = 1;
}
int cx = 0;
int cy = CharHeight;
const uint8_t *coverage = NULL;
if ( Rasteriser != NULL ) {
Rasteriser->Draw_Char( ch, xOrigin );
coverage = Rasteriser->Get_Coverage();
cx = Rasteriser->Get_Advance( ch ) + PixelOverlap + xOrigin; // GetTextExtentPoint32W's cx, then as above
}
Update_Current_Buffer( cx );
uint16* curr_buffer_p = BufferList[BufferList.Count () - 1]->Buffer;
curr_buffer_p += CurrPixelOffset;
for (int row = 0; row < cy; row ++) {
for (int col = 0; col < cx; col ++) {
const uint8 pixel_value = (coverage != NULL && row < box && col < box) ? coverage[row * box + col] : 0;
uint16 pixel_color = 0;
if (pixel_value != 0) {
pixel_color = 0x0FFF;
}
uint8 alpha_value = (uint8)( sqrt( pixel_value / 255.0 ) * 15.0 + 0.5 );
*curr_buffer_p++ = pixel_color | (alpha_value << 12);
}
}
FontCharsClassCharDataStruct *char_data = W3DNEW FontCharsClassCharDataStruct;
char_data->Value = ch;
char_data->Width = cx;
char_data->Buffer = BufferList[BufferList.Count () - 1]->Buffer + CurrPixelOffset;
if ( ch < 256 ) {
ASCIICharArray[ch] = char_data;
} else {
UnicodeCharArray[ch - FirstUnicodeChar] = char_data;
}
CurrPixelOffset += ((cx+PixelOverlap) * CharHeight);
return char_data;
#endif
}
////////////////////////////////////////////////////////////////////////////////////
//
// Update_Current_Buffer
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Update_Current_Buffer (int char_width)
{
//
// Check to see if we need to allocate a new buffer
//
bool needs_new_buffer = (BufferList.Count () == 0);
if (needs_new_buffer == false) {
//
// Would we extend past this buffer?
//
if ( (CurrPixelOffset + (char_width * CharHeight)) > CHAR_BUFFER_LEN ) {
needs_new_buffer = true;
}
}
//
// Do we need to create a new surface?
//
if (needs_new_buffer)
{
FontCharsBuffer* new_buffer = W3DNEW FontCharsBuffer;
BufferList.Add( new_buffer );
CurrPixelOffset = 0;
}
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Create_GDI_Font
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Create_GDI_Font (const char *font_name)
{
#if defined(_WIN32)
HDC screen_dc = ::GetDC ((HWND)WW3D::Get_Window());
const char *fontToUseForGenerals = "Arial";
bool doingGenerals = false;
if (strcmp(font_name, "Generals")==0) {
font_name = fontToUseForGenerals;
doingGenerals = true;
}
//
// Calculate the height of the font in logical units
//
const int dotsPerInch = 96; // always use 96. jba.
int font_height = -MulDiv (PointSize, dotsPerInch, 72);
int fontWidth = 0; // use font default.
if (doingGenerals) {
//fontWidth = -font_height*0.35f; //2 pixels tighter.
fontWidth = -font_height*0.40f; // one pixel tighter
}
PixelOverlap = (-font_height)/8;
// Sanity check in case of perversion. :)
if (PixelOverlap<0) PixelOverlap = 0;
if (PixelOverlap>4) PixelOverlap = 4;
//
// Create the Windows font
//
DWORD bold = IsBold ? FW_BOLD : FW_NORMAL;
DWORD italic = 0;
GDIFont = ::CreateFont (font_height, fontWidth, 0, 0, bold, italic,
FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
VARIABLE_PITCH, font_name);
//
// Set-up the fields of the BITMAPINFOHEADER
// Note: Top-down DIBs use negative height in Win32.
//
BITMAPINFOHEADER bitmap_info = { 0 };
bitmap_info.biSize = sizeof (BITMAPINFOHEADER);
bitmap_info.biWidth = PointSize * 2;
bitmap_info.biHeight = -(PointSize * 2);
bitmap_info.biPlanes = 1;
bitmap_info.biBitCount = 24;
bitmap_info.biCompression = BI_RGB;
bitmap_info.biSizeImage = ((PointSize * PointSize * 4) * 3);
bitmap_info.biXPelsPerMeter = 0;
bitmap_info.biYPelsPerMeter = 0;
bitmap_info.biClrUsed = 0;
bitmap_info.biClrImportant = 0;
//
// Create a bitmap that we can access the bits directly of
//
GDIBitmap = ::CreateDIBSection ( screen_dc,
(const BITMAPINFO *)&bitmap_info,
DIB_RGB_COLORS,
(void **)&GDIBitmapBits,
NULL,
0L);
//
// Create a device context we can select the font and bitmap into
//
MemDC = ::CreateCompatibleDC (screen_dc);
//
// Release our temporary screen DC
//
::ReleaseDC ((HWND)WW3D::Get_Window(), screen_dc);
//
// Now select the BMP and font into the DC
//
OldGDIBitmap = (HBITMAP)::SelectObject (MemDC, GDIBitmap);
OldGDIFont = (HFONT)::SelectObject (MemDC, GDIFont);
::SetBkColor (MemDC, RGB (0, 0, 0));
::SetTextColor (MemDC, RGB (255, 255, 255));
//
// Lookup the pixel height of the font
//
TEXTMETRIC text_metric = { 0 };
::GetTextMetrics (MemDC, &text_metric);
CharHeight = text_metric.tmHeight;
CharAscent = text_metric.tmAscent;
CharOverhang = text_metric.tmOverhang;
if (doingGenerals) {
CharOverhang = 0;
}
#else
/* GDI's font with FreeType (D6): the same face, height, width and weight as the CreateFont above -
"Generals" is Arial squeezed to an lfWidth of 0.40 x its height - and the TEXTMETRIC's three
numbers from the same file's own tables (glyphrasteriser.h says which). Without a font, the
glyphs are blank and a line is still a point size tall, so layout divides by nothing. */
const char *fontToUseForGenerals = "Arial";
bool doingGenerals = false;
if (strcmp(font_name, "Generals")==0) {
font_name = fontToUseForGenerals;
doingGenerals = true;
}
const int dotsPerInch = 96; // always use 96. jba.
const int font_height = -((PointSize * dotsPerInch + 36) / 72); // -MulDiv (PointSize, dotsPerInch, 72), for a positive size
int fontWidth = 0; // use font default.
if (doingGenerals) {
fontWidth = -font_height*0.40f; // one pixel tighter
}
PixelOverlap = (-font_height)/8;
if (PixelOverlap<0) PixelOverlap = 0;
if (PixelOverlap>4) PixelOverlap = 4;
delete Rasteriser; // a second Create without a Free, if one ever comes
Rasteriser = new GlyphRasteriserClass;
if ( Rasteriser->Create_Font( font_name, -font_height, fontWidth, IsBold, PointSize * 2 ) ) {
CharHeight = Rasteriser->Get_Metrics().Height;
CharAscent = Rasteriser->Get_Metrics().Ascent;
CharOverhang = Rasteriser->Get_Metrics().Overhang;
if (doingGenerals) {
CharOverhang = 0;
}
} else {
WWDEBUG_SAY(("FontCharsClass: no font file for '%s'; its glyphs are blank\n", font_name));
delete Rasteriser;
Rasteriser = NULL;
CharHeight = PointSize;
CharAscent = PointSize;
CharOverhang = 0;
PixelOverlap = 0;
}
#endif
}
////////////////////////////////////////////////////////////////////////////////////
//
// Free_GDI_Font
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Free_GDI_Font (void)
{
#if defined(_WIN32)
//
// Select the old font back into the DC and delete
// our font object
//
if ( GDIFont != NULL ) {
::SelectObject( MemDC, OldGDIFont );
::DeleteObject( GDIFont );
GDIFont = NULL;
}
//
// Select the old bitmap back into the DC and delete
// our bitmap object
//
if ( GDIBitmap != NULL ) {
::SelectObject( MemDC, OldGDIBitmap );
::DeleteObject( GDIBitmap );
GDIBitmap = NULL;
}
//
// Delete our memory DC
//
if ( MemDC != NULL ) {
::DeleteDC( MemDC );
MemDC = NULL;
}
#else
delete Rasteriser;
Rasteriser = NULL;
#endif
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Initialize_GDI_Font
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Initialize_GDI_Font (const char *font_name, int point_size, bool is_bold)
{
//
// Build a unique name from the font name and its size
//
Name.Format ("%s%d", font_name, point_size);
//
// Remember these settings
//
GDIFontName = font_name;
PointSize = point_size;
IsBold = is_bold;
//
// Create the actual font object
//
Create_GDI_Font (font_name);
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Is_Font
//
////////////////////////////////////////////////////////////////////////////////////
bool
FontCharsClass::Is_Font (const char *font_name, int point_size, bool is_bold)
{
bool retval = false;
//
// Check to see if both the name and height matches...
//
if ( (GDIFontName.Compare_No_Case (font_name) == 0) &&
(point_size == PointSize) &&
(is_bold == IsBold))
{
retval = true;
}
return retval;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Grow_Unicode_Array
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Grow_Unicode_Array (WideChar ch)
{
//
// Don't do anything if character is in the ASCII range
//
if ( ch < 256 ) {
return ;
}
//
// Don't do anything if character is in the currently allocated range
//
if ( ch >= FirstUnicodeChar && ch <= LastUnicodeChar ) {
return ;
}
uint16 first_index = min( FirstUnicodeChar, (uint16)ch );
uint16 last_index = max( LastUnicodeChar, (uint16)ch );
uint16 count = (last_index - first_index) + 1;
//
// Allocate enough memory to hold the new cells
//
FontCharsClassCharDataStruct **new_array = W3DNEWARRAY FontCharsClassCharDataStruct *[count];
::memset (new_array, 0, sizeof (FontCharsClassCharDataStruct *) * count);
//
// Copy the contents of the old array into the new array
//
if ( UnicodeCharArray != NULL ) {
int start_offset = (FirstUnicodeChar - first_index);
int old_count = (LastUnicodeChar - FirstUnicodeChar) + 1;
::memcpy (&new_array[start_offset], UnicodeCharArray, sizeof (FontCharsClassCharDataStruct *) * old_count);
//
// Delete the old array
//
delete [] UnicodeCharArray;
UnicodeCharArray = NULL;
}
FirstUnicodeChar = first_index;
LastUnicodeChar = last_index;
UnicodeCharArray = new_array;
return ;
}
////////////////////////////////////////////////////////////////////////////////////
//
// Free_Character_Arrays
//
////////////////////////////////////////////////////////////////////////////////////
void
FontCharsClass::Free_Character_Arrays (void)
{
if ( UnicodeCharArray != NULL ) {
int count = (LastUnicodeChar - FirstUnicodeChar) + 1;
//
// Delete each member of the unicode array
//
for (int index = 0; index < count; index ++) {
if ( UnicodeCharArray[index] != NULL ) {
delete UnicodeCharArray[index];
UnicodeCharArray[index] = NULL;
}
}
//
// Delete the array itself
//
delete [] UnicodeCharArray;
UnicodeCharArray = NULL;
}
//
// Delete each member of the ascii character array
//
for (int index = 0; index < 256; index ++) {
if ( ASCIICharArray[index] != NULL ) {
delete ASCIICharArray[index];
ASCIICharArray[index] = NULL;
}
}
return ;
}