SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [FNDD_CONVERT_RTF2TEXT]
-- =================================================================
-- Converts a RTF text to a regular text
-- 
-- Returns: NVARCHAR - text
-- =================================================================
-- Copyright (c) 2025 by Digital Data GmbH
--
-- Digital Data GmbH • Ludwig-Rinn-Strasse 16 • D-35452 Heuchelheim
-- Tel.: 0641/202360 • E-Mail: info-flow@digitaldata.works
-- =================================================================
-- Creation Date / Author:    26.09.2024 / HE,MK
-- Version Date / Editor:     25.03.2025 / HE,MK
-- Version Number:            1.2.0.0
-- =================================================================
-- History:
-- 26.09.2024 / HE,MK - First Version
-- 14.12.2024 / MK - code optimisation, new additional parameters
-- 25.03.2025 / HE,MK - Remove rtf format strings

CREATE OR ALTER FUNCTION [dbo].[FNDD_CONVERT_RTF2TEXT](
    @pRTF 					nvarchar(max),			-- Give the RTF text, you want to convert
	@pREMOVE_LINE_WRAP		BIT				= NULL,	-- Set to 1 to remove line wraps
	@pREMOVE_DOUBLE_BLANKS	BIT				= NULL		-- Set to 1 to remove unnecessary blanks
)
RETURNS nvarchar(max)
AS
BEGIN

	-- decalare new vars because of parameter sniffing
	DECLARE	@RTF					NVARCHAR(max)	= ISNULL(@pRTF,''),
			@REMOVE_LINE_WRAP		BIT				= ISNULL(@pREMOVE_LINE_WRAP,1),
			@REMOVE_DOUBLE_BLANKS	BIT				= ISNULL(@pREMOVE_DOUBLE_BLANKS,1);

	-- decalare runtime vars
    DECLARE @Pos1					int,
			@Pos2					int,
			@hex 					varchar(316);

    DECLARE @Stage table   ([Char] char(1), [Pos] int);
	
	IF (LEN(@RTF) > 1) BEGIN
	
		INSERT  @Stage ([Char], [Pos])
		
		SELECT SUBSTRING(@rtf, [Number], 1),
			   [Number]
		  FROM [master]..[spt_values]
		 WHERE ([Type] = 'p')
		   AND (SUBSTRING(@rtf, Number, 1) IN ('{', '}'));

		SELECT @Pos1 = MIN([Pos])
			 , @Pos2 = MAX([Pos])
		  FROM @Stage;

		DELETE
		  FROM @Stage
		 WHERE ([Pos] IN (@Pos1, @Pos2));

		WHILE (1 = 1) BEGIN
			SELECT TOP 1 @Pos1 = s1.[Pos]
				 , @Pos2 = s2.[Pos]
			  FROM @Stage s1
				INNER JOIN @Stage s2 ON s2.[Pos] > s1.[Pos]
			 WHERE (s1.[Char] = '{')
			   AND (s2.[Char] = '}')
			ORDER BY s2.[Pos] - s1.[Pos];

			IF @@ROWCOUNT = 0
				BREAK

			DELETE
			  FROM @Stage
			 WHERE ([Pos] IN (@Pos1, @Pos2));

			UPDATE @Stage
			   SET [Pos] = [Pos] - @Pos2 + @Pos1 - 1
			 WHERE ([Pos] > @Pos2);

			SET @rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, '');
		END;

		SET @rtf = REPLACE(@rtf, '\pard', '');
		SET @rtf = REPLACE(@rtf, '\par', '');
		SET @rtf = STUFF(@rtf, 1, CHARINDEX(' ', @rtf), '');

		WHILE (Right(@rtf, 1) IN (' ', CHAR(13), CHAR(10), '}')) BEGIN
			SELECT @rtf = SUBSTRING(@rtf, 1, (LEN(@rtf + 'x') - 2));
			IF LEN(@rtf) = 0 BREAK
		END;
		
		SET @Pos1 = CHARINDEX('\''', @rtf);

		WHILE (@Pos1 > 0) BEGIN
			IF (@Pos1 > 0) BEGIN
				SET @hex = '0x' + SUBSTRING(@rtf, @Pos1 + 2, 2);
				SET @rtf = REPLACE(@rtf, SUBSTRING(@rtf, @Pos1, 4),CHAR(CONVERT(int, CONVERT (binary(1), @hex,1))));
				SET @Pos1 = CHARINDEX('\''', @rtf);
			END;
		END;

		SET @rtf = @rtf + ' ';
		SET @Pos1 = PATINDEX('%\%[0123456789][\ ]%', @rtf);

		WHILE (@Pos1 > 0) BEGIN

			SET @Pos2 = CHARINDEX(' ', @rtf, @Pos1 + 1);

			IF (@Pos2 < @Pos1) BEGIN
				SET @Pos2 = CHARINDEX('\', @rtf, @Pos1 + 1);
			END;

			IF (@Pos2 < @Pos1) BEGIN
				SET @rtf = SUBSTRING(@rtf, 1, @Pos1 - 1);
				SET @Pos1 = 0;
			END; ELSE BEGIN
				SET @rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, '');
				SET @Pos1 = PATINDEX('%\%[0123456789][\ ]%', @rtf);
			END;
		END;

		IF (@REMOVE_LINE_WRAP = 1) BEGIN
			SET @rtf = REPLACE(@rtf,char(10),' ');
			SET @rtf = REPLACE(@rtf,char(13),' ');
		END;
		
		IF (@REMOVE_DOUBLE_BLANKS = 1) BEGIN
			WHILE (@rtf like '%  %') BEGIN
				IF (@rtf like '%  %') BEGIN
					SET @rtf = REPLACE(@rtf,'  ',' ');
				END;
			END;
		END;
		
		-- Anyway remove trailing spaces
		SET @rtf = LTRIM(RTRIM(@rtf));

		-- Replace rtf format strings
		SET @rtf = Replace(@rtf,'Riched20 10.0.17763} ','');

	END;

	RETURN @rtf;

END;