Ecere SDK/eC Forums • View topic - 字体的使用方法




Ecere SDK/eC Forums

Ecere Developers Community

字体的使用方法

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.

字体的使用方法

Postby liqi98136 » Sun Sep 25, 2011 8:42 pm

方法1
利用window.font属性改变其surface.font

Code: Select all

class Form1 : Window
{
   text = "Fonts";
   background = activeBorder;
 ...   
   font = { "Comic Sans MS", 10, bold = true };
...
void OnRedraw(Surface surface)
   {
...
surface.WriteText( 50, 20,  str,  strlen(str));
...

整个surface使用父字体
方法2
定义资源
Code: Select all

FontResource fontRes{ faceName = "Comic Sans MS", size = 8.25f ,bold = true,italic = true,underline  = true ,window=this};

surface.TextFont(fontRes.font);
surface.WriteText( 50, 20,  str,  strlen(str));

系统自动管理字体
方法3
通过显示系统Display,手动管理
Code: Select all


Font fonts ;
...
bool OnLoadGraphics(void)
   {
      fonts = displaySystem.LoadFont("Comic Sans MS", 10, 0);//*方法3*/
      return true;
   }

   void OnUnloadGraphics(void)
   {
      displaySystem.UnloadFont(fonts);//*方法3*/
   }
...
surface.TextFont(fonts);
surface.WriteText( 50, 20,  str,  strlen(str));
...


完整实例
Code: Select all

import "ecere"

static char * verse1 = "Not marble, nor the gilded monuments";
static char * verse2 = "Of princes, shall outlive this powerful rhyme;";
static char * verse3 = "But you shall shine more bright in these contents";
static char * verse4 = "Than unswept stone, besmear'd with sluttish time.";
static char * verse5 = "When wasteful war shall statues overturn,";
static char * verse6 = "And broils root out the work of masonry,";
static char * verse7 = "Nor Mars his sword, nor war's quick fire shall burn";
static char * verse8 = "The living record of your memory.";
static char * verse9 = "'Gainst death, and all oblivious enmity";
static char * verse10 = "Shall you pace forth; your praise shall still find room ";
static char * verse11 = "Even in the eyes of all posterity";
static char * verse12 = "That wear this world out to the ending doom.";
static char * verse13 = "So, till the judgment that yourself arise,";
static char * verse14 = "You live in this, and dwell in lovers' eyes.";

class Form1 : Window
{
   text = "Fonts";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 464, 336 };
   anchor = { horz = -27, vert = -60 };
   
   
   //font = { "Comic Sans MS", 10, bold = true };//方法1
   //FontResource fontRes{ faceName = "Comic Sans MS", size = 8.25f ,bold = true,italic = true,underline  = true,window=this};//*方法2*/

   Font fonts ;
   void OnRedraw(Surface surface)
   {
   
    surface.TextFont(fonts);
    //surface.TextFont(fontRes.font);

    surface.WriteText( 50, 20,  verse1,  strlen(verse1));
    surface.WriteText( 50, 40,  verse2,  strlen(verse2));
    surface.WriteText( 50, 60,  verse3,  strlen(verse3));
    surface.WriteText( 50, 80,  verse4,  strlen(verse4));
    surface.WriteText( 50, 100, verse5,  strlen(verse5));
    surface.WriteText( 50, 120, verse6,  strlen(verse6));
    surface.WriteText( 50, 140, verse7,  strlen(verse7));
    surface.WriteText( 50, 160, verse8,  strlen(verse8));
    surface.WriteText( 50, 180, verse9,  strlen(verse9));
    surface.WriteText( 50, 200, verse10, strlen(verse10));
    surface.WriteText( 50, 220, verse11, strlen(verse11));
    surface.WriteText( 50, 240, verse12, strlen(verse12));
    surface.WriteText( 50, 260, verse13, strlen(verse13));
    surface.WriteText( 50, 280, verse14, strlen(verse14));
   

   }

   bool OnLoadGraphics(void)
   {
      fonts = displaySystem.LoadFont("Comic Sans MS", 10, 0);//*方法3*/
      return true;
   }

   void OnUnloadGraphics(void)
   {
      displaySystem.UnloadFont(fonts);//*方法3*/
   }
}

Form1 form1 {};

liqi98136
 
Posts: 53
Joined: Sun Jan 17, 2010 11:37 pm

Re: 字体的使用,后面两种方法有时候不起作用?

Postby jerome » Sun Sep 25, 2011 9:33 pm

The usual ways to use fonts is through the Window::font property.

Defining a FontResource does not automatically load it.

1. You might want however to use one than more font in a specific window, in which sense it makes sense to define additional font resources. This is done in a way very similar to the BitmapResource usage (BitmapResource and FontResource both inherit from the same class to manage DisplaySystem resources). You simply need to set to the 'window' member of the FontResource to the Window that will make use of it:
Code: Select all
FontResource fontRes{ "Comic Sans MS", 8.25f, window = this };

2. You can skip the FontResource class, and load the font directly (Just like you can load a Bitmap object directly). This should be done in the OnLoadGraphics window event, and the font should be unloaded in OnUnloadGraphics:
Code: Select all
   Font myFont;
   bool OnLoadGraphics()
   {
      myFont = displaySystem.LoadFont("Comic Sans MS", 10, 0);
      return true;
   }
 
   void OnUnloadGraphics()
   {
      displaySystem.UnloadFont(myFont);
   }
 
   void OnRedraw(Surface surface)
   {
      surface.font = myFont;
      surface.WriteText( 50, 20, verse1, strlen(verse1));
   }

3. You should not create a new DisplaySystem (Especially not in an OnRedraw method!) when you already have a Window object, which already has a DisplaySystem (accessible as 'displaySystem'). The only time it would be useful to create your own DisplaySystem would be to render to a memory bitmap. You could then create an LFB system for that purpose:
Code: Select all
import "ecere"
 
class FontTest : Application
{
   Bitmap bitmap { };
   DisplaySystem lfbSystem { };
   void Main()
   {
      Font font;
      lfbSystem.Create("LFB", null, false);
      font = lfbSystem.LoadFont("Comic Sans MS", 24, { });
      if(bitmap.Allocate(null, 600, 80, 0, pixelFormat888, false))
      {
         Surface surface = bitmap.GetSurface(0,0, null);
         char * text = "Form is emptiness, Emptiness is form";
         surface.background = darkBlue;
         surface.foreground = azure;
         surface.font = font;
         surface.Clear(colorBuffer);       
         surface.WriteText(10, 10, text, strlen(text));
         delete surface;
         bitmap.Save("fontTest.png", null, null);         
         ShellOpen("fontTest.png");
      }
      lfbSystem.UnloadFont(font);
   }
}


Regards,

Jerome
jerome
Site Admin
 
Posts: 614
Joined: Sat Jan 16, 2010 11:16 pm


Return to 欢迎使用中文讨论

Who is online

Users browsing this forum: No registered users and 5 guests

cron