1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     font_CharWriter.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 33578 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_FONT_CTR_FONT_CHAR_WRITER_H_
17 #define NN_FONT_CTR_FONT_CHAR_WRITER_H_
18 
19 // Upper left origin
20 #define COORDINATE_LT 1
21 
22 #include <cstddef>
23 #include <cstdio>
24 #include <GLES2/gl2.h>
25 #include <nn/types.h>
26 #include <nn/math.h>
27 #include <nn/util/util_Color.h>
28 #include <nn/font/font_Font.h>
29 #include <nn/font/CTR/font_TextWriterResource.h>
30 
31 namespace nn {
32 namespace font {
33 namespace CTR {
34 
35 struct DispStringBuffer;
36 
37 //---------------------------------------------------------------------------
38 //
39 //---------------------------------------------------------------------------
40 class CharWriter
41 {
42 public:
43     /* ------------------------------------------------------------------------
44             Types
45        ------------------------------------------------------------------------ */
46 
47     //
48     enum GradationMode
49     {
50         GRADMODE_NONE,                          //
51         GRADMODE_H,                             //
52         GRADMODE_V,                             //
53 
54         NUM_OF_GRADMODE
55     };
56 
57 
58 
59     /* ------------------------------------------------------------------------
60             Constants
61        ------------------------------------------------------------------------ */
62     static const u32 DEFAULT_COLOR_MAPPING_MIN = 0x00000000UL;
63     static const u32 DEFAULT_COLOR_MAPPING_MAX = 0xFFFFFFFFUL;
64 
65 
66     /* ------------------------------------------------------------------------
67             Functions
68        ------------------------------------------------------------------------ */
69 
70     //
71     //
72 
73     //
74                         CharWriter();
75 
76     //
77                         ~CharWriter();
78 
79     //
80 
81     //
82     //
83 
84     //
85     //
86     //
87     //
SetFont(const Font * pFont)88     void                SetFont(const Font* pFont)  { m_pFont = pFont; }
89 
90     //
91     //
92     //
93     //
94     //
GetFont()95     const Font*         GetFont() const             { return m_pFont; }
96 
97     //
98 
99     //
100     //
101 
102     //
103     void                SetupGX();
104 
105     //
FinalizeGX()106     static void         FinalizeGX()
107     {
108         TextWriterResource::FinalizeGX();
109     }
110 
111     //
112 
113     //
114     //
115 
116     //
117     //
118     //
119     //
120     //
SetColorMapping(util::Color8 min,util::Color8 max)121     void                SetColorMapping(
122                             util::Color8  min,
123                             util::Color8  max
124     )
125     {
126         m_ColorMapping.min = min;
127         m_ColorMapping.max = max;
128     }
129 
130     //
131     //
132     //
133     //
GetColorMappingMin()134     const util::Color8  GetColorMappingMin() const { return m_ColorMapping.min; }
135 
136     //
137     //
138     //
139     //
GetColorMappingMax()140     const util::Color8  GetColorMappingMax() const { return m_ColorMapping.max; }
141 
142     //
ResetColorMapping()143     void                ResetColorMapping()
144     {
145         SetColorMapping(DEFAULT_COLOR_MAPPING_MIN, DEFAULT_COLOR_MAPPING_MAX);
146     }
147 
148     //
149 
150     //
151     //
152 
153     //
154     //
155     //
156     //
SetAlpha(u8 alpha)157     void                SetAlpha(u8 alpha)
158     {
159         m_Alpha = alpha;
160     }
161 
162     //Gets the additional alpha value used during text rendering.
163 
164     //
165     //
166     //
167     //
GetAlpha()168     u8                  GetAlpha() const    { return m_Alpha; }
169 
170     //
171     //
172     //
173     //
SetGradationMode(GradationMode mode)174     void                SetGradationMode(GradationMode mode)
175     {
176         NN_FONT_MINMAX_ASSERT(mode, GRADMODE_NONE, NUM_OF_GRADMODE - 1);
177         m_GradationMode = mode;
178         UpdateVertexColor();
179     }
180 
181     //
182     //
183     //
184     //
GetGradationMode()185     GradationMode       GetGradationMode() const    { return m_GradationMode; }
186 
187     //
188     //
189     //
190     //
SetTextColor(util::Color8 color)191     void                SetTextColor(util::Color8 color)
192     {
193         m_TextColors[internal::TEXTCOLOR_START] = color;
194         UpdateVertexColor();
195     }
196 
197     //
198     //
199     //
200     //
201     //
SetTextColor(util::Color8 start,util::Color8 end)202     void                SetTextColor(
203                             util::Color8  start,
204                             util::Color8  end
205     )
206     {
207         m_TextColors[internal::TEXTCOLOR_START] = start;
208         m_TextColors[internal::TEXTCOLOR_END  ] = end;
209         UpdateVertexColor();
210     }
211 
212     //
213     //
214     //
215     //
GetTextColor()216     const util::Color8   GetTextColor() const           { return m_TextColors[internal::TEXTCOLOR_START]; }
217 
218     //
219     //
220     //
221     //
GetGradationStartColor()222     const util::Color8   GetGradationStartColor() const { return m_TextColors[internal::TEXTCOLOR_START]; }
223 
224     //
225     //
226     //
227     //
GetGradationEndColor()228     const util::Color8   GetGradationEndColor() const   { return m_TextColors[internal::TEXTCOLOR_END]; }
229 
230     //
231 
232     //
233     //
234 
235     //
236     //
237     //
238     //
239     //
SetScale(f32 hScale,f32 vScale)240     void                SetScale(
241                             f32 hScale,
242                             f32 vScale
243     )
244     {
245         m_Scale.x = hScale;
246         m_Scale.y = vScale;
247     }
248 
249     //
250     //
251     //
252     //
SetScale(f32 hvScale)253     void                SetScale(f32 hvScale)
254     {
255         m_Scale.x = hvScale;
256         m_Scale.y = hvScale;
257     }
258 
259     //
260     //
261     //
262     //
GetScaleH()263     f32                 GetScaleH() const   { return m_Scale.x; }
264 
265     //
266     //
267     //
268     //
GetScaleV()269     f32                 GetScaleV() const   { return m_Scale.y; }
270 
271     //
272     //
273     //
274     //
275     //
276     void                SetFontSize(
277                             f32 width,
278                             f32 height);
279 
280     //
281     //
282     //
283     //
284     void                SetFontSize(f32 height);
285 
286     //
287     //
288     //
289     //
290     //
291     f32                 GetFontWidth() const;
292 
293     //
294     //
295     //
296     //
297     //
298     f32                 GetFontHeight() const;
299 
300     //
301     //
302     //
303     //
304     f32                 GetFontAscent() const;
305 
306     //
307     //
308     //
309     //
310     f32                 GetFontDescent() const;
311 
312     //
313 
314     //
315     //
316 
317     //
318     //
319     //
320     //
EnableFixedWidth(bool isFixed)321     void                EnableFixedWidth(bool isFixed)  { m_IsWidthFixed = isFixed; }
322 
323     //
324     //
325     //
326     //
IsWidthFixed()327     bool                IsWidthFixed() const            { return m_IsWidthFixed; }
328 
329     //
330     //
331     //
332     //
SetFixedWidth(f32 width)333     void                SetFixedWidth(f32 width)        { m_FixedWidth = width; }
334 
335     //
336     //
337     //
338     //
GetFixedWidth()339     f32                 GetFixedWidth() const           { return m_FixedWidth; }
340 
341     //
342 
343     //
344     //
345 
346     //
347     //
348     //
349     //
350     //
351     //
352     f32                 Print(CharCode code);
353 
354     //
355     //
356     //
357     //
358     void                DrawGlyph(const Glyph& glyph);
359 
360     //
361 
362     //
363     //
364 
365     //
366     //
367     //
368     //
369     //
SetCursor(f32 x,f32 y)370     void                SetCursor(
371                             f32 x,
372                             f32 y
373     )
374     {
375         m_CursorPos.x = x;
376         m_CursorPos.y = y;
377     }
378 
379     //
380     //
381     //
382     //
383     //
384     //
SetCursor(f32 x,f32 y,f32 z)385     void                SetCursor(
386                             f32 x,
387                             f32 y,
388                             f32 z
389     )
390     {
391         m_CursorPos.x = x;
392         m_CursorPos.y = y;
393         m_CursorPos.z = z;
394     }
395 
396     //
397     //
398     //
399     //
400     //
MoveCursor(f32 dx,f32 dy)401     void                MoveCursor(
402                             f32 dx,
403                             f32 dy
404     )
405     {
406         m_CursorPos.x += dx;
407         m_CursorPos.y += dy;
408     }
409 
410     //
411     //
412     //
413     //
414     //
415     //
MoveCursor(f32 dx,f32 dy,f32 dz)416     void                MoveCursor(
417                             f32 dx,
418                             f32 dy,
419                             f32 dz
420     )
421     {
422         m_CursorPos.x += dx;
423         m_CursorPos.y += dy;
424         m_CursorPos.z += dz;
425     }
426 
427     //
428     //
429     //
430     //
SetCursorX(f32 x)431     void                SetCursorX(f32 x)           { m_CursorPos.x = x; }
432 
433     //
434     //
435     //
436     //
SetCursorY(f32 y)437     void                SetCursorY(f32 y)           { m_CursorPos.y = y; }
438 
439     //
440     //
441     //
442     //
SetCursorZ(f32 z)443     void                SetCursorZ(f32 z)           { m_CursorPos.z = z; }
444 
445     //
446     //
447     //
448     //
MoveCursorX(f32 dx)449     void                MoveCursorX(f32 dx)         { m_CursorPos.x += dx; }
450 
451     //
452     //
453     //
454     //
MoveCursorY(f32 dy)455     void                MoveCursorY(f32 dy)         { m_CursorPos.y += dy; }
456 
457     //
458     //
459     //
460     //
MoveCursorZ(f32 dz)461     void                MoveCursorZ(f32 dz)         { m_CursorPos.z += dz; }
462 
463     //
464     //
465     //
466     //
GetCursorX()467     f32                 GetCursorX() const          { return m_CursorPos.x; }
468 
469     //
470     //
471     //
472     //
GetCursorY()473     f32                 GetCursorY() const          { return m_CursorPos.y; }
474 
475     //
476     //
477     //
478     //
GetCursorZ()479     f32                 GetCursorZ() const          { return m_CursorPos.z; }
480 
481     //
482 
483     //
484     //
485 
486     //
487     //
488     //
489     //
GetTextWriterResource()490     TextWriterResource* GetTextWriterResource() const
491     {
492         return m_pTextWriterResource;
493     }
494 
495     //
496     //
497     //
498     //
SetTextWriterResource(TextWriterResource * pTextWriterResource)499     void                SetTextWriterResource(TextWriterResource* pTextWriterResource)
500     {
501         m_pTextWriterResource = pTextWriterResource;
502     }
503 
504     //
505 
506     //
507     //
508 
509     //
510     //
511     //
512     //
513     //
514     //
515     static u32          GetDispStringBufferSize(u32 charNum);
516 
517     //
518     //
519     //
520     //
521     //
522     //
523     //
524     static DispStringBuffer*
525                         InitDispStringBuffer(
526                             void*   drawBuffer,
527                             u32     charNum);
528 
529     //
530     //
531     //
532     //
GetDispStringBuffer()533     DispStringBuffer*   GetDispStringBuffer() const
534     {
535         return m_pDispStringBuffer;
536     }
537 
538     //
539     //
540     //
541     //
542     //
SetDispStringBuffer(DispStringBuffer * buffer)543     void                SetDispStringBuffer(DispStringBuffer* buffer)
544     {
545         m_pDispStringBuffer = buffer;
546     }
547 
548     //
549     //
550     //
551     void                StartPrint();
552 
553     //
554     //
555     //
556     void                EndPrint();
557 
558     //
559     //
560     void                UseCommandBuffer();
561 
562     //
563 
564 private:
565 
566     /* ------------------------------------------------------------------------
567             Types
568        ------------------------------------------------------------------------ */
569     typedef math::VEC2      CharScale;
570     typedef math::VEC3      CursorPos;
571 
572     struct ColorMapping
573     {
574         util::Color8        min;
575         util::Color8        max;
576     };
577 
578     /* ------------------------------------------------------------------------
579             Functions
580        ------------------------------------------------------------------------ */
581     //---- Prepare to draw
582 
583     //
584     //
585     //
586     //
587     void                SetupGXDefault(bool bAlphaTex = false);
588 
589     //
590     //
591     //
592     //
593     //
594     void                SetupGXWithColorMapping(bool bAlphaTex = false);
595 
596     //
597     void                SetupVertexFormat();
598 
599     //
600     void                SetupGXCommon();
601 
602     //
603     //
604     //
605     //
606     //
607     //
608     void                PrintGlyph(
609                             f32             x,
610                             const Glyph&    glyph);
611 
612     //
613     void                UpdateVertexColor();
614 
615     //
616     //
617     //
618     //
619     void                LoadTexture(const Glyph& glyph);
620 
621     /* ------------------------------------------------------------------------
622             Variables
623        ------------------------------------------------------------------------ */
624     static const GLushort       s_VertexIndexs[];
625 
626     ColorMapping                m_ColorMapping;         //
627     NN_PADDING3;
628     util::Color8                m_VertexColors[internal::TRIFAN_VTX_MAX];
629                                                         //
630     util::Color8                m_TextColors[internal::TEXTCOLOR_MAX];  //
631     GradationMode               m_GradationMode;
632     CharScale                   m_Scale;                //
633     CursorPos                   m_CursorPos;            //
634     f32                         m_FixedWidth;           //
635     const Font*                 m_pFont;                //
636 
637     //
638     TextWriterResource*         m_pTextWriterResource;
639 
640     //
641     DispStringBuffer*           m_pDispStringBuffer;
642 
643     bool                        m_IsWidthFixed;         //
644     u8                          m_Alpha;                //
645     NN_PADDING2;
646 };
647 
648 }   // namespace CTR
649 }   // namespace font
650 }   // namespace nn
651 #endif //  NN_FONT_CTR_FONT_CHAR_WRITER_H_
652