office-gobmx/svgio/inc/svgcharacternode.hxx
Mike Kaganski 6e6081b340 tdf#157103: fix SVG whitespace handling
Previous code tried to hack some tricks to restore whitespaces after
trimming them according to the xml:space attribute value. But it was
wrong in multiple ways.

1. The collapsed space must stay in the block where its start was.
   When a block ended with a space, then trimming the space from it,
   and adding to a next block, can change the size of the space.
2. The shift of a line (e.g., specifying x and y values) doesn't end
   the logical line. A space before such a shift must be kept by the
   same rules, as if there weren't a shift.
3. A block with xml:space="preserve" is treated as if it consists of
   all non-whitespace characters, even if its leading or trailing
   characters are spaces. I.e., a trailing space in a block before,
   or a leading space in a block after, should be collapsed into a
   single space, not removed - even when the space-preserving block
   itself is made invisible.

Change-Id: Ic778d1e9d6b9d0c342ea74ad78d44bb47bc8d708
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166239
Tested-by: Mike Kaganski <mike.kaganski@collabora.com>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
2024-04-20 12:16:08 +02:00

79 lines
2.9 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you 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 .
*/
#pragma once
#include <sal/config.h>
#include <rtl/ref.hxx>
#include <string_view>
#include <drawinglayer/attribute/fontattribute.hxx>
#include "svgtextnode.hxx"
#include "svgtextposition.hxx"
namespace drawinglayer::primitive2d { class TextSimplePortionPrimitive2D; }
namespace svgio::svgreader
{
class SvgCharacterNode final : public SvgNode
{
private:
/// the string data
OUString maText;
SvgTspanNode* mpParentLine;
bool mbHadTrailingSpace = false;
/// local helpers
rtl::Reference<drawinglayer::primitive2d::BasePrimitive2D> createSimpleTextPrimitive(
SvgTextPosition& rSvgTextPosition,
const SvgStyleAttributes& rSvgStyleAttributes) const;
void decomposeTextWithStyle(
drawinglayer::primitive2d::Primitive2DContainer& rTarget,
SvgTextPosition& rSvgTextPosition,
const SvgStyleAttributes& rSvgStyleAttributes) const;
public:
SvgCharacterNode(
SvgDocument& rDocument,
SvgNode* pParent,
OUString aText);
virtual ~SvgCharacterNode() override;
static drawinglayer::attribute::FontAttribute getFontAttribute(
const SvgStyleAttributes& rSvgStyleAttributes);
virtual const SvgStyleAttributes* getSvgStyleAttributes() const override;
void decomposeText(drawinglayer::primitive2d::Primitive2DContainer& rTarget, SvgTextPosition& rSvgTextPosition) const;
SvgCharacterNode* whiteSpaceHandling(SvgCharacterNode* pPreviousCharacterNode);
void concatenate(std::u16string_view rText);
/// Text content
const OUString& getText() const { return maText; }
void setParentLine(SvgTspanNode* pParentLine) { mpParentLine = pParentLine; }
};
} // end of namespace svgio::svgreader
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */