File: text_frame.cc

package info (click to toggle)
google-gadgets 0.11.2-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 16,820 kB
  • ctags: 20,323
  • sloc: cpp: 116,722; ansic: 18,000; sh: 9,269; makefile: 2,676; xml: 2,138; lex: 459
file content (399 lines) | stat: -rw-r--r-- 11,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
  Copyright 2008 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "text_frame.h"
#include "basic_element.h"
#include "color.h"
#include "gadget_consts.h"
#include "graphics_interface.h"
#include "string_utils.h"
#include "texture.h"
#include "view.h"
#include "small_object.h"

namespace ggadget{

static const Color kDefaultColor(0, 0, 0);

static const char *kAlignNames[] = {
  "left", "center", "right", "justify"
};
static const char *kVAlignNames[] = {
  "top", "middle", "bottom"
};
static const char *kTrimmingNames[] = {
  "none",
  "character",
  "word",
  "character-ellipsis",
  "word-ellipsis",
  "path-ellipsis"
};

class TextFrame::Impl : public SmallObject<> {
 public:
  Impl(BasicElement *owner, View *view)
    : owner_(owner),
      view_(view),
      font_(NULL),
      color_texture_(new Texture(kDefaultColor, 1.0)),
      width_(0.0),
      height_(0.0),
      size_(kDefaultFontSize),
      // font_name_ is left blank to indicate default font.
      flags_(CanvasInterface::TEXT_FLAGS_NONE),
      trimming_(CanvasInterface::TRIMMING_NONE),
      align_(CanvasInterface::ALIGN_LEFT),
      valign_(CanvasInterface::VALIGN_TOP),
      bold_(false),
      italic_(false),
      size_is_default_(true) {
  }

  ~Impl() {
    delete color_texture_;
    color_texture_ = NULL;
    ClearFont();
  }

  void ClearFont() {
    if (font_) {
      font_->Destroy();
      font_ = NULL;
    }
  }

  void ResetFont() {
    ClearFont();
    ResetExtents();
  }

  void ResetExtents() {
    width_ = height_ = 0;
    QueueDraw();
  }

  bool SetUpFont() {
    if (size_is_default_) {
      int default_size = view_->GetDefaultFontSize();
      if (default_size != size_) {
        size_ = default_size;
        ResetFont();
      }
    }

    // The FontInterface object is cached on draw.
    if (!font_) {
      font_ = view_->GetGraphics()->NewFont(
          font_name_.empty() ? kDefaultFontName : font_name_,
          size_,
          italic_ ? FontInterface::STYLE_ITALIC : FontInterface::STYLE_NORMAL,
          bold_ ? FontInterface::WEIGHT_BOLD : FontInterface::WEIGHT_NORMAL);
      if (!font_) {
        return false;
      }
    }

    if (text_.empty()) {
      width_ = height_ = 0;
    } else if (width_ == 0 && height_ == 0) {
      CanvasInterface *canvas = view_->GetGraphics()->NewCanvas(5, 5);
      canvas->GetTextExtents(text_.c_str(), font_, flags_, 0,
                             &width_, &height_);
      canvas->Destroy();
    }
    return true;
  }

  void QueueDraw() {
    if (owner_)
      owner_->QueueDraw();
  }

  BasicElement *owner_;
  View *view_;
  FontInterface *font_;
  Texture *color_texture_;
  double width_;
  double height_;
  double size_;

  std::string font_name_;
  std::string color_;
  std::string text_;

