00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <nds.h>
00021 #include <stdio.h>
00022 #include <ft2build.h>
00023 #include FT_FREETYPE_H
00024
00025 #include <string>
00026 #include <vector>
00027 using std::string;
00028 using std::vector;
00029
00030 #include "utf8.h"
00031
00032 #include "types.h"
00033 #include "textbox.h"
00034 #include "textboxhandler.h"
00035 #include "files.h"
00036
00037 void TextBoxHandler::Init()
00038 {
00039 if (FT_Init_FreeType (&library_))
00040 iprintf ("Error ocurred in FreeType load. ");
00041
00042
00043 faces_.push_back(NULL);
00044 faces_.push_back(NULL);
00045 faces_[Types::MONA_FONT] = NULL;
00046 faces_[Types::VERA_FONT] = NULL;
00047 }
00048
00049 TextBox* TextBoxHandler::NewTextBox (Types::Screen::selector screen,
00050 int bgid, Types::Font font, int size,
00051 int x, int y, int width, int height)
00052 {
00053 if (faces_[font] == NULL)
00054 {
00055
00056 FT_Face face;
00057 int error = FT_New_Face (library_, Files::filepath(font).c_str(), 0, &face);
00058 if (error == FT_Err_Unknown_File_Format)
00059 {
00060 iprintf ("the font file could be opened and read, but it appears that "
00061 "its font format is unsupported.");
00062 }
00063 else if (error)
00064 {
00065 iprintf ("the font file could not be opened or read, or it's broken.");
00066 }
00067 else
00068
00069 faces_[font] = face;
00070 }
00071 TextBox* tb = new TextBox ();
00072 tb->Init(screen, bgid, faces_[font], size, x, y, width, height);
00073 text_boxes_.push_back(tb);
00074 return tb;
00075 }
00076
00077 void TextBoxHandler::DestroyTextBox (TextBox* tb)
00078 {
00079 size_t i = 0;
00080 for (; text_boxes_[i] != tb && i < text_boxes_.size(); ++i);
00081 if (text_boxes_[i] == tb)
00082 {
00083 delete tb;
00084 text_boxes_.erase(text_boxes_.begin()+i);
00085 }
00086 }
00087
00088 void TextBoxHandler::PrintTextBox (TextBox* tb)
00089 {
00090 size_t i = 0;
00091 for (; text_boxes_[i] != tb && i < text_boxes_.size(); ++i);
00092 if (text_boxes_[i] == tb)
00093 {
00094
00095 if (i > 0)
00096 if (!text_boxes_[i]->independent() && !text_boxes_[i-1]->independent() &&
00097 !text_boxes_[i]->floats() && !text_boxes_[i-1]->floats() &&
00098 text_boxes_[i]->bgid() == text_boxes_[i-1]->bgid())
00099 text_boxes_[i]->Adjust(text_boxes_[i-1]->y() + text_boxes_[i-1]->height());
00100
00101 text_boxes_[i]->Print();
00102 }
00103 }
00104
00105 void TextBoxHandler::PrintAll (Types::Screen::selector screen)
00106 {
00107 for (size_t i = 0; i < text_boxes_.size(); ++i)
00108 {
00109 if (screen == text_boxes_[i]->screen() && text_boxes_[i]->visible())
00110 {
00111 text_boxes_[i]->Print();
00112 if (i < text_boxes_.size()-1)
00113 if (!text_boxes_[i]->independent() && !text_boxes_[i+1]->independent() &&
00114 !text_boxes_[i]->floats() && !text_boxes_[i+1]->floats() &&
00115 text_boxes_[i]->bgid() == text_boxes_[i+1]->bgid())
00116 text_boxes_[i+1]->Adjust(text_boxes_[i]->y() + text_boxes_[i]->height());
00117 }
00118 }
00119 }
00120
00121 TextBoxHandler::~TextBoxHandler()
00122 {
00123 for (size_t i = 0; i < faces_.size(); ++i)
00124 FT_Done_Face(faces_[i]);
00125 FT_Done_FreeType(library_);
00126 }
00127
00128