1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
7 base.require('base.range');
8 base.require('ui.event_target');
10 base.exportTo('ccfv', function() {
11 function QuadViewViewport(bbox,
13 opt_dontPadBbox, opt_devicePixelRatio) {
14 ui.EventTarget.call(this);
16 throw new Error('Cannot initialize a viewport with an empty bbox');
18 this.setWorldBBox(bbox, opt_dontPadBbox);
21 if (opt_devicePixelRatio)
22 devicePixelRatio = opt_devicePixelRatio;
24 devicePixelRatio = window.devicePixelRatio || 1;
30 if (devicePixelRatio > 1)
39 QuadViewViewport.prototype = {
41 __proto__: ui.EventTarget.prototype,
53 updateScale_: function() {
54 this.worldPixelsPerDevicePixel_ = this.scale_;
55 this.devicePixelsPerLayoutPixel_ = 1 / devicePixelRatio;
58 this.worldRect.width * this.worldPixelsPerDevicePixel_;
60 this.worldRect.height * this.worldPixelsPerDevicePixel_;
62 this.layoutWidth = this.deviceWidth * this.devicePixelsPerLayoutPixel_;
63 this.layoutHeight = this.deviceHeight * this.devicePixelsPerLayoutPixel_;
65 this.transformWorldToDevicePixels_ = mat2d.create();
66 this.transformDevicePixelsToWorld_ = mat2d.create();
67 this.updateTransform_();
70 setWorldBBox: function(bbox, opt_dontPadBbox) {
71 var world_rect = bbox.asRect();
73 if (opt_dontPadBbox) {
76 world_pad = Math.min(world_rect.width,
77 world_rect.height) * 0.10;
80 world_rect.enlarge(world_pad);
81 this.worldRect = world_rect;
83 this.updateTransform_();
87 updateTransform_: function() {
88 if (!this.transformWorldToDevicePixels_)
91 mat2d.identity(this.transformWorldToDevicePixels_);
92 mat2d.translateInplaceXY(
93 this.transformWorldToDevicePixels_,
94 -this.worldRect.left, -this.worldRect.top);
95 mat2d.scaleInplaceXY(this.transformWorldToDevicePixels_,
96 this.worldPixelsPerDevicePixel_,
97 this.worldPixelsPerDevicePixel_);
99 mat2d.invert(this.transformDevicePixelsToWorld_,
100 this.transformWorldToDevicePixels_);
103 layoutPixelsToWorldPixels2: function(v) {
104 var tmp = this.layoutPixelsToDevicePixels2(v);
105 return this.devicePixelsToWorldPixels2(tmp);
108 layoutPixelsToDevicePixels2: function(v) {
109 var res = vec2.create();
110 return vec2.scale(res, v, 1 / this.devicePixelsPerLayoutPixel_);
113 devicePixelsToWorldPixels2: function(v) {
114 var res = vec2.create();
115 vec2.transformMat2d(res, v, this.transformDevicePixelsToWorld_);
119 getWorldToDevicePixelTransform: function() {
120 return this.transformDevicePixelsToWorld_;
123 getDeviceLineWidthAssumingTransformIsApplied: function(
124 desired_device_line_width) {
125 return desired_device_line_width / this.worldPixelsPerDevicePixel_;
128 applyTransformToContext: function(ctx) {
129 var transform = this.transformWorldToDevicePixels_;
130 ctx.transform(transform[0], transform[1], transform[2],
131 transform[3], transform[4], transform[5]);
134 forceRedrawAll: function() {
138 didChange_: function() {
139 base.dispatchSimpleEvent(this, 'change', false, false);
144 QuadViewViewport: QuadViewViewport,