2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2010-05-17 14:50:04 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
#include "SkCanvas.h"
|
2011-09-28 15:24:00 +00:00
|
|
|
#include "SkColorShader.h"
|
2010-05-17 14:50:04 +00:00
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkTextBox.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
2011-09-28 15:24:00 +00:00
|
|
|
extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
|
|
|
|
|
2012-08-16 17:35:58 +00:00
|
|
|
#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
|
2011-03-01 15:18:14 +00:00
|
|
|
extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
|
2011-03-02 14:34:11 +00:00
|
|
|
#endif
|
2011-03-01 15:18:14 +00:00
|
|
|
|
2011-03-02 14:34:11 +00:00
|
|
|
static const char gText[] =
|
2012-08-23 18:19:56 +00:00
|
|
|
"When in the Course of human events it becomes necessary for one people "
|
|
|
|
"to dissolve the political bands which have connected them with another "
|
|
|
|
"and to assume among the powers of the earth, the separate and equal "
|
|
|
|
"station to which the Laws of Nature and of Nature's God entitle them, "
|
|
|
|
"a decent respect to the opinions of mankind requires that they should "
|
|
|
|
"declare the causes which impel them to the separation.";
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2011-05-04 14:31:07 +00:00
|
|
|
class TextBoxView : public SampleView {
|
2011-03-02 14:34:11 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
TextBoxView() {
|
2012-08-16 17:35:58 +00:00
|
|
|
#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
|
2012-08-23 18:19:56 +00:00
|
|
|
LOGFONT lf;
|
|
|
|
sk_bzero(&lf, sizeof(lf));
|
|
|
|
lf.lfHeight = 9;
|
|
|
|
SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
|
|
|
|
lf.lfHeight = 12;
|
|
|
|
SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
|
|
|
|
// we assert that different sizes should not affect which face we get
|
|
|
|
SkASSERT(tf0 == tf1);
|
|
|
|
tf0->unref();
|
|
|
|
tf1->unref();
|
2011-03-02 14:34:11 +00:00
|
|
|
#endif
|
2011-09-28 15:24:00 +00:00
|
|
|
}
|
2011-03-02 14:34:11 +00:00
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
protected:
|
|
|
|
// overrides from SkEventSink
|
|
|
|
virtual bool onQuery(SkEvent* evt) {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
2012-09-30 11:30:01 +00:00
|
|
|
SampleCode::TitleR(evt, "TextBox");
|
2010-05-17 14:50:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2011-03-02 14:34:11 +00:00
|
|
|
|
2011-09-28 15:24:00 +00:00
|
|
|
void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
|
|
|
|
canvas->clipRect(SkRect::MakeWH(w, h));
|
|
|
|
canvas->drawColor(bg);
|
2012-08-23 18:19:56 +00:00
|
|
|
SkScalar margin = 20;
|
2010-05-17 14:50:04 +00:00
|
|
|
SkTextBox tbox;
|
2012-08-23 18:19:56 +00:00
|
|
|
tbox.setMode(SkTextBox::kLineBreak_Mode);
|
|
|
|
tbox.setBox(margin, margin,
|
|
|
|
w - margin, h - margin);
|
|
|
|
tbox.setSpacing(SkIntToScalar(3)/3, 0);
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2011-04-14 17:53:24 +00:00
|
|
|
paint.setLCDRenderText(true);
|
2011-09-28 15:24:00 +00:00
|
|
|
paint.setColor(fg);
|
2012-08-23 18:19:56 +00:00
|
|
|
tbox.setText(gText, strlen(gText), paint);
|
2010-05-17 14:50:04 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
for (int i = 9; i < 24; i += 2) {
|
|
|
|
paint.setTextSize(SkIntToScalar(i));
|
|
|
|
tbox.draw(canvas);
|
|
|
|
canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
|
|
|
|
}
|
2010-05-17 14:50:04 +00:00
|
|
|
}
|
2011-03-02 14:34:11 +00:00
|
|
|
|
2011-09-28 15:24:00 +00:00
|
|
|
virtual void onDrawContent(SkCanvas* canvas) {
|
|
|
|
SkScalar width = this->width() / 3;
|
|
|
|
drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
|
|
|
|
canvas->translate(width, 0);
|
|
|
|
drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
|
|
|
|
canvas->translate(width, 0);
|
|
|
|
drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
|
|
|
|
canvas->translate(0, this->height()/2);
|
|
|
|
drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
|
|
|
|
}
|
|
|
|
|
2010-05-17 14:50:04 +00:00
|
|
|
private:
|
2011-09-08 13:37:07 +00:00
|
|
|
typedef SampleView INHERITED;
|
2010-05-17 14:50:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new TextBoxView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|