~frantisek-princ/helenos/ext4

« back to all changes in this revision

Viewing changes to uspace/lib/draw/font.c

  • Committer: Frantisek Princ
  • Date: 2012-08-24 14:07:52 UTC
  • mfrom: (1235.1.401 HelenOS.mainline)
  • Revision ID: frantisek.princ@gmail.com-20120824140752-pihztxo52535kxo1
Merge with mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2012 Petr Koupy
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * - Redistributions of source code must retain the above copyright
 
10
 *   notice, this list of conditions and the following disclaimer.
 
11
 * - Redistributions in binary form must reproduce the above copyright
 
12
 *   notice, this list of conditions and the following disclaimer in the
 
13
 *   documentation and/or other materials provided with the distribution.
 
14
 * - The name of the author may not be used to endorse or promote products
 
15
 *   derived from this software without specific prior written permission.
 
16
 *
 
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 */
 
28
 
 
29
/** @addtogroup draw
 
30
 * @{
 
31
 */
 
32
/**
 
33
 * @file
 
34
 */
 
35
 
 
36
#include <assert.h>
 
37
#include <malloc.h>
 
38
 
 
39
#include "font.h"
 
40
#include "font/embedded.h"
 
41
#include "drawctx.h"
 
42
 
 
43
void font_init(font_t *font, font_decoder_type_t decoder, char *path, uint16_t points)
 
44
{
 
45
        font->points = points;
 
46
 
 
47
        switch (decoder) {
 
48
        case FONT_DECODER_EMBEDDED:
 
49
                font->decoder = &fd_embedded;
 
50
                break;
 
51
        default:
 
52
                font->decoder = NULL;
 
53
                break;
 
54
        }
 
55
 
 
56
        if (font->decoder) {
 
57
                font->decoder->init(path, &font->glyph_count, &font->decoder_data);
 
58
 
 
59
                if (font->glyph_count > 0) {
 
60
                        font->glyphs = (surface_t **) malloc(sizeof(surface_t *) * font->glyph_count);
 
61
                } else {
 
62
                        font->glyphs = NULL;
 
63
                }
 
64
 
 
65
                if (font->glyphs) {
 
66
                        for (size_t i = 0; i < font->glyph_count; ++i) {
 
67
                                font->glyphs[i] = NULL;
 
68
                        }
 
69
                } else {
 
70
                        font->glyph_count = 0;
 
71
                }
 
72
        } else {
 
73
                font->glyph_count = 0;
 
74
                font->glyphs = NULL;
 
75
                font->decoder_data = NULL;
 
76
        }
 
77
}
 
78
 
 
79
void font_release(font_t *font)
 
80
{
 
81
        if (font->glyphs) {
 
82
                for (size_t i = 0; i < font->glyph_count; ++i) {
 
83
                        if (font->glyphs[i]) {
 
84
                                surface_destroy(font->glyphs[i]);
 
85
                        }
 
86
                }
 
87
                free(font->glyphs);
 
88
        }
 
89
        
 
90
        if (font->decoder) {
 
91
                font->decoder->release(font->decoder_data);
 
92
        }
 
93
}
 
94
 
 
95
void font_get_box(font_t *font, char *text, sysarg_t *width, sysarg_t *height)
 
96
{
 
97
        assert(width);
 
98
        assert(height);
 
99
 
 
100
        (*width) = 0;
 
101
        (*height) = 0;
 
102
 
 
103
        if (!text) {
 
104
                return;
 
105
        }
 
106
 
 
107
        while (*text) {
 
108
                uint16_t glyph_idx = font->decoder->resolve(*text, font->decoder_data);
 
109
                if (glyph_idx < font->glyph_count) {
 
110
                        if (!font->glyphs[glyph_idx]) {
 
111
                                font->glyphs[glyph_idx] =
 
112
                                    font->decoder->render(glyph_idx, font->points);
 
113
                        }
 
114
 
 
115
                        surface_t *glyph = font->glyphs[glyph_idx];
 
116
                        if (glyph) {
 
117
                                sysarg_t w;
 
118
                                sysarg_t h;
 
119
                                surface_get_resolution(glyph, &w, &h);
 
120
                                (*width) += w;
 
121
                                (*height) = (*height) < h ? h : (*height);
 
122
                        }
 
123
                }
 
124
                ++text;
 
125
        }
 
126
}
 
127
 
 
128
void font_draw_text(font_t *font, drawctx_t *context, source_t *source,
 
129
    const char *text, sysarg_t x, sysarg_t y)
 
130
{
 
131
        assert(context);
 
132
        assert(source);
 
133
 
 
134
        drawctx_save(context);
 
135
        drawctx_set_compose(context, compose_over);
 
136
 
 
137
        while (*text) {
 
138
                uint16_t glyph_idx = font->decoder->resolve(*text, font->decoder_data);
 
139
                if (glyph_idx < font->glyph_count) {
 
140
                        if (!font->glyphs[glyph_idx]) {
 
141
                                font->glyphs[glyph_idx] =
 
142
                                    font->decoder->render(glyph_idx, font->points);
 
143
                        }
 
144
 
 
145
                        surface_t *glyph = font->glyphs[glyph_idx];
 
146
                        if (glyph) {
 
147
                                sysarg_t w;
 
148
                                sysarg_t h;
 
149
                                surface_get_resolution(glyph, &w, &h);
 
150
 
 
151
                                transform_t transform;
 
152
                                transform_identity(&transform);
 
153
                                transform_translate(&transform, x, y);
 
154
                                source_set_transform(source, transform);
 
155
                                source_set_mask(source, glyph, false);
 
156
                                drawctx_transfer(context, x, y, w, h);
 
157
 
 
158
                                x += w;
 
159
                        }
 
160
                }
 
161
                ++text;
 
162
        }
 
163
 
 
164
        drawctx_restore(context);
 
165
        source_set_mask(source, NULL, false);
 
166
}
 
167
 
 
168
/** @}
 
169
 */