lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / writerfilter / source / dmapper / WrapPolygonHandler.cxx
blob862dae53e2e289f5ce51fce8aa68d89bc007d4a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/drawing/PointSequence.hpp>
21 #include <comphelper/sequence.hxx>
23 #include <ooxml/resourceids.hxx>
25 #include "ConversionHelper.hxx"
26 #include "WrapPolygonHandler.hxx"
27 #include "util.hxx"
29 #include <sal/log.hxx>
31 namespace writerfilter {
33 using namespace com::sun::star;
35 namespace dmapper {
37 WrapPolygon::WrapPolygon()
41 WrapPolygon::~WrapPolygon()
45 void WrapPolygon::addPoint(const awt::Point & rPoint)
47 mPoints.push_back(rPoint);
50 WrapPolygon::Points_t::iterator WrapPolygon::begin()
52 return mPoints.begin();
55 WrapPolygon::Points_t::iterator WrapPolygon::end()
57 return mPoints.end();
60 WrapPolygon::Pointer_t WrapPolygon::move(const awt::Point & rPoint)
62 WrapPolygon::Pointer_t pResult(new WrapPolygon);
64 Points_t::iterator aIt = begin();
65 Points_t::iterator aItEnd = end();
67 while (aIt != aItEnd)
69 awt::Point aPoint(aIt->X + rPoint.X, aIt->Y + rPoint.Y);
70 pResult->addPoint(aPoint);
71 ++aIt;
74 return pResult;
77 WrapPolygon::Pointer_t WrapPolygon::scale(const Fraction & rFractionX, const Fraction & rFractionY)
79 WrapPolygon::Pointer_t pResult(new WrapPolygon);
81 Points_t::iterator aIt = begin();
82 Points_t::iterator aItEnd = end();
84 while (aIt != aItEnd)
86 awt::Point aPoint((Fraction(long(aIt->X)) * rFractionX).operator long(), (Fraction(long(aIt->Y)) * rFractionY).operator long());
87 pResult->addPoint(aPoint);
88 ++aIt;
91 return pResult;
94 WrapPolygon::Pointer_t WrapPolygon::correctWordWrapPolygon(const awt::Size & rSrcSize)
96 WrapPolygon::Pointer_t pResult;
98 const long nWrap100Percent = 21600;
100 Fraction aMove(nWrap100Percent, rSrcSize.Width);
101 aMove = aMove * Fraction(15, 1);
102 awt::Point aMovePoint(aMove.operator long(), 0);
103 pResult = move(aMovePoint);
105 Fraction aScaleX = nWrap100Percent / (nWrap100Percent + aMove);
106 Fraction aScaleY = nWrap100Percent / (nWrap100Percent - aMove);
107 pResult = pResult->scale(aScaleX, aScaleY);
109 Fraction aScaleSrcX(rSrcSize.Width, nWrap100Percent);
110 Fraction aScaleSrcY(rSrcSize.Height, nWrap100Percent);
111 pResult = pResult->scale(aScaleSrcX, aScaleSrcY);
113 return pResult;
116 WrapPolygon::Pointer_t WrapPolygon::correctWordWrapPolygonPixel(const awt::Size & rSrcSize)
118 WrapPolygon::Pointer_t pResult;
121 * https://msdn.microsoft.com/en-us/library/ee342530.aspx
123 * Image wrapping polygons in Microsoft Word use a fixed coordinate space
124 * that is 21600 units x 21600 units. Coordinate (0,0) is the upper left
125 * corner of the image and coordinate (21600,21600) is the lower right
126 * corner of the image. Microsoft Word scales the size of the wrapping
127 * polygon units to fit the size of the image. The 21600 value is a legacy
128 * artifact from the drawing layer of early versions of Microsoft Office.
130 const long nWrap100Percent = 21600;
132 Fraction aScaleX(rSrcSize.Width, nWrap100Percent);
133 Fraction aScaleY(rSrcSize.Height, nWrap100Percent);
134 pResult = scale(aScaleX, aScaleY);
136 return pResult;
139 drawing::PointSequenceSequence WrapPolygon::getPointSequenceSequence() const
141 drawing::PointSequenceSequence aPolyPolygon(1);
142 drawing::PointSequence aPolygon = comphelper::containerToSequence(mPoints);
143 aPolyPolygon[0] = aPolygon;
144 return aPolyPolygon;
147 WrapPolygonHandler::WrapPolygonHandler()
148 : LoggedProperties("WrapPolygonHandler")
149 , mpPolygon(new WrapPolygon)
150 , mnX(0)
151 , mnY(0)
155 WrapPolygonHandler::~WrapPolygonHandler()
159 void WrapPolygonHandler::lcl_attribute(Id Name, Value & val)
161 sal_Int32 nIntValue = val.getInt();
163 switch(Name)
165 case NS_ooxml::LN_CT_Point2D_x:
166 mnX = nIntValue;
167 break;
168 case NS_ooxml::LN_CT_Point2D_y:
169 mnY = nIntValue;
170 break;
171 default:
172 SAL_WARN("writerfilter", "WrapPolygonHandler::lcl_attribute: unhandled token: " << Name);
173 break;
177 void WrapPolygonHandler::lcl_sprm(Sprm & _sprm)
179 switch (_sprm.getId())
181 case NS_ooxml::LN_CT_WrapPath_lineTo:
182 case NS_ooxml::LN_CT_WrapPath_start:
184 resolveSprmProps(*this, _sprm);
186 awt::Point aPoint(mnX, mnY);
187 mpPolygon->addPoint(aPoint);
189 break;
190 default:
191 SAL_WARN("writerfilter", "WrapPolygonHandler::lcl_sprm: unhandled token: " << _sprm.getId());
192 break;
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */