Skip to content

Commit 7919a60

Browse files
committed
Implement all of glk_image_draw_scaled_ext() for text buffer windows
1 parent 3b1cb61 commit 7919a60

File tree

5 files changed

+87
-14
lines changed

5 files changed

+87
-14
lines changed

GlkDll/GlkGraphic.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ CWinGlkGraphic::CWinGlkGraphic()
3030

3131
m_iWidth = 0;
3232
m_iHeight = 0;
33-
m_iDisplay = 0;
3433
m_bAlpha = false;
34+
35+
m_iDisplay = 0;
36+
m_ImageRule = 0;
37+
m_iFixedWidth = 0;
38+
m_iFixedHeight = 0;
39+
m_ScaleWidth = 1.0;
40+
m_ScaleHeight = 1.0;
41+
m_MaxWidth = 1.0;
3542
};
3643

3744
CWinGlkGraphic::~CWinGlkGraphic()

GlkDll/GlkGraphic.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ class CWinGlkGraphic
3232

3333
int m_iWidth;
3434
int m_iHeight;
35-
int m_iDisplay;
3635
bool m_bAlpha;
36+
37+
int m_iDisplay;
38+
unsigned int m_ImageRule;
39+
int m_iFixedWidth;
40+
int m_iFixedHeight;
41+
double m_ScaleWidth;
42+
double m_ScaleHeight;
43+
double m_MaxWidth;
3744
};
3845

3946
/////////////////////////////////////////////////////////////////////////////

