st/vega: Add color transformation support.
[mesa.git] / src / gallium / state_trackers / vega / api_text.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #include "VG/openvg.h"
28
29 #include "vg_context.h"
30 #include "text.h"
31 #include "api.h"
32
33 #include "util/u_memory.h"
34
35 #ifdef OPENVG_VERSION_1_1
36
37 VGFont vegaCreateFont(VGint glyphCapacityHint)
38 {
39 struct vg_context *ctx = vg_current_context();
40
41 if (glyphCapacityHint < 0) {
42 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
43 return VG_INVALID_HANDLE;
44 }
45
46 return (VGFont) font_create(glyphCapacityHint);
47 }
48
49 void vegaDestroyFont(VGFont f)
50 {
51 struct vg_font *font = (struct vg_font *)f;
52 struct vg_context *ctx = vg_current_context();
53
54 if (f == VG_INVALID_HANDLE) {
55 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
56 return;
57 }
58 if (!vg_object_is_valid((void *) font, VG_OBJECT_FONT)) {
59 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
60 return;
61 }
62
63 font_destroy(font);
64 }
65
66 void vegaSetGlyphToPath(VGFont font,
67 VGuint glyphIndex,
68 VGPath path,
69 VGboolean isHinted,
70 const VGfloat glyphOrigin[2],
71 const VGfloat escapement[2])
72 {
73 struct vg_context *ctx = vg_current_context();
74 struct path *pathObj;
75 struct vg_font *f;
76
77 if (font == VG_INVALID_HANDLE ||
78 !vg_context_is_object_valid(ctx, VG_OBJECT_FONT, (void *)font)) {
79 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
80 return;
81 }
82 if (!glyphOrigin || !escapement ||
83 !is_aligned(glyphOrigin) || !is_aligned(escapement)) {
84 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
85 return;
86 }
87 if (path != VG_INVALID_HANDLE &&
88 !vg_context_is_object_valid(ctx, VG_OBJECT_PATH, (void *)path)) {
89 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
90 return;
91 }
92
93 pathObj = (struct path*) path;
94 f = (struct vg_font*) font;
95
96 font_set_glyph_to_path(f, glyphIndex, pathObj,
97 isHinted, glyphOrigin, escapement);
98 }
99
100 void vegaSetGlyphToImage(VGFont font,
101 VGuint glyphIndex,
102 VGImage image,
103 const VGfloat glyphOrigin[2],
104 const VGfloat escapement[2])
105 {
106 struct vg_context *ctx = vg_current_context();
107 struct vg_image *img_obj;
108 struct vg_font *f;
109
110 if (font == VG_INVALID_HANDLE ||
111 !vg_context_is_object_valid(ctx, VG_OBJECT_FONT, (void *)font)) {
112 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
113 return;
114 }
115 if (!glyphOrigin || !escapement ||
116 !is_aligned(glyphOrigin) || !is_aligned(escapement)) {
117 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
118 return;
119 }
120 if (image != VG_INVALID_HANDLE &&
121 !vg_context_is_object_valid(ctx, VG_OBJECT_IMAGE, (void *)image)) {
122 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
123 return;
124 }
125
126 img_obj = (struct vg_image*)image;
127 f = (struct vg_font*)font;
128
129 font_set_glyph_to_image(f, glyphIndex, img_obj, glyphOrigin, escapement);
130 }
131
132 void vegaClearGlyph(VGFont font,
133 VGuint glyphIndex)
134 {
135 struct vg_context *ctx = vg_current_context();
136 struct vg_font *f;
137
138 if (font == VG_INVALID_HANDLE) {
139 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
140 return;
141 }
142
143 f = (struct vg_font*) font;
144
145 font_clear_glyph(f, glyphIndex);
146 }
147
148 void vegaDrawGlyph(VGFont font,
149 VGuint glyphIndex,
150 VGbitfield paintModes,
151 VGboolean allowAutoHinting)
152 {
153 struct vg_context *ctx = vg_current_context();
154 struct vg_font *f;
155
156 if (font == VG_INVALID_HANDLE) {
157 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
158 return;
159 }
160 if (paintModes & (~(VG_STROKE_PATH|VG_FILL_PATH))) {
161 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
162 return;
163 }
164 f = (struct vg_font*)font;
165
166 font_draw_glyph(f, glyphIndex, paintModes, allowAutoHinting);
167 }
168
169 void vegaDrawGlyphs(VGFont font,
170 VGint glyphCount,
171 const VGuint *glyphIndices,
172 const VGfloat *adjustments_x,
173 const VGfloat *adjustments_y,
174 VGbitfield paintModes,
175 VGboolean allowAutoHinting)
176 {
177 struct vg_context *ctx = vg_current_context();
178 struct vg_font *f;
179
180 if (font == VG_INVALID_HANDLE) {
181 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
182 return;
183 }
184 if (glyphCount <= 0) {
185 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
186 return;
187 }
188 if (!glyphIndices || !is_aligned(glyphIndices)) {
189 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
190 return;
191 }
192 if ((adjustments_x && !is_aligned(adjustments_x)) ||
193 (adjustments_y && !is_aligned(adjustments_y))) {
194 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
195 return;
196 }
197 if (paintModes & (~(VG_STROKE_PATH|VG_FILL_PATH))) {
198 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
199 return;
200 }
201
202 f = (struct vg_font*)font;
203
204 font_draw_glyphs(f, glyphCount, glyphIndices,
205 adjustments_x, adjustments_y, paintModes, allowAutoHinting);
206 }
207
208 #endif /* OPENVG_VERSION_1_1 */