forked from Gargaj/Bonzomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatform.cpp
631 lines (515 loc) · 14.6 KB
/
Platform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Some of this code was originally written for the ScintillaGL project by:
// Copyright 2011 by Mykhailo Parfeniuk
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include <assert.h>
#include <vector>
#include <map>
#include "Platform.h"
#include "Scintilla.h"
#include "UniConversion.h"
#include "XPM.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"
#include "Renderer.h"
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
//////////////////////////////////////////////////////////////////////////
ColourDesired MakeRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a=0xFF)
{
return a<<24 | b<<16 | g<<8 | r;
}
ColourDesired Platform::Chrome()
{
return MakeRGBA(0xe0, 0xe0, 0xe0);
}
ColourDesired Platform::ChromeHighlight()
{
return MakeRGBA(0xff, 0xff, 0xff);
}
const char *Platform::DefaultFont()
{
return "Lucida Console";
}
int Platform::DefaultFontSize()
{
return 10;
}
unsigned int Platform::DoubleClickTime()
{
return 500; // Half a second
}
bool Platform::MouseButtonBounce()
{
return true;
}
void Platform::Assert(const char *c, const char *file, int line)
{
char buffer[2000];
sprintf(buffer, "Assertion [%s] failed at %s %d", c, file, line);
strcat(buffer, "\r\n");
assert(false);
}
int Platform::Minimum(int a, int b) { return a<b ? a : b; }
int Platform::Maximum(int a, int b) { return a>b ? a : b; }
int Platform::Clamp(int val, int minVal, int maxVal) { return Minimum( maxVal, Maximum( val, minVal ) ); }
#ifdef TRACE
void Platform::DebugPrintf(const char *format, ...)
{
char buffer[2000];
va_list pArguments;
va_start(pArguments, format);
vsprintf(buffer,format,pArguments);
va_end(pArguments);
Platform::DebugDisplay(buffer);
}
#else
void Platform::DebugPrintf(const char *, ...)
{
}
#endif
//////////////////////////////////////////////////////////////////////////
// FONT
#define CHARACTER_COUNT 512 // first 512 chars of unicode should be sufficient
struct stbtt_Font
{
stbtt_fontinfo fontinfo;
stbtt_bakedchar cdata[CHARACTER_COUNT];
float scale;
Renderer::Texture * texture;
};
Font::Font() : fid(0)
{
}
Font::~Font()
{
}
void Font::Create(const FontParameters &fp)
{
stbtt_Font* newFont = new stbtt_Font;
FILE* f = fopen(fp.faceName, "rb");
assert(f);
fseek(f, 0, SEEK_END);
size_t len = ftell(f);
fseek(f, 0, SEEK_SET);
int texSize = 512;
unsigned char* buf = (unsigned char*)malloc(len);
fread(buf, 1, len, f);
fclose(f);
unsigned char* bmp = new unsigned char[texSize*texSize];
stbtt_BakeFontBitmap(buf, 0, fp.size, bmp, texSize, texSize, 0, CHARACTER_COUNT, newFont->cdata); // no guarantee this fits!
#ifdef _DEBUG
FILE* dump = fopen("font.raw", "wb");
fwrite(bmp,texSize,texSize,dump);
fclose(dump);
#endif // _DEBUG
newFont->texture = Renderer::CreateA8TextureFromData( texSize, texSize, bmp );
stbtt_InitFont(&newFont->fontinfo, buf, 0);
newFont->scale = stbtt_ScaleForPixelHeight(&newFont->fontinfo, fp.size);
delete [] bmp;
fid = newFont;
}
void Font::Release()
{
if (fid)
{
free(((stbtt_Font*)fid)->fontinfo.data);
Renderer::ReleaseTexture( ((stbtt_Font*)fid)->texture );
delete (stbtt_Font*)fid;
}
}
//////////////////////////////////////////////////////////////////////////
// SURFACE
#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif
class SurfaceImpl : public Surface {
ColourDesired penColour;
float currentX;
float currentY;
bool unicodeMode;
int codePage;
bool initialised;
PRectangle clipRect;
public:
SurfaceImpl();
virtual ~SurfaceImpl();
void Init(WindowID wid);
void Init(SurfaceID sid, WindowID wid);
void InitPixMap(int width, int height, Surface *surface, WindowID wid);
void Release();
bool Initialised();
void PenColour(ColourDesired fore);
int LogPixelsY();
int DeviceHeightFont(int points);
void MoveTo(float x, float y);
void LineTo(float x, float y);
void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back);
void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back);
void FillRectangle(PRectangle rc, ColourDesired back);
void FillRectangle(PRectangle rc, Surface &surfacePattern);
void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back);
void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int flags);
void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back);
void Copy(PRectangle rc, Point from, Surface &surfaceSource);
//virtual void DrawPixmap(PRectangle rc, Point from, Pixmap pixmap);
virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage);
void DrawTextBase(PRectangle rc, Font &font, float ybase, const char *s, int len, ColourDesired fore);
void DrawTextNoClip(PRectangle rc, Font &font, float ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
void DrawTextClipped(PRectangle rc, Font &font, float ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
void DrawTextTransparent(PRectangle rc, Font &font, float ybase, const char *s, int len, ColourDesired fore);
void MeasureWidths(Font &font, const char *s, int len, float *positions);
float WidthText(Font &font, const char *s, int len);
float WidthChar(Font &font, char ch);
float Ascent(Font &font);
float Descent(Font &font);
float InternalLeading(Font &font);
float ExternalLeading(Font &font);
float Height(Font &font);
float AverageCharWidth(Font &font);
void MoveTo(int x, int y);
void LineTo(int x, int y);
void SetClip(PRectangle rc);
void FlushCachedState();
void SetUnicodeMode(bool unicodeMode_);
void SetDBCSMode(int codePage);
};
#ifdef SCI_NAMESPACE
}
#endif
SurfaceImpl::SurfaceImpl()
: currentX(0), currentY(0)
{
unicodeMode = false;
codePage = 0;
initialised = false;
}
SurfaceImpl::~SurfaceImpl()
{
}
void SurfaceImpl::Init( WindowID wid )
{
initialised = true;
}
void SurfaceImpl::Init( SurfaceID sid, WindowID wid )
{
initialised = true;
}
void SurfaceImpl::InitPixMap( int width, int height, Surface *surface, WindowID wid )
{
initialised = true;
}
void SurfaceImpl::Release()
{
}
bool SurfaceImpl::Initialised()
{
return initialised;
}
void SurfaceImpl::PenColour(ColourDesired fore)
{
penColour = fore;
}
int SurfaceImpl::LogPixelsY()
{
return 72;
}
int SurfaceImpl::DeviceHeightFont(int points)
{
int logPix = LogPixelsY();
return (points * logPix + logPix / 2) / 72;
}
void SurfaceImpl::MoveTo(float x, float y)
{
currentX = x;
currentY = y;
}
void SurfaceImpl::LineTo(float targetX, float targetY)
{
Renderer::RenderLine(
Renderer::Vertex( currentX+0.5f, currentY+0.5f, penColour.AsLong() ),
Renderer::Vertex( targetX+0.5f, targetY+0.5f, penColour.AsLong() )
);
currentX = targetX;
currentY = targetY;
}
void SurfaceImpl::MoveTo( int x, int y )
{
MoveTo( (float)x, (float)y );
}
void SurfaceImpl::LineTo( int x, int y )
{
LineTo( (float)x, (float)y );
}
void SurfaceImpl::Polygon(Point* /*pts*/, int /*npts*/, ColourDesired /*fore*/, ColourDesired /*back*/)
{
assert(!"Implemented");
}
void SurfaceImpl::RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)
{
FillRectangle(rc, back);
PenColour(fore);
MoveTo( rc.left , rc.top );
LineTo( rc.right, rc.top );
LineTo( rc.right, rc.bottom );
LineTo( rc.left, rc.bottom);
LineTo( rc.left, rc.top );
}
void SurfaceImpl::FillRectangle(PRectangle rc, ColourDesired back)
{
Renderer::BindTexture(NULL);
Renderer::RenderQuad(
Renderer::Vertex( rc.left , rc.top, back.AsLong() ),
Renderer::Vertex( rc.right, rc.top, back.AsLong() ),
Renderer::Vertex( rc.right, rc.bottom, back.AsLong() ),
Renderer::Vertex( rc.left , rc.bottom, back.AsLong() )
);
}
void SurfaceImpl::FillRectangle(PRectangle rc, Surface & surfacePattern)
{
FillRectangle( rc, 0xd0000000 );
}
void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)
{
RectangleDraw( rc, fore, back );
}
void SurfaceImpl::AlphaRectangle(PRectangle rc, int /*cornerSize*/, ColourDesired fill, int alphaFill,
ColourDesired /*outline*/, int /*alphaOutline*/, int /*flags*/)
{
unsigned int back = fill.AsLong() & 0xFFFFFF | ((alphaFill & 0xFF) << 24);
FillRectangle(rc, back);
}
void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage)
{
assert(!"Implemented");
}
void SurfaceImpl::Ellipse(PRectangle /*rc*/, ColourDesired /*fore*/, ColourDesired /*back*/)
{
assert(!"Implemented");
}
void SurfaceImpl::Copy( PRectangle rc, Point from, Surface &surfaceSource )
{
// we don't assert here because this is often used
// however, we don't support it right now
}
void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font, float ybase, const char *str, int len, ColourDesired fore)
{
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
Renderer::BindTexture( realFont->texture );
float x = rc.left, y = ybase;
while (len-- > 0)
{
unsigned int c = *str;
unsigned int charLength = UTF8CharLength(c);
if (charLength > 1)
{
c = 0;
UTF16FromUTF8( str, charLength, (wchar_t*)&c, sizeof(unsigned int) );
}
stbtt_aligned_quad quad;
stbtt_GetBakedQuad( realFont->cdata, realFont->texture->width, realFont->texture->height, c, &x, &y, &quad, 1 );
Renderer::RenderQuad(
Renderer::Vertex( quad.x0, quad.y0, fore.AsLong(), quad.s0, quad.t0 ),
Renderer::Vertex( quad.x1, quad.y0, fore.AsLong(), quad.s1, quad.t0 ),
Renderer::Vertex( quad.x1, quad.y1, fore.AsLong(), quad.s1, quad.t1 ),
Renderer::Vertex( quad.x0, quad.y1, fore.AsLong(), quad.s0, quad.t1 )
);
str += charLength;
len -= charLength - 1;
}
}
void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, float ybase, const char *s, int len,
ColourDesired fore, ColourDesired /*back*/)
{
DrawTextBase(rc, font, ybase, s, len, fore);
}
void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, float ybase, const char *s, int len,
ColourDesired fore, ColourDesired /*back*/)
{
DrawTextBase(rc, font, ybase, s, len, fore);
}
void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font, float ybase, const char *s, int len,
ColourDesired fore)
{
DrawTextBase(rc, font, ybase, s, len, fore);
}
void SurfaceImpl::MeasureWidths(Font & font, const char *str, int len, float *positions)
{
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
float position = 0;
char * p = (char*)str;
while (len-- > 0)
{
unsigned int c = *p;
unsigned int charLength = UTF8CharLength(c);
if (charLength > 1)
{
c = 0;
UTF16FromUTF8( str, charLength, (wchar_t*)&c, sizeof(unsigned int) );
}
int advance = 0, leftBearing = 0;
stbtt_GetCodepointHMetrics(&realFont->fontinfo, c, &advance, &leftBearing);
position += advance;
for (int i=0; i<charLength; i++) // we need to loop here because UTF8 characters count as multiple unless their position is the same
*positions++ = position*realFont->scale;
p += charLength;
len -= charLength - 1;
}
}
float SurfaceImpl::WidthText(Font & font, const char *str, int len)
{
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
float position = 0;
while (len-- > 0)
{
unsigned int c = *str;
unsigned int charLength = UTF8CharLength(c);
if (charLength > 1)
{
c = 0;
UTF16FromUTF8( str, charLength, (wchar_t*)&c, sizeof(unsigned int) );
}
int advance = 0, leftBearing = 0;
stbtt_GetCodepointHMetrics(&realFont->fontinfo, c, &advance, &leftBearing);
position += advance*realFont->scale;//TODO: +Kerning
str += charLength;
len -= charLength - 1;
}
return position;
}
float SurfaceImpl::WidthChar(Font &font, char ch)
{
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
int advance, leftBearing;
stbtt_GetCodepointHMetrics(&realFont->fontinfo, ch, &advance, &leftBearing);
return advance * realFont->scale;
}
float SurfaceImpl::Ascent(Font &font)
{
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&realFont->fontinfo, &ascent, &descent, &lineGap);
return ascent * realFont->scale;
}
float SurfaceImpl::Descent(Font &font)
{
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&realFont->fontinfo, &ascent, &descent, &lineGap);
return -descent * realFont->scale;
}
float SurfaceImpl::InternalLeading(Font &)
{
//WTF is this?????
return 0;
}
float SurfaceImpl::ExternalLeading(Font& font)
{
//WTF is this?????
stbtt_Font* realFont = (stbtt_Font*)font.GetID();
int ascent, descent, lineGap;
stbtt_GetFontVMetrics(&realFont->fontinfo, &ascent, &descent, &lineGap);
return lineGap * realFont->scale;
}
float SurfaceImpl::Height(Font &font)
{
return Ascent(font) + Descent(font);
}
float SurfaceImpl::AverageCharWidth(Font &font)
{
return WidthChar(font, 'n');
}
void SurfaceImpl::SetClip(PRectangle rc)
{
// we deal with this in the renderer
}
void SurfaceImpl::FlushCachedState()
{}
void SurfaceImpl::SetUnicodeMode( bool mode )
{
unicodeMode = mode;
}
void SurfaceImpl::SetDBCSMode( int cp )
{
codePage = cp;
}
Surface *Surface::Allocate(int technology)
{
return new SurfaceImpl;
}
//////////////////////////////////////////////////////////////////////////
// Window
Window::~Window()
{
}
void Window::Destroy()
{
}
bool Window::HasFocus()
{
return false;
}
std::map<Scintilla::WindowID,Scintilla::PRectangle> rects;
PRectangle Window::GetPosition()
{
return rects[wid];
}
void Window::SetPosition(PRectangle rc)
{
rects[wid] = rc;
}
void Window::SetPositionRelative(PRectangle rc, Window w)
{
}
PRectangle Window::GetClientPosition()
{
return PRectangle( 0, 0, rects[wid].Width(), rects[wid].Height() );
}
void Window::Show(bool show)
{
}
void Window::InvalidateAll()
{
}
void Window::InvalidateRectangle(PRectangle rc)
{
}
void Window::SetFont(Font &font)
{
}
void Window::SetCursor(Cursor curs)
{
}
PRectangle Window::GetMonitorRect(Point pt)
{
return PRectangle( 0, 0, Renderer::nWidth, Renderer::nHeight );
}
//////////////////////////////////////////////////////////////////////////
// Menus
Menu::Menu() : mid(0)
{
assert(!"Implemented");
}
void Menu::CreatePopUp()
{
assert(!"Implemented");
}
void Menu::Destroy()
{
assert(!"Implemented");
}
void Menu::Show(Point pt, Window &w)
{
assert(!"Implemented");
}
//////////////////////////////////////////////////////////////////////////
// ListBox implementation
ListBox *ListBox::Allocate()
{
return NULL;
}