1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // This is a test program that uses Minikin to layout and draw some text.
18 // At the moment, it just draws a string into /data/local/tmp/foo.pgm.
19 
20 #include <stdio.h>
21 #include <vector>
22 #include <fstream>
23 
24 #include <unicode/unistr.h>
25 #include <unicode/utf16.h>
26 
27 #include <minikin/MinikinFontFreeType.h>
28 #include <minikin/Layout.h>
29 
30 using std::vector;
31 using namespace android;
32 using namespace minikin;
33 
34 FT_Library library;  // TODO: this should not be a global
35 
makeFontCollection()36 FontCollection *makeFontCollection() {
37     vector<FontFamily *>typefaces;
38     const char *fns[] = {
39         "/system/fonts/Roboto-Regular.ttf",
40         "/system/fonts/Roboto-Italic.ttf",
41         "/system/fonts/Roboto-BoldItalic.ttf",
42         "/system/fonts/Roboto-Light.ttf",
43         "/system/fonts/Roboto-Thin.ttf",
44         "/system/fonts/Roboto-Bold.ttf",
45         "/system/fonts/Roboto-ThinItalic.ttf",
46         "/system/fonts/Roboto-LightItalic.ttf"
47     };
48 
49     FontFamily *family = new FontFamily();
50     FT_Face face;
51     FT_Error error;
52     for (size_t i = 0; i < sizeof(fns)/sizeof(fns[0]); i++) {
53         const char *fn = fns[i];
54         printf("adding %s\n", fn);
55         error = FT_New_Face(library, fn, 0, &face);
56         if (error != 0) {
57             printf("error loading %s, %d\n", fn, error);
58         }
59         MinikinFont *font = new MinikinFontFreeType(face);
60         family->addFont(font);
61     }
62     typefaces.push_back(family);
63 
64 #if 1
65     family = new FontFamily();
66     const char *fn = "/system/fonts/DroidSansDevanagari-Regular.ttf";
67     error = FT_New_Face(library, fn, 0, &face);
68     MinikinFont *font = new MinikinFontFreeType(face);
69     family->addFont(font);
70     typefaces.push_back(family);
71 #endif
72 
73     return new FontCollection(typefaces);
74 }
75 
runMinikinTest()76 int runMinikinTest() {
77     FT_Error error = FT_Init_FreeType(&library);
78     if (error) {
79         return -1;
80     }
81     Layout::init();
82 
83     FontCollection *collection = makeFontCollection();
84     Layout layout;
85     layout.setFontCollection(collection);
86     const char *text = "fine world \xe0\xa4\xa8\xe0\xa4\xae\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4\xe0\xa5\x87";
87     int bidiFlags = 0;
88     FontStyle fontStyle;
89     MinikinPaint paint;
90     paint.size = 32;
91     icu::UnicodeString icuText = icu::UnicodeString::fromUTF8(text);
92     layout.doLayout(icuText.getBuffer(), 0, icuText.length(), icuText.length(), bidiFlags, fontStyle, paint);
93     layout.dump();
94     Bitmap bitmap(250, 50);
95     layout.draw(&bitmap, 10, 40, 32);
96     std::ofstream o;
97     o.open("/data/local/tmp/foo.pgm", std::ios::out | std::ios::binary);
98     bitmap.writePnm(o);
99     return 0;
100 }
101 
main()102 int main() {
103     return runMinikinTest();
104 }
105