  CanvasInterface::TextFlag flags_    : 3;
  CanvasInterface::Trimming trimming_ : 3;
  CanvasInterface::Alignment align_   : 2;
  CanvasInterface::VAlignment valign_ : 2;
  bool bold_                          : 1;
  bool italic_                        : 1;
  bool size_is_default_               : 1;
};

TextFrame::TextFrame(BasicElement *owner, View *view)
    : impl_(new Impl(owner, view)) {
}

void TextFrame::RegisterClassProperties(
    TextFrame *(*delegate_getter)(BasicElement *),
    const TextFrame *(*delegate_getter_const)(BasicElement *)) {
  BasicElement *owner = impl_->owner_;
  ASSERT(owner);
  // Register Properties
  // All properties are registered except for GetText/SetText
  // since some elements call it "caption" while others call it "innerText."
  // Elements may also want to do special handling on SetText.
  owner->RegisterProperty("bold",
      NewSlot(&TextFrame::IsBold, delegate_getter_const),
      NewSlot(&TextFrame::SetBold, delegate_getter));
  owner->RegisterProperty("color",
      NewSlot(&TextFrame::GetColor, delegate_getter_const),
      NewSlot<void, const Variant &>(&TextFrame::SetColor, delegate_getter));
  owner->RegisterProperty("font",
      NewSlot(&TextFrame::GetFont, delegate_getter_const),
      NewSlot(&TextFrame::SetFont, delegate_getter));
  owner->RegisterProperty("italic",
      NewSlot(&TextFrame::IsItalic, delegate_getter_const),
      NewSlot(&TextFrame::SetItalic, delegate_getter));
  owner->RegisterProperty("size",
      NewSlot(&TextFrame::GetSize, delegate_getter_const),
      NewSlot(&TextFrame::SetSize, delegate_getter));
  owner->RegisterProperty("strikeout",
      NewSlot(&TextFrame::IsStrikeout, delegate_getter_const),
      NewSlot(&TextFrame::SetStrikeout, delegate_getter));
  owner->RegisterProperty("underline",
      NewSlot(&TextFrame::IsUnderline, delegate_getter_const),
      NewSlot(&TextFrame::SetUnderline, delegate_getter));
  owner->RegisterProperty("wordWrap",
      NewSlot(&TextFrame::IsWordWrap, delegate_getter_const),
      NewSlot(&TextFrame::SetWordWrap, delegate_getter));

  owner->RegisterStringEnumProperty("align",
      NewSlot(&TextFrame::GetAlign, delegate_getter_const),
      NewSlot(&TextFrame::SetAlign, delegate_getter),
      kAlignNames, arraysize(kAlignNames));
  owner->RegisterStringEnumProperty("vAlign",
      NewSlot(&TextFrame::GetVAlign, delegate_getter_const),
      NewSlot(&TextFrame::SetVAlign, delegate_getter),
      kVAlignNames, arraysize(kVAlignNames));
  owner->RegisterStringEnumProperty("trimming",
      NewSlot(&TextFrame::GetTrimming, delegate_getter_const),
      NewSlot(&TextFrame::SetTrimming, delegate_getter),
      kTrimmingNames, arraysize(kTrimmingNames));
}

TextFrame::~TextFrame() {
  delete impl_;
  impl_ = NULL;
}

CanvasInterface::Alignment TextFrame::GetAlign() const {
  return impl_->align_;
}

void TextFrame::SetAlign(CanvasInterface::Alignment align) {
  if (align != impl_->align_) {
    impl_->align_ = align;
    impl_->QueueDraw();
  }
}

bool TextFrame::IsBold() const {
  return impl_->bold_;
}

void TextFrame::SetBold(bool bold) {
  if (bold != impl_->bold_) {
    impl_->bold_ = bold;
    impl_->ResetFont();
  }
}

Variant TextFrame::GetColor() const {
  return Variant(Texture::GetSrc(impl_->color_texture_));
}

void TextFrame::SetColor(const Variant &color) {
  delete impl_->color_texture_;
  impl_->color_texture_ = impl_->view_->LoadTexture(color);
  if (!impl_->color_texture_)
    impl_->color_texture_ = new Texture(kDefaultColor, 1.0);
  impl_->QueueDraw();
}

void TextFrame::SetColor(const Color &color, double opacity) {
  delete impl_->color_texture_;
  impl_->color_texture_ = new Texture(color, opacity);
  impl_->QueueDraw();
}

std::string TextFrame::GetFont() const {
  return impl_->font_name_;
}

void TextFrame::SetFont(const char *font) {
  if (AssignIfDiffer(font, &impl_->font_name_, strcasecmp)) {
    impl_->ResetFont();
  }
}

bool TextFrame::IsItalic() const {
  return impl_->italic_;
}

void TextFrame::SetItalic(bool italic) {
  if (italic != impl_->italic_) {
    impl_->italic_ = italic;
    impl_->ResetFont();
  }
}

double TextFrame::GetSize() const {
  return impl_->size_is_default_ ? -1 : impl_->size_;
}

void TextFrame::SetSize(double size) {
  if (size == -1) {
    impl_->size_is_default_ = true;
    size = impl_->view_->GetDefaultFontSize();
  } else {
    impl_->size_is_default_ = false;
  }
  if (size != impl_->size_) {
    impl_->size_ = size;
    impl_->ResetFont();
  }
}

double TextFrame::GetCurrentSize() const {
  return impl_->size_;
}

bool TextFrame::IsStrikeout() const {
  return impl_->flags_ & CanvasInterface::TEXT_FLAGS_STRIKEOUT;
}

void TextFrame::SetStrikeout(bool strikeout) {
  if (strikeout == !(impl_->flags_ & CanvasInterface::TEXT_FLAGS_STRIKEOUT)) {
    // Casting to TextFlag to avoid conversion warning when compiling by the
    // latest gcc.
    impl_->flags_ = static_cast<CanvasInterface::TextFlag>(
        impl_->flags_ ^ CanvasInterface::TEXT_FLAGS_STRIKEOUT);
    impl_->ResetFont();
  }
}

CanvasInterface::Trimming TextFrame::GetTrimming() const {
  return impl_->trimming_;
}

void TextFrame::SetTrimming(CanvasInterface::Trimming trimming) {
  if (trimming != impl_->trimming_) {
    impl_->trimming_ = trimming;
    impl_->QueueDraw();
  }
}

bool TextFrame::IsUnderline() const {
  return impl_->flags_ & CanvasInterface::TEXT_FLAGS_UNDERLINE;
}

void TextFrame::SetUnderline(bool underline) {
  if (underline == !(impl_->flags_ & CanvasInterface::TEXT_FLAGS_UNDERLINE)) {
    // Casting to TextFlag to avoid conversion warning when compiling by the
    // latest gcc.
    impl_->flags_ = static_cast<CanvasInterface::TextFlag>(
        impl_->flags_ ^ CanvasInterface::TEXT_FLAGS_UNDERLINE);
    impl_->ResetFont();
  }
}

CanvasInterface::VAlignment TextFrame::GetVAlign() const {
  return impl_->valign_;
}

void TextFrame::SetVAlign(CanvasInterface::VAlignment valign) {
  if (valign != impl_->valign_) {
    impl_->valign_ = valign;
    impl_->QueueDraw();
  }
}

bool TextFrame::IsWordWrap() const {
  return impl_->flags_ & CanvasInterface::TEXT_FLAGS_WORDWRAP;
}

void TextFrame::SetWordWrap(bool wrap) {
  if (wrap == !(impl_->flags_ & CanvasInterface::TEXT_FLAGS_WORDWRAP)) {
    // Casting to TextFlag to avoid conversion warning when compiling by the
    // latest gcc.
    impl_->flags_ = static_cast<CanvasInterface::TextFlag>(
        impl_->flags_ ^ CanvasInterface::TEXT_FLAGS_WORDWRAP);
    impl_->ResetFont();
  }
}

std::string TextFrame::GetText() const {
  return impl_->text_;
}

bool TextFrame::SetText(const std::string &text) {
  if (text != impl_->text_) {
    impl_->text_ = text;
    impl_->ResetExtents();
    return true;
  }
  return false;
}

void TextFrame::DrawWithTexture(CanvasInterface *canvas, double x, double y,
                                double width, double height, Texture *texture) {
  ASSERT(texture);
  impl_->SetUpFont();
  if (impl_->font_ && !impl_->text_.empty()) {
    texture->DrawText(canvas, x, y, width, height,
                      impl_->text_.c_str(), impl_->font_,
                      impl_->align_, impl_->valign_,
                      impl_->trimming_, impl_->flags_);
  }
}

void TextFrame::Draw(CanvasInterface *canvas, double x, double y,
                     double width, double height) {
  DrawWithTexture(canvas, x, y, width, height, impl_->color_texture_);
}

void TextFrame::GetSimpleExtents(double *width, double *height) {
  impl_->SetUpFont();
  *width = impl_->width_;
  *height = impl_->height_;
}

void TextFrame::GetExtents(double in_width, double *width, double *height) {
  impl_->SetUpFont();
  if (in_width >= impl_->width_) {
    *width = impl_->width_;
    *height = impl_->height_;
  } else {
    CanvasInterface *canvas = impl_->view_->GetGraphics()->NewCanvas(5, 5);
    canvas->GetTextExtents(impl_->text_.c_str(), impl_->font_, impl_->flags_,
                           in_width, width, height);
    canvas->Destroy();
  }
}

} // namespace ggadget