GlkDll/GlkWindowGfx.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ bool CWinGlkWndGraphics::DrawGraphic(CWinGlkGraphic* pGraphic, int iValue1, int
237237
{
238238
// Create a temporary DIBSection
239239
CDC* pWndDC = GetDC();
240-
DibSection.CreateBitmap(pWndDC->GetSafeHdc(),iWidth,iHeight);
240+
BOOL created = DibSection.CreateBitmap(pWndDC->GetSafeHdc(),iWidth,iHeight);
241241
ReleaseDC(pWndDC);
242+
if (!created)
243+
return false;
242244

243245
// Draw the graphic into the temporary DIBSection
244246
ppvBits = DibSection.GetBits();

GlkDll/GlkWindowTextBuffer.cpp

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ void CWinGlkWndTextBuffer::SizeWindow(CRect* pSize)
143143
{
144144
CWinGlkWnd::SizeWindow(pSize);
145145
ClearFormatting(-1);
146+
ResizeGraphics(pSize);
146147
}
147148

148149
void CWinGlkWndTextBuffer::ClearWindow(void)
@@ -538,39 +539,53 @@ bool CWinGlkWndTextBuffer::DrawGraphic(CWinGlkGraphic* pGraphic, int iValue1, in
538539
{
539540
if (pGraphic->m_pPixels && pGraphic->m_pHeader)
540541
{
541-
// Get the width and height
542+
// Save the arguments needed later to recalculate the size of the graphic
543+
pGraphic->m_iDisplay = iValue1;
544+
pGraphic->m_ImageRule = iImageRule;
545+
pGraphic->m_MaxWidth = (double)iMaxWidth / 0x10000;
546+
542547
switch (iImageRule & imagerule_WidthMask)
543548
{
544549
case imagerule_WidthOrig:
545-
iWidth = pGraphic->m_pHeader->biWidth;
550+
pGraphic->m_iFixedWidth = pGraphic->m_pHeader->biWidth;
546551
break;
547552
case imagerule_WidthFixed:
553+
pGraphic->m_iFixedWidth = iWidth;
554+
break;
555+
case imagerule_WidthRatio:
556+
pGraphic->m_ScaleWidth = (double)iWidth / 0x10000;
548557
break;
549558
default:
550559
return false;
551560
}
552561
switch (iImageRule & imagerule_HeightMask)
553562
{
554563
case imagerule_HeightOrig:
555-
iHeight = abs(pGraphic->m_pHeader->biHeight);
564+
pGraphic->m_iFixedHeight = abs(pGraphic->m_pHeader->biHeight);
556565
break;
557566
case imagerule_HeightFixed:
567+
pGraphic->m_iFixedHeight = iHeight;
568+
break;
569+
case imagerule_AspectRatio:
570+
{
571+
double aspect = ((double)abs(pGraphic->m_pHeader->biHeight)) / pGraphic->m_pHeader->biWidth;
572+
pGraphic->m_ScaleHeight = aspect * ((double)iHeight / 0x10000);
573+
}
558574
break;
559575
default:
560576
return false;
561577
}
562578

579+
// Store the graphic
563580
if (m_TextBuffer.GetSize() == 0)
564581
AddNewParagraph();
565582
CParagraph* last = m_TextBuffer[m_TextBuffer.GetUpperBound()];
566-
567-
pGraphic->m_iWidth = iWidth;
568-
pGraphic->m_iHeight = iHeight;
569-
pGraphic->m_iDisplay = iValue1;
570-
571-
// Store the graphic
572583
if (last->AddGraphic(pGraphic))
573584
{
585+
CRect ClientArea;
586+
GetClientRect(ClientArea);
587+
last->SetGraphicSize(pGraphic,&ClientArea);
588+
574589
ClearFormatting(m_TextBuffer.GetUpperBound());
575590
bDelete = false;
576591
return true;
@@ -917,6 +932,12 @@ void CWinGlkWndTextBuffer::ClearFormatting(int iPara)
917932
}
918933
}
919934

935+
void CWinGlkWndTextBuffer::ResizeGraphics(CRect* pWindowSize)
936+
{
937+
for (int i = 0; i < m_TextBuffer.GetSize(); i++)
938+
m_TextBuffer[i]->ResizeGraphics(pWindowSize);
939+
}
940+
920941
template<class XCHAR> void CWinGlkWndTextBuffer::PaintInputBuffer(
921942
CWinGlkDC& dc, const XCHAR* input, int inputLen, CPaintInfo& Info)
922943
{
@@ -1363,8 +1384,8 @@ void CWinGlkWndTextBuffer::CPaintInfo::DrawGraphic(CWinGlkGraphic* pGraphic, int
13631384

13641385
// Create a temporary DIBSection
13651386
CDibSection DibSection;
1366-
DibSection.CreateBitmap(m_DeviceContext.GetSafeHdc(),
1367-
pGraphic->m_iWidth,pGraphic->m_iHeight);
1387+
if (!DibSection.CreateBitmap(m_DeviceContext.GetSafeHdc(),pGraphic->m_iWidth,pGraphic->m_iHeight))
1388+
return;
13681389
CBitmap* pOldBitmap = CDibSection::SelectDibSection(dcMem,&DibSection);
13691390

13701391
// Is this graphic being scaled?
@@ -1597,6 +1618,39 @@ bool CWinGlkWndTextBuffer::CParagraph::ClearFormatting(void)
15971618
return m_bClearAll;
15981619
}
15991620

1621+
void CWinGlkWndTextBuffer::CParagraph::SetGraphicSize(CWinGlkGraphic* pGraphic, CRect* pWindowSize)
1622+
{
1623+
int width = pWindowSize->Width();
1624+
1625+
if ((pGraphic->m_ImageRule & imagerule_WidthMask) == imagerule_WidthRatio)
1626+
pGraphic->m_iWidth = (int)(pGraphic->m_ScaleWidth * width);
1627+
else
1628+
pGraphic->m_iWidth = pGraphic->m_iFixedWidth;
1629+
1630+
if ((pGraphic->m_ImageRule & imagerule_HeightMask) == imagerule_AspectRatio)
1631+
pGraphic->m_iHeight = (int)(pGraphic->m_ScaleHeight * pGraphic->m_iWidth);
1632+
else
1633+
pGraphic->m_iHeight = pGraphic->m_iFixedHeight;
1634+
1635+
if (pGraphic->m_MaxWidth > 0.0)
1636+
{
1637+
double ratio = (double)pGraphic->m_iHeight / (double)pGraphic->m_iWidth;
1638+
if (pGraphic->m_iWidth > (width * pGraphic->m_MaxWidth))
1639+
{
1640+
pGraphic->m_iWidth = (int)(width * pGraphic->m_MaxWidth);
1641+
pGraphic->m_iHeight = (int)(ratio * pGraphic->m_iWidth);
1642+
}
1643+
}
1644+
}
1645+
1646+
void CWinGlkWndTextBuffer::CParagraph::ResizeGraphics(CRect* pWindowSize)
1647+
{
1648+
for (int i = 0; i < m_InlineGraphics.GetSize(); i++)
1649+
SetGraphicSize(m_InlineGraphics[i],pWindowSize);
1650+
for (int i = 0; i < m_MarginGraphics.GetSize(); i++)
1651+
SetGraphicSize(m_MarginGraphics[i],pWindowSize);
1652+
}
1653+
16001654
void CWinGlkWndTextBuffer::CParagraph::Format(CPaintInfo& Info)
16011655
{
16021656
// Only format if there is no formatting already set up

GlkDll/GlkWindowTextBuffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ class CWinGlkWndTextBuffer : public CWinGlkWnd
173173
void AddColourChange(const CTextColours& colours);
174174

175175
bool ClearFormatting(void);
176+
void SetGraphicSize(CWinGlkGraphic* pGraphic, CRect* pWindowSize);
177+
void ResizeGraphics(CRect* pWindowSize);
176178
void Format(CPaintInfo& Info);
177179
void Update(CPaintInfo& Info, int& iOffset, CWinGlkWnd* pWnd);
178180
bool Paint(CPaintInfo& Info, int& iFinalLeft, int& iFinalTop, bool bMark);
@@ -251,6 +253,7 @@ class CWinGlkWndTextBuffer : public CWinGlkWnd
251253
void AddNewParagraph(void);
252254
void GetLastShown(int &iLastPara, int& iLastChar);
253255
void ClearFormatting(int iPara);
256+
void ResizeGraphics(CRect* pWindowSize);
254257

255258
template<class XCHAR> void PaintInputBuffer(
256259
CWinGlkDC& dc, const XCHAR* input, int inputLen, CPaintInfo& Info);

0 commit comments

Comments
 (0)