skia2/gm/internal_links.cpp
Mike Klein 33d2055e59 GM: some header cleanup
gm.h includes sk_tool_utils.h but does not use it.

The bulk of this CL makes each gm that uses sk_tool_utils include it.

sk_tool_utils.h also provided SkRandom and SkTDArray,
so a couple GMs add those headers too.

Change-Id: Ieb2a7c542f0ca89c3223f744fc11b0ff37af36c1
Reviewed-on: https://skia-review.googlesource.com/10014
Commit-Queue: Mike Klein <mtklein@chromium.org>
Reviewed-by: Herb Derby <herb@google.com>
2017-03-22 18:11:49 +00:00

79 lines
2.5 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "sk_tool_utils.h"
#include "SkAnnotation.h"
#include "SkData.h"
namespace skiagm {
/** Draws two rectangles. In output formats that support internal links (PDF),
* clicking the one labeled "Link to A" should take you to the one labeled
* "Target A". Note that you'll need to zoom your PDF viewer in a fair bit in
* order for the scrolling to not be blocked by the edge of the document.
*/
class InternalLinksGM : public GM {
public:
InternalLinksGM() {
this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
}
protected:
virtual SkString onShortName() {
return SkString("internal_links");
}
virtual SkISize onISize() {
return SkISize::Make(700, 500);
}
virtual void onDraw(SkCanvas* canvas) {
sk_sp<SkData> name(SkData::MakeWithCString("target-a"));
canvas->save();
canvas->translate(SkIntToScalar(100), SkIntToScalar(100));
drawLabeledRect(canvas, "Link to A", 0, 0);
SkRect rect = SkRect::MakeXYWH(0, 0, SkIntToScalar(50), SkIntToScalar(20));
SkAnnotateLinkToDestination(canvas, rect, name.get());
canvas->restore();
canvas->save();
canvas->translate(SkIntToScalar(200), SkIntToScalar(200));
SkPoint point = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(50));
drawLabeledRect(canvas, "Target A", point.x(), point.y());
SkAnnotateNamedDestination(canvas, point, name.get());
canvas->restore();
}
private:
/** Draw an arbitrary rectangle at a given location and label it with some
* text. */
void drawLabeledRect(SkCanvas* canvas, const char* text, SkScalar x, SkScalar y) {
SkPaint paint;
paint.setColor(SK_ColorBLUE);
SkRect rect = SkRect::MakeXYWH(x, y,
SkIntToScalar(50), SkIntToScalar(20));
canvas->drawRect(rect, paint);
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setTextSize(SkIntToScalar(25));
paint.setColor(SK_ColorBLACK);
canvas->drawText(text, strlen(text), x, y, paint);
}
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new InternalLinksGM; }
static GMRegistry reg(MyFactory);